[Haskell-beginners] Monad transformers and MonadPlus

Brent Yorgey byorgey at seas.upenn.edu
Fri Jul 30 06:48:13 EDT 2010


On Fri, Jul 30, 2010 at 03:08:44AM -0700, Johann Bach wrote:
> This interesting page in Wikibooks describes how to write MaybeT:
> 
> http://en.wikibooks.org/wiki/Haskell/Monad_transformers
> 
> And it makes MaybeT an instance of MonadPlus. Even when used with IO
> as the inner monad.
> 
> the constraint on the inner monad is Monad, not Monad plus:
> 
>   instance (Monad m) => MonadPlus (MaybeT m) where
> 
> Apparently IO is not normally an instance of MonadPlus. It is
> interesting to be able to combine IO operations with mplus, because
> the idea of "trying computations until one succeeds" is so common in
> IO.
> 
> Then I started wondering if StateT or ErrorT could be used to make IO
> the inner monad of a MonadPlus instance. Well, StateT is an instance
> of MonadPlus but with a MonadPlus constraint on the inner monad, so IO
> won't cut it.
> 
> This leads me to wonder if there is a way to write mplus and mzero in
> StateT or ErrorT without a MonadPlus constraint on the inner monad.
> But if not, what was special about MaybeT as described in Wikibooks?

It should be possible to do this with ErrorT, but not with StateT.
The thing that is special about MaybeT (and ErrorT) is that it adds a
notion of failure (and choice), which is exactly what MonadPlus is
about.  A MaybeT IO computation is an IO computation that might fail.
StateT just adds some state -- if we didn't already have a notion of
failure before, we won't get one by adding some state.

-Brent


More information about the Beginners mailing list