continue_foo_normally -> continue_foo_snapshot
[lambda.git] / topics / week13_native_continuation_operators.mdwn
1 [[!toc]]
2
3 ## Explicit and Implicit ##
4
5 Consider two kinds of video games. The first are 80s-style cabinets, that might suppress most awareness of your outside environment, but you can still directly perceive the controls, the "new game" button, and so on:
6
7 [[/images/star-wars-arcade.gif]]
8
9 The second are more immersive games with VR goggles and gloves:
10
11 [[/images/virtual-reality.jpg]]
12
13 In this second kind of game, you don't see or feel the goggles or gloves (anyway, you don't perceive them _as_ goggles or gloves), and you needn't normally perceive any "new game" button. But the game might have some "magic gesture" you can perform, such as holding your left elbow while simultaneously stamping your right foot twice, that would invoke a special menu in your visual display, containing among other things a "new game" button.
14
15 I want to offer the contrast between these two kinds of games, and the ways that you can perceive and handle the "new game" button, as analogy for the contrast between explicit and implicit mutation, which we looked at earlier, and also the contrast between explicit and implicit continuations, which we're beginning to look at now.
16
17 With explicit mutation operators in the language, our code looks like this:
18
19     let x = cell 0 in
20     ... get x ...
21     ... put 1 into x ...
22
23 With implicit mutation operators in the language, it looks instead like this:
24
25     var x = 0 in
26     ... x ...
27     ... x := 1 ...
28
29 The first two lines aren't very different from what we'd have without mutation:
30
31     let x = 0 in
32     ... x ...
33
34 The first line used the keyword `var` instead of the more familiar `let`, but that's just to signal that the variable we're introducing is mutable. Syntactically it acts just like a variant spelling of `let`. Also we access the contents of the variable in the same way, with just `x`. Whereas with the explicit reference cells, we have to say `get x`. There we can "see" the reference cell and have to explicitly "look inside it" to get at its contents. That's like seeing the "new game" button and other controls during the normal use of the video game. In the third line of the implicit mutation code, we have the "magic gesture", `x := 1`, which does something you couldn't do in the code without mutation. This is like bringing up the "new game" display by the magic elbow-and-stomping gesture, which doesn't work in real life. This lets us achieve the same effect that we did in the explicit code using `put 1 into x`, but without us needing to (or being able to) explicitly inspect or manipulate the reference cell itself.
35
36 Turning to continuations, so far we've seen how to explicitly manipulate them, as in:
37
38     let rec tc (xs : char list) (k : char list -> char list) =
39       ... tc xs' (fun tail -> ... k ... tail) in
40     tc some_list identity
41
42 Here we explicitly pass around continuations in the `k` argument, beginning with the `identity` or do-nothing continuation, but then modifying the continuation at each recursive invocation of `tc`.
43
44 What the **continuation or control operators** like `let/cc`, `reset`, `shift`, `abort`, and so on do is give us a "magic gesture" alternative, where we can let the continuations usually be *implicit* in the way our code is structured, but when we perform the magic gesture (that is, use some of these special operators), the continuation gets converted from its implicit form into an explicit function that's bound to a variable we supply.
45
46
47 ## A Bestiary of operators for magically distilling implicit continuations into explicit functions ##
48
49 The continuation operators come in a variety of forms. You'll only be using a few of them (if any) in a single application. But here we'll present a couple of them side-by-side.
50
51 One issue is whether the continuation operators you're working with are "full-strength" or not. As we said, what these operators do is distill an implicit continuation into a function that you can explicitly invoke or manipulate (pass into or return from a function). If they're "full-strength", then there aren't constraints on _where_ or _how many times_ you can invoke that continuation function. Anywhere you have access to some variable that's bound to the continuation, you can invoke it as often as you like. More handicapped continuations are only invocable a single time, or only in certain regions of the code. Sometimes these handicapped continuations are provided because they're easier to implement, and the language designers haven't gotten around to implementing full-strength continuations yet. Or a language might provide _both_ handicapped and full-strength continuations, because the former can be implemented more efficiently. For applications like coroutines or exceptions/aborts, that we looked at before, typically all that's needed is a handicapped form of continuations. If your language has an `abort` operation, typically you'll only be invoking it once within a single execution path, and only inside the box that you want to abort from.
52
53 For our discussion, though, we'll just be looking at the full-strength continuations. You can learn about different ways they might be handicapped later.
54
55 The next issue is whether the continuations are _delimited_ or not. In [[our discussion of aborts|week13_coroutines_exceptions_and_aborts#index3h2]], we had a box, and what `abort` did was skip the rest of the code inside the box and resume execution at the outside border of the box. This is the pattern of a **delimited continuation**, with the box being the delimiter. There are a bunch of different operators that have been proposed for dealing with delimited continuations. Many of them are interdefinable (though the interdefinitions are sometimes complex). We won't be trying to survey them all. The ones we'll suggest as a paradigm are the pair of `reset` and `shift`. The first of these marks where the box goes, and the second has two roles: (i) it marks where you should start skipping (if you're going to "skip the rest of the code inside the box"), and (ii) it specifies a variable `k` that we bind to the continuation representing that skipped code. Thus we have:
56
57     initial outside code
58     +---reset--------------------+
59     | initial inside code        |
60     | shift k ( ... )            |
61     | remaining inside code      |
62     +----------------------------+
63     remaining outside code
64
65 Really in the implementation of this there are _two_ continuations or snapshots being tracked. There's the potentially skipped code, represented by `remaining inside code` above; and there's also the continuation/snapshot that we resume with if we do skip that code, represented by `remaining outside code`. But only the first of these gets bound to a variable, `k` in the above diagram. What happens in this diagram is that `initial outside code` runs, then `initial inside code` runs, then `remaining inside code` is distilled into a function and bound to the variable `k`, then we run the `( ... )` code with `k` so bound. If that `( ... )` code invokes `k` by applying it to an argument, then `remaining inside code` is run as though the supplied argument were what the `shift k ( ... )` bit evaluated to. If the `( ... )` code doesn't invoke `k`, but just ends with a normal result like `10`, then the `remaining inside code` is skipped and we resume execution with the outside, implicitly snapshotted code `remaining outside code`.
66
67 You may encounter references to `prompt` and `control`. These are variants of `reset` and `shift` that differ in only subtle ways. As we said, there are lots of variants of these that we're not going to try to survey.
68
69 We talked before about `abort`. This can be expressed in terms of `reset` and `shift`. At the end of our discussion of abort, we said that this diagram:
70
71     let foo x =
72     +---try begin----------------+
73     |       (if x = 1 then 10    |
74     |       else abort 20        |
75     |       ) + 100              |
76     +---end----------------------+
77     in (foo 2) + 1000;;
78
79 could be written in Scheme with either:
80
81     #lang racket
82     (require racket/control)
83
84     (let ([foo (lambda (x)
85                  (reset
86                   (+
87                     (if (eqv? x 1) 10 (abort 20))
88                     100)))])
89       (+ (foo 2) 1000))
90
91 or:
92
93     #lang racket
94     (require racket/control)
95
96     (let ([foo (lambda (x)
97                  (reset
98                   (+
99                     (shift k
100                       (if (eqv? x 1) (k 10) 20))
101                     100)))])
102       (+ (foo 2) 1000))
103
104 That shows you how `abort` can be expressed in terms of `shift`. Rewriting the Scheme code into a more OCaml-ish syntax, it might look something like this:
105
106     let foo x = reset (shift k -> if x = 1 then k 10 else 20) + 100) in
107     foo 2 + 1000
108
109 However, OCaml doesn't have any continuation operators in its standard deployment. If you [[installed Oleg's delimcc library|/rosetta3/#delimcc]], you can use the previous code after first doing this:
110
111     # #directory "+../delimcc";;
112     # #load "delimcc.cma";;
113     # let reset_label = ref None;;
114     # let reset body =
115         let p = Delimcc.new_prompt () in
116         let oldp = !reset_label in
117         reset_label := Some p; let res = Delimcc.push_prompt p body in
118         reset_label := oldp; res;;
119     # let shift fun_k = match !reset_label with
120       | None -> failwith "shift must be inside reset"
121       | Some p -> Delimcc.shift p fun_k;;
122
123 Also, the previous code has to be massaged a bit to have the right syntax. What you really need to write is:
124
125     let foo x = reset (fun () -> shift (fun k -> if x = 1 then k 10 else 20) + 100) in
126     foo 2 + 1000
127
128 That will return `1020` just like the Scheme code does. If you said `... foo 1 + 1000`, you'll instead get `1110`.
129
130 That was all *delimited* continuation operators. There's also the **undelimited continuation operators**, which historically were developed first. Here you don't see the same kind of variety that you do with the delimited continuation operators. Essentially, there is just one full-strength undelimited continuation operator. But there are several different syntactic forms for working with it. (Also, a language might provide handicapped continuation operators alongside, or instead of, the full-strength one. Some loser languages don't even do that much.) The historically best-known of these is expressed in Scheme as `call-with-current-continuation`, or `call/cc` for short. But we think it's a bit easier to instead use the variant `let/cc`. The following code is equivalent, and shows how these two forms relate to each other:
131
132     (let/cc k ...)
133
134     (call/cc (lambda (k) ...))
135
136 `(let/cc k ...)` is a lot like `(shift k ...)` (or in the OCaml version, `shift (fun k -> ...)`), except that it doesn't need a surrounding `reset ( ... )` (in OCaml, `reset (fun () -> ...)`). For the undelimited continuation operator, the box is understood to be *the whole rest of the top-level computation*. If you're running a file, that's all the rest of the file that would have been executed after the syntactic hole filled by `(let/cc k ...)`. With `(shift k ...)`, the code that gets bound to `k` doesn't get executed unless you specifically invoke `k`; but `let/cc` works differently in this respect. Thus:
137
138     (+ 100 (let/cc k 1))
139
140 returns `101`, whereas:
141
142     (reset (+ 100 (shift k 1)))
143
144 only returns `1`. It is possible to duplicate the behavior of `let/cc` using `reset`/`shift`, but you have to structure your code in certain ways to do it. In order to duplicate the behavior of `reset`/`shift` using `let/cc`, you need to also make use of a mutable reference cell. So in that sense delimited continuations are more powerful and undelimited continuations are sort-of a special case.
145
146 (In the OCaml code above for using delimited continuations, there is a mutable reference cell `reset_label`, but this is just for convenience. Oleg's library is designed for use with _multiple_ reset blocks having different labels, then when you invoke `shift` you have to specify which labeled reset block you want to potentially skip the rest of. We haven't introduced that complexity into our discussion, so for convenience we worked around it in showing you how to use `reset` and `shift` in OCaml. And the mutable reference cell was only playing the role of enabling us to work around the need to explicitly specify the `reset` block's label.)
147
148 You may have noticed in some of our Scheme code we had the preface `(require racket/control)`. You don't need to do anything special (in Racket) to use `call/cc` or `let/cc`, but you do need that preface to be able to use `reset` and `shift` and `abort`.
149
150 ## Examples of using these continuation operators ##
151
152 Here are some examples of using these different continuation operators. The continuation that gets bound to `k` will be in bold. I'll use an OCaml-ish syntax because that's easiest to read, but these examples don't work as-is in OCaml. The `reset`/`shift` examples need to be massaged into the form displayed above for OCaml; and the `let/cc` examples don't work in OCaml because that's not provided. Alternatively, you could massage all of these into Scheme syntax. You shouldn't find that hard.
153
154 1.  <pre><b>100 + </b>let/cc k (10 + 1)</pre>
155     This evaluates to `111`. Nothing exotic happens here.
156
157 2.  <pre><b>100 + </b>let/cc k (10 + k 1)</pre>
158     `k` is again bound to `100 + < >`. Note that after invoking `k 1`, the rest of the body of `let/cc k ( ... )` is discarded, so the result is simply `101`. See example 11, below, for contrast with `shift k ( ... )`.
159
160 3.  You aren't restricted to calling a full-strength continuation function only once; nor are you restricted to calling it only inside the `let/cc` block. For example:
161     <pre><b>let p = </b>let/cc k (1,k) <b>in
162     let y = snd p (2, ident) in
163     (fst p, y)</b></pre>
164     In the first line, we bind the continuation function (the bold code) to `k` and then bind the variable `p` to the pair of `1` and that function.
165     In the second line, we extract the continuation function from the pair `p` and apply it to the argument `(2, ident)`. That results in us discarding the rest of *that* computation and instead executing the following:
166     <pre><b>let p = </b>(2, ident) <b>in
167     let y = snd p (2, ident) in
168     (fst p, y)</b></pre>
169     which in turn results in the nested pair `(2, (2, ident))`.
170     Notice how the first time through, when `p`'s second element is a continuation, applying it to an argument is a bit like time-travel? The metaphysically impossible kind of time-travel, where you can change what happened. The second time through, `p` gets bound to a different pair, whose second element is the ordinary `ident` function.
171
172 4.  <pre><b>1000 + (100 + </b>abort 11<b>)</b></pre>
173     Here the box is implicit, understood to be the rest of the code. The result is just the abort value `11`, because the bold code is skipped.
174
175 5.  <pre>1000 + reset <b>(100 + </b>abort 11<b>)</b></pre>
176     Here the box or delimiter is explicitly specified. The bold code is skipped, but the outside code `1000 + < >` is still executed, so we get `1011`.
177
178 6.  <pre>1000 + reset <b>(100 + </b>shift k (10 + 1)<b>)</b></pre>
179     Equivalent to preceding. We bind the bold code to `k` but then never apply `k`, so the value `10 + 1` is supplied directly to the outside code `1000 + < >`, resulting in `1011`.
180
181 7.  <pre>1000 + reset <b>(100 + </b>shift k (k (10 + 1))<b>)</b></pre>
182     Here we do invoke the captured continuation, so what gets passed to the outside code `1000 + < >` is `k (10 + 1)`, that is, `(100 + (10 + 1))`. Result is `1111`.
183     In general, if the last thing that happens inside a `shift k ( ... )` body is that `k` is applied to an argument, then we do continue running the bold code between `shift k ( ... )` and the edge of the `reset` box.
184
185 8.  <pre>1000 + reset <b>(100 + </b>shift k (10 + k 1)<b>)</b></pre>
186     This also results in `1111`, but via a different path than the preceding. First, note that `k` is bound to `100 + < >`. So `k 1` is `101`. Then `10 + k 1` is `10 + 101`. Then we exit the body of `shift k ( ... )`, without invoking `k` again, so we don't add `100` any more times. Thus we pass `10 + 101` to the outside code `1000 + < >`. So the result is `1000 + (10 + 101)` or `1111`. (Whereas in the preceding example, the result was `1000 + (100 + 11)`. The order in which the operations are performed is different. If we used a non-commutative operation instead of `+`, the results of these two examples would be different from each other.)
187
188 9.  <pre>1000 + reset <b>(100 + </b>shift k (k)<b>)</b> 1</pre>
189     Here `k` is bound to `100 + < >`. That function `k` is what's returned by the `shift k ( ... )` block, and since `k` isn't invoked (applied) when doing so, the rest of the bold `reset` block is skipped (for now). So we resume the outside code `1000 + < > 1`, with what fills the gap `< >` being the function that was bound to `k`. Thus this is equivalent to `1000 + (fun x -> 100 + x) 1` or `1000 + 101` or `1101`.
190
191 10. <pre>1000 + reset <b>(100 + </b>shift k (k (k 1))<b>)</b></pre>
192     Here `k` is bound to `100 + < >`. Thus `k 1` is `101`. Now there are two ways to think about what happens next. (Both are valid.) One way to think is that since the `shift` block ends with an additional outermost application of `k`, then as described in example 7 above, we continue through the bold code with the value `k 1` or `101`. Thus we get `100 + 101`, and then we continue with the outermost code `1000 + < >`, getting `1000 + (100 + 101)`, or `1201`. The other way to think is that since `k` is `100 + < >`, and `k 1` is `101`, then `k (k 1)` is `201`. Now we leave the `shift` block *without* executing the bold code a third time (we've already taken account of the two applications of `k`), resuming with the outside code `1000 + < >`, thereby getting `1000 + 201` as before.
193
194 11. Here's a comparison of `let/cc` to `shift`. Recall example 2 above was:
195     <pre><b>100 + </b>let/cc k (10 + k 1)</pre>
196     which evaluated to `101`. The parallel code where we instead capture the continuation using `shift k ( ... )` would look like this:
197     <pre>reset <b>(100 + </b>shift k (10 + k 1)<b>)</b></pre>
198     But this evaluates differently. In the `let/cc` example, `k` is bound to the rest of the computation *including its termination*, so after executing `k 1` we never come back and finish with `10 + < >`. A `let/cc`-bound `k` never returns to the context where it was invoked. Whereas the `shift`-bound `k` only includes up to the edge of the `reset` box --- here, the rest of the computation, but *not including* its termination. So after `k 1`, if there is still code inside the body of `shift`, as there is here, we continue executing it. Thus the `shift` code evaluates to `111` not to `101`.
199
200     Thus code using `let/cc` can't be *straightforwardly* translated into code using `shift`. It can be translated, but the algorithm will be more complex.
201
202
203 ## Some call/cc (or let/cc) exercises from The Seasoned Schemer ##
204
205 Here are a series of examples from *The Seasoned Schemer*, which we recommended at the start of term. It's not necessary to have the book to follow the exercises, though if you do have it, its walkthroughs will give you useful assistance.
206
207 For reminders about Scheme syntax, see [[here|/exercises/assignment12/#scheme]], and [[here|/rosetta1]] and [[here|/rosetta3]]. Other resources are on our [[Learning Scheme]] page.
208
209 Most of the examples assume the following preface:
210
211         #lang racket
212
213         (define (atom? x)
214           (and (not (pair? x)) (not (null? x))))
215
216 Now try to figure out what this function does:
217
218         (define alpha
219           (lambda (a lst)
220             (let/cc k ; now what will happen when k is called?
221               (letrec ([aux (lambda (l)
222                               (cond
223                                 [(null? l) '()]
224                                 [(eq? (car l) a) (k (aux (cdr l)))]
225                                 [else (cons (car l) (aux (cdr l)))]))])
226                 (aux lst)))))
227         
228 Here is [[the answer|cps_hint_1]], but try to figure it out for yourself.
229
230 Next, try to figure out what this function does:
231
232         (define beta
233           (lambda (lst)
234             (let/cc k ; now what will happen when k is called?
235               (letrec ([aux (lambda (l)
236                               (cond
237                                 [(null? l) '()]
238                                 [(atom? (car l)) (k (car l))]
239                                 [else (begin
240                                         ; what will the value of the next line be? why is it ignored?
241                                         (aux (car l))
242                                         (aux (cdr l)))]))])
243                 (aux lst)))))
244
245 Here is [[the answer|cps_hint_2]], but try to figure it out for yourself.
246
247 Next, try to figure out what this function does:
248
249         (define gamma
250           (lambda (a lst)
251             (letrec ([aux (lambda (l k)
252                             (cond
253                               [(null? l) (k 'notfound)]
254                               [(eq? (car l) a) (cdr l)]
255                               [(atom? (car l)) (cons (car l) (aux (cdr l) k))]
256                               [else
257                                ; what happens when (car l) exists but isn't an atom?
258                                (let ([car2 (let/cc k2 ; now what will happen when k2 is called?
259                                              (aux (car l) k2))])
260                                  (cond
261                                    ; when will the following condition be met? what happens then?
262                                    [(eq? car2 'notfound) (cons (car l) (aux (cdr l) k))]
263                                    [else (cons car2 (cdr l))]))]))]
264                      [lst2 (let/cc k1 ; now what will happen when k1 is called?
265                              (aux lst k1))])
266               (cond
267                 ; when will the following condition be met?
268                 [(eq? lst2 'notfound) lst]
269                 [else lst2]))))
270
271 Here is [[the answer|cps_hint_3]], but try to figure it out for yourself.
272
273 Here is the hardest example. Try to figure out what this function does:
274
275         (define delta
276           (letrec ([yield (lambda (x) x)]
277                    [resume (lambda (x) x)]
278                    [walk (lambda (l)
279                            (cond
280                              ; is this the only case where walk returns a non-atom?
281                              [(null? l) '()]
282                              [(atom? (car l)) (begin
283                                                 (let/cc k2 (begin
284                                                   (set! resume k2) ; now what will happen when resume is called?
285                                                   ; when the next line is executed, what will yield be bound to?
286                                                   (yield (car l))))
287                                                 ; when will the next line be executed?
288                                                 (walk (cdr l)))]
289                              [else (begin
290                                      ; what will the value of the next line be? why is it ignored?
291                                      (walk (car l))
292                                      (walk (cdr l)))]))]
293                    [next (lambda () ; next is a thunk
294                            (let/cc k3 (begin
295                              (set! yield k3) ; now what will happen when yield is called?
296                              ; when the next line is executed, what will resume be bound to?
297                              (resume 'blah))))]
298                    [check (lambda (prev)
299                             (let ([n (next)])
300                               (cond
301                                 [(eq? n prev) #t]
302                                 [(atom? n) (check n)]
303                                 ; when will n fail to be an atom?
304                                 [else #f])))])
305             (lambda (lst)
306               (let ([fst (let/cc k1 (begin
307                            (set! yield k1) ; now what will happen when yield is called?
308                            (walk lst)
309                            ; when will the next line be executed?
310                            (yield '())))])
311                 (cond
312                   [(atom? fst) (check fst)]
313                   ; when will fst fail to be an atom?
314                   [else #f])
315                 ))))
316
317 Here is [[the answer|cps_hint_4]], but again, first try to figure it out for yourself.