| 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.Sum. |
| 13 |
Require Import Crypto.Util.Structures.Equalities.Sum. |
| 14 |
Require Import Crypto.Util.Structures.Orders.Sum. |
| 15 |
Require Import Crypto.Util.Tactics.SplitInContext. |
| 16 |
Require Import Crypto.Util.Tactics.DestructHead. |
| 17 |
Require Import Crypto.Util.Tactics.BreakMatch. |
| 18 |
Require Import Crypto.Util.Tactics.SetoidSubst. |
| 19 |
|
| 20 |
Local Set Implicit Arguments. |
| 21 |
|
| 22 |
Module WSumSetsOn (E1 : Equalities.DecidableType) (E2 : Equalities.DecidableType) |
| 23 |
(W1 : WSetsOn E1) (W2 : WSetsOn E2). (* <: WSetsOn (SumDecidableType E1 E2). *) |
| 24 |
Module Import WSumSetsOn_E. |
| 25 |
Module E := SumDecidableType E1 E2. |
| 26 |
End WSumSetsOn_E. |
| 27 |
Local Existing Instances E.eq_equiv W1.In_compat W2.In_compat. |
| 28 |
|
| 29 |
Definition elt := E.t. |
| 30 |
Definition t := (W1.t * W2.t)%type. |
| 31 |
|
| 32 |
Definition lift_tT {A B C} (F : A -> B -> C) |
| 33 |
(f1 : W1.t -> A) (f2 : W2.t -> B) |
| 34 |
: t -> C |
| 35 |
:= fun x => F (f1 (fst x)) (f2 (snd x)). |
| 36 |
|
| 37 |
Definition lift_ttT {A B C} (F : A -> B -> C) |
| 38 |
(f1 : W1.t -> W1.t -> A) (f2 : W2.t -> W2.t -> B) |
| 39 |
: t -> t -> C |
| 40 |
:= fun x y => F (f1 (fst x) (fst y)) (f2 (snd x) (snd y)). |
| 41 |
|
| 42 |
Definition lift_eT {A} (f1 : E1.t -> A) (f2 : E2.t -> A) |
| 43 |
: elt -> A |
| 44 |
:= fun e => match e with |
| 45 |
| inl e => f1 e |
| 46 |
| inr e => f2 e |
| 47 |
end. |
| 48 |
|
| 49 |
Definition drop_eT {A} (f : elt -> A) |
| 50 |
: (E1.t -> A) * (E2.t -> A) |
| 51 |
:= (fun e => f (inl e), fun e => f (inr e)). |
| 52 |
|
| 53 |
Definition lift_et (f1 : E1.t -> W1.t) (f2 : E2.t -> W2.t) |
| 54 |
: elt -> t |
| 55 |
:= fun e => (match e with |
| 56 |
| inl e => f1 e |
| 57 |
| inr _ => W1.empty |
| 58 |
end, |
| 59 |
match e with |
| 60 |
| inl _ => W2.empty |
| 61 |
| inr e => f2 e |
| 62 |
end). |
| 63 |
|
| 64 |
Definition lift_etT {A} (f1 : E1.t -> W1.t -> A) (f2 : E2.t -> W2.t -> A) |
| 65 |
: elt -> t -> A |
| 66 |
:= fun e x => match e with |
| 67 |
| inl e => f1 e (fst x) |
| 68 |
| inr e => f2 e (snd x) |
| 69 |
end. |
| 70 |
|
| 71 |
Definition lift_ett (f1 : E1.t -> W1.t -> W1.t) (f2 : E2.t -> W2.t -> W2.t) |
| 72 |
: elt -> t -> t |
| 73 |
:= fun e x => (match e with |
| 74 |
| inl e => f1 e (fst x) |
| 75 |
| inr _ => fst x |
| 76 |
end, |
| 77 |
match e with |
| 78 |
| inl _ => snd x |
| 79 |
| inr e => f2 e (snd x) |
| 80 |
end). |
| 81 |
|
| 82 |
Definition lift_ttt (f1 : W1.t -> W1.t -> W1.t) (f2 : W2.t -> W2.t -> W2.t) |
| 83 |
: t -> t -> t |
| 84 |
:= fun x y => (f1 (fst x) (fst y), f2 (snd x) (snd y)). |
| 85 |
|
| 86 |
Definition lift_eT_tT {A B} (F : B -> B -> B) |
| 87 |
(f1 : (E1.t -> A) -> W1.t -> B) (f2 : (E2.t -> A) -> W2.t -> B) |
| 88 |
: (elt -> A) -> t -> B |
| 89 |
:= fun f x => F (f1 (fst (drop_eT f)) (fst x)) |
| 90 |
(f2 (snd (drop_eT f)) (snd x)). |
| 91 |
|
| 92 |
Definition lift_eT_tt {A} |
| 93 |
(f1 : (E1.t -> A) -> W1.t -> W1.t) (f2 : (E2.t -> A) -> W2.t -> W2.t) |
| 94 |
: (elt -> A) -> t -> t |
| 95 |
:= fun f x => (f1 (fst (drop_eT f)) (fst x), |
| 96 |
f2 (snd (drop_eT f)) (snd x)). |
| 97 |
|
| 98 |
Definition lift_eT_tt2 {A} |
| 99 |
(f1 : (E1.t -> A) -> W1.t -> W1.t * W1.t) (f2 : (E2.t -> A) -> W2.t -> W2.t * W2.t) |
| 100 |
: (elt -> A) -> t -> t * t |
| 101 |
:= fun f x => let f1v := f1 (fst (drop_eT f)) (fst x) in |
| 102 |
let f2v := f2 (snd (drop_eT f)) (snd x) in |
| 103 |
((fst f1v, fst f2v), (snd f1v, snd f2v)). |
| 104 |
|
| 105 |
Definition choose_option {A} (x : option A) (y : option A) : option A |
| 106 |
:= match x, y with |
| 107 |
| Some v, _ | _, Some v => Some v |
| 108 |
| None, None => None |
| 109 |
end. |
| 110 |
|
| 111 |
Definition choose_option_sum {A B} (v1 : option A) (v2 : option B) |
| 112 |
:= choose_option |
| 113 |
(option_map (@inl _ _) v1) |
| 114 |
(option_map (@inr _ _) v2). |
| 115 |
|
| 116 |
Definition choose_option_sum_rev {A B} (v1 : option A) (v2 : option B) |
| 117 |
:= choose_option |
| 118 |
(option_map (@inr _ _) v2) |
| 119 |
(option_map (@inl _ _) v1). |
| 120 |
|
| 121 |
Definition empty : t := (W1.empty, W2.empty). |
| 122 |
Definition is_empty : t -> bool := lift_tT andb W1.is_empty W2.is_empty. |
| 123 |
Definition mem : elt -> t -> bool := lift_etT W1.mem W2.mem. |
| 124 |
Definition add : elt -> t -> t := lift_ett W1.add W2.add. |
| 125 |
Definition remove : elt -> t -> t := lift_ett W1.remove W2.remove. |
| 126 |
Definition singleton : elt -> t := lift_et W1.singleton W2.singleton. |
| 127 |
Definition union : t -> t -> t := lift_ttt W1.union W2.union. |
| 128 |
Definition inter : t -> t -> t := lift_ttt W1.inter W2.inter. |
| 129 |
Definition diff : t -> t -> t := lift_ttt W1.diff W2.diff. |
| 130 |
Definition equal : t -> t -> bool := lift_ttT andb W1.equal W2.equal. |
| 131 |
Definition subset : t -> t -> bool := lift_ttT andb W1.subset W2.subset. |
| 132 |
Definition fold B : (elt -> B -> B) -> t -> B -> B |
| 133 |
:= fun f v i |
| 134 |
=> @W2.fold |
| 135 |
B (snd (drop_eT f)) (snd v) |
| 136 |
(@W1.fold |
| 137 |
B (fst (drop_eT f)) (fst v) |
| 138 |
i). |
| 139 |
Definition for_all : (elt -> bool) -> t -> bool := lift_eT_tT andb W1.for_all W2.for_all. |
| 140 |
Definition exists_ : (elt -> bool) -> t -> bool := lift_eT_tT orb W1.exists_ W2.exists_. |
| 141 |
Definition filter : (elt -> bool) -> t -> t := lift_eT_tt W1.filter W2.filter. |
| 142 |
Definition partition : (elt -> bool) -> t -> t * t := lift_eT_tt2 W1.partition W2.partition. |
| 143 |
Definition cardinal : t -> nat := lift_tT Nat.add W1.cardinal W2.cardinal. |
| 144 |
Definition elements : t -> list elt |
| 145 |
:= lift_tT (fun l1 l2 => List.map (@inl _ _) l1 ++ List.map (@inr _ _) l2) |
| 146 |
W1.elements |
| 147 |
W2.elements. |
| 148 |
Definition choose : t -> option elt |
| 149 |
:= lift_tT choose_option_sum W1.choose W2.choose. |
| 150 |
Definition In : elt -> t -> Prop := lift_etT W1.In W2.In. |
| 151 |
|
| 152 |
Local Ltac t_Proper := |
| 153 |
cbv -[fst snd] in *; |
| 154 |
repeat first [ progress cbn [fst snd] in * |
| 155 |
| progress intros |
| 156 |
| progress destruct_head'_sum |
| 157 |
| progress split_and |
| 158 |
| progress firstorder |
| 159 |
| match goal with |
| 160 |
| [ H : forall x y : _ + _, _ |- _ ] |
| 161 |
=> pose proof (fun x y => H (inl x) (inl y)); |
| 162 |
pose proof (fun x y => H (inl x) (inr y)); |
| 163 |
pose proof (fun x y => H (inr x) (inl y)); |
| 164 |
pose proof (fun x y => H (inr x) (inr y)); |
| 165 |
clear H |
| 166 |
end ]. |
| 167 |
|
| 168 |
Local Instance Proper_lift_tT {A B C RA RB RC R1 R2} |
| 169 |
: Proper ((RA ==> RB ==> RC) ==> (R1 ==> RA) ==> (R2 ==> RB) ==> RelProd R1 R2 ==> RC) (@lift_tT A B C). |
| 170 |
Proof. t_Proper. Qed. |
| 171 |
|
| 172 |
Local Instance Proper_lift_ttT {A B C RA RB RC R1 R2} |
| 173 |
: Proper ((RA ==> RB ==> RC) ==> (R1 ==> R1 ==> RA) ==> (R2 ==> R2 ==> RB) ==> RelProd R1 R2 ==> RelProd R1 R2 ==> RC) (@lift_ttT A B C). |
| 174 |
Proof. t_Proper. Qed. |
| 175 |
|
| 176 |
Local Instance Proper_lift_eT {A RA} |
| 177 |
: Proper ((E1.eq ==> RA) ==> (E2.eq ==> RA) ==> E.eq ==> RA) (@lift_eT A). |
| 178 |
Proof. t_Proper. Qed. |
| 179 |
|
| 180 |
Local Instance Proper_drop_eT {A RA} |
| 181 |
: Proper ((E.eq ==> RA) ==> RelProd (E1.eq ==> RA) (E2.eq ==> RA)) (@drop_eT A). |
| 182 |
Proof. t_Proper. Qed. |
| 183 |
|
| 184 |
Local Instance Proper_lift_et {R1 R2} |
| 185 |
{R1_empty : Proper R1 W1.empty} |
| 186 |
{R2_empty : Proper R2 W2.empty} |
| 187 |
: Proper ((E1.eq ==> R1) ==> (E2.eq ==> R2) ==> E.eq ==> RelProd R1 R2) lift_et. |
| 188 |
Proof. t_Proper. Qed. |
| 189 |
|
| 190 |
Local Instance Proper_lift_etT {A RA R1 R2} |
| 191 |
: Proper ((E1.eq ==> R1 ==> RA) ==> (E2.eq ==> R2 ==> RA) ==> E.eq ==> RelProd R1 R2 ==> RA) (@lift_etT A). |
| 192 |
Proof. t_Proper. Qed. |
| 193 |
|
| 194 |
Local Instance Proper_lift_ett {R1 R2} |
| 195 |
: Proper ((E1.eq ==> R1 ==> R1) ==> (E2.eq ==> R2 ==> R2) ==> E.eq ==> RelProd R1 R2 ==> RelProd R1 R2) |
| 196 |
lift_ett. |
| 197 |
Proof. t_Proper. Qed. |
| 198 |
|
| 199 |
Local Instance Proper_lift_ttt {R1 R2} |
| 200 |
: Proper ((R1 ==> R1 ==> R1) ==> (R2 ==> R2 ==> R2) ==> (RelProd R1 R2 ==> RelProd R1 R2 ==> RelProd R1 R2)) |
| 201 |
lift_ttt. |
| 202 |
Proof. t_Proper. Qed. |
| 203 |
|
| 204 |
Local Instance Proper_lift_eT_tT {A B RA RB R1 R2} |
| 205 |
: Proper ((RB ==> RB ==> RB) ==> ((E1.eq ==> RA) ==> R1 ==> RB) ==> ((E2.eq ==> RA) ==> R2 ==> RB) ==> (E.eq ==> RA) ==> RelProd R1 R2 ==> RB) |
| 206 |
(@lift_eT_tT A B). |
| 207 |
Proof. t_Proper. Qed. |
| 208 |
|
| 209 |
Local Instance Proper_lift_eT_tt {A RA R1 R2} |
| 210 |
: Proper (((E1.eq ==> RA) ==> R1 ==> R1) ==> ((E2.eq ==> RA) ==> R2 ==> R2) ==> (E.eq ==> RA) ==> RelProd R1 R2 ==> RelProd R1 R2) |
| 211 |
(@lift_eT_tt A). |
| 212 |
Proof. t_Proper. Qed. |
| 213 |
|
| 214 |
Local Instance Proper_lift_eT_tt2 {A RA R1 R2} |
| 215 |
: Proper (((E1.eq ==> RA) ==> R1 ==> R1 * R1) ==> ((E2.eq ==> RA) ==> R2 ==> R2 * R2) ==> (E.eq ==> RA) ==> RelProd R1 R2 ==> (R1 * R2) * (R1 * R2)) |
| 216 |
(@lift_eT_tt2 A). |
| 217 |
Proof. t_Proper. Qed. |
| 218 |
|
| 219 |
Local Existing Instance eq_subrelation. |
| 220 |
|
| 221 |
Global Instance In_compat : Proper (E.eq ==> eq ==> iff) In | 5. |
| 222 |
Proof. |
| 223 |
cbv [In]. |
| 224 |
partial_application_tactic. |
| 225 |
partial_application_tactic. |
| 226 |
class_apply @subrelation_proper. |
| 227 |
typeclasses eauto. |
| 228 |
exact _. |
| 229 |
repeat match goal with |
| 230 |
| [ |- subrelation (_ ==> _) (_ ==> _) ] |
| 231 |
=> class_apply @subrelation_respectful |
| 232 |
end. |
| 233 |
all: try reflexivity. |
| 234 |
all: typeclasses eauto. |
| 235 |
Qed. |
| 236 |
Definition Equal s s' := forall a : elt, In a s <-> In a s'. |
| 237 |
Definition Subset s s' := forall a : elt, In a s -> In a s'. |
| 238 |
Definition Empty s := forall a : elt, ~ In a s. |
| 239 |
Definition For_all (P : elt -> Prop) s := forall x, In x s -> P x. |
| 240 |
Definition Exists (P : elt -> Prop) s := exists x, In x s /\ P x. |
| 241 |
|
| 242 |
Definition Equal_alt : t -> t -> Prop := lift_ttT and W1.Equal W2.Equal. |
| 243 |
Definition Subset_alt : t -> t -> Prop := lift_ttT and W1.Subset W2.Subset. |
| 244 |
Definition Empty_alt : t -> Prop := lift_tT and W1.Empty W2.Empty. |
| 245 |
Definition For_all_alt : (elt -> Prop) -> t -> Prop := lift_eT_tT and W1.For_all W2.For_all. |
| 246 |
Definition Exists_alt : (elt -> Prop) -> t -> Prop := lift_eT_tT or W1.Exists W2.Exists. |
| 247 |
|
| 248 |
Lemma forall_lift_etT_dep {A f1 f2} (P : elt -> (t -> A) -> Prop) |
| 249 |
: (forall a : elt, P a (@lift_etT A f1 f2 a)) |
| 250 |
<-> ((forall a : W1.elt, P (inl a) (fun v => f1 a (fst v))) |
| 251 |
/\ (forall a : W2.elt, P (inr a) (fun v => f2 a (snd v)))). |
| 252 |
Proof. |
| 253 |
cbv [lift_etT]. |
| 254 |
split; [ intro H; split; intro x; [ specialize (H (inl x)) | specialize (H (inr x)) ] |
| 255 |
| intros [H1 H2] [x|x]; [ specialize (H1 x) | specialize (H2 x) ] ]; |
| 256 |
assumption. |
| 257 |
Qed. |
| 258 |
|
| 259 |
Lemma exists_lift_etT_dep {A f1 f2} (P : elt -> (t -> A) -> Prop) |
| 260 |
: (exists a : elt, P a (@lift_etT A f1 f2 a)) |
| 261 |
<-> ((exists a : W1.elt, P (inl a) (fun v => f1 a (fst v))) |
| 262 |
\/ (exists a : W2.elt, P (inr a) (fun v => f2 a (snd v)))). |
| 263 |
Proof. |
| 264 |
cbv [lift_etT]. |
| 265 |
split; [ intros [[x|x] H]; constructor; exists x; assumption |
| 266 |
| intros [[x H]|[x H]]; (unshelve econstructor; [ constructor; exact x | assumption ]) ]. |
| 267 |
Qed. |
| 268 |
|
| 269 |
Local Ltac t_alt_iff := |
| 270 |
cbv [In |
| 271 |
Equal_alt Subset_alt Empty_alt For_all_alt Exists_alt |
| 272 |
Equal Subset Empty For_all Exists |
| 273 |
W1.Equal W1.Subset W1.Empty W1.For_all W1.Exists |
| 274 |
W2.Equal W2.Subset W2.Empty W2.For_all W2.Exists]; |
| 275 |
let get_P lift_f P := |
| 276 |
lazymatch P with |
| 277 |
| fun a : ?A => ?P |
| 278 |
=> let P' := fresh in |
| 279 |
constr:( |
| 280 |
fun a : elt |
| 281 |
=> match P return _ with |
| 282 |
| P' |
| 283 |
=> ltac:(let P := (eval cbv delta [P'] in P') in |
| 284 |
clear P'; |
| 285 |
lazymatch (eval pattern (lift_f a) in P) with |
| 286 |
| ?P _ => exact P |
| 287 |
end) |
| 288 |
end) |
| 289 |
end in |
| 290 |
lazymatch goal with |
| 291 |
| [ |- context[lift_etT ?f1 ?f2] ] |
| 292 |
=> lazymatch goal with |
| 293 |
| [ |- (forall a : elt, @?P a) <-> _ ] |
| 294 |
=> let P' := get_P (lift_etT f1 f2) P in |
| 295 |
setoid_rewrite (@forall_lift_etT_dep _ f1 f2 P') |
| 296 |
| [ |- (exists a : elt, @?P a) <-> _ ] |
| 297 |
=> let P' := get_P (lift_etT f1 f2) P in |
| 298 |
setoid_rewrite (@exists_lift_etT_dep _ f1 f2 P') |
| 299 |
end |
| 300 |
end; |
| 301 |
try reflexivity. |
| 302 |
|
| 303 |
Lemma Equal_alt_iff s s' : Equal s s' <-> Equal_alt s s'. |
| 304 |
Proof. t_alt_iff. Qed. |
| 305 |
Lemma Subset_alt_iff s s' : Subset s s' <-> Subset_alt s s'. |
| 306 |
Proof. t_alt_iff. Qed. |
| 307 |
Lemma Empty_alt_iff s : Empty s <-> Empty_alt s. |
| 308 |
Proof. t_alt_iff. Qed. |
| 309 |
Lemma For_all_alt_iff P s |
| 310 |
(P_Proper : Proper (E.eq ==> iff) P) |
| 311 |
: For_all P s <-> For_all_alt P s. |
| 312 |
Proof. t_alt_iff. Qed. |
| 313 |
Lemma Exists_alt_iff P s |
| 314 |
(P_Proper : Proper (E.eq ==> iff) P) |
| 315 |
: Exists P s <-> Exists_alt P s. |
| 316 |
Proof. t_alt_iff. Qed. |
| 317 |
|
| 318 |
Local Notation "s [=] t" := (Equal s t) (at level 70, no associativity). |
| 319 |
Local Notation "s [<=] t" := (Subset s t) (at level 70, no associativity). |
| 320 |
|
| 321 |
Definition eq := Equal. |
| 322 |
Local Instance eq_equiv : Equivalence eq. |
| 323 |
Proof. |
| 324 |
cbv [eq Equal]; split; repeat intro. |
| 325 |
{ reflexivity. } |
| 326 |
{ symmetry; auto. } |
| 327 |
{ etransitivity; eauto. } |
| 328 |
Qed. |
| 329 |
Lemma eq_dec (x y : t) : {eq x y} + {~eq x y}. |
| 330 |
Proof. |
| 331 |
destruct (W1.eq_dec (fst x) (fst y)) as [H1|H1], (W2.eq_dec (snd x) (snd y)) as [H2|H2]; |
| 332 |
[ left | right.. ]; |
| 333 |
cbv [eq]; rewrite Equal_alt_iff; cbv [Equal_alt lift_ttT]; auto; |
| 334 |
intros [? ?]; auto. |
| 335 |
Defined. |
| 336 |
|
| 337 |
Lemma W1_In_empty_False_iff : forall x, W1.In x W1.empty <-> False. |
| 338 |
Proof. pose proof W1.empty_spec. firstorder. Qed. |
| 339 |
Lemma W2_In_empty_False_iff : forall x, W2.In x W2.empty <-> False. |
| 340 |
Proof. pose proof W2.empty_spec. firstorder. Qed. |
| 341 |
|
| 342 |
Create HintDb sum_set_alt discriminated. |
| 343 |
|
| 344 |
#[global] |
| 345 |
Hint Unfold |
| 346 |
empty |
| 347 |
is_empty |
| 348 |
mem |
| 349 |
add |
| 350 |
remove |
| 351 |
singleton |
| 352 |
union |
| 353 |
inter |
| 354 |
diff |
| 355 |
equal |
| 356 |
subset |
| 357 |
fold |
| 358 |
for_all |
| 359 |
exists_ |
| 360 |
filter |
| 361 |
partition |
| 362 |
cardinal |
| 363 |
elements |
| 364 |
choose |
| 365 |
In |
| 366 |
option_map |
| 367 |
lift_ttT |
| 368 |
lift_etT |
| 369 |
lift_tT |
| 370 |
lift_ett |
| 371 |
lift_et |
| 372 |
lift_ttt |
| 373 |
lift_eT_tt |
| 374 |
lift_eT_tT |
| 375 |
lift_eT_tt2 |
| 376 |
drop_eT |
| 377 |
flip |
| 378 |
choose_option |
| 379 |
choose_option_sum |
| 380 |
choose_option_sum_rev |
| 381 |
Empty_alt Equal_alt |
| 382 |
: sum_set_alt. |
| 383 |
|
| 384 |
#[global] |
| 385 |
Hint Rewrite Empty_alt_iff Equal_alt_iff Subset_alt_iff For_all_alt_iff Exists_alt_iff |
| 386 |
W1.is_empty_spec |
| 387 |
W1.mem_spec |
| 388 |
W1.add_spec |
| 389 |
W1.remove_spec |
| 390 |
W1.singleton_spec |
| 391 |
W1.union_spec |
| 392 |
W1.inter_spec |
| 393 |
W1.diff_spec |
| 394 |
W1.equal_spec |
| 395 |
W1.subset_spec |
| 396 |
W1.fold_spec |
| 397 |
W1.for_all_spec |
| 398 |
W1.exists_spec |
| 399 |
W1.partition_spec1 |
| 400 |
W1.partition_spec2 |
| 401 |
W1.filter_spec |
| 402 |
W1.cardinal_spec |
| 403 |
W1_In_empty_False_iff |
| 404 |
W2.is_empty_spec |
| 405 |
W2.mem_spec |
| 406 |
W2.add_spec |
| 407 |
W2.remove_spec |
| 408 |
W2.singleton_spec |
| 409 |
W2.union_spec |
| 410 |
W2.inter_spec |
| 411 |
W2.diff_spec |
| 412 |
W2.equal_spec |
| 413 |
W2.subset_spec |
| 414 |
W2.fold_spec |
| 415 |
W2.for_all_spec |
| 416 |
W2.exists_spec |
| 417 |
W2.partition_spec1 |
| 418 |
W2.partition_spec2 |
| 419 |
W2.filter_spec |
| 420 |
W2.cardinal_spec |
| 421 |
W2_In_empty_False_iff |
| 422 |
app_length |
| 423 |
map_length |
| 424 |
fold_left_app |
| 425 |
fold_left_map |
| 426 |
(@ForallOrdPairs_map_iff) |
| 427 |
Bool.andb_true_iff |
| 428 |
Bool.orb_true_iff |
| 429 |
: sum_set_alt. |
| 430 |
|
| 431 |
Local Hint Resolve |
| 432 |
W1.empty_spec |
| 433 |
W1.elements_spec2w |
| 434 |
W1.choose_spec1 |
| 435 |
W1.choose_spec2 |
| 436 |
W2.empty_spec |
| 437 |
W2.elements_spec2w |
| 438 |
W2.choose_spec1 |
| 439 |
W2.choose_spec2 |
| 440 |
: core. |
| 441 |
|
| 442 |
Local Ltac spec_t := |
| 443 |
repeat first [ reflexivity |
| 444 |
| progress cbn [fst snd E.eq sumwise] in * |
| 445 |
| progress intros |
| 446 |
| progress repeat autorewrite with sum_set_alt |
| 447 |
| progress repeat autounfold with sum_set_alt in * |
| 448 |
| progress auto |
| 449 |
| break_innermost_match_step |
| 450 |
| clear; tauto |
| 451 |
| exact _ |
| 452 |
| apply conj; reflexivity ]. |
| 453 |
|
| 454 |
Local Hint Extern 2 => Proper_compose_hint : typeclass_instances. |
| 455 |
|
| 456 |
Section Spec. |
| 457 |
Variable s s': t. |
| 458 |
Variable x y : elt. |
| 459 |
Variable f : elt -> bool. |
| 460 |
Notation compatb := (Proper (E.eq==>Logic.eq)) (only parsing). |
| 461 |
Lemma mem_spec : mem x s = true <-> In x s. |
| 462 |
Proof using Type. spec_t. Qed. |
| 463 |
Lemma equal_spec : equal s s' = true <-> s[=]s'. |
| 464 |
Proof using Type. spec_t. Qed. |
| 465 |
Lemma subset_spec : subset s s' = true <-> s[<=]s'. |
| 466 |
Proof using Type. spec_t. Qed. |
| 467 |
Lemma empty_spec : Empty empty. |
| 468 |
Proof using Type. spec_t. Qed. |
| 469 |
Lemma is_empty_spec : is_empty s = true <-> Empty s. |
| 470 |
Proof using Type. spec_t. Qed. |
| 471 |
Lemma add_spec : In y (add x s) <-> E.eq y x \/ In y s. |
| 472 |
Proof using Type. spec_t. Qed. |
| 473 |
Lemma remove_spec : In y (remove x s) <-> In y s /\ ~E.eq y x. |
| 474 |
Proof using Type. spec_t. Qed. |
| 475 |
Lemma singleton_spec : In y (singleton x) <-> E.eq y x. |
| 476 |
Proof using Type. spec_t. Qed. |
| 477 |
Lemma union_spec : In x (union s s') <-> In x s \/ In x s'. |
| 478 |
Proof using Type. spec_t. Qed. |
| 479 |
Lemma inter_spec : In x (inter s s') <-> In x s /\ In x s'. |
| 480 |
Proof using Type. spec_t. Qed. |
| 481 |
Lemma diff_spec : In x (diff s s') <-> In x s /\ ~In x s'. |
| 482 |
Proof using Type. spec_t. Qed. |
| 483 |
Lemma fold_spec : forall (A : Type) (i : A) (f : elt -> A -> A), |
| 484 |
fold f s i = fold_left (flip f) (elements s) i. |
| 485 |
Proof using Type. spec_t. Qed. |
| 486 |
Lemma cardinal_spec : cardinal s = length (elements s). |
| 487 |
Proof using Type. spec_t. Qed. |
| 488 |
Lemma filter_spec : compatb f -> |
| 489 |
(In x (filter f s) <-> In x s /\ f x = true). |
| 490 |
Proof using Type. spec_t. Qed. |
| 491 |
Lemma for_all_spec : compatb f -> |
| 492 |
(for_all f s = true <-> For_all (fun x => f x = true) s). |
| 493 |
Proof using Type. spec_t. repeat intro; repeat (f_equiv; trivial). Qed. |
| 494 |
Lemma exists_spec : compatb f -> |
| 495 |
(exists_ f s = true <-> Exists (fun x => f x = true) s). |
| 496 |
Proof using Type. spec_t. repeat intro; repeat (f_equiv; trivial). Qed. |
| 497 |
Lemma partition_spec1 : compatb f -> |
| 498 |
fst (partition f s) [=] filter f s. |
| 499 |
Proof using Type. spec_t. Qed. |
| 500 |
Lemma partition_spec2 : compatb f -> |
| 501 |
snd (partition f s) [=] filter (fun x => negb (f x)) s. |
| 502 |
Proof using Type. spec_t. Qed. |
| 503 |
Lemma elements_spec1 : InA E.eq x (elements s) <-> In x s. |
| 504 |
Proof using Type. |
| 505 |
cbv [In lift_tT lift_etT elements]; |
| 506 |
rewrite InA_app_iff; |
| 507 |
break_innermost_match; |
| 508 |
progress rewrite <- ?W1.elements_spec1, <- ?W2.elements_spec1. |
| 509 |
all: split; [ intros [H|H]; revert H | ]; |
| 510 |
try (apply InA_map_iff; reflexivity); |
| 511 |
try (intro H; constructor; revert H; apply InA_map_iff; reflexivity). |
| 512 |
all: intro H; exfalso; revert H. |
| 513 |
all: rewrite InA_alt; setoid_rewrite in_map_iff; |
| 514 |
intros [[?|?] [H1 [? [? ?]]]]; cbn [E.eq sumwise] in *; |
| 515 |
inversion_sum; |
| 516 |
try assumption. |
| 517 |
Qed. |
| 518 |
|
| 519 |
Lemma elements_spec2w : NoDupA E.eq (elements s). |
| 520 |
Proof using Type. |
| 521 |
clear. |
| 522 |
pose proof (W1.elements_spec2w (fst s)). |
| 523 |
pose proof (W2.elements_spec2w (snd s)). |
| 524 |
spec_t. |
| 525 |
apply NoDupA_app; try exact _. |
| 526 |
1,2: rewrite NoDupA_altdef, ForallOrdPairs_map_iff in *; assumption. |
| 527 |
{ setoid_rewrite InA_alt; setoid_rewrite in_map_iff; |
| 528 |
firstorder; subst; cbv [E.eq sumwise] in *; break_innermost_match_hyps; |
| 529 |
try assumption. } |
| 530 |
Qed. |
| 531 |
Lemma choose_spec1 : choose s = Some x -> In x s. |
| 532 |
Proof using Type. |
| 533 |
pose proof (@W1.choose_spec1 (fst s)). |
| 534 |
pose proof (@W2.choose_spec1 (snd s)). |
| 535 |
cbv [In choose option_map choose_option choose_option_sum lift_etT lift_tT]. |
| 536 |
break_innermost_match; intros; inversion_option; inversion_sum; subst; eauto. |
| 537 |
Qed. |
| 538 |
Lemma choose_spec2 : choose s = None -> Empty s. |
| 539 |
Proof using Type. spec_t; break_innermost_match_hyps; inversion_option; auto. Qed. |
| 540 |
End Spec. |
| 541 |
End WSumSetsOn. |
| 542 |
|
| 543 |
Module SumSetsOn (E1 : Orders.OrderedType) (E2 : Orders.OrderedType) |
| 544 |
(W1 : SetsOn E1) (W2 : SetsOn E2) (* <: SetsOn (SumOrderedType E1 E2) *). |
| 545 |
Include WSumSetsOn E1 E2 W1 W2. |
| 546 |
Module Import SumSetsOn_E. |
| 547 |
Module E := SumOrderedType E1 E2. |
| 548 |
End SumSetsOn_E. |
| 549 |
Local Existing Instances E.lt_strorder E.lt_compat. |
| 550 |
Local Existing Instances E.eq_equiv W1.In_compat W2.In_compat. |
| 551 |
|
| 552 |
Definition choose_comparison (x y : comparison) : comparison |
| 553 |
:= match x with |
| 554 |
| Eq => y |
| 555 |
| _ => x |
| 556 |
end. |
| 557 |
|
| 558 |
Definition prod_lt {A B} (A_eq A_lt : relation A) (B_lt : relation B) : relation (A * B) |
| 559 |
:= fun x y => A_lt (fst x) (fst y) \/ (A_eq (fst x) (fst y) /\ B_lt (snd x) (snd y)). |
| 560 |
|
| 561 |
Definition compare : t -> t -> comparison := lift_ttT choose_comparison W1.compare W2.compare. |
| 562 |
Definition min_elt : t -> option elt := lift_tT choose_option_sum W1.min_elt W2.min_elt. |
| 563 |
Definition max_elt : t -> option elt := lift_tT choose_option_sum_rev W1.max_elt W2.max_elt. |
| 564 |
Definition lt : t -> t -> Prop := prod_lt W1.eq W1.lt W2.lt. |
| 565 |
#[global] |
| 566 |
Hint Unfold compare min_elt max_elt lt W1.eq prod_lt : sum_set_alt. |
| 567 |
|
| 568 |
#[global] |
| 569 |
Instance lt_strorder : StrictOrder lt | 1. |
| 570 |
Proof. |
| 571 |
destruct W1.eq_equiv, W1.lt_strorder, W2.lt_strorder. |
| 572 |
cbv [W1.eq lt prod_lt] in *. |
| 573 |
split. |
| 574 |
{ intros [x1 x2] H. |
| 575 |
pose proof (@irreflexivity _ W1.lt _ x1). |
| 576 |
pose proof (@irreflexivity _ W2.lt _ x2). |
| 577 |
pose proof (@reflexivity _ W1.Equal _ x1). |
| 578 |
cbv [complement] in *. |
| 579 |
destruct_head'_or; destruct_head'_and; eauto. } |
| 580 |
{ intros [x1 x2] [y1 y2] [z1 z2]; cbn [fst snd] in *; intros. |
| 581 |
pose proof (@transitivity _ W1.lt _ x1 y1 z1). |
| 582 |
pose proof (@transitivity _ W2.lt _ x2 y2 z2). |
| 583 |
pose proof (@W1.lt_compat); cbv [Proper respectful] in *. |
| 584 |
split_iff. |
| 585 |
destruct_head'_or; destruct_head'_and; eauto 5. } |
| 586 |
Qed. |
| 587 |
|
| 588 |
#[global] |
| 589 |
Instance lt_compat : Proper (eq==>eq==>iff) lt | 1. |
| 590 |
Proof. |
| 591 |
pose proof W1.lt_compat as H1; pose proof W2.lt_compat as H2. |
| 592 |
pose proof (@reflexivity _ W1.Equal _). |
| 593 |
pose proof (@reflexivity _ W2.Equal _). |
| 594 |
all: repeat first [ progress cbv [eq lt prod_lt W1.eq W2.eq Proper respectful fst snd Equal_alt lift_ttT] in * |
| 595 |
| progress rewrite Equal_alt_iff in * |
| 596 |
| progress destruct_head'_and |
| 597 |
| progress intros |
| 598 |
| progress split_iff |
| 599 |
| progress break_innermost_match_hyps |
| 600 |
| progress setoid_subst_rel W1.Equal |
| 601 |
| progress setoid_subst_rel W2.Equal |
| 602 |
| progress destruct_head'_or |
| 603 |
| apply conj |
| 604 |
| solve [ auto ] ]. |
| 605 |
Qed. |
| 606 |
|
| 607 |
Local Ltac spec_t' := |
| 608 |
repeat first [ reflexivity |
| 609 |
| progress cbn [fst snd E.eq sumwise] in * |
| 610 |
| progress intros |
| 611 |
| progress repeat autorewrite with sum_set_alt |
| 612 |
| progress repeat autounfold with sum_set_alt in * |
| 613 |
| progress auto |
| 614 |
| break_innermost_match_step |
| 615 |
| clear; tauto |
| 616 |
| exact _ |
| 617 |
| assumption |
| 618 |
| symmetry; assumption |
| 619 |
| apply conj; reflexivity |
| 620 |
| progress break_innermost_match_hyps |
| 621 |
| progress subst |
| 622 |
| progress inversion_option |
| 623 |
| progress inversion_sum |
| 624 |
| progress destruct_head'_and |
| 625 |
| exfalso; now eauto ]. |
| 626 |
|
| 627 |
Section Spec. |
| 628 |
Variable s s': t. |
| 629 |
Variable x y : elt. |
| 630 |
|
| 631 |
Lemma compare_spec : CompSpec eq lt s s' (compare s s'). |
| 632 |
Proof using Type. |
| 633 |
pose proof (@symmetry _ W1.Equal _). |
| 634 |
cbv [compare eq lt prod_lt lift_ttT choose_comparison W1.eq]. |
| 635 |
destruct (W1.compare_spec (fst s) (fst s')), (W2.compare_spec (snd s) (snd s')); |
| 636 |
cbv [W1.eq W2.eq] in *; |
| 637 |
constructor; now spec_t'. |
| 638 |
Qed. |
| 639 |
|
| 640 |
Lemma elements_spec2 : sort E.lt (elements s). |
| 641 |
Proof using Type. |
| 642 |
cbv [elements lift_tT]. |
| 643 |
eapply SortA_app; rewrite ?Sorted_map_iff; |
| 644 |
try (apply W1.elements_spec2 + apply W2.elements_spec2 + apply E.eq_equiv). |
| 645 |
cbv [sumwise]. |
| 646 |
intros ??; setoid_rewrite InA_alt; setoid_rewrite in_map_iff; intros. |
| 647 |
repeat first [ progress destruct_head'_ex |
| 648 |
| progress destruct_head'_and |
| 649 |
| progress subst |
| 650 |
| break_innermost_match_hyps_step |
| 651 |
| exfalso; assumption |
| 652 |
| exact I ]. |
| 653 |
Qed. |
| 654 |
|
| 655 |
Ltac elt_spec lem1 lem2 lem3 := |
| 656 |
spec_t'; |
| 657 |
repeat match goal with |
| 658 |
| [ H : _ = _ :> option _ |- _ ] |
| 659 |
=> (eapply lem1 in H + eapply lem2 in H + eapply lem3 in H); [ | (idtac + symmetry); eassumption.. ] |
| 660 |
end; |
| 661 |
try match goal with H : ~ ?R _ _ |- ~ _ => intro; apply H; clear H end; |
| 662 |
cbv [not W1.Empty W2.Empty] in *; |
| 663 |
spec_t'. |
| 664 |
|
| 665 |
Lemma min_elt_spec1 : min_elt s = Some x -> In x s. |
| 666 |
Proof using Type. elt_spec W1.min_elt_spec1 W2.min_elt_spec1 (). Qed. |
| 667 |
Lemma min_elt_spec2 : min_elt s = Some x -> In y s -> ~ E.lt y x. |
| 668 |
Proof using Type. elt_spec W1.min_elt_spec2 W2.min_elt_spec2 W1.min_elt_spec3. Qed. |
| 669 |
Lemma min_elt_spec3 : min_elt s = None -> Empty s. |
| 670 |
Proof using Type. elt_spec W1.min_elt_spec3 W2.min_elt_spec3 (). Qed. |
| 671 |
|
| 672 |
Lemma max_elt_spec1 : max_elt s = Some x -> In x s. |
| 673 |
Proof using Type. elt_spec W1.max_elt_spec1 W2.max_elt_spec1 (). Qed. |
| 674 |
Lemma max_elt_spec2 : max_elt s = Some x -> In y s -> ~ E.lt x y. |
| 675 |
Proof using Type. elt_spec W1.max_elt_spec2 W2.max_elt_spec2 W2.max_elt_spec3. Qed. |
| 676 |
Lemma max_elt_spec3 : max_elt s = None -> Empty s. |
| 677 |
Proof using Type. elt_spec W1.max_elt_spec3 W2.max_elt_spec3 (). Qed. |
| 678 |
|
| 679 |
Lemma choose_spec3 : choose s = Some x -> choose s' = Some y -> |
| 680 |
Equal s s' -> E.eq x y. |
| 681 |
Proof using Type. |
| 682 |
rewrite Equal_alt_iff; cbv [Equal_alt lift_ttT]. |
| 683 |
elt_spec W1.choose_spec3 W2.choose_spec3 (). |
| 684 |
all: cbv [W1.Equal W2.Equal] in *; split_iff. |
| 685 |
all: elt_spec W1.choose_spec2 W2.choose_spec2 (). |
| 686 |
all: elt_spec W1.choose_spec1 W2.choose_spec1 (). |
| 687 |
Qed. |
| 688 |
End Spec. |
| 689 |
End SumSetsOn. |
| 690 |
|
| 691 |
Module WSumSets (W1 : WSets) (W2 : WSets) <: WSets. |
| 692 |
Module E := SumDecidableType W1.E W2.E. |
| 693 |
Include WSumSetsOn W1.E W2.E W1 W2. |
| 694 |
End WSumSets. |
| 695 |
|
| 696 |
Module SumSets (W1 : Sets) (W2 : Sets) <: Sets. |
| 697 |
Module E := SumOrderedType W1.E W2.E. |
| 698 |
Include SumSetsOn W1.E W2.E W1 W2. |
| 699 |
End SumSets. |