The Monad Transformer Library
The developer of this repository has not created any items for sale yet. Need a bug fixed? Help with integration? A different license? Create a request here:
MTL is a collection of monad classes, extending the
transformerspackage, using functional dependencies for generic lifting of monadic actions.
Transformers in MTL are divided into classes and data types. Classes define the monadic operations of transformers. Data types, generally from the
transformerspackage, implement transformers, and MTL provides instances for all the transformer type classes.
transformersuse a common module, data type, and function naming scheme. As an example, let's imagine we have a transformer
Control.Monad.Foomodule, we'd find:
MonadFoowith the transformer operations.
FooTwith instances for all monad transformer classes.
runFooT. For the actual transformers, there are usually a number of useful runner functions.
When using monad transformers, you often need to "lift" a monadic action into your transformed monadic action. This is done using the
lift :: (Monad m, MonadTrans t) => m a -> t m a
m ais lifted into the transformer action
t m a.
As an example, here we lift an action of type
IO ainto an action of type
ExceptT MyError IO a:
data MyError = EmptyLine
mightFail :: ExceptT MyError IO () mightFail = do l
The following outlines the available monad classes and transformers in MTL andtransformers. For more details, and the corresponding documentation of themtlversion you are using, see the documentation on Hackage.
The Continuation monad transformer adds the ability to use continuation-passing style (CPS) in a monadic computation. Continuations can be used to manipulate the control flow of a program, e.g. early exit, error handling, or suspending a computation.
The Error monad transformer has been deprecated in favor of
The Except monad transformer adds the ability to fail with an error in a monadic computation.
The Identity monad transformer does not add any abilities to a monad. It simply applies the bound function to its inner monad without any modification.
This transformer combines the list monad with another monad. Note that this does not yield a monad unless the inner monad is commutative.
A convenient transformer that combines the Reader, Writer, and State monad transformers.
Control.Monad.RWS.Lazy.RWST(which is the default, exported by
The Reader monad transformer represents a computation which can read values from an environment.
The State monad transformer represents a computation which can read and write internal state values. If you only need to read values, you might want to use Reader instead.
Control.Monad.State.Lazy.StateT(the default, exported by
The Writer monad transformer represents a computation that can produce a stream of data in addition to the computed values. This can be used to collect values in some data structure with a
Monoidinstance. This can be used for things like logging and accumulating values throughout a computation.