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