[[!toc]]
So far, we've defined monads as single-layered boxes. In practice, one often wants to combine the abilities of several monads. Corresponding to each monad like Reader, there's a corresponding ReaderT **monad transformer**. That takes an existing monad M and wraps Readerish monad packaging around it. The way these are defined parallels the way the single-layer versions are defined. For example, here's the Reader monad:
(* monadic operations for the Reader monad *)
type 'a reader =
env -> 'a;;
let mid (a : 'a) : 'a reader =
fun e -> a;;
let mbind (xx: 'a reader) (k : 'a -> 'b reader) : 'b reader =
fun e -> (fun x -> k x e) (xx e);;
We've just beta-expanded the familiar `k (xx e) e` into `(fun x -> k x e) (xx e)`. We did that so as to factor out the parts where any Reader monad is being supplied as an argument to another function. That will help make some patterns that are coming up more salient.
That was the plain Reader monad. Now if we want instead to wrap some other monad M inside Readerish packaging. How could we do it?
Well, one way to proceed would be to just let values of the other monad M be the `'a` in your `'a reader`. Then you could apply `reader_mbind` to get at the wrapped `'a M`, and then apply `M.bind` to get at *its* wrapped `'a`. This sometimes works.
But there are two problems: (1) It's cumbersome having to work with *both* `reader_mbind` and `M.bind`. It'd be nice to figure out some systematic way to connect the plumbing of the different monadic layers, so that we could have a *single* `mbind` that took our `'a M_inside_Reader`, and sequenced it with a single `'a -> 'b M_inside_Reader` function. Similarly for `mid`. This is what the ReaderT monad transformer will let us do.
(2) For some combinations of monads, the best way to implement a Tish monadic wrapper around an inner M monad won't be equivalent to either an `('a m) t` or an `('a t) m`. It will be a tighter intermingling of the two. So some natural activities will remain out of reach until we equip ourselves to go beyond `('a m) t`s and so on.
What we want in general are monadic transformers. For example, a ReaderT transformer will be parameterized not just on the type of its innermost contents `'a`, but also on the monadic box `M` that wraps `'a`. It will make use of monad `M`'s existing operations `M.unit` and `M.bind`, together with the Reader patterns for `mid` and `mbind`, to define a new monad ReaderT(M) that fuses the behavior of Reader and M.
To be clear: ReaderT isn't itself a monad. It's a kind of Readerish packaging (wrapping paper) that converts one monadic box M into another monadic box ReaderT(M).
Here's how it's implemented:
(* monadic operations for the ReaderT monadic transformer *)
(* We're not giving valid OCaml code, but rather something
* that's conceptually easier to digest.
* How you really need to write this in OCaml is more circuitous... *)
type 'a readerT(M) =
env -> 'a M;;
(* this _happens_ also to be the type of an ('a M) reader
* but as we noted, you can't rely on that pattern always to hold *)
let mid (a : 'a) : 'a readerT(M) =
fun e -> M.unit a;;
let mbind (xx : 'a readerT(M)) (k : 'a -> 'b readerT(M)) : 'b readerT(M) =
fun e -> M.bind (xx e) (fun x -> k x e);;
Notice the key differences: where before `mid` was implemented by a function that just returned `a`, now we
instead return `M.unit a`. Where before `mbind` just supplied value `xx e`
of type `'a reader` as an argument to a function, now we instead
`M.bind` the corresponding value to the function. Notice also the differences
in the types.
What is the relation between Reader and ReaderT? Well, suppose you started with the Identity monad:
type 'a identity = 'a;;
let mid (a : 'a) : 'a = a;;
let mbind (xx : 'a) (k : 'a -> 'b) : 'b = k xx;;
and you used the ReaderT transformer to wrap the Identity monad inside Readerish packaging. What do you suppose you would get?
The relations between the State monad and the StateT monadic transformer are parallel:
(* monadic operations for the State monad *)
type 'a state =
store -> ('a * store);;
let mid (a : 'a) : 'a state =
fun s -> (a, s);;
let mbind (xx : 'a state) (k : 'a -> 'b state) : 'b state =
fun s -> (fun (a, s') -> k a s') (xx s);;
We've used `(fun (a, s') -> k a s') (xx s)` instead of the more familiar `let (a, s') = xx s in k a s'` in order to factor out the part where a value of type `'a state` is supplied as an argument to a function. Now StateT will be:
(* monadic operations for the StateT monadic transformer *)
type 'a stateT(M) =
store -> ('a * store) M;;
(* notice this is not an ('a M) state *)
let mid (a : 'a) : 'a stateT(M) =
fun s -> M.unit (a, s);;
let mbind (xx : 'a stateT(M)) (k : 'a -> 'b stateT(M)) : 'b stateT(M) =
fun s -> M.bind (xx s) (fun (a, s') -> k a s');;
Do you see the pattern? Where before `mid` was implemented by a function that returned an `'a * store` value, now we instead use `M.unit` to return an `('a * store) M` value. Where before `mbind` supplied an `'a state` value `(xx s)` as an argument to a function, now we instead `M.bind` it to that function.
Once again, what do you think you'd get if you wrapped StateT monadic packaging around an Identity monad?
We spell out all the common monads, their common dedicated operations (such as `lookup`- and `shift`-like operations for the Reader monad), and monad transformer cousins of all of these, in an OCaml [[monad library]]. Read the linked page for details about how to use the library, and some design choices we made. Our [[State Monad Tutorial]] gives some more examples of using the library.
When a T monadic layer encloses an inner M monad, the T's interface is the most exposed one. To use operations defined in the inner M monad, you'll have to `hoist` them into the outer T packaging. Haskell calls this operation `lift`, but we call it `hoist` because the term "lift" is already now too overloaded. The `hoist` operation brings monadic values or functions from a wrapped monad out into the wrapping.
Here's an example. Suppose `S` is an instance of a State monad:
# #use "path/to/monads.ml";;
# module S = State_monad(struct type store = int end);;
and `MS` is a MaybeT wrapped around `S`:
# module MS = Maybe_monad.T(S);;
Then if you want to use an `S`-specific monad like `modify succ` inside `MS`, you'll have to use `MS`'s `hoist` function, like this:
# MS.(...hoist (S.modify succ) ...)
Each monad transformer's `hoist` function will be defined differently. They have to obey the following laws:
* `Outer.hoist (Inner.unit a) <~~> Outer.unit a`
* `Outer.hoist (Inner.bind xx k) <~~> Outer.bind (Outer.hoist xx) (fun x -> Outer.hoist (k x))`
We said that when T encloses M, you can rely on T's interface to be most exposed. That is intuitive. What you cannot also assume is that the implementing type has a Tish structure surrounding an Mish structure. Often it will be reverse: a ListT(Maybe) is implemented by a `'a list option`, not by an `'a option list`. Until you've tried to write the code to a monadic transformer library yourself, this will probably remain counter-intuitive. But you don't need to concern yourself with it in practise. Think of what you have as a ListT(Maybe); don't worry about whether the underlying implementation is as an `'a list option` or an `'a option list` or as something more complicated.
Notice from the code for StateT, above, that an `'a stateT(M)` is not an `('a M) state`; neither is it an `('a state) M`. The pattern by which we transform the types from a Blah monad to a BlahT monad transformer is:
't0 ---> 't0 M
't1 -> 't0 ---> 't1 -> 't0 M
('t1 -> 't0) -> 't0 ---> ('t1 -> 't0 M) -> 't0 M
Ken Shan's paper [Monads for natural language semantics](http://arxiv.org/abs/cs/0205026v1) (2001) discusses how to systematically move from some base monads to the corresponding monad transformers. But as he notes, his algorithm isn't the only one possible, and it only applies to monads whose type has a certain form. (Reader and State have that form; List for example doesn't.)
As best we know, figuring out how a monad transformer should be defined is still something of an art, not something that can be done mechanically. However, you can think that all of the art goes into deciding what StateT and so on should be; having figured that out, plain State would follow as the simple case where StateT is parameterized on the Identity monad.
Apart from whose interface is outermost, the behavior of a StateT(Maybe) and a MaybeT(State) will partly coincide. But in certain crucial respects they will diverge, and you need to think carefully about which behavior you want and what the appropriate layering is for your needs. Consider these examples:
# module MS = Maybe_monad.T(S);;
# module SM = S.T(Maybe_monad);;
# MS.(run (hoist (S.modify succ) >> zero () >> hoist S.get >>= fun cur -> mid (cur+10) )) 0;;
- : int option * S.store = (None, 1)
# MS.(run (hoist (S.modify succ) >> zero () >> hoist (S.put 5) )) 0;;
- : unit option * S.store = (None, 1)
Although we have a wrapped `None`, notice that the store (as it was at the point of failure) is still retrievable.
# SM.(run (modify succ >> hoist (Maybe_monad.zero ()) >> get >>= fun cur -> mid (cur+10) )) 0;;
- : (int * S.store) Maybe_monad.result = None
When Maybe is on the inside, on the other hand, a failure means the whole computation has failed, and even the store is no longer available.
Here's an example wrapping Maybe around List, and vice versa:
# module LM = List_monad.T(Maybe_monad);;
# module ML = Maybe_monad.T(List_monad);;
# ML.(run ((++) (zero ()) (mid 20) >>= fun i -> mid (i+10)));;
- : int ML.result = [Some 30]
When List is on the inside, the failed results just get dropped and the computation proceeds without them.
# LM.(run ((++) (hoist (Maybe_monad.zero ())) (mid 20) >>= fun i -> mid (i+10)));;
- : int LM.result = None
On the other hand, when Maybe is on the inside, failures abort the whole computation.
This is fun. Notice the difference it makes whether the second `++` is native to the outer `List_monad`, or whether it's the inner `List_monad`'s `++` hoisted into the outer wrapper:
# module LL = List_monad.T(List_monad);;
# LL.(run((++) (mid 1) (mid 2) >>= fun i -> (++) (mid i) (mid (10*i)) ));;
- : int LL.result = \[[1; 10; 2; 20]]
# LL.(run((++) (mid 1) (mid 2) >>= fun i -> hoist L.((++) (mid i) (mid (10*i)) )));;
- : int LL.result = [[1; 2]; [1; 20]; [10; 2]; [10; 20]]
Further Reading
---------------
* This is excellent, everyone should read: [Monad Transformers Step by Step](http://www.grabmueller.de/martin/www/pub/Transformers.pdf)
* Read Part III of [All About Monads](http://web.archive.org/web/20071106232016/haskell.org/all_about_monads/html/introIII.html). This link is to an archived version, the main link to haskell.org seems to be broken. Some but not all of this site has been [absorbed into the Haskell wikibook](http://en.wikibooks.org/wiki/Haskell/Monad_transformers).