| | 10 | == Examples == |
| | 11 | |
| | 12 | one area where this is particularly useful is when dealing with monad transformers. for example the jhc typechecking monad is declared as follows |
| | 13 | {{{ |
| | 14 | data TcEnv = TcEnv { ... } |
| | 15 | |
| | 16 | newtype TI a = TI (ReaderT TcEnv IO a) |
| | 17 | deriving(Monad,MonadFix,MonadIO,MonadReader TcEnv,Functor) |
| | 18 | }}} |
| | 19 | and we end up with a complete monad with every interesting instance defined |
| | 20 | for free. |
| | 21 | |
| | 22 | It is also good for selectively hiding properties of monad transformers |
| | 23 | |
| | 24 | {{{ |
| | 25 | -- | Unique integer generator monad transformer. |
| | 26 | newtype UniqT m a = UniqT (StateT Int m a) |
| | 27 | deriving(Monad,MonadTrans, Functor, MonadFix, MonadPlus) |
| | 28 | }}} |
| | 29 | |
| | 30 | notice that while it uses a state transformer internally, the MonadState class |
| | 31 | is intentionally left out of the deriving clause. this means that calls to the |
| | 32 | state monad will pass through the UniqT making stacking monads signifigantly |
| | 33 | easier. |
| | 34 | |