| 1 |
From Coq Require Import Bool. |
| 2 |
From Coq Require Import List. |
| 3 |
From Coq Require Import Equalities. |
| 4 |
From Coq Require Import OrderedType. |
| 5 |
From Coq Require Import Orders. |
| 6 |
From Coq Require Import OrdersEx. |
| 7 |
From Coq Require Import FMapInterface. |
| 8 |
Require Import Crypto.Util.ListUtil. |
| 9 |
Require Import Crypto.Util.ListUtil.SetoidList. |
| 10 |
Require Import Crypto.Util.ListUtil.SetoidListRev. |
| 11 |
Require Import Crypto.Util.Compose. |
| 12 |
Require Import Crypto.Util.Logic.Forall. |
| 13 |
Require Import Crypto.Util.Logic.Exists. |
| 14 |
Require Import Crypto.Util.Option. |
| 15 |
Require Import Crypto.Util.Structures.Equalities. |
| 16 |
Require Import Crypto.Util.Structures.Orders. |
| 17 |
Require Import Crypto.Util.Structures.Orders.Flip. |
| 18 |
Require Import Crypto.Util.FSets.FMapInterface. |
| 19 |
Require Import Crypto.Util.Sorting.Sorted.Proper. |
| 20 |
Require Import Crypto.Util.Tactics.SplitInContext. |
| 21 |
Require Import Crypto.Util.Tactics.DestructHead. |
| 22 |
Require Import Crypto.Util.Tactics.SpecializeAllWays. |
| 23 |
Require Import Crypto.Util.Tactics.SpecializeBy. |
| 24 |
Require Import Crypto.Util.Tactics.SpecializeUnderBindersBy. |
| 25 |
Require Import Crypto.Util.Tactics.BreakMatch. |
| 26 |
Require Import Crypto.Util.Tactics.SetoidSubst. |
| 27 |
|
| 28 |
Local Set Implicit Arguments. |
| 29 |
(* TODO: move to global settings *) |
| 30 |
Local Set Keyed Unification. |
| 31 |
|
| 32 |
Module FlipWSfun (E : DecidableTypeOrig) (W' : WSfun E) <: WSfun E. |
| 33 |
Module E' := E <+ UpdateEq. |
| 34 |
Global Remove Hints E'.eq_refl E'.eq_sym E'.eq_trans : core. |
| 35 |
Local Hint Resolve E'.eq_refl E'.eq_sym E'.eq_trans : core. |
| 36 |
|
| 37 |
Local Instance W'_eq_key_equiv elt : Equivalence (@W'.eq_key elt) | 10. split; cbv; eauto. Qed. |
| 38 |
Local Instance W'_eq_key_elt_equiv elt : Equivalence (@W'.eq_key_elt elt) | 10. split; repeat intros [? ?]; cbv in *; subst; eauto. Qed. |
| 39 |
|
| 40 |
Definition key := W'.key. |
| 41 |
Definition t := W'.t. |
| 42 |
Definition empty := W'.empty. |
| 43 |
Definition is_empty := W'.is_empty. |
| 44 |
Definition add := W'.add. |
| 45 |
Definition find := W'.find. |
| 46 |
Definition remove := W'.remove. |
| 47 |
Definition mem := W'.mem. |
| 48 |
Definition map := W'.map. |
| 49 |
Definition mapi := W'.mapi. |
| 50 |
Definition map2 := W'.map2. |
| 51 |
Definition cardinal := W'.cardinal. |
| 52 |
Definition equal := W'.equal. |
| 53 |
Definition MapsTo := W'.MapsTo. |
| 54 |
Definition In := W'.In. |
| 55 |
Definition eq_key := W'.eq_key. |
| 56 |
Definition eq_key_elt := W'.eq_key_elt. |
| 57 |
Definition MapsTo_1 := W'.MapsTo_1. |
| 58 |
Definition mem_1 := W'.mem_1. |
| 59 |
Definition mem_2 := W'.mem_2. |
| 60 |
Definition empty_1 := W'.empty_1. |
| 61 |
Definition is_empty_1 := W'.is_empty_1. |
| 62 |
Definition is_empty_2 := W'.is_empty_2. |
| 63 |
Definition add_1 := W'.add_1. |
| 64 |
Definition add_2 := W'.add_2. |
| 65 |
Definition add_3 := W'.add_3. |
| 66 |
Definition remove_1 := W'.remove_1. |
| 67 |
Definition remove_2 := W'.remove_2. |
| 68 |
Definition remove_3 := W'.remove_3. |
| 69 |
Definition find_1 := W'.find_1. |
| 70 |
Definition find_2 := W'.find_2. |
| 71 |
Definition Empty := W'.Empty. |
| 72 |
Definition Equal := W'.Equal. |
| 73 |
Definition Equiv := W'.Equiv. |
| 74 |
Definition Equivb := W'.Equivb. |
| 75 |
Definition equal_1 := W'.equal_1. |
| 76 |
Definition equal_2 := W'.equal_2. |
| 77 |
Definition map_1 := W'.map_1. |
| 78 |
Definition map_2 := W'.map_2. |
| 79 |
Definition mapi_1 := W'.mapi_1. |
| 80 |
Definition mapi_2 := W'.mapi_2. |
| 81 |
Definition map2_1 := W'.map2_1. |
| 82 |
Definition map2_2 := W'.map2_2. |
| 83 |
|
| 84 |
Definition elements elt (v : t elt) : list (key * elt) |
| 85 |
:= List.rev_append (W'.elements v) nil. |
| 86 |
Definition fold elt A (f : key -> elt -> A -> A) (m : t elt) (a : A) : A |
| 87 |
:= W'.fold (fun k v acc a => acc (f k v a)) m id a. |
| 88 |
|
| 89 |
Section Spec. |
| 90 |
Variable elt : Type. |
| 91 |
Variable m m' m'' : t elt. |
| 92 |
Variable x y z : key. |
| 93 |
Variable e e' : elt. |
| 94 |
|
| 95 |
Lemma elements_1 : |
| 96 |
MapsTo x e m -> InA (@eq_key_elt _) (x,e) (elements m). |
| 97 |
Proof using Type. |
| 98 |
clear; cbv [elements]; rewrite <- rev_alt, InA_rev. |
| 99 |
apply W'.elements_1. |
| 100 |
Qed. |
| 101 |
Lemma elements_2 : |
| 102 |
InA (@eq_key_elt _) (x,e) (elements m) -> MapsTo x e m. |
| 103 |
Proof using Type. |
| 104 |
clear; cbv [elements]; rewrite <- rev_alt, InA_rev. |
| 105 |
apply W'.elements_2. |
| 106 |
Qed. |
| 107 |
Lemma elements_3w : NoDupA (@eq_key _) (elements m). |
| 108 |
Proof using Type. |
| 109 |
clear; cbv [elements]; rewrite <- rev_alt. |
| 110 |
apply NoDupA_rev; try exact _. |
| 111 |
apply W'.elements_3w. |
| 112 |
Qed. |
| 113 |
Lemma cardinal_1 : cardinal m = length (elements m). |
| 114 |
Proof using Type. |
| 115 |
clear; cbv [elements]; rewrite <- rev_alt, rev_length. |
| 116 |
apply W'.cardinal_1. |
| 117 |
Qed. |
| 118 |
Lemma fold_1 : |
| 119 |
forall (A : Type) (i : A) (f : key -> elt -> A -> A), |
| 120 |
fold f m i = fold_left (fun a p => f (fst p) (snd p) a) (elements m) i. |
| 121 |
Proof using Type. |
| 122 |
clear; cbv [fold elements]; intros; rewrite <- rev_alt, W'.fold_1, fold_left_rev_higher_order. |
| 123 |
reflexivity. |
| 124 |
Qed. |
| 125 |
End Spec. |
| 126 |
End FlipWSfun. |
| 127 |
|
| 128 |
Module FlipWeakMap (W' : WS) <: WS. |
| 129 |
Module E := W'.E. |
| 130 |
Global Remove Hints E.eq_refl E.eq_sym E.eq_trans : core. |
| 131 |
Include FlipWSfun W'.E W'. |
| 132 |
End FlipWeakMap. |
| 133 |
|
| 134 |
Module FlipUsualWeakMap (W' : UsualWS) <: UsualWS := FlipWeakMap W'. |
| 135 |
|
| 136 |
Module FlipSfun (E' : OrderedTypeOrig) (W' : Sfun E'). |
| 137 |
Include FlipWSfun E' W'. |
| 138 |
Section elt. |
| 139 |
Variable elt:Type. |
| 140 |
Definition lt_key (p p':key*elt) := flip E'.lt (fst p) (fst p'). |
| 141 |
|
| 142 |
Lemma elements_3 m : sort lt_key (elements m). |
| 143 |
Proof using Type. |
| 144 |
cbv [elements lt_key]. |
| 145 |
rewrite <- rev_alt, SortA_rev_iff. |
| 146 |
apply W'.elements_3. |
| 147 |
Qed. |
| 148 |
End elt. |
| 149 |
End FlipSfun. |
| 150 |
|
| 151 |
Module FlipUsualMap (S' : UsualS) <: UsualS. |
| 152 |
Module E <: UsualOrderedTypeOrig := FlipUsualOrderedTypeOrig S'.E. |
| 153 |
Global Remove Hints E.eq_refl E.eq_sym E.eq_trans : core. |
| 154 |
Include FlipSfun S'.E S'. |
| 155 |
End FlipUsualMap. |
| 156 |
|
| 157 |
Module FlipMap (S' : S) <: S. |
| 158 |
Module E <: OrderedTypeOrig := FlipOrderedTypeOrig S'.E. |
| 159 |
Global Remove Hints E.eq_refl E.eq_sym E.eq_trans : core. |
| 160 |
Include FlipSfun S'.E S'. |
| 161 |
End FlipMap. |
| 162 |
|
| 163 |
Module FlipSord (S' : Sord) <: Sord. |
| 164 |
Module Data <: OrderedTypeOrig := FlipOrderedTypeOrig S'.Data. |
| 165 |
Module MapS <: S := FlipMap S'.MapS. |
| 166 |
Import MapS. |
| 167 |
|
| 168 |
Definition t := S'.t. |
| 169 |
Definition eq := S'.eq. |
| 170 |
Definition lt := flip S'.lt. |
| 171 |
Definition eq_refl := S'.eq_refl. |
| 172 |
Definition eq_sym := S'.eq_sym. |
| 173 |
Definition eq_trans := S'.eq_trans. |
| 174 |
Lemma lt_trans : forall m1 m2 m3 : t, lt m1 m2 -> lt m2 m3 -> lt m1 m3. |
| 175 |
Proof. cbv [lt flip]; intros; eapply S'.lt_trans; eassumption. Qed. |
| 176 |
Lemma lt_not_eq : forall m1 m2 : t, lt m1 m2 -> ~ eq m1 m2. |
| 177 |
Proof. cbv [lt flip]; intros * H H'; apply S'.lt_not_eq in H; apply H, S'.eq_sym, H'. Qed. |
| 178 |
|
| 179 |
Definition cmp e e' := match Data.compare e e' with EQ _ => true | _ => false end. |
| 180 |
|
| 181 |
Lemma cmp_iff : forall e e', cmp e e' = flip S'.cmp e e'. |
| 182 |
Proof. |
| 183 |
cbv [cmp flip S'.cmp]. |
| 184 |
intros e e'. |
| 185 |
destruct Data.compare, S'.Data.compare; try reflexivity. |
| 186 |
all: cbv [flip] in *. |
| 187 |
all: match goal with |
| 188 |
| [ H : _ |- _ ] => apply S'.Data.lt_not_eq in H; [] |
| 189 |
end. |
| 190 |
all: try now exfalso; eauto using S'.Data.eq_sym. |
| 191 |
Qed. |
| 192 |
|
| 193 |
Lemma compare : forall m1 m2, Compare lt eq m1 m2. |
| 194 |
Proof. |
| 195 |
intros m1 m2. |
| 196 |
destruct (S'.compare m2 m1). |
| 197 |
all: constructor; (idtac + apply S'.eq_sym); assumption. |
| 198 |
Defined. |
| 199 |
|
| 200 |
Lemma eq_iff : forall m m', Equivb cmp m m' <-> eq m m'. |
| 201 |
Proof. |
| 202 |
intros m m'; cbv [eq]. |
| 203 |
rewrite (conj (@eq_sym m m') (@eq_sym m' m) : iff _ _). |
| 204 |
rewrite <- (conj (@S'.eq_1 m' m) (@S'.eq_2 m' m) : iff _ _). |
| 205 |
cbv [Equivb S'.MapS.Equivb S'.MapS.Equiv Cmp eq flip] in *. |
| 206 |
firstorder; first [ rewrite cmp_iff | setoid_rewrite <- cmp_iff ]; cbv [flip]. |
| 207 |
all: eauto. |
| 208 |
Qed. |
| 209 |
|
| 210 |
Lemma eq_1 : forall m m', Equivb cmp m m' -> eq m m'. |
| 211 |
Proof. intros *; apply eq_iff. Qed. |
| 212 |
Lemma eq_2 : forall m m', eq m m' -> Equivb cmp m m'. |
| 213 |
Proof. intros *; apply eq_iff. Qed. |
| 214 |
End FlipSord. |