monad laws link
[lambda.git] / monad_transformers.mdwn
1
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:
3
4         (* monadic operations for the Reader monad *)
5
6         type 'a reader =
7                 env -> 'a;;
8         let unit (a : 'a) : 'a reader =
9                 fun e -> a;;
10         let bind (u: 'a reader) (f : 'a -> 'b reader) : 'b reader =
11                 fun e -> (fun v -> f v e) (u e);;
12
13 We've just beta-expanded the familiar `f (u e) e` into `(fun v -> f v e) (u e)`, in order to factor out the parts where any Reader monad is being supplied as an argument to another function. Then if we want instead to add a Reader layer to some arbitrary other monad M, with its own M.unit and M.bind, here's how we do it:
14
15         (* monadic operations for the ReaderT monadic transformer *)
16
17         (* We're not giving valid OCaml code, but rather something
18          * that's conceptually easier to digest.
19          * How you really need to write this in OCaml is more circuitous...
20          * see http://lambda.jimpryor.net/code/tree_monadize.ml for some details. *)
21
22         type ('a, M) readerT =
23                 env -> 'a M;;
24         (* this is just an 'a M reader; but don't rely on that pattern to generalize *)
25
26         let unit (a : 'a) : ('a, M) readerT =
27                 fun e -> M.unit a;;
28
29         let bind (u : ('a, M) readerT) (f : 'a -> ('b, M) readerT) : ('b, M) readerT =
30                 fun e -> M.bind (u e) (fun v -> f v e);;
31
32 Notice the key differences: where before we just returned `a`, now we instead return `M.unit a`. Where before we just supplied value `u e` of type `'a reader` as an argument to a function, now we instead `M.bind` the `'a reader` to that function. Notice also the differences in the types.
33
34 What is the relation between Reader and ReaderT? Well, suppose you started with the Identity monad:
35
36         type 'a identity = 'a;;
37         let unit (a : 'a) : 'a = a;;
38         let bind (u : 'a) (f : 'a -> 'b) : 'b = f u;;
39
40 and you used the ReaderT transformer to add a Reader monad layer to the Identity monad. What do you suppose you would get?
41
42 The relations between the State monad and the StateT monadic transformer are parallel:
43
44         (* monadic operations for the State monad *)
45
46         type 'a state =
47                 store -> ('a * store);;
48
49         let unit (a : 'a) : 'a state =
50                 fun s -> (a, s);;
51
52         let bind (u : 'a state) (f : 'a -> 'b state) : 'b state =
53                 fun s -> (fun (a, s') -> f a s') (u s);;
54
55 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:
56
57         (* monadic operations for the StateT monadic transformer *)
58
59         type ('a, M) stateT =
60                 store -> ('a * store) M;;
61         (* notice this is not an 'a M state *)
62
63         let unit (a : 'a) : ('a, M) stateT =
64                 fun s -> M.unit (a, s);;
65
66         let bind (u : ('a, M) stateT) (f : 'a -> ('b, M) stateT) : ('b, M) stateT =
67                 fun s -> M.bind (u s) (fun (a, s') -> f a s');;
68
69 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.
70
71 Okay, now let's do the same thing for our Tree monad.
72
73         (* monadic operations for the Tree monad *)
74
75         type 'a tree =
76                 Leaf of 'a | Node of ('a tree) * ('a tree);;
77
78         let unit (a: 'a) : 'a tree =
79                 Leaf a;;
80
81         let rec bind (u : 'a tree) (f : 'a -> 'b tree) : 'b tree =
82             match u with
83             | Leaf a -> f a;;
84             | Node (l, r) -> (fun l' r' -> Node (l', r')) (bind l f) (bind r f);;
85
86         (* monadic operations for the TreeT monadic transformer *)
87         (* NOTE THIS IS NOT YET WORKING --- STILL REFINING *)
88
89         type ('a, M) treeT =
90                 'a tree M;;
91
92         let unit (a: 'a) : ('a, M) tree =
93                 M.unit (Leaf a);;
94
95         let rec bind (u : ('a, M) tree) (f : 'a -> ('b, M) tree) : ('b, M) tree =
96             match u with
97             | Leaf a -> M.bind (f a) (fun b -> M.unit (Leaf b))
98             | Node (l, r) -> M.bind (bind l f) (fun l' ->
99                                                         M.bind (bind r f) (fun r' ->
100                                                                 M.unit (Node (l', r'));;
101
102 Compare this definition of `bind` for the TreeT monadic transformer to our earlier definition of `tree_monadize`, specialized for the Reader monad:
103
104         let rec tree_monadize (f : 'a -> 'b reader) (t : 'a tree) : 'b tree reader =
105             match t with
106             | Leaf a -> reader_bind (f a) (fun b -> reader_unit (Leaf b))
107             | Node (l, r) -> reader_bind (tree_monadize f l) (fun l' ->
108                                reader_bind (tree_monadize f r) (fun r' ->
109                                  reader_unit (Node (l', r'))));;
110
111