-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGcd.v
241 lines (197 loc) · 8.72 KB
/
Gcd.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
Require Import Coq.PArith.BinPos
Coq.ZArith.ZArith
Lia Coq.Arith.Compare_dec.
Module Reducing.
Local Open Scope Z_scope.
Section wf_proof.
Let f (c : Z) (a : Z) := Z.abs_nat (a - c).
Definition zwf (c : Z) (x y : Z) := (f c x < f c y)%nat.
Lemma zwf_well_founded (c : Z) : well_founded (zwf c).
Proof.
exact (well_founded_ltof Z (f c)).
Defined.
Lemma pos_acc (x : Z) : forall a, Zpos a < x -> Acc Pos.lt a.
Proof.
induction (zwf_well_founded 1 x) as [z Hz IHz].
intros ? Hxa.
constructor; intros y Hy.
constructor; intros v Hv.
eapply IHz with (y := Z.pos y).
unfold zwf. apply Zabs_nat_lt.
split; nia. nia.
Defined.
Lemma poslt_wf : well_founded Pos.lt.
Proof.
red; intros ?.
constructor; intros y Hy.
eapply pos_acc.
instantiate (1 := Z.pos a).
nia.
Defined.
About poslt_wf.
End wf_proof.
Section Extgcd.
Variable (x y : positive).
Definition poseven (a : positive) : bool :=
match a with
| xO _ => true
| _ => false
end.
Lemma poseven_div : forall p, true = poseven p -> (Pos.div2 p < p)%positive.
Proof.
induction p; simpl; intro H; try (inversion H).
nia.
Qed.
Definition binary_gcd (u v g : positive) (a b c d : Z) : Z * Z * positive.
Proof.
revert g a b c d.
refine ((fix binary_gcd u v (H : Acc Pos.lt (Pos.add u v)) { struct H } := _) u v (poslt_wf _)).
intros g a b c d.
case_eq (poseven u); case_eq (poseven v); intros Hv Hu.
+ refine (binary_gcd (Pos.div2 u) (Pos.div2 v) _ (Pos.mul 2 g) a b c d).
now apply Acc_inv with (1 := H), Pos.add_lt_mono; apply poseven_div.
+ refine (match Z.even a, Z.even b with
| true, true => binary_gcd (Pos.div2 u) v _ g (Z.div a 2) (Z.div b 2) c d
| _ , _ => binary_gcd (Pos.div2 u) v _ g (Z.div (a + Zpos y) 2) (Z.div (b - Zpos x) 2) c d
end); now apply Acc_inv with (1 := H), Pos.add_lt_mono_r, poseven_div.
+ refine (match Z.even c, Z.even d with
| true, true => binary_gcd u (Pos.div2 v) _ g a b (Z.div c 2) (Z.div d 2)
| _ , _ => binary_gcd u (Pos.div2 v) _ g a b (Z.div (c + Zpos y) 2) (Z.div (d - Zpos x) 2)
end); now apply Acc_inv with (1 := H), Pos.add_lt_mono_l, poseven_div.
+ case_eq (u ?= v)%positive; intros Huv.
* exact (a, b, Pos.mul g v).
* refine (binary_gcd u (Pos.sub v u) _ g a b (Z.sub c a) (Z.sub d b)).
apply Acc_inv with (1 := H).
rewrite Pos.compare_lt_iff in Huv; nia.
* refine (binary_gcd (Pos.sub u v) v _ g (Z.sub a c) (Z.sub b d) c d).
apply Acc_inv with (1 := H).
rewrite Pos.compare_gt_iff in Huv; nia.
Defined.
End Extgcd.
Section Extgcds.
Variable (x y : positive).
Definition bgcd (u v g : positive) (a b c d : Z) : Z * Z * positive.
Proof.
revert g a b c d.
refine ((fix bgcd u v (H : Acc Pos.lt (Pos.add u v)) {struct H} :=
match u as up, v as vp return (u, v) = (up, vp) -> _ with
| xH, xH => fun Huv g a b c d => (a, b, Pos.mul g v)
| xH, xO pv => fun Huv g a b c d => match Z.even c, Z.even d with
| true, true => bgcd xH pv _ g a b (Z.div c 2) (Z.div d 2)
| _, _ => bgcd xH pv _ g a b (Z.div (c + Zpos y) 2) (Z.div (d - Zpos x) 2)
end
| xH, xI pv => fun Huv g a b c d => bgcd u (Pos.sub v u) _ g a b (Z.sub c a) (Z.sub d b)
| xO pu, xH => fun Huv g a b c d => match Z.even a, Z.even b with
| true, true => bgcd pu xH _ g (Z.div a 2) (Z.div b 2) c d
| _, _ => bgcd pu xH _ g (Z.div (a + Zpos y) 2) (Z.div (b - Zpos x) 2) c d
end
| xO pu, xO pv => fun Huv g a b c d => bgcd pu pv _ (Pos.mul 2 g) a b c d
| xO pu, xI pv => fun Huv g a b c d => match Z.even a, Z.even b with
| true, true => bgcd pu (xI pv) _ g (Z.div a 2) (Z.div b 2) c d
| _, _ => bgcd pu (xI pv) _ g (Z.div (a + Zpos y) 2) (Z.div (b - Zpos x) 2) c d
end
| xI pu, xH => fun Huv g a b c d => bgcd (Pos.sub u v) v _ g (Z.sub a c) (Z.sub b d) c d
| xI pu, xO pv => fun Huv g a b c d => match Z.even c, Z.even d with
| true, true => bgcd (xI pu) pv _ g a b (Z.div c 2) (Z.div d 2)
| _, _ => bgcd (xI pu) pv _ g a b (Z.div (c + Zpos y) 2) (Z.div (d - Zpos x) 2)
end
| xI pu, xI pv => fun Huv g a b c d => match (pu ?= pv)%positive with
| Lt => bgcd u (Pos.sub v u) _ g a b (Z.sub c a) (Z.sub d b)
| Eq => (a, b, Pos.mul g v)
| Gt => bgcd (Pos.sub u v) v _ g (Z.sub a c) (Z.sub b d) c d
end
end eq_refl) u v (poslt_wf _ )); inversion Huv; subst; clear Huv;
try (apply Acc_inv with (1 := H); nia).
Defined.
End Extgcds.
Time Eval vm_compute in binary_gcd 2234500 485700 2234500 485700 1 1 0 0 1.
Time Eval vm_compute in bgcd 2234500 485700 2234500 485700 1 1 0 0 1.
Section Extgcdfuel.
Fixpoint bin_gcd (n : nat) (x y : positive) (u v g : positive) (a b c d : Z) : Z * Z * positive :=
match n with
| 0%nat => (a, b, Pos.mul g v)
| S n' =>
match poseven u, poseven v with
| true, true => bin_gcd n' x y (Pos.div2 u) (Pos.div2 v) (Pos.mul 2 g) a b c d
| true, false =>
match Z.even a, Z.even b with
| true, true => bin_gcd n' x y (Pos.div2 u) v g (Z.div a 2) (Z.div b 2) c d
| _, _ => bin_gcd n' x y (Pos.div2 u) v g (Z.div (a + Zpos y) 2) (Z.div (b - Zpos x) 2) c d
end
| false, true =>
match Z.even c, Z.even d with
| true, true => bin_gcd n' x y u (Pos.div2 v) g a b (Z.div c 2) (Z.div d 2)
| _, _ => bin_gcd n' x y u (Pos.div2 v) g a b (Z.div (c + Zpos y) 2) (Z.div (d - Zpos x) 2)
end
| false, false =>
match (u ?= v)%positive with
| Lt => bin_gcd n' x y u (Pos.sub v u) g a b (Z.sub c a) (Z.sub d b)
| Eq => (a, b, Pos.mul g v)
| Gt => bin_gcd n' x y (Pos.sub u v) v g (Z.sub a c) (Z.sub b d) c d
end
end
end.
Time Eval vm_compute in bin_gcd 100 22345 485 22345 485 1 1 0 0 1.
Definition binary_extended_gcd (a b : positive) :=
bin_gcd (2 * (Pos.size_nat a + Pos.size_nat b + 2)) a b a b 1 1 0 0 1.
Time Eval compute in binary_extended_gcd 9873492734 6434423.
End Extgcdfuel.
End Reducing.
Module NotReducing.
Require Import Coq.ZArith.Zwf.
Lemma poslt_wf : well_founded Pos.lt.
Proof.
unfold well_founded.
assert (forall (x : Z) a, x = Zpos a -> Acc Pos.lt a).
intros x. induction (Zwf_well_founded 1 x);
intros a Hxa.
constructor; intros y Hy.
eapply H0 with (y := Z.pos y).
unfold Zwf. split; nia.
reflexivity.
intros ?. eapply H with (x := Z.pos a).
reflexivity.
Defined.
About poslt_wf.
About Reducing.poslt_wf.
Section T.
Variable (x y : positive).
Definition bgcd (u v g : positive) (a b c d : Z) : Z * Z * positive.
Proof.
revert g a b c d.
refine ((fix bgcd u v (H : Acc Pos.lt (Pos.add u v)) {struct H} :=
match u as up, v as vp return (u, v) = (up, vp) -> _ with
| xH, xH => fun Huv g a b c d => (a, b, Pos.mul g v)
| xH, xO pv => fun Huv g a b c d => match Z.even c, Z.even d with
| true, true => bgcd xH pv _ g a b (Z.div c 2) (Z.div d 2)
| _, _ => bgcd xH pv _ g a b (Z.div (c + Zpos y) 2) (Z.div (d - Zpos x) 2)
end
| xH, xI pv => fun Huv g a b c d => bgcd u (Pos.sub v u) _ g a b (Z.sub c a) (Z.sub d b)
| xO pu, xH => fun Huv g a b c d => match Z.even a, Z.even b with
| true, true => bgcd pu xH _ g (Z.div a 2) (Z.div b 2) c d
| _, _ => bgcd pu xH _ g (Z.div (a + Zpos y) 2) (Z.div (b - Zpos x) 2) c d
end
| xO pu, xO pv => fun Huv g a b c d => bgcd pu pv _ (Pos.mul 2 g) a b c d
| xO pu, xI pv => fun Huv g a b c d => match Z.even a, Z.even b with
| true, true => bgcd pu (xI pv) _ g (Z.div a 2) (Z.div b 2) c d
| _, _ => bgcd pu (xI pv) _ g (Z.div (a + Zpos y) 2) (Z.div (b - Zpos x) 2) c d
end
| xI pu, xH => fun Huv g a b c d => bgcd (Pos.sub u v) v _ g (Z.sub a c) (Z.sub b d) c d
| xI pu, xO pv => fun Huv g a b c d => match Z.even c, Z.even d with
| true, true => bgcd (xI pu) pv _ g a b (Z.div c 2) (Z.div d 2)
| _, _ => bgcd (xI pu) pv _ g a b (Z.div (c + Zpos y) 2) (Z.div (d - Zpos x) 2)
end
| xI pu, xI pv => fun Huv g a b c d => match (pu ?= pv)%positive with
| Lt => bgcd u (Pos.sub v u) _ g a b (Z.sub c a) (Z.sub d b)
| Eq => (a, b, Pos.mul g v)
| Gt => bgcd (Pos.sub u v) v _ g (Z.sub a c) (Z.sub b d) c d
end
end eq_refl) u v (Reducing.poslt_wf _ )); inversion Huv; subst; clear Huv;
try (apply Acc_inv with (1 := H); nia).
Defined.
About bgcd.
About poslt_wf.
End T.
(* This one does not reduce to any value *)
Time Eval compute in bgcd 2 3 2 3 1 1 0 0 1.
End NotReducing.