# Difference between revisions of "Monad Transformers"

(FAQ) |
(→See also: no domain catamorph.de anymore) |
||

(8 intermediate revisions by 5 users not shown) | |||

Line 1: | Line 1: | ||

− | There are currently |
+ | There are currently several packages that implement similar interfaces to [[Monad Transformers Explained|monad transformers]] (besides an additional 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/transformers transformers]: provides the classes <code>MonadTrans</code> and <code>MonadIO</code>, as well as concrete monad transformers such as <code>StateT</code>. The monad <code>State s a</code> is only a type synonym for <code>StateT s Identity a</code>. Thus both <code>State</code> and <code>StateT</code> can be accessed by the same methods like <code>put</code> and <code>get</code>. However, this only works if <code>StateT</code> is the top-most transformer in a monad transformer stack. This package is Haskell 98 and thus can be also used with [[JHC]]. |
||

− | * The newer implementation is derived from the former one and is split into the following components: |
||

+ | * [http://hackage.haskell.org/package/mtl mtl] (Monad Transformer Library) comes in two versions: |
||

⚫ | |||

⚫ | ** version 1 was the first implementation, containing the classes <code>MonadTrans</code> and <code>MonadIO</code>, concrete monad transformers such as <code>StateT</code> and [[multi-parameter type class]]es with [[functional dependencies]] such as <code>MonadState</code>. Monads like <code>State</code> and their transformer counterparts like <code>StateT</code> are distinct types and can be accessed uniformly only through a type class abstraction like <code>MonadState</code>. This version is now obsolete. |
||

− | ** [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. |
||

+ | ** version 2 re-exports the classes and monad transformers of the transformers package, and adds [[multi-parameter type class]]es with [[functional dependencies]] such as <code>MonadState</code>. |
||

⚫ | |||

+ | *:Version 2 of the MTL has some small [[Incompatibilities between MTL 1 and MTL 2|incompatibilities]] relative to version 1. See "[[Upgrading from MTL 1 to MTL 2]]" for instructions on how to make code written for version 1 work with version 2. |
||

+ | :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/monads-fd monads-fd]: this was the prototype of the new mtl implementation. It is now obsolete, and simply re-exports mtl version 2. |
||

⚫ | |||

== How can I use MTL and transformers together? == |
== 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? |
||

+ | MTL and transformers use different module names, but share common classes, type constructors and functions, so they are fully compatible. |
||

− | A: You can use the <code>-hide-package</code> option of GHC. [[Cabal]] uses the <code>-hide-all-packages</code> option and then explicitly makes every package visible with <code>-package</code>. |
||

== Shall I use MTL or transformers? == |
== Shall I use MTL or transformers? == |
||

− | Transformers is Haskell 98 and thus more portable. |
+ | Transformers is Haskell 98 and thus more portable, and doesn't tie you to functional dependencies. |

+ | But because it lacks the monad classes, you'll have to lift operations to the composite monad yourself ([http://hackage.haskell.org/packages/archive/transformers/latest/doc/html/Control-Monad-Trans-Class.html#g:2 examples]). |
||

== How to move from MTL to transformers? == |
== How to move from MTL to transformers? == |
||

Line 20: | Line 22: | ||

Many package using <code>MTL</code> can be ported to <code>transformers</code> with only slight modifications. |
Many package using <code>MTL</code> can be ported to <code>transformers</code> with only slight modifications. |
||

Modules require the <code>Trans</code> infix, |
Modules require the <code>Trans</code> infix, |
||

− | e.g. < |
+ | e.g. <code>import Control.Monad.State ...</code> must be replaced by <code>import Control.Monad.Trans.State ...</code>. |

− | Since < |
+ | Since <code>State</code> is only a type synonym, there is no longer a constructor named <code>State</code>. |

− | For constructing you must use the function < |
+ | For constructing you must use the function <code>state</code> and instead of matching patterns you must call <code>runState</code>. |

== See also == |
== See also == |
||

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

+ | * [https://page.mi.fu-berlin.de/scravy/realworldhaskell/materialien/monad-transformers-step-by-step.pdf Monad Transformers Step by Step] |
||

+ | * [[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 |

## Latest revision as of 19:37, 17 June 2018

There are currently several packages that implement similar interfaces to monad transformers (besides an additional package with a similar goal but different API named MonadLib):

- transformers: provides the classes
`MonadTrans`

and`MonadIO`

, as well as concrete monad transformers such as`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. - mtl (Monad Transformer Library) comes in two versions:
- version 1 was the first implementation, containing the classes
`MonadTrans`

and`MonadIO`

, concrete monad transformers such as`StateT`

and multi-parameter type classes with functional dependencies such as`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`

. This version is now obsolete. - version 2 re-exports the classes and monad transformers of the transformers package, and adds multi-parameter type classes with functional dependencies such as
`MonadState`

.

- Version 2 of the MTL has some small incompatibilities relative to version 1. See "Upgrading from MTL 1 to MTL 2" for instructions on how to make code written for version 1 work with version 2.

- version 1 was the first implementation, containing the classes

- Because of the functional dependencies, MTL can currently (2010-03) only used in Hugs and GHC. MTL was the first implementation.

- monads-fd: this was the prototype of the new mtl implementation. It is now obsolete, and simply re-exports mtl version 2.
- monads-tf: Provides a different abstraction using type families. Unfortunately the module names of
`mtl`

and`monads-tf`

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

## Contents

## How can I use MTL and transformers together?

MTL and transformers use different module names, but share common classes, type constructors and functions, so they are fully compatible.

## Shall I use MTL or transformers?

Transformers is Haskell 98 and thus more portable, and doesn't tie you to functional dependencies. But because it lacks the monad classes, you'll have to lift operations to the composite monad yourself (examples).

## 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
- 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