| 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 |
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. |
| 14 |
Require Import Crypto.Util.Structures.Orders.Iso. |
| 15 |
Require Import Crypto.Util.Sorting.Sorted.Proper. |
| 16 |
Require Import Crypto.Util.Tactics.SplitInContext. |
| 17 |
Require Import Crypto.Util.Tactics.DestructHead. |
| 18 |
Require Import Crypto.Util.Tactics.SpecializeAllWays. |
| 19 |
Require Import Crypto.Util.Tactics.SpecializeBy. |
| 20 |
Require Import Crypto.Util.Tactics.SpecializeUnderBindersBy. |
| 21 |
Require Import Crypto.Util.Tactics.BreakMatch. |
| 22 |
Require Import Crypto.Util.Tactics.SetoidSubst. |
| 23 |
|
| 24 |
Local Set Implicit Arguments. |
| 25 |
(* TODO: move to global settings *) |
| 26 |
Local Set Keyed Unification. |
| 27 |
|
| 28 |
Module IsoWSfun (E' : DecidableType) (W' : WSfun E') |
| 29 |
(E : IsoDecidableTypeOrig E') <: WSfun E. |
| 30 |
Local Existing Instances E.Proper_to_ E.Proper_of_. |
| 31 |
|
| 32 |
Local Instance E_eq_equiv : Equivalence E.eq | 10. split; hnf; eauto. Qed. |
| 33 |
Local Instance E'_eq_equiv : Equivalence E'.eq | 10. split; hnf; eauto. Qed. |
| 34 |
Local Instance W'_eq_key_equiv elt : Equivalence (@W'.eq_key elt) | 10. split; cbv; eauto. Qed. |
| 35 |
Local Instance W'_eq_key_elt_equiv elt : Equivalence (@W'.eq_key_elt elt) | 10. split; repeat intros [? ?]; cbv in *; subst; eauto. Qed. |
| 36 |
|
| 37 |
Definition key := E.t. |
| 38 |
|
| 39 |
Global Hint Transparent key : core. |
| 40 |
|
| 41 |
Definition t := W'.t. |
| 42 |
|
| 43 |
Definition lift {A} (f : W'.key -> A) : key -> A |
| 44 |
:= fun x => f (E.to_ x). |
| 45 |
Definition liftho {A B} (f : (W'.key -> A) -> B) : (key -> A) -> B |
| 46 |
:= fun f' => f (fun x => f' (E.of_ x)). |
| 47 |
|
| 48 |
Definition empty : forall elt, t elt := W'.empty. |
| 49 |
Definition is_empty : forall elt, t elt -> bool := W'.is_empty. |
| 50 |
Definition add elt : key -> elt -> t elt -> t elt := lift (@W'.add elt). |
| 51 |
Definition find elt : key -> t elt -> option elt := lift (@W'.find elt). |
| 52 |
Definition remove elt : key -> t elt -> t elt := lift (@W'.remove elt). |
| 53 |
Definition mem elt : key -> t elt -> bool := lift (@W'.mem elt). |
| 54 |
Definition map : forall elt elt', (elt -> elt') -> t elt -> t elt' := W'.map. |
| 55 |
Definition mapi elt elt' : (key -> elt -> elt') -> t elt -> t elt' := liftho (@W'.mapi elt elt'). |
| 56 |
Definition map2 : forall elt elt' elt'', (option elt -> option elt' -> option elt'') -> t elt -> t elt' -> t elt'' := W'.map2. |
| 57 |
Definition elements elt (v : t elt) : list (key * elt) := List.map (fun kv => (E.of_ (fst kv), snd kv)) (W'.elements v). |
| 58 |
Definition cardinal : forall elt, t elt -> nat := W'.cardinal. |
| 59 |
Definition fold elt A : (key -> elt -> A -> A) -> t elt -> A -> A := liftho (@W'.fold elt A). |
| 60 |
Definition equal : forall elt, (elt -> elt -> bool) -> t elt -> t elt -> bool := W'.equal. |
| 61 |
Definition MapsTo elt : key -> elt -> t elt -> Prop := lift (@W'.MapsTo elt). |
| 62 |
Definition In elt : key -> t elt -> Prop := lift (@W'.In elt). |
| 63 |
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').*) |
| 64 |
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').*) |
| 65 |
|
| 66 |
Local Instance Proper_lift {A R f} |
| 67 |
{f_Proper : Proper (E'.eq ==> R) f} |
| 68 |
: Proper (E.eq ==> R) (@lift A f). |
| 69 |
Proof. |
| 70 |
cbv [lift]; repeat intro; do 2 f_equiv; assumption. |
| 71 |
Qed. |
| 72 |
|
| 73 |
Local Instance Proper_liftho {A B RA RB f} |
| 74 |
{f_Proper : Proper ((E'.eq ==> RA) ==> RB) f} |
| 75 |
: Proper ((E.eq ==> RA) ==> RB) (@liftho A B f). |
| 76 |
Proof. |
| 77 |
cbv [liftho]; do 3 (repeat intro; f_equiv; try eassumption). |
| 78 |
Qed. |
| 79 |
|
| 80 |
Local Instance Proper_W'_eq_key_elt_iff elt |
| 81 |
: Proper (eq ==> RelationPairs.RelProd E'.eq eq ==> iff) (@W'.eq_key_elt elt). |
| 82 |
Proof. cbv; repeat intro; subst; destruct_head'_prod; destruct_head'_and; subst; firstorder (subst; eauto). Qed. |
| 83 |
|
| 84 |
Local Instance Proper_W'_eq_key_elt_impl elt |
| 85 |
: Proper (eq ==> RelationPairs.RelProd E'.eq eq ==> impl) (@W'.eq_key_elt elt). |
| 86 |
Proof. cbv; repeat intro; subst; destruct_head'_prod; destruct_head'_and; subst; firstorder (subst; eauto). Qed. |
| 87 |
|
| 88 |
Local Instance Proper_W'_eq_key_elt_flip_impl elt |
| 89 |
: Proper (eq ==> RelationPairs.RelProd E'.eq eq ==> flip impl) (@W'.eq_key_elt elt). |
| 90 |
Proof. cbv; repeat intro; subst; destruct_head'_prod; destruct_head'_and; subst; firstorder (subst; eauto). Qed. |
| 91 |
|
| 92 |
Local Instance Proper_W'_eq_key_elt_iff' elt |
| 93 |
: Proper (@W'.eq_key_elt elt ==> @W'.eq_key_elt elt ==> iff) (@W'.eq_key_elt elt). |
| 94 |
Proof. cbv; repeat intro; subst; destruct_head'_prod; destruct_head'_and; subst; firstorder (subst; eauto). Qed. |
| 95 |
|
| 96 |
Local Instance Proper_W'_eq_key_elt_impl' elt |
| 97 |
: Proper (@W'.eq_key_elt elt ==> @W'.eq_key_elt elt ==> impl) (@W'.eq_key_elt elt). |
| 98 |
Proof. cbv; repeat intro; subst; destruct_head'_prod; destruct_head'_and; subst; firstorder (subst; eauto). Qed. |
| 99 |
|
| 100 |
Local Instance Proper_W'_eq_key_elt_flip_impl' elt |
| 101 |
: Proper (@W'.eq_key_elt elt ==> @W'.eq_key_elt elt ==> flip impl) (@W'.eq_key_elt elt). |
| 102 |
Proof. cbv; repeat intro; subst; destruct_head'_prod; destruct_head'_and; subst; firstorder (subst; eauto). Qed. |
| 103 |
|
| 104 |
Local Instance Proper_W'_eq_key_iff elt |
| 105 |
: Proper (@W'.eq_key elt ==> @W'.eq_key elt ==> iff) (@W'.eq_key elt). |
| 106 |
Proof. cbv; repeat intro; subst; destruct_head'_prod; destruct_head'_and; subst; firstorder (subst; eauto). Qed. |
| 107 |
|
| 108 |
Local Instance Proper_W'_eq_key_impl elt |
| 109 |
: Proper (@W'.eq_key elt ==> @W'.eq_key elt ==> impl) (@W'.eq_key elt). |
| 110 |
Proof. cbv; repeat intro; subst; destruct_head'_prod; destruct_head'_and; subst; firstorder (subst; eauto). Qed. |
| 111 |
|
| 112 |
Local Instance Proper_W'_eq_key_flip_impl elt |
| 113 |
: Proper (@W'.eq_key elt ==> @W'.eq_key elt ==> flip impl) (@W'.eq_key elt). |
| 114 |
Proof. cbv; repeat intro; subst; destruct_head'_prod; destruct_head'_and; subst; firstorder (subst; eauto). Qed. |
| 115 |
|
| 116 |
Lemma eq_to_iff x y : E'.eq (E.to_ x) (E.to_ y) <-> E.eq x y. |
| 117 |
Proof. |
| 118 |
split; intro H; [ | now f_equiv ]. |
| 119 |
etransitivity; rewrite <- E.of_to; [ | reflexivity ]; f_equiv; assumption. |
| 120 |
Qed. |
| 121 |
|
| 122 |
Lemma eq_to_of_iff x y |
| 123 |
: E'.eq (E.to_ x) y <-> E.eq x (E.of_ y). |
| 124 |
Proof. |
| 125 |
split; intro H; (rewrite -> H || rewrite <- H); |
| 126 |
(rewrite E.to_of || rewrite E.of_to); |
| 127 |
reflexivity. |
| 128 |
Qed. |
| 129 |
|
| 130 |
Lemma eq_to_of_iff' x y |
| 131 |
: E'.eq y (E.to_ x) <-> E.eq (E.of_ y) x. |
| 132 |
Proof. split; symmetry; apply eq_to_of_iff; symmetry; assumption. Qed. |
| 133 |
|
| 134 |
Local Instance W'_Equal_Equivalence elt : Equivalence (@W'.Equal elt) | 10. |
| 135 |
Proof. split; cbv; firstorder eauto using eq_trans. Qed. |
| 136 |
|
| 137 |
Local Ltac t_Proper_helper := |
| 138 |
cbv [W'.Equal W'.In Proper respectful option_eq]; |
| 139 |
repeat first [ progress intros |
| 140 |
| progress subst |
| 141 |
| progress destruct_head'_ex |
| 142 |
| congruence |
| 143 |
| reflexivity |
| 144 |
| eassumption |
| 145 |
| progress break_innermost_match_hyps |
| 146 |
| progress break_innermost_match |
| 147 |
| apply W'.find_2 |
| 148 |
| match goal with |
| 149 |
| [ H : E'.eq ?x ?y |- _ ] |
| 150 |
=> (idtac + symmetry in H); eapply W'.MapsTo_1 in H; [ | apply W'.find_2; eassumption ] |
| 151 |
| [ H : _ |- _ ] => apply W'.find_1 in H |
| 152 |
| [ |- Some _ = None ] => exfalso |
| 153 |
| [ |- ?x = None ] => destruct x eqn:? |
| 154 |
| [ |- _ <-> _ ] => split |
| 155 |
| [ H : W'.find ?k ?m = Some ?v, H' : forall k', W'.find k' ?m = W'.find k' ?m' |- _ ] |
| 156 |
=> unique assert (W'.find k m' = Some v) by now rewrite <- H |
| 157 |
| [ H : W'.find ?k ?m' = Some ?v, H' : forall k', W'.find k' ?m = W'.find k' ?m' |- _ ] |
| 158 |
=> unique assert (W'.find k m = Some v) by now rewrite <- H |
| 159 |
end |
| 160 |
| eexists ]. |
| 161 |
|
| 162 |
Local Instance W'_find_Proper_eq elt : Proper (E'.eq ==> @W'.Equal _ ==> option_eq eq) (@W'.find elt) | 10. |
| 163 |
Proof. t_Proper_helper. Qed. |
| 164 |
|
| 165 |
Local Instance W'_MapsTo_Proper_eq_iff elt : Proper (E'.eq ==> eq ==> @W'.Equal _ ==> iff) (@W'.MapsTo elt) | 10. |
| 166 |
Proof. t_Proper_helper. Qed. |
| 167 |
|
| 168 |
Local Instance W'_In_compat elt : Proper (E'.eq ==> @W'.Equal _ ==> iff) (@W'.In elt) | 10. |
| 169 |
Proof. t_Proper_helper. Qed. |
| 170 |
|
| 171 |
Definition Empty elt (m : t elt) := forall a e, ~ MapsTo a e m. |
| 172 |
Definition Equal elt (m m' : t elt) := forall y, find y m = find y m'. |
| 173 |
Definition Equiv elt (eq_elt:elt->elt->Prop) m m' := |
| 174 |
(forall k, In k m <-> In k m') /\ |
| 175 |
(forall k e e', MapsTo k e m -> MapsTo k e' m' -> eq_elt e e'). |
| 176 |
Definition Equivb elt (cmp: elt->elt->bool) := Equiv (Cmp cmp). |
| 177 |
|
| 178 |
Definition Empty_alt : forall elt, t elt -> Prop := W'.Empty. |
| 179 |
Definition Equal_alt : forall elt, t elt -> t elt -> Prop := W'.Equal. |
| 180 |
Definition Equiv_alt : forall elt, (elt -> elt -> Prop) -> t elt -> t elt -> Prop := W'.Equiv. |
| 181 |
Definition Equivb_alt : forall elt, (elt->elt->bool) -> t elt -> t elt -> Prop := W'.Equivb. |
| 182 |
|
| 183 |
Lemma forall_lift_key_dep {A} f (P : key -> A -> Prop) R |
| 184 |
(f_Proper : Proper (E'.eq ==> R) f) |
| 185 |
(P_Proper : Proper (E.eq ==> R ==> Basics.impl) P) |
| 186 |
: (forall a : key, P a (@lift A f a)) <-> (forall a : W'.key, P (E.of_ a) (f a)). |
| 187 |
Proof. |
| 188 |
cbv [lift]. |
| 189 |
split; intro H; intro x; [ specialize (H (E.of_ x)) | specialize (H (E.to_ x)) ]. |
| 190 |
all: eapply P_Proper; try eassumption. |
| 191 |
all: f_equiv. |
| 192 |
all: apply E.to_of + apply E.of_to. |
| 193 |
Qed. |
| 194 |
|
| 195 |
Local Ltac t_alt_iff := |
| 196 |
cbv [In find MapsTo |
| 197 |
Equal_alt Empty_alt Equiv_alt Equivb_alt |
| 198 |
Equal Empty Equiv Equivb]; |
| 199 |
let rec get_P lift_f P := |
| 200 |
lazymatch P with |
| 201 |
| fun a : ?A => ?P |
| 202 |
=> let P' := fresh in |
| 203 |
let a' := fresh in |
| 204 |
constr:(fun a' : A |
| 205 |
=> ltac:(let P := constr:(match a' with a => P end) in |
| 206 |
let res := get_P (lift_f a') P in |
| 207 |
exact res)) |
| 208 |
| _ |
| 209 |
=> lazymatch (eval pattern lift_f in P) with |
| 210 |
| ?P _ => exact P |
| 211 |
end |
| 212 |
end in |
| 213 |
let rec handle_one_lift LHS := |
| 214 |
lazymatch LHS with |
| 215 |
| ?G1 /\ ?G2 |
| 216 |
=> handle_one_lift G1; [ handle_one_lift G2 | .. ] |
| 217 |
| context[lift ?f] |
| 218 |
=> lazymatch LHS with |
| 219 |
| (forall a : key, @?P a) |
| 220 |
=> let P' := get_P (lift f) P in |
| 221 |
setoid_rewrite (@forall_lift_key_dep _ f P') |
| 222 |
| _ => fail 0 "unhandled" LHS (* |
| 223 |
| |
| 224 |
| [ |- (exists a : elt, @?P a) <-> _ ] |
| 225 |
=> let P' := get_P (lift f) P in |
| 226 |
setoid_rewrite (@exists_lift_dep _ f P')*) |
| 227 |
end |
| 228 |
| _ => idtac |
| 229 |
end in |
| 230 |
let rec handle_lift _ := |
| 231 |
let LHS := lazymatch goal with |- ?LHS <-> _ => LHS end in |
| 232 |
lazymatch LHS with |
| 233 |
| context[lift ?f] => handle_one_lift LHS; [ handle_lift () | .. ] |
| 234 |
| _ => idtac |
| 235 |
end in |
| 236 |
handle_lift (); |
| 237 |
try exact _; |
| 238 |
[ try reflexivity |
| 239 |
| try solve [ cbv [Proper respectful Basics.impl Basics.flip option_eq]; |
| 240 |
intros; split_iff; |
| 241 |
specialize_under_binders_by reflexivity; |
| 242 |
specialize_all_ways; break_innermost_match_hyps; inversion_option; subst; |
| 243 |
firstorder eauto; congruence ].. |
| 244 |
]. |
| 245 |
|
| 246 |
Lemma Equal_alt_iff elt (s s' : t elt) : Equal s s' <-> Equal_alt s s'. |
| 247 |
Proof. t_alt_iff. Qed. |
| 248 |
Lemma Empty_alt_iff elt (s : t elt) : Empty s <-> Empty_alt s. |
| 249 |
Proof. t_alt_iff. Qed. |
| 250 |
Lemma Equiv_alt_iff elt eq_elt (s s' : t elt) : Equiv eq_elt s s' <-> Equiv_alt eq_elt s s'. |
| 251 |
Proof. t_alt_iff. Qed. |
| 252 |
Lemma Equivb_alt_iff elt cmp (s s' : t elt) : Equivb cmp s s' <-> Equivb_alt cmp s s'. |
| 253 |
Proof. t_alt_iff. Qed. |
| 254 |
|
| 255 |
Create HintDb iso_map_alt discriminated. |
| 256 |
Create HintDb iso_map_alt1 discriminated. |
| 257 |
Create HintDb iso_map_alt2 discriminated. |
| 258 |
Create HintDb iso_map_alt3 discriminated. |
| 259 |
|
| 260 |
Global |
| 261 |
Hint Unfold |
| 262 |
empty |
| 263 |
is_empty |
| 264 |
mem |
| 265 |
add |
| 266 |
find |
| 267 |
remove |
| 268 |
mem |
| 269 |
map |
| 270 |
mapi |
| 271 |
map2 |
| 272 |
elements |
| 273 |
cardinal |
| 274 |
fold |
| 275 |
equal |
| 276 |
MapsTo |
| 277 |
In |
| 278 |
Empty |
| 279 |
Empty_alt |
| 280 |
Equiv_alt |
| 281 |
Equivb_alt |
| 282 |
eq_key |
| 283 |
eq_key_elt |
| 284 |
W'.eq_key |
| 285 |
W'.eq_key_elt |
| 286 |
option_map |
| 287 |
lift |
| 288 |
liftho |
| 289 |
: iso_map_alt. |
| 290 |
|
| 291 |
#[global] |
| 292 |
Hint Rewrite Empty_alt_iff Equal_alt_iff Equiv_alt_iff Equivb_alt_iff |
| 293 |
eq_to_iff |
| 294 |
eq_to_of_iff |
| 295 |
eq_to_of_iff' |
| 296 |
(*W'.is_empty_spec |
| 297 |
W'.mem_spec |
| 298 |
W'.add_spec |
| 299 |
W'.remove_spec |
| 300 |
W'.singleton_spec |
| 301 |
W'.union_spec |
| 302 |
W'.inter_spec |
| 303 |
W'.diff_spec |
| 304 |
W'.equal_spec |
| 305 |
W'.subset_spec |
| 306 |
W'.fold_spec |
| 307 |
W'.for_all_spec |
| 308 |
W'.exists_spec |
| 309 |
W'.partition_spec1 |
| 310 |
W'.partition_spec2 |
| 311 |
W'.filter_spec |
| 312 |
W'.cardinal_spec *) |
| 313 |
W'.cardinal_1 |
| 314 |
W'.fold_1 |
| 315 |
E.of_to |
| 316 |
E.to_of |
| 317 |
map_length |
| 318 |
fold_left_map |
| 319 |
: iso_map_alt. |
| 320 |
|
| 321 |
Global Hint Resolve |
| 322 |
W'.MapsTo_1 |
| 323 |
W'.mem_1 |
| 324 |
W'.empty_1 |
| 325 |
W'.is_empty_1 |
| 326 |
W'.add_1 |
| 327 |
W'.remove_1 |
| 328 |
W'.find_1 |
| 329 |
W'.elements_1 |
| 330 |
W'.equal_1 |
| 331 |
W'.map_1 |
| 332 |
W'.mapi_1 |
| 333 |
W'.map2_1 |
| 334 |
: iso_map_alt1. |
| 335 |
Global Hint Resolve |
| 336 |
W'.mem_2 |
| 337 |
W'.is_empty_2 |
| 338 |
W'.add_2 |
| 339 |
W'.remove_2 |
| 340 |
W'.find_2 |
| 341 |
W'.elements_2 |
| 342 |
W'.equal_2 |
| 343 |
W'.map_2 |
| 344 |
W'.mapi_2 |
| 345 |
W'.map2_2 |
| 346 |
: iso_map_alt2. |
| 347 |
Global Hint Resolve |
| 348 |
W'.add_3 |
| 349 |
W'.remove_3 |
| 350 |
W'.elements_3w |
| 351 |
: iso_map_alt3. |
| 352 |
Global Hint Extern 10 => rewrite eq_to_iff in * |
| 353 |
: iso_map_alt1 iso_map_alt2 iso_map_alt3. |
| 354 |
|
| 355 |
(*Local Hint Resolve |
| 356 |
W'.empty_spec |
| 357 |
W'.elements_spec2w |
| 358 |
W'.choose_spec1 |
| 359 |
W'.choose_spec2 |
| 360 |
: core.*) |
| 361 |
|
| 362 |
Local Ltac spec_t_step_quick |
| 363 |
:= first [ progress intros |
| 364 |
| progress cbn [fst snd] in * |
| 365 |
| apply (f_equal2 (@pair _ _)) |
| 366 |
| progress repeat autorewrite with iso_map_alt in * |
| 367 |
| progress repeat autounfold with iso_map_alt in * |
| 368 |
| match goal with H : _ |- _ => refine H end |
| 369 |
| reflexivity |
| 370 |
| progress auto |
| 371 |
| exact _ |
| 372 |
| progress destruct_head'_and |
| 373 |
| progress destruct_head'_ex |
| 374 |
| progress specialize_under_binders_by eassumption ]. |
| 375 |
|
| 376 |
Global Hint Extern 100 |
| 377 |
=> spec_t_step_quick |
| 378 |
: iso_map_alt1 iso_map_alt2 iso_map_alt3. |
| 379 |
|
| 380 |
Local Ltac spec_t |
| 381 |
:= repeat first [ spec_t_step_quick |
| 382 |
| solve [ eauto with iso_map_alt1 nocore ] |
| 383 |
| solve [ eauto with iso_map_alt2 nocore ] |
| 384 |
| solve [ eauto with iso_map_alt3 nocore ] ]. |
| 385 |
|
| 386 |
Local Hint Extern 2 => Proper_compose_hint : typeclass_instances. |
| 387 |
|
| 388 |
Section Spec. |
| 389 |
|
| 390 |
Variable elt:Type. |
| 391 |
Variable elt' elt'' : Type. |
| 392 |
Variable m m' m'' : t elt. |
| 393 |
Variable x y z : key. |
| 394 |
Variable e e' : elt. |
| 395 |
|
| 396 |
Lemma MapsTo_1 : E.eq x y -> MapsTo x e m -> MapsTo y e m. |
| 397 |
Proof using Type. spec_t. Qed. |
| 398 |
Lemma mem_1 : In x m -> mem x m = true. |
| 399 |
Proof using Type. spec_t. Qed. |
| 400 |
Lemma mem_2 : mem x m = true -> In x m. |
| 401 |
Proof using Type. spec_t. Qed. |
| 402 |
Lemma empty_1 : Empty (@empty elt). |
| 403 |
Proof using Type. spec_t. Qed. |
| 404 |
Lemma is_empty_1 : Empty m -> is_empty m = true. |
| 405 |
Proof using Type. spec_t. Qed. |
| 406 |
Lemma is_empty_2 : is_empty m = true -> Empty m. |
| 407 |
Proof using Type. spec_t. Qed. |
| 408 |
Lemma add_1 : E.eq x y -> MapsTo y e (add x e m). |
| 409 |
Proof using Type. spec_t. Qed. |
| 410 |
Lemma add_2 : ~ E.eq x y -> MapsTo y e m -> MapsTo y e (add x e' m). |
| 411 |
Proof using Type. spec_t. Qed. |
| 412 |
Lemma add_3 : ~ E.eq x y -> MapsTo y e (add x e' m) -> MapsTo y e m. |
| 413 |
Proof using Type. spec_t. Qed. |
| 414 |
Lemma remove_1 : E.eq x y -> ~ In y (remove x m). |
| 415 |
Proof using Type. spec_t. Qed. |
| 416 |
Lemma remove_2 : ~ E.eq x y -> MapsTo y e m -> MapsTo y e (remove x m). |
| 417 |
Proof using Type. spec_t. Qed. |
| 418 |
Lemma remove_3 : MapsTo y e (remove x m) -> MapsTo y e m. |
| 419 |
Proof using Type. spec_t. Qed. |
| 420 |
Lemma find_1 : MapsTo x e m -> find x m = Some e. |
| 421 |
Proof using Type. spec_t. Qed. |
| 422 |
Lemma find_2 : find x m = Some e -> MapsTo x e m. |
| 423 |
Proof using Type. spec_t. Qed. |
| 424 |
Lemma elements_1 : |
| 425 |
MapsTo x e m -> InA (@eq_key_elt _) (x,e) (elements m). |
| 426 |
Proof using Type. spec_t. rewrite InA_map_iff; spec_t. Qed. |
| 427 |
Lemma elements_2 : |
| 428 |
InA (@eq_key_elt _) (x,e) (elements m) -> MapsTo x e m. |
| 429 |
Proof using Type. spec_t. rewrite InA_map_iff in *; spec_t. Qed. |
| 430 |
Lemma elements_3w : NoDupA (@eq_key _) (elements m). |
| 431 |
Proof using Type. |
| 432 |
pose proof (W'.elements_3w m). |
| 433 |
spec_t. |
| 434 |
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 | ]. |
| 435 |
rewrite List.map_map. |
| 436 |
setoid_rewrite (_ : eqlistA (@W'.eq_key _) (List.map _ _) _); |
| 437 |
[ |
| 438 |
| eapply Proper_map; [ | reflexivity ] ]; |
| 439 |
[ rewrite List.map_id; eassumption |
| 440 |
| cbv -[fst snd]; cbn ]. |
| 441 |
spec_t. |
| 442 |
Qed. |
| 443 |
Lemma cardinal_1 : cardinal m = length (elements m). |
| 444 |
Proof using Type. spec_t. Qed. |
| 445 |
|
| 446 |
Lemma fold_1 : |
| 447 |
forall (A : Type) (i : A) (f : key -> elt -> A -> A), |
| 448 |
fold f m i = fold_left (fun a p => f (fst p) (snd p) a) (elements m) i. |
| 449 |
Proof using Type. spec_t. Qed. |
| 450 |
|
| 451 |
Variable cmp : elt -> elt -> bool. |
| 452 |
|
| 453 |
Lemma equal_1 : Equivb cmp m m' -> equal cmp m m' = true. |
| 454 |
Proof using Type. spec_t. Qed. |
| 455 |
Lemma equal_2 : equal cmp m m' = true -> Equivb cmp m m'. |
| 456 |
Proof using Type. spec_t. Qed. |
| 457 |
End Spec. |
| 458 |
|
| 459 |
Lemma map_1 : forall (elt elt':Type)(m: t elt)(x:key)(e:elt)(f:elt->elt'), |
| 460 |
MapsTo x e m -> MapsTo x (f e) (map f m). |
| 461 |
Proof. spec_t. Qed. |
| 462 |
Lemma map_2 : forall (elt elt':Type)(m: t elt)(x:key)(f:elt->elt'), |
| 463 |
In x (map f m) -> In x m. |
| 464 |
Proof. spec_t. Qed. |
| 465 |
Lemma mapi_1 (elt elt':Type)(m: t elt)(x:key)(e:elt) |
| 466 |
(f:key->elt->elt') |
| 467 |
: MapsTo x e m -> |
| 468 |
exists y, E.eq y x /\ MapsTo x (f y e) (mapi f m). |
| 469 |
Proof. |
| 470 |
pose proof (fun x => @W'.mapi_1 elt elt' m x e (fun x => f (E.of_ x))). |
| 471 |
spec_t. |
| 472 |
eexists. |
| 473 |
split; [ | eassumption ]. |
| 474 |
spec_t. |
| 475 |
Qed. |
| 476 |
Lemma mapi_2 |
| 477 |
: forall (elt elt':Type)(m: t elt)(x:key) |
| 478 |
(f:key->elt->elt'), In x (mapi f m) -> In x m. |
| 479 |
Proof. spec_t. Qed. |
| 480 |
Lemma map2_1 |
| 481 |
: forall (elt elt' elt'':Type)(m: t elt)(m': t elt') |
| 482 |
(x:key)(f:option elt->option elt'->option elt''), |
| 483 |
In x m \/ In x m' -> |
| 484 |
find x (map2 f m m') = f (find x m) (find x m'). |
| 485 |
Proof. spec_t. Qed. |
| 486 |
Lemma map2_2 |
| 487 |
: forall (elt elt' elt'':Type)(m: t elt)(m': t elt') |
| 488 |
(x:key)(f:option elt->option elt'->option elt''), |
| 489 |
In x (map2 f m m') -> In x m \/ In x m'. |
| 490 |
Proof. spec_t. Qed. |
| 491 |
End IsoWSfun. |
| 492 |
|
| 493 |
Module IsoWS (W' : WS) (E : IsoDecidableTypeOrig W'.E) <: WS. |
| 494 |
Module E := E. |
| 495 |
Global Remove Hints E.eq_refl E.eq_sym E.eq_trans : core. |
| 496 |
Include IsoWSfun W'.E W' E. |
| 497 |
End IsoWS. |
| 498 |
|
| 499 |
Module IsoSfun (E' : OrderedTypeOrig) (W' : Sfun E') (E : IsoOrderedTypeOrig E') <: Sfun E. |
| 500 |
Include IsoWSfun E' W' E. |
| 501 |
Section elt. |
| 502 |
Variable elt:Type. |
| 503 |
Definition lt_key (p p':key*elt) := E.lt (fst p) (fst p'). |
| 504 |
|
| 505 |
Lemma elements_3 m : sort lt_key (elements m). |
| 506 |
Proof using Type. |
| 507 |
pose proof (W'.elements_3 m). |
| 508 |
cbv [elements lt_key W'.lt_key] in *. |
| 509 |
rewrite Sorted_map_iff. |
| 510 |
cbn [fst snd]. |
| 511 |
eapply Sorted_Proper_impl; [ .. | reflexivity | eassumption ]. |
| 512 |
cbv; intros; destruct_head'_prod; destruct_head'_and; subst. |
| 513 |
apply E.Proper_of_lt; assumption. |
| 514 |
Qed. |
| 515 |
End elt. |
| 516 |
End IsoSfun. |
| 517 |
|
| 518 |
Module IsoS (S' : S) (E' : IsoMiniOrderedType S'.E) <: S. |
| 519 |
Module E <: IsoOrderedTypeOrig S'.E := E' <+ OT_of_MOT. |
| 520 |
Global Remove Hints E.eq_refl E.eq_sym E.eq_trans : core. |
| 521 |
Include IsoSfun S'.E S' E. |
| 522 |
End IsoS. |
| 523 |
|
| 524 |
(* TODO: |
| 525 |
Module IsoSord (S' : Sord) (Data' : IsoMiniOrderedType S'.Data) (E : IsoMiniOrderedType S'.MapS.E) (S'_iso : IsoMiniOrderedType S') <: Sord <: IsoOrderedType S'. |
| 526 |
Module Data <: IsoOrderedType S'.Data := Data' <+ OT_of_MOT. |
| 527 |
Module MapS <: S := IsoS S'.MapS E. |
| 528 |
Import MapS. |
| 529 |
|
| 530 |
Definition t := MapS.t Data.t. |
| 531 |
|
| 532 |
Definition eq : t -> t -> Prop. |
| 533 |
refine (_ S'.eq). |
| 534 |
compute. |
| 535 |
pose MapS.E. |
| 536 |
Parameter lt : t -> t -> Prop. |
| 537 |
|
| 538 |
Axiom eq_refl : forall m : t, eq m m. |
| 539 |
Axiom eq_sym : forall m1 m2 : t, eq m1 m2 -> eq m2 m1. |
| 540 |
Axiom eq_trans : forall m1 m2 m3 : t, eq m1 m2 -> eq m2 m3 -> eq m1 m3. |
| 541 |
Axiom lt_trans : forall m1 m2 m3 : t, lt m1 m2 -> lt m2 m3 -> lt m1 m3. |
| 542 |
Axiom lt_not_eq : forall m1 m2 : t, lt m1 m2 -> ~ eq m1 m2. |
| 543 |
|
| 544 |
Definition cmp e e' := match Data.compare e e' with EQ _ => true | _ => false end. |
| 545 |
|
| 546 |
Parameter eq_1 : forall m m', Equivb cmp m m' -> eq m m'. |
| 547 |
Parameter eq_2 : forall m m', eq m m' -> Equivb cmp m m'. |
| 548 |
|
| 549 |
Parameter compare : forall m1 m2, Compare lt eq m1 m2. |
| 550 |
(** Total ordering between maps. [Data.compare] is a total ordering |
| 551 |
used to compare data associated with equal keys in the two maps. *) |
| 552 |
|
| 553 |
|
| 554 |
Include S. |
| 555 |
Include OT_of_MOT. |
| 556 |
End IsoSord. |
| 557 |
Definition t := MapS.t Data.t. |
| 558 |
|
| 559 |
Print IsoMiniOrderedType. |
| 560 |
Definition eq : t -> t -> Prop := S'.eq. |
| 561 |
Parameter lt : t -> t -> Prop. |
| 562 |
|
| 563 |
Axiom eq_refl : forall m : t, eq m m. |
| 564 |
Axiom eq_sym : forall m1 m2 : t, eq m1 m2 -> eq m2 m1. |
| 565 |
Axiom eq_trans : forall m1 m2 m3 : t, eq m1 m2 -> eq m2 m3 -> eq m1 m3. |
| 566 |
Axiom lt_trans : forall m1 m2 m3 : t, lt m1 m2 -> lt m2 m3 -> lt m1 m3. |
| 567 |
Axiom lt_not_eq : forall m1 m2 : t, lt m1 m2 -> ~ eq m1 m2. |
| 568 |
|
| 569 |
Definition cmp e e' := match Data.compare e e' with EQ _ => true | _ => false end. |
| 570 |
|
| 571 |
Parameter eq_1 : forall m m', Equivb cmp m m' -> eq m m'. |
| 572 |
Parameter eq_2 : forall m m', eq m m' -> Equivb cmp m m'. |
| 573 |
|
| 574 |
Parameter compare : forall m1 m2, Compare lt eq m1 m2. |
| 575 |
(** Total ordering between maps. [Data.compare] is a total ordering |
| 576 |
used to compare data associated with equal keys in the two maps. *) |
| 577 |
|
| 578 |
End Sord. |
| 579 |
|
| 580 |
*) |