tweak index
[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 int`, `Det Int`,
33 `VP int`, 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 way to build a complex type from some basic type.  In the division
46   example, the polymorphism of the `'a option` type provides a way of
47   building an option out of any other type of object.  People often
48   use a container metaphor: if `x` has type `int option`, then `x` is
49   a box that (may) contain an integer.
50
51     `type 'a option = None | Some of 'a;;`
52
53 * A way to turn an ordinary value into a monadic value.  In Ocaml, we
54   did this for any integer `n` by mapping it to
55   the option `Some n`.  To be official, we can define a function
56   called unit:
57
58     `let unit x = Some x;;`
59
60     `val unit : 'a -> 'a option = <fun>`
61
62     So `unit` is a way to put something inside of a box.
63
64 * A bind operation (note the type):
65
66      `let bind m f = match m with None -> None | Some n -> f n;;`
67
68      `val bind : 'a option -> ('a -> 'b option) -> 'b option = <fun>`
69
70      `bind` takes two arguments (a monadic object and a function from
71      ordinary objects to monadic objects), and returns a monadic
72      object.
73
74      Intuitively, the interpretation of what `bind` does is like this:
75      the first argument computes a monadic object m, which will
76      evaluate to a box containing some ordinary value, call it `x`.
77      Then the second argument uses `x` to compute a new monadic
78      value.  Conceptually, then, we have
79
80     `let bind m f = (let x = unwrap m in f x);;`
81
82     The guts of the definition of the `bind` operation amount to
83     specifying how to unwrap the monadic object `m`.  In the bind
84     opertor for the option monad, we unwraped the option monad by
85     matching the monadic object `m` with `Some n`--whenever `m`
86     happend to be a box containing an integer `n`, this allowed us to
87     get our hands on that `n` and feed it to `f`.
88
89 So the "option monad" consists of the polymorphic option type, the
90 unit function, and the bind function.  With the option monad, we can
91 think of the "safe division" operation
92
93 <pre>
94 # let divide num den = if den = 0 then None else Some (num/den);;
95 val divide : int -> int -> int option = <fun>
96 </pre>
97
98 as basically a function from two integers to an integer, except with
99 this little bit of option frill, or option plumbing, on the side.
100
101 A note on notation: Haskell uses the infix operator `>>=` to stand
102 for `bind`.  I really hate that symbol.  Following Wadler, I prefer to
103 infix five-pointed star, or on a keyboard, `*`.
104
105
106 The Monad laws
107 --------------
108
109 Just like good robots, monads must obey three laws designed to prevent
110 them from hurting the people that use them or themselves.
111
112 *    Left identity: unit is a left identity for the bind operation.
113      That is, for all `f:'a -> 'a M`, where `'a M` is a monadic
114      object, we have `(unit x) * f == f x`.  For instance, `unit` is a
115      function of type `'a -> 'a option`, so we have
116
117 <pre>
118 # let ( * ) m f = match m with None -> None | Some n -> f n;;
119 val ( * ) : 'a option -> ('a -> 'b option) -> 'b option = <fun>
120 # let unit x = Some x;;
121 val unit : 'a -> 'a option = <fun>
122
123 # unit 2;;
124 - : int option = Some 2
125 # unit 2 * unit;;
126 - : int option = Some 2
127
128 # divide 6 2;;
129 - : int option = Some 3
130 # unit 2 * divide 6;;
131 - : int option = Some 3
132
133 # divide 6 0;;
134 - : int option = None
135 # unit 0 * divide 6;;
136 - : int option = None
137 </pre>
138
139 The parentheses is the magic for telling Ocaml that the
140 function to be defined (in this case, the name of the function
141 is `*`, pronounced "bind") is an infix operator, so we write
142 `m * f` or `( * ) m f` instead of `* m f`.
143
144 *    Associativity: bind obeys a kind of associativity, like this:
145
146     `(m * f) * g == m * (fun x -> f x * g)`
147
148     If you don't understand why the lambda form is necessary (the "fun
149     x" part), you need to look again at the type of bind.
150
151     Some examples of associativity in the option monad:
152
153 <pre>
154 # Some 3 * unit * unit;; 
155 - : int option = Some 3
156 # Some 3 * (fun x -> unit x * unit);;
157 - : int option = Some 3
158
159 # Some 3 * divide 6 * divide 2;;
160 - : int option = Some 1
161 # Some 3 * (fun x -> divide 6 x * divide 2);;
162 - : int option = Some 1
163
164 # Some 3 * divide 2 * divide 6;;
165 - : int option = None
166 # Some 3 * (fun x -> divide 2 x * divide 6);;
167 - : int option = None
168 </pre>
169
170 Of course, associativity must hold for arbitrary functions of
171 type `'a -> M 'a`, where `M` is the monad type.  It's easy to
172 convince yourself that the bind operation for the option monad
173 obeys associativity by dividing the inputs into cases: if `m`
174 matches `None`, both computations will result in `None`; if
175 `m` matches `Some n`, and `f n` evalutes to `None`, then both
176 computations will again result in `None`; and if the value of
177 `f n` matches `Some r`, then both computations will evaluate
178 to `g r`.
179
180 *    Right identity: unit is a right identity for bind.  That is, 
181      `m * unit == m` for all monad objects `m`.  For instance,
182
183 <pre>
184 # Some 3 * unit;;
185 - : int option = Some 3
186 # None * unit;;
187 - : 'a option = None
188 </pre>
189
190 Now, if you studied algebra, you'll remember that a *monoid* is an
191 associative operation with a left and right identity.  For instance,
192 the natural numbers along with multiplication form a monoid with 1
193 serving as the left and right identity.  That is, temporarily using
194 `*` to mean arithmetic multiplication, `1 * n == n == n * 1` for all
195 `n`, and `(a * b) * c == a * (b * c)` for all `a`, `b`, and `c`.  As
196 presented here, a monad is not exactly a monoid, because (unlike the
197 arguments of a monoid operation) the two arguments of the bind are of
198 different types.  But if we generalize bind so that both arguments are
199 of type `'a -> M 'a`, then we get plain identity laws and
200 associativity laws, and the monad laws are exactly like the monoid
201 laws (see <http://www.haskell.org/haskellwiki/Monad_Laws>, near the bottom).
202
203
204 Monad outlook
205 -------------
206
207 We're going to be using monads for a number of different things in the
208 weeks to come.  The first main application will be the State monad,
209 which will enable us to model mutation: variables whose values appear
210 to change as the computation progresses.  Later, we will study the
211 Continuation monad.
212
213 The intensionality monad
214 ------------------------
215
216 In the meantime, we'll see a linguistic application for monads:
217 intensional function application.  In Shan (2001) [Monads for natural
218 language semantics](http://arxiv.org/abs/cs/0205026v1), Ken shows that
219 making expressions sensitive to the world of evaluation is
220 conceptually the same thing as making use of a *reader monad* (which
221 we'll see again soon).  This technique was beautifully re-invented
222 by Ben-Avi and Winter (2007) in their paper [A modular
223 approach to
224 intensionality](http://parles.upf.es/glif/pub/sub11/individual/bena_wint.pdf),
225 though without explicitly using monads.
226
227 All of the code in the discussion below can be found here: [[intensionality-monad.ml]].
228 To run it, download the file, start Ocaml, and say 
229
230     # #use "intensionality-monad.ml";;
231
232 Note the extra `#` attached to the directive `use`.
233
234 Here's the idea: since people can have different attitudes towards
235 different propositions that happen to have the same truth value, we
236 can't have sentences denoting simple truth values.  If we did, then if John
237 believed that the earth was round, it would force him to believe
238 Fermat's last theorem holds, since both propositions are equally true.
239 The traditional solution is to allow sentences to denote a function
240 from worlds to truth values, what Montague called an intension.  
241 So if `s` is the type of possible worlds, we have the following
242 situation:
243
244
245 <pre>
246 Extensional types                 Intensional types       Examples
247 -------------------------------------------------------------------
248
249 S         s->t                    s->t                    John left
250 DP        s->e                    s->e                    John
251 VP        s->e->t                 s->(s->e)->t            left
252 Vt        s->e->e->t              s->(s->e)->(s->e)->t    saw
253 Vs        s->t->e->t              s->(s->t)->(s->e)->t    thought
254 </pre>
255
256 This system is modeled on the way Montague arranged his grammar.
257 There are significant simplifications: for instance, determiner
258 phrases are thought of as corresponding to individuals rather than to
259 generalized quantifiers.  If you're curious about the initial `s`'s
260 in the extensional types, they're there because the behavior of these
261 expressions depends on which world they're evaluated at.  If you are
262 in a situation in which you can hold the evaluation world constant,
263 you can further simplify the extensional types.  Usually, the
264 dependence of the extension of an expression on the evaluation world
265 is hidden in a superscript, or built into the lexical interpretation
266 function.
267
268 The main difference between the intensional types and the extensional
269 types is that in the intensional types, the arguments are functions
270 from worlds to extensions: intransitive verb phrases like "left" now
271 take intensional concepts as arguments (type s->e) rather than plain
272 individuals (type e), and attitude verbs like "think" now take
273 propositions (type s->t) rather than truth values (type t).
274
275 The intenstional types are more complicated than the intensional
276 types.  Wouldn't it be nice to keep the complicated types to just
277 those attitude verbs that need to worry about intensions, and keep the
278 rest of the grammar as extensional as possible?  This desire is
279 parallel to our earlier desire to limit the concern about division by
280 zero to the division function, and let the other functions, like
281 addition or multiplication, ignore division-by-zero problems as much
282 as possible.
283
284 So here's what we do:
285
286 In Ocaml, we'll use integers to model possible worlds:
287
288     type s = int;;
289     type e = char;;
290     type t = bool;;
291
292 Characters (characters in the computational sense, i.e., letters like
293 `'a'` and `'b'`, not Kaplanian characters) will model individuals, and
294 Ocaml booleans will serve for truth values.
295
296 <pre>
297 type 'a intension = s -> 'a;;
298 let unit x (w:s) = x;;
299
300 let ann = unit 'a';;
301 let bill = unit 'b';;
302 let cam = unit 'c';;
303 </pre>
304
305 In our monad, the intension of an extensional type `'a` is `s -> 'a`,
306 a function from worlds to extensions.  Our unit will be the constant
307 function (an instance of the K combinator) that returns the same
308 individual at each world.
309
310 Then `ann = unit 'a'` is a rigid designator: a constant function from
311 worlds to individuals that returns `'a'` no matter which world is used
312 as an argument.
313
314 Let's test compliance with the left identity law:
315
316 <pre>
317 # let bind m f (w:s) = f (m w) w;;
318 val bind : (s -> 'a) -> ('a -> s -> 'b) -> s -> 'b = <fun>
319 # bind (unit 'a') unit 1;;
320 - : char = 'a'
321 </pre>
322
323 We'll assume that this and the other laws always hold.
324
325 We now build up some extensional meanings:
326
327     let left w x = match (w,x) with (2,'c') -> false | _ -> true;;
328
329 This function says that everyone always left, except for Cam in world
330 2 (i.e., `left 2 'c' == false`).
331
332 Then the way to evaluate an extensional sentence is to determine the
333 extension of the verb phrase, and then apply that extension to the
334 extension of the subject:
335
336 <pre>
337 let extapp fn arg w = fn w (arg w);;
338
339 extapp left ann 1;;
340 # - : bool = true
341
342 extapp left cam 2;;
343 # - : bool = false
344 </pre>
345
346 `extapp` stands for "extensional function application".
347 So Ann left in world 1, but Cam didn't leave in world 2.
348
349 A transitive predicate:
350
351     let saw w x y = (w < 2) && (y < x);;
352     extapp (extapp saw bill) ann 1;; (* true *)
353     extapp (extapp saw bill) ann 2;; (* false *)
354
355 In world 1, Ann saw Bill and Cam, and Bill saw Cam.  No one saw anyone
356 in world two.
357
358 Good.  Now for intensions:
359
360     let intapp fn arg w = fn w arg;;
361
362 The only difference between intensional application and extensional
363 application is that we don't feed the evaluation world to the argument.
364 (See Montague's rules of (intensional) functional application, T4 -- T10.)
365 In other words, instead of taking an extension as an argument,
366 Montague's predicates take a full-blown intension.  
367
368 But for so-called extensional predicates like "left" and "saw", 
369 the extra power is not used.  We'd like to define intensional versions
370 of these predicates that depend only on their extensional essence.
371 Just as we used bind to define a version of addition that interacted
372 with the option monad, we now use bind to intensionalize an
373 extensional verb:
374
375 <pre>
376 let lift pred w arg = bind arg (fun x w -> pred w x) w;;
377
378 intapp (lift left) ann 1;; (* true: Ann still left in world 1 *)
379 intapp (lift left) cam 2;; (* false: Cam still didn't leave in world 2 *)
380 </pre>
381
382 Because `bind` unwraps the intensionality of the argument, when the
383 lifted "left" receives an individual concept (e.g., `unit 'a'`) as
384 argument, it's the extension of the individual concept (i.e., `'a'`)
385 that gets fed to the basic extensional version of "left".  (For those
386 of you who know Montague's PTQ, this use of bind captures Montague's
387 third meaning postulate.)
388
389 Likewise for extensional transitive predicates like "saw":
390
391 <pre>
392 let lift2 pred w arg1 arg2 = 
393   bind arg1 (fun x -> bind arg2 (fun y w -> pred w x y)) w;;
394 intapp (intapp (lift2 saw) bill) ann 1;;  (* true: Ann saw Bill in world 1 *)
395 intapp (intapp (lift2 saw) bill) ann 2;;  (* false: No one saw anyone in world 2 *)
396 </pre>
397
398 Crucially, an intensional predicate does not use `bind` to consume its
399 arguments.  Attitude verbs like "thought" are intensional with respect
400 to their sentential complement, but extensional with respect to their
401 subject (as Montague noticed, almost all verbs in English are
402 extensional with respect to their subject; a possible exception is "appear"):
403
404 <pre>
405 let think (w:s) (p:s->t) (x:e) = 
406   match (x, p 2) with ('a', false) -> false | _ -> p w;;
407 </pre>
408
409 Ann disbelieves any proposition that is false in world 2.  Apparently,
410 she firmly believes we're in world 2.  Everyone else believes a
411 proposition iff that proposition is true in the world of evaluation.
412
413 <pre>
414 intapp (lift (intapp think
415                      (intapp (lift left)
416                              (unit 'b'))))
417        (unit 'a') 
418 1;; (* true *)
419 </pre>
420
421 So in world 1, Ann thinks that Bill left (because in world 2, Bill did leave).
422
423 The `lift` is there because "think Bill left" is extensional wrt its
424 subject.  The important bit is that "think" takes the intension of
425 "Bill left" as its first argument.
426
427 <pre>
428 intapp (lift (intapp think
429                      (intapp (lift left)
430                              (unit 'c'))))
431        (unit 'a') 
432 1;; (* false *)
433 </pre>
434
435 But even in world 1, Ann doesn't believe that Cam left (even though he
436 did: `intapp (lift left) cam 1 == true`).  Ann's thoughts are hung up
437 on what is happening in world 2, where Cam doesn't leave.
438
439 *Small project*: add intersective ("red") and non-intersective
440  adjectives ("good") to the fragment.  The intersective adjectives
441  will be extensional with respect to the nominal they combine with
442  (using bind), and the non-intersective adjectives will take
443  intensional arguments.
444
445 Finally, note that within an intensional grammar, extensional funtion
446 application is essentially just bind:
447
448 <pre>
449 # let swap f x y = f y x;;
450 # bind cam (swap left) 2;;
451 - : bool = false
452 </pre>