MonadPlus
From HaskellWiki
(Difference between revisions)
| Line 21: | Line 21: | ||
* '''Left Catch''' — this is rarely advocated, but <tt>Maybe</tt> and <tt>IO</tt> satisfy this as an alternative to '''Left Distribution'''. | * '''Left Catch''' — this is rarely advocated, but <tt>Maybe</tt> and <tt>IO</tt> satisfy this as an alternative to '''Left Distribution'''. | ||
mplus (return a) b = return a | mplus (return a) b = return a | ||
| + | |||
| + | === Which satisfies what? === | ||
| + | |||
| + | <tt>[]</tt> satisfies '''Monoid''', '''Left Zero''', and '''Left Distribution'''. | ||
| + | |||
| + | <tt>Maybe</tt>, <tt>IO</tt> and <tt>STM</tt> satisfy '''Monoid''', '''Left Zero''', and '''Left Catch'''. | ||
== Which rules? == | == Which rules? == | ||
Revision as of 00:34, 16 January 2006
| import Control.Monad |
The MonadPlus class is defined like this:
class (Monad m) => MonadPlus m where mzero :: m a mplus :: m a -> m a -> m a
The precise set of rules that MonadPlus should obey is not agreed upon.
- Monoid — mplus and mzero form a monoid:
mplus mzero a = a mplus a mzero = a mplus (mplus a b) c = mplus a (mplus b c)
- Left Zero — mzero is a left zero for >>=:
mzero >>= k = mzero
- Left Distribution:
mplus a b >>= k = mplus (a >>= k) (b >>= k)
- Left Catch — this is rarely advocated, but Maybe and IO satisfy this as an alternative to Left Distribution.
mplus (return a) b = return a
1 Which satisfies what?
[] satisfies Monoid, Left Zero, and Left Distribution.
Maybe, IO and STM satisfy Monoid, Left Zero, and Left Catch.
2 Which rules?
Martin & Gibbons choose Monoid, Left Zero, and Left Distribution. This makes [] a MonadPlus, but not Maybe or IO.
3 What should be done?
It is proposed that the class be separated into MonadZero, MonadPlus, MonadOr. See MonadPlus reform proposal.
