Difference between revisions of "MonadPlus"

From HaskellWiki
Jump to navigation Jump to search
m (clean up)
m (Minor formatting changes)
 
(One intermediate revision by one other user not shown)
Line 11: Line 11:
   
 
* '''Monoid''' &mdash; <hask>mplus</hask> and <hask>mzero</hask> form a monoid:
 
* '''Monoid''' &mdash; <hask>mplus</hask> and <hask>mzero</hask> form a monoid:
<haskell>
+
:<haskell>
 
mplus mzero a = a
 
mplus mzero a = a
 
mplus a mzero = a
 
mplus a mzero = a
Line 17: Line 17:
 
</haskell>
 
</haskell>
   
* '''Left Zero''' &mdash; <hask>mzero</hask> is a left zero for <tt>&gt;&gt;=</tt>:
+
* '''Left Zero''' &mdash; <hask>mzero</hask> is a left zero for <code>(>>=)</code>:
<haskell>
+
:<haskell>
 
mzero >>= k = mzero
 
mzero >>= k = mzero
 
</haskell>
 
</haskell>
   
 
* '''Left Distribution''':
 
* '''Left Distribution''':
<haskell>
+
:<haskell>
 
mplus a b >>= k = mplus (a >>= k) (b >>= k)
 
mplus a b >>= k = mplus (a >>= k) (b >>= k)
 
</haskell>
 
</haskell>
   
 
* '''Left Catch''' &mdash; this is rarely advocated, but <tt>Maybe</tt> and <tt>IO</tt> satisfy this as an alternative to '''Left Distribution'''.
 
* '''Left Catch''' &mdash; this is rarely advocated, but <tt>Maybe</tt> and <tt>IO</tt> satisfy this as an alternative to '''Left Distribution'''.
<haskell>
+
:<haskell>
 
mplus (return a) b = return a
 
mplus (return a) b = return a
 
</haskell>
 
</haskell>
Line 34: Line 34:
 
=== Which satisfies what? ===
 
=== Which satisfies what? ===
   
<hask>[]</hask> satisfies '''Monoid''', '''Left Zero''', and '''Left Distribution'''.
+
* <hask>[]</hask> satisfies '''Monoid''', '''Left Zero''', and '''Left Distribution'''.
   
<hask>Maybe</hask>, <hask>IO</hask> and <hask>STM</hask> 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 <hask>[]</hask> a MonadPlus, but not <hask>Maybe</hask> or <hask>IO</hask>.
+
[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 <code>MonadPlus</code>, 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 <hask>MonadZero</hask>, <hask>MonadPlus</hask>, <hask>MonadOr</hask>. 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 22:55, 5 April 2021

MonadPlus class (base)
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.

  • Monoidmplus and mzero form a monoid:
mplus mzero a = a
mplus a mzero = a
mplus (mplus a b) c = mplus a (mplus b c)
  • Left Zeromzero 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 satisfies what?

  • [] satisfies Monoid, Left Zero, and Left Distribution.
  • Maybe, IO and STM satisfy Monoid, Left Zero, and Left Catch.

Which rules?

Martin & Gibbons choose Monoid, Left Zero, and Left Distribution. This makes [] a MonadPlus, but not Maybe or IO.

What should be done?

It is proposed that the class be separated into MonadZero, MonadPlus, MonadOr. See MonadPlus reform proposal.