Monad Transformers
From HaskellWiki
(Difference between revisions)
(initial version with links to many discussions of MTL and Transformers) |
(explanation of transformers set of packages) |
||
| Line 2: | Line 2: | ||
besides a third package with a similar goal but different API named [[MonadLib]]: | besides a third package with a similar goal but different API named [[MonadLib]]: | ||
| - | * [http://hackage.haskell.org/package/mtl MTL] - Monad Transformers Library: provides concrete monad transformers like <hask>StateT</hask> and abstractions using multi-parameter | + | * [http://hackage.haskell.org/package/mtl MTL] - Monad Transformers Library: provides concrete monad transformers like <hask>StateT</hask> and abstractions using [[multi-parameter type class]]es with [[functional dependencies]] like <hask>MonadState</hask>. Monads like <hask>State</hask> and their transformer counterparts like <hask>StateT</hask> are distinct types and can be accessed uniformly only through a type class abstraction like <hask>MonadState</hask>. Because of the functional dependencies, MTL can currently (2010-03) only used in [[Hugs]] and [[GHC]]. MTL was the first implementation. |
| - | * [http://hackage.haskell.org/package/transformers transformers] | + | * The newer implementation is derived from the former one and is split into the following components: |
| + | ** [http://hackage.haskell.org/package/transformers transformers]: Provide only concrete transformers like <hask>StateT</hask>. The monad <hask>State s a</hask> is only a type synonym for <hask>StateT s Identity a</hask>. Thus both <hask>State</hask> and <hask>StateT</hask> can be accessed by the same methods like <hask>put</hask> and <hask>get</hask>. However, this only works, if <hask>StateT</hask> is the top-most transformer in a monad transformer stack. This package is Haskell 98 and thus can be also used with [[JHC]]. | ||
| + | ** [http://hackage.haskell.org/package/monads-fd monads-fd]: Provides the same type classes with functional dependencies like MTL. They allow using <hask>State</hask> methods also for <hask>StateT</hask> transformers within a transformer stack. | ||
| + | ** [http://hackage.haskell.org/package/monads-tf monads-tf]: Provides a different abstraction using [[type families]]. Unfortunately the names of <code>monads-fd</code> and <code>monads-tf</code> clash, | ||
| + | thus you can currently not import both packages in one package. | ||
| + | |||
| + | |||
| + | == How can I use MTL and transformers together? == | ||
| + | |||
| + | == Shall I use MTL or transformers? == | ||
| + | |||
| + | == How to move from MTL to transformers? == | ||
| + | |||
| + | Any package using <code>MTL</code> can be ported to <code>transformers</code> and <code>monads-fd</code> with only slight variations. | ||
| + | Modules require the <code>Trans</code> infix, | ||
| + | e.g. <hask>import Control.Monad.State ...</hask> must be replaced by <hask>import Control.Monad.Trans.State ...</hask>. | ||
| + | Since <hask>State</hask> is only a type synonym, there is no longer a constructor named <hask>State</hask>. | ||
| + | For constructing you must use the function <hask>state</hask> and instead of pattern matching you must call <hask>runState</hask>. | ||
== See also == | == See also == | ||
Revision as of 22:58, 5 March 2010
There are currently two sets of packages that implement similar interfaces to monad transformers, besides a third package with a similar goal but different API named MonadLib:
- MTL - Monad Transformers Library: provides concrete monad transformers like and abstractions using multi-parameter type classes with functional dependencies likeStateT. Monads likeMonadStateand their transformer counterparts likeStateare distinct types and can be accessed uniformly only through a type class abstraction likeStateT. Because of the functional dependencies, MTL can currently (2010-03) only used in Hugs and GHC. MTL was the first implementation.MonadState
- The newer implementation is derived from the former one and is split into the following components:
- transformers: Provide only concrete transformers like . The monadStateTis only a type synonym forState s a. Thus bothStateT s Identity aandStatecan be accessed by the same methods likeStateTandput. However, this only works, ifgetis the top-most transformer in a monad transformer stack. This package is Haskell 98 and thus can be also used with JHC.StateT
- monads-fd: Provides the same type classes with functional dependencies like MTL. They allow using methods also forStatetransformers within a transformer stack.StateT
- monads-tf: Provides a different abstraction using type families. Unfortunately the names of
monads-fdandmonads-tfclash,
- transformers: Provide only concrete transformers like
thus you can currently not import both packages in one package.
Contents |
1 How can I use MTL and transformers together?
2 Shall I use MTL or transformers?
3 How to move from MTL to transformers?
Any package using MTL can be ported to transformers and monads-fd with only slight variations.
Modules require the Trans infix,
import Control.Monad.State ...
import Control.Monad.Trans.State ...
State
State
state
runState
4 See also
- Monad Transformers Explained
- http://www.haskell.org/pipermail/libraries/2009-March/011415.html
- http://www.haskell.org/pipermail/libraries/2009-December/012914.html
- http://www.haskell.org/pipermail/haskell-cafe/2010-January/071842.html
- http://www.mail-archive.com/debian-haskell@lists.debian.org/msg01241.html
Categories: Packages | Monad | FAQ
