GitHub - haskell/mtl: The Monad Transformer Library (original) (raw)

MTL is a collection of monad classes, extending the transformerspackage, 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 transformerFoo.

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 thelift function from MonadTrans in the Control.Monad.Trans.Classmodule:

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