Debian Codegraph @

kernel/names.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 (* File created around Apr 1994 for CiC V5.10.5 by Chet Murthy collecting
12 parts of file initial.ml from CoC V4.8, Dec 1988, by Gérard Huet,
13 Thierry Coquand and Christine Paulin *)
14 (* Hash-consing by Bruno Barras in Feb 1998 *)
15 (* Extra functions for splitting/generation of identifiers by Hugo Herbelin *)
16 (* Restructuration by Jacek Chrzaszcz as part of the implementation of
17 the module system, Aug 2002 *)
18 (* Abstraction over the type of constant for module inlining by Claudio
19 Sacerdoti, Nov 2004 *)
20 (* Miscellaneous features or improvements by Hugo Herbelin,
21 Élie Soubiran, ... *)
22
23 open Pp
24 open Util
25
26 (** {6 Identifiers } *)
27
28 (** Representation and operations on identifiers. *)
29 module Id =
30 struct
31 type t = string
32
33 let equal = String.equal
34
35 let compare = String.compare
36
37 let hash = String.hash
38
39 let check_valid ?(strict=true) x =
40 let iter (fatal, x) = if fatal || strict then CErrors.user_err Pp.(str x) in
41 Option.iter iter (Unicode.ident_refutation x)
42
43 let is_valid s = match Unicode.ident_refutation s with
44 | None -> true
45 | Some _ -> false
46
47 let is_valid_ident_part s = match Unicode.ident_refutation ("x"^s) with
48 | None -> true
49 | Some _ -> false
50
51 let of_bytes s =
52 let s = Bytes.to_string s in
53 check_valid s;
54 snd @@ String.hcons s
55
56 let of_string s =
57 let () = check_valid s in
58 snd @@ String.hcons s
59
60 let of_string_soft s =
61 let () = check_valid ~strict:false s in
62 snd @@ String.hcons s
63
64 let to_string id = id
65
66 let print id = str id
67
68 module Set = CString.Set
69 module Map = CString.Map
70 module Pred = CString.Pred
71
72 module List = String.List
73
74 let hcons = String.hcons
75
76 end
77
78 (** Representation and operations on identifiers that are allowed to be anonymous
79 (i.e. "_" in concrete syntax). *)
80 module Name =
81 struct
82 type t = Anonymous (** anonymous identifier *)
83 | Name of Id.t (** non-anonymous identifier *)
84
85 let mk_name id =
86 Name id
87
88 let is_anonymous = function
89 | Anonymous -> true
90 | Name _ -> false
91
92 let is_name = is_anonymous %> not
93
94 let compare n1 n2 = match n1, n2 with
95 | Anonymous, Anonymous -> 0
96 | Name id1, Name id2 -> Id.compare id1 id2
97 | Anonymous, Name _ -> -1
98 | Name _, Anonymous -> 1
99
100 let equal n1 n2 = match n1, n2 with
101 | Anonymous, Anonymous -> true
102 | Name id1, Name id2 -> String.equal id1 id2
103 | _ -> false
104
105 let hash = function
106 | Anonymous -> 0
107 | Name id -> Id.hash id
108
109 let print = function
110 | Anonymous -> str "_"
111 | Name id -> Id.print id
112
113 module Self_Hashcons =
114 struct
115 type nonrec t = t
116 let hashcons = function
117 | Name id ->
118 let h, id = Id.hcons id in
119 h, Name id
120 | Anonymous -> 0, Anonymous
121 let eq n1 n2 =
122 n1 == n2 ||
123 match (n1,n2) with
124 | (Name id1, Name id2) -> id1 == id2
125 | (Anonymous,Anonymous) -> true
126 | _ -> false
127 end
128
129 module Hname = Hashcons.Make(Self_Hashcons)
130
131 let hcons = Hashcons.simple_hcons Hname.generate Hname.hcons ()
132
133 end
134
135 (** Alias, to import constructors. *)
136 type name = Name.t = Anonymous | Name of Id.t
137
138 (** {6 Various types based on identifiers } *)
139
140 type variable = Id.t
141
142 (** {6 Directory paths = section names paths } *)
143
144 (** Dirpaths are lists of module identifiers.
145 The actual representation is reversed to optimise sharing:
146 Corelib.A.B is ["B";"A";"Corelib"] *)
147
148 let dummy_module_name = "If you see this, it's a bug"
149
150 module DirPath =
151 struct
152 module Self = struct
153 type t = Id.t list
154
155 let compare = List.compare Id.compare
156 let equal = List.equal Id.equal
157 end
158 include Self
159
160 let rec hash accu = function
161 | [] -> accu
162 | id :: dp ->
163 let accu = Hashset.Combine.combine (Id.hash id) accu in
164 hash accu dp
165
166 let hash dp = hash 0 dp
167
168 let make x = x
169 let repr x = x
170
171 let empty = []
172
173 let is_empty = List.is_empty
174
175 let to_string = function
176 | [] -> "<>"
177 | sl -> String.concat "." (List.rev_map Id.to_string sl)
178
179 let print dp = str (to_string dp)
180
181 let dummy = [dummy_module_name]
182
183 module Hdir = Hashcons.Hlist(Id)
184
185 let hcons = Hashcons.simple_hcons Hdir.generate Hdir.hcons ()
186
187 module Set = Set.Make(Self)
188 module Map = Map.Make(Self)
189
190 end
191
192 module DPset = DirPath.Set
193 module DPmap = DirPath.Map
194
195 (** {6 Unique names for bound modules } *)
196
197 module MBId =
198 struct
199 type t = int * Id.t * DirPath.t
200
201 let gen =
202 let seed = ref 0 in fun () ->
203 let ans = !seed in
204 let () = incr seed in
205 ans
206
207 let make dir s = (gen(), s, dir)
208
209 let repr mbid = mbid
210
211 let to_string (_i, s, p) =
212 DirPath.to_string p ^ "." ^ s
213
214 let debug_to_string (i, s, p) =
215 "<"^DirPath.to_string p ^"#" ^ s ^"#"^ string_of_int i^">"
216
217 let compare (x : t) (y : t) =
218 if x == y then 0
219 else match (x, y) with
220 | (nl, idl, dpl), (nr, idr, dpr) ->
221 let ans = Int.compare nl nr in
222 if not (Int.equal ans 0) then ans
223 else
224 let ans = Id.compare idl idr in
225 if not (Int.equal ans 0) then ans
226 else
227 DirPath.compare dpl dpr
228
229 let equal x y = x == y ||
230 let (i1, id1, p1) = x in
231 let (i2, id2, p2) = y in
232 Int.equal i1 i2 && Id.equal id1 id2 && DirPath.equal p1 p2
233
234 let to_id (_, s, _) = s
235
236 open Hashset.Combine
237
238 let hash (i, id, dp) =
239 combine3 (Int.hash i) (Id.hash id) (DirPath.hash dp)
240
241 module Self_Hashcons =
242 struct
243 type nonrec t = t
244 let hashcons (n,s,dir) =
245 let hs, s = Id.hcons s in
246 let hdir, dir = DirPath.hcons dir in
247 combine3 (Int.hash n) hs hdir, (n,s,dir)
248 let eq ((n1,s1,dir1) as x) ((n2,s2,dir2) as y) =
249 (x == y) ||
250 (Int.equal n1 n2 && s1 == s2 && dir1 == dir2)
251 end
252
253 module HashMBId = Hashcons.Make(Self_Hashcons)
254
255 let hcons = Hashcons.simple_hcons HashMBId.generate HashMBId.hcons ()
256
257 module Self = struct
258 type nonrec t = t
259 let compare = compare
260 end
261
262 module Set = Set.Make(Self)
263 module Map = CMap.Make(Self)
264
265 end
266
267 module MBImap = MBId.Map
268 module MBIset = MBId.Set
269
270 (** {6 The module part of the kernel name } *)
271
272 module ModPath = struct
273
274 type t =
275 | MPfile of DirPath.t
276 | MPbound of MBId.t
277 | MPdot of t * Id.t
278
279 type module_path = t
280
281 let rec is_bound = function
282 | MPbound _ -> true
283 | MPdot(mp,_) -> is_bound mp
284 | _ -> false
285
286 let rec to_string = function
287 | MPfile sl -> DirPath.to_string sl
288 | MPbound uid -> MBId.to_string uid
289 | MPdot (mp,l) -> to_string mp ^ "." ^ Id.to_string l
290
291 let print mp = str (to_string mp)
292
293 let rec debug_to_string = function
294 | MPfile sl -> DirPath.to_string sl
295 | MPbound uid -> MBId.debug_to_string uid
296 | MPdot (mp,l) -> debug_to_string mp ^ "." ^ Id.to_string l
297
298 (** we compare labels first if both are MPdots *)
299 let rec compare mp1 mp2 =
300 if mp1 == mp2 then 0
301 else match mp1, mp2 with
302 | MPfile p1, MPfile p2 -> DirPath.compare p1 p2
303 | MPbound id1, MPbound id2 -> MBId.compare id1 id2
304 | MPdot (mp1, l1), MPdot (mp2, l2) ->
305 let c = String.compare l1 l2 in
306 if not (Int.equal c 0) then c
307 else compare mp1 mp2
308 | MPfile _, _ -> -1
309 | MPbound _, MPfile _ -> 1
310 | MPbound _, MPdot _ -> -1
311 | MPdot _, _ -> 1
312
313 let rec equal mp1 mp2 = mp1 == mp2 ||
314 match mp1, mp2 with
315 | MPfile p1, MPfile p2 -> DirPath.equal p1 p2
316 | MPbound id1, MPbound id2 -> MBId.equal id1 id2
317 | MPdot (mp1, l1), MPdot (mp2, l2) -> String.equal l1 l2 && equal mp1 mp2
318 | (MPfile _ | MPbound _ | MPdot _), _ -> false
319
320 let rec subpath mp mp' =
321 if equal mp mp' then true
322 else match mp' with
323 | MPdot (mp', _) -> subpath mp mp'
324 | _ -> false
325
326 open Hashset.Combine
327
328 let rec hash = function
329 | MPfile dp -> combinesmall 1 (DirPath.hash dp)
330 | MPbound id -> combinesmall 2 (MBId.hash id)
331 | MPdot (mp, lbl) ->
332 combinesmall 3 (combine (hash mp) (Id.hash lbl))
333
334 let dummy = MPfile DirPath.dummy
335
336 let rec dp = function
337 | MPfile sl -> sl
338 | MPbound (_,_,dp) -> dp
339 | MPdot (mp,_l) -> dp mp
340
341 module Self_Hashcons = struct
342 type t = module_path
343 let hashcons hcons = function
344 | MPfile dir ->
345 let hdir, dir = DirPath.hcons dir in
346 combinesmall 1 hdir, MPfile dir
347 | MPbound m ->
348 let hm, m = MBId.hcons m in
349 combinesmall 2 hm, MPbound m
350 | MPdot (md,l) ->
351 let hmd, md = hcons md in
352 let hl, l = Id.hcons l in
353 combinesmall 3 (combine hmd hl), MPdot (md, l)
354 let eq d1 d2 =
355 d1 == d2 ||
356 match d1,d2 with
357 | MPfile dir1, MPfile dir2 -> dir1 == dir2
358 | MPbound m1, MPbound m2 -> m1 == m2
359 | MPdot (mod1,l1), MPdot (mod2,l2) -> l1 == l2 && equal mod1 mod2
360 (* XXX use physical equality for mod1 = mod2 *)
361 | _ -> false
362 end
363
364 module HashMP = Hashcons.MakeRec(Self_Hashcons)
365
366 let hcons = Hashcons.simple_hcons HashMP.generate HashMP.hcons ()
367
368 module Self = struct
369 type nonrec t = t
370 let compare = compare
371 end
372
373 module Set = Set.Make(Self)
374 module Map = CMap.Make(Self)
375
376 end
377
378 module MPset = ModPath.Set
379 module MPmap = ModPath.Map
380
381 (** {6 Kernel names } *)
382
383 module KerName = struct
384
385 type t = {
386 modpath : ModPath.t;
387 knlabel : Id.t;
388 refhash : int;
389 (** Lazily computed hash. If unset, it is set to negative values. *)
390 }
391
392 type kernel_name = t
393
394 let make modpath knlabel =
395 let open Hashset.Combine in
396 let refhash = combine (ModPath.hash modpath) (Id.hash knlabel) in
397 (* Truncate for backwards compatibility w.r.t. ordering *)
398 let refhash = refhash land 0x3FFFFFFF in
399 { modpath; knlabel; refhash; }
400
401 let repr kn = (kn.modpath, kn.knlabel)
402
403 let modpath kn = kn.modpath
404 let label kn = kn.knlabel
405
406 let to_string_gen mp_to_string kn =
407 mp_to_string kn.modpath ^ "." ^ Id.to_string kn.knlabel
408
409 let to_string kn = to_string_gen ModPath.to_string kn
410
411 let debug_to_string kn = to_string_gen ModPath.debug_to_string kn
412
413 let print kn = str (to_string kn)
414
415 let debug_print kn = str (debug_to_string kn)
416
417 let compare (kn1 : kernel_name) (kn2 : kernel_name) =
418 if kn1 == kn2 then 0
419 else
420 let c = String.compare kn1.knlabel kn2.knlabel in
421 if not (Int.equal c 0) then c
422 else
423 ModPath.compare kn1.modpath kn2.modpath
424
425 let equal kn1 kn2 =
426 let h1 = kn1.refhash in
427 let h2 = kn2.refhash in
428 if 0 <= h1 && 0 <= h2 && not (Int.equal h1 h2) then false
429 else
430 Id.equal kn1.knlabel kn2.knlabel &&
431 ModPath.equal kn1.modpath kn2.modpath
432
433 let hash kn = kn.refhash
434
435 module Self_Hashcons = struct
436 type t = kernel_name
437 let hashcons kn =
438 let { modpath = mp; knlabel = l; refhash; } = kn in
439 let _, mp = ModPath.hcons mp in
440 let _, l = Id.hcons l in
441 refhash, { modpath = mp; knlabel = l; refhash; }
442 let eq kn1 kn2 =
443 kn1.modpath == kn2.modpath && kn1.knlabel == kn2.knlabel
444 end
445
446 module HashKN = Hashcons.Make(Self_Hashcons)
447
448 let hcons = Hashcons.simple_hcons HashKN.generate HashKN.hcons ()
449
450 module Self = struct
451 type nonrec t = t
452 let compare = compare
453 let hash = hash
454 end
455
456 module Map = HMap.Make(Self)
457 module Set = Map.Set
458 module Pred = Predicate.Make(Self)
459
460 end
461
462 module KNmap = KerName.Map
463 module KNpred = KerName.Pred
464 module KNset = KerName.Set
465
466 (** {6 Kernel pairs } *)
467
468 module type EqType =
469 sig
470 type t
471 val compare : t -> t -> int
472 val equal : t -> t -> bool
473 val hash : t -> int
474 end
475
476 module type QNameS =
477 sig
478 type t
479 module CanOrd : EqType with type t = t
480 module UserOrd : EqType with type t = t
481 val canonize : t -> t
482 end
483
484 (** For constant and inductive names, we use a kernel name couple (kn1,kn2)
485 where kn1 corresponds to the name used at toplevel (i.e. what the user see)
486 and kn2 corresponds to the canonical kernel name i.e. in the environment
487 we have {% kn1 \rhd_{\delta}^* kn2 \rhd_{\delta} t %}
488
489 Invariants :
490 - the user and canonical kn may differ only on their [module_path],
491 the dirpaths and labels should be the same
492 - when user and canonical parts differ, we cannot be in a section
493 anymore, hence the dirpath must be empty
494 - two pairs with the same user part should have the same canonical part
495 in a given environment (though with backtracking, the hash-table can
496 contains pairs with same user part but different canonical part from
497 a previous state of the session)
498
499 Note: since most of the time the canonical and user parts are equal,
500 we handle this case with a particular constructor to spare some memory *)
501
502 module KerPair = struct
503
504 type t =
505 | Same of KerName.t (** user = canonical *)
506 | Dual of KerName.t * KerName.t (** user then canonical *)
507
508 type kernel_pair = t
509
510 let canonical = function
511 | Same kn -> kn
512 | Dual (_,kn) -> kn
513
514 let user = function
515 | Same kn -> kn
516 | Dual (kn,_) -> kn
517
518 let canonize kp = match kp with
519 | Same _ -> kp
520 | Dual (_, kn) -> Same kn
521
522 let same kn = Same kn
523 let make knu knc = if KerName.equal knu knc then Same knc else Dual (knu,knc)
524
525 let make1 = same
526 let make2 mp l = same (KerName.make mp l)
527 let label kp = KerName.label (user kp)
528 let modpath kp = KerName.modpath (user kp)
529
530 let change_label kp lbl =
531 let (mp1,l1) = KerName.repr (user kp)
532 and (mp2,l2) = KerName.repr (canonical kp) in
533 assert (String.equal l1 l2);
534 if String.equal lbl l1 then kp
535 else
536 let kn = KerName.make mp1 lbl in
537 if mp1 == mp2 then same kn
538 else make kn (KerName.make mp2 lbl)
539
540 let to_string kp = KerName.to_string (user kp)
541 let print kp = str (to_string kp)
542
543 let debug_to_string = function
544 | Same kn -> "(" ^ KerName.debug_to_string kn ^ ")"
545 | Dual (knu,knc) ->
546 "(" ^ KerName.debug_to_string knu ^ "," ^ KerName.debug_to_string knc ^ ")"
547
548 let debug_print kp = str (debug_to_string kp)
549
550 (** For ordering kernel pairs, both user or canonical parts may make
551 sense, according to your needs: user for the environments, canonical
552 for other uses (ex: non-logical things). *)
553
554 module UserOrd = struct
555 type t = kernel_pair
556 let compare x y = KerName.compare (user x) (user y)
557 let equal x y = x == y || KerName.equal (user x) (user y)
558 let hash x = KerName.hash (user x)
559 end
560
561 module CanOrd = struct
562 type t = kernel_pair
563 let compare x y = KerName.compare (canonical x) (canonical y)
564 let equal x y = x == y || KerName.equal (canonical x) (canonical y)
565 let hash x = KerName.hash (canonical x)
566 end
567
568 module Self_Hashcons =
569 struct
570 type t = kernel_pair
571 let hashcons = function
572 | Same kn ->
573 let hkn, kn = KerName.hcons kn in
574 hkn, Same kn
575 | Dual (knu,knc) ->
576 let hknu, knu = KerName.hcons knu in
577 let hknc, knc = KerName.hcons knc in
578 Hashset.Combine.combine hknu hknc, make knu knc
579 let eq x y = (* physical comparison on subterms *)
580 x == y ||
581 match x,y with
582 | Same x, Same y -> x == y
583 | Dual (ux,cx), Dual (uy,cy) -> ux == uy && cx == cy
584 | (Same _ | Dual _), _ -> false
585 (** Hash-consing (despite having the same user part implies having
586 the same canonical part is a logical invariant of the system, it
587 is not necessarily an invariant in memory, so we treat kernel
588 names as they are syntactically for hash-consing) *)
589 end
590
591 module HashKP = Hashcons.Make(Self_Hashcons)
592
593 let hcons = Hashcons.simple_hcons HashKP.generate HashKP.hcons ()
594
595 end
596
597 (** {6 Constant Names} *)
598
599 module Constant = KerPair
600
601 module Cmap = HMap.Make(Constant.CanOrd)
602 (** A map whose keys are constants (values of the {!Constant.t} type).
603 Keys are ordered wrt. "canonical form" of the constant. *)
604
605 module Cmap_env = HMap.Make(Constant.UserOrd)
606 (** A map whose keys are constants (values of the {!Constant.t} type).
607 Keys are ordered wrt. "user form" of the constant. *)
608
609 module Cpred = Predicate.Make(Constant.CanOrd)
610 module Cset = Cmap.Set
611 module Cset_env = Cmap_env.Set
612
613 (** {6 Names of mutual inductive types } *)
614
615 module MutInd = KerPair
616
617 module Mindmap = HMap.Make(MutInd.CanOrd)
618 module Mindset = Mindmap.Set
619 module Mindmap_env = HMap.Make(MutInd.UserOrd)
620
621 module Ind =
622 struct
623 (** Designation of a (particular) inductive type. *)
624 type t = MutInd.t (* the name of the inductive type *)
625 * int (* the position of this inductive type
626 within the block of mutually-recursive inductive types.
627 BEWARE: indexing starts from 0. *)
628 let modpath (mind, _) = MutInd.modpath mind
629
630 module CanOrd =
631 struct
632 type nonrec t = t
633 let equal (m1, i1) (m2, i2) = Int.equal i1 i2 && MutInd.CanOrd.equal m1 m2
634 let compare (m1, i1) (m2, i2) =
635 let c = Int.compare i1 i2 in
636 if Int.equal c 0 then MutInd.CanOrd.compare m1 m2 else c
637 let hash (m, i) =
638 Hashset.Combine.combine (MutInd.CanOrd.hash m) (Int.hash i)
639 end
640
641 module UserOrd =
642 struct
643 type nonrec t = t
644 let equal (m1, i1) (m2, i2) =
645 Int.equal i1 i2 && MutInd.UserOrd.equal m1 m2
646 let compare (m1, i1) (m2, i2) =
647 let c = Int.compare i1 i2 in
648 if Int.equal c 0 then MutInd.UserOrd.compare m1 m2 else c
649 let hash (m, i) =
650 Hashset.Combine.combine (MutInd.UserOrd.hash m) (Int.hash i)
651 end
652
653 let canonize ((mind, i) as ind) =
654 let mind' = MutInd.canonize mind in
655 if mind' == mind then ind else (mind', i)
656
657 end
658
659 module Construct =
660 struct
661 (** Designation of a (particular) constructor of a (particular) inductive type. *)
662 type t = Ind.t (* designates the inductive type *)
663 * int (* the index of the constructor
664 BEWARE: indexing starts from 1. *)
665
666 let modpath (ind, _) = Ind.modpath ind
667
668 module CanOrd =
669 struct
670 type nonrec t = t
671 let equal (ind1, j1) (ind2, j2) = Int.equal j1 j2 && Ind.CanOrd.equal ind1 ind2
672 let compare (ind1, j1) (ind2, j2) =
673 let c = Int.compare j1 j2 in
674 if Int.equal c 0 then Ind.CanOrd.compare ind1 ind2 else c
675 let hash (ind, i) =
676 Hashset.Combine.combine (Ind.CanOrd.hash ind) (Int.hash i)
677 end
678
679 module UserOrd =
680 struct
681 type nonrec t = t
682 let equal (ind1, j1) (ind2, j2) =
683 Int.equal j1 j2 && Ind.UserOrd.equal ind1 ind2
684 let compare (ind1, j1) (ind2, j2) =
685 let c = Int.compare j1 j2 in
686 if Int.equal c 0 then Ind.UserOrd.compare ind1 ind2 else c
687 let hash (ind, i) =
688 Hashset.Combine.combine (Ind.UserOrd.hash ind) (Int.hash i)
689 end
690
691 let canonize ((ind, i) as cstr) =
692 let ind' = Ind.canonize ind in
693 if ind' == ind then cstr else (ind', i)
694
695 end
696
697 (** Designation of a (particular) inductive type. *)
698 type inductive = Ind.t
699
700 (** Designation of a (particular) constructor of a (particular) inductive type. *)
701 type constructor = Construct.t
702
703 let ith_mutual_inductive (mind, _) i = (mind, i)
704 let ith_constructor_of_inductive ind i = (ind, i)
705 let inductive_of_constructor (ind, _i) = ind
706 let index_of_constructor (_ind, i) = i
707
708 module Indset = Set.Make(Ind.CanOrd)
709 module Indset_env = Set.Make(Ind.UserOrd)
710 module Indmap = Map.Make(Ind.CanOrd)
711 module Indmap_env = Map.Make(Ind.UserOrd)
712
713 module Constrset = Set.Make(Construct.CanOrd)
714 module Constrset_env = Set.Make(Construct.UserOrd)
715 module Constrmap = Map.Make(Construct.CanOrd)
716 module Constrmap_env = Map.Make(Construct.UserOrd)
717
718 (** {6 Hash-consing of name objects } *)
719
720 module Hind = Hashcons.Make(
721 struct
722 type t = inductive
723 let hashcons (mind, i) =
724 let hmind, mind = MutInd.hcons mind in
725 Hashset.Combine.combine hmind (Int.hash i), (mind, i)
726 let eq (mind1,i1) (mind2,i2) = mind1 == mind2 && Int.equal i1 i2
727 end)
728
729 let hcons_con = Constant.hcons
730 let hcons_mind = MutInd.hcons
731 let hcons_ind = Hashcons.simple_hcons Hind.generate Hind.hcons ()
732
733 module Hconstruct = Hashcons.Make(
734 struct
735 type t = constructor
736 let hashcons (ind, j) =
737 let hind, ind = hcons_ind ind in
738 Hashset.Combine.combine hind (Int.hash j), (ind, j)
739 let eq (ind1, j1) (ind2, j2) = ind1 == ind2 && Int.equal j1 j2
740 end)
741
742 let hcons_construct = Hashcons.simple_hcons Hconstruct.generate Hconstruct.hcons ()
743
744 (*****************)
745
746 type 'a tableKey =
747 | ConstKey of 'a
748 | VarKey of Id.t
749 | RelKey of Int.t
750
751 type inv_rel_key = int (* index in the [rel_context] part of environment
752 starting by the end, {\em inverse}
753 of de Bruijn indice *)
754
755 let eq_table_key f ik1 ik2 =
756 if ik1 == ik2 then true
757 else match ik1,ik2 with
758 | ConstKey c1, ConstKey c2 -> f c1 c2
759 | VarKey id1, VarKey id2 -> Id.equal id1 id2
760 | RelKey k1, RelKey k2 -> Int.equal k1 k2
761 | _ -> false
762
763 let hash_table_key f ik =
764 let open Hashset.Combine in
765 match ik with
766 | ConstKey c -> combinesmall 1 (f c)
767 | VarKey id -> combinesmall 2 (Id.hash id)
768 | RelKey i -> combinesmall 3 (Int.hash i)
769
770 (*******************************************************************)
771 (** Compatibility layers *)
772
773 let eq_constant_key = Constant.UserOrd.equal
774
775 (** Compatibility layer for [ModPath] *)
776
777 type module_path = ModPath.t =
778 | MPfile of DirPath.t
779 | MPbound of MBId.t
780 | MPdot of module_path * Id.t
781
782 (** Compatibility layer for [Constant] *)
783
784 module Projection =
785 struct
786 module Repr = struct
787 type t =
788 { proj_ind : inductive;
789 proj_npars : int;
790 proj_arg : int;
791 proj_name : Constant.t;
792 (** The only relevant data is the label, the rest is canonically derived
793 from proj_ind. *)
794 }
795
796 let make proj_ind ~proj_npars ~proj_arg proj_name =
797 let proj_name = KerPair.change_label (fst proj_ind) proj_name in
798 {proj_ind;proj_npars;proj_arg;proj_name}
799
800 let inductive c = c.proj_ind
801
802 let mind c = fst c.proj_ind
803
804 let constant c = c.proj_name
805
806 let label c = Constant.label c.proj_name
807
808 let npars c = c.proj_npars
809
810 let arg c = c.proj_arg
811
812 let compare_gen a b =
813 let c = Int.compare a.proj_arg b.proj_arg in
814 if c <> 0 then c
815 else
816 let c = Int.compare a.proj_npars b.proj_npars in
817 if c <> 0 then c
818 else
819 Id.compare (Constant.label a.proj_name) (Constant.label b.proj_name)
820
821 module CanOrd = struct
822 type nonrec t = t
823
824 let compare a b =
825 let c = Ind.CanOrd.compare a.proj_ind b.proj_ind in
826 if c <> 0 then c
827 else compare_gen a b
828
829 let equal a b = compare a b == 0
830
831 let hash p =
832 Hashset.Combine.combinesmall p.proj_arg (Ind.CanOrd.hash p.proj_ind)
833 end
834 module UserOrd = struct
835 type nonrec t = t
836
837 let compare a b =
838 let c = Ind.UserOrd.compare a.proj_ind b.proj_ind in
839 if c <> 0 then c
840 else compare_gen a b
841
842 let equal a b = compare a b == 0
843
844 let hash p =
845 Hashset.Combine.combinesmall p.proj_arg (Ind.UserOrd.hash p.proj_ind)
846 end
847
848 let canonize p =
849 let { proj_ind; proj_npars; proj_arg; proj_name } = p in
850 let proj_ind' = Ind.canonize proj_ind in
851 let proj_name' = Constant.canonize proj_name in
852 if proj_ind' == proj_ind && proj_name' == proj_name then p
853 else { proj_ind = proj_ind'; proj_name = proj_name'; proj_npars; proj_arg }
854
855 module Self_Hashcons = struct
856 type nonrec t = t
857 let hashcons p =
858 let hind, ind = hcons_ind p.proj_ind in
859 let _, na = Constant.hcons p.proj_name in
860 Hashset.Combine.combinesmall p.proj_arg hind, {
861 proj_ind = ind;
862 proj_npars = p.proj_npars;
863 proj_arg = p.proj_arg;
864 proj_name = na;
865 }
866 let eq p p' =
867 p == p' || (p.proj_ind == p'.proj_ind && p.proj_npars == p'.proj_npars && p.proj_arg == p'.proj_arg && p.proj_name == p'.proj_name)
868 end
869 module HashRepr = Hashcons.Make(Self_Hashcons)
870 let hcons = Hashcons.simple_hcons HashRepr.generate HashRepr.hcons ()
871
872 let map_npars f p =
873 let npars = p.proj_npars in
874 let npars' = f npars in
875 if npars == npars' then p
876 else {p with proj_npars = npars'}
877
878 let map f p =
879 let ind = fst p.proj_ind in
880 let ind' = f ind in
881 if ind == ind' then p
882 else
883 let proj_name = KerPair.change_label ind' (label p) in
884 {p with proj_ind = (ind',snd p.proj_ind); proj_name}
885
886 let to_string p = Constant.to_string (constant p)
887 let print p = Constant.print (constant p)
888 end
889
890 type t = Repr.t * bool
891
892 let make c b = (c, b)
893
894 let mind (c,_) = Repr.mind c
895 let inductive (c,_) = Repr.inductive c
896 let npars (c,_) = Repr.npars c
897 let arg (c,_) = Repr.arg c
898 let constant (c,_) = Repr.constant c
899 let label (c,_) = Repr.label c
900 let repr = fst
901 let unfolded = snd
902 let unfold (c, b as p) = if b then p else (c, true)
903
904 module CanOrd = struct
905 type nonrec t = t
906 let compare (p, b) (p', b') =
907 let c = Bool.compare b b' in
908 if c <> 0 then c else Repr.CanOrd.compare p p'
909 let equal (c, b as x) (c', b' as x') =
910 x == x' || b = b' && Repr.CanOrd.equal c c'
911 let hash (c, b) = (if b then 1 else 0) + Repr.CanOrd.hash c
912 end
913 module UserOrd = struct
914 type nonrec t = t
915 let compare (p, b) (p', b') =
916 let c = Bool.compare b b' in
917 if c <> 0 then c else Repr.UserOrd.compare p p'
918 let equal (c, b as x) (c', b' as x') =
919 x == x' || b = b' && Repr.UserOrd.equal c c'
920 let hash (c, b) = (if b then 1 else 0) + Repr.UserOrd.hash c
921 end
922
923 module Self_Hashcons =
924 struct
925 type nonrec t = t
926 let hashcons (c,b) =
927 let hc, c = Repr.hcons c in
928 (if b then 1 else 0) + hc, (c,b)
929 let eq ((c,b) as x) ((c',b') as y) =
930 x == y || (c == c' && b == b')
931 end
932
933 let canonize ((r, u) as p) =
934 let r' = Repr.canonize r in
935 if r' == r then p else (r', u)
936
937 module HashProjection = Hashcons.Make(Self_Hashcons)
938
939 let hcons = Hashcons.simple_hcons HashProjection.generate HashProjection.hcons ()
940
941 let map f (c, b as x) =
942 let c' = Repr.map f c in
943 if c' == c then x else (c', b)
944
945 let map_npars f (c, b as x) =
946 let c' = Repr.map_npars f c in
947 if c' == c then x else (c', b)
948
949 let to_string p = Constant.to_string (constant p)
950 let print p = Constant.print (constant p)
951
952 let debug_to_string p =
953 (if unfolded p then "unfolded(" else "") ^
954 Constant.debug_to_string (constant p) ^
955 (if unfolded p then ")" else "")
956 let debug_print p = str (debug_to_string p)
957
958 end
959
960 module PRmap = HMap.Make(Projection.Repr.CanOrd)
961 module PRset = PRmap.Set
962 module PRpred = Predicate.Make(Projection.Repr.CanOrd)
963
964 module GlobRefInternal = struct
965
966 type t =
967 | VarRef of variable (** A reference to the section-context. *)
968 | ConstRef of Constant.t (** A reference to the environment. *)
969 | IndRef of inductive (** A reference to an inductive type. *)
970 | ConstructRef of constructor (** A reference to a constructor of an inductive type. *)
971
972 let global_eq_gen eq_cst eq_ind eq_cons x y =
973 x == y ||
974 match x, y with
975 | ConstRef cx, ConstRef cy -> eq_cst cx cy
976 | IndRef indx, IndRef indy -> eq_ind indx indy
977 | ConstructRef consx, ConstructRef consy -> eq_cons consx consy
978 | VarRef v1, VarRef v2 -> Id.equal v1 v2
979 | (VarRef _ | ConstRef _ | IndRef _ | ConstructRef _), _ -> false
980
981 let global_ord_gen ord_cst ord_ind ord_cons x y =
982 if x == y then 0
983 else match x, y with
984 | VarRef v1, VarRef v2 -> Id.compare v1 v2
985 | VarRef _, _ -> -1
986 | _, VarRef _ -> 1
987 | ConstRef cx, ConstRef cy -> ord_cst cx cy
988 | ConstRef _, _ -> -1
989 | _, ConstRef _ -> 1
990 | IndRef indx, IndRef indy -> ord_ind indx indy
991 | IndRef _, _ -> -1
992 | _ , IndRef _ -> 1
993 | ConstructRef consx, ConstructRef consy -> ord_cons consx consy
994
995 let global_hash_gen hash_cst hash_ind hash_cons gr =
996 let open Hashset.Combine in
997 match gr with
998 | ConstRef c -> combinesmall 1 (hash_cst c)
999 | IndRef i -> combinesmall 2 (hash_ind i)
1000 | ConstructRef c -> combinesmall 3 (hash_cons c)
1001 | VarRef id -> combinesmall 4 (Id.hash id)
1002
1003 end
1004
1005 module GlobRef = struct
1006
1007 type t = GlobRefInternal.t =
1008 | VarRef of variable (** A reference to the section-context. *)
1009 | ConstRef of Constant.t (** A reference to the environment. *)
1010 | IndRef of inductive (** A reference to an inductive type. *)
1011 | ConstructRef of constructor (** A reference to a constructor of an inductive type. *)
1012
1013 (* By default, [global_reference] are ordered on their canonical part *)
1014
1015 module CanOrd = struct
1016 type t = GlobRefInternal.t
1017 let compare gr1 gr2 =
1018 GlobRefInternal.global_ord_gen Constant.CanOrd.compare Ind.CanOrd.compare Construct.CanOrd.compare gr1 gr2
1019 let equal gr1 gr2 = GlobRefInternal.global_eq_gen Constant.CanOrd.equal Ind.CanOrd.equal Construct.CanOrd.equal gr1 gr2
1020 let hash gr = GlobRefInternal.global_hash_gen Constant.CanOrd.hash Ind.CanOrd.hash Construct.CanOrd.hash gr
1021 end
1022
1023 module UserOrd = struct
1024 type t = GlobRefInternal.t
1025 let compare gr1 gr2 =
1026 GlobRefInternal.global_ord_gen Constant.UserOrd.compare Ind.UserOrd.compare Construct.UserOrd.compare gr1 gr2
1027 let equal gr1 gr2 = GlobRefInternal.global_eq_gen Constant.UserOrd.equal Ind.UserOrd.equal Construct.UserOrd.equal gr1 gr2
1028 let hash gr = GlobRefInternal.global_hash_gen Constant.UserOrd.hash Ind.UserOrd.hash Construct.UserOrd.hash gr
1029 end
1030
1031 let canonize gr = match gr with
1032 | VarRef _ -> gr
1033 | ConstRef c ->
1034 let c' = Constant.canonize c in
1035 if c' == c then gr else ConstRef c'
1036 | IndRef ind ->
1037 let ind' = Ind.canonize ind in
1038 if ind' == ind then gr else IndRef ind'
1039 | ConstructRef c ->
1040 let c' = Construct.canonize c in
1041 if c' == c then gr else ConstructRef c'
1042
1043 let is_bound = function
1044 | VarRef _ -> false
1045 | ConstRef cst -> ModPath.is_bound (Constant.modpath cst)
1046 | IndRef (ind,_) | ConstructRef ((ind,_),_) -> ModPath.is_bound (MutInd.modpath ind)
1047
1048 module Map = HMap.Make(CanOrd)
1049 module Set = Map.Set
1050
1051 (* Alternative sets and maps indexed by the user part of the kernel names *)
1052
1053 module Map_env = HMap.Make(UserOrd)
1054 module Set_env = Map_env.Set
1055
1056 let print = function
1057 | VarRef x -> Id.print x
1058 | ConstRef x -> Constant.print x
1059 | IndRef (m,i) -> surround (MutInd.print m ++ strbrk ", " ++ int i )
1060 | ConstructRef ((m,i),j) -> surround (MutInd.print m ++ strbrk ", " ++ int i ++ strbrk ", " ++ int j)
1061
1062 end
1063
1064 (** Located identifiers and objects with syntax. *)
1065
1066 type lident = Id.t CAst.t
1067 type lname = Name.t CAst.t
1068 type lstring = string CAst.t
1069
1070 let lident_eq = CAst.eq Id.equal
1071
1072 (** Deprecated *)
1073 module Label =
1074 struct
1075 include Id
1076 let make = Id.of_string
1077 let of_id id = id
1078 let to_id id = id
1079 end