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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
open CErrors
open Util
open Pp
open Names
open Libnames
open Globnames
type object_prefix = {
obj_dir : DirPath.t;
obj_mp : ModPath.t;
}
let eq_op op1 op2 =
DirPath.equal op1.obj_dir op2.obj_dir &&
ModPath.equal op1.obj_mp op2.obj_mp
module GlobDirRef = struct
type t =
| DirOpenModule of object_prefix
| DirOpenModtype of object_prefix
| DirOpenSection of object_prefix
let equal r1 r2 = match r1, r2 with
| DirOpenModule op1, DirOpenModule op2 -> eq_op op1 op2
| DirOpenModtype op1, DirOpenModtype op2 -> eq_op op1 op2
| DirOpenSection op1, DirOpenSection op2 -> eq_op op1 op2
| (DirOpenModule _ | DirOpenModtype _ | DirOpenSection _), _ -> false
end
exception GlobalizationError of qualid
let error_global_not_found ~info qid =
let info = Option.cata (Loc.add_loc info) info qid.CAst.loc in
Exninfo.iraise (GlobalizationError qid, info)
type visibility = Until of int | Exactly of int
let map_visibility f = function
| Until i -> Until (f i)
| Exactly i -> Exactly (f i)
module type UserName = sig
type t
val equal : t -> t -> bool
val to_string : t -> string
val repr : t -> Id.t * module_ident list
end
module type EqualityType =
sig
type t
val equal : t -> t -> bool
end
module type NAMETREE = sig
type elt
type t
type user_name
val empty : t
val push : visibility -> user_name -> elt -> t -> t
val locate : qualid -> t -> elt
val find : user_name -> t -> elt
val exists : user_name -> t -> bool
val user_name : qualid -> t -> user_name
val shortest_qualid_gen : ?loc:Loc.t -> (Id.t -> bool) -> user_name -> t -> qualid
val shortest_qualid : ?loc:Loc.t -> Id.Set.t -> user_name -> t -> qualid
val find_prefixes : qualid -> t -> elt list
(** Matches a prefix of [qualid], useful for completion *)
val match_prefixes : qualid -> t -> elt list
end
module Make (U : UserName) (E : EqualityType) : NAMETREE
with type user_name = U.t and type elt = E.t =
struct
type elt = E.t
let warn_masking_absolute =
CWarnings.create ~name:"masking-absolute-name" ~category:"deprecated"
(fun n -> str ("Trying to mask the absolute name \"" ^ U.to_string n ^ "\"!"))
type user_name = U.t
type path_status =
Nothing
| Relative of user_name * elt
| Absolute of user_name * elt
type nametree =
{ path : path_status;
map : nametree ModIdmap.t }
let mktree p m = { path=p; map=m }
let empty_tree = mktree Nothing ModIdmap.empty
type t = nametree Id.Map.t
let empty = Id.Map.empty
let rec push_until uname o level tree = function
| modid :: path ->
let modify _ mc = push_until uname o (level-1) mc path in
let map =
try ModIdmap.modify modid modify tree.map
with Not_found ->
let ptab = modify () empty_tree in
ModIdmap.add modid ptab tree.map
in
let this =
if level <= 0 then
match tree.path with
| Absolute (n,_) ->
warn_masking_absolute n; tree.path
| Nothing
| Relative _ -> Relative (uname,o)
else tree.path
in
mktree this map
| [] ->
match tree.path with
| Absolute (uname',o') ->
if E.equal o' o then begin
assert (U.equal uname uname');
tree
end
else
user_err Pp.(str @@ "Cannot mask the absolute name \""
^ U.to_string uname' ^ "\"!")
| Nothing
| Relative _ -> mktree (Absolute (uname,o)) tree.map
let rec push_exactly uname o level tree = function
| [] ->
anomaly (Pp.str "Prefix longer than path! Impossible!")
| modid :: path ->
if Int.equal level 0 then
let this =
match tree.path with
| Absolute (n,_) ->
warn_masking_absolute n; tree.path
| Nothing
| Relative _ -> Relative (uname,o)
in
mktree this tree.map
else
let modify _ mc = push_exactly uname o (level-1) mc path in
let map =
try ModIdmap.modify modid modify tree.map
with Not_found ->
let ptab = modify () empty_tree in
ModIdmap.add modid ptab tree.map
in
mktree tree.path map
let push visibility uname o tab =
let id,dir = U.repr uname in
let modify _ ptab = match visibility with
| Until i -> push_until uname o (i-1) ptab dir
| Exactly i -> push_exactly uname o (i-1) ptab dir
in
try Id.Map.modify id modify tab
with Not_found ->
let ptab = modify () empty_tree in
Id.Map.add id ptab tab
let rec search tree = function
| modid :: path -> search (ModIdmap.find modid tree.map) path
| [] -> tree.path
let find_node qid tab =
let (dir,id) = repr_qualid qid in
search (Id.Map.find id tab) (DirPath.repr dir)
let locate qid tab =
let o = match find_node qid tab with
| Absolute (uname,o) | Relative (uname,o) -> o
| Nothing -> raise Not_found
in
o
let user_name qid tab =
let uname = match find_node qid tab with
| Absolute (uname,o) | Relative (uname,o) -> uname
| Nothing -> raise Not_found
in
uname
let find uname tab =
let id,l = U.repr uname in
match search (Id.Map.find id tab) l with
Absolute (_,o) -> o
| _ -> raise Not_found
let exists uname tab =
try
let _ = find uname tab in
true
with
Not_found -> false
let shortest_qualid_gen ?loc hidden uname tab =
let id,dir = U.repr uname in
let hidden = hidden id in
let rec find_uname pos dir tree =
let is_empty = match pos with [] -> true | _ -> false in
match tree.path with
| Absolute (u,_) | Relative (u,_)
when U.equal u uname && not (is_empty && hidden) -> List.rev pos
| _ ->
match dir with
[] -> raise Not_found
| id::dir -> find_uname (id::pos) dir (ModIdmap.find id tree.map)
in
let ptab = Id.Map.find id tab in
let found_dir = find_uname [] dir ptab in
make_qualid ?loc (DirPath.make found_dir) id
let shortest_qualid ?loc ctx uname tab =
shortest_qualid_gen ?loc (fun id -> Id.Set.mem id ctx) uname tab
let push_node node l =
match node with
| Absolute (_,o) | Relative (_,o) when not (List.mem_f E.equal o l) -> o::l
| _ -> l
let rec flatten_idmap tab l =
let f _ tree l = flatten_idmap tree.map (push_node tree.path l) in
ModIdmap.fold f tab l
let rec search_prefixes tree = function
| modid :: path -> search_prefixes (ModIdmap.find modid tree.map) path
| [] -> List.rev (flatten_idmap tree.map (push_node tree.path []))
let find_prefixes qid tab =
try
let (dir,id) = repr_qualid qid in
search_prefixes (Id.Map.find id tab) (DirPath.repr dir)
with Not_found -> []
let match_prefixes =
let cprefix x y = CString.(compare x (sub y 0 (min (length x) (length y)))) in
fun qid tab ->
try
let (dir,id) = repr_qualid qid in
let id_prefix = cprefix Id.(to_string id) in
let matches = Id.Map.filter_range (fun x -> id_prefix Id.(to_string x)) tab in
let matches = Id.Map.mapi (fun _key tab -> search_prefixes tab (DirPath.repr dir)) matches in
CList.flatten @@ Id.Map.(fold (fun _ r l -> r :: l) matches [])
with Not_found -> []
end
module FullPath =
struct
type t = full_path
let equal = eq_full_path
let to_string = string_of_path
let repr sp =
let dir,id = repr_path sp in
id, (DirPath.repr dir)
end
module ExtRefEqual = ExtRefOrdered
module MPEqual = Names.ModPath
module ExtRefTab = Make(FullPath)(ExtRefEqual)
module MPTab = Make(FullPath)(MPEqual)
type ccitab = ExtRefTab.t
let the_ccitab = Summary.ref ~name:"ccitab" (ExtRefTab.empty : ccitab)
module DirPath' =
struct
include DirPath
let repr dir = match DirPath.repr dir with
| [] -> anomaly (Pp.str "Empty dirpath.")
| id :: l -> (id, l)
end
module MPDTab = Make(DirPath')(MPEqual)
module DirTab = Make(DirPath')(GlobDirRef)
module UnivIdEqual =
struct
type t = Univ.UGlobal.t
let equal = Univ.UGlobal.equal
end
module UnivTab = Make(FullPath)(UnivIdEqual)
type univtab = UnivTab.t
let the_univtab = Summary.ref ~name:"univtab" (UnivTab.empty : univtab)
type globrevtab = full_path ExtRefMap.t
let the_globrevtab = Summary.ref ~name:"globrevtab" (ExtRefMap.empty : globrevtab)
type mprevtab = DirPath.t MPmap.t
type mptrevtab = full_path MPmap.t
module UnivIdOrdered =
struct
type t = Univ.UGlobal.t
let hash = Univ.UGlobal.hash
let compare = Univ.UGlobal.compare
end
module UnivIdMap = HMap.Make(UnivIdOrdered)
type univrevtab = full_path UnivIdMap.t
let the_univrevtab = Summary.ref ~name:"univrevtab" (UnivIdMap.empty : univrevtab)
(** Module-related nametab *)
module Modules = struct
type t = {
modtypetab : MPTab.t;
modtab : MPDTab.t;
dirtab : DirTab.t;
modrevtab : mprevtab;
modtyperevtab : mptrevtab;
}
let initial = {
modtypetab = MPTab.empty;
modtab = MPDTab.empty;
dirtab = DirTab.empty;
modrevtab = MPmap.empty;
modtyperevtab = MPmap.empty
}
let nametab, summary_tag =
Summary.ref_tag ~name:"MODULES-NAMETAB" initial
let freeze () = !nametab
let unfreeze v = nametab := v
end
let push_xref visibility sp xref =
match visibility with
| Until _ ->
the_ccitab := ExtRefTab.push visibility sp xref !the_ccitab;
the_globrevtab := ExtRefMap.add xref sp !the_globrevtab
| _ ->
begin
if ExtRefTab.exists sp !the_ccitab then
let open GlobRef in
match ExtRefTab.find sp !the_ccitab with
| TrueGlobal( ConstRef _) | TrueGlobal( IndRef _) |
TrueGlobal( ConstructRef _) as xref ->
the_ccitab := ExtRefTab.push visibility sp xref !the_ccitab;
| _ ->
the_ccitab := ExtRefTab.push visibility sp xref !the_ccitab;
else
the_ccitab := ExtRefTab.push visibility sp xref !the_ccitab;
end
let push_cci visibility sp ref =
push_xref visibility sp (TrueGlobal ref)
let push_abbreviation visibility sp kn =
push_xref visibility sp (Abbrev kn)
let push = push_cci
let push_modtype vis sp kn =
let open Modules in
nametab := { !nametab with
modtypetab = MPTab.push vis sp kn !nametab.modtypetab;
modtyperevtab = MPmap.add kn sp !nametab.modtyperevtab;
}
let push_module vis dir mp =
let open Modules in
nametab := { !nametab with
modtab = MPDTab.push vis dir mp !nametab.modtab;
modrevtab = MPmap.add mp dir !nametab.modrevtab;
}
let push_dir vis dir dir_ref =
let open Modules in
nametab := { !nametab with
dirtab = DirTab.push vis dir dir_ref !Modules.nametab.dirtab;
}
let push_universe vis sp univ =
the_univtab := UnivTab.push vis sp univ !the_univtab;
the_univrevtab := UnivIdMap.add univ sp !the_univrevtab
let locate_extended qid = ExtRefTab.locate qid !the_ccitab
let locate qid = match locate_extended qid with
| TrueGlobal ref -> ref
| Abbrev _ -> raise Not_found
let full_name_cci qid = ExtRefTab.user_name qid !the_ccitab
let locate_abbreviation qid = match locate_extended qid with
| TrueGlobal _ -> raise Not_found
| Abbrev kn -> kn
let locate_modtype qid = MPTab.locate qid Modules.(!nametab.modtypetab)
let full_name_modtype qid = MPTab.user_name qid Modules.(!nametab.modtypetab)
let locate_universe qid = UnivTab.locate qid !the_univtab
let locate_dir qid = DirTab.locate qid Modules.(!nametab.dirtab)
let locate_module qid = MPDTab.locate qid Modules.(!nametab.modtab)
let full_name_module qid = MPDTab.user_name qid Modules.(!nametab.modtab)
let locate_section qid =
match locate_dir qid with
| GlobDirRef.DirOpenSection { obj_dir; _ } -> obj_dir
| _ -> raise Not_found
let locate_all qid =
List.fold_right (fun a l -> match a with TrueGlobal a -> a::l | _ -> l)
(ExtRefTab.find_prefixes qid !the_ccitab) []
let locate_extended_all qid = ExtRefTab.find_prefixes qid !the_ccitab
let locate_extended_all_dir qid = DirTab.find_prefixes qid Modules.(!nametab.dirtab)
let locate_extended_all_modtype qid = MPTab.find_prefixes qid Modules.(!nametab.modtypetab)
let locate_extended_all_module qid = MPDTab.find_prefixes qid Modules.(!nametab.modtab)
let completion_canditates qualid =
ExtRefTab.match_prefixes qualid !the_ccitab
let locate_constant qid =
let open GlobRef in
match locate_extended qid with
| TrueGlobal (ConstRef kn) -> kn
| _ -> raise Not_found
let global_of_path sp =
match ExtRefTab.find sp !the_ccitab with
| TrueGlobal ref -> ref
| _ -> raise Not_found
let extended_global_of_path sp = ExtRefTab.find sp !the_ccitab
let global qid =
try match locate_extended qid with
| TrueGlobal ref -> ref
| Abbrev _ ->
user_err ?loc:qid.CAst.loc
(str "Unexpected reference to a notation: " ++
pr_qualid qid ++ str ".")
with Not_found as exn ->
let _, info = Exninfo.capture exn in
error_global_not_found ~info qid
let exists_cci sp = ExtRefTab.exists sp !the_ccitab
let exists_dir dir = DirTab.exists dir Modules.(!nametab.dirtab)
let exists_module dir = MPDTab.exists dir Modules.(!nametab.modtab)
let exists_modtype sp = MPTab.exists sp Modules.(!nametab.modtypetab)
let exists_universe kn = UnivTab.exists kn !the_univtab
let path_of_global ref =
let open GlobRef in
match ref with
| VarRef id -> make_path DirPath.empty id
| _ -> ExtRefMap.find (TrueGlobal ref) !the_globrevtab
let dirpath_of_global ref =
fst (repr_path (path_of_global ref))
let basename_of_global ref =
snd (repr_path (path_of_global ref))
let path_of_abbreviation kn =
ExtRefMap.find (Abbrev kn) !the_globrevtab
let dirpath_of_module mp =
MPmap.find mp Modules.(!nametab.modrevtab)
let path_of_modtype mp =
MPmap.find mp Modules.(!nametab.modtyperevtab)
let path_of_universe mp =
UnivIdMap.find mp !the_univrevtab
let shortest_qualid_of_global ?loc ctx ref =
let open GlobRef in
match ref with
| VarRef id -> make_qualid ?loc DirPath.empty id
| _ ->
let sp = ExtRefMap.find (TrueGlobal ref) !the_globrevtab in
ExtRefTab.shortest_qualid ?loc ctx sp !the_ccitab
let shortest_qualid_of_abbreviation ?loc ctx kn =
let sp = path_of_abbreviation kn in
ExtRefTab.shortest_qualid ?loc ctx sp !the_ccitab
let shortest_qualid_of_module ?loc mp =
let dir = MPmap.find mp Modules.(!nametab.modrevtab) in
MPDTab.shortest_qualid ?loc Id.Set.empty dir Modules.(!nametab.modtab)
let shortest_qualid_of_modtype ?loc kn =
let sp = MPmap.find kn Modules.(!nametab.modtyperevtab) in
MPTab.shortest_qualid ?loc Id.Set.empty sp Modules.(!nametab.modtypetab)
let shortest_qualid_of_universe ?loc ctx kn =
let sp = UnivIdMap.find kn !the_univrevtab in
UnivTab.shortest_qualid_gen ?loc (fun id -> Id.Map.mem id ctx) sp !the_univtab
let pr_global_env env ref =
try pr_qualid (shortest_qualid_of_global env ref)
with Not_found as exn ->
let exn, info = Exninfo.capture exn in
if CDebug.(get_flag misc) then Feedback.msg_debug (Pp.str "pr_global_env not found");
Exninfo.iraise (exn, info)
let global_inductive qid =
let open GlobRef in
match global qid with
| IndRef ind -> ind
| ref ->
user_err ?loc:qid.CAst.loc
(pr_qualid qid ++ spc () ++ str "is not an inductive type.")