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:
(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)
(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)