da4a4f4bf22a3b5b732668356dcc4ddd3fec8185
[lambda.git] / week7.mdwn
1 [[!toc]]
2
3 Monads
4 ------
5
6 Start by (re)reading the discussion of monads in the lecture notes for
7 week 6 [Towards Monads](http://lambda.jimpryor.net//week6/#index4h2).
8 In those notes, we saw a way to separate thinking about error
9 conditions (such as trying to divide by zero) from thinking about
10 normal arithmetic computations.  We did this by making use of the
11 option monad: in each place where we had something of type `int`, we
12 put instead something of type `int option`, which is a sum type
13 consisting either of just an integer, or else some special value which
14 we could interpret as signaling that something had gone wrong.
15
16 The goal was to make normal computing as convenient as possible: when
17 we're adding or multiplying, we don't have to worry about generating
18 any new errors, so we do want to think about the difference between
19 `int`s and `int option`s.  We tried to accomplish this by defining a
20 `bind` operator, which enabled us to peel away the `option` husk to get
21 at the delicious integer inside.  There was also a homework problem
22 which made this even more convenient by mapping any binary operation
23 on plain integers into a lifted operation that understands how to deal
24 with `int option`s in a sensible way.
25
26 [Linguitics note: Dividing by zero is supposed to feel like a kind of
27 presupposition failure.  If we wanted to adapt this approach to
28 building a simple account of presupposition projection, we would have
29 to do several things.  First, we would have to make use of the
30 polymorphism of the `option` type.  In the arithmetic example, we only
31 made use of `int option`s, but when we're composing natural language
32 expression meanings, we'll need to use types like `N option`, `Det option`,
33 `VP option`, and so on.  But that works automatically, because we can use
34 any type for the `'a` in `'a option`.  Ultimately, we'd want to have a
35 theory of accommodation, and a theory of the situations in which
36 material within the sentence can satisfy presuppositions for other
37 material that otherwise would trigger a presupposition violation; but,
38 not surprisingly, these refinements will require some more
39 sophisticated techniques than the super-simple option monad.]
40
41 So what exactly is a monad?  As usual, we're not going to be pedantic
42 about it, but for our purposes, 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         it 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 `x` has type `int option`,
50         then `x` 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 `n` by mapping it to
56         the option `Some n`.  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 m f = match m with None -> None | Some n -> f n;;
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 options
96         and end with bool options).
97
98         Intuitively, the interpretation of what `bind` does is like this:
99         the first argument is a monadic value m, 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 m f = (let x = unbox m in f x);;
105
106         The guts of the definition of the `bind` operation amount to
107         specifying how to unbox the monadic value `m`.  In the bind
108         opertor for the option monad, we unboxed the option monad by
109         matching the monadic value `m` with `Some n`---whenever `m`
110         happened to be a box containing an integer `n`, this allowed us to
111         get our hands on that `n` and feed it to `f`.
112
113         If the monadic box didn't contain any ordinary value, then
114         we just 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 `n`, then the `bind` operation would probably
119         be defined so as to make sure that the result of `f n` was also
120         a singing box. If `f` also inserted 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.  With the option monad, we can
129 think of the "safe division" operation
130
131 <pre>
132 # let divide' num den = if den = 0 then None else Some (num/den);;
133 val divide' : int -> int -> int option = <fun>
134 </pre>
135
136 as basically a function from two integers to an integer, except with
137 this little bit of option plumbing on the side.
138
139 A note on notation: Haskell uses the infix operator `>>=` to stand
140 for `bind`. Chris really hates that symbol.  Following Wadler, he prefers to
141 use an infix five-pointed star, or on a keyboard, `*`. Jim on the other hand
142 thinks `>>=` is what the literature uses and students won't be able to
143 avoid it. Moreover, although &#8902; is OK (though not a convention that's been picked up), overloading the multiplication symbol invites its own confusion
144 and Jim feels very uneasy about that. If not `>>=` then we should use
145 some other unfamiliar infix symbol (but `>>=` already is such...)
146
147 In any case, the course leaders will work this out somehow. In the meantime,
148 as you read around, wherever you see `m >>= f`, that means `bind m f`. Also,
149 if you ever see this notation:
150
151         do
152                 x <- m
153                 f x
154
155 That's a Haskell shorthand for `m >>= (\x -> f x)`, that is, `bind m f`.
156 Similarly:
157
158         do
159                 x <- m
160                 y <- n
161                 f x y
162
163 is shorthand for `m >>= (\x -> n >>= (\y -> f x y))`, that is, `bind m (fun x
164 -> bind n (fun y -> f x y))`. Those who did last week's homework may recognize
165 this.
166
167 (Note that the above "do" notation comes from Haskell. We're mentioning it here
168 because you're likely to see it when reading about monads. It won't work in
169 OCaml. In fact, the `<-` symbol already means something different in OCaml,
170 having to do with mutable record fields. We'll be discussing mutation someday
171 soon.)
172
173
174 The Monad laws
175 --------------
176
177 Just like good robots, monads must obey three laws designed to prevent
178 them from hurting the people that use them or themselves.
179
180 *       **Left identity: unit is a left identity for the bind operation.**
181         That is, for all `f:'a -> 'a m`, where `'a m` is a monadic
182         object, we have `(unit x) * f == f x`.  For instance, `unit` is a
183         function of type `'a -> 'a option`, so we have
184
185 <pre>
186 # let ( * ) m f = match m with None -> None | Some n -> f n;;
187 val ( * ) : 'a option -> ('a -> 'b option) -> 'b option = <fun>
188 # let unit x = Some x;;
189 val unit : 'a -> 'a option = <fun>
190
191 # unit 2;;
192 - : int option = Some 2
193 # unit 2 * unit;;
194 - : int option = Some 2
195
196 # divide 6 2;;
197 - : int option = Some 3
198 # unit 2 * divide 6;;
199 - : int option = Some 3
200
201 # divide 6 0;;
202 - : int option = None
203 # unit 0 * divide 6;;
204 - : int option = None
205 </pre>
206
207 The parentheses is the magic for telling OCaml that the
208 function to be defined (in this case, the name of the function
209 is `*`, pronounced "bind") is an infix operator, so we write
210 `m * f` or `( * ) m f` instead of `* m f`.
211
212 *       **Associativity: bind obeys a kind of associativity**. Like this:
213
214                 (m * f) * g == m * (fun x -> f x * g)
215
216         If you don't understand why the lambda form is necessary (the "fun
217         x" part), you need to look again at the type of bind.
218
219         Some examples of associativity in the option monad:
220
221 <pre>
222 # Some 3 * unit * unit;; 
223 - : int option = Some 3
224 # Some 3 * (fun x -> unit x * unit);;
225 - : int option = Some 3
226
227 # Some 3 * divide 6 * divide 2;;
228 - : int option = Some 1
229 # Some 3 * (fun x -> divide 6 x * divide 2);;
230 - : int option = Some 1
231
232 # Some 3 * divide 2 * divide 6;;
233 - : int option = None
234 # Some 3 * (fun x -> divide 2 x * divide 6);;
235 - : int option = None
236 </pre>
237
238 Of course, associativity must hold for arbitrary functions of
239 type `'a -> 'a m`, where `m` is the monad type.  It's easy to
240 convince yourself that the bind operation for the option monad
241 obeys associativity by dividing the inputs into cases: if `m`
242 matches `None`, both computations will result in `None`; if
243 `m` matches `Some n`, and `f n` evalutes to `None`, then both
244 computations will again result in `None`; and if the value of
245 `f n` matches `Some r`, then both computations will evaluate
246 to `g r`.
247
248 *       **Right identity: unit is a right identity for bind.**  That is, 
249         `m * unit == m` for all monad objects `m`.  For instance,
250
251 <pre>
252 # Some 3 * unit;;
253 - : int option = Some 3
254 # None * unit;;
255 - : 'a option = None
256 </pre>
257
258 Now, if you studied algebra, you'll remember that a *monoid* is an
259 associative operation with a left and right identity.  For instance,
260 the natural numbers along with multiplication form a monoid with 1
261 serving as the left and right identity.  That is, temporarily using
262 `*` to mean arithmetic multiplication, `1 * n == n == n * 1` for all
263 `n`, and `(a * b) * c == a * (b * c)` for all `a`, `b`, and `c`.  As
264 presented here, a monad is not exactly a monoid, because (unlike the
265 arguments of a monoid operation) the two arguments of the bind are of
266 different types.  But if we generalize bind so that both arguments are
267 of type `'a -> 'a m`, then we get plain identity laws and
268 associativity laws, and the monad laws are exactly like the monoid
269 laws (see <http://www.haskell.org/haskellwiki/Monad_Laws>, near the bottom).
270
271
272 Monad outlook
273 -------------
274
275 We're going to be using monads for a number of different things in the
276 weeks to come.  The first main application will be the State monad,
277 which will enable us to model mutation: variables whose values appear
278 to change as the computation progresses.  Later, we will study the
279 Continuation monad.
280
281 The intensionality monad
282 ------------------------
283
284 In the meantime, we'll see a linguistic application for monads:
285 intensional function application.  In Shan (2001) [Monads for natural
286 language semantics](http://arxiv.org/abs/cs/0205026v1), Ken shows that
287 making expressions sensitive to the world of evaluation is
288 conceptually the same thing as making use of a *reader monad* (which
289 we'll see again soon).  This technique was beautifully re-invented
290 by Ben-Avi and Winter (2007) in their paper [A modular
291 approach to
292 intensionality](http://parles.upf.es/glif/pub/sub11/individual/bena_wint.pdf),
293 though without explicitly using monads.
294
295 All of the code in the discussion below can be found here: [[intensionality-monad.ml]].
296 To run it, download the file, start OCaml, and say 
297
298         # #use "intensionality-monad.ml";;
299
300 Note the extra `#` attached to the directive `use`.
301
302 Here's the idea: since people can have different attitudes towards
303 different propositions that happen to have the same truth value, we
304 can't have sentences denoting simple truth values.  If we did, then if John
305 believed that the earth was round, it would force him to believe
306 Fermat's last theorem holds, since both propositions are equally true.
307 The traditional solution is to allow sentences to denote a function
308 from worlds to truth values, what Montague called an intension.  
309 So if `s` is the type of possible worlds, we have the following
310 situation:
311
312
313 <pre>
314 Extensional types                 Intensional types       Examples
315 -------------------------------------------------------------------
316
317 S         s->t                    s->t                    John left
318 DP        s->e                    s->e                    John
319 VP        s->e->t                 s->(s->e)->t            left
320 Vt        s->e->e->t              s->(s->e)->(s->e)->t    saw
321 Vs        s->t->e->t              s->(s->t)->(s->e)->t    thought
322 </pre>
323
324 This system is modeled on the way Montague arranged his grammar.
325 There are significant simplifications: for instance, determiner
326 phrases are thought of as corresponding to individuals rather than to
327 generalized quantifiers.  If you're curious about the initial `s`'s
328 in the extensional types, they're there because the behavior of these
329 expressions depends on which world they're evaluated at.  If you are
330 in a situation in which you can hold the evaluation world constant,
331 you can further simplify the extensional types.  Usually, the
332 dependence of the extension of an expression on the evaluation world
333 is hidden in a superscript, or built into the lexical interpretation
334 function.
335
336 The main difference between the intensional types and the extensional
337 types is that in the intensional types, the arguments are functions
338 from worlds to extensions: intransitive verb phrases like "left" now
339 take intensional concepts as arguments (type s->e) rather than plain
340 individuals (type e), and attitude verbs like "think" now take
341 propositions (type s->t) rather than truth values (type t).
342
343 The intenstional types are more complicated than the intensional
344 types.  Wouldn't it be nice to keep the complicated types to just
345 those attitude verbs that need to worry about intensions, and keep the
346 rest of the grammar as extensional as possible?  This desire is
347 parallel to our earlier desire to limit the concern about division by
348 zero to the division function, and let the other functions, like
349 addition or multiplication, ignore division-by-zero problems as much
350 as possible.
351
352 So here's what we do:
353
354 In OCaml, we'll use integers to model possible worlds:
355
356         type s = int;;
357         type e = char;;
358         type t = bool;;
359
360 Characters (characters in the computational sense, i.e., letters like
361 `'a'` and `'b'`, not Kaplanian characters) will model individuals, and
362 OCaml booleans will serve for truth values.
363
364 <pre>
365 type 'a intension = s -> 'a;;
366 let unit x (w:s) = x;;
367
368 let ann = unit 'a';;
369 let bill = unit 'b';;
370 let cam = unit 'c';;
371 </pre>
372
373 In our monad, the intension of an extensional type `'a` is `s -> 'a`,
374 a function from worlds to extensions.  Our unit will be the constant
375 function (an instance of the K combinator) that returns the same
376 individual at each world.
377
378 Then `ann = unit 'a'` is a rigid designator: a constant function from
379 worlds to individuals that returns `'a'` no matter which world is used
380 as an argument.
381
382 Let's test compliance with the left identity law:
383
384 <pre>
385 # let bind m f (w:s) = f (m w) w;;
386 val bind : (s -> 'a) -> ('a -> s -> 'b) -> s -> 'b = <fun>
387 # bind (unit 'a') unit 1;;
388 - : char = 'a'
389 </pre>
390
391 We'll assume that this and the other laws always hold.
392
393 We now build up some extensional meanings:
394
395         let left w x = match (w,x) with (2,'c') -> false | _ -> true;;
396
397 This function says that everyone always left, except for Cam in world
398 2 (i.e., `left 2 'c' == false`).
399
400 Then the way to evaluate an extensional sentence is to determine the
401 extension of the verb phrase, and then apply that extension to the
402 extension of the subject:
403
404 <pre>
405 let extapp fn arg w = fn w (arg w);;
406
407 extapp left ann 1;;
408 # - : bool = true
409
410 extapp left cam 2;;
411 # - : bool = false
412 </pre>
413
414 `extapp` stands for "extensional function application".
415 So Ann left in world 1, but Cam didn't leave in world 2.
416
417 A transitive predicate:
418
419         let saw w x y = (w < 2) && (y < x);;
420         extapp (extapp saw bill) ann 1;; (* true *)
421         extapp (extapp saw bill) ann 2;; (* false *)
422
423 In world 1, Ann saw Bill and Cam, and Bill saw Cam.  No one saw anyone
424 in world two.
425
426 Good.  Now for intensions:
427
428         let intapp fn arg w = fn w arg;;
429
430 The only difference between intensional application and extensional
431 application is that we don't feed the evaluation world to the argument.
432 (See Montague's rules of (intensional) functional application, T4 -- T10.)
433 In other words, instead of taking an extension as an argument,
434 Montague's predicates take a full-blown intension.  
435
436 But for so-called extensional predicates like "left" and "saw", 
437 the extra power is not used.  We'd like to define intensional versions
438 of these predicates that depend only on their extensional essence.
439 Just as we used bind to define a version of addition that interacted
440 with the option monad, we now use bind to intensionalize an
441 extensional verb:
442
443 <pre>
444 let lift pred w arg = bind arg (fun x w -> pred w x) w;;
445
446 intapp (lift left) ann 1;; (* true: Ann still left in world 1 *)
447 intapp (lift left) cam 2;; (* false: Cam still didn't leave in world 2 *)
448 </pre>
449
450 Because `bind` unwraps the intensionality of the argument, when the
451 lifted "left" receives an individual concept (e.g., `unit 'a'`) as
452 argument, it's the extension of the individual concept (i.e., `'a'`)
453 that gets fed to the basic extensional version of "left".  (For those
454 of you who know Montague's PTQ, this use of bind captures Montague's
455 third meaning postulate.)
456
457 Likewise for extensional transitive predicates like "saw":
458
459 <pre>
460 let lift2 pred w arg1 arg2 = 
461   bind arg1 (fun x -> bind arg2 (fun y w -> pred w x y)) w;;
462 intapp (intapp (lift2 saw) bill) ann 1;;  (* true: Ann saw Bill in world 1 *)
463 intapp (intapp (lift2 saw) bill) ann 2;;  (* false: No one saw anyone in world 2 *)
464 </pre>
465
466 Crucially, an intensional predicate does not use `bind` to consume its
467 arguments.  Attitude verbs like "thought" are intensional with respect
468 to their sentential complement, but extensional with respect to their
469 subject (as Montague noticed, almost all verbs in English are
470 extensional with respect to their subject; a possible exception is "appear"):
471
472 <pre>
473 let think (w:s) (p:s->t) (x:e) = 
474   match (x, p 2) with ('a', false) -> false | _ -> p w;;
475 </pre>
476
477 Ann disbelieves any proposition that is false in world 2.  Apparently,
478 she firmly believes we're in world 2.  Everyone else believes a
479 proposition iff that proposition is true in the world of evaluation.
480
481 <pre>
482 intapp (lift (intapp think
483                      (intapp (lift left)
484                              (unit 'b'))))
485        (unit 'a') 
486 1;; (* true *)
487 </pre>
488
489 So in world 1, Ann thinks that Bill left (because in world 2, Bill did leave).
490
491 The `lift` is there because "think Bill left" is extensional wrt its
492 subject.  The important bit is that "think" takes the intension of
493 "Bill left" as its first argument.
494
495 <pre>
496 intapp (lift (intapp think
497                      (intapp (lift left)
498                              (unit 'c'))))
499        (unit 'a') 
500 1;; (* false *)
501 </pre>
502
503 But even in world 1, Ann doesn't believe that Cam left (even though he
504 did: `intapp (lift left) cam 1 == true`).  Ann's thoughts are hung up
505 on what is happening in world 2, where Cam doesn't leave.
506
507 *Small project*: add intersective ("red") and non-intersective
508  adjectives ("good") to the fragment.  The intersective adjectives
509  will be extensional with respect to the nominal they combine with
510  (using bind), and the non-intersective adjectives will take
511  intensional arguments.
512
513 Finally, note that within an intensional grammar, extensional funtion
514 application is essentially just bind:
515
516 <pre>
517 # let swap f x y = f y x;;
518 # bind cam (swap left) 2;;
519 - : bool = false
520 </pre>