<html><head></head><body><div><div><div style="display: none; border: 0px; width: 0px; height: 0px; overflow: hidden; visibility: hidden;"><img src="https://r.superhuman.com/H6JIVSt0f7p_9Hq9EDxN8TJU814jNPk29Hh6flDILR11UjQGV_FEmd3XRsSe5h7_5d1M8swt1jQ3pyIbrHrPCwZFSfSj5zRPPpLK3H4B3N2EmBMLlbBxBNT1Dk0iy39SzTcwF5rjiu9-zJRjuJJ7u-XAXuA8_gTH_plmi2vIFgqQp-2HC0l_zV0.gif" alt=" " width="1" height="0" style="display: none; border: 0px; width: 0px; height: 0px; overflow: hidden; visibility: hidden;"/><!-- --></div><div><div><div>I can't think of an example where a raw lazy `sum` would be preferable to a strict version. If summing over infinite data structures, it will always be prefixed by a `take` or a `drop` prior to summing to make sure we work with finite chunks. In which case, a strict sum is still better. Maybe someone has a more complex, contrived example showing the usefulness of lazy sums. <br/></div><div><br/></div><div>The fact that no one seems to be able to name one in this or other discussions, or come up with a contrived example, is a good indicator to me that at least the default should be strict in this case. We should offer a lazy variant as a second option, not the first.<br/></div><div><br/></div><div>I'm in favor of Oleg's `foldMap` implementation. It's really clean. Also thank you Hécate!<br/></div><div><br/></div><div>Cheers,<br/></div><div>Emily</div></div><br/><div class="gmail_signature"></div></div><br/><div><div class="gmail_quote">On Tue, Oct 20, 2020 at 12:40 PM, Sylvain Henry <span dir="ltr"><<a href="mailto:sylvain@haskus.fr" target="_blank">sylvain@haskus.fr</a>></span> wrote:<br/><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="gmail_extra"><div class="gmail_quote sh-color-black sh-color" style="null" id="null"><p class="sh-color-black sh-color">Do we have an actual example of the use of a lazy sum in the
wild?</p>
<p class="sh-color-black sh-color">If the most common case is the strict one, making `sum` strict
would be a better default. If need be we could also provide
`lazySum` or something, but is there really a need?</p>
<p class="sh-color-black sh-color">Sylvain<br/>
</p>
<p class="sh-color-black sh-color"><br/>
</p>
<div class="moz-cite-prefix sh-color-black sh-color">On 18/10/2020 22:16, Oleg Grenrus
wrote:<br/>
</div>
<blockquote cite="mid:2fecff7d-1a18-5c94-5e90-27c80c7fae14@iki.fi" type="cite" class="sh-color-black sh-color">
<p class="sh-color-black sh-color">Sorry I wasn't clear myself. Option C is "add sum'"<br/>
<br/>
For lists:<br/>
<br/>
sum = foldr (+) 0<br/>
sum' = foldl' (+) 0<br/>
<br/>
For Foldable<br/>
<br/>
sum = getSum #. foldMap Sum<br/>
sum' = getSum #. foldMap' Sum</p>
<p class="sh-color-black sh-color">- Oleg<br/>
</p>
<div class="moz-cite-prefix sh-color-black sh-color">On 18.10.2020 23.11, Hécate wrote:<br/>
</div>
<blockquote cite="mid:77399542-9ab4-381a-a8ce-3ddfe518fb9a@glitchbra.in" type="cite" class="sh-color-black sh-color">
<p class="sh-color-black sh-color">Indeed, and I initially went to suggest a `foldMap'`-based
implementation to keep with the current implementation of many
Foldable functions that are based on `foldMap` rather than a
raw `fold`.<br/>
</p>
<div class="moz-cite-prefix sh-color-black sh-color">On 18/10/2020 22:04, Oleg Grenrus
wrote:<br/>
</div>
<blockquote cite="mid:035231ea-7187-f096-d5bc-29ffe752d41d@iki.fi" type="cite" class="sh-color-black sh-color"> For
the sake of bigger audience I didn't bother mentioning #.
which is a coercion helper. It's essentially better (.) when
the first argument is newtype constructor (i.e. coerce).<br/>
<br/>
So with Option A (strict):<br/>
<br/>
sum = getSum #. foldMap' Sum<br/>
<br/>
Or Option B (lazy)<br/>
<br/>
sum = getSum #. foldMap Sum<br/>
<br/>
---<br/>
<br/>
There is also third option, Option C:<br/>
<br/>
sum = foldr (+) 0<br/>
sum' = foldl' (+) 0<br/>
<br/>
I don't think this is worthwhile, but it is an option.<br/>
(to rehash, I don't consider maintaining status quo to be an
option at all).<br/>
<br/>
- Oleg
<div class="moz-cite-prefix sh-color-black sh-color">On 18.10.2020 22.54, Vanessa
McHale wrote:<br/>
</div>
<blockquote cite="mid:ace346e5-c6d1-0cb3-7262-111d15b10b09@gmail.com" type="cite" class="sh-color-black sh-color">
<p class="sh-color-black sh-color">It's <br/>
</p>
<p class="sh-color-black sh-color">sum = getSum #. foldMap Sum<br/>
<br/>
in base.</p>
<div class="moz-cite-prefix sh-color-black sh-color">On 10/18/20 2:49 PM, Oleg
Grenrus wrote:<br/>
</div>
<blockquote cite="mid:cbe97c20-f365-a72e-f6e0-b00c5280f0a2@iki.fi" type="cite" class="sh-color-black sh-color">
<p class="sh-color-black sh-color">The problem is the current definition of sum for lists
which uses foldl, i.e non-strict left fold</p>
<p class="sh-color-black sh-color"> sum = foldl (+) 0<br/>
<br/>
It's utterly broken. Either we should change it to
foldl' to work on some types where addition is strict,
Option A:<br/>
<br/>
sum = foldl' (+) 0<br/>
<br/>
or alternatively (to make people using lazy accumulator
types), Option B:<br/>
<br/>
sum = foldr (+) 0<br/>
<br/>
The current state is no good for anyone or anything.<br/>
<br/>
---<br/>
<br/>
Related issue which Hecate didn't clearly mention, is
that Foldable class default implementation has<br/>
<br/>
class Foldable f where<br/>
...<br/>
sum = getSum . foldMap Sum -- this is "good" lazy
definition<br/>
<br/>
If we select option A, then I argue that for consistency
the default `Foldable.sum` should be<br/>
<br/>
sum = getSum . foldMap' Sum -- strict foldMap'<br/>
<br/>
If we select option B, Foldable definition doesn't need
to be changed.<br/>
<br/>
---<br/>
<br/>
I repeat, using non-strict left fold, foldl, for sum and
product is not good for anything.<br/>
Either foldr or foldl'.<br/>
<br/>
I have no strong preference. Current state is
unacceptable.<br/>
<br/>
- Oleg<br/>
</p>
<div class="moz-cite-prefix sh-color-black sh-color">On 18.10.2020 22.24, Henning
Thielemann wrote:<br/>
</div>
<blockquote cite="mid:alpine.DEB.2.20.2010182121500.5252@sputnik" type="cite" class="sh-color-black sh-color"> <br/>
On Sun, 18 Oct 2020, Hécate wrote: <br/>
<br/>
<blockquote type="cite" class="sh-color-black sh-color">In conclusion, leaving things to
the optimiser that could be trivially made fast every
time seems needlessly risky. <br/>
</blockquote>
<br/>
`seq` is still a hack. A strict 'sum' and 'product'
would still fail on a lazy accumulator type, say a lazy
pair type. If at all, sum and product should be
deepseq-strict. So currently, letting the optimiser make
a lazy sum strict is still the smaller hack.<br/>
<br/>
<fieldset class="mimeAttachmentHeader sh-color-black sh-color"></fieldset>
<pre wrap="" class="moz-quote-pre sh-color-black sh-color">_______________________________________________
Libraries mailing list
<a href="mailto:Libraries@haskell.org" class="moz-txt-link-abbreviated" target="_blank" rel="noopener noreferrer">Libraries@<wbr/>haskell.<wbr/>org</a>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries" class="moz-txt-link-freetext" target="_blank" rel="noopener noreferrer">http:/<wbr/>/<wbr/>mail.<wbr/>haskell.<wbr/>org/<wbr/>cgi-bin/<wbr/>mailman/<wbr/>listinfo/<wbr/>libraries</a>
</pre>
</blockquote>
<br/>
<fieldset class="mimeAttachmentHeader sh-color-black sh-color"></fieldset>
<pre wrap="" class="moz-quote-pre sh-color-black sh-color">_______________________________________________
Libraries mailing list
<a href="mailto:Libraries@haskell.org" class="moz-txt-link-abbreviated" target="_blank" rel="noopener noreferrer">Libraries@<wbr/>haskell.<wbr/>org</a>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries" class="moz-txt-link-freetext" target="_blank" rel="noopener noreferrer">http:/<wbr/>/<wbr/>mail.<wbr/>haskell.<wbr/>org/<wbr/>cgi-bin/<wbr/>mailman/<wbr/>listinfo/<wbr/>libraries</a>
</pre>
</blockquote>
<br/>
<fieldset class="mimeAttachmentHeader sh-color-black sh-color"></fieldset>
<pre wrap="" class="moz-quote-pre sh-color-black sh-color">_______________________________________________
Libraries mailing list
<a href="mailto:Libraries@haskell.org" class="moz-txt-link-abbreviated" target="_blank" rel="noopener noreferrer">Libraries@<wbr/>haskell.<wbr/>org</a>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries" class="moz-txt-link-freetext" target="_blank" rel="noopener noreferrer">http:/<wbr/>/<wbr/>mail.<wbr/>haskell.<wbr/>org/<wbr/>cgi-bin/<wbr/>mailman/<wbr/>listinfo/<wbr/>libraries</a>
</pre>
</blockquote>
<br/>
<fieldset class="mimeAttachmentHeader sh-color-black sh-color"></fieldset>
<pre wrap="" class="moz-quote-pre sh-color-black sh-color">_______________________________________________
Libraries mailing list
<a href="mailto:Libraries@haskell.org" class="moz-txt-link-abbreviated" target="_blank" rel="noopener noreferrer">Libraries@<wbr/>haskell.<wbr/>org</a>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries" class="moz-txt-link-freetext" target="_blank" rel="noopener noreferrer">http:/<wbr/>/<wbr/>mail.<wbr/>haskell.<wbr/>org/<wbr/>cgi-bin/<wbr/>mailman/<wbr/>listinfo/<wbr/>libraries</a>
</pre>
</blockquote>
<pre cols="72" class="moz-signature sh-color-black sh-color">--
Hécate <img src="https://emojis.superhuman.com/2728.png" alt="✨" title="✨" style="height: 15px !important; width: 15px !important; vertical-align: text-bottom !important;" height="15" width="15"/>
IRC: Uniaika
WWW: <a href="https://glitchbra.in" class="moz-txt-link-freetext" target="_blank" rel="noopener noreferrer">https:/<wbr/>/<wbr/>glitchbra.<wbr/>in</a>
RUN: BSD</pre>
<br/>
<fieldset class="mimeAttachmentHeader sh-color-black sh-color"></fieldset>
<pre wrap="" class="moz-quote-pre sh-color-black sh-color">_______________________________________________
Libraries mailing list
<a href="mailto:Libraries@haskell.org" class="moz-txt-link-abbreviated" target="_blank" rel="noopener noreferrer">Libraries@<wbr/>haskell.<wbr/>org</a>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries" class="moz-txt-link-freetext" target="_blank" rel="noopener noreferrer">http:/<wbr/>/<wbr/>mail.<wbr/>haskell.<wbr/>org/<wbr/>cgi-bin/<wbr/>mailman/<wbr/>listinfo/<wbr/>libraries</a>
</pre>
</blockquote>
<br/>
<fieldset class="mimeAttachmentHeader sh-color-black sh-color"></fieldset>
<pre wrap="" class="moz-quote-pre sh-color-black sh-color">_______________________________________________
Libraries mailing list
<a href="mailto:Libraries@haskell.org" class="moz-txt-link-abbreviated" target="_blank" rel="noopener noreferrer">Libraries@<wbr/>haskell.<wbr/>org</a>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries" class="moz-txt-link-freetext" target="_blank" rel="noopener noreferrer">http:/<wbr/>/<wbr/>mail.<wbr/>haskell.<wbr/>org/<wbr/>cgi-bin/<wbr/>mailman/<wbr/>listinfo/<wbr/>libraries</a>
</pre>
</blockquote>
<p class="sh-color-black sh-color">_______________________________________________
<br/>
Libraries mailing list
<br/>
<a target="_blank" rel="noopener noreferrer" href="mailto:Libraries@haskell.org">Libraries@<wbr/>haskell.<wbr/>org</a>
<br/>
<a target="_blank" rel="noopener noreferrer" href="http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries">http:/<wbr/>/<wbr/>mail.<wbr/>haskell.<wbr/>org/<wbr/>cgi-bin/<wbr/>mailman/<wbr/>listinfo/<wbr/>libraries</a></p></div></div></blockquote></div></div><br/></div></div></body></html>