2 So far, we've defined monads as single-layered things. Though in the Groenendijk, Stokhoff, and Veltmann homework, we had to figure out how to combine Reader, State, and Set monads in an ad-hoc way. 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 adds a Reader monad layer to it. The way these are defined parallels the way the single-layer versions are defined. For example, here's the Reader monad:
4 (* monadic operations for the Reader monad *)
8 let unit (a : 'a) : 'a reader =
10 let bind (u: 'a reader) (f : 'a -> 'b reader) : 'b reader =
11 fun e -> (fun v -> f v e) (u e);;
13 We've just beta-expanded the familiar `f (u e) e` into `(fun v -> f v
14 e) (u e)`, in order to factor out the parts where any Reader monad is
15 being supplied as an argument to another function, as illustrated in
16 the `bind` function in the following example. Then if we want instead
17 to add a Reader layer to some arbitrary other monad M, with its own
18 M.unit and M.bind, here's how we do it:
20 (* monadic operations for the ReaderT monadic transformer *)
22 (* We're not giving valid OCaml code, but rather something
23 * that's conceptually easier to digest.
24 * How you really need to write this in OCaml is more circuitous...
25 * see http://lambda.jimpryor.net/code/tree_monadize.ml for some details. *)
27 type ('a, M) readerT =
29 (* this is just an 'a M reader; but don't rely on that pattern to generalize *)
31 let unit (a : 'a) : ('a, M) readerT =
34 let bind (u : ('a, M) readerT) (f : 'a -> ('b, M) readerT) : ('b, M) readerT =
35 fun e -> M.bind (u e) (fun v -> f v e);;
37 Notice the key differences: where before we just returned `a`, now we
38 instead return `M.unit a`. Where before we just supplied value `u e`
39 of type `'a reader` as an argument to a function, now we instead
40 `M.bind` the `'a reader` to that function. Notice also the differences
43 What is the relation between Reader and ReaderT? Well, suppose you started with the Identity monad:
45 type 'a identity = 'a;;
46 let unit (a : 'a) : 'a = a;;
47 let bind (u : 'a) (f : 'a -> 'b) : 'b = f u;;
49 and you used the ReaderT transformer to add a Reader monad layer to the Identity monad. What do you suppose you would get?
51 The relations between the State monad and the StateT monadic transformer are parallel:
53 (* monadic operations for the State monad *)
56 store -> ('a * store);;
58 let unit (a : 'a) : 'a state =
61 let bind (u : 'a state) (f : 'a -> 'b state) : 'b state =
62 fun s -> (fun (a, s') -> f a s') (u s);;
64 We've used `(fun (a, s') -> f a s') (u s)` instead of the more familiar `let (a, s') = u s in f 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:
66 (* monadic operations for the StateT monadic transformer *)
69 store -> ('a * store) M;;
70 (* notice this is not an 'a M state *)
72 let unit (a : 'a) : ('a, M) stateT =
73 fun s -> M.unit (a, s);;
75 let bind (u : ('a, M) stateT) (f : 'a -> ('b, M) stateT) : ('b, M) stateT =
76 fun s -> M.bind (u s) (fun (a, s') -> f a s');;
78 Do you see the pattern? Where ordinarily we'd return an `'a` value, now we instead return an `'a M` value. Where ordinarily we'd supply a `'a state` value as an argument to a function, now we instead `M.bind` it to that function.
80 Okay, now let's do the same thing for our Tree monad.
82 (* monadic operations for the Tree monad *)
85 Leaf of 'a | Node of ('a tree) * ('a tree);;
87 let unit (a: 'a) : 'a tree =
90 let rec bind (u : 'a tree) (f : 'a -> 'b tree) : 'b tree =
93 | Node (l, r) -> (fun l' r' -> Node (l', r')) (bind l f) (bind r f);;
95 (* monadic operations for the TreeT monadic transformer *)
96 (* NOTE THIS IS NOT YET WORKING --- STILL REFINING *)
101 let unit (a: 'a) : ('a, M) tree =
104 let rec bind (u : ('a, M) tree) (f : 'a -> ('b, M) tree) : ('b, M) tree =
106 | Leaf a -> M.bind (f a) (fun b -> M.unit (Leaf b))
107 | Node (l, r) -> M.bind (bind l f) (fun l' ->
108 M.bind (bind r f) (fun r' ->
109 M.unit (Node (l', r'));;
111 Compare this definition of `bind` for the TreeT monadic transformer to our earlier definition of `tree_monadize`, specialized for the Reader monad:
113 let rec tree_monadize (f : 'a -> 'b reader) (t : 'a tree) : 'b tree reader =
115 | Leaf a -> reader_bind (f a) (fun b -> reader_unit (Leaf b))
116 | Node (l, r) -> reader_bind (tree_monadize f l) (fun l' ->
117 reader_bind (tree_monadize f r) (fun r' ->
118 reader_unit (Node (l', r'))));;