Debian Codegraph @

src/Util/FSets/FMapSum.v

raw
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 From Coq Require Import FMapFacts.
7 Require Import Crypto.Util.ListUtil.
8 Require Import Crypto.Util.ListUtil.SetoidList.
9 Require Import Crypto.Util.Compose.
10 Require Import Crypto.Util.Logic.Forall.
11 Require Import Crypto.Util.Logic.Exists.
12 Require Import Crypto.Util.Sum.
13 Require Import Crypto.Util.Prod.
14 Require Import Crypto.Util.Option.
15 Require Import Crypto.Util.Relations.
16 Require Import Crypto.Util.Structures.Equalities.
17 Require Import Crypto.Util.Structures.Equalities.Prod.
18 Require Import Crypto.Util.Structures.Equalities.Sum.
19 Require Import Crypto.Util.Structures.Orders.
20 Require Import Crypto.Util.Structures.Orders.Prod.
21 Require Import Crypto.Util.Structures.Orders.Sum.
22 Require Import Crypto.Util.FSets.FMapInterface.
23 Require Import Crypto.Util.FSets.FMapFacts.
24 Require Import Crypto.Util.Sorting.Sorted.Proper.
25 Require Import Crypto.Util.Tactics.SplitInContext.
26 Require Import Crypto.Util.Tactics.DestructHead.
27 Require Import Crypto.Util.Tactics.SpecializeAllWays.
28 Require Import Crypto.Util.Tactics.SpecializeBy.
29 Require Import Crypto.Util.Tactics.SpecializeUnderBindersBy.
30 Require Import Crypto.Util.Tactics.Head.
31 Require Import Crypto.Util.Tactics.BreakMatch.
32 Require Import Crypto.Util.Tactics.SetoidSubst.
33
34 Local Set Implicit Arguments.
35 (* TODO: move to global settings *)
36 Local Set Keyed Unification.
37 Local Open Scope bool_scope.
38 Local Open Scope lazy_bool_scope.
39 Local Open Scope type_scope.
40
41 Module SumWSfun_gen (E1 : DecidableTypeOrig) (E2 : DecidableTypeOrig) (M1 : WSfun E1) (M2 : WSfun E2).
42 Module Type ESig := SumTyp E1 E2 <+ HasEq <+ IsEqOrig <+ HasEqDec.
43 Module Type ESigCompat (E : ESig).
44 Axiom eq_alt : forall x y, E.eq x y <-> sumwise E1.eq E2.eq x y.
45 End ESigCompat.
46 Module E1 <: DecidableTypeBoth := E1 <+ UpdateEq.
47 Global Remove Hints E1.eq_refl E1.eq_sym E1.eq_trans : core.
48 Module E2 <: DecidableTypeBoth := E2 <+ UpdateEq.
49 Global Remove Hints E2.eq_refl E2.eq_sym E2.eq_trans : core.
50 Module M1' := M1 <+ WFacts_fun E1 <+ WFacts_fun_RemoveHints E1 <+ WAdditionalFacts_fun E1.
51 Module M2' := M2 <+ WFacts_fun E2 <+ WFacts_fun_RemoveHints E2 <+ WAdditionalFacts_fun E2.
52 Local Existing Instances
53 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
54 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
55 | 10.
56 Local Existing Instances
57 M1'.eq_key_equiv M1'.eq_key_elt_equiv M1'.Equal_Equivalence
58 M2'.eq_key_equiv M2'.eq_key_elt_equiv M2'.Equal_Equivalence
59 | 10.
60 Local Existing Instances
61 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
62 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
63 .
64 Module SumWSfun_gen (E : ESig) (ECompat : ESigCompat E) <: WSfun E.
65 Module E' <: DecidableTypeBoth := E <+ UpdateEq <+ ECompat.
66 Global Remove Hints E.eq_refl E.eq_sym E.eq_trans : core.
67 Local Hint Resolve E1.eq_refl E1.eq_sym E1.eq_trans
68 E2.eq_refl E2.eq_sym E2.eq_trans
69 E.eq_refl E.eq_sym E.eq_trans
70 : core.
71
72 Definition key := E.t.
73
74 Global Hint Transparent key : core.
75
76 (* 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) *)
77 Record underlying_map elt := { underlying :> M1.t elt * M2.t elt }.
78 Declare Scope underlying_map_scope.
79 Bind Scope underlying_map_scope with map.
80 Notation "( x , y , .. , z )" := {| underlying := (pair .. (pair x y) .. z) |} : underlying_map_scope.
81 Definition t := underlying_map.
82 Bind Scope underlying_map_scope with t.
83 Local Delimit Scope underlying_map_scope with t.
84 Local Open Scope underlying_map_scope.
85
86 Module Import _Extra1.
87 Definition liftK {A} (f1 : M1.key -> A) (f2 : M2.key -> A) : key -> A
88 := fun x => match x with
89 | inl x => f1 x
90 | inr x => f2 x
91 end.
92 Definition liftT {elt} (f1 : M1.t elt) (f2 : M2.t elt) : t elt
93 := (f1, f2).
94 Definition liftTT {elt elt'}
95 (f1 : M1.t elt -> M1.t elt')
96 (f2 : M2.t elt -> M2.t elt')
97 : t elt -> t elt'
98 := fun m => (f1 (fst m), f2 (snd m)).
99 Definition lift_TT {elt elt' A}
100 (f1 : A -> M1.t elt -> M1.t elt')
101 (f2 : A -> M2.t elt -> M2.t elt')
102 : A -> t elt -> t elt'
103 := fun a => liftTT (f1 a) (f2 a).
104 Definition liftTTT {elt elt' elt''}
105 (f1 : M1.t elt -> M1.t elt' -> M1.t elt'')
106 (f2 : M2.t elt -> M2.t elt' -> M2.t elt'')
107 : t elt -> t elt' -> t elt''
108 := fun m m' => (f1 (fst m) (fst m'), f2 (snd m) (snd m')).
109 Definition lift_TTT {elt elt' elt'' A}
110 (f1 : A -> M1.t elt -> M1.t elt' -> M1.t elt'')
111 (f2 : A -> M2.t elt -> M2.t elt' -> M2.t elt'')
112 : A -> t elt -> t elt' -> t elt''
113 := fun a => liftTTT (f1 a) (f2 a).
114 Definition liftK_TT {A elt}
115 (f1 : M1.key -> A -> M1.t elt -> M1.t elt)
116 (f2 : M2.key -> A -> M2.t elt -> M2.t elt)
117 : key -> A -> t elt -> t elt
118 := fun k a m => (match k with
119 | inl k => f1 k a (fst m)
120 | inr _ => fst m
121 end,
122 match k with
123 | inl _ => snd m
124 | inr k => f2 k a (snd m)
125 end).
126 Definition liftKT_ {A elt}
127 (f1 : M1.key -> M1.t elt -> A)
128 (f2 : M2.key -> M2.t elt -> A)
129 : key -> t elt -> A
130 := fun k m => match k with
131 | inl k => f1 k (fst m)
132 | inr k => f2 k (snd m)
133 end.
134 Definition liftK_T_ {A B elt}
135 (f1 : M1.key -> A -> M1.t elt -> B)
136 (f2 : M2.key -> A -> M2.t elt -> B)
137 : key -> A -> t elt -> B
138 := fun k a m => match k with
139 | inl k => f1 k a (fst m)
140 | inr k => f2 k a (snd m)
141 end.
142 Definition liftKTT {elt}
143 (f1 : M1.key -> M1.t elt -> M1.t elt)
144 (f2 : M2.key -> M2.t elt -> M2.t elt)
145 : key -> t elt -> t elt
146 := fun k m => (match k with
147 | inl k => f1 k (fst m)
148 | inr _ => fst m
149 end,
150 match k with
151 | inl _ => snd m
152 | inr k => f2 k (snd m)
153 end).
154 Definition lifthoTT {elt elt' A}
155 (f1 : (M1.key -> A) -> M1.t elt -> M1.t elt')
156 (f2 : (M2.key -> A) -> M2.t elt -> M2.t elt')
157 : (key -> A) -> t elt -> t elt'
158 := fun f m => (f1 (fun k => f (inl k)) (fst m),
159 f2 (fun k => f (inr k)) (snd m)).
160 End _Extra1.
161
162 Definition empty elt : t elt := Eval hnf in liftT (@M1.empty _) (@M2.empty _).
163 Definition is_empty elt (m : t elt) : bool := M1.is_empty (fst m) &&& M2.is_empty (snd m).
164 Definition add elt : key -> elt -> t elt -> t elt := liftK_TT (@M1.add elt) (@M2.add elt).
165 Definition find elt : key -> t elt -> option elt := liftKT_ (@M1.find elt) (@M2.find elt).
166 Definition remove elt : key -> t elt -> t elt := liftKTT (@M1.remove elt) (@M2.remove elt).
167 Definition mem elt : key -> t elt -> bool := liftKT_ (@M1.mem elt) (@M2.mem elt).
168 Definition map elt elt' : (elt -> elt') -> t elt -> t elt' := lift_TT (@M1.map elt elt') (@M2.map elt elt').
169 Definition mapi elt elt' : (key -> elt -> elt') -> t elt -> t elt' := lifthoTT (@M1.mapi elt elt') (@M2.mapi elt elt').
170 Definition map2 elt elt' elt'' : (option elt -> option elt' -> option elt'') -> t elt -> t elt' -> t elt'' := lift_TTT (@M1.map2 elt elt' elt'') (@M2.map2 elt elt' elt'').
171 Definition elements elt (v : t elt) : list (key * elt)
172 := (List.map (fun kv => (inl (fst kv), snd kv)%core) (M1.elements (fst v)))
173 ++ List.map (fun kv => (inr (fst kv), snd kv)%core) (M2.elements (snd v)).
174 Definition cardinal elt (m : t elt) : nat := M1.cardinal (fst m) + M2.cardinal (snd m).
175 Definition fold elt A (f : key -> elt -> A -> A) (m : t elt) (i : A) : A
176 := M2.fold (fun k => f (inr k)) (snd m) (M1.fold (fun k => f (inl k)) (fst m) i).
177 Definition equal elt (eqb : elt -> elt -> bool) (m m' : t elt) : bool
178 := M1.equal eqb (fst m) (fst m') &&& M2.equal eqb (snd m) (snd m').
179 Definition MapsTo elt : key -> elt -> t elt -> Prop := liftK_T_ (@M1.MapsTo elt) (@M2.MapsTo elt).
180 Definition eq_key elt (p p':key*elt) := E.eq (fst p) (fst p').
181 Definition eq_key_elt elt (p p':key*elt) :=
182 E.eq (fst p) (fst p') /\ (snd p) = (snd p').
183
184 Definition In elt (k : key) (m : t elt) := exists e, MapsTo k e m.
185 Definition Empty elt (m : t elt) := forall a e, ~ MapsTo a e m.
186 Definition Equal elt (m m' : t elt) := forall y, find y m = find y m'.
187 Definition Equiv elt (eq_elt:elt->elt->Prop) m m' :=
188 (forall k, In k m <-> In k m') /\
189 (forall k e e', MapsTo k e m -> MapsTo k e' m' -> eq_elt e e').
190 Definition Equivb elt (cmp: elt->elt->bool) := Equiv (Cmp cmp).
191
192 Module Import _Extra2.
193 Local Instance eq_key_equiv elt : Equivalence (@eq_key elt) | 10.
194 Proof.
195 split; cbv; intros; break_innermost_match; break_innermost_match_hyps; try ((idtac + symmetry + etransitivity + exfalso); (eassumption + reflexivity)).
196 Qed.
197 Local Instance eq_key_elt_equiv elt : Equivalence (@eq_key_elt elt) | 10.
198 Proof.
199 split; cbv; intros; break_innermost_match; break_innermost_match_hyps; destruct_head'_and; split; try ((idtac + symmetry + etransitivity + exfalso); (eassumption + reflexivity)).
200 Qed.
201
202 Definition In_alt elt : key -> t elt -> Prop := liftKT_ (@M1.In elt) (@M2.In elt).
203 Definition Empty_alt elt (m : t elt) : Prop := M1.Empty (fst m) /\ M2.Empty (snd m).
204 Definition Equal_alt elt (m m' : t elt) : Prop := M1.Equal (fst m) (fst m') /\ M2.Equal (snd m) (snd m').
205 Definition Equiv_alt elt (cmp : elt -> elt -> Prop) (m m' : t elt) : Prop := M1.Equiv cmp (fst m) (fst m') /\ M2.Equiv cmp (snd m) (snd m').
206 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').
207
208 Local Ltac t_alt_iff :=
209 cbv [find MapsTo
210 In_alt Equal_alt Empty_alt Equiv_alt Equivb_alt
211 In Equal Empty Equiv Equivb
212 M1.In M1.Equal M1.Empty M1.Equiv M1.Equivb
213 M2.In M2.Equal M2.Empty M2.Equiv M2.Equivb
214 liftK_T_ liftKT_];
215 repeat first [ progress destruct_head' key
216 | reflexivity
217 | apply conj
218 | progress intros
219 | assumption
220 | progress destruct_head'_and
221 | progress destruct_head'_ex
222 | progress destruct_head' iff
223 | progress specialize_by eauto
224 | progress cbv beta iota in *
225 | match goal with
226 | [ H : forall a : M1.key, _, k : _ |- _ ] => specialize (H k)
227 | [ H : forall a : M2.key, _, k : _ |- _ ] => specialize (H k)
228 | [ H : _, k : _ |- _ ] => specialize (H (inl k))
229 | [ H : _, k : _ |- _ ] => specialize (H (inr k))
230 end
231 | solve [ eauto with nocore ] ].
232
233 Lemma In_alt_iff elt k (s : t elt) : In k s <-> In_alt k s.
234 Proof. t_alt_iff. Qed.
235 Lemma Equal_alt_iff elt (s s' : t elt) : Equal s s' <-> Equal_alt s s'.
236 Proof. t_alt_iff. Qed.
237 Lemma Empty_alt_iff elt (s : t elt) : Empty s <-> Empty_alt s.
238 Proof. t_alt_iff. Qed.
239 Lemma Equiv_alt_iff elt eq_elt (s s' : t elt) : Equiv eq_elt s s' <-> Equiv_alt eq_elt s s'.
240 Proof. t_alt_iff. Qed.
241 Lemma Equivb_alt_iff elt cmp (s s' : t elt) : Equivb cmp s s' <-> Equivb_alt cmp s s'.
242 Proof. t_alt_iff. Qed.
243 End _Extra2.
244
245 Local Existing Instances eq_key_equiv eq_key_elt_equiv | 10.
246
247 Create HintDb sum_map_alt discriminated.
248 Create HintDb sum_map_alt1 discriminated.
249 Create HintDb sum_map_alt2 discriminated.
250 Create HintDb sum_map_alt3 discriminated.
251
252 Global
253 Hint Unfold
254 In_alt
255 empty
256 is_empty
257 mem
258 add
259 find
260 remove
261 mem
262 map
263 mapi
264 map2
265 elements
266 cardinal
267 fold
268 equal
269 MapsTo
270 In
271 Empty
272 Empty_alt
273 Equiv_alt
274 Equivb_alt
275 eq_key
276 eq_key_elt
277 M1.eq_key
278 M1.eq_key_elt
279 M2.eq_key
280 M2.eq_key_elt
281 option_map
282 lift
283 liftK
284 liftT
285 liftTT
286 lift_TT
287 liftTTT
288 lift_TTT
289 liftK_TT
290 liftKT_
291 liftK_T_
292 liftKTT
293 lifthoTT
294 sumwise
295 : sum_map_alt.
296
297 #[global]
298 Hint Rewrite In_alt_iff Empty_alt_iff Equal_alt_iff Equiv_alt_iff Equivb_alt_iff
299 M1.cardinal_1
300 M1.fold_1
301 M2.cardinal_1
302 M2.fold_1
303 E'.eq_alt
304 Bool.andb_true_iff
305 InA_app_iff
306 map_length
307 app_length
308 fold_left_map
309 fold_left_app
310 : sum_map_alt.
311
312 Global Hint Resolve
313 M1.MapsTo_1
314 M1.mem_1
315 M1.empty_1
316 M1.is_empty_1
317 M1.add_1
318 M1.remove_1
319 M1.find_1
320 M1.elements_1
321 M1.equal_1
322 M1.map_1
323 M1.mapi_1
324 M1.map2_1
325 M2.MapsTo_1
326 M2.mem_1
327 M2.empty_1
328 M2.is_empty_1
329 M2.add_1
330 M2.remove_1
331 M2.find_1
332 M2.elements_1
333 M2.equal_1
334 M2.map_1
335 M2.mapi_1
336 M2.map2_1
337 : sum_map_alt1.
338 Global Hint Resolve
339 M1.mem_2
340 M1.is_empty_2
341 M1.add_2
342 M1.remove_2
343 M1.find_2
344 M1.elements_2
345 M1.equal_2
346 M1.map_2
347 M1.mapi_2
348 M1.map2_2
349 M2.mem_2
350 M2.is_empty_2
351 M2.add_2
352 M2.remove_2
353 M2.find_2
354 M2.elements_2
355 M2.equal_2
356 M2.map_2
357 M2.mapi_2
358 M2.map2_2
359 : sum_map_alt2.
360 Global Hint Resolve
361 M1.add_3
362 M1.remove_3
363 M1.elements_3w
364 M2.add_3
365 M2.remove_3
366 M2.elements_3w
367 : sum_map_alt3.
368
369 #[global]
370 Hint Constructors ex and or
371 : sum_map_alt1 sum_map_alt2 sum_map_alt3.
372 #[global]
373 Hint Extern 10
374 => progress unfold M1.In, M2.In in *
375 : sum_map_alt1 sum_map_alt2 sum_map_alt3.
376
377 Local Ltac spec_t_step_quick
378 := first [ progress intros
379 | progress cbn [fst snd underlying] in *
380 | apply (f_equal2 (@pair _ _))
381 | progress destruct_head'_False
382 | rewrite <- andb_lazy_alt
383 | progress repeat autorewrite with sum_map_alt in *
384 | progress change E1.t with M1.key in *
385 | progress change E2.t with M2.key in *
386 | progress repeat autounfold with sum_map_alt in *
387 | match goal with H : _ |- _ => refine H end
388 | congruence
389 | reflexivity
390 | progress auto
391 | exact _
392 | progress destruct_head'_and
393 | progress destruct_head'_ex
394 | progress break_innermost_match_hyps
395 | progress break_innermost_match
396 | progress destruct_head'_or
397 | progress specialize_under_binders_by eassumption
398 | progress subst ].
399
400 Global Hint Extern 100
401 => spec_t_step_quick
402 : sum_map_alt1 sum_map_alt2 sum_map_alt3.
403
404 Local Ltac spec_t
405 := repeat first [ spec_t_step_quick
406 | solve [ eauto with sum_map_alt1 nocore ]
407 | solve [ eauto with sum_map_alt2 nocore ]
408 | solve [ eauto with sum_map_alt3 nocore ] ].
409
410 Local Hint Extern 2 => Proper_compose_hint : typeclass_instances.
411
412 Section Spec.
413
414 Variable elt:Type.
415 Variable elt' elt'' : Type.
416 Variable m m' m'' : t elt.
417 Variable x y z : key.
418 Variable e e' : elt.
419
420 Lemma MapsTo_1 : E.eq x y -> MapsTo x e m -> MapsTo y e m.
421 Proof using Type. clear; spec_t. Qed.
422 Lemma mem_1 : In x m -> mem x m = true.
423 Proof using Type. clear; spec_t. Qed.
424 Lemma mem_2 : mem x m = true -> In x m.
425 Proof using Type. clear; spec_t. Qed.
426 Lemma empty_1 : Empty (@empty elt).
427 Proof using Type. clear; spec_t. Qed.
428 Lemma is_empty_1 : Empty m -> is_empty m = true.
429 Proof using Type. clear; spec_t. Qed.
430 Lemma is_empty_2 : is_empty m = true -> Empty m.
431 Proof using Type. clear; spec_t. Qed.
432 Lemma add_1 : E.eq x y -> MapsTo y e (add x e m).
433 Proof using Type. clear; spec_t. Qed.
434 Lemma add_2 : ~ E.eq x y -> MapsTo y e m -> MapsTo y e (add x e' m).
435 Proof using Type. clear; spec_t. Qed.
436 Lemma add_3 : ~ E.eq x y -> MapsTo y e (add x e' m) -> MapsTo y e m.
437 Proof using Type. clear; spec_t. Qed.
438 Lemma remove_1 : E.eq x y -> ~ In y (remove x m).
439 Proof using Type. clear; spec_t. Qed.
440 Lemma remove_2 : ~ E.eq x y -> MapsTo y e m -> MapsTo y e (remove x m).
441 Proof using Type. clear; spec_t. Qed.
442 Lemma remove_3 : MapsTo y e (remove x m) -> MapsTo y e m.
443 Proof using Type. clear; spec_t. Qed.
444 Lemma find_1 : MapsTo x e m -> find x m = Some e.
445 Proof using Type. clear; spec_t. Qed.
446 Lemma find_2 : find x m = Some e -> MapsTo x e m.
447 Proof using Type. clear; spec_t. Qed.
448 Lemma elements_1 :
449 MapsTo x e m -> InA (@eq_key_elt _) (x,e)%core (elements m).
450 Proof using Type.
451 clear; spec_t.
452 all: [ > left | right ]; rewrite InA_map_iff; spec_t.
453 Qed.
454 Lemma elements_2 :
455 InA (@eq_key_elt _) (x,e)%core (elements m) -> MapsTo x e m.
456 Proof using Type.
457 clear; spec_t.
458 all: try solve [ rewrite InA_map_iff in *; spec_t ].
459 all: rewrite InA_alt in *; destruct_head'_ex; rewrite in_map_iff in *.
460 all: spec_t.
461 Qed.
462 Lemma elements_3w : NoDupA (@eq_key _) (elements m).
463 Proof using Type.
464 pose proof (M1.elements_3w (fst m)).
465 pose proof (M2.elements_3w (snd m)).
466 spec_t; apply NoDupA_app; try exact _.
467 all:
468 [ >
469 |
470 | now intros *; rewrite !InA_alt; setoid_rewrite in_map_iff; spec_t ].
471 all: eapply NoDupA_map_inv'; [ | eassumption ]; spec_t.
472 Qed.
473 Lemma cardinal_1 : cardinal m = length (elements m).
474 Proof using Type. clear; spec_t. Qed.
475
476 Lemma fold_1 :
477 forall (A : Type) (i : A) (f : key -> elt -> A -> A),
478 fold f m i = fold_left (fun a p => f (fst p) (snd p) a) (elements m) i.
479 Proof using Type. clear; spec_t. Qed.
480
481 Variable cmp : elt -> elt -> bool.
482
483 Lemma equal_1 : Equivb cmp m m' -> equal cmp m m' = true.
484 Proof using Type. clear; spec_t. Qed.
485 Lemma equal_2 : equal cmp m m' = true -> Equivb cmp m m'.
486 Proof using Type. clear; spec_t. Qed.
487 End Spec.
488
489 Lemma map_1 : forall (elt elt':Type)(m: t elt)(x:key)(e:elt)(f:elt->elt'),
490 MapsTo x e m -> MapsTo x (f e) (map f m).
491 Proof. clear; spec_t. Qed.
492 Lemma map_2 : forall (elt elt':Type)(m: t elt)(x:key)(f:elt->elt'),
493 In x (map f m) -> In x m.
494 Proof. clear; spec_t. Qed.
495 Lemma mapi_1 (elt elt':Type)(m: t elt)(x:key)(e:elt)
496 (f:key->elt->elt')
497 : MapsTo x e m ->
498 exists y, E.eq y x /\ MapsTo x (f y e) (mapi f m).
499 Proof.
500 pose proof (fun x => @M1.mapi_1 elt elt' (fst m) x e (fun x => f (inl x))).
501 pose proof (fun x => @M2.mapi_1 elt elt' (snd m) x e (fun x => f (inr x))).
502 spec_t.
503 all: eexists.
504 all: split; [ | eassumption ].
505 all: spec_t.
506 Qed.
507 Lemma mapi_2
508 : forall (elt elt':Type)(m: t elt)(x:key)
509 (f:key->elt->elt'), In x (mapi f m) -> In x m.
510 Proof. clear; spec_t. Qed.
511 Lemma map2_1
512 : forall (elt elt' elt'':Type)(m: t elt)(m': t elt')
513 (x:key)(f:option elt->option elt'->option elt''),
514 In x m \/ In x m' ->
515 find x (map2 f m m') = f (find x m) (find x m').
516 Proof. clear; spec_t. Qed.
517 Lemma map2_2
518 : forall (elt elt' elt'':Type)(m: t elt)(m': t elt')
519 (x:key)(f:option elt->option elt'->option elt''),
520 In x (map2 f m m') -> In x m \/ In x m'.
521 Proof. clear; spec_t. Qed.
522 End SumWSfun_gen.
523 End SumWSfun_gen.
524
525 Module SumWSfun (E1 : DecidableTypeOrig) (E2 : DecidableTypeOrig) (M1 : WSfun E1) (M2 : WSfun E2).
526 Module _SumWSfun.
527 Module Outer := SumWSfun_gen E1 E2 M1 M2.
528 Module E := SumDecidableTypeOrig E1 E2.
529 Module ECompat <: Outer.ESigCompat E.
530 Lemma eq_alt : forall x y, E.eq x y <-> sumwise E1.eq E2.eq x y.
531 Proof. cbv [E.eq]; reflexivity. Qed.
532 End ECompat.
533 Module Inner := Outer.SumWSfun_gen E ECompat.
534 End _SumWSfun.
535 Include _SumWSfun.Inner.
536 End SumWSfun.
537
538 Module SumUsualWeakMap (M1 : UsualWS) (M2 : UsualWS) <: UsualWS.
539 Module Outer := SumWSfun_gen M1.E M2.E M1 M2.
540 Module E := SumUsualDecidableTypeOrig M1.E M2.E.
541 Module ECompat <: Outer.ESigCompat E.
542 Lemma eq_alt : forall x y, E.eq x y <-> sumwise M1.E.eq M2.E.eq x y.
543 Proof. cbv [E.eq sumwise M1.E.eq M2.E.eq]; intros; break_innermost_match; intuition congruence. Qed.
544 End ECompat.
545 Module Inner := Outer.SumWSfun_gen E ECompat.
546 Include Inner.
547 End SumUsualWeakMap.
548
549 Module SumWeakMap (M1 : WS) (M2 : WS) <: WS.
550 Include SumWSfun M1.E M2.E M1 M2.
551 Module E := _SumWSfun.E.
552 End SumWeakMap.
553
554 Module SumSfun_gen (E1 : OrderedTypeOrig) (E2 : OrderedTypeOrig) (M1 : Sfun E1) (M2 : Sfun E2).
555 Module SumWSfun := SumWSfun_gen E1 E2 M1 M2.
556 Module Type ESig := SumWSfun.ESig <+ HasLt <+ HasMiniOrderedType.
557 Module Type ESigCompat (E : ESig).
558 Include SumWSfun.ESigCompat E.
559 Axiom lt_alt : forall x y, E.lt x y <-> sum_le E1.lt E2.lt x y.
560 End ESigCompat.
561 Module SumSfun_gen (E : ESig) (ECompat : ESigCompat E) <: Sfun E.
562 Include SumWSfun.SumWSfun_gen E ECompat.
563 Local Existing Instances _Extra2.eq_key_equiv _Extra2.eq_key_elt_equiv.
564 Section elt.
565 Variable elt:Type.
566 Definition lt_key (p p':key*elt) := E.lt (fst p) (fst p').
567
568 Lemma elements_3 m : sort lt_key (elements m).
569 Proof using Type.
570 pose proof (M1.elements_3 (fst m)).
571 pose proof (M2.elements_3 (snd m)).
572 cbv [elements lt_key M1.lt_key M2.lt_key] in *.
573 setoid_rewrite ECompat.lt_alt; cbv [sum_le].
574 eapply SortA_app with (eqA:=@eq_key _); try exact _;
575 intros *;
576 rewrite ?Sorted_map_iff, ?InA_alt;
577 cbn [fst snd]; cbv [eq_key]; try assumption.
578 setoid_rewrite in_map_iff.
579 setoid_rewrite ECompat.eq_alt.
580 cbv [sumwise].
581 repeat first [ progress intros
582 | progress subst
583 | progress cbn [fst snd] in *
584 | progress destruct_head'_and
585 | progress destruct_head'_ex
586 | progress break_innermost_match
587 | exact I
588 | exfalso; assumption ].
589 Qed.
590 End elt.
591 End SumSfun_gen.
592 End SumSfun_gen.
593
594 Module SumSfun (E1 : OrderedTypeOrig) (E2 : OrderedTypeOrig) (M1 : Sfun E1) (M2 : Sfun E2).
595 Module _SumSfun.
596 Module Outer := SumSfun_gen E1 E2 M1 M2.
597 Module E := SumOrderedTypeOrig E1 E2.
598 Module ECompat <: Outer.ESigCompat E.
599 Lemma eq_alt : forall x y, E.eq x y <-> sumwise E1.eq E2.eq x y.
600 Proof. cbv [E.eq]; reflexivity. Qed.
601 Lemma lt_alt : forall x y, E.lt x y <-> sum_le E1.lt E2.lt x y.
602 Proof. cbv [E.lt]; reflexivity. Qed.
603 End ECompat.
604 Module Inner := Outer.SumSfun_gen E ECompat.
605 End _SumSfun.
606 Include _SumSfun.Inner.
607 End SumSfun.
608
609 Module SumUsualMap (M1 : UsualS) (M2 : UsualS).
610 Module Outer := SumSfun_gen M1.E M2.E M1 M2.
611 Module E := SumUsualOrderedTypeOrig M1.E M2.E.
612 Module ECompat <: Outer.ESigCompat E.
613 Lemma eq_alt : forall x y, E.eq x y <-> sumwise M1.E.eq M2.E.eq x y.
614 Proof. cbv [E.eq sumwise M1.E.eq M2.E.eq]; intros; break_innermost_match; intuition congruence. Qed.
615 Lemma lt_alt : forall x y, E.lt x y <-> sum_le M1.E.lt M2.E.lt x y.
616 Proof. cbv [E.lt]; reflexivity. Qed.
617 End ECompat.
618 Module Inner := Outer.SumSfun_gen E ECompat.
619 Include Inner.
620 End SumUsualMap.
621
622 Module SumMap (M1 : S) (M2 : S) <: S.
623 Include SumSfun M1.E M2.E M1 M2.
624 Module E := _SumSfun.E.
625 End SumMap.
626
627 (* TODO:
628 Module IsoSord (S' : Sord) (Data' : IsoMiniOrderedType S'.Data) (E : IsoMiniOrderedType S'.MapS.E) (S'_iso : IsoMiniOrderedType S') <: Sord <: IsoOrderedType S'.
629 Module Data <: IsoOrderedType S'.Data := Data' <+ OT_of_MOT.
630 Module MapS <: S := IsoS S'.MapS E.
631 Import MapS.
632
633 Definition t := MapS.t Data.t.
634
635 Definition eq : t -> t -> Prop.
636 refine (_ S'.eq).
637 compute.
638 pose MapS.E.
639 Parameter lt : t -> t -> Prop.
640
641 Axiom eq_refl : forall m : t, eq m m.
642 Axiom eq_sym : forall m1 m2 : t, eq m1 m2 -> eq m2 m1.
643 Axiom eq_trans : forall m1 m2 m3 : t, eq m1 m2 -> eq m2 m3 -> eq m1 m3.
644 Axiom lt_trans : forall m1 m2 m3 : t, lt m1 m2 -> lt m2 m3 -> lt m1 m3.
645 Axiom lt_not_eq : forall m1 m2 : t, lt m1 m2 -> ~ eq m1 m2.
646
647 Definition cmp e e' := match Data.compare e e' with EQ _ => true | _ => false end.
648
649 Parameter eq_1 : forall m m', Equivb cmp m m' -> eq m m'.
650 Parameter eq_2 : forall m m', eq m m' -> Equivb cmp m m'.
651
652 Parameter compare : forall m1 m2, Compare lt eq m1 m2.
653 (** Total ordering between maps. [Data.compare] is a total ordering
654 used to compare data associated with equal keys in the two maps. *)
655
656
657 Include S.
658 Include OT_of_MOT.
659 End IsoSord.
660 Definition t := MapS.t Data.t.
661
662 Print IsoMiniOrderedType.
663 Definition eq : t -> t -> Prop := S'.eq.
664 Parameter lt : t -> t -> Prop.
665
666 Axiom eq_refl : forall m : t, eq m m.
667 Axiom eq_sym : forall m1 m2 : t, eq m1 m2 -> eq m2 m1.
668 Axiom eq_trans : forall m1 m2 m3 : t, eq m1 m2 -> eq m2 m3 -> eq m1 m3.
669 Axiom lt_trans : forall m1 m2 m3 : t, lt m1 m2 -> lt m2 m3 -> lt m1 m3.
670 Axiom lt_not_eq : forall m1 m2 : t, lt m1 m2 -> ~ eq m1 m2.
671
672 Definition cmp e e' := match Data.compare e e' with EQ _ => true | _ => false end.
673
674 Parameter eq_1 : forall m m', Equivb cmp m m' -> eq m m'.
675 Parameter eq_2 : forall m m', eq m m' -> Equivb cmp m m'.
676
677 Parameter compare : forall m1 m2, Compare lt eq m1 m2.
678 (** Total ordering between maps. [Data.compare] is a total ordering
679 used to compare data associated with equal keys in the two maps. *)
680
681 End Sord.
682
683 *)