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
open Lang

module Make (M : sig
  module Error : sig
    exception Unbound_variable of string

    exception Unbound_literal of Lang.literal
  end
end) =
struct
  let tbl = Hashtbl.create 512

  let literal = function
    | Unit ->
        ()
    | Bool _b ->
        ()
    | Custom s ->
        if not @@ Hashtbl.mem tbl s then
          raise @@ M.Error.Unbound_literal (Custom s)

  let const = function Literal l -> literal l | Var _id -> ()

  let rec expr = function
    | Const c ->
        const c
    | Bind (_p, e1, e2) ->
        expr e1 ; expr e2
    | Abstract (_b, _p, e) ->
        expr e
    | Apply (e1, e2) ->
        expr e1 ; expr e2
    | Match (_origin, match_expr, cases) ->
        List.iter literal (List.map fst cases) ;
        List.iter expr (match_expr :: List.map snd cases)
    | Type (id, cons, e) ->
        List.iter
          (fun con -> Hashtbl.add tbl con (Types.Custom (id, cons)))
          cons ;
        expr e

  let expr e =
    expr e ;
    fun x -> Hashtbl.find tbl x
end