-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathshallow_state.ml
55 lines (46 loc) · 1.08 KB
/
shallow_state.ml
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
(* TEST
include stdlib_alpha;
runtime5;
{ bytecode; }
{ native; }
*)
module Effect = Stdlib_alpha.Effect
open Effect
(*
let handle_state init f x =
let rec loop state k x =
continue k x with
| result -> result, state
| effect Get, k -> loop state k state
| effect Set new_state, k -> loop new_state k ()
in
loop init (fiber f) x
*)
type 'a op =
| Get : int op
| Set : int -> unit op
module Eff = Effect.Make (struct
type 'a t = 'a op
end)
open Eff
let handle_state init f x =
let rec handle (state : int) = function
| Value result -> result, state
| Exception e -> raise e
| Operation (Get, k) -> handle state (continue k state [])
| Operation (Set new_state, k) -> handle new_state (continue k () [])
in
let res = run (fun h -> f h x) in
handle init res
;;
let comp h () =
Printf.printf "Initial state: %d\n" (perform h Get);
perform h (Set 42);
Printf.printf "Updated state: %d\n" (perform h Get);
perform h (Set 43)
;;
let main () =
let (), i = handle_state 0 comp () in
Printf.printf "Final state: %d\n" i
;;
let _ = main ()