Debian Codegraph @

src/Util/FSets/FMapFacts.v

raw
1 From Coq Require Import FMapInterface.
2 From Coq Require Import FMapFacts.
3 From Coq Require Import Equalities.
4 From Coq Require Import Orders.
5 From Coq Require Import RelationPairs.
6 Require Import Crypto.Util.Structures.Equalities.
7 Require Import Crypto.Util.Structures.Orders.
8 Require Import Crypto.Util.Option.
9 Require Import Crypto.Util.Prod.
10 Require Import Crypto.Util.Sigma.
11 Require Import Crypto.Util.Tactics.SpecializeBy.
12 Require Import Crypto.Util.Tactics.SpecializeUnderBindersBy.
13 Require Import Crypto.Util.Tactics.InHypUnderBindersDo.
14 Require Import Crypto.Util.Tactics.BreakMatch.
15 Require Import Crypto.Util.Tactics.SetoidSubst.
16 Require Import Crypto.Util.Tactics.SplitInContext.
17 Require Import Crypto.Util.Tactics.DestructHead.
18 Require Import Crypto.Util.Tactics.UniquePose.
19
20 Local Set Keyed Unification.
21
22
23 Module Type WFacts_funT (E:DecidableTypeOrig)(M:WSfun E) := Nop <+ WFacts_fun E M.
24 Module WFacts_fun_RemoveHints (E:DecidableTypeOrig)(M:WSfun E) (F : WFacts_funT E M).
25 Global Remove Hints
26 F.Empty_m_Proper
27 F.EqualSetoid
28 F.EqualSetoid_Reflexive
29 F.EqualSetoid_Symmetric
30 F.EqualSetoid_Transitive
31 F.EqualSetoid_relation
32 F.In_m_Proper
33 F.KeySetoid
34 F.KeySetoid_Reflexive
35 F.KeySetoid_Symmetric
36 F.KeySetoid_Transitive
37 F.KeySetoid_relation
38 F.MapsTo_m_Proper
39 F.add_m_Proper
40 F.find_m_Proper
41 F.is_empty_m_Proper
42 F.map_m_Proper
43 F.mem_m_Proper
44 F.remove_m_Proper
45 : typeclass_instances.
46 End WFacts_fun_RemoveHints.
47 Module WFacts_RemoveHints (M:WS) (F:WFacts_funT M.E M) := WFacts_fun_RemoveHints M.E M F.
48 Module Facts_RemoveHints := WFacts_RemoveHints.
49
50 Module Type WProperties_funT (E:DecidableTypeOrig)(M:WSfun E) := Nop <+ WProperties_fun E M.
51 Module WProperties_fun_RemoveHints (E:DecidableTypeOrig)(M:WSfun E) (F : WProperties_funT E M).
52 Include WFacts_fun_RemoveHints E M F.F.
53 Global Remove Hints
54 F.Disjoint_m_Proper
55 F.Partition_m_Proper
56 F.cardinal_m_Proper
57 F.diff_m_Proper
58 F.restrict_m_Proper
59 F.update_m_Proper
60 : typeclass_instances.
61 End WProperties_fun_RemoveHints.
62 Module WProperties_RemoveHints (M:WS) (F:WProperties_funT M.E M) := WProperties_fun_RemoveHints M.E M F.
63 Module Properties_RemoveHints := WProperties_RemoveHints.
64
65 Module Type OrdPropertiesT (M:S) := Nop <+ OrdProperties M.
66 Module OrdProperties_RemoveHints (M:S) (P:OrdPropertiesT M).
67 Include OrderedTypeOrigFacts_RemoveHints M.E P.ME.
68 Include KeyOrderedType_RemoveHints M.E P.O.
69 Include Properties_RemoveHints M P.P.
70 End OrdProperties_RemoveHints.
71
72
73 Local Ltac t_destr_conj_step :=
74 first [ progress subst
75 | progress destruct_head'_False
76 | progress destruct_head'_and
77 | progress destruct_head'_ex
78 | progress destruct_head'_True
79 | progress destruct_head'_unit
80 | progress destruct_head' sig
81 | progress inversion_option
82 | progress inversion_sigma
83 | progress inversion_pair
84 | progress cbn [fst snd proj1_sig proj2_sig] in *
85 | match goal with
86 | [ H : _ :: _ = nil |- _ ] => inversion H
87 | [ H : nil = _ :: _ |- _ ] => inversion H
88 | [ H : nil = nil |- _ ] => clear H
89 | [ H : _ :: _ = _ :: _ |- _ ] => inversion H; clear H
90 | [ H : eqlistA _ nil _ |- _ ] => inversion H; clear H
91 | [ H : eqlistA _ _ nil |- _ ] => inversion H; clear H
92 | [ H : eqlistA _ (_ :: _) _ |- _ ] => inversion H; clear H
93 | [ H : eqlistA _ _ (_ :: _) |- _ ] => inversion H; clear H
94 | [ H : InA _ _ (_ :: _) |- _ ] => inversion H; clear H
95 | [ H : InA _ _ nil |- _ ] => inversion H; clear H
96 | [ H : List.In _ (List.map _ _) |- _ ] => rewrite in_map_iff in H
97 | [ H : List.In _ (List.flat_map _ _) |- _ ] => rewrite in_flat_map in H
98 | [ H : ?x = ?x |- _ ] => clear H
99 | [ H : ?T, H' : ?T |- _ ] => clear H
100 | [ H : ?x = Some _, H' : context[?x] |- _ ] => rewrite H in H'
101 | [ H : ?x = None, H' : context[?x] |- _ ] => rewrite H in H'
102 | [ H : Some _ = ?x |- _ ] => symmetry in H
103 | [ H : None = ?x |- _ ] => symmetry in H
104 end
105 | discriminate ].
106
107 Local Ltac t_destr_step :=
108 first [ t_destr_conj_step
109 | progress destruct_head'_or
110 | progress destruct_head' option
111 | break_innermost_match_hyps_step
112 | break_innermost_match_step ].
113
114
115 Module WAdditionalFacts_fun (E:DecidableTypeOrig)(Import M:WSfun E).
116 Module Import _WAdditionalFacts_fun.
117 Module WFacts := WFacts_fun E M <+ WFacts_fun_RemoveHints E M.
118 Module WProperties := WProperties_fun E M <+ WProperties_fun_RemoveHints E M.
119 End _WAdditionalFacts_fun.
120 Import _WAdditionalFacts_fun.WFacts.
121 Import _WAdditionalFacts_fun.WProperties.
122 Local Existing Instances
123 Empty_m_Proper
124 EqualSetoid
125 EqualSetoid_Reflexive
126 EqualSetoid_Symmetric
127 EqualSetoid_Transitive
128 EqualSetoid_relation
129 In_m_Proper
130 KeySetoid
131 KeySetoid_Reflexive
132 KeySetoid_Symmetric
133 KeySetoid_Transitive
134 KeySetoid_relation
135 MapsTo_m_Proper
136 add_m_Proper
137 find_m_Proper
138 is_empty_m_Proper
139 map_m_Proper
140 mem_m_Proper
141 remove_m_Proper
142 .
143
144 Local Instance Proper_eq_key_elt_iff elt
145 : Proper (eq ==> RelationPairs.RelProd E.eq eq ==> iff) (@M.eq_key_elt elt).
146 Proof. cbv; repeat intro; subst; destruct_head'_prod; destruct_head'_and; subst; firstorder (subst; eauto). Qed.
147
148 Local Instance Proper_eq_key_elt_impl elt
149 : Proper (eq ==> RelationPairs.RelProd E.eq eq ==> impl) (@M.eq_key_elt elt).
150 Proof. cbv; repeat intro; subst; destruct_head'_prod; destruct_head'_and; subst; firstorder (subst; eauto). Qed.
151
152 Local Instance Proper_eq_key_elt_flip_impl elt
153 : Proper (eq ==> RelationPairs.RelProd E.eq eq ==> flip impl) (@M.eq_key_elt elt).
154 Proof. cbv; repeat intro; subst; destruct_head'_prod; destruct_head'_and; subst; firstorder (subst; eauto). Qed.
155
156 Local Instance Proper_eq_key_elt_iff' elt
157 : Proper (@M.eq_key_elt elt ==> @M.eq_key_elt elt ==> iff) (@M.eq_key_elt elt).
158 Proof. cbv; repeat intro; subst; destruct_head'_prod; destruct_head'_and; subst; firstorder (subst; eauto). Qed.
159
160 Local Instance Proper_eq_key_elt_impl' elt
161 : Proper (@M.eq_key_elt elt ==> @M.eq_key_elt elt ==> impl) (@M.eq_key_elt elt).
162 Proof. cbv; repeat intro; subst; destruct_head'_prod; destruct_head'_and; subst; firstorder (subst; eauto). Qed.
163
164 Local Instance Proper_eq_key_elt_flip_impl' elt
165 : Proper (@M.eq_key_elt elt ==> @M.eq_key_elt elt ==> flip impl) (@M.eq_key_elt elt).
166 Proof. cbv; repeat intro; subst; destruct_head'_prod; destruct_head'_and; subst; firstorder (subst; eauto). Qed.
167
168 Local Instance Proper_eq_key_iff elt
169 : Proper (@M.eq_key elt ==> @M.eq_key elt ==> iff) (@M.eq_key elt).
170 Proof. cbv; repeat intro; subst; destruct_head'_prod; destruct_head'_and; subst; firstorder (subst; eauto). Qed.
171
172 Local Instance Proper_eq_key_impl elt
173 : Proper (@M.eq_key elt ==> @M.eq_key elt ==> impl) (@M.eq_key elt).
174 Proof. cbv; repeat intro; subst; destruct_head'_prod; destruct_head'_and; subst; firstorder (subst; eauto). Qed.
175
176 Local Instance Proper_eq_key_flip_impl elt
177 : Proper (@M.eq_key elt ==> @M.eq_key elt ==> flip impl) (@M.eq_key elt).
178 Proof. cbv; repeat intro; subst; destruct_head'_prod; destruct_head'_and; subst; firstorder (subst; eauto). Qed.
179
180 Local Instance Equal_Equivalence elt : Equivalence (@M.Equal elt) | 10.
181 Proof. split; cbv; firstorder eauto using eq_trans. Qed.
182
183 Global Hint Extern 1 (ProperProxy (@M.Equal _) _) => apply reflexive_proper_proxy : typeclass_instances.
184
185 Local Ltac t_Proper_helper :=
186 cbv [M.Equal M.In Proper respectful option_eq];
187 repeat first [ progress intros
188 | progress subst
189 | progress destruct_head'_ex
190 | congruence
191 | reflexivity
192 | eassumption
193 | progress break_innermost_match_hyps
194 | progress break_innermost_match
195 | apply M.find_2
196 | match goal with
197 | [ |- M.find ?x ?y = M.find ?x' ?y' ]
198 => destruct (M.find x y) eqn:?, (M.find x' y') eqn:?
199 | [ H : E.eq ?x ?y |- _ ]
200 => (idtac + symmetry in H); eapply M.MapsTo_1 in H; [ | apply M.find_2; eassumption ]
201 | [ H : _ |- _ ] => apply M.find_1 in H
202 | [ |- Some _ = None ] => exfalso
203 | [ |- ?x = None ] => destruct x eqn:?
204 | [ |- _ <-> _ ] => split
205 | [ H : M.find ?k ?m = Some ?v, H' : forall k', M.find k' ?m = M.find k' ?m' |- _ ]
206 => unique assert (M.find k m' = Some v) by now rewrite <- H
207 | [ H : M.find ?k ?m' = Some ?v, H' : forall k', M.find k' ?m = M.find k' ?m' |- _ ]
208 => unique assert (M.find k m = Some v) by now rewrite <- H
209 end
210 | eexists ].
211
212 Local Instance find_Proper_eq elt : Proper (E.eq ==> @M.Equal _ ==> eq) (@M.find elt) | 10.
213 Proof. t_Proper_helper. Qed.
214
215 Local Instance MapsTo_Proper_eq_iff elt : Proper (E.eq ==> eq ==> @M.Equal _ ==> iff) (@M.MapsTo elt) | 10.
216 Proof. t_Proper_helper. Qed.
217
218 Local Instance In_compat elt : Proper (E.eq ==> @M.Equal _ ==> iff) (@M.In elt) | 10.
219 Proof. t_Proper_helper. Qed.
220
221 Local Instance eq_key_equiv elt : Equivalence (@eq_key elt) | 10.
222 Proof.
223 split; cbv; intros; break_innermost_match; break_innermost_match_hyps; try ((idtac + symmetry + etransitivity + exfalso); (eassumption + reflexivity)).
224 Qed.
225 Local Instance eq_key_elt_equiv elt : Equivalence (@eq_key_elt elt) | 10.
226 Proof.
227 split; cbv; intros; break_innermost_match; break_innermost_match_hyps; destruct_head'_and; split; try ((idtac + symmetry + etransitivity + exfalso); (eassumption + reflexivity)).
228 Qed.
229
230 Lemma find_empty elt x : M.find x (@M.empty elt) = None.
231 Proof.
232 pose proof (@M.empty_1 elt x) as H.
233 cbv in H; setoid_rewrite find_mapsto_iff in H.
234 destruct M.find; intuition congruence.
235 Qed.
236
237 Local Ltac Proper_equiv_t :=
238 cbv; setoid_rewrite find_mapsto_iff;
239 repeat split; intros; subst; split_and; eauto.
240
241 Local Instance Proper_Equiv_eq_impl elt
242 : Proper ((eq ==> eq ==> impl) ==> eq ==> eq ==> impl) (@M.Equiv elt) | 10.
243 Proof. Proper_equiv_t. Qed.
244 Local Instance Proper_Equiv_eq_flip_impl elt
245 : Proper ((eq ==> eq ==> flip impl) ==> eq ==> eq ==> flip impl) (@M.Equiv elt) | 10.
246 Proof. Proper_equiv_t. Qed.
247 Local Instance Proper_Equiv_eq_iff elt
248 : Proper ((eq ==> eq ==> iff) ==> eq ==> eq ==> iff) (@M.Equiv elt) | 10.
249 Proof. Proper_equiv_t. Qed.
250 Local Instance Proper_Equiv_eq_impl_pointwise elt
251 : Proper (pointwise_relation _ (pointwise_relation _ impl) ==> eq ==> eq ==> impl) (@M.Equiv elt) | 10.
252 Proof. Proper_equiv_t. Qed.
253 Local Instance Proper_Equiv_eq_flip_impl_pointwise elt
254 : Proper (pointwise_relation _ (pointwise_relation _ (flip impl)) ==> eq ==> eq ==> flip impl) (@M.Equiv elt) | 10.
255 Proof. Proper_equiv_t. Qed.
256 Local Instance Proper_Equiv_eq_iff_pointwise elt
257 : Proper (pointwise_relation _ (pointwise_relation _ iff) ==> eq ==> eq ==> iff) (@M.Equiv elt) | 10.
258 Proof. Proper_equiv_t. Qed.
259
260 Definition empty_1' elt : forall x y z, False := @M.empty_1 elt.
261
262 Local Ltac t_full_step :=
263 first [ t_destr_step
264 | progress cbv [M.In not option_map] in *
265 | progress intros
266 | match goal with
267 | [ H : context[M.MapsTo] |- _ ] => setoid_rewrite find_mapsto_iff in H
268 | [ |- context[M.MapsTo] ] => setoid_rewrite find_mapsto_iff
269 end
270 | progress specialize_under_binders_by eapply ex_intro
271 | progress specialize_under_binders_by eassumption
272 | progress specialize_under_binders_by reflexivity
273 | exfalso; assumption
274 | progress inversion_option
275 | progress subst
276 | reflexivity
277 | break_innermost_match_step
278 | match goal with
279 | [ |- context[M.find ?x ?y] ] => destruct (M.find x y) eqn:?
280 end ].
281
282
283 Lemma mapi_o_ex : forall (elt elt':Type)(m: M.t elt)(x:M.key)(f:M.key->elt->elt'), exists y, E.eq y x /\ M.find x (M.mapi f m) = option_map (f y) (M.find x m).
284 Proof.
285 pose proof M.mapi_1 as H.
286 pose proof M.mapi_2 as H'.
287 repeat t_full_step.
288 all: match goal with
289 | [ H : M.find _ (M.mapi _ _) = _ |- _ ]
290 => in_hyp_under_binders_do (fun H' => rewrite H in H')
291 end.
292 all: repeat t_full_step; eauto.
293 Qed.
294
295 Lemma mapi_o_ex_impl (elt elt':Type)(m: M.t elt)(f:M.key->elt->elt')(P:_->Prop)
296 : (forall x e, M.find x (M.mapi f m) = Some e -> P e)
297 -> (forall x, exists y, E.eq y x /\ forall e, option_map (f y) (M.find x m) = Some e -> P e).
298 Proof.
299 intros H x.
300 specialize (H x).
301 destruct (@mapi_o_ex _ _ m x f) as [? [H0 H1]].
302 eexists; split; [ eassumption | ].
303 specialize_under_binders_by rewrite H1.
304 intuition congruence.
305 Qed.
306
307 Lemma map2_o
308 : forall (elt elt' elt'':Type)(m: M.t elt)(m': M.t elt')
309 (x:M.key)(f:option elt->option elt'->option elt''),
310 M.find x (M.map2 f m m') = match M.find x m, M.find x m' with
311 | None, None => None
312 | x, y => f x y
313 end.
314 Proof.
315 intros *.
316 pose proof (@M.map2_1 elt elt' elt'' m m' x f) as H.
317 pose proof (@M.map2_2 elt elt' elt'' m m' x f) as H'.
318 cbv [M.In] in *.
319 setoid_rewrite find_mapsto_iff in H.
320 setoid_rewrite find_mapsto_iff in H'.
321 specialize_under_binders_by eapply ex_intro.
322 destruct (M.find x (M.map2 f m m')); break_innermost_match; try reflexivity;
323 specialize_by eauto; specialize_under_binders_by reflexivity; eauto.
324 firstorder congruence.
325 Qed.
326
327 Lemma forall_In_elements_iff elt (P : _ -> Prop) (m : M.t elt)
328 (P_Proper : Proper (@M.eq_key_elt _ ==> Basics.flip impl) P)
329 : (forall i, List.In i (M.elements m) -> P i)
330 <-> (forall k v, M.find k m = Some v -> P (k, v)).
331 Proof.
332 setoid_rewrite <- find_mapsto_iff.
333 setoid_rewrite elements_mapsto_iff.
334 setoid_rewrite InA_alt.
335 split; intros; destruct_head' prod; repeat t_destr_step; firstorder eauto.
336 Qed.
337 Lemma forall_In_elements_snd_iff elt (P : _ -> Prop) (m : M.t elt)
338 : (forall i, List.In i (M.elements m) -> P (snd i))
339 <-> (forall k v, M.find k m = Some v -> P v).
340 Proof.
341 rewrite forall_In_elements_iff; [ reflexivity | ].
342 cbv; repeat intro; repeat t_destr_step; assumption.
343 Qed.
344
345 Lemma fold_remove elt A (eqA : relation A)
346 {EqvA : Equivalence eqA}
347 (f : key -> elt -> A -> A)
348 {f_Proper : Proper (E.eq ==> eq ==> eqA ==> eqA) f}
349 (Hf : transpose_neqkey eqA f)
350 (m : t elt) k e i
351 (Hk : find k m = Some e)
352 : eqA (fold f m i) (f k e (fold f (remove k m) i)).
353 Proof.
354 apply fold_Add; try assumption.
355 { cbv [In]; setoid_rewrite find_mapsto_iff.
356 setoid_rewrite remove_o; break_innermost_match; firstorder (try congruence; auto). }
357 { cbv [Add]; intro.
358 rewrite add_o, remove_o; break_innermost_match; setoid_subst_rel E.eq; auto. }
359 Qed.
360
361 Lemma fold_add_remove elt A (eqA : relation A)
362 {EqvA : Equivalence eqA}
363 (f : key -> elt -> A -> A)
364 {f_Proper : Proper (E.eq ==> eq ==> eqA ==> eqA) f}
365 (Hf : transpose_neqkey eqA f)
366 (m : t elt) k e i
367 : eqA (fold f (add k e m) i) (f k e (fold f (remove k m) i)).
368 Proof.
369 apply fold_Add; try assumption.
370 { cbv [In]; setoid_rewrite find_mapsto_iff.
371 setoid_rewrite remove_o; break_innermost_match; firstorder (try congruence; auto). }
372 { cbv [Add]; intro.
373 rewrite !add_o, remove_o; break_innermost_match; reflexivity. }
374 Qed.
375
376 Lemma cardinal_add elt (m : t elt) k v
377 : cardinal (add k v m) = if mem k m then cardinal m else S (cardinal m).
378 Proof.
379 rewrite !cardinal_fold.
380 break_innermost_match; rewrite <- ?not_mem_in_iff, ?mem_find_b in *.
381 all: try rewrite fold_add by (try exact _; eauto; repeat intro; subst; reflexivity).
382 all: try rewrite fold_add_remove by (try exact _; repeat intro; subst; reflexivity).
383 all: try reflexivity.
384 all: break_innermost_match_hyps; try congruence.
385 { symmetry; eapply fold_remove with (f:=fun _ _ => S); repeat split; repeat intro; try eassumption; subst; reflexivity. }
386 Qed.
387 End WAdditionalFacts_fun.
388
389 Module WAdditionalFacts (M:WS) := WAdditionalFacts_fun M.E M.
390
391 Module OrdAdditionalFacts_fun (E:OrderedTypeOrig)(Import M:Sfun E).
392 Global Instance lt_key_Transitive elt : Transitive (@lt_key elt) | 10.
393 Proof. cbv; intros *; eapply E.lt_trans. Qed.
394 End OrdAdditionalFacts_fun.
395
396 Module OrdAdditionalFacts (M:S) := OrdAdditionalFacts_fun M.E M.
397 Module AdditionalFacts (M:S) := WAdditionalFacts M <+ OrdAdditionalFacts M.