Why are `sum` and `product` defined via foldMap' and not foldl'?

Merijn Verstraaten merijn at inconsistent.nl
Wed Dec 23 13:08:51 UTC 2020


Also, note that the benchmark from the original email:
1) uses ghci, rendering it meaningless as it's not remotely representative of performance of compiled code
2) uses the list instance of Foldable which does *not* use the defaults shown in the original email, but uses explicit definitions.

The foldMap' code shown (but not used!) *is* strict, so would, presumably perform comparably to foldl'. Unlike the foldl version of sum that Foldable for lists currently uses.

Cheers,
Merijn

> On 23 Dec 2020, at 13:55, Oleg Grenrus <oleg.grenrus at iki.fi> wrote:
> 
> I stand corrected.
> https://gitlab.haskell.org/ghc/ghc/-/merge_requests/4355 is merged
> 
> and seems I misread the original e-mail.
> 
> Foldable should prefer foldMap' (or foldMap) because it offers most
> opportunities (Monoid mappend is associative). Trees, SnocList work
> better by default.
> 
> For lists the implementation could use foldl', because in that special
> case it makes more sense.
> 
> These methods (sum, product) are part of Foldable class specifically so
> they can be overriden.
> 
> - Oleg
> 
> On 23.12.2020 12.44, Oleg Grenrus wrote:
>> Because the related change proposal(s) are never accepted nor implemented.
>> 
>> Most recent one is quite recent though.
>> https://mail.haskell.org/pipermail/libraries/2020-October/030862.html
>> 
>> - Oleg
>> 
>> On 23.12.2020 8.32, Viktor Dukhovni wrote:
>>> Is there a compelling reason for:
>>> 
>>>    sum = getSum #. foldMap' Sum
>>>    product = getProduct #. foldMap' Product
>>> 
>>> rather than:
>>> 
>>>    sum = foldl' (+) 0
>>>    product = foldl' (*) 1
>>> 
>>> A quick ghci session with today's GHC head yields:
>>> 
>>>    λ> import qualified Data.Foldable as F
>>>    λ> :set +s
>>> 
>>>    λ> F.sum [0..10000000]
>>>    50000005000000
>>>    (2.98 secs, 1,612,368,368 bytes)
>>> 
>>>    λ> F.foldl' (+) 0 [0..10000000]
>>>    50000005000000
>>>    (0.28 secs, 880,065,112 bytes)
>>> 
>>> The `foldl'` variant looks substantially more efficient (at least for
>>> lists), is there some important context in which `foldMap'` is
>>> preferable?
>>> 
>> _______________________________________________
>> Libraries mailing list
>> Libraries at haskell.org
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
> _______________________________________________
> Libraries mailing list
> Libraries at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: Message signed with OpenPGP
URL: <http://mail.haskell.org/pipermail/libraries/attachments/20201223/942b703c/attachment.sig>


More information about the Libraries mailing list