Debian Codegraph @

src/Util/FSets/FMapSect.v

raw
1 From Coq Require Import Bool.
2 From Coq Require Import List.
3 From Coq Require Import Orders.
4 From Coq Require Import OrdersEx.
5 From Coq Require Import FMapInterface.
6 From Coq Require Import Program.
7 Require Import Crypto.Util.ListUtil.
8 Require Import Crypto.Util.ListUtil.Forall.
9 Require Import Crypto.Util.ListUtil.SetoidList.
10 Require Import Crypto.Util.Compose.
11 Require Import Crypto.Util.Logic.Forall.
12 Require Import Crypto.Util.Logic.Exists.
13 Require Import Crypto.Util.Option.
14 Require Import Crypto.Util.Structures.Equalities.Iso.
15 Require Import Crypto.Util.Structures.Orders.
16 Require Import Crypto.Util.Structures.Orders.Iso.
17 Require Import Crypto.Util.Sorting.Sorted.Proper.
18 Require Import Crypto.Util.Tactics.SplitInContext.
19 Require Import Crypto.Util.Tactics.DestructHead.
20 Require Import Crypto.Util.Tactics.SpecializeAllWays.
21 Require Import Crypto.Util.Tactics.SpecializeBy.
22 Require Import Crypto.Util.Tactics.SpecializeUnderBindersBy.
23 Require Import Crypto.Util.Tactics.BreakMatch.
24 Require Import Crypto.Util.Tactics.SetoidSubst.
25
26 Local Set Implicit Arguments.
27 (* TODO: move to global settings *)
28 Local Set Keyed Unification.
29 Local Open Scope lazy_bool_scope.
30
31 Create HintDb fmapsect discriminated.
32
33 Module WSectSfun (E' : DecidableType) (W' : WSfun E')
34 (E : SectDecidableTypeOrig E') <: WSfun E.
35 Local Existing Instances E.Proper_to_ E.Proper_of_.
36
37 Local Instance E_eq_equiv : Equivalence E.eq | 10. split; hnf; eauto. Qed.
38 Local Instance E'_eq_equiv : Equivalence E'.eq | 10. split; hnf; eauto. Qed.
39 Local Instance W'_eq_key_equiv elt : Equivalence (@W'.eq_key elt) | 10. split; cbv; eauto. Qed.
40 Local Instance W'_eq_key_elt_equiv elt : Equivalence (@W'.eq_key_elt elt) | 10. split; repeat intros [? ?]; cbv in *; subst; eauto. Qed.
41
42 Definition key := E.t.
43
44 Global Hint Transparent key : core.
45
46 Definition is_proper_key (x : W'.key) : bool
47 := if E'.eq_dec x (E.to_ (E.of_ x)) then true else false.
48 Definition t elt := { m : W'.t elt | forall x e, W'.MapsTo x e m -> is_proper_key x = true }.
49
50 Local Obligation Tactic := cbv beta; intros.
51
52 Notation mk m' := (exist (fun m : W'.t _ => forall x e, W'.MapsTo x e m -> is_proper_key x = true) m' (fun x e pf => _)).
53
54 Definition lift {A} (f : W'.key -> A) : key -> A
55 := fun x => f (E.to_ x).
56 Definition liftho {A B} (f : (W'.key -> A) -> B) : (key -> A) -> B
57 := fun f' => f (fun x => f' (E.of_ x)).
58
59 Lemma is_proper_key_to k : is_proper_key (E.to_ k) = true.
60 Proof.
61 cbv [is_proper_key].
62 destruct E'.eq_dec; try reflexivity.
63 rewrite E.of_to in *.
64 exfalso; eauto.
65 Qed.
66 #[global]
67 Hint Immediate is_proper_key_to : fmapsect.
68
69 Global Instance Proper_is_proper_key : Proper (E'.eq ==> eq) is_proper_key | 10.
70 Proof.
71 repeat intro; cbv [is_proper_key].
72 do 2 destruct E'.eq_dec; setoid_subst_rel E'.eq; try reflexivity.
73 all: exfalso; eauto.
74 Qed.
75
76 Local Ltac handle_pf :=
77 repeat first [ progress intros
78 | progress cbv beta in *
79 | progress cbv [lift liftho] in *
80 | progress cbn [proj1_sig] in *
81 | exfalso; assumption
82 | progress subst
83 | progress inversion_option
84 | match goal with
85 | [ H : W'.MapsTo _ _ (W'.empty _) |- _ ] => apply W'.empty_1 in H
86 | [ H : W'.MapsTo _ _ (W'.remove _ _) |- _ ] => apply W'.remove_3 in H
87 | [ H : W'.MapsTo ?y ?e (W'.add ?x ?v ?m) |- _ ]
88 => destruct (E'.eq_dec x y);
89 [ let H' := fresh in
90 pose proof (@W'.add_1 _ m x y v ltac:(assumption)) as H';
91 apply W'.find_1 in H;
92 apply W'.find_1 in H';
93 rewrite H in H';
94 clear H
95 | apply W'.add_3 in H; [ | assumption ] ]
96 | [ H : W'.MapsTo _ _ (W'.map ?f _) |- _ ]
97 => let H' := fresh in
98 let H'' := fresh in
99 destruct (@W'.map_2 _ _ _ _ _ (ex_intro _ _ H)) as [? H'];
100 pose proof (@W'.map_1 _ _ _ _ _ f H') as H'';
101 apply W'.find_1 in H;
102 apply W'.find_1 in H'';
103 rewrite H in H'';
104 clear H
105 | [ H : W'.MapsTo _ _ (W'.mapi ?f _) |- _ ]
106 => let H' := fresh in
107 let H'' := fresh in
108 destruct (@W'.mapi_2 _ _ _ _ _ (ex_intro _ _ H)) as [? H'];
109 destruct (@W'.mapi_1 _ _ _ _ _ f H') as [? [? H'']];
110 apply W'.find_1 in H;
111 apply W'.find_1 in H'';
112 rewrite H in H'';
113 clear H
114 end
115 | solve [ eauto with nocore fmapsect ]
116 | progress setoid_subst_rel E'.eq
117 | progress destruct_head' t
118 | match goal with
119 | [ H : W'.MapsTo _ _ (W'.map2 ?f _ _) |- _ ]
120 => let H' := fresh in
121 let H'' := fresh in
122 pose proof (@W'.map2_2 _ _ _ _ _ _ _ (ex_intro _ _ H)) as H';
123 pose proof (@W'.map2_1 _ _ _ _ _ _ f H') as H'';
124 apply W'.find_1 in H;
125 rewrite H in H'';
126 clear H;
127 destruct H' as [[? H']|[? H']];
128 pose proof H' as H;
129 apply W'.find_1 in H;
130 rewrite H in H'';
131 clear H
132 end ].
133
134 Local Obligation Tactic := try abstract handle_pf; handle_pf.
135
136 Program Definition empty elt : t elt := mk (@W'.empty elt).
137 Program Definition is_empty : forall elt, t elt -> bool := W'.is_empty.
138 Program Definition add elt (k : key) (v : elt) (m : t elt) : t elt
139 := mk (lift (@W'.add elt) k v m).
140 Program Definition find elt : key -> t elt -> option elt := lift (@W'.find elt).
141 Program Definition remove elt (k : key) (m : t elt) : t elt
142 := mk (lift (@W'.remove elt) k m).
143 Program Definition mem elt : key -> t elt -> bool := lift (@W'.mem elt).
144 Program Definition map elt elt' (f : elt -> elt') (m : t elt) : t elt'
145 := mk (W'.map f m).
146 Program Definition mapi elt elt' (f : key -> elt -> elt') (m : t elt) : t elt'
147 := mk (liftho (@W'.mapi elt elt') f m).
148 Program Definition map2 elt elt' elt'' (f : option elt -> option elt' -> option elt'') (m : t elt) (m' : t elt') : t elt''
149 := mk (W'.map2 f m m').
150 Program Definition elements elt (v : t elt) : list (key * elt) := List.map (fun kv => (E.of_ (fst kv), snd kv)) (W'.elements v).
151 Program Definition cardinal : forall elt, t elt -> nat := W'.cardinal.
152 Program Definition fold elt A : (key -> elt -> A -> A) -> t elt -> A -> A := liftho (@W'.fold elt A).
153 Program Definition equal : forall elt, (elt -> elt -> bool) -> t elt -> t elt -> bool := W'.equal.
154 Program Definition MapsTo elt : key -> elt -> t elt -> Prop := lift (@W'.MapsTo elt).
155 Program Definition In elt : key -> t elt -> Prop := lift (@W'.In elt).
156 Definition eq_key elt (p p' : key * elt) : Prop := E.eq (fst p) (fst p'). (* W'.eq_key (E.to_ (fst p), snd p) (E.to_ (fst p'), snd p').*)
157 Definition eq_key_elt elt (p p' : key * elt) : Prop := E.eq (fst p) (fst p') /\ snd p = snd p'. (*W'.eq_key_elt (E.to_ (fst p), snd p) (E.to_ (fst p'), snd p').*)
158
159 Local Instance Proper_lift {A R f}
160 {f_Proper : Proper (E'.eq ==> R) f}
161 : Proper (E.eq ==> R) (@lift A f).
162 Proof.
163 cbv [lift]; repeat intro; do 2 f_equiv; assumption.
164 Qed.
165
166 Local Instance Proper_liftho {A B RA RB f}
167 {f_Proper : Proper ((E'.eq ==> RA) ==> RB) f}
168 : Proper ((E.eq ==> RA) ==> RB) (@liftho A B f).
169 Proof.
170 cbv [liftho]; do 3 (repeat intro; f_equiv; try eassumption).
171 Qed.
172
173 Local Instance Proper_W'_eq_key_elt_iff elt
174 : Proper (eq ==> RelationPairs.RelProd E'.eq eq ==> iff) (@W'.eq_key_elt elt).
175 Proof. cbv; repeat intro; subst; destruct_head'_prod; destruct_head'_and; subst; firstorder (subst; eauto). Qed.
176
177 Local Instance Proper_W'_eq_key_elt_impl elt
178 : Proper (eq ==> RelationPairs.RelProd E'.eq eq ==> impl) (@W'.eq_key_elt elt).
179 Proof. cbv; repeat intro; subst; destruct_head'_prod; destruct_head'_and; subst; firstorder (subst; eauto). Qed.
180
181 Local Instance Proper_W'_eq_key_elt_flip_impl elt
182 : Proper (eq ==> RelationPairs.RelProd E'.eq eq ==> flip impl) (@W'.eq_key_elt elt).
183 Proof. cbv; repeat intro; subst; destruct_head'_prod; destruct_head'_and; subst; firstorder (subst; eauto). Qed.
184
185 Local Instance Proper_W'_eq_key_elt_iff' elt
186 : Proper (@W'.eq_key_elt elt ==> @W'.eq_key_elt elt ==> iff) (@W'.eq_key_elt elt).
187 Proof. cbv; repeat intro; subst; destruct_head'_prod; destruct_head'_and; subst; firstorder (subst; eauto). Qed.
188
189 Local Instance Proper_W'_eq_key_elt_impl' elt
190 : Proper (@W'.eq_key_elt elt ==> @W'.eq_key_elt elt ==> impl) (@W'.eq_key_elt elt).
191 Proof. cbv; repeat intro; subst; destruct_head'_prod; destruct_head'_and; subst; firstorder (subst; eauto). Qed.
192
193 Local Instance Proper_W'_eq_key_elt_flip_impl' elt
194 : Proper (@W'.eq_key_elt elt ==> @W'.eq_key_elt elt ==> flip impl) (@W'.eq_key_elt elt).
195 Proof. cbv; repeat intro; subst; destruct_head'_prod; destruct_head'_and; subst; firstorder (subst; eauto). Qed.
196
197 Local Instance Proper_W'_eq_key_iff elt
198 : Proper (@W'.eq_key elt ==> @W'.eq_key elt ==> iff) (@W'.eq_key elt).
199 Proof. cbv; repeat intro; subst; destruct_head'_prod; destruct_head'_and; subst; firstorder (subst; eauto). Qed.
200
201 Local Instance Proper_W'_eq_key_impl elt
202 : Proper (@W'.eq_key elt ==> @W'.eq_key elt ==> impl) (@W'.eq_key elt).
203 Proof. cbv; repeat intro; subst; destruct_head'_prod; destruct_head'_and; subst; firstorder (subst; eauto). Qed.
204
205 Local Instance Proper_W'_eq_key_flip_impl elt
206 : Proper (@W'.eq_key elt ==> @W'.eq_key elt ==> flip impl) (@W'.eq_key elt).
207 Proof. cbv; repeat intro; subst; destruct_head'_prod; destruct_head'_and; subst; firstorder (subst; eauto). Qed.
208
209 Lemma eq_to_iff x y : E'.eq (E.to_ x) (E.to_ y) <-> E.eq x y.
210 Proof.
211 split; intro H; [ | now f_equiv ].
212 etransitivity; rewrite <- E.of_to; [ | reflexivity ]; f_equiv; assumption.
213 Qed.
214
215 Lemma eq_to_of_impl x y
216 : E'.eq (E.to_ x) y -> E.eq x (E.of_ y).
217 Proof.
218 intro H; (rewrite -> H || rewrite <- H);
219 rewrite E.of_to;
220 reflexivity.
221 Qed.
222
223 Lemma eq_to_of_impl' x y
224 : E'.eq y (E.to_ x) -> E.eq (E.of_ y) x.
225 Proof. intro; symmetry; apply eq_to_of_impl; symmetry; assumption. Qed.
226
227 Lemma eq_to_of_flip_impl_proper_key x y
228 (H' : is_proper_key y = true)
229 : E.eq x (E.of_ y) -> E'.eq (E.to_ x) y.
230 Proof.
231 intro H; (rewrite -> H || rewrite <- H);
232 cbv [is_proper_key] in *;
233 break_innermost_match_hyps;
234 try congruence;
235 (idtac + symmetry); assumption.
236 Qed.
237
238 Lemma eq_to_of_flip_impl_proper_key' x y
239 (H' : is_proper_key y = true)
240 : E.eq (E.of_ y) x -> E'.eq y (E.to_ x).
241 Proof. intro; symmetry; apply eq_to_of_flip_impl_proper_key; auto; symmetry; assumption. Qed.
242
243 Lemma eq_to_of_iff_proper_key x y
244 (H' : is_proper_key y = true)
245 : E'.eq (E.to_ x) y <-> E.eq x (E.of_ y).
246 Proof.
247 split; (apply eq_to_of_impl + apply eq_to_of_flip_impl_proper_key); assumption.
248 Qed.
249
250 Lemma eq_to_of_iff_proper_key' x y
251 (H' : is_proper_key y = true)
252 : E'.eq y (E.to_ x) <-> E.eq (E.of_ y) x.
253 Proof.
254 split; (apply eq_to_of_impl' + apply eq_to_of_flip_impl_proper_key'); assumption.
255 Qed.
256
257 Local Instance W'_Equal_Equivalence elt : Equivalence (@W'.Equal elt) | 10.
258 Proof. split; cbv; firstorder eauto using eq_trans. Qed.
259
260 Local Ltac t_Proper_helper :=
261 cbv [W'.Equal W'.In Proper respectful option_eq];
262 repeat first [ progress intros
263 | progress subst
264 | progress destruct_head'_ex
265 | congruence
266 | reflexivity
267 | eassumption
268 | progress break_innermost_match_hyps
269 | progress break_innermost_match
270 | apply W'.find_2
271 | match goal with
272 | [ H : E'.eq ?x ?y |- _ ]
273 => (idtac + symmetry in H); eapply W'.MapsTo_1 in H; [ | apply W'.find_2; eassumption ]
274 | [ H : _ |- _ ] => apply W'.find_1 in H
275 | [ |- Some _ = None ] => exfalso
276 | [ |- ?x = None ] => destruct x eqn:?
277 | [ |- _ <-> _ ] => split
278 | [ H : W'.find ?k ?m = Some ?v, H' : forall k', W'.find k' ?m = W'.find k' ?m' |- _ ]
279 => unique assert (W'.find k m' = Some v) by now rewrite <- H
280 | [ H : W'.find ?k ?m' = Some ?v, H' : forall k', W'.find k' ?m = W'.find k' ?m' |- _ ]
281 => unique assert (W'.find k m = Some v) by now rewrite <- H
282 end
283 | eexists ].
284
285 Local Instance W'_find_Proper_eq elt : Proper (E'.eq ==> @W'.Equal _ ==> option_eq eq) (@W'.find elt) | 10.
286 Proof. t_Proper_helper. Qed.
287
288 Local Instance W'_MapsTo_Proper_eq_iff elt : Proper (E'.eq ==> eq ==> @W'.Equal _ ==> iff) (@W'.MapsTo elt) | 10.
289 Proof. t_Proper_helper. Qed.
290
291 Local Instance W'_In_compat elt : Proper (E'.eq ==> @W'.Equal _ ==> iff) (@W'.In elt) | 10.
292 Proof. t_Proper_helper. Qed.
293
294 Definition Empty elt (m : t elt) := forall a e, ~ MapsTo a e m.
295 Definition Equal elt (m m' : t elt) := forall y, find y m = find y m'.
296 Definition Equiv elt (eq_elt:elt->elt->Prop) m m' :=
297 (forall k, In k m <-> In k m') /\
298 (forall k e e', MapsTo k e m -> MapsTo k e' m' -> eq_elt e e').
299 Definition Equivb elt (cmp: elt->elt->bool) := Equiv (Cmp cmp).
300
301 Program Definition Empty_alt : forall elt, t elt -> Prop := W'.Empty.
302 Program Definition Equal_alt : forall elt, t elt -> t elt -> Prop := W'.Equal.
303 Program Definition Equiv_alt : forall elt, (elt -> elt -> Prop) -> t elt -> t elt -> Prop := W'.Equiv.
304 Program Definition Equivb_alt : forall elt, (elt->elt->bool) -> t elt -> t elt -> Prop := W'.Equivb.
305
306 Lemma forall_lift_key_dep {A} f (P : key -> A -> Prop) R
307 (f_Proper : Proper (E'.eq ==> R) f)
308 (P_Proper : Proper (E.eq ==> R ==> Basics.impl) P)
309 : (forall a : key, P a (@lift A f a)) <-> (forall a : W'.key, is_proper_key a = true -> P (E.of_ a) (f a)).
310 Proof.
311 cbv [lift].
312 split; intro H; intro x; [ intro P'; specialize (H (E.of_ x)) | specialize (H (E.to_ x)) ].
313 all: cbv [is_proper_key] in *.
314 all: break_innermost_match_hyps.
315 all: rewrite ?E.of_to in *.
316 all: cbv [not] in *.
317 all: try discriminate.
318 all: specialize_by reflexivity.
319 all: trivial.
320 all: try now exfalso.
321 eapply P_Proper; try eassumption; try reflexivity.
322 apply f_Proper.
323 (idtac + symmetry); eassumption.
324 Qed.
325
326 Lemma forall_lift_key_dep_no_proper {A} f (P : key -> A -> Prop) R
327 (f_Proper : Proper (E'.eq ==> R) f)
328 (P_Proper : Proper (E.eq ==> R ==> Basics.impl) P)
329 (H_side_cond : forall a : W'.key, is_proper_key a = false -> P (E.of_ a) (f a))
330 : (forall a : key, P a (@lift A f a)) <-> (forall a : W'.key, P (E.of_ a) (f a)).
331 Proof.
332 rewrite forall_lift_key_dep by eassumption.
333 apply pull_forall_iff; intro a.
334 specialize (H_side_cond a).
335 destruct (is_proper_key a); intuition.
336 Qed.
337
338 Local Ltac t_alt_iff :=
339 cbv [In find MapsTo
340 Equal_alt Empty_alt Equiv_alt Equivb_alt
341 Equal Empty Equiv Equivb];
342 let rec get_P lift_f P :=
343 lazymatch P with
344 | fun a : ?A => ?P
345 => let P' := fresh in
346 let a' := fresh in
347 constr:(fun a' : A
348 => ltac:(let P := constr:(match a' with a => P end) in
349 let res := get_P (lift_f a') P in
350 exact res))
351 | _
352 => lazymatch (eval pattern lift_f in P) with
353 | ?P _ => exact P
354 end
355 end in
356 let rec handle_one_lift LHS :=
357 lazymatch LHS with
358 | ?G1 /\ ?G2
359 => handle_one_lift G1; [ handle_one_lift G2 | .. ]
360 | context[lift ?f]
361 => lazymatch LHS with
362 | (forall a : key, @?P a)
363 => let P' := get_P (lift f) P in
364 setoid_rewrite (@forall_lift_key_dep_no_proper _ f P')
365 | _ => fail 0 "unhandled" LHS (*
366 |
367 | [ |- (exists a : elt, @?P a) <-> _ ]
368 => let P' := get_P (lift f) P in
369 setoid_rewrite (@exists_lift_dep _ f P')*)
370 end
371 | _ => idtac
372 end in
373 let rec handle_lift _ :=
374 let LHS := lazymatch goal with |- ?LHS <-> _ => LHS end in
375 lazymatch LHS with
376 | context[lift ?f] => handle_one_lift LHS; [ handle_lift () | .. ]
377 | _ => idtac
378 end in
379 handle_lift ();
380 try exact _;
381 [ try reflexivity; cbv [W'.Equal W'.Empty W'.Equiv W'.Equivb]
382 | try solve [ cbv [Proper respectful Basics.impl Basics.flip option_eq];
383 intros; split_iff;
384 destruct_head' t; cbn [proj1_sig] in *;
385 specialize_under_binders_by reflexivity;
386 specialize_all_ways; break_innermost_match_hyps; inversion_option; subst;
387 firstorder eauto; congruence ];
388 lazymatch goal with
389 | [ |- context[is_proper_key _ = false] ]
390 => repeat first [ progress destruct_head' t
391 | progress cbn [proj1_sig] in *
392 | progress specialize_under_binders_by apply W'.find_2
393 | congruence
394 | progress specialize_all_ways_under_binders_by eassumption
395 | progress intros
396 | match goal with
397 | [ |- context[W'.find ?x ?m] ] => destruct (W'.find x m) eqn:?
398 end ]
399 | _ => idtac
400 end ..
401 ].
402
403 Lemma Equal_alt_iff elt (s s' : t elt) : Equal s s' <-> Equal_alt s s'.
404 Proof. t_alt_iff. Qed.
405 Lemma Empty_alt_iff elt (s : t elt) : Empty s <-> Empty_alt s.
406 Proof. t_alt_iff. Qed.
407 Lemma Equiv_alt_iff elt eq_elt (s s' : t elt) : Equiv eq_elt s s' <-> Equiv_alt eq_elt s s'.
408 Proof. t_alt_iff. Qed.
409 Lemma Equivb_alt_iff elt cmp (s s' : t elt) : Equivb cmp s s' <-> Equivb_alt cmp s s'.
410 Proof. t_alt_iff. Qed.
411
412 Create HintDb fmapsect_is_proper_key discriminated.
413 Create HintDb iso_map_alt discriminated.
414 Create HintDb iso_map_alt1 discriminated.
415 Create HintDb iso_map_alt2 discriminated.
416 Create HintDb iso_map_alt3 discriminated.
417
418 Global
419 Hint Unfold
420 empty
421 is_empty
422 mem
423 add
424 find
425 remove
426 mem
427 map
428 mapi
429 map2
430 elements
431 cardinal
432 fold
433 equal
434 MapsTo
435 In
436 Empty
437 Empty_alt
438 Equiv_alt
439 Equivb_alt
440 eq_key
441 eq_key_elt
442 W'.eq_key
443 W'.eq_key_elt
444 option_map
445 lift
446 liftho
447 : iso_map_alt.
448
449 #[global]
450 Hint Rewrite Empty_alt_iff Equal_alt_iff Equiv_alt_iff Equivb_alt_iff
451 eq_to_iff
452 (*eq_to_of_impl
453 eq_to_of_impl'*)
454 (*W'.is_empty_spec
455 W'.mem_spec
456 W'.add_spec
457 W'.remove_spec
458 W'.singleton_spec
459 W'.union_spec
460 W'.inter_spec
461 W'.diff_spec
462 W'.equal_spec
463 W'.subset_spec
464 W'.fold_spec
465 W'.for_all_spec
466 W'.exists_spec
467 W'.partition_spec1
468 W'.partition_spec2
469 W'.filter_spec
470 W'.cardinal_spec *)
471 W'.cardinal_1
472 W'.fold_1
473 E.of_to
474 (*E.to_of*)
475 map_length
476 fold_left_map
477 : iso_map_alt.
478
479 Global Hint Resolve
480 W'.MapsTo_1
481 W'.mem_1
482 W'.empty_1
483 W'.is_empty_1
484 W'.add_1
485 W'.remove_1
486 W'.find_1
487 W'.elements_1
488 W'.equal_1
489 W'.map_1
490 W'.mapi_1
491 W'.map2_1
492 : iso_map_alt1.
493 Global Hint Resolve
494 W'.mem_2
495 W'.is_empty_2
496 W'.add_2
497 W'.remove_2
498 W'.find_2
499 W'.elements_2
500 W'.equal_2
501 W'.map_2
502 W'.mapi_2
503 W'.map2_2
504 : iso_map_alt2.
505 Global Hint Resolve
506 W'.add_3
507 W'.remove_3
508 W'.elements_3w
509 : iso_map_alt3.
510 Global Hint Extern 10 => rewrite eq_to_iff in *
511 : iso_map_alt1 iso_map_alt2 iso_map_alt3.
512
513 Global Hint Immediate
514 eq_to_of_impl
515 eq_to_of_impl'
516 : iso_map_alt1 iso_map_alt2 iso_map_alt3 core.
517
518 (*Local Hint Resolve
519 W'.empty_spec
520 W'.elements_spec2w
521 W'.choose_spec1
522 W'.choose_spec2
523 : core.*)
524
525 Local Ltac spec_t_step_quick
526 := first [ progress intros
527 | progress cbn [fst snd] in *
528 | progress subst
529 | apply (f_equal2 (@pair _ _))
530 | progress repeat autorewrite with iso_map_alt in *
531 | progress repeat autounfold with iso_map_alt in *
532 | match goal with H : _ |- _ => refine H end
533 | reflexivity
534 | progress auto
535 | exact _
536 | progress destruct_head'_and
537 | progress destruct_head'_ex
538 | progress specialize_under_binders_by eassumption
539 | match goal with
540 | [ H : _ * _ |- _ ] => let H' := fresh in rename H into H'; destruct H'
541 end ].
542
543 Global Hint Extern 100
544 => spec_t_step_quick
545 : iso_map_alt1 iso_map_alt2 iso_map_alt3.
546
547 Local Ltac spec_t
548 := repeat first [ spec_t_step_quick
549 | solve [ eauto with iso_map_alt1 nocore ]
550 | solve [ eauto with iso_map_alt2 nocore ]
551 | solve [ eauto with iso_map_alt3 nocore ] ].
552
553 Local Hint Extern 2 => Proper_compose_hint : typeclass_instances.
554
555 #[global]
556 Hint Transparent W'.eq_key_elt : fmapsect_is_proper_key.
557
558 Lemma is_proper_key_of_InA_elements elt k v (m : t elt)
559 (H : InA (@W'.eq_key_elt _) (k, v) (W'.elements (` m)))
560 : is_proper_key k = true.
561 Proof.
562 destruct m as [m H']; cbn [proj1_sig] in *.
563 spec_t.
564 Qed.
565
566 Lemma is_proper_key_of_In_elements elt k v (m : t elt)
567 (H : List.In (k, v) (W'.elements (` m)))
568 : is_proper_key k = true.
569 Proof.
570 eapply is_proper_key_of_InA_elements.
571 rewrite InA_alt.
572 eexists (_, _); split; [ | eassumption ]; split; reflexivity.
573 Qed.
574
575 #[global]
576 Hint Resolve
577 is_proper_key_of_InA_elements
578 is_proper_key_of_In_elements
579 : fmapsect_is_proper_key.
580
581 #[global]
582 Hint Rewrite
583 eq_to_of_iff_proper_key
584 eq_to_of_iff_proper_key
585 using solve [ eauto with nocore fmapsect_is_proper_key ]
586 : iso_map_alt.
587
588 Section Spec.
589
590 Variable elt:Type.
591 Variable elt' elt'' : Type.
592 Variable m m' m'' : t elt.
593 Variable x y z : key.
594 Variable e e' : elt.
595
596 Lemma MapsTo_1 : E.eq x y -> MapsTo x e m -> MapsTo y e m.
597 Proof using Type. clear. spec_t. Qed.
598 Lemma mem_1 : In x m -> mem x m = true.
599 Proof using Type. clear. spec_t. Qed.
600 Lemma mem_2 : mem x m = true -> In x m.
601 Proof using Type. clear. spec_t. Qed.
602 Lemma empty_1 : Empty (@empty elt).
603 Proof using Type. clear. spec_t. Qed.
604 Lemma is_empty_1 : Empty m -> is_empty m = true.
605 Proof using Type. clear. spec_t. Qed.
606 Lemma is_empty_2 : is_empty m = true -> Empty m.
607 Proof using Type. clear. spec_t. Qed.
608 Lemma add_1 : E.eq x y -> MapsTo y e (add x e m).
609 Proof using Type. clear. spec_t. Qed.
610 Lemma add_2 : ~ E.eq x y -> MapsTo y e m -> MapsTo y e (add x e' m).
611 Proof using Type. clear. spec_t. Qed.
612 Lemma add_3 : ~ E.eq x y -> MapsTo y e (add x e' m) -> MapsTo y e m.
613 Proof using Type. clear. spec_t. Qed.
614 Lemma remove_1 : E.eq x y -> ~ In y (remove x m).
615 Proof using Type. clear. spec_t. Qed.
616 Lemma remove_2 : ~ E.eq x y -> MapsTo y e m -> MapsTo y e (remove x m).
617 Proof using Type. clear. spec_t. Qed.
618 Lemma remove_3 : MapsTo y e (remove x m) -> MapsTo y e m.
619 Proof using Type. clear. spec_t. Qed.
620 Lemma find_1 : MapsTo x e m -> find x m = Some e.
621 Proof using Type. clear. spec_t. Qed.
622 Lemma find_2 : find x m = Some e -> MapsTo x e m.
623 Proof using Type. clear. spec_t. Qed.
624 Lemma elements_1 :
625 MapsTo x e m -> InA (@eq_key_elt _) (x,e) (elements m).
626 Proof using Type. clear. spec_t. rewrite InA_map_iff_strict_InA in *; spec_t. Qed.
627 Lemma elements_2 :
628 InA (@eq_key_elt _) (x,e) (elements m) -> MapsTo x e m.
629 Proof using Type. clear. spec_t. rewrite InA_map_iff_strict_InA in *; spec_t. Qed.
630 Lemma elements_3w : NoDupA (@eq_key _) (elements m).
631 Proof using Type.
632 clear.
633 pose proof (W'.elements_3w (` m)).
634 spec_t.
635 apply NoDupA_map_inv with (f:=fun p => (E.to_ (fst p), snd p)) (eqB:=@W'.eq_key _); [ cbv; intros *; break_innermost_match; now spec_t | ].
636 rewrite List.map_map.
637 setoid_rewrite (_ : eqlistA (@W'.eq_key _) (List.map _ _) _);
638 [ instantiate (1:=List.map (fun x => x) _); rewrite List.map_id; eassumption
639 | ].
640 rewrite eqlistA_altdef, Forall2_map_map_iff, Forall2_Forall, Forall_forall.
641 cbv [W'.eq_key Proper]; cbn [fst snd].
642 spec_t.
643 Qed.
644 Lemma cardinal_1 : cardinal m = length (elements m).
645 Proof using Type. clear. spec_t. Qed.
646
647 Lemma fold_1 :
648 forall (A : Type) (i : A) (f : key -> elt -> A -> A),
649 fold f m i = fold_left (fun a p => f (fst p) (snd p) a) (elements m) i.
650 Proof using Type. clear. spec_t. Qed.
651
652 Variable cmp : elt -> elt -> bool.
653
654 Lemma equal_1 : Equivb cmp m m' -> equal cmp m m' = true.
655 Proof using Type. clear. spec_t. Qed.
656 Lemma equal_2 : equal cmp m m' = true -> Equivb cmp m m'.
657 Proof using Type. clear. spec_t. Qed.
658 End Spec.
659
660 Lemma map_1 : forall (elt elt':Type)(m: t elt)(x:key)(e:elt)(f:elt->elt'),
661 MapsTo x e m -> MapsTo x (f e) (map f m).
662 Proof. clear. spec_t. Qed.
663 Lemma map_2 : forall (elt elt':Type)(m: t elt)(x:key)(f:elt->elt'),
664 In x (map f m) -> In x m.
665 Proof. clear. spec_t. Qed.
666 Lemma mapi_1 (elt elt':Type)(m: t elt)(x:key)(e:elt)
667 (f:key->elt->elt')
668 : MapsTo x e m ->
669 exists y, E.eq y x /\ MapsTo x (f y e) (mapi f m).
670 Proof.
671 clear.
672 pose proof (fun x => @W'.mapi_1 elt elt' (`m) x e (fun x => f (E.of_ x))).
673 spec_t.
674 eexists.
675 split; [ | eassumption ].
676 spec_t.
677 Qed.
678 Lemma mapi_2
679 : forall (elt elt':Type)(m: t elt)(x:key)
680 (f:key->elt->elt'), In x (mapi f m) -> In x m.
681 Proof. clear. spec_t. Qed.
682 Lemma map2_1
683 : forall (elt elt' elt'':Type)(m: t elt)(m': t elt')
684 (x:key)(f:option elt->option elt'->option elt''),
685 In x m \/ In x m' ->
686 find x (map2 f m m') = f (find x m) (find x m').
687 Proof. clear. spec_t. Qed.
688 Lemma map2_2
689 : forall (elt elt' elt'':Type)(m: t elt)(m': t elt')
690 (x:key)(f:option elt->option elt'->option elt''),
691 In x (map2 f m m') -> In x m \/ In x m'.
692 Proof. clear. spec_t. Qed.
693 End WSectSfun.
694
695 Module WSectS (W' : WS) (E : SectDecidableTypeOrig W'.E) <: WS.
696 Module E := E.
697 Include WSectSfun W'.E W' E.
698 End WSectS.
699
700 Module SectSfun (E' : OrderedTypeOrig) (W' : Sfun E') (E : SectOrderedTypeOrig E') <: Sfun E.
701 Include WSectSfun E' W' E.
702 Module E'Compat := E' <+ UpdateEq <+ UpdateStrOrder_Compat.
703 Module ECompat := E <+ UpdateEq <+ UpdateStrOrder_Compat.
704 Section elt.
705 Variable elt:Type.
706 Definition lt_key (p p':key*elt) := E.lt (fst p) (fst p').
707
708 Lemma elements_3 m : sort lt_key (elements m).
709 Proof using Type.
710 pose proof (W'.elements_3 (`m)).
711 cbv [elements lt_key W'.lt_key] in *.
712 rewrite Sorted_map_iff.
713 cbn [fst snd].
714 destruct m as [m pf]; cbn [proj1_sig] in *.
715 specialize_under_binders_by apply W'.elements_2.
716 induction H; constructor; [ | destruct_head' HdRel; constructor ].
717 all: repeat first [ progress specialize_all_ways_under_binders_by eapply InA_cons_hd
718 | progress specialize_all_ways_under_binders_by eapply InA_cons_tl ].
719 all: specialize_under_binders_by apply conj.
720 all: cbn [fst snd] in *.
721 all: specialize_under_binders_by reflexivity.
722 all: specialize_by_assumption.
723 all: eauto.
724 all: cbv [is_proper_key] in *; break_innermost_match_hyps; try discriminate.
725 destruct_head'_prod; cbn [fst snd] in *.
726 clear pf.
727 repeat match goal with
728 | [ H : Sorted _ _ |- _ ] => clear H
729 | [ H : context[InA] |- _ ] => clear H
730 | [ H : ?x = ?x |- _ ] => clear H
731 end.
732 match goal with
733 | [ |- E.lt ?x ?y ]
734 => destruct (E.compare x y) as [pf'|pf'|pf']; try assumption
735 end.
736 { rewrite pf' in *.
737 setoid_subst_rel E'.eq.
738 exfalso; eapply E'.lt_not_eq; (idtac + symmetry); eassumption. }
739 { apply E.Proper_to_lt in pf'.
740 repeat match goal with
741 | [ H : E'.eq ?x (E.to_ (E.of_ ?x)) |- _ ]
742 => rewrite <- H in *
743 end.
744 exfalso; eapply E'.lt_not_eq; try reflexivity; eapply E'.lt_trans; eassumption. }
745 Qed.
746 End elt.
747 End SectSfun.
748
749 Module SectS (S' : S) (E' : SectMiniOrderedType S'.E) <: S.
750 Module E <: SectOrderedTypeOrig S'.E := E' <+ OT_of_MOT.
751 Global Remove Hints E.eq_refl E.eq_sym E.eq_trans : core.
752 Include SectSfun S'.E S' E.
753 End SectS.
754
755 (* TODO:
756 Module SectSord (S' : Sord) (Data' : SectMiniOrderedType S'.Data) (E : SectMiniOrderedType S'.MapS.E) (S'_iso : SectMiniOrderedType S') <: Sord <: SectOrderedType S'.
757 Module Data <: SectOrderedType S'.Data := Data' <+ OT_of_MOT.
758 Module MapS <: S := SectS S'.MapS E.
759 Import MapS.
760
761 Definition t := MapS.t Data.t.
762
763 Definition eq : t -> t -> Prop.
764 refine (_ S'.eq).
765 compute.
766 pose MapS.E.
767 Parameter lt : t -> t -> Prop.
768
769 Axiom eq_refl : forall m : t, eq m m.
770 Axiom eq_sym : forall m1 m2 : t, eq m1 m2 -> eq m2 m1.
771 Axiom eq_trans : forall m1 m2 m3 : t, eq m1 m2 -> eq m2 m3 -> eq m1 m3.
772 Axiom lt_trans : forall m1 m2 m3 : t, lt m1 m2 -> lt m2 m3 -> lt m1 m3.
773 Axiom lt_not_eq : forall m1 m2 : t, lt m1 m2 -> ~ eq m1 m2.
774
775 Definition cmp e e' := match Data.compare e e' with EQ _ => true | _ => false end.
776
777 Parameter eq_1 : forall m m', Equivb cmp m m' -> eq m m'.
778 Parameter eq_2 : forall m m', eq m m' -> Equivb cmp m m'.
779
780 Parameter compare : forall m1 m2, Compare lt eq m1 m2.
781 (** Total ordering between maps. [Data.compare] is a total ordering
782 used to compare data associated with equal keys in the two maps. *)
783
784
785 Include S.
786 Include OT_of_MOT.
787 End SectSord.
788 Definition t := MapS.t Data.t.
789
790 Print SectMiniOrderedType.
791 Definition eq : t -> t -> Prop := S'.eq.
792 Parameter lt : t -> t -> Prop.
793
794 Axiom eq_refl : forall m : t, eq m m.
795 Axiom eq_sym : forall m1 m2 : t, eq m1 m2 -> eq m2 m1.
796 Axiom eq_trans : forall m1 m2 m3 : t, eq m1 m2 -> eq m2 m3 -> eq m1 m3.
797 Axiom lt_trans : forall m1 m2 m3 : t, lt m1 m2 -> lt m2 m3 -> lt m1 m3.
798 Axiom lt_not_eq : forall m1 m2 : t, lt m1 m2 -> ~ eq m1 m2.
799
800 Definition cmp e e' := match Data.compare e e' with EQ _ => true | _ => false end.
801
802 Parameter eq_1 : forall m m', Equivb cmp m m' -> eq m m'.
803 Parameter eq_2 : forall m m', eq m m' -> Equivb cmp m m'.
804
805 Parameter compare : forall m1 m2, Compare lt eq m1 m2.
806 (** Total ordering between maps. [Data.compare] is a total ordering
807 used to compare data associated with equal keys in the two maps. *)
808
809 End Sord.
810
811 *)