Debian Codegraph @

plugins/ltac/extraargs.mlg

raw
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 DECLARE PLUGIN "rocq-runtime.plugins.ltac"
12
13 {
14
15 open Pp
16 open Stdarg
17 open Tacarg
18 open Procq.Prim
19 open Procq.Constr
20 open Names
21 open Taccoerce
22 open Tacinterp
23 open Locus
24
25 (** Adding scopes for generic arguments not defined through ARGUMENT EXTEND *)
26
27 (** We need g_ltac to initialize tactic_value *)
28 let () = G_ltac.for_extraargs
29
30 let create_generic_quotation name e wit =
31 let inject (loc, v) = Tacexpr.TacGeneric (Some name, Genarg.in_gen (Genarg.rawwit wit) v) in
32 Tacentries.create_ltac_quotation ~plugin:"rocq-runtime.plugins.ltac" name inject (e, None)
33
34 let () = create_generic_quotation "integer" Procq.Prim.integer Stdarg.wit_int
35 let () = create_generic_quotation "string" Procq.Prim.string Stdarg.wit_string
36
37 let () = create_generic_quotation "smart_global" Procq.Prim.smart_global Stdarg.wit_smart_global
38
39 let () = create_generic_quotation "ident" Procq.Prim.ident Stdarg.wit_ident
40 let () = create_generic_quotation "reference" Procq.Prim.reference Stdarg.wit_ref
41 let () = create_generic_quotation "uconstr" Procq.Constr.lconstr Stdarg.wit_uconstr
42 let () = create_generic_quotation "constr" Procq.Constr.lconstr Stdarg.wit_constr
43 let () = create_generic_quotation "ipattern" Pltac.simple_intropattern wit_simple_intropattern
44 let () = create_generic_quotation "open_constr" Procq.Constr.lconstr Stdarg.wit_open_constr
45 let () =
46 let inject (loc, v) = Tacexpr.Tacexp v in
47 Tacentries.create_ltac_quotation ~plugin:"rocq-runtime.plugins.ltac" "ltac" inject (Pltac.ltac_expr, Some 5)
48
49 (** Backward-compatible tactic notation entry names *)
50
51 let () =
52 let register name entry = Tacentries.register_tactic_notation_entry name entry in
53 register "hyp" wit_hyp;
54 register "simple_intropattern" wit_simple_intropattern;
55 register "integer" wit_integer;
56 register "reference" wit_ref;
57 ()
58
59 (* Rewriting orientation *)
60
61 let pr_orient _prc _prlc _prt = function
62 | true -> Pp.mt ()
63 | false -> Pp.str " <-"
64
65 }
66
67 ARGUMENT EXTEND orient TYPED AS bool PRINTED BY { pr_orient }
68 | [ "->" ] -> { true }
69 | [ "<-" ] -> { false }
70 | [ ] -> { true }
71 END
72
73 {
74
75 let pr_int _ _ _ i = Pp.int i
76
77 let _natural = Procq.Prim.natural
78
79 }
80
81 ARGUMENT EXTEND natural TYPED AS int PRINTED BY { pr_int }
82 | [ _natural(i) ] -> { i }
83 END
84
85 {
86
87 let pr_orient = pr_orient () () ()
88
89 let pr_int_list = Pp.pr_sequence Pp.int
90 let pr_int_list_full _prc _prlc _prt l = pr_int_list l
91
92 let pr_occurrences _prc _prlc _prt l =
93 match l with
94 | ArgArg x -> pr_int_list x
95 | ArgVar { CAst.loc = loc; v=id } -> Id.print id
96
97 let occurrences_of = function
98 | [] -> NoOccurrences
99 | n::_ as nl when n < 0 -> AllOccurrencesBut (List.map abs nl)
100 | nl ->
101 if List.exists (fun n -> n < 0) nl then
102 CErrors.user_err Pp.(str "Illegal negative occurrence number.");
103 OnlyOccurrences nl
104
105 let coerce_to_int v = match Value.to_int v with
106 | None -> raise (CannotCoerceTo "an integer")
107 | Some n -> n
108
109 let int_list_of_VList v = match Value.to_list v with
110 | Some l -> List.map (fun n -> coerce_to_int n) l
111 | _ -> raise (CannotCoerceTo "an integer")
112
113 let interp_occs ist env sigma l =
114 match l with
115 | ArgArg x -> x
116 | ArgVar ({ CAst.v = id } as locid) ->
117 (try int_list_of_VList (Id.Map.find id ist.lfun)
118 with Not_found | CannotCoerceTo _ -> [interp_int ist locid])
119
120 let glob_occs ist l = l
121
122 let subst_occs evm l = l
123
124 }
125
126 ARGUMENT EXTEND occurrences
127 TYPED AS int list
128 PRINTED BY { pr_int_list_full }
129
130 INTERPRETED BY { interp_occs }
131 GLOBALIZED BY { glob_occs }
132 SUBSTITUTED BY { subst_occs }
133
134 RAW_PRINTED BY { pr_occurrences }
135 GLOB_PRINTED BY { pr_occurrences }
136
137 | [ ne_integer_list(l) ] -> { ArgArg l }
138 | [ hyp(id) ] -> { ArgVar id }
139 END
140
141 {
142
143 let pr_occurrences = pr_occurrences () () ()
144
145 let pr_gen env sigma prc _prlc _prtac x = prc env sigma x
146
147 let pr_globc env sigma lvl _prc _prlc _prtac glob =
148 Printer.pr_closed_glob_n_env env sigma lvl glob
149
150 let interp_glob ist env sigma t = Tacinterp.interp_glob_closure ist env sigma t
151
152 let glob_glob = Tacintern.intern_constr
153
154 let pr_lconstr env sigma _ prc _ c = prc env sigma c
155
156 let subst_glob = Tacsubst.subst_glob_constr_and_expr
157
158 }
159
160 ARGUMENT EXTEND glob
161 PRINTED BY { fun c -> pr_globc env sigma Ppconstr.lsimpleconstr c }
162
163 INTERPRETED BY { interp_glob }
164 GLOBALIZED BY { glob_glob }
165 SUBSTITUTED BY { subst_glob }
166
167 RAW_PRINTED BY { pr_gen env sigma }
168 GLOB_PRINTED BY { pr_gen env sigma }
169 | [ constr(c) ] -> { c }
170 END
171
172 {
173
174 let l_constr = Procq.Constr.lconstr
175
176 }
177
178 ARGUMENT EXTEND lconstr
179 TYPED AS constr
180 PRINTED BY { pr_lconstr env sigma }
181 | [ l_constr(c) ] -> { c }
182 END
183
184 ARGUMENT EXTEND lglob
185 TYPED AS glob
186 PRINTED BY { fun c -> pr_globc env sigma Ppconstr.ltop c }
187
188 INTERPRETED BY { interp_glob }
189 GLOBALIZED BY { glob_glob }
190 SUBSTITUTED BY { subst_glob }
191
192 RAW_PRINTED BY { pr_gen env sigma }
193 GLOB_PRINTED BY { pr_gen env sigma }
194 | [ lconstr(c) ] -> { c }
195 END
196
197 {
198
199 type 'id gen_place = ('id * hyp_location_flag) option
200
201 type loc_place = lident gen_place
202 type place = Id.t gen_place
203
204 let pr_gen_place pr_id = function
205 | None -> Pp.mt ()
206 | Some (id, InHyp) -> str "in " ++ pr_id id
207 | Some (id, InHypTypeOnly) ->
208 str "in (type of " ++ pr_id id ++ str ")"
209 | Some (id, InHypValueOnly) ->
210 str "in (value of " ++ pr_id id ++ str ")"
211
212 let pr_loc_place _ _ _ = pr_gen_place (fun { CAst.v = id } -> Id.print id)
213 let pr_place _ _ _ = pr_gen_place Id.print
214 let pr_hloc = pr_loc_place () () ()
215
216 let intern_place ist = function
217 | None -> None
218 | Some (id, hl) -> Some (Tacintern.intern_hyp ist id, hl)
219
220 let interp_place ist env sigma = function
221 | None -> None
222 | Some (id, hl) -> Some (Tacinterp.interp_hyp ist env sigma id, hl)
223
224 let subst_place subst pl = pl
225
226 }
227
228 ARGUMENT EXTEND hloc
229 PRINTED BY { pr_place }
230 INTERPRETED BY { interp_place }
231 GLOBALIZED BY { intern_place }
232 SUBSTITUTED BY { subst_place }
233 RAW_PRINTED BY { pr_loc_place }
234 GLOB_PRINTED BY { pr_loc_place }
235 | [ ] ->
236 { None }
237 | [ "in" "|-" "*" ] ->
238 { None }
239 | [ "in" ident(id) ] ->
240 { Some ((CAst.make id), InHyp) }
241 | [ "in" "(" "type" "of" ident(id) ")" ] ->
242 { Some ((CAst.make id), InHypTypeOnly) }
243 | [ "in" "(" "value" "of" ident(id) ")" ] ->
244 { Some ((CAst.make id), InHypValueOnly) }
245
246 END
247
248 {
249
250 let pr_rename _ _ _ (n, m) = Id.print n ++ str " into " ++ Id.print m
251
252 }
253
254 ARGUMENT EXTEND rename
255 TYPED AS (ident * ident)
256 PRINTED BY { pr_rename }
257 | [ ident(n) "into" ident(m) ] -> { (n, m) }
258 END
259
260 (* Julien: Mise en commun des differentes version de replace with in by *)
261
262 {
263
264 let pr_by_arg_tac env sigma _prc _prlc prtac opt_c =
265 match opt_c with
266 | None -> mt ()
267 | Some t -> hov 2 (str "by" ++ spc () ++ prtac env sigma (Constrexpr.LevelLe 3) t)
268
269 }
270
271 ARGUMENT EXTEND by_arg_tac
272 TYPED AS tactic option
273 PRINTED BY { pr_by_arg_tac env sigma }
274 | [ "by" tactic3(c) ] -> { Some c }
275 | [ ] -> { None }
276 END
277
278 {
279
280 let pr_by_arg_tac env sigma prtac opt_c = pr_by_arg_tac env sigma () () prtac opt_c
281
282 let pr_in_clause _ _ _ cl = Pptactic.pr_in_clause Pputils.pr_lident cl
283 let pr_in_top_clause _ _ _ cl = Pptactic.pr_in_clause Id.print cl
284 let in_clause' = Pltac.in_clause
285
286 }
287
288 ARGUMENT EXTEND in_clause
289 TYPED AS clause_dft_concl
290 PRINTED BY { pr_in_top_clause }
291 RAW_PRINTED BY { pr_in_clause }
292 GLOB_PRINTED BY { pr_in_clause }
293 | [ in_clause'(cl) ] -> { cl }
294 END
295
296 {
297
298 let local_test_lpar_id_colon =
299 let open Procq.Lookahead in
300 to_entry "lpar_id_colon" begin
301 lk_kw "(" >> lk_ident >> lk_kw ":"
302 end
303
304 let pr_lpar_id_colon _ _ _ _ = mt ()
305
306 }
307
308 ARGUMENT EXTEND test_lpar_id_colon TYPED AS unit PRINTED BY { pr_lpar_id_colon }
309 | [ local_test_lpar_id_colon(x) ] -> { () }
310 END
311
312 {
313
314 (* Work around a limitation of the macro system *)
315 let strategy_level0 = Procq.Prim.strategy_level
316
317 let pr_strategy _ _ _ v = Conv_oracle.pr_level v
318
319 }
320
321 ARGUMENT EXTEND strategy_level PRINTED BY { pr_strategy }
322 | [ strategy_level0(n) ] -> { n }
323 END
324
325 {
326
327 let intern_strategy ist v = match v with
328 | ArgVar id -> ArgVar (Tacintern.intern_hyp ist id)
329 | ArgArg v -> ArgArg v
330
331 let subst_strategy _ v = v
332
333 let interp_strategy ist env sigma = function
334 | ArgArg n -> n
335 | ArgVar { CAst.v = id; CAst.loc } ->
336 let v =
337 try Id.Map.find id ist.lfun
338 with Not_found ->
339 CErrors.user_err ?loc
340 (str "Unbound variable " ++ Id.print id ++ str".")
341 in
342 let v =
343 try Tacinterp.Value.cast (Genarg.topwit wit_strategy_level) v
344 with CErrors.UserError _ -> Taccoerce.error_ltac_variable ?loc id None v "a strategy_level"
345 in
346 v
347
348 let pr_loc_strategy _ _ _ v = Pputils.pr_or_var Conv_oracle.pr_level v
349
350 }
351
352 ARGUMENT EXTEND strategy_level_or_var
353 TYPED AS strategy_level
354 PRINTED BY { pr_strategy }
355 INTERPRETED BY { interp_strategy }
356 GLOBALIZED BY { intern_strategy }
357 SUBSTITUTED BY { subst_strategy }
358 RAW_PRINTED BY { pr_loc_strategy }
359 GLOB_PRINTED BY { pr_loc_strategy }
360 | [ strategy_level(n) ] -> { ArgArg n }
361 | [ identref(id) ] -> { ArgVar id }
362 END