Community
Modules 24

Control.Monad.Trans.Reader

experimental

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.

The Reader monad

type Reader r = ReaderT r Identity

The parameterizable reader monad, which is non-strict.

Computations are functions of a shared environment.

The return function ignores the environment, while m >>= k passes the inherited environment to both subcomputations:

<<images/bind-ReaderT.svg>>

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

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

runReader :: Reader r a -> r -> a

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

A Reader to run.

r

An initial environment.

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

Transform the value returned by a Reader.

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

withReader :: (r' -> r) -> Reader r a -> Reader r' a

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

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

Computation to run in the modified environment.

(r' -> r)

The function to modify the environment.

The ReaderT monad transformer

newtype ReaderT r m a

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

The return function ignores the environment, while m >>= k passes the inherited environment to both subcomputations:

<<images/bind-ReaderT.svg>>

ReaderT r m is strict if and only if m is.

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

Transform the computation inside a ReaderT.

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

withReaderT :: (r' -> r) -> ReaderT r m a -> ReaderT r' m a

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

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

Computation to run in the modified environment.

(r' -> r)

The function to modify the environment.

Reader operations

ask :: (Monad m) => ReaderT r m r

Fetch the value of the environment.

local :: (r -> r) -> ReaderT r m a -> ReaderT r m a

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

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

Computation to run in the modified environment.

(r -> r)

The function to modify the environment.

asks :: (Monad m) => (r -> a) -> ReaderT r m a

Retrieve a function of the current environment.

  • asks f = liftM f ask

(r -> a)

The selector function to apply to the environment.

Lifting other operations

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

Lift a callCC operation to the new monad.

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

Lift a catchE operation to the new monad.

Instances

13
  • instance (Generic r, Generic m, Generic a) => Generic (ReaderT r m a)
  • instance (Generic1 r, Generic1 m, Generic1 a) => Generic1 (ReaderT r m a)
  • instance (Functor m) => Functor (ReaderT r m)
  • instance (Applicative m) => Applicative (ReaderT r m)
  • instance (Alternative m) => Alternative (ReaderT r m)
  • instance (Monad m) => Monad (ReaderT r m)
  • instance (Fail.MonadFail m) => Fail.MonadFail (ReaderT r m)
  • instance (MonadPlus m) => MonadPlus (ReaderT r m)
  • instance (MonadFix m) => MonadFix (ReaderT r m)
  • instance MonadTrans (ReaderT r)
  • instance (MonadIO m) => MonadIO (ReaderT r m)
  • instance (MonadZip m) => MonadZip (ReaderT r m)
  • instance Contravariant m => Contravariant (ReaderT r m)
Maintainer
R.Paterson@city.ac.uk
Portability
portable
Copyright
(c) Andy Gill 2001, (c) Oregon Graduate Institute of Science and Technology, 2001
License
BSD-style (see the file LICENSE)
Build record and source files

Community contribution. The service has not verified this documentation.

Target: Linux on x86-64. Some inferred types and resolved links are not available.

Generator: aihc-haddock-858b1951-web-1

Metadata snapshot: ac77ab7d2b1e88487eeab41661abedb7bfb8bc846deb92e7874c08d166bff65d

Open the dependency plan · Download the documentation model

Import a package

Enter a package name and an exact version from Hackage. The site contains imported versions only.