# MonadPlus reform proposal

### From HaskellWiki

(Difference between revisions)

BrettGiles (Talk | contribs) m (Adding category) |
Nmessenger (Talk | contribs) m (added pretty colors!) |
||

Line 7: | Line 7: | ||

=== MonadZero === |
=== MonadZero === |
||

− | class Monad m => MonadZero m where |
+ | <haskell> |

− | mzero :: m a |
+ | class Monad m => MonadZero m where |

+ | mzero :: m a |
||

+ | </haskell> |
||

satisfying '''Left Zero''': |
satisfying '''Left Zero''': |
||

− | mzero >>= k = mzero |
+ | |

+ | <haskell> |
||

+ | mzero >>= k = mzero |
||

+ | </haskell> |
||

=== MonadPlus === |
=== MonadPlus === |
||

− | class MonadZero m => MonadPlus m where |
+ | <haskell> |

− | mplus :: m a -> m a -> m a |
+ | class MonadZero m => MonadPlus m where |

+ | mplus :: m a -> m a -> m a |
||

+ | </haskell> |
||

satisfying '''Monoid''' and '''Left Distribution''': |
satisfying '''Monoid''' and '''Left Distribution''': |
||

− | mplus mzero b = b |
+ | |

− | mplus a mzero = a |
+ | <haskell> |

− | mplus (mplus a b) c = mplus a (mplus b c) |
+ | mplus mzero b = b |

− | mplus a b >>= k = mplus (a >>= k) (b >>= k) |
+ | mplus a mzero = a |

+ | mplus (mplus a b) c = mplus a (mplus b c) |
||

+ | mplus a b >>= k = mplus (a >>= k) (b >>= k) |
||

+ | </haskell> |
||

=== MonadOr === |
=== MonadOr === |
||

− | class MonadZero m => MonadOr m where |
+ | <haskell> |

− | morelse :: m a -> m a -> m a |
+ | class MonadZero m => MonadOr m where |

+ | morelse :: m a -> m a -> m a |
||

+ | </haskell> |
||

satisfying '''Monoid''' and '''Left Catch''': |
satisfying '''Monoid''' and '''Left Catch''': |
||

− | morelse mzero b = b |
+ | |

− | morelse a mzero = a |
+ | <haskell> |

− | morelse (morelse a b) c = morelse a (morelse b c) |
+ | morelse mzero b = b |

− | morelse (return a) b = return a |
+ | morelse a mzero = a |

+ | morelse (morelse a b) c = morelse a (morelse b c) |
||

+ | morelse (return a) b = return a |
||

+ | </haskell> |
||

== Instances of both == |
== Instances of both == |
||

Line 39: | Line 39: | ||

Some types could be made instances of both. For instance: |
Some types could be made instances of both. For instance: |
||

− | instance MonadOr [] where |
+ | <haskell> |

+ | instance MonadOr [] where |
||

morelse [] b = b |
morelse [] b = b |
||

morelse a b = a |
morelse a b = a |
||

+ | </haskell> |
||

[[Category:Proposals]] [[Category:Monad]] |
[[Category:Proposals]] [[Category:Monad]] |

## Revision as of 00:26, 4 January 2007

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**.

## Contents |

## 1 Proposal

It is proposed that MonadPlus be split like this:

### 1.1 MonadZero

class Monad m => MonadZero m where mzero :: m a

satisfying **Left Zero**:

mzero >>= k = mzero

### 1.2 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)

### 1.3 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

## 2 Instances of both

Some types could be made instances of both. For instance:

instance MonadOr [] where morelse [] b = b morelse a b = a