432ab630e549b64389ae08b0f3f20f7277dc9476
[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
25 We start with a simulation of semantic composition:
26
27         (cons (cons 'the 'man) 
28                   (cons 'read
29                                 (cons 'the
30                                           'book)))
31
32 That evaluates to nested structure of pairs, that Scheme displays as:
33
34         '((the . man) . (read . (the . book)))
35
36 If you try it yourself, you may see instead:
37
38         '((the . man) read the . book)
39
40 This is shorthand for the same thing. Just trust me on that.
41
42 What's going on here?
43 =====================
44
45 `(cons M N)` is a request to build an ordered pair out of the values M and N.
46 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
47 Scheme would be confused by what argument the `.` is supposed to be. So, you say:
48
49         (cons M N)
50
51 and that evaluates to an ordered pair, and Scheme displays that ordered pair as
52
53         '(M . N)
54
55 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.
56
57 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.
58
59 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.
60
61 `'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 `(quote the)`, just read it as a verbose (and perhaps misleading) way of writing `'the`, not as the application of any function to any value.
62
63 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.
64
65 And we built a tree out of those values, representing the tree by a nested structure of pairs of leaf-labels.
66
67 The program we submitted to Scheme:
68
69         (cons (cons 'the 'man) 
70                   (cons 'read
71                                 (cons 'the
72                                           'book)))
73
74 evaluates to the nested structure of pairs that Scheme displays as:
75
76         '((the . man) . (read . (the . book)))
77
78 ---or as an equivalent shorthand. And although there aren't `'`s prefixed to each of the elements of this nested structure, those elements are still the `'the`, `'man` and so on primitive atomic values that we specified. Not the values (if any) possessed by some variables `the`, `man`, and so on.
79
80 We can think of this nested structure of pairs as the tree:
81
82                                  /----------------\
83                                 /                  \
84                            /                    \
85                           /                      \
86                          /                        \                                  
87                         / \                      / \
88                    /   \                    /   \
89                   /     \                  /     \
90                  /       \                /       \
91         meaning of   meaning of   meaning of   \
92           "the"        "man"       "read"      / \
93                                                                                   /   \
94                                                                                  /     \
95                                                                                 /       \
96                                                                         meaning of  meaning of
97                                                                           "the"      "book"
98
99 Okay, let's get back to "damn."
100
101 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`.
102
103 What's a "thunk"?
104 =================
105
106 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:
107
108         (lambda () 3)
109
110 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>`)
111 not an integer. Whereas `(t)` is an integer not a function.
112
113 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.
114
115 So for uniformity we're going to make `damn` be a thunk right from the beginning.
116
117 As we said, `damn` starts as a thunk that returns a trivial adjectival meaning `'id`:
118
119         (define damn (lambda () 'id))
120
121 Now we can say:
122
123         (cons (cons 'the 'man) 
124                   (cons 'read
125                                 (cons 'the
126                                           (cons (damn) 
127                                                 'book))))
128
129 and we get back:
130
131         ((the . man) . (read . (the . (id . book))))
132
133 If you try this yourself, you may see instead:
134
135         '((the . man) read the id . book)
136
137
138
139 Now we want to get some affective meaning into damn. So we might try:
140
141
142         (define damn (lambda () 'bad))
143
144 But then:
145
146         (cons (cons 'the 'man) 
147                   (cons 'read
148                                 (cons 'the
149                                           (cons (damn) 
150                                                 'book))))
151
152 gives us:
153
154         ((the . man) . (read . (the . (bad . book))))
155
156 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:
157
158         (define damn (lambda () (cons 'side-effect 'bad)))
159
160 But then we'd get:
161
162         ((the . man) . (read . (the . ((side-effect . bad) . book))))
163
164 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.
165
166
167 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.
168
169 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.
170
171         (call/cc (lambda k ...))
172
173 is Scheme's way of saying:
174         
175 >       bind the continuation of this complex expression to `k` and evaluate the `...`
176
177
178 So now we define `damn` like this:
179
180         (define damn (lambda () (call/cc (lambda (k) (print "bad") (k 'id)))))
181
182 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`.
183
184 What happens then when we evaluate:
185
186         (cons (cons 'the 'man) 
187                   (cons 'read
188                                 (cons 'the
189                                           (cons (damn) 
190                                                 'book))))
191
192 We get something like this:
193
194 <blockquote>
195 <bold>"bad"</bad> ((the . man) . (read . (the . (id . book))))
196 </blockquote>
197
198 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`.
199
200 **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:
201
202         (define three-thunk (lambda () (print "hi") 3))
203
204 and then ask for the evaluation of:
205
206         (+ 2 (three-thunk))
207
208 you'll see something like:
209
210 <blockquote>
211 <bold>"hi"</bad> 5
212 </blockquote>
213
214 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.
215
216 So a better demonstration would do without any device like `print` that already incorporates continuations implicitly. Any continuation-manipulation should be fully explicit.
217
218 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:
219
220         ((side-effect . bad) . ((the . man) . (read . (the . (id . book)))))
221
222 Only we want to get this from the evaluation of:
223
224         (cons (cons 'the 'man) 
225                   (cons 'read
226                                 (cons 'the
227                                           (cons (damn) 
228                                                 'book))))
229
230 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.
231
232 How to do this?
233
234 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:
235
236
237         (define damn (lambda () (call/cc (lambda (k) (cons (cons 'side-effect 'bad) (k 'id))))))
238
239
240 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)`.
241
242 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) ...)`.
243
244 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.
245
246 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:
247
248         (call/cc (lambda k ...))
249
250 we instead write:
251
252         (shift k ...)
253
254 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:
255
256         (reset ...)
257
258 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.
259
260 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.
261
262 Going back to the beginning, then. We start with:
263
264         (define damn (lambda () 'id))
265
266 We evaluate:
267
268         (reset (cons (cons 'the 'man) 
269                   (cons 'read
270                                 (cons 'the
271                                           (cons (damn) 
272                                                 'book)))))
273
274 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.
275
276 Evaluating that gives us:
277
278         ((the . man) . (read . (the . (id . book))))
279
280
281 Now to pair that with an affective side-issue content, we'd instead define `damn` as:
282
283         (require racket/control) ; this tells Scheme to let us use shift and reset
284         (define damn (lambda () (shift k (cons (cons 'side-effect 'bad) (k 'id)))))
285
286 And voila:
287
288 (reset (cons (cons 'the 'man) 
289       (cons 'read
290             (cons 'the
291                   (cons (damn) 
292                         'book)))))
293
294 evaluates to:
295
296         ((side-effect bad) ((the . man) . (read . (the . (id . book)))))
297
298 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.
299
300
301 Ken Shan, however, pointed out a lovely way to get to the same end-point still using only undelimited continuations (`call/cc`).
302
303 (let ((pragma
304        ; An ordered pair whose first component is the assertion
305        ; operator, a unary function, and whose second component
306        ; is the meaning of "damn", a thunk.
307        (call/cc (lambda (k)
308           (cons (lambda (p) p)
309                 (lambda () (k (cons (lambda (p) (cons (cons 'side-effect 'bad) p))
310                                     (lambda () 'id)))))))))
311   (let ((assert (car pragma)) ; this binds assert to the first element of the pair pragma
312         (damn   (cdr pragma))) ; this binds damn to the second element of the pair pragma
313     (assert (cons (cons 'the 'student) (cons 'read (cons 'the (cons (damn) 'book)))))))
314
315 We won't do much to explain this. We'll just leave it for you to chew on.
316
317
318
319
320         #lang racket
321         ;(define damn (lambda () 'id))
322         (define damn (lambda () (call/cc (lambda (k) 
323                                                                           ; (k 'id)
324                                                                            (print "Something's bad")
325                                                                            (k 'id)
326                                                                            ))))
327
328         (list (list 'the (list (damn) 'man))
329                   (list 'read 
330                                 (list 'the (list (damn) 'book))))
331
332
333
334
335
336         #lang racket
337         (require racket/control)
338
339         (define damn0 (lambda ()
340                                         'id))
341
342         (define damn1 (lambda ()
343                                         (cons '("side effect" bad)
344                                                   'id)))
345
346         (define damn2 (lambda () (shift k
347                                                                         (cons '("side effect" bad) 
348                                                                                   (list (k 'id))))))
349
350         (define damn3 (lambda () (shift k
351                                                                         (list (k 'id)
352                                                                                   '("side effect" bad)))))
353
354
355 ; Now if we use damn0, our compositional semantics will work OK but
356 ; we don't yet have any affective contribution:
357
358         (list "main content" 'i (list 'like (list 'the (damn0) 'boy)))
359         ; '("main content" i (like (the id boy)))
360
361
362 ; If we use damn1, we've added in the affective side effect:
363
364         (list "main content" 'i (list 'like (list 'the (damn1) 'boy)))
365         ; '("main content" i (like (the (("side effect" bad) . id) boy)))
366
367 ; However, the context (list 'the ... 'boy) is now being asked to operate
368 ; on an element (("side effect" bad) . id), and it may complain it doesn't
369 ; know what that is. It knows how to use 'id to get (list 'the 'id 'boy),
370 ; and how to use 'bad to get (list 'the 'bad 'boy), but we're supposed to
371 ; have something different here.
372
373 ; To get what we want we need to use (delimited) continuations:
374         (reset (list "main content" 'i (list 'like (list 'the (damn2) 'boy))))
375         ; '(("side effect" bad) ("main content" i (like (the id boy))))
376
377 ; or to get the side effect at the end:
378
379         (reset (list "main content" 'i (list 'like (list 'the (damn3) 'boy))))
380         ; '(("main content" i (like (the id boy))) ("side effect" bad))
381
382 ; If you're working in the interactive interpreter, the outermost "reset" here
383 ; is already in its default position, so it doesn't need to be explicitly
384 ; specified:
385
386         (list "main content" 'i (list 'like (list 'the (damn2) 'boy)))
387         ; '(("side effect" bad) ("main content" i (like (the id boy))))
388
389 ; However, if you're executing this as a file, you would need to include explicit resets.
390
391
392
393 ; Instead of using reset/shift you could use an element like "print" in
394 ; building the side effect, as we did in class. Here you wouldn't require an
395 ; explicit continuation, but as Chris said, that's because "print" already
396 ; represents an implicit continuation.
397
398         (define damn4 (lambda () (begin (print "bad") 'id)))
399         (list "main content" 'i (list 'like (list 'the (damn4) 'boy)))
400         ; "bad"'("main content" i (like (the id boy)))
401 ;
402
403