Debian Codegraph @

src/Util/ListUtil/Forall.v

raw
1 From Coq Require Import Lia.
2 From Coq Require Import Morphisms.
3 From Coq Require Import List.
4 Require Import Crypto.Util.Tactics.SpecializeBy.
5 Require Import Crypto.Util.Tactics.SplitInContext.
6 Require Import Crypto.Util.Tactics.DestructHead.
7 Require Import Crypto.Util.Tactics.BreakMatch.
8 Require Import Crypto.Util.Option.
9
10 Definition Forallb {A} (P : A -> bool) (ls : list A) : bool
11 := List.fold_right andb true (List.map P ls).
12
13 Lemma unfold_Forallb {A P} ls
14 : @Forallb A P ls
15 = match ls with
16 | nil => true
17 | cons x xs => andb (P x) (Forallb P xs)
18 end.
19 Proof using Type. destruct ls; reflexivity. Qed.
20
21 Lemma Forall_Forallb_iff {A} (P : A -> bool) (Q : A -> Prop) (ls : list A)
22 (H : forall x, In x ls -> P x = true <-> Q x)
23 : Forallb P ls = true <-> Forall Q ls.
24 Proof using Type.
25 induction ls as [|x xs IHxs]; simpl; rewrite unfold_Forallb.
26 { intuition. }
27 { simpl in *.
28 rewrite Bool.andb_true_iff, IHxs
29 by (intros; apply H; eauto).
30 split; intro H'; inversion H'; subst; constructor; intuition;
31 apply H; eauto. }
32 Qed.
33
34 Local Ltac t_Forall2_step :=
35 first [ progress intros
36 | progress subst
37 | progress destruct_head'_and
38 | progress destruct_head'_ex
39 | progress specialize_by_assumption
40 | progress split_iff
41 | progress split_and
42 | apply conj
43 | progress cbn [List.map List.seq List.repeat List.rev List.firstn List.skipn List.length] in *
44 | exfalso; assumption
45 | solve [ eauto ]
46 | match goal with
47 | [ |- List.Forall2 _ _ _ ] => constructor
48 | [ |- List.Forall _ _ ] => constructor
49 | [ H : List.Forall2 _ nil (cons _ _) |- _ ] => inversion H
50 | [ H : List.Forall2 _ (cons _ _) nil |- _ ] => inversion H
51 | [ H : List.Forall2 _ (cons _ _) (cons _ _) |- _ ] => inversion H; clear H
52 | [ H : List.Forall2 _ (cons _ _) ?x |- _ ] => is_var x; inversion H; clear H
53 | [ H : List.Forall2 _ ?x (cons _ _) |- _ ] => is_var x; inversion H; clear H
54 | [ H : List.Forall2 _ nil ?x |- _ ] => is_var x; inversion H; clear H
55 | [ H : List.Forall2 _ ?x nil |- _ ] => is_var x; inversion H; clear H
56 | [ H : List.Forall _ (cons _ _) |- _ ] => inversion H; clear H
57 | [ H : List.Forall2 _ (List.app _ _) _ |- _ ] => apply Forall2_app_inv_l in H
58 | [ H : List.Forall2 _ _ (List.app _ _) |- _ ] => apply Forall2_app_inv_r in H
59 | [ H : nil = _ ++ _ |- _ ] => symmetry in H
60 | [ H : _ ++ _ = nil |- _ ] => apply app_eq_nil in H
61 | [ H : cons _ _ = nil |- _ ] => inversion H
62 | [ H : nil = cons _ _ |- _ ] => inversion H
63 | [ H : cons _ _ = cons _ _ |- _ ] => inversion H; clear H
64 | [ H : _ ++ _ :: nil = _ ++ _ :: nil |- _ ] => apply app_inj_tail in H
65 | [ |- List.Forall2 _ (_ ++ _ :: nil) (_ ++ _ :: nil) ] => apply Forall2_app
66 end ].
67 Local Ltac t_Forall2 := repeat t_Forall2_step.
68
69 Lemma Forall2_map_l_iff {A A' B R f ls1 ls2}
70 : @List.Forall2 A' B R (List.map f ls1) ls2
71 <-> @List.Forall2 A B (fun x y => R (f x) y) ls1 ls2.
72 Proof using Type.
73 revert ls2; induction ls1 as [|l ls IHls], ls2 as [|l' ls'];
74 t_Forall2.
75 Qed.
76 Lemma Forall2_map_r_iff {A B B' R f ls1 ls2}
77 : @List.Forall2 A B' R ls1 (List.map f ls2)
78 <-> @List.Forall2 A B (fun x y => R x (f y)) ls1 ls2.
79 Proof using Type.
80 revert ls2; induction ls1 as [|l ls IHls], ls2 as [|l' ls'];
81 t_Forall2.
82 Qed.
83 Lemma Forall2_map_map_iff {A A' B B' R f g ls1 ls2}
84 : @List.Forall2 A' B' R (List.map f ls1) (List.map g ls2)
85 <-> @List.Forall2 A B (fun x y => R (f x) (g y)) ls1 ls2.
86 Proof using Type.
87 revert ls2; induction ls1 as [|l ls IHls], ls2 as [|l' ls'];
88 t_Forall2.
89 Qed.
90
91 Lemma Forall2_flip_iff {A B P xs ys}
92 : @Forall2 A B P xs ys <-> Forall2 (Basics.flip P) ys xs.
93 Proof using Type. split; induction 1; constructor; assumption. Qed.
94
95 Lemma Forall2_Forall {A R ls}
96 : @List.Forall2 A A R ls ls
97 <-> @List.Forall A (Proper R) ls.
98 Proof using Type. induction ls as [|l ls IHls]; t_Forall2. Qed.
99
100 Lemma Forall_seq {R start len}
101 : List.Forall R (seq start len) <-> (forall x, (start <= x < start + len)%nat -> R x).
102 Proof using Type.
103 revert start; induction len as [|len IHlen]; intro start;
104 [ | specialize (IHlen (S start)) ].
105 all: repeat first [ t_Forall2_step
106 | lia
107 | exfalso; lia
108 | match goal with
109 | [ H : ?R ?x |- ?R ?y ]
110 => destruct (PeanoNat.Nat.eq_dec x y); [ subst; assumption | clear H ]
111 | [ H : _ |- _ ] => apply H; clear H
112 end ].
113 Qed.
114
115 Lemma Forall2_repeat_iff {A B R x y n m}
116 : @List.Forall2 A B R (List.repeat x n) (List.repeat y m)
117 <-> ((n <> 0%nat -> R x y) /\ n = m).
118 Proof using Type.
119 revert m; induction n as [|n IHn], m as [|m]; [ | | | specialize (IHn m) ];
120 t_Forall2; try congruence.
121 Qed.
122
123 Lemma Forall2_rev_iff {A B R ls1 ls2}
124 : @List.Forall2 A B R (List.rev ls1) (List.rev ls2)
125 <-> @List.Forall2 A B R ls1 ls2.
126 Proof using Type.
127 revert ls2; induction ls1 as [|l ls IHls], ls2 as [|l' ls'];
128 t_Forall2.
129 Qed.
130
131 Lemma Forall2_rev_lr_iff {A B R ls1 ls2}
132 : @List.Forall2 A B R (List.rev ls1) ls2 <-> @List.Forall2 A B R ls1 (List.rev ls2).
133 Proof using Type.
134 rewrite <- (rev_involutive ls2), Forall2_rev_iff, !rev_involutive; reflexivity.
135 Qed.
136
137 Lemma Forall2_firstn_skipn_iff n {A B R ls1 ls2}
138 : @List.Forall2 A B R ls1 ls2 <-> (List.Forall2 R (List.firstn n ls1) (List.firstn n ls2) /\ List.Forall2 R (List.skipn n ls1) (List.skipn n ls2)).
139 Proof. revert ls1 ls2; induction n, ls1, ls2; t_Forall2. Qed.
140
141 Lemma Forall2_firstn {A B R ls1 ls2 n}
142 : @List.Forall2 A B R ls1 ls2 -> @List.Forall2 A B R (List.firstn n ls1) (List.firstn n ls2).
143 Proof using Type. rewrite (Forall2_firstn_skipn_iff n); tauto. Qed.
144
145 Lemma Forall2_skipn {A B R ls1 ls2 n}
146 : @List.Forall2 A B R ls1 ls2 -> @List.Forall2 A B R (List.skipn n ls1) (List.skipn n ls2).
147 Proof using Type. rewrite (Forall2_firstn_skipn_iff n); tauto. Qed.
148
149 Lemma Forall_firstn_skipn_iff n {A R ls}
150 : @List.Forall A R ls <-> (List.Forall R (List.firstn n ls) /\ List.Forall R (List.skipn n ls)).
151 Proof. revert ls; induction n, ls; t_Forall2. Qed.
152
153 Lemma Forall_firstn {A R ls n}
154 : @List.Forall A R ls -> @List.Forall A R (List.firstn n ls).
155 Proof using Type. rewrite (Forall_firstn_skipn_iff n); tauto. Qed.
156
157 Lemma Forall_skipn {A R ls n}
158 : @List.Forall A R ls -> @List.Forall A R (List.skipn n ls).
159 Proof using Type. rewrite (Forall_firstn_skipn_iff n); tauto. Qed.
160
161 Lemma Forall2_app_l_iff {A B R ls1 ls2 ls}
162 : @List.Forall2 A B R (ls1 ++ ls2) ls <-> (List.Forall2 R ls1 (List.firstn (List.length ls1) ls) /\ List.Forall2 R ls2 (List.skipn (List.length ls1) ls)).
163 Proof. rewrite (Forall2_firstn_skipn_iff (List.length ls1)), firstn_app, skipn_app, firstn_all, skipn_all, PeanoNat.Nat.sub_diag, firstn_O, skipn_O, app_nil_l, app_nil_r; reflexivity. Qed.
164
165 Lemma Forall2_app_r_iff {A B R ls1 ls2 ls}
166 : @List.Forall2 A B R ls (ls1 ++ ls2) <-> (List.Forall2 R (List.firstn (List.length ls1) ls) ls1 /\ List.Forall2 R (List.skipn (List.length ls1) ls) ls2).
167 Proof. rewrite Forall2_flip_iff, Forall2_app_l_iff, <- !Forall2_flip_iff; reflexivity. Qed.
168
169 Lemma eq_length_Forall2 {A B R ls1 ls2}
170 : @List.Forall2 A B R ls1 ls2 -> List.length ls1 = List.length ls2.
171 Proof using Type.
172 revert ls2; induction ls1, ls2; t_Forall2.
173 Qed.
174
175 Lemma Forall2_combine {A B C D R1 R2 R3 ls1 ls2 ls3 ls4}
176 (HR : forall x y z w, (R1 x y : Prop) -> (R2 z w : Prop) -> (R3 (x, z) (y, w) : Prop))
177 : @List.Forall2 A B R1 ls1 ls2
178 -> @List.Forall2 C D R2 ls3 ls4
179 -> List.Forall2 R3 (List.combine ls1 ls3) (List.combine ls2 ls4).
180 Proof using Type.
181 revert ls2 ls3 ls4; induction ls1, ls2, ls3, ls4; t_Forall2.
182 Qed.
183
184 Lemma Forall_forall_iff_nth_error {A R ls}
185 : @Forall A R ls
186 <-> (forall i v, nth_error ls i = Some v -> R v).
187 Proof using Type.
188 rewrite Forall_forall.
189 split; eauto using nth_error_In.
190 intros ? ? H'; apply In_nth_error in H'.
191 firstorder auto.
192 Qed.
193
194 Lemma Forall_forall_iff_nth_error_match {A R ls}
195 : @Forall A R ls
196 <-> forall i, match nth_error ls i with Some v => R v | None => True end.
197 Proof using Type.
198 rewrite Forall_forall_iff_nth_error.
199 split; intros H i; try intro; specialize (H i); break_match; break_match_hyps; eauto; congruence.
200 Qed.
201
202 Lemma Forall2_forall_iff_nth_error {A B R xs ys}
203 : @Forall2 A B R xs ys
204 <-> forall i, option_eq R (nth_error xs i) (nth_error ys i).
205 Proof using Type.
206 revert ys; induction xs as [|x xs IHxs], ys as [|y ys];
207 [ | | | specialize (IHxs ys) ]; t_Forall2.
208 all: repeat first [ t_Forall2_step
209 | progress cbn [option_eq nth_error] in *
210 | progress inversion_option
211 | match goal with
212 | [ H : forall x, nth_error (cons _ _) x = None |- _ ] => specialize (H O)
213 | [ H : forall x, option_eq _ (nth_error (cons _ _) x) None |- _ ] => specialize (H O)
214 | [ |- context[nth_error (cons _ _) ?x] ] => is_var x; destruct x
215 | [ H : forall i, option_eq _ _ (nth_error (cons _ _) ?x) |- _ ]
216 => pose proof (H O);
217 pose proof (fun i => H (S i));
218 clear H
219 | [ H : forall i, option_eq _ (nth_error (cons _ _) ?x) _ |- _ ]
220 => pose proof (H O);
221 pose proof (fun i => H (S i));
222 clear H
223 | [ |- context[nth_error nil ?x] ] => is_var x; destruct x
224 end ].
225 Qed.
226
227 Lemma Forall2_forall_iff'' {A B R xs ys d1 d2}
228 : (@Forall2 A B R xs ys /\ R d1 d2)
229 <-> (length xs = length ys
230 /\ (forall i, R (nth_default d1 xs i) (nth_default d2 ys i))).
231 Proof using Type.
232 t_Forall2; cbv [nth_default] in *.
233 all: repeat first [ eapply eq_length_Forall2; eassumption
234 | rewrite Forall2_forall_iff_nth_error
235 | t_Forall2_step
236 | progress cbn [option_eq] in *
237 | progress inversion_option
238 | match goal with
239 | [ H : Forall2 _ _ _ |- _ ] => rewrite Forall2_forall_iff_nth_error in H
240 | [ H : forall i : nat, _ |- context[nth_error _ ?n] ]
241 => specialize (H n)
242 | [ H' : List.length ?ls = _, H : forall i : nat, _ |- _ ]
243 => specialize (H (List.length ls)); rewrite ?nth_error_length_error in H by lia
244 end
245 | break_innermost_match_step
246 | break_innermost_match_hyps_step
247 | match goal with
248 | [ H : nth_error _ _ = None |- _ ] => rewrite nth_error_None in H
249 | [ H : nth_error ?l ?n = Some _ |- _ ]
250 => assert (n < List.length l) by (apply nth_error_Some; congruence);
251 clear H
252 end
253 | lia ].
254 Qed.
255
256 Lemma Forall2_forall_In_combine_iff {A B} R xs ys
257 : @List.Forall2 A B R xs ys
258 <-> (List.length xs = List.length ys
259 /\ forall x y, List.In (x, y) (List.combine xs ys) -> R x y).
260 Proof using Type.
261 split; revert ys; induction xs as [|x xs IHxs], ys as [|y ys]; cbn in *; intro H.
262 all: inversion_clear H; split_and;
263 try solve [ repeat constructor; intuition (congruence + eauto) ].
264 Qed.
265
266 Global Instance Forall2_Proper_impl {A B}
267 : Proper (pointwise_relation _ (pointwise_relation _ Basics.impl) ==> eq ==> eq ==> Basics.impl)
268 (@List.Forall2 A B) | 10.
269 Proof using Type.
270 cbv [Basics.impl respectful pointwise_relation].
271 repeat intro; subst; rewrite Forall2_forall_In_combine_iff in *.
272 destruct_head'_and; split; eauto.
273 Qed.
274
275 Global Instance Forall2_Proper_iff {A B}
276 : Proper (pointwise_relation _ (pointwise_relation _ iff) ==> eq ==> eq ==> iff)
277 (@List.Forall2 A B) | 10.
278 Proof using Type.
279 cbv [respectful pointwise_relation].
280 repeat intro; subst; rewrite !Forall2_forall_In_combine_iff in *.
281 split_iff.
282 repeat (split || destruct_head'_and); eauto.
283 Qed.
284
285 Lemma Forall2_Forall_iff_ignore_r {A B P ls1 ls2}
286 : @Forall2 A B (fun _ => P) ls1 ls2 <-> (length ls1 = length ls2 /\ Forall P ls2).
287 Proof using Type.
288 revert ls1 ls2; induction ls1, ls2; t_Forall2; exfalso; lia.
289 Qed.
290
291 Lemma Forall2_Forall_ignore_l {A B P ls1 ls2}
292 : @Forall2 A B (fun x _ => P x) ls1 ls2 <-> (length ls1 = length ls2 /\ Forall P ls1).
293 Proof using Type. now rewrite Forall2_flip_iff; cbv [Basics.flip]; rewrite Forall2_Forall_iff_ignore_r. Qed.
294
295 Lemma Forall2_flip {A B} {R : A -> B -> Prop} xs ys :
296 Forall2 R xs ys -> Forall2 (fun y x => R x y) ys xs.
297 Proof using Type. induction 1; eauto. Qed.
298
299 Lemma length_Forall2 {A B : Type} {xs ys} {P:A->B->Prop} : Forall2 P xs ys -> length xs = length ys.
300 Proof using Type. induction 1; cbn; eauto. Qed.
301
302 Section weaken.
303 Context {A B} {P Q:A->B->Prop} (H : forall (a : A) (b : B), P a b -> Q a b).
304 Fixpoint Forall2_weaken args args' (pf : Forall2 P args args') : Forall2 Q args args' :=
305 match pf with
306 | Forall2_nil => Forall2_nil _
307 | Forall2_cons _ _ _ _ Rxy k => Forall2_cons _ _ (H _ _ Rxy) (Forall2_weaken _ _ k)
308 end.
309 End weaken.
310 Lemma Forall2_map_l {A' A B} (f : A' -> A) {R : A -> B -> Prop} (xs : list A') (ys : list B) :
311 Forall2 R (List.map f xs) ys <-> Forall2 (fun x y => R (f x) y) xs ys.
312 Proof using Type.
313 remember (List.map f xs) as fxs eqn:Heqn.
314 split; intros H; (revert fxs Heqn + revert xs Heqn); induction H; intros ? Heqn;
315 try destruct xs; cbn in *; inversion Heqn; clear Heqn; subst;
316 try congruence; eauto.
317 Qed.
318 Import RelationClasses.
319 Lemma Reflexive_forall2 {A} {R} : @Reflexive A R -> Reflexive (Forall2 R).
320 Proof using Type. intros ? l; induction l; eauto. Qed.
321 Global Hint Extern 1 (Reflexive (Forall2 _)) => simple apply @Reflexive_forall2 : typeclass_instances.
322 Lemma Forall2_eq {A} (xs ys : list A) : Forall2 eq xs ys <-> xs = ys.
323 Proof using Type. split; induction 1; subst; eauto; reflexivity. Qed.
324 Lemma Forall2_trans {A B C} {AB BC} {xs : list A} {ys : list B} :
325 Forall2 AB xs ys -> forall {zs : list C}, Forall2 BC ys zs ->
326 Forall2 (fun x z => exists y, AB x y /\ BC y z) xs zs.
327 Proof using Type. induction 1; inversion 1; subst; econstructor; eauto. Qed.
328
329 Lemma Forall2_nil_nil_iff A B P
330 : @Forall2 A B P nil nil <-> True.
331 Proof using Type. split; eauto. Qed.
332 Lemma Forall2_nil_cons_iff A B P x y
333 : @Forall2 A B P nil (cons x y) <-> False.
334 Proof using Type. split; firstorder inversion_one_head Forall2. Qed.
335 Lemma Forall2_nil_l_iff A B P ls
336 : @Forall2 A B P nil ls <-> ls = nil.
337 Proof using Type. split; firstorder (try inversion_one_head Forall2; subst; auto). Qed.
338 Lemma Forall2_nil_r_iff A B P ls
339 : @Forall2 A B P ls nil <-> ls = nil.
340 Proof using Type. split; firstorder (try inversion_one_head Forall2; subst; auto). Qed.
341 Lemma Forall2_cons_nil_iff A B P x y
342 : @Forall2 A B P (cons x y) nil <-> False.
343 Proof using Type. split; firstorder inversion_one_head Forall2. Qed.
344 Lemma Forall2_cons_cons_iff A B P x xs y ys
345 : @Forall2 A B P (cons x xs) (cons y ys) <-> (P x y /\ Forall2 P xs ys).
346 Proof using Type. split; firstorder (inversion_one_head Forall2; auto). Qed.
347 Lemma Forall2_cons_l_ex_iff A B P x xs ls
348 : @Forall2 A B P (cons x xs) ls <-> (exists y ys, ls = cons y ys /\ P x y /\ Forall2 P xs ys).
349 Proof using Type. split; firstorder (inversion_one_head Forall2; eauto). Qed.
350 Lemma Forall2_cons_r_ex_iff A B P x xs ls
351 : @Forall2 A B P ls (cons x xs) <-> (exists y ys, ls = cons y ys /\ P y x /\ Forall2 P ys xs).
352 Proof using Type. split; firstorder (inversion_one_head Forall2; eauto). Qed.
353
354 Lemma pull_ex_Forall_iff {A B} {R : A -> B -> Prop} {ls}
355 : Forall (fun a => ex (R a)) ls <-> exists ls', Forall2 R ls ls'.
356 Proof using Type.
357 induction ls as [|x xs IH];
358 rewrite ?Forall_nil_iff, ?Forall_cons_iff, ?IH;
359 try setoid_rewrite Forall2_nil_l_iff;
360 try setoid_rewrite Forall2_cons_l_ex_iff;
361 repeat firstorder (try inversion_one_head Forall; eauto 7).
362 Qed.
363
364 Lemma pull_ex_Forall2_l_iff {A B C} {R : A -> B -> C -> Prop} {ls1 ls2}
365 : Forall2 (fun a b => ex (R a b)) ls1 ls2 <-> exists ls3, List.length ls1 = List.length ls3 /\ Forall2 (fun ac b => R (fst ac) b (snd ac)) (List.combine ls1 ls3) ls2.
366 Proof using Type.
367 revert ls2; induction ls1 as [|x xs IH], ls2 as [|y ys]; try specialize (IH ys).
368 all: rewrite ?Forall2_nil_nil_iff, ?Forall2_nil_cons_iff, ?Forall2_cons_nil_iff, ?Forall2_cons_cons_iff, ?IH.
369 all: try setoid_rewrite Forall2_nil_r_iff.
370 all: try setoid_rewrite Forall2_nil_l_iff.
371 all: try setoid_rewrite Forall2_cons_r_ex_iff.
372 all: repeat first [ progress cbn [List.length List.combine] in *
373 | break_innermost_match_hyps_step
374 | progress subst
375 | solve [ exists nil; eauto ]
376 | match goal with
377 | [ H : S _ = S _ |- _ ] => inversion H; clear H
378 | [ H : _ :: _ = _ :: _ |- _ ] => inversion H; clear H
379 end
380 | progress firstorder first [ congruence | eauto 7 ]
381 | eexists (_ :: _) ].
382 Qed.
383
384 Lemma pull_ex_Forall2_r_iff {A B C} {R : A -> B -> C -> Prop} {ls1 ls2}
385 : Forall2 (fun a b => ex (R a b)) ls1 ls2 <-> exists ls3, List.length ls2 = List.length ls3 /\ Forall2 (fun a bc => R a (fst bc) (snd bc)) ls1 (List.combine ls2 ls3).
386 Proof using Type.
387 setoid_rewrite Forall2_flip_iff; cbv [Basics.flip].
388 rewrite pull_ex_Forall2_l_iff; reflexivity.
389 Qed.
390
391 Lemma Forall2_concat_l_ex_iff A B P xs ls
392 : @Forall2 A B P (concat xs) ls <-> (exists ys, ls = concat ys /\ Forall2 (Forall2 P) xs ys).
393 Proof using Type.
394 revert ls; induction xs as [|x xs IH]; cbn [concat]; intros.
395 all: rewrite ?Forall2_nil_l_iff, ?Forall2_nil_r_iff.
396 all: split; intros; firstorder (subst; auto).
397 all: rewrite ?Forall2_nil_l_iff, ?Forall2_nil_r_iff in *; subst; cbn [concat].
398 all: try solve [ reflexivity
399 | exists nil; cbn [concat]; eauto ].
400 all: repeat first [ progress subst
401 | progress cbn [concat]
402 | assumption
403 | reflexivity
404 | match goal with
405 | [ H : ex _ |- _ ] => destruct H
406 | [ H : and _ _ |- _ ] => destruct H
407 | [ H : Forall2 _ (_ ++ _) _ |- _ ] => apply Forall2_app_inv_l in H
408 | [ H : Forall2 _ (_ :: _) _ |- _ ] => rewrite Forall2_cons_l_ex_iff in H
409 end
410 | apply Forall2_app
411 | rewrite IH; eauto
412 | progress rewrite IH in *
413 | eexists (_ :: _); split; cbn [concat]; [ | constructor; eassumption ] ].
414 Qed.
415
416 Lemma Forall2_concat_r_ex_iff A B P ls xs
417 : @Forall2 A B P ls (concat xs) <-> (exists ys, ls = concat ys /\ Forall2 (Forall2 P) ys xs).
418 Proof using Type.
419 split; intro H; [ apply Forall2_flip, Forall2_concat_l_ex_iff in H | apply Forall2_flip, Forall2_concat_l_ex_iff ].
420 all: destruct H as [ys H]; exists ys; firstorder (subst; auto).
421 all: apply Forall2_flip; (eapply Forall2_weaken; [ | eassumption ]).
422 all: apply Forall2_flip.
423 Qed.
424
425 Lemma Forall2_concat_concat A B P xs ys
426 : Forall2 (Forall2 P) xs ys -> @Forall2 A B P (concat xs) (concat ys).
427 Proof using Type.
428 rewrite Forall2_concat_l_ex_iff; intros; repeat esplit; assumption.
429 Qed.