[Haskell-cafe] Still stacking monad transformers
Jonathan Cast
jonathanccast at fastmail.fm
Mon Oct 13 13:33:53 EDT 2008
On Mon, 2008-10-13 at 18:28 +0100, Andrew Coppin wrote:
> Reid Barton wrote:
> > It's not difficult: the operation is called
> >
> > mplus :: MyMonad a -> MyMonad a -> MyMonad a
> >
> > and already exists (assuming the author of ListT has not forgotten to
> > write a MonadPlus instance).
> >
>
> I see... I was under the impression that "mplus" is just any arbitrary
> binary operation over a given monad. How do you know what it does for a
> specific monad?
Process of elimination. Sometimes, this doesn't narrow things down to a
single operation, but it gives you a good idea of what you're supposed
to expect.
Firstly, mplus and mzero form a (natural) monoid, put together. That
rules out a number of binary operations right there.
Secondly, mzero has a null law with (>>=):
mzero >>= f = mzero
So, if you have
a `mplus` b
and a calls mzero at some point (not inside another call to mplus ---
nice and informal, that description :), then you know b will be executed
instead. (Maybe b will be executed *anyway*. I didn't say anything
about that).
So mplus and mzero are basically suitable for three kinds of things:
* Exception handling
* Back-tracking
* Parallelism
Usually, when you see a MonadPlus instance, you expect one or more of
these.
That's in the general case.
ListT is a special case; the (somewhat idealized) specification of ListT
(what people want to happen when they use ListT) is that ListT m in some
sense `adds back-tracking' to m. Where back-tracking choice is
implemented by mplus.
jcc
More information about the Haskell-Cafe
mailing list