(no commit message)
[lambda.git] / week7.mdwn
1 [[!toc]]
2
3
4 Towards Monads: Safe division
5 -----------------------------
6
7 [This section used to be near the end of the lecture notes for week 6]
8
9 We begin by reasoning about what should happen when someone tries to
10 divide by zero.  This will lead us to a general programming technique
11 called a *monad*, which we'll see in many guises in the weeks to come.
12
13 Integer division presupposes that its second argument
14 (the divisor) is not zero, upon pain of presupposition failure.
15 Here's what my OCaml interpreter says:
16
17     # 12/0;;
18     Exception: Division_by_zero.
19
20 So we want to explicitly allow for the possibility that
21 division will return something other than a number.
22 We'll use OCaml's `option` type, which works like this:
23
24     # type 'a option = None | Some of 'a;;
25     # None;;
26     - : 'a option = None
27     # Some 3;;
28     - : int option = Some 3
29
30 So if a division is normal, we return some number, but if the divisor is
31 zero, we return `None`. As a mnemonic aid, we'll append a `'` to the end of our new divide function.
32
33 <pre>
34 let div' (x:int) (y:int) =
35   match y with
36           0 -> None
37     | _ -> Some (x / y);;
38
39 (*
40 val div' : int -> int -> int option = fun
41 # div' 12 2;;
42 - : int option = Some 6
43 # div' 12 0;;
44 - : int option = None
45 # div' (div' 12 2) 3;;
46 Characters 4-14:
47   div' (div' 12 2) 3;;
48         ^^^^^^^^^^
49 Error: This expression has type int option
50        but an expression was expected of type int
51 *)
52 </pre>
53
54 This starts off well: dividing 12 by 2, no problem; dividing 12 by 0,
55 just the behavior we were hoping for.  But we want to be able to use
56 the output of the safe-division function as input for further division
57 operations.  So we have to jack up the types of the inputs:
58
59 <pre>
60 let div' (u:int option) (v:int option) =
61   match u with
62           None -> None
63         | Some x -> (match v with
64                                   Some 0 -> None
65                                 | Some y -> Some (x / y));;
66
67 (*
68 val div' : int option -> int option -> int option = <fun>
69 # div' (Some 12) (Some 2);;
70 - : int option = Some 6
71 # div' (Some 12) (Some 0);;
72 - : int option = None
73 # div' (div' (Some 12) (Some 0)) (Some 3);;
74 - : int option = None
75 *)
76 </pre>
77
78 Beautiful, just what we need: now we can try to divide by anything we
79 want, without fear that we're going to trigger any system errors.
80
81 I prefer to line up the `match` alternatives by using OCaml's
82 built-in tuple type:
83
84 <pre>
85 let div' (u:int option) (v:int option) =
86   match (u, v) with
87           (None, _) -> None
88     | (_, None) -> None
89     | (_, Some 0) -> None
90         | (Some x, Some y) -> Some (x / y);;
91 </pre>
92
93 So far so good.  But what if we want to combine division with
94 other arithmetic operations?  We need to make those other operations
95 aware of the possibility that one of their arguments has triggered a
96 presupposition failure:
97
98 <pre>
99 let add' (u:int option) (v:int option) =
100   match (u, v) with
101           (None, _) -> None
102     | (_, None) -> None
103     | (Some x, Some y) -> Some (x + y);;
104
105 (*
106 val add' : int option -> int option -> int option = <fun>
107 # add' (Some 12) (Some 4);;
108 - : int option = Some 16
109 # add' (div' (Some 12) (Some 0)) (Some 4);;
110 - : int option = None
111 *)
112 </pre>
113
114 This works, but is somewhat disappointing: the `add'` operation
115 doesn't trigger any presupposition of its own, so it is a shame that
116 it needs to be adjusted because someone else might make trouble.
117
118 But we can automate the adjustment.  The standard way in OCaml,
119 Haskell, etc., is to define a `bind` operator (the name `bind` is not
120 well chosen to resonate with linguists, but what can you do). To continue our mnemonic association, we'll put a `'` after the name "bind" as well.
121
122 <pre>
123 let bind' (u: int option) (f: int -> (int option)) =
124   match u with
125           None -> None
126     | Some x -> f x;;
127
128 let add' (u: int option) (v: int option)  =
129   bind' u (fun x -> bind' v (fun y -> Some (x + y)));;
130
131 let div' (u: int option) (v: int option) =
132   bind' u (fun x -> bind' v (fun y -> if (0 = y) then None else Some (x / y)));;
133
134 (*
135 #  div' (div' (Some 12) (Some 2)) (Some 3);;
136 - : int option = Some 2
137 #  div' (div' (Some 12) (Some 0)) (Some 3);;
138 - : int option = None
139 # add' (div' (Some 12) (Some 0)) (Some 3);;
140 - : int option = None
141 *)
142 </pre>
143
144 Compare the new definitions of `add'` and `div'` closely: the definition
145 for `add'` shows what it looks like to equip an ordinary operation to
146 survive in dangerous presupposition-filled world.  Note that the new
147 definition of `add'` does not need to test whether its arguments are
148 None objects or real numbers---those details are hidden inside of the
149 `bind'` function.
150
151 The definition of `div'` shows exactly what extra needs to be said in
152 order to trigger the no-division-by-zero presupposition.
153
154 [Linguitics note: Dividing by zero is supposed to feel like a kind of
155 presupposition failure.  If we wanted to adapt this approach to
156 building a simple account of presupposition projection, we would have
157 to do several things.  First, we would have to make use of the
158 polymorphism of the `option` type.  In the arithmetic example, we only
159 made use of `int option`s, but when we're composing natural language
160 expression meanings, we'll need to use types like `N option`, `Det option`,
161 `VP option`, and so on.  But that works automatically, because we can use
162 any type for the `'a` in `'a option`.  Ultimately, we'd want to have a
163 theory of accommodation, and a theory of the situations in which
164 material within the sentence can satisfy presuppositions for other
165 material that otherwise would trigger a presupposition violation; but,
166 not surprisingly, these refinements will require some more
167 sophisticated techniques than the super-simple option monad.]
168
169
170 Monads in General
171 -----------------
172
173 We've just seen a way to separate thinking about error conditions
174 (such as trying to divide by zero) from thinking about normal
175 arithmetic computations.  We did this by making use of the `option`
176 type: in each place where we had something of type `int`, we put
177 instead something of type `int option`, which is a sum type consisting
178 either of one choice with an `int` payload, or else a `None` choice
179 which we interpret as signaling that something has gone wrong.
180
181 The goal was to make normal computing as convenient as possible: when
182 we're adding or multiplying, we don't have to worry about generating
183 any new errors, so we would rather not think about the difference
184 between `int`s and `int option`s.  We tried to accomplish this by
185 defining a `bind` operator, which enabled us to peel away the `option`
186 husk to get at the delicious integer inside.  There was also a
187 homework problem which made this even more convenient by defining a
188 `lift` operator that mapped any binary operation on plain integers
189 into a lifted operation that understands how to deal with `int
190 option`s in a sensible way.
191
192 So what exactly is a monad?  We can consider a monad to be a system
193 that provides at least the following three elements:
194
195 *       A complex type that's built around some more basic type. Usually
196         the complex type will be polymorphic, and so can apply to different basic types.
197         In our division example, the polymorphism of the `'a option` type
198         provides a way of building an option out of any other type of object.
199         People often use a container metaphor: if `u` has type `int option`,
200         then `u` is a box that (may) contain an integer.
201
202                 type 'a option = None | Some of 'a;;
203
204 *       A way to turn an ordinary value into a monadic value.  In OCaml, we
205         did this for any integer `x` by mapping it to
206         the option `Some x`.  In the general case, this operation is
207         known as `unit` or `return.` Both of those names are terrible. This
208         operation is only very loosely connected to the `unit` type we were
209         discussing earlier (whose value is written `()`). It's also only
210         very loosely connected to the "return" keyword in many other
211         programming languages like C. But these are the names that the literature
212         uses.  [The rationale for "unit" comes from the monad laws
213         (see below), where the unit function serves as an identity,
214         just like the unit number (i.e., 1) serves as the identity
215         object for multiplication.  The rationale for "return" comes
216         from a misguided desire to resonate with C programmers and
217         other imperative types.]
218
219         The unit/return operation is a way of lifting an ordinary object into
220         the monadic box you've defined, in the simplest way possible. You can think
221         of the singleton function as an example: it takes an ordinary object
222         and returns a set containing that object. In the example we've been
223         considering:
224
225                 let unit x = Some x;;
226                 val unit : 'a -> 'a option = <fun>
227
228         So `unit` is a way to put something inside of a monadic box. It's crucial
229         to the usefulness of monads that there will be monadic boxes that
230         aren't the result of that operation. In the option/maybe monad, for
231         instance, there's also the empty box `None`. In another (whimsical)
232         example, you might have, in addition to boxes merely containing integers,
233         special boxes that contain integers and also sing a song when they're opened. 
234
235         The unit/return operation will always be the simplest, conceptually
236         most straightforward way to lift an ordinary value into a monadic value
237         of the monadic type in question.
238
239 *       Thirdly, an operation that's often called `bind`. As we said before, this is another
240         unfortunate name: this operation is only very loosely connected to
241         what linguists usually mean by "binding." In our option/maybe monad, the
242         bind operation is:
243
244                 let bind u f = match u with None -> None | Some x -> f x;;
245                 val bind : 'a option -> ('a -> 'b option) -> 'b option = <fun>
246
247         Note the type: `bind` takes two arguments: first, a monadic box
248         (in this case, an `'a option`); and second, a function from
249         ordinary objects to monadic boxes. `bind` then returns a monadic
250         value: in this case, a `'b option` (you can start with, e.g., `int option`s
251         and end with `bool option`s).
252
253         Intuitively, the interpretation of what `bind` does is this:
254         the first argument is a monadic value `u`, which 
255         evaluates to a box that (maybe) contains some ordinary value, call it `x`.
256         Then the second argument uses `x` to compute a new monadic
257         value.  Conceptually, then, we have
258
259                 let bind u f = (let x = unbox u in f x);;
260
261         The guts of the definition of the `bind` operation amount to
262         specifying how to unbox the monadic value `u`.  In the `bind`
263         operator for the option monad, we unboxed the monadic value by
264         matching it with the pattern `Some x`---whenever `u`
265         happened to be a box containing an integer `x`, this allowed us to
266         get our hands on that `x` and feed it to `f`.
267
268         If the monadic box didn't contain any ordinary value,
269         we instead pass through the empty box unaltered.
270
271         In a more complicated case, like our whimsical "singing box" example
272         from before, if the monadic value happened to be a singing box
273         containing an integer `x`, then the `bind` operation would probably
274         be defined so as to make sure that the result of `f x` was also
275         a singing box. If `f` also wanted to insert a song, you'd have to decide
276         whether both songs would be carried through, or only one of them.
277         (Are you beginning to realize how wierd and wonderful monads
278         can be?)
279
280         There is no single `bind` function that dictates how this must go.
281         For each new monadic type, this has to be worked out in an
282         useful way.
283
284 So the "option/maybe monad" consists of the polymorphic `option` type, the
285 `unit`/return function, and the `bind` function.
286
287
288 A note on notation: Haskell uses the infix operator `>>=` to stand for
289 `bind`: wherever you see `u >>= f`, that means `bind u f`.
290 Wadler uses &#8902;, but that hasn't been widely adopted (unfortunately).
291
292 Also, if you ever see this notation:
293
294         do
295                 x <- u
296                 f x
297
298 That's a Haskell shorthand for `u >>= (\x -> f x)`, that is, `bind u f`.
299 Similarly:
300
301         do
302                 x <- u
303                 y <- v
304                 f x y
305
306 is shorthand for `u >>= (\x -> v >>= (\y -> f x y))`, that is, `bind u
307 (fun x -> bind v (fun y -> f x y))`. Those who did last week's
308 homework may recognize this last expression.  You can think of the
309 notation like this: take the singing box `u` and evaluate it (which
310 includes listening to the song).  Take the int contained in the
311 singing box (the end result of evaluting `u`) and bind the variable
312 `x` to that int.  So `x <- u` means "Sing me up an int, which I'll call
313 `x`".
314
315 (Note that the above "do" notation comes from Haskell. We're mentioning it here
316 because you're likely to see it when reading about monads. It won't work in
317 OCaml. In fact, the `<-` symbol already means something different in OCaml,
318 having to do with mutable record fields. We'll be discussing mutation someday
319 soon.)
320
321 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:
322
323         # type 'a list
324
325 The `unit`/return operation is:
326
327         # let unit x = [x];;
328         val unit : 'a -> 'a list = <fun>
329
330 That is, the simplest way to lift an `'a` into an `'a list` is just to make a
331 singleton list of that `'a`. Finally, the `bind` operation is:
332
333         # let bind u f = List.concat (List.map f u);;
334         val bind : 'a list -> ('a -> 'b list) -> 'b list = <fun>
335         
336 What's going on here? Well, consider `List.map f u` first. This goes through all
337 the members of the list `u`. There may be just a single member, if `u = unit x`
338 for some `x`. Or on the other hand, there may be no members, or many members. In
339 any case, we go through them in turn and feed them to `f`. Anything that gets fed
340 to `f` will be an `'a`. `f` takes those values, and for each one, returns a `'b list`.
341 For example, it might return a list of all that value's divisors. Then we'll
342 have a bunch of `'b list`s. The surrounding `List.concat ( )` converts that bunch
343 of `'b list`s into a single `'b list`:
344
345         # List.concat [[1]; [1;2]; [1;3]; [1;2;4]]
346         - : int list = [1; 1; 2; 1; 3; 1; 2; 4]
347
348 So now we've seen two monads: the option/maybe monad, and the list monad. For any
349 monadic system, there has to be a specification of the complex monad type,
350 which will be parameterized on some simpler type `'a`, and the `unit`/return
351 operation, and the `bind` operation. These will be different for different
352 monadic systems.
353
354 Many monadic systems will also define special-purpose operations that only make
355 sense for that system.
356
357 Although the `unit` and `bind` operation are defined differently for different
358 monadic systems, there are some general rules they always have to follow.
359
360
361 The Monad Laws
362 --------------
363
364 Just like good robots, monads must obey three laws designed to prevent
365 them from hurting the people that use them or themselves.
366
367 *       **Left identity: unit is a left identity for the bind operation.**
368         That is, for all `f:'a -> 'b m`, where `'b m` is a monadic
369         type, we have `(unit x) >>= f == f x`.  For instance, `unit` is itself
370         a function of type `'a -> 'a m`, so we can use it for `f`:
371
372                 # let unit x = Some x;;
373                 val unit : 'a -> 'a option = <fun>
374                 # let ( >>= ) u f = match u with None -> None | Some x -> f x;;
375                 val ( >>= ) : 'a option -> ('a -> 'b option) -> 'b option = <fun>
376
377         The parentheses is the magic for telling OCaml that the
378         function to be defined (in this case, the name of the function
379         is `>>=`, pronounced "bind") is an infix operator, so we write
380         `u >>= f` or equivalently `( >>= ) u f` instead of `>>= u
381         f`.
382
383                 # unit 2;;
384                 - : int option = Some 2
385                 # unit 2 >>= unit;;
386                 - : int option = Some 2
387
388         Now, for a less trivial instance of a function from `int`s to `int option`s:
389
390                 # let divide x y = if 0 = y then None else Some (x/y);;
391                 val divide : int -> int -> int option = <fun>
392                 # divide 6 2;;
393                 - : int option = Some 3
394                 # unit 2 >>= divide 6;;
395                 - : int option = Some 3
396
397                 # divide 6 0;;
398                 - : int option = None
399                 # unit 0 >>= divide 6;;
400                 - : int option = None
401
402
403 *       **Associativity: bind obeys a kind of associativity**. Like this:
404
405                 (u >>= f) >>= g == u >>= (fun x -> f x >>= g)
406
407         If you don't understand why the lambda form is necessary (the
408         "fun x -> ..." part), you need to look again at the type of `bind`.
409
410         Some examples of associativity in the option monad (bear in
411         mind that in the Ocaml implementation of integer division, 2/3
412         evaluates to zero, throwing away the remainder):
413
414                 # Some 3 >>= unit >>= unit;; 
415                 - : int option = Some 3
416                 # Some 3 >>= (fun x -> unit x >>= unit);;
417                 - : int option = Some 3
418
419                 # Some 3 >>= divide 6 >>= divide 2;;
420                 - : int option = Some 1
421                 # Some 3 >>= (fun x -> divide 6 x >>= divide 2);;
422                 - : int option = Some 1
423
424                 # Some 3 >>= divide 2 >>= divide 6;;
425                 - : int option = None
426                 # Some 3 >>= (fun x -> divide 2 x >>= divide 6);;
427                 - : int option = None
428
429 Of course, associativity must hold for *arbitrary* functions of
430 type `'a -> 'b m`, where `m` is the monad type.  It's easy to
431 convince yourself that the `bind` operation for the option monad
432 obeys associativity by dividing the inputs into cases: if `u`
433 matches `None`, both computations will result in `None`; if
434 `u` matches `Some x`, and `f x` evalutes to `None`, then both
435 computations will again result in `None`; and if the value of
436 `f x` matches `Some y`, then both computations will evaluate
437 to `g y`.
438
439 *       **Right identity: unit is a right identity for bind.**  That is, 
440         `u >>= unit == u` for all monad objects `u`.  For instance,
441
442                 # Some 3 >>= unit;;
443                 - : int option = Some 3
444                 # None >>= unit;;
445                 - : 'a option = None
446
447
448 More details about monads
449 -------------------------
450
451 If you studied algebra, you'll remember that a *monoid* is an
452 associative operation with a left and right identity.  For instance,
453 the natural numbers along with multiplication form a monoid with 1
454 serving as the left and right identity.  That is, `1 * u == u == u * 1` for all
455 `u`, and `(u * v) * w == u * (v * w)` for all `u`, `v`, and `w`.  As
456 presented here, a monad is not exactly a monoid, because (unlike the
457 arguments of a monoid operation) the two arguments of the bind are of
458 different types.  But it's possible to make the connection between
459 monads and monoids much closer. This is discussed in [Monads in Category
460 Theory](/advanced_topics/monads_in_category_theory).
461 See also <http://www.haskell.org/haskellwiki/Monad_Laws>.
462
463 Here are some papers that introduced monads into functional programming:
464
465 *       [Eugenio Moggi, Notions of Computation and Monads](http://www.disi.unige.it/person/MoggiE/ftp/ic91.pdf): Information and Computation 93 (1) 1991. Would be very difficult reading for members of this seminar. However, the following two papers should be accessible.
466
467 *       [Philip Wadler. The essence of functional programming](http://homepages.inf.ed.ac.uk/wadler/papers/essence/essence.ps):
468 invited talk, *19'th Symposium on Principles of Programming Languages*, ACM Press, Albuquerque, January 1992.
469 <!--    This paper explores the use monads to structure functional programs. No prior knowledge of monads or category theory is required.
470         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.
471         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.-->
472
473 *       [Philip Wadler. Monads for Functional Programming](http://homepages.inf.ed.ac.uk/wadler/papers/marktoberdorf/baastad.pdf):
474 in M. Broy, editor, *Marktoberdorf Summer School on Program Design
475 Calculi*, Springer Verlag, NATO ASI Series F: Computer and systems
476 sciences, Volume 118, August 1992. Also in J. Jeuring and E. Meijer,
477 editors, *Advanced Functional Programming*, Springer Verlag, 
478 LNCS 925, 1995. Some errata fixed August 2001.
479 <!--    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.-->
480
481
482 There's a long list of monad tutorials on the [[Offsite Reading]] page. (Skimming the titles is somewhat amusing.) If you are confused by monads, make use of these resources. Read around until you find a tutorial pitched at a level that's helpful for you.
483
484 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
485 Theory](/advanced_topics/monads_in_category_theory) notes do so, for example.
486
487 Here are some of the other general monad operations. You don't have to master these; they're collected here for your reference.
488
489 You may sometimes see:
490
491         u >> v
492
493 That just means:
494
495         u >>= fun _ -> v
496
497 that is:
498
499         bind u (fun _ -> v)
500
501 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.
502
503 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:
504
505         # let even x = (x mod 2 = 0);;
506         val g : int -> bool = <fun>
507
508 `even` has the type `int -> bool`. Now what if we want to convert it into an operation on the option/maybe monad?
509
510         # let lift g = fun u -> bind u (fun x -> Some (g x));;
511         val lift : ('a -> 'b) -> 'a option -> 'b option = <fun>
512
513 `lift even` will now be a function from `int option`s to `bool option`s. We can
514 also define a lift operation for binary functions:
515
516         # let lift2 g = fun u v -> bind u (fun x -> bind v (fun y -> Some (g x y)));;
517         val lift2 : ('a -> 'b -> 'c) -> 'a option -> 'b option -> 'c option = <fun>
518
519 `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.
520
521 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`!
522
523 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.
524
525 In general, any lift/map operation can be relied on to satisfy these laws:
526
527         * lift id = id
528         * lift (compose f g) = compose (lift f) (lift g)
529
530 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
531 two computations to give the same result:
532
533         List.map (fun x -> f (g x)) lst
534         List.map f (List.map g lst)
535
536 Another general monad operation is called `ap` in Haskell---short for "apply." (They also use `<*>`, but who can remember that?) This works like this:
537
538         ap [f] [x; y] = [f x; f y]
539         ap (Some f) (Some x) = Some (f x)
540
541 and so on. Here are the laws that any `ap` operation can be relied on to satisfy:
542
543         ap (unit id) u = u
544         ap (ap (ap (unit compose) u) v) w = ap u (ap v w)
545         ap (unit f) (unit x) = unit (f x)
546         ap u (unit x) = ap (unit (fun f -> f x)) u
547
548 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
549 we went from:
550
551         [[1]; [1;2]; [1;3]; [1;2;4]]
552
553 to:
554
555         [1; 1; 2; 1; 3; 1; 2; 4]
556
557 That is the `join` operation.
558
559 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:
560
561         lift f u = u >>= compose unit f
562         lift f u = ap (unit f) u
563         lift2 f u v = u >>= (fun x -> v >>= (fun y -> unit (f x y)))
564         lift2 f u v = ap (lift f u) v = ap (ap (unit f) u) v
565         ap u v = u >>= (fun f -> lift f v)
566         ap u v = lift2 id u v
567         join m2 = m2 >>= id
568         u >>= f = join (lift f u)
569         u >> v = u >>= (fun _ -> v)
570         u >> v = lift2 (fun _ -> id) u v
571
572
573
574 Monad outlook
575 -------------
576
577 We're going to be using monads for a number of different things in the
578 weeks to come.  One major application will be the State monad,
579 which will enable us to model mutation: variables whose values appear
580 to change as the computation progresses.  Later, we will study the
581 Continuation monad.
582
583 But first, we'll look at several linguistic applications for monads, based
584 on what's called the *Reader monad*.
585
586 ##[[Reader monad for Variable Binding]]##
587
588 ##[[Reader monad for Intensionality]]##
589