<div dir="auto">I'm pretty strongly in favour of a sum implemented using foldMap'. The status quo is atrocious (cf. Oleg's emails), and the utility of a lazy right fold is low in practise.</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Oct 21, 2020, 14:38 Emily Pillmore <<a href="mailto:emilypi@cohomolo.gy">emilypi@cohomolo.gy</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div><div><div style="display:none;border:0px;width:0px;height:0px;overflow: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"></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></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" rel="noreferrer">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" id="m_1061459394644272651null"><p>Do we have an actual example of the use of a lazy sum in the
      wild?</p>
    <p>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>Sylvain<br>
    </p>
    <p><br>
    </p>
    <div>On 18/10/2020 22:16, Oleg Grenrus
      wrote:<br>
    </div>
    <blockquote type="cite">
      
      <p>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>- Oleg<br>
      </p>
      <div>On 18.10.2020 23.11, Hécate wrote:<br>
      </div>
      <blockquote type="cite">
        <p>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>On 18/10/2020 22:04, Oleg Grenrus
          wrote:<br>
        </div>
        <blockquote type="cite"> 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>On 18.10.2020 22.54, Vanessa
            McHale wrote:<br>
          </div>
          <blockquote type="cite">
            <p>It's <br>
            </p>
            <p>sum = getSum #. foldMap Sum<br>
              <br>
              in base.</p>
            <div>On 10/18/20 2:49 PM, Oleg
              Grenrus wrote:<br>
            </div>
            <blockquote type="cite">
              <p>The problem is the current definition of sum for lists
                which uses foldl, i.e non-strict left fold</p>
              <p>    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>On 18.10.2020 22.24, Henning
                Thielemann wrote:<br>
              </div>
              <blockquote type="cite"> <br>
                On Sun, 18 Oct 2020, Hécate wrote: <br>
                <br>
                <blockquote type="cite">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></fieldset>
                <pre>_______________________________________________
Libraries mailing list
<a href="mailto:Libraries@haskell.org" rel="noopener noreferrer noreferrer" target="_blank">Libraries@haskell.org</a>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries" rel="noopener noreferrer noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries</a>
</pre>
              </blockquote>
              <br>
              <fieldset></fieldset>
              <pre>_______________________________________________
Libraries mailing list
<a href="mailto:Libraries@haskell.org" rel="noopener noreferrer noreferrer" target="_blank">Libraries@haskell.org</a>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries" rel="noopener noreferrer noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries</a>
</pre>
            </blockquote>
            <br>
            <fieldset></fieldset>
            <pre>_______________________________________________
Libraries mailing list
<a href="mailto:Libraries@haskell.org" rel="noopener noreferrer noreferrer" target="_blank">Libraries@haskell.org</a>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries" rel="noopener noreferrer noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries</a>
</pre>
          </blockquote>
          <br>
          <fieldset></fieldset>
          <pre>_______________________________________________
Libraries mailing list
<a href="mailto:Libraries@haskell.org" rel="noopener noreferrer noreferrer" target="_blank">Libraries@haskell.org</a>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries" rel="noopener noreferrer noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries</a>
</pre>
        </blockquote>
        <pre cols="72">-- 
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" rel="noopener noreferrer noreferrer" target="_blank">https://glitchbra.in</a>
RUN: BSD</pre>
        <br>
        <fieldset></fieldset>
        <pre>_______________________________________________
Libraries mailing list
<a href="mailto:Libraries@haskell.org" rel="noopener noreferrer noreferrer" target="_blank">Libraries@haskell.org</a>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries" rel="noopener noreferrer noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries</a>
</pre>
      </blockquote>
      <br>
      <fieldset></fieldset>
      <pre>_______________________________________________
Libraries mailing list
<a href="mailto:Libraries@haskell.org" rel="noopener noreferrer noreferrer" target="_blank">Libraries@haskell.org</a>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries" rel="noopener noreferrer noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries</a>
</pre>
    </blockquote>
  


<p>_______________________________________________
<br>
Libraries mailing list
<br>
<a rel="noopener noreferrer noreferrer" href="mailto:Libraries@haskell.org" target="_blank">Libraries@haskell.org</a>
<br>
<a rel="noopener noreferrer noreferrer" href="http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries</a></p></div></div></blockquote></div></div><br></div></div></div>_______________________________________________<br>
Libraries mailing list<br>
<a href="mailto:Libraries@haskell.org" target="_blank" rel="noreferrer">Libraries@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries" rel="noreferrer noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries</a><br>
</blockquote></div>