X-Git-Url: http://lambda.jimpryor.net/git/gitweb.cgi?p=lambda.git;a=blobdiff_plain;f=cps_and_continuation_operators.mdwn;h=258f7227f66f093cf5d7e5f1989ea25f943ecc6e;hp=3b0dda78ffd40b2ad7e4da0ea855c2e388fbc339;hb=9a23d7fc448e48268a7b60ce9ff3e55222cd833a;hpb=62f467c824015512f41061fe2777dbc326649f5b diff --git a/cps_and_continuation_operators.mdwn b/cps_and_continuation_operators.mdwn index 3b0dda78..258f7227 100644 --- a/cps_and_continuation_operators.mdwn +++ b/cps_and_continuation_operators.mdwn @@ -401,40 +401,41 @@ Here are some more examples of using delimited control operators. We present eac Example 1: - ; (+ 100 (+ 10 (abort 1))) ~~> 1 + ; (+ 1000 (+ 100 (abort 11))) ~~> 11 - app2 (op2 plus) (var hundred) - (app2 (op2 plus) (var ten) (abort (var one))) + app2 (op2 plus) (var thousand) + (app2 (op2 plus) (var hundred) (abort (var eleven))) # Continuation_monad.(run0( - abort 1 >>= fun i -> - unit (10+i) >>= fun j -> - unit (100+j)));; - - : int = 1 + abort 11 >>= fun i -> + unit (100+i) >>= fun j -> + unit (1000+j)));; + - : int = 11 When no `reset` is specified, there's understood to be an implicit one surrounding the entire computation (but unlike in the case of `callcc`, you still can't capture up to *and including* the end of the computation). So it makes no difference if we say instead: # Continuation_monad.(run0( reset ( - abort 1 >>= fun i -> - unit (10+i) >>= fun j -> - unit (100+j))));; - - : int = 1 + abort 11 >>= fun i -> + unit (100+i) >>= fun j -> + unit (1000+j))));; + - : int = 11 Example 2: - ; (+ 100 (reset (+ 10 (abort 1)))) ~~> 101 + ; (+ 1000 (reset (+ 100 (abort 11)))) ~~> 1011 - app2 (op2 plus) (var hundred) - (reset (app2 (op2 plus) (var ten) (abort (var one)))) + app2 (op2 plus) (var thousand) + (reset (app2 (op2 plus) (var hundred) (abort (var eleven)))) # Continuation_monad.(run0( reset ( - abort 1 >>= fun i -> - unit (10+i)) >>= fun j -> - unit (100+j)));; - - : int = 101 + abort 11 >>= fun i -> + unit (100+i) + ) >>= fun j -> + unit (1000+j)));; + - : int = 1011 Example 3: @@ -477,7 +478,7 @@ To demonstrate the different adding order between Examples 4 and 5, we use `::` (shift (\k. ((op2 plus) (var ten) (app (var k) (var one))))))) Continuation_monad.(let v = reset ( - let u = shift (fun k -> k [1] >>= fun x -> unit (10 :: x)) + let u = shift (fun k -> k [1] >>= fun x -> unit (10 :: x)) in u >>= fun x -> unit (100 :: x) ) in let w = v >>= fun x -> unit (1000 :: x) in run0 w) @@ -503,7 +504,7 @@ Example 7: (shift (\k. app (var k) (app (var k) (var one)))))) Continuation_monad.(let v = reset ( - let u = shift (fun k -> k 1 >>= fun x -> k x) + let u = shift (fun k -> k 1 >>= fun x -> k x) in u >>= fun x -> unit (10 + x) ) in let w = v >>= fun x -> unit (100 + x) in run0 w)