| 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.Prod. |
| 13 |
Require Import Crypto.Util.Structures.Equalities.Bool. |
| 14 |
Require Import Crypto.Util.Structures.Equalities.Prod. |
| 15 |
Require Import Crypto.Util.Structures.Orders. |
| 16 |
Require Import Crypto.Util.Structures.Orders.Bool. |
| 17 |
Require Import Crypto.Util.Structures.Orders.Prod. |
| 18 |
Require Import Crypto.Util.Structures.Orders.Option. |
| 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.Head. |
| 26 |
Require Import Crypto.Util.Tactics.BreakMatch. |
| 27 |
Require Import Crypto.Util.Tactics.SetoidSubst. |
| 28 |
|
| 29 |
Local Set Implicit Arguments. |
| 30 |
(* TODO: move to global settings *) |
| 31 |
Local Set Keyed Unification. |
| 32 |
Local Set Keep Proof Equalities. |
| 33 |
Local Open Scope lazy_bool_scope. |
| 34 |
|
| 35 |
Local Ltac t_safe_step := |
| 36 |
first [ progress cbv in * |
| 37 |
| progress intros |
| 38 |
| reflexivity |
| 39 |
| progress subst |
| 40 |
| progress destruct_head'_ex |
| 41 |
| progress destruct_head'_and |
| 42 |
| progress destruct_head'_False |
| 43 |
| progress destruct_head' prod |
| 44 |
| progress destruct_head' option |
| 45 |
| progress destruct_head'_bool |
| 46 |
| progress destruct_head'_or |
| 47 |
| progress inversion_option |
| 48 |
| progress break_innermost_match |
| 49 |
| progress break_innermost_match_hyps |
| 50 |
| progress specialize_under_binders_by exact tt |
| 51 |
| progress specialize_under_binders_by eapply ex_intro |
| 52 |
| apply conj |
| 53 |
| congruence |
| 54 |
| progress invlist InA |
| 55 |
| match goal with |
| 56 |
| [ |- NoDupA _ (_ :: _) ] => constructor |
| 57 |
| [ |- NoDupA _ nil ] => constructor |
| 58 |
| [ H : ?x = ?y :> Compare _ _ _ _ |- _ = _ ] |
| 59 |
=> let x := head x in |
| 60 |
let y := head y in |
| 61 |
is_constructor x; |
| 62 |
is_constructor y; |
| 63 |
inversion H; clear H |
| 64 |
| [ H : forall k : bool, _ |- _ ] |
| 65 |
=> pose proof (H true); pose proof (H false); clear H |
| 66 |
end |
| 67 |
| solve [ eauto ] ]. |
| 68 |
Local Ltac t := |
| 69 |
repeat first [ t_safe_step |
| 70 |
| progress specialize_under_binders_by reflexivity |
| 71 |
| progress specialize_under_binders_by solve [ constructor; constructor; constructor; constructor ] ]. |
| 72 |
|
| 73 |
Module BoolMap <: S with Module E := BoolOrderedTypeOrig. |
| 74 |
Module E := BoolOrderedTypeOrig. |
| 75 |
Definition key := bool. |
| 76 |
Definition t (elt : Type) := (option elt * option elt)%type. |
| 77 |
|
| 78 |
Section Types. |
| 79 |
|
| 80 |
Variable elt:Type. |
| 81 |
|
| 82 |
Definition empty : t elt := (None, None). |
| 83 |
Definition is_empty (m : t elt) : bool := match m with (None, None) => true | _ => false end. |
| 84 |
Definition add (k : key) (v : elt) (m : t elt) : t elt |
| 85 |
:= (if k then Some v else fst m, if k then snd m else Some v). |
| 86 |
Definition find (k : key) (m : t elt) : option elt := if k then fst m else snd m. |
| 87 |
Definition remove (k : key) (m : t elt) : t elt |
| 88 |
:= (if k then None else fst m, if k then snd m else None). |
| 89 |
Definition mem (k : key) (m : t elt) : bool |
| 90 |
:= match (if k then fst m else snd m) with |
| 91 |
| Some _ => true |
| 92 |
| None => false |
| 93 |
end. |
| 94 |
|
| 95 |
Variable elt' elt'' : Type. |
| 96 |
|
| 97 |
Definition map (f : elt -> elt') (m : t elt) : t elt' |
| 98 |
:= (option_map f (fst m), option_map f (snd m)). |
| 99 |
Definition mapi (f : key -> elt -> elt') (m : t elt) : t elt' |
| 100 |
:= (option_map (f true) (fst m), option_map (f false) (snd m)). |
| 101 |
Definition map2 (f : option elt -> option elt' -> option elt'') (m : t elt) (m' : t elt') : t elt'' |
| 102 |
:= (match fst m, fst m' with |
| 103 |
| None, None => None |
| 104 |
| m, m' => f m m' |
| 105 |
end, |
| 106 |
match snd m, snd m' with |
| 107 |
| None, None => None |
| 108 |
| m, m' => f m m' |
| 109 |
end). |
| 110 |
Definition elements (m : t elt) : list (key*elt) |
| 111 |
:= match m with |
| 112 |
| (None, None) => nil |
| 113 |
| (Some v1, None) => (true, v1) :: nil |
| 114 |
| (None, Some v2) => (false, v2) :: nil |
| 115 |
| (Some v1, Some v2) => (false, v2) :: (true, v1) :: nil |
| 116 |
end. |
| 117 |
Definition cardinal (m : t elt) : nat := List.length (elements m). |
| 118 |
Definition fold A (f : key -> elt -> A -> A) (m : t elt) (acc : A) : A |
| 119 |
:= fold_left (fun a p => f (fst p) (snd p) a) (elements m) acc. |
| 120 |
Definition equal (eqb : elt -> elt -> bool) (m m' : t elt) : bool |
| 121 |
:= option_beq eqb (fst m) (fst m') &&& option_beq eqb (snd m) (snd m'). |
| 122 |
|
| 123 |
Section Spec. |
| 124 |
|
| 125 |
Variable m m' m'' : t elt. |
| 126 |
Variable x y z : key. |
| 127 |
Variable e e' : elt. |
| 128 |
|
| 129 |
Local Hint Constructors NoDupA : core. |
| 130 |
|
| 131 |
Definition MapsTo (k : key) (v : elt) (m : t elt) : Prop |
| 132 |
:= (k = true /\ fst m = Some v) \/ (k = false /\ snd m = Some v). |
| 133 |
|
| 134 |
Definition In (k:key)(m: t elt) : Prop := exists e:elt, MapsTo k e m. |
| 135 |
|
| 136 |
Definition Empty m := forall (a : key)(e:elt) , ~ MapsTo a e m. |
| 137 |
|
| 138 |
Definition eq_key (p p':key*elt) := E.eq (fst p) (fst p'). |
| 139 |
|
| 140 |
Definition eq_key_elt (p p':key*elt) := |
| 141 |
E.eq (fst p) (fst p') /\ (snd p) = (snd p'). |
| 142 |
|
| 143 |
Lemma MapsTo_1 : E.eq x y -> MapsTo x e m -> MapsTo y e m. |
| 144 |
Proof using Type. clear; t. Qed. |
| 145 |
Lemma mem_1 : In x m -> mem x m = true. |
| 146 |
Proof using Type. clear; t. Qed. |
| 147 |
Lemma mem_2 : mem x m = true -> In x m. |
| 148 |
Proof using Type. clear; t. Qed. |
| 149 |
Lemma empty_1 : Empty empty. |
| 150 |
Proof using Type. clear; t. Qed. |
| 151 |
Lemma is_empty_1 : Empty m -> is_empty m = true. |
| 152 |
Proof using Type. clear; t. Qed. |
| 153 |
Lemma is_empty_2 : is_empty m = true -> Empty m. |
| 154 |
Proof using Type. clear; t. Qed. |
| 155 |
Lemma add_1 : E.eq x y -> MapsTo y e (add x e m). |
| 156 |
Proof using Type. clear; t. Qed. |
| 157 |
Lemma add_2 : ~ E.eq x y -> MapsTo y e m -> MapsTo y e (add x e' m). |
| 158 |
Proof using Type. clear; t. Qed. |
| 159 |
Lemma add_3 : ~ E.eq x y -> MapsTo y e (add x e' m) -> MapsTo y e m. |
| 160 |
Proof using Type. clear; t. Qed. |
| 161 |
Lemma remove_1 : E.eq x y -> ~ In y (remove x m). |
| 162 |
Proof using Type. clear; t. Qed. |
| 163 |
Lemma remove_2 : ~ E.eq x y -> MapsTo y e m -> MapsTo y e (remove x m). |
| 164 |
Proof using Type. clear; t. Qed. |
| 165 |
Lemma remove_3 : MapsTo y e (remove x m) -> MapsTo y e m. |
| 166 |
Proof using Type. clear; t. Qed. |
| 167 |
Lemma find_1 : MapsTo x e m -> find x m = Some e. |
| 168 |
Proof using Type. clear; t. Qed. |
| 169 |
Lemma find_2 : find x m = Some e -> MapsTo x e m. |
| 170 |
Proof using Type. clear; t. Qed. |
| 171 |
Lemma elements_1 : |
| 172 |
MapsTo x e m -> InA eq_key_elt (x,e) (elements m). |
| 173 |
Proof using Type. clear; t. Qed. |
| 174 |
Lemma elements_2 : |
| 175 |
InA eq_key_elt (x,e) (elements m) -> MapsTo x e m. |
| 176 |
Proof using Type. clear; t. Qed. |
| 177 |
Lemma elements_3w : NoDupA eq_key (elements m). |
| 178 |
Proof using Type. clear; t. Qed. |
| 179 |
Lemma cardinal_1 : cardinal m = length (elements m). |
| 180 |
Proof using Type. clear; t. Qed. |
| 181 |
Lemma fold_1 : |
| 182 |
forall (A : Type) (i : A) (f : key -> elt -> A -> A), |
| 183 |
fold f m i = fold_left (fun a p => f (fst p) (snd p) a) (elements m) i. |
| 184 |
Proof using Type. clear; t. Qed. |
| 185 |
Definition Equal m m' := forall y, find y m = find y m'. |
| 186 |
Definition Equiv (eq_elt:elt->elt->Prop) m m' := |
| 187 |
(forall k, In k m <-> In k m') /\ |
| 188 |
(forall k e e', MapsTo k e m -> MapsTo k e' m' -> eq_elt e e'). |
| 189 |
Definition Equivb (cmp: elt->elt->bool) := Equiv (Cmp cmp). |
| 190 |
|
| 191 |
Variable cmp : elt -> elt -> bool. |
| 192 |
|
| 193 |
Lemma equal_1 : Equivb cmp m m' -> equal cmp m m' = true. |
| 194 |
Proof using Type. clear; t. Qed. |
| 195 |
Lemma equal_2 : equal cmp m m' = true -> Equivb cmp m m'. |
| 196 |
Proof using Type. clear; t. Qed. |
| 197 |
End Spec. |
| 198 |
End Types. |
| 199 |
|
| 200 |
Lemma map_1 : forall (elt elt':Type)(m: t elt)(x:key)(e:elt)(f:elt->elt'), |
| 201 |
MapsTo x e m -> MapsTo x (f e) (map f m). |
| 202 |
Proof using Type. clear; t. Qed. |
| 203 |
Lemma map_2 : forall (elt elt':Type)(m: t elt)(x:key)(f:elt->elt'), |
| 204 |
In x (map f m) -> In x m. |
| 205 |
Proof using Type. clear; t. Qed. |
| 206 |
|
| 207 |
Lemma mapi_1 : forall (elt elt':Type)(m: t elt)(x:key)(e:elt) |
| 208 |
(f:key->elt->elt'), MapsTo x e m -> |
| 209 |
exists y, E.eq y x /\ MapsTo x (f y e) (mapi f m). |
| 210 |
Proof using Type. clear; t. Qed. |
| 211 |
Lemma mapi_2 : forall (elt elt':Type)(m: t elt)(x:key) |
| 212 |
(f:key->elt->elt'), In x (mapi f m) -> In x m. |
| 213 |
Proof using Type. clear; t. Qed. |
| 214 |
Lemma map2_1 : forall (elt elt' elt'':Type)(m: t elt)(m': t elt') |
| 215 |
(x:key)(f:option elt->option elt'->option elt''), |
| 216 |
In x m \/ In x m' -> |
| 217 |
find x (map2 f m m') = f (find x m) (find x m'). |
| 218 |
Proof using Type. clear; t. Qed. |
| 219 |
|
| 220 |
Lemma map2_2 : forall (elt elt' elt'':Type)(m: t elt)(m': t elt') |
| 221 |
(x:key)(f:option elt->option elt'->option elt''), |
| 222 |
In x (map2 f m m') -> In x m \/ In x m'. |
| 223 |
Proof using Type. clear; t. Qed. |
| 224 |
Section elt. |
| 225 |
Variable elt:Type. |
| 226 |
Definition lt_key (p p':key*elt) := E.lt (fst p) (fst p'). |
| 227 |
Lemma elements_3 : forall m, sort lt_key (elements m). |
| 228 |
Proof using Type. clear; t. Qed. |
| 229 |
End elt. |
| 230 |
End BoolMap. |
| 231 |
|
| 232 |
Module BoolMapOrd (D : OrderedTypeOrig) <: Sord |
| 233 |
with Module Data := D |
| 234 |
with Module MapS := BoolMap. |
| 235 |
Module Data <: OrderedTypeOrig := D. |
| 236 |
Module MapS <: S := BoolMap. |
| 237 |
Module _DataOpt := OptionOrderedTypeOrig Data. |
| 238 |
Import MapS. |
| 239 |
|
| 240 |
Definition t := BoolMap.t Data.t. |
| 241 |
Include ProdHasEq _DataOpt _DataOpt. |
| 242 |
Include ProdHasLt _DataOpt _DataOpt. |
| 243 |
Include ProdIsEqOrig _DataOpt _DataOpt. |
| 244 |
Include ProdIsStrOrderOrig _DataOpt _DataOpt. |
| 245 |
Definition cmp e e' := match Data.compare e e' with EQ _ => true | _ => false end. |
| 246 |
Lemma eq_1 : forall m m', Equivb cmp m m' -> eq m m'. |
| 247 |
Proof. t. Qed. |
| 248 |
Lemma eq_2 : forall m m', eq m m' -> Equivb cmp m m'. |
| 249 |
Proof. pose proof Data.lt_not_eq. pose proof Data.eq_sym. t. Qed. |
| 250 |
|
| 251 |
Definition compare : forall m1 m2, Compare lt eq m1 m2. |
| 252 |
Proof. |
| 253 |
intros [[m1|] [m1'|]] [[m2|] [m2'|]]; try destruct (D.compare m1 m2); try destruct (D.compare m1' m2'); |
| 254 |
repeat match goal with |
| 255 |
| [ H : D.eq _ _ |- _ ] |
| 256 |
=> unique pose proof (@D.eq_sym _ _ H) |
| 257 |
end; |
| 258 |
solve [ constructor; cbv; (constructor + assumption); (constructor + assumption); (constructor + assumption) ]. |
| 259 |
Defined. |
| 260 |
End BoolMapOrd. |
| 261 |
|
| 262 |
Module BoolMapUsualOrd (D : UsualOrderedTypeOrig) <: Sord |
| 263 |
with Module Data := D |
| 264 |
with Module MapS := BoolMap. |
| 265 |
Module Data <: UsualOrderedTypeOrig := D. |
| 266 |
Module MapS <: S := BoolMap. |
| 267 |
Module _DataOpt := OptionUsualOrderedTypeOrig Data. |
| 268 |
Import MapS. |
| 269 |
|
| 270 |
Definition t := BoolMap.t Data.t. |
| 271 |
Include ProdHasUsualEq _DataOpt _DataOpt. |
| 272 |
Include ProdHasLt _DataOpt _DataOpt. |
| 273 |
Include ProdUsualIsEqOrig _DataOpt _DataOpt. |
| 274 |
Include ProdUsualIsStrOrderOrig _DataOpt _DataOpt. |
| 275 |
Definition cmp e e' := match Data.compare e e' with EQ _ => true | _ => false end. |
| 276 |
Lemma eq_1 : forall m m', Equivb cmp m m' -> eq m m'. |
| 277 |
Proof. t. Qed. |
| 278 |
Lemma eq_2 : forall m m', eq m m' -> Equivb cmp m m'. |
| 279 |
Proof. pose proof Data.lt_not_eq. pose proof Data.eq_sym. t. Qed. |
| 280 |
|
| 281 |
Definition compare : forall m1 m2, Compare lt eq m1 m2. |
| 282 |
Proof. |
| 283 |
intros [[m1|] [m1'|]] [[m2|] [m2'|]]; try destruct (D.compare m1 m2); try destruct (D.compare m1' m2'); |
| 284 |
subst; |
| 285 |
solve [ constructor; cbv; (constructor + assumption); (constructor + assumption); (constructor + assumption) ]. |
| 286 |
Defined. |
| 287 |
End BoolMapUsualOrd. |