Debian Codegraph @

plugins/ltac/tacenv.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 Pp
13 open Names
14 open Tacexpr
15
16 (** Nametab for tactics *)
17
18 module TacticV = struct
19 include KerName
20 let is_var _ = None
21 module Map = KerName.Map
22 let stage = Summary.Stage.Interp
23 let summary_name = "ltac1tab"
24 end
25 module TacticTab = Nametab.EasyNoWarn(TacticV)()
26
27 let push_tactic vis sp kn = TacticTab.push vis sp kn
28
29 let locate_tactic qid = TacticTab.locate qid
30
31 let locate_extended_all_tactic qid = TacticTab.locate_all qid
32
33 let exists_tactic kn = TacticTab.exists kn
34
35 let path_of_tactic kn = TacticTab.to_path kn
36
37 let shortest_qualid_of_tactic kn = TacticTab.shortest_qualid Id.Set.empty kn
38
39 (** Tactic notations (TacAlias) *)
40
41 type alias = KerName.t
42 type alias_tactic =
43 { alias_args: Id.t list;
44 alias_body: glob_tactic_expr;
45 alias_deprecation: Deprecation.t option;
46 }
47
48 let alias_map = Summary.ref ~name:"tactic-alias"
49 (KerName.Map.empty : alias_tactic KerName.Map.t)
50
51 let register_alias key tac =
52 alias_map := KerName.Map.add key tac !alias_map
53
54 let interp_alias key =
55 try KerName.Map.find key !alias_map
56 with Not_found -> CErrors.anomaly (str "Unknown tactic alias: " ++ KerName.print key ++ str ".")
57
58 let check_alias key = KerName.Map.mem key !alias_map
59
60 (** ML tactic extensions (TacML) *)
61
62 type ml_tactic =
63 Geninterp.Val.t list -> Geninterp.interp_sign -> unit Proofview.tactic
64
65 module MLName =
66 struct
67 type t = ml_tactic_name
68 let compare tac1 tac2 =
69 let c = String.compare tac1.mltac_tactic tac2.mltac_tactic in
70 if c = 0 then String.compare tac1.mltac_plugin tac2.mltac_plugin
71 else c
72 end
73
74 module MLTacMap = Map.Make(MLName)
75
76 let pr_tacname t =
77 str t.mltac_plugin ++ str "::" ++ str t.mltac_tactic
78
79 let tac_tab = ref MLTacMap.empty
80
81 let register_ml_tactic ?(overwrite = false) s (t : ml_tactic array) =
82 let () =
83 if MLTacMap.mem s !tac_tab then
84 if overwrite then
85 tac_tab := MLTacMap.remove s !tac_tab
86 else
87 CErrors.anomaly (str "Cannot redeclare tactic " ++ pr_tacname s ++ str ".")
88 in
89 tac_tab := MLTacMap.add s t !tac_tab
90
91 let interp_ml_tactic { mltac_name = s; mltac_index = i } =
92 try
93 let tacs = MLTacMap.find s !tac_tab in
94 let () = if Array.length tacs <= i then raise Not_found in
95 tacs.(i)
96 with Not_found ->
97 CErrors.user_err
98 (str "The tactic " ++ pr_tacname s ++ str " is not installed.")
99
100 (***************************************************************************)
101 (* Tactic registration *)
102
103 (* Summary and Object declaration *)
104
105 open Libobject
106
107 type ltac_entry = {
108 tac_for_ml : bool;
109 tac_body : glob_tactic_expr;
110 tac_redef : ModPath.t list;
111 tac_deprecation : Deprecation.t option
112 }
113
114 let mactab =
115 Summary.ref (KerName.Map.empty : ltac_entry KerName.Map.t)
116 ~name:"tactic-definition"
117
118 let ltac_entries () = !mactab
119
120 let interp_ltac r = (KerName.Map.find r !mactab).tac_body
121
122 let is_ltac_for_ml_tactic r = (KerName.Map.find r !mactab).tac_for_ml
123
124 let add ~depr kn b t =
125 let entry = {
126 tac_for_ml = b;
127 tac_body = t;
128 tac_redef = [];
129 tac_deprecation = depr;
130 }
131 in
132 mactab := KerName.Map.add kn entry !mactab
133
134 let replace kn path t =
135 let entry _ e = { e with tac_body = t; tac_redef = path :: e.tac_redef } in
136 mactab := KerName.Map.modify kn entry !mactab
137
138 let tac_deprecation kn =
139 try (KerName.Map.find kn !mactab).tac_deprecation with Not_found -> None
140
141 type tacdef = {
142 local : bool;
143 id : Id.t;
144 for_ml : bool;
145 expr : glob_tactic_expr;
146 depr : Deprecation.t option;
147 }
148
149 let load_md i (prefix, {local; id; for_ml=b; expr=t; depr}) =
150 let sp, kn = Lib.make_oname prefix id in
151 let () = if not local then push_tactic (Nametab.Until i) sp kn in
152 add ~depr kn b t
153
154 let open_md i (prefix, {local; id; for_ml=b; expr=t; depr}) =
155 (* todo: generate a warning when non-unique, record choices for non-unique mappings *)
156 let sp, kn = Lib.make_oname prefix id in
157 let () = if not local then push_tactic (Nametab.Exactly i) sp kn in
158 ()
159
160 let cache_md (prefix, {local; id; for_ml=b; expr=t; depr}) =
161 let sp, kn = Lib.make_oname prefix id in
162 let () = push_tactic (Nametab.Until 1) sp kn in
163 add ~depr kn b t
164
165 let subst_md (subst, {local; id; for_ml; expr=t; depr}) =
166 {local; id; for_ml; expr=Tacsubst.subst_tactic subst t; depr}
167
168 let classify_md _ = Substitute
169
170 let inMD : tacdef -> obj =
171 declare_named_object_gen {(default_object "TAC-DEFINITION") with
172 cache_function = cache_md;
173 load_function = load_md;
174 open_function = filtered_open open_md;
175 subst_function = subst_md;
176 classify_function = classify_md}
177
178 let register_ltac for_ml local ?deprecation id tac =
179 Lib.add_leaf (inMD {local; id; for_ml; expr=tac; depr=deprecation})
180
181 type tacreplace = {
182 repl_local : Libobject.locality;
183 repl_tac : ltac_constant;
184 repl_expr : glob_tactic_expr;
185 }
186
187 let load_replace i (prefix, {repl_local; repl_tac; repl_expr=t}) =
188 match repl_local with
189 | Local | Export -> ()
190 | SuperGlobal ->
191 replace repl_tac prefix.obj_mp t
192
193 let open_replace i (prefix, {repl_local; repl_tac; repl_expr=t}) =
194 match repl_local with
195 | Local | SuperGlobal -> ()
196 | Export ->
197 replace repl_tac prefix.obj_mp t
198
199 let cache_replace (prefix, {repl_local; repl_tac; repl_expr=t}) =
200 replace repl_tac prefix.obj_mp t
201
202 let subst_replace (subst, {repl_local; repl_tac; repl_expr}) =
203 { repl_local;
204 repl_tac=Mod_subst.subst_kn subst repl_tac;
205 repl_expr=Tacsubst.subst_tactic subst repl_expr;
206 }
207
208 let classify_replace o = match o.repl_local with
209 | Local -> Dispose
210 | Export | SuperGlobal -> Substitute
211
212 let inReplace : tacreplace -> obj =
213 declare_named_object_gen {(default_object "TAC-REDEFINITION") with
214 cache_function = cache_replace;
215 load_function = load_replace;
216 (* should this be simple_open ie only redefine when importing the
217 module containing the redef instead of a parent? *)
218 open_function = filtered_open open_replace;
219 subst_function = subst_replace;
220 classify_function = classify_replace}
221
222 let redefine_ltac repl_local repl_tac repl_expr =
223 Lib.add_leaf (inReplace {repl_local; repl_tac; repl_expr})