Debian Codegraph @

engine/evarnames.ml

raw symbols SCIP
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 open Names
12 open Nameops
13
14 (** This file contains methods for manipulating qualified evar names. *)
15
16 module EvSet = Evar.Set
17 module EvMap = Evar.Map
18
19 (** If true, this option enables automatic generation of goal names. *)
20 let { Goptions.get = generate_goal_names } =
21 Goptions.declare_bool_option_and_ref
22 ~key:["Generate";"Goal";"Names"]
23 ~value:false
24 ()
25
26 (** Internal representation of qualified evar names.
27
28 Example: "x.y.z" is represented as [{ basename: "z"; path: ["y"; "x"] }] *)
29 module EvarQualid :
30 sig
31 type t =
32 { basename: Id.t;
33 path: Id.t list }
34 val make : Libnames.qualid -> t
35 val repr : t -> Libnames.full_path
36 end =
37 struct
38 type t =
39 { basename: Id.t;
40 path: Id.t list }
41
42 let make path =
43 let (dp, id) = Libnames.repr_qualid path in
44 { basename = id; path = DirPath.repr dp }
45
46 let repr { basename; path } =
47 Libnames.make_path (DirPath.make path) basename
48
49 end
50
51 (** Module for evar name resolution, using a reversed trie.
52
53 Example: given evars "true.A" (?X1), "true.A" (?X2) and "false.A" (?X3), we
54 have the following trie:
55
56 [
57 {
58 A -> {
59 true (?X1, ?X2),
60 false (?X3)
61 }
62 }
63 ]
64
65 In this representation, determining whether a qualified name is unambiguous
66 amounts to checking whether the node has a single value. For example, "A"
67 does not resolve to an evar, "true.A" is ambiguous (?X1, ?X2), while
68 "false.A" (?X3) is unambiguous.
69 *)
70 module NameResolution :
71 sig
72 type t
73
74 (** Returns an empty trie. *)
75 val empty : t
76
77 (** Adds a new binding for the evar at the shortest unambiguous suffix of the
78 given qualified name, if possible. If there is no such suffix, creates an
79 ambiguous node. *)
80 val add : EvarQualid.t -> Evar.t -> t -> t
81
82 (** Transfers the qualified name of the first evar to the second evar. *)
83 val transfer : EvarQualid.t -> Evar.t -> Evar.t -> t -> t
84
85 (** Removes the qualified name of the given evar from name resolution. *)
86 val remove : EvarQualid.t -> Evar.t -> t -> t
87
88 (** Returns the shortest unambiguous name of the given qualified name.
89 Raises [Not_found] if the evar is not present at a suffix of the qualified
90 name. *)
91 val shortest_name : EvarQualid.t -> Evar.t -> t -> EvarQualid.t
92
93 (** Returns the list of bindings for the given qualified name. *)
94 val find : EvarQualid.t -> t -> Evar.Set.t
95
96 (** Returns true if there exists a binding that has the given basename. *)
97 val mem_basename : Id.t -> t -> bool
98 end =
99 struct
100 (** Represents a trie node. For code deduplication reasons, the root is also a node
101 with an empty value. *)
102 type t =
103 { value: Evar.Set.t;
104 children: t Id.Map.t }
105
106 open EvarQualid
107
108 let empty =
109 { value = Evar.Set.empty; children = Id.Map.empty }
110
111 let is_empty { value; children } =
112 Evar.Set.is_empty value && Id.Map.is_empty children
113
114 let rec add path ev node =
115 match path with
116 | segment :: rest ->
117 let update = function
118 | Some child -> Some (add rest ev child)
119 | None -> Some { value = Evar.Set.singleton ev; children = Id.Map.empty }
120 in
121 { node with children = Id.Map.update segment update node.children }
122 | [] ->
123 { node with value = Evar.Set.add ev node.value }
124
125 let add { basename; path } ev trie = add (basename :: path) ev trie
126
127 let rec transfer path ev ev' node =
128 match path with
129 | segment :: rest ->
130 { node with children = Id.Map.modify segment (fun _ child -> transfer rest ev ev' child) node.children }
131 | [] ->
132 { node with value = Evar.Set.add ev' (Evar.Set.remove ev node.value) }
133
134 let transfer { basename; path } ev ev' trie = transfer (basename :: path) ev ev' trie
135
136 let[@tail_mod_cons] rec shortest_name path ev node =
137 if Evar.Set.mem ev node.value then []
138 else
139 match path with
140 | segment :: rest -> segment :: shortest_name rest ev (Id.Map.find segment node.children)
141 | [] -> raise Not_found
142
143 let shortest_name { basename; path } ev trie =
144 match shortest_name (basename :: path) ev trie with
145 | basename :: path -> { basename; path }
146 | [] -> assert false
147
148 let rec find path node =
149 match path with
150 | segment :: rest ->
151 begin match Id.Map.find_opt segment node.children with
152 | Some segment -> find rest segment
153 | None -> Evar.Set.empty
154 end
155 | [] -> node.value
156
157 let find { basename; path } trie = find (basename :: path) trie
158
159 let rec remove path ev node =
160 match path with
161 | segment :: rest ->
162 let update_child = function
163 | Some child -> remove rest ev child
164 | None -> None
165 in
166 let node = { node with children = Id.Map.update segment update_child node.children } in
167 (* Prune empty nodes. *)
168 if is_empty node then None else Some node
169 | [] ->
170 let node = { node with value = Evar.Set.remove ev node.value } in
171 if is_empty node then None else Some node
172
173 let remove { basename; path } ev trie =
174 match remove (basename :: path) ev trie with
175 | Some trie -> trie
176 | None -> empty
177
178 let mem_basename basename trie =
179 Id.Map.mem basename trie.children
180 end
181
182 type t =
183 { basename_map : Id.t EvMap.t;
184 (** Map from evar to basename. *)
185
186 name_resolution : NameResolution.t;
187 (** Trie for resolving qualified names to evars. *)
188
189 fresh_gen : Fresh.t;
190 (** Fresh basename generator (to support [refine ?[?A]]) *)
191
192 parent_map : Evar.t EvMap.t;
193 (** Map from evar to its parent, if any. *)
194
195 children_map : EvSet.t EvMap.t;
196 (** Map from an evar to its children that are pending. Essentially the
197 reverse of [parent_map]. *)
198
199 removed_evars : EvSet.t;
200 (** Set of evars marked for removal, and thus unfocusable, whose names are still used as
201 the parent of an open goal. *)
202 }
203
204 let empty =
205 { basename_map = EvMap.empty;
206 name_resolution = NameResolution.empty;
207 fresh_gen = Fresh.empty;
208 parent_map = EvMap.empty;
209 children_map = EvMap.empty;
210 removed_evars = EvSet.empty
211 }
212
213 (** Returns the absolute path of [ev], obtained by following the [parent_map]. *)
214 let[@tail_mod_cons] rec path ev evn =
215 match EvMap.find_opt ev evn.parent_map with
216 | Some parent ->
217 begin match EvMap.find_opt parent evn.basename_map with
218 | Some parent_name -> parent_name :: path parent evn
219 | None -> []
220 end
221 | None -> []
222
223 (** Return the absolute qualified name of [ev]. *)
224 let absolute_name ev evn =
225 match EvMap.find_opt ev evn.basename_map with
226 | Some basename -> Some EvarQualid.{ basename; path = path ev evn }
227 | None -> None
228
229 (** Returns the shortest name that resolves to [ev], or [None] if [ev] does not
230 resolve to a name. *)
231 let shortest_name ev evn =
232 match absolute_name ev evn with
233 | Some name -> Some (NameResolution.shortest_name name ev evn.name_resolution)
234 | None -> None
235
236 (* Returns the set of focusable evars that have the given qualid as name. *)
237 let get_matching_evars qualid evn =
238 let evs = NameResolution.find qualid evn.name_resolution in
239 (* Do not consider removed evars as conflicts for name resolution purposes *)
240 Evar.Set.diff evs evn.removed_evars
241
242 let register_parent ev parent evn =
243 let add_child = function
244 | Some children -> Some (EvSet.add ev children)
245 | None -> Some (EvSet.singleton ev)
246 in
247 { evn with
248 parent_map = EvMap.add ev parent evn.parent_map;
249 children_map = EvMap.update parent add_child evn.children_map }
250
251 let add basename ev ?parent evn =
252 let evn =
253 match parent with
254 | Some parent -> register_parent ev parent evn
255 | None -> evn
256 in
257 let qualid = EvarQualid.{ basename; path = path ev evn } in
258 { evn with
259 basename_map = EvMap.add ev basename evn.basename_map;
260 name_resolution = NameResolution.add qualid ev evn.name_resolution;
261 fresh_gen = Fresh.add basename evn.fresh_gen }
262
263 let add_fresh basename ev ?parent evn =
264 let evn =
265 match parent with
266 | Some parent -> register_parent ev parent evn
267 | None -> evn
268 in
269 let qualid = EvarQualid.{ basename; path = path ev evn } in
270 let conflicts = get_matching_evars qualid evn in
271 if Evar.Set.is_empty conflicts then
272 (* No need to give the parent since it's already registered *)
273 add basename ev evn
274 else
275 (* Generate a fresh basename and try again. *)
276 let basename, fresh_gen = Fresh.fresh basename evn.fresh_gen in
277 add basename ev { evn with fresh_gen }
278
279 let rec remove ev evn =
280 match EvMap.find_opt ev evn.basename_map with
281 | None -> evn
282 | Some basename ->
283 (* When defining an evar and making its name unresolvable, there are two scenarios:
284 - The evar has no remaining children, in which case we can safely remove
285 it from all maps since it is not used for name resolution. We also try
286 to remove recursively its parent, since solving the evar might have
287 closed the parent.
288 - The evar has some children which might rely on the parent's name for
289 name resolution. In that case, we simply add it to [removed_evars] (so
290 that [name_of ev] fails), and removal will occur when all children will
291 be solved. *)
292 let children =
293 match EvMap.find_opt ev evn.children_map with
294 | Some children -> children
295 | None -> EvSet.empty
296 in
297 if EvSet.is_empty children then
298 let parent = EvMap.find_opt ev evn.parent_map in
299 let name_resolution =
300 match shortest_name ev evn with
301 | Some name -> NameResolution.remove name ev evn.name_resolution
302 | None -> assert false
303 in
304 let evn =
305 { basename_map = EvMap.remove ev evn.basename_map;
306 name_resolution;
307 fresh_gen =
308 (* If the basename still exists in the new trie, do not remove. *)
309 if NameResolution.mem_basename basename name_resolution then evn.fresh_gen
310 else Fresh.remove basename evn.fresh_gen;
311 children_map = EvMap.remove ev evn.children_map;
312 parent_map = EvMap.remove ev evn.parent_map;
313 removed_evars = EvSet.remove ev evn.removed_evars;
314 }
315 in
316 (* If there is a parent, try to remove it recursively as well. *)
317 match parent with
318 | Some parent ->
319 let evn = remove parent evn in
320 (* Rollback if removal failed. *)
321 { evn with removed_evars = EvSet.remove ev evn.removed_evars }
322 | None -> evn
323 else
324 (* Mark [ev] as deleted. *)
325 { evn with removed_evars = EvSet.add ev evn.removed_evars }
326
327 let transfer_name ev ev' evn =
328 (* We assume that [ev] is an open goal, hence undefined and
329 has no children. *)
330
331 (* Transfer the name. *)
332 let basename_map, name_resolution =
333 match shortest_name ev evn with
334 | Some name ->
335 let basename_map = EvMap.add ev' name.basename (EvMap.remove ev evn.basename_map) in
336 let name_resolution = NameResolution.transfer name ev ev' evn.name_resolution in
337 basename_map, name_resolution
338 | None -> (* [ev] has no name. *)
339 evn.basename_map, evn.name_resolution
340 in
341 (* If [ev] has a parent, we update the parent's children. *)
342 let parent_map, children_map =
343 match EvMap.find_opt ev evn.parent_map with
344 | Some parent ->
345 let parent_map = EvMap.add ev' parent (EvMap.remove ev evn.parent_map) in
346 let children_map = EvMap.modify parent (fun _ children -> EvSet.add ev' (EvSet.remove ev children)) evn.children_map in
347 parent_map, children_map
348 | None ->
349 evn.parent_map, evn.children_map
350 in
351 { evn with basename_map; name_resolution; parent_map; children_map }
352
353 type set_kind =
354 | SetEmpty
355 | SetSingleton of Evar.t
356 | SetOther
357
358 let classify_set s =
359 if Evar.Set.is_empty s then SetEmpty
360 else
361 let evk = Evar.Set.choose s in
362 let s = Evar.Set.remove evk s in
363 if Evar.Set.is_empty s then SetSingleton evk
364 else SetOther
365
366 let name_of ev evn =
367 match shortest_name ev evn with
368 | None -> None
369 | Some name ->
370 let conflicts = get_matching_evars name evn in
371 (* TODO: we should the caller handle the conflict themselves instead of
372 generating nonsensical names in linear time. *)
373 match classify_set conflicts with
374 | SetEmpty | SetSingleton _ -> Some (EvarQualid.repr name)
375 | SetOther ->
376 let nconflicts = Evar.Set.cardinal conflicts in
377 (* If the qualified name is ambiguous, we append a suffix corresponding to the index in the list. *)
378 let { EvarQualid.basename; path } = name in
379 let i = nconflicts - CList.index Evar.equal ev (Evar.Set.elements conflicts) - 1 in
380 let basename =
381 if Int.equal i (-1) then basename
382 else Id.of_string ((Id.to_string name.basename) ^ (string_of_int i))
383 in
384 Some (Libnames.make_path (DirPath.make path) basename)
385
386 let has_name ev evn =
387 not (EvSet.mem ev evn.removed_evars) && EvMap.mem ev evn.basename_map
388
389 let has_unambiguous_name ev evn =
390 match shortest_name ev evn with
391 | None -> false
392 | Some name ->
393 let matches = get_matching_evars name evn in
394 match classify_set matches with
395 | SetEmpty | SetOther -> false
396 | SetSingleton e -> Evar.equal e ev
397
398 let resolve fp evn =
399 let qualid = EvarQualid.make fp in
400 let evs = get_matching_evars qualid evn in
401 let open Pp in
402 match classify_set evs with
403 | SetEmpty -> raise Not_found
404 | SetSingleton ev -> ev
405 | SetOther ->
406 CErrors.user_err ?loc:fp.loc (str "Ambiguous evar name " ++ Libnames.pr_qualid fp ++ str ".")