Community
Modules 24

Control.Monad.RWS.Strict

experimental

Strict RWS monad.

Inspired by the paper Functional Programming with Overloading and Higher-Order Polymorphism, Mark P Jones (http://web.cecs.pdx.edu/~mpj/) Advanced School of Functional Programming, 1995.

The RWS monad

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

rws :: (r -> s -> (a, s, w)) -> RWS r w s a

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

runRWS :: RWS r w s a -> r -> s -> (a, s, w)

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

evalRWS :: RWS r w s a -> r -> s -> (a, w)

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

RWS computation to execute

r

initial environment

s

initial value

(a, w)

final value and output

execRWS :: RWS r w s a -> r -> s -> (s, w)

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

RWS computation to execute

r

initial environment

s

initial value

(s, w)

final state and output

mapRWS :: ((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.

  • runRWS (mapRWS f m) r s = f (runRWS m r s)

withRWS :: (r' -> s -> (r, s)) -> RWS r w s a -> RWS r' w s a

withRWS f m executes action m with an initial environment and state modified by applying f.

  • runRWS (withRWS f m) r s = uncurry (runRWS m) (f r s)

The RWST monad transformer

newtype RWST r w s m 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.

RWST :: r -> s -> m (a, s, w) -> RWST r w s m a

runRWST :: RWST r w s m a -> r -> s -> m (a, s, w)

evalRWST :: (Monad m) => RWST r w s m a -> r -> s -> m (a, w)

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

computation to execute

computation yielding final value and output

r

initial environment

s

initial value

execRWST :: (Monad m) => RWST r w s m a -> r -> s -> m (s, w)

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

computation to execute

computation yielding final state and output

r

initial environment

s

initial value

mapRWST :: (m (a, s, w) -> n (b, s, w')) -> RWST r w s m a -> 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)

withRWST :: (r' -> s -> (r, s)) -> RWST r w s m a -> RWST r' w s m a

withRWST f m executes action m with an initial environment and state modified by applying f.

  • runRWST (withRWST f m) r s = uncurry (runRWST m) (f r s)

Strict Reader-writer-state monads

module Control.Monad.RWS.Class mtl-2.3.2 · 10 declarations

asks :: MonadReader r m => (r -> a) -> m a

Retrieves a function of the current environment.

(r -> a)

The selector function to apply to the environment.

censor :: 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.

  • censor f m = pass (liftM (\x -> (x,f)) m)

gets :: MonadState s m => (s -> a) -> m a

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

listens :: 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.

  • listens f m = liftM (id *** f) (listen m)

modify :: 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.

modify' :: MonadState s m => (s -> s) -> m ()

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

class (Monoid w, MonadReader r m, MonadWriter w m, MonadState s m) => MonadRWS r w s m

class Monad m => MonadReader r m

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

ask :: m r

Retrieves the monad environment.

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

Executes a computation in a modified environment.

Reader to run in the modified environment.

(r -> r)

The function to modify the environment.

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

Retrieves a function of the current environment.

(r -> a)

The selector function to apply to the environment.

class Monad m => MonadState s m

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

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.

class (Monoid w, Monad m) => MonadWriter w m

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.

module Control.Monad.Trans mtl-2.3.2 · 2 declarations

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

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:

  • lift . return = return

  • lift (m >>= f) = lift m >>= (lift . f)

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.

lift :: (Monad m) => m a -> t m a

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

Maintainer
libraries@haskell.org
Portability
non-portable (multi-param classes, functional dependencies)
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. The generator reported 6 diagnostics.

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

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

Metadata snapshot: 032b29dc8ecd03eaae7b1a84a40ef8560125381762749b2f47333bc9a4f4bb8e

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.