f3cb414384d1f5adfa33c32215d5e18465f0f2cf
[lambda.git] / topics / week9_using_monad_library.mdwn
1 [[!toc levels=2]]
2
3 ## What functions are in the OCaml Monad modules? ##
4
5 If you want to see the "signature" of some OCaml library or "module" --- that is, a list of the types it exports, and also of the types of function values (and other values) that it exports, you can do this.
6
7 If you know the name of the module but not the name of its module type, or if its module type doesn't have a name, you can do this:
8
9     module type SOMETHING = sig include module type of ModuleYouAreInterestedIn end
10
11 More commonly, though, you'll know that the module uses a specific *named* module type. In that case, here is what to do. An example is that modules you create using `Monad.Reader(struct type env = ... end).M` --- really you have to do that in two steps, first say `module R_E = Monad.Reader(struct type env = ... end)`, next write `R_E.M`; but for brief reading I'll just write `Monad.Reader(struct type env = ... end).M` --- these modules will all have the module type `Monad.READER`. We can see an expansion of `Monad.READER` by writing:
12
13     module type SOMETHING = sig include Monad.READER end
14
15 Then OCaml will respond:
16
17     module type SOMETHING =
18       sig
19         type env
20
21         type 'a t
22         type 'a result = env -> 'a
23         val run : 'a t -> 'a result
24
25         val map : ('a -> 'b) -> 'a t -> 'b t
26         val mid : 'a -> 'a t
27         val map2 : ('a -> 'b -> 'c) -> 'a t -> 'b t -> 'c t
28         val mapply : ('a -> 'b) t -> 'a t -> 'b t
29         val ( >> ) : 'a t -> 'b t -> 'b t
30         val ( << ) : 'a t -> 'b t -> 'a t
31         val ( >>= ) : 'a t -> ('a -> 'b t) -> 'b t
32         val ( >=> ) : ('a -> 'b t) -> ('b -> 'c t) -> 'a -> 'c t
33         val ( <=< ) : ('b -> 'c t) -> ('a -> 'b t) -> 'a -> 'c t
34         val join : 'a t t -> 'a t
35         val ignore : 'a t -> unit t
36         val seq : 'a t list -> 'a list t
37         val seq_ignore : unit t list -> unit t
38         val do_when : bool -> unit t -> unit t
39         val do_unless : bool -> unit t -> unit t
40
41         val ask : env t
42         val asks : (env -> 'a) -> 'a t
43         val shift : (env -> env) -> 'a t -> 'a t
44       end
45
46 Here I've reordered the elements a bit from OCaml's actual presentation to make it easier to explain them. First, there is the type of the `env` that you will supply. In this signature it's listed abstractly (no concrete definition of the type is supplied, it's just declared that it exists), but in practice this type will be unified with the envionment type that you supply. Next there is the abstract type `'a t`. This is the main type of the Reader Monad. Behind the scenes it will be the same as the type `'a result`, that is a function from `env`s to `'a`s. But OCaml keeps the type `'a t` abstract, and only exposes the actual implementation of the type when you apply the `run` function to `'a t` values. The `run` function is for most Monads just implemented as an identity function. Its effect is primarily to shift between the abstract type, whose definition OCaml treats as private, and the concrete implementation of that type, named `'a result`. Note that even though the `'a t`s and `'a result`s will usually be represented by the same structures in OCaml's memory, our library insists that you only use the `'a t` version for the monadic functions like `map` and `>>=` and so on. Also there is no translation function that lets you take an `'a result` that you've built by hand and convert it to an `'a t`. You have to build your `'a t`s using the operations that the Monad library provides for doing so.
47
48 The next block of stuff in the `Monad.READER` module type are functions common to all monads. Some monads (such as List and Option) additionally have `mzero : 'a t` and some related operations. Finally, at the end are the operations specific to the Reader monad. These have mostly been given the same names as they have in Haskell's monad libraries. Here is a quick explanation:
49
50 *   `ask` is a Reader monadic value that, when given an `env`, returns that very `env` as its payload. So its type is an `env t`. And it is implemented as `fun e -> e`.
51
52 *   `asks` is a variant of `ask`. Haskell several times uses the convention that where `blah` is some operation that gives you some raw value, `blahs` is a variant that gives you the result of applying some *s*elector function or handler to the raw value. In this case, `asks` takes a parameter, which is a function that operates on `env`s and gives some result. `asks handler` then constitutes an `'a t`, where `'a` is the type of the handler's result. For example, if my environments are associations of some sort between `char`s and `int`s, and `lookup 'x'` is a function that takes an `env` as a further argument and returns what `int` that environment associates the char `'x'` with, then:
53
54         let getx = asks (lookup 'x')
55
56     will declare an `int t` that, when supplied with an environment, returns the `int` that that environment associates the `char` `'x'` with.
57
58 *   `shift` is a function that takes two arguments, an "`env`-shifting" function and some Reader monadic value. It returns a new Reader monadic value that, when supplied with an `env`, will process the original monadic value only with the `env` shifted in the specified way. (Haskell calls the `shift` operation `local`, but I thought it would be a bit better to call it `shift` --- enough of an improvement to justify changing the name.)
59
60     For example, if `insert 'x' 1` is a function that operates on `env`s and gives a new `env` which now associates the char `'x'` with `1`, then:
61
62         let letx xint body = shift (insert 'x' xint) body
63
64     will declare an operation that takes an `int` `xint` and a monadic value `body`, and evaluates `body` in the context <code>let x = <i>xint</i> in ...</code>. If we wanted instead to have a version which accepted not an `int`, but rather an `int` Reader, we could write instead:
65
66         let letx xx body = xx >>= fun xint -> shift (insert 'x' xint) body
67
68
69 ### Examples ###
70
71 Here are some examples of using the Reader Monad modules to evaluate some simple expressions using bound variables. First, you could look at [[this Haskell code|/code/reader1.hs]]. It `import`s the `Control.Monad.Reader` library, which is where Haskell's Reader monad can be found. It declares an `Env` type that we'll implement as a simple *function* from `Char`s to `Int`s. Then it defines an "empty" environment `env0`, and a function `insert` for adding new bindings to an `env`. Next, we make a general function `getint` that can create monadic values like the `getx` illustrated above. We show how to use `getx` and `gety` to write monadic versions of `y + x` and `3 + x`. Next, we define a `letx` function as illustrated above (the second version, that takes a monadic value `xx` as its argument). We show how to use this to write a monadic version of `let x = 2 in y + x`. The final line of the file applies `runReader` to the monadic value we've built --- this is Haskell's way of doing what we do in OCaml with `run`, namely to remove the abstraction barrier and see what concrete type is really constituting our `Reader Env a`s --- and we supply it with the empty environment, which will be sufficient since the expression we're interpreting has no free variables. Haskell binds the variable `res` to the result. You can run this code inside `ghci` by typing `:load /path/to/reader1.hs`. (You may also be able to say `:add ...` instead of `:load ...`.) Then type `res`, and Haskell will report back `5`.
72
73 [[This OCaml code|/code/reader1.ml]] does exactly the same thing only using our OCaml monad libraries instead. The biggest difference from the Haskell version is in the first few lines, where we have to generate a Reader monad module parameterized on the `env` type that we intend to work with.
74
75 Here's a more complicated example. This time we want to be able to bind variables to lambda abstracts as well as to `int`s. So our `env` type will need to be more complex; it will have to associate `char`s with a disjoint sum of `int`s and lambda abstracts. Now what will the type of the lambda abstracts be? Let's just restrict our attention to abstracts whose bodies return `int`s. But they might get those `int`s by performing operations on bound variables, so the body expressions need to be interpreted monadically, as `int Reader`s. We'll construe the whole lambda abstract as a  function from `int Reader`s (that is, the monadic values which are provided as arguments to the lambda abstract) to their results, so the lambda abstract will have the type `int Reader -> int Reader`. In OCaml that will be `int R.t -> int R.t`, and in Haskell `Reader Env Int -> Reader Env Int`. Since variables can be bound to either `int`s or to lambda abstracts, we declare our environments like this in OCaml:
76
77     type bound = Int of int | Fun of (int R.t -> int R.t)
78     type env = char -> bound
79
80 and like this in Haskell:
81
82     data Bound = Int Int | Fun (Reader Env Int -> Reader Env Int)
83     type Env = Char -> Bound
84
85 There is a tricky issue in the OCaml case, though, in that when working with OCaml, we have to *generate* our `R` Reader monad module, parameterized on the type of the `env`, but here we see that we need access to the *type* `'a R.t` from the generated `R` module in order to declare the `env`. Fortunately, it is possible to do this, by having the module that declares the `env` and the module that has our Reader monad in it be mutually recursively defined. The first few lines of [[this OCaml code|/code/reader2.ml]] do the tricky work.
86
87 After that, our [[Haskell code|/code/reader2.hs]] and [[OCaml code|/code/reader2.ml]] proceed basically the same, allowing for the difference in syntax and vocabulary between Haskell and OCaml. The `getint` function works like before, except now we have to pull the `int` out from behind the `Int` constructor of our disjoint sum type `bound`. We have a parallel `getfun` function. Then we interpret the variable `x` using the monadic value `getint 'x'`, and we interpret the variable `f` using the monadic value `getfun 'f'`. The `letx` operation is similarly adjusted, and we also have a parallel `letf`.
88
89 The really new thing in this code, compared to the previous example, is our definition of a monadic value to interpret the lambda abstract `\y -> y + x`, that `f` gets bound to. And also our interpretation of the expression `f 3`, which looks up a function that the variable `f` is bound to, and then applies it to (a monadically-lifted version of) `3`. (We have the argument be monadically lifted so that we could also say, for example, `f y`.) You can examine the code to see how we do these things.
90
91
92 ## OK, what else is in the OCaml Monad modules? ##
93
94 I won't give an exhaustive list here. But here is the output of `module type SOMETHING = sig include Monad.BLAH end` for some of the `BLAH`s:
95
96
97     module type STATE =
98       sig
99         type store
100         type 'a t
101         type 'a result = store -> 'a * store
102         (* plus the other usual monadic stuff, and: *)
103         val get : store t
104         val gets : (store -> 'a) -> 'a t
105         val modify : (store -> store) -> unit t
106         val put : store -> unit t
107       end
108
109 The `store` type has to be provided by you, when you generate the module, similarly to as in the Reader monad. The `'a result` type shows the real definition of an `'a State` type, otherwise kept abstract as `'a t`. Instead of the special operations `ask` and so on that the Reader monad has, State has the operations `get`, `gets`, `modify`, and `put`. The first two work just like `ask` and `asks` did for the Reader monad. The third one works *similarly* to `shift` for the Reader monad, with the crucial difference that the rebinding that `shift` introduces is in effect only for the `body` argument of the `shift` operation. Outside of that `body`, we revert to the originally supplied `env`. But notice that `modify` doesn't take any `body` argument. `modify` introduces changes to the supplied `store` that once introduced *stay in place*, until we manually change them again. Thus with the Reader monad you'd do things like this:
110
111     R.(xx >>= fun x -> ... shift (insert ...) body >>= fun y -> (* now we're using the unshifted env *) ...)
112
113 With the State monad you'd instead do things like this:
114
115     S.(xx >>= fun x -> ... modify (fun old_store -> new_store) >>= fun () -> (* we continue using the modified store, until it's modified once again *) ...)
116
117 Since the pattern `... >>= fun () -> ...` or `... >>= fun variable_you_never_use -> ...` occurs often when working with monads, there's a shorthand: you can instead say `... >> ...`, with `>>` in place of `>>= fun pattern ->`.
118
119 Here's another monad module signature:
120
121     module type WRITER =
122       sig
123         type log
124         type 'a t
125         type 'a result = 'a * log
126         (* plus the other usual monadic stuff, and: *)
127         val listen : 'a t -> ('a * log) t
128         val listens : (log -> 'b) -> 'a t -> ('a * 'b) t
129         val tell : log -> unit t
130         val censor : (log -> log) -> 'a t -> 'a t
131       end
132
133 Writer is very similar to Reader: first, it is parameterized on something like an `env`, here called a `log`. (A typical implementation for `log` would be the `string` type.) Second, the Writer operations `listen`, `listens`, and `censor` parallel the Reader operations `ask`, `asks`, and `shift`. But one difference is that with Writer, you cannot choose what initial `env` (`log`) to supply. You always begin with the `empty` `log` (such as `""` for `string`s). A second difference is that the types differ. Compare:
134
135     module type READER =
136       sig
137         ...
138         val ask : env t
139         val asks : (env -> 'a) -> 'a t
140         val shift : (env -> env) -> 'a t -> 'a t
141       end
142
143 Whereas Writer's `censor` and Reader's `shift` have isomorphic types, there is some extra complextity to Writer's `listen` and `listens`, compared to `ask` and `asks`. What this extra complexity means is that for `Writer`, listening happens only in a local context. You can't `listen` to what got written to the log before you installed your `listen`ing tap. But you can return payloads that are dependent on what you've heard in the local context.
144
145 Unlike Reader, Writer also has a `tell` operation, which is akin to the `put` operation in the State monad. The difference is that the `tell` function takes a `log` as argument and *appends* that to the existing `log`. You can't erase or overwrite elements already in the `log`; you can only append to it. However, if you like, you can `censor` the log generated by any local context. (Inside the local context, the log isn't yet censored; the censoring only affects what's seen downstream as the contributions made by that context to the log.)
146
147 Here's a complex example that illustrates this. First we will use the helper function `String.upper` (from "juli8.ml") and a second helper function that we define like this:
148
149     let bracket log = "{" ^ log ^ "}"
150
151 Next, we construct some monadic values and reveal them at the end using `run`:
152
153     module W_L = Monad.Writer(struct
154       type log = string
155       let empty = ""
156       let append s1 s2 = if s1 = "" then s2 else if s2 = "" then s1 else s1 ^ " " ^ s2
157     end)
158     module W = Writer1.M;;
159     W.(let xx = tell "one" >> listens bracket (tell "two" >> mid 10) in
160        let yy = censor String.upper (tell "zero" >> listens bracket xx) in
161        let zz = tell "before" >> yy >>= fun y -> tell "after" >> mid y in
162        ...);;
163
164 The monadic value `xx` writes `"one"` to the log, then discards the resulting `()` payload (it continues `>> ...` rather than `>>= fun var -> ...`). Then we have a use of `listens`. This will evaluate its body `tell "two" >> mid 10` and return as payload a pair of the body's original payload and a `bracket`ed copy of the local log. Thus the payload of `listens bracket (tell "two" >> mid 10)` will be `(10, "{two}")`. Its log will be `"two"`. The `"one"` that got written to the log earlier isn't accessible to `listens`; however it does stay in the overall log, to which the `listens ...` construction contributes. Hence the result of `run xx`, showing first the payload and then the log, would be:
165
166     - : (int * string) W.result = ((10, "{two}"), "one two")
167
168 Now `yy` uses that `xx` monadic value to illustrate the use of `censor`. Here we have `censor` apply `String.upper` to the log generated in the local context it's applied to, hence the result of  `run yy` would be:
169
170     - : ((int * string) * string) W.result = (((10, "{two}"), "{one two}"), "ZERO ONE TWO")
171
172 The final value `zz` shows what happens to entries written to the log before and after the `censor`ing that occurs in `yy`, namely nothing. That is, `run zz` is:
173
174     - : ((int * string) * string) W.result = (((10, "{two}"), "{one two}"), "before ZERO ONE TWO after")
175
176 Let's look at some more familiar monad signatures. Here is one:
177
178     module type OPTION =
179       sig
180         type 'a t
181         type 'a result = 'a option
182         (* plus the other usual monadic stuff, and: *)
183         val mzero : 'a t
184         val guard : bool -> unit t
185         val test : ('a option -> bool) -> 'a t -> 'a t
186       end
187
188 This is what's exposed in the `Monad.Option.M` module (with `Option` and `List`, you can also leave off the initial `Monad.`). In the parent `Monad.Option` module itself, there are many more operations. Similarly, `Monad.List` (aka just `List`) exposes many more operations than `Monad.List.M` does. The `.M` modules restrict us to just the monadic interface. Unlike Reader and State and Writer, the Option and List monads don't need to be parameterized on environments or anything else of that sort. The Option monad has three additional monadic operations, analogues of which are also all present in List. First, there is the `mzero` monadic value, implemented as `None` and satisfying the Monad Laws for `mzero` we explained elsewhere. The key one to remember is that `mzero` aborts a chain of composed Kleisli functions. That is, `mzero >>= anything` is always `mzero`. `guard` takes a boolean argument and if its false, gives `mzero`. If the argument is true, it just gives the uninteresting `mid ()`, hence the typical way to use `guard` is as:
189
190     module O = Option.M;;    
191     O.(guard some_bool_expr >> more_monadic_stuff)
192
193 If `some_bool_expr` is true, then this will ignore its payload and go on to compute `more_monadic_stuff`; if it's false, then the whole chain gets ignored because of the distinctive behavior of `mzero`.
194
195 The third special operation in the Option monad is `test`. This lets you supply a function that takes an ordinary `'a option` type (that is, one where the "abstraction curtain" imposed by the `'a O.t` type is not in place) and returns a `bool`. Then you take an Option monadic value (one where the "abstraction curtain" *is* in place). OCaml will temporarily remove the abstraction curtain on the second argument and see how the function you supplied assesses it. If the result is `true`, then the result is identical to that Option monadic value, unaltered. If the result is `false`, then the result is `mzero`. (For those of you who know Frank Veltman's work on dynamic semantics for epistemic modals, this `test` (or the version of it for sets of worlds) is a key component.)
196
197 Here is the List monadic interface:
198
199     module type LIST =
200       sig
201         type 'a t
202         type 'a result = 'a list
203         (* plus the other usual monadic stuff, and: *)
204         val mzero : 'a t
205         val guard : bool -> unit t
206         val test : ('a list -> bool) -> 'a t -> 'a t
207         val ( ++ ) : 'a t -> 'a t -> 'a t
208         val pick : 'a t -> ('a * 'a t) t
209       end
210
211 The `mzero` and `guard` and `test` operations work analogously to the ones in the Option monad. The `++` (infix) operation is like `List.append` (OCaml also uses `@` for that), with the difference that `++` is defined on the abstract List monadic values of type `'a List.M.t`, not the OCaml native lists (with the "abstraction curtain" removed). In Haskell, `++` works on either native lists or elements of the List monad, because Haskell doesn't distinguish them. Haskell doesn't impose an abstraction curtain in the case of its List and Maybe monads. `pick` is an operation that transforms (the abstract version of) `[1; 2; 3]` to (the abstract version of) `[(1, [2; 3]); (2, [1; 3]); (3, [1; 2])]`.
212
213 Here is another monadic interface:
214
215     module type TREE =
216       sig
217         type 'a tree
218         type 'a t
219         type 'a result = 'a tree
220         (* plus the other usual monadic stuff, and: *)
221         val ( ++ ) : 'a t -> 'a t -> 'a t
222       end
223
224 This is the signature/module type for the Monad.LTree module. ("LTree" for *leaf-labeled* trees.)
225
226 You can create leaf-only trees using the monadic function `mid`. You can join two trees together using the function `++`, paralleling the one in List. Note that in the Tree case, unlike the List case, `++` is not associative: `xx ++ (yy ++ zz)` is not the same as `(xx + yy) ++ zz`. Nor is there any `mzero` for trees as implemented by this module.
227