[Haskell-cafe] List instance of Alternative: why (++)?
Joe Quinn
headprogrammingczar at gmail.com
Sat May 6 13:02:15 UTC 2017
The documentation for Alternative describes it as "a monoid over an
applicative functor". That makes for the following documented laws for
Alternative:
x <|> (y <|> z) = (x <|> y) <|> z
empty <|> x = x = x <|> empty
This makes your coalesce a valid (<|>) if you take empty = [], but as
Chris points out it's not as intuitive from the perspective of certain
other nice-to-have properties.
On 5/6/2017 2:36 AM, Chris Smith wrote:
> 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.
>
> 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.
>
> On Fri, May 5, 2017 at 11:12 PM, Theodore Lief Gannon
> <tanuki at gmail.com <mailto:tanuki at gmail.com>> wrote:
>
> Fiddling around, I found myself wanting:
>
> coalesce :: [a] -> [a] -> [a]
> -- or -- :: (Foldable t) => t a -> t a -> t a
> coalesce a b = if null a then b else a
>
> I expected this to be (<|>)(it is for Maybe!) but instead I find
> no canonical implementation of it anywhere, and what seems like a
> useless instance Alternative []. What's the rationale?
>
> _______________________________________________
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> <http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe>
> Only members subscribed via the mailman list are allowed to post.
>
>
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> Only members subscribed via the mailman list are allowed to post.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20170506/dc4486e0/attachment.html>
More information about the Haskell-Cafe
mailing list