Debian Codegraph @

src/Util/MSets/MSetIso.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 MSetInterface.
6 Require Import Crypto.Util.ListUtil.
7 Require Import Crypto.Util.ListUtil.SetoidList.
8 Require Import Crypto.Util.Compose.
9 Require Import Crypto.Util.Logic.Forall.
10 Require Import Crypto.Util.Logic.Exists.
11 Require Import Crypto.Util.Option.
12 Require Import Crypto.Util.Structures.Equalities.Iso.
13 Require Import Crypto.Util.Structures.Orders.Iso.
14 Require Import Crypto.Util.Tactics.SplitInContext.
15 Require Import Crypto.Util.Tactics.DestructHead.
16 Require Import Crypto.Util.Tactics.BreakMatch.
17 Require Import Crypto.Util.Tactics.SetoidSubst.
18
19 Local Set Implicit Arguments.
20
21 Module IsoWSetsOn (E' : Equalities.DecidableType) (W' : WSetsOn E')
22 (E : IsoDecidableType E') <: WSetsOn E.
23 Local Existing Instances E.Proper_to_ E.Proper_of_ W'.In_compat.
24
25 Definition elt := E.t.
26 Definition t := W'.t.
27
28 Definition lift {A} (f : W'.elt -> A) : elt -> A
29 := fun x => f (E.to_ x).
30 Definition liftho {A B} (f : (W'.elt -> A) -> B) : (elt -> A) -> B
31 := fun f' => f (fun x => f' (E.of_ x)).
32
33 Definition empty : t := W'.empty.
34 Definition is_empty : t -> bool := W'.is_empty.
35 Definition mem : elt -> t -> bool := lift W'.mem.
36 Definition add : elt -> t -> t := lift W'.add.
37 Definition remove : elt -> t -> t := lift W'.remove.
38 Definition singleton : elt -> t := lift W'.singleton.
39 Definition union : t -> t -> t := W'.union.
40 Definition inter : t -> t -> t := W'.inter.
41 Definition diff : t -> t -> t := W'.diff.
42 Definition equal : t -> t -> bool := W'.equal.
43 Definition subset : t -> t -> bool := W'.subset.
44 Definition fold B : (elt -> B -> B) -> t -> B -> B := liftho (@W'.fold B).
45 Definition for_all : (elt -> bool) -> t -> bool := liftho W'.for_all.
46 Definition exists_ : (elt -> bool) -> t -> bool := liftho W'.exists_.
47 Definition filter : (elt -> bool) -> t -> t := liftho W'.filter.
48 Definition partition : (elt -> bool) -> t -> t * t := liftho W'.partition.
49 Definition cardinal : t -> nat := W'.cardinal.
50 Definition elements (v : t) : list elt := List.map E.of_ (W'.elements v).
51 Definition choose (v : t) : option elt := option_map E.of_ (W'.choose v).
52 Definition In : elt -> t -> Prop := lift W'.In.
53
54 Local Instance Proper_lift {A R f}
55 {f_Proper : Proper (E'.eq ==> R) f}
56 : Proper (E.eq ==> R) (@lift A f).
57 Proof.
58 cbv [lift]; repeat intro; do 2 f_equiv; assumption.
59 Qed.
60
61 Local Instance Proper_liftho {A B RA RB f}
62 {f_Proper : Proper ((E'.eq ==> RA) ==> RB) f}
63 : Proper ((E.eq ==> RA) ==> RB) (@liftho A B f).
64 Proof.
65 cbv [liftho]; do 3 (repeat intro; f_equiv; try eassumption).
66 Qed.
67
68 Local Instance In_compat : Proper (E.eq ==> eq ==> iff) In := _.
69 Definition Equal s s' := forall a : elt, In a s <-> In a s'.
70 Definition Subset s s' := forall a : elt, In a s -> In a s'.
71 Definition Empty s := forall a : elt, ~ In a s.
72 Definition For_all (P : elt -> Prop) s := forall x, In x s -> P x.
73 Definition Exists (P : elt -> Prop) s := exists x, In x s /\ P x.
74
75 Definition Equal_alt : t -> t -> Prop := W'.Equal.
76 Definition Subset_alt : t -> t -> Prop := W'.Subset.
77 Definition Empty_alt : t -> Prop := W'.Empty.
78 Definition For_all_alt : (elt -> Prop) -> t -> Prop := liftho W'.For_all.
79 Definition Exists_alt : (elt -> Prop) -> t -> Prop := liftho W'.Exists.
80
81 Lemma forall_lift_dep {A} f (P : elt -> A -> Prop) R
82 (f_Proper : Proper (E'.eq ==> R) f)
83 (P_Proper : Proper (E.eq ==> R ==> Basics.impl) P)
84 : (forall a : elt, P a (@lift A f a)) <-> (forall a : W'.elt, P (E.of_ a) (f a)).
85 Proof.
86 cbv [lift].
87 split; intro H; intro x; [ specialize (H (E.of_ x)) | specialize (H (E.to_ x)) ].
88 all: eapply P_Proper; try eassumption.
89 all: f_equiv.
90 all: apply E.to_of + apply E.of_to.
91 Qed.
92
93 Lemma forall_lift {A} f (P : A -> Prop) R
94 (f_Proper : Proper (E'.eq ==> R) f)
95 (P_Proper : Proper (R ==> Basics.impl) P)
96 : (forall a : elt, P (@lift A f a)) <-> (forall a : W'.elt, P (f a)).
97 Proof.
98 apply (@forall_lift_dep A f (fun _ => P) R); [ | intros ??? ]; assumption.
99 Qed.
100
101 Lemma exists_lift_dep {A} f (P : elt -> A -> Prop) R
102 (f_Proper : Proper (E'.eq ==> R) f)
103 (P_Proper : Proper (E.eq ==> R ==> Basics.impl) P)
104 : (exists a : elt, P a (@lift A f a)) <-> (exists a : W'.elt, P (E.of_ a) (f a)).
105 Proof.
106 cbv [lift].
107 split; intros [x H]; [ exists (E.to_ x) | exists (E.of_ x) ].
108 all: eapply P_Proper; try eassumption.
109 all: f_equiv.
110 all: symmetry; apply E.to_of + apply E.of_to.
111 Qed.
112
113 Lemma exists_lift {A} f (P : A -> Prop) R
114 (f_Proper : Proper (E'.eq ==> R) f)
115 (P_Proper : Proper (R ==> Basics.impl) P)
116 : (exists a : elt, P (@lift A f a)) <-> (exists a : W'.elt, P (f a)).
117 Proof.
118 apply (@exists_lift_dep A f (fun _ => P) R); [ | intros ??? ]; assumption.
119 Qed.
120
121 Local Ltac t_alt_iff :=
122 cbv [In
123 Equal_alt Subset_alt Empty_alt For_all_alt Exists_alt
124 Equal Subset Empty For_all Exists];
125 let get_P lift_f P :=
126 lazymatch P with
127 | fun a : ?A => ?P
128 => let P' := fresh in
129 constr:(
130 fun a : elt
131 => match P return _ with
132 | P'
133 => ltac:(let P := (eval cbv delta [P'] in P') in
134 clear P';
135 lazymatch (eval pattern (lift_f a) in P) with
136 | ?P _ => exact P
137 end)
138 end)
139 end in
140 lazymatch goal with
141 | [ |- context[lift ?f] ]
142 => lazymatch goal with
143 | [ |- (forall a : elt, @?P a) <-> _ ]
144 => let P' := get_P (lift f) P in
145 setoid_rewrite (@forall_lift_dep _ f P')
146 | [ |- (exists a : elt, @?P a) <-> _ ]
147 => let P' := get_P (lift f) P in
148 setoid_rewrite (@exists_lift_dep _ f P')
149 end
150 end;
151 try exact _;
152 [ try reflexivity
153 | try solve [ cbv [Proper respectful Basics.impl Basics.flip];
154 intros; split_iff;
155 repeat match goal with
156 | [ H : forall x y, x = y -> _ |- _ ] => specialize (fun x => H x x eq_refl)
157 end;
158 firstorder eauto ]..
159 ].
160
161 Lemma Equal_alt_iff s s' : Equal s s' <-> Equal_alt s s'.
162 Proof. t_alt_iff. Qed.
163 Lemma Subset_alt_iff s s' : Subset s s' <-> Subset_alt s s'.
164 Proof. t_alt_iff. Qed.
165 Lemma Empty_alt_iff s : Empty s <-> Empty_alt s.
166 Proof. t_alt_iff. Qed.
167 Lemma For_all_alt_iff P s
168 (P_Proper : Proper (E.eq ==> iff) P)
169 : For_all P s <-> For_all_alt P s.
170 Proof. t_alt_iff. Qed.
171 Lemma Exists_alt_iff P s
172 (P_Proper : Proper (E.eq ==> iff) P)
173 : Exists P s <-> Exists_alt P s.
174 Proof. t_alt_iff. Qed.
175
176 Local Notation "s [=] t" := (Equal s t) (at level 70, no associativity).
177 Local Notation "s [<=] t" := (Subset s t) (at level 70, no associativity).
178
179 Definition eq := Equal.
180 Local Instance eq_equiv : Equivalence eq.
181 Proof.
182 cbv [eq Equal]; split; repeat intro.
183 { reflexivity. }
184 { symmetry; auto. }
185 { etransitivity; eauto. }
186 Qed.
187 Lemma eq_dec (x y : t) : {eq x y} + {~eq x y}.
188 Proof.
189 destruct (W'.eq_dec x y) as [H|H]; [ left | right ];
190 cbv [eq]; rewrite Equal_alt_iff; cbv [Equal_alt]; assumption.
191 Defined.
192
193 Lemma eq_to_iff x y : E'.eq (E.to_ x) (E.to_ y) <-> E.eq x y.
194 Proof.
195 split; intro H; [ | now f_equiv ].
196 etransitivity; rewrite <- E.of_to; [ | reflexivity ]; f_equiv; assumption.
197 Qed.
198
199 Lemma eq_to_of_iff x y
200 : E'.eq (E.to_ x) y <-> E.eq x (E.of_ y).
201 Proof.
202 split; intro H; (rewrite -> H || rewrite <- H);
203 (rewrite E.to_of || rewrite E.of_to);
204 reflexivity.
205 Qed.
206
207 Create HintDb iso_set_alt discriminated.
208
209 #[global]
210 Hint Unfold
211 empty
212 is_empty
213 mem
214 add
215 remove
216 singleton
217 union
218 inter
219 diff
220 equal
221 subset
222 fold
223 for_all
224 exists_
225 filter
226 partition
227 cardinal
228 elements
229 choose
230 In
231 option_map
232 lift
233 liftho
234 Empty_alt Equal_alt
235 : iso_set_alt.
236
237 #[global]
238 Hint Rewrite Empty_alt_iff Equal_alt_iff Subset_alt_iff For_all_alt_iff Exists_alt_iff
239 eq_to_iff
240 W'.is_empty_spec
241 W'.mem_spec
242 W'.add_spec
243 W'.remove_spec
244 W'.singleton_spec
245 W'.union_spec
246 W'.inter_spec
247 W'.diff_spec
248 W'.equal_spec
249 W'.subset_spec
250 W'.fold_spec
251 W'.for_all_spec
252 W'.exists_spec
253 W'.partition_spec1
254 W'.partition_spec2
255 W'.filter_spec
256 W'.cardinal_spec
257 E.of_to
258 E.to_of
259 map_length
260 fold_left_map
261 : iso_set_alt.
262
263 Local Hint Resolve
264 W'.empty_spec
265 W'.elements_spec2w
266 W'.choose_spec1
267 W'.choose_spec2
268 : core.
269
270 Local Ltac spec_t
271 := repeat first [ reflexivity
272 | progress intros
273 | progress repeat autorewrite with iso_set_alt
274 | progress repeat autounfold with iso_set_alt in *
275 | progress auto
276 | exact _ ].
277
278 Local Hint Extern 2 => Proper_compose_hint : typeclass_instances.
279
280 Section Spec.
281 Variable s s': t.
282 Variable x y : elt.
283 Variable f : elt -> bool.
284 Notation compatb := (Proper (E.eq==>Logic.eq)) (only parsing).
285 Lemma mem_spec : mem x s = true <-> In x s.
286 Proof using Type. spec_t. Qed.
287 Lemma equal_spec : equal s s' = true <-> s[=]s'.
288 Proof using Type. spec_t. Qed.
289 Lemma subset_spec : subset s s' = true <-> s[<=]s'.
290 Proof using Type. spec_t. Qed.
291 Lemma empty_spec : Empty empty.
292 Proof using Type. spec_t. Qed.
293 Lemma is_empty_spec : is_empty s = true <-> Empty s.
294 Proof using Type. spec_t. Qed.
295 Lemma add_spec : In y (add x s) <-> E.eq y x \/ In y s.
296 Proof using Type. spec_t. Qed.
297 Lemma remove_spec : In y (remove x s) <-> In y s /\ ~E.eq y x.
298 Proof using Type. spec_t. Qed.
299 Lemma singleton_spec : In y (singleton x) <-> E.eq y x.
300 Proof using Type. spec_t. Qed.
301 Lemma union_spec : In x (union s s') <-> In x s \/ In x s'.
302 Proof using Type. spec_t. Qed.
303 Lemma inter_spec : In x (inter s s') <-> In x s /\ In x s'.
304 Proof using Type. spec_t. Qed.
305 Lemma diff_spec : In x (diff s s') <-> In x s /\ ~In x s'.
306 Proof using Type. spec_t. Qed.
307 Lemma fold_spec : forall (A : Type) (i : A) (f : elt -> A -> A),
308 fold f s i = fold_left (flip f) (elements s) i.
309 Proof using Type. spec_t. Qed.
310 Lemma cardinal_spec : cardinal s = length (elements s).
311 Proof using Type. spec_t. Qed.
312 Lemma filter_spec : compatb f ->
313 (In x (filter f s) <-> In x s /\ f x = true).
314 Proof using Type. spec_t. Qed.
315 Lemma for_all_spec : compatb f ->
316 (for_all f s = true <-> For_all (fun x => f x = true) s).
317 Proof using Type. spec_t. repeat intro; repeat (f_equiv; trivial). Qed.
318 Lemma exists_spec : compatb f ->
319 (exists_ f s = true <-> Exists (fun x => f x = true) s).
320 Proof using Type. spec_t. repeat intro; repeat (f_equiv; trivial). Qed.
321 Lemma partition_spec1 : compatb f ->
322 fst (partition f s) [=] filter f s.
323 Proof using Type. spec_t. Qed.
324 Lemma partition_spec2 : compatb f ->
325 snd (partition f s) [=] filter (fun x => negb (f x)) s.
326 Proof using Type. spec_t. Qed.
327 Lemma elements_spec1 : InA E.eq x (elements s) <-> In x s.
328 Proof using Type.
329 cbv [In lift elements]; rewrite <- W'.elements_spec1.
330 spec_t.
331 apply InA_map_iff, eq_to_of_iff.
332 Qed.
333 Lemma elements_spec2w : NoDupA E.eq (elements s).
334 Proof using Type.
335 spec_t.
336 apply NoDupA_map_inv with (f:=E.to_) (eqB:=E'.eq); try exact _; [].
337 rewrite List.map_map.
338 setoid_rewrite (Proper_map _ _ _ _ _ _);
339 [ rewrite List.map_id
340 | intros ???; etransitivity; [ eapply E.to_of | eassumption ]
341 | reflexivity ].
342 apply W'.elements_spec2w.
343 Qed.
344 Lemma choose_spec1 : choose s = Some x -> In x s.
345 Proof using Type.
346 cbv [In choose option_map lift].
347 break_innermost_match; intros; inversion_option; subst.
348 rewrite E.to_of; auto.
349 Qed.
350 Lemma choose_spec2 : choose s = None -> Empty s.
351 Proof using Type. spec_t; break_innermost_match_hyps; inversion_option; auto. Qed.
352 End Spec.
353 End IsoWSetsOn.
354
355 Module IsoSetsOn (E' : Orders.OrderedType) (W' : SetsOn E')
356 (E : IsoOrderedType E') <: SetsOn E.
357 Local Existing Instances E.Proper_to_ E.Proper_of_ E.Proper_of_lt E.Proper_to_lt W'.In_compat.
358 Include (IsoWSetsOn E' W' E).
359
360 Definition compare : t -> t -> comparison := W'.compare.
361 Definition min_elt (v : t) : option elt := option_map E.of_ (W'.min_elt v).
362 Definition max_elt (v : t) : option elt := option_map E.of_ (W'.max_elt v).
363 Definition lt : t -> t -> Prop := W'.lt.
364 #[global]
365 Instance lt_strorder : StrictOrder lt | 1 := W'.lt_strorder.
366 #[global]
367 Instance lt_compat : Proper (eq==>eq==>iff) lt | 1.
368 Proof.
369 cbv [eq]; intros ?? H ?? H'; apply W'.lt_compat;
370 autorewrite with iso_set_alt in *;
371 assumption.
372 Qed.
373
374 #[global]
375 Hint Unfold compare min_elt max_elt lt : iso_set_alt.
376
377 Local Ltac spec_t'
378 := repeat first [ reflexivity
379 | progress intros
380 | progress repeat autorewrite with iso_set_alt in *
381 | progress repeat autounfold with iso_set_alt in *
382 | progress auto
383 | exact _
384 | progress break_innermost_match_hyps
385 | progress subst
386 | progress inversion_option
387 | match goal with
388 | [ H : E'.lt ?x (E.to_ ?y) |- E.lt (E.of_ ?x) ?y ]
389 => rewrite <- (E.of_to y); apply E.Proper_of_lt; assumption
390 | [ H : E.lt ?x (E.of_ ?y) |- E'.lt (E.to_ ?x) ?y ]
391 => rewrite <- (E.to_of y); apply E.Proper_to_lt; assumption
392 | [ H : E'.lt (E.to_ ?y) ?x |- E.lt ?y (E.of_ ?x) ]
393 => rewrite <- (E.of_to y); apply E.Proper_of_lt; assumption
394 | [ H : E.lt (E.of_ ?y) ?x |- E'.lt ?y (E.to_ ?x) ]
395 => rewrite <- (E.to_of y); apply E.Proper_to_lt; assumption
396 end ].
397
398 Section Spec.
399 Variable s s': t.
400 Variable x y : elt.
401
402 Lemma compare_spec : CompSpec eq lt s s' (compare s s').
403 Proof using Type.
404 cbv [compare eq lt].
405 destruct (W'.compare_spec s s'); constructor; now spec_t'.
406 Qed.
407
408 Lemma elements_spec2 : sort E.lt (elements s).
409 Proof using Type.
410 cbv [elements].
411 induction (W'.elements_spec2 s); cbn [List.map]; constructor;
412 try assumption.
413 rewrite InfA_alt with (eqA:=E'.eq) in * by (assumption || exact _).
414 rewrite InfA_alt with (eqA:=E.eq) in * by (assumption || exact _).
415 intros v H'.
416 let H := match goal with H : forall y, InA _ y _ -> E'.lt _ y |- _ => H end in
417 specialize (H (E.to_ v));
418 rewrite InA_map_iff in H'; [ specialize (H H') | apply eq_to_of_iff ].
419 spec_t'.
420 Qed.
421
422 Ltac elt_spec lem :=
423 spec_t';
424 match goal with H : _ = _ :> option _ |- _ => eapply lem in H; [ | eassumption.. ] end;
425 try match goal with H : ~ ?R _ _ |- ~ _ => intro; apply H; clear H end;
426 spec_t'.
427
428 Lemma min_elt_spec1 : min_elt s = Some x -> In x s.
429 Proof using Type. elt_spec W'.min_elt_spec1. Qed.
430 Lemma min_elt_spec2 : min_elt s = Some x -> In y s -> ~ E.lt y x.
431 Proof using Type. elt_spec W'.min_elt_spec2. Qed.
432 Lemma min_elt_spec3 : min_elt s = None -> Empty s.
433 Proof using Type. elt_spec W'.min_elt_spec3. Qed.
434
435 Lemma max_elt_spec1 : max_elt s = Some x -> In x s.
436 Proof using Type. elt_spec W'.max_elt_spec1. Qed.
437 Lemma max_elt_spec2 : max_elt s = Some x -> In y s -> ~ E.lt x y.
438 Proof using Type. elt_spec W'.max_elt_spec2. Qed.
439 Lemma max_elt_spec3 : max_elt s = None -> Empty s.
440 Proof using Type. elt_spec W'.max_elt_spec3. Qed.
441
442 Lemma choose_spec3 : choose s = Some x -> choose s' = Some y ->
443 Equal s s' -> E.eq x y.
444 Proof using Type.
445 spec_t'.
446 eapply E.Proper_of_, W'.choose_spec3; eassumption.
447 Qed.
448 End Spec.
449 End IsoSetsOn.
450
451 Module IsoWSets (W' : WSets) (E0 : IsoDecidableType W'.E) <: WSets.
452 Module E := E0.
453 Include IsoWSetsOn W'.E W' E.
454 End IsoWSets.
455
456 Module IsoSets (W' : Sets) (E0 : IsoOrderedType W'.E) <: Sets.
457 Module E := E0.
458 Include IsoSetsOn W'.E W' E.
459 End IsoSets.