Skip to content

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: SetObject

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: SetObject

MaybeT didactic-model

MaybeT(*, name: str = 'Transformer')

Bases: _TransBase

Maybe transformer: MaybeT(m)(A) = m(A + 1).

Stacks a partiality / presupposition-failure effect on top of an arbitrary inner monad.

ContT didactic-model

ContT(*, name: str = 'Transformer', answer: SetObject)

Bases: _TransBase

Continuation transformer: ContT(ρ)(m)(A) = (A → m(ρ)) → m(ρ).

Stacks a continuation effect on top of an arbitrary inner monad.

ATTRIBUTE DESCRIPTION
answer

The answer type ρ.

TYPE: SetObject

WriterT didactic-model

WriterT(*, name: str = 'Transformer', monoid: SetObject)

Bases: _TransBase

Writer transformer: WriterT(M)(m)(A) = m(A × M).

Stacks an accumulator effect on top of an arbitrary inner monad.

ATTRIBUTE DESCRIPTION
monoid

The accumulator type M.

TYPE: SetObject