| Line No. | Rev | Author | Line |
|---|---|---|---|
| 1 | 1 | paulosilva | |
| 2 | {-# OPTIONS_GHC -Wall #-} | ||
| 3 | |||
| 4 | ------------------------------------------------------------------------------- | ||
| 5 | |||
| 6 | {- | | ||
| 7 | Module : Control.MonadOr | ||
| 8 | Description : MonadOr definition. | ||
| 9 | Copyright : (c) Paulo Silva | ||
| 10 | License : LGPL | ||
| 11 | |||
| 12 | Maintainer : paufil@di.uminho.pt | ||
| 13 | Stability : experimental | ||
| 14 | Portability : portable | ||
| 15 | |||
| 16 | Monad definition satisfying the Monoid and Left Catch laws as refered in | ||
| 17 | <http://www.haskell.org/haskellwiki/MonadPlus_reform_proposal> | ||
| 18 | |||
| 19 | -} | ||
| 20 | |||
| 21 | ------------------------------------------------------------------------------- | ||
| 22 | |||
| 23 | module Control.MonadOr ( | ||
| 24 | MonadOr, | ||
| 25 | 4 | paulosilva | mzero, |
| 26 | 1 | paulosilva | morelse |
| 27 | ) where | ||
| 28 | |||
| 29 | 3 | paulosilva | import Control.Monad.Error |
| 30 | import Control.Monad.State | ||
| 31 | 1 | paulosilva | |
| 32 | ------------------------------------------------------------------------------- | ||
| 33 | |||
| 34 | class MonadPlus m => MonadOr m where | ||
| 35 | morelse :: m a -> m a -> m a | ||
| 36 | |||
| 37 | ------------------------------------------------------------------------------- | ||
| 38 | |||
| 39 | instance MonadOr [] where | ||
| 40 | morelse [] b = b | ||
| 41 | morelse a _ = a | ||
| 42 | |||
| 43 | ------------------------------------------------------------------------------- | ||
| 44 | |||
| 45 | -- This instance is equal to mplus for maybe | ||
| 46 | instance MonadOr Maybe where | ||
| 47 | morelse = mplus | ||
| 48 | |||
| 49 | ------------------------------------------------------------------------------- | ||
| 50 | |||
| 51 | 3 | paulosilva | instance (Monad m, Error e) => MonadOr (ErrorT e m) where |
| 52 | morelse = mplus | ||
| 53 | |||
| 54 | ------------------------------------------------------------------------------- | ||
| 55 | |||
| 56 | instance MonadOr m => MonadOr (StateT s m) where | ||
| 57 | m `morelse` n = StateT $ \s -> runStateT m s `morelse` runStateT n s | ||
| 58 | |||
| 59 | ------------------------------------------------------------------------------- |