-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathdyn_wind.ml
More file actions
32 lines (29 loc) · 796 Bytes
/
Copy pathdyn_wind.ml
File metadata and controls
32 lines (29 loc) · 796 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
(* User-land dynamic wind:
http://okmij.org/ftp/continuations/implementations.html#dynamic-wind *)
open Effect
open Effect.Deep
let dynamic_wind before_thunk thunk after_thunk =
before_thunk ();
let res =
match thunk () with
| v -> v
| exception e -> after_thunk (); raise e
| effect e, k ->
after_thunk ();
let res' = perform e in
before_thunk ();
continue k res'
in
after_thunk ();
res
type _ eff += E : unit eff
let () =
let bt () = Printf.printf "IN\n" in
let at () = Printf.printf "OUT\n" in
let foo () =
Printf.printf "perform E\n"; perform E;
Printf.printf "perform E\n"; perform E;
Printf.printf "done\n"
in
try dynamic_wind bt foo at with
| effect E, k -> Printf.printf "handled E\n"; continue k ()