Monad transformer

From testwiki
Jump to navigation Jump to search

Template:One source 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(A?) (where A? denotes the option type) is defined by:

return:A→M(A?)a↦return(Justa)bind:M(A?)→(A→M(B?))→M(B?)m↦f↦bindm(a↦{return Nothingif a=Nothingfa′if a=Justa′)lift:M(A)→M(A?)m↦bindm(a↦return(Justa))

The exception monad transformer

Given any monad MA, the exception monad transformer M(A+E) (where Template:Mvar is the type of exceptions) is defined by:

return:A→M(A+E)a↦return(valuea)bind:M(A+E)→(A→M(B+E))→M(B+E)m↦f↦bindm(a↦{return err eif a=errefa′if a=valuea′)lift:MA→M(A+E)m↦bindm(a↦return(valuea))

The reader monad transformer

Given any monad MA, the reader monad transformer E→MA (where Template:Mvar is the environment type) is defined by:

return:A→E→MAa↦e↦returnabind:(E→MA)→(A→E→MB)→E→MBm↦k↦e↦bind(me)(a↦kae)lift:MA→E→MAa↦e↦a

The state monad transformer

Given any monad MA, the state monad transformer S→M(A×S) (where Template:Mvar is the state type) is defined by:

return:A→S→M(A×S)a↦s↦return(a,s)bind:(S→M(A×S))→(A→S→M(B×S))→S→M(B×S)m↦k↦s↦bind(ms)((a,s′)↦kas′)lift:MA→S→M(A×S)m↦s↦bindm(a↦return(a,s))

The writer monad transformer

Given any monad MA, the writer monad transformer M(W×A) (where Template:Mvar is endowed with a monoid operation Template:Math with identity element ε) is defined by:

return:A→M(W×A)a↦return(ε,a)bind:M(W×A)→(A→M(W×B))→M(W×B)m↦f↦bindm((w,a)↦bind(fa)((w′,b)↦return(w∗w′,b)))lift:MA→M(W×A)m↦bindm(a↦return(ε,a))

The continuation monad transformer

Given any monad MA, the continuation monad transformer maps an arbitrary type Template:Mvar into functions of type (A→MR)→MR, where Template:Mvar is the result type of the continuation. It is defined by:

return:A→(A→MR)→MRa↦k↦kabind:((A→MR)→MR)→(A→(B→MR)→MR)→(B→MR)→MRc↦f↦k↦c(a↦fak)lift:MA→(A→MR)→MRbind

Note that monad transformations are usually not commutative: for instance, applying the state transformer to the option monad yields a type S→(A×S)? (a computation which may fail and yield no final state), whereas the converse transformation has type S→(A?×S) (a computation which yields a final state and an optional return value).

See also

References

Template:Reflist

Template:Wikibooks

Template:Expand section