On Wed, Nov 18, 2009 at 7:43 AM, Ben Millwood <span dir="ltr">&lt;<a href="mailto:haskell@benmachine.co.uk">haskell@benmachine.co.uk</a>&gt;</span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
It looks quite neat to use the Maybe monoid here:<br>
<br>
&gt; import Data.Monoid<br>
&gt; searchList p = foldr (\x -&gt; if p x then mappend (Just [x]) else id) Nothing<br>
<br>
but it seems that the Maybe Monoid instance keeps this strict. I<br>
fiddled with this a bit, and came up with the following:<br>
<br>
&gt; instance (Monoid m) =&gt; Monoid (Maybe m) where<br>
&gt;  mempty = Nothing -- as usual<br>
&gt;  mappend (Just x) y = Just $ mappend x (fromMaybe mempty y)<br>
&gt;  mappend Nothing y = y<br></blockquote><div><br>The existing Monoid instance for &#39;Maybe a&#39; lifts what is logically a Semigroup into a Monoid by extending the domain of the operation with a unit (Nothing). Alas, This is annoyingly not the same behavior as the MonadPlus behavior for Maybe, unlike all of the other cases where MonadPlus and Monoid happen to exist in a manner in which they coincide, and since there is no Semigroup class, it lies and claims that it transforms Monoid m =&gt; Monoid (Maybe m).<br>
<br>Your version uses mempty from the underlying monoid, so it would break any code that relied on the existing &#39;lifted Semigroup&#39; interpretation of the Maybe Monoid, which safely operate lift Semigroups-that-claim-to-be-Monoids where mempty = undefined<br>
<br>-Edward Kmett<br></div></div>