# Monad Transformers

### From HaskellWiki

(Difference between revisions)

(Added links) |
RossPaterson (Talk | contribs) (update for mtl-2) |
||

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

− | * [http://hackage.haskell.org/package/ | + | * [http://hackage.haskell.org/package/transformers transformers]: provides the classes <hask>MonadTrans</hask> and <hask>MonadIO</hask>, as well as concrete monad transformers such as <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/mtl mtl] (Monad Transformer Library) comes in two versions: | |

− | + | ** version 1 was the first implementation, containing the classes <hask>MonadTrans</hask> and <hask>MonadIO</hask>, concrete monad transformers such as <hask>StateT</hask> and [[multi-parameter type class]]es with [[functional dependencies]] such as <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>. This version is now obsolete. | |

− | + | ** 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 <hask>MonadState</hask>. | |

− | ** [http://hackage.haskell.org/package/monads-tf monads-tf]: Provides a different abstraction using [[type families]]. Unfortunately the names of <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. | ||

+ | * [http://hackage.haskell.org/package/monads-tf monads-tf]: Provides a different abstraction using [[type families]]. Unfortunately the module names of <code>mtl</code> and <code>monads-tf</code> clash, so you can currently not import both packages in one package. | ||

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

## Revision as of 13:34, 15 December 2010

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 andMonadTrans, as well as concrete monad transformers such asMonadIO. 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
- mtl (Monad Transformer Library) comes in two versions:
- version 1 was the first implementation, containing the classes andMonadTrans, concrete monad transformers such asMonadIOand multi-parameter type classes with functional dependencies such asStateT. Monads likeMonadStateand their transformer counterparts likeStateare distinct types and can be accessed uniformly only through a type class abstraction likeStateT. This version is now obsolete.MonadState
- 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.
- 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 |

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

## 2 Shall I use MTL or transformers?

Transformers is Haskell 98 and thus more portable.

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

import Control.Monad.State ...

import Control.Monad.Trans.State ...

State

State

state

runState

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