mtl (original) (raw)
mtl: Monad classes for transformers, using functional dependencies
MTL is a collection of monad classes, extending the transformers
package, using functional dependencies for generic lifting of monadic actions.
Modules
[Index] [Quick Jump]
- Control
- Monad
* Control.Monad.Accum
* Control.Monad.Cont
* Control.Monad.Cont.Class
* Error
* Control.Monad.Error.Class
* Control.Monad.Except
* Control.Monad.Identity
* Control.Monad.RWS
* Control.Monad.RWS.CPS
* Control.Monad.RWS.Class
* Control.Monad.RWS.Lazy
* Control.Monad.RWS.Strict
* Control.Monad.Reader
* Control.Monad.Reader.Class
* Control.Monad.Select
* Control.Monad.State
* Control.Monad.State.Class
* Control.Monad.State.Lazy
* Control.Monad.State.Strict
* Control.Monad.Trans
* Control.Monad.Writer
* Control.Monad.Writer.CPS
* Control.Monad.Writer.Class
* Control.Monad.Writer.Lazy
* Control.Monad.Writer.Strict
- Monad
Versions [RSS] | 1.0, 1.1.0.0, 1.1.0.1, 1.1.0.2, 1.1.1.0, 1.1.1.1, 2.0.0.0, 2.0.1.0, 2.0.1.1, 2.1, 2.1.1, 2.1.2, 2.1.3.1, 2.2, 2.2.0.1, 2.2.1, 2.2.2, 2.3, 2.3.1 (info) | ||||
---|---|---|---|---|---|
Change log | CHANGELOG.markdown | ||||
Dependencies | base (>=4.12 && <4.15 | | >=4.16 && <5), transformers (>=0.5.6 && <0.7) [details] | |||
Tested with | ghc ==8.6.5 | | ==8.8.4 | ==8.10.7 | ||
License | BSD-3-Clause | ||||
Author | Andy Gill | ||||
Maintainer | chessai chessai1996@gmail.com, Emily Pillmore emilypi@cohomolo.gy, Koz Ross koz.ross@retro-freedom.nz | ||||
Revised | Revision 1 made by phadej at 2022-12-27T18:47:14Z | ||||
Category | Control | ||||
Home page | http://github.com/haskell/mtl | ||||
Bug tracker | http://github.com/haskell/mtl/issues | ||||
Source repo | head: git clone https://github.com/haskell/mtl.git | ||||
Uploaded | by topos at 2022-10-31T21:28:04Z | ||||
Distributions | Arch:2.2.2, Fedora:2.3.1, FreeBSD:2.2.1 | ||||
Reverse Dependencies | 4171 direct, 11208 indirect [details] | ||||
Downloads | 491142 total (18 in the last 30 days) | ||||
Rating | 2.75 (votes: 21)[estimated by Bayesian average] | ||||
Your Rating | λ λ λ | ||||
Status | Docs available [build log]Last success reported on 2022-10-31 [all 1 reports] |
Readme for mtl-2.3.1
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 transformerFoo
.
In the Control.Monad.Foo
module, we'd find:
- A type class
MonadFoo
with the transformer operations. - A data type
FooT
with instances for all monad transformer classes. - Functions to run the transformed computation, e.g.
runFooT
. For the actual transformers, there are usually a number of useful runner functions.
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.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.
Control.Monad.Cont
The Continuation monad transformer adds the ability to usecontinuation-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.- Class:
Control.Monad.Cont.Class.MonadCont
- Transformer:
Control.Monad.Cont.ContT
- Class:
Control.Monad.Error
(deprecated!)
The Error monad transformer has been deprecated in favor ofControl.Monad.Except
.Control.Monad.Except
The Except monad transformer adds the ability to fail with an error in a monadic computation.- Class:
Control.Monad.Except.Class.MonadError
- Transformer:
Control.Monad.Except.ExceptT
- Class:
Control.Monad.Identity
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.- Transformer:
Control.Monad.Trans.Identity.IdentityT
(in thetransformers
package) - Identity functor and monad:
Data.Functor.Identity.Identity
(in thebase
package)
- Transformer:
Control.Monad.RWS
A convenient transformer that combines the Reader, Writer, and State monad transformers.- Lazy transformer:
Control.Monad.RWS.Lazy.RWST
(which is the default, exported byControl.Monad.RWS
) - Strict transformer:
Control.Monad.RWS.Strict.RWST
- Lazy transformer:
Control.Monad.Reader
The Reader monad transformer represents a computation which can read values from an environment.- Class:
Control.Monad.Reader.Class.MonadReader
- Transformer:
Control.Monad.Reader.ReaderT
- Class:
Control.Monad.State
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 useReaderinstead.- Class:
Control.Monad.State.Class.MonadState
- Lazy transformer:
Control.Monad.State.Lazy.StateT
(the default, exported byControl.Monad.State
) - Strict transformer:
Control.Monad.State.Strict.StateT
- Class:
Control.Monad.Writer
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 aMonoid
instance. This can be used for things like logging and accumulating values throughout a computation.- Class:
Control.Monad.Writer.Class.MonadWriter
- Lazy transformers:
Control.Monad.Writer.Lazy.WriterT
- Strict transformers:
Control.Monad.Writer.Strict.WriterT
- Class:
Control.Monad.Accum
TheAccum
monad transformer represents a computation which manages append-only state, or a writer that can read all previous inputs. It binds a function to a monadic value by lazily accumulating subcomputations via(<>)
. For more general access, use State instead.- Class:
Control.Monad.Accum
- Transformer:
Control.Monad.Trans.Accum.AccumT
- Class:
Control.Monad.Select
TheSelect
monad transformer represents a computation which can do backtracking search using a 'ranked' evaluation strategy. Binding a function to a monad value chains together evaluation strategies in the sense that the results of previous strategies may influence subsequent rank and evaluation strategies in subcomputations.- Class:
Control.Monad.Select
- Transformer:
Control.Monad.Trans.Select.SelectT
- Class:
Resources
- mtl on Hackage
- The Monad Transformerschapter in "What I Wish I Knew When Learning Haskell".
- References:
- This package is inspired by the paper Functional Programming with Overloading and Higher-Order Polymorphism, by Mark P Jones, in Advanced School of Functional Programming, 1995 (http://web.cecs.pdx.edu/~mpj/pubs/springschool.html).