<div dir="auto"><div>I second this expectation. I've recently had a hard time ensuring laziness in my work and it's been comforting in rooting out failures to know that the standard libraries, at least, are concerned about it.<br><br><div class="gmail_quote"><div dir="ltr">On Tue, May 22, 2018, 16:57 Tikhon Jelvis <<a href="mailto:tikhon@jelv.is">tikhon@jelv.is</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">I think the extra laziness makes sense here—it matches the behavior of common functions like &&. My general expectation is that functions are as lazy as they can be and, in the case of operators with two arguments, that evaluation goes left-to-right. (Again like &&.)<div class="gmail_extra"><br><div class="gmail_quote">On Tue, May 22, 2018 at 4:37 PM, David Feuer <span dir="ltr"><<a href="mailto:david.feuer@gmail.com" target="_blank" rel="noreferrer">david.feuer@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I think extra laziness here would be a bit surprising.<br>
<div><div class="m_2575988770453419055h5"><br>
On Tue, May 22, 2018 at 5:57 PM, Donnacha Oisín Kidney<br>
<<a href="mailto:mail@doisinkidney.com" target="_blank" rel="noreferrer">mail@doisinkidney.com</a>> wrote:<br>
> The current semigroup instance for Maybe looks like this:<br>
><br>
> instance Semigroup a => Semigroup (Maybe a) where<br>
> Nothing <> b = b<br>
> a <> Nothing = a<br>
> Just a <> Just b = Just (a <> b)<br>
><br>
> However, it could be lazier:<br>
><br>
> instance Semigroup a => Semigroup (Maybe a) where<br>
> Nothing <> b = b<br>
> Just a <> b = Just (maybe a (a<>) b)<br>
><br>
> This causes different behaviour for Data.Semigroup.First and<br>
> Data.Monoid.First:<br>
><br>
> >>> Data.Monoid.getFirst . foldMap pure $ [1..]<br>
> Just 1<br>
> >>> fmap Data.Semigroup.getFirst . Data.Semigroup.getOption . foldMap<br>
> (pure.pure) $ [1..]<br>
> _|_<br>
><br>
> A different definition for `Option` gets back the old behaviour:<br>
><br>
> newtype LeftOption a = LeftOption { getLeftOption :: Maybe a }<br>
><br>
> instance Semigroup a => Semigroup (LeftOption a) where<br>
> LeftOption Nothing <> ys = ys<br>
> LeftOption (Just x) <> LeftOption ys = LeftOption (Just (maybe x (x<>)<br>
> ys))<br>
><br>
> instance Semigroup a => Monoid (LeftOption a) where<br>
> mempty = LeftOption Nothing<br>
> mappend = (<>)<br>
><br>
> >>> fmap Data.Semigroup.getFirst . getLeftOption . foldMap (LeftOption .<br>
> Just . Data.Semigroup.First) $ [1..]<br>
> Just 1<br>
><br>
> Is there any benefit to the extra strictness? Should this be changed?<br>
><br>
> Another consideration is that the definition could equivalently be<br>
> right-strict, to get the desired behaviour for Last, but I think the<br>
> left-strict definition probably follows the conventions more.<br>
><br>
> I originally posted this to reddit<br>
> (<a href="https://www.reddit.com/r/haskell/comments/8lbzan/semigroup_maybe_too_strict/" rel="noreferrer noreferrer" target="_blank">https://www.reddit.com/r/haskell/comments/8lbzan/semigroup_maybe_too_strict/</a>)<br>
> and was encouraged to post it here.<br>
><br>
</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>
><br>
_______________________________________________<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><br></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></div></div>