Re: [Haskell-beginners] Monad transformers and MonadPlus
You could always add a MondPlus instance to IO directly (or a newtype wrapper). 'mplus' would set up exception handling and 'mzero' would through an exception. You might want to limit it to a subset of exceptions, though. Antoine On Jul 30, 2010 5:09 AM, "Johann Bach" <johann.bach1127@gmail.com> 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? _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
On Friday 30 July 2010 13:56:44, Antoine Latter wrote:
You could always add a MondPlus instance to IO directly (or a newtype wrapper).
'mplus' would set up exception handling and 'mzero' would through an exception. You might want to limit it to a subset of exceptions, though.
Once upon a time, there was a MonadPlus instance for IO. However, one of the laws a MonadPlus instance should satisfy is m >> mzero === mzero You can't have that in IO, since there's no unPutStrLn and such. Therefore, the MonadPlus instance for IO was removed.
On Fri, Jul 30, 2010 at 02:48:17PM +0200, Daniel Fischer wrote:
On Friday 30 July 2010 13:56:44, Antoine Latter wrote:
You could always add a MondPlus instance to IO directly (or a newtype wrapper).
'mplus' would set up exception handling and 'mzero' would through an exception. You might want to limit it to a subset of exceptions, though.
Once upon a time, there was a MonadPlus instance for IO. However, one of the laws a MonadPlus instance should satisfy is
m >> mzero === mzero
You can't have that in IO, since there's no unPutStrLn and such. Therefore, the MonadPlus instance for IO was removed.
If I understand correctly, however, there is some debate over what the "correct" laws for MonadPlus ought to be. One can also imagine other useful monads which satisfy other MonadPlus laws but not the one above, such as MaybeT (State Int), in which failure short-circuits any future modifications to the state, but does not roll back any state modifications that may have happened up to the point of failure. -Brent
On Friday 30 July 2010 19:23:00, Brent Yorgey wrote:
If I understand correctly, however, there is some debate over what the "correct" laws for MonadPlus ought to be.
Yes, and the haskellwiki page on MonadPlus doesn't even mention the 'right zero' property (but the Control.Monad haddocks do).
One can also imagine other useful monads which satisfy other MonadPlus laws but not the one above, such as MaybeT (State Int), in which failure short-circuits any future modifications to the state, but does not roll back any state modifications that may have happened up to the point of failure.
-Brent
Yep, there are several useful variants possible. That is probably why there's no final agreement on the laws.
participants (3)
-
Antoine Latter -
Brent Yorgey -
Daniel Fischer