<div dir="ltr">Where <font face="monospace, monospace">mappend</font>, <font face="monospace, monospace">mplus</font> and <font face="monospace, monospace"><|></font> and <font face="monospace, monospace"><></font> are defined, do they (and should they) always produce the same results?</div><div class="gmail_extra"><br><div class="gmail_quote">On Sun, May 7, 2017 at 5:23 PM, Jon Purdy <span dir="ltr"><<a href="mailto:evincarofautumn@gmail.com" target="_blank">evincarofautumn@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">D’oh, that’s what I get for writing untested code in an email.<div><br></div><div>Neutral doesn’t seem necessary since we have “null” in Foldable. I was thinking more along these lines:</div><div><br></div><div>instance (Alternative f, Foldable f) => Alternative (LeftBiased f) where</div><div> empty = LeftBiased empty</div><div> LeftBiased a <|> LeftBiased b = LeftBiased (if null a then b else a)<br><div class="gmail_extra"><br></div><div class="gmail_extra">Under the assumption that “null empty” always holds. I think using the Alternative constraint for just “empty” makes sense because LeftBiased and RightBiased should only differ from the wrapped type in the implementation of (<|>), but it still seems a little iffy somehow.</div><div class="gmail_extra"><br></div><div class="gmail_extra">To make the wrapping slightly less painful, another good bikeshed colour would be Pre/Post. (Dunno what you’d call “Unbiased” in that case, though.)</div><div class="gmail_extra"><br></div><div class="gmail_extra">On a related note, I recall there was some discussion a while back about making a Monoid instance for Map where mappend is “unionWith mappend” instead of the left-biased “union”. These wrappers could also be used for that sort of thing, and it’d be nice to have a single standard for them with all the different use cases fleshed out.</div><div class="gmail_extra"><br><div class="gmail_quote"><div><div class="h5">On Sat, May 6, 2017 at 4:19 PM, MarLinn <span dir="ltr"><<a href="mailto:monkleyon@gmail.com" target="_blank">monkleyon@gmail.com</a>></span> wrote:<br></div></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div><div class="h5">
<div bgcolor="#FFFFFF"><span class="m_-6910634604553860747m_6479097449220985488gmail-">
<br>
<div class="m_-6910634604553860747m_6479097449220985488gmail-m_442407239938230924moz-cite-prefix">On 2017-05-07 00:23, Jon Purdy wrote:<br>
</div>
<blockquote type="cite">
<div dir="ltr">I’ve wanted this before as well. Maybe we should
throw a newtype at it?
<div><br>
</div>
<div>newtype LeftBiased a = LeftBiased [a]</div>
<div>instance Alternative (LeftBiased a) where</div>
<div> empty = []</div>
<div> [] <|> b = b</div>
<div> a <|> _ = a</div>
<div><br>
</div>
<div>
<div>newtype RightBiased a = RightBiased [a]</div>
<div>instance Alternative (RightBiased a) where</div>
<div> empty = []</div>
<div> a <|> [] = a</div>
<div> _ <|> b = b</div>
</div>
<div><br>
</div>
</div>
</blockquote>
</span><p>You forgot the fun wrapping and unwrapping. But no matter. Let's
generalize!</p>
<pre> class Neutral a where
neutral :: a
isNeutral :: a -> Bool</pre>
<div>
<pre> instance Neutral a => Alternative (LeftBiased a) where
empty = LeftBiased neutral
(LeftBiased a) <|> (LeftBiased b) = LeftBiased $ if isNeutral a then b else a</pre>
</div>
<div>
<pre> instance Neutral a => Alternative (RightBiased a) where
empty = RightBiased neutral
(RightBiased a) <|> (RightBiased b) = RightBiased $ if isNeutral b then a else b</pre>
</div>
<p>Why?</p>
<pre> type AllRight e a = LeftBiased (Either e a)
type AnyRight e a = RightBiased (Either e a)
instance Neutral a => Neutral (AllRight e a) where
neutral = Right $ LeftBiased neutral
isNeutral = fmap isRight
instance Neutral e => Neutral (AnyRight e a) where
neutral = Left $ RightBiased neutral
isNeutral = fmap isLeft</pre>
<p>Is this a bit silly? Yes. My actual goal is to show that these
concepts are bigger than they might appear, and how painful all
those wrappers are. This is to advertise my language extension
from my separate thread. And also because it's silly fun. Mostly
that.</p><span class="m_-6910634604553860747m_6479097449220985488gmail-">
<p><br>
</p>
<blockquote type="cite">
<div dir="ltr">
<div>newtype Unbiased a = Unbiased (Maybe a)</div>
<div>instance (Monoid m) => Alternative (Unbiased m) where</div>
<div> empty = Nothing</div>
<div> Just a <|> Just b = Just (a <> b)</div>
<div> _ <|> Just b = Just b</div>
<div> Just a <|> _ = Just a</div>
<div> _ <|> _ = Nothing</div>
</div>
</blockquote>
</span><p>Mh, that's just <tt>liftA2 (<>) a b <|> a <|>
b</tt> in terms of the regular instance. Now that is easy to
generalize – just don't use it for lists.<br>
</p>
Cheers,<br>
MarLinn<br>
</div>
<br></div></div><span class="">______________________________<wbr>_________________<br>
Haskell-Cafe mailing list<br>
To (un)subscribe, modify options or view archives go to:<br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bi<wbr>n/mailman/listinfo/haskell-caf<wbr>e</a><br>
Only members subscribed via the mailman list are allowed to post.<br></span></blockquote></div><br></div></div></div>
<br>______________________________<wbr>_________________<br>
Haskell-Cafe mailing list<br>
To (un)subscribe, modify options or view archives go to:<br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-<wbr>bin/mailman/listinfo/haskell-<wbr>cafe</a><br>
Only members subscribed via the mailman list are allowed to post.<br></blockquote></div><br></div>