package dolmen

  1. Overview
  2. Docs

Source file scope.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280

(* This file is free software, part of Archsat. See file "LICENSE" for more details. *)

(* Wrapper around polymorphic identifiers *)
(* ************************************************************************ *)

module type S = Dolmen_intf.Scope.S
  with type name := Name.t
module type Arg = Dolmen_intf.Id.Scope_Full
  with type path := Path.t
   and type namespace := Namespace.t

(* Wrapping type/terms vars/consts into ids *)
(* ************************************************************************ *)

type 'a id = 'a Dolmen_intf.Scope.id =
  | Ty_var : 'ty_var -> < ty_var : 'ty_var; .. > id
  | Ty_cst : 'ty_cst -> < ty_cst : 'ty_cst; .. > id
  | Term_var : 'term_var -> < term_var : 'term_var; .. > id
  | Term_cst : 'term_cst -> < term_cst : 'term_cst; .. > id

module Wrap
    (Ty_var : Arg)(Ty_cst : Arg)
    (Term_var : Arg)(Term_cst : Arg)
  : Arg with type t = <
      ty_var : Ty_var.t;
      ty_cst : Ty_cst.t;
      term_var :  Term_var.t;
      term_cst : Term_cst.t
    > id
= struct

  type t = <
    ty_var : Ty_var.t;
    ty_cst : Ty_cst.t;
    term_var : Term_var.t;
    term_cst : Term_cst.t
  > id


  let equal i j =
    match i, j with
    | Ty_var v, Ty_var v' -> Ty_var.equal v v'
    | Ty_cst c, Ty_cst c' -> Ty_cst.equal c c'
    | Term_var v, Term_var v' -> Term_var.equal v v'
    | Term_cst c, Term_cst c' -> Term_cst.equal c c'
    | _ -> false

  let path = function
    | Ty_var v -> Ty_var.path v
    | Ty_cst c -> Ty_cst.path c
    | Term_var v -> Term_var.path v
    | Term_cst c -> Term_cst.path c

  let namespace = function
    | Ty_var v -> Ty_var.namespace v
    | Ty_cst c -> Ty_cst.namespace c
    | Term_var v -> Term_var.namespace v
    | Term_cst c -> Term_cst.namespace c

  module Map = struct

    type key = t
    type 'a t = {
      ty_vars : 'a Ty_var.Map.t;
      ty_csts : 'a Ty_cst.Map.t;
      term_vars : 'a Term_var.Map.t;
      term_csts : 'a Term_cst.Map.t;
    }

    let empty = {
      ty_vars = Ty_var.Map.empty;
      ty_csts = Ty_cst.Map.empty;
      term_vars = Term_var.Map.empty;
      term_csts = Term_cst.Map.empty;
    }

    let find_exn k t =
      match k with
      | Ty_var v -> Ty_var.Map.find_exn v t.ty_vars
      | Ty_cst c -> Ty_cst.Map.find_exn c t.ty_csts
      | Term_var v -> Term_var.Map.find_exn v t.term_vars
      | Term_cst c -> Term_cst.Map.find_exn c t.term_csts

    let find_opt k t =
      match k with
      | Ty_var v -> Ty_var.Map.find_opt v t.ty_vars
      | Ty_cst c -> Ty_cst.Map.find_opt c t.ty_csts
      | Term_var v -> Term_var.Map.find_opt v t.term_vars
      | Term_cst c -> Term_cst.Map.find_opt c t.term_csts

    let add k x t =
      match k with
      | Ty_var v -> { t with ty_vars = Ty_var.Map.add v x t.ty_vars; }
      | Ty_cst c -> { t with ty_csts = Ty_cst.Map.add c x t.ty_csts; }
      | Term_var v -> { t with term_vars = Term_var.Map.add v x t.term_vars; }
      | Term_cst c -> { t with term_csts = Term_cst.Map.add c x t.term_csts; }

    let find_add k f t =
      match k with
      | Ty_var v -> { t with ty_vars = Ty_var.Map.find_add v f t.ty_vars; }
      | Ty_cst c -> { t with ty_csts = Ty_cst.Map.find_add c f t.ty_csts; }
      | Term_var v -> { t with term_vars = Term_var.Map.find_add v f t.term_vars; }
      | Term_cst c -> { t with term_csts = Term_cst.Map.find_add c f t.term_csts; }

    let iter f t =
      Ty_var.Map.iter (fun v -> f (Ty_var v)) t.ty_vars;
      Ty_cst.Map.iter (fun c -> f (Ty_cst c)) t.ty_csts;
      Term_var.Map.iter (fun v -> f (Term_var v)) t.term_vars;
      Term_cst.Map.iter (fun c -> f (Term_cst c)) t.term_csts;
      ()

    let fold f t acc =
      let acc = Ty_var.Map.fold (fun v x acc -> f (Ty_var v) x acc) t.ty_vars acc in
      let acc = Ty_cst.Map.fold (fun c x acc -> f (Ty_cst c) x acc) t.ty_csts acc in
      let acc = Term_var.Map.fold (fun v x acc -> f (Term_var v) x acc) t.term_vars acc in
      let acc = Term_cst.Map.fold (fun c x acc -> f (Term_cst c) x acc) t.term_csts acc in
      acc
  end

end

(* Printing wrappers for escapped sequences *)
(* ************************************************************************ *)


module Make(Tid : Arg) : S with type id := Tid.t = struct

  (* Note:
     Tid : module for Typied identifiers
     Pid : module for Parsed/Printed identifiers *)
  module Pid = Id

  type binding =
    | Same of Pid.t
    | Renamed of { original : Name.t; renamed : Pid.t; }

  type on_conflict =
    | Error
    | Shadow
    | Rename

  type rename =
    | Rename : {
        acc : 'acc;
        rename : 'acc -> Name.t -> 'acc * Name.t
      } -> rename

  type conf = {
    rename : rename;
    sanitize : Tid.t -> Name.t -> Name.t;
    on_conflict :
      prev_id:Tid.t ->
      new_id:Tid.t ->
      name:Name.t ->
      on_conflict;
  }

  type t = {
    conf : conf;
    in_scope : Tid.t Pid.Map.t;
    bindings : binding Tid.Map.t;
  }

  exception Conflict of {
      prev_id : Tid.t;
      prev_binding : binding;
      new_id : Tid.t;
      new_binding : binding;
    }

  let pid = function
    | Same id | Renamed { renamed = id; original = _; } -> id

  let mk_rename init rename = Rename { acc = init; rename; }

  let empty ~rename ~sanitize ~on_conflict =
    let conf = { sanitize; rename; on_conflict; } in
    { conf;
      bindings = Tid.Map.empty;
      in_scope = Pid.Map.empty; }


  (* Adding escapped sequences *)
  (* ************************************************************************ *)

  let binding original pid =
    if Name.equal (Pid.name pid) original
    then Same pid
    else Renamed { original; renamed = pid; }

  (* do NOT export: this functions unconditionally overwrites any
     pre-existing binding *)
  let unsafe_add t tid original pid =
    let binding = binding original pid in
    let t =
      { t with
      in_scope = Pid.Map.add pid tid t.in_scope;
      bindings = Tid.Map.add tid binding t.bindings; }
    in
    t

  let rec find_name t ns name acc rename =
    let new_acc, new_name = rename acc name in
    let pid = Pid.create ns new_name in
    match Pid.Map.find_opt pid t.in_scope with
    | None -> pid
    | Some _other_name -> find_name t ns name new_acc rename

  let bind t id =
    match Tid.Map.find_opt id t.bindings with
    | Some _ ->
      assert false (* binding already bound id, TODO: proper error *)
    | None ->
      let original =
        match Tid.path id with
        | Local { name; } | Absolute { name; path = []; } ->
          Name.simple name
        | Absolute { name; path; } ->
          Name.qualified path name
      in
      let ns = Tid.namespace id in
      let name = t.conf.sanitize id original in
      let pid = Pid.create ns name in
      begin match Pid.Map.find_opt pid t.in_scope with
        | None ->
          (* no name conflict *)
          unsafe_add t id original pid
        | Some prev_id ->
          begin match Tid.Map.find_opt prev_id t.bindings with
            | None ->
              (* internal error: the [bindings] and [in_scope] fields
                 should form a bijection. *)
              assert false
            | Some prev_binding ->
              if Tid.equal id prev_id then
                (* we may want to emit a warning, but this is harmless *)
                t
              else begin
                match t.conf.on_conflict ~prev_id ~new_id:id ~name with
                | Error ->
                  raise (Conflict { prev_id; prev_binding; new_id = id;
                                    new_binding = binding original pid; })
                | Shadow ->
                  unsafe_add t id original pid
                | Rename ->
                  let Rename { acc; rename; } = t.conf.rename in
                  let pid = find_name t ns name acc rename in
                  (* sanity check *)
                  assert (Name.equal (Pid.name pid) (t.conf.sanitize id (Pid.name pid)));
                  unsafe_add t id original pid
              end
          end
      end

  (* Printing *)
  (* ************************************************************************ *)

  let name t id =
    (* TODO: allow for globals from other modules
       to be printed without being bound *)
    match Tid.Map.find_opt id t.bindings with
    | None ->
      (* TODO: proper error, missing id. *)
      failwith (Format.asprintf "cannot find binding for id '%a'" Path.print (Tid.path id))
    | Some binding ->
      let pid = pid binding in
      begin match Pid.Map.find_opt pid t.in_scope with
        | Some id' ->
          if Tid.equal id id'
          then Pid.name pid
          else assert false (* TODO: proper error; id had been shadowed by id' *)
        | None ->
          (* internal invariant error: [bindings] and [in_scope]
             do not represent a bijection. *)
          assert false
      end

end