Debian Codegraph @

src/Util/FSets/FMapOption.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.Prod.
13 Require Import Crypto.Util.Option.
14 Require Import Crypto.Util.Relations.
15 Require Import Crypto.Util.Structures.Equalities.
16 Require Import Crypto.Util.Structures.Equalities.Unit.
17 Require Import Crypto.Util.Structures.Equalities.Prod.
18 Require Import Crypto.Util.Structures.Equalities.Option.
19 Require Import Crypto.Util.Structures.Orders.
20 Require Import Crypto.Util.Structures.Orders.Unit.
21 Require Import Crypto.Util.Structures.Orders.Prod.
22 Require Import Crypto.Util.Structures.Orders.Option.
23 Require Import Crypto.Util.FSets.FMapInterface.
24 Require Import Crypto.Util.FSets.FMapFacts.
25 Require Import Crypto.Util.FSets.FMapUnit.
26 Require Import Crypto.Util.Sorting.Sorted.Proper.
27 Require Import Crypto.Util.Tactics.SplitInContext.
28 Require Import Crypto.Util.Tactics.DestructHead.
29 Require Import Crypto.Util.Tactics.SpecializeAllWays.
30 Require Import Crypto.Util.Tactics.SpecializeBy.
31 Require Import Crypto.Util.Tactics.SpecializeUnderBindersBy.
32 Require Import Crypto.Util.Tactics.Head.
33 Require Import Crypto.Util.Tactics.BreakMatch.
34 Require Import Crypto.Util.Tactics.SetoidSubst.
35
36 Local Set Implicit Arguments.
37 (* TODO: move to global settings *)
38 Local Set Keyed Unification.
39 Local Open Scope bool_scope.
40 Local Open Scope lazy_bool_scope.
41 Local Open Scope type_scope.
42
43 Module OptionWSfun_gen (E2 : DecidableTypeOrig) (M2 : WSfun E2).
44 Module Type ESig := OptionTyp E2 <+ HasEq <+ IsEqOrig <+ HasEqDec.
45 Module Type ESigCompat (E : ESig).
46 Axiom eq_alt : forall x y, E.eq x y <-> option_eq E2.eq x y.
47 End ESigCompat.
48 Module E1 <: DecidableTypeBoth := UnitDecidableTypeBoth.
49 Module E2 <: DecidableTypeBoth := E2 <+ UpdateEq.
50 Global Remove Hints E2.eq_refl E2.eq_sym E2.eq_trans : core.
51 Module M1 <: WSfun E1 := UnitMap.
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 OptionWSfun_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 E2.eq_refl E2.eq_sym E2.eq_trans : core.
70 Local Hint Resolve E.eq_refl E.eq_sym E.eq_trans : 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 | None => f1 tt
90 | Some 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 | None => f1 tt a (fst m)
120 | Some _ => fst m
121 end,
122 match k with
123 | None => snd m
124 | Some 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 | None => f1 tt (fst m)
132 | Some 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 | None => f1 tt a (fst m)
140 | Some 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 | None => f1 tt (fst m)
148 | Some _ => fst m
149 end,
150 match k with
151 | None => snd m
152 | Some 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 (None)) (fst m),
159 f2 (fun k => f (Some 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 => (None, snd kv)%core) (M1.elements (fst v)))
173 ++ List.map (fun kv => (Some (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 (Some k)) (snd m) (M1.fold (fun k => f (None)) (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 tt)
227 | [ H : forall a : M2.key, _, k : _ |- _ ] => specialize (H k)
228 | [ H : _, k : _ |- _ ] => specialize (H (Some k))
229 | [ H : _ |- _ ] => specialize (H None)
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 Local Existing Instances eq_key_equiv eq_key_elt_equiv | 10.
245
246 Create HintDb option_map_alt discriminated.
247 Create HintDb option_map_alt1 discriminated.
248 Create HintDb option_map_alt2 discriminated.
249 Create HintDb option_map_alt3 discriminated.
250
251 Global
252 Hint Unfold
253 In_alt
254 empty
255 is_empty
256 mem
257 add
258 find
259 remove
260 mem
261 map
262 mapi
263 map2
264 elements
265 cardinal
266 fold
267 equal
268 MapsTo
269 In
270 Empty
271 Empty_alt
272 Equiv_alt
273 Equivb_alt
274 eq_key
275 eq_key_elt
276 M1.eq_key
277 M1.eq_key_elt
278 M2.eq_key
279 M2.eq_key_elt
280 option_map
281 lift
282 liftK
283 liftT
284 liftTT
285 lift_TT
286 liftTTT
287 lift_TTT
288 liftK_TT
289 liftKT_
290 liftK_T_
291 liftKTT
292 lifthoTT
293 option_eq
294 M1.E.eq
295 : option_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 : option_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 : option_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 : option_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 : option_map_alt3.
368
369 #[global]
370 Hint Constructors ex and or
371 : option_map_alt1 option_map_alt2 option_map_alt3.
372 #[global]
373 Hint Extern 10
374 => progress unfold M1.In, M2.In in *
375 : option_map_alt1 option_map_alt2 option_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 option_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 option_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 | progress destruct_head'_prod
400 | progress destruct_head'_unit
401 | progress destruct_head' M1.key ].
402
403 Global Hint Extern 100
404 => spec_t_step_quick
405 : option_map_alt1 option_map_alt2 option_map_alt3.
406
407 Local Ltac spec_t
408 := repeat first [ spec_t_step_quick
409 | solve [ eauto with option_map_alt1 nocore ]
410 | solve [ eauto with option_map_alt2 nocore ]
411 | solve [ eauto with option_map_alt3 nocore ] ].
412
413 Local Hint Extern 2 => Proper_compose_hint : typeclass_instances.
414
415 Section Spec.
416
417 Variable elt:Type.
418 Variable elt' elt'' : Type.
419 Variable m m' m'' : t elt.
420 Variable x y z : key.
421 Variable e e' : elt.
422
423 Lemma MapsTo_1 : E.eq x y -> MapsTo x e m -> MapsTo y e m.
424 Proof using Type. clear; spec_t. Qed.
425 Lemma mem_1 : In x m -> mem x m = true.
426 Proof using Type. clear; spec_t. Qed.
427 Lemma mem_2 : mem x m = true -> In x m.
428 Proof using Type. clear; spec_t. Qed.
429 Lemma empty_1 : Empty (@empty elt).
430 Proof using Type. clear; spec_t. Qed.
431 Lemma is_empty_1 : Empty m -> is_empty m = true.
432 Proof using Type. clear; spec_t. Qed.
433 Lemma is_empty_2 : is_empty m = true -> Empty m.
434 Proof using Type. clear; spec_t. Qed.
435 Lemma add_1 : E.eq x y -> MapsTo y e (add x e m).
436 Proof using Type. clear; spec_t. Qed.
437 Lemma add_2 : ~ E.eq x y -> MapsTo y e m -> MapsTo y e (add x e' m).
438 Proof using Type. clear; spec_t. Qed.
439 Lemma add_3 : ~ E.eq x y -> MapsTo y e (add x e' m) -> MapsTo y e m.
440 Proof using Type. clear; spec_t. Qed.
441 Lemma remove_1 : E.eq x y -> ~ In y (remove x m).
442 Proof using Type. clear; spec_t. Qed.
443 Lemma remove_2 : ~ E.eq x y -> MapsTo y e m -> MapsTo y e (remove x m).
444 Proof using Type. clear; spec_t. Qed.
445 Lemma remove_3 : MapsTo y e (remove x m) -> MapsTo y e m.
446 Proof using Type. clear; spec_t. Qed.
447 Lemma find_1 : MapsTo x e m -> find x m = Some e.
448 Proof using Type. clear; spec_t. Qed.
449 Lemma find_2 : find x m = Some e -> MapsTo x e m.
450 Proof using Type. clear; spec_t. Qed.
451 Lemma elements_1 :
452 MapsTo x e m -> InA (@eq_key_elt _) (x,e)%core (elements m).
453 Proof using Type.
454 clear; spec_t.
455 all: [ > right | left ]; rewrite InA_map_iff; spec_t.
456 intuition eauto.
457 Qed.
458 Lemma elements_2 :
459 InA (@eq_key_elt _) (x,e)%core (elements m) -> MapsTo x e m.
460 Proof using Type.
461 clear; spec_t.
462 all: (apply M1.elements_2 + apply M2.elements_2).
463 all: try solve [ rewrite InA_map_iff in *; spec_t ].
464 all: rewrite InA_alt in *; destruct_head'_ex; rewrite in_map_iff in *.
465 all: spec_t.
466 repeat esplit; eauto; eauto.
467 Qed.
468 Lemma elements_3w : NoDupA (@eq_key _) (elements m).
469 Proof using Type.
470 pose proof (M1.elements_3w (fst m)).
471 pose proof (M2.elements_3w (snd m)).
472 spec_t; apply NoDupA_app; try exact _.
473 all:
474 [ >
475 |
476 | now intros *; rewrite !InA_alt; setoid_rewrite in_map_iff; spec_t ].
477 all: eapply NoDupA_map_inv'; [ | eassumption ]; spec_t.
478 Qed.
479 Lemma cardinal_1 : cardinal m = length (elements m).
480 Proof using Type. clear; spec_t. Qed.
481
482 Lemma fold_1 :
483 forall (A : Type) (i : A) (f : key -> elt -> A -> A),
484 fold f m i = fold_left (fun a p => f (fst p) (snd p) a) (elements m) i.
485 Proof using Type. clear; spec_t. Qed.
486
487 Variable cmp : elt -> elt -> bool.
488
489 Lemma equal_1 : Equivb cmp m m' -> equal cmp m m' = true.
490 Proof using Type. clear; spec_t. Qed.
491 Lemma equal_2 : equal cmp m m' = true -> Equivb cmp m m'.
492 Proof using Type. clear; spec_t. Qed.
493 End Spec.
494
495 Lemma map_1 : forall (elt elt':Type)(m: t elt)(x:key)(e:elt)(f:elt->elt'),
496 MapsTo x e m -> MapsTo x (f e) (map f m).
497 Proof. clear; spec_t. Qed.
498 Lemma map_2 : forall (elt elt':Type)(m: t elt)(x:key)(f:elt->elt'),
499 In x (map f m) -> In x m.
500 Proof. clear; spec_t. Qed.
501 Lemma mapi_1 (elt elt':Type)(m: t elt)(x:key)(e:elt)
502 (f:key->elt->elt')
503 : MapsTo x e m ->
504 exists y, E.eq y x /\ MapsTo x (f y e) (mapi f m).
505 Proof.
506 pose proof (fun x => @M1.mapi_1 elt elt' (fst m) x e (fun x => f None)).
507 pose proof (fun x => @M2.mapi_1 elt elt' (snd m) x e (fun x => f (Some x))).
508 spec_t.
509 all: eexists.
510 all: split; [ | eassumption ].
511 all: spec_t.
512 Qed.
513 Lemma mapi_2
514 : forall (elt elt':Type)(m: t elt)(x:key)
515 (f:key->elt->elt'), In x (mapi f m) -> In x m.
516 Proof. clear; spec_t. Qed.
517 Lemma map2_1
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 m \/ In x m' ->
521 find x (map2 f m m') = f (find x m) (find x m').
522 Proof. clear; spec_t. Qed.
523 Lemma map2_2
524 : forall (elt elt' elt'':Type)(m: t elt)(m': t elt')
525 (x:key)(f:option elt->option elt'->option elt''),
526 In x (map2 f m m') -> In x m \/ In x m'.
527 Proof. clear; spec_t. Qed.
528 End OptionWSfun_gen.
529 End OptionWSfun_gen.
530
531 Module OptionWSfun (E2 : DecidableTypeOrig) (M2 : WSfun E2).
532 Module _OptionWSfun.
533 Module Outer := OptionWSfun_gen E2 M2.
534 Module E := OptionDecidableTypeOrig E2.
535 Module ECompat <: Outer.ESigCompat E.
536 Lemma eq_alt : forall x y, E.eq x y <-> option_eq E2.eq x y.
537 Proof. cbv [E.eq]; reflexivity. Qed.
538 End ECompat.
539 Module Inner := Outer.OptionWSfun_gen E ECompat.
540 End _OptionWSfun.
541 Include _OptionWSfun.Inner.
542 End OptionWSfun.
543
544 Module OptionUsualWeakMap (M2 : UsualWS) <: UsualWS.
545 Module Outer := OptionWSfun_gen M2.E M2.
546 Module E := OptionUsualDecidableTypeOrig M2.E.
547 Module ECompat <: Outer.ESigCompat E.
548 Lemma eq_alt : forall x y, E.eq x y <-> option_eq M2.E.eq x y.
549 Proof. cbv [E.eq option_eq M2.E.eq]; intros; break_innermost_match; intuition congruence. Qed.
550 End ECompat.
551 Module Inner := Outer.OptionWSfun_gen E ECompat.
552 Include Inner.
553 End OptionUsualWeakMap.
554
555 Module OptionWeakMap (M2 : WS) <: WS.
556 Include OptionWSfun M2.E M2.
557 Module E := _OptionWSfun.E.
558 End OptionWeakMap.
559
560 Module OptionSfun_gen (E2 : OrderedTypeOrig) (M2 : Sfun E2).
561 Module OptionWSfun := OptionWSfun_gen E2 M2.
562 Module O := OptionHasLt E2.
563 Module E1 <: OrderedTypeOrig := UnitOrderedTypeOrig.
564 Module M1 <: Sfun E1 := UnitMap.
565 Module Type ESig := OptionWSfun.ESig <+ HasLt <+ HasMiniOrderedType.
566 Module Type ESigCompat (E : ESig).
567 Include OptionWSfun.ESigCompat E.
568 Axiom lt_alt : forall x y, E.lt x y <-> O.lt x y.
569 End ESigCompat.
570 Module OptionSfun_gen (E : ESig) (ECompat : ESigCompat E) <: Sfun E.
571 Include OptionWSfun.OptionWSfun_gen E ECompat.
572 Local Existing Instances _Extra2.eq_key_equiv _Extra2.eq_key_elt_equiv.
573 Section elt.
574 Variable elt:Type.
575 Definition lt_key (p p':key*elt) := E.lt (fst p) (fst p').
576
577 Lemma elements_3 m : sort lt_key (elements m).
578 Proof using Type.
579 pose proof (M1.elements_3 (fst m)).
580 pose proof (M2.elements_3 (snd m)).
581 cbv [elements lt_key M1.lt_key M2.lt_key] in *.
582 setoid_rewrite ECompat.lt_alt; cbv [O.lt].
583 eapply SortA_app with (eqA:=@eq_key _); try exact _;
584 intros *;
585 rewrite ?Sorted_map_iff, ?InA_alt;
586 cbn [fst snd]; cbv [eq_key]; try assumption.
587 setoid_rewrite in_map_iff.
588 setoid_rewrite ECompat.eq_alt.
589 cbv [option_eq].
590 repeat first [ progress intros
591 | progress subst
592 | progress cbn [fst snd] in *
593 | progress destruct_head'_and
594 | progress destruct_head'_ex
595 | progress destruct_head'_prod
596 | progress inversion_option
597 | progress break_innermost_match
598 | exact I
599 | exfalso; assumption ].
600 Qed.
601 End elt.
602 End OptionSfun_gen.
603 End OptionSfun_gen.
604
605 Module OptionSfun (E2 : OrderedTypeOrig) (M2 : Sfun E2).
606 Module _OptionSfun.
607 Module Outer := OptionSfun_gen E2 M2.
608 Module E := OptionOrderedTypeOrig E2.
609 Module ECompat <: Outer.ESigCompat E.
610 Lemma eq_alt : forall x y, E.eq x y <-> option_eq E2.eq x y.
611 Proof. cbv [E.eq]; reflexivity. Qed.
612 Lemma lt_alt : forall x y, E.lt x y <-> E.lt x y.
613 Proof. cbv [E.lt]; reflexivity. Qed.
614 End ECompat.
615 Module Inner := Outer.OptionSfun_gen E ECompat.
616 End _OptionSfun.
617 Include _OptionSfun.Inner.
618 End OptionSfun.
619
620 Module OptionUsualMap (M2 : UsualS) <: UsualS.
621 Module Outer := OptionSfun_gen M2.E M2.
622 Module E := OptionUsualOrderedTypeOrig M2.E.
623 Module ECompat <: Outer.ESigCompat E.
624 Lemma eq_alt : forall x y, E.eq x y <-> option_eq M2.E.eq x y.
625 Proof. cbv [E.eq option_eq M2.E.eq]; intros; break_innermost_match; intuition congruence. Qed.
626 Lemma lt_alt : forall x y, E.lt x y <-> E.lt x y.
627 Proof. cbv [E.lt]; reflexivity. Qed.
628 End ECompat.
629 Module Inner := Outer.OptionSfun_gen E ECompat.
630 Include Inner.
631 End OptionUsualMap.
632
633 Module OptionMap (M2 : S) <: S.
634 Include OptionSfun M2.E M2.
635 Module E := _OptionSfun.E.
636 End OptionMap.
637
638 (* TODO:
639 Module IsoSord (S' : Sord) (Data' : IsoMiniOrderedType S'.Data) (E : IsoMiniOrderedType S'.MapS.E) (S'_iso : IsoMiniOrderedType S') <: Sord <: IsoOrderedType S'.
640 Module Data <: IsoOrderedType S'.Data := Data' <+ OT_of_MOT.
641 Module MapS <: S := IsoS S'.MapS E.
642 Import MapS.
643
644 Definition t := MapS.t Data.t.
645
646 Definition eq : t -> t -> Prop.
647 refine (_ S'.eq).
648 compute.
649 pose MapS.E.
650 Parameter lt : t -> t -> Prop.
651
652 Axiom eq_refl : forall m : t, eq m m.
653 Axiom eq_sym : forall m1 m2 : t, eq m1 m2 -> eq m2 m1.
654 Axiom eq_trans : forall m1 m2 m3 : t, eq m1 m2 -> eq m2 m3 -> eq m1 m3.
655 Axiom lt_trans : forall m1 m2 m3 : t, lt m1 m2 -> lt m2 m3 -> lt m1 m3.
656 Axiom lt_not_eq : forall m1 m2 : t, lt m1 m2 -> ~ eq m1 m2.
657
658 Definition cmp e e' := match Data.compare e e' with EQ _ => true | _ => false end.
659
660 Parameter eq_1 : forall m m', Equivb cmp m m' -> eq m m'.
661 Parameter eq_2 : forall m m', eq m m' -> Equivb cmp m m'.
662
663 Parameter compare : forall m1 m2, Compare lt eq m1 m2.
664 (** Total ordering between maps. [Data.compare] is a total ordering
665 used to compare data associated with equal keys in the two maps. *)
666
667
668 Include S.
669 Include OT_of_MOT.
670 End IsoSord.
671 Definition t := MapS.t Data.t.
672
673 Print IsoMiniOrderedType.
674 Definition eq : t -> t -> Prop := S'.eq.
675 Parameter lt : t -> t -> Prop.
676
677 Axiom eq_refl : forall m : t, eq m m.
678 Axiom eq_sym : forall m1 m2 : t, eq m1 m2 -> eq m2 m1.
679 Axiom eq_trans : forall m1 m2 m3 : t, eq m1 m2 -> eq m2 m3 -> eq m1 m3.
680 Axiom lt_trans : forall m1 m2 m3 : t, lt m1 m2 -> lt m2 m3 -> lt m1 m3.
681 Axiom lt_not_eq : forall m1 m2 : t, lt m1 m2 -> ~ eq m1 m2.
682
683 Definition cmp e e' := match Data.compare e e' with EQ _ => true | _ => false end.
684
685 Parameter eq_1 : forall m m', Equivb cmp m m' -> eq m m'.
686 Parameter eq_2 : forall m m', eq m m' -> Equivb cmp m m'.
687
688 Parameter compare : forall m1 m2, Compare lt eq m1 m2.
689 (** Total ordering between maps. [Data.compare] is a total ordering
690 used to compare data associated with equal keys in the two maps. *)
691
692 End Sord.
693
694 *)