haskell / mtl

The Monad Transformer Library
http://www.haskell.org/haskellwiki/Monad_Transformers
Other
360 stars 63 forks source link
haskell monad monad-transformers

mtl Hackage Build Status

MTL is a collection of monad classes, extending the transformers package, using functional dependencies for generic lifting of monadic actions.

Structure

Transformers in MTL are divided into classes and data types. Classes define the monadic operations of transformers. Data types, generally from the transformers package, implement transformers, and MTL provides instances for all the transformer type classes.

MTL and transformers use a common module, data type, and function naming scheme. As an example, let's imagine we have a transformer Foo.

In the Control.Monad.Foo module, we'd find:

Lifting

When using monad transformers, you often need to "lift" a monadic action into your transformed monadic action. This is done using the lift function from MonadTrans in the Control.Monad.Trans.Class module:

lift :: (Monad m, MonadTrans t) => m a -> t m a

The action m a is lifted into the transformer action t m a.

As an example, here we lift an action of type IO a into an action of type ExceptT MyError IO a:

data MyError = EmptyLine

mightFail :: ExceptT MyError IO ()
mightFail = do
  l <- lift getLine
  when (null l) (throwError EmptyLine)

Transformers

The following outlines the available monad classes and transformers in MTL and transformers. For more details, and the corresponding documentation of the mtl version you are using, see the documentation on Hackage.

Resources