Debian Codegraph @

src/Util/FSets/FMapTrie/ShapeEx.v

raw
1 From Coq Require Import OrderedTypeEx.
2 From Coq Require Import PArith.
3 From Coq Require Import FMapPositive.
4 Require Import Crypto.Util.Option.
5 Require Import Crypto.Util.Equality.
6 Require Import Crypto.Util.Structures.OrdersEx.
7 Require Import Crypto.Util.Tactics.Head.
8 Require Import Crypto.Util.Tactics.DestructHead.
9 Require Import Crypto.Util.Tactics.BreakMatch.
10 Require Import Crypto.Util.Tactics.SetEvars.
11 Require Import Crypto.Util.FSets.FMapTrie.Shape.
12 Require Import Crypto.Util.FSets.FMapN.
13 Require Import Crypto.Util.FSets.FMapZ.
14 Import EqNotations.
15 Import EverythingGen.
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
23 Inductive option_dep A : bool -> Type :=
24 | Some_dep b : A -> option_dep b
25 | None_dep : option_dep false.
26 Arguments Some_dep {A b} _.
27 Arguments None_dep {A}.
28
29 Local Ltac t :=
30 first [ instantiate (1:=ltac:(eassumption)); reflexivity
31 | match goal with
32 | [ |- ?LHS = (fun b : ?T => @?RHS b) ]
33 => lazymatch RHS with
34 | fun '(a, b) => _
35 => is_evar LHS; instantiate (1:=ltac:(first [ intros [a b] | intros [? ?] ]));
36 try t
37 end
38 end
39 | try instantiate (1:=ltac:(intros)); progress cbv beta iota; t
40 | instantiate (1:=ltac:(intros)); cbv beta;
41 first [ reflexivity | apply f_equal | apply f_equal2 | apply f_equal3 | apply fg_equal
42 | match goal with
43 | [ |- ?LHS = (fun b : ?T => @?RHS b) ]
44 => cut (forall a : T, LHS a = RHS a); [ shelve | intro ]
45 end
46 | break_innermost_match_step ];
47 instantiate (1:=ltac:(intros)); cbv beta iota;
48 t
49 | idtac ].
50
51 Local Ltac handle_id_fix :=
52 intros;
53 cbv beta;
54 set_evars;
55 (tryif (match goal with |- context[id_fix] => idtac | |- context[id_fix_2] => idtac end)
56 then (intros;
57 let m := fresh "m" in
58 let x := fresh "x" in
59 let IH := fresh "IH" in
60 lazymatch goal with
61 | [ |- id_fix _ ] => cbv [id_fix ]; fix IH 1; intros m; revert m
62 | [ |- id_fix_2 _ ] => cbv [id_fix_2]; fix IH 2; intros x m; revert x m
63 end;
64 revert IH)
65 else idtac);
66 shelve_unifiable.
67
68 Local Ltac subst_partial_evars :=
69 repeat match goal with
70 | [ H := ?ev |- _ ]
71 => has_evar ev; assert_fails is_evar ev; revert H; set_evars; intro
72 end.
73 Local Ltac partial_unify e :=
74 let do_rep evb e m IH IHm eIHm
75 := (is_evar evb;
76 let v1 := fresh in
77 let v2 := fresh in
78 set (v1 := IHm);
79 pose eIHm as v2;
80 destruct m;
81 change v1 with v2; subst v1 v2 e) in
82 lazymatch goal with
83 | [ ev := ?evb, IH := _ : id_fix _ |- _ ]
84 => let m := lazymatch goal with |- context G[IH ?m] => m end in
85 do_rep evb e m IH (IH m) (e IH m)
86 | [ ev := ?evb, IH := _ : id_fix_2 _ |- _ ]
87 => let x := lazymatch goal with |- context[IH ?x ?m] => x end in
88 let m := lazymatch goal with |- context[IH x ?m] => m end in
89 do_rep evb e m IH (IH x m) (e IH x m)
90 | _ => idtac
91 end.
92
93 Module PositiveMapTypFunctor <: TypFunctor.
94 Local Unset Primitive Projections.
95 Local Set Nonrecursive Elimination Schemes.
96 Local Set Boolean Equality Schemes.
97 Local Set Decidable Equality Schemes.
98 Inductive t' (elt : Type) := Node' { data : option elt ; map : option_dep (PositiveMap.t (t' elt)) (Option.is_None data) }.
99 Definition t := t'.
100 Local Notation node_NonEmpty x y := match x, y with None, None => False | _, _ => True end.
101 Definition Node elt (data : option elt) (map : option (PositiveMap.t (t elt))) (pf : node_NonEmpty data map) : t elt
102 := @Node' _ data (match map, data return node_NonEmpty data map -> option_dep _ (Option.is_None data) with
103 | Some m, _ => fun _ => Some_dep m
104 | None, Some _ => fun _ => None_dep
105 | None, None => fun pf => match pf with end
106 end pf).
107 Global Arguments Node [elt] _ _ _.
108 Definition t_rect elt (P : t elt -> Type)
109 (val : forall (v : option elt) (m : option (PositiveMap.t (t elt))) (pf : node_NonEmpty v m), P (Node v m pf))
110 (m : t elt)
111 : P m
112 := Eval cbv beta in
113 match m return P m with
114 | {| data := d ; map := m |}
115 => match m in option_dep _ b, d return forall pf : is_None d = b, P {| data := d ; map := rew <- [option_dep _] pf in m |} with
116 | None_dep, None
117 => fun pf => match pf with
118 | eq_refl => I
119 end
120 | Some_dep _ m, Some d
121 => fun pf => match pf with
122 | eq_refl => val (Some d) (Some m) I
123 end
124 | None_dep as m, Some d
125 => fun pf => match pf with
126 | eq_refl => ltac:(refine (fun x => x)) (val (Some d) None I)
127 end
128 | Some_dep _ m, None
129 => fun pf => match pf with
130 | eq_refl => val None (Some m) I
131 end
132 end eq_refl
133 end.
134 End PositiveMapTypFunctor.
135
136 Module PositiveMapTrieInd <: Trie PositiveOrderedTypeBits PositiveMap.
137 Definition t := PositiveMapTypFunctor.t.
138 Include TrieShape PositiveOrderedTypeBits PositiveMap.
139
140 Definition PositiveMap_find_alt A
141 := fix find (i : PositiveMap.key) (m : PositiveMap.t A) {struct m} : option A :=
142 match m with
143 | @PositiveMap.Leaf _ => None
144 | PositiveMap.Node l o r =>
145 match i with
146 | BinNums.xI ii => find ii r
147 | BinNums.xO ii => find ii l
148 | BinNums.xH => o
149 end
150 end.
151 Lemma eq_PositiveMap_find_alt A : forall i m, @PositiveMap.find A i m = @PositiveMap_find_alt A i m.
152 Proof using Type.
153 induction i, m; cbn in *; auto.
154 Qed.
155
156 Definition t_ind_full elt (P : t elt -> Prop)
157 (H : forall d m pf,
158 (forall k v, match m with Some m => PositiveMap.find k m = Some v -> P v | None => True end)
159 -> P (PositiveMapTypFunctor.Node d m pf))
160 : forall m, P m.
161 Proof using Type.
162 fix t_ind_full 1.
163 induction m as [d m pf] using PositiveMapTypFunctor.t_rect.
164 specialize (H d m pf).
165 apply H; clear H.
166 destruct m as [m|]; [ | intros; exact I ].
167 induction m;
168 [ clear t_ind_full
169 | match goal with
170 | [ H : option _ |- _ ]
171 => let x := fresh "x" in
172 destruct H as [x|];
173 [ specialize (t_ind_full x)
174 | clear t_ind_full ]
175 end ].
176 all: intros k v H.
177 all: try solve [ exfalso; clear -H; abstract (destruct k; cbn in H; inversion_option) ].
178 all: destruct k; cbn [PositiveMap.find] in *.
179 all: lazymatch goal with
180 | [ H : None = Some _ |- _ ] => exfalso; clear -H; abstract inversion_option
181 | [ H : ?P ?x, H' : Some ?x = Some ?y |- ?P ?y ]
182 => refine (rew [P] (f_equal (fun v => Option.value v x) H') in H)
183 | _ => idtac
184 end.
185 all: eauto with nocore.
186 Defined.
187
188 Section everything.
189 Let everything' : Everything.
190 Proof.
191 unshelve esplit.
192 all: try exact PositiveMapTypFunctor.Node.
193 all: try exact PositiveMapTypFunctor.t_rect.
194 all: try exact t_ind_full.
195 all: try exact PositiveMapAdditionalFacts.xmap2_lr.
196 all: try exact PositiveMap.xgmap2_l.
197 all: try exact PositiveMap.xgmap2_r.
198 all: handle_id_fix.
199 all: subst_partial_evars.
200 all: try (destruct_head' option; destruct_head'_True; destruct_head'_False; reflexivity).
201 all: partial_unify e.
202 all: try reflexivity.
203 Defined.
204 Definition everything := Eval cbv [everything'] in everything'.
205 End everything.
206 End PositiveMapTrieInd.
207
208 Module NMapTypFunctor <: TypFunctor.
209 Local Unset Primitive Projections.
210 Local Set Nonrecursive Elimination Schemes.
211 Local Set Boolean Equality Schemes.
212 Local Set Decidable Equality Schemes.
213 Inductive t' (elt : Type) := Node' { data : option elt ; map : option_dep (NMap.t (t' elt)) (Option.is_None data) }.
214 Definition t := t'.
215 Local Notation node_NonEmpty x y := match x, y with None, None => False | _, _ => True end.
216 Definition Node elt (data : option elt) (map : option (NMap.t (t elt))) (pf : node_NonEmpty data map) : t elt
217 := @Node' _ data (match map, data return node_NonEmpty data map -> option_dep _ (Option.is_None data) with
218 | Some m, _ => fun _ => Some_dep m
219 | None, Some _ => fun _ => None_dep
220 | None, None => fun pf => match pf with end
221 end pf).
222 Global Arguments Node [elt] _ _ _.
223 Definition t_rect elt (P : t elt -> Type)
224 (val : forall (v : option elt) (m : option (NMap.t (t elt))) (pf : node_NonEmpty v m), P (Node v m pf))
225 (m : t elt)
226 : P m
227 := Eval cbv beta in
228 match m return P m with
229 | {| data := d ; map := m |}
230 => match m in option_dep _ b, d return forall pf : is_None d = b, P {| data := d ; map := rew <- [option_dep _] pf in m |} with
231 | None_dep, None
232 => fun pf => match pf with
233 | eq_refl => I
234 end
235 | Some_dep _ m, Some d
236 => fun pf => match pf with
237 | eq_refl => val (Some d) (Some m) I
238 end
239 | None_dep as m, Some d
240 => fun pf => match pf with
241 | eq_refl => ltac:(refine (fun x => x)) (val (Some d) None I)
242 end
243 | Some_dep _ m, None
244 => fun pf => match pf with
245 | eq_refl => val None (Some m) I
246 end
247 end eq_refl
248 end.
249 End NMapTypFunctor.
250
251 Module NMapTrieInd <: Trie NOrderedTypeBits NMap.
252 Definition t := NMapTypFunctor.t.
253 Include TrieShape NOrderedTypeBits NMap.
254
255 Definition NMap_find_alt
256 := ltac:(let v := (eval cbv -[PositiveMap.find fst snd] in NMap.find) in
257 lazymatch (eval pattern PositiveMap.find in v) with
258 | ?P _ => let v := (eval cbv beta in (P PositiveMapTrieInd.PositiveMap_find_alt)) in
259 exact v
260 end).
261
262 Lemma eq_NMap_find_alt A : forall i m, @NMap.find A i m = @NMap_find_alt A i m.
263 Proof using Type.
264 cbv -[PositiveMap.find PositiveMapTrieInd.PositiveMap_find_alt].
265 intros; break_innermost_match; rewrite ?PositiveMapTrieInd.eq_PositiveMap_find_alt; reflexivity.
266 Qed.
267
268 Definition t_ind_full elt (P : t elt -> Prop)
269 (H : forall d m pf,
270 (forall k v, match m with Some m => NMap.find k m = Some v -> P v | None => True end)
271 -> P (NMapTypFunctor.Node d m pf))
272 : forall m, P m.
273 Proof using Type.
274 fix t_ind_full 1.
275 induction m as [d m pf] using NMapTypFunctor.t_rect.
276 specialize (H d m pf).
277 apply H; clear H.
278 destruct m as [[[m0 m]]|]; [ | intros; exact I ].
279 intros [|k]; [ | revert k ].
280 { cbn.
281 destruct m0 as [m0|]; [ | clear; intros; inversion_option ].
282 specialize (t_ind_full m0).
283 intro v.
284 refine (fun pf => match pf with
285 | eq_refl => t_ind_full
286 end). }
287 cbv -[PositiveMap.find t].
288 induction m;
289 [ clear t_ind_full
290 | match goal with
291 | [ H : option _ |- _ ]
292 => let x := fresh "x" in
293 destruct H as [x|];
294 [ specialize (t_ind_full x)
295 | clear t_ind_full ]
296 end ].
297 all: intros k v H.
298 all: try solve [ exfalso; clear -H; abstract (destruct k; cbn in H; inversion_option) ].
299 all: destruct k; cbn [PositiveMap.find] in *.
300 all: lazymatch goal with
301 | [ H : None = Some _ |- _ ] => exfalso; clear -H; abstract inversion_option
302 | [ H : ?P ?x, H' : Some ?x = Some ?y |- ?P ?y ]
303 => refine (rew [P] (f_equal (fun v => Option.value v x) H') in H)
304 | _ => idtac
305 end.
306 all: eauto with nocore.
307 Defined.
308
309 Section map2.
310 Variable A B C : Type.
311 Variable f : option A -> option B -> option C.
312
313 Definition NMap_xmap2_l (m : NMap.t A) : NMap.t C
314 := ltac:(let v := (eval cbv -[PositiveMap.map2] in (NMap.map2 f m (@NMap.empty _))) in
315 lazymatch (eval pattern (PositiveMap.map2 f) in v) with
316 | ?P _ => let v := (eval cbv beta in (P (fun m _ => PositiveMap.xmap2_l f m))) in
317 exact v
318 end).
319
320 Definition NMap_xmap2_r (m : NMap.t B) : NMap.t C
321 := ltac:(let v := (eval cbv -[PositiveMap.map2] in (NMap.map2 f (@NMap.empty _) m)) in
322 lazymatch (eval pattern (PositiveMap.map2 f) in v) with
323 | ?P _ => let v := (eval cbv beta in (P (fun _ m => PositiveMap.xmap2_r f m))) in
324 exact v
325 end).
326
327 Lemma NMap_xgmap2_l : forall (i : NMap.key) (m : NMap.t A),
328 f None None = None -> NMap.find i (NMap_xmap2_l m) = f (NMap.find i m) None.
329 Proof using Type.
330 cbv -[PositiveMap.find PositiveMap.xmap2_l]; intros; break_innermost_match; eauto.
331 all: now rewrite PositiveMap.xgmap2_l by assumption.
332 Qed.
333
334 Lemma NMap_xgmap2_r : forall (i : NMap.key) (m : NMap.t B),
335 f None None = None -> NMap.find i (NMap_xmap2_r m) = f None (NMap.find i m).
336 Proof using Type.
337 cbv -[PositiveMap.find PositiveMap.xmap2_r]; intros; break_innermost_match; eauto.
338 all: now rewrite PositiveMap.xgmap2_r by assumption.
339 Qed.
340 End map2.
341 Lemma NMap_xmap2_lr :
342 forall (A B : Type)(f g: option A -> option A -> option B)(m : NMap.t A),
343 (forall (i j : option A), f i j = g j i) ->
344 NMap_xmap2_l f m = NMap_xmap2_r g m.
345 Proof.
346 cbv -[PositiveMap.xmap2_r PositiveMap.xmap2_l].
347 intros; break_innermost_match; repeat (f_equal; eauto using PositiveMapAdditionalFacts.xmap2_lr).
348 Qed.
349
350 Section everything.
351 Let everything' : Everything.
352 Proof.
353 unshelve esplit.
354 all: try exact NMapTypFunctor.Node.
355 all: try exact NMapTypFunctor.t_rect.
356 all: try exact t_ind_full.
357 all: try exact NMap_xmap2_lr.
358 all: try exact NMap_xgmap2_l.
359 all: try exact NMap_xgmap2_r.
360 all: handle_id_fix.
361 all: subst_partial_evars.
362 all: try (destruct_head' option; destruct_head'_True; destruct_head'_False; reflexivity).
363 all: partial_unify e.
364 all: try reflexivity.
365 Defined.
366 Definition everything := Eval cbv [everything'] in everything'.
367 End everything.
368 End NMapTrieInd.
369
370 Module ZMapTypFunctor <: TypFunctor.
371 Local Unset Primitive Projections.
372 Local Set Nonrecursive Elimination Schemes.
373 Local Set Boolean Equality Schemes.
374 Local Set Decidable Equality Schemes.
375 Inductive t' (elt : Type) := Node' { data : option elt ; map : option_dep (ZMap.t (t' elt)) (Option.is_None data) }.
376 Definition t := t'.
377 Local Notation node_NonEmpty x y := match x, y with None, None => False | _, _ => True end.
378 Definition Node elt (data : option elt) (map : option (ZMap.t (t elt))) (pf : node_NonEmpty data map) : t elt
379 := @Node' _ data (match map, data return node_NonEmpty data map -> option_dep _ (Option.is_None data) with
380 | Some m, _ => fun _ => Some_dep m
381 | None, Some _ => fun _ => None_dep
382 | None, None => fun pf => match pf with end
383 end pf).
384 Global Arguments Node [elt] _ _ _.
385 Definition t_rect elt (P : t elt -> Type)
386 (val : forall (v : option elt) (m : option (ZMap.t (t elt))) (pf : node_NonEmpty v m), P (Node v m pf))
387 (m : t elt)
388 : P m
389 := Eval cbv beta in
390 match m return P m with
391 | {| data := d ; map := m |}
392 => match m in option_dep _ b, d return forall pf : is_None d = b, P {| data := d ; map := rew <- [option_dep _] pf in m |} with
393 | None_dep, None
394 => fun pf => match pf with
395 | eq_refl => I
396 end
397 | Some_dep _ m, Some d
398 => fun pf => match pf with
399 | eq_refl => val (Some d) (Some m) I
400 end
401 | None_dep as m, Some d
402 => fun pf => match pf with
403 | eq_refl => ltac:(refine (fun x => x)) (val (Some d) None I)
404 end
405 | Some_dep _ m, None
406 => fun pf => match pf with
407 | eq_refl => val None (Some m) I
408 end
409 end eq_refl
410 end.
411 End ZMapTypFunctor.
412
413 Module ZMapTrieInd <: Trie ZOrderedTypeBits ZMap.
414 Definition t := ZMapTypFunctor.t.
415 Include TrieShape ZOrderedTypeBits ZMap.
416
417 Definition ZMap_find_alt
418 := ltac:(let v := (eval cbv -[PositiveMap.find fst snd] in ZMap.find) in
419 lazymatch (eval pattern PositiveMap.find in v) with
420 | ?P _ => let v := (eval cbv beta in (P PositiveMapTrieInd.PositiveMap_find_alt)) in
421 exact v
422 end).
423
424 Lemma eq_ZMap_find_alt A : forall i m, @ZMap.find A i m = @ZMap_find_alt A i m.
425 Proof using Type.
426 cbv -[PositiveMap.find PositiveMapTrieInd.PositiveMap_find_alt].
427 intros; break_innermost_match; rewrite ?PositiveMapTrieInd.eq_PositiveMap_find_alt; reflexivity.
428 Qed.
429
430 Definition t_ind_full elt (P : t elt -> Prop)
431 (H : forall d m pf,
432 (forall k v, match m with Some m => ZMap.find k m = Some v -> P v | None => True end)
433 -> P (ZMapTypFunctor.Node d m pf))
434 : forall m, P m.
435 Proof using Type.
436 fix t_ind_full 1.
437 induction m as [d m pf] using ZMapTypFunctor.t_rect.
438 specialize (H d m pf).
439 apply H; clear H.
440 destruct m as [[[mn [[m0 m]]]]|]; [ | intros; exact I ].
441 intros [|k|k]; [ | revert k | revert k ].
442 all: cbv -[PositiveMap.find t].
443 { destruct m0 as [m0|]; [ | clear; intros; inversion_option ].
444 specialize (t_ind_full m0).
445 intro v.
446 refine (fun pf => match pf with
447 | eq_refl => t_ind_full
448 end). }
449 all: let m := lazymatch goal with |- context[PositiveMap.find _ ?m] => m end in
450 induction m;
451 [ clear t_ind_full
452 | match goal with
453 | [ H : option _ |- _ ]
454 => let x := fresh "x" in
455 destruct H as [x|];
456 [ specialize (t_ind_full x)
457 | clear t_ind_full ]
458 end ].
459 all: intros k v H.
460 all: try solve [ exfalso; clear -H; abstract (destruct k; cbn in H; inversion_option) ].
461 all: destruct k; cbn [PositiveMap.find] in *.
462 all: lazymatch goal with
463 | [ H : None = Some _ |- _ ] => exfalso; clear -H; abstract inversion_option
464 | [ H : ?P ?x, H' : Some ?x = Some ?y |- ?P ?y ]
465 => refine (rew [P] (f_equal (fun v => Option.value v x) H') in H)
466 | _ => idtac
467 end.
468 all: eauto with nocore.
469 Defined.
470
471 Section map2.
472 Variable A B C : Type.
473 Variable f : option A -> option B -> option C.
474
475 Definition ZMap_xmap2_l (m : ZMap.t A) : ZMap.t C
476 := ltac:(let v := (eval cbv -[PositiveMap.map2] in (ZMap.map2 f m (@ZMap.empty _))) in
477 lazymatch (eval pattern (PositiveMap.map2 f) in v) with
478 | ?P _ => let v := (eval cbv beta in (P (fun m _ => PositiveMap.xmap2_l f m))) in
479 exact v
480 end).
481
482 Definition ZMap_xmap2_r (m : ZMap.t B) : ZMap.t C
483 := ltac:(let v := (eval cbv -[PositiveMap.map2] in (ZMap.map2 f (@ZMap.empty _) m)) in
484 lazymatch (eval pattern (PositiveMap.map2 f) in v) with
485 | ?P _ => let v := (eval cbv beta in (P (fun _ m => PositiveMap.xmap2_r f m))) in
486 exact v
487 end).
488
489 Lemma ZMap_xgmap2_l : forall (i : ZMap.key) (m : ZMap.t A),
490 f None None = None -> ZMap.find i (ZMap_xmap2_l m) = f (ZMap.find i m) None.
491 Proof using Type.
492 cbv -[PositiveMap.find PositiveMap.xmap2_l]; intros; break_innermost_match; eauto.
493 all: now rewrite PositiveMap.xgmap2_l by assumption.
494 Qed.
495
496 Lemma ZMap_xgmap2_r : forall (i : ZMap.key) (m : ZMap.t B),
497 f None None = None -> ZMap.find i (ZMap_xmap2_r m) = f None (ZMap.find i m).
498 Proof using Type.
499 cbv -[PositiveMap.find PositiveMap.xmap2_r]; intros; break_innermost_match; eauto.
500 all: now rewrite PositiveMap.xgmap2_r by assumption.
501 Qed.
502 End map2.
503 Lemma ZMap_xmap2_lr :
504 forall (A B : Type)(f g: option A -> option A -> option B)(m : ZMap.t A),
505 (forall (i j : option A), f i j = g j i) ->
506 ZMap_xmap2_l f m = ZMap_xmap2_r g m.
507 Proof.
508 cbv -[PositiveMap.xmap2_r PositiveMap.xmap2_l].
509 intros; break_innermost_match; repeat (f_equal; eauto using PositiveMapAdditionalFacts.xmap2_lr).
510 Qed.
511
512 Section everything.
513 Let everything' : Everything.
514 Proof.
515 unshelve esplit.
516 all: try exact ZMapTypFunctor.Node.
517 all: try exact ZMapTypFunctor.t_rect.
518 all: try exact t_ind_full.
519 all: try exact ZMap_xmap2_lr.
520 all: try exact ZMap_xgmap2_l.
521 all: try exact ZMap_xgmap2_r.
522 all: handle_id_fix.
523 all: subst_partial_evars.
524 all: try (destruct_head' option; destruct_head'_True; destruct_head'_False; reflexivity).
525 all: partial_unify e.
526 all: try reflexivity.
527 Defined.
528 Definition everything := Eval cbv [everything'] in everything'.
529 End everything.
530 End ZMapTrieInd.