<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<div class="moz-cite-prefix">The documentation for Alternative
describes it as "a monoid over an applicative functor". That makes
for the following documented laws for Alternative:<br>
<pre>x <|> (y <|> z) = (x <|> y) <|> z
empty <|> x = x = x <|> empty
</pre>
This makes your <tt>coalesce</tt> a valid <tt>(<|>)</tt>
if you take <tt>empty = []</tt>, but as Chris points out it's not
as intuitive from the perspective of certain other nice-to-have
properties.<br>
<br>
On 5/6/2017 2:36 AM, Chris Smith wrote:<br>
</div>
<blockquote
cite="mid:CAPq5PvJc11kKdg8DQsiB+WqP7YYfVXKTA1iFxvqBMwY3hBUgsQ@mail.gmail.com"
type="cite">
<div dir="ltr">The usual intuition behind the list
Functor/Applicative/Monad instances are that they represents
non-deterministic values, which can have any of some list of
possible values. In this case, the natural interpretation of
<|> is as a non-deterministic choice of two possible
computations. So the list of possible results would include
anything from either computation. Your implementation, on the
other hand, would represent a left-biased choice, where the
right alternative is only used if the left is impossible.
<div><br>
</div>
<div>It's hard to look at laws, because there's apparently
little agreement on the proper laws for Alternative. It looks
possible that as an Applicative and Alternative, this would be
fine; but the Alternative instance you propose would work in
odd ways with the Monad instance. That is, if f x == [] for
any x in (non-empty) xs, then something like (xs <|> ys)
>>= f would yield an empty list, while (xs >>= f)
<|> (ys >>= f) would not. But, this isn't a law
or anything, you could chalk it up as counter-intuitive, but
not disqualifying.</div>
</div>
<div class="gmail_extra"><br>
<div class="gmail_quote">On Fri, May 5, 2017 at 11:12 PM,
Theodore Lief Gannon <span dir="ltr"><<a
moz-do-not-send="true" href="mailto:tanuki@gmail.com"
target="_blank">tanuki@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">Fiddling around, I found myself wanting:
<div><br>
</div>
<div><font face="monospace, monospace">coalesce :: [a]
-> [a] -> [a]</font></div>
<div><font face="monospace, monospace">-- or -- ::
(Foldable t) => t a -> t a -> t a</font></div>
<div><font face="monospace, monospace">coalesce a b = if
null a then b else a</font></div>
<div><br>
</div>
<div>I expected this to be <font face="monospace,
monospace">(<|>)</font><font face="arial,
helvetica, sans-serif"> (it is for </font><font
face="monospace, monospace">Maybe</font><font
face="arial, helvetica, sans-serif">!) </font>but
instead I find no canonical implementation of it
anywhere, and what seems like a useless <font
face="monospace, monospace">instance Alternative []</font>.
What's the rationale?</div>
</div>
<br>
______________________________<wbr>_________________<br>
Haskell-Cafe mailing list<br>
To (un)subscribe, modify options or view archives go to:<br>
<a moz-do-not-send="true"
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>
<br>
<fieldset class="mimeAttachmentHeader"></fieldset>
<br>
<pre wrap="">_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
<a class="moz-txt-link-freetext" href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe">http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe</a>
Only members subscribed via the mailman list are allowed to post.</pre>
</blockquote>
<p><br>
</p>
</body>
</html>