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

Chris Smith cdsmith at gmail.com
Sat May 6 06:36:07 UTC 2017

```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>
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?
>
> _______________________________________________