HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Modulererebase-1.21.2Haskell2010

Control.Monad.RWS

  • 2 types
  • 6 classes
  • 16 values
typetype RWS r w s = RWST r w s Identity
#

A monad containing an environment of type r, output of type w and an updatable state of type s.

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.

valuegets :: MonadState s m => (s -> a) -> m a
#

Gets specific component of the state, using a projection function supplied.

valuemodify :: MonadState s m => (s -> s) -> m ()
#

Monadic state transformer.

Maps an old state to a new state inside a state monad. The old state is thrown away.

     Main> :t modify ((+1) :: Int -> Int)
     modify (...) :: (MonadState Int a) => a ()

This says that modify (+1) acts over any Monad that is a member of the MonadState class, with an Int state.

valuemodify' :: MonadState s m => (s -> s) -> m ()
#

A variant of modify in which the computation is strict in the new state.

valuecensor :: MonadWriter w m => (w -> w) -> m a -> m a
#

censor f m is an action that executes the action m and applies the function f to its output, leaving the return value unchanged.

valuelistens :: MonadWriter w m => (w -> b) -> m a -> m (a, b)
#

listens f m is an action that executes the action m and adds the result of applying f to the output to the value of the computation.

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 Monad m => MonadState s (m :: Type -> Type) | m -> s where
#

Minimal definition is either both of get and put or just state

Methods

  • get :: m s

    Return the state from the internals of the monad.

  • put :: s -> m ()

    Replace the state inside the monad.

  • state :: (s -> (a, s)) -> m a

    Embed a simple state action into the monad.

Instances26MonadState, …
classclass (Monoid w, Monad m) => MonadWriter w (m :: Type -> Type) | m -> w where
#

Methods

  • writer :: (a, w) -> m a

    writer (a,w) embeds a simple writer action.

  • tell :: w -> m ()

    tell w is an action that produces the output w.

  • listen :: m a -> m (a, w)

    listen m is an action that executes the action m and adds its output to the value of the computation.

  • pass :: m (a, w -> w) -> m a

    pass m is an action that executes the action m, which returns a value and a function, and returns the value, applying the function to the output.

Instances25MonadWriter, …
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 RWST r w s (m :: Type -> Type) a
#

A monad transformer adding reading an environment of type r, collecting an output of type w and updating a state of type s to an inner monad m.

Constructors

Instances40MonadRWS, MonadAccum, MonadError, MonadReader, MonadState, MonadWriter, …
valueevalRWS
  1. :: RWS r w s a

    RWS computation to execute

  2. -> r

    initial environment

  3. -> s

    initial value

  4. -> (a, w)

    final value and output

#

Evaluate a computation with the given initial state and environment, returning the final value and output, discarding the final state.

valueevalRWST
  1. :: Monad m
  2. => RWST r w s m a

    computation to execute

  3. -> r

    initial environment

  4. -> s

    initial value

  5. -> m (a, w)

    computation yielding final value and output

#

Evaluate a computation with the given initial state and environment, returning the final value and output, discarding the final state.

valueexecRWS
  1. :: RWS r w s a

    RWS computation to execute

  2. -> r

    initial environment

  3. -> s

    initial value

  4. -> (s, w)

    final state and output

#

Evaluate a computation with the given initial state and environment, returning the final state and output, discarding the final value.

valueexecRWST
  1. :: Monad m
  2. => RWST r w s m a

    computation to execute

  3. -> r

    initial environment

  4. -> s

    initial value

  5. -> m (s, w)

    computation yielding final state and output

#

Evaluate a computation with the given initial state and environment, returning the final state and output, discarding the final value.

valuemapRWS :: ((a, s, w) -> (b, s, w')) -> RWS r w s a -> RWS r w' s b
#

Map the return value, final state and output of a computation using the given function.

valuerunRWS :: RWS r w s a -> r -> s -> (a, s, w)
#

Unwrap an RWS computation as a function. (The inverse of rws.)

valuerws :: (r -> s -> (a, s, w)) -> RWS r w s a
#

Construct an RWS computation from a function. (The inverse of runRWS.)

classclass (Monoid w, MonadReader r m, MonadWriter w m, MonadState s m) => MonadRWS r w s (m :: Type -> Type) | m -> r, m -> w, m -> s
#
Instances7MonadRWS, …