forked from ocaml-multicore/effects-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate.ml
More file actions
43 lines (34 loc) · 903 Bytes
/
state.ml
File metadata and controls
43 lines (34 loc) · 903 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
33
34
35
36
37
38
39
40
41
42
43
open Printf
module type STATE = sig
type t
val put : t -> unit
val get : unit -> t
val run : (unit -> unit) -> init:t -> unit
end
module State (S : sig type t end) : STATE with type t = S.t = struct
type t = S.t
effect Put : t -> unit
let put v = perform (Put v)
effect Get : t
let get () = perform Get
let run f ~init =
let comp =
match f () with
| () -> (fun s -> ())
| effect (Put s') k -> (fun s -> continue k () s')
| effect Get k -> (fun s -> continue k s s)
in comp init
end
module IS = State (struct type t = int end)
module SS = State (struct type t = string end)
let foo () : unit =
printf "%d\n" (IS.get ());
IS.put 42;
printf "%d\n" (IS.get ());
IS.put 21;
printf "%d\n" (IS.get ());
SS.put "hello";
printf "%s\n" (SS.get ());
SS.put "world";
printf "%s\n" (SS.get ())
let _ = IS.run (fun () -> SS.run foo "") 0