split off reader, intens monads; link to week7
[lambda.git] / week7.mdwn
1 [[!toc]]
2
3
4 Monads
5 ------
6
7 Start by (re)reading the discussion of monads in the lecture notes for
8 week 6 [[Towards Monads]].
9 In those notes, we saw a way to separate thinking about error
10 conditions (such as trying to divide by zero) from thinking about
11 normal arithmetic computations.  We did this by making use of the
12 `option` type: in each place where we had something of type `int`, we
13 put instead something of type `int option`, which is a sum type
14 consisting either of one choice with an `int` payload, or else a `None`
15 choice which we interpret as  signaling that something has gone wrong.
16
17 The goal was to make normal computing as convenient as possible: when
18 we're adding or multiplying, we don't have to worry about generating
19 any new errors, so we do want to think about the difference between
20 `int`s and `int option`s.  We tried to accomplish this by defining a
21 `bind` operator, which enabled us to peel away the `option` husk to get
22 at the delicious integer inside.  There was also a homework problem
23 which made this even more convenient by mapping any binary operation
24 on plain integers into a lifted operation that understands how to deal
25 with `int option`s in a sensible way.
26
27 [Linguitics note: Dividing by zero is supposed to feel like a kind of
28 presupposition failure.  If we wanted to adapt this approach to
29 building a simple account of presupposition projection, we would have
30 to do several things.  First, we would have to make use of the
31 polymorphism of the `option` type.  In the arithmetic example, we only
32 made use of `int option`s, but when we're composing natural language
33 expression meanings, we'll need to use types like `N option`, `Det option`,
34 `VP option`, and so on.  But that works automatically, because we can use
35 any type for the `'a` in `'a option`.  Ultimately, we'd want to have a
36 theory of accommodation, and a theory of the situations in which
37 material within the sentence can satisfy presuppositions for other
38 material that otherwise would trigger a presupposition violation; but,
39 not surprisingly, these refinements will require some more
40 sophisticated techniques than the super-simple option monad.]
41
42 So what exactly is a monad?  We can consider a monad to be a system
43 that provides at least the following three elements:
44
45 *       A complex type that's built around some more basic type. Usually
46         the complex type will be polymorphic, and so can apply to different basic types.
47         In our division example, the polymorphism of the `'a option` type
48         provides a way of building an option out of any other type of object.
49         People often use a container metaphor: if `u` has type `int option`,
50         then `u` is a box that (may) contain an integer.
51
52                 type 'a option = None | Some of 'a;;
53
54 *       A way to turn an ordinary value into a monadic value.  In OCaml, we
55         did this for any integer `x` by mapping it to
56         the option `Some x`.  In the general case, this operation is
57         known as `unit` or `return.` Both of those names are terrible. This
58         operation is only very loosely connected to the `unit` type we were
59         discussing earlier (whose value is written `()`). It's also only
60         very loosely connected to the "return" keyword in many other
61         programming languages like C. But these are the names that the literature
62         uses.
63
64         The unit/return operation is a way of lifting an ordinary object into
65         the monadic box you've defined, in the simplest way possible. You can think
66         of the singleton function as an example: it takes an ordinary object
67         and returns a set containing that object. In the example we've been
68         considering:
69
70                 let unit x = Some x;;
71                 val unit : 'a -> 'a option = <fun>
72
73         So `unit` is a way to put something inside of a monadic box. It's crucial
74         to the usefulness of monads that there will be monadic boxes that
75         aren't the result of that operation. In the option/maybe monad, for
76         instance, there's also the empty box `None`. In another (whimsical)
77         example, you might have, in addition to boxes merely containing integers,
78         special boxes that contain integers and also sing a song when they're opened. 
79
80         The unit/return operation will always be the simplest, conceptually
81         most straightforward way to lift an ordinary value into a monadic value
82         of the monadic type in question.
83
84 *       Thirdly, an operation that's often called `bind`. This is another
85         unfortunate name: this operation is only very loosely connected to
86         what linguists usually mean by "binding." In our option/maybe monad, the
87         bind operation is:
88
89                 let bind u f = match u with None -> None | Some x -> f x;;
90                 val bind : 'a option -> ('a -> 'b option) -> 'b option = <fun>
91
92         Note the type: `bind` takes two arguments: first, a monadic box
93         (in this case, an `'a option`); and second, a function from
94         ordinary objects to monadic boxes. `bind` then returns a monadic
95         value: in this case, a `'b option` (you can start with, e.g., `int option`s
96         and end with `bool option`s).
97
98         Intuitively, the interpretation of what `bind` does is this:
99         the first argument is a monadic value `u`, which 
100         evaluates to a box that (maybe) contains some ordinary value, call it `x`.
101         Then the second argument uses `x` to compute a new monadic
102         value.  Conceptually, then, we have
103
104                 let bind u f = (let x = unbox u in f x);;
105
106         The guts of the definition of the `bind` operation amount to
107         specifying how to unbox the monadic value `u`.  In the `bind`
108         operator for the option monad, we unboxed the monadic value by
109         matching it with the pattern `Some x`---whenever `u`
110         happened to be a box containing an integer `x`, this allowed us to
111         get our hands on that `x` and feed it to `f`.
112
113         If the monadic box didn't contain any ordinary value,
114         we instead pass through the empty box unaltered.
115
116         In a more complicated case, like our whimsical "singing box" example
117         from before, if the monadic value happened to be a singing box
118         containing an integer `x`, then the `bind` operation would probably
119         be defined so as to make sure that the result of `f x` was also
120         a singing box. If `f` also wanted to insert a song, you'd have to decide
121         whether both songs would be carried through, or only one of them.
122
123         There is no single `bind` function that dictates how this must go.
124         For each new monadic type, this has to be worked out in an
125         useful way.
126
127 So the "option/maybe monad" consists of the polymorphic `option` type, the
128 `unit`/return function, and the `bind` function.
129
130
131 A note on notation: Haskell uses the infix operator `>>=` to stand
132 for `bind`. Chris really hates that symbol.  Following Wadler, he prefers to
133 use an infix five-pointed star &#8902;, or on a keyboard, `*`. Jim on the other hand
134 thinks `>>=` is what the literature uses and students won't be able to
135 avoid it. Moreover, although &#8902; is OK (though not a convention that's been picked up), overloading the multiplication symbol invites its own confusion
136 and Jim feels very uneasy about that. If not `>>=` then we should use
137 some other unfamiliar infix symbol (but `>>=` already is such...)
138
139 In any case, the course leaders will work this out somehow. In the meantime,
140 as you read around, wherever you see `u >>= f`, that means `bind u f`. Also,
141 if you ever see this notation:
142
143         do
144                 x <- u
145                 f x
146
147 That's a Haskell shorthand for `u >>= (\x -> f x)`, that is, `bind u f`.
148 Similarly:
149
150         do
151                 x <- u
152                 y <- v
153                 f x y
154
155 is shorthand for `u >>= (\x -> v >>= (\y -> f x y))`, that is, `bind u (fun x
156 -> bind v (fun y -> f x y))`. Those who did last week's homework may recognize
157 this last expression.
158
159 (Note that the above "do" notation comes from Haskell. We're mentioning it here
160 because you're likely to see it when reading about monads. It won't work in
161 OCaml. In fact, the `<-` symbol already means something different in OCaml,
162 having to do with mutable record fields. We'll be discussing mutation someday
163 soon.)
164
165 As we proceed, we'll be seeing a variety of other monad systems. For example, another monad is the list monad. Here the monadic type is:
166
167         # type 'a list
168
169 The `unit`/return operation is:
170
171         # let unit x = [x];;
172         val unit : 'a -> 'a list = <fun>
173
174 That is, the simplest way to lift an `'a` into an `'a list` is just to make a
175 singleton list of that `'a`. Finally, the `bind` operation is:
176
177         # let bind u f = List.concat (List.map f u);;
178         val bind : 'a list -> ('a -> 'b list) -> 'b list = <fun>
179         
180 What's going on here? Well, consider `List.map f u` first. This goes through all
181 the members of the list `u`. There may be just a single member, if `u = unit x`
182 for some `x`. Or on the other hand, there may be no members, or many members. In
183 any case, we go through them in turn and feed them to `f`. Anything that gets fed
184 to `f` will be an `'a`. `f` takes those values, and for each one, returns a `'b list`.
185 For example, it might return a list of all that value's divisors. Then we'll
186 have a bunch of `'b list`s. The surrounding `List.concat ( )` converts that bunch
187 of `'b list`s into a single `'b list`:
188
189         # List.concat [[1]; [1;2]; [1;3]; [1;2;4]]
190         - : int list = [1; 1; 2; 1; 3; 1; 2; 4]
191
192 So now we've seen two monads: the option/maybe monad, and the list monad. For any
193 monadic system, there has to be a specification of the complex monad type,
194 which will be parameterized on some simpler type `'a`, and the `unit`/return
195 operation, and the `bind` operation. These will be different for different
196 monadic systems.
197
198 Many monadic systems will also define special-purpose operations that only make
199 sense for that system.
200
201 Although the `unit` and `bind` operation are defined differently for different
202 monadic systems, there are some general rules they always have to follow.
203
204
205 The Monad Laws
206 --------------
207
208 Just like good robots, monads must obey three laws designed to prevent
209 them from hurting the people that use them or themselves.
210
211 *       **Left identity: unit is a left identity for the bind operation.**
212         That is, for all `f:'a -> 'a m`, where `'a m` is a monadic
213         type, we have `(unit x) * f == f x`.  For instance, `unit` is itself
214         a function of type `'a -> 'a m`, so we can use it for `f`:
215
216                 # let unit x = Some x;;
217                 val unit : 'a -> 'a option = <fun>
218                 # let ( * ) u f = match u with None -> None | Some x -> f x;;
219                 val ( * ) : 'a option -> ('a -> 'b option) -> 'b option = <fun>
220
221         The parentheses is the magic for telling OCaml that the
222         function to be defined (in this case, the name of the function
223         is `*`, pronounced "bind") is an infix operator, so we write
224         `u * f` or `( * ) u f` instead of `* u f`. Now:
225
226                 # unit 2;;
227                 - : int option = Some 2
228                 # unit 2 * unit;;
229                 - : int option = Some 2
230
231                 # let divide x y = if 0 = y then None else Some (x/y);;
232                 val divide : int -> int -> int option = <fun>
233                 # divide 6 2;;
234                 - : int option = Some 3
235                 # unit 2 * divide 6;;
236                 - : int option = Some 3
237
238                 # divide 6 0;;
239                 - : int option = None
240                 # unit 0 * divide 6;;
241                 - : int option = None
242
243
244 *       **Associativity: bind obeys a kind of associativity**. Like this:
245
246                 (u * f) * g == u * (fun x -> f x * g)
247
248         If you don't understand why the lambda form is necessary (the "fun
249         x" part), you need to look again at the type of `bind`.
250
251         Some examples of associativity in the option monad:
252
253                 # Some 3 * unit * unit;; 
254                 - : int option = Some 3
255                 # Some 3 * (fun x -> unit x * unit);;
256                 - : int option = Some 3
257
258                 # Some 3 * divide 6 * divide 2;;
259                 - : int option = Some 1
260                 # Some 3 * (fun x -> divide 6 x * divide 2);;
261                 - : int option = Some 1
262
263                 # Some 3 * divide 2 * divide 6;;
264                 - : int option = None
265                 # Some 3 * (fun x -> divide 2 x * divide 6);;
266                 - : int option = None
267
268 Of course, associativity must hold for *arbitrary* functions of
269 type `'a -> 'a m`, where `m` is the monad type.  It's easy to
270 convince yourself that the `bind` operation for the option monad
271 obeys associativity by dividing the inputs into cases: if `u`
272 matches `None`, both computations will result in `None`; if
273 `u` matches `Some x`, and `f x` evalutes to `None`, then both
274 computations will again result in `None`; and if the value of
275 `f x` matches `Some y`, then both computations will evaluate
276 to `g y`.
277
278 *       **Right identity: unit is a right identity for bind.**  That is, 
279         `u * unit == u` for all monad objects `u`.  For instance,
280
281                 # Some 3 * unit;;
282                 - : int option = Some 3
283                 # None * unit;;
284                 - : 'a option = None
285
286
287 More details about monads
288 -------------------------
289
290 If you studied algebra, you'll remember that a *monoid* is an
291 associative operation with a left and right identity.  For instance,
292 the natural numbers along with multiplication form a monoid with 1
293 serving as the left and right identity.  That is, temporarily using
294 `*` to mean arithmetic multiplication, `1 * u == u == u * 1` for all
295 `u`, and `(u * v) * w == u * (v * w)` for all `u`, `v`, and `w`.  As
296 presented here, a monad is not exactly a monoid, because (unlike the
297 arguments of a monoid operation) the two arguments of the bind are of
298 different types.  But it's possible to make the connection between
299 monads and monoids much closer. This is discussed in [Monads in Category
300 Theory](/advanced_notes/monads_in_category_theory).
301 See also <http://www.haskell.org/haskellwiki/Monad_Laws>.
302
303 Here are some papers that introduced monads into functional programming:
304
305 *       [Eugenio Moggi, Notions of Computation and Monads](http://www.disi.unige.it/person/MoggiE/ftp/ic91.pdf): Information and Computation 93 (1) 1991.
306
307 *       [Philip Wadler. Monads for Functional Programming](http://homepages.inf.ed.ac.uk/wadler/papers/marktoberdorf/baastad.pdf):
308 in M. Broy, editor, *Marktoberdorf Summer School on Program Design Calculi*, Springer Verlag, NATO ASI Series F: Computer and systems sciences, Volume 118, August 1992. Also in J. Jeuring and E. Meijer, editors, *Advanced Functional Programming*, Springer Verlag, LNCS 925, 1995. Some errata fixed August 2001.
309 <!--    The use of monads to structure functional programs is described. Monads provide a convenient framework for simulating effects found in other languages, such as global state, exception handling, output, or non-determinism. Three case studies are looked at in detail: how monads ease the modification of a simple evaluator; how monads act as the basis of a datatype of arrays subject to in-place update; and how monads can be used to build parsers.-->
310
311 *       [Philip Wadler. The essence of functional programming](http://homepages.inf.ed.ac.uk/wadler/papers/essence/essence.ps):
312 invited talk, *19'th Symposium on Principles of Programming Languages*, ACM Press, Albuquerque, January 1992.
313 <!--    This paper explores the use monads to structure functional programs. No prior knowledge of monads or category theory is required.
314         Monads increase the ease with which programs may be modified. They can mimic the effect of impure features such as exceptions, state, and continuations; and also provide effects not easily achieved with such features. The types of a program reflect which effects occur.
315         The first section is an extended example of the use of monads. A simple interpreter is modified to support various extra features: error messages, state, output, and non-deterministic choice. The second section describes the relation between monads and continuation-passing style. The third section sketches how monads are used in a compiler for Haskell that is written in Haskell.-->
316
317 *       [Daniel Friedman. A Schemer's View of Monads](/schemersviewofmonads.ps): from <https://www.cs.indiana.edu/cgi-pub/c311/doku.php?id=home> but hosted the link above is to a local copy.
318
319 There's a long list of monad tutorials on the [[Offsite Reading]] page. Skimming the titles makes me laugh.
320
321 In the presentation we gave above---which follows the functional programming conventions---we took `unit`/return and `bind` as the primitive operations. From these a number of other general monad operations can be derived. It's also possible to take some of the others as primitive. The [Monads in Category
322 Theory](/advanced_notes/monads_in_category_theory) notes do so, for example.
323
324 Here are some of the other general monad operations. You don't have to master these; they're collected here for your reference.
325
326 You may sometimes see:
327
328         u >> v
329
330 That just means:
331
332         u >>= fun _ -> v
333
334 that is:
335
336         bind u (fun _ -> v)
337
338 You could also do `bind u (fun x -> v)`; we use the `_` for the function argument to be explicit that that argument is never going to be used.
339
340 The `lift` operation we asked you to define for last week's homework is a common operation. The second argument to `bind` converts `'a` values into `'b m` values---that is, into instances of the monadic type. What if we instead had a function that merely converts `'a` values into `'b` values, and we want to use it with our monadic type. Then we "lift" that function into an operation on the monad. For example:
341
342         # let even x = (x mod 2 = 0);;
343         val g : int -> bool = <fun>
344
345 `even` has the type `int -> bool`. Now what if we want to convert it into an operation on the option/maybe monad?
346
347         # let lift g = fun u -> bind u (fun x -> Some (g x));;
348         val lift : ('a -> 'b) -> 'a option -> 'b option = <fun>
349
350 `lift even` will now be a function from `int option`s to `bool option`s. We can
351 also define a lift operation for binary functions:
352
353         # let lift2 g = fun u v -> bind u (fun x -> bind v (fun y -> Some (g x y)));;
354         val lift2 : ('a -> 'b -> 'c) -> 'a option -> 'b option -> 'c option = <fun>
355
356 `lift2 (+)` will now be a function from `int option`s  and `int option`s to `int option`s. This should look familiar to those who did the homework.
357
358 The `lift` operation (just `lift`, not `lift2`) is sometimes also called the `map` operation. (In Haskell, they say `fmap` or `<$>`.) And indeed when we're working with the list monad, `lift f` is exactly `List.map f`!
359
360 Wherever we have a well-defined monad, we can define a lift/map operation for that monad. The examples above used `Some (g x)` and so on; in the general case we'd use `unit (g x)`, using the specific `unit` operation for the monad we're working with.
361
362 In general, any lift/map operation can be relied on to satisfy these laws:
363
364         * lift id = id
365         * lift (compose f g) = compose (lift f) (lift g)
366
367 where `id` is `fun x -> x` and `compose f g` is `fun x -> f (g x)`. If you think about the special case of the map operation on lists, this should make sense. `List.map id lst` should give you back `lst` again. And you'd expect these
368 two computations to give the same result:
369
370         List.map (fun x -> f (g x)) lst
371         List.map f (List.map g lst)
372
373 Another general monad operation is called `ap` in Haskell---short for "apply." (They also use `<*>`, but who can remember that?) This works like this:
374
375         ap [f] [x; y] = [f x; f y]
376         ap (Some f) (Some x) = Some (f x)
377
378 and so on. Here are the laws that any `ap` operation can be relied on to satisfy:
379
380         ap (unit id) u = u
381         ap (ap (ap (unit compose) u) v) w = ap u (ap v w)
382         ap (unit f) (unit x) = unit (f x)
383         ap u (unit x) = ap (unit (fun f -> f x)) u
384
385 Another general monad operation is called `join`. This is the operation that takes you from an iterated monad to a single monad. Remember when we were explaining the `bind` operation for the list monad, there was a step where
386 we went from:
387
388         [[1]; [1;2]; [1;3]; [1;2;4]]
389
390 to:
391
392         [1; 1; 2; 1; 3; 1; 2; 4]
393
394 That is the `join` operation.
395
396 All of these operations can be defined in terms of `bind` and `unit`; or alternatively, some of them can be taken as primitive and `bind` can be defined in terms of them. Here are various interdefinitions:
397
398         lift f u = u >>= compose unit f
399         lift f u = ap (unit f) u
400         lift2 f u v = u >>= (fun x -> v >>= (fun y -> unit (f x y)))
401         lift2 f u v = ap (lift f u) v = ap (ap (unit f) u) v
402         ap u v = u >>= (fun f -> lift f v)
403         ap u v = lift2 id u v
404         join m2 = m2 >>= id
405         u >>= f = join (lift f u)
406         u >> v = u >>= (fun _ -> v)
407         u >> v = lift2 (fun _ -> id) u v
408
409
410
411 Monad outlook
412 -------------
413
414 We're going to be using monads for a number of different things in the
415 weeks to come.  The first main application will be the State monad,
416 which will enable us to model mutation: variables whose values appear
417 to change as the computation progresses.  Later, we will study the
418 Continuation monad.
419
420 In the meantime, we'll look at several linguistic applications for monads, based
421 on what's called the *reader monad*.
422
423 ##[[Reader monad]]##
424
425 ##[[Intensionality monad]]##
426