- let example n : (int * int) =
- Continuation_monad.(let u = callcc (fun k ->
- (if n < 0 then k 0 else unit [n + 100])
- (* all of the following is skipped by k 0; the end type int is k's input type *)
- >>= fun [x] -> unit (x + 1)
- )
- (* k 0 starts again here, outside the callcc (...); the end type int * int is k's output type *)
- >>= fun x -> unit (x, 0)
- in run0 u)
-
-
- (* (+ 1000 (prompt (+ 100 (shift k (+ 10 1))))) ~~> 1011 *)
- let example1 () : int =
- Continuation_monad.(let v = reset (
- let u = shift (fun k -> unit (10 + 1))
- in u >>= fun x -> unit (100 + x)
- ) in let w = v >>= fun x -> unit (1000 + x)
- in run0 w)
-
- (* (+ 1000 (prompt (+ 100 (shift k (k (+ 10 1)))))) ~~> 1111 *)
- let example2 () =
- Continuation_monad.(let v = reset (
- let u = shift (fun k -> k (10 :: [1]))
- in u >>= fun x -> unit (100 :: x)
- ) in let w = v >>= fun x -> unit (1000 :: x)
- in run0 w)
-
- (* (+ 1000 (prompt (+ 100 (shift k (+ 10 (k 1)))))) ~~> 1111 but added differently *)
- let example3 () =
- Continuation_monad.(let v = reset (
- 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)
-
- (* (+ 100 ((prompt (+ 10 (shift k k))) 1)) ~~> 111 *)
- (* not sure if this example can be typed without a sum-type *)
-
- (* (+ 100 (prompt (+ 10 (shift k (k (k 1)))))) ~~> 121 *)
- let example5 () : int =
- Continuation_monad.(let v = reset (
- let u = shift (fun k -> k 1 >>= k)
- in u >>= fun x -> unit (10 + x)
- ) in let w = v >>= fun x -> unit (100 + x)
- in run0 w)
-
- ;;
-
-More:
-
- print_endline "=== test bare Continuation ============";;
-
- (1011, 1111, 1111, 121);;
- (example1(), example2(), example3(), example5());;
- ((111,0), (0,0));;
- (example ~+10, example ~-10);;
+ ; (+ 100 (reset (+ 10 (shift k (k (k 1)))))) ~~> 121
+
+ app2 (op2 plus) (var hundred)
+ (reset (app2 (op2 plus) (var ten)
+ (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)
+ in u >>= fun x -> unit (10 + x)
+ ) in let w = v >>= fun x -> unit (100 + x)
+ in run0 w)
+ - : int = 121