Debian Codegraph @

src/Arithmetic/Partition.v

raw
1 From Coq Require Import Lia.
2 From Coq Require Import ZArith.
3 From Coq Require Import List.
4 From Coq Require Import Orders.
5 Require Import Crypto.Arithmetic.Core.
6 Require Import Crypto.Util.ListUtil.
7 Require Import Crypto.Util.ZUtil.EquivModulo.
8 Require Import Crypto.Util.ZUtil.Modulo Crypto.Util.ZUtil.Div.
9
10 Require Import Crypto.Util.Notations.
11 Import ListNotations Weight. Local Open Scope Z_scope.
12
13 (* extra name wrapper so partition won't be confused with List.partition *)
14 Module Partition.
15 Definition partition (weight : nat -> Z) n x :=
16 map (fun i => (x mod weight (S i)) / weight i) (seq 0 n).
17 End Partition.
18
19 Section PartitionProofs.
20 Context weight {wprops : @weight_properties weight}.
21 Local Notation partition := (Partition.partition weight).
22
23 Lemma partition_step n x :
24 partition (S n) x = partition n x ++ [(x mod weight (S n)) / weight n].
25 Proof using Type.
26 cbv [partition]. rewrite seq_snoc.
27 autorewrite with natsimplify push_map. reflexivity.
28 Qed.
29
30 Lemma length_partition n x : length (partition n x) = n.
31 Proof using Type. cbv [partition]; distr_length. Qed.
32 Hint Rewrite length_partition : distr_length.
33
34 Lemma eval_partition n x :
35 Positional.eval weight n (partition n x) = x mod (weight n).
36 Proof using wprops.
37 induction n; intros.
38 { cbn. rewrite (weight_0); auto with zarith. }
39 { rewrite (Z.div_mod (x mod weight (S n)) (weight n)) by auto with zarith.
40 rewrite <-Znumtheory.Zmod_div_mod by (try apply Z.mod_divide; auto with zarith).
41 rewrite partition_step, Positional.eval_snoc with (n:=n) by distr_length.
42 lia. }
43 Qed.
44
45 Lemma partition_Proper n :
46 Proper (Z.equiv_modulo (weight n) ==> eq) (partition n).
47 Proof using wprops.
48 cbv [Proper Z.equiv_modulo respectful].
49 intros x y Hxy; induction n; intros.
50 { reflexivity. }
51 { assert (Hxyn : x mod weight n = y mod weight n).
52 { erewrite (Znumtheory.Zmod_div_mod _ (weight (S n)) x), (Znumtheory.Zmod_div_mod _ (weight (S n)) y), Hxy
53 by (try apply Z.mod_divide; auto with zarith);
54 reflexivity. }
55 rewrite !partition_step, IHn by eauto.
56 rewrite (Z.div_mod (x mod weight (S n)) (weight n)), (Z.div_mod (y mod weight (S n)) (weight n)) by auto with zarith.
57 rewrite <-!Znumtheory.Zmod_div_mod by (try apply Z.mod_divide; auto with zarith).
58 rewrite Hxy, Hxyn; reflexivity. }
59 Qed.
60
61 (* This is basically a shortcut for:
62 apply partition_Proper; [ | cbv [Z.equiv_modulo] *)
63 Lemma partition_eq_mod x y n :
64 x mod weight n = y mod weight n ->
65 partition n x = partition n y.
66 Proof using wprops. apply partition_Proper. Qed.
67
68 Lemma nth_default_partition d n x i :
69 (i < n)%nat ->
70 nth_default d (partition n x) i = x mod weight (S i) / weight i.
71 Proof using Type.
72 cbv [partition]; intros.
73 rewrite map_nth_default with (x:=0%nat) by distr_length.
74 autorewrite with push_nth_default natsimplify. reflexivity.
75 Qed.
76
77 Lemma nth_default_partition_full d n x i :
78 nth_default d (partition n x) i = if lt_dec i n then x mod weight (S i) / weight i else d.
79 Proof using Type.
80 break_innermost_match;
81 try now rewrite nth_default_out_of_bounds by distr_length.
82 now rewrite nth_default_partition by lia.
83 Qed.
84
85 Fixpoint recursive_partition n i x :=
86 match n with
87 | O => []
88 | S n' => x mod (weight (S i) / weight i) :: recursive_partition n' (S i) (x / (weight (S i) / weight i))
89 end.
90
91 Lemma recursive_partition_equiv' n : forall x j,
92 map (fun i => x mod weight (S i) / weight i) (seq j n) = recursive_partition n j (x / weight j).
93 Proof using wprops.
94 induction n; [reflexivity|].
95 intros; cbn. rewrite IHn.
96 pose proof (@weight_positive _ wprops j).
97 pose proof (@weight_divides _ wprops j).
98 f_equal;
99 repeat match goal with
100 | _ => rewrite Z.mod_pull_div by auto with zarith
101 | _ => rewrite weight_multiples by auto with zarith
102 | _ => progress autorewrite with zsimplify_fast zdiv_to_mod pull_Zdiv
103 | _ => reflexivity
104 end.
105 Qed.
106
107 Lemma recursive_partition_equiv n x :
108 partition n x = recursive_partition n 0%nat x.
109 Proof using wprops.
110 cbv [partition]. rewrite recursive_partition_equiv'.
111 rewrite weight_0 by auto; autorewrite with zsimplify_fast.
112 reflexivity.
113 Qed.
114
115 Lemma length_recursive_partition n : forall i x,
116 length (recursive_partition n i x) = n.
117 Proof using Type.
118 induction n; cbn [recursive_partition]; [reflexivity | ].
119 intros; distr_length; auto.
120 Qed.
121
122 Lemma drop_high_to_length_partition n m x :
123 (n <= m)%nat ->
124 Positional.drop_high_to_length n (partition m x) = partition n x.
125 Proof using Type.
126 cbv [Positional.drop_high_to_length partition]; intros.
127 autorewrite with push_firstn.
128 rewrite Nat.min_l by lia.
129 reflexivity.
130 Qed.
131
132 Lemma partition_0 n : partition n 0 = Positional.zeros n.
133 Proof.
134 cbv [partition].
135 erewrite Positional.zeros_ext_map with (p:=seq 0 n) by distr_length.
136 apply map_ext; intros.
137 autorewrite with zsimplify; reflexivity.
138 Qed.
139
140 End PartitionProofs.
141 #[global]
142 Hint Rewrite length_partition length_recursive_partition : distr_length.
143 #[global]
144 Hint Rewrite eval_partition using (solve [auto; distr_length]) : push_eval.
145 #[global]
146 Hint Rewrite nth_default_partition_full : push_nth_default.