Debian Codegraph @

src/Util/ListUtil/SetoidListFlatMap.v

raw
1 From Coq Require Import List.
2 From Coq Require Import Setoid.
3 From Coq Require Import SetoidList.
4 Require Import Crypto.Util.Tactics.DestructHead.
5 Require Import Crypto.Util.Tactics.SpecializeBy.
6 Require Import Crypto.Util.Tactics.SpecializeUnderBindersBy.
7 Require Import Crypto.Util.ListUtil.StdlibCompat.
8
9 Import ListNotations.
10
11 Lemma NoDupA_flat_map {B A} {eqB : relation B} {eqA : relation A}
12 {eqvA : Equivalence eqA}
13 {rB : Reflexive eqB}
14 (f : B -> list A) (ls : list B)
15 : Forall (NoDupA eqA) (List.map f ls)
16 -> NoDupA eqB ls
17 -> (forall x y, ~eqB x y -> InA eqB x ls -> InA eqB y ls -> forall x', InA eqA x' (f x) -> InA eqA x' (f y) -> False)
18 -> NoDupA eqA (flat_map f ls).
19 Proof.
20 rewrite Forall_map.
21 induction 1; inversion 1; subst; cbn [flat_map]; intro H'; [ constructor | ].
22 apply NoDupA_app; eauto; [].
23 invlist NoDupA.
24 intros.
25 rewrite ?InA_alt in *.
26 cbv [not] in *.
27 specialize_under_binders_by rewrite InA_alt.
28 specialize_under_binders_by eapply ex_intro.
29 specialize_under_binders_by apply conj.
30 destruct_head'_ex.
31 destruct_head'_and.
32 rewrite in_flat_map in *.
33 destruct_head'_ex.
34 destruct_head'_and.
35 cbn [In] in *.
36 eauto.
37 Qed.
38
39 Lemma SortA_flat_map {B A} {eqB ltB : relation B} {eqA ltA : relation A}
40 {eqvA : Equivalence eqA}
41 {rB : Reflexive eqB}
42 {rlB : Transitive ltB}
43 (f : B -> list A) (ls : list B)
44 : Forall (Sorted ltA) (List.map f ls)
45 -> Sorted ltB ls
46 -> (forall x y, ltB x y -> InA eqB x ls -> InA eqB y ls -> forall x' y', InA eqA x' (f x) -> InA eqA y' (f y) -> ltA x' y')
47 -> Sorted ltA (flat_map f ls).
48 Proof.
49 rewrite Forall_map.
50 induction 1; inversion 1; subst; cbn [flat_map]; intro H'; [ constructor | ].
51 eapply SortA_app; eauto; [].
52 invlist Sorted.
53 intros.
54 rewrite ?InA_alt in *.
55 cbv [not] in *.
56 specialize_under_binders_by rewrite InA_alt.
57 specialize_under_binders_by eapply ex_intro.
58 specialize_under_binders_by apply conj.
59 destruct_head'_ex.
60 destruct_head'_and.
61 rewrite in_flat_map in *.
62 destruct_head'_ex.
63 destruct_head'_and.
64 cbn [In] in *.
65 eauto.
66 eapply H';
67 [ | multimatch goal with H : _ |- _ => exact H end + idtac .. ];
68 [ | solve [ eauto ] .. ].
69 match goal with
70 | [ H0 : Sorted ltB ?l, H1 : HdRel ltB ?x ?l, H2 : In ?y ?l, H3 : Transitive ltB |- ltB ?x ?y ]
71 => clear -H0 H1 H2 H3;
72 revert dependent x; induction H0; cbn [In] in *; try solve [ exfalso; assumption ]
73 end.
74 intros; destruct_head'_or; subst; invlist HdRel; eauto.
75 Qed.