[Haskell-cafe] List instance of Alternative: why (++)?

Damian Nadales damian.nadales at gmail.com
Sun May 28 07:24:47 UTC 2017


> 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

In this case `(xs >>= f) <|> (ys >>= f)` will also be empty as far as
I can see...

I'm also trying to make some sense out of this definition of
alternative for lists. For `Maybe` we also have a left biased
alternative, and despite this I find it quite useful...

> 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>
> 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
>> 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.


More information about the Haskell-Cafe mailing list