| 1 |
From Coq Require Import Program. |
| 2 |
From Coq Require Import Bool. |
| 3 |
From Coq Require Import List. |
| 4 |
From Coq Require Import Orders. |
| 5 |
From Coq Require Import OrdersEx. |
| 6 |
From Coq Require Import FMapInterface. |
| 7 |
From Coq Require Import FMapFacts. |
| 8 |
From Coq Require Import RelationPairs. |
| 9 |
Require Import Crypto.Util.ListUtil. |
| 10 |
Require Import Crypto.Util.ListUtil.SetoidList. |
| 11 |
Require Import Crypto.Util.ListUtil.SetoidListFlatMap. |
| 12 |
Require Import Crypto.Util.ListUtil.StdlibCompat. |
| 13 |
Require Import Crypto.Util.Compose. |
| 14 |
Require Import Crypto.Util.Logic.Forall. |
| 15 |
Require Import Crypto.Util.Logic.Exists. |
| 16 |
Require Import Crypto.Util.Prod. |
| 17 |
Require Import Crypto.Util.Option. |
| 18 |
Require Import Crypto.Util.Relations. |
| 19 |
Require Import Crypto.Util.Structures.Equalities. |
| 20 |
Require Import Crypto.Util.Structures.Equalities.Prod. |
| 21 |
Require Import Crypto.Util.Structures.Orders. |
| 22 |
Require Import Crypto.Util.Structures.Orders.Prod. |
| 23 |
Require Import Crypto.Util.FSets.FMapInterface. |
| 24 |
Require Import Crypto.Util.FSets.FMapFacts. |
| 25 |
Require Import Crypto.Util.Sorting.Sorted.Proper. |
| 26 |
Require Import Crypto.Util.Tactics.SplitInContext. |
| 27 |
Require Import Crypto.Util.Tactics.DestructHead. |
| 28 |
Require Import Crypto.Util.Tactics.SpecializeAllWays. |
| 29 |
Require Import Crypto.Util.Tactics.SpecializeBy. |
| 30 |
Require Import Crypto.Util.Tactics.SpecializeUnderBindersBy. |
| 31 |
Require Import Crypto.Util.Tactics.Head. |
| 32 |
Require Import Crypto.Util.Tactics.BreakMatch. |
| 33 |
Require Import Crypto.Util.Tactics.SetoidSubst. |
| 34 |
|
| 35 |
Local Set Implicit Arguments. |
| 36 |
(* TODO: move to global settings *) |
| 37 |
Local Set Keyed Unification. |
| 38 |
Local Open Scope option_scope. |
| 39 |
Local Open Scope bool_scope. |
| 40 |
Local Open Scope lazy_bool_scope. |
| 41 |
Local Open Scope type_scope. |
| 42 |
|
| 43 |
Module ProdWSfun_gen (E1 : DecidableTypeOrig) (E2 : DecidableTypeOrig) (M1 : WSfun E1) (M2 : WSfun E2). |
| 44 |
Module Type ESig := ProdTyp E1 E2 <+ HasEq <+ IsEqOrig <+ HasEqDec. |
| 45 |
Module Type ESigCompat (E : ESig). |
| 46 |
Axiom eq_alt : forall x y, E.eq x y <-> RelProd E1.eq E2.eq x y. |
| 47 |
End ESigCompat. |
| 48 |
Module E1 <: DecidableTypeBoth := E1 <+ UpdateEq. |
| 49 |
Global Remove Hints E1.eq_refl E1.eq_sym E1.eq_trans : core. |
| 50 |
Module E2 <: DecidableTypeBoth := E2 <+ UpdateEq. |
| 51 |
Global Remove Hints E2.eq_refl E2.eq_sym E2.eq_trans : core. |
| 52 |
Module M1' := M1 <+ WFacts_fun E1 <+ WFacts_fun_RemoveHints E1 <+ WAdditionalFacts_fun E1. |
| 53 |
Module M2' := M2 <+ WFacts_fun E2 <+ WFacts_fun_RemoveHints E2 <+ WAdditionalFacts_fun E2. |
| 54 |
Local Existing Instances |
| 55 |
M1'.Empty_m_Proper M1'.In_m_Proper M1'.MapsTo_m_Proper M1'.add_m_Proper M1'.find_m_Proper M1'.is_empty_m_Proper M1'.map_m_Proper M1'.mem_m_Proper M1'.remove_m_Proper |
| 56 |
M2'.Empty_m_Proper M2'.In_m_Proper M2'.MapsTo_m_Proper M2'.add_m_Proper M2'.find_m_Proper M2'.is_empty_m_Proper M2'.map_m_Proper M2'.mem_m_Proper M2'.remove_m_Proper |
| 57 |
| 10. |
| 58 |
Local Existing Instances |
| 59 |
M1'.eq_key_equiv M1'.eq_key_elt_equiv M1'.Equal_Equivalence |
| 60 |
M2'.eq_key_equiv M2'.eq_key_elt_equiv M2'.Equal_Equivalence |
| 61 |
| 10. |
| 62 |
Local Existing Instances |
| 63 |
M1'.Proper_eq_key_elt_iff M1'.Proper_eq_key_elt_impl M1'.Proper_eq_key_elt_flip_impl M1'.Proper_eq_key_elt_iff' M1'.Proper_eq_key_elt_impl' M1'.Proper_eq_key_elt_flip_impl' M1'.Proper_eq_key_iff M1'.Proper_eq_key_impl M1'.Proper_eq_key_flip_impl M1'.find_Proper_eq M1'.MapsTo_Proper_eq_iff M1'.In_compat M1'.Proper_Equiv_eq_impl M1'.Proper_Equiv_eq_flip_impl M1'.Proper_Equiv_eq_iff M1'.Proper_Equiv_eq_impl_pointwise M1'.Proper_Equiv_eq_flip_impl_pointwise M1'.Proper_Equiv_eq_iff_pointwise |
| 64 |
M2'.Proper_eq_key_elt_iff M2'.Proper_eq_key_elt_impl M2'.Proper_eq_key_elt_flip_impl M2'.Proper_eq_key_elt_iff' M2'.Proper_eq_key_elt_impl' M2'.Proper_eq_key_elt_flip_impl' M2'.Proper_eq_key_iff M2'.Proper_eq_key_impl M2'.Proper_eq_key_flip_impl M2'.find_Proper_eq M2'.MapsTo_Proper_eq_iff M2'.In_compat M2'.Proper_Equiv_eq_impl M2'.Proper_Equiv_eq_flip_impl M2'.Proper_Equiv_eq_iff M2'.Proper_Equiv_eq_impl_pointwise M2'.Proper_Equiv_eq_flip_impl_pointwise M2'.Proper_Equiv_eq_iff_pointwise |
| 65 |
. |
| 66 |
Module ProdWSfun_gen (E : ESig) (ECompat : ESigCompat E) <: WSfun E. |
| 67 |
Module E' <: DecidableTypeBoth := E <+ UpdateEq <+ ECompat. |
| 68 |
Global Remove Hints E'.eq_refl E'.eq_sym E'.eq_trans : core. |
| 69 |
Local Hint Resolve E1.eq_refl E1.eq_sym E1.eq_trans |
| 70 |
E2.eq_refl E2.eq_sym E2.eq_trans |
| 71 |
E'.eq_refl E'.eq_sym E'.eq_trans |
| 72 |
: core. |
| 73 |
|
| 74 |
Definition key := E.t. |
| 75 |
|
| 76 |
Global Hint Transparent key : core. |
| 77 |
|
| 78 |
(* Create a record so that the normal form of the type isn't exponentially sized when we nest things, see COQBUG(https://github.com/coq/coq/issues/16172) *) |
| 79 |
Record M2_t_NonEmpty elt := { M2_m :> M2.t elt ; M2_NonEmpty :> ~M2.Empty M2_m }. |
| 80 |
Global Arguments Build_M2_t_NonEmpty [elt] _ _. |
| 81 |
Local Notation M2_mk := (@Build_M2_t_NonEmpty _). |
| 82 |
Definition t elt := M1.t (M2_t_NonEmpty elt). |
| 83 |
|
| 84 |
Local Ltac t_obgl := |
| 85 |
repeat first [ progress intros |
| 86 |
| progress cbv [M2.Empty Option.value not option_map] in * |
| 87 |
| progress cbn [M2_m] in * |
| 88 |
| reflexivity |
| 89 |
| exfalso; assumption |
| 90 |
| congruence |
| 91 |
| match goal with |
| 92 |
| [ H := _ |- _ ] => subst H |
| 93 |
| [ H : forall a e, M2.MapsTo _ _ (M2.add _ _ _) -> False |- _ ] |
| 94 |
=> specialize (H _ _ ltac:(eapply M2.add_1; reflexivity)) |
| 95 |
| [ H : context[M2.is_empty] |- _ ] |
| 96 |
=> rewrite M2.is_empty_1 in H by assumption |
| 97 |
| [ H : forall a e, M2.MapsTo a e (M2.mapi ?f ?m) -> False |- _ ] |
| 98 |
=> assert (forall a e, M2.MapsTo a e m -> False) |
| 99 |
by (let H' := fresh in |
| 100 |
intros ? ? H'; eapply M2.mapi_1 in H'; destruct H' as [?[? H']]; |
| 101 |
eapply H, H'); |
| 102 |
clear H |
| 103 |
| [ H : forall k e, _ = _ -> M2.In _ _ \/ M2.In _ _ -> False |- _ ] |
| 104 |
=> pose proof (fun k e pf pf' => H k e (pf pf') (or_introl pf')); |
| 105 |
pose proof (fun k e pf pf' => H k e (pf pf') (or_intror pf')); |
| 106 |
clear H |
| 107 |
end |
| 108 |
| progress subst |
| 109 |
| progress specialize_under_binders_by eapply M2.map_1 |
| 110 |
| progress destruct_head' M2_t_NonEmpty |
| 111 |
| progress destruct_head'_and |
| 112 |
| progress destruct_head' option |
| 113 |
| progress break_innermost_match_hyps |
| 114 |
| progress specialize_under_binders_by (match goal with |- M2.MapsTo _ _ (M2.map2 _ _ _) => idtac end; |
| 115 |
eapply M2.find_2; |
| 116 |
rewrite M2.map2_1) ]. |
| 117 |
|
| 118 |
Local Obligation Tactic := try abstract t_obgl; t_obgl. |
| 119 |
|
| 120 |
Definition empty elt : t elt := @M1.empty _. |
| 121 |
Definition is_empty elt : t elt -> bool := @M1.is_empty _. |
| 122 |
Program Definition add elt (k : key) (v : elt) (m : t elt) : t elt |
| 123 |
:= let m2 := M2.add (snd k) v (Option.value (option_map (@M2_m _) (M1.find (fst k) m)) (@M2.empty _)) in |
| 124 |
M1.add (fst k) (M2_mk m2 _) m. |
| 125 |
Definition find elt (k : key) (m : t elt) : option elt |
| 126 |
:= m2 <- M1.find (fst k) m; M2.find (snd k) (M2_m m2). |
| 127 |
Program Definition remove elt (k : key) (m : t elt) : t elt |
| 128 |
:= match M1.find (fst k) m with |
| 129 |
| None => m |
| 130 |
| Some m2 |
| 131 |
=> let m2 := M2.remove (snd k) m2 in |
| 132 |
match M2.is_empty m2 with |
| 133 |
| true => M1.remove (fst k) m |
| 134 |
| false => M1.add (fst k) (M2_mk m2 _) m |
| 135 |
end |
| 136 |
end. |
| 137 |
Definition mem elt (k : key) (m : t elt) : bool |
| 138 |
:= match find k m with Some _ => true | _ => false end. |
| 139 |
Program Definition map elt elt' (f : elt -> elt') : t elt -> t elt' |
| 140 |
:= M1.map (fun m => M2_mk (M2.map f (M2_m m)) _). |
| 141 |
Program Definition mapi elt elt' (f : key -> elt -> elt') : t elt -> t elt' |
| 142 |
:= M1.mapi (fun k1 m => M2_mk (M2.mapi (fun k2 => f (k1, k2)) (M2_m m)) _). |
| 143 |
Program Definition map2 elt elt' elt'' (f : option elt -> option elt' -> option elt'') |
| 144 |
(f' := match f None None with |
| 145 |
| None => f |
| 146 |
| _ => fun x y => match x, y with |
| 147 |
| None, None => None |
| 148 |
| _, _ => f x y |
| 149 |
end |
| 150 |
end) |
| 151 |
: t elt -> t elt' -> t elt'' |
| 152 |
:= M1.map2 (fun m1 m2 |
| 153 |
=> if match m1, m2 with None, None => true | _, _ => false end |
| 154 |
then None |
| 155 |
else |
| 156 |
let m1' := Option.value (option_map (@M2_m _) m1) (M2.empty _) in |
| 157 |
let m2' := Option.value (option_map (@M2_m _) m2) (M2.empty _) in |
| 158 |
let m' := M2.map2 f' m1' m2' in |
| 159 |
match M2.is_empty m' with |
| 160 |
| true => None |
| 161 |
| false => Some (M2_mk m' _) |
| 162 |
end). |
| 163 |
Definition elements elt (m : t elt) : list (key * elt) |
| 164 |
:= List.flat_map (fun '(k1, m) => List.map (fun '(k2, v) => ((k1, k2), v)) (M2.elements (M2_m m))) (M1.elements m). |
| 165 |
Definition cardinal elt (m : t elt) : nat := List.length (elements m). |
| 166 |
Definition fold elt A (f : key -> elt -> A -> A) : t elt -> A -> A |
| 167 |
:= M1.fold (fun k1 m => M2.fold (fun k2 => f (k1, k2)) (M2_m m)). |
| 168 |
Definition equal elt (eqb : elt -> elt -> bool) : t elt -> t elt -> bool |
| 169 |
:= M1.equal (fun m m' => M2.equal eqb (M2_m m) (M2_m m')). |
| 170 |
Definition MapsTo elt (k : key) (v : elt) (m : t elt) : Prop |
| 171 |
:= exists m2, M1.MapsTo (fst k) m2 m /\ M2.MapsTo (snd k) v (M2_m m2). |
| 172 |
Definition eq_key elt (p p':key*elt) := E.eq (fst p) (fst p'). |
| 173 |
Definition eq_key_elt elt (p p':key*elt) := |
| 174 |
E.eq (fst p) (fst p') /\ (snd p) = (snd p'). |
| 175 |
|
| 176 |
Definition In elt (k : key) (m : t elt) := exists e, MapsTo k e m. |
| 177 |
Definition Empty elt (m : t elt) := forall a e, ~ MapsTo a e m. |
| 178 |
Definition Equal elt (m m' : t elt) := forall y, find y m = find y m'. |
| 179 |
Definition Equiv elt (eq_elt:elt->elt->Prop) m m' := |
| 180 |
(forall k, In k m <-> In k m') /\ |
| 181 |
(forall k e e', MapsTo k e m -> MapsTo k e' m' -> eq_elt e e'). |
| 182 |
Definition Equivb elt (cmp: elt->elt->bool) := Equiv (Cmp cmp). |
| 183 |
|
| 184 |
Module Import _Extra1. |
| 185 |
Local Instance eq_key_equiv elt : Equivalence (@eq_key elt) | 10. |
| 186 |
Proof. |
| 187 |
split; cbv; intros; break_innermost_match; break_innermost_match_hyps; try ((idtac + symmetry + etransitivity + exfalso); (eassumption + reflexivity)). |
| 188 |
Qed. |
| 189 |
Local Instance eq_key_elt_equiv elt : Equivalence (@eq_key_elt elt) | 10. |
| 190 |
Proof. |
| 191 |
split; cbv; intros; break_innermost_match; break_innermost_match_hyps; destruct_head'_and; split; try ((idtac + symmetry + etransitivity + exfalso); (eassumption + reflexivity)). |
| 192 |
Qed. |
| 193 |
|
| 194 |
Definition Empty_alt elt (m : t elt) : Prop := M1.Empty m. |
| 195 |
(*Definition In_alt elt (k : key) (m : t elt) : Prop := liftKT_ (@M1.In elt) (@M2.In elt). |
| 196 |
Definition Equal_alt elt (m m' : t elt) : Prop := M1.Equal (fst m) (fst m') /\ M2.Equal (snd m) (snd m').*) |
| 197 |
Definition Equiv_alt elt (cmp : elt -> elt -> Prop) (m m' : t elt) : Prop := M1.Equiv (fun m m' => M2.Equiv cmp (M2_m m) (M2_m m')) m m'. |
| 198 |
(*Definition Equivb_alt elt (cmp : elt -> elt -> bool) (m m' : t elt) : Prop := M1.Equivb cmp (fst m) (fst m') /\ M2.Equivb cmp (snd m) (snd m'). |
| 199 |
*) |
| 200 |
|
| 201 |
(* Lemma In_alt_iff elt k (s : t elt) : In k s <-> In_alt k s. |
| 202 |
Proof. t_alt_iff. Qed. |
| 203 |
Lemma Equal_alt_iff elt (s s' : t elt) : Equal s s' <-> Equal_alt s s'. |
| 204 |
Proof. t_alt_iff. Qed.*) |
| 205 |
Lemma Empty_alt_iff elt (s : t elt) : Empty s <-> Empty_alt s. |
| 206 |
Proof. |
| 207 |
cbv [Empty Empty_alt MapsTo M1.Empty M2.Empty not M2.key key M1.key E.t]. |
| 208 |
split; repeat intro. |
| 209 |
all: repeat first [ progress destruct_head' M2_t_NonEmpty |
| 210 |
| progress destruct_head'_ex |
| 211 |
| progress destruct_head'_and |
| 212 |
| progress specialize_dep_under_binders_by apply pair |
| 213 |
| progress specialize_dep_under_binders_by eexists |
| 214 |
| progress cbn [fst snd proj1_sig] in * |
| 215 |
| progress specialize_under_binders_by eassumption |
| 216 |
| solve [ eauto ] ]. |
| 217 |
Qed. |
| 218 |
Lemma Equiv_alt_iff elt eq_elt (s s' : t elt) : Equiv eq_elt s s' <-> Equiv_alt eq_elt s s'. |
| 219 |
Proof. |
| 220 |
cbv [Equiv Equiv_alt MapsTo In M1.Equiv M2.Equiv M1.In M2.In key M1.key M2.key not]. |
| 221 |
setoid_rewrite M1'.find_mapsto_iff; setoid_rewrite M2'.find_mapsto_iff. |
| 222 |
split; [ pose I as FWD | pose I as BAK ]. |
| 223 |
all: repeat first [ progress intros |
| 224 |
| progress destruct_head' M2_t_NonEmpty |
| 225 |
| progress destruct_head'_ex |
| 226 |
| progress destruct_head'_and |
| 227 |
| progress split_iff |
| 228 |
| progress specialize_dep_under_binders_by apply pair |
| 229 |
| progress specialize_dep_under_binders_by eexists |
| 230 |
| progress cbn [fst snd ProdWSfun_gen.M2_m] in * |
| 231 |
| progress cbv [not] in * |
| 232 |
| apply conj |
| 233 |
| match goal with |
| 234 |
| [ |- context[@M1.find ?a ?b ?c] ] => destruct (@M1.find a b c) eqn:? |
| 235 |
| [ |- context[@M2.find ?a ?b ?c] ] => destruct (@M2.find a b c) eqn:? |
| 236 |
end |
| 237 |
| solve [ eauto ] |
| 238 |
| congruence |
| 239 |
| progress specialize_under_binders_by eassumption ]. |
| 240 |
all: lazymatch goal with |
| 241 |
| [ H : forall t e, M2.find t ?m = Some e -> ex _, H' : M2.Empty ?m -> False |- _ ] |
| 242 |
=> specialize (fun t e H' => H t e ltac:(apply M2.find_1, M2.elements_2, H')); |
| 243 |
let H'' := fresh in |
| 244 |
let H''' := fresh in |
| 245 |
pose proof H' as H''; |
| 246 |
cbv [M2.Empty not] in H''; |
| 247 |
specialize (fun (H''' : forall a e, ~ _) => H'' (fun a e pf => ltac:(eapply (H''' a e), M2.elements_1, pf))); |
| 248 |
setoid_rewrite InA_alt in H''; |
| 249 |
cbv [not M2.eq_key_elt] in H''; |
| 250 |
destruct (M2.elements m); |
| 251 |
cbn [List.In] in H''; |
| 252 |
[ specialize (H'' ltac:(firstorder tauto)); |
| 253 |
exfalso; assumption |
| 254 |
| specialize_under_binders_by eapply InA_cons_hd ] |
| 255 |
end. |
| 256 |
all: repeat first [ progress cbv [M2.eq_key_elt] in * |
| 257 |
| progress cbn [fst snd proj1_sig] in * |
| 258 |
| progress destruct_head' M2_t_NonEmpty |
| 259 |
| progress destruct_head'_ex |
| 260 |
| progress destruct_head'_and |
| 261 |
| progress specialize_dep_under_binders_by apply conj |
| 262 |
| progress specialize_dep_under_binders_by reflexivity |
| 263 |
| congruence ]. |
| 264 |
Qed. |
| 265 |
(* |
| 266 |
Lemma Equivb_alt_iff elt cmp (s s' : t elt) : Equivb cmp s s' <-> Equivb_alt cmp s s'. |
| 267 |
Proof. t_alt_iff. Qed. |
| 268 |
*) |
| 269 |
End _Extra1. |
| 270 |
Local Existing Instances eq_key_equiv eq_key_elt_equiv | 10. |
| 271 |
|
| 272 |
Create HintDb prod_map_alt discriminated. |
| 273 |
Create HintDb prod_map_alt1 discriminated. |
| 274 |
Create HintDb prod_map_alt2 discriminated. |
| 275 |
Create HintDb prod_map_alt3 discriminated. |
| 276 |
|
| 277 |
Global |
| 278 |
Hint Unfold |
| 279 |
(*In_alt*) |
| 280 |
empty |
| 281 |
is_empty |
| 282 |
mem |
| 283 |
add |
| 284 |
find |
| 285 |
remove |
| 286 |
mem |
| 287 |
map |
| 288 |
mapi |
| 289 |
map2 |
| 290 |
elements |
| 291 |
cardinal |
| 292 |
fold |
| 293 |
equal |
| 294 |
MapsTo |
| 295 |
In |
| 296 |
Empty |
| 297 |
Empty_alt |
| 298 |
Equiv_alt |
| 299 |
(*Equivb_alt*) |
| 300 |
eq_key |
| 301 |
eq_key_elt |
| 302 |
M1.eq_key |
| 303 |
M1.eq_key_elt |
| 304 |
M2.eq_key |
| 305 |
M2.eq_key_elt |
| 306 |
option_map |
| 307 |
RelProd |
| 308 |
relation_conjunction |
| 309 |
predicate_intersection |
| 310 |
pointwise_extension |
| 311 |
RelCompFun |
| 312 |
Option.bind |
| 313 |
option_map |
| 314 |
Option.value |
| 315 |
not |
| 316 |
: prod_map_alt. |
| 317 |
|
| 318 |
#[global] |
| 319 |
Hint Rewrite (*In_alt_iff*) Empty_alt_iff (*Equal_alt_iff*) Equiv_alt_iff (*Equivb_alt_iff*) |
| 320 |
M1.cardinal_1 |
| 321 |
M1.fold_1 |
| 322 |
M2.cardinal_1 |
| 323 |
M2.fold_1 |
| 324 |
M1'.find_mapsto_iff |
| 325 |
M2'.find_mapsto_iff |
| 326 |
M1'.find_empty |
| 327 |
M2'.find_empty |
| 328 |
M1'.add_o |
| 329 |
M2'.add_o |
| 330 |
M1'.remove_o |
| 331 |
M2'.remove_o |
| 332 |
M1'.map_o |
| 333 |
M2'.map_o |
| 334 |
M1'.map2_o |
| 335 |
M2'.map2_o |
| 336 |
E'.eq_alt |
| 337 |
Bool.andb_true_iff |
| 338 |
InA_app_iff |
| 339 |
map_length |
| 340 |
app_length |
| 341 |
fold_left_map |
| 342 |
fold_left_app |
| 343 |
in_map_iff |
| 344 |
in_flat_map |
| 345 |
: prod_map_alt. |
| 346 |
|
| 347 |
Global Hint Resolve |
| 348 |
M1.MapsTo_1 |
| 349 |
M1.mem_1 |
| 350 |
M1.empty_1 |
| 351 |
M1'.empty_1' |
| 352 |
M1.is_empty_1 |
| 353 |
M1.add_1 |
| 354 |
M1.remove_1 |
| 355 |
M1.find_1 |
| 356 |
M1.elements_1 |
| 357 |
M1.equal_1 |
| 358 |
M1.map_1 |
| 359 |
M1.mapi_1 |
| 360 |
M1.map2_1 |
| 361 |
M2.MapsTo_1 |
| 362 |
M2.mem_1 |
| 363 |
M2.empty_1 |
| 364 |
M2'.empty_1' |
| 365 |
M2.is_empty_1 |
| 366 |
M2.add_1 |
| 367 |
M2.remove_1 |
| 368 |
M2.find_1 |
| 369 |
M2.elements_1 |
| 370 |
M2.equal_1 |
| 371 |
M2.map_1 |
| 372 |
M2.mapi_1 |
| 373 |
M2.map2_1 |
| 374 |
: prod_map_alt1. |
| 375 |
Global Hint Resolve |
| 376 |
M1.mem_2 |
| 377 |
M1.is_empty_2 |
| 378 |
M1.add_2 |
| 379 |
M1.remove_2 |
| 380 |
M1.find_2 |
| 381 |
M1.elements_2 |
| 382 |
M1.equal_2 |
| 383 |
M1.map_2 |
| 384 |
M1.mapi_2 |
| 385 |
M1.map2_2 |
| 386 |
M2.mem_2 |
| 387 |
M2.is_empty_2 |
| 388 |
M2.add_2 |
| 389 |
M2.remove_2 |
| 390 |
M2.find_2 |
| 391 |
M2.elements_2 |
| 392 |
M2.equal_2 |
| 393 |
M2.map_2 |
| 394 |
M2.mapi_2 |
| 395 |
M2.map2_2 |
| 396 |
: prod_map_alt2. |
| 397 |
Global Hint Resolve |
| 398 |
M1.add_3 |
| 399 |
M1.remove_3 |
| 400 |
M1.elements_3w |
| 401 |
M2.add_3 |
| 402 |
M2.remove_3 |
| 403 |
M2.elements_3w |
| 404 |
: prod_map_alt3. |
| 405 |
|
| 406 |
#[global] |
| 407 |
Hint Constructors ex and or |
| 408 |
: prod_map_alt1 prod_map_alt2 prod_map_alt3. |
| 409 |
#[global] |
| 410 |
Hint Extern 10 |
| 411 |
=> progress unfold M1.In, M2.In in * |
| 412 |
: prod_map_alt1 prod_map_alt2 prod_map_alt3. |
| 413 |
|
| 414 |
Local Ltac t := |
| 415 |
repeat first [ progress intros |
| 416 |
| progress subst |
| 417 |
| progress inversion_option |
| 418 |
| progress inversion_pair |
| 419 |
| progress inversion_list |
| 420 |
| reflexivity |
| 421 |
| discriminate |
| 422 |
| progress cbv [not] in * |
| 423 |
| solve [ exfalso; auto with nocore ] |
| 424 |
| progress specialize_by_assumption |
| 425 |
| progress destruct_head'_ex |
| 426 |
| progress destruct_head'_and |
| 427 |
| progress break_innermost_match |
| 428 |
| progress break_innermost_match_hyps |
| 429 |
| progress destruct_head'_or |
| 430 |
| progress cbv [Option.is_Some Option.is_None Option.value Option.bind M1.eq_key_elt M2.eq_key_elt] in * |
| 431 |
| progress autounfold with prod_map_alt in * |
| 432 |
| match goal with |
| 433 |
| [ H : eqlistA _ nil _ |- _ ] => inversion H; clear H |
| 434 |
| [ H : eqlistA _ _ nil |- _ ] => inversion H; clear H |
| 435 |
| [ H : eqlistA _ (_ :: _) _ |- _ ] => inversion H; clear H |
| 436 |
| [ H : eqlistA _ _ (_ :: _) |- _ ] => inversion H; clear H |
| 437 |
| [ H : InA _ _ (_ :: _) |- _ ] => inversion H; clear H |
| 438 |
| [ H : InA _ _ nil |- _ ] => inversion H; clear H |
| 439 |
| [ H : List.In _ (List.map _ _) |- _ ] => rewrite in_map_iff in H |
| 440 |
| [ H : List.In _ (List.flat_map _ _) |- _ ] => rewrite in_flat_map in H |
| 441 |
| [ H : ?x = ?x |- _ ] => clear H |
| 442 |
| [ H : ?T, H' : ?T |- _ ] => clear H |
| 443 |
| [ H : ?x = Some _, H' : context[?x] |- _ ] => rewrite H in H' |
| 444 |
| [ H : ?x = None, H' : context[?x] |- _ ] => rewrite H in H' |
| 445 |
| [ H : Some _ = ?x |- _ ] => symmetry in H |
| 446 |
| [ H : None = ?x |- _ ] => symmetry in H |
| 447 |
end |
| 448 |
| progress autorewrite with prod_map_alt in * |
| 449 |
| solve [ eauto using ex_intro, eq_refl with nocore ] |
| 450 |
| progress specialize_by reflexivity |
| 451 |
| progress specialize_under_binders_by reflexivity |
| 452 |
| progress specialize_under_binders_by progress autorewrite with prod_map_alt |
| 453 |
| progress cbn in * ]. |
| 454 |
|
| 455 |
Local Ltac pose_MapsTo_as_find lem := |
| 456 |
let H := fresh in |
| 457 |
first [ (pose proof M1.find_1 as H + pose proof M2.find_1 as H); |
| 458 |
guarded_specialize_hyp_under_binders_by guard_nondep H ltac:(eapply lem; try first [ eapply M1.find_2 | eapply M2.find_2 ]) |
| 459 |
| pose proof lem as H; |
| 460 |
guarded_specialize_hyp_under_binders_by guard_nondep H ltac:(first [ eapply M1.find_2 | eapply M2.find_2 ]) ]. |
| 461 |
|
| 462 |
Local Ltac spec_t_step_quick |
| 463 |
:= first [ progress intros |
| 464 |
| progress cbn [fst snd proj1_sig] in * |
| 465 |
| apply (f_equal2 (@pair _ _)) |
| 466 |
| progress destruct_head'_False |
| 467 |
| rewrite <- andb_lazy_alt |
| 468 |
| progress repeat autorewrite with prod_map_alt in * |
| 469 |
| progress change E1.t with M1.key in * |
| 470 |
| progress change E2.t with M2.key in * |
| 471 |
| progress repeat autounfold with prod_map_alt in * |
| 472 |
| match goal with H : _ |- _ => refine H end |
| 473 |
| congruence |
| 474 |
| reflexivity |
| 475 |
| progress auto |
| 476 |
| exact _ |
| 477 |
| progress destruct_head'_and |
| 478 |
| progress destruct_head'_ex |
| 479 |
| progress break_innermost_match_hyps |
| 480 |
| progress break_innermost_match |
| 481 |
| progress destruct_head'_or |
| 482 |
| progress specialize_under_binders_by eassumption |
| 483 |
| progress inversion_option |
| 484 |
| progress subst |
| 485 |
| match goal with |
| 486 |
| [ H : M1.MapsTo ?k ?v ?m, H' : context[M1.find ?k ?m] |- _ ] |
| 487 |
=> erewrite M1.find_1 in H' by exact H |
| 488 |
| [ H : M2.MapsTo ?k ?v ?m, H' : context[M2.find ?k ?m] |- _ ] |
| 489 |
=> erewrite M2.find_1 in H' by exact H |
| 490 |
end |
| 491 |
| progress specialize_under_binders_by apply conj ]. |
| 492 |
|
| 493 |
Global Hint Extern 100 |
| 494 |
=> spec_t_step_quick |
| 495 |
: prod_map_alt1 prod_map_alt2 prod_map_alt3. |
| 496 |
|
| 497 |
Local Ltac saturate_find_step := |
| 498 |
let cleanup := |
| 499 |
repeat match goal with |
| 500 |
| [ H : ?T, H' : ?T |- _ ] => clear H' |
| 501 |
end in |
| 502 |
first [ progress inversion_option |
| 503 |
| progress subst |
| 504 |
| exfalso; assumption |
| 505 |
| progress cleanup |
| 506 |
| progress (specialize_under_binders_by (idtac; match goal with |- _ -> False => eauto with nocore end); cleanup) |
| 507 |
| match goal with |
| 508 |
| [ H : E1.eq _ _ |- _ ] |
| 509 |
=> unique pose proof (E1.eq_sym H) |
| 510 |
| [ H : E1.eq _ _, H' : context[E1.eq _ _ -> False] |- _ ] |
| 511 |
=> lazymatch type of H' with | _ -> False => fail | _ => idtac end; |
| 512 |
clear H' |
| 513 |
| [ H : E1.eq _ _ |- _ ] => progress (specialize_all_ways_under_binders_by exact H; cleanup) |
| 514 |
| [ H : E2.eq _ _ |- _ ] |
| 515 |
=> unique pose proof (E2.eq_sym H) |
| 516 |
| [ H : E2.eq _ _, H' : context[E2.eq _ _ -> False] |- _ ] |
| 517 |
=> lazymatch type of H' with | _ -> False => fail | _ => idtac end; |
| 518 |
clear H' |
| 519 |
| [ H : E2.eq _ _ |- _ ] => progress (specialize_all_ways_under_binders_by exact H; cleanup) |
| 520 |
| [ H : context[forall a, M1.find _ _ = _], H' : M1.find _ _ = _ |- _ ] |
| 521 |
=> let H'' := fresh in |
| 522 |
pose proof H' as H''; |
| 523 |
rewrite H in H'' by fail; |
| 524 |
let T := type of H'' in |
| 525 |
lazymatch T with |
| 526 |
| Some _ = Some _ |
| 527 |
=> progress inversion_option; progress subst |
| 528 |
| Some _ = None => inversion_option |
| 529 |
| None = Some _ => inversion_option |
| 530 |
| _ |
| 531 |
=> revert H''; |
| 532 |
lazymatch goal with |
| 533 |
| [ H : T |- _ ] => fail |
| 534 |
| _ => idtac T; intro |
| 535 |
end |
| 536 |
end |
| 537 |
| [ H : context[forall a, M2.find _ _ = _], H' : M2.find _ _ = _ |- _ ] |
| 538 |
=> let H'' := fresh in |
| 539 |
pose proof H' as H''; |
| 540 |
rewrite H in H'' by fail; |
| 541 |
let T := type of H'' in |
| 542 |
lazymatch T with |
| 543 |
| Some _ = Some _ |
| 544 |
=> progress inversion_option; progress subst |
| 545 |
| Some _ = None => inversion_option |
| 546 |
| None = Some _ => inversion_option |
| 547 |
| _ |
| 548 |
=> revert H''; |
| 549 |
lazymatch goal with |
| 550 |
| [ H : T |- _ ] => fail |
| 551 |
| _ => idtac T; intro |
| 552 |
end |
| 553 |
end |
| 554 |
end |
| 555 |
| progress (specialize_gen_under_binders_by' true ltac:(eauto with nocore); cleanup) |
| 556 |
| match goal with |
| 557 |
| [ H : M1.find _ _ = Some _ |- _ ] => progress (specialize_all_ways_under_binders_by exact H; cleanup) |
| 558 |
| [ H : M2.find _ _ = Some _ |- _ ] => progress (specialize_all_ways_under_binders_by exact H; cleanup) |
| 559 |
end ]. |
| 560 |
Local Ltac saturate_find := repeat saturate_find_step. |
| 561 |
|
| 562 |
Local Hint Extern 2 => Proper_compose_hint : typeclass_instances. |
| 563 |
|
| 564 |
Local Ltac spec_t_nosubst_step := |
| 565 |
first [ progress intros |
| 566 |
| exfalso; assumption |
| 567 |
| reflexivity |
| 568 |
| progress inversion_option |
| 569 |
| progress subst |
| 570 |
| progress destruct_head'_ex |
| 571 |
| progress destruct_head'_and |
| 572 |
| progress destruct_head' key |
| 573 |
| progress cbn [fst snd] in * |
| 574 |
| progress specialize_by_assumption |
| 575 |
| progress specialize_by reflexivity |
| 576 |
| progress specialize_under_binders_by apply conj |
| 577 |
| progress specialize_under_binders_by eapply ex_intro |
| 578 |
| progress autorewrite with prod_map_alt in * |
| 579 |
| progress autounfold with prod_map_alt in * |
| 580 |
| setoid_rewrite M1'.find_mapsto_iff |
| 581 |
| setoid_rewrite M2'.find_mapsto_iff |
| 582 |
| match goal with |
| 583 |
| [ H : ?x = ?x |- _ ] => clear H |
| 584 |
| [ H : true = _ |- _ ] => symmetry in H |
| 585 |
| [ H : context[M1.MapsTo] |- _ ] => setoid_rewrite M1'.find_mapsto_iff in H |
| 586 |
| [ H : context[M2.MapsTo] |- _ ] => setoid_rewrite M2'.find_mapsto_iff in H |
| 587 |
| [ H : forall x, Some _ = Some x -> _ |- _ ] => specialize (H _ eq_refl) |
| 588 |
| [ H : forall x, Some x = Some _ -> _ |- _ ] => specialize (H _ eq_refl) |
| 589 |
| [ H : ?x = Some _, H' : context[?x] |- _ ] => rewrite H in H' |
| 590 |
| [ H : ?x = None, H' : context[?x] |- _ ] => rewrite H in H' |
| 591 |
end |
| 592 |
| progress break_innermost_match |
| 593 |
| progress break_innermost_match_hyps |
| 594 |
| match goal with |
| 595 |
| [ |- iff _ _ ] => split |
| 596 |
end |
| 597 |
| congruence |
| 598 |
| solve [ eauto ] |
| 599 |
| match goal with |
| 600 |
| [ |- context[@M1.find ?elt ?x ?m] ] => destruct (@M1.find elt x m) eqn:? |
| 601 |
| [ |- context[@M2.find ?elt ?x ?m] ] => destruct (@M2.find elt x m) eqn:? |
| 602 |
end |
| 603 |
| (* should figure out how to generalize these into lemmas *) |
| 604 |
match goal with |
| 605 |
| [ H : M1.is_empty (M1.remove ?k ?m) = true, H' : M1.find ?k' ?m = Some _, H'' : E2.eq ?k ?k' -> False |- _ ] |
| 606 |
=> exfalso; clear -H H' H''; |
| 607 |
apply M1.is_empty_2 in H; |
| 608 |
cbv [M1.Empty] in H; |
| 609 |
setoid_rewrite M1'.find_mapsto_iff in H; |
| 610 |
specialize (H k') |
| 611 |
| [ H : M2.is_empty (M2.remove ?k ?m) = true, H' : M2.find ?k' ?m = Some _, H'' : E2.eq ?k ?k' -> False |- _ ] |
| 612 |
=> exfalso; clear -H H' H''; |
| 613 |
apply M2.is_empty_2 in H; |
| 614 |
cbv [M2.Empty] in H; |
| 615 |
setoid_rewrite M2'.find_mapsto_iff in H; |
| 616 |
specialize (H k') |
| 617 |
end ]. |
| 618 |
Local Ltac spec_t_step := |
| 619 |
first [ spec_t_nosubst_step |
| 620 |
| progress setoid_subst_rel E1.eq |
| 621 |
| progress setoid_subst_rel E2.eq ]. |
| 622 |
|
| 623 |
Local Ltac spec_t := repeat spec_t_step. |
| 624 |
|
| 625 |
Module Import _Extra2. |
| 626 |
Lemma find_iff elt0 k v m0 : @MapsTo elt0 k v m0 <-> find k m0 = Some v. |
| 627 |
Proof using Type. clear; spec_t. Qed. |
| 628 |
Lemma add_full elt m0 v x' y' : @find elt y' (add x' v m0) = if E.eq_dec x' y' then Some v else find y' m0. |
| 629 |
Proof using Type. clear; spec_t. Qed. |
| 630 |
Lemma remove_full elt m0 x' y' : @find elt y' (remove x' m0) = if E.eq_dec x' y' then None else find y' m0. |
| 631 |
Proof using Type. clear; spec_t. Qed. |
| 632 |
Lemma map_full : forall (elt elt':Type)(m: t elt)(x:key)(f:elt->elt'), find x (map f m) = option_map f (find x m). |
| 633 |
Proof using Type. spec_t. Qed. |
| 634 |
End _Extra2. |
| 635 |
|
| 636 |
Section Spec. |
| 637 |
|
| 638 |
Variable elt:Type. |
| 639 |
Variable elt' elt'' : Type. |
| 640 |
Variable m m' m'' : t elt. |
| 641 |
Variable x y z : key. |
| 642 |
Variable e e' : elt. |
| 643 |
|
| 644 |
Lemma MapsTo_1 : E.eq x y -> MapsTo x e m -> MapsTo y e m. |
| 645 |
Proof using Type. clear; spec_t. Qed. |
| 646 |
Lemma mem_1 : In x m -> mem x m = true. |
| 647 |
Proof using Type. clear; spec_t. Qed. |
| 648 |
Lemma mem_2 : mem x m = true -> In x m. |
| 649 |
Proof using Type. clear; spec_t. Qed. |
| 650 |
Lemma empty_1 : Empty (@empty elt). |
| 651 |
Proof using Type. clear; pose proof M1.empty_1; spec_t. Qed. |
| 652 |
Lemma is_empty_1 : Empty m -> is_empty m = true. |
| 653 |
Proof using Type. clear; pose proof M1.is_empty_1; spec_t. Qed. |
| 654 |
Lemma is_empty_2 : is_empty m = true -> Empty m. |
| 655 |
Proof using Type. clear; pose proof M1.is_empty_2; spec_t. Qed. |
| 656 |
Lemma find_1 : MapsTo x e m -> find x m = Some e. |
| 657 |
Proof using Type. apply find_iff. Qed. |
| 658 |
Lemma find_2 : find x m = Some e -> MapsTo x e m. |
| 659 |
Proof using Type. apply find_iff. Qed. |
| 660 |
Lemma add_1 : E.eq x y -> MapsTo y e (add x e m). |
| 661 |
Proof using Type. clear; intros; rewrite !find_iff, !add_full in *; spec_t. Qed. |
| 662 |
Lemma add_2 : ~ E.eq x y -> MapsTo y e m -> MapsTo y e (add x e' m). |
| 663 |
Proof using Type. clear; intros; rewrite !find_iff, !add_full in *; spec_t. Qed. |
| 664 |
Lemma add_3 : ~ E.eq x y -> MapsTo y e (add x e' m) -> MapsTo y e m. |
| 665 |
Proof using Type. clear; intros; rewrite !find_iff, !add_full in *; spec_t. Qed. |
| 666 |
Lemma remove_1 : E.eq x y -> ~ In y (remove x m). |
| 667 |
Proof using Type. clear; cbv [In]; setoid_rewrite find_iff; setoid_rewrite remove_full; spec_t. Qed. |
| 668 |
Lemma remove_2 : ~ E.eq x y -> MapsTo y e m -> MapsTo y e (remove x m). |
| 669 |
Proof using Type. clear; cbv [In]; setoid_rewrite find_iff; setoid_rewrite remove_full; spec_t. Qed. |
| 670 |
Lemma remove_3 : MapsTo y e (remove x m) -> MapsTo y e m. |
| 671 |
Proof using Type. clear; cbv [In]; setoid_rewrite find_iff; setoid_rewrite remove_full; spec_t. Qed. |
| 672 |
Let elements_iff : |
| 673 |
InA (@eq_key_elt _) (x,e) (elements m) <-> MapsTo x e m. |
| 674 |
Proof. |
| 675 |
clear; cbv [MapsTo elements]. |
| 676 |
setoid_rewrite M1'.elements_mapsto_iff. |
| 677 |
setoid_rewrite M2'.elements_mapsto_iff. |
| 678 |
setoid_rewrite InA_alt. |
| 679 |
setoid_rewrite in_flat_map. |
| 680 |
repeat first [ setoid_rewrite ECompat.eq_alt |
| 681 |
| spec_t_step |
| 682 |
| progress destruct_head' prod ]. |
| 683 |
all: repeat first [ eexists (_, _) | apply conj | rewrite in_map_iff | eexists ]; cbn [fst snd]. |
| 684 |
all: try eassumption. |
| 685 |
all: try exact eq_refl. |
| 686 |
all: reflexivity. |
| 687 |
Qed. |
| 688 |
Lemma elements_1 : |
| 689 |
MapsTo x e m -> InA (@eq_key_elt _) (x,e) (elements m). |
| 690 |
Proof using Type. apply elements_iff. Qed. |
| 691 |
Lemma elements_2 : |
| 692 |
InA (@eq_key_elt _) (x,e) (elements m) -> MapsTo x e m. |
| 693 |
Proof using Type. apply elements_iff. Qed. |
| 694 |
Lemma elements_3w : NoDupA (@eq_key _) (elements m). |
| 695 |
Proof using Type. |
| 696 |
clear. |
| 697 |
pose proof (M1.elements_3w m). |
| 698 |
cbv [elements]. |
| 699 |
eapply @NoDupA_flat_map; try eassumption; try exact _. |
| 700 |
{ rewrite Forall_map, Forall_forall; intros; break_innermost_match. |
| 701 |
eapply NoDupA_map_inv'; [ | apply M2.elements_3w ]. |
| 702 |
spec_t. } |
| 703 |
{ cbv [eq_key M1.eq_key]; intros; rewrite !InA_alt in *. |
| 704 |
spec_t. } |
| 705 |
Qed. |
| 706 |
Lemma cardinal_1 : cardinal m = length (elements m). |
| 707 |
Proof using Type. clear; spec_t. Qed. |
| 708 |
|
| 709 |
Hint Rewrite fold_left_flat_map fold_left_map : prod_map_alt. |
| 710 |
Lemma fold_1 : |
| 711 |
forall (A : Type) (i : A) (f : key -> elt -> A -> A), |
| 712 |
fold f m i = fold_left (fun a p => f (fst p) (snd p) a) (elements m) i. |
| 713 |
Proof using Type. |
| 714 |
clear; repeat first [ f_equiv; repeat intro; [] |
| 715 |
| progress spec_t ]. |
| 716 |
Qed. |
| 717 |
|
| 718 |
Variable cmp : elt -> elt -> bool. |
| 719 |
Let equal_iff : Equivb cmp m m' <-> equal cmp m m' = true. |
| 720 |
Proof. |
| 721 |
clear; cbv [equal Equivb] in *. |
| 722 |
rewrite Equiv_alt_iff, <- M1'.equal_iff. |
| 723 |
cbv [Equiv_alt M1.Equivb Cmp]. |
| 724 |
f_equiv; repeat intro. |
| 725 |
rewrite <- M2'.equal_iff; reflexivity. |
| 726 |
Qed. |
| 727 |
Lemma equal_1 : Equivb cmp m m' -> equal cmp m m' = true. |
| 728 |
Proof using Type. apply equal_iff. Qed. |
| 729 |
Lemma equal_2 : equal cmp m m' = true -> Equivb cmp m m'. |
| 730 |
Proof using Type. apply equal_iff. Qed. |
| 731 |
End Spec. |
| 732 |
|
| 733 |
Lemma map_1 : forall (elt elt':Type)(m: t elt)(x:key)(e:elt)(f:elt->elt'), |
| 734 |
MapsTo x e m -> MapsTo x (f e) (map f m). |
| 735 |
Proof using Type. intros *; rewrite !find_iff, map_full; cbv [option_map]; break_innermost_match; spec_t. Qed. |
| 736 |
Lemma map_2 : forall (elt elt':Type)(m: t elt)(x:key)(f:elt->elt'), |
| 737 |
In x (map f m) -> In x m. |
| 738 |
Proof using Type. intros *; cbv [In]; setoid_rewrite find_iff; setoid_rewrite map_full; cbv [option_map]; break_innermost_match; spec_t. Qed. |
| 739 |
Lemma mapi_1 (elt elt':Type)(m: t elt)(x:key)(e:elt) |
| 740 |
(f:key->elt->elt') |
| 741 |
: MapsTo x e m -> |
| 742 |
exists y, E.eq y x /\ MapsTo x (f y e) (mapi f m). |
| 743 |
Proof using Type. |
| 744 |
cbv [mapi]. |
| 745 |
all: repeat first [ match goal with |
| 746 |
| [ |- context[M1.mapi ?f ?m] ] |
| 747 |
=> pose proof (fun k e => @M1.mapi_1 _ _ m k e f); |
| 748 |
set (M1.mapi f m) in * |
| 749 |
| [ |- context[M2.mapi ?f ?m] ] |
| 750 |
=> pose proof (fun k e => @M2.mapi_1 _ _ m k e f); |
| 751 |
set (M2.mapi f m) in * |
| 752 |
end |
| 753 |
| setoid_rewrite ECompat.eq_alt |
| 754 |
| spec_t_nosubst_step |
| 755 |
| specialize_under_binders_by eassumption |
| 756 |
| match goal with |
| 757 |
| [ H : E1.eq _ ?x |- _ ] => setoid_subst'' E1.eq x |
| 758 |
| [ H : E2.eq _ ?x |- _ ] => setoid_subst'' E2.eq x |
| 759 |
end ]. |
| 760 |
eexists (_, _); eauto. |
| 761 |
Qed. |
| 762 |
Lemma mapi_2 |
| 763 |
: forall (elt elt':Type)(m: t elt)(x:key) |
| 764 |
(f:key->elt->elt'), In x (mapi f m) -> In x m. |
| 765 |
Proof using Type. |
| 766 |
cbv [mapi In]. |
| 767 |
all: repeat first [ match goal with |
| 768 |
| [ H : context[M1.mapi ?f ?m] |- _ ] |
| 769 |
=> pose proof (fun k => @M1.mapi_2 _ _ m k f); |
| 770 |
pose proof (fun k e => @M1.mapi_1 _ _ m k e f); |
| 771 |
set (M1.mapi f m) in * |
| 772 |
| [ H : context[M2.mapi ?f ?m] |- _ ] |
| 773 |
=> pose proof (fun k => @M2.mapi_2 _ _ m k f); |
| 774 |
pose proof (fun k e => @M2.mapi_1 _ _ m k e f); |
| 775 |
set (M2.mapi f m) in * |
| 776 |
end |
| 777 |
| setoid_rewrite ECompat.eq_alt |
| 778 |
| spec_t_nosubst_step |
| 779 |
| progress cbv [M1.In M2.In] in * |
| 780 |
| progress cbn [proj1_sig] in * |
| 781 |
| specialize_under_binders_by eassumption ]. |
| 782 |
Qed. |
| 783 |
Lemma map2_1 |
| 784 |
: forall (elt elt' elt'':Type)(m: t elt)(m': t elt') |
| 785 |
(x:key)(f:option elt->option elt'->option elt''), |
| 786 |
In x m \/ In x m' -> |
| 787 |
find x (map2 f m m') = f (find x m) (find x m'). |
| 788 |
Proof using Type. |
| 789 |
cbv [In]. |
| 790 |
setoid_rewrite find_iff. |
| 791 |
intros. |
| 792 |
all: cbv [find map2] in *. |
| 793 |
all: rewrite M1.map2_1 |
| 794 |
by (cbv [M1.In find Option.bind option_map Option.value] in *; setoid_rewrite M1'.find_mapsto_iff; break_innermost_match_hyps; destruct_head'_or; destruct_head'_ex; inversion_option; eauto). |
| 795 |
cbv [Option.value option_map Option.bind] in *. |
| 796 |
break_match; break_match_hyps. |
| 797 |
all: inversion_option; subst; cbn [proj1_sig] in *. |
| 798 |
all: try discriminate. |
| 799 |
all: try solve [ destruct_head'_or; destruct_head'_ex; congruence ]. |
| 800 |
all: break_match; break_match_hyps. |
| 801 |
all: try discriminate. |
| 802 |
all: repeat match goal with |
| 803 |
| [ H : ?x = ?x |- _ ] => clear H |
| 804 |
| [ H : false = _ |- _ ] => symmetry in H |
| 805 |
| [ H : true = _ |- _ ] => symmetry in H |
| 806 |
end. |
| 807 |
all: lazymatch goal with |
| 808 |
| [ H : M2.is_empty (M2.map2 _ _ _) = true |- _ ] |
| 809 |
=> apply M2.is_empty_2 in H; |
| 810 |
cbv [M2.Empty not] in H; |
| 811 |
setoid_rewrite M2'.find_mapsto_iff in H; |
| 812 |
specialize_under_binders_by (rewrite M2.map2_1; cbv [M2.In]; try setoid_rewrite M2'.find_mapsto_iff; destruct_head'_or; destruct_head'_ex; eauto) |
| 813 |
| _ |
| 814 |
=> rewrite @M2.map2_1 in * |
| 815 |
by (cbv [M2.In]; setoid_rewrite M2'.find_mapsto_iff; destruct_head'_or; destruct_head'_ex; inversion_option; eauto) |
| 816 |
end. |
| 817 |
all: repeat first [ reflexivity |
| 818 |
| progress rewrite ?M2'.find_empty in * |
| 819 |
| progress inversion_option |
| 820 |
| progress subst |
| 821 |
| destruct_head'_or; destruct_head'_ex; congruence |
| 822 |
| destruct_head'_or; destruct_head'_ex; [] |
| 823 |
| progress break_match |
| 824 |
| progress break_match_hyps |
| 825 |
| solve [ eauto ] |
| 826 |
| match goal with |
| 827 |
| [ H : ?x = Some _ |- _ ] => rewrite H in * |
| 828 |
| [ H : ?x = None |- _ ] => rewrite H in * |
| 829 |
| [ |- None = Some _ ] => exfalso |
| 830 |
| [ |- None = ?x ] => destruct x eqn:? |
| 831 |
end ]. |
| 832 |
Qed. |
| 833 |
Lemma map2_2 |
| 834 |
: forall (elt elt' elt'':Type)(m: t elt)(m': t elt') |
| 835 |
(x:key)(f:option elt->option elt'->option elt''), |
| 836 |
In x (map2 f m m') -> In x m \/ In x m'. |
| 837 |
Proof using Type. |
| 838 |
cbv [In]. |
| 839 |
setoid_rewrite find_iff. |
| 840 |
cbv [find map2 Option.bind]. |
| 841 |
intros * HIn. |
| 842 |
repeat first [ progress destruct_head'_ex |
| 843 |
| progress inversion_option |
| 844 |
| match goal with |
| 845 |
| [ H : match ?x with _ => _ end = Some _ |- _ ] |
| 846 |
=> destruct x eqn:? |
| 847 |
end ]. |
| 848 |
lazymatch goal with |
| 849 |
| [ H : M1.find _ (M1.map2 _ _ _) = Some _ |- _ ] |
| 850 |
=> pose proof M1.map2_2 as H'; |
| 851 |
cbv [M1.In] in H'; |
| 852 |
setoid_rewrite M1'.find_mapsto_iff in H'; |
| 853 |
specialize_under_binders_by (eexists; exact H) |
| 854 |
end. |
| 855 |
pose proof M1.map2_1 as H''; |
| 856 |
cbv [M1.In] in H''; |
| 857 |
setoid_rewrite M1'.find_mapsto_iff in H''; |
| 858 |
specialize_under_binders_by exact H'. |
| 859 |
destruct H' as [[? H']|[? H']]; |
| 860 |
rewrite H' in *; |
| 861 |
rewrite H'' in *. |
| 862 |
all: cbv [option_map Option.bind Option.value] in *. |
| 863 |
all: repeat first [ progress inversion_option |
| 864 |
| progress subst |
| 865 |
| progress cbn [M2_m] in * |
| 866 |
| progress break_match_hyps ]. |
| 867 |
all: lazymatch goal with |
| 868 |
| [ H : context[M2.find _ (M2.map2 _ _ _)] |- _ ] |
| 869 |
=> let H' := fresh in |
| 870 |
pose proof M2.map2_2 as H'; |
| 871 |
cbv [M2.In] in H'; |
| 872 |
setoid_rewrite M2'.find_mapsto_iff in H'; |
| 873 |
specialize_under_binders_by (eexists; exact H); |
| 874 |
rewrite M2.map2_1 in H |
| 875 |
by (cbv [M2.In]; setoid_rewrite M2'.find_mapsto_iff; exact H') |
| 876 |
end. |
| 877 |
all: repeat first [ progress inversion_option |
| 878 |
| progress subst |
| 879 |
| progress cbn [proj1_sig] in * |
| 880 |
| rewrite M2'.find_empty in * |
| 881 |
| solve [ eauto ] |
| 882 |
| destruct_head'_or; destruct_head'_ex; inversion_option; subst; [] |
| 883 |
| progress break_match_hyps ]. |
| 884 |
Qed. |
| 885 |
End ProdWSfun_gen. |
| 886 |
End ProdWSfun_gen. |
| 887 |
|
| 888 |
Module ProdWSfun (E1 : DecidableTypeOrig) (E2 : DecidableTypeOrig) (M1 : WSfun E1) (M2 : WSfun E2). |
| 889 |
Module _ProdWSfun. |
| 890 |
Module Outer := ProdWSfun_gen E1 E2 M1 M2. |
| 891 |
Module E := ProdDecidableTypeOrig E1 E2. |
| 892 |
Module ECompat <: Outer.ESigCompat E. |
| 893 |
Lemma eq_alt : forall x y, E.eq x y <-> RelProd E1.eq E2.eq x y. |
| 894 |
Proof. cbv [E.eq]; reflexivity. Qed. |
| 895 |
End ECompat. |
| 896 |
Module Inner := Outer.ProdWSfun_gen E ECompat. |
| 897 |
End _ProdWSfun. |
| 898 |
Include _ProdWSfun.Inner. |
| 899 |
End ProdWSfun. |
| 900 |
|
| 901 |
Module ProdUsualWeakMap (M1 : UsualWS) (M2 : UsualWS) <: UsualWS. |
| 902 |
Module Outer := ProdWSfun_gen M1.E M2.E M1 M2. |
| 903 |
Module E := ProdUsualDecidableTypeOrig M1.E M2.E. |
| 904 |
Module ECompat <: Outer.ESigCompat E. |
| 905 |
Lemma eq_alt : forall x y, E.eq x y <-> RelProd M1.E.eq M2.E.eq x y. |
| 906 |
Proof. cbv; intros; break_innermost_match; intuition congruence. Qed. |
| 907 |
End ECompat. |
| 908 |
Module Inner := Outer.ProdWSfun_gen E ECompat. |
| 909 |
Include Inner. |
| 910 |
End ProdUsualWeakMap. |
| 911 |
|
| 912 |
Module ProdWeakMap (M1 : WS) (M2 : WS) <: WS. |
| 913 |
Include ProdWSfun M1.E M2.E M1 M2. |
| 914 |
Module E := _ProdWSfun.E. |
| 915 |
End ProdWeakMap. |
| 916 |
|
| 917 |
Module ProdSfun_gen (E1 : OrderedTypeOrig) (E2 : OrderedTypeOrig) (M1 : Sfun E1) (M2 : Sfun E2). |
| 918 |
Module ProdWSfun := ProdWSfun_gen E1 E2 M1 M2. |
| 919 |
Module P := ProdHasLt E1 E2. |
| 920 |
Module Type ESig := ProdWSfun.ESig <+ HasLt <+ HasMiniOrderedType. |
| 921 |
Module Type ESigCompat (E : ESig). |
| 922 |
Include ProdWSfun.ESigCompat E. |
| 923 |
Axiom lt_alt : forall x y, E.lt x y <-> P.lt x y. |
| 924 |
End ESigCompat. |
| 925 |
Module ProdSfun_gen (E : ESig) (ECompat : ESigCompat E) <: Sfun E. |
| 926 |
Include ProdWSfun.ProdWSfun_gen E ECompat. |
| 927 |
Module E1' := E1 <+ UpdateStrOrder. |
| 928 |
Global Remove Hints E1'.eq_refl E1'.eq_sym E1'.eq_trans : core. |
| 929 |
Module E2' := E2 <+ UpdateStrOrder. |
| 930 |
Global Remove Hints E2'.eq_refl E2'.eq_sym E2'.eq_trans : core. |
| 931 |
Local Existing Instances _Extra1.eq_key_equiv _Extra1.eq_key_elt_equiv. |
| 932 |
Local Hint Resolve E1'.eq_refl E1'.eq_sym E1'.eq_trans |
| 933 |
E2'.eq_refl E2'.eq_sym E2'.eq_trans |
| 934 |
: core. |
| 935 |
Section elt. |
| 936 |
Variable elt:Type. |
| 937 |
Definition lt_key (p p':key*elt) := E.lt (fst p) (fst p'). |
| 938 |
|
| 939 |
Lemma elements_3 m : sort lt_key (elements m). |
| 940 |
Proof using Type. |
| 941 |
pose proof (M1.elements_3 m). |
| 942 |
cbv [elements]. |
| 943 |
eapply @SortA_flat_map with (eqA:=@eq_key _) (eqB:=eq); try eassumption; try rewrite Forall_map, Forall_forall. |
| 944 |
all: match goal with |
| 945 |
| [ |- ?G ] => has_evar G; shelve |
| 946 |
| _ => idtac |
| 947 |
end. |
| 948 |
all: try exact _. |
| 949 |
all: lazymatch goal with |
| 950 |
| [ |- Transitive _ ] |
| 951 |
=> cbv [M1.lt_key]; repeat intro; eapply E1.lt_trans; eassumption |
| 952 |
| _ => idtac |
| 953 |
end. |
| 954 |
{ match goal with |
| 955 |
| [ |- context[Sorted] ] |
| 956 |
=> intros; break_innermost_match; rewrite Sorted_map_iff |
| 957 |
end. |
| 958 |
eapply Sorted_Proper_impl; [ | | apply M2.elements_3 ]; [ | reflexivity ]; |
| 959 |
repeat intro; break_innermost_match; cbv [lt_key M2.lt_key] in *; rewrite ECompat.lt_alt; cbv [P.lt]; cbn [fst snd] in *; |
| 960 |
destruct_head'_and; subst. |
| 961 |
pose proof (_ : Proper (_ ==> E2.eq ==> _) E2.lt). |
| 962 |
setoid_subst_rel E2.eq. |
| 963 |
eauto. } |
| 964 |
Unshelve. |
| 965 |
{ cbv [M1.lt_key lt_key]. |
| 966 |
intros; rewrite ECompat.lt_alt; cbv [P.lt]; break_innermost_match_hyps. |
| 967 |
rewrite InA_alt in *; destruct_head'_ex; destruct_head'_and. |
| 968 |
cbv [eq_key] in *; rewrite ECompat.eq_alt in *. |
| 969 |
repeat autounfold with prod_map_alt in *. |
| 970 |
rewrite in_map_iff in *. |
| 971 |
destruct_head'_ex; destruct_head'_and; break_innermost_match_hyps. |
| 972 |
subst. |
| 973 |
cbn [fst snd] in *. |
| 974 |
repeat match goal with H : List.In _ _ |- _ => clear H end. |
| 975 |
setoid_subst_rel E1.eq. |
| 976 |
left; assumption. } |
| 977 |
Qed. |
| 978 |
End elt. |
| 979 |
End ProdSfun_gen. |
| 980 |
End ProdSfun_gen. |
| 981 |
|
| 982 |
Module ProdSfun (E1 : OrderedTypeOrig) (E2 : OrderedTypeOrig) (M1 : Sfun E1) (M2 : Sfun E2). |
| 983 |
Module _ProdSfun. |
| 984 |
Module Outer := ProdSfun_gen E1 E2 M1 M2. |
| 985 |
Module E := ProdOrderedTypeOrig E1 E2. |
| 986 |
Module ECompat <: Outer.ESigCompat E. |
| 987 |
Lemma eq_alt : forall x y, E.eq x y <-> RelProd E1.eq E2.eq x y. |
| 988 |
Proof. cbv [E.eq]; reflexivity. Qed. |
| 989 |
Lemma lt_alt : forall x y, E.lt x y <-> E.lt x y. |
| 990 |
Proof. cbv [E.lt]; reflexivity. Qed. |
| 991 |
End ECompat. |
| 992 |
Module Inner := Outer.ProdSfun_gen E ECompat. |
| 993 |
End _ProdSfun. |
| 994 |
Include _ProdSfun.Inner. |
| 995 |
End ProdSfun. |
| 996 |
|
| 997 |
Module ProdUsualMap (M1 : UsualS) (M2 : UsualS) <: UsualS. |
| 998 |
Module Outer := ProdSfun_gen M1.E M2.E M1 M2. |
| 999 |
Module E := ProdUsualOrderedTypeOrig M1.E M2.E. |
| 1000 |
Module ECompat <: Outer.ESigCompat E. |
| 1001 |
Lemma eq_alt : forall x y, E.eq x y <-> RelProd M1.E.eq M2.E.eq x y. |
| 1002 |
Proof. cbv; intros; break_innermost_match; intuition congruence. Qed. |
| 1003 |
Lemma lt_alt : forall x y, E.lt x y <-> E.lt x y. |
| 1004 |
Proof. cbv [E.lt]; reflexivity. Qed. |
| 1005 |
End ECompat. |
| 1006 |
Module Inner := Outer.ProdSfun_gen E ECompat. |
| 1007 |
Include Inner. |
| 1008 |
End ProdUsualMap. |
| 1009 |
|
| 1010 |
Module ProdMap (M1 : S) (M2 : S) <: S. |
| 1011 |
Include ProdSfun M1.E M2.E M1 M2. |
| 1012 |
Module E := _ProdSfun.E. |
| 1013 |
End ProdMap. |
| 1014 |
|
| 1015 |
(* TODO: |
| 1016 |
Module IsoSord (S' : Sord) (Data' : IsoMiniOrderedType S'.Data) (E : IsoMiniOrderedType S'.MapS.E) (S'_iso : IsoMiniOrderedType S') <: Sord <: IsoOrderedType S'. |
| 1017 |
Module Data <: IsoOrderedType S'.Data := Data' <+ OT_of_MOT. |
| 1018 |
Module MapS <: S := IsoS S'.MapS E. |
| 1019 |
Import MapS. |
| 1020 |
|
| 1021 |
Definition t := MapS.t Data.t. |
| 1022 |
|
| 1023 |
Definition eq : t -> t -> Prop. |
| 1024 |
refine (_ S'.eq). |
| 1025 |
compute. |
| 1026 |
pose MapS.E. |
| 1027 |
Parameter lt : t -> t -> Prop. |
| 1028 |
|
| 1029 |
Axiom eq_refl : forall m : t, eq m m. |
| 1030 |
Axiom eq_sym : forall m1 m2 : t, eq m1 m2 -> eq m2 m1. |
| 1031 |
Axiom eq_trans : forall m1 m2 m3 : t, eq m1 m2 -> eq m2 m3 -> eq m1 m3. |
| 1032 |
Axiom lt_trans : forall m1 m2 m3 : t, lt m1 m2 -> lt m2 m3 -> lt m1 m3. |
| 1033 |
Axiom lt_not_eq : forall m1 m2 : t, lt m1 m2 -> ~ eq m1 m2. |
| 1034 |
|
| 1035 |
Definition cmp e e' := match Data.compare e e' with EQ _ => true | _ => false end. |
| 1036 |
|
| 1037 |
Parameter eq_1 : forall m m', Equivb cmp m m' -> eq m m'. |
| 1038 |
Parameter eq_2 : forall m m', eq m m' -> Equivb cmp m m'. |
| 1039 |
|
| 1040 |
Parameter compare : forall m1 m2, Compare lt eq m1 m2. |
| 1041 |
(** Total ordering between maps. [Data.compare] is a total ordering |
| 1042 |
used to compare data associated with equal keys in the two maps. *) |
| 1043 |
|
| 1044 |
|
| 1045 |
Include S. |
| 1046 |
Include OT_of_MOT. |
| 1047 |
End IsoSord. |
| 1048 |
Definition t := MapS.t Data.t. |
| 1049 |
|
| 1050 |
Print IsoMiniOrderedType. |
| 1051 |
Definition eq : t -> t -> Prop := S'.eq. |
| 1052 |
Parameter lt : t -> t -> Prop. |
| 1053 |
|
| 1054 |
Axiom eq_refl : forall m : t, eq m m. |
| 1055 |
Axiom eq_sym : forall m1 m2 : t, eq m1 m2 -> eq m2 m1. |
| 1056 |
Axiom eq_trans : forall m1 m2 m3 : t, eq m1 m2 -> eq m2 m3 -> eq m1 m3. |
| 1057 |
Axiom lt_trans : forall m1 m2 m3 : t, lt m1 m2 -> lt m2 m3 -> lt m1 m3. |
| 1058 |
Axiom lt_not_eq : forall m1 m2 : t, lt m1 m2 -> ~ eq m1 m2. |
| 1059 |
|
| 1060 |
Definition cmp e e' := match Data.compare e e' with EQ _ => true | _ => false end. |
| 1061 |
|
| 1062 |
Parameter eq_1 : forall m m', Equivb cmp m m' -> eq m m'. |
| 1063 |
Parameter eq_2 : forall m m', eq m m' -> Equivb cmp m m'. |
| 1064 |
|
| 1065 |
Parameter compare : forall m1 m2, Compare lt eq m1 m2. |
| 1066 |
(** Total ordering between maps. [Data.compare] is a total ordering |
| 1067 |
used to compare data associated with equal keys in the two maps. *) |
| 1068 |
|
| 1069 |
End Sord. |
| 1070 |
|
| 1071 |
*) |