[GHC] #15521: Provide a strict version of sum

#15521: Provide a strict version of sum -------------------------------------+------------------------------------- Reporter: dnadales | Owner: (none) Type: feature | Status: new request | Priority: normal | Milestone: 8.6.1 Component: Prelude | Version: 8.4.3 Keywords: | Operating System: Unknown/Multiple Architecture: | Type of failure: None/Unknown Unknown/Multiple | Test Case: | Blocked By: Blocking: | Related Tickets: Differential Rev(s): | Wiki Page: -------------------------------------+------------------------------------- When adding huge list of numbers, a strict version of `sum` is preferred to avoid an unnecessary use of memory. A strict version of `sum` can be easily written using `foldl'` but it'd be nicer if a function such as: {{{#!hs sum' = foldl' (+) 0 }}} would be provided in the prelude already. Does this makes sense? -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/15521 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#15521: Provide a strict version of sum -------------------------------------+------------------------------------- Reporter: dnadales | Owner: (none) Type: feature request | Status: new Priority: normal | Milestone: 8.6.1 Component: Prelude | Version: 8.4.3 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Comment (by f-a): Is there any occourrence when using a lazy `sum` is more appropriate than using its stricter version? -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/15521#comment:1 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

Is there any occourrence when using a lazy `sum` is more appropriate
#15521: Provide a strict version of sum -------------------------------------+------------------------------------- Reporter: dnadales | Owner: (none) Type: feature request | Status: new Priority: normal | Milestone: 8.6.1 Component: Prelude | Version: 8.4.3 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Comment (by ulysses4ever): Replying to [comment:1 f-a]: than using its stricter version? One might go with a usual non-termination argument: if you have divergent computation inside your list AND you don't actually use the result of `sum`, then you get a different behaviour. Similar argument applies for “expensive” computation inside the list. Also, Haskell Report (the Haskell standard) says that `sum` should behave lazily (AFAIK). So, changing the behavior of `sum` seems a bit of extreme to me. I'd rather argue for adding `sum' / product'` to `Prelude`. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/15521#comment:2 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#15521: Provide a strict version of sum -------------------------------------+------------------------------------- Reporter: dnadales | Owner: (none) Type: feature request | Status: new Priority: normal | Milestone: 8.6.1 Component: Prelude | Version: 8.4.3 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Comment (by f-a): Thanks ulysses4ever Replying to [comment:2 ulysses4ever]:
Also, Haskell Report (the Haskell standard) says that `sum` should behave lazily (AFAIK). So, changing the behavior of `sum` seems a bit of extreme to me. I'd rather argue for adding `sum' / product'` to `Prelude`.
I thought the same, but: {{{ -- from page 192 of the "Haskell 2010 Language Report" sum :: Num a => [a] -> a The sum function computes the sum of a finite list of numbers. }}} Is mandatory laziness stated somewhere else? -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/15521#comment:3 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#15521: Provide a strict version of sum -------------------------------------+------------------------------------- Reporter: dnadales | Owner: (none) Type: feature request | Status: new Priority: normal | Milestone: 8.6.1 Component: Prelude | Version: 8.4.3 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Comment (by ulysses4ever): Maybe the HR argument is not that important, because the [http://hackage.haskell.org/package/base-4.11.1.0/docs/src/Data.Foldable.html... actual definition] is far from HR anyway. Just some code could brake if you change the semantics (see above). -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/15521#comment:4 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#15521: Provide a strict version of sum -------------------------------------+------------------------------------- Reporter: dnadales | Owner: (none) Type: feature request | Status: new Priority: normal | Milestone: 8.6.1 Component: Prelude | Version: 8.4.3 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Comment (by svenpanne): Be careful, you're mixing things up here, because there are actually 3 different `sum`s: * In `Prelude`: This must behave like the definition on p. 121 from the report mentioned above. * In `Data.List`: The report doesn't state anything regarding strictness, but following the principle of least surprise, this should better behave like `Prelude.sum`. Note that GHC 7.10 (i.e. `base` 4.8) generalized the signature a bit. * In `Data.Foldable`: Basically the same holds here as for the previous item. Note that I'm not saying that the current strictness behavior of `sum` is a brilliant idea, but it's basically far too late to change this. Doing such a change would definitely wreak havoc in the package ecosystem, because it is not even reflected by a change in the signature. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/15521#comment:5 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#15521: Provide a strict version of sum -------------------------------------+------------------------------------- Reporter: dnadales | Owner: (none) Type: feature request | Status: new Priority: normal | Milestone: 8.6.1 Component: Prelude | Version: 8.4.3 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Comment (by f-a): Yeah risky idea for very little gain. Sorry for having derailed this thread! -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/15521#comment:6 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#15521: Provide a strict version of sum -------------------------------------+------------------------------------- Reporter: dnadales | Owner: (none) Type: feature request | Status: new Priority: normal | Milestone: 8.6.1 Component: Prelude | Version: 8.4.3 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Comment (by sjakobi): Adding a strict `sum'` function would fit well with the planned addition of `foldMap'` to `Foldable`: Phab:D4924 I don't think it's necessary to add `sum'` as `Foldable` method though: Just adding it as function to `Data.Foldable` should be good enough. An alternative implementation would be {{{ sum' = getSum #. foldMap' Sum }}} but I don't currently see any advantages to this version. Furthermore, if we add `sum'`, we should also add `product'` for consistency. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/15521#comment:7 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#15521: Provide a strict version of sum -------------------------------------+------------------------------------- Reporter: dnadales | Owner: (none) Type: feature request | Status: new Priority: normal | Milestone: 8.6.1 Component: Prelude | Version: 8.4.3 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Changes (by bgamari): * cc: core-libraries-committee@… (added) Comment: This is really something that needs to be through the Core Libraries Committee. See the [[https://wiki.haskell.org/Library_submissions|Haskell Wiki]] for details on the committee's proposal process. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/15521#comment:8 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler
participants (1)
-
GHC