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.
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.
type RWS r w s = RWST r w s IdentityA 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 aConstruct 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
rinitial environment
sinitial 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
rinitial environment
sinitial value
(s, w)final state and output
mapRWS :: ((a, s, w) -> (b, s, w')) -> RWS r w s a -> RWS r w' s bMap 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 executes action withRWS f mm with an initial environment
and state modified by applying f.
runRWS (withRWS f m) r s = uncurry (runRWS m) (f r s)
newtype RWST r w s m aA 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 arunRWST :: 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
rinitial environment
sinitial 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
rinitial environment
sinitial value
mapRWST :: (m (a, s, w) -> n (b, s, w')) -> RWST r w s m a -> RWST r w' s n bMap 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 executes action withRWST f mm with an initial environment
and state modified by applying f.
runRWST (withRWST f m) r s = uncurry (runRWST m) (f r s)
Control.Monad.RWS.Class mtl-2.3.2 · 10 declarationsasks :: MonadReader r m => (r -> a) -> m aRetrieves 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 is an action that executes the action censor f mm 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 aGets specific component of the state, using a projection function supplied.
listens :: MonadWriter w m => (w -> b) -> m a -> m (a, b) is an action that executes the action listens f mm 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 mclass Monad m => MonadReader r mSee examples in Control.Monad.Reader.
Note, the partially applied function type (->) r is a simple reader monad.
See the instance declaration below.
ask :: m rRetrieves the monad environment.
local :: (r -> r) -> m a -> m aExecutes 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 aRetrieves a function of the current environment.
(r -> a)The selector function to apply to the environment.
class Monad m => MonadState s mMinimal definition is either both of get and put or just state
get :: m sReturn the state from the internals of the monad.
put :: s -> m ()Replace the state inside the monad.
state :: (s -> (a, s)) -> m aEmbed a simple state action into the monad.
class (Monoid w, Monad m) => MonadWriter w mwriter :: (a, w) -> m a embeds a simple writer action.writer (a,w)
tell :: w -> m () is an action that produces the output tell ww.
listen :: m a -> m (a, w) is an action that executes the action listen mm and adds
its output to the value of the computation.
pass :: m (a, w -> w) -> m a is an action that executes the action pass mm, which
returns a value and a function, and returns the value, applying
the function to the output.
Control.Monad.Trans mtl-2.3.2 · 2 declarationsclass (Monad m) => MonadIO mliftIO :: IO a -> m aclass (forall m. Monad m => Monad (t m)) => MonadTrans tThe 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. enabled by the
Monad m => Monad (t m)QuantifiedConstraints extension.
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 aLift a computation from the argument monad to the constructed monad.
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