Source file envs.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
open Domain.Lib
open Lang
open Util.Source

(* Variable environment functor *)

module VarMap = struct
  include Map.Make (Var)
end

module MakeVarEnv (V : sig
  type t

  val to_string : t -> string
end) =
struct
  include VarMap

  type t = V.t VarMap.t

  let to_string env =
    let to_string_binding (var, v) =
      Var.to_string var ^ " : " ^ V.to_string v
    in
    let bindings = bindings env in
    String.concat ", " (List.map to_string_binding bindings)
end

module VEnv = MakeVarEnv (Value)

(* Type definition environment *)

module TDEnv = struct
  include MakeTIdEnv (Type.Typdef)

  let rec unroll (tdenv : t) (typ : Il.typ) : Il.typ =
    match typ.it with
    | VarT (tid, targs) -> (
        let td = find tid tdenv in
        match td with
        | Param | Extern | Defining _ -> typ
        | Defined (tparams, deftyp) -> (
            let theta = TIdMap.of_lists tparams targs in
            match deftyp.it with
            | PlainT typ ->
                let typ = Type.Subst.subst_typ theta typ in
                unroll tdenv typ
            | _ -> typ))
    | _ -> typ
end

module TDTbl = MakeTIdTbl (Type.Typdef)