Debian Codegraph @

plugins/ltac/g_rewrite.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 (* Syntax for rewriting with strategies *)
12
13 {
14
15 open Names
16 open Locus
17 open Constrexpr
18 open Glob_term
19 open Genintern
20 open Geninterp
21 open Extraargs
22 open Rewrite
23 open ComRewrite
24 open Stdarg
25 open Tactypes
26 open Procq.Prim
27 open Procq.Constr
28 open Pvernac.Vernac_
29 open Pltac
30 open Vernacextend
31
32 }
33
34 DECLARE PLUGIN "rocq-runtime.plugins.ltac"
35
36 {
37
38 type constr_expr_with_bindings = constr_expr with_bindings
39 type glob_constr_with_bindings = glob_constr_and_expr with_bindings
40 type glob_constr_with_bindings_sign = interp_sign * glob_constr_and_expr with_bindings
41
42 let pr_glob_constr_with_bindings_sign env sigma _ _ _ (ge : glob_constr_with_bindings_sign) =
43 Printer.pr_glob_constr_env env sigma (fst (fst (snd ge)))
44 let pr_glob_constr_with_bindings env sigma _ _ _ (ge : glob_constr_with_bindings) =
45 Printer.pr_glob_constr_env env sigma (fst (fst ge))
46 let pr_constr_expr_with_bindings env sigma prc _ _ (ge : constr_expr_with_bindings) = prc env sigma (fst ge)
47 let interp_glob_constr_with_bindings ist _ _ c = (ist, c)
48 let glob_glob_constr_with_bindings ist l = Tacintern.intern_constr_with_bindings ist l
49 let subst_glob_constr_with_bindings s c =
50 Tacsubst.subst_glob_with_bindings s c
51
52 }
53
54 ARGUMENT EXTEND glob_constr_with_bindings
55 PRINTED BY { pr_glob_constr_with_bindings_sign env sigma }
56
57 INTERPRETED BY { interp_glob_constr_with_bindings }
58 GLOBALIZED BY { glob_glob_constr_with_bindings }
59 SUBSTITUTED BY { subst_glob_constr_with_bindings }
60
61 RAW_PRINTED BY { pr_constr_expr_with_bindings env sigma }
62 GLOB_PRINTED BY { pr_glob_constr_with_bindings env sigma }
63
64 | [ constr_with_bindings(bl) ] -> { bl }
65 END
66
67 {
68
69 let subst_strategy sub = map_strategy
70 (Tacsubst.subst_glob_constr_and_expr sub)
71 (Tacsubst.subst_glob_red_expr sub)
72 (fun x -> x)
73
74 let pr_strategy _ _ _ (s : strategy) = Pp.str "<strategy>"
75 let pr_raw_strategy env sigma prc prlc _ (s : Tacexpr.raw_strategy) =
76 let prr = Pptactic.pr_red_expr env sigma (prc, prlc, Pputils.pr_or_by_notation Libnames.pr_qualid, prc,Pputils.pr_or_var Pp.int, Redexpr.pr_raw_user_red_expr) in
77 Rewrite.pr_strategy (prc env sigma) prr Pputils.pr_lident s
78 let pr_glob_strategy env sigma prc prlc _ (s : Tacexpr.glob_strategy) =
79 let prcst = Pputils.pr_or_var Pptactic.(pr_and_short_name (pr_evaluable_reference_env env)) in
80 let prr = Pptactic.pr_red_expr env sigma (prc, prlc, prcst, prc, Pputils.pr_or_var Pp.int, Redexpr.pr_glob_user_red_expr) in
81 Rewrite.pr_strategy (prc env sigma) prr Id.print s
82
83 }
84
85 ARGUMENT EXTEND rewstrategy
86 PRINTED BY { pr_strategy }
87
88 INTERPRETED BY { Tacinterp.interp_strategy }
89 GLOBALIZED BY { Tacintern.intern_strategy }
90 SUBSTITUTED BY { subst_strategy }
91
92 RAW_PRINTED BY { pr_raw_strategy env sigma }
93 GLOB_PRINTED BY { pr_glob_strategy env sigma }
94 END
95
96 GRAMMAR EXTEND Gram
97 GLOBAL: rewstrategy;
98 rewstrategy:
99 [ NONA
100 [ IDENT "fix"; id = identref; ":="; s = rewstrategy1 -> { StratFix (id, s) }
101 | h = ne_rewstrategy1_list_sep_semicolon -> { h } ] ]
102 ;
103 ne_rewstrategy1_list_sep_semicolon:
104 [ LEFTA
105 [ h = SELF; ";"; h' = rewstrategy1 -> { StratBinary (Compose, h, h') }
106 | h = rewstrategy1 -> { h } ] ]
107 ;
108 rewstrategy1:
109 [ RIGHTA
110 [ "<-"; c = constr -> { StratConstr (c, false) }
111 | IDENT "subterms"; h = SELF -> { StratUnary (Subterms, h) }
112 | IDENT "subterm"; h = SELF -> { StratUnary (Subterm, h) }
113 | IDENT "innermost"; h = SELF -> { StratUnary(Innermost, h) }
114 | IDENT "outermost"; h = SELF -> { StratUnary(Outermost, h) }
115 | IDENT "bottomup"; h = SELF -> { StratUnary(Bottomup, h) }
116 | IDENT "topdown"; h = SELF -> { StratUnary(Topdown, h) }
117 | IDENT "progress"; h = SELF -> { StratUnary (Progress, h) }
118 | IDENT "try"; h = SELF -> { StratUnary (Try, h) }
119 | IDENT "any"; h = SELF -> { StratUnary (Any, h) }
120 | IDENT "repeat"; h = SELF -> { StratUnary (Repeat, h) }
121 | IDENT "choice"; h = LIST1 rewstrategy0 -> { StratNAry (Choice, h) }
122 | IDENT "old_hints"; h = preident -> { StratHints (true, h) }
123 | IDENT "hints"; h = preident -> { StratHints (false, h) }
124 | IDENT "terms"; h = LIST0 constr -> { StratTerms h }
125 | IDENT "eval"; r = red_expr -> { StratEval r }
126 | IDENT "fold"; c = constr -> { StratFold c }
127 | h = rewstrategy0 -> { h } ] ]
128 ;
129 rewstrategy0:
130 [ NONA
131 [ c = constr -> { StratConstr (c, true) }
132 | IDENT "id" -> { StratId }
133 | IDENT "fail" -> { StratFail }
134 | IDENT "refl" -> { StratRefl }
135 | "("; h = rewstrategy; ")" -> { h } ] ]
136 ;
137 END
138
139 (* By default the strategy for "rewrite_db" is top-down *)
140
141 {
142
143 let db_strat db = StratUnary (Topdown, StratHints (false, db))
144 let cl_rewrite_clause_db ist db = cl_rewrite_clause_strat (strategy_of_ast (db_strat db))
145
146 let cl_rewrite_clause (ist, c) b occ cl =
147 let c env sigma = Tacinterp.interp_open_constr_with_bindings ist env sigma c in
148 cl_rewrite_clause c b occ cl
149
150 }
151
152 TACTIC EXTEND rewrite_strat
153 | [ "rewrite_strat" rewstrategy(s) "in" hyp(id) ] -> { cl_rewrite_clause_strat s (Some id) }
154 | [ "rewrite_strat" rewstrategy(s) ] -> { cl_rewrite_clause_strat s None }
155 | [ "rewrite_db" preident(db) "in" hyp(id) ] -> { cl_rewrite_clause_db ist db (Some id) }
156 | [ "rewrite_db" preident(db) ] -> { cl_rewrite_clause_db ist db None }
157 END
158
159 {
160
161 let clsubstitute o c =
162 Proofview.Goal.enter begin fun gl ->
163 let is_tac id = match DAst.get (fst (fst (snd c))) with GVar id' when Id.equal id' id -> true | _ -> false in
164 let hyps = Tacmach.pf_ids_of_hyps gl in
165 Tacticals.tclMAP
166 (fun cl ->
167 match cl with
168 | Some id when is_tac id -> Tacticals.tclIDTAC
169 | _ -> cl_rewrite_clause c o AllOccurrences cl)
170 (None :: List.map (fun id -> Some id) hyps)
171 end
172
173 }
174
175 TACTIC EXTEND substitute
176 | [ "substitute" orient(o) glob_constr_with_bindings(c) ] -> { clsubstitute o c }
177 END
178
179
180 (* Compatibility with old Setoids *)
181
182 TACTIC EXTEND setoid_rewrite
183 | [ "setoid_rewrite" orient(o) glob_constr_with_bindings(c) ]
184 -> { cl_rewrite_clause c o AllOccurrences None }
185 | [ "setoid_rewrite" orient(o) glob_constr_with_bindings(c) "in" hyp(id) ] ->
186 { cl_rewrite_clause c o AllOccurrences (Some id) }
187 | [ "setoid_rewrite" orient(o) glob_constr_with_bindings(c) "at" occurrences(occ) ] ->
188 { cl_rewrite_clause c o (occurrences_of occ) None }
189 | [ "setoid_rewrite" orient(o) glob_constr_with_bindings(c) "at" occurrences(occ) "in" hyp(id)] ->
190 { cl_rewrite_clause c o (occurrences_of occ) (Some id) }
191 | [ "setoid_rewrite" orient(o) glob_constr_with_bindings(c) "in" hyp(id) "at" occurrences(occ)] ->
192 { cl_rewrite_clause c o (occurrences_of occ) (Some id) }
193 END
194
195 VERNAC COMMAND EXTEND AddRelation CLASSIFIED AS SIDEFF
196 | #[ atts = rewrite_attributes; ] [ "Add" "Relation" constr(a) constr(aeq) "reflexivity" "proved" "by" constr(lemma1)
197 "symmetry" "proved" "by" constr(lemma2) "as" identref(n) ] ->
198 { declare_relation atts a aeq n (Some lemma1) (Some lemma2) None }
199
200 | #[ atts = rewrite_attributes; ] [ "Add" "Relation" constr(a) constr(aeq) "reflexivity" "proved" "by" constr(lemma1)
201 "as" identref(n) ] ->
202 { declare_relation atts a aeq n (Some lemma1) None None }
203 | #[ atts = rewrite_attributes; ] [ "Add" "Relation" constr(a) constr(aeq) "as" identref(n) ] ->
204 { declare_relation atts a aeq n None None None }
205 END
206
207 VERNAC COMMAND EXTEND AddRelation2 CLASSIFIED AS SIDEFF
208 | #[ atts = rewrite_attributes; ] [ "Add" "Relation" constr(a) constr(aeq) "symmetry" "proved" "by" constr(lemma2)
209 "as" identref(n) ] ->
210 { declare_relation atts a aeq n None (Some lemma2) None }
211 | #[ atts = rewrite_attributes; ] [ "Add" "Relation" constr(a) constr(aeq) "symmetry" "proved" "by" constr(lemma2) "transitivity" "proved" "by" constr(lemma3) "as" identref(n) ] ->
212 { declare_relation atts a aeq n None (Some lemma2) (Some lemma3) }
213 END
214
215 VERNAC COMMAND EXTEND AddRelation3 CLASSIFIED AS SIDEFF
216 | #[ atts = rewrite_attributes; ] [ "Add" "Relation" constr(a) constr(aeq) "reflexivity" "proved" "by" constr(lemma1)
217 "transitivity" "proved" "by" constr(lemma3) "as" identref(n) ] ->
218 { declare_relation atts a aeq n (Some lemma1) None (Some lemma3) }
219 | #[ atts = rewrite_attributes; ] [ "Add" "Relation" constr(a) constr(aeq) "reflexivity" "proved" "by" constr(lemma1)
220 "symmetry" "proved" "by" constr(lemma2) "transitivity" "proved" "by" constr(lemma3)
221 "as" identref(n) ] ->
222 { declare_relation atts a aeq n (Some lemma1) (Some lemma2) (Some lemma3) }
223 | #[ atts = rewrite_attributes; ] [ "Add" "Relation" constr(a) constr(aeq) "transitivity" "proved" "by" constr(lemma3)
224 "as" identref(n) ] ->
225 { declare_relation atts a aeq n None None (Some lemma3) }
226 END
227
228 {
229
230 type binders_argtype = local_binder_expr list
231
232 let wit_binders =
233 (Genarg.create_arg "binders" : binders_argtype Genarg.vernac_genarg_type)
234
235 let binders = Procq.create_generic_entry2 "binders" (Genarg.rawwit wit_binders)
236
237 let () =
238 let raw_printer l = Genprint.PrinterBasic (fun env sigma -> Pp.pr_non_empty_arg Ppconstr.(pr_binders ~flags:(current_flags()) env sigma) l) in
239 Genprint.register_vernac_print0 wit_binders raw_printer
240
241 }
242
243 GRAMMAR EXTEND Gram
244 GLOBAL: binders;
245 binders:
246 [ [ b = Procq.Constr.binders -> { b } ] ];
247 END
248
249 VERNAC COMMAND EXTEND AddParametricRelation CLASSIFIED AS SIDEFF
250 | #[ atts = rewrite_attributes; ] [ "Add" "Parametric" "Relation" binders(b) ":" constr(a) constr(aeq)
251 "reflexivity" "proved" "by" constr(lemma1)
252 "symmetry" "proved" "by" constr(lemma2) "as" identref(n) ] ->
253 { declare_relation atts ~binders:b a aeq n (Some lemma1) (Some lemma2) None }
254 | #[ atts = rewrite_attributes; ] [ "Add" "Parametric" "Relation" binders(b) ":" constr(a) constr(aeq)
255 "reflexivity" "proved" "by" constr(lemma1)
256 "as" identref(n) ] ->
257 { declare_relation atts ~binders:b a aeq n (Some lemma1) None None }
258 | #[ atts = rewrite_attributes; ] [ "Add" "Parametric" "Relation" binders(b) ":" constr(a) constr(aeq) "as" identref(n) ] ->
259 { declare_relation atts ~binders:b a aeq n None None None }
260 END
261
262 VERNAC COMMAND EXTEND AddParametricRelation2 CLASSIFIED AS SIDEFF
263 | #[ atts = rewrite_attributes; ] [ "Add" "Parametric" "Relation" binders(b) ":" constr(a) constr(aeq) "symmetry" "proved" "by" constr(lemma2)
264 "as" identref(n) ] ->
265 { declare_relation atts ~binders:b a aeq n None (Some lemma2) None }
266 | #[ atts = rewrite_attributes; ] [ "Add" "Parametric" "Relation" binders(b) ":" constr(a) constr(aeq) "symmetry" "proved" "by" constr(lemma2) "transitivity" "proved" "by" constr(lemma3) "as" identref(n) ] ->
267 { declare_relation atts ~binders:b a aeq n None (Some lemma2) (Some lemma3) }
268 END
269
270 VERNAC COMMAND EXTEND AddParametricRelation3 CLASSIFIED AS SIDEFF
271 | #[ atts = rewrite_attributes; ] [ "Add" "Parametric" "Relation" binders(b) ":" constr(a) constr(aeq) "reflexivity" "proved" "by" constr(lemma1)
272 "transitivity" "proved" "by" constr(lemma3) "as" identref(n) ] ->
273 { declare_relation atts ~binders:b a aeq n (Some lemma1) None (Some lemma3) }
274 | #[ atts = rewrite_attributes; ] [ "Add" "Parametric" "Relation" binders(b) ":" constr(a) constr(aeq) "reflexivity" "proved" "by" constr(lemma1)
275 "symmetry" "proved" "by" constr(lemma2) "transitivity" "proved" "by" constr(lemma3)
276 "as" identref(n) ] ->
277 { declare_relation atts ~binders:b a aeq n (Some lemma1) (Some lemma2) (Some lemma3) }
278 | #[ atts = rewrite_attributes; ] [ "Add" "Parametric" "Relation" binders(b) ":" constr(a) constr(aeq) "transitivity" "proved" "by" constr(lemma3)
279 "as" identref(n) ] ->
280 { declare_relation atts ~binders:b a aeq n None None (Some lemma3) }
281 END
282
283 {
284
285 let morphism_tactic =
286 let open Tacexpr in
287 let name = "Corelib.Classes.SetoidTactics.add_morphism_tactic" in
288 let tacqid = Libnames.qualid_of_string name in
289 let tac = CAst.make @@ TacArg (TacCall (CAst.make (tacqid, []))) in
290 Tacinterp.interp tac
291
292 }
293
294 VERNAC COMMAND EXTEND AddSetoid1 CLASSIFIED AS SIDEFF
295 | #[ atts = rewrite_attributes; ] [ "Add" "Setoid" constr(a) constr(aeq) constr(t) "as" identref(n) ] ->
296 {
297 add_setoid atts [] a aeq t n
298 }
299 | #[ atts = rewrite_attributes; ] [ "Add" "Parametric" "Setoid" binders(binders) ":" constr(a) constr(aeq) constr(t) "as" identref(n) ] ->
300 {
301 add_setoid atts binders a aeq t n
302 }
303 | #[ atts = rewrite_attributes; ] ![ open_proof ] [ "Add" "Morphism" constr(m) ":" identref(n) ]
304 => { VtStartProof(GuaranteesOpacity, [n.CAst.v]) }
305 -> { if Lib.is_modtype () then
306 CErrors.user_err Pp.(str "Add Morphism cannot be used in a module type. Use Parameter Morphism instead.");
307 add_morphism_interactive atts ~tactic:morphism_tactic m n }
308 | #[ atts = rewrite_attributes; ] [ "Declare" "Morphism" constr(m) ":" identref(n) ]
309 => { VtSideff([n.CAst.v], VtLater) }
310 -> { add_morphism_as_parameter atts m n }
311 | #[ atts = rewrite_attributes; ] ![ open_proof ] [ "Add" "Morphism" constr(m) "with" "signature" lconstr(s) "as" identref(n) ]
312 => { VtStartProof(GuaranteesOpacity,[n.CAst.v]) }
313 -> { add_morphism atts ~tactic:morphism_tactic [] m s n }
314 | #[ atts = rewrite_attributes; ] ![ open_proof ] [ "Add" "Parametric" "Morphism" binders(binders) ":" constr(m)
315 "with" "signature" lconstr(s) "as" identref(n) ]
316 => { VtStartProof(GuaranteesOpacity,[n.CAst.v]) }
317 -> { add_morphism atts ~tactic:morphism_tactic binders m s n }
318 END
319
320 TACTIC EXTEND setoid_symmetry
321 | [ "setoid_symmetry" ] -> { setoid_symmetry }
322 | [ "setoid_symmetry" "in" hyp(n) ] -> { setoid_symmetry_in n }
323 END
324
325 TACTIC EXTEND setoid_reflexivity
326 | [ "setoid_reflexivity" ] -> { setoid_reflexivity }
327 END
328
329 TACTIC EXTEND setoid_transitivity
330 | [ "setoid_transitivity" constr(t) ] -> { setoid_transitivity (Some t) }
331 | [ "setoid_etransitivity" ] -> { setoid_transitivity None }
332 END
333
334 VERNAC COMMAND EXTEND PrintRewriteHintDb CLASSIFIED AS QUERY
335 | [ "Print" "Rewrite" "HintDb" preident(s) ] ->
336 { Feedback.msg_notice (Autorewrite.print_rewrite_hintdb s) }
337 END