Debian Codegraph @

src/Util/FSets/FMapTrie/Shape.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 RelationPairs.
7 Require Import Crypto.Util.Option.
8 Require Import Crypto.Util.Prod.
9 Require Import Crypto.Util.ListUtil.
10 Require Import Crypto.Util.ListUtil.SetoidList.
11 Require Import Crypto.Util.ListUtil.SetoidListFlatMap.
12 Require Import Crypto.Util.ListUtil.StdlibCompat.
13 Require Import Crypto.Util.Tactics.SpecializeBy.
14 Require Import Crypto.Util.Tactics.DestructHead.
15 Require Import Crypto.Util.Tactics.SpecializeUnderBindersBy.
16
17 Local Set Implicit Arguments.
18 Local Set Primitive Projections.
19 Local Unset Strict Implicit.
20 (* TODO: move to global settings *)
21 Local Set Keyed Unification.
22 Import ListNotations.
23 Local Open Scope option_scope.
24 Local Open Scope bool_scope.
25 Local Open Scope lazy_bool_scope.
26 Local Open Scope type_scope.
27 Local Open Scope list_scope.
28
29 Module Type TypFunctor.
30 Parameter t : Type -> Type.
31 End TypFunctor.
32
33 Module Import EverythingGen.
34 Polymorphic Definition id_fix {T} (x : T) := x.
35 Polymorphic Definition id_fix_2 {T} (x : T) := x.
36 Polymorphic Definition id_case {T} (x : T) := x.
37 Section __.
38 Context (t : Type -> Type)
39 (map_key : Type)
40 (map_t : Type -> Type)
41 (map_empty : forall elt, map_t elt)
42 (map_is_empty : forall elt, map_t elt -> bool)
43 (map_fold : forall elt A : Type, (map_key -> elt -> A -> A) -> map_t elt -> A -> A)
44 (map_mapi : forall elt elt' : Type, (map_key -> elt -> elt') -> map_t elt -> map_t elt')
45 (map_map2 : forall elt elt' elt'' : Type, (option elt -> option elt' -> option elt'') -> map_t elt -> map_t elt' -> map_t elt'')
46 (map_find : forall elt : Type, map_key -> map_t elt -> option elt).
47
48 Local Notation t' elt := (option (t elt)).
49 Local Notation node_NonEmpty x y := match x, y with None, None => False | _, _ => True end.
50 Local Notation node_NonEmpty' x y := match y, x with None, None => False | _, _ => True end.
51 Local Notation map_t_t elt := (map_t (t elt)).
52
53 Section step.
54 Context (Node : forall elt (d : option elt) (m : option (map_t_t elt)), node_NonEmpty d m -> t elt)
55 (t_case_nd : forall (elt : Type) (P : Type),
56 (forall (v : option elt) (m : option (map_t_t elt)), node_NonEmpty v m -> P)
57 -> t elt -> P)
58 (map_xmap2_l : forall elt elt' elt'', (option elt -> option elt' -> option elt'') -> map_t elt -> map_t elt'')
59 (map_xmap2_r : forall elt elt' elt'', (option elt -> option elt' -> option elt'') -> map_t elt' -> map_t elt'').
60
61 Local Arguments Node {_} _ _ _.
62
63 Definition empty' : forall elt, t' elt
64 := fun _ => None.
65
66 Definition mapi_step elt elt'
67 (mapi : (list map_key -> elt -> elt') -> t elt -> t elt')
68 (f : list map_key -> elt -> elt')
69 : t elt -> t elt'
70 := let d_of d := option_map (f nil) d in
71 let m_of m
72 := option_map
73 (@map_mapi _ _ (fun k => mapi (fun ks => f (k :: ks))))
74 m in
75 t_case_nd
76 (fun d m pf
77 => Node
78 (d_of d) (m_of m)
79 (match d, m return node_NonEmpty d m -> node_NonEmpty (d_of d) (m_of m) with
80 | None, None => fun pf => pf
81 | _, _ => fun 'I => I
82 end pf)).
83
84 Local Notation dec_empty m := (if map_is_empty m then None else Some m).
85
86 Definition map2_step
87 elt elt' elt'' (f : option elt -> option elt' -> option elt'')
88 (map2_l : t elt -> t' elt'')
89 (map2_r : t elt' -> t' elt'')
90 (map2 : t elt -> t elt' -> t' elt'')
91 (m1 : t elt) (m2 : t elt')
92 : t' elt''
93 := let F := (fun m1 m2
94 => match m1, m2 with
95 | Some m1, Some m2 => map2 m1 m2
96 | Some m1, None => map2_l m1
97 | None, Some m2 => map2_r m2
98 | None, None => None
99 end) in
100 let f' x y := match x, y with
101 | None, None => None
102 | x, y => f x y
103 end in
104 t_case_nd
105 (fun d1 m1 pf1
106 => t_case_nd
107 (fun d2 m2 pf2
108 => let m := match m1, m2 with
109 | Some m1, Some m2
110 => dec_empty (@map_map2 _ _ _ F m1 m2)
111 | Some m1, None
112 => dec_empty (map_xmap2_l F m1)
113 | None, Some m2
114 => dec_empty (map_xmap2_r F m2)
115 | None, None => None
116 end in
117 match f' d1 d2, m with
118 | None, None => None
119 | d, m => Some (Node d m I)
120 end)
121 m2)
122 m1.
123 Definition map2_l_step
124 elt elt' elt'' (f : option elt -> option elt' -> option elt'')
125 (map2_l : t elt -> t' elt'')
126 (m1 : t elt)
127 : t' elt''
128 := let f' x y := match x, y with
129 | None, None => None
130 | x, y => f x y
131 end in
132 t_case_nd
133 (fun d1 m1 pf1
134 => let m := match m1 with
135 | Some m1
136 => dec_empty
137 (@map_xmap2_l
138 (t elt) (t elt') (t elt'')
139 (fun m1 _ => match m1 with
140 | None => None
141 | Some m1 => map2_l m1
142 end)
143 m1)
144 | None => None
145 end in
146 match f' d1 None, m with
147 | None, None => None
148 | d, m => Some (Node d m I)
149 end)
150 m1.
151 Definition map2_r_step
152 elt elt' elt'' (f : option elt -> option elt' -> option elt'')
153 (map2_r : t elt' -> t' elt'')
154 (m2 : t elt')
155 : t' elt''
156 := let f' x y := match x, y with
157 | None, None => None
158 | x, y => f x y
159 end in
160 t_case_nd
161 (fun d2 m2 pf2
162 => let m := match m2 with
163 | Some m2
164 => dec_empty
165 (@map_xmap2_r
166 (t elt) (t elt') (t elt'')
167 (fun _ m2 => match m2 with
168 | None => None
169 | Some m2 => map2_r m2
170 end)
171 m2)
172 | None => None
173 end in
174 match f' None d2, m with
175 | None, None => None
176 | d, m => Some (Node d m I)
177 end)
178 m2.
179 Definition fold_step elt A
180 (fold : (list map_key -> elt -> A -> A) -> t elt -> A -> A)
181 (f : list map_key -> elt -> A -> A)
182 (m : t elt)
183 (a : A)
184 : A
185 := t_case_nd
186 (fun d m pf
187 => let a := match d with
188 | None => a
189 | Some d => f [] d a
190 end in
191 match m with
192 | Some m
193 => @map_fold
194 _ _
195 (fun k => fold (fun ks => f (k :: ks)))
196 m
197 a
198 | None => a
199 end)
200 m.
201 Definition recursively_non_empty_step elt
202 (recursively_non_empty : t elt -> bool)
203 (m : t elt)
204 : bool
205 := t_case_nd
206 (fun d m pf
207 => match m with
208 | Some m
209 => @map_fold
210 _ _
211 (fun _ m P => andb P (recursively_non_empty m))
212 m
213 (negb (map_is_empty m))
214 | None => true
215 end)
216 m.
217 End step.
218
219 Class EverythingGen
220 := { Node : forall elt (d : option elt) (m : option (map_t_t elt)), node_NonEmpty d m -> t elt
221 ; t_case : forall elt (P : t elt -> Type),
222 (forall d m pf, P (@Node elt d m pf))
223 -> forall m, P m
224 ; t_case_beta : forall elt P rec d m pf, @t_case elt P rec (@Node elt d m pf) = rec d m pf
225 ; t_case_nd
226 := fun elt P => @t_case elt (fun _ => P)
227 ; t_ind : forall elt (P : t elt -> Prop),
228 (forall d m pf, (forall k v, match m with Some m => @map_find _ k m = Some v -> P v | None => True end) -> P (@Node elt d m pf))
229 -> forall m, P m
230 ; empty : forall elt, t' elt
231 := fun _ => None
232 ; mapi : forall elt elt', id_fix_2 ((list map_key -> elt -> elt') -> t elt -> t elt')
233 ; mapi_beta : forall elt elt' f m, @mapi elt elt' f m = mapi_step Node t_case_nd (@mapi elt elt') f m
234
235 ; map_xmap2_l : forall elt elt' elt'', (option elt -> option elt' -> option elt'') -> map_t elt -> map_t elt''
236 ; map_xmap2_r : forall elt elt' elt'', (option elt -> option elt' -> option elt'') -> map_t elt' -> map_t elt''
237 ; map_xmap2_lr : forall A B (f g : option A -> option A -> option B) m,
238 (forall i j, f i j = g j i) -> map_xmap2_l f m = map_xmap2_r g m
239 ; map_xgmap2_l : forall A B C (f : option A -> option B -> option C) i m,
240 f None None = None -> @map_find _ i (map_xmap2_l f m) = f (@map_find _ i m) None
241 ; map_xgmap2_r : forall A B C (f : option A -> option B -> option C) i m,
242 f None None = None -> @map_find _ i (map_xmap2_r f m) = f None (@map_find _ i m)
243
244 ; map2_l : forall elt elt' elt'', (option elt -> option elt' -> option elt'') -> id_fix (t elt -> t' elt'')
245 ; map2_r : forall elt elt' elt'', (option elt -> option elt' -> option elt'') -> id_fix (t elt' -> t' elt'')
246 ; map2 : forall elt elt' elt'', (option elt -> option elt' -> option elt'') -> id_fix (t elt -> t elt' -> t' elt'')
247 ; map2_l_beta : forall elt elt' elt'' f m,
248 @map2_l elt elt' elt'' f m = map2_l_step Node t_case_nd map_xmap2_l f (map2_l f) m
249 ; map2_r_beta : forall elt elt' elt'' f m,
250 @map2_r elt elt' elt'' f m = map2_r_step Node t_case_nd map_xmap2_r f (map2_r f) m
251 ; map2_beta : forall elt elt' elt'' f m1 m2,
252 @map2 elt elt' elt'' f m1 m2 = map2_step Node t_case_nd map_xmap2_l map_xmap2_r f (map2_l f) (map2_r f) (map2 f) m1 m2
253 ; fold : forall elt (A : Type), id_fix_2 ((list map_key -> elt -> A -> A) -> t elt -> A -> A)
254 ; fold_beta : forall elt A f m a,
255 @fold elt A f m a = fold_step t_case_nd (@fold elt A) f m a
256 ; recursively_non_empty : forall elt, id_fix (t elt -> bool)
257 ; recursively_non_empty_beta : forall elt m,
258 @recursively_non_empty elt m = recursively_non_empty_step t_case_nd (@recursively_non_empty elt) m
259 }.
260 Definition Node' {E : EverythingGen} elt (d : option elt) (m : option (map_t_t elt)) : node_NonEmpty' d m -> t elt
261 := match d, m return node_NonEmpty' d m -> _ with
262 | None, None => fun pf => match pf with end
263 | d, (Some _ | None) as m => @Node E elt d m
264 end.
265 Definition oNode_dec_empty {E : EverythingGen} elt (d : option elt) (m : map_t (t elt)) : option (t elt)
266 := match d, (if map_is_empty m then None else Some m) with
267 | None, None => None
268 | d, (None | Some _) as m
269 => Some (Node (d:=d) (m:=m) I)
270 end.
271 End __.
272 End EverythingGen.
273 Global Arguments Node {t _ _ _ _ _ _ _ _ _} d m _.
274 Global Arguments Node' {t _ _ _ _ _ _ _ _ _} d m _.
275
276 Create HintDb trie_db discriminated.
277
278 #[global]
279 Hint Rewrite
280 t_case_beta
281 mapi_beta
282 map2_beta
283 map2_l_beta
284 map2_r_beta
285 fold_beta
286 recursively_non_empty_beta
287 : trie_db.
288
289 #[global]
290 Hint Unfold
291 id_fix
292 id_fix_2
293 id_case
294 Node'
295 oNode_dec_empty
296 empty'
297 mapi_step
298 map2_step
299 map2_l_step
300 map2_r_step
301 fold_step
302 recursively_non_empty_step
303 t_case_nd
304 empty
305 : trie_db.
306
307 Module Type TrieShape (label : DecidableType) (map : FMapInterface.WSfun label) (Import T : TypFunctor).
308 Notation Everything := (@EverythingGen t map.key map.t map.is_empty map.fold map.mapi map.map2 map.find).
309 End TrieShape.
310
311 Module Type Trie (label : DecidableType) (map : FMapInterface.WSfun label).
312 Include TypFunctor.
313 Include TrieShape label map.
314 Parameter Inline everything : Everything.
315 #[global]
316 Existing Instance everything.
317 End Trie.
318
319 Module TrieTactics.
320 Ltac t_destr_conj_step :=
321 first [ progress subst
322 | progress destruct_head'_False
323 | progress destruct_head'_and
324 | progress destruct_head'_ex
325 | progress destruct_head'_True
326 | progress destruct_head'_unit
327 | progress destruct_head' sig
328 | progress inversion_option
329 | progress inversion_sigma
330 | progress inversion_list
331 | progress inversion_pair
332 | progress cbn [fst snd proj1_sig proj2_sig] in *
333 | match goal with
334 | [ H : eqlistA _ nil _ |- _ ] => inversion H; clear H
335 | [ H : eqlistA _ _ nil |- _ ] => inversion H; clear H
336 | [ H : eqlistA _ (_ :: _) _ |- _ ] => inversion H; clear H
337 | [ H : eqlistA _ _ (_ :: _) |- _ ] => inversion H; clear H
338 | [ H : InA _ _ (_ :: _) |- _ ] => inversion H; clear H
339 | [ H : InA _ _ nil |- _ ] => inversion H; clear H
340 | [ H : List.In _ (List.map _ _) |- _ ] => rewrite in_map_iff in H
341 | [ H : List.In _ (List.flat_map _ _) |- _ ] => rewrite in_flat_map in H
342 | [ H : ?x = ?x |- _ ] => clear H
343 | [ H : ?T, H' : ?T |- _ ] => clear H
344 | [ H : ?x = Some _, H' : context[?x] |- _ ] => rewrite H in H'
345 | [ H : ?x = None, H' : context[?x] |- _ ] => rewrite H in H'
346 | [ H : Some _ = ?x |- _ ] => symmetry in H
347 | [ H : None = ?x |- _ ] => symmetry in H
348 end
349 | discriminate ].
350 Ltac t_destr_step :=
351 first [ t_destr_conj_step
352 | progress destruct_head'_or
353 | progress destruct_head' option
354 | break_innermost_match_hyps_step
355 | break_innermost_match_step ].
356 Ltac t_specialize_safe_step :=
357 first [ progress specialize_by_assumption
358 | progress cbv [not] in *
359 | progress specialize_by reflexivity
360 | progress specialize_under_binders_by apply conj
361 | progress specialize_under_binders_by eapply ex_intro
362 | progress specialize_under_binders_by apply eqlistA_cons ].
363
364 Ltac inversion_Node_step :=
365 match goal with
366 | [ H : Node _ _ _ = Node _ _ _ |- _ ]
367 => let H' := fresh in
368 pose proof (f_equal (t_case_nd (fun d m pf => (d, m))) H) as H';
369 clear H;
370 cbv [t_case_nd] in *;
371 autorewrite with trie_db in H'
372 end.
373 Ltac inversion_Node := repeat inversion_Node_step.
374 End TrieTactics.