Source file Reader.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
open StdlibShim

module type Param =
sig
  type env
end

module type S =
sig
  include Param

  val read : unit -> env
  val scope : (env -> env) -> (unit -> 'a) -> 'a
  val run : env:env -> (unit -> 'a) -> 'a
end

module Make (P : Param) =
struct
  include P

  type _ Effect.t += Read : env Effect.t

  let read () = Effect.perform Read

  let run ~(env:env) f =
    let open Effect.Deep in
    try_with f ()
      { effc = fun (type a) (eff : a Effect.t) ->
            match eff with
            | Read -> Option.some @@ fun (k : (a, _) continuation) ->
              continue k env
            | _ -> None }

  let scope f c = run ~env:(f @@ read ()) c
end