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