MonadPlus
From HaskellWiki
(Difference between revisions)
m (Remove Category:Standard classes - now in template) |
|||
(One intermediate revision by one user not shown) | |||
Line 2: | Line 2: | ||
The '''MonadPlus''' class is defined like this: |
The '''MonadPlus''' class is defined like this: |
||
− | class (Monad m) => MonadPlus m where |
+ | <haskell> |
+ | class (Monad m) => MonadPlus m where |
||
mzero :: m a |
mzero :: m a |
||
mplus :: m a -> m a -> m a |
mplus :: m a -> m a -> m a |
||
+ | </haskell> |
||
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: |
+ | * '''Monoid''' — <hask>mplus</hask> and <hask>mzero</hask> form a monoid: |
− | mplus mzero a = a |
+ | <haskell> |
− | mplus a mzero = a |
+ | mplus mzero a = a |
− | mplus (mplus a b) c = mplus a (mplus b c) |
+ | mplus a mzero = a |
+ | mplus (mplus a b) c = mplus a (mplus b c) |
||
+ | </haskell> |
||
− | * '''Left Zero''' — <tt>mzero</tt> is a left zero for <tt>>>=</tt>: |
+ | * '''Left Zero''' — <hask>mzero</hask> is a left zero for <tt>>>=</tt>: |
− | mzero >>= k = mzero |
+ | <haskell> |
+ | mzero >>= k = mzero |
||
+ | </haskell> |
||
* '''Left Distribution''': |
* '''Left Distribution''': |
||
− | mplus a b >>= k = mplus (a >>= k) (b >>= k) |
+ | <haskell> |
+ | mplus a b >>= k = mplus (a >>= k) (b >>= k) |
||
+ | </haskell> |
||
* '''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 |
+ | <haskell> |
+ | mplus (return a) b = return a |
||
+ | </haskell> |
||
=== Which satisfies what? === |
=== Which satisfies what? === |
||
− | <tt>[]</tt> satisfies '''Monoid''', '''Left Zero''', and '''Left Distribution'''. |
+ | <hask>[]</hask> satisfies '''Monoid''', '''Left Zero''', and '''Left Distribution'''. |
− | <tt>Maybe</tt>, <tt>IO</tt> and <tt>STM</tt> satisfy '''Monoid''', '''Left Zero''', and '''Left Catch'''. |
+ | <hask>Maybe</hask>, <hask>IO</hask> and <hask>STM</hask> satisfy '''Monoid''', '''Left Zero''', and '''Left Catch'''. |
== Which rules? == |
== 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>. |
+ | [http://web.comlab.ox.ac.uk/oucl/work/jeremy.gibbons/publications/tactics.pdf Martin & Gibbons] choose '''Monoid''', '''Left Zero''', and '''Left Distribution'''. This makes <hask>[]</hask> a MonadPlus, but not <hask>Maybe</hask> or <hask>IO</hask>. |
== What should be done? == |
== What should be done? == |
||
− | It is proposed that the class be separated into MonadZero, MonadPlus, MonadOr. See [[MonadPlus reform proposal]]. |
+ | It is proposed that the class be separated into <hask>MonadZero</hask>, <hask>MonadPlus</hask>, <hask>MonadOr</hask>. See [[MonadPlus reform proposal]]. |
− | |||
− | [[Category:Standard classes]] |
Latest revision as of 02:40, 31 July 2008
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 — andmplusform a monoid:mzero
mplus mzero a = a mplus a mzero = a mplus (mplus a b) c = mplus a (mplus b c)
- Left Zero — is a left zero for >>=:mzero
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
[edit] 1 Which satisfies what?
[]
Maybe
IO
STM
[edit] 2 Which rules?
Martin & Gibbons choose Monoid, Left Zero, and Left Distribution. This makes[]
Maybe
IO
[edit] 3 What should be done?
It is proposed that the class be separated intoMonadZero
MonadPlus
MonadOr