various tweaks
[lambda.git] / topics / week13_coroutines_exceptions_and_aborts.mdwn
index de7e8c0..df9c134 100644 (file)
@@ -345,11 +345,11 @@ We can get that by some further rearranging of the code:
     in let outer_snapshot = fun box ->
         let foo_result = box
         in (foo_result) + 1000
-    in let continue_foo_normally = fun from_value ->
+    in let continue_foo_snapshot = fun from_value ->
         let value = from_value + 100
         in outer_snapshot value
     in (* start of foo_applied_to_x *)
-        if x = 1 then continue_foo_normally 10
+        if x = 1 then continue_foo_snapshot 10
         else outer_snapshot 20;;
 
 And this is indeed what is happening, at a fundamental level, when you use an expression like `abort 20`. Here is the original code for comparison:
@@ -374,21 +374,21 @@ You can think of them as functions that represent "how the rest of the computati
 
 The key idea behind working with continuations is that we're *inverting control*. In the fragment above, the code `(if x = 1 then ... else outer_snapshot 20) + 100`---which is written as if it were to supply a value to the outside context that we snapshotted---itself *makes non-trivial use of* that snapshot. So it has to be able to refer to that snapshot; the snapshot has to somehow be available to our inside-the-box code as an *argument* or bound variable. That is: the code that is *written* like it's supplying an argument to the outside context is instead *getting that context as its own argument*. He who is written as value-supplying slave is instead become the outer context's master.
 
-In fact you've already seen this several times this semester---recall how in our implementation of pairs in the untyped lambda-calculus, the handler who wanted to use the pair's components had *in the first place to be supplied to the pair as an argument*. So the exotica from the end of the seminar was already on the scene in some of our earliest steps. Recall also what we did with our [[abortable list traversals|/topics/week12_abortable_traversals]].
+In fact you've already seen this several times this semester---recall how in our implementation of pairs in the untyped lambda-calculus, the handler who wanted to use the pair's components had *in the first place to be supplied to the pair as an argument*. So the exotica from the end of the seminar was already on the scene in some of our earliest steps. Recall also what we did with our [[abortable list traversals|/topics/week12_abortable_traversals]]. (The `outer_snapshot` corresponds to the "done" handler in those traversals; and the `continue_foo_snapshot` to the "keep_going" handler.)
 
 This inversion of control should also remind you of Montague's treatment of determiner phrases in ["The Proper Treatment of Quantification in Ordinary English"](http://www.blackwellpublishing.com/content/BPL_Images/Content_store/Sample_chapter/0631215417%5CPortner.pdf) (PTQ).
 
-A naive semantics for atomic sentences will say the subject term is of type `e`, and the predicate of type `e -> t`, and that the subject provides an argument to the function expressed by the predicate.
+A naive semantics for atomic sentences will say the subject term is of type `e`, and the predicate of type `e -> t`, and that the subject provides an argument to the function expressed by the predicate.
 
-Monatague proposed we instead take the subject term to be of type `(e -> t) -> t`, and that now it'd be the predicate (still of type `e -> t`) that provides an argument to the function expressed by the subject.
+Monatague proposed we instead take the subject term to be of type `(e -> t) -> t`, and that now it'd be the predicate (still of type `e -> t`) that provides an argument to the function expressed by the subject.
 
-If all the subject did then was supply an `e` to the `e -> t` it receives as an argument, we wouldn't have gained anything we weren't already able to do. But of course, there are other things the subject can do with the `e -> t` it receives as an argument. For instance, it can check whether anything in the domain satisfies that `e -> t`; or whether most things do; and so on.
+If all the subject did then was supply an `e` to the `e -> t` it receives as an argument, we wouldn't have gained anything we weren't already able to do. But of course, there are other things the subject can do with the `e -> t` it receives as an argument. For instance, it can check whether anything in the domain satisfies that `e -> t`; or whether most things do; and so on.
 
 This inversion of who is the argument and who is the function receiving the argument is paradigmatic of working with continuations.
 
 Continuations come in many varieties. There are **undelimited continuations**, expressed in Scheme via `(call/cc (lambda (k) ...))` or the shorthand `(let/cc k ...)`. (`call/cc` is itself shorthand for `call-with-current-continuation`.) These capture "the entire rest of the computation." There are also **delimited continuations**, expressed in Scheme via `(reset ... (shift k ...) ...)` or `(prompt ... (control k ...) ...)` or any of several other operations. There are subtle differences between those that we won't be exploring in the seminar. Ken Shan has done terrific work exploring the relations of these operations to each other.
 
-When working with continuations, it's easiest in the first place to write them out explicitly, the way that we explicitly wrote out the "snapshot" continuations when we transformed this:
+When working with continuations, it's easiest in the beginning to write them out explicitly, the way that we explicitly wrote out the "snapshot" continuations when we transformed this:
 
     let foo x =
     +---try begin----------------+
@@ -404,11 +404,11 @@ into this:
     in let outer_snapshot = fun box ->
         let foo_result = box
         in (foo_result) + 1000
-    in let continue_foo_normally = fun from_value ->
+    in let continue_foo_snapshot = fun from_value ->
         let value = from_value + 100
         in outer_snapshot value
     in (* start of foo_applied_to_x *)
-        if x = 1 then continue_foo_normally 10
+        if x = 1 then continue_foo_snapshot 10
         else outer_snapshot 20;;
 
 Code written in the latter form is said to be written in **explicit continuation-passing style** or CPS. Later we'll talk about algorithms that mechanically convert an entire program into CPS.
@@ -442,10 +442,10 @@ There are also different kinds of "syntactic sugar" we can use to hide the conti
       let outer_snapshot = fun box ->
           let foo_result = box
           in (foo_result) + 1000
-      in let continue_foo_normally = fun from_value ->
+      in let continue_foo_snapshot = fun from_value ->
           let value = from_value + 100
           in outer_snapshot value
-      in if x = 1 then continue_foo_normally 10
+      in if x = 1 then continue_foo_snapshot 10
       else outer_snapshot 20;;
 
 # let test_shift x =