Debian Codegraph @

src/Util/FSets/FMapTrie.v

raw
1 (* Finite map library. *)
2
3 (** * FMapTrie *)
4
5 (** This module implements tries.
6 It follows the implementation from Coq's clib, to some extent.
7 *)
8 From Coq Require Import Bool.
9 From Coq Require Import List.
10 From Coq Require Import Orders.
11 From Coq Require Import OrdersEx.
12 From Coq Require Import FMapInterface.
13 From Coq Require Import FMapFacts.
14 From Coq Require Import RelationPairs.
15 Require Import Crypto.Util.ListUtil.
16 Require Import Crypto.Util.ListUtil.SetoidList.
17 Require Import Crypto.Util.ListUtil.SetoidListFlatMap.
18 Require Import Crypto.Util.ListUtil.StdlibCompat.
19 Require Import Crypto.Util.Compose.
20 Require Import Crypto.Util.Logic.Forall.
21 Require Import Crypto.Util.Logic.Exists.
22 Require Import Crypto.Util.Prod.
23 Require Import Crypto.Util.Option.
24 Require Import Crypto.Util.Relations.
25 Require Import Crypto.Util.Structures.Equalities.
26 Require Import Crypto.Util.Structures.Equalities.List.
27 Require Import Crypto.Util.Structures.Orders.
28 Require Import Crypto.Util.Structures.Orders.List.
29 Require Import Crypto.Util.FSets.FMapInterface.
30 Require Import Crypto.Util.FSets.FMapFacts.
31 Require Import Crypto.Util.Sorting.Sorted.Proper.
32 Require Import Crypto.Util.Tactics.SplitInContext.
33 Require Import Crypto.Util.Tactics.DestructHead.
34 Require Import Crypto.Util.Tactics.SetEvars.
35 Require Import Crypto.Util.Tactics.SubstEvars.
36 Require Import Crypto.Util.Tactics.SpecializeAllWays.
37 Require Import Crypto.Util.Tactics.SpecializeBy.
38 Require Import Crypto.Util.Tactics.SpecializeUnderBindersBy.
39 Require Import Crypto.Util.Tactics.InHypUnderBindersDo.
40 Require Import Crypto.Util.Tactics.Head.
41 Require Import Crypto.Util.Tactics.BreakMatch.
42 Require Import Crypto.Util.Tactics.SetoidSubst.
43 Require Import Crypto.Util.FSets.FMapTrie.Shape.
44 Import TrieTactics.
45 Import EverythingGen.
46
47 Local Set Implicit Arguments.
48 Local Set Primitive Projections.
49 Local Unset Strict Implicit.
50 (* TODO: move to global settings *)
51 Local Set Keyed Unification.
52 Import ListNotations.
53 Local Open Scope option_scope.
54 Local Open Scope bool_scope.
55 Local Open Scope lazy_bool_scope.
56 Local Open Scope type_scope.
57 Local Open Scope list_scope.
58
59 Module Import ListWSfun_gen_proofs.
60 Section __.
61 Class args :=
62 { T_t : Type -> Type
63 ; Y_t : Type
64 ; M_t : Type -> Type
65 ; E_t := list Y_t
66 ; Y_eq : relation Y_t
67 ; E_eq : relation E_t
68 ; Y_eq_refl : Reflexive Y_eq
69 ; Y_eq_sym : Symmetric Y_eq
70 ; Y_eq_trans : Transitive Y_eq
71 ; Y_eq_dec : forall x y, {Y_eq x y} + {~Y_eq x y}
72 ; E_eq_refl : Reflexive E_eq
73 ; E_eq_sym : Symmetric E_eq
74 ; E_eq_trans : Transitive E_eq
75 ; E_eq_dec : forall x y, {E_eq x y} + {~E_eq x y}
76 ; E'_eq_alt : forall x y, E_eq x y <-> eqlistA Y_eq x y
77 ; M_empty : forall elt, M_t elt
78 ; M_is_empty : forall elt, M_t elt -> bool
79 ; M_add : forall elt, Y_t -> elt -> M_t elt -> M_t elt
80 ; M_find : forall elt : Type, Y_t -> M_t elt -> option elt
81 ; M_remove : forall elt, Y_t -> M_t elt -> M_t elt
82 ; M_mem : forall elt, Y_t -> M_t elt -> bool
83 ; M_map : forall elt elt', (elt -> elt') -> M_t elt -> M_t elt'
84 ; M_mapi : forall elt elt' : Type, (Y_t -> elt -> elt') -> M_t elt -> M_t elt'
85 ; M_map2 : forall elt elt' elt'' : Type, (option elt -> option elt' -> option elt'') -> M_t elt -> M_t elt' -> M_t elt''
86 ; M_elements : forall elt, M_t elt -> list (Y_t*elt)
87 ; M_cardinal : forall elt, M_t elt -> nat
88 ; M_fold : forall elt A : Type, (Y_t -> elt -> A -> A) -> M_t elt -> A -> A
89 ; M_equal : forall elt, (elt -> elt -> bool) -> M_t elt -> M_t elt -> bool
90 ; M_MapsTo : forall elt, Y_t -> elt -> M_t elt -> Prop
91 ; M_In := fun elt (k:Y_t)(m: M_t elt)=> exists e:elt, M_MapsTo k e m
92 ; M_Empty elt m := forall (a : Y_t)(e:elt) , ~ M_MapsTo a e m
93 ; M_eq_key elt := fun (p p':Y_t*elt) => Y_eq (fst p) (fst p')
94 ; M_eq_key_elt elt := fun (p p':Y_t*elt) => Y_eq (fst p) (fst p') /\ (snd p) = (snd p')
95 ; M_MapsTo_1 : forall elt m x y (e : elt), Y_eq x y -> M_MapsTo x e m -> M_MapsTo y e m
96 ; M_mem_1 : forall elt (m : M_t elt) x, M_In x m -> M_mem x m = true
97 ; M_mem_2 : forall elt (m : M_t elt) x, M_mem x m = true -> M_In x m
98 ; M_empty_1 : forall elt, M_Empty (@M_empty elt)
99 ; M_is_empty_1 : forall elt (m : M_t elt), M_Empty m -> M_is_empty m = true
100 ; M_is_empty_2 : forall elt (m : M_t elt), M_is_empty m = true -> M_Empty m
101 ; M_add_1 : forall elt (m : M_t elt) x y (e : elt), Y_eq x y -> M_MapsTo y e (M_add x e m)
102 ; M_add_2 : forall elt (m : M_t elt) x y (e e' : elt), ~ Y_eq x y -> M_MapsTo y e m -> M_MapsTo y e (M_add x e' m)
103 ; M_add_3 : forall elt (m : M_t elt) x y (e e' : elt), ~ Y_eq x y -> M_MapsTo y e (M_add x e' m) -> M_MapsTo y e m
104 ; M_remove_1 : forall elt (m : M_t elt) x y, Y_eq x y -> ~ M_In y (M_remove x m)
105 ; M_remove_2 : forall elt (m : M_t elt) x y (e : elt), ~ Y_eq x y -> M_MapsTo y e m -> M_MapsTo y e (M_remove x m)
106 ; M_remove_3 : forall elt (m : M_t elt) x y (e : elt), M_MapsTo y e (M_remove x m) -> M_MapsTo y e m
107 ; M_find_1 : forall elt (m : M_t elt) x (e : elt), M_MapsTo x e m -> M_find x m = Some e
108 ; M_find_2 : forall elt (m : M_t elt) x (e : elt), M_find x m = Some e -> M_MapsTo x e m
109 ; M_elements_1 :
110 forall elt (m : M_t elt) x (e : elt), M_MapsTo x e m -> InA (@M_eq_key_elt _) (x,e) (M_elements m)
111 ; M_elements_2 :
112 forall elt (m : M_t elt) x (e : elt), InA (@M_eq_key_elt _) (x,e) (M_elements m) -> M_MapsTo x e m
113 ; M_elements_3w : forall elt m, NoDupA (@M_eq_key elt) (M_elements m)
114 ; M_cardinal_1 : forall elt (m : M_t elt), M_cardinal m = length (M_elements m)
115 ; M_fold_1 :
116 forall elt (m : M_t elt) (A : Type) (i : A) (f : Y_t -> elt -> A -> A),
117 M_fold f m i = fold_left (fun a p => f (fst p) (snd p) a) (M_elements m) i
118 ; M_Equal elt m m' := forall y, @M_find elt y m = @M_find elt y m'
119 ; M_Equiv elt := fun (eq_elt:elt->elt->Prop) m m' =>
120 (forall k, M_In k m <-> M_In k m') /\
121 (forall k e e', M_MapsTo k e m -> M_MapsTo k e' m' -> eq_elt e e')
122 ; M_Equivb elt := fun (cmp: elt->elt->bool) => M_Equiv (Cmp cmp)
123 ; M_equal_1 : forall elt (m m' : M_t elt) cmp, M_Equivb cmp m m' -> M_equal cmp m m' = true
124 ; M_equal_2 : forall elt (m m' : M_t elt) cmp, M_equal cmp m m' = true -> M_Equivb cmp m m'
125 ; M_map_1 : forall (elt elt':Type)(m: M_t elt)(x:Y_t)(e:elt)(f:elt->elt'),
126 M_MapsTo x e m -> M_MapsTo x (f e) (M_map f m)
127 ; M_map_2 : forall (elt elt':Type)(m: M_t elt)(x:Y_t)(f:elt->elt'),
128 M_In x (M_map f m) -> M_In x m
129 ; M_mapi_1 : forall (elt elt':Type)(m: M_t elt)(x:Y_t)(e:elt)
130 (f:Y_t->elt->elt'), M_MapsTo x e m ->
131 exists y, Y_eq y x /\ M_MapsTo x (f y e) (M_mapi f m)
132 ; M_mapi_2 : forall (elt elt':Type)(m: M_t elt)(x:Y_t)
133 (f:Y_t->elt->elt'), M_In x (M_mapi f m) -> M_In x m
134 ; M_map2_1 : forall (elt elt' elt'':Type)(m: M_t elt)(m': M_t elt')
135 (x:Y_t)(f:option elt->option elt'->option elt''),
136 M_In x m \/ M_In x m' ->
137 M_find x (M_map2 f m m') = f (M_find x m) (M_find x m')
138 ; M_map2_2 : forall (elt elt' elt'':Type)(m: M_t elt)(m': M_t elt')
139 (x:Y_t)(f:option elt->option elt'->option elt''),
140 M_In x (M_map2 f m m') -> M_In x m \/ M_In x m'
141 ; M'_find_mapsto_iff : forall elt (m : M_t elt) x e, M_MapsTo x e m <-> M_find x m = Some e
142 ; M'_add_o : forall elt (m : M_t elt) x y e, M_find y (M_add x e m) = if Y_eq_dec x y then Some e else M_find y m
143 ; M'_remove_o : forall elt (m : M_t elt) x y,
144 M_find y (M_remove x m) = if Y_eq_dec x y then None else M_find y m
145 ; M'_mapi_o_ex : forall (elt elt':Type)(m: M_t elt)(x:Y_t)(f:Y_t->elt->elt'), exists y, Y_eq y x /\ M_find x (M_mapi f m) = option_map (f y) (M_find x m)
146 ; M'_mapi_o_ex_impl : forall (elt elt':Type)(m: M_t elt)(f:Y_t->elt->elt')(P:_->Prop),
147 (forall x e, M_find x (M_mapi f m) = Some e -> P e)
148 -> (forall x, exists y, Y_eq y x /\ forall e, option_map (f y) (M_find x m) = Some e -> P e)
149 ; M'_map2_o : forall (elt elt' elt'':Type)(m: M_t elt)(m': M_t elt')
150 (x:Y_t)(f:option elt->option elt'->option elt''),
151 M_find x (M_map2 f m m') = match M_find x m, M_find x m' with
152 | None, None => None
153 | x, y => f x y
154 end
155 ; M'_is_empty_iff : forall elt (m : M_t elt), M_Empty m <-> M_is_empty m = true
156 ; M'_find_empty : forall elt x, M_find x (@M_empty elt) = None
157 ; M'_forall_In_elements_snd_iff : forall elt (P : _ -> Prop) (m : M_t elt),
158 (forall i, List.In i (M_elements m) -> P (snd i))
159 <-> (forall k v, M_find k m = Some v -> P v)
160 ; M'_map_o : forall elt elt' (m : M_t elt) x (f:elt->elt'),
161 M_find x (M_map f m) = Datatypes.option_map f (M_find x m)
162 ; M'_empty_1' elt : forall x y z, False := @M_empty_1 elt
163 ; M'_Proper_eq_key_elt_iff : forall elt, Proper (eq ==> RelationPairs.RelProd Y_eq eq ==> iff) (@M_eq_key_elt elt)
164 ; M'_Proper_eq_key_elt_impl : forall elt, Proper (eq ==> RelationPairs.RelProd Y_eq eq ==> impl) (@M_eq_key_elt elt)
165 ; M'_Proper_eq_key_elt_flip_impl : forall elt, Proper (eq ==> RelationPairs.RelProd Y_eq eq ==> flip impl) (@M_eq_key_elt elt)
166 ; M'_Proper_eq_key_elt_iff' : forall elt, Proper (@M_eq_key_elt elt ==> @M_eq_key_elt elt ==> iff) (@M_eq_key_elt elt)
167 ; M'_Proper_eq_key_elt_impl' : forall elt, Proper (@M_eq_key_elt elt ==> @M_eq_key_elt elt ==> impl) (@M_eq_key_elt elt)
168 ; M'_Proper_eq_key_elt_flip_impl' : forall elt, Proper (@M_eq_key_elt elt ==> @M_eq_key_elt elt ==> flip impl) (@M_eq_key_elt elt)
169 ; M'_Proper_eq_key_iff : forall elt, Proper (@M_eq_key elt ==> @M_eq_key elt ==> iff) (@M_eq_key elt)
170 ; M'_Proper_eq_key_impl : forall elt, Proper (@M_eq_key elt ==> @M_eq_key elt ==> impl) (@M_eq_key elt)
171 ; M'_Proper_eq_key_flip_impl : forall elt, Proper (@M_eq_key elt ==> @M_eq_key elt ==> flip impl) (@M_eq_key elt)
172 ; M'_Equal_Equivalence : forall elt, Equivalence (@M_Equal elt)
173 ; M'_find_Proper_eq : forall elt, Proper (Y_eq ==> @M_Equal _ ==> eq) (@M_find elt)
174 ; M'_MapsTo_Proper_eq_iff : forall elt, Proper (Y_eq ==> eq ==> @M_Equal _ ==> iff) (@M_MapsTo elt)
175 ; M'_In_compat : forall elt, Proper (Y_eq ==> @M_Equal _ ==> iff) (@M_In elt)
176 ; M'_eq_key_equiv : forall elt, Equivalence (@M_eq_key elt)
177 ; M'_eq_key_elt_equiv : forall elt, Equivalence (@M_eq_key_elt elt)
178 ; M'_Proper_Equiv_eq_impl : forall elt, Proper ((eq ==> eq ==> impl) ==> eq ==> eq ==> impl) (@M_Equiv elt)
179 ; M'_Proper_Equiv_eq_flip_impl : forall elt, Proper ((eq ==> eq ==> flip impl) ==> eq ==> eq ==> flip impl) (@M_Equiv elt)
180 ; M'_Proper_Equiv_eq_iff : forall elt, Proper ((eq ==> eq ==> iff) ==> eq ==> eq ==> iff) (@M_Equiv elt)
181 ; M'_Proper_Equiv_eq_impl_pointwise : forall elt, Proper (pointwise_relation _ (pointwise_relation _ impl) ==> eq ==> eq ==> impl) (@M_Equiv elt)
182 ; M'_Proper_Equiv_eq_flip_impl_pointwise : forall elt, Proper (pointwise_relation _ (pointwise_relation _ (flip impl)) ==> eq ==> eq ==> flip impl) (@M_Equiv elt)
183 ; M'_Proper_Equiv_eq_iff_pointwise : forall elt, Proper (pointwise_relation _ (pointwise_relation _ iff) ==> eq ==> eq ==> iff) (@M_Equiv elt)
184 ; everything : @EverythingGen T_t Y_t M_t M_is_empty M_fold M_mapi M_map2 M_find
185 }.
186 Local Notation "'T.t'" := T_t.
187 Local Notation "'Y.t'" := Y_t.
188 Local Notation "'Y.eq'" := Y_eq.
189 Local Notation "'Y.eq_refl'" := Y_eq_refl.
190 Local Notation "'Y.eq_sym'" := Y_eq_sym.
191 Local Notation "'Y.eq_trans'" := Y_eq_trans.
192 Local Notation "'Y.eq_dec'" := Y_eq_dec.
193 Local Notation "'E.t'" := E_t.
194 Local Notation "'E.eq'" := E_eq.
195 Local Notation "'E.eq_refl'" := E_eq_refl.
196 Local Notation "'E.eq_sym'" := E_eq_sym.
197 Local Notation "'E.eq_trans'" := E_eq_trans.
198 Local Notation "'E.eq_dec'" := E_eq_dec.
199 Local Notation "'E'.eq_alt'" := E'_eq_alt.
200 Local Notation "'M.t'" := M_t.
201 Local Notation "'M.empty'" := M_empty.
202 Local Notation "'M.is_empty'" := M_is_empty.
203 Local Notation "'M.add'" := M_add.
204 Local Notation "'M.find'" := M_find.
205 Local Notation "'M.remove'" := M_remove.
206 Local Notation "'M.mem'" := M_mem.
207 Local Notation "'M.map'" := M_map.
208 Local Notation "'M.mapi'" := M_mapi.
209 Local Notation "'M.map2'" := M_map2.
210 Local Notation "'M.elements'" := M_elements.
211 Local Notation "'M.cardinal'" := M_cardinal.
212 Local Notation "'M.fold'" := M_fold.
213 Local Notation "'M.equal'" := M_equal.
214 Local Notation "'M.MapsTo'" := M_MapsTo.
215 Local Notation "'M.In'" := M_In.
216 Local Notation "'M.Empty'" := M_Empty.
217 Local Notation "'M.eq_key'" := M_eq_key.
218 Local Notation "'M.eq_key_elt'" := M_eq_key_elt.
219 Local Notation "'M.MapsTo_1'" := M_MapsTo_1.
220 Local Notation "'M.mem_1'" := M_mem_1.
221 Local Notation "'M.mem_2'" := M_mem_2.
222 Local Notation "'M.empty_1'" := M_empty_1.
223 Local Notation "'M.is_empty_1'" := M_is_empty_1.
224 Local Notation "'M.is_empty_2'" := M_is_empty_2.
225 Local Notation "'M.add_1'" := M_add_1.
226 Local Notation "'M.add_2'" := M_add_2.
227 Local Notation "'M.add_3'" := M_add_3.
228 Local Notation "'M.remove_1'" := M_remove_1.
229 Local Notation "'M.remove_2'" := M_remove_2.
230 Local Notation "'M.remove_3'" := M_remove_3.
231 Local Notation "'M.find_1'" := M_find_1.
232 Local Notation "'M.find_2'" := M_find_2.
233 Local Notation "'M.elements_1'" := M_elements_1.
234 Local Notation "'M.elements_2'" := M_elements_2.
235 Local Notation "'M.elements_3w'" := M_elements_3w.
236 Local Notation "'M.cardinal_1'" := M_cardinal_1.
237 Local Notation "'M.fold_1'" := M_fold_1.
238 Local Notation "'M.Equal'" := M_Equal.
239 Local Notation "'M.Equiv'" := M_Equiv.
240 Local Notation "'M.Equivb'" := M_Equivb.
241 Local Notation "'M.equal_1'" := M_equal_1.
242 Local Notation "'M.equal_2'" := M_equal_2.
243 Local Notation "'M.map_1'" := M_map_1.
244 Local Notation "'M.map_2'" := M_map_2.
245 Local Notation "'M.mapi_1'" := M_mapi_1.
246 Local Notation "'M.mapi_2'" := M_mapi_2.
247 Local Notation "'M.map2_1'" := M_map2_1.
248 Local Notation "'M.map2_2'" := M_map2_2.
249 Local Notation "'M'.find_mapsto_iff'" := M'_find_mapsto_iff.
250 Local Notation "'M'.add_o'" := M'_add_o.
251 Local Notation "'M'.remove_o'" := M'_remove_o.
252 Local Notation "'M'.mapi_o_ex'" := M'_mapi_o_ex.
253 Local Notation "'M'.mapi_o_ex_impl'" := M'_mapi_o_ex_impl.
254 Local Notation "'M'.map2_o'" := M'_map2_o.
255 Local Notation "'M'.is_empty_iff'" := M'_is_empty_iff.
256 Local Notation "'M'.find_empty'" := M'_find_empty.
257 Local Notation "'M'.forall_In_elements_snd_iff'" := M'_forall_In_elements_snd_iff.
258 Local Notation "'M'.map_o'" := M'_map_o.
259 Local Notation "'M'.empty_1''" := M'_empty_1'.
260 Context {args : args}.
261 Local Definition T_t' := T_t.
262 Local Definition Y_t' := Y_t.
263 Local Definition Y_eq' := Y_eq.
264 Local Definition Y_eq_refl' := Y_eq_refl.
265 Local Definition Y_eq_sym' := Y_eq_sym.
266 Local Definition Y_eq_trans' := Y_eq_trans.
267 Local Definition Y_eq_dec' := Y_eq_dec.
268 Local Definition E_t' := E_t.
269 Local Definition E_eq' := E_eq.
270 Local Definition E_eq_refl' := E_eq_refl.
271 Local Definition E_eq_sym' := E_eq_sym.
272 Local Definition E_eq_trans' := E_eq_trans.
273 Local Definition E_eq_dec' := E_eq_dec.
274 Local Definition E'_eq_alt' := E'_eq_alt.
275 Local Definition M_t' := M_t.
276 Local Definition M_empty' := M_empty.
277 Local Definition M_is_empty' := M_is_empty.
278 Local Definition M_add' := M_add.
279 Local Definition M_find' := M_find.
280 Local Definition M_remove' := M_remove.
281 Local Definition M_mem' := M_mem.
282 Local Definition M_map' := M_map.
283 Local Definition M_mapi' := M_mapi.
284 Local Definition M_map2' := M_map2.
285 Local Definition M_elements' := M_elements.
286 Local Definition M_cardinal' := M_cardinal.
287 Local Definition M_fold' := M_fold.
288 Local Definition M_equal' := M_equal.
289 Local Definition M_MapsTo' := M_MapsTo.
290 Local Definition M_In' := M_In.
291 Local Definition M_Empty' := M_Empty.
292 Local Definition M_eq_key' := M_eq_key.
293 Local Definition M_eq_key_elt' := M_eq_key_elt.
294 Local Definition M_MapsTo_1' := M_MapsTo_1.
295 Local Definition M_mem_1' := M_mem_1.
296 Local Definition M_mem_2' := M_mem_2.
297 Local Definition M_empty_1' := M_empty_1.
298 Local Definition M_is_empty_1' := M_is_empty_1.
299 Local Definition M_is_empty_2' := M_is_empty_2.
300 Local Definition M_add_1' := M_add_1.
301 Local Definition M_add_2' := M_add_2.
302 Local Definition M_add_3' := M_add_3.
303 Local Definition M_remove_1' := M_remove_1.
304 Local Definition M_remove_2' := M_remove_2.
305 Local Definition M_remove_3' := M_remove_3.
306 Local Definition M_find_1' := M_find_1.
307 Local Definition M_find_2' := M_find_2.
308 Local Definition M_elements_1' := M_elements_1.
309 Local Definition M_elements_2' := M_elements_2.
310 Local Definition M_elements_3w' := M_elements_3w.
311 Local Definition M_cardinal_1' := M_cardinal_1.
312 Local Definition M_fold_1' := M_fold_1.
313 Local Definition M_Equal' := M_Equal.
314 Local Definition M_Equiv' := M_Equiv.
315 Local Definition M_Equivb' := M_Equivb.
316 Local Definition M_equal_1' := M_equal_1.
317 Local Definition M_equal_2' := M_equal_2.
318 Local Definition M_map_1' := M_map_1.
319 Local Definition M_map_2' := M_map_2.
320 Local Definition M_mapi_1' := M_mapi_1.
321 Local Definition M_mapi_2' := M_mapi_2.
322 Local Definition M_map2_1' := M_map2_1.
323 Local Definition M_map2_2' := M_map2_2.
324 Local Definition M'_find_mapsto_iff' := M'_find_mapsto_iff.
325 Local Definition M'_add_o' := M'_add_o.
326 Local Definition M'_remove_o' := M'_remove_o.
327 Local Definition M'_mapi_o_ex' := M'_mapi_o_ex.
328 Local Definition M'_mapi_o_ex_impl' := M'_mapi_o_ex_impl.
329 Local Definition M'_map2_o' := M'_map2_o.
330 Local Definition M'_is_empty_iff' := M'_is_empty_iff.
331 Local Definition M'_find_empty' := M'_find_empty.
332 Local Definition M'_forall_In_elements_snd_iff' := M'_forall_In_elements_snd_iff.
333 Local Definition M'_map_o' := M'_map_o.
334 Local Definition M'_empty_1'' := M'_empty_1'.
335 Local Existing Instances everything Y_eq_refl Y_eq_sym Y_eq_trans E_eq_refl E_eq_sym E_eq_trans.
336 Local Existing Instances
337 M'_Proper_eq_key_elt_iff
338 M'_Proper_eq_key_elt_impl
339 M'_Proper_eq_key_elt_flip_impl
340 M'_Proper_eq_key_elt_iff'
341 M'_Proper_eq_key_elt_impl'
342 M'_Proper_eq_key_elt_flip_impl'
343 M'_Proper_eq_key_iff
344 M'_Proper_eq_key_impl
345 M'_Proper_eq_key_flip_impl
346 M'_Equal_Equivalence
347 M'_find_Proper_eq
348 M'_MapsTo_Proper_eq_iff
349 M'_In_compat
350 M'_eq_key_equiv
351 M'_eq_key_elt_equiv
352 M'_Proper_Equiv_eq_impl
353 M'_Proper_Equiv_eq_flip_impl
354 M'_Proper_Equiv_eq_iff
355 M'_Proper_Equiv_eq_impl_pointwise
356 M'_Proper_Equiv_eq_flip_impl_pointwise
357 M'_Proper_Equiv_eq_iff_pointwise
358 | 10.
359 Local Hint Resolve Y_eq_refl' E_eq_refl' : core.
360 Local Hint Immediate Y_eq_sym' E_eq_sym' : core.
361 Local Hint Extern 1 (ProperProxy (@M_Equal _ _) _) => apply reflexive_proper_proxy : typeclass_instances.
362 Local Instance Y_eq_equiv : Equivalence Y.eq. Proof. split; exact _. Defined.
363 Local Instance E_eq_equiv : Equivalence E.eq. Proof. split; exact _. Defined.
364 Local Notation t' elt := (option (T.t elt)).
365 Local Notation t'_P m := (True -> match m with None => True | Some m' => recursively_non_empty m' = true end) (only parsing).
366 Local Notation mk m pf := (exist (fun m' => t'_P m') m (fun 'I => pf)).
367 Definition t elt := { m : t' elt | t'_P m }.
368 Local Notation t_ind := (t_ind (EverythingGen:=@everything args)).
369 Definition key := list Y.t.
370
371 Local Ltac t_obgl :=
372 repeat first [ progress intros
373 | progress cbv [M_Empty Option.value not option_map] in *
374 | reflexivity
375 | congruence
376 | t_destr_step
377 | match goal with
378 | [ H := _ |- _ ] => subst H
379 | [ H : forall a e, M.MapsTo _ _ (M.add _ _ _) -> False |- _ ]
380 => specialize (H _ _ ltac:(eapply M.add_1; reflexivity))
381 | [ H : context[M.is_empty] |- _ ]
382 => rewrite M.is_empty_1 in H by assumption
383 | [ H : forall a e, M.MapsTo a e (M.mapi ?f ?m) -> False |- _ ]
384 => assert (forall a e, M.MapsTo a e m -> False)
385 by (let H' := fresh in
386 intros ? ? H'; eapply M.mapi_1 in H'; destruct H' as [?[? H']];
387 eapply H, H');
388 clear H
389 | [ H : forall k e, _ = _ -> M.In _ _ \/ M.In _ _ -> False |- _ ]
390 => pose proof (fun k e pf pf' => H k e (pf pf') (or_introl pf'));
391 pose proof (fun k e pf pf' => H k e (pf pf') (or_intror pf'));
392 clear H
393 end
394 | progress subst
395 | progress specialize_under_binders_by eapply M.map_1
396 | progress break_innermost_match_hyps
397 | progress specialize_under_binders_by (match goal with |- M.MapsTo _ _ (M.map2 _ _ _) => idtac end;
398 eapply M.find_2;
399 rewrite M.map2_1) ].
400
401 Local Ltac pose_MapsTo_as_find lem :=
402 let H := fresh in
403 first [ pose proof M.find_1 as H;
404 guarded_specialize_hyp_under_binders_by guard_nondep H ltac:(eapply lem; try eapply M.find_2)
405 | pose proof lem as H;
406 guarded_specialize_hyp_under_binders_by guard_nondep H ltac:(eapply M.find_2) ].
407
408 Global Instance fold_ext {elt A}
409 : Proper ((pointwise_relation _ (pointwise_relation _ (pointwise_relation _ eq))) ==> eq ==> eq ==> eq)
410 (fold (elt:=elt) (A:=A))
411 | 10.
412 Proof using Type.
413 intros f g Hfg ? m -> ? x ->; revert x.
414 cbv in Hfg.
415 revert f g Hfg.
416 induction m as [d m pf IH] using (t_ind (elt:=elt)); intros.
417 repeat (autounfold with trie_db; autorewrite with trie_db).
418 clear pf.
419 break_innermost_match_hyps; try now break_innermost_match; eauto.
420 rewrite !M.fold_1.
421 erewrite fold_left_ext_in; [ f_equal; try reflexivity; break_innermost_match; eauto | ].
422 intros.
423 pose_MapsTo_as_find M.elements_2.
424 specialize_under_binders_by (rewrite InA_alt; eexists; split; try eassumption; repeat split; try apply Y.eq_refl).
425 eapply IH; [ | intros; apply Hfg ]; eassumption.
426 Qed.
427 Global Instance fold_Proper_eq {elt A}
428 : Proper ((eq ==> eq ==> eq ==> eq) ==> eq ==> eq ==> eq)
429 (fold (elt:=elt) (A:=A))
430 | 10.
431 Proof using Type.
432 intros f g Hfg; repeat intro; eapply fold_ext; cbv in *; eauto.
433 Qed.
434
435 Local Ltac Proper_equiv_t :=
436 cbv;
437 setoid_rewrite M'.find_mapsto_iff;
438 repeat split; intros; subst; split_and; eauto.
439
440 Local Ltac t_full_step :=
441 first [ t_destr_step
442 | progress cbv [M_In not option_map] in *
443 | progress intros
444 | match goal with
445 | [ H : context[M.MapsTo] |- _ ] => setoid_rewrite M'.find_mapsto_iff in H
446 | [ |- context[M.MapsTo] ] => setoid_rewrite M'.find_mapsto_iff
447 end
448 | progress specialize_under_binders_by eapply ex_intro
449 | progress specialize_under_binders_by eassumption
450 | progress specialize_under_binders_by reflexivity
451 | exfalso; assumption
452 | progress inversion_option
453 | progress subst
454 | reflexivity
455 | break_innermost_match_step
456 | match goal with
457 | [ |- context[M.find ?x ?y] ] => destruct (M.find x y) eqn:?
458 end ].
459
460 Create HintDb list_map_non_empty_db discriminated.
461
462 Local Ltac t_non_empty_handle_add_step :=
463 first [ match goal with
464 | [ H : context[M.find _ (M.add _ _ _)] |- _ ]
465 => setoid_rewrite M'.add_o in H
466 | [ |- context[M.find _ (M.add _ _ _)] ]
467 => setoid_rewrite M'.add_o
468 | [ H : forall k v, (if Y.eq_dec ?x k then @?A k v else @?B k v) = Some v -> @?P k v |- _ ]
469 => assert (forall v, A x v = Some v -> P x v)
470 by (let v := fresh in
471 intros v;
472 specialize (H x v);
473 destruct (Y.eq_dec x x);
474 [ exact H | now exfalso; eauto ]);
475 assert (forall k v, (Y.eq x k -> False) -> B k v = Some v -> False)
476 by (let k := fresh in
477 let v := fresh in
478 let H' := fresh in
479 intros k v H';
480 specialize (H k v);
481 destruct (Y.eq_dec x k);
482 [ now exfalso; eauto | exact H ]);
483 clear H
484 end ].
485 Local Ltac t_non_empty_handle_remove_step :=
486 first [ match goal with
487 | [ H : context[M.find _ (M.remove _ _)] |- _ ]
488 => setoid_rewrite M'.remove_o in H
489 | [ |- context[M.find _ (M.remove _ _)] ]
490 => setoid_rewrite M'.remove_o
491 end ].
492 Local Ltac t_non_empty_handle_mapi_step :=
493 first [ match goal with
494 | [ H : context[M.find _ (M.mapi _ _)] |- _ ]
495 => setoid_rewrite M'.mapi_o_ex in H
496 | [ |- context[M.find _ (M.mapi _ _)] ]
497 => setoid_rewrite M'.mapi_o_ex
498 | [ H : context[M.find ?k (M.mapi ?f ?m)] |- _ ]
499 => let H' := fresh in
500 destruct (@M'_mapi_o_ex _ _ _ m k f) as [? [? H']];
501 rewrite H' in H; rewrite ?H' in *; clear H'; cbv [option_map] in *
502 | [ |- context[M.find ?k (M.mapi ?f ?m)] ]
503 => let H' := fresh in
504 destruct (@M'_mapi_o_ex _ _ _ m k f) as [? [? H']];
505 rewrite H'; rewrite ?H' in *; clear H'; cbv [option_map] in *
506 | [ H : forall x e, M.find x (M.mapi ?f ?m) = Some e -> @?P e |- _ ]
507 => pose proof (@M'_mapi_o_ex_impl _ _ _ m f P H);
508 clear H
509 end ].
510 Local Ltac t_non_empty_handle_map2_step :=
511 first [ match goal with
512 | [ H : context[M.find _ (M.map2 _ _ _)] |- _ ]
513 => setoid_rewrite M'.map2_o in H
514 | [ |- context[M.find _ (M.map2 _ _ _)] ]
515 => setoid_rewrite M'.map2_o
516 | [ H : context[M.find _ (map_xmap2_l _ _)] |- _ ]
517 => setoid_rewrite map_xgmap2_l in H; [ | reflexivity ]
518 | [ H : context[M.find _ (map_xmap2_r _ _)] |- _ ]
519 => setoid_rewrite map_xgmap2_r in H; [ | reflexivity ]
520 end ].
521 Local Ltac t_non_empty_extra_step :=
522 first [ match goal with
523 | [ H : forall k v, M.find k ?m = Some v -> ?f v = true, H' : forall k' v', M.find k' ?m = Some v' -> ?f v' = true -> _ |- _ ]
524 => specialize (fun k' v' pf => H' k' v' pf (H _ _ pf))
525 | [ H : forall k v, M.find k ?m = Some v -> ?f v = true, H' : forall k' v', M.find k' ?m = Some v' -> _ -> ?f v' = true -> _ |- _ ]
526 => specialize (fun k' v' pf pf' => H' k' v' pf pf' (H _ _ pf))
527 end ].
528
529 Local Ltac t_non_empty_step :=
530 first [ t_destr_conj_step
531 | assumption
532 | reflexivity
533 | progress specialize_under_binders_by exact I
534 | progress intros
535 | rewrite @M_fold_1 in *
536 | rewrite <- (fun c => @fold_left_map _ _ c andb), @fold_left_andb_truth_map_iff in *
537 | rewrite negb_true_iff, <- not_true_iff_false in *
538 | rewrite <- @M'_is_empty_iff in *
539 | rewrite @M'_find_empty in *
540 | t_destr_step
541 | progress cbv [M_Empty not] in *
542 | progress cbn [option_map Option.bind] in *
543 | progress specialize_under_binders_by (idtac; lazymatch goal with |- True -> _ => idtac end;
544 intros _)
545 | match goal with
546 | [ H : forall x, Some ?y = Some x -> _ |- _ ]
547 => specialize (H y eq_refl)
548 | [ H : context[M.MapsTo] |- _ ] => setoid_rewrite M'.find_mapsto_iff in H
549 | [ |- context[M.MapsTo] ] => setoid_rewrite M'.find_mapsto_iff
550 | [ H : context[forall i, List.In i (M.elements _) -> ?f (snd i) = true] |- _ ]
551 => setoid_rewrite M'.forall_In_elements_snd_iff with (P:=fun e => f e = true) in H
552 | [ |- context[forall i, List.In i (M.elements _) -> ?f (snd i) = true] ]
553 => setoid_rewrite M'.forall_In_elements_snd_iff with (P:=fun e => f e = true)
554 end
555 | t_non_empty_handle_add_step
556 | t_non_empty_handle_remove_step
557 | t_non_empty_handle_map2_step
558 | t_non_empty_handle_mapi_step
559 | t_non_empty_extra_step
560 | match goal with
561 | [ |- _ /\ _ ] => split
562 end
563 | solve [ eauto with nocore ] ].
564
565 Section Types.
566 Variable elt:Type.
567 Definition empty : t elt := mk None I.
568 Definition is_empty (m : t elt) : bool := Option.is_None (proj1_sig m).
569 Fixpoint add' (x : key) (y : elt) (m : t' elt) {struct x} : T.t elt
570 := match x return T.t elt with
571 | []
572 => Node (Some y) (m <- m; t_case_nd (fun _ m _ => m) m) I
573 | x :: xs
574 => let add_to d m' m := Node' d (Some (M.add x (add' xs y m') m)) I in
575 let default d := add_to d None (@M_empty _ _) in
576 match m return T.t elt with
577 | None
578 => default None
579 | Some m
580 => t_case_nd
581 (fun d m pf
582 => match m return T.t elt with
583 | None => default d
584 | Some m
585 => add_to d (M.find x m) m
586 end)
587 m
588 end
589 end.
590 Lemma add_non_empty x y m (pf : t'_P m) : t'_P (Some (add' x y m)).
591 Proof using Type.
592 intros _; specialize (pf I); cbv beta iota.
593 revert m pf.
594 induction x as [|x xs IH], m as [m|];
595 try (induction m as [d m] using (t_case (elt:=elt)));
596 cbn [add' Option.bind];
597 repeat (break_innermost_match; autounfold with trie_db; autorewrite with trie_db).
598 all: repeat t_non_empty_step.
599 all: solve [ apply IH; clear IH; repeat t_non_empty_step ].
600 Qed.
601 Definition add (x : key) (y : elt) (m : t elt) : t elt
602 := mk (Some (add' x y (proj1_sig m))) (add_non_empty x y (proj2_sig m) I).
603 Fixpoint find' (x : key) (m : t' elt) {struct x} : option elt
604 := (m <- m;
605 match x with
606 | [] => t_case_nd (fun v _ _ => v) m
607 | x :: xs
608 => t_case_nd
609 (fun v m pf => m <- m; find' xs (M.find x m))
610 m
611 end).
612 Definition find (x : key) (m : t elt) : option elt
613 := find' x (proj1_sig m).
614 Fixpoint remove' (k : key) (m : t' elt) {struct k} : t' elt
615 := (m <- m;
616 match k return _ with
617 | []
618 => t_case_nd
619 (fun d m pf
620 => match m return _ with
621 | None => None
622 | Some m => Some (Node' None (Some m) I)
623 end)
624 m
625 | x :: xs
626 => t_case_nd
627 (fun d m pf
628 => let default : t' elt := Some (Node d m pf) in
629 match m return _ with
630 | None => default
631 | Some m
632 => match remove' xs (M.find x m) return _ with
633 | None
634 => oNode_dec_empty d (M.remove x m)
635 | Some m'
636 => Some (Node' d (Some (M.add x m' m)) I)
637 end
638 end)
639 m
640 end).
641 Lemma remove_non_empty x m (pf : t'_P m) : t'_P (remove' x m).
642 Proof using Type.
643 intros _; specialize (pf I); cbv beta iota.
644 destruct (remove' x m) as [m'|] eqn:H; [ | exact I ].
645 revert m m' H pf.
646 induction x as [|x xs IH], m as [m|];
647 try (induction m as [d m] using (t_case (elt:=elt)));
648 cbn [remove' Option.bind];
649 intros *;
650 repeat match goal with
651 | [ |- Some _ = Some _ -> _ ] => intro; inversion_option; subst
652 | [ |- None = Some _ -> _ ] => intro; inversion_option; subst
653 | _ => break_innermost_match; autounfold with trie_db; autorewrite with trie_db
654 end.
655 all: repeat t_non_empty_step.
656 all: solve [ eapply IH; [ eassumption | ]; clear IH; repeat t_non_empty_step ].
657 Qed.
658 Definition remove (x : key) (m : t elt) : t elt
659 := mk (remove' x (proj1_sig m)) (remove_non_empty x (proj2_sig m) I).
660 Definition mem (x : key) (m : t elt) : bool := Option.is_Some (find x m).
661 Variable elt' elt'' : Type.
662 Lemma mapi_non_empty (f : key -> elt -> elt') (m : t' elt) (pf : t'_P m) : t'_P (option_map (mapi f) m).
663 Proof using Type.
664 clear -pf.
665 intros _; specialize (pf I); cbv beta iota.
666 destruct m as [m|]; cbv [option_map]; [ | exact I ].
667 revert f pf.
668 induction m as [d m pf IH] using (t_ind (elt:=elt)); intro f;
669 repeat (autounfold with trie_db; autorewrite with trie_db).
670 all: repeat t_non_empty_step.
671 all: repeat match goal with
672 | [ H : context[recursively_non_empty] |- _ ] => clear H
673 end.
674 all: exactly_once (idtac; multimatch goal with
675 | [ H : ?T -> False |- False ]
676 => apply H; clear H
677 end).
678 all: intros; specialize_all_ways.
679 all: repeat t_non_empty_step.
680 Qed.
681 Definition mapi (f : key -> elt -> elt') (m : t elt) : t elt'
682 := mk (option_map (mapi f) (proj1_sig m)) (mapi_non_empty f (proj2_sig m) I).
683 Definition map (f : elt -> elt') : t elt -> t elt' := mapi (fun _ => f).
684 Definition map2' (f : option elt -> option elt' -> option elt'') (m1 : t' elt) (m2 : t' elt') : t' elt''
685 := match m1, m2 with
686 | None, None => None
687 | Some m1, Some m2 => map2 f m1 m2
688 | Some m1, None => map2_l f m1
689 | None, Some m2 => map2_r f m2
690 end.
691 Lemma map2_l_non_empty f m : t'_P (map2_l (elt:=elt) (elt':=elt') (elt'':=elt'') f m).
692 Proof using Type.
693 clear.
694 intros _; cbv beta iota.
695 destruct (map2_l f m) as [m'|] eqn:H; [ | exact I ].
696 revert m m' H.
697 induction m as [d m pf IH] using (t_ind (elt:=_));
698 intro m'; induction m' as [d' m' pf'] using (t_case (elt:=_)).
699 all: repeat first [ match goal with
700 | [ |- Some _ = Some _ -> _ ] => intro; inversion_option; subst
701 | [ |- None = Some _ -> _ ] => intro; inversion_option; subst
702 end
703 | progress inversion_Node
704 | progress (break_match; autounfold with trie_db; autorewrite with trie_db) ].
705 all: repeat t_non_empty_step.
706 Qed.
707 Lemma map2_r_non_empty f m : t'_P (map2_r (elt:=elt) (elt':=elt') (elt'':=elt'') f m).
708 Proof using Type.
709 clear.
710 intros _; cbv beta iota.
711 destruct (map2_r f m) as [m'|] eqn:H; [ | exact I ].
712 revert m m' H.
713 induction m as [d m pf IH] using (t_ind (elt:=_));
714 intro m'; induction m' as [d' m' pf'] using (t_case (elt:=_)).
715 all: repeat first [ match goal with
716 | [ |- Some _ = Some _ -> _ ] => intro; inversion_option; subst
717 | [ |- None = Some _ -> _ ] => intro; inversion_option; subst
718 end
719 | progress inversion_Node
720 | progress (break_match; autounfold with trie_db; autorewrite with trie_db) ].
721 all: repeat t_non_empty_step.
722 Qed.
723 Lemma map2_non_empty f m1 m2 : t'_P (map2 (elt:=elt) (elt':=elt') (elt'':=elt'') f m1 m2).
724 Proof using Type.
725 clear.
726 intros _; cbv beta iota.
727 destruct (map2 f m1 m2) as [m'|] eqn:H; [ | exact I ].
728 revert m1 m2 m' H.
729 induction m1 as [d1 m1 pf1 IH1] using (t_ind (elt:=_));
730 intro m2; induction m2 as [d2 m2 pf2] using (t_case (elt:=_));
731 intro m'; induction m' as [d' m' pf'] using (t_case (elt:=_)).
732 all: repeat first [ match goal with
733 | [ |- Some _ = Some _ -> _ ] => intro; inversion_option; subst
734 | [ |- None = Some _ -> _ ] => intro; inversion_option; subst
735 end
736 | progress inversion_Node
737 | progress (break_match; autounfold with trie_db; autorewrite with trie_db) ].
738 all: repeat t_non_empty_step.
739 all: lazymatch goal with
740 | [ H : map2_l ?f ?m = Some _ |- _ ]
741 => generalize (@map2_l_non_empty f m I); rewrite H; clear H
742 | [ H : map2_r ?f ?m = Some _ |- _ ]
743 => generalize (@map2_r_non_empty f m I); rewrite H; clear H
744 end.
745 all: trivial.
746 Qed.
747 Lemma map2'_non_empty f m1 m2 : t'_P (map2' f m1 m2).
748 Proof using Type.
749 clear.
750 intros _; cbv beta iota.
751 destruct m1 as [m1|], m2 as [m2|]; cbn [map2'];
752 try first [ apply map2_l_non_empty | apply map2_r_non_empty | apply map2_non_empty ]; exact I.
753 Qed.
754 Definition map2 (f : option elt -> option elt' -> option elt'') (m1 : t elt) (m2 : t elt') : t elt''
755 := mk (map2' f (proj1_sig m1) (proj1_sig m2)) (map2'_non_empty _ _ _ I).
756
757 Definition fold' A (f : key -> elt -> A -> A) (m : t' elt) (a : A) : A
758 := match m with
759 | None => a
760 | Some m => fold f m a
761 end.
762 Definition fold A (f : key -> elt -> A -> A) (m : t elt) (a : A) : A
763 := fold' f (proj1_sig m) a.
764 Definition elements_with_key_prefix_and (m : t' elt) (k_prefix : key) prefix : list (key*elt)
765 := List.rev_append (fold' (fun k v rest => (k_prefix ++ k, v) :: rest) m prefix) nil.
766 Definition elements' (m : t' elt) : list (key*elt) := elements_with_key_prefix_and m nil nil.
767 Definition elements (m : t elt) : list (key*elt) := elements' (proj1_sig m).
768 Definition cardinal (m : t elt) : nat := List.length (elements m).
769 End Types.
770
771 Definition equal elt (cmp : elt -> elt -> bool) (m1 m2 : t elt) : bool
772 := let cmp' x y
773 := match x, y with
774 | Some x, Some y => Some (cmp x y)
775 | Some _, None
776 | None, Some _
777 => Some false
778 | None, None => None
779 end in
780 fold (fun _ => andb) (map2 cmp' m1 m2) true.
781 Definition MapsTo elt (k : key) (v : elt) (m : t elt) : Prop
782 := find k m = Some v.
783 Definition eq_key elt (p p':key*elt) := E.eq (fst p) (fst p').
784 Definition eq_key_elt elt (p p':key*elt) :=
785 E.eq (fst p) (fst p') /\ (snd p) = (snd p').
786
787 Definition In elt (k : key) (m : t elt) := exists e, MapsTo k e m.
788 Definition Empty elt (m : t elt) := forall a e, ~ MapsTo a e m.
789 Definition Equal elt (m m' : t elt) := forall y, find y m = find y m'.
790 Definition Equiv elt (eq_elt:elt->elt->Prop) m m' :=
791 (forall k, In k m <-> In k m') /\
792 (forall k e e', MapsTo k e m -> MapsTo k e' m' -> eq_elt e e').
793 Definition Equivb elt (cmp: elt->elt->bool) := Equiv (Cmp cmp).
794
795 Create HintDb list_map_alt discriminated.
796 Create HintDb list_map_alt1 discriminated.
797 Create HintDb list_map_alt2 discriminated.
798 Create HintDb list_map_alt3 discriminated.
799
800 Local Ltac t :=
801 repeat first [ progress intros
802 | t_destr_step
803 | reflexivity
804 | progress cbv [not] in *
805 | solve [ exfalso; auto with nocore ]
806 | t_specialize_safe_step
807 | progress break_innermost_match
808 | progress break_innermost_match_hyps
809 | progress cbv [Option.is_Some Option.is_None Option.value Option.bind M_eq_key_elt] in *
810 | progress autounfold with list_map_alt in *
811 | progress autounfold with trie_db in *
812 | match goal with
813 | [ H : eqlistA _ nil _ |- _ ] => inversion H; clear H
814 | [ H : eqlistA _ _ nil |- _ ] => inversion H; clear H
815 | [ H : eqlistA _ (_ :: _) _ |- _ ] => inversion H; clear H
816 | [ H : eqlistA _ _ (_ :: _) |- _ ] => inversion H; clear H
817 | [ H : InA _ _ (_ :: _) |- _ ] => inversion H; clear H
818 | [ H : InA _ _ nil |- _ ] => inversion H; clear H
819 | [ H : List.In _ (List.map _ _) |- _ ] => rewrite in_map_iff in H
820 | [ H : List.In _ (List.flat_map _ _) |- _ ] => rewrite in_flat_map in H
821 | [ H : ?x = ?x |- _ ] => clear H
822 | [ H : ?T, H' : ?T |- _ ] => clear H
823 | [ H : ?x = Some _, H' : context[?x] |- _ ] => rewrite H in H'
824 | [ H : ?x = None, H' : context[?x] |- _ ] => rewrite H in H'
825 | [ H : proj1_sig ?x = _ |- _ ] => is_var x; destruct x
826 | [ H : Some _ = ?x |- _ ] => symmetry in H
827 | [ H : None = ?x |- _ ] => symmetry in H
828 end
829 | progress autorewrite with list_map_alt in *
830 | progress autorewrite with trie_db in *
831 | solve [ eauto using ex_intro, eq_refl with nocore ]
832 | progress specialize_under_binders_by reflexivity
833 | progress specialize_under_binders_by progress autorewrite with trie_db
834 | progress specialize_under_binders_by progress autorewrite with list_map_alt
835 | progress cbn in * ].
836
837 Lemma find'_None elt k : @find' elt k None = None.
838 Proof using Type. case k; t. Qed.
839 Hint Rewrite find'_None : list_map_alt.
840 Lemma find_None elt k pf : @find elt k (mk None pf) = None.
841 Proof using Type. case k; t. Qed.
842 Hint Rewrite find_None : list_map_alt.
843
844 Definition Empty_alt elt (m : t elt) : Prop := proj1_sig m = None.
845
846 Lemma Empty_alt_iff elt (s : t elt) : Empty s <-> Empty_alt s.
847 Proof using Type.
848 cbv [Empty Empty_alt MapsTo M_Empty not key Y_t find proj1_sig].
849 split; repeat intro; destruct_head' t; destruct_head' option; try reflexivity; inversion_option;
850 exfalso;
851 try solve [ destruct_one_head list; inversion_option ].
852 let t := match goal with H : T.t _ |- _ => H end in
853 induction t as [d m pf] using (t_ind (elt:=elt)).
854 let H := match goal with H : forall a e, find' a _ = Some e -> False |- _ => H end in
855 pose proof (H nil); pose proof (fun x y => H (x :: y)); clear H.
856 cbn [find' Option.bind] in *.
857 repeat (autounfold with trie_db in *; autorewrite with trie_db in * ).
858 specialize_under_binders_by rewrite t_case_beta.
859 repeat t_non_empty_step.
860 let rec tac _ :=
861 first [ t_non_empty_step
862 | match goal with
863 | [ H : _ = Some _ |- _ ]
864 => progress specialize_under_binders_by rewrite H;
865 solve [ repeat tac () ]
866 | [ H : _ |- _ ] => apply H; clear H; solve [ repeat tac () ]
867 end ] in
868 tac ().
869 Qed.
870
871 Local Instance eq_key_equiv elt : Equivalence (@eq_key elt) | 10.
872 Proof using Type.
873 split; cbv; intros; break_innermost_match; break_innermost_match_hyps; try ((idtac + symmetry + etransitivity + exfalso); (eassumption + reflexivity)).
874 Qed.
875 Local Instance eq_key_elt_equiv elt : Equivalence (@eq_key_elt elt) | 10.
876 Proof using Type.
877 split; cbv; intros; break_innermost_match; break_innermost_match_hyps; destruct_head'_and; split; try ((idtac + symmetry + etransitivity + exfalso); (eassumption + reflexivity)).
878 Qed.
879 Local Existing Instances eq_key_equiv eq_key_elt_equiv | 10.
880
881 Hint Unfold
882 (*In_alt*)
883 empty
884 is_empty
885 mem
886 add
887 (*find*)
888 (*remove*)
889 mem
890 map
891 mapi
892 map2
893 elements
894 cardinal
895 fold
896 equal
897 MapsTo
898 In
899 Empty
900 Empty_alt
901 (*Equiv_alt*)
902 (*Equivb_alt*)
903 eq_key
904 eq_key_elt
905 M_eq_key
906 M_eq_key_elt
907 option_map
908 RelProd
909 relation_conjunction
910 predicate_intersection
911 pointwise_extension
912 RelCompFun
913 Option.is_Some
914 Option.is_None
915 Option.bind
916 option_map
917 Option.value
918 not
919 Node'
920 oNode_dec_empty
921 : list_map_alt.
922
923 Hint Rewrite Empty_alt_iff (*Equiv_alt_iff*)
924 M_cardinal_1
925 M_fold_1
926 M'_find_mapsto_iff
927 M'_find_empty
928 M'_add_o
929 M'_remove_o
930 M'_map_o
931 M'_map2_o
932 E'_eq_alt
933 Bool.andb_true_iff
934 InA_app_iff
935 map_length
936 app_length
937 fold_left_map
938 fold_left_app
939 in_map_iff
940 in_flat_map
941 : list_map_alt.
942
943 Hint Resolve
944 M_MapsTo_1'
945 M_mem_1'
946 M_empty_1'
947 M'_empty_1''
948 M_is_empty_1'
949 M_add_1'
950 M_remove_1'
951 M_find_1'
952 M_elements_1'
953 M_equal_1'
954 M_map_1'
955 M_mapi_1'
956 M_map2_1'
957 : list_map_alt1.
958 Hint Resolve
959 M_mem_2'
960 M_is_empty_2'
961 M_add_2'
962 M_remove_2'
963 M_find_2'
964 M_elements_2'
965 M_equal_2'
966 M_map_2'
967 M_mapi_2'
968 M_map2_2'
969 : list_map_alt2.
970 Hint Resolve
971 M_add_3'
972 M_remove_3'
973 M_elements_3w'
974 : list_map_alt3.
975
976 Hint Constructors ex and or
977 : list_map_alt1 list_map_alt2 list_map_alt3.
978 Hint Extern 10
979 => progress unfold M_In in *
980 : list_map_alt1 list_map_alt2 list_map_alt3.
981
982
983 Local Ltac spec_t_step_quick
984 := first [ progress intros
985 | progress cbn [fst snd proj1_sig] in *
986 | apply (f_equal2 (@pair _ _))
987 | progress destruct_head'_False
988 | rewrite <- andb_lazy_alt
989 | progress repeat autorewrite with list_map_alt in *
990 (*| progress change Y.t with M.key in **)
991 | progress repeat autounfold with list_map_alt in *
992 | match goal with H : _ |- _ => refine H end
993 | congruence
994 | reflexivity
995 | progress auto
996 | exact _
997 | progress destruct_head'_and
998 | progress destruct_head'_ex
999 | progress break_innermost_match_hyps
1000 | progress break_innermost_match
1001 | progress destruct_head'_or
1002 | progress specialize_under_binders_by eassumption
1003 | progress inversion_option
1004 | progress subst
1005 | match goal with
1006 | [ H : M.MapsTo ?k ?v ?m, H' : context[M.find ?k ?m] |- _ ]
1007 => erewrite M.find_1 in H' by exact H
1008 end
1009 | progress specialize_under_binders_by apply conj ].
1010
1011 Hint Extern 100
1012 => spec_t_step_quick
1013 : list_map_alt1 list_map_alt2 list_map_alt3.
1014
1015 Local Ltac saturate_find_step :=
1016 let cleanup :=
1017 repeat match goal with
1018 | [ H : ?T, H' : ?T |- _ ] => clear H'
1019 end in
1020 first [ progress inversion_option
1021 | progress subst
1022 | exfalso; assumption
1023 | progress cleanup
1024 | progress (specialize_under_binders_by (idtac; match goal with |- _ -> False => eauto with nocore end); cleanup)
1025 | match goal with
1026 | [ H : Y.eq _ _ |- _ ]
1027 => unique pose proof (Y.eq_sym H)
1028 | [ H : Y.eq _ _, H' : context[Y.eq _ _ -> False] |- _ ]
1029 => lazymatch type of H' with | _ -> False => fail | _ => idtac end;
1030 clear H'
1031 | [ H : Y.eq _ _ |- _ ] => progress (specialize_all_ways_under_binders_by exact H; cleanup)
1032 | [ H : context[forall a, M.find _ _ = _], H' : M.find _ _ = _ |- _ ]
1033 => let H'' := fresh in
1034 pose proof H' as H'';
1035 rewrite H in H'' by fail;
1036 let T := type of H'' in
1037 lazymatch T with
1038 | Some _ = Some _
1039 => progress inversion_option; progress subst
1040 | Some _ = None => inversion_option
1041 | None = Some _ => inversion_option
1042 | _
1043 => revert H'';
1044 lazymatch goal with
1045 | [ H : T |- _ ] => fail
1046 | _ => idtac T; intro
1047 end
1048 end
1049 end
1050 | progress (specialize_gen_under_binders_by' true ltac:(eauto with nocore); cleanup)
1051 | match goal with
1052 | [ H : M.find _ _ = Some _ |- _ ] => progress (specialize_all_ways_under_binders_by exact H; cleanup)
1053 end ].
1054 Local Ltac saturate_find := repeat saturate_find_step.
1055
1056 Local Hint Extern 2 => Proper_compose_hint : typeclass_instances.
1057
1058 Local Ltac spec_t_nosubst_step_quick :=
1059 first [ progress intros
1060 | t_destr_step
1061 | reflexivity
1062 | progress destruct_head' t
1063 | progress destruct_head' key
1064 | t_specialize_safe_step ].
1065 Local Ltac spec_t_nosubst_step :=
1066 first [ spec_t_nosubst_step_quick
1067 | progress autorewrite with trie_db in *
1068 | progress autounfold with trie_db in *
1069 | progress autorewrite with list_map_alt in *
1070 | progress autounfold with list_map_alt in *
1071 | progress cbn [find add'] in *
1072 | setoid_rewrite M'.find_mapsto_iff
1073 | match goal with
1074 | [ H : context[M.MapsTo] |- _ ] => setoid_rewrite M'.find_mapsto_iff in H
1075 end
1076 | progress break_innermost_match
1077 | progress break_innermost_match_hyps
1078 | match goal with
1079 | [ |- iff _ _ ] => split
1080 end
1081 | congruence
1082 | solve [ eauto ]
1083 | match goal with
1084 | [ |- context[@M_find ?args ?elt ?x ?m] ] => destruct (@M_find args elt x m) eqn:?
1085 end
1086 (*| (* should figure out how to generalize these into lemmas *)
1087 match goal with
1088 | [ H : M.is_empty (M.remove ?k ?m) = true, H' : M.find ?k' ?m = Some _, H'' : E2.eq ?k ?k' -> False |- _ ]
1089 => exfalso; clear -H H' H'';
1090 apply M.is_empty_2 in H;
1091 cbv [M.Empty] in H;
1092 setoid_rewrite M'.find_mapsto_iff in H;
1093 specialize (H k')
1094 | [ H : M2.is_empty (M2.remove ?k ?m) = true, H' : M2.find ?k' ?m = Some _, H'' : E2.eq ?k ?k' -> False |- _ ]
1095 => exfalso; clear -H H' H'';
1096 apply M2.is_empty_2 in H;
1097 cbv [M2.Empty] in H;
1098 setoid_rewrite M2_find_iff in H;
1099 specialize (H k')
1100 end*) ].
1101 Local Ltac spec_t_step :=
1102 first [ spec_t_nosubst_step
1103 | progress setoid_subst_rel Y.eq ].
1104
1105 Local Ltac spec_t := repeat spec_t_step.
1106
1107 Lemma find'_iff elt0 k v m0 : @MapsTo elt0 k v m0 <-> find' k (proj1_sig m0) = Some v.
1108 Proof using Type. clear; spec_t. Qed.
1109 Lemma find_iff elt0 k v m0 : @MapsTo elt0 k v m0 <-> find k m0 = Some v.
1110 Proof using Type. clear; spec_t. Qed.
1111
1112 Section Spec.
1113
1114 Variable elt:Type.
1115 Variable elt' elt'' : Type.
1116 Variable m m' m'' : t elt.
1117 Variable x y z : key.
1118 Variable e e' : elt.
1119 Lemma MapsTo_1 : E.eq x y -> MapsTo x e m -> MapsTo y e m.
1120 Proof using Type.
1121 clear.
1122 cbv [MapsTo find].
1123 destruct m as [m' pf]; cbn [proj1_sig]; clear m pf.
1124 revert x y m'.
1125 induction x as [|x' xs IH], y as [|y' ys], m' as [m|]; clear x y m';
1126 try specialize (IH ys);
1127 try (induction m as [d m'] using (t_case (elt:=elt)); clear m).
1128 all: t.
1129 all: pose_MapsTo_as_find M.MapsTo_1.
1130 all: repeat match goal with
1131 | [ H : context[find' _ (@M_find _ ?a ?b ?c)] |- _ ]
1132 => destruct (@M_find _ a b c) eqn:?
1133 | [ |- context[find' _ (@M_find _ ?a ?b ?c)] ]
1134 => destruct (@M_find _ a b c) eqn:?
1135 end.
1136 all: specialize_under_binders_by eassumption.
1137 all: t.
1138 Qed.
1139 Lemma mem_1 : In x m -> mem x m = true.
1140 Proof using Type. clear; t. Qed.
1141 Lemma mem_2 : mem x m = true -> In x m.
1142 Proof using Type. clear; t. Qed.
1143 Lemma empty_1 : Empty (@empty elt).
1144 Proof using Type. clear; pose proof M.empty_1; spec_t. Qed.
1145 Lemma is_empty_1 : Empty m -> is_empty m = true.
1146 Proof using Type. clear; pose proof M.is_empty_1; spec_t. Qed.
1147 Lemma is_empty_2 : is_empty m = true -> Empty m.
1148 Proof using Type. clear; pose proof M.is_empty_2; spec_t. Qed.
1149 Lemma find_1 : MapsTo x e m -> find x m = Some e.
1150 Proof using Type. apply find_iff. Qed.
1151 Lemma find_2 : find x m = Some e -> MapsTo x e m.
1152 Proof using Type. apply find_iff. Qed.
1153 Hint Rewrite fold_left_flat_map fold_left_map : list_map_alt.
1154 Lemma elements_alt_1 (m0 : t' elt) k_prefix i : elements_with_key_prefix_and m0 k_prefix (List.rev i) = fold' (fun k v rest => rest ++ [(k_prefix ++ k, v)]) m0 i.
1155 Proof using Type.
1156 clear.
1157 pose proof (_ : forall A, Proper (eq ==> eq ==> eq) (@cons A)).
1158 cbv [elements elements_with_key_prefix_and fold'].
1159 rewrite <- rev_alt; destruct m0 as [m'|]; [ | rewrite rev_involutive; reflexivity ].
1160 revert i k_prefix.
1161 induction m' as [d m pf IH] using (t_ind (elt:=elt)); clear m'; intros.
1162 repeat (autounfold with trie_db; autorewrite with trie_db).
1163 destruct m; try (now destruct d; cbn [List.rev]; rewrite rev_involutive); [].
1164 rewrite !M.fold_1.
1165 setoid_rewrite (@app_cons_app_app _ k_prefix).
1166 etransitivity;
1167 [
1168 | apply fold_left_ext_in; intros;
1169 pose_MapsTo_as_find M.elements_2;
1170 specialize_under_binders_by (rewrite InA_alt; eexists; split; try eassumption; repeat split; try apply Y.eq_refl);
1171 eapply IH; eassumption ].
1172 clear IH pf.
1173 set (i1 := match d with None => i | _ => _ end).
1174 set (i2 := match d with None => List.rev i | _ => _ end).
1175 replace i2 with (List.rev i1)
1176 by (subst i1 i2; break_innermost_match; cbn [rev]; rewrite ?rev_app_distr; cbn [rev List.app]; reflexivity).
1177 clearbody i1.
1178 clear i2 i.
1179 revert i1.
1180 set (ls := M.elements _).
1181 induction ls as [|? ? IH]; cbn [fold_left]; intros;
1182 [ | rewrite <- IH ]; now rewrite rev_involutive.
1183 Qed.
1184 Lemma elements_with_key_prefix_and_cps_id (m0 : t' elt) k_prefix prefix : elements_with_key_prefix_and m0 k_prefix prefix = (List.rev prefix) ++ elements_with_key_prefix_and m0 k_prefix nil.
1185 Proof using Type.
1186 clear.
1187 pose proof (_ : forall A, Proper (eq ==> eq ==> eq) (@cons A)).
1188 set (n := nil); change n with (List.rev n); subst n.
1189 rewrite <- (List.rev_involutive prefix), !elements_alt_1, List.rev_involutive.
1190 cbv [fold'].
1191 destruct m0 as [m'|]; [ | now rewrite ?app_nil_r ].
1192 generalize (List.rev prefix); clear prefix; intro prefix.
1193 revert prefix k_prefix.
1194 induction m' as [d m pf IH] using (t_ind (elt:=elt)); clear m'; intros.
1195 repeat (autounfold with trie_db; autorewrite with trie_db).
1196 destruct m; try (now destruct d; rewrite ?app_nil_r, ?app_nil_l); [].
1197 rewrite !M.fold_1.
1198 setoid_rewrite (@app_cons_app_app _ k_prefix).
1199 etransitivity;
1200 [ apply fold_left_ext_in; intros;
1201 pose_MapsTo_as_find M.elements_2;
1202 specialize_under_binders_by (rewrite InA_alt; eexists; split; try eassumption; repeat split; try apply Y.eq_refl);
1203 eapply IH; eassumption
1204 | ].
1205 etransitivity;
1206 [
1207 | apply f_equal;
1208 apply fold_left_ext_in; intros;
1209 pose_MapsTo_as_find M.elements_2;
1210 specialize_under_binders_by (rewrite InA_alt; eexists; split; try eassumption; repeat split; try apply Y.eq_refl);
1211 symmetry; eapply IH; eassumption ].
1212 clear IH pf.
1213 set (i1 := match d with None => prefix | _ => _ end).
1214 set (i2 := match d with None => nil | _ => _ end).
1215 replace i1 with (prefix ++ i2)
1216 by (subst i1 i2; break_innermost_match; cbn [List.app]; now rewrite ?app_nil_r).
1217 clearbody i2.
1218 clear i1 d.
1219 revert i2.
1220 set (ls := M.elements _).
1221 induction ls as [|? ? IH]; cbn [fold_left]; intros;
1222 [ reflexivity | ].
1223 now rewrite <- IH, ?app_assoc.
1224 Qed.
1225 Lemma elements_with_key_prefix_alt (m0 : t' elt) k_prefix prefix
1226 : elements_with_key_prefix_and m0 k_prefix (List.rev prefix)
1227 = prefix
1228 ++ match m0 with
1229 | None => []
1230 | Some m
1231 => t_case_nd
1232 (fun d m pf
1233 => match d with
1234 | Some d => [(k_prefix ++ [], d)]
1235 | None => []
1236 end
1237 ++ (List.flat_map
1238 (fun km => elements_with_key_prefix_and (Some (snd km)) (k_prefix ++ [fst km]) nil)
1239 match m with
1240 | Some m => M.elements m
1241 | None => []
1242 end))
1243 m
1244 end.
1245 Proof using Type.
1246 clear.
1247 pose proof (_ : forall A, Proper (eq ==> eq ==> eq) (@cons A)).
1248 rewrite elements_with_key_prefix_and_cps_id, rev_involutive.
1249 apply f_equal.
1250 change (@nil ?A) with (List.rev (@nil A)) at 1.
1251 rewrite elements_alt_1.
1252 cbv [fold'].
1253 destruct m0 as [m'|]; [ | now rewrite ?app_nil_r ].
1254 induction m' as [d m pf] using (t_case (elt:=elt)); clear m'.
1255 repeat (autounfold with trie_db; autorewrite with trie_db).
1256 destruct m as [m'|]; [ | break_innermost_match; cbn; now rewrite ?app_nil_r ].
1257 cbn [List.app].
1258 clear pf.
1259 set (d' := match d with None => nil | Some _ => _ end).
1260 rewrite !M.fold_1.
1261 setoid_rewrite (@app_cons_app_app _ k_prefix).
1262 set (ls := M.elements _).
1263 clearbody d'; clear d.
1264 revert d'.
1265 induction ls as [|?? IH]; cbn -[elements_with_key_prefix_and]; intros;
1266 [ now rewrite ?app_nil_r | ].
1267 rewrite IH, !app_assoc, ?app_nil_r.
1268 apply f_equal2; [ | reflexivity ].
1269 rewrite <- (List.rev_involutive d'), <- elements_with_key_prefix_and_cps_id, elements_alt_1, rev_involutive; reflexivity.
1270 Qed.
1271 Lemma map_elements_with_key_prefix_and k_prefix (m0 : t' elt)
1272 : List.map
1273 (fun '(ks, v) => (k_prefix ++ ks, v))
1274 (elements' m0)
1275 = elements_with_key_prefix_and m0 k_prefix nil.
1276 Proof using Type.
1277 clear.
1278 pose proof (_ : forall A, Proper (eq ==> eq ==> eq) (@cons A)).
1279 cbv [elements'].
1280 set (i := nil) at 2 3.
1281 etransitivity; [ | set_evars; rewrite <- (app_nil_r k_prefix); subst_evars; reflexivity ].
1282 set (k_prefix' := nil).
1283 change i with (List.rev i) at 1.
1284 change i with (List.rev (List.map (fun '(ks, v) => (k_prefix ++ ks, v)) i)) at 2.
1285 rewrite !elements_with_key_prefix_alt.
1286 destruct m0 as [m'|]; [ | reflexivity ].
1287 rewrite map_app.
1288 apply f_equal.
1289 clearbody i k_prefix'.
1290 clear.
1291 revert k_prefix k_prefix'.
1292 induction m' as [d m pf IH] using (t_ind (elt:=elt)); clear m'; intros.
1293 repeat (autounfold with trie_db; autorewrite with trie_db).
1294 destruct m as [m'|]; [ | break_innermost_match; cbn; now rewrite ?app_nil_r ].
1295 rewrite map_app.
1296 apply f_equal2;
1297 [ now break_innermost_match; cbn; rewrite ?app_nil_r
1298 | ].
1299 rewrite map_flat_map.
1300 apply ListUtil.flat_map_ext; intros.
1301 rewrite <- app_assoc.
1302 change (@nil ?A) with (List.rev (@nil A)).
1303 rewrite !elements_with_key_prefix_alt; cbn.
1304 pose_MapsTo_as_find M.elements_2.
1305 specialize_under_binders_by (rewrite InA_alt; eexists; split; try eassumption; repeat split; try apply Y.eq_refl).
1306 eapply IH; eassumption.
1307 Qed.
1308 Lemma elements'_alt (m0 : t' elt)
1309 : elements' m0
1310 = match m0 with
1311 | None => []
1312 | Some m
1313 => t_case_nd
1314 (fun d m pf
1315 => match d with
1316 | Some d => [([], d)]
1317 | None => []
1318 end
1319 ++ (List.flat_map
1320 (fun '(k, m) => List.map
1321 (fun '(ks, v) => (k :: ks, v))
1322 (elements' (Some m)))
1323 match m with
1324 | Some m => M.elements m
1325 | None => []
1326 end))
1327 m
1328 end.
1329 Proof using Type.
1330 clear.
1331 unfold elements' at 1.
1332 change (@nil ?A) with (List.rev (@nil A)) at 2.
1333 rewrite elements_with_key_prefix_alt; cbn -[elements'].
1334 destruct m0 as [m'|]; [ | reflexivity ].
1335 induction m' as [d m pf] using (t_case (elt:=elt)); clear m'.
1336 repeat (autounfold with trie_db; autorewrite with trie_db).
1337 repeat (f_equiv; []; repeat intro).
1338 break_innermost_match.
1339 rewrite (@map_elements_with_key_prefix_and [_]).
1340 reflexivity.
1341 Qed.
1342 Lemma fold_1 :
1343 forall (A : Type) (i : A) (f : key -> elt -> A -> A),
1344 fold f m i = fold_left (fun a p => f (fst p) (snd p) a) (elements m) i.
1345 Proof using Type.
1346 clear.
1347 intros A i f.
1348 cbv [fold fold' elements].
1349 destruct m as [[m'|] pf']; clear m; [ | reflexivity ].
1350 cbn [proj1_sig] in *; clear pf'.
1351 revert f i.
1352 induction m' as [d m pf IH] using (t_ind (elt:=elt)); clear m'; intros.
1353 rewrite elements'_alt.
1354 repeat (autounfold with trie_db; autorewrite with trie_db).
1355 destruct m as [m'|]; [ | now break_innermost_match ].
1356 specialize_under_binders_by (apply M.find_1, M.elements_2; rewrite InA_alt; eexists; repeat split; try reflexivity).
1357 clear pf.
1358 rewrite M.fold_1.
1359 rewrite fold_left_app, fold_left_flat_map.
1360 etransitivity; [ apply fold_left_ext_in | f_equal; try reflexivity; break_innermost_match; cbn; reflexivity ]; cbn.
1361 intros.
1362 break_innermost_match.
1363 erewrite IH by eassumption.
1364 rewrite fold_left_map; f_equiv; repeat intro; subst; break_innermost_match; reflexivity.
1365 Qed.
1366
1367 Lemma add'_o m0 v x' y' : @find' elt y' (Some (add' x' v m0)) = if E.eq_dec x' y' then Some v else find' y' m0.
1368 Proof using Type.
1369 clear.
1370 revert x' y' m0.
1371 induction x' as [|x xs IH], y' as [|y ys], m0 as [m|];
1372 first [ specialize (IH ys) | try clear IH ];
1373 try (induction m as [d m'] using (t_case (elt:=elt)); clear m).
1374 all: repeat first [ progress invlist eqlistA
1375 | progress cbn [find' add' Option.bind]
1376 | progress cbv [not] in *
1377 | progress autounfold with trie_db
1378 | reflexivity
1379 | progress specialize_under_binders_by apply eqlistA_cons
1380 | match goal with
1381 | [ IH : forall m, find' ?k (Some (add' ?xs ?v m)) = _ |- context[find' ?k (Some (add' ?xs ?v ?m'))] ]
1382 => rewrite IH
1383 end
1384 | progress break_innermost_match_step
1385 | progress autorewrite with trie_db list_map_alt in *
1386 | progress setoid_subst_rel Y.eq ].
1387 all: t.
1388 Qed.
1389 Lemma add_1 : E.eq x y -> MapsTo y e (add x e m).
1390 Proof using Type. clear; cbv [find add]; intros; rewrite !find'_iff, !add'_o in *; spec_t. Qed.
1391 Lemma add_2 : ~ E.eq x y -> MapsTo y e m -> MapsTo y e (add x e' m).
1392 Proof using Type. clear; cbv [find add]; intros; rewrite !find'_iff, !add'_o in *; spec_t. Qed.
1393 Lemma add_3 : ~ E.eq x y -> MapsTo y e (add x e' m) -> MapsTo y e m.
1394 Proof using Type. clear; cbv [find add]; intros; rewrite !find'_iff, !add'_o in *; spec_t. Qed.
1395 Lemma remove'_o m0 x' y'
1396 : @find' elt y' (remove' x' m0) = if E.eq_dec x' y' then None else find' y' m0.
1397 Proof using Type.
1398 clear.
1399 revert x' y' m0.
1400 induction x' as [|x xs IH], y' as [|y ys], m0 as [m|];
1401 first [ specialize (IH ys) | try clear IH ];
1402 try (induction m as [d m'] using (t_case (elt:=elt)); clear m).
1403 all: repeat first [ progress intros
1404 | progress subst
1405 | progress inversion_option
1406 | progress inversion_sigma
1407 | progress invlist eqlistA
1408 | progress cbn [find' remove' Option.bind]
1409 | progress cbv [not] in *
1410 | progress autounfold with trie_db
1411 | progress split_iff
1412 | reflexivity
1413 | exfalso; assumption
1414 | progress specialize_under_binders_by assumption
1415 | progress specialize_under_binders_by apply eqlistA_nil
1416 | progress specialize_under_binders_by apply eqlistA_cons
1417 | match goal with
1418 | [ H : forall m, find' ?ys (remove' ?xs m) = _, H' : remove' ?xs _ = Some ?v |- context[Some ?v] ]
1419 => rewrite <- H', H
1420 | [ H : forall m, find' ?ys (remove' ?xs m) = find' ?ys m, H' : remove' ?xs ?m' = None |- context[find' ?ys ?m'] ]
1421 => rewrite <- H, H'
1422 | [ H : context[@M_is_empty_1 _ ?a ?b] |- _ ]
1423 => generalize dependent (@M_is_empty_1 _ a b)
1424 end
1425 | progress break_innermost_match_step
1426 | progress autorewrite with trie_db list_map_alt in *
1427 | progress cbv [dec_empty_cps] in *
1428 | apply conj
1429 | progress break_innermost_match_hyps
1430 | progress setoid_subst_rel Y.eq
1431 | match goal with
1432 | [ H : M.is_empty (M.remove ?x ?m) = true |- context[M.find ?y ?m] ]
1433 => destruct (Y.eq_dec x y); specialize_under_binders_by assumption;
1434 setoid_subst_rel Y.eq;
1435 rewrite <- @M'_is_empty_iff in H;
1436 cbv [M_Empty not] in H;
1437 try now exfalso
1438 | [ |- None = find' _ (@M_find _ ?a ?b ?c) ]
1439 => destruct (@M_find _ a b c) eqn:?
1440 end ].
1441 all: specialize_all_ways.
1442 all: t.
1443 Qed.
1444 Lemma remove_1 : E.eq x y -> ~ In y (remove x m).
1445 Proof using Type. clear; cbv [In]; setoid_rewrite find_iff; setoid_rewrite remove'_o; spec_t. Qed.
1446 Lemma remove_2 : ~ E.eq x y -> MapsTo y e m -> MapsTo y e (remove x m).
1447 Proof using Type. clear; cbv [In]; setoid_rewrite find_iff; setoid_rewrite remove'_o; spec_t. Qed.
1448 Lemma remove_3 : MapsTo y e (remove x m) -> MapsTo y e m.
1449 Proof using Type. clear; cbv [In]; setoid_rewrite find_iff; setoid_rewrite remove'_o; spec_t. Qed.
1450
1451 Let elements_iff :
1452 MapsTo x e m <-> InA (@eq_key_elt _) (x,e) (elements m).
1453 Proof.
1454 clear; cbv [MapsTo].
1455 rewrite InA_alt.
1456 cbv [eq_key_elt]; cbn [fst snd].
1457 destruct m as [m' pf]; clear m.
1458 cbv [find elements proj1_sig]; clear pf; rename m' into m.
1459 revert m; induction x as [|?? IH];
1460 (intros [m'|];
1461 [ induction m' as [d m pf] using (t_case (elt:=elt)); clear m'
1462 | now rewrite elements'_alt; destruct x; cbn; split; intros; inversion_option; firstorder ]).
1463 all: rewrite elements'_alt.
1464 all: repeat (autounfold with trie_db; autorewrite with trie_db).
1465 all: destruct m as [m'|].
1466 all: setoid_rewrite in_app_iff.
1467 all: setoid_rewrite in_flat_map.
1468 all: cbn [find' Option.bind].
1469 all: repeat (cbn [Option.bind]; autounfold with trie_db; autorewrite with trie_db).
1470 1-2: now split; intros; subst; t;
1471 eexists (_, _); cbn; repeat esplit; try reflexivity; eauto.
1472 all: try (rewrite IH; clear IH).
1473 all: split; intros; inversion_option.
1474 all: repeat first [ progress destruct_head'_ex
1475 | progress destruct_head'_and
1476 | progress destruct_head'_or
1477 | progress destruct_head'_False
1478 | progress subst
1479 | progress cbn [List.In fst snd] in *
1480 | progress cbv [M_eq_key_elt] in *
1481 | rewrite @E'_eq_alt in *
1482 | rewrite in_map_iff in *
1483 | rewrite InA_alt in *
1484 | progress invlist eqlistA
1485 | break_innermost_match_hyps_step
1486 | apply conj
1487 | apply eqlistA_cons
1488 | eassumption
1489 | erewrite M.find_1
1490 by (apply M.elements_2; rewrite InA_alt; eexists; split; [ | eassumption ]; repeat esplit; eassumption)
1491 | reflexivity
1492 | match goal with
1493 | [ H : M.find _ _ = Some _ |- _ ] => apply M.find_2, M.elements_1 in H
1494 | [ |- False \/ _ ] => right
1495 | [ |- ((nil, _) = (cons _ _, _) \/ False) \/ _ ] => right
1496 | [ H : List.In _ (elements' (M.find ?k ?m)) |- _ ]
1497 => destruct (M.find k m) eqn:?
1498 | [ H : context[elements' None] |- _ ]
1499 => cbn in H
1500 end
1501 | eexists (_, _)
1502 | rewrite <- surjective_pairing ].
1503 Qed.
1504 Lemma elements_1 :
1505 MapsTo x e m -> InA (@eq_key_elt _) (x,e) (elements m).
1506 Proof using Type. apply elements_iff. Qed.
1507 Lemma elements_2 :
1508 InA (@eq_key_elt _) (x,e) (elements m) -> MapsTo x e m.
1509 Proof using Type. apply elements_iff. Qed.
1510 Lemma elements_3w : NoDupA (@eq_key _) (elements m).
1511 Proof using Type.
1512 clear.
1513 cbv [elements proj1_sig]; destruct m as [[m'|] _]; clear m;
1514 [ | now constructor ].
1515 induction m' as [d m pf IH] using (t_ind (elt:=elt)); clear m'.
1516 rewrite elements'_alt.
1517 repeat (autounfold with trie_db; autorewrite with trie_db).
1518 apply NoDupA_app; try exact _; break_innermost_match; repeat constructor;
1519 intros *; try (rewrite !InA_alt; cbv [eq_key]); cbn [fst snd List.In proj1_sig] in *; clear pf.
1520 all: repeat first [ intro
1521 | progress cbn [fst snd proj1_sig] in *
1522 | progress destruct_head'_ex
1523 | progress destruct_head'_and
1524 | progress destruct_head'_False
1525 | progress subst
1526 | progress destruct_head'_or
1527 | rewrite in_flat_map in *
1528 | rewrite in_map_iff in *
1529 | break_innermost_match_hyps_step
1530 | progress destruct_head' prod
1531 | progress setoid_subst_rel E.eq
1532 | rewrite @E'_eq_alt in *
1533 | progress invlist eqlistA ].
1534 all: [ > ].
1535 let x := match goal with |- NoDupA _ (flat_map _ (M.elements ?m)) => m end in
1536 pose proof (M.elements_3w x).
1537 specialize_under_binders_by (apply M.find_1, M.elements_2; rewrite InA_alt; eexists; repeat split; try reflexivity).
1538 eapply @NoDupA_flat_map; try eassumption; try exact _.
1539 { rewrite Forall_map, Forall_forall; intros; break_innermost_match.
1540 eapply NoDupA_map_inv'; [ | eapply IH; eassumption ].
1541 spec_t. }
1542 { cbv [eq_key M_eq_key]; intros; rewrite !InA_alt in *.
1543 repeat match goal with
1544 | [ H : context[M.elements _] |- _ ] => clear H
1545 | [ H : context[@elements' ?A] |- _ ] => generalize dependent (@elements' A); intros
1546 | [ H : ~M.Empty _ |- _ ] => clear H
1547 | [ H : option _ |- _ ] => clear H
1548 end.
1549 repeat first [ t_destr_step
1550 | progress destruct_head' prod
1551 | progress setoid_subst_rel Y.eq
1552 | rewrite @E'_eq_alt in *
1553 | t_specialize_safe_step ]. }
1554 Qed.
1555 Lemma cardinal_1 : cardinal m = length (elements m).
1556 Proof using Type. clear; spec_t. Qed.
1557 End Spec.
1558
1559 Lemma mapi_o_ex : forall (elt elt':Type)(m: t elt)(x:key)(f:key->elt->elt'), exists y, E.eq y x /\ find x (mapi f m) = option_map (f y) (find x m).
1560 Proof using Type.
1561 cbv [mapi find].
1562 intros elt elt' [[m|] pf]; [ | intros [|]; eexists; split; reflexivity ].
1563 cbn [proj1_sig]; clear pf.
1564 induction m as [d m pf IH] using (t_ind (elt:=elt)).
1565 intros [|x xs] f.
1566 all: cbn [find']; cbn [option_map Option.bind].
1567 all: repeat (autounfold with trie_db; autorewrite with trie_db).
1568 all: try (eexists; split; reflexivity).
1569 all: [ > ].
1570 destruct m as [m|]; [ | now cbn; eauto ].
1571 cbn [option_map proj1_sig Option.bind] in *.
1572 clear pf.
1573 edestruct M'.mapi_o_ex as [? [HY H]]; rewrite H.
1574 destruct (M.find x m) eqn:H'; rewrite ?find'_None; cbn [option_map] in *.
1575 1: specialize_under_binders_by eassumption.
1576 1: do 2 (let t := open_constr:(_) in evar (v : t); specialize (IH v); subst v).
1577 1: destruct IH as [? [? IH]].
1578 1: rewrite IH.
1579 all: clear IH H H'.
1580 all: eexists; rewrite E'.eq_alt in *; repeat constructor; try eassumption; reflexivity.
1581 Qed.
1582 Lemma map_o : forall (elt elt':Type)(m: t elt)(x:key)(f:elt->elt'), find x (map f m) = option_map f (find x m).
1583 Proof using Type.
1584 cbv [map].
1585 intros.
1586 edestruct mapi_o_ex as [? [? H]].
1587 rewrite H; reflexivity.
1588 Qed.
1589 Lemma map2'_o
1590 : forall (elt elt' elt'':Type)(m: t' elt)(m': t' elt')
1591 (x:key)(f:option elt->option elt'->option elt''),
1592 find' x (map2' f m m') = match find' x m, find' x m' with
1593 | None, None => None
1594 | x, y => f x y
1595 end.
1596 Proof using Type.
1597 intros elt elt' elt'' m m' x f.
1598 revert m m'.
1599 induction x as [|x xs IH].
1600 { cbv [map2' find' Option.bind]; intros.
1601 repeat first [ t_destr_conj_step
1602 | reflexivity
1603 | break_innermost_match_step
1604 | break_innermost_match_hyps_step
1605 | progress break_match_hyps
1606 | progress autounfold with trie_db in *
1607 | progress inversion_Node
1608 | match goal with
1609 | [ |- context[t_case _ ?v] ]
1610 => is_var v; induction v using (t_case (elt:=_));
1611 autorewrite with trie_db in *
1612 end
1613 | progress autorewrite with trie_db in * ]. }
1614 intros [m|] [m'|]; try reflexivity;
1615 try induction m as [d [m|] pf] using (t_case (elt:=elt));
1616 try induction m' as [d' [m'|] pf'] using (t_case (elt:=elt'));
1617 first [ specialize (IH (M.find x m)) | specialize (IH None) ];
1618 first [ specialize (IH (M.find x m')) | specialize (IH None) ].
1619 all: rewrite ?find'_None in *.
1620 all: cbn [find']; cbv [map2' Option.bind] in *.
1621 all: repeat first [ reflexivity
1622 | progress repeat (autounfold with trie_db in *; autorewrite with trie_db in * )
1623 | progress break_match
1624 | match goal with
1625 | [ |- context[t_case _ ?v] ]
1626 => is_var v; induction v using (t_case (elt:=_));
1627 autorewrite with trie_db in *
1628 end ].
1629 all: repeat first [ t_destr_conj_step
1630 | reflexivity
1631 | progress inversion_Node
1632 | match goal with
1633 | [ H : M.is_empty ?m = true |- _ ]
1634 => pose proof (M.is_empty_2 H : M.Empty m);
1635 clear H
1636 | [ H : M.Empty (M.map2 _ _ _) |- _ ]
1637 => cbv [M_Empty not] in H;
1638 setoid_rewrite M'.find_mapsto_iff in H;
1639 setoid_rewrite M'.map2_o in H;
1640 specialize (H x)
1641 | [ H : M.Empty (map_xmap2_l _ _) |- _ ]
1642 => cbv [M_Empty not] in H;
1643 specialize (H x);
1644 setoid_rewrite M'.find_mapsto_iff in H;
1645 rewrite map_xgmap2_l in H by reflexivity
1646 | [ H : M.Empty (map_xmap2_r _ _) |- _ ]
1647 => cbv [M_Empty not] in H;
1648 specialize (H x);
1649 setoid_rewrite M'.find_mapsto_iff in H;
1650 rewrite map_xgmap2_r in H by reflexivity
1651 | [ H : forall e, ?x = Some e -> False |- _ ]
1652 => destruct x eqn:?; [ specialize (H _ eq_refl); exfalso; exact H | clear H ]
1653 end
1654 | rewrite @M'_map2_o
1655 | rewrite map_xgmap2_l by reflexivity
1656 | rewrite map_xgmap2_r by reflexivity
1657 | rewrite find'_None in *
1658 | break_innermost_match_step
1659 | break_innermost_match_hyps_step
1660 | progress break_match_hyps
1661 | congruence ].
1662 Qed.
1663
1664 Lemma map_1 : forall (elt elt':Type)(m: t elt)(x:key)(e:elt)(f:elt->elt'),
1665 MapsTo x e m -> MapsTo x (f e) (map f m).
1666 Proof using Type. intros *; rewrite !find_iff, map_o; cbv [option_map]; break_innermost_match; spec_t. Qed.
1667 Lemma map_2 : forall (elt elt':Type)(m: t elt)(x:key)(f:elt->elt'),
1668 In x (map f m) -> In x m.
1669 Proof using Type. intros *; cbv [In]; setoid_rewrite find_iff; setoid_rewrite map_o; cbv [option_map]; break_innermost_match; spec_t. Qed.
1670 Lemma mapi_1 (elt elt':Type)(m: t elt)(x:key)(e:elt)
1671 (f:key->elt->elt')
1672 : MapsTo x e m ->
1673 exists y, E.eq y x /\ MapsTo x (f y e) (mapi f m).
1674 Proof using Type.
1675 setoid_rewrite find_iff.
1676 edestruct mapi_o_ex as [?[? H]]; rewrite H.
1677 intros ->.
1678 repeat esplit; eassumption.
1679 Qed.
1680 Lemma mapi_2
1681 : forall (elt elt':Type)(m: t elt)(x:key)
1682 (f:key->elt->elt'), In x (mapi f m) -> In x m.
1683 Proof using Type.
1684 intros *; cbv [In].
1685 setoid_rewrite find_iff.
1686 edestruct mapi_o_ex as [?[? ->]].
1687 destruct find; cbn; intros; repeat t_destr_step; eauto.
1688 Qed.
1689 Lemma map2_1
1690 : forall (elt elt' elt'':Type)(m: t elt)(m': t elt')
1691 (x:key)(f:option elt->option elt'->option elt''),
1692 In x m \/ In x m' ->
1693 find x (map2 f m m') = f (find x m) (find x m').
1694 Proof using Type.
1695 cbv [In]; intros *.
1696 setoid_rewrite find'_iff.
1697 cbv [find map2 proj1_sig]; intros *.
1698 destruct m as [m _], m' as [m' _].
1699 rewrite !map2'_o.
1700 break_innermost_match; try reflexivity.
1701 firstorder congruence.
1702 Qed.
1703 Lemma map2_2
1704 : forall (elt elt' elt'':Type)(m: t elt)(m': t elt')
1705 (x:key)(f:option elt->option elt'->option elt''),
1706 In x (map2 f m m') -> In x m \/ In x m'.
1707 Proof using Type.
1708 cbv [In]; intros *.
1709 setoid_rewrite find'_iff.
1710 cbv [find map2 proj1_sig]; intros *.
1711 destruct m as [m _], m' as [m' _].
1712 rewrite !map2'_o.
1713 break_innermost_match; firstorder; inversion_option; eauto.
1714 Qed.
1715
1716 Section Spec_equal.
1717 Variable elt:Type.
1718 Variable m m' : t elt.
1719 Variable cmp : elt -> elt -> bool.
1720
1721 Let equal_iff : equal cmp m m' = true <-> Equivb cmp m m'.
1722 Proof.
1723 clear; cbv [equal Equivb] in *.
1724 cbv [Equiv In Cmp].
1725 setoid_rewrite find_iff.
1726 rewrite fold_1, <- fold_left_rev_right, <- fold_right_map, fold_right_andb_true_map_iff.
1727 setoid_rewrite <- in_rev.
1728 repeat split; intros; repeat t_destr_step.
1729 all: lazymatch goal with
1730 | [ |- exists e, ?x = Some e ] => destruct x eqn:?; eauto
1731 | _ => idtac
1732 end.
1733 all: lazymatch goal with
1734 | [ H : forall i, List.In i ?ls -> snd i = true |- _ ]
1735 => assert (forall i, InA (@eq_key_elt _) i ls -> snd i = true);
1736 [ let i := fresh "i" in
1737 intro i; rewrite InA_alt; cbv [eq_key_elt snd]; intros;
1738 specialize (fun i0 => H (i0, snd i));
1739 repeat t_destr_conj_step;
1740 break_innermost_match_hyps; subst;
1741 cbn [snd] in *;
1742 eapply H; eassumption
1743 | clear H ]
1744 | [ H : List.In ?i ?ls |- _ ]
1745 => assert (InA (@eq_key_elt _) i ls);
1746 [ let i := fresh "i" in
1747 rewrite InA_alt; cbv [eq_key_elt];
1748 eexists; repeat esplit; try eassumption; reflexivity
1749 | clear H ]
1750 end.
1751 all: specialize_under_binders_by (apply elements_1, find_2; setoid_rewrite map2'_o).
1752 all: destruct_head' t; cbv [proj1_sig find] in *.
1753 all: repeat match goal with
1754 | [ H : _ = Some _ |- _ ]
1755 => progress specialize_under_binders_by rewrite H
1756 | [ H : _ = None |- _ ]
1757 => progress specialize_under_binders_by rewrite H
1758 end.
1759 all: specialize_under_binders_by reflexivity.
1760 all: cbn [snd] in *.
1761 all: try congruence.
1762 all: destruct_head' prod.
1763 all: specialize_all_ways.
1764 all: split_iff.
1765 all: lazymatch goal with
1766 | [ H : InA _ _ (elements (map2 _ _ _)) |- _ ]
1767 => apply elements_2, find_1 in H;
1768 setoid_rewrite map2'_o in H
1769 end.
1770 all: cbv [proj1_sig] in *.
1771 all: break_innermost_match_hyps; specialize_under_binders_by eapply ex_intro.
1772 all: specialize_under_binders_by reflexivity.
1773 all: repeat t_destr_conj_step.
1774 all: specialize_under_binders_by eassumption.
1775 all: eassumption.
1776 Qed.
1777 Lemma equal_1 : Equivb cmp m m' -> equal cmp m m' = true.
1778 Proof using Type. apply equal_iff. Qed.
1779 Lemma equal_2 : equal cmp m m' = true -> Equivb cmp m m'.
1780 Proof using Type. apply equal_iff. Qed.
1781 End Spec_equal.
1782
1783 Section ord.
1784 Class args_ord := {
1785 Y_lt : relation Y_t
1786 ; E_lt : relation E_t
1787 ; P_lt : relation (list Y_t)
1788 ; Y'_lt_compat : Proper (Y_eq ==> Y_eq ==> iff) Y_lt
1789 ; P'_lt_compat : Proper (eqlistA Y_eq ==> eqlistA Y_eq ==> iff) P_lt
1790 ; Y'_lt_trans : Transitive Y_lt
1791 ; P'_lt_trans : Transitive P_lt
1792 ; ECompat_lt_alt : forall x y, E_lt x y <-> P_lt x y
1793 ; M_lt_key elt := fun (p p':Y_t*elt) => Y_lt (fst p) (fst p')
1794 ; M_lt_key_Transitive : forall elt, Transitive (@M_lt_key elt)
1795 ; M_elements_3 : forall elt m, sort (@M_lt_key elt) (M_elements m)
1796 ; P_lt_nil_cons : forall x xs, P_lt nil (cons x xs)
1797 ; P_lt_cons_cons : forall x xs y ys, P_lt (cons x xs) (cons y ys) <-> (Y_lt x y \/ (Y_eq x y /\ P_lt xs ys))
1798 }.
1799 Local Notation "'Y.lt'" := Y_lt.
1800 Local Notation "'E.lt'" := E_lt.
1801 Local Notation "'P.lt'" := P_lt.
1802 Local Notation "'Y'.lt_compat'" := Y'_lt_compat.
1803 Local Notation "'P'.lt_compat'" := P'_lt_compat.
1804 Local Notation "'Y'.lt_trans'" := Y'_lt_trans.
1805 Local Notation "'P'.lt_trans'" := P'_lt_trans.
1806 Local Notation "'ECompat.lt_alt'" := ECompat_lt_alt.
1807 Local Notation "'M.lt_key'" := M_lt_key.
1808 Local Notation "'M.elements_3'" := M_elements_3.
1809 Context {args_ord : args_ord}.
1810 Local Existing Instances Y'_lt_compat P'_lt_compat | 100.
1811 Local Existing Instances Y'_lt_trans P'_lt_trans M_lt_key_Transitive | 10.
1812 Local Hint Resolve P_lt_nil_cons : core.
1813 Section elt.
1814 Variable elt:Type.
1815 Definition lt_key (p p':key*elt) := E.lt (fst p) (fst p').
1816
1817 Lemma elements_3 m : sort lt_key (elements m).
1818 Proof using Type.
1819 pose proof Y'.lt_compat.
1820 pose proof P'.lt_compat.
1821 destruct m as [[m|] pfm]; [ | constructor ].
1822 cbv [elements proj1_sig].
1823 clear pfm.
1824 induction m as [d m pf IH] using (t_ind (elt:=elt)).
1825 rewrite elements'_alt.
1826 repeat (autounfold with trie_db; autorewrite with trie_db).
1827 destruct m as [m|]; [ | now break_innermost_match; cbn; repeat constructor ].
1828 eapply @SortA_app with (eqA:=@eq_key_elt _); try exact _.
1829 all: try solve [ break_innermost_match; repeat constructor ].
1830 all: try solve [
1831 intros *; rewrite !InA_alt; cbv [eq_key_elt]; break_innermost_match;
1832 cbn [List.In]; intros;
1833 destruct_head' prod;
1834 repeat t_destr_step;
1835 cbv [lt_key]; cbn;
1836 rewrite @E'_eq_alt, @ECompat_lt_alt in *;
1837 repeat t_destr_step; cbn; auto ].
1838 all: [ > ].
1839 cbn [proj1_sig] in *; clear pf d.
1840 eapply @SortA_flat_map with (eqA:=@eq_key _) (eqB:=eq); try eassumption; try eapply M.elements_3; try rewrite Forall_map, Forall_forall; try exact _.
1841 all: [ > | ].
1842 all: [ >
1843 | cbv [M_lt_key lt_key eq_key]; setoid_rewrite InA_alt; intros;
1844 destruct_head' prod;
1845 repeat t_destr_step;
1846 rewrite @E'_eq_alt, @ECompat_lt_alt in *;
1847 repeat t_destr_step; cbn; rewrite P_lt_cons_cons;
1848 repeat match goal with
1849 | [ H : List.In _ _ |- _ ] => clear H
1850 end;
1851 setoid_subst_rel Y.eq;
1852 setoid_subst_rel (eqlistA Y.eq);
1853 now eauto ].
1854 match goal with
1855 | [ |- context[Sorted] ]
1856 => intros; break_innermost_match; rewrite Sorted_map_iff
1857 end.
1858 eassert (H' : InA _ _ (M.elements m));
1859 [ rewrite InA_alt
1860 | eapply M.elements_2, M.find_1 in H' ];
1861 [ repeat esplit; try eassumption; reflexivity
1862 | ].
1863 specialize (IH _ _ H').
1864 eapply Sorted_Proper_impl; [ | | eapply IH ]; [ | reflexivity ];
1865 repeat intro; break_innermost_match; cbv [lt_key eq_key_elt] in *; rewrite ECompat.lt_alt; cbn [fst snd] in *;
1866 rewrite P_lt_cons_cons;
1867 destruct_head'_and; subst;
1868 rewrite @E'_eq_alt, @ECompat_lt_alt in *.
1869 right; split; try reflexivity.
1870 repeat match goal with
1871 | [ H : M.find _ _ = _ |- _ ] => clear H
1872 | [ H : List.In _ _ |- _ ] => clear H
1873 | [ H : Sorted _ _ |- _ ] => clear H
1874 | [ H : ~M.Empty _ |- _ ] => clear H
1875 end.
1876 setoid_subst_rel (eqlistA Y.eq).
1877 assumption.
1878 Qed.
1879 End elt.
1880 End ord.
1881 End __.
1882 End ListWSfun_gen_proofs.
1883
1884 Module ListWSfun_gen (Y : DecidableTypeOrig) (M : WSfun Y) (Import T : Trie Y M).
1885 Module Type ESig := ListTyp Y <+ HasEq <+ IsEqOrig <+ HasEqDec.
1886 Module Type ESigCompat (E : ESig).
1887 Axiom eq_alt : forall x y, E.eq x y <-> eqlistA Y.eq x y.
1888 End ESigCompat.
1889 Module Y' <: DecidableTypeBoth := Y <+ UpdateEq.
1890 Global Remove Hints Y'.eq_refl Y'.eq_sym Y'.eq_trans : core.
1891 Module M' := WFacts_fun Y M <+ WFacts_fun_RemoveHints Y M <+ WAdditionalFacts_fun Y M.
1892 Local Existing Instances
1893 M'.eq_key_equiv M'.eq_key_elt_equiv M'.Equal_Equivalence
1894 | 10.
1895 Local Existing Instances
1896 M'.Proper_eq_key_elt_iff M'.Proper_eq_key_elt_impl M'.Proper_eq_key_elt_flip_impl M'.Proper_eq_key_elt_iff' M'.Proper_eq_key_elt_impl' M'.Proper_eq_key_elt_flip_impl' M'.Proper_eq_key_iff M'.Proper_eq_key_impl M'.Proper_eq_key_flip_impl M'.find_Proper_eq M'.MapsTo_Proper_eq_iff M'.In_compat M'.Proper_Equiv_eq_impl M'.Proper_Equiv_eq_flip_impl M'.Proper_Equiv_eq_iff M'.Proper_Equiv_eq_impl_pointwise M'.Proper_Equiv_eq_flip_impl_pointwise M'.Proper_Equiv_eq_iff_pointwise
1897 .
1898 Module ListWSfun_gen (E : ESig) (ECompat : ESigCompat E) <: WSfun E.
1899 Module E' <: DecidableTypeBoth := E <+ UpdateEq <+ ECompat.
1900 Global Remove Hints E'.eq_refl E'.eq_sym E'.eq_trans : core.
1901 Local Hint Resolve Y'.eq_refl Y'.eq_sym Y'.eq_trans
1902 E'.eq_refl E'.eq_sym E'.eq_trans
1903 : core.
1904
1905 Import ListWSfun_gen_proofs.
1906 Definition _argsv : args.
1907 Proof.
1908 eapply (@Build_args T.t Y.t M.t Y.eq E.eq); try exact _.
1909 all: first [ exact E.eq_dec
1910 | exact E'.eq_alt
1911 | exact M.MapsTo_1
1912 | exact M.mem_1
1913 | exact M.mem_2
1914 | exact M.empty_1
1915 | exact M.is_empty_1
1916 | exact M.is_empty_2
1917 | exact M.add_1
1918 | exact M.add_2
1919 | exact M.add_3
1920 | exact M.remove_1
1921 | exact M.remove_2
1922 | exact M.remove_3
1923 | exact M.find_1
1924 | exact M.find_2
1925 | exact M.elements_1
1926 | exact M.elements_2
1927 | exact M.elements_3w
1928 | exact M.cardinal_1
1929 | exact M.fold_1
1930 | exact M.Equal
1931 | exact M.Equiv
1932 | exact M.Equivb
1933 | exact M.equal_1
1934 | exact M.equal_2
1935 | exact M.map_1
1936 | exact M.map_2
1937 | exact M.mapi_1
1938 | exact M.mapi_2
1939 | exact M.map2_1
1940 | exact M.map2_2
1941 | exact M'.find_mapsto_iff
1942 | exact M'.add_o
1943 | exact M'.remove_o
1944 | exact M'.mapi_o_ex
1945 | exact M'.mapi_o_ex_impl
1946 | exact M'.map2_o
1947 | exact M'.is_empty_iff
1948 | exact M'.find_empty
1949 | exact M'.forall_In_elements_snd_iff
1950 | exact M'.map_o
1951 | exact M'.empty_1' ].
1952 Defined.
1953 Local Hint Extern 0 args => (let v := (eval cbv [_argsv] in _argsv) in exact v) : typeclass_instances.
1954
1955 Definition key := Eval cbv in key.
1956 Definition t := Eval cbv in t.
1957 Definition empty := Eval cbv in empty.
1958 Definition is_empty elt (m : t elt) := Eval cbv -[is_None proj1_sig] in is_empty m.
1959 Definition add := add.
1960 Definition find := find.
1961 Definition remove := remove.
1962 Definition mem := mem.
1963 Definition mapi := mapi.
1964 Definition map := map.
1965 Definition map2 := map2.
1966 Definition fold := fold.
1967 Definition elements := elements.
1968 Definition cardinal := cardinal.
1969 Definition equal := equal.
1970 Definition MapsTo := MapsTo.
1971 Definition In := In.
1972 Definition Empty := Empty.
1973 Definition eq_key := eq_key.
1974 Definition eq_key_elt := eq_key_elt.
1975 Definition MapsTo_1 := MapsTo_1.
1976 Definition mem_1 := mem_1.
1977 Definition mem_2 := mem_2.
1978 Definition empty_1 := empty_1.
1979 Definition is_empty_1 := is_empty_1.
1980 Definition is_empty_2 := is_empty_2.
1981 Definition add_1 := add_1.
1982 Definition add_2 := add_2.
1983 Definition add_3 := add_3.
1984 Definition remove_1 := remove_1.
1985 Definition remove_2 := remove_2.
1986 Definition remove_3 := remove_3.
1987 Definition find_1 := find_1.
1988 Definition find_2 := find_2.
1989 Definition elements_1 := elements_1.
1990 Definition elements_2 := elements_2.
1991 Definition elements_3w := elements_3w.
1992 Definition cardinal_1 := cardinal_1.
1993 Definition fold_1 := fold_1.
1994 Definition Equal := Equal.
1995 Definition Equiv := Equiv.
1996 Definition Equivb := Equivb.
1997 Definition equal_1 := equal_1.
1998 Definition equal_2 := equal_2.
1999 Definition map_1 := map_1.
2000 Definition map_2 := map_2.
2001 Definition mapi_1 := mapi_1.
2002 Definition mapi_2 := mapi_2.
2003 Definition map2_1 := map2_1.
2004 Definition map2_2 := map2_2.
2005 End ListWSfun_gen.
2006 End ListWSfun_gen.
2007
2008 Module ListWSfun (Y : DecidableTypeOrig) (M : WSfun Y) (Import T : Trie Y M).
2009 Module _ListWSfun.
2010 Module Outer := ListWSfun_gen Y M T.
2011 Module E := ListDecidableTypeOrig Y.
2012 Module ECompat <: Outer.ESigCompat E.
2013 Lemma eq_alt : forall x y, E.eq x y <-> eqlistA Y.eq x y.
2014 Proof. cbv [E.eq]; reflexivity. Qed.
2015 End ECompat.
2016 Module Inner <: WSfun E := Outer.ListWSfun_gen E ECompat.
2017 End _ListWSfun.
2018 Include _ListWSfun.Inner.
2019 End ListWSfun.
2020
2021 Module ListUsualWeakMap (M : UsualWS) (Import T : Trie M.E M) <: UsualWS.
2022 Module Outer := ListWSfun_gen M.E M T.
2023 Module E := ListUsualDecidableTypeOrig M.E.
2024 Module ECompat <: Outer.ESigCompat E.
2025 Lemma eq_alt : forall x y, E.eq x y <-> eqlistA M.E.eq x y.
2026 Proof. cbv; intros; rewrite eqlistA_eq_iff; intuition. Qed.
2027 End ECompat.
2028 Module Inner <: WSfun E := Outer.ListWSfun_gen E ECompat.
2029 Include Inner.
2030 End ListUsualWeakMap.
2031
2032 Module ListWeakMap (M : WS) (T : Trie M.E M) <: WS.
2033 Include ListWSfun M.E M T.
2034 Module E := _ListWSfun.E.
2035 End ListWeakMap.
2036
2037 Module ListSfun_gen (Y : OrderedTypeOrig) (M : Sfun Y) (Import T : Trie Y M).
2038 Module ListWSfun := ListWSfun_gen Y M T.
2039 Module P := ListHasLt Y.
2040 Module Type ESig := ListWSfun.ESig <+ HasLt <+ HasMiniOrderedType.
2041 Module Type ESigCompat (E : ESig).
2042 Include ListWSfun.ESigCompat E.
2043 Axiom lt_alt : forall x y, E.lt x y <-> P.lt x y.
2044 End ESigCompat.
2045 Module M' := OrdAdditionalFacts_fun Y M.
2046 Module ListSfun_gen (E : ESig) (ECompat : ESigCompat E) <: Sfun E.
2047 Include ListWSfun.ListWSfun_gen E ECompat.
2048 Module Y' := Y <+ UpdateEq <+ UpdateStrOrder.
2049 Global Remove Hints Y'.eq_refl Y'.eq_sym Y'.eq_trans : core.
2050 Module P' := ListStrOrder Y'.
2051
2052 Import ListWSfun_gen_proofs.
2053 Local Hint Extern 0 args => (let v := (eval cbv [_argsv] in _argsv) in exact v) : typeclass_instances.
2054 Definition _args_ordv : args_ord.
2055 Proof.
2056 let c := constr:(@Build_args_ord _ Y.lt E.lt P.lt) in
2057 eapply c; try exact _.
2058 all: first [ exact ECompat.lt_alt
2059 | exact M.elements_3
2060 | cbn; intros; first [ exact I | reflexivity ] ].
2061 Defined.
2062 Local Hint Extern 0 args_ord => (let v := (eval cbv [_args_ordv] in _args_ordv) in exact v) : typeclass_instances.
2063
2064 Definition lt_key elt (p p':key*elt) := Eval cbv -[fst] in lt_key p p'.
2065 Definition elements_3 := elements_3.
2066 End ListSfun_gen.
2067 End ListSfun_gen.
2068
2069 Module ListSfun (Y : OrderedTypeOrig) (M : Sfun Y) (T : Trie Y M).
2070 Module _ListSfun.
2071 Module Outer := ListSfun_gen Y M T.
2072 Module E := ListOrderedTypeOrig Y.
2073 Module ECompat <: Outer.ESigCompat E.
2074 Lemma eq_alt : forall x y, E.eq x y <-> eqlistA Y.eq x y.
2075 Proof. cbv [E.eq]; reflexivity. Qed.
2076 Lemma lt_alt : forall x y, E.lt x y <-> E.lt x y.
2077 Proof. cbv [E.lt]; reflexivity. Qed.
2078 End ECompat.
2079 Module Inner <: Sfun E := Outer.ListSfun_gen E ECompat.
2080 End _ListSfun.
2081 Include _ListSfun.Inner.
2082 End ListSfun.
2083
2084 Module ListUsualMap (M : UsualS) (T : Trie M.E M) <: UsualS.
2085 Module Outer := ListSfun_gen M.E M T.
2086 Module E := ListUsualOrderedTypeOrig M.E.
2087 Module ECompat <: Outer.ESigCompat E.
2088 Lemma eq_alt : forall x y, E.eq x y <-> eqlistA M.E.eq x y.
2089 Proof. cbv; intros; rewrite eqlistA_eq_iff; intuition. Qed.
2090 Lemma lt_alt : forall x y, E.lt x y <-> E.lt x y.
2091 Proof. cbv [E.lt]; reflexivity. Qed.
2092 End ECompat.
2093 Module Inner <: Sfun E := Outer.ListSfun_gen E ECompat.
2094 Include Inner.
2095 End ListUsualMap.
2096
2097 Module ListMap (M : S) (T : Trie M.E M) <: S.
2098 Include ListSfun M.E M T.
2099 Module E := _ListSfun.E.
2100 End ListMap.
2101
2102 (* TODO:
2103 Module IsoSord (S' : Sord) (Data' : IsoMiniOrderedType S'.Data) (E : IsoMiniOrderedType S'.MapS.E) (S'_iso : IsoMiniOrderedType S') <: Sord <: IsoOrderedType S'.
2104 Module Data <: IsoOrderedType S'.Data := Data' <+ OT_of_MOT.
2105 Module MapS <: S := IsoS S'.MapS E.
2106 Import MapS.
2107
2108 Definition t := MapS.t Data.t.
2109
2110 Definition eq : t -> t -> Prop.
2111 refine (_ S'.eq).
2112 compute.
2113 pose MapS.E.
2114 Parameter lt : t -> t -> Prop.
2115
2116 Axiom eq_refl : forall m : t, eq m m.
2117 Axiom eq_sym : forall m1 m2 : t, eq m1 m2 -> eq m2 m1.
2118 Axiom eq_trans : forall m1 m2 m3 : t, eq m1 m2 -> eq m2 m3 -> eq m1 m3.
2119 Axiom lt_trans : forall m1 m2 m3 : t, lt m1 m2 -> lt m2 m3 -> lt m1 m3.
2120 Axiom lt_not_eq : forall m1 m2 : t, lt m1 m2 -> ~ eq m1 m2.
2121
2122 Definition cmp e e' := match Data.compare e e' with EQ _ => true | _ => false end.
2123
2124 Parameter eq_1 : forall m m', Equivb cmp m m' -> eq m m'.
2125 Parameter eq_2 : forall m m', eq m m' -> Equivb cmp m m'.
2126
2127 Parameter compare : forall m1 m2, Compare lt eq m1 m2.
2128 (** Total ordering between maps. [Data.compare] is a total ordering
2129 used to compare data associated with equal keys in the two maps. *)
2130
2131
2132 Include S.
2133 Include OT_of_MOT.
2134 End IsoSord.
2135 Definition t := MapS.t Data.t.
2136
2137 Print IsoMiniOrderedType.
2138 Definition eq : t -> t -> Prop := S'.eq.
2139 Parameter lt : t -> t -> Prop.
2140
2141 Axiom eq_refl : forall m : t, eq m m.
2142 Axiom eq_sym : forall m1 m2 : t, eq m1 m2 -> eq m2 m1.
2143 Axiom eq_trans : forall m1 m2 m3 : t, eq m1 m2 -> eq m2 m3 -> eq m1 m3.
2144 Axiom lt_trans : forall m1 m2 m3 : t, lt m1 m2 -> lt m2 m3 -> lt m1 m3.
2145 Axiom lt_not_eq : forall m1 m2 : t, lt m1 m2 -> ~ eq m1 m2.
2146
2147 Definition cmp e e' := match Data.compare e e' with EQ _ => true | _ => false end.
2148
2149 Parameter eq_1 : forall m m', Equivb cmp m m' -> eq m m'.
2150 Parameter eq_2 : forall m m', eq m m' -> Equivb cmp m m'.
2151
2152 Parameter compare : forall m1 m2, Compare lt eq m1 m2.
2153 (** Total ordering between maps. [Data.compare] is a total ordering
2154 used to compare data associated with equal keys in the two maps. *)
2155
2156 End Sord.
2157
2158 *)