Debian Codegraph @

plugins/ltac2/tac2env.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 Names
13 open Libnames
14 open Tac2expr
15 open Tac2val
16
17 type global_data = {
18 gdata_expr : glb_tacexpr;
19 gdata_type : type_scheme;
20 gdata_mutable : bool;
21 gdata_deprecation : Deprecation.t option;
22 gdata_mutation_history : ModPath.t list;
23 }
24
25 type constructor_data = {
26 cdata_prms : int;
27 cdata_type : type_constant;
28 cdata_args : int glb_typexpr list;
29 cdata_indx : int option;
30 }
31
32 type projection_data = {
33 pdata_prms : int;
34 pdata_type : type_constant;
35 pdata_ptyp : int glb_typexpr;
36 pdata_mutb : bool;
37 pdata_indx : int;
38 }
39
40 type alias_data = {
41 alias_body : raw_tacexpr;
42 alias_depr : Deprecation.t option;
43 }
44
45 type ltac_state = {
46 ltac_tactics : global_data KerName.Map.t;
47 ltac_constructors : constructor_data KerName.Map.t;
48 ltac_projections : projection_data KerName.Map.t;
49 ltac_types : glb_quant_typedef KerName.Map.t;
50 ltac_aliases : alias_data KerName.Map.t;
51 }
52
53 let empty_state = {
54 ltac_tactics = KerName.Map.empty;
55 ltac_constructors = KerName.Map.empty;
56 ltac_projections = KerName.Map.empty;
57 ltac_types = KerName.Map.empty;
58 ltac_aliases = KerName.Map.empty;
59 }
60
61 type compile_info = {
62 source : string;
63 }
64
65 let ltac_state = Summary.ref empty_state ~name:"ltac2-state"
66
67 let compiled_tacs = Summary.ref ~local:true ~name:"ltac2-compiled-state" KerName.Map.empty
68
69 type notation_data =
70 | UntypedNota of raw_tacexpr
71 | TypedNota of {
72 nota_prms : int;
73 nota_argtys : int glb_typexpr Id.Map.t;
74 nota_ty : int glb_typexpr;
75 nota_body : glb_tacexpr;
76 }
77
78 let ltac_notations = Summary.ref KerName.Map.empty ~name:"ltac2-notations"
79
80 let define_global kn e =
81 let state = !ltac_state in
82 ltac_state := { state with ltac_tactics = KerName.Map.add kn e state.ltac_tactics }
83
84 let interp_global kn =
85 let data = KerName.Map.find kn ltac_state.contents.ltac_tactics in
86 data
87
88 let set_compiled_global kn info v =
89 assert (not (interp_global kn).gdata_mutable);
90 compiled_tacs := KerName.Map.add kn (info,v) !compiled_tacs
91
92 let get_compiled_global kn = KerName.Map.find_opt kn !compiled_tacs
93
94 let globals () = (!ltac_state).ltac_tactics
95
96 let define_constructor kn t =
97 let state = !ltac_state in
98 ltac_state := {
99 state with
100 ltac_constructors = KerName.Map.add kn t state.ltac_constructors;
101 }
102
103 let interp_constructor kn = KerName.Map.find kn ltac_state.contents.ltac_constructors
104
105 let find_all_constructors_in_type kn =
106 KerName.Map.filter (fun _ data -> KerName.equal kn data.cdata_type) (!ltac_state).ltac_constructors
107
108 let define_projection kn t =
109 let state = !ltac_state in
110 ltac_state := { state with ltac_projections = KerName.Map.add kn t state.ltac_projections }
111
112 let interp_projection kn = KerName.Map.find kn ltac_state.contents.ltac_projections
113
114 let define_type kn e =
115 let state = !ltac_state in
116 ltac_state := { state with ltac_types = KerName.Map.add kn e state.ltac_types }
117
118 let interp_type kn = KerName.Map.find kn ltac_state.contents.ltac_types
119
120 let define_alias ?deprecation kn tac =
121 let state = !ltac_state in
122 let data = { alias_body = tac; alias_depr = deprecation } in
123 ltac_state := { state with ltac_aliases = KerName.Map.add kn data state.ltac_aliases }
124
125 let interp_alias kn = KerName.Map.find kn ltac_state.contents.ltac_aliases
126
127 let define_notation kn tac =
128 ltac_notations := KerName.Map.add kn tac !ltac_notations
129
130 let interp_notation kn = KerName.Map.find kn !ltac_notations
131
132 module ML =
133 struct
134 type t = ml_tactic_name
135 let compare n1 n2 =
136 let c = String.compare n1.mltac_plugin n2.mltac_plugin in
137 if Int.equal c 0 then String.compare n1.mltac_tactic n2.mltac_tactic
138 else c
139 end
140
141 module MLMap = Map.Make(ML)
142
143 let primitive_map = ref MLMap.empty
144
145 let define_primitive name f =
146 let f = match f with
147 | ValCls f -> ValCls (annotate_closure (FrPrim name) f)
148 | _ -> f
149 in
150 primitive_map := MLMap.add name f !primitive_map
151 let interp_primitive name = MLMap.find name !primitive_map
152
153 (** Name management *)
154
155 type tacref = Tac2expr.tacref =
156 | TacConstant of ltac_constant
157 | TacAlias of ltac_alias
158
159 module TacRef =
160 struct
161 type t = tacref
162 let compare r1 r2 = match r1, r2 with
163 | TacConstant c1, TacConstant c2 -> KerName.compare c1 c2
164 | TacAlias c1, TacAlias c2 -> KerName.compare c1 c2
165 | TacConstant _, TacAlias _ -> -1
166 | TacAlias _, TacConstant _ -> 1
167
168 let equal r1 r2 = compare r1 r2 == 0
169 end
170
171 module DefV = struct
172 include TacRef
173 let is_var _ = None
174 let stage = Summary.Stage.Interp
175 let summary_name = "ltac2-deftab"
176 module Map = Map.Make(TacRef)
177 end
178 module DefTab = Nametab.EasyNoWarn(DefV)()
179
180 module CtorV = struct
181 include KerName
182 let is_var _ = None
183 module Map = KerName.Map
184 let stage = Summary.Stage.Interp
185 let summary_name = "ltac2-ctortab"
186 let object_name = "Ltac2 constructor"
187 let warning_name_base = "ltac2-constructor"
188 end
189 module CtorTab = Nametab.Easy(CtorV)()
190
191 module TypV = struct
192 include KerName
193 let is_var _ = None
194 let stage = Summary.Stage.Interp
195 let summary_name = "ltac2-typtab"
196 module Map = KerName.Map
197 end
198 module TypTab = Nametab.EasyNoWarn(TypV)()
199
200 module ProjV = struct
201 include KerName
202 let is_var _ = None
203 let stage = Summary.Stage.Interp
204 let summary_name = "ltac2-projtab"
205 module Map = KerName.Map
206 end
207 module ProjTab = Nametab.EasyNoWarn(ProjV)()
208
209 let push_ltac = DefTab.push
210
211 let locate_ltac = DefTab.locate
212
213 let locate_extended_all_ltac = DefTab.locate_all
214
215 let path_of_ltac = DefTab.to_path
216
217 let shortest_qualid_of_ltac = DefTab.shortest_qualid
218
219 let push_constructor ?user_warns vis sp kn =
220 (* XXX support qf *)
221 let user_warns = Option.map UserWarn.with_empty_qf user_warns in
222 CtorTab.push ?wdata:user_warns vis sp kn
223
224 (* XXX expose nowarn argument? *)
225 let locate_constructor qid = CtorTab.locate qid
226
227 let locate_extended_all_constructor = CtorTab.locate_all
228
229 let path_of_constructor = CtorTab.to_path
230
231 let shortest_qualid_of_constructor kn = CtorTab.shortest_qualid Id.Set.empty kn
232
233 let push_type = TypTab.push
234
235 let locate_type = TypTab.locate
236
237 let locate_extended_all_type = TypTab.locate_all
238
239 let path_of_type = TypTab.to_path
240
241 let shortest_qualid_of_type ?loc kn = TypTab.shortest_qualid Id.Set.empty ?loc kn
242
243 let push_projection = ProjTab.push
244
245 let locate_projection = ProjTab.locate
246
247 let locate_extended_all_projection = ProjTab.locate_all
248
249 let shortest_qualid_of_projection kn = ProjTab.shortest_qualid Id.Set.empty kn
250
251 type 'a or_glb_tacexpr =
252 | GlbVal of 'a
253 | GlbTacexpr of glb_tacexpr
254
255 type environment = {
256 env_ist : valexpr Id.Map.t;
257 }
258
259 type ('a, 'b, 'r) intern_fun = Genintern.glob_sign -> 'a -> 'b * 'r glb_typexpr
260
261 type ('a, 'b) ml_object = {
262 ml_intern : 'r. ('a, 'b or_glb_tacexpr, 'r) intern_fun;
263 ml_subst : Mod_subst.substitution -> 'b -> 'b;
264 ml_interp : environment -> 'b -> valexpr Proofview.tactic;
265 ml_print : Environ.env -> Evd.evar_map -> 'b -> Pp.t;
266 ml_raw_print : Environ.env -> Evd.evar_map -> 'a -> Pp.t;
267 }
268
269 module MLTypeObj =
270 struct
271 type ('a, 'b) t = ('a, 'b) ml_object
272 end
273
274 module MLType = Tac2dyn.ArgMap(MLTypeObj)
275
276 let ml_object_table = ref MLType.empty
277
278 let define_ml_object t tpe =
279 ml_object_table := MLType.add t (MLType.Pack tpe) !ml_object_table
280
281 let interp_ml_object t =
282 try
283 let MLType.Pack ans = MLType.find t !ml_object_table in
284 ans
285 with Not_found ->
286 CErrors.anomaly Pp.(str "Unknown object type " ++ str (Tac2dyn.Arg.repr t))
287
288 (** Absolute paths *)
289
290 let rocq_prefix =
291 MPfile (DirPath.make (List.map Id.of_string ["Init"; "Ltac2"]))
292
293 let coq_prefix = rocq_prefix
294
295 let std_prefix =
296 MPfile (DirPath.make (List.map Id.of_string ["Std"; "Ltac2"]))
297
298 let ltac1_prefix =
299 MPfile (DirPath.make (List.map Id.of_string ["Ltac1"; "Ltac2"]))
300
301 (** Generic arguments *)
302
303 type var_quotation_kind =
304 | ConstrVar
305 | PretermVar
306 | PatternVar
307 | HypVar
308
309 let wit_ltac2_constr = Genarg.make0 "ltac2:in-constr"
310 let wit_ltac2_var_quotation = Genarg.make0 "ltac2:quotation"
311 let wit_ltac2_tac = Genarg.make0 "ltac2:tactic"
312
313 let () = Geninterp.register_val0 wit_ltac2_tac
314 (Some (Geninterp.val_tag (Genarg.topwit Stdarg.wit_unit)))
315
316 let is_constructor_id id =
317 let id = Id.to_string id in
318 assert (String.length id > 0);
319 match id with
320 | "true" | "false" -> true (* built-in constructors *)
321 | _ ->
322 match id.[0] with
323 | 'A'..'Z' -> true
324 | _ -> false
325
326 let is_constructor qid =
327 let (_, id) = repr_qualid qid in
328 is_constructor_id id