# Difference between revisions of "Monad Transformers"

(FAQ) |
(Added links) |
||

Line 28: | Line 28: | ||

* [[Monad Transformers Explained]] |
* [[Monad Transformers Explained]] |
||

+ | * [http://www.grabmueller.de/martin/www/pub/Transformers.pdf Monad Transformers Step by Step] (PDF) |
||

+ | * [http://www.haskell.org/all_about_monads/html/ All About Monads] |
||

* http://www.haskell.org/pipermail/libraries/2009-March/011415.html |
* 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/libraries/2009-December/012914.html |

## Revision as of 07:25, 30 October 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
`StateT`

and abstractions using multi-parameter type classes with functional dependencies like`MonadState`

. Monads like`State`

and their transformer counterparts like`StateT`

are distinct types and can be accessed uniformly only through a type class abstraction like`MonadState`

. Because of the functional dependencies, MTL can currently (2010-03) only used in Hugs and GHC. MTL was the first implementation. - The newer implementation is derived from the former one and is split into the following components:
- transformers: Provide only concrete transformers like
`StateT`

. The monad`State s a`

is only a type synonym for`StateT s Identity a`

. Thus both`State`

and`StateT`

can be accessed by the same methods like`put`

and`get`

. However, this only works, if`StateT`

is the top-most transformer in a monad transformer stack. This package is Haskell 98 and thus can be also used with JHC. - monads-fd: Provides the same type classes with functional dependencies like MTL. They allow using
`State`

methods also for`StateT`

transformers within a transformer stack. - monads-tf: Provides a different abstraction using type families. Unfortunately the names of
`monads-fd`

and`monads-tf`

clash, thus you can currently not import both packages in one package.

- transformers: Provide only concrete transformers like

## Contents

## How can I use MTL and transformers together?

Q: When I use ghc or ghci it complains about the same module names in mtl and transformers or monads-fd. How can I resolve these name clashes?
A: You can use the `-hide-package`

option of GHC. Cabal uses the `-hide-all-packages`

option and then explicitly makes every package visible with `-package`

.

## Shall I use MTL or transformers?

Transformers is Haskell 98 and thus more portable.

## How to move from MTL to transformers?

Many package using `MTL`

can be ported to `transformers`

with only slight modifications.
Modules require the `Trans`

infix,
e.g. `import Control.Monad.State ...`

must be replaced by `import Control.Monad.Trans.State ...`

.
Since `State`

is only a type synonym, there is no longer a constructor named `State`

.
For constructing you must use the function `state`

and instead of matching patterns you must call `runState`

.

## See also

- Monad Transformers Explained
- Monad Transformers Step by Step (PDF)
- All About Monads
- 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