Monad Transformers¶
StateT, ReaderT, MaybeT, ContT, and WriterT stack one effect
on another.
transformers
¶
Monad transformers: stack monadic effects.
A MonadTrans instance T provides a lift
embedding an inner-monad Kleisli arrow into the transformer.
Stacking T(m) for a base monad m produces a new monad
whose effects combine T's and m's.
Available transformers:
StateT(state)— adds state-threading to any inner monad.ReaderT(env)— adds reader-effect to any inner monad.MaybeT— adds presupposition-failure to any inner monad.ContT(answer)— adds continuation-passing to any inner monad.WriterT(monoid)— adds accumulator side-channel to any inner monad.
Each transformer is itself a Monad instance once applied to
a base monad; the transformer's lift is a MonadTrans
operation.
StateT
didactic-model
¶
StateT(*, name: str = 'Transformer', state: SetObject)
Bases: _TransBase
State transformer: StateT(σ)(m)(A) = σ → m(A × σ).
Stacks a state-threading effect on top of an arbitrary inner
monad. The standard non-stacked State monad is recovered as
StateT(σ)(Identity).
| ATTRIBUTE | DESCRIPTION |
|---|---|
state |
The state type
TYPE:
|
ReaderT
didactic-model
¶
ReaderT(*, name: str = 'Transformer', env: SetObject)
Bases: _TransBase
Reader transformer: ReaderT(ρ)(m)(A) = ρ → m(A).
Stacks a reader effect on top of an arbitrary inner monad.
| ATTRIBUTE | DESCRIPTION |
|---|---|
env |
The environment type
TYPE:
|
MaybeT
didactic-model
¶
Bases: _TransBase
Maybe transformer: MaybeT(m)(A) = m(A + 1).
Stacks a partiality / presupposition-failure effect on top of an arbitrary inner monad.