Debian Codegraph @

engine/nameops.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 Util
12 open Names
13
14 (* Utilities *)
15
16 module Subscript =
17 struct
18
19 type t = {
20 ss_zero : int;
21 (** Number of leading zeros of the subscript *)
22 ss_subs : int;
23 (** Digital value of the subscript, zero meaning empty *)
24 }
25
26 let rec overflow n =
27 Int.equal (n mod 10) 9 && (Int.equal (n / 10) 0 || overflow (n / 10))
28
29 let zero = { ss_subs = 0; ss_zero = 0 }
30
31 let succ s =
32 if Int.equal s.ss_subs 0 then
33 if Int.equal s.ss_zero 0 then
34 (* [] -> [0] *)
35 { ss_zero = 1; ss_subs = 0 }
36 else
37 (* [0...00] -> [0..01] *)
38 { ss_zero = s.ss_zero - 1; ss_subs = 1 }
39 else if overflow s.ss_subs then
40 if Int.equal s.ss_zero 0 then
41 (* [9...9] -> [10...0] *)
42 { ss_zero = 0; ss_subs = 1 + s.ss_subs }
43 else
44 (* [0...009...9] -> [0...010...0] *)
45 { ss_zero = s.ss_zero - 1; ss_subs = 1 + s.ss_subs }
46 else
47 (* [0...0n] -> [0...0{n+1}] *)
48 { ss_zero = s.ss_zero; ss_subs = s.ss_subs + 1 }
49
50 let equal s1 s2 =
51 Int.equal s1.ss_zero s2.ss_zero && Int.equal s1.ss_subs s2.ss_subs
52
53 let compare s1 s2 =
54 (* Lexicographic order is reversed in order to ensure that [succ] is strictly
55 increasing. *)
56 let c = Int.compare s1.ss_subs s2.ss_subs in
57 if Int.equal c 0 then Int.compare s1.ss_zero s2.ss_zero else c
58
59 end
60
61 let code_of_0 = Char.code '0'
62 let code_of_9 = Char.code '9'
63
64 let cut_ident skip_quote s =
65 let s = Id.to_string s in
66 let slen = String.length s in
67 (* [n'] is the position of the first non nullary digit *)
68 let rec numpart n n' =
69 if Int.equal n 0 then
70 (* ident made of _ and digits only [and ' if skip_quote]: don't cut it *)
71 slen
72 else
73 let c = Char.code (String.get s (n-1)) in
74 if Int.equal c code_of_0 && not (Int.equal n slen) then
75 numpart (n-1) n'
76 else if code_of_0 <= c && c <= code_of_9 then
77 numpart (n-1) (n-1)
78 else if skip_quote && (Int.equal c (Char.code '\'') || Int.equal c (Char.code '_')) then
79 numpart (n-1) (n-1)
80 else
81 n'
82 in
83 numpart slen slen
84
85 let repr_ident s =
86 let numstart = cut_ident false s in
87 let s = Id.to_string s in
88 let slen = String.length s in
89 if Int.equal numstart slen then
90 (s, None)
91 else
92 (String.sub s 0 numstart,
93 Some (int_of_string (String.sub s numstart (slen - numstart))))
94
95 let make_ident sa = function
96 | Some n ->
97 let c = Char.code (String.get sa (String.length sa -1)) in
98 let s =
99 if c < code_of_0 || c > code_of_9 then sa ^ (string_of_int n)
100 else sa ^ "_" ^ (string_of_int n) in
101 Id.of_string s
102 | None -> Id.of_string sa
103
104 let root_of_id id =
105 let suffixstart = cut_ident true id in
106 Id.of_string_soft (String.sub (Id.to_string id) 0 suffixstart)
107
108 (* Return the same identifier as the original one but whose {i subscript} is incremented.
109 If the original identifier does not have a suffix, [0] is appended to it.
110
111 Example mappings:
112
113 [bar] ↦ [bar0]
114 [bar0] ↦ [bar1]
115 [bar00] ↦ [bar01]
116 [bar1] ↦ [bar2]
117 [bar01] ↦ [bar02]
118 [bar9] ↦ [bar10]
119 [bar09] ↦ [bar10]
120 [bar99] ↦ [bar100]
121 *)
122 let increment_subscript id =
123 let id = Id.to_string id in
124 let len = String.length id in
125 let rec add carrypos =
126 let c = id.[carrypos] in
127 if is_digit c then
128 if Int.equal (Char.code c) (Char.code '9') then begin
129 assert (carrypos>0);
130 add (carrypos-1)
131 end
132 else begin
133 let newid = Bytes.of_string id in
134 Bytes.fill newid (carrypos+1) (len-1-carrypos) '0';
135 Bytes.set newid carrypos (Char.chr (Char.code c + 1));
136 newid
137 end
138 else begin
139 let newid = Bytes.of_string (id^"0") in
140 if carrypos < len-1 then begin
141 Bytes.fill newid (carrypos+1) (len-1-carrypos) '0';
142 Bytes.set newid (carrypos+1) '1'
143 end;
144 newid
145 end
146 in Id.of_bytes (add (len-1))
147
148 let has_subscript id =
149 let id = Id.to_string id in
150 is_digit (id.[String.length id - 1])
151
152 let get_subscript id =
153 let id0 = id in
154 let id = Id.to_string id in
155 let len = String.length id in
156 let rec get_suf accu pos =
157 if pos < 0 then (pos, accu)
158 else
159 let c = id.[pos] in
160 if is_digit c then get_suf (Char.code c - Char.code '0' :: accu) (pos - 1)
161 else (pos, accu)
162 in
163 let (pos, suf) = get_suf [] (len - 1) in
164 if Int.equal pos (len - 1) then (id0, Subscript.zero)
165 else
166 let id = String.sub id 0 (pos + 1) in
167 let rec compute_zeros accu = function
168 | [] -> (accu, [])
169 | 0 :: l -> compute_zeros (succ accu) l
170 | _ :: _ as l -> (accu, l)
171 in
172 let (ss_zero, suf) = compute_zeros 0 suf in
173 let rec compute_suf accu = function
174 | [] -> accu
175 | n :: l -> compute_suf (10 * accu + n) l
176 in
177 let ss_subs = compute_suf 0 suf in
178 (Id.of_string id, { Subscript.ss_subs; ss_zero; })
179
180 let add_subscript id ss =
181 if Subscript.equal Subscript.zero ss then id
182 else if Int.equal ss.Subscript.ss_subs 0 then
183 let id = Id.to_string id in
184 let pad = String.make ss.Subscript.ss_zero '0' in
185 Id.of_string (Printf.sprintf "%s%s" id pad)
186 else
187 let id = Id.to_string id in
188 let pad = String.make ss.Subscript.ss_zero '0' in
189 let suf = ss.Subscript.ss_subs in
190 Id.of_string (Printf.sprintf "%s%s%i" id pad suf)
191
192 let forget_subscript id =
193 let numstart = cut_ident false id in
194 let newid = Bytes.make (numstart+1) '0' in
195 String.blit (Id.to_string id) 0 newid 0 numstart;
196 (Id.of_bytes newid)
197
198 let add_suffix id s = Id.of_string (Id.to_string id ^ s)
199 let add_prefix s id = Id.of_string (s ^ Id.to_string id)
200
201 let atompart_of_id id = fst (repr_ident id)
202
203 (** Segment trees: efficient lookup of the next free integer *)
204 module SegTree :
205 sig
206 type t
207 val empty : t
208 val mem : int -> t -> bool
209 val add : int -> t -> t
210 val remove : int -> t -> t
211 val union : t -> t -> t
212
213 val next : int -> t -> int
214 (** [next n s] returns the smallest integer [k] not in [s] s.t. [n <= k] *)
215
216 val fresh : int -> t -> int * t
217 (** Efficient composition of [next] and [add] *)
218
219 val max_elt_opt : t -> int option
220
221 end =
222 struct
223
224 module Segment =
225 struct
226 type t = int * int (* segment [p, q[, in particular p < q *)
227 let compare (p, _) (q, _) = Int.compare p q
228 end
229
230 module SegSet = Set.Make(Segment)
231
232 type t = SegSet.t
233 (* Invariants: forall [p1, q1[, [p2, q2[ in such a set, either:
234 - p1 = p2 and q1 = q2
235 - p1 < q1 < p2 < q2
236 - p2 < q2 < p1 < q1 *)
237
238 let empty = SegSet.empty
239
240 let mem n s =
241 let find (_p, q) = n < q in
242 match SegSet.find_first_opt find s with
243 | None -> false
244 | Some (p, _q) -> p <= n
245
246 let add n s =
247 let find_min (_p, q) = n < q in
248 let find_max (_p, q) = q <= n in
249 match SegSet.find_first_opt find_min s with
250 | None ->
251 (* n larger than all elements *)
252 begin match SegSet.max_elt_opt s with
253 | None -> SegSet.add (n, n + 1) s
254 | Some (pl, ql) ->
255 if Int.equal n ql then SegSet.add (pl, n + 1) (SegSet.remove (pl, ql) s)
256 else SegSet.add (n, n + 1) s
257 end
258 | Some (pr, qr) ->
259 if pr <= n then s (* already present *)
260 else match SegSet.find_last_opt find_max s with
261 | None ->
262 (* n smaller than all elements *)
263 if Int.equal pr (n + 1) then
264 SegSet.add (n, qr) (SegSet.remove (pr, qr) s)
265 else SegSet.add (n, n + 1) s
266 | Some (pl, ql) ->
267 (* pl < ql <= n < pr < qr *)
268 if Int.equal ql n && Int.equal pr (n + 1) then
269 SegSet.add (pl, qr) (SegSet.remove (pl, ql) (SegSet.remove (pr, qr) s))
270 else if Int.equal ql n then
271 SegSet.add (pl, n + 1) (SegSet.remove (pl, ql) s)
272 else if Int.equal pr (n + 1) then
273 SegSet.add (n, qr) (SegSet.remove (pr, qr) s)
274 else
275 SegSet.add (n, n + 1) s
276
277 (* could probably be more efficient but I couldn't figure it out *)
278 let rec add_seg (x,y) s =
279 let s = add x s in
280 if Int.equal (x+1) y then s
281 else add_seg (x+1,y) s
282
283 let union s1 s2 =
284 let count s = SegSet.fold (fun (p,q) acc -> acc + (q-p)) s 0 in
285 let smaller, bigger =
286 if count s1 <= count s2 then s1, s2
287 else s2, s1
288 in
289 SegSet.fold add_seg smaller bigger
290
291 let remove n s =
292 let find_min (_p, q) = n < q in
293 match SegSet.find_first_opt find_min s with
294 | None -> s
295 | Some (pr, qr) ->
296 if pr <= n then
297 let s = SegSet.remove (pr, qr) s in
298 if Int.equal (pr + 1) qr then s
299 else if Int.equal pr n then SegSet.add (n + 1, qr) s
300 else if Int.equal (n + 1) qr then SegSet.add (pr, n) s
301 else SegSet.add (pr, n) (SegSet.add (n + 1, qr) s)
302 else s
303
304 let next n s =
305 let find (_p, q) = n < q in
306 match SegSet.find_first_opt find s with
307 | None -> n
308 | Some (p, q) -> if p <= n then q else n
309
310 let fresh n s =
311 let find_min (_p, q) = n < q in
312 let find_max (_p, q) = q <= n in
313 match SegSet.find_first_opt find_min s with
314 | None ->
315 let s = match SegSet.max_elt_opt s with
316 | None -> SegSet.add (n, n + 1) s
317 | Some (pl, ql) ->
318 if Int.equal n ql then SegSet.add (pl, n + 1) (SegSet.remove (pl, ql) s)
319 else SegSet.add (n, n + 1) s
320 in
321 n, s
322 | Some (pr, qr) ->
323 if pr <= n then
324 (* equivalent to adding qr *)
325 let next = SegSet.find_first_opt (fun (p, _q) -> qr < p) s in
326 let s = match next with
327 | None -> SegSet.add (pr, qr + 1) (SegSet.remove (pr, qr) s)
328 | Some (pk, qk) ->
329 if Int.equal (qr + 1) pk then
330 SegSet.add (pr, qk) (SegSet.remove (pk, qk) (SegSet.remove (pr, qr) s))
331 else
332 SegSet.add (pr, qr + 1) (SegSet.remove (pr, qr) s)
333 in
334 qr, s
335 else
336 let s = match SegSet.find_last_opt find_max s with
337 | None ->
338 if Int.equal pr (n + 1) then
339 SegSet.add (n, qr) (SegSet.remove (pr, qr) s)
340 else SegSet.add (n, n + 1) s
341 | Some (pl, ql) ->
342 if Int.equal ql n && Int.equal pr (n + 1) then
343 SegSet.add (pl, qr) (SegSet.remove (pl, ql) (SegSet.remove (pr, qr) s))
344 else if Int.equal ql n then
345 SegSet.add (pl, n + 1) (SegSet.remove (pl, ql) s)
346 else if Int.equal pr (n + 1) then
347 SegSet.add (n, qr) (SegSet.remove (pr, qr) s)
348 else
349 SegSet.add (n, n + 1) s
350 in
351 n, s
352
353 let max_elt_opt s = match SegSet.max_elt_opt s with
354 | None -> None
355 | Some (p, q) -> Some (q - 1)
356
357 end
358
359 module SubSet =
360 struct
361
362 type t = {
363 num : SegTree.t;
364 pre : SegTree.t list; (* lists are OK because we are already logarithmic *)
365 }
366 (* We represent sets of subscripts by case-splitting on ss_zero.
367 If it is zero, we store the number in the [num] set. Otherwise, we know
368 the set of possible values is finite. At position k, [pre] contains a set
369 of maximum size 10^k representing k-digit numbers with at least one leading
370 zero. *)
371
372 let empty = {
373 num = SegTree.empty;
374 pre = [];
375 }
376
377 let rec pow10 k accu = if k <= 0 then accu else pow10 (k - 1) (10 * accu)
378
379 let rec log10 n accu = if n <= 0 then accu else log10 (n / 10) (accu + 1)
380
381 let max_subscript ss =
382 let exp = log10 ss.Subscript.ss_subs 0 + ss.Subscript.ss_zero - 1 in
383 pow10 exp 1
384
385 let add ss s =
386 let open Subscript in
387 if Int.equal ss.ss_zero 0 then
388 { s with num = SegTree.add ss.ss_subs s.num }
389 else
390 let pre =
391 let len = List.length s.pre in
392 if len < ss.ss_zero then
393 s.pre @ List.make (ss.ss_zero - len) SegTree.empty
394 else s.pre
395 in
396 let set = match List.nth_opt pre (ss.ss_zero - 1) with
397 | None -> assert false
398 | Some m -> SegTree.add ss.ss_subs m
399 in
400 { s with pre = List.assign pre (ss.ss_zero - 1) set }
401
402 let union {num=num1; pre=pre1} {num=num2; pre=pre2} =
403 let rec merge_pre pre1 pre2 = match pre1, pre2 with
404 | [], x | x, [] -> x
405 | v1 :: rest1, v2 :: rest2 -> SegTree.union v1 v2 :: merge_pre rest1 rest2
406 in
407 let num = SegTree.union num1 num2 in
408 {num; pre=merge_pre pre1 pre2}
409
410 let remove ss s =
411 let open Subscript in
412 if Int.equal ss.ss_zero 0 then
413 { s with num = SegTree.remove ss.ss_subs s.num }
414 else match List.nth_opt s.pre (ss.ss_zero - 1) with
415 | None -> s
416 | Some m ->
417 let m = SegTree.remove ss.ss_subs m in
418 { s with pre = List.assign s.pre (ss.ss_zero - 1) m }
419
420 let mem ss s =
421 let open Subscript in
422 if Int.equal ss.ss_zero 0 then SegTree.mem ss.ss_subs s.num
423 else match List.nth_opt s.pre (ss.ss_zero - 1) with
424 | None -> false
425 | Some m -> SegTree.mem ss.ss_subs m
426
427 let ss_O = { Subscript.ss_zero = 1; ss_subs = 0 } (* [0] *)
428
429 let next ss s =
430 let open Subscript in
431 if ss.ss_zero > 0 then
432 match List.nth_opt s.pre (ss.ss_zero - 1) with
433 | None -> ss
434 | Some m ->
435 let next = SegTree.next ss.ss_subs m in
436 let max = max_subscript ss in
437 if max <= next then (* overflow *)
438 { ss_zero = 0; ss_subs = SegTree.next max s.num }
439 else
440 { ss_zero = ss.ss_zero; ss_subs = next }
441 else if Int.equal ss.ss_subs 0 then
442 (* Handle specially [] *)
443 if not @@ SegTree.mem 0 s.num then Subscript.zero
444 else match s.pre with
445 | [] -> ss_O
446 | m :: _ ->
447 if SegTree.mem 0 m then { ss_zero = 0; ss_subs = SegTree.next 1 s.num }
448 else ss_O
449 else
450 { ss_zero = 0; ss_subs = SegTree.next ss.ss_subs s.num }
451
452 let fresh ss s =
453 let open Subscript in
454 if ss.ss_zero > 0 then
455 match List.nth_opt s.pre (ss.ss_zero - 1) with
456 | None -> ss, add ss s
457 | Some m ->
458 let subs, m = SegTree.fresh ss.ss_subs m in
459 let max = max_subscript ss in
460 if max <= subs then
461 let subs, num = SegTree.fresh max s.num in
462 { ss_zero = 0; ss_subs = subs }, { s with num }
463 else
464 let s = { s with pre = List.assign s.pre (ss.ss_zero - 1) m } in
465 { ss_zero = ss.ss_zero; ss_subs = subs }, s
466 else if Int.equal ss.ss_subs 0 then
467 if not @@ SegTree.mem 0 s.num then
468 Subscript.zero, { num = SegTree.add 0 s.num; pre = s.pre }
469 else match s.pre with
470 | [] -> ss_O, { num = s.num; pre = [SegTree.add 0 SegTree.empty] }
471 | m :: rem ->
472 if SegTree.mem 0 m then
473 let subs, num = SegTree.fresh 1 s.num in
474 { ss_zero = 0; ss_subs = subs }, { num; pre = s.pre }
475 else ss_O, { num = s.num; pre = SegTree.add 0 SegTree.empty :: rem }
476 else
477 let subs, num = SegTree.fresh ss.ss_subs s.num in
478 { ss_zero = 0; ss_subs = subs }, { s with num }
479
480 let max_elt_opt s =
481 let mapi i m = match SegTree.max_elt_opt m with
482 | None -> None
483 | Some k -> Some { Subscript.ss_zero = i; ss_subs = k }
484 in
485 let maxs = List.mapi mapi (s.num :: s.pre) in
486 let fold s accu = match s with
487 | None -> accu
488 | Some ss ->
489 match accu with
490 | None -> Some ss
491 | Some ss' -> if Subscript.compare ss ss' <= 0 then accu else s
492 in
493 List.fold_left fold None maxs
494
495 end
496
497 module Fresh =
498 struct
499
500 type t = SubSet.t Id.Map.t
501
502 let empty = Id.Map.empty
503
504 let add id m =
505 let (id, s) = get_subscript id in
506 let old = try Id.Map.find id m with Not_found -> SubSet.empty in
507 Id.Map.add id (SubSet.add s old) m
508
509 let union m1 m2 = Id.Map.union (fun _ s1 s2 -> Some (SubSet.union s1 s2)) m1 m2
510
511 let remove id m =
512 let (id, s) = get_subscript id in
513 match Id.Map.find id m with
514 | old -> Id.Map.add id (SubSet.remove s old) m
515 | exception Not_found -> m
516
517 let mem id m =
518 let (id, s) = get_subscript id in
519 try SubSet.mem s (Id.Map.find id m)
520 with Not_found -> false
521
522 let next id0 m =
523 let (id, s) = get_subscript id0 in
524 match Id.Map.find_opt id m with
525 | None -> id0
526 | Some old ->
527 let ss = SubSet.next s old in
528 add_subscript id ss
529
530 let fresh id0 m =
531 let (id, s) = get_subscript id0 in
532 match Id.Map.find_opt id m with
533 | None ->
534 id0, Id.Map.add id (SubSet.add s SubSet.empty) m
535 | Some old ->
536 let ss, n = SubSet.fresh s old in
537 add_subscript id ss, Id.Map.add id n m
538
539 let of_list l =
540 List.fold_left (fun accu id -> add id accu) empty l
541
542 let of_set s =
543 Id.Set.fold add s empty
544
545 let of_named_context_val s =
546 of_set @@ Environ.ids_of_named_context_val s
547
548 let max_map s =
549 let filter id m = SubSet.max_elt_opt m in
550 Id.Map.filter_map filter s
551
552 end
553
554 (* Names *)
555
556 module type ExtName =
557 sig
558
559 include module type of struct include Names.Name end
560
561 exception IsAnonymous
562
563 val fold_left : ('a -> Id.t -> 'a) -> 'a -> t -> 'a
564 val fold_right : (Id.t -> 'a -> 'a) -> t -> 'a -> 'a
565 val iter : (Id.t -> unit) -> t -> unit
566 val map : (Id.t -> Id.t) -> t -> t
567 val fold_left_map : ('a -> Id.t -> 'a * Id.t) -> 'a -> t -> 'a * t
568 val fold_right_map : (Id.t -> 'a -> Id.t * 'a) -> Name.t -> 'a -> Name.t * 'a
569 val get_id : t -> Id.t
570 val pick : t -> t -> t
571 val pick_annot : (t,'r) Context.pbinder_annot -> (t,'r) Context.pbinder_annot -> (t,'r) Context.pbinder_annot
572 val cons : t -> Id.t list -> Id.t list
573 val to_option : Name.t -> Id.t option
574
575 end
576
577 module Name : ExtName =
578 struct
579
580 include Names.Name
581
582 exception IsAnonymous
583
584 let fold_left f a = function
585 | Name id -> f a id
586 | Anonymous -> a
587
588 let fold_right f na a =
589 match na with
590 | Name id -> f id a
591 | Anonymous -> a
592
593 let iter f na = fold_right (fun x () -> f x) na ()
594
595 let map f = function
596 | Name id -> Name (f id)
597 | Anonymous -> Anonymous
598
599 let fold_left_map f a = function
600 | Name id -> let (a, id) = f a id in (a, Name id)
601 | Anonymous -> a, Anonymous
602
603 let fold_right_map f na a = match na with
604 | Name id -> let (id, a) = f id a in (Name id, a)
605 | Anonymous -> Anonymous, a
606
607 let get_id = function
608 | Name id -> id
609 | Anonymous -> raise IsAnonymous
610
611 let pick na1 na2 =
612 match na1 with
613 | Name _ -> na1
614 | Anonymous -> na2
615
616 let pick_annot na1 na2 =
617 let open Context in
618 match na1.binder_name with
619 | Name _ -> na1 | Anonymous -> na2
620
621 let cons na l =
622 match na with
623 | Anonymous -> l
624 | Name id -> id::l
625
626 let to_option = function
627 | Anonymous -> None
628 | Name id -> Some id
629
630 end
631
632 (* Metavariables *)
633 let pr_meta = Pp.int
634 let string_of_meta = string_of_int