HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Modulererebase-1.21.2Haskell2010

Control.Monad.Reader

  • 2 types
  • 3 classes
  • 6 values
  • Packagererebase-1.21.2
  • Exports11
  • LanguageHaskell2010
  • LicenceMIT
  • SourceReader.hs
valueasks
  1. :: MonadReader r m
  2. => (r -> a)

    The selector function to apply to the environment.

  3. -> m a
#

Retrieves a function of the current environment.

valuerunReader
  1. :: Reader r a

    A Reader to run.

  2. -> r

    An initial environment.

  3. -> a
#

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

classclass Monad m => MonadIO (m :: Type -> Type) 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:

Methods

  • liftIO :: IO a -> m a

    Lift a computation from the IO monad. This allows us to run IO computations in any monadic stack, so long as it supports these kinds of operations (i.e. IO is the base monad for the stack).

    Example
    import Control.Monad.Trans.State -- from the "transformers" library
    
    printState :: Show s => StateT s IO ()
    printState = do
      state <- get
      liftIO $ print state

    Had we omitted liftIO, we would have ended up with this error:

    • Couldn't match type ‘IO’ with ‘StateT s IO’
     Expected type: StateT s IO ()
       Actual type: IO ()

    The important part here is the mismatch between StateT s IO () and IO ().

    Luckily, we know of a function that takes an IO a and returns an (m a): liftIO, enabling us to run the program and see the expected results:

    > evalStateT printState "hello"
    "hello"
    
    > evalStateT printState 3
    3
    
Instances26MonadIO, …
classclass Monad m => MonadReader r (m :: Type -> Type) | m -> r where
#

See examples in Control.Monad.Reader. Note, the partially applied function type (->) r is a simple reader monad. See the instance declaration below.

Methods

  • ask :: m r

    Retrieves the monad environment.

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

    Executes a computation in a modified environment.

  • reader :: (r -> a) -> m a

    Retrieves a function of the current environment.

Instances29MonadReader, …
classclass (forall (m :: Type -> Type). Monad m => Monad (t m)) => MonadTrans (t :: (Type -> Type) -> Type -> Type) where
#

The class of monad transformers. For any monad m, the result t m should also be a monad, and lift should be a monad transformation from m to t m, i.e. it should satisfy the following laws:

Since 0.6.0.0 and for GHC 8.6 and later, the requirement that t m be a Monad is enforced by the implication constraint forall m. Monad m => Monad (t m) enabled by the QuantifiedConstraints extension.

Ambiguity error with GHC 9.0 to 9.2.2

These versions of GHC have a bug (https://gitlab.haskell.org/ghc/ghc/-/issues/20582) which causes constraints like

(MonadTrans t, forall m. Monad m => Monad (t m)) => ...

to be reported as ambiguous. For transformers 0.6 and later, this can be fixed by removing the second constraint, which is implied by the first.

Methods

  • lift :: Monad m => m a -> t m a

    Lift a computation from the argument monad to the constructed monad.

Instances32MonadTrans, …
newtypenewtype ReaderT r (m :: Type -> Type) 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:

image: images/bind-ReaderT.svg

Constructors

Instances53Generic1, MonadAccum, MonadError, MonadReader, MonadState, MonadWriter, …