[Haskell-cafe] MonadList?

Michael Shulman viritrilbia at gmail.com
Wed Sep 13 18:24:25 EDT 2006


On 9/13/06, Bertram Felgenhauer <bertram.felgenhauer at googlemail.com> wrote:
> Michael Shulman wrote:
> >
> > class MonadList m where
> >    option :: [a] -> m a
> [...]
>
> There's no need for an extra class, it can be done with MonadPlus:
>
> option :: MonadPlus m => [a] -> m a
> option = msum . map return

But this doesn't always give the behavior I want.  It works for any
monad of the form (ListT m), but not for a monad like (ErrorT e []).
I would want

runErrorT $ do
  x <- option [1..3]
  return x

to return [Right 1, Right 2, Right 3], but with your definition it
returns [Right 1].  This is because (ErrorT e []) inherits its
instance of MonadPlus from Error, not from [].  (Is there a reason for
this, or is it just assumed that this is the more frequently desired
behavior?)  However, I declare

instance (Error e) => MonadList (ErrorT e []) where
    option = lift

then the above code does return [Right 1, Right 2, Right 3].

Mike


More information about the Haskell-Cafe mailing list