Debian Codegraph @

test-suite/ssr/under.v

raw
1 Require Import ssreflect.
2 Require Import ssrbool TestSuite.ssr_mini_mathcomp.
3 Global Unset SsrOldRewriteGoalsOrder.
4
5 (* under <names>: {occs}[patt]<lemma>.
6 under <names>: {occs}[patt]<lemma> by tac1.
7 under <names>: {occs}[patt]<lemma> by [tac1 | ...].
8 *)
9
10 Set Implicit Arguments.
11 Unset Strict Implicit.
12 Unset Printing Implicit Defensive.
13
14 Axiom daemon : False. Ltac myadmit := case: daemon.
15
16 Module Mocks.
17
18 (* Mock bigop.v definitions to test the behavior of under with bigops
19 without requiring mathcomp *)
20
21 Definition eqfun :=
22 fun (A B : Type) (f g : forall _ : B, A) => forall x : B, @eq A (f x) (g x).
23
24 Section Defix.
25 Variables (T : Type) (n : nat) (f : forall _ : T, T) (x : T).
26 Fixpoint loop (m : nat) : T :=
27 match m return T with
28 | O => x
29 | S i => f (loop i)
30 end.
31 Definition iter := loop n.
32 End Defix.
33
34 Parameter eq_bigl :
35 forall (R : Type) (idx : R) (op : forall (_ : R) (_ : R), R) (I : Type)
36 (r : list I) (P1 P2 : pred I) (F : forall _ : I, R) (_ : @eqfun bool I P1 P2),
37 @eq R (@bigop R I idx r (fun i : I => @BigBody R I i op (P1 i) (F i)))
38 (@bigop R I idx r (fun i : I => @BigBody R I i op (P2 i) (F i))).
39
40 Parameter eq_big :
41 forall (R : Type) (idx : R) (op : forall (_ : R) (_ : R), R) (I : Type)
42 (r : list I) (P1 P2 : pred I) (F1 F2 : forall _ : I, R) (_ : @eqfun bool I P1 P2)
43 (_ : forall (i : I) (_ : is_true (P1 i)), @eq R (F1 i) (F2 i)),
44 @eq R (@bigop R I idx r (fun i : I => @BigBody R I i op (P1 i) (F1 i)))
45 (@bigop R I idx r (fun i : I => @BigBody R I i op (P2 i) (F2 i))).
46
47 Parameter eq_bigr :
48 forall (R : Type) (idx : R) (op : forall (_ : R) (_ : R), R) (I : Type)
49 (r : list I) (P : pred I) (F1 F2 : forall _ : I, R)
50 (_ : forall (i : I) (_ : is_true (P i)), @eq R (F1 i) (F2 i)),
51 @eq R (@bigop R I idx r (fun i : I => @BigBody R I i op (P i) (F1 i)))
52 (@bigop R I idx r (fun i : I => @BigBody R I i op (P i) (F2 i))).
53
54 Parameter big_const_nat :
55 forall (R : Type) (idx : R) (op : forall (_ : R) (_ : R), R) (m n : nat) (x : R),
56 @eq R (@bigop R nat idx (index_iota m n) (fun i : nat => @BigBody R nat i op true x))
57 (@iter R (subn n m) (op x) idx).
58
59 Delimit Scope N_scope with num.
60 Delimit Scope nat_scope with N.
61
62 Reserved Notation "\sum_ ( m <= i < n | P ) F"
63 (at level 41, F at level 41, i, m, n at level 50,
64 format "'[' \sum_ ( m <= i < n | P ) '/ ' F ']'").
65 Reserved Notation "\sum_ ( m <= i < n ) F"
66 (at level 41, F at level 41, i, m, n at level 50,
67 format "'[' \sum_ ( m <= i < n ) '/ ' F ']'").
68
69 Local Notation "+%N" := addn (at level 0, only parsing).
70
71 Notation "\sum_ ( m <= i < n | P ) F" :=
72 (\big[+%N/0%N]_(m <= i < n | P%B) F%N) : (*nat_scope*) big_scope.
73 Notation "\sum_ ( m <= i < n ) F" :=
74 (\big[+%N/0%N]_(m <= i < n) F%N) : (*nat_scope*) big_scope.
75
76 Parameter iter_addn_0 : forall m n : nat, @eq nat (@iter nat n (addn m) O) (muln m n).
77
78 End Mocks.
79
80 Import Mocks.
81
82 (*****************************************************************************)
83
84 Lemma test_big_nested_1 (F G : nat -> nat) (m n : nat) :
85 \sum_(0 <= i < m) \sum_(0 <= j < n | odd (j * 1)) (i + j) =
86 \sum_(0 <= i < m) \sum_(0 <= j < n | odd j) (j + i).
87 Proof.
88 (* in interactive mode *)
89 under eq_bigr => i Hi.
90 under eq_big => [j|j Hj].
91 { rewrite muln1. over. }
92 { rewrite addnC. over. }
93 simpl. (* or: cbv beta. *)
94 over.
95 by [].
96 Qed.
97
98 Lemma test_big_nested_2 (F G : nat -> nat) (m n : nat) :
99 \sum_(0 <= i < m) \sum_(0 <= j < n | odd (j * 1)) (i + j) =
100 \sum_(0 <= i < m) \sum_(0 <= j < n | odd j) (j + i).
101 Proof.
102 (* in one-liner mode *)
103 under eq_bigr => i Hi do under eq_big => [j|j Hj] do [rewrite muln1 | rewrite addnC ].
104 done.
105 Qed.
106
107 Lemma test_big_2cond_0intro (F : nat -> nat) (m : nat) :
108 \sum_(0 <= i < m | odd (i + 1)) (i + 2) >= 0.
109 Proof.
110 (* in interactive mode *)
111 under eq_big.
112 { move=> n; rewrite (addnC n 1); over. }
113 { move=> i Hi; rewrite (addnC i 2); over. }
114 done.
115 Qed.
116
117 Lemma test_big_2cond_1intro (F : nat -> nat) (m : nat) :
118 \sum_(0 <= i < m | odd (i + 1)) (i + 2) >= 0.
119 Proof.
120 (* in interactive mode *)
121 Fail under eq_big => i.
122 (* as it amounts to [under eq_big => [i]] *)
123 Abort.
124
125 Lemma test_big_2cond_all (F : nat -> nat) (m : nat) :
126 \sum_(0 <= i < m | odd (i + 1)) (i + 2) >= 0.
127 Proof.
128 (* in interactive mode *)
129 Fail under eq_big => *.
130 (* as it amounts to [under eq_big => [*]] *)
131 Abort.
132
133 Lemma test_big_2cond_all_implied (F : nat -> nat) (m : nat) :
134 \sum_(0 <= i < m | odd (i + 1)) (i + 2) >= 0.
135 Proof.
136 (* in one-liner mode *)
137 under eq_big do [rewrite addnC
138 |rewrite addnC].
139 (* amounts to [under eq_big => [*|*] do [...|...]] *)
140 done.
141 Qed.
142
143 Lemma test_big_patt1 (F G : nat -> nat) (n : nat) :
144 \sum_(0 <= i < n) (F i + G i) = \sum_(0 <= i < n) (G i + F i) + 0.
145 Proof.
146 under [in RHS]eq_bigr => i Hi.
147 by rewrite addnC over.
148 done.
149 Qed.
150
151 Lemma test_big_patt2 (F G : nat -> nat) (n : nat) :
152 \sum_(0 <= i < n) (F i + F i) =
153 \sum_(0 <= i < n) 0 + \sum_(0 <= i < n) (F i * 2).
154 Proof.
155 under [X in _ = _ + X]eq_bigr => i Hi do rewrite mulnS muln1.
156 by rewrite big_const_nat iter_addn_0.
157 Qed.
158
159 Lemma test_big_occs (F G : nat -> nat) (n : nat) :
160 \sum_(0 <= i < n) (i * 0) = \sum_(0 <= i < n) (i * 0) + \sum_(0 <= i < n) (i * 0).
161 Proof.
162 under {2}[in RHS]eq_bigr => i Hi do rewrite muln0.
163 by rewrite big_const_nat iter_addn_0 mul0n addn0.
164 Qed.
165
166 Lemma test_big_occs_inH (F G : nat -> nat) (n : nat) :
167 \sum_(0 <= i < n) (i * 0) = \sum_(0 <= i < n) (i * 0) + \sum_(0 <= i < n) (i * 0) -> True.
168 Proof.
169 move=> H.
170 do [under {2}[in RHS]eq_bigr => i Hi do rewrite muln0] in H.
171 by rewrite big_const_nat iter_addn_0 mul0n addn0 in H.
172 Qed.
173
174 (* Solely used, one such renaming is useless in practice, but it works anyway *)
175 Lemma test_big_cosmetic (F G : nat -> nat) (m n : nat) :
176 \sum_(0 <= i < m) \sum_(0 <= j < n | odd (j * 1)) (i + j) =
177 \sum_(0 <= i < m) \sum_(0 <= j < n | odd j) (j + i).
178 Proof.
179 under [RHS]eq_bigr => a A do under eq_bigr => b B do []. (* renaming bound vars *)
180 myadmit.
181 Qed.
182
183 Lemma test_big_andb (F : nat -> nat) (m n : nat) :
184 \sum_(0 <= i < 5 | odd i && (i == 1)) i = 1.
185 Proof.
186 under eq_bigl => i do [rewrite andb_idl; first by move/eqP->].
187 under eq_bigr => i do move/eqP=>{1}->. (* the 2nd occ should not be touched *)
188 myadmit.
189 Qed.
190
191 Lemma test_foo (f1 f2 : nat -> nat) (f_eq : forall n, f1 n = f2 n)
192 (G : (nat -> nat) -> nat)
193 (Lem : forall f1 f2 : nat -> nat,
194 True ->
195 (forall n, f1 n = f2 n) ->
196 False = False ->
197 G f1 = G f2) :
198 G f1 = G f2.
199 Proof.
200 (*
201 under x: Lem.
202 - done.
203 - rewrite f_eq; over.
204 - done.
205 *)
206 under Lem => [|x|] do [done|rewrite f_eq|done].
207 done.
208 Qed.
209
210
211 (* Inspired From Coquelicot.Lub. *)
212 (* http://coquelicot.saclay.inria.fr/html/Coquelicot.Lub.html#Lub_Rbar_eqset *)
213
214 Parameters (R Rbar : Set) (R0 : R) (Rbar0 : Rbar).
215 Parameter Rbar_le : Rbar -> Rbar -> Prop.
216 Parameter Lub_Rbar : (R -> Prop) -> Rbar.
217 Parameter Lub_Rbar_eqset :
218 forall E1 E2 : R -> Prop,
219 (forall x : R, E1 x <-> E2 x) ->
220 Lub_Rbar E1 = Lub_Rbar E2.
221
222 Lemma test_Lub_Rbar (E : R -> Prop) :
223 Rbar_le Rbar0 (Lub_Rbar (fun x => x = R0 \/ E x)).
224 Proof.
225 under Lub_Rbar_eqset => r.
226 by rewrite over.
227 Abort.
228
229 Lemma ex_iff R (P1 P2 : R -> Prop) :
230 (forall x : R, P1 x <-> P2 x) -> ((exists x, P1 x) <-> (exists x, P2 x)).
231 Proof.
232 by move=> H; split; move=> [x Hx]; exists x; apply H.
233 Qed.
234
235 Arguments ex_iff [R P1] P2 iffP12.
236
237 (** Load the [setoid_rewrite] machinery *)
238 Require Setoid.
239
240 (** Replay the tactics from [test_Lub_Rbar] in this new environment *)
241 Lemma test_Lub_Rbar_again (E : R -> Prop) :
242 Rbar_le Rbar0 (Lub_Rbar (fun x => x = R0 \/ E x)).
243 Proof.
244 under Lub_Rbar_eqset => r.
245 by rewrite over.
246 Abort.
247
248 Lemma test_ex_iff (P : nat -> Prop) : (exists x, P x) -> True.
249 under ex_iff => n. (* this requires [Setoid] *)
250 by rewrite over.
251 by move=> _.
252 Qed.
253
254 Section TestGeneric.
255 Context {A B : Type} {R : nat -> B -> B -> Prop}
256 `{!forall n : nat, RelationClasses.Equivalence (R n)}.
257 Variables (F : (A -> A -> B) -> B).
258 Hypothesis ex_gen : forall (n : nat) (P1 P2 : A -> A -> B),
259 (forall x y : A, R n (P1 x y) (P2 x y)) -> (R n (F P1) (F P2)).
260 Arguments ex_gen [n P1] P2 _.
261 Lemma test_ex_gen (P1 P2 : A -> A -> B) (n : nat) :
262 (forall x y : A, P2 x y = P2 y x) ->
263 R n (F P1) (F P2) /\ True -> True.
264 Proof.
265 move=> P2C.
266 under [X in R _ _ X]ex_gen => a b.
267 by rewrite P2C over.
268 by move => _.
269 Qed.
270 End TestGeneric.
271
272 Import Setoid.
273 (* to expose [Coq.Relations.Relation_Definitions.reflexive],
274 [Coq.Classes.RelationClasses.RewriteRelation], and so on. *)
275
276 Section TestGeneric2.
277 (* Some toy abstract example with a parameterized setoid type *)
278 Record Setoid (m n : nat) : Type :=
279 { car : Type
280 ; Rel : car -> car -> Prop
281 ; refl : reflexive _ Rel
282 ; sym : symmetric _ Rel
283 ; trans : transitive _ Rel
284 }.
285
286 Context {m n : nat}.
287 Add Parametric Relation (s : Setoid m n) : (car s) (@Rel _ _ s)
288 reflexivity proved by (@refl _ _ s)
289 symmetry proved by (@sym _ _ s)
290 transitivity proved by (@trans _ _ s)
291 as eq_rel.
292
293 Context {A : Type} {s1 s2 : Setoid m n}.
294
295 Let B := @car m n s1.
296 Let C := @car m n s2.
297 Variable (F : C -> (A -> A -> B) -> C).
298 Hypothesis rel2_gen :
299 forall (c1 c2 : C) (P1 P2 : A -> A -> B),
300 Rel c1 c2 ->
301 (forall a b : A, Rel (P1 a b) (P2 a b)) ->
302 Rel (F c1 P1) (F c2 P2).
303 Arguments rel2_gen [c1] c2 [P1] P2 _ _.
304 Lemma test_rel2_gen (c : C) (P : A -> A -> B)
305 (toy_hyp : forall a b, P a b = P b a) :
306 Rel (F c P) (F c (fun a b => P b a)).
307 Proof.
308 under [here in Rel _ here]rel2_gen.
309 - over.
310 - by move=> a b; rewrite toy_hyp over.
311 - reflexivity.
312 Qed.
313 End TestGeneric2.
314
315 Section TestPreOrder.
316 (* inspired by https://github.com/rocq-prover/rocq/pull/10022#issuecomment-530101950 *)
317
318 Require Import Morphisms.
319
320 (** Tip to tell rewrite that the LHS of [leq' x y (:= leq x y = true)]
321 is x, not [leq x y] *)
322 Definition rel_true {T} (R : rel T) x y := is_true (R x y).
323 Definition leq' : nat -> nat -> Prop := rel_true leq.
324
325 Parameter leq_add :
326 forall m1 m2 n1 n2 : nat, m1 <= n1 -> m2 <= n2 -> m1 + m2 <= n1 + n2.
327 Parameter leq_mul :
328 forall m1 m2 n1 n2 : nat, m1 <= n1 -> m2 <= n2 -> m1 * m2 <= n1 * n2.
329
330 Local Notation "+%N" := addn (at level 0, only parsing).
331
332 (** Context lemma *)
333 Lemma leq'_big : forall I (F G : I -> nat) (r : seq I),
334 (forall i : I, leq' (F i) (G i)) ->
335 (leq' (\big[+%N/0%N]_(i <- r) F i) (\big[+%N/0%N]_(i <- r) G i)).
336 Proof.
337 red=> F G m n HFG.
338 apply: (big_ind2 leq _ _ (P := xpredT) (op1 := addn) (op2 := addn)) =>//.
339 move=> *; exact: leq_add.
340 move=> *; exact: HFG.
341 Qed.
342
343 (** Instances for [setoid_rewrite] *)
344 Instance leq'_rr : RewriteRelation leq' := {}.
345
346 Instance leq'_proper_addn : Proper (leq' ==> leq' ==> leq') addn.
347 Proof. move=> a1 b1 le1 a2 b2 le2; exact/leq_add. Qed.
348
349 Instance leq'_proper_muln : Proper (leq' ==> leq' ==> leq') muln.
350 Proof. move=> a1 b1 le1 a2 b2 le2; exact/leq_mul. Qed.
351
352
353 Instance leq'_preorder : PreOrder leq'.
354 (** encompasses [Reflexive] *)
355 Proof. rewrite /leq' /rel_true; split =>// ??? A B; exact: leq_trans A B. Qed.
356
357 Instance leq'_reflexive : Reflexive leq'.
358 Proof. by rewrite /leq' /rel_true. Qed.
359
360 Parameter leq_add2l :
361 forall p m n : nat, (p + m <= p + n) = (m <= n).
362
363 Lemma test : forall n : nat,
364 (1 + 2 * (\big[+%N/0]_(i < n) (3 + i)) * 4 + 5 <= 6 + 24 * n + 8 * n * n)%N.
365 Proof.
366 move=> n; rewrite -[is_true _]/(rel_true _ _ _) -/leq'.
367 have lem : forall (i : nat), i < n -> leq' (3 + i) (3 + n).
368 { by move=> i Hi; rewrite /leq' /rel_true leq_add2l; apply/ltnW. }
369
370 under leq'_big => i.
371 {
372 rewrite UnderE.
373
374 (* instantiate the evar with the bound "3 + n" *)
375 apply: lem; exact: ltn_ord.
376 }
377 cbv beta.
378
379 now_show (leq' (1 + 2 * \big[+%N/0]_(i < n) (3 + n) * 4 + 5) (6 + 24 * n + 8 * n * n)).
380 (* uninteresting end of proof, omitted *)
381 Abort.
382
383 End TestPreOrder.