| Copyright | (c) Andy Gill 2001, (c) Oregon Graduate Institute of Science and Technology, 2001, (C) 2015 KONISHI Yohsuke | 
|---|---|
| License | BSD-style (see the file LICENSE) | 
| Maintainer | ocean0yohsuke@gmail.com | 
| Stability | experimental | 
| Portability | --- | 
| Safe Haskell | Safe | 
| Language | Haskell2010 | 
DeepControl.Monad.Trans
Contents
Description
This module enables you to program in Monad-Transformer style for more deeper level than the usual Control.Monad.Trans module expresses.
You would realize exactly what more deeper level means by reading the example codes, which are attached on the page bottom.
Note: all the MonadTransx instances for Level-4 and Level-5 haven't been written yet.
- class Monad m => MonadIO m where
- trans :: (Monad m, MonadTrans t) => m a -> t m a
- class MonadTrans t where
- class MonadTrans2 t where
- class MonadTrans3 t where
- class MonadTrans4 t where
- class MonadTrans5 t where
MonadIO
class Monad m => MonadIO m where
Monads in which IO computations may be embedded.
 Any monad built by applying a sequence of monad transformers to the
 IO monad will be an instance of this class.
Instances should satisfy the following laws, which state that liftIO
 is a transformer of monads:
Instances
MonadTrans
trans :: (Monad m, MonadTrans t) => m a -> t m a Source
Alias for lift
class MonadTrans t where
The class of monad transformers.  Instances should satisfy the
 following laws, which state that lift is a monad transformation:
Methods
lift :: Monad m => m a -> t m a
Lift a computation from the argument monad to the constructed monad.
Instances
| MonadTrans ListT | |
| MonadTrans MaybeT | |
| MonadTrans IdentityT | |
| MonadTrans (ContT r) | |
| MonadTrans (ReaderT r) | |
| MonadTrans (StateT s) | |
| MonadTrans (StateT s) | |
| MonadTrans (ExceptT e) | |
| Error e => MonadTrans (ErrorT e) | |
| Monoid w => MonadTrans (WriterT w) | |
| Monoid w => MonadTrans (WriterT w) | |
| Monoid w => MonadTrans (RWST r w s) | |
| Monoid w => MonadTrans (RWST r w s) | 
class MonadTrans2 t where Source
Instances
| MonadTrans2 (ReaderT2 r) Source | |
| MonadTrans2 (StateT2 s) Source | |
| Monoid w => MonadTrans2 (WriterT2 w) Source | |
| Monoid w => MonadTrans2 (RWST2 r w s) Source | 
class MonadTrans3 t where Source
Instances
| MonadTrans3 (ReaderT3 r) Source | |
| MonadTrans3 (StateT3 s) Source | |
| Monoid w => MonadTrans3 (WriterT3 w) Source | |
| Monoid w => MonadTrans3 (RWST3 r w s) Source | 
class MonadTrans4 t where Source
class MonadTrans5 t where Source
Level-2 example
Here is a monad transformer example how to implement Ackermann function, improved to stop within a certain limit of time, with ReaderT2-IO-Maybe monad, a level-2 monad-transformation.
import DeepControl.Applicative
import DeepControl.Commutative (commute)
import DeepControl.Monad ((>-))
import DeepControl.Monad.Trans (trans2)
import DeepControl.Monad.Trans.Reader
import System.Timeout (timeout)
type TimeLimit = Int
ackermann :: TimeLimit -> Int -> Int ->
             IO (Maybe Int)                        -- IO-Maybe Monad
ackermann timelimit x y = timeout timelimit (ackermannIO x y)
  where
    ackermannIO :: Int -> Int -> IO Int
    ackermannIO 0 n = (*:) $ n + 1
    ackermannIO m n | m > 0 && n == 0 = ackermannIO (m-1) 1
                    | m > 0 && n > 0  = ackermannIO m (n-1) >>= ackermannIO (m-1)
ackermannR :: Int -> Int ->
              ReaderT2 TimeLimit IO Maybe Int      -- ReaderT2-IO-Maybe monad
ackermannR x y = do
    timelimit <- ask
    trans2 $ ackermann timelimit x y               -- transform(lift) IO-Maybe function to ReaderT2-IO-Maybe function
calc_ackermann :: TimeLimit -> Int -> Int -> IO (Maybe Int)
calc_ackermann timelimit x y = ackermannR x y >- \r -> runReaderT2 r timelimit
-- λ> commute $ calc_ackermann 1000 |$> [0..4] |* 4
-- [Just 5,Just 6,Just 11,Just 125,Nothing]