transformers-0.5.2.0: Concrete functor and monad transformers

Copyright | (c) Andy Gill 2001 (c) Oregon Graduate Institute of Science and Technology 2001
License | BSD-style (see the file LICENSE)
Maintainer | R.Paterson@city.ac.uk
Stability | experimental
Portability | portable
Safe Haskell | Safe
Language | Haskell98

Control.Monad.Trans.Reader

Contents

- The Reader monad
- The ReaderT monad transformer
- Reader operations
- Lifting other operations

Description

Declaration of the `ReaderT` monad transformer, which adds a static environment to a given monad.

If the computation is to modify the stored information, use Control.Monad.Trans.State instead.

Synopsis

- type Reader r = ReaderT r Identity
- reader :: Monad m => (r -> a) -> ReaderT r m a
- runReader :: Reader r a -> r -> a
- mapReader :: (a -> b) -> Reader r a -> Reader r b
- withReader :: (r' -> r) -> Reader r a -> Reader r' a
- newtype ReaderT r m a = ReaderT {
  - runReaderT :: r -> m a
  }
- mapReaderT :: (m a -> n b) -> ReaderT r m a -> ReaderT r n b
- withReaderT :: (r' -> r) -> ReaderT r m a -> ReaderT r' m a
- ask :: Monad m => ReaderT r m r
- local :: (r -> r) -> ReaderT r m a -> ReaderT r m a
- asks :: Monad m => (r -> a) -> ReaderT r m a
- liftCallCC :: CallCC m a b -> CallCC (ReaderT r m) a b
- liftCatch :: Catch e m a -> Catch e (ReaderT r m) a

# The Reader monad

type Reader r = ReaderT r Identity Source #

The parameterizable reader monad.

Computations are functions of a shared environment.

The `return` function ignores the environment, while `>>=` passes the inherited environment to both subcomputations.

reader :: Monad m => (r -> a) -> ReaderT r m a Source #

Constructor for computations in the reader monad (equivalent to `asks`).

runReader Source #

Arguments

:: Reader r a | A `Reader` to run.
-> r | An initial environment.
-> a |

Runs a `Reader` and extracts the final value from it. (The inverse of `reader`.)

mapReader :: (a -> b) -> Reader r a -> Reader r b Source #

Transform the value returned by a `Reader`.

-     runReader (mapReader f m) = f . runReader m

withReader Source #

Arguments

:: (r' -> r) | The function to modify the environment.
-> Reader r a | Computation to run in the modified environment.
-> Reader r' a |

Execute a computation in a modified environment (a specialization of `withReaderT`).

-     runReader (withReader f m) = runReader m . f

# The ReaderT monad transformer

newtype ReaderT r m a Source #

The reader monad transformer, which adds a read-only environment to the given monad.

The `return` function ignores the environment, while `>>=` passes the inherited environment to both subcomputations.

Constructors

ReaderT |
Fields runReaderT :: r -> m a

Instances

MonadTrans (ReaderT * r) # |
Methods lift :: Monad m => m a -> ReaderT * r m a Source #
Monad m => Monad (ReaderT * r m) # |
Methods (>>=) :: ReaderT * r m a -> (a -> ReaderT * r m b) -> ReaderT * r m b Source # (>>) :: ReaderT * r m a -> ReaderT * r m b -> ReaderT * r m b Source # return :: a -> ReaderT * r m a Source # fail :: String -> ReaderT * r m a Source #
Functor m => Functor (ReaderT * r m) # |
Methods fmap :: (a -> b) -> ReaderT * r m a -> ReaderT * r m b Source # (<$) :: a -> ReaderT * r m b -> ReaderT * r m a Source #
MonadFix m => MonadFix (ReaderT * r m) # |
Methods mfix :: (a -> ReaderT * r m a) -> ReaderT * r m a Source #
MonadFail m => MonadFail (ReaderT * r m) # |
Methods fail :: String -> ReaderT * r m a Source #
Applicative m => Applicative (ReaderT * r m) # |
Methods pure :: a -> ReaderT * r m a Source # (<*>) :: ReaderT * r m (a -> b) -> ReaderT * r m a -> ReaderT * r m b Source # (*>) :: ReaderT * r m a -> ReaderT * r m b -> ReaderT * r m b Source # (<*) :: ReaderT * r m a -> ReaderT * r m b -> ReaderT * r m a Source #
MonadZip m => MonadZip (ReaderT * r m) # |
Methods mzip :: ReaderT * r m a -> ReaderT * r m b -> ReaderT * r m (a, b) Source # mzipWith :: (a -> b -> c) -> ReaderT * r m a -> ReaderT * r m b -> ReaderT * r m c Source # munzip :: ReaderT * r m (a, b) -> (ReaderT * r m a, ReaderT * r m b) Source #
MonadIO m => MonadIO (ReaderT * r m) # |
Methods liftIO :: IO a -> ReaderT * r m a Source #
Alternative m => Alternative (ReaderT * r m) # |
Methods empty :: ReaderT * r m a Source # (<|>) :: ReaderT * r m a -> ReaderT * r m a -> ReaderT * r m a Source # some :: ReaderT * r m a -> ReaderT * r m [a] Source # many :: ReaderT * r m a -> ReaderT * r m [a] Source #
MonadPlus m => MonadPlus (ReaderT * r m) # |
Methods mzero :: ReaderT * r m a Source # mplus :: ReaderT * r m a -> ReaderT * r m a -> ReaderT * r m a Source #

mapReaderT :: (m a -> n b) -> ReaderT r m a -> ReaderT r n b Source #

Transform the computation inside a `ReaderT`.

-     runReaderT (mapReaderT f m) = f . runReaderT m

withReaderT Source #

Arguments

:: (r' -> r) | The function to modify the environment.
-> ReaderT r m a | Computation to run in the modified environment.
-> ReaderT r' m a |

Execute a computation in a modified environment (a more general version of `local`).

-     runReaderT (withReaderT f m) = runReaderT m . f

# Reader operations

ask :: Monad m => ReaderT r m r Source #

Fetch the value of the environment.

local Source #

Arguments

:: (r -> r) | The function to modify the environment.
-> ReaderT r m a | Computation to run in the modified environment.
-> ReaderT r m a |

Execute a computation in a modified environment (a specialization of `withReaderT`).

-     runReaderT (local f m) = runReaderT m . f

asks Source #

Arguments

:: Monad m |
=> (r -> a) | The selector function to apply to the environment.
-> ReaderT r m a |

Retrieve a function of the current environment.

-     asks f = liftM f ask

# Lifting other operations

liftCallCC :: CallCC m a b -> CallCC (ReaderT r m) a b Source #

Lift a `callCC` operation to the new monad.

liftCatch :: Catch e m a -> Catch e (ReaderT r m) a Source #

Lift a `catchE` operation to the new monad.