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

Moduletransformers-0.6.1.1Haskell2010

Control.Monad.Trans.RWS.CPS

A monad transformer that combines ReaderT, WriterT and StateT. This version uses continuation-passing-style for the writer part to achieve constant space usage. For a lazy version with the same interface, see Control.Monad.Trans.RWS.Lazy.

  • 2 types
  • 30 values

The RWS monad

7 declarations
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.

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

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

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

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

valueevalRWS
  1. :: Monoid w
  2. => RWS r w s a

    RWS computation to execute

  3. -> r

    initial environment

  4. -> s

    initial value

  5. -> (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.

valueexecRWS
  1. :: Monoid w
  2. => RWS r w s a

    RWS computation to execute

  3. -> r

    initial environment

  4. -> s

    initial value

  5. -> (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.

valuemapRWS
  1. :: (Monoid w, Monoid w')
  2. => (a, s, w) -> (b, s, w')
  3. -> RWS r w s a
  4. -> RWS r w' s b
#

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

The RWST monad transformer

7 declarations
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.

Instances11MonadTrans, Monad, Functor, MonadFix, MonadFail, Applicative, …
valuerwsT :: (Functor m, Monoid w) => (r -> s -> m (a, s, w)) -> RWST r w s m a
#

Construct an RWST computation from a function. (The inverse of runRWST.)

valuerunRWST :: Monoid w => RWST r w s m a -> r -> s -> m (a, s, w)
#

Unwrap an RWST computation as a function. (The inverse of rwsT.)

valueevalRWST
  1. :: (Monad m, Monoid w)
  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.

valueexecRWST
  1. :: (Monad m, Monoid w)
  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.

valuemapRWST
  1. :: (Monad n, Monoid w, Monoid w')
  2. => m (a, s, w) -> n (b, s, w')
  3. -> RWST r w s m a
  4. -> RWST r w' s n b
#

Map the inner computation using the given function.

  • runRWST (mapRWST f m) r s = f (runRWST m r s) mapRWST :: (m (a, s, w) -> n (b, s, w')) -> RWST r w s m a -> RWST r w' s n b

Reader operations

4 declarations
valuereader :: Monad m => (r -> a) -> RWST r w s m a
#

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

valueask :: Monad m => RWST r w s m r
#

Fetch the value of the environment.

Writer operations

6 declarations
valuewriter :: (Monoid w, Monad m) => (a, w) -> RWST r w s m a
#

Construct a writer computation from a (result, output) pair.

valuepass
  1. :: (Monoid w, Monoid w', Monad m)
  2. => RWST r w s m (a, w -> w')
  3. -> RWST r w' s 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.

State operations

5 declarations
valuestate :: Monad m => (s -> (a, s)) -> RWST r w s m a
#

Construct a state monad computation from a state transformer function.

valueget :: Monad m => RWST r w s m s
#

Fetch the current value of the state within the monad.

valueput :: Monad m => s -> RWST r w s m ()
#

put s sets the state within the monad to s.

valuemodify :: Monad m => (s -> s) -> RWST r w s m ()
#

modify f is an action that updates the state to the result of applying f to the current state.

valuegets :: Monad m => (s -> a) -> RWST r w s m a
#

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

Lifting other operations

3 declarations
valueliftCallCC :: CallCC m (a, s, w) (b, s, w) -> CallCC (RWST r w s m) a b
#

Uniform lifting of a callCC operation to the new monad. This version rolls back to the original state on entering the continuation.

valueliftCallCC' :: CallCC m (a, s, w) (b, s, w) -> CallCC (RWST r w s m) a b
#

In-situ lifting of a callCC operation to the new monad. This version uses the current state on entering the continuation.