Opened 13 months ago

Last modified 13 months ago

#15521 new feature request

Provide a strict version of sum

Reported by: dnadales Owned by:
Priority: normal Milestone: 8.6.1
Component: Prelude Version: 8.4.3
Keywords: Cc: core-libraries-committee@…
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:

Description

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:

sum' = foldl' (+) 0

would be provided in the prelude already.

Does this makes sense?

Change History (8)

comment:1 Changed 13 months ago by f-a

Is there any occourrence when using a lazy sum is more appropriate than using its stricter version?

comment:2 in reply to:  1 ; Changed 13 months ago by ulysses4ever

Replying to f-a:

Is there any occourrence when using a lazy sum is more appropriate 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.

comment:3 in reply to:  2 Changed 13 months ago by f-a

Thanks ulysses4ever

Replying to 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?

Edit: found it

-- p. 121
-- sum and product compute the sum or product of a finite list of numbers.
sum, product :: (Num a) => [a] -> a
sum = foldl (+) 0
product = foldl (*) 1
Last edited 13 months ago by f-a (previous) (diff)

comment:4 Changed 13 months ago by ulysses4ever

Maybe the HR argument is not that important, because the actual definition is far from HR anyway. Just some code could brake if you change the semantics (see above).

comment:5 Changed 13 months ago by svenpanne

Be careful, you're mixing things up here, because there are actually 3 different sums:

  • 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.

comment:6 Changed 13 months ago by f-a

Yeah risky idea for very little gain. Sorry for having derailed this thread!

comment:7 Changed 13 months ago 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.

comment:8 Changed 13 months ago by bgamari

Cc: core-libraries-committee@… added

This is really something that needs to be through the Core Libraries Committee. See the Haskell Wiki for details on the committee's proposal process.

Note: See TracTickets for help on using tickets.