Debian Codegraph @

src/Util/ListUtil/SetoidList.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.Option.
5
6 Import ListNotations.
7
8 Lemma list_elementwise_eqlistA : forall {T R} (l1 l2 : list T),
9 (forall i, option_eq R (nth_error l1 i) (nth_error l2 i)) -> eqlistA R l1 l2.
10 Proof.
11 induction l1, l2; intros H; try solve [ constructor ];
12 pose proof (H 0%nat) as Hfirst; cbn in Hfirst; try solve [ inversion Hfirst ].
13 constructor; [ assumption | ].
14 apply IHl1.
15 intros i; specialize (H (S i)).
16 assumption.
17 Qed.
18
19 Lemma nth_error_Proper_eqlistA {T R}
20 : Proper (eqlistA R ==> eq ==> option_eq R) (@nth_error T).
21 Proof.
22 intros ls1 ls2 Hls n n' ?; subst n'.
23 revert ls1 ls2 Hls.
24 induction n; intros ls1 ls2 Hls; destruct Hls; cbn; auto.
25 Qed.
26
27 Lemma eqlistA_eq_iff {A}
28 : forall ls1 ls2, eqlistA (@eq A) ls1 ls2 <-> ls1 = ls2.
29 Proof.
30 induction ls1 as [|x xs IHxs], ls2 as [|y ys].
31 all: split; intro H; inversion_clear H; repeat constructor; subst; f_equal; try reflexivity.
32 apply IHxs; assumption.
33 Qed.
34
35 Lemma InA_map_strict A B (f : A -> B) (eqA eqB : relation _) a b l
36 (f_Proper_a_b : forall v, List.In v l -> eqA a v -> eqB b (f v))
37 : InA eqA a l -> InA eqB b (List.map f l).
38 Proof.
39 rewrite !InA_alt.
40 intros [v H]; exists (f v); split; [ now apply f_Proper_a_b | now apply in_map ].
41 Qed.
42
43 Lemma InA_map_strict' A B (f : A -> B) (eqA eqB : relation _) a b l
44 (f_Proper_a_b : forall v, List.In v l -> eqB b (f v) -> eqA a v)
45 : InA eqB b (List.map f l) -> InA eqA a l.
46 Proof using Type.
47 rewrite !InA_alt.
48 intros [? [? H']].
49 rewrite in_map_iff in H'.
50 destruct H' as [? [? ?]].
51 subst.
52 eexists; eauto.
53 Qed.
54
55 Lemma InA_map_iff_strict A B (f : A -> B) (eqA eqB : relation _) a b l
56 (f_Proper_a_b : forall v, List.In v l -> eqA a v <-> eqB b (f v))
57 : InA eqB b (List.map f l) <-> InA eqA a l.
58 Proof.
59 split; (apply InA_map_strict + apply InA_map_strict'); apply f_Proper_a_b.
60 Qed.
61
62 Lemma InA_map_strict_InA A B (f : A -> B) (eqA eqB : relation _) a b l
63 {_ : Reflexive eqA}
64 (f_Proper_a_b : forall v, InA eqA v l -> eqA a v -> eqB b (f v))
65 : InA eqA a l -> InA eqB b (List.map f l).
66 Proof.
67 apply InA_map_strict; intros; eapply f_Proper_a_b; try assumption.
68 rewrite InA_alt; eauto.
69 Qed.
70
71 Lemma InA_map_strict_InA' A B (f : A -> B) (eqA eqB : relation _) a b l
72 {_ : Reflexive eqA}
73 (f_Proper_a_b : forall v, InA eqA v l -> eqB b (f v) -> eqA a v)
74 : InA eqB b (List.map f l) -> InA eqA a l.
75 Proof.
76 apply InA_map_strict'; intros; eapply f_Proper_a_b; try assumption.
77 rewrite InA_alt; eauto.
78 Qed.
79
80 Lemma InA_map_iff_strict_InA A B (f : A -> B) (eqA eqB : relation _) a b l
81 {_ : Reflexive eqA}
82 (f_Proper_a_b : forall v, InA eqA v l -> eqA a v <-> eqB b (f v))
83 : InA eqB b (List.map f l) <-> InA eqA a l.
84 Proof.
85 apply InA_map_iff_strict; intros; eapply f_Proper_a_b; try assumption.
86 rewrite InA_alt; eauto.
87 Qed.
88
89 Lemma InA_map_strict_InA_PER A B (f : A -> B) (eqA eqB : relation _) a b l
90 {_ : Symmetric eqA} {_ : Transitive eqA}
91 (f_Proper_a_b : forall v, InA eqA v l -> eqA a v -> eqB b (f v))
92 : InA eqA a l -> InA eqB b (List.map f l).
93 Proof.
94 apply InA_map_strict; intros; eapply f_Proper_a_b; try assumption.
95 rewrite InA_alt; eauto.
96 Qed.
97
98 Lemma InA_map A B (f : A -> B) (eqA eqB : relation _) a b l
99 (f_Proper_a_b : forall v, eqA a v -> eqB b (f v))
100 : InA eqA a l -> InA eqB b (List.map f l).
101 Proof. apply InA_map_strict; eauto. Qed.
102
103 Lemma InA_map' A B (f : A -> B) (eqA eqB : relation _) a b l
104 (f_Proper_a_b : forall v, eqB b (f v) -> eqA a v)
105 : InA eqB b (List.map f l) -> InA eqA a l.
106 Proof. apply InA_map_strict'; eauto. Qed.
107
108 Lemma InA_map_iff A B (f : A -> B) (eqA eqB : relation _) a b l
109 (f_Proper_a_b : forall v, eqA a v <-> eqB b (f v))
110 : InA eqB b (List.map f l) <-> InA eqA a l.
111 Proof. apply InA_map_iff_strict; eauto. Qed.
112
113 Lemma NoDupA_map_inv A B (f : A -> B) (eqA eqB : relation _) (l : list A)
114 {f_Proper : Proper (eqA ==> eqB) f}
115 : NoDupA eqB (List.map f l) -> NoDupA eqA l.
116 Proof.
117 induction l as [|? ? IH]; cbn [List.map]; intro H; inversion_clear H;
118 [ constructor | ].
119 specialize (IH ltac:(assumption)).
120 constructor; [ | assumption ].
121 let H := match goal with H : ~ InA _ _ _ |- _ => H end in
122 intro H'; apply H; clear H.
123 eapply InA_map; [ | eassumption ].
124 intro; apply f_Proper.
125 Qed.
126
127 Lemma NoDupA_map_inv' A B (f : A -> B) (eqA eqB : relation _) (l : list A)
128 (f_Proper : forall a v : A, eqB (f a) (f v) -> eqA a v)
129 : NoDupA eqA l -> NoDupA eqB (List.map f l).
130 Proof.
131 induction l as [|? ? IH]; cbn [List.map]; intro H; inversion_clear H;
132 [ constructor | ].
133 specialize (IH ltac:(assumption)).
134 constructor; [ | assumption ].
135 let H := match goal with H : ~ InA _ _ _ |- _ => H end in
136 intro H'; apply H; clear H.
137 eapply InA_map'; [ | eassumption ].
138 intro; apply f_Proper.
139 Qed.
140
141 Global Instance InA_Proper_impl {A} {eqA : relation A}
142 {eqA_Proper : Proper (eqA ==> eqA ==> Basics.impl) eqA}
143 : Proper (eqA ==> eqlistA eqA ==> Basics.impl) (@InA A eqA) | 10.
144 Proof.
145 cbv [Basics.impl] in *.
146 intros a a' Ha l l' Hl.
147 induction Hl as [|?????? IH].
148 { intro H'; inversion H'. }
149 { intro H'; inversion_clear H';
150 try solve [ constructor; eapply eqA_Proper; eassumption
151 | constructor; eauto ]. }
152 Qed.
153
154 Global Instance InA_Proper_flip_impl {A} {eqA : relation A}
155 {eqA_Proper : Proper (eqA ==> eqA ==> Basics.flip Basics.impl) eqA}
156 : Proper (eqA ==> eqlistA eqA ==> Basics.flip Basics.impl) (@InA A eqA) | 10.
157 Proof.
158 cbv [Basics.impl] in *.
159 intros a a' Ha l l' Hl.
160 induction Hl as [|?????? IH].
161 { intro H'; inversion H'. }
162 { intro H'; inversion_clear H';
163 try solve [ constructor; eapply eqA_Proper; eassumption
164 | constructor; eauto ]. }
165 Qed.
166
167 (** We could just use [now typeclasses eauto with core] in Coq >= 8.10, but it doesn't work in 8.9 :-( *)
168 Local Ltac grepeat_eapply :=
169 multimatch goal with
170 | [ H : _ |- _ ] => eapply H; clear H
171 end;
172 solve [ grepeat_eapply ].
173
174 Global Instance InA_Proper_iff {A} {eqA : relation A}
175 {eqA_Proper : Proper (eqA ==> eqA ==> iff) eqA}
176 : Proper (eqA ==> eqlistA eqA ==> iff) (@InA A eqA) | 10.
177 Proof.
178 unshelve (split; (eapply InA_Proper_impl + eapply InA_Proper_flip_impl); assumption).
179 all: repeat intro; solve [ edestruct eqA_Proper; grepeat_eapply ].
180 Qed.
181
182 Global Instance NoDupA_Proper_eqlistA_impl {A eqA}
183 {eqA_Proper : Proper (eqA ==> eqA ==> flip impl) eqA}
184 : Proper (eqlistA eqA ==> impl) (@NoDupA A eqA) | 10.
185 Proof.
186 cbv [flip impl].
187 intros l1 l2 Hl.
188 induction Hl as [|? ? ? ? ? ? IH].
189 { constructor. }
190 { intro H'; inversion_clear H'; constructor; eauto.
191 all: match goal with
192 | [ H : ~InA _ _ _ |- ~ InA _ _ _ ] => intro H'; apply H; clear H
193 end.
194 eapply InA_Proper_flip_impl; eassumption. }
195 Qed.
196
197 Global Instance NoDupA_Proper_eqlistA_flip_impl {A eqA}
198 {eqA_Proper : Proper (eqA ==> eqA ==> impl) eqA}
199 : Proper (eqlistA eqA ==> flip impl) (@NoDupA A eqA) | 10.
200 Proof.
201 cbv [flip impl].
202 intros l1 l2 Hl.
203 induction Hl as [|? ? ? ? ? ? IH].
204 { constructor. }
205 { intro H'; inversion_clear H'; constructor; eauto.
206 all: match goal with
207 | [ H : ~InA _ _ _ |- ~ InA _ _ _ ] => intro H'; apply H; clear H
208 end.
209 eapply InA_Proper_impl; eassumption. }
210 Qed.
211
212 Global Instance NoDupA_Proper_eqlistA_iff {A eqA}
213 {eqA_Proper : Proper (eqA ==> eqA ==> iff) eqA}
214 : Proper (eqlistA eqA ==> iff) (@NoDupA A eqA) | 10.
215 Proof.
216 unshelve (split; (eapply NoDupA_Proper_eqlistA_impl + eapply NoDupA_Proper_eqlistA_flip_impl); assumption).
217 all: repeat intro; solve [ edestruct eqA_Proper; grepeat_eapply ].
218 Qed.