Debian Codegraph @

tests-stdlib/test_open_terms.v

raw
1 From Stdlib Require Import ZArith Arith List FunctionalExtensionality.
2 From elpi Require Import elpi.
3
4 Lemma ring_example x : x + 1 = 1 + x.
5 Proof. ring. Qed.
6
7 Goal forall l, map (fun x => x + 1) l = map (fun x => 1 + x) l.
8 Proof.
9 Fail replace (x + 1) with (1 + x) by ring.
10 replace (fun x => x + 1) with (fun x => 1 + x).
11 easy.
12 extensionality x.
13 ring.
14 Qed.
15
16 Lemma let_congr {T1 T2 : Type} (b1 b2 : T1) (F1 F2 : T1 -> T2) :
17 b1 = b2 -> (forall x : T1, F1 x = F2 x) ->
18 (let x := b1 in F1 x) = (let x := b2 in F2 x).
19 Proof.
20 intros bq fq; rewrite bq.
21 lazy zeta; apply fq.
22 Qed.
23
24 Lemma map_ext_in_equal2 [A B : Type] (f g : A -> B) (l1 l2 : list A):
25 (forall a : A, In a l1 -> f a = g a) -> l1 = l2 ->
26 map f l1 = map g l2.
27 Proof.
28 now intros exth l1l2; rewrite <- l1l2; apply map_ext_in.
29 Qed.
30
31 Lemma app_prf [A B : Type] (f g : A -> B) (v1 v2 : A) :
32 f = g -> v1 = v2 -> f v1 = g v2.
33 Proof. now intros fg v1v2; rewrite fg, v1v2. Qed.
34
35 Lemma app_prf1 [A : Type] [B : A -> Type] (f g : forall x, B x) (v : A) :
36 f = g -> f v = g v.
37 Proof. now intros fg; rewrite fg. Qed.
38
39 Ltac lazy_beta := lazy beta.
40
41 Elpi Tactic replace.
42
43 Elpi Accumulate lp:{{
44
45 pred preserve_bound_variables i:term o:term.
46
47 preserve_bound_variables I O :-
48 (((pi N T F N1 T1 F1 \
49 copy (fun N T F) (fun N1 T1 F1) :- !,
50 copy T T1,
51 fresh-name N T N1,
52 (@pi-decl N1 T1 x\
53 copy (F x) (F1 x))),
54 (pi B B1 N T F N1 T1 F1 \
55 copy (let N T B F)(let N1 T1 B1 F1) :- !,
56 copy T T1,
57 copy B B1,
58 fresh-name N T N1,
59 (@pi-decl N1 T1 x\ copy (F x) (F1 x))),
60 (pi N T F N1 T1 F1 \
61 copy (prod N T F) (prod N1 T1 F1) :- !,
62 copy T T1,
63 fresh-name N T N1,
64 (@pi-decl N1 T1 x\
65 copy (F x) (F1 x)))) => copy I O).
66
67 func fresh-name name, term -> name.
68
69 fresh-name N T M :-
70 coq.ltac.fresh-id {coq.name->id N} T Mi,
71 coq.id->name Mi M.
72
73 pred mk-app-prf i:list term, i:list term, i: list term, o:term.
74
75 mk-app-prf [F, _] [F, _] [{{@refl_equal _ _}}, P] {{f_equal lp:F lp:P}} :-
76 non-dependent-type F,!.
77 mk-app-prf [F, _, _] [F, _, _] [{{@refl_equal _ _}}, P1, P2]
78 {{f_equal2 lp:F lp:P1 lp:P2}} :-
79 non-dependent-type F,!.
80 mk-app-prf [F, _, _, _] [F, _, _, _] [{{@refl_equal _ _}}, P1, P2, P3]
81 {{f_equal3 lp:F lp:P1 lp:P2 lp:P3}} :-
82 non-dependent-type F,!.
83 mk-app-prf [F, _, _, _, _] [F, _, _, _, _]
84 [{{@refl_equal _ _}}, P1, P2, P3, P4]
85 {{f_equal4 lp:F lp:P1 lp:P2 lp:P3 lp:P4}} :-
86 non-dependent-type F,!.
87 % TODO use nary_congruence from ssreflect
88 mk-app-prf [F, _, _, _, _, _] [F, _, _, _, _, _]
89 [{{@refl_equal _ _}}, P1, P2, P3, P4, P5]
90 {{f_equal5 lp:F lp:P1 lp:P2 lp:P3 lp:P4 lp:P5}} :-
91 non-dependent-type F,!.
92 mk-app-prf [F1, A] [F2, A] [Pf, {{@refl_equal _ _}}]
93 {{app_prf1 lp:F1 lp:F2 lp:A lp:Pf}} :- !.
94 mk-app-prf [F1, A] [F2, B] [Pf, Pa]
95 {{app_prf lp:F1 lp:F2 lp:A lp:B lp:Pf lp:Pa}} :-
96 coq.typecheck F1 (prod _ _ Ft) ok,
97 (pi c1 c2 \ Ft c1 = Ft c2),!.
98 mk-app-prf [F1, A | Args1] [F2, A |Args2] [Pf, {{@refl_equal _ _}} | Ps]
99 P :- !,
100 mk-app-prf [app [F1, A] | Args1] [app [F2, A] | Args2]
101 [{{app_prf1 lp:F1 lp:F2 lp:A lp:Pf}} | Ps] P.
102
103 mk-app-prf [F1, A | Args1] [F2, B | Args2] [Pf, Pa | Ps] P :-
104 coq.typecheck F1 (prod _ _ Ft) ok,
105 (pi c1 c2 \ Ft c1 = Ft c2),!,
106 mk-app-prf [app [F1, A] | Args1] [app [F2, B] | Args2]
107 [{{app_prf lp:F1 lp:F2 lp:A lp:B lp:Pf lp:Pa}} | Ps] P.
108
109 pred fold-map2 i:list term i:A i:(pred i:term, i:A, o:term, o:term, o:A)
110 o:list term o:list term o:A.
111
112 fold-map2 [] A _ [] [] A.
113 fold-map2 [T | L] A F [T1 | L1] [P1 | PL] A2 :-
114 F T A T1 P1 A1, fold-map2 L A1 F L1 PL A2.
115
116 pred instantiate_pair i:name, i:term, i:term, i:pair argument argument,
117 o:pair argument argument.
118
119 pred instantiate i:name, i:term, i:term, i:argument, o:argument.
120
121 pred remove_one_unknown i:name, i:term, i:term, i:term, o:term.
122
123 % TODO : needs a fix in a rocq-elpi to detect if renaming has happened in
124 % in the current context.
125 remove_one_unknown N _T C (fun N1 _T1 F) Res :-
126 {coq.name->id N} = {coq.name->id N1},!,
127 Res = (F C),!.
128
129 remove_one_unknown N T C (fun N1 T1 F) (fun N1 T1 Res) :-
130 (@pi-decl N1 T1 x \
131 remove_one_unknown N T C (F x) (Res x)),!.
132
133 instantiate N T C (open-trm 1 F) (open-trm 0 Res) :-
134 remove_one_unknown N T C F Res,!.
135
136 instantiate N T C (open-trm I F) (open-trm J Res) :-
137 remove_one_unknown N T C F Res,!,
138 J is I - 1.
139
140 instantiate _N _T _C (open-trm 0 A) (open-trm 0 A):- !.
141
142 instantiate _N _T _C (open-trm _ _ as It) It :- !.
143
144 instantiate_pair N T C (pr A1 A2) (pr B1 B2) :-
145 std.do! [
146 std.assert! (instantiate N T C A1 B1) "first instantiate failed",
147 instantiate N T C A2 B2].
148
149 pred mk-equality i:(pair argument argument), i:term i:A, o:term, o:term, o:A.
150
151 mk-equality (pr (open-trm 0 S) (open-trm 0 T)) S A T P A :- !,
152 TY = {{lp:S = lp:T}},
153 coq.typecheck-ty TY _ ok,
154 coq.typecheck P TY ok.
155
156 :name "mk-equality:start"
157 mk-equality _RW X A Z Y A :- name X,!,
158 X = Z, {{@refl_equal _ lp:X}} = Y, !.
159
160 mk-equality _RW (global _ as C) A C {{@refl_equal _ lp:C}} A :- !.
161
162 mk-equality _RW (pglobal _ _ as C) A C {{@refl_equal _ lp:C}} A :- !.
163
164 mk-equality _RW (sort _ as C) A C {{@refl_equal _ lp:C}} A :- !.
165
166 mk-equality RW (fun N T F) A (fun N1 T F) Res A :-
167 fresh-name N T N1,
168 @pi-decl N T x\
169 (instantiate_pair N T x RW (RW1 x),
170 mk-equality (RW1 x) (F x) A _ {{@refl_equal _ _}} _A2,!,
171 Res = {{@refl_equal _ _}}).
172
173 mk-equality RW (fun N T F) A (fun N1 T F1)
174 {{functional_extensionality lp:{{(fun N T F)}} lp:{{(fun N T F1)}}
175 lp:{{(fun N1 T Prf)}}}} A1 :- !,
176 fresh-name N T N1,
177 @pi-decl N1 T x\
178 (std.assert! (instantiate_pair N1 T x RW (RW1 x)) "instantiate_pair failed",!,
179 mk-equality (RW1 x) (F x) A (F1 x) (Prf x) A1).
180
181 % TODO: write unit tests for the following clauses
182 mk-equality RW (let N T B F as C) A (let N1 T B F) {{@refl_equal _ lp:C}} A :-
183 mk-equality RW B A _ {{@refl_equal _ _}} A2,
184 fresh-name N T N1,
185 (@pi-decl N1 T x\
186 (std.assert! (instantiate_pair N1 T x RW (RW1 x)) "instantiate_pair failed",!,
187 mk-equality (RW1 x) (F x) A2 _ {{@refl_equal _ _}} _A3)),!.
188
189 mk-equality RW (let N T B F) A (let N1 T B1 F1)
190 {{let_congr lp:B lp:B1 lp:{{fun N1 T F}} lp:{{fun N1 T F1}}
191 lp:PB lp:PF}} A3:- !,
192 fresh-name N T N1,
193 mk-equality RW B A B1 PB A2,
194 @pi-decl N1 T x\
195 (instantiate_pair N1 T x RW (RW1 x),!,
196 mk-equality (RW1 x) (F x) A2 (F1 x) PF A3).
197
198 mk-equality RW (prod N T F) A (prod N1 T F1) (fun N1 T P1) A2 :- !,
199 fresh-name N T N1,
200 (@pi-decl N1 T x\
201 (instantiate_pair N1 T x RW (RW1 x),!,
202 mk-equality (RW1 x) (F x) A (F1 x) (P1 x) A2)),!.
203
204 mk-equality RW (app L as C) A C {{@refl_equal _ lp:C}} A :-
205 fold-map2 L A (mk-equality RW) _ L1 _A1,
206 std.forall L1 (c \ (sigma A B \ c = {{@refl_equal lp:A lp:B}})),!.
207
208 mk-equality RW {{@map lp:T1 lp:T2 lp:{{fun N _ F}} lp:L}}
209 A
210 {{@map lp:T1 lp:T2 lp:{{fun N1 T1 F1}} lp:L1}}
211 R A2 :- !,
212 fresh-name N T1 N1,
213 @pi-decl N1 T1 x\
214 (instantiate_pair N1 T1 x RW (RW1 x),!,
215 @pi-decl `H` (Ext_Hyp x) h\
216 mk-equality (RW1 x) (F x) A (F1 x) (Pf x h) A1),
217 mk-equality RW L A1 L1 Pl A2,
218 R = {{map_ext_in_equal2 lp:{{fun N1 _ F}} lp:{{fun N1 _ F1}} lp:L lp:L1
219 lp:{{fun N1 T1 x\
220 (fun `H` (Ext_Hyp x) h\ Pf x h)}} lp:Pl }}.
221
222 mk-equality RW (app L) A (app L1) Prf A1 :-
223 fold-map2 L A (mk-equality RW) L1 P1 A1,
224 mk-app-prf L L1 P1 Prf.
225
226 pred non-dependent-type i:term.
227
228 non-dependent-type F :-
229 coq.typecheck F Ty ok,
230 ndpt Ty.
231
232 pred ndpt i:term.
233
234 ndpt (prod _N _T F) :-
235 pi c1 c2\ F c1 = F c2,!,
236 pi c\ ndpt (F c).
237
238 ndpt (prod _ _ _) :- !, fail.
239
240 ndpt _ :- !.
241
242
243 pred equal-app i:list term, i:list term, i:list term, o:term.
244
245 equal-app [F, A] [F1, A1] [Pf, Pa]
246 {{app_prf lp:F lp:F1 lp:A lp:A1 lp:Pf lp:Pa}} :- !.
247
248 equal-app [F, A | Args] [F1, A1 | Args1] [Pf, Pa | Ps]
249 R :-
250 equal-app [app [F, A] | Args] [app [F1, A1] | Args1]
251 [{{app_prf lp:F lp:F1 lp:A lp:A1 lp:Pf lp:Pa}} | Ps] R.
252
253 mk-equality _RW (fix _N _Rno _Ty _F as C) A C {{@refl_equal _ lp:C}} A :- !.
254 mk-equality _RW (match _T _Rty _B as C) A C {{@refl_equal _ lp:C}} A :- !.
255 mk-equality _RW (primitive _ as C) A C {{@refl_equal _ lp:C}} A :- !.
256 mk-equality _RW (uvar _M _L as C) A C {{@refl_equal _ lp:C}} A :- !.
257 % when used in CHR rules
258 mk-equality _RW (uvar _X _L as C) A C {{@refl_equal _ lp:C}} A :- !.
259
260 solve (goal _ _ {{lp:X = lp:Y }} _ [Arg1, Arg2] as G) GL1 :-
261 %coq.say Arg1 Arg2,
262 mk-equality (pr Arg1 Arg2) Y [] Y2 P _,
263 std.assert-ok! (coq.typecheck P {{lp:Y = lp:Y2}}) "proof incorrect",!,
264 preserve_bound_variables X X1,
265 refine {{@eq_trans _ lp:X1 lp:Y2 _ _ (eq_sym lp:P)}} G GL,
266 if (GL = [Ng, Ng2])
267 (coq.ltac.open (g\gl\coq.ltac.call "lazy_beta" [] g gl ok) Ng2 GL_aux,
268 GL1 = [Ng | GL_aux])
269 (GL1 = GL).
270 % refine {{eq_sym (@eq_trans lp:Y lp:Y2 lp:X lp:P (eq_sym _))}} G GL.
271
272 solve (goal _ _ _ _ [Arg1, Arg2]) _ :-
273 coq.say Arg1,
274 coq.say Arg2,
275 fail.
276
277 solve (goal _ _ _ _ [] as _G) _GL :-
278 coq.say "failed".
279 }}.
280
281 Tactic Notation (at level 0) "repl" uconstr(x) uconstr(y) :=
282 (elpi replace ltac_open_term:(x) ltac_open_term:(y)).
283
284 Open Scope Z_scope. (* Otherwise ring fails. *)
285
286 (* The tactic repl only handles goals that have the shape of an
287 equality, and only attempts to perform rewrites in the right-hand-side of
288 that equality. In this example, we replace a formula containing a variable
289 that is not well defined outside the map expression with a formula that also
290 contains the such a variable. But a variable with the same name is
291 actually bound in the goal, and the expression to be replaced occurs inside
292 the lambda-expression.
293 The number of variables occuring in the replacement formulas that
294 are unknown at the root of the goals conclusion can be arbitrary big,
295 but they all have to be fillable with bound variables where the binding
296 happens in the goal, in a nested fashion. *)
297
298 (* This test illustrates the case where there is no unknown. *)
299 Goal forall x, x = 1 -> 2 = x + 1.
300 intros x x1.
301 repl x 1;[ | assumption].
302 ring.
303 Qed.
304
305 (* This test illustrates the case where there is one unknown in
306 both the expression-to-be-replaced and the replacing expression. *)
307 Goal forall l, map (fun x => x + 1) l = map (fun x => x + (1 + 0)) l.
308 Proof.
309 intros l.
310 repl (x + (1 + 0)) (x + 1); cycle 1.
311 ring.
312 easy.
313 Qed.
314
315 (* This test illustrates the case where there are two unknowns in
316 both the expression-to-be-replaced and the replacing expression. *)
317 Goal forall n,
318 map (fun i => map (fun j => (i + j)) (map Z.of_nat (seq 1 n)))
319 (map Z.of_nat (seq 1 n)) =
320 map (fun i => map (fun j => (j + i)) (map Z.of_nat (seq 1 n)))
321 (map Z.of_nat (seq 1 n)).
322 intros n.
323 repl (j + i) (i + j).
324 easy.
325 ring.
326 Qed.
327
328 (* This is a typical bigop theorem. The proof does not use our new
329 tactic. *)
330 Lemma fold_neutral {A B : Type}(f : A -> A -> A) (a0 : A) :
331 (forall x, f x a0 = x) ->
332 forall (l : list B), fold_right f a0 (map (fun x => a0) l) = a0.
333 Proof.
334 intros neutral; induction l as [ | b l Ih]; simpl; try rewrite Ih; auto.
335 Qed.
336
337 (* The next two tests illustrate the fact that the whole formula may contain
338 polymorphic functions. *)
339 (* This test illustrates the case where the formula to be replaced contains
340 an instance of the variable bound in a lambda expression occuring in the
341 right-hand-side of the equality, but the replacing formula does not. *)
342 Goal forall l, fold_right Z.add 0 (map (fun x => x - x) l) = 0.
343 Proof.
344 intros l.
345 symmetry.
346 (* TODO: this should not fail. *)
347 repl (x - x) 0; cycle 1.
348 ring.
349 symmetry.
350 apply (fold_neutral Z.add 0 Z.add_0_r).
351 Qed.
352
353 (* This test illustrates the case where the formula to be replaced contains
354 no instance of a bound variable, but the replacing formula does. *)
355 Goal forall (l : list Z), 0 = fold_right Z.add 0 (map (fun x => x - x + 0) l).
356 intros l.
357 repl 0 (x - x); cycle 1.
358 ring.
359 repl (x - x + (x - x)) 0; cycle 1.
360 ring.
361 symmetry.
362 apply fold_neutral.
363 intros x; ring.
364 Qed.
365
366 (* this illustrates the case where a bound variable is clashing with another
367 existing variable in the context, with the same name. *)
368 Goal forall (x : Z) l y, map (fun x => x + y) l = map (fun x => x + (y + 0)) l.
369 Proof.
370 intros x l y.
371 (* This illustrates that the names seen by the user are recognized properly
372 by the tactic.*)
373 repl (x0 + (y + 0)) (x0 + y).
374 progress repl (x0 + y) (y + x0).
375 repl (y + x0) (x0 + y).
376 easy.
377 all:ring.
378 Qed.
379
380 (* If there are several lambda expressions, one of which is not concerned
381 with the rewrite rule, it should not fail. *)
382
383 Goal forall l, l = map (fun x => x + 1) (map (fun y => y - 1) l).
384 Proof.
385 intros l.
386 repl(x + 1) (1 + x); cycle 1.
387 ring.
388 repl (y - 1) ((-1) + y); cycle 1.
389 ring.
390 rewrite map_map.
391 repl (1 + ((-1) + x)) x; cycle 1.
392 ring.
393 rewrite map_id.
394 easy.
395 Qed.
396
397 Section sandbox.
398
399 Variable x : Z.
400
401 (* This goal illustrates the case where a bound variable clashes with
402 a section variable. *)
403 Goal forall l y, map (fun x => x + y) l = map (fun x => x + (y + 0)) l.
404 Proof.
405 intros l y.
406 repl (x0 + (y + 0)) (x0 + y); cycle 1.
407 ring.
408 (* TODO: elpi generates an ugly name in a subterm of the goal that we did not
409 modify, when it could avoid it. *)
410 now apply map_ext_in; intros a _; ring.
411 Qed.
412
413 End sandbox.
414