fbpx
Wikipedia

Monad transformer

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 edit

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 edit

The option monad transformer edit

Given any monad  , the option monad transformer   (where   denotes the option type) is defined by:

 

The exception monad transformer edit

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

 

The reader monad transformer edit

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

 

The state monad transformer edit

Given any monad  , the state monad transformer   (where S is the state type) is defined by:

 

The writer monad transformer edit

Given any monad  , the writer monad transformer   (where W is endowed with a monoid operation with identity element  ) is defined by:

 

The continuation monad transformer edit

Given any monad  , the continuation monad transformer maps an arbitrary type R into functions of type  , where R is the result type of the continuation. It is defined by:

 

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

See also edit

References edit

  1. ^ Liang, Sheng; Hudak, Paul; Jones, Mark (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.

External links edit

  • A highly technical blog post briefly reviewing some of the literature on monad transformers and related concepts, with a focus on categorical-theoretic treatment

monad, transformer, this, article, relies, largely, entirely, single, source, relevant, discussion, found, talk, page, please, help, improve, this, article, introducing, citations, additional, sources, find, sources, news, newspapers, books, scholar, jstor, no. This article relies largely or entirely on a single source Relevant discussion may be found on the talk page Please help improve this article by introducing citations to additional sources Find sources Monad transformer news newspapers books scholar JSTOR November 2023 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 Contents 1 Definition 2 Examples 2 1 The option monad transformer 2 2 The exception monad transformer 2 3 The reader monad transformer 2 4 The state monad transformer 2 5 The writer monad transformer 2 6 The continuation monad transformer 3 See also 4 References 5 External linksDefinition editA monad transformer consists of A type constructor t of kind gt gt gt Monad operations return and bind or an equivalent formulation for all t m where m is a monad satisfying the monad laws An additional operation lift m a gt t m a satisfying the following laws 1 the notation bind below indicates infix application lift return return lift m bind k lift m bind lift k Examples editThe option monad transformer edit Given any monad M A displaystyle mathrm M A nbsp the option monad transformer M A displaystyle mathrm M left A right nbsp where A displaystyle A nbsp denotes the option type is defined by r e t u r n A M A a r e t u r n J u s t a b i n d M A A M B M B m f b i n d m a return Nothing if a N o t h i n g f a if a J u s t a l i f t M A M A m b i n d m a r e t u r n J u s t a displaystyle begin array ll mathrm return amp A rightarrow mathrm M left A right a mapsto mathrm return mathrm Just a mathrm bind amp mathrm M left A right rightarrow left A rightarrow mathrm M left B right right rightarrow mathrm M left B right m mapsto f mapsto mathrm bind m left a mapsto begin cases mbox return Nothing amp mbox if a mathrm Nothing f a amp mbox if a mathrm Just a end cases right mathrm lift amp mathrm M A rightarrow mathrm M left A right m mapsto mathrm bind m a mapsto mathrm return mathrm Just a end array nbsp The exception monad transformer edit Given any monad M A displaystyle mathrm M A nbsp the exception monad transformer M A E displaystyle mathrm M A E nbsp where E is the type of exceptions is defined by r e t u r n A M A E a r e t u r n v a l u e a b i n d M A E A M B E M B E m f b i n d m a return err e if a e r r e f a if a v a l u e a l i f t M A M A E m b i n d m a r e t u r n v a l u e a displaystyle begin array ll mathrm return amp A rightarrow mathrm M A E a mapsto mathrm return mathrm value a mathrm bind amp mathrm M A E rightarrow A rightarrow mathrm M B E rightarrow mathrm M B E m mapsto f mapsto mathrm bind m left a mapsto begin cases mbox return err e amp mbox if a mathrm err e f a amp mbox if a mathrm value a end cases right mathrm lift amp mathrm M A rightarrow mathrm M A E m mapsto mathrm bind m a mapsto mathrm return mathrm value a end array nbsp The reader monad transformer edit Given any monad M A displaystyle mathrm M A nbsp the reader monad transformer E M A displaystyle E rightarrow mathrm M A nbsp where E is the environment type is defined by r e t u r n A E M A a e r e t u r n a b i n d E M A A E M B E M B m k e b i n d m e a k a e l i f t M A E M A a e a displaystyle begin array ll mathrm return amp A rightarrow E rightarrow mathrm M A a mapsto e mapsto mathrm return a mathrm bind amp E rightarrow mathrm M A rightarrow A rightarrow E rightarrow mathrm M B rightarrow E rightarrow mathrm M B m mapsto k mapsto e mapsto mathrm bind m e a mapsto k a e mathrm lift amp mathrm M A rightarrow E rightarrow mathrm M A a mapsto e mapsto a end array nbsp The state monad transformer edit Given any monad M A displaystyle mathrm M A nbsp the state monad transformer S M A S displaystyle S rightarrow mathrm M A times S nbsp where S is the state type is defined by r e t u r n A S M A S a s r e t u r n a s b i n d S M A S A S M B S S M B S m k s b i n d m s a s k a s l i f t M A S M A S m s b i n d m a r e t u r n a s displaystyle begin array ll mathrm return amp A rightarrow S rightarrow mathrm M A times S a mapsto s mapsto mathrm return a s mathrm bind amp S rightarrow mathrm M A times S rightarrow A rightarrow S rightarrow mathrm M B times S rightarrow S rightarrow mathrm M B times S m mapsto k mapsto s mapsto mathrm bind m s a s mapsto k a s mathrm lift amp mathrm M A rightarrow S rightarrow mathrm M A times S m mapsto s mapsto mathrm bind m a mapsto mathrm return a s end array nbsp The writer monad transformer edit Given any monad M A displaystyle mathrm M A nbsp the writer monad transformer M W A displaystyle mathrm M W times A nbsp where W is endowed with a monoid operation with identity element e displaystyle varepsilon nbsp is defined by r e t u r n A M W A a r e t u r n e a b i n d M W A A M W B M W B m f b i n d m w a b i n d f a w b r e t u r n w w b l i f t M A M W A m b i n d m a r e t u r n e a displaystyle begin array ll mathrm return amp A rightarrow mathrm M W times A a mapsto mathrm return varepsilon a mathrm bind amp mathrm M W times A rightarrow A rightarrow mathrm M W times B rightarrow mathrm M W times B m mapsto f mapsto mathrm bind m w a mapsto mathrm bind f a w b mapsto mathrm return w w b mathrm lift amp mathrm M A rightarrow mathrm M W times A m mapsto mathrm bind m a mapsto mathrm return varepsilon a end array nbsp The continuation monad transformer edit Given any monad M A displaystyle mathrm M A nbsp the continuation monad transformer maps an arbitrary type R into functions of type A M R M R displaystyle A rightarrow mathrm M R rightarrow mathrm M R nbsp where R is the result type of the continuation It is defined by r e t u r n A A M R M R a k k a b i n d A M R M R A B M R M R B M R M R c f k c a f a k l i f t M A A M R M R b i n d displaystyle begin array ll mathrm return colon amp A rightarrow left A rightarrow mathrm M R right rightarrow mathrm M R a mapsto k mapsto k a mathrm bind colon amp left left A rightarrow mathrm M R right rightarrow mathrm M R right rightarrow left A rightarrow left B rightarrow mathrm M R right rightarrow mathrm M R right rightarrow left B rightarrow mathrm M R right rightarrow mathrm M R c mapsto f mapsto k mapsto c left a mapsto f a k right mathrm lift colon amp mathrm M A rightarrow A rightarrow mathrm M R rightarrow mathrm M R mathrm bind end array nbsp Note that monad transformations are usually not commutative for instance applying the state transformer to the option monad yields a type S A S displaystyle S rightarrow left A times S right nbsp a computation which may fail and yield no final state whereas the converse transformation has type S A S displaystyle S rightarrow left A times S right nbsp a computation which yields a final state and an optional return value See also editMonads in functional programmingReferences edit Liang Sheng Hudak Paul Jones Mark 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 External links edit nbsp The Wikibook Haskell has a page on the topic of Monad transformers A highly technical blog post briefly reviewing some of the literature on monad transformers and related concepts with a focus on categorical theoretic treatmentThis section needs expansion You can help by adding to it May 2008 Retrieved from https en wikipedia org w index php title Monad transformer amp oldid 1183957356, wikipedia, wiki, book, books, library,

article

, read, download, free, free download, mp3, video, mp4, 3gp, jpg, jpeg, gif, png, picture, music, song, movie, book, game, games.