Difference between revisions of "MonadPlus"
Jump to navigation
Jump to search
Line 1: | Line 1: | ||
+ | {{Standard class|MonadPlus|module=Control.Monad|module-doc=Control-Monad|package=base}} |
||
The '''MonadPlus''' class is defined like this: |
The '''MonadPlus''' class is defined like this: |
||
Line 6: | Line 7: | ||
The precise set of rules that MonadPlus should obey is not agreed upon. |
The precise set of rules that MonadPlus should obey is not agreed upon. |
||
+ | |||
+ | * '''Monoid''' — <tt>mplus</tt> and <tt>mzero</tt> form a monoid: |
||
+ | mplus mzero a = a |
||
+ | mplus a mzero = a |
||
+ | mplus (mplus a b) c = mplus a (mplus b c) |
||
+ | |||
+ | * '''Left Zero''' — <tt>mzero</tt> is a left zero for <tt>>>=</tt>: |
||
+ | mzero >>= k = mzero |
||
+ | |||
+ | * '''Left Distribution''': |
||
+ | mplus a b >>= k = mplus (a >>= k) (b >>= k) |
||
+ | |||
+ | * '''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 |
||
+ | |||
+ | == Which rules? == |
||
+ | |||
+ | [http://web.comlab.ox.ac.uk/oucl/work/jeremy.gibbons/publications/tactics.pdf Martin & Gibbons] choose '''Monoid''', '''Left Zero''', and '''Left Distribution'''. This makes <tt>[]</tt> a MonadPlus, but not <tt>Maybe</tt> or <tt>IO</tt>. |
||
+ | |||
+ | [[Category:Standard Classes]] |
Revision as of 23:16, 15 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
Which rules?
Martin & Gibbons choose Monoid, Left Zero, and Left Distribution. This makes [] a MonadPlus, but not Maybe or IO.