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
open StdlibShim
module type Param =
sig
type state
end
module type S =
sig
include Param
val get : unit -> state
val set : state -> unit
val modify : (state -> state) -> unit
val run : init:state -> (unit -> 'a) -> 'a
end
module Make (P : Param) =
struct
include P
type _ Effect.t +=
| Get : state Effect.t
| Set : state -> unit Effect.t
let get () = Effect.perform Get
let set st = Effect.perform (Set st)
let run ~(init:state) f =
let open Effect.Deep in
let st = ref init in
try_with f ()
{ effc = fun (type a) (eff : a Effect.t) ->
match eff with
| Get -> Option.some @@ fun (k : (a, _) continuation) ->
continue k !st
| Set v -> Option.some @@ fun (k : (a, _) continuation) ->
st := v; continue k ()
| _ -> None }
let modify f = set @@ f @@ get ()
end