Monad transformer

From testwiki
Revision as of 07:36, 22 April 2024 by imported>OAbot (Open access bot: doi updated in citation with #oabot.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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:AM(A?)=areturn(Justa)bind:M(A?)(AM(B?))M(B?)=mfbindm(a{return Nothingif a=Nothingfaif a=Justa)lift:M(A)M(A?)=mbindm(areturn(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:AM(A+E)=areturn(valuea)bind:M(A+E)(AM(B+E))M(B+E)=mfbindm(a{return err eif a=errefaif a=valuea)lift:MAM(A+E)=mbindm(areturn(valuea))

The reader monad transformer

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

return:AEMA=aereturnabind:(EMA)(AEMB)EMB=mkebind(me)(akae)lift:MAEMA=aea

The state monad transformer

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

return:ASM(A×S)=asreturn(a,s)bind:(SM(A×S))(ASM(B×S))SM(B×S)=mksbind(ms)((a,s)kas)lift:MASM(A×S)=msbindm(areturn(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:AM(W×A)=areturn(ε,a)bind:M(W×A)(AM(W×B))M(W×B)=mfbindm((w,a)bind(fa)((w,b)return(w*w,b)))lift:MAM(W×A)=mbindm(areturn(ε,a))

The continuation monad transformer

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

return:A(AMR)MR=akkabind:((AMR)MR)(A(BMR)MR)(BMR)MR=cfkc(afak)lift:MA(AMR)MR=bind

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