X-Git-Url: http://lambda.jimpryor.net/git/gitweb.cgi?p=lambda.git;a=blobdiff_plain;f=topics%2F_cps_and_continuation_operators.mdwn;h=679f0fb420bcfb2913860cca60396125da2b117e;hp=72b0034235e0c2be0e891c350ed168dee3b6360f;hb=186ef55ff472f81b0b62acde8948ed9025f26d04;hpb=b2faf9836a400084a4ed5c64ccf056e6edf7be57 diff --git a/topics/_cps_and_continuation_operators.mdwn b/topics/_cps_and_continuation_operators.mdwn index 72b00342..679f0fb4 100644 --- a/topics/_cps_and_continuation_operators.mdwn +++ b/topics/_cps_and_continuation_operators.mdwn @@ -175,6 +175,8 @@ So too will examples. We'll give some examples, and show you how to try them out +-- cutting for control operators -- + 3. `callcc` was originally introduced in Scheme. There it's written `call/cc` and is an abbreviation of `call-with-current-continuation`. Instead of the somewhat bulky form: (call/cc (lambda (k) ...)) @@ -211,6 +213,8 @@ What happens here? First, we capture the continuation where `p` is about to be a '(2 2 . #) +-- end of cut -- + Ok, so now let's see how to perform these same computations via CPS. In the lambda evaluator: @@ -276,6 +280,8 @@ The third example is more difficult to make work with the monadic library, becau +-- cutting following section for control operators -- + Some callcc/letcc exercises --------------------------- @@ -464,6 +470,10 @@ The box is working like a reset. The `abort` is implemented with a `shift`. Earl `snapshot` here corresponds to the code outside the `reset`. `continue_normally` is the middle block of code, between the `shift` and its surrounding `reset`. This is what gets bound to the `k` in our `shift`. The `if...` statement is inside a `shift`. Notice there that we invoke the bound continuation to "continue normally". We just invoke the outer continuation, saved in `snapshot` when we placed the `reset`, to skip the "continue normally" code and immediately abort to outside the box. + +-- end of cut -- + + Using `shift` and `reset` operators in OCaml, this would look like this: #require "delimcc";; @@ -515,6 +525,8 @@ In collecting these CPS transforms and implementing the monadic versions, we've * Sabry, "Note on axiomatizing the semantics of control operators" (1996) +-- cutting some of the following for control operators -- + Examples of shift/reset/abort -----------------------------