Jump to content

Monad transformer

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Classicalecon (talk | contribs) at 16:51, 1 January 2010 (Examples). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

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 , the option monad transformer (where denotes the option type) is defined by:

The exception monad transformer

Given any monad , the exception monad transformer (where is the type of exceptions) is defined by:

See also

References

  1. ^ Liang, Sheng (1995). "Monad transformers and modular interpreters" (PDF). Proceedings of the 22nd ACM SIGPLAN-SIGACT symposium on Principles of programming languages. New York, NY: ACM. pp. 333–343. doi:10.1145/199448.199528. {{cite conference}}: Unknown parameter |booktitle= ignored (|book-title= suggested) (help); Unknown parameter |coauthors= ignored (|author= suggested) (help)
  • [1] - a highly technical blog post briefly reviewing some of the literature on monad transformers and related concepts, with a focus on categorical-theoretic treatment