Skip to content

Commit df58ad5

Browse files
committed
[nametab] Introduce type of imperative nametabs, unify API
This is both a cleanup and a step towards pushing the state upwards, and handling the nametabs functionally. Related to rocq-prover#16746 and rocq-prover/rfcs#65
1 parent ff5b5d8 commit df58ad5

32 files changed

+268
-159
lines changed

engine/namegen.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,14 @@ let is_imported_ref = let open GlobRef in function
8181

8282
let is_global id =
8383
try
84-
let ref = Nametab.locate (qualid_of_ident id) in
84+
let ref = Nametab.GlobRef.locate (qualid_of_ident id) in
8585
not (is_imported_ref ref)
8686
with Not_found ->
8787
false
8888

8989
let is_constructor id =
9090
try
91-
match Nametab.locate (qualid_of_ident id) with
91+
match Nametab.GlobRef.locate (qualid_of_ident id) with
9292
| GlobRef.ConstructRef _ -> true
9393
| _ -> false
9494
with Not_found ->

interp/abbreviation.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ let add_abbreviation kn abbrev =
3333
abbrev_table := KNmap.add kn abbrev !abbrev_table
3434

3535
let load_abbreviation i ((sp,kn),(_local,abbrev)) =
36-
if Nametab.exists_cci sp then
36+
if Nametab.GlobRef.exists sp then
3737
user_err
3838
(Id.print (basename sp) ++ str " already exists.");
3939
add_abbreviation kn abbrev;
40-
Nametab.push_abbreviation (Nametab.Until i) sp kn
40+
Nametab.Abbrev.push (Nametab.Until i) sp kn
4141

4242
let is_alias_of_already_visible_name sp = function
4343
| _,NRef (ref,_) ->
@@ -49,7 +49,7 @@ let is_alias_of_already_visible_name sp = function
4949
let open_abbreviation i ((sp,kn),(_local,abbrev)) =
5050
let pat = abbrev.abbrev_pattern in
5151
if not (Int.equal i 1 && is_alias_of_already_visible_name sp pat) then begin
52-
Nametab.push_abbreviation (Nametab.Exactly i) sp kn;
52+
Nametab.Abbrev.push (Nametab.Exactly i) sp kn;
5353
if not abbrev.abbrev_onlyparsing then
5454
(* Redeclare it to be used as (short) name in case an other (distfix)
5555
notation was declared in between *)

interp/constrintern.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -797,7 +797,7 @@ let terms_of_binders bl =
797797
| PatVar (Name id) -> CRef (qualid_of_ident id, None)
798798
| PatVar (Anonymous) -> error_cannot_coerce_wildcard_term ?loc ()
799799
| PatCstr (c,l,_) ->
800-
let qid = qualid_of_path ?loc (Nametab.path_of_global (GlobRef.ConstructRef c)) in
800+
let qid = qualid_of_path ?loc (Nametab.GlobRef.path (GlobRef.ConstructRef c)) in
801801
let hole = CAst.make ?loc @@ CHole (None,IntroAnonymous,None) in
802802
let params = List.make (Inductiveops.inductive_nparams (Global.env()) (fst c)) hole in
803803
CAppExpl ((qid,None),params @ List.map term_of_pat l)) pt in
@@ -1160,7 +1160,7 @@ let intern_sort_name ~local_univs = function
11601160
match local with
11611161
| Some u -> GUniv u
11621162
| None ->
1163-
try GUniv (Univ.Level.make (Nametab.locate_universe qid))
1163+
try GUniv (Univ.Level.make (Nametab.Universe.locate qid))
11641164
with Not_found ->
11651165
if is_id && local_univs.unb_univs
11661166
then GLocalUniv (CAst.make ?loc:qid.loc (qualid_basename qid))
@@ -2805,7 +2805,7 @@ let interp_named_context_evars ?(program_mode=false) ?(impl_env=empty_internaliz
28052805
let known_universe_level_name evd lid =
28062806
try Evd.universe_of_name evd lid.CAst.v
28072807
with Not_found ->
2808-
let u = Nametab.locate_universe (Libnames.qualid_of_lident lid) in
2808+
let u = Nametab.Universe.locate (Libnames.qualid_of_lident lid) in
28092809
Univ.Level.make u
28102810

28112811
let known_glob_level evd = function

interp/dumpglob.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ let add_glob_gen ?loc sp lib_dp ty =
248248

249249
let add_glob ?loc ref =
250250
if dump () then
251-
let sp = Nametab.path_of_global ref in
251+
let sp = Nametab.GlobRef.path ref in
252252
let lib_dp = Lib.library_part ref in
253253
let ty = type_of_global_ref ref in
254254
add_glob_gen ?loc sp lib_dp ty
@@ -259,7 +259,7 @@ let mp_of_kn kn =
259259

260260
let add_glob_kn ?loc kn =
261261
if dump () then
262-
let sp = Nametab.path_of_abbreviation kn in
262+
let sp = Nametab.Abbrev.path kn in
263263
let lib_dp = Names.ModPath.dp (mp_of_kn kn) in
264264
add_glob_gen ?loc sp lib_dp "abbrev"
265265

interp/implicit_quantifiers.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ let implicit_application env ty =
188188
let ({CAst.v=(qid, _, _)} as clapp) = destClassAppExpl ty in
189189
if Libnames.idset_mem_qualid qid env then None
190190
else
191-
let gr = Nametab.locate qid in
191+
let gr = Nametab.GlobRef.locate qid in
192192
if Typeclasses.is_class gr then Some (clapp, gr) else None
193193
with Not_found -> None
194194
in

interp/modintern.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ let lookup_module_or_modtype kind qid =
4949
let loc = qid.CAst.loc in
5050
try
5151
if kind == ModType then raise Not_found;
52-
let mp = Nametab.locate_module qid in
52+
let mp = Nametab.Module.locate qid in
5353
Dumpglob.dump_modref ?loc mp "modtype"; (mp,Module)
5454
with Not_found ->
5555
try
5656
if kind == Module then raise Not_found;
57-
let mp = Nametab.locate_modtype qid in
57+
let mp = Nametab.Module.locate qid in
5858
Dumpglob.dump_modref ?loc mp "mod"; (mp,ModType)
5959
with Not_found as exn ->
6060
let _, info = Exninfo.capture exn in

interp/notation.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,7 +1189,7 @@ let q_list () = qualid_of_ref "core.list.type"
11891189
let q_byte () = qualid_of_ref "core.byte.type"
11901190

11911191
let unsafe_locate_ind q =
1192-
match Nametab.locate q with
1192+
match Nametab.GlobRef.locate q with
11931193
| GlobRef.IndRef i -> i
11941194
| _ -> raise Not_found
11951195

@@ -1686,7 +1686,7 @@ let is_printing_inactive_rule rule pat =
16861686
| NotationRule (scope,ntn) ->
16871687
not (is_printing_active_in_scope (scope,ntn) pat)
16881688
| AbbrevRule kn ->
1689-
try let _ = Nametab.path_of_abbreviation kn in false with Not_found -> true
1689+
try let _ = Nametab.Abbrev.path kn in false with Not_found -> true
16901690

16911691
let availability_of_notation (ntn_scope,ntn) scopes =
16921692
find_without_delimiters (has_active_parsing_rule_in_scope ntn) (ntn_scope,Some ntn) (make_current_scopes scopes)

library/coqlib.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,13 @@ let register_ref s c =
7474
(* Generic functions to find Coq objects *)
7575

7676
let has_suffix_in_dirs dirs ref =
77-
let dir = Libnames.dirpath (Nametab.path_of_global ref) in
77+
let dir = Libnames.dirpath (Nametab.GlobRef.path ref) in
7878
List.exists (fun d -> Libnames.is_dirpath_prefix_of d dir) dirs
7979

8080
let gen_reference_in_modules locstr dirs s =
8181
let dirs = List.map make_dir dirs in
8282
let qualid = Libnames.qualid_of_string s in
83-
let all = Nametab.locate_all qualid in
83+
let all = Nametab.GlobRef.locate_all qualid in
8484
let all = List.sort_uniquize GlobRef.UserOrd.compare all in
8585
let these = List.filter (has_suffix_in_dirs dirs) all in
8686
match these with
@@ -93,7 +93,7 @@ let gen_reference_in_modules locstr dirs s =
9393
CErrors.anomaly ~label:locstr
9494
Pp.(str "ambiguous name " ++ str s ++ str " can represent "
9595
++ prlist_with_sep pr_comma (fun x ->
96-
Libnames.pr_path (Nametab.path_of_global x)) l ++ str " in module"
96+
Libnames.pr_path (Nametab.GlobRef.path x)) l ++ str " in module"
9797
++ str (if List.length dirs > 1 then "s " else " ")
9898
++ prlist_with_sep pr_comma DirPath.print dirs ++ str ".")
9999

library/lib.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,8 @@ let start_mod is_type export id mp fs =
189189
in
190190
let prefix = Nametab.{ obj_dir = dir; obj_mp = mp; } in
191191
let exists =
192-
if is_type then Nametab.exists_cci (make_path id)
193-
else Nametab.exists_module dir
192+
if is_type then Nametab.GlobRef.exists (make_path id)
193+
else Nametab.Module.exists dir
194194
in
195195
if exists then
196196
CErrors.user_err Pp.(Id.print id ++ str " already exists.");
@@ -373,12 +373,12 @@ let open_section id =
373373
let opp = !lib_state.path_prefix in
374374
let obj_dir = Libnames.add_dirpath_suffix opp.Nametab.obj_dir id in
375375
let prefix = Nametab.{ obj_dir; obj_mp = opp.obj_mp; } in
376-
if Nametab.exists_dir obj_dir then
376+
if Nametab.Dir.exists obj_dir then
377377
CErrors.user_err Pp.(Id.print id ++ str " already exists.");
378378
let fs = Summary.freeze_summaries ~marshallable:false in
379379
add_entry (OpenedSection (prefix, fs));
380380
(*Pushed for the lifetime of the section: removed by unfrozing the summary*)
381-
Nametab.(push_dir (Until 1) obj_dir (GlobDirRef.DirOpenSection obj_dir));
381+
Nametab.(Dir.push (Until 1) obj_dir (GlobDirRef.DirOpenSection obj_dir));
382382
lib_state := { !lib_state with path_prefix = prefix }
383383

384384
(* Restore lib_stk and summaries as before the section opening, and

library/nametab.ml

Lines changed: 87 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ end
8282
the same object.
8383
*)
8484
module type NAMETREE = sig
85-
type elt
8685
type t
86+
type elt
8787
type user_name
8888

8989
val empty : t
@@ -317,6 +317,7 @@ module ExtRefEqual = Globnames.ExtRefOrdered
317317
module MPEqual = Names.ModPath
318318

319319
module ExtRefTab = Make(FullPath)(ExtRefEqual)
320+
320321
module MPTab = Make(FullPath)(MPEqual)
321322

322323
type ccitab = ExtRefTab.t
@@ -331,14 +332,17 @@ struct
331332
end
332333

333334
module MPDTab = Make(DirPath')(MPEqual)
335+
334336
module DirTab = Make(DirPath')(GlobDirRef)
335337

336338
module UnivIdEqual =
337339
struct
338340
type t = Univ.UGlobal.t
339341
let equal = Univ.UGlobal.equal
340342
end
343+
341344
module UnivTab = Make(FullPath)(UnivIdEqual)
345+
342346
type univtab = UnivTab.t
343347
let the_univtab = Summary.ref ~name:"univtab" (UnivTab.empty : univtab)
344348

@@ -425,7 +429,7 @@ let push_cci visibility sp ref =
425429
let push_abbreviation visibility sp kn =
426430
push_xref visibility sp (Abbrev kn)
427431

428-
let push = push_cci
432+
let _push = push_cci
429433

430434
let push_modtype vis sp kn =
431435
let open Modules in
@@ -495,11 +499,11 @@ let locate_all qid =
495499

496500
let locate_extended_all qid = ExtRefTab.find_prefixes qid !the_ccitab
497501

498-
let locate_extended_all_dir qid = DirTab.find_prefixes qid Modules.(!nametab.dirtab)
502+
let _locate_extended_all_dir qid = DirTab.find_prefixes qid Modules.(!nametab.dirtab)
499503

500-
let locate_extended_all_modtype qid = MPTab.find_prefixes qid Modules.(!nametab.modtypetab)
504+
let _locate_extended_all_modtype qid = MPTab.find_prefixes qid Modules.(!nametab.modtypetab)
501505

502-
let locate_extended_all_module qid = MPDTab.find_prefixes qid Modules.(!nametab.modtab)
506+
let _locate_extended_all_module qid = MPDTab.find_prefixes qid Modules.(!nametab.modtab)
503507

504508
(* Completion *)
505509
let completion_canditates qualid =
@@ -560,7 +564,7 @@ let basename_of_global ref =
560564
let path_of_abbreviation kn =
561565
Globnames.ExtRefMap.find (Abbrev kn) !the_globrevtab
562566

563-
let dirpath_of_module mp =
567+
let _dirpath_of_module mp =
564568
MPmap.find mp Modules.(!nametab.modrevtab)
565569

566570
let path_of_modtype mp =
@@ -609,3 +613,80 @@ let global_inductive qid =
609613
| ref ->
610614
CErrors.user_err ?loc:qid.CAst.loc
611615
Pp.(pr_qualid qid ++ spc () ++ str "is not an inductive type.")
616+
617+
(** Type of nametabs (imperative) *)
618+
module type S = sig
619+
620+
type elt
621+
type path
622+
623+
val push : visibility -> path -> elt -> unit
624+
val locate : qualid -> elt
625+
val locate_all : qualid -> elt list
626+
val exists : path -> bool
627+
val path : elt -> path
628+
629+
(* future work *)
630+
(* val shortest_qualid : *)
631+
end
632+
633+
module GlobRef_ = GlobRef
634+
module GlobRef : S
635+
with type elt := GlobRef.t and type path := full_path =
636+
struct
637+
let push = push_cci
638+
let locate = locate
639+
let locate_all = locate_all
640+
let exists = exists_cci
641+
let path = path_of_global
642+
end
643+
644+
module Abbrev : S
645+
with type elt := Globnames.abbreviation and type path := full_path =
646+
struct
647+
let push = push_abbreviation
648+
let locate = locate_abbreviation
649+
let locate_all x = locate_extended_all x |> List.filter_map (fun x -> match x with Globnames.Abbrev a -> Some a | _ -> None)
650+
let exists = exists_cci
651+
let path = path_of_abbreviation
652+
end
653+
654+
module ModType : S
655+
with type elt := ModPath.t and type path := full_path =
656+
struct
657+
let push = push_modtype
658+
let locate = locate_modtype
659+
let locate_all = Obj.magic
660+
let exists = exists_modtype
661+
let path = path_of_modtype
662+
end
663+
664+
module Module : S
665+
with type elt := ModPath.t and type path := DirPath.t =
666+
struct
667+
let push = push_module
668+
let locate = locate_module
669+
let locate_all = Obj.magic
670+
let exists = exists_module
671+
let path = Obj.magic
672+
end
673+
674+
module Dir : S
675+
with type elt := GlobDirRef.t and type path := DirPath.t =
676+
struct
677+
let push = push_dir
678+
let locate = locate_dir
679+
let locate_all = Obj.magic
680+
let exists = exists_dir
681+
let path = Obj.magic
682+
end
683+
684+
module Universe : S
685+
with type elt := Univ.UGlobal.t and type path := full_path =
686+
struct
687+
let push = push_universe
688+
let locate = locate_universe
689+
let locate_all = Obj.magic
690+
let exists = exists_universe
691+
let path = path_of_universe
692+
end

0 commit comments

Comments
 (0)