Monad transformer explained

In functional programming, a monad transformer is a type constructor which takes a monad as an argument and returns a monad as a result.

Monad transformers can be used to compose features encapsulated by monads – such as state, exception handling, and I/O – in a modular way. Typically, a monad transformer is created by generalising an existing monad; applying the resulting monad transformer to the identity monad yields a monad which is equivalent to the original monad (ignoring any necessary boxing and unboxing).

Definition

A monad transformer consists of:

  1. A type constructor t of kind (* -> *) -> * -> *
  2. Monad operations return and bind (or an equivalent formulation) for all t m where m is a monad, satisfying the monad laws
  3. An additional operation, lift :: m a -> t m a, satisfying the following laws:[1] (the notation `bind` below indicates infix application):
    1. lift . return = return
    2. lift (m `bind` k) = (lift m) `bind` (lift . k)

Examples

The option monad transformer

Given any monad

MA

, the option monad transformer

M\left(A?\right)

(where

A?

denotes the option type) is defined by:

\begin{array}{ll} return:&A\rarrM\left(A?\right)=a\mapstoreturn(Justa)\\ bind:&M\left(A?\right)\rarr\left(A\rarrM\left(B?\right)\right)\rarrM\left(B?\right)=m\mapstof\mapstobindm\left(a\mapsto\begin{cases}returnNothing&ifa=Nothing\fa'&ifa=Justa'\end{cases}\right)\\ lift:&M(A)\rarrM\left(A?\right)=m\mapstobindm(a\mapstoreturn(Justa))\end{array}

The exception monad transformer

Given any monad

MA

, the exception monad transformer

M(A+E)

(where is the type of exceptions) is defined by:

\begin{array}{ll} return:&A\rarrM(A+E)=a\mapstoreturn(valuea)\\ bind:&M(A+E)\rarr(A\rarrM(B+E))\rarrM(B+E)=m\mapstof\mapstobindm\left(a\mapsto\begin{cases}returnerre&ifa=erre\fa'&ifa=valuea'\end{cases}\right)\\ lift:&MA\rarrM(A+E)=m\mapstobindm(a\mapstoreturn(valuea))\\ \end{array}

The reader monad transformer

Given any monad

MA

, the reader monad transformer

E\rarrMA

(where is the environment type) is defined by:

\begin{array}{ll} return:&A\rarrE\rarrMA=a\mapstoe\mapstoreturna\\ bind:&(E\rarrMA)\rarr(A\rarrE\rarrMB)\rarrE\rarrMB=m\mapstok\mapstoe\mapstobind(me)(a\mapstokae)\\ lift:&MA\rarrE\rarrMA=a\mapstoe\mapstoa\\ \end{array}

The state monad transformer

Given any monad

MA

, the state monad transformer

S\rarrM(A x S)

(where is the state type) is defined by:

\begin{array}{ll} return:&A\rarrS\rarrM(A x S)=a\mapstos\mapstoreturn(a,s)\\ bind:&(S\rarrM(A x S))\rarr(A\rarrS\rarrM(B x S))\rarrS\rarrM(B x S)=m\mapstok\mapstos\mapstobind(ms)((a,s')\mapstokas')\\ lift:&MA\rarrS\rarrM(A x S)=m\mapstos\mapstobindm(a\mapstoreturn(a,s))\end{array}

The writer monad transformer

Given any monad

MA

, the writer monad transformer

M(W x A)

(where is endowed with a monoid operation with identity element

\varepsilon

) is defined by:

\begin{array}{ll} return:&A\rarrM(W x A)=a\mapstoreturn(\varepsilon,a)\\ bind:&M(W x A)\rarr(A\rarrM(W x B))\rarrM(W x B)=m\mapstof\mapstobindm((w,a)\mapstobind(fa)((w',b)\mapstoreturn(w*w',b)))\\ lift:&MA\rarrM(W x A)=m\mapstobindm(a\mapstoreturn(\varepsilon,a))\\ \end{array}

The continuation monad transformer

Given any monad

MA

, the continuation monad transformer maps an arbitrary type into functions of type

(A\rarrMR)\rarrMR

, where is the result type of the continuation. It is defined by:

\begin{array}{ll} return\colon&A\rarr\left(A\rarrMR\right)\rarrMR=a\mapstok\mapstoka\\ bind\colon&\left(\left(A\rarrMR\right)\rarrMR\right)\rarr\left(A\rarr\left(B\rarrMR\right)\rarrMR\right)\rarr\left(B\rarrMR\right)\rarrMR=c\mapstof\mapstok\mapstoc\left(a\mapstofak\right)\\ lift\colon&MA\rarr(A\rarrMR)\rarrMR=bind\end{array}

Note that monad transformations are usually not commutative: for instance, applying the state transformer to the option monad yields a type

S\rarr\left(A x S\right)?

(a computation which may fail and yield no final state), whereas the converse transformation has type

S\rarr\left(A? x S\right)

(a computation which yields a final state and an optional return value).

See also

External links

Notes and References

  1. Sheng . Liang . Hudak, Paul . Jones, Mark . Monad transformers and modular interpreters . Proceedings of the 22nd ACM SIGPLAN-SIGACT symposium on Principles of programming languages . 333–343 . ACM . 1995 . New York, NY . PDF . 10.1145/199448.199528. free .