Debian Codegraph @

interp/abbreviation.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 Pp
12 open CErrors
13 open Names
14 open Libnames
15 open Libobject
16 open Lib
17 open Notationextern
18
19 type abbreviation = {
20 abbrev_local : Libobject.locality;
21 abbrev_pattern : Notation_term.interpretation;
22 abbrev_onlyparsing : bool;
23 abbrev_user_warns : Globnames.extended_global_reference UserWarn.with_qf option;
24 abbrev_activated : bool; (* Not really necessary in practice *)
25 abbrev_src : Loc.t option;
26 }
27
28 type data = full_path * abbreviation
29
30 let interp (_, a) = a.abbrev_pattern
31 let full_path (fp, _) = fp
32 let enabled (_, a) = a.abbrev_activated
33 let only_parsing (_, a) = a.abbrev_onlyparsing
34
35 let abbrev_table =
36 Summary.ref (KerName.Map.empty : data KerName.Map.t) ~name:"ABBREVIATIONS"
37
38 let add kn fpa =
39 abbrev_table := KerName.Map.add kn fpa !abbrev_table
40
41 let fold f acc =
42 KerName.Map.fold f !abbrev_table acc
43
44 let find_opt k =
45 KerName.Map.find_opt k !abbrev_table
46
47 let toggle_aux ~on ~use k (sp, data) =
48 if data.abbrev_activated != on then begin
49 abbrev_table := KerName.Map.add k (sp, {data with abbrev_activated = on}) !abbrev_table;
50 match use with
51 | OnlyPrinting -> ()
52 | (OnlyParsing | ParsingAndPrinting) when on ->
53 Nametab.push_abbreviation ?user_warns:data.abbrev_user_warns (Nametab.Until 1) sp k;
54 Nametab.push_abbreviation (Nametab.Exactly 1) sp k
55 | OnlyParsing | ParsingAndPrinting ->
56 Nametab.remove_abbreviation sp k
57 end
58
59 let toggle ~on ~use k =
60 toggle_aux ~on ~use k (KerName.Map.find k !abbrev_table)
61
62 let toggle_if ~on ~use filter =
63 fold (fun k ((_, a) as abbr) _ ->
64 if a.abbrev_activated != on && filter k abbr then
65 toggle_aux ~on ~use k abbr
66 ) ()
67
68 let is_alias_of_already_visible_name sp = function
69 | _,Notation_term.NRef (ref,None) ->
70 let (dir,id) = repr_qualid (Nametab.shortest_qualid_of_global ~force_short:true Id.Set.empty ref) in
71 DirPath.is_empty dir && Id.equal id (basename sp)
72 | _ ->
73 false
74
75 let load_abbreviation i ((sp,kn),abbrev) =
76 if Nametab.exists_cci sp then
77 user_err
78 (Id.print (basename sp) ++ str " already exists.");
79 add kn (sp, abbrev);
80 Nametab.push_abbreviation ?user_warns:abbrev.abbrev_user_warns (Nametab.Until i) sp kn;
81 abbrev.abbrev_src |> Option.iter (fun loc -> Nametab.set_cci_src_loc (Abbrev kn) loc);
82 let pat = abbrev.abbrev_pattern in
83 let () = if not abbrev.abbrev_onlyparsing &&
84 abbrev.abbrev_local <> Export &&
85 not (Int.equal i 1 && is_alias_of_already_visible_name sp pat) then
86 Notationextern.declare_uninterpretation (Global.env ()) (AbbrevRule kn) pat
87 in
88 ()
89
90 let open_abbreviation i ((sp,kn),abbrev) =
91 let pat = abbrev.abbrev_pattern in
92 if not (Int.equal i 1 && is_alias_of_already_visible_name sp pat) then begin
93 Nametab.push_abbreviation (Nametab.Exactly i) sp kn;
94 if not abbrev.abbrev_onlyparsing &&
95 Int.equal i 1 &&
96 abbrev.abbrev_local <> SuperGlobal then
97 Notationextern.declare_uninterpretation (Global.env ()) (AbbrevRule kn) pat
98 end
99
100 let import i sp kn =
101 let _,abbrev = KerName.Map.get kn !abbrev_table in
102 open_abbreviation i ((sp,kn),abbrev)
103
104 let cache_abbreviation d =
105 load_abbreviation 1 d;
106 open_abbreviation 1 d
107
108 let subst_abbreviation (subst,abbrev) =
109 let abbrev_pattern = Notation_ops.subst_interpretation subst abbrev.abbrev_pattern in
110 { abbrev with abbrev_pattern }
111
112 let classify_abbreviation abbrev =
113 if abbrev.abbrev_local = Local then Dispose else Substitute
114
115 let inAbbreviation : Id.t -> abbreviation -> obj =
116 declare_named_object {(default_object "ABBREVIATION") with
117 cache_function = cache_abbreviation;
118 load_function = load_abbreviation;
119 open_function = filtered_open open_abbreviation;
120 subst_function = subst_abbreviation;
121 classify_function = classify_abbreviation }
122
123 let declare ~local user_warns id ~onlyparsing pat =
124 let abbrev = {
125 abbrev_local = local;
126 abbrev_pattern = pat;
127 abbrev_onlyparsing = onlyparsing;
128 abbrev_user_warns = user_warns;
129 abbrev_activated = true;
130 abbrev_src = Loc.get_current_command_loc();
131 }
132 in
133 add_leaf (inAbbreviation id abbrev)
134
135 (* Remark: do not check for activation (if not activated, it is already not supposed to be located) *)
136 let find_interp kn =
137 let _,abbrev = KerName.Map.find kn !abbrev_table in
138 abbrev.abbrev_pattern