2738ec1847e902d32af50ac60f6375349f42db17
[lambda.git] / damn.mdwn
1 Expressives such as "damn" have side effects that don't affect the
2 at-issue value of the sentence in which they occur.  What this claim
3 says is unpacked at some length here: <http://tinyurl.com/cbarker/salt/interaction/salt.pdf>.
4
5 In brief, "The man read the damn book" means the same thing as "The
6 man read the book" as far as what must be the case in the world for
7 the sentence to be true.  However, the sentence with the "damn" in it
8 in addition conveys the claim that something about the described
9 situtation is not as it should be.  (The person who is committed to
10 that claim is whoever utters the sentence.)
11
12 So we need a way of evaluating sentences that allows "damn" to launch
13 a side effect without affecting the truth conditions of the sentence
14 in which it occurs.
15
16 Furthermore, we don't want to change the meaning of "the", "man",
17 "read", or "book"---those elements are completely innocent, and
18 shouldn't be burdened with helping compute affective content.
19
20
21
22 What we did in Monday's seminar:
23
24 We start with a simulation of semantic composition:
25
26         (cons (cons 'the 'man) 
27                   (cons 'read
28                                 (cons 'the
29                                           'book)))
30
31 That evaluates to nested structure of pairs, that Scheme displays as:
32
33         '((the . man) . (read . (the . book)))
34
35 If you try it yourself, you may see instead:
36
37         '((the . man) read the . book)
38
39 This is shorthand for the same thing. Just trust me on that.
40
41 What's going on here?
42
43 `(cons M N)` is a request to build an ordered pair out of the values M and N.
44 Scheme displays that pair as `'(M . N)` You can't write `(M . N)` yourself and expect Scheme to understand that you're talking about this pair. If you tried, to, Scheme would think you're trying to apply the function M to some arguments, which you're not, and also
45 Scheme would be confused by what argument the `.` is supposed to be. So, you say:
46
47         (cons M N)
48
49 and that evaluates to an ordered pair, and Scheme displays that ordered pair as
50
51         '(M . N)
52
53 You *can* write `'(M . N)` (with the prefixed single quote), and Scheme will understand you then. However, we're going to be using that same single quote prefix to do something else in a moment, and I don't want now to explain how these uses are related. So we'll write out `(cons M N)` longhand, and we'll leave the `'(M . N)` notation to Scheme for displaying the pair we built.
54
55 There is an underlying reason why parentheses are used both when displaying the ordered pair, and also to mean "apply this function to these arguments." However, at this point, you may well see this as a confusing overloading of parentheses to fill different syntactic roles.
56
57 Now what about the elements of our ordered pairs. Why do we say `(cons 'the 'man)`. Why are those single quotes there? Well, if you just said `(cons the man)`, Scheme would understand `the` and `man` to be variables, and it would complain that you hadn't bound these variables to any values. We don't want to build an ordered pair out of the values possessed by variables `the` and `man`. Instead, we want to just make up some primitive value THE to stand for the meaning of an object-language determiner, and some primitive value MAN to stand for the meaning of an object-language noun phrase. The notation `'the` is Scheme's way of designating a primitive atomic value. Note there is no closing single quote, only a prefixed one. Scheme calls these primitive atomic values "symbols." That term is a bit misleading, because the symbol `'the` is not the same as the variable `the`. Neither is it the same as what's called the string `"the"`. The latter is a structured value, composed out of three character values. The symbol `'the`, on the other hand, is an atomic value. It has no parts. (The notation the programmer uses to designate this atomic value has four characters, but the value designated itself has no parts.) If you think this is all somewhat confusing, you're right. It gets easier with practice.
58
59 `'the` can also be written `(quote the)`. This is even more confusing, because here the `the` is not interpreted as a variable. (Try `(let* ((the 3)) (quote the))`.) If you come across this, just read `(quote the)` as a verbose (and perhaps misleading) way of writing `'the`, not as the application of any function to any value.
60
61 Okay, so what we've done is just create a bunch of new atomic values `'the`, `'man`, and so on. Scheme doesn't know how to do much with these. It knows for instance that `'the` is the same value as `'the` and a different value than `'man`. But it doesn't know much more than that. That's all we need or want here.
62
63 And we built a tree out of those values, representing the tree by a nested structure of pairs of leaf-labels.
64
65 The program we submitted to Scheme:
66
67         (cons (cons 'the 'man) 
68                   (cons 'read
69                                 (cons 'the
70                                           'book)))
71
72 evaluates to the nested structure of pairs that Scheme displays as:
73
74         '((the . man) . (read . (the . book)))
75
76 and that we can think of as the tree:
77
78                                  /----------------\
79                                 /                  \
80                            /                    \
81                           /                      \
82                          /                        \                                  
83                         / \                      / \
84                    /   \                    /   \
85                   /     \                  /     \
86                  /       \                /       \
87         meaning of   meaning of   meaning of   \
88           "the"        "man"       "read"      / \
89                                                                                   /   \
90                                                                                  /     \
91                                                                                 /       \
92                                                                         meaning of  meaning of
93                                                                           "the"      "book"
94
95 Okay, let's get back to "damn."
96
97 We start by defining `damn` as a "thunk" that when applied to zero arguments returns a trivial adjectival meaning, which we'll designate with the primitive symbol `'id`.
98
99 What's a "thunk"?
100
101 Remember, in Scheme you can have functions that take one value, and also functions that take two values, and also functions that take zero values. The last ones are called "thunks." The thunk is not identical to the value it returns. For instance:
102
103         (lambda () 3)
104
105 is a thunk that returns the integer 3. If we bind the variable `t` to that thunk, then `t` is a function (Scheme will display it as`#<procedure>`)
106 not an integer. Whereas `(t)` is an integer not a function.
107
108 There's no reason yet on hand for us to make `damn` be a thunk. For present purposes, we could also just define `damn` to be the symbol `'id`. But what we're going to go on to do does require us to make `damn` be a thunk. The reason for that is to postpone the evaluation of some expressions until the continuations we want to operate on are in place.
109
110 So for uniformity we're going to make `damn` be a thunk right from the beginning.
111
112 As we said, `damn` starts as a thunk that returns a trivial adjectival meaning `'id`:
113
114         (define damn (lambda () 'id))
115
116 Now we can say:
117
118         (cons (cons 'the 'man) 
119                   (cons 'read
120                                 (cons 'the
121                                           (cons (damn) 
122                                                 'book))))
123
124 and we get back:
125
126         ((the . man) . (read . (the . (id . book))))
127
128 If you try this yourself, you may see instead:
129
130         '((the . man) read the id . book)
131
132
133
134 Now we want to get some affective meaning into damn. So we might try:
135
136
137         (define damn (lambda () 'bad))
138
139 But then:
140
141         (cons (cons 'the 'man) 
142                   (cons 'read
143                                 (cons 'the
144                                           (cons (damn) 
145                                                 'book))))
146
147 gives us:
148
149         ((the . man) . (read . (the . (bad . book))))
150
151 Which is not quite what we're looking for. We don't want to contribute the normal adjectival meaning of "bad" to the proposition asserted. Instead we want badness to be a side-issue linguistic contribution. We might try:
152
153         (define damn (lambda () (cons 'side-effect 'bad)))
154
155 But then we'd get:
156
157         ((the . man) . (read . (the . ((side-effect . bad) . book))))
158
159 and we said at the outset that the context `(the . ( ... . book))` shouldn't need to know how to interact with affective meanings. That's precisely the problem we're trying to solve.
160
161
162 A promising way to handle this is with **continuations**, which you will get much more familiar with as this seminar progresses. Don't worry about not understanding what's going on quite yet. This is just an advertisement that's supposed to provoke your imagination.
163
164 Chris and others have applied the apparatus of continuations to the analysis of expressives in the paper cited at the top. For a simple in-class demonstration, we tried to do the following.
165
166         (call/cc (lambda k ...))
167
168 is Scheme's way of saying:
169         
170 >       bind the continuation of this complex expression to `k` and evaluate the `...`
171
172
173 So now we define `damn` like this:
174
175         (define damn (lambda () (call/cc (lambda (k) (print "bad") (k 'id)))))
176
177 In other words, `damn` is a thunk. When that thunk is evaluated (`(damn)`), we capture the pending future of the computation and bind that to `k`. Then we print "bad" and supply the argument `'id` to `k`. This last step means we go on evaluating the pending future contribution as if `(damn)` had simply returned `'id`.
178
179 What happens then when we evaluate:
180
181         (cons (cons 'the 'man) 
182                   (cons 'read
183                                 (cons 'the
184                                           (cons (damn) 
185                                                 'book))))
186
187 We get something like this:
188
189 <blockquote>
190 <bold>"bad"</bad> ((the . man) . (read . (the . (id . book))))
191 </blockquote>
192
193 Yay! The affective meaning has jumped out of the compositional evaluation of the main sentence, and the context `(the . (... . book))` only has to deal with the trivial adjectival meaning `'id`.
194
195 **But.** As came out in discussion, the `print` we're using here already constitutes a kind of side-effect mechanism of its own. If you say:
196
197         (define three-thunk (lambda () (print "hi") 3))
198
199 and then ask for the evaluation of:
200
201         (+ 2 (three-thunk))
202
203 you'll see something like:
204
205 <blockquote>
206 <bold>"hi"</bad> 5
207 </blockquote>
208
209 So the demonstration we tried in class was pedagogically flawed. It didn't properly display how continuations are a minimally effective apparatus for representing affective meaning. In fact, continuations were still doing the work, but it wasn't the explicit continuations we were writing out for you. It was instead continuations implicit in the `print` operation.
210
211 So a better demonstration would do without any device like `print` that already incorporates continuations implicitly. Any continuation-manipulation should be fully explicit.
212
213 Instead of representing the side-issue affective contribution by printing "bad", let's instead try to build a pair of side-effect contributions and at-issue assertion. Then what we want would be something like:
214
215         ((side-effect . bad) . ((the . man) . (read . (the . (id . book)))))
216
217 Only we want to get this from the evaluation of:
218
219         (cons (cons 'the 'man) 
220                   (cons 'read
221                                 (cons 'the
222                                           (cons (damn) 
223                                                 'book))))
224
225 where `(damn)` doesn't have widest scope. And we don't want to have to recruit all the other semantic material into accepting and passing along a possible affective argument.
226
227 How to do this?
228
229 It's not immediately clear how to do it with "undelimited" continuations, of the sort captured by `call/cc`. This is the natural first thing to try:
230
231
232         (define damn (lambda () (call/cc (lambda (k) (cons (cons 'side-effect 'bad) (k 'id))))))
233
234
235 The idea here is we capture the continuation that the thunk `(damn)` has when it gets evaluated. This continuation is bound to the variable `k`. We supply `'id` as an argument to that continuation. When the main, at-issue tree is all built, then we return a pair `((side-effect bad) AT-ISSUE-TREE)`.
236
237 However, this doesn't work. The reason is that an undelimited continuation represents the future of the evaluation of `(damn)` *until the end of the computation*. So when `'id` is supplied to `k`, we go back to building the at-issue tree until we're finished *and that's the end of the computation*. We never get to go back and evaluate the context `(cons (cons 'side-effect 'bad) ...)`.
238
239 The straightforward way to fix this is to use, not undelimited continuations, but instead a more powerful apparatus called "delimited continuations." These too will be explained in due course, don't expect to understand all this now.
240
241 A delimited continuation is captured not by using `call/cc`, but instead by using a variety of other operators. We'll use the operator `shift`. This substitutes for `call/cc`. The syntax in Scheme is slightly different. Whereas we wrote:
242
243         (call/cc (lambda k ...))
244
245 we instead write:
246
247         (shift k ...)
248
249 but the behavior is the same. It's just that now our continuation doesn't stretch until the end of the computation, but only up to some specified limit. The limit of the continuation is specified using the syntax:
250
251         (reset ...)
252
253 This is a kind of continuation-scope-marker. There are some interesting default behaviors if you don't explicitly specify where the limits are. But we'll be fully explicit here.
254
255 If a block `...` never invokes a shift, then `(reset ...)` will evaluate just the same as `...`. So for uniformity, we can designate our continuation-scopes even on computations that don't capture and manipulate continuations.
256
257 Going back to the beginning, then. We start with:
258
259         (define damn (lambda () 'id))
260
261 We evaluate:
262
263         (reset (cons (cons 'the 'man) 
264                   (cons 'read
265                                 (cons 'the
266                                           (cons (damn) 
267                                                 'book)))))
268
269 Remember, the reset isn't actually *doing* anything. It's not a function that's taking the other material as an argument. It's instead a scope-marker. Here it's not even needed (and in fact in the interactive interpreter, it wouldn't even be needed when we invoke continuations, because of the default position it takes).  But we're inserting it to be explicit and uniform.
270
271 Evaluating that gives us:
272
273         ((the . man) . (read . (the . (id . book))))
274
275
276 Now to pair that with an affective side-issue content, we'd instead define `damn` as:
277
278         (require racket/control) ; this tells Scheme to let us use shift and reset
279         (define damn (lambda () (shift k (cons (cons 'side-effect 'bad) (k 'id)))))
280
281 And voila:
282
283 (reset (cons (cons 'the 'man) 
284       (cons 'read
285             (cons 'the
286                   (cons (damn) 
287                         'book)))))
288
289 evaluates to:
290
291         ((side-effect bad) ((the . man) . (read . (the . (id . book)))))
292
293 So that's the straightforward way of repairing the strategy we used in class, without using `print`. We also have to switch to using delimited continuations.
294
295
296 Ken Shan, however, pointed out a lovely way to get to the same end-point still using only undelimited continuations (`call/cc`).
297
298 (let ((pragma
299        ; An ordered pair whose first component is the assertion
300        ; operator, a unary function, and whose second component
301        ; is the meaning of "damn", a thunk.
302        (call/cc (lambda (k)
303           (cons (lambda (p) p)
304                 (lambda () (k (cons (lambda (p) (cons (cons 'side-effect 'bad) p))
305                                     (lambda () 'id)))))))))
306   (let ((assert (car pragma)) ; this binds assert to the first element of the pair pragma
307         (damn   (cdr pragma))) ; this binds damn to the second element of the pair pragma
308     (assert (cons (cons 'the 'student) (cons 'read (cons 'the (cons (damn) 'book)))))))
309
310 We won't do much to explain this. We'll just leave it for you to chew on.
311
312
313
314
315         #lang racket
316         ;(define damn (lambda () 'id))
317         (define damn (lambda () (call/cc (lambda (k) 
318                                                                           ; (k 'id)
319                                                                            (print "Something's bad")
320                                                                            (k 'id)
321                                                                            ))))
322
323         (list (list 'the (list (damn) 'man))
324                   (list 'read 
325                                 (list 'the (list (damn) 'book))))
326
327
328
329
330
331         #lang racket
332         (require racket/control)
333
334         (define damn0 (lambda ()
335                                         'id))
336
337         (define damn1 (lambda ()
338                                         (cons '("side effect" bad)
339                                                   'id)))
340
341         (define damn2 (lambda () (shift k
342                                                                         (cons '("side effect" bad) 
343                                                                                   (list (k 'id))))))
344
345         (define damn3 (lambda () (shift k
346                                                                         (list (k 'id)
347                                                                                   '("side effect" bad)))))
348
349
350 ; Now if we use damn0, our compositional semantics will work OK but
351 ; we don't yet have any affective contribution:
352
353         (list "main content" 'i (list 'like (list 'the (damn0) 'boy)))
354         ; '("main content" i (like (the id boy)))
355
356
357 ; If we use damn1, we've added in the affective side effect:
358
359         (list "main content" 'i (list 'like (list 'the (damn1) 'boy)))
360         ; '("main content" i (like (the (("side effect" bad) . id) boy)))
361
362 ; However, the context (list 'the ... 'boy) is now being asked to operate
363 ; on an element (("side effect" bad) . id), and it may complain it doesn't
364 ; know what that is. It knows how to use 'id to get (list 'the 'id 'boy),
365 ; and how to use 'bad to get (list 'the 'bad 'boy), but we're supposed to
366 ; have something different here.
367
368 ; To get what we want we need to use (delimited) continuations:
369         (reset (list "main content" 'i (list 'like (list 'the (damn2) 'boy))))
370         ; '(("side effect" bad) ("main content" i (like (the id boy))))
371
372 ; or to get the side effect at the end:
373
374         (reset (list "main content" 'i (list 'like (list 'the (damn3) 'boy))))
375         ; '(("main content" i (like (the id boy))) ("side effect" bad))
376
377 ; If you're working in the interactive interpreter, the outermost "reset" here
378 ; is already in its default position, so it doesn't need to be explicitly
379 ; specified:
380
381         (list "main content" 'i (list 'like (list 'the (damn2) 'boy)))
382         ; '(("side effect" bad) ("main content" i (like (the id boy))))
383
384 ; However, if you're executing this as a file, you would need to include explicit resets.
385
386
387
388 ; Instead of using reset/shift you could use an element like "print" in
389 ; building the side effect, as we did in class. Here you wouldn't require an
390 ; explicit continuation, but as Chris said, that's because "print" already
391 ; represents an implicit continuation.
392
393         (define damn4 (lambda () (begin (print "bad") 'id)))
394         (list "main content" 'i (list 'like (list 'the (damn4) 'boy)))
395         ; "bad"'("main content" i (like (the id boy)))
396 ;
397
398