Difference between revisions of "MonadPlus reform proposal"
Jump to navigation
Jump to search
Line 17: | Line 17: | ||
satisfying '''Monoid''' and '''Left Distribution''': |
satisfying '''Monoid''' and '''Left Distribution''': |
||
− | mplus mzero |
+ | mplus mzero b = b |
mplus a mzero = a |
mplus a mzero = a |
||
mplus (mplus a b) c = mplus a (mplus b c) |
mplus (mplus a b) c = mplus a (mplus b c) |
||
Line 28: | Line 28: | ||
satisfying '''Monoid''' and '''Left Catch''': |
satisfying '''Monoid''' and '''Left Catch''': |
||
− | morelse mzero |
+ | morelse mzero b = b |
morelse a mzero = a |
morelse a mzero = a |
||
morelse (morelse a b) c = morelse a (morelse b c) |
morelse (morelse a b) c = morelse a (morelse b c) |
||
morelse (return a) b = return a |
morelse (return a) b = return a |
||
+ | |||
+ | == Instances of both == |
||
+ | |||
+ | Some types could be made instances of both. For instance: |
||
+ | |||
+ | instance MonadOr [] where |
||
+ | morelse [] b = b |
||
+ | morelse a b = a |
Revision as of 23:35, 15 January 2006
The MonadPlus class is ambiguous: while all instances satisfy Monoid and Left Zero, some such as [] satisfy Left Distribution, while others such as Maybe and IO satisfy Left Catch.
It is proposed that MonadPlus be split like this:
MonadZero
class Monad m => MonadZero m where mzero :: m a
satisfying Left Zero:
mzero >>= k = mzero
MonadPlus
class MonadZero m => MonadPlus m where mplus :: m a -> m a -> m a
satisfying Monoid and Left Distribution:
mplus mzero b = b mplus a mzero = a mplus (mplus a b) c = mplus a (mplus b c) mplus a b >>= k = mplus (a >>= k) (b >>= k)
MonadOr
class MonadZero m => MonadOr m where morelse :: m a -> m a -> m a
satisfying Monoid and Left Catch:
morelse mzero b = b morelse a mzero = a morelse (morelse a b) c = morelse a (morelse b c) morelse (return a) b = return a
Instances of both
Some types could be made instances of both. For instance:
instance MonadOr [] where morelse [] b = b morelse a b = a