| author | wenzelm | 
| Sun, 18 Jan 2015 19:09:41 +0100 | |
| changeset 59394 | bc3a21ca23aa | 
| parent 59061 | 67771d267ff2 | 
| child 60430 | ce559c850a27 | 
| permissions | -rw-r--r-- | 
| 58023 | 1  | 
(* Author: Manuel Eberl *)  | 
2  | 
||
| 58889 | 3  | 
section {* Abstract euclidean algorithm *}
 | 
| 58023 | 4  | 
|
5  | 
theory Euclidean_Algorithm  | 
|
6  | 
imports Complex_Main  | 
|
7  | 
begin  | 
|
8  | 
||
9  | 
context semiring_div  | 
|
10  | 
begin  | 
|
11  | 
||
| 59061 | 12  | 
abbreviation is_unit :: "'a \<Rightarrow> bool"  | 
| 58023 | 13  | 
where  | 
| 59061 | 14  | 
"is_unit x \<equiv> x dvd 1"  | 
| 58023 | 15  | 
|
16  | 
definition associated :: "'a \<Rightarrow> 'a \<Rightarrow> bool"  | 
|
17  | 
where  | 
|
18  | 
"associated x y \<longleftrightarrow> x dvd y \<and> y dvd x"  | 
|
19  | 
||
| 59061 | 20  | 
definition ring_inv :: "'a \<Rightarrow> 'a"  | 
21  | 
where  | 
|
22  | 
"ring_inv x = 1 div x"  | 
|
23  | 
||
| 58023 | 24  | 
lemma unit_prod [intro]:  | 
25  | 
"is_unit x \<Longrightarrow> is_unit y \<Longrightarrow> is_unit (x * y)"  | 
|
| 59061 | 26  | 
by (subst mult_1_left [of 1, symmetric], rule mult_dvd_mono)  | 
| 58023 | 27  | 
|
28  | 
lemma unit_ring_inv:  | 
|
29  | 
"is_unit y \<Longrightarrow> x div y = x * ring_inv y"  | 
|
| 59061 | 30  | 
by (simp add: div_mult_swap ring_inv_def)  | 
| 58023 | 31  | 
|
32  | 
lemma unit_ring_inv_ring_inv [simp]:  | 
|
33  | 
"is_unit x \<Longrightarrow> ring_inv (ring_inv x) = x"  | 
|
| 59061 | 34  | 
unfolding ring_inv_def  | 
| 58023 | 35  | 
by (metis div_mult_mult1_if div_mult_self1_is_id dvd_mult_div_cancel mult_1_right)  | 
36  | 
||
37  | 
lemma inv_imp_eq_ring_inv:  | 
|
38  | 
"a * b = 1 \<Longrightarrow> ring_inv a = b"  | 
|
39  | 
by (metis dvd_mult_div_cancel dvd_mult_right mult_1_right mult.left_commute one_dvd ring_inv_def)  | 
|
40  | 
||
41  | 
lemma ring_inv_is_inv1 [simp]:  | 
|
42  | 
"is_unit a \<Longrightarrow> a * ring_inv a = 1"  | 
|
| 59061 | 43  | 
unfolding ring_inv_def by simp  | 
| 58023 | 44  | 
|
45  | 
lemma ring_inv_is_inv2 [simp]:  | 
|
46  | 
"is_unit a \<Longrightarrow> ring_inv a * a = 1"  | 
|
47  | 
by (simp add: ac_simps)  | 
|
48  | 
||
49  | 
lemma unit_ring_inv_unit [simp, intro]:  | 
|
50  | 
assumes "is_unit x"  | 
|
51  | 
shows "is_unit (ring_inv x)"  | 
|
52  | 
proof -  | 
|
53  | 
from assms have "1 = ring_inv x * x" by simp  | 
|
| 59061 | 54  | 
then show "is_unit (ring_inv x)" by (rule dvdI)  | 
| 58023 | 55  | 
qed  | 
56  | 
||
57  | 
lemma mult_unit_dvd_iff:  | 
|
58  | 
"is_unit y \<Longrightarrow> x * y dvd z \<longleftrightarrow> x dvd z"  | 
|
59  | 
proof  | 
|
60  | 
assume "is_unit y" "x * y dvd z"  | 
|
61  | 
then show "x dvd z" by (simp add: dvd_mult_left)  | 
|
62  | 
next  | 
|
63  | 
assume "is_unit y" "x dvd z"  | 
|
64  | 
then obtain k where "z = x * k" unfolding dvd_def by blast  | 
|
65  | 
with `is_unit y` have "z = (x * y) * (ring_inv y * k)"  | 
|
66  | 
by (simp add: mult_ac)  | 
|
67  | 
then show "x * y dvd z" by (rule dvdI)  | 
|
68  | 
qed  | 
|
69  | 
||
70  | 
lemma div_unit_dvd_iff:  | 
|
71  | 
"is_unit y \<Longrightarrow> x div y dvd z \<longleftrightarrow> x dvd z"  | 
|
72  | 
by (subst unit_ring_inv) (assumption, simp add: mult_unit_dvd_iff)  | 
|
73  | 
||
74  | 
lemma dvd_mult_unit_iff:  | 
|
75  | 
"is_unit y \<Longrightarrow> x dvd z * y \<longleftrightarrow> x dvd z"  | 
|
76  | 
proof  | 
|
77  | 
assume "is_unit y" and "x dvd z * y"  | 
|
78  | 
have "z * y dvd z * (y * ring_inv y)" by (subst mult_assoc [symmetric]) simp  | 
|
79  | 
also from `is_unit y` have "y * ring_inv y = 1" by simp  | 
|
80  | 
finally have "z * y dvd z" by simp  | 
|
81  | 
with `x dvd z * y` show "x dvd z" by (rule dvd_trans)  | 
|
82  | 
next  | 
|
83  | 
assume "x dvd z"  | 
|
84  | 
then show "x dvd z * y" by simp  | 
|
85  | 
qed  | 
|
86  | 
||
87  | 
lemma dvd_div_unit_iff:  | 
|
88  | 
"is_unit y \<Longrightarrow> x dvd z div y \<longleftrightarrow> x dvd z"  | 
|
89  | 
by (subst unit_ring_inv) (assumption, simp add: dvd_mult_unit_iff)  | 
|
90  | 
||
91  | 
lemmas unit_dvd_iff = mult_unit_dvd_iff div_unit_dvd_iff dvd_mult_unit_iff dvd_div_unit_iff  | 
|
92  | 
||
93  | 
lemma unit_div [intro]:  | 
|
94  | 
"is_unit x \<Longrightarrow> is_unit y \<Longrightarrow> is_unit (x div y)"  | 
|
95  | 
by (subst unit_ring_inv) (assumption, rule unit_prod, simp_all)  | 
|
96  | 
||
97  | 
lemma unit_div_mult_swap:  | 
|
98  | 
"is_unit z \<Longrightarrow> x * (y div z) = x * y div z"  | 
|
99  | 
by (simp only: unit_ring_inv [of _ y] unit_ring_inv [of _ "x*y"] ac_simps)  | 
|
100  | 
||
101  | 
lemma unit_div_commute:  | 
|
102  | 
"is_unit y \<Longrightarrow> x div y * z = x * z div y"  | 
|
103  | 
by (simp only: unit_ring_inv [of _ x] unit_ring_inv [of _ "x*z"] ac_simps)  | 
|
104  | 
||
105  | 
lemma unit_imp_dvd [dest]:  | 
|
106  | 
"is_unit y \<Longrightarrow> y dvd x"  | 
|
| 59061 | 107  | 
by (rule dvd_trans [of _ 1]) simp_all  | 
| 58023 | 108  | 
|
109  | 
lemma dvd_unit_imp_unit:  | 
|
110  | 
"is_unit y \<Longrightarrow> x dvd y \<Longrightarrow> is_unit x"  | 
|
| 59061 | 111  | 
by (rule dvd_trans)  | 
| 58023 | 112  | 
|
113  | 
lemma ring_inv_0 [simp]:  | 
|
114  | 
"ring_inv 0 = 0"  | 
|
115  | 
unfolding ring_inv_def by simp  | 
|
116  | 
||
117  | 
lemma unit_ring_inv'1:  | 
|
118  | 
assumes "is_unit y"  | 
|
119  | 
shows "x div (y * z) = x * ring_inv y div z"  | 
|
120  | 
proof -  | 
|
121  | 
from assms have "x div (y * z) = x * (ring_inv y * y) div (y * z)"  | 
|
122  | 
by simp  | 
|
123  | 
also have "... = y * (x * ring_inv y) div (y * z)"  | 
|
124  | 
by (simp only: mult_ac)  | 
|
125  | 
also have "... = x * ring_inv y div z"  | 
|
126  | 
by (cases "y = 0", simp, rule div_mult_mult1)  | 
|
127  | 
finally show ?thesis .  | 
|
128  | 
qed  | 
|
129  | 
||
130  | 
lemma associated_comm:  | 
|
131  | 
"associated x y \<Longrightarrow> associated y x"  | 
|
132  | 
by (simp add: associated_def)  | 
|
133  | 
||
134  | 
lemma associated_0 [simp]:  | 
|
135  | 
"associated 0 b \<longleftrightarrow> b = 0"  | 
|
136  | 
"associated a 0 \<longleftrightarrow> a = 0"  | 
|
137  | 
unfolding associated_def by simp_all  | 
|
138  | 
||
139  | 
lemma associated_unit:  | 
|
140  | 
"is_unit x \<Longrightarrow> associated x y \<Longrightarrow> is_unit y"  | 
|
| 59061 | 141  | 
unfolding associated_def using dvd_unit_imp_unit by auto  | 
| 58023 | 142  | 
|
143  | 
lemma is_unit_1 [simp]:  | 
|
144  | 
"is_unit 1"  | 
|
| 59061 | 145  | 
by simp  | 
| 58023 | 146  | 
|
147  | 
lemma not_is_unit_0 [simp]:  | 
|
148  | 
"\<not> is_unit 0"  | 
|
| 59061 | 149  | 
by auto  | 
| 58023 | 150  | 
|
151  | 
lemma unit_mult_left_cancel:  | 
|
152  | 
assumes "is_unit x"  | 
|
153  | 
shows "(x * y) = (x * z) \<longleftrightarrow> y = z"  | 
|
154  | 
proof -  | 
|
155  | 
from assms have "x \<noteq> 0" by auto  | 
|
156  | 
then show ?thesis by (metis div_mult_self1_is_id)  | 
|
157  | 
qed  | 
|
158  | 
||
159  | 
lemma unit_mult_right_cancel:  | 
|
160  | 
"is_unit x \<Longrightarrow> (y * x) = (z * x) \<longleftrightarrow> y = z"  | 
|
161  | 
by (simp add: ac_simps unit_mult_left_cancel)  | 
|
162  | 
||
163  | 
lemma unit_div_cancel:  | 
|
164  | 
"is_unit x \<Longrightarrow> (y div x) = (z div x) \<longleftrightarrow> y = z"  | 
|
165  | 
apply (subst unit_ring_inv[of _ y], assumption)  | 
|
166  | 
apply (subst unit_ring_inv[of _ z], assumption)  | 
|
167  | 
apply (rule unit_mult_right_cancel, erule unit_ring_inv_unit)  | 
|
168  | 
done  | 
|
169  | 
||
170  | 
lemma unit_eq_div1:  | 
|
171  | 
"is_unit y \<Longrightarrow> x div y = z \<longleftrightarrow> x = z * y"  | 
|
172  | 
apply (subst unit_ring_inv, assumption)  | 
|
173  | 
apply (subst unit_mult_right_cancel[symmetric], assumption)  | 
|
174  | 
apply (subst mult_assoc, subst ring_inv_is_inv2, assumption, simp)  | 
|
175  | 
done  | 
|
176  | 
||
177  | 
lemma unit_eq_div2:  | 
|
178  | 
"is_unit y \<Longrightarrow> x = z div y \<longleftrightarrow> x * y = z"  | 
|
179  | 
by (subst (1 2) eq_commute, simp add: unit_eq_div1, subst eq_commute, rule refl)  | 
|
180  | 
||
181  | 
lemma associated_iff_div_unit:  | 
|
182  | 
"associated x y \<longleftrightarrow> (\<exists>z. is_unit z \<and> x = z * y)"  | 
|
183  | 
proof  | 
|
184  | 
assume "associated x y"  | 
|
185  | 
show "\<exists>z. is_unit z \<and> x = z * y"  | 
|
186  | 
proof (cases "x = 0")  | 
|
187  | 
assume "x = 0"  | 
|
188  | 
then show "\<exists>z. is_unit z \<and> x = z * y" using `associated x y`  | 
|
189  | 
by (intro exI[of _ 1], simp add: associated_def)  | 
|
190  | 
next  | 
|
191  | 
assume [simp]: "x \<noteq> 0"  | 
|
192  | 
hence [simp]: "x dvd y" "y dvd x" using `associated x y`  | 
|
193  | 
unfolding associated_def by simp_all  | 
|
194  | 
hence "1 = x div y * (y div x)"  | 
|
| 
59009
 
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
 
haftmann 
parents: 
58953 
diff
changeset
 | 
195  | 
by (simp add: div_mult_swap)  | 
| 59061 | 196  | 
hence "is_unit (x div y)" ..  | 
| 
59009
 
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
 
haftmann 
parents: 
58953 
diff
changeset
 | 
197  | 
moreover have "x = (x div y) * y" by simp  | 
| 58023 | 198  | 
ultimately show ?thesis by blast  | 
199  | 
qed  | 
|
200  | 
next  | 
|
201  | 
assume "\<exists>z. is_unit z \<and> x = z * y"  | 
|
202  | 
then obtain z where "is_unit z" and "x = z * y" by blast  | 
|
203  | 
hence "y = x * ring_inv z" by (simp add: algebra_simps)  | 
|
204  | 
hence "x dvd y" by simp  | 
|
205  | 
moreover from `x = z * y` have "y dvd x" by simp  | 
|
206  | 
ultimately show "associated x y" unfolding associated_def by simp  | 
|
207  | 
qed  | 
|
208  | 
||
209  | 
lemmas unit_simps = mult_unit_dvd_iff div_unit_dvd_iff dvd_mult_unit_iff  | 
|
210  | 
dvd_div_unit_iff unit_div_mult_swap unit_div_commute  | 
|
211  | 
unit_mult_left_cancel unit_mult_right_cancel unit_div_cancel  | 
|
212  | 
unit_eq_div1 unit_eq_div2  | 
|
213  | 
||
214  | 
end  | 
|
215  | 
||
216  | 
context ring_div  | 
|
217  | 
begin  | 
|
218  | 
||
219  | 
lemma is_unit_neg [simp]:  | 
|
220  | 
"is_unit (- x) \<Longrightarrow> is_unit x"  | 
|
| 59061 | 221  | 
by simp  | 
| 58023 | 222  | 
|
223  | 
lemma is_unit_neg_1 [simp]:  | 
|
224  | 
"is_unit (-1)"  | 
|
| 59061 | 225  | 
by simp  | 
| 58023 | 226  | 
|
227  | 
end  | 
|
228  | 
||
229  | 
lemma is_unit_nat [simp]:  | 
|
230  | 
"is_unit (x::nat) \<longleftrightarrow> x = 1"  | 
|
| 59061 | 231  | 
by simp  | 
| 58023 | 232  | 
|
233  | 
lemma is_unit_int:  | 
|
234  | 
"is_unit (x::int) \<longleftrightarrow> x = 1 \<or> x = -1"  | 
|
| 59061 | 235  | 
by auto  | 
| 58023 | 236  | 
|
237  | 
text {*
 | 
|
238  | 
A Euclidean semiring is a semiring upon which the Euclidean algorithm can be  | 
|
239  | 
implemented. It must provide:  | 
|
240  | 
  \begin{itemize}
 | 
|
241  | 
\item division with remainder  | 
|
242  | 
  \item a size function such that @{term "size (a mod b) < size b"} 
 | 
|
243  | 
        for any @{term "b \<noteq> 0"}
 | 
|
244  | 
\item a normalisation factor such that two associated numbers are equal iff  | 
|
245  | 
they are the same when divided by their normalisation factors.  | 
|
246  | 
  \end{itemize}
 | 
|
247  | 
The existence of these functions makes it possible to derive gcd and lcm functions  | 
|
248  | 
for any Euclidean semiring.  | 
|
249  | 
*}  | 
|
250  | 
class euclidean_semiring = semiring_div +  | 
|
251  | 
fixes euclidean_size :: "'a \<Rightarrow> nat"  | 
|
252  | 
fixes normalisation_factor :: "'a \<Rightarrow> 'a"  | 
|
253  | 
assumes mod_size_less [simp]:  | 
|
254  | 
"b \<noteq> 0 \<Longrightarrow> euclidean_size (a mod b) < euclidean_size b"  | 
|
255  | 
assumes size_mult_mono:  | 
|
256  | 
"b \<noteq> 0 \<Longrightarrow> euclidean_size (a * b) \<ge> euclidean_size a"  | 
|
257  | 
assumes normalisation_factor_is_unit [intro,simp]:  | 
|
258  | 
"a \<noteq> 0 \<Longrightarrow> is_unit (normalisation_factor a)"  | 
|
259  | 
assumes normalisation_factor_mult: "normalisation_factor (a * b) =  | 
|
260  | 
normalisation_factor a * normalisation_factor b"  | 
|
261  | 
assumes normalisation_factor_unit: "is_unit x \<Longrightarrow> normalisation_factor x = x"  | 
|
262  | 
assumes normalisation_factor_0 [simp]: "normalisation_factor 0 = 0"  | 
|
263  | 
begin  | 
|
264  | 
||
265  | 
lemma normalisation_factor_dvd [simp]:  | 
|
266  | 
"a \<noteq> 0 \<Longrightarrow> normalisation_factor a dvd b"  | 
|
267  | 
by (rule unit_imp_dvd, simp)  | 
|
268  | 
||
269  | 
lemma normalisation_factor_1 [simp]:  | 
|
270  | 
"normalisation_factor 1 = 1"  | 
|
271  | 
by (simp add: normalisation_factor_unit)  | 
|
272  | 
||
273  | 
lemma normalisation_factor_0_iff [simp]:  | 
|
274  | 
"normalisation_factor x = 0 \<longleftrightarrow> x = 0"  | 
|
275  | 
proof  | 
|
276  | 
assume "normalisation_factor x = 0"  | 
|
277  | 
hence "\<not> is_unit (normalisation_factor x)"  | 
|
278  | 
by (metis not_is_unit_0)  | 
|
279  | 
then show "x = 0" by force  | 
|
280  | 
next  | 
|
281  | 
assume "x = 0"  | 
|
282  | 
then show "normalisation_factor x = 0" by simp  | 
|
283  | 
qed  | 
|
284  | 
||
285  | 
lemma normalisation_factor_pow:  | 
|
286  | 
"normalisation_factor (x ^ n) = normalisation_factor x ^ n"  | 
|
287  | 
by (induct n) (simp_all add: normalisation_factor_mult power_Suc2)  | 
|
288  | 
||
289  | 
lemma normalisation_correct [simp]:  | 
|
290  | 
"normalisation_factor (x div normalisation_factor x) = (if x = 0 then 0 else 1)"  | 
|
291  | 
proof (cases "x = 0", simp)  | 
|
292  | 
assume "x \<noteq> 0"  | 
|
293  | 
let ?nf = "normalisation_factor"  | 
|
294  | 
from normalisation_factor_is_unit[OF `x \<noteq> 0`] have "?nf x \<noteq> 0"  | 
|
295  | 
by (metis not_is_unit_0)  | 
|
296  | 
have "?nf (x div ?nf x) * ?nf (?nf x) = ?nf (x div ?nf x * ?nf x)"  | 
|
297  | 
by (simp add: normalisation_factor_mult)  | 
|
298  | 
also have "x div ?nf x * ?nf x = x" using `x \<noteq> 0`  | 
|
| 
59009
 
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
 
haftmann 
parents: 
58953 
diff
changeset
 | 
299  | 
by simp  | 
| 58023 | 300  | 
also have "?nf (?nf x) = ?nf x" using `x \<noteq> 0`  | 
301  | 
normalisation_factor_is_unit normalisation_factor_unit by simp  | 
|
302  | 
finally show ?thesis using `x \<noteq> 0` and `?nf x \<noteq> 0`  | 
|
303  | 
by (metis div_mult_self2_is_id div_self)  | 
|
304  | 
qed  | 
|
305  | 
||
306  | 
lemma normalisation_0_iff [simp]:  | 
|
307  | 
"x div normalisation_factor x = 0 \<longleftrightarrow> x = 0"  | 
|
308  | 
by (cases "x = 0", simp, subst unit_eq_div1, blast, simp)  | 
|
309  | 
||
310  | 
lemma associated_iff_normed_eq:  | 
|
311  | 
"associated a b \<longleftrightarrow> a div normalisation_factor a = b div normalisation_factor b"  | 
|
312  | 
proof (cases "b = 0", simp, cases "a = 0", metis associated_0(1) normalisation_0_iff, rule iffI)  | 
|
313  | 
let ?nf = normalisation_factor  | 
|
314  | 
assume "a \<noteq> 0" "b \<noteq> 0" "a div ?nf a = b div ?nf b"  | 
|
315  | 
hence "a = b * (?nf a div ?nf b)"  | 
|
316  | 
apply (subst (asm) unit_eq_div1, blast, subst (asm) unit_div_commute, blast)  | 
|
317  | 
apply (subst div_mult_swap, simp, simp)  | 
|
318  | 
done  | 
|
319  | 
with `a \<noteq> 0` `b \<noteq> 0` have "\<exists>z. is_unit z \<and> a = z * b"  | 
|
320  | 
by (intro exI[of _ "?nf a div ?nf b"], force simp: mult_ac)  | 
|
321  | 
with associated_iff_div_unit show "associated a b" by simp  | 
|
322  | 
next  | 
|
323  | 
let ?nf = normalisation_factor  | 
|
324  | 
assume "a \<noteq> 0" "b \<noteq> 0" "associated a b"  | 
|
325  | 
with associated_iff_div_unit obtain z where "is_unit z" and "a = z * b" by blast  | 
|
326  | 
then show "a div ?nf a = b div ?nf b"  | 
|
327  | 
apply (simp only: `a = z * b` normalisation_factor_mult normalisation_factor_unit)  | 
|
328  | 
apply (rule div_mult_mult1, force)  | 
|
329  | 
done  | 
|
330  | 
qed  | 
|
331  | 
||
332  | 
lemma normed_associated_imp_eq:  | 
|
333  | 
  "associated a b \<Longrightarrow> normalisation_factor a \<in> {0, 1} \<Longrightarrow> normalisation_factor b \<in> {0, 1} \<Longrightarrow> a = b"
 | 
|
334  | 
by (simp add: associated_iff_normed_eq, elim disjE, simp_all)  | 
|
335  | 
||
336  | 
lemmas normalisation_factor_dvd_iff [simp] =  | 
|
337  | 
unit_dvd_iff [OF normalisation_factor_is_unit]  | 
|
338  | 
||
339  | 
lemma euclidean_division:  | 
|
340  | 
fixes a :: 'a and b :: 'a  | 
|
341  | 
assumes "b \<noteq> 0"  | 
|
342  | 
obtains s and t where "a = s * b + t"  | 
|
343  | 
and "euclidean_size t < euclidean_size b"  | 
|
344  | 
proof -  | 
|
345  | 
from div_mod_equality[of a b 0]  | 
|
346  | 
have "a = a div b * b + a mod b" by simp  | 
|
347  | 
with that and assms show ?thesis by force  | 
|
348  | 
qed  | 
|
349  | 
||
350  | 
lemma dvd_euclidean_size_eq_imp_dvd:  | 
|
351  | 
assumes "a \<noteq> 0" and b_dvd_a: "b dvd a" and size_eq: "euclidean_size a = euclidean_size b"  | 
|
352  | 
shows "a dvd b"  | 
|
353  | 
proof (subst dvd_eq_mod_eq_0, rule ccontr)  | 
|
354  | 
assume "b mod a \<noteq> 0"  | 
|
355  | 
from b_dvd_a have b_dvd_mod: "b dvd b mod a" by (simp add: dvd_mod_iff)  | 
|
356  | 
from b_dvd_mod obtain c where "b mod a = b * c" unfolding dvd_def by blast  | 
|
357  | 
with `b mod a \<noteq> 0` have "c \<noteq> 0" by auto  | 
|
358  | 
with `b mod a = b * c` have "euclidean_size (b mod a) \<ge> euclidean_size b"  | 
|
359  | 
using size_mult_mono by force  | 
|
360  | 
moreover from `a \<noteq> 0` have "euclidean_size (b mod a) < euclidean_size a"  | 
|
361  | 
using mod_size_less by blast  | 
|
362  | 
ultimately show False using size_eq by simp  | 
|
363  | 
qed  | 
|
364  | 
||
365  | 
function gcd_eucl :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"  | 
|
366  | 
where  | 
|
367  | 
"gcd_eucl a b = (if b = 0 then a div normalisation_factor a else gcd_eucl b (a mod b))"  | 
|
368  | 
by (pat_completeness, simp)  | 
|
369  | 
termination by (relation "measure (euclidean_size \<circ> snd)", simp_all)  | 
|
370  | 
||
371  | 
declare gcd_eucl.simps [simp del]  | 
|
372  | 
||
373  | 
lemma gcd_induct: "\<lbrakk>\<And>b. P b 0; \<And>a b. 0 \<noteq> b \<Longrightarrow> P b (a mod b) \<Longrightarrow> P a b\<rbrakk> \<Longrightarrow> P a b"  | 
|
374  | 
proof (induct a b rule: gcd_eucl.induct)  | 
|
375  | 
  case ("1" m n)
 | 
|
376  | 
then show ?case by (cases "n = 0") auto  | 
|
377  | 
qed  | 
|
378  | 
||
379  | 
definition lcm_eucl :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"  | 
|
380  | 
where  | 
|
381  | 
"lcm_eucl a b = a * b div (gcd_eucl a b * normalisation_factor (a * b))"  | 
|
382  | 
||
383  | 
(* Somewhat complicated definition of Lcm that has the advantage of working  | 
|
384  | 
for infinite sets as well *)  | 
|
385  | 
||
386  | 
definition Lcm_eucl :: "'a set \<Rightarrow> 'a"  | 
|
387  | 
where  | 
|
388  | 
"Lcm_eucl A = (if \<exists>l. l \<noteq> 0 \<and> (\<forall>x\<in>A. x dvd l) then  | 
|
389  | 
let l = SOME l. l \<noteq> 0 \<and> (\<forall>x\<in>A. x dvd l) \<and> euclidean_size l =  | 
|
390  | 
(LEAST n. \<exists>l. l \<noteq> 0 \<and> (\<forall>x\<in>A. x dvd l) \<and> euclidean_size l = n)  | 
|
391  | 
in l div normalisation_factor l  | 
|
392  | 
else 0)"  | 
|
393  | 
||
394  | 
definition Gcd_eucl :: "'a set \<Rightarrow> 'a"  | 
|
395  | 
where  | 
|
396  | 
  "Gcd_eucl A = Lcm_eucl {d. \<forall>a\<in>A. d dvd a}"
 | 
|
397  | 
||
398  | 
end  | 
|
399  | 
||
400  | 
class euclidean_semiring_gcd = euclidean_semiring + gcd + Gcd +  | 
|
401  | 
assumes gcd_gcd_eucl: "gcd = gcd_eucl" and lcm_lcm_eucl: "lcm = lcm_eucl"  | 
|
402  | 
assumes Gcd_Gcd_eucl: "Gcd = Gcd_eucl" and Lcm_Lcm_eucl: "Lcm = Lcm_eucl"  | 
|
403  | 
begin  | 
|
404  | 
||
405  | 
lemma gcd_red:  | 
|
406  | 
"gcd x y = gcd y (x mod y)"  | 
|
407  | 
by (metis gcd_eucl.simps mod_0 mod_by_0 gcd_gcd_eucl)  | 
|
408  | 
||
409  | 
lemma gcd_non_0:  | 
|
410  | 
"y \<noteq> 0 \<Longrightarrow> gcd x y = gcd y (x mod y)"  | 
|
411  | 
by (rule gcd_red)  | 
|
412  | 
||
413  | 
lemma gcd_0_left:  | 
|
414  | 
"gcd 0 x = x div normalisation_factor x"  | 
|
415  | 
by (simp only: gcd_gcd_eucl, subst gcd_eucl.simps, subst gcd_eucl.simps, simp add: Let_def)  | 
|
416  | 
||
417  | 
lemma gcd_0:  | 
|
418  | 
"gcd x 0 = x div normalisation_factor x"  | 
|
419  | 
by (simp only: gcd_gcd_eucl, subst gcd_eucl.simps, simp add: Let_def)  | 
|
420  | 
||
421  | 
lemma gcd_dvd1 [iff]: "gcd x y dvd x"  | 
|
422  | 
and gcd_dvd2 [iff]: "gcd x y dvd y"  | 
|
423  | 
proof (induct x y rule: gcd_eucl.induct)  | 
|
424  | 
fix x y :: 'a  | 
|
425  | 
assume IH1: "y \<noteq> 0 \<Longrightarrow> gcd y (x mod y) dvd y"  | 
|
426  | 
assume IH2: "y \<noteq> 0 \<Longrightarrow> gcd y (x mod y) dvd (x mod y)"  | 
|
427  | 
||
428  | 
have "gcd x y dvd x \<and> gcd x y dvd y"  | 
|
429  | 
proof (cases "y = 0")  | 
|
430  | 
case True  | 
|
431  | 
then show ?thesis by (cases "x = 0", simp_all add: gcd_0)  | 
|
432  | 
next  | 
|
433  | 
case False  | 
|
434  | 
with IH1 and IH2 show ?thesis by (simp add: gcd_non_0 dvd_mod_iff)  | 
|
435  | 
qed  | 
|
436  | 
then show "gcd x y dvd x" "gcd x y dvd y" by simp_all  | 
|
437  | 
qed  | 
|
438  | 
||
439  | 
lemma dvd_gcd_D1: "k dvd gcd m n \<Longrightarrow> k dvd m"  | 
|
440  | 
by (rule dvd_trans, assumption, rule gcd_dvd1)  | 
|
441  | 
||
442  | 
lemma dvd_gcd_D2: "k dvd gcd m n \<Longrightarrow> k dvd n"  | 
|
443  | 
by (rule dvd_trans, assumption, rule gcd_dvd2)  | 
|
444  | 
||
445  | 
lemma gcd_greatest:  | 
|
446  | 
fixes k x y :: 'a  | 
|
447  | 
shows "k dvd x \<Longrightarrow> k dvd y \<Longrightarrow> k dvd gcd x y"  | 
|
448  | 
proof (induct x y rule: gcd_eucl.induct)  | 
|
449  | 
case (1 x y)  | 
|
450  | 
show ?case  | 
|
451  | 
proof (cases "y = 0")  | 
|
452  | 
assume "y = 0"  | 
|
453  | 
with 1 show ?thesis by (cases "x = 0", simp_all add: gcd_0)  | 
|
454  | 
next  | 
|
455  | 
assume "y \<noteq> 0"  | 
|
456  | 
with 1 show ?thesis by (simp add: gcd_non_0 dvd_mod_iff)  | 
|
457  | 
qed  | 
|
458  | 
qed  | 
|
459  | 
||
460  | 
lemma dvd_gcd_iff:  | 
|
461  | 
"k dvd gcd x y \<longleftrightarrow> k dvd x \<and> k dvd y"  | 
|
462  | 
by (blast intro!: gcd_greatest intro: dvd_trans)  | 
|
463  | 
||
464  | 
lemmas gcd_greatest_iff = dvd_gcd_iff  | 
|
465  | 
||
466  | 
lemma gcd_zero [simp]:  | 
|
467  | 
"gcd x y = 0 \<longleftrightarrow> x = 0 \<and> y = 0"  | 
|
468  | 
by (metis dvd_0_left dvd_refl gcd_dvd1 gcd_dvd2 gcd_greatest)+  | 
|
469  | 
||
470  | 
lemma normalisation_factor_gcd [simp]:  | 
|
471  | 
"normalisation_factor (gcd x y) = (if x = 0 \<and> y = 0 then 0 else 1)" (is "?f x y = ?g x y")  | 
|
472  | 
proof (induct x y rule: gcd_eucl.induct)  | 
|
473  | 
fix x y :: 'a  | 
|
474  | 
assume IH: "y \<noteq> 0 \<Longrightarrow> ?f y (x mod y) = ?g y (x mod y)"  | 
|
475  | 
then show "?f x y = ?g x y" by (cases "y = 0", auto simp: gcd_non_0 gcd_0)  | 
|
476  | 
qed  | 
|
477  | 
||
478  | 
lemma gcdI:  | 
|
479  | 
"k dvd x \<Longrightarrow> k dvd y \<Longrightarrow> (\<And>l. l dvd x \<Longrightarrow> l dvd y \<Longrightarrow> l dvd k)  | 
|
480  | 
\<Longrightarrow> normalisation_factor k = (if k = 0 then 0 else 1) \<Longrightarrow> k = gcd x y"  | 
|
481  | 
by (intro normed_associated_imp_eq) (auto simp: associated_def intro: gcd_greatest)  | 
|
482  | 
||
483  | 
sublocale gcd!: abel_semigroup gcd  | 
|
484  | 
proof  | 
|
485  | 
fix x y z  | 
|
486  | 
show "gcd (gcd x y) z = gcd x (gcd y z)"  | 
|
487  | 
proof (rule gcdI)  | 
|
488  | 
have "gcd (gcd x y) z dvd gcd x y" "gcd x y dvd x" by simp_all  | 
|
489  | 
then show "gcd (gcd x y) z dvd x" by (rule dvd_trans)  | 
|
490  | 
have "gcd (gcd x y) z dvd gcd x y" "gcd x y dvd y" by simp_all  | 
|
491  | 
hence "gcd (gcd x y) z dvd y" by (rule dvd_trans)  | 
|
492  | 
moreover have "gcd (gcd x y) z dvd z" by simp  | 
|
493  | 
ultimately show "gcd (gcd x y) z dvd gcd y z"  | 
|
494  | 
by (rule gcd_greatest)  | 
|
495  | 
show "normalisation_factor (gcd (gcd x y) z) = (if gcd (gcd x y) z = 0 then 0 else 1)"  | 
|
496  | 
by auto  | 
|
497  | 
fix l assume "l dvd x" and "l dvd gcd y z"  | 
|
498  | 
with dvd_trans[OF _ gcd_dvd1] and dvd_trans[OF _ gcd_dvd2]  | 
|
499  | 
have "l dvd y" and "l dvd z" by blast+  | 
|
500  | 
with `l dvd x` show "l dvd gcd (gcd x y) z"  | 
|
501  | 
by (intro gcd_greatest)  | 
|
502  | 
qed  | 
|
503  | 
next  | 
|
504  | 
fix x y  | 
|
505  | 
show "gcd x y = gcd y x"  | 
|
506  | 
by (rule gcdI) (simp_all add: gcd_greatest)  | 
|
507  | 
qed  | 
|
508  | 
||
509  | 
lemma gcd_unique: "d dvd a \<and> d dvd b \<and>  | 
|
510  | 
normalisation_factor d = (if d = 0 then 0 else 1) \<and>  | 
|
511  | 
(\<forall>e. e dvd a \<and> e dvd b \<longrightarrow> e dvd d) \<longleftrightarrow> d = gcd a b"  | 
|
512  | 
by (rule, auto intro: gcdI simp: gcd_greatest)  | 
|
513  | 
||
514  | 
lemma gcd_dvd_prod: "gcd a b dvd k * b"  | 
|
515  | 
using mult_dvd_mono [of 1] by auto  | 
|
516  | 
||
517  | 
lemma gcd_1_left [simp]: "gcd 1 x = 1"  | 
|
518  | 
by (rule sym, rule gcdI, simp_all)  | 
|
519  | 
||
520  | 
lemma gcd_1 [simp]: "gcd x 1 = 1"  | 
|
521  | 
by (rule sym, rule gcdI, simp_all)  | 
|
522  | 
||
523  | 
lemma gcd_proj2_if_dvd:  | 
|
524  | 
"y dvd x \<Longrightarrow> gcd x y = y div normalisation_factor y"  | 
|
525  | 
by (cases "y = 0", simp_all add: dvd_eq_mod_eq_0 gcd_non_0 gcd_0)  | 
|
526  | 
||
527  | 
lemma gcd_proj1_if_dvd:  | 
|
528  | 
"x dvd y \<Longrightarrow> gcd x y = x div normalisation_factor x"  | 
|
529  | 
by (subst gcd.commute, simp add: gcd_proj2_if_dvd)  | 
|
530  | 
||
531  | 
lemma gcd_proj1_iff: "gcd m n = m div normalisation_factor m \<longleftrightarrow> m dvd n"  | 
|
532  | 
proof  | 
|
533  | 
assume A: "gcd m n = m div normalisation_factor m"  | 
|
534  | 
show "m dvd n"  | 
|
535  | 
proof (cases "m = 0")  | 
|
536  | 
assume [simp]: "m \<noteq> 0"  | 
|
537  | 
from A have B: "m = gcd m n * normalisation_factor m"  | 
|
538  | 
by (simp add: unit_eq_div2)  | 
|
539  | 
show ?thesis by (subst B, simp add: mult_unit_dvd_iff)  | 
|
540  | 
qed (insert A, simp)  | 
|
541  | 
next  | 
|
542  | 
assume "m dvd n"  | 
|
543  | 
then show "gcd m n = m div normalisation_factor m" by (rule gcd_proj1_if_dvd)  | 
|
544  | 
qed  | 
|
545  | 
||
546  | 
lemma gcd_proj2_iff: "gcd m n = n div normalisation_factor n \<longleftrightarrow> n dvd m"  | 
|
547  | 
by (subst gcd.commute, simp add: gcd_proj1_iff)  | 
|
548  | 
||
549  | 
lemma gcd_mod1 [simp]:  | 
|
550  | 
"gcd (x mod y) y = gcd x y"  | 
|
551  | 
by (rule gcdI, metis dvd_mod_iff gcd_dvd1 gcd_dvd2, simp_all add: gcd_greatest dvd_mod_iff)  | 
|
552  | 
||
553  | 
lemma gcd_mod2 [simp]:  | 
|
554  | 
"gcd x (y mod x) = gcd x y"  | 
|
555  | 
by (rule gcdI, simp, metis dvd_mod_iff gcd_dvd1 gcd_dvd2, simp_all add: gcd_greatest dvd_mod_iff)  | 
|
556  | 
||
557  | 
lemma normalisation_factor_dvd' [simp]:  | 
|
558  | 
"normalisation_factor x dvd x"  | 
|
559  | 
by (cases "x = 0", simp_all)  | 
|
560  | 
||
561  | 
lemma gcd_mult_distrib':  | 
|
562  | 
"k div normalisation_factor k * gcd x y = gcd (k*x) (k*y)"  | 
|
563  | 
proof (induct x y rule: gcd_eucl.induct)  | 
|
564  | 
case (1 x y)  | 
|
565  | 
show ?case  | 
|
566  | 
proof (cases "y = 0")  | 
|
567  | 
case True  | 
|
568  | 
then show ?thesis by (simp add: normalisation_factor_mult gcd_0 algebra_simps div_mult_div_if_dvd)  | 
|
569  | 
next  | 
|
570  | 
case False  | 
|
571  | 
hence "k div normalisation_factor k * gcd x y = gcd (k * y) (k * (x mod y))"  | 
|
572  | 
using 1 by (subst gcd_red, simp)  | 
|
573  | 
also have "... = gcd (k * x) (k * y)"  | 
|
574  | 
by (simp add: mult_mod_right gcd.commute)  | 
|
575  | 
finally show ?thesis .  | 
|
576  | 
qed  | 
|
577  | 
qed  | 
|
578  | 
||
579  | 
lemma gcd_mult_distrib:  | 
|
580  | 
"k * gcd x y = gcd (k*x) (k*y) * normalisation_factor k"  | 
|
581  | 
proof-  | 
|
582  | 
let ?nf = "normalisation_factor"  | 
|
583  | 
from gcd_mult_distrib'  | 
|
584  | 
have "gcd (k*x) (k*y) = k div ?nf k * gcd x y" ..  | 
|
585  | 
also have "... = k * gcd x y div ?nf k"  | 
|
586  | 
by (metis dvd_div_mult dvd_eq_mod_eq_0 mod_0 normalisation_factor_dvd)  | 
|
587  | 
finally show ?thesis  | 
|
| 
59009
 
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
 
haftmann 
parents: 
58953 
diff
changeset
 | 
588  | 
by simp  | 
| 58023 | 589  | 
qed  | 
590  | 
||
591  | 
lemma euclidean_size_gcd_le1 [simp]:  | 
|
592  | 
assumes "a \<noteq> 0"  | 
|
593  | 
shows "euclidean_size (gcd a b) \<le> euclidean_size a"  | 
|
594  | 
proof -  | 
|
595  | 
have "gcd a b dvd a" by (rule gcd_dvd1)  | 
|
596  | 
then obtain c where A: "a = gcd a b * c" unfolding dvd_def by blast  | 
|
597  | 
with `a \<noteq> 0` show ?thesis by (subst (2) A, intro size_mult_mono) auto  | 
|
598  | 
qed  | 
|
599  | 
||
600  | 
lemma euclidean_size_gcd_le2 [simp]:  | 
|
601  | 
"b \<noteq> 0 \<Longrightarrow> euclidean_size (gcd a b) \<le> euclidean_size b"  | 
|
602  | 
by (subst gcd.commute, rule euclidean_size_gcd_le1)  | 
|
603  | 
||
604  | 
lemma euclidean_size_gcd_less1:  | 
|
605  | 
assumes "a \<noteq> 0" and "\<not>a dvd b"  | 
|
606  | 
shows "euclidean_size (gcd a b) < euclidean_size a"  | 
|
607  | 
proof (rule ccontr)  | 
|
608  | 
assume "\<not>euclidean_size (gcd a b) < euclidean_size a"  | 
|
609  | 
with `a \<noteq> 0` have "euclidean_size (gcd a b) = euclidean_size a"  | 
|
610  | 
by (intro le_antisym, simp_all)  | 
|
611  | 
with assms have "a dvd gcd a b" by (auto intro: dvd_euclidean_size_eq_imp_dvd)  | 
|
612  | 
hence "a dvd b" using dvd_gcd_D2 by blast  | 
|
613  | 
with `\<not>a dvd b` show False by contradiction  | 
|
614  | 
qed  | 
|
615  | 
||
616  | 
lemma euclidean_size_gcd_less2:  | 
|
617  | 
assumes "b \<noteq> 0" and "\<not>b dvd a"  | 
|
618  | 
shows "euclidean_size (gcd a b) < euclidean_size b"  | 
|
619  | 
using assms by (subst gcd.commute, rule euclidean_size_gcd_less1)  | 
|
620  | 
||
621  | 
lemma gcd_mult_unit1: "is_unit a \<Longrightarrow> gcd (x*a) y = gcd x y"  | 
|
622  | 
apply (rule gcdI)  | 
|
623  | 
apply (rule dvd_trans, rule gcd_dvd1, simp add: unit_simps)  | 
|
624  | 
apply (rule gcd_dvd2)  | 
|
625  | 
apply (rule gcd_greatest, simp add: unit_simps, assumption)  | 
|
626  | 
apply (subst normalisation_factor_gcd, simp add: gcd_0)  | 
|
627  | 
done  | 
|
628  | 
||
629  | 
lemma gcd_mult_unit2: "is_unit a \<Longrightarrow> gcd x (y*a) = gcd x y"  | 
|
630  | 
by (subst gcd.commute, subst gcd_mult_unit1, assumption, rule gcd.commute)  | 
|
631  | 
||
632  | 
lemma gcd_div_unit1: "is_unit a \<Longrightarrow> gcd (x div a) y = gcd x y"  | 
|
633  | 
by (simp add: unit_ring_inv gcd_mult_unit1)  | 
|
634  | 
||
635  | 
lemma gcd_div_unit2: "is_unit a \<Longrightarrow> gcd x (y div a) = gcd x y"  | 
|
636  | 
by (simp add: unit_ring_inv gcd_mult_unit2)  | 
|
637  | 
||
638  | 
lemma gcd_idem: "gcd x x = x div normalisation_factor x"  | 
|
639  | 
by (cases "x = 0") (simp add: gcd_0_left, rule sym, rule gcdI, simp_all)  | 
|
640  | 
||
641  | 
lemma gcd_right_idem: "gcd (gcd p q) q = gcd p q"  | 
|
642  | 
apply (rule gcdI)  | 
|
643  | 
apply (simp add: ac_simps)  | 
|
644  | 
apply (rule gcd_dvd2)  | 
|
645  | 
apply (rule gcd_greatest, erule (1) gcd_greatest, assumption)  | 
|
| 
59009
 
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
 
haftmann 
parents: 
58953 
diff
changeset
 | 
646  | 
apply simp  | 
| 58023 | 647  | 
done  | 
648  | 
||
649  | 
lemma gcd_left_idem: "gcd p (gcd p q) = gcd p q"  | 
|
650  | 
apply (rule gcdI)  | 
|
651  | 
apply simp  | 
|
652  | 
apply (rule dvd_trans, rule gcd_dvd2, rule gcd_dvd2)  | 
|
653  | 
apply (rule gcd_greatest, assumption, erule gcd_greatest, assumption)  | 
|
| 
59009
 
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
 
haftmann 
parents: 
58953 
diff
changeset
 | 
654  | 
apply simp  | 
| 58023 | 655  | 
done  | 
656  | 
||
657  | 
lemma comp_fun_idem_gcd: "comp_fun_idem gcd"  | 
|
658  | 
proof  | 
|
659  | 
fix a b show "gcd a \<circ> gcd b = gcd b \<circ> gcd a"  | 
|
660  | 
by (simp add: fun_eq_iff ac_simps)  | 
|
661  | 
next  | 
|
662  | 
fix a show "gcd a \<circ> gcd a = gcd a"  | 
|
663  | 
by (simp add: fun_eq_iff gcd_left_idem)  | 
|
664  | 
qed  | 
|
665  | 
||
666  | 
lemma coprime_dvd_mult:  | 
|
667  | 
assumes "gcd k n = 1" and "k dvd m * n"  | 
|
668  | 
shows "k dvd m"  | 
|
669  | 
proof -  | 
|
670  | 
let ?nf = "normalisation_factor"  | 
|
671  | 
from assms gcd_mult_distrib [of m k n]  | 
|
672  | 
have A: "m = gcd (m * k) (m * n) * ?nf m" by simp  | 
|
673  | 
from `k dvd m * n` show ?thesis by (subst A, simp_all add: gcd_greatest)  | 
|
674  | 
qed  | 
|
675  | 
||
676  | 
lemma coprime_dvd_mult_iff:  | 
|
677  | 
"gcd k n = 1 \<Longrightarrow> (k dvd m * n) = (k dvd m)"  | 
|
678  | 
by (rule, rule coprime_dvd_mult, simp_all)  | 
|
679  | 
||
680  | 
lemma gcd_dvd_antisym:  | 
|
681  | 
"gcd a b dvd gcd c d \<Longrightarrow> gcd c d dvd gcd a b \<Longrightarrow> gcd a b = gcd c d"  | 
|
682  | 
proof (rule gcdI)  | 
|
683  | 
assume A: "gcd a b dvd gcd c d" and B: "gcd c d dvd gcd a b"  | 
|
684  | 
have "gcd c d dvd c" by simp  | 
|
685  | 
with A show "gcd a b dvd c" by (rule dvd_trans)  | 
|
686  | 
have "gcd c d dvd d" by simp  | 
|
687  | 
with A show "gcd a b dvd d" by (rule dvd_trans)  | 
|
688  | 
show "normalisation_factor (gcd a b) = (if gcd a b = 0 then 0 else 1)"  | 
|
| 
59009
 
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
 
haftmann 
parents: 
58953 
diff
changeset
 | 
689  | 
by simp  | 
| 58023 | 690  | 
fix l assume "l dvd c" and "l dvd d"  | 
691  | 
hence "l dvd gcd c d" by (rule gcd_greatest)  | 
|
692  | 
from this and B show "l dvd gcd a b" by (rule dvd_trans)  | 
|
693  | 
qed  | 
|
694  | 
||
695  | 
lemma gcd_mult_cancel:  | 
|
696  | 
assumes "gcd k n = 1"  | 
|
697  | 
shows "gcd (k * m) n = gcd m n"  | 
|
698  | 
proof (rule gcd_dvd_antisym)  | 
|
699  | 
have "gcd (gcd (k * m) n) k = gcd (gcd k n) (k * m)" by (simp add: ac_simps)  | 
|
700  | 
also note `gcd k n = 1`  | 
|
701  | 
finally have "gcd (gcd (k * m) n) k = 1" by simp  | 
|
702  | 
hence "gcd (k * m) n dvd m" by (rule coprime_dvd_mult, simp add: ac_simps)  | 
|
703  | 
moreover have "gcd (k * m) n dvd n" by simp  | 
|
704  | 
ultimately show "gcd (k * m) n dvd gcd m n" by (rule gcd_greatest)  | 
|
705  | 
have "gcd m n dvd (k * m)" and "gcd m n dvd n" by simp_all  | 
|
706  | 
then show "gcd m n dvd gcd (k * m) n" by (rule gcd_greatest)  | 
|
707  | 
qed  | 
|
708  | 
||
709  | 
lemma coprime_crossproduct:  | 
|
710  | 
assumes [simp]: "gcd a d = 1" "gcd b c = 1"  | 
|
711  | 
shows "associated (a * c) (b * d) \<longleftrightarrow> associated a b \<and> associated c d" (is "?lhs \<longleftrightarrow> ?rhs")  | 
|
712  | 
proof  | 
|
713  | 
assume ?rhs then show ?lhs unfolding associated_def by (fast intro: mult_dvd_mono)  | 
|
714  | 
next  | 
|
715  | 
assume ?lhs  | 
|
716  | 
from `?lhs` have "a dvd b * d" unfolding associated_def by (metis dvd_mult_left)  | 
|
717  | 
hence "a dvd b" by (simp add: coprime_dvd_mult_iff)  | 
|
718  | 
moreover from `?lhs` have "b dvd a * c" unfolding associated_def by (metis dvd_mult_left)  | 
|
719  | 
hence "b dvd a" by (simp add: coprime_dvd_mult_iff)  | 
|
720  | 
moreover from `?lhs` have "c dvd d * b"  | 
|
| 
59009
 
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
 
haftmann 
parents: 
58953 
diff
changeset
 | 
721  | 
unfolding associated_def by (auto dest: dvd_mult_right simp add: ac_simps)  | 
| 58023 | 722  | 
hence "c dvd d" by (simp add: coprime_dvd_mult_iff gcd.commute)  | 
723  | 
moreover from `?lhs` have "d dvd c * a"  | 
|
| 
59009
 
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
 
haftmann 
parents: 
58953 
diff
changeset
 | 
724  | 
unfolding associated_def by (auto dest: dvd_mult_right simp add: ac_simps)  | 
| 58023 | 725  | 
hence "d dvd c" by (simp add: coprime_dvd_mult_iff gcd.commute)  | 
726  | 
ultimately show ?rhs unfolding associated_def by simp  | 
|
727  | 
qed  | 
|
728  | 
||
729  | 
lemma gcd_add1 [simp]:  | 
|
730  | 
"gcd (m + n) n = gcd m n"  | 
|
731  | 
by (cases "n = 0", simp_all add: gcd_non_0)  | 
|
732  | 
||
733  | 
lemma gcd_add2 [simp]:  | 
|
734  | 
"gcd m (m + n) = gcd m n"  | 
|
735  | 
using gcd_add1 [of n m] by (simp add: ac_simps)  | 
|
736  | 
||
737  | 
lemma gcd_add_mult: "gcd m (k * m + n) = gcd m n"  | 
|
738  | 
by (subst gcd.commute, subst gcd_red, simp)  | 
|
739  | 
||
740  | 
lemma coprimeI: "(\<And>l. \<lbrakk>l dvd x; l dvd y\<rbrakk> \<Longrightarrow> l dvd 1) \<Longrightarrow> gcd x y = 1"  | 
|
741  | 
by (rule sym, rule gcdI, simp_all)  | 
|
742  | 
||
743  | 
lemma coprime: "gcd a b = 1 \<longleftrightarrow> (\<forall>d. d dvd a \<and> d dvd b \<longleftrightarrow> is_unit d)"  | 
|
| 59061 | 744  | 
by (auto intro: coprimeI gcd_greatest dvd_gcd_D1 dvd_gcd_D2)  | 
| 58023 | 745  | 
|
746  | 
lemma div_gcd_coprime:  | 
|
747  | 
assumes nz: "a \<noteq> 0 \<or> b \<noteq> 0"  | 
|
748  | 
defines [simp]: "d \<equiv> gcd a b"  | 
|
749  | 
defines [simp]: "a' \<equiv> a div d" and [simp]: "b' \<equiv> b div d"  | 
|
750  | 
shows "gcd a' b' = 1"  | 
|
751  | 
proof (rule coprimeI)  | 
|
752  | 
fix l assume "l dvd a'" "l dvd b'"  | 
|
753  | 
then obtain s t where "a' = l * s" "b' = l * t" unfolding dvd_def by blast  | 
|
| 
59009
 
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
 
haftmann 
parents: 
58953 
diff
changeset
 | 
754  | 
moreover have "a = a' * d" "b = b' * d" by simp_all  | 
| 58023 | 755  | 
ultimately have "a = (l * d) * s" "b = (l * d) * t"  | 
| 
59009
 
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
 
haftmann 
parents: 
58953 
diff
changeset
 | 
756  | 
by (simp_all only: ac_simps)  | 
| 58023 | 757  | 
hence "l*d dvd a" and "l*d dvd b" by (simp_all only: dvd_triv_left)  | 
758  | 
hence "l*d dvd d" by (simp add: gcd_greatest)  | 
|
| 
59009
 
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
 
haftmann 
parents: 
58953 
diff
changeset
 | 
759  | 
then obtain u where "d = l * d * u" ..  | 
| 
 
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
 
haftmann 
parents: 
58953 
diff
changeset
 | 
760  | 
then have "d * (l * u) = d" by (simp add: ac_simps)  | 
| 
 
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
 
haftmann 
parents: 
58953 
diff
changeset
 | 
761  | 
moreover from nz have "d \<noteq> 0" by simp  | 
| 
 
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
 
haftmann 
parents: 
58953 
diff
changeset
 | 
762  | 
with div_mult_self1_is_id have "d * (l * u) div d = l * u" .  | 
| 
 
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
 
haftmann 
parents: 
58953 
diff
changeset
 | 
763  | 
ultimately have "1 = l * u"  | 
| 
 
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
 
haftmann 
parents: 
58953 
diff
changeset
 | 
764  | 
using `d \<noteq> 0` by simp  | 
| 
 
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
 
haftmann 
parents: 
58953 
diff
changeset
 | 
765  | 
then show "l dvd 1" ..  | 
| 58023 | 766  | 
qed  | 
767  | 
||
768  | 
lemma coprime_mult:  | 
|
769  | 
assumes da: "gcd d a = 1" and db: "gcd d b = 1"  | 
|
770  | 
shows "gcd d (a * b) = 1"  | 
|
771  | 
apply (subst gcd.commute)  | 
|
772  | 
using da apply (subst gcd_mult_cancel)  | 
|
773  | 
apply (subst gcd.commute, assumption)  | 
|
774  | 
apply (subst gcd.commute, rule db)  | 
|
775  | 
done  | 
|
776  | 
||
777  | 
lemma coprime_lmult:  | 
|
778  | 
assumes dab: "gcd d (a * b) = 1"  | 
|
779  | 
shows "gcd d a = 1"  | 
|
780  | 
proof (rule coprimeI)  | 
|
781  | 
fix l assume "l dvd d" and "l dvd a"  | 
|
782  | 
hence "l dvd a * b" by simp  | 
|
783  | 
with `l dvd d` and dab show "l dvd 1" by (auto intro: gcd_greatest)  | 
|
784  | 
qed  | 
|
785  | 
||
786  | 
lemma coprime_rmult:  | 
|
787  | 
assumes dab: "gcd d (a * b) = 1"  | 
|
788  | 
shows "gcd d b = 1"  | 
|
789  | 
proof (rule coprimeI)  | 
|
790  | 
fix l assume "l dvd d" and "l dvd b"  | 
|
791  | 
hence "l dvd a * b" by simp  | 
|
792  | 
with `l dvd d` and dab show "l dvd 1" by (auto intro: gcd_greatest)  | 
|
793  | 
qed  | 
|
794  | 
||
795  | 
lemma coprime_mul_eq: "gcd d (a * b) = 1 \<longleftrightarrow> gcd d a = 1 \<and> gcd d b = 1"  | 
|
796  | 
using coprime_rmult[of d a b] coprime_lmult[of d a b] coprime_mult[of d a b] by blast  | 
|
797  | 
||
798  | 
lemma gcd_coprime:  | 
|
799  | 
assumes z: "gcd a b \<noteq> 0" and a: "a = a' * gcd a b" and b: "b = b' * gcd a b"  | 
|
800  | 
shows "gcd a' b' = 1"  | 
|
801  | 
proof -  | 
|
| 
59009
 
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
 
haftmann 
parents: 
58953 
diff
changeset
 | 
802  | 
from z have "a \<noteq> 0 \<or> b \<noteq> 0" by simp  | 
| 58023 | 803  | 
with div_gcd_coprime have "gcd (a div gcd a b) (b div gcd a b) = 1" .  | 
804  | 
also from assms have "a div gcd a b = a'" by (metis div_mult_self2_is_id)+  | 
|
805  | 
also from assms have "b div gcd a b = b'" by (metis div_mult_self2_is_id)+  | 
|
806  | 
finally show ?thesis .  | 
|
807  | 
qed  | 
|
808  | 
||
809  | 
lemma coprime_power:  | 
|
810  | 
assumes "0 < n"  | 
|
811  | 
shows "gcd a (b ^ n) = 1 \<longleftrightarrow> gcd a b = 1"  | 
|
812  | 
using assms proof (induct n)  | 
|
813  | 
case (Suc n) then show ?case  | 
|
814  | 
by (cases n) (simp_all add: coprime_mul_eq)  | 
|
815  | 
qed simp  | 
|
816  | 
||
817  | 
lemma gcd_coprime_exists:  | 
|
818  | 
assumes nz: "gcd a b \<noteq> 0"  | 
|
819  | 
shows "\<exists>a' b'. a = a' * gcd a b \<and> b = b' * gcd a b \<and> gcd a' b' = 1"  | 
|
820  | 
apply (rule_tac x = "a div gcd a b" in exI)  | 
|
821  | 
apply (rule_tac x = "b div gcd a b" in exI)  | 
|
| 
59009
 
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
 
haftmann 
parents: 
58953 
diff
changeset
 | 
822  | 
apply (insert nz, auto intro: div_gcd_coprime)  | 
| 58023 | 823  | 
done  | 
824  | 
||
825  | 
lemma coprime_exp:  | 
|
826  | 
"gcd d a = 1 \<Longrightarrow> gcd d (a^n) = 1"  | 
|
827  | 
by (induct n, simp_all add: coprime_mult)  | 
|
828  | 
||
829  | 
lemma coprime_exp2 [intro]:  | 
|
830  | 
"gcd a b = 1 \<Longrightarrow> gcd (a^n) (b^m) = 1"  | 
|
831  | 
apply (rule coprime_exp)  | 
|
832  | 
apply (subst gcd.commute)  | 
|
833  | 
apply (rule coprime_exp)  | 
|
834  | 
apply (subst gcd.commute)  | 
|
835  | 
apply assumption  | 
|
836  | 
done  | 
|
837  | 
||
838  | 
lemma gcd_exp:  | 
|
839  | 
"gcd (a^n) (b^n) = (gcd a b) ^ n"  | 
|
840  | 
proof (cases "a = 0 \<and> b = 0")  | 
|
841  | 
assume "a = 0 \<and> b = 0"  | 
|
842  | 
then show ?thesis by (cases n, simp_all add: gcd_0_left)  | 
|
843  | 
next  | 
|
844  | 
assume A: "\<not>(a = 0 \<and> b = 0)"  | 
|
845  | 
hence "1 = gcd ((a div gcd a b)^n) ((b div gcd a b)^n)"  | 
|
846  | 
using div_gcd_coprime by (subst sym, auto simp: div_gcd_coprime)  | 
|
847  | 
hence "(gcd a b) ^ n = (gcd a b) ^ n * ..." by simp  | 
|
848  | 
also note gcd_mult_distrib  | 
|
849  | 
also have "normalisation_factor ((gcd a b)^n) = 1"  | 
|
850  | 
by (simp add: normalisation_factor_pow A)  | 
|
851  | 
also have "(gcd a b)^n * (a div gcd a b)^n = a^n"  | 
|
852  | 
by (subst ac_simps, subst div_power, simp, rule dvd_div_mult_self, rule dvd_power_same, simp)  | 
|
853  | 
also have "(gcd a b)^n * (b div gcd a b)^n = b^n"  | 
|
854  | 
by (subst ac_simps, subst div_power, simp, rule dvd_div_mult_self, rule dvd_power_same, simp)  | 
|
855  | 
finally show ?thesis by simp  | 
|
856  | 
qed  | 
|
857  | 
||
858  | 
lemma coprime_common_divisor:  | 
|
859  | 
"gcd a b = 1 \<Longrightarrow> x dvd a \<Longrightarrow> x dvd b \<Longrightarrow> is_unit x"  | 
|
860  | 
apply (subgoal_tac "x dvd gcd a b")  | 
|
| 59061 | 861  | 
apply simp  | 
| 58023 | 862  | 
apply (erule (1) gcd_greatest)  | 
863  | 
done  | 
|
864  | 
||
865  | 
lemma division_decomp:  | 
|
866  | 
assumes dc: "a dvd b * c"  | 
|
867  | 
shows "\<exists>b' c'. a = b' * c' \<and> b' dvd b \<and> c' dvd c"  | 
|
868  | 
proof (cases "gcd a b = 0")  | 
|
869  | 
assume "gcd a b = 0"  | 
|
| 
59009
 
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
 
haftmann 
parents: 
58953 
diff
changeset
 | 
870  | 
hence "a = 0 \<and> b = 0" by simp  | 
| 58023 | 871  | 
hence "a = 0 * c \<and> 0 dvd b \<and> c dvd c" by simp  | 
872  | 
then show ?thesis by blast  | 
|
873  | 
next  | 
|
874  | 
let ?d = "gcd a b"  | 
|
875  | 
assume "?d \<noteq> 0"  | 
|
876  | 
from gcd_coprime_exists[OF this]  | 
|
877  | 
obtain a' b' where ab': "a = a' * ?d" "b = b' * ?d" "gcd a' b' = 1"  | 
|
878  | 
by blast  | 
|
879  | 
from ab'(1) have "a' dvd a" unfolding dvd_def by blast  | 
|
880  | 
with dc have "a' dvd b*c" using dvd_trans[of a' a "b*c"] by simp  | 
|
881  | 
from dc ab'(1,2) have "a'*?d dvd (b'*?d) * c" by simp  | 
|
882  | 
hence "?d * a' dvd ?d * (b' * c)" by (simp add: mult_ac)  | 
|
| 
59009
 
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
 
haftmann 
parents: 
58953 
diff
changeset
 | 
883  | 
with `?d \<noteq> 0` have "a' dvd b' * c" by simp  | 
| 58023 | 884  | 
with coprime_dvd_mult[OF ab'(3)]  | 
885  | 
have "a' dvd c" by (subst (asm) ac_simps, blast)  | 
|
886  | 
with ab'(1) have "a = ?d * a' \<and> ?d dvd b \<and> a' dvd c" by (simp add: mult_ac)  | 
|
887  | 
then show ?thesis by blast  | 
|
888  | 
qed  | 
|
889  | 
||
890  | 
lemma pow_divides_pow:  | 
|
891  | 
assumes ab: "a ^ n dvd b ^ n" and n: "n \<noteq> 0"  | 
|
892  | 
shows "a dvd b"  | 
|
893  | 
proof (cases "gcd a b = 0")  | 
|
894  | 
assume "gcd a b = 0"  | 
|
| 
59009
 
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
 
haftmann 
parents: 
58953 
diff
changeset
 | 
895  | 
then show ?thesis by simp  | 
| 58023 | 896  | 
next  | 
897  | 
let ?d = "gcd a b"  | 
|
898  | 
assume "?d \<noteq> 0"  | 
|
899  | 
from n obtain m where m: "n = Suc m" by (cases n, simp_all)  | 
|
| 
59009
 
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
 
haftmann 
parents: 
58953 
diff
changeset
 | 
900  | 
from `?d \<noteq> 0` have zn: "?d ^ n \<noteq> 0" by (rule power_not_zero)  | 
| 58023 | 901  | 
from gcd_coprime_exists[OF `?d \<noteq> 0`]  | 
902  | 
obtain a' b' where ab': "a = a' * ?d" "b = b' * ?d" "gcd a' b' = 1"  | 
|
903  | 
by blast  | 
|
904  | 
from ab have "(a' * ?d) ^ n dvd (b' * ?d) ^ n"  | 
|
905  | 
by (simp add: ab'(1,2)[symmetric])  | 
|
906  | 
hence "?d^n * a'^n dvd ?d^n * b'^n"  | 
|
907  | 
by (simp only: power_mult_distrib ac_simps)  | 
|
| 
59009
 
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
 
haftmann 
parents: 
58953 
diff
changeset
 | 
908  | 
with zn have "a'^n dvd b'^n" by simp  | 
| 58023 | 909  | 
hence "a' dvd b'^n" using dvd_trans[of a' "a'^n" "b'^n"] by (simp add: m)  | 
910  | 
hence "a' dvd b'^m * b'" by (simp add: m ac_simps)  | 
|
911  | 
with coprime_dvd_mult[OF coprime_exp[OF ab'(3), of m]]  | 
|
912  | 
have "a' dvd b'" by (subst (asm) ac_simps, blast)  | 
|
913  | 
hence "a'*?d dvd b'*?d" by (rule mult_dvd_mono, simp)  | 
|
914  | 
with ab'(1,2) show ?thesis by simp  | 
|
915  | 
qed  | 
|
916  | 
||
917  | 
lemma pow_divides_eq [simp]:  | 
|
918  | 
"n \<noteq> 0 \<Longrightarrow> a ^ n dvd b ^ n \<longleftrightarrow> a dvd b"  | 
|
919  | 
by (auto intro: pow_divides_pow dvd_power_same)  | 
|
920  | 
||
921  | 
lemma divides_mult:  | 
|
922  | 
assumes mr: "m dvd r" and nr: "n dvd r" and mn: "gcd m n = 1"  | 
|
923  | 
shows "m * n dvd r"  | 
|
924  | 
proof -  | 
|
925  | 
from mr nr obtain m' n' where m': "r = m*m'" and n': "r = n*n'"  | 
|
926  | 
unfolding dvd_def by blast  | 
|
927  | 
from mr n' have "m dvd n'*n" by (simp add: ac_simps)  | 
|
928  | 
hence "m dvd n'" using coprime_dvd_mult_iff[OF mn] by simp  | 
|
929  | 
then obtain k where k: "n' = m*k" unfolding dvd_def by blast  | 
|
930  | 
with n' have "r = m * n * k" by (simp add: mult_ac)  | 
|
931  | 
then show ?thesis unfolding dvd_def by blast  | 
|
932  | 
qed  | 
|
933  | 
||
934  | 
lemma coprime_plus_one [simp]: "gcd (n + 1) n = 1"  | 
|
935  | 
by (subst add_commute, simp)  | 
|
936  | 
||
937  | 
lemma setprod_coprime [rule_format]:  | 
|
938  | 
"(\<forall>i\<in>A. gcd (f i) x = 1) \<longrightarrow> gcd (\<Prod>i\<in>A. f i) x = 1"  | 
|
939  | 
apply (cases "finite A")  | 
|
940  | 
apply (induct set: finite)  | 
|
941  | 
apply (auto simp add: gcd_mult_cancel)  | 
|
942  | 
done  | 
|
943  | 
||
944  | 
lemma coprime_divisors:  | 
|
945  | 
assumes "d dvd a" "e dvd b" "gcd a b = 1"  | 
|
946  | 
shows "gcd d e = 1"  | 
|
947  | 
proof -  | 
|
948  | 
from assms obtain k l where "a = d * k" "b = e * l"  | 
|
949  | 
unfolding dvd_def by blast  | 
|
950  | 
with assms have "gcd (d * k) (e * l) = 1" by simp  | 
|
951  | 
hence "gcd (d * k) e = 1" by (rule coprime_lmult)  | 
|
952  | 
also have "gcd (d * k) e = gcd e (d * k)" by (simp add: ac_simps)  | 
|
953  | 
finally have "gcd e d = 1" by (rule coprime_lmult)  | 
|
954  | 
then show ?thesis by (simp add: ac_simps)  | 
|
955  | 
qed  | 
|
956  | 
||
957  | 
lemma invertible_coprime:  | 
|
| 
59009
 
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
 
haftmann 
parents: 
58953 
diff
changeset
 | 
958  | 
assumes "x * y mod m = 1"  | 
| 
 
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
 
haftmann 
parents: 
58953 
diff
changeset
 | 
959  | 
shows "coprime x m"  | 
| 
 
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
 
haftmann 
parents: 
58953 
diff
changeset
 | 
960  | 
proof -  | 
| 
 
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
 
haftmann 
parents: 
58953 
diff
changeset
 | 
961  | 
from assms have "coprime m (x * y mod m)"  | 
| 
 
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
 
haftmann 
parents: 
58953 
diff
changeset
 | 
962  | 
by simp  | 
| 
 
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
 
haftmann 
parents: 
58953 
diff
changeset
 | 
963  | 
then have "coprime m (x * y)"  | 
| 
 
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
 
haftmann 
parents: 
58953 
diff
changeset
 | 
964  | 
by simp  | 
| 
 
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
 
haftmann 
parents: 
58953 
diff
changeset
 | 
965  | 
then have "coprime m x"  | 
| 
 
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
 
haftmann 
parents: 
58953 
diff
changeset
 | 
966  | 
by (rule coprime_lmult)  | 
| 
 
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
 
haftmann 
parents: 
58953 
diff
changeset
 | 
967  | 
then show ?thesis  | 
| 
 
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
 
haftmann 
parents: 
58953 
diff
changeset
 | 
968  | 
by (simp add: ac_simps)  | 
| 
 
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
 
haftmann 
parents: 
58953 
diff
changeset
 | 
969  | 
qed  | 
| 58023 | 970  | 
|
971  | 
lemma lcm_gcd:  | 
|
972  | 
"lcm a b = a * b div (gcd a b * normalisation_factor (a*b))"  | 
|
973  | 
by (simp only: lcm_lcm_eucl gcd_gcd_eucl lcm_eucl_def)  | 
|
974  | 
||
975  | 
lemma lcm_gcd_prod:  | 
|
976  | 
"lcm a b * gcd a b = a * b div normalisation_factor (a*b)"  | 
|
977  | 
proof (cases "a * b = 0")  | 
|
978  | 
let ?nf = normalisation_factor  | 
|
979  | 
assume "a * b \<noteq> 0"  | 
|
| 58953 | 980  | 
hence "gcd a b \<noteq> 0" by simp  | 
| 58023 | 981  | 
from lcm_gcd have "lcm a b * gcd a b = gcd a b * (a * b div (?nf (a*b) * gcd a b))"  | 
982  | 
by (simp add: mult_ac)  | 
|
983  | 
also from `a * b \<noteq> 0` have "... = a * b div ?nf (a*b)"  | 
|
| 58953 | 984  | 
by (simp_all add: unit_ring_inv'1 unit_ring_inv)  | 
| 58023 | 985  | 
finally show ?thesis .  | 
| 58953 | 986  | 
qed (auto simp add: lcm_gcd)  | 
| 58023 | 987  | 
|
988  | 
lemma lcm_dvd1 [iff]:  | 
|
989  | 
"x dvd lcm x y"  | 
|
990  | 
proof (cases "x*y = 0")  | 
|
991  | 
assume "x * y \<noteq> 0"  | 
|
| 58953 | 992  | 
hence "gcd x y \<noteq> 0" by simp  | 
| 58023 | 993  | 
let ?c = "ring_inv (normalisation_factor (x*y))"  | 
994  | 
from `x * y \<noteq> 0` have [simp]: "is_unit (normalisation_factor (x*y))" by simp  | 
|
995  | 
from lcm_gcd_prod[of x y] have "lcm x y * gcd x y = x * ?c * y"  | 
|
996  | 
by (simp add: mult_ac unit_ring_inv)  | 
|
997  | 
hence "lcm x y * gcd x y div gcd x y = x * ?c * y div gcd x y" by simp  | 
|
998  | 
with `gcd x y \<noteq> 0` have "lcm x y = x * ?c * y div gcd x y"  | 
|
999  | 
by (subst (asm) div_mult_self2_is_id, simp_all)  | 
|
1000  | 
also have "... = x * (?c * y div gcd x y)"  | 
|
1001  | 
by (metis div_mult_swap gcd_dvd2 mult_assoc)  | 
|
1002  | 
finally show ?thesis by (rule dvdI)  | 
|
| 58953 | 1003  | 
qed (auto simp add: lcm_gcd)  | 
| 58023 | 1004  | 
|
1005  | 
lemma lcm_least:  | 
|
1006  | 
"\<lbrakk>a dvd k; b dvd k\<rbrakk> \<Longrightarrow> lcm a b dvd k"  | 
|
1007  | 
proof (cases "k = 0")  | 
|
1008  | 
let ?nf = normalisation_factor  | 
|
1009  | 
assume "k \<noteq> 0"  | 
|
1010  | 
hence "is_unit (?nf k)" by simp  | 
|
1011  | 
hence "?nf k \<noteq> 0" by (metis not_is_unit_0)  | 
|
1012  | 
assume A: "a dvd k" "b dvd k"  | 
|
| 58953 | 1013  | 
hence "gcd a b \<noteq> 0" using `k \<noteq> 0` by auto  | 
| 58023 | 1014  | 
from A obtain r s where ar: "k = a * r" and bs: "k = b * s"  | 
1015  | 
unfolding dvd_def by blast  | 
|
| 58953 | 1016  | 
with `k \<noteq> 0` have "r * s \<noteq> 0"  | 
1017  | 
by auto (drule sym [of 0], simp)  | 
|
| 58023 | 1018  | 
hence "is_unit (?nf (r * s))" by simp  | 
1019  | 
let ?c = "?nf k div ?nf (r*s)"  | 
|
1020  | 
from `is_unit (?nf k)` and `is_unit (?nf (r * s))` have "is_unit ?c" by (rule unit_div)  | 
|
1021  | 
hence "?c \<noteq> 0" using not_is_unit_0 by fast  | 
|
1022  | 
from ar bs have "k * k * gcd s r = ?nf k * k * gcd (k * s) (k * r)"  | 
|
| 58953 | 1023  | 
by (subst mult_assoc, subst gcd_mult_distrib[of k s r], simp only: ac_simps)  | 
| 58023 | 1024  | 
also have "... = ?nf k * k * gcd ((r*s) * a) ((r*s) * b)"  | 
1025  | 
by (subst (3) `k = a * r`, subst (3) `k = b * s`, simp add: algebra_simps)  | 
|
1026  | 
also have "... = ?c * r*s * k * gcd a b" using `r * s \<noteq> 0`  | 
|
1027  | 
by (subst gcd_mult_distrib'[symmetric], simp add: algebra_simps unit_simps)  | 
|
1028  | 
finally have "(a*r) * (b*s) * gcd s r = ?c * k * r * s * gcd a b"  | 
|
1029  | 
by (subst ar[symmetric], subst bs[symmetric], simp add: mult_ac)  | 
|
1030  | 
hence "a * b * gcd s r * (r * s) = ?c * k * gcd a b * (r * s)"  | 
|
1031  | 
by (simp add: algebra_simps)  | 
|
1032  | 
hence "?c * k * gcd a b = a * b * gcd s r" using `r * s \<noteq> 0`  | 
|
1033  | 
by (metis div_mult_self2_is_id)  | 
|
1034  | 
also have "... = lcm a b * gcd a b * gcd s r * ?nf (a*b)"  | 
|
1035  | 
by (subst lcm_gcd_prod[of a b], metis gcd_mult_distrib gcd_mult_distrib')  | 
|
1036  | 
also have "... = lcm a b * gcd s r * ?nf (a*b) * gcd a b"  | 
|
1037  | 
by (simp add: algebra_simps)  | 
|
1038  | 
finally have "k * ?c = lcm a b * gcd s r * ?nf (a*b)" using `gcd a b \<noteq> 0`  | 
|
1039  | 
by (metis mult.commute div_mult_self2_is_id)  | 
|
1040  | 
hence "k = lcm a b * (gcd s r * ?nf (a*b)) div ?c" using `?c \<noteq> 0`  | 
|
1041  | 
by (metis div_mult_self2_is_id mult_assoc)  | 
|
1042  | 
also have "... = lcm a b * (gcd s r * ?nf (a*b) div ?c)" using `is_unit ?c`  | 
|
1043  | 
by (simp add: unit_simps)  | 
|
1044  | 
finally show ?thesis by (rule dvdI)  | 
|
1045  | 
qed simp  | 
|
1046  | 
||
1047  | 
lemma lcm_zero:  | 
|
1048  | 
"lcm a b = 0 \<longleftrightarrow> a = 0 \<or> b = 0"  | 
|
1049  | 
proof -  | 
|
1050  | 
let ?nf = normalisation_factor  | 
|
1051  | 
  {
 | 
|
1052  | 
assume "a \<noteq> 0" "b \<noteq> 0"  | 
|
1053  | 
hence "a * b div ?nf (a * b) \<noteq> 0" by (simp add: no_zero_divisors)  | 
|
| 
59009
 
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
 
haftmann 
parents: 
58953 
diff
changeset
 | 
1054  | 
moreover from `a \<noteq> 0` and `b \<noteq> 0` have "gcd a b \<noteq> 0" by simp  | 
| 58023 | 1055  | 
ultimately have "lcm a b \<noteq> 0" using lcm_gcd_prod[of a b] by (intro notI, simp)  | 
1056  | 
  } moreover {
 | 
|
1057  | 
assume "a = 0 \<or> b = 0"  | 
|
1058  | 
hence "lcm a b = 0" by (elim disjE, simp_all add: lcm_gcd)  | 
|
1059  | 
}  | 
|
1060  | 
ultimately show ?thesis by blast  | 
|
1061  | 
qed  | 
|
1062  | 
||
1063  | 
lemmas lcm_0_iff = lcm_zero  | 
|
1064  | 
||
1065  | 
lemma gcd_lcm:  | 
|
1066  | 
assumes "lcm a b \<noteq> 0"  | 
|
1067  | 
shows "gcd a b = a * b div (lcm a b * normalisation_factor (a * b))"  | 
|
1068  | 
proof-  | 
|
| 
59009
 
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
 
haftmann 
parents: 
58953 
diff
changeset
 | 
1069  | 
from assms have "gcd a b \<noteq> 0" by (simp add: lcm_zero)  | 
| 58023 | 1070  | 
let ?c = "normalisation_factor (a*b)"  | 
1071  | 
from `lcm a b \<noteq> 0` have "?c \<noteq> 0" by (intro notI, simp add: lcm_zero no_zero_divisors)  | 
|
1072  | 
hence "is_unit ?c" by simp  | 
|
1073  | 
from lcm_gcd_prod [of a b] have "gcd a b = a * b div ?c div lcm a b"  | 
|
1074  | 
by (subst (2) div_mult_self2_is_id[OF `lcm a b \<noteq> 0`, symmetric], simp add: mult_ac)  | 
|
1075  | 
also from `is_unit ?c` have "... = a * b div (?c * lcm a b)"  | 
|
1076  | 
by (simp only: unit_ring_inv'1 unit_ring_inv)  | 
|
1077  | 
finally show ?thesis by (simp only: ac_simps)  | 
|
1078  | 
qed  | 
|
1079  | 
||
1080  | 
lemma normalisation_factor_lcm [simp]:  | 
|
1081  | 
"normalisation_factor (lcm a b) = (if a = 0 \<or> b = 0 then 0 else 1)"  | 
|
1082  | 
proof (cases "a = 0 \<or> b = 0")  | 
|
1083  | 
case True then show ?thesis  | 
|
| 58953 | 1084  | 
by (auto simp add: lcm_gcd)  | 
| 58023 | 1085  | 
next  | 
1086  | 
case False  | 
|
1087  | 
let ?nf = normalisation_factor  | 
|
1088  | 
from lcm_gcd_prod[of a b]  | 
|
1089  | 
have "?nf (lcm a b) * ?nf (gcd a b) = ?nf (a*b) div ?nf (a*b)"  | 
|
1090  | 
by (metis div_by_0 div_self normalisation_correct normalisation_factor_0 normalisation_factor_mult)  | 
|
1091  | 
also have "... = (if a*b = 0 then 0 else 1)"  | 
|
| 58953 | 1092  | 
by simp  | 
1093  | 
finally show ?thesis using False by simp  | 
|
| 58023 | 1094  | 
qed  | 
1095  | 
||
1096  | 
lemma lcm_dvd2 [iff]: "y dvd lcm x y"  | 
|
1097  | 
using lcm_dvd1 [of y x] by (simp add: lcm_gcd ac_simps)  | 
|
1098  | 
||
1099  | 
lemma lcmI:  | 
|
1100  | 
"\<lbrakk>x dvd k; y dvd k; \<And>l. x dvd l \<Longrightarrow> y dvd l \<Longrightarrow> k dvd l;  | 
|
1101  | 
normalisation_factor k = (if k = 0 then 0 else 1)\<rbrakk> \<Longrightarrow> k = lcm x y"  | 
|
1102  | 
by (intro normed_associated_imp_eq) (auto simp: associated_def intro: lcm_least)  | 
|
1103  | 
||
1104  | 
sublocale lcm!: abel_semigroup lcm  | 
|
1105  | 
proof  | 
|
1106  | 
fix x y z  | 
|
1107  | 
show "lcm (lcm x y) z = lcm x (lcm y z)"  | 
|
1108  | 
proof (rule lcmI)  | 
|
1109  | 
have "x dvd lcm x y" and "lcm x y dvd lcm (lcm x y) z" by simp_all  | 
|
1110  | 
then show "x dvd lcm (lcm x y) z" by (rule dvd_trans)  | 
|
1111  | 
||
1112  | 
have "y dvd lcm x y" and "lcm x y dvd lcm (lcm x y) z" by simp_all  | 
|
1113  | 
hence "y dvd lcm (lcm x y) z" by (rule dvd_trans)  | 
|
1114  | 
moreover have "z dvd lcm (lcm x y) z" by simp  | 
|
1115  | 
ultimately show "lcm y z dvd lcm (lcm x y) z" by (rule lcm_least)  | 
|
1116  | 
||
1117  | 
fix l assume "x dvd l" and "lcm y z dvd l"  | 
|
1118  | 
have "y dvd lcm y z" by simp  | 
|
1119  | 
from this and `lcm y z dvd l` have "y dvd l" by (rule dvd_trans)  | 
|
1120  | 
have "z dvd lcm y z" by simp  | 
|
1121  | 
from this and `lcm y z dvd l` have "z dvd l" by (rule dvd_trans)  | 
|
1122  | 
from `x dvd l` and `y dvd l` have "lcm x y dvd l" by (rule lcm_least)  | 
|
1123  | 
from this and `z dvd l` show "lcm (lcm x y) z dvd l" by (rule lcm_least)  | 
|
1124  | 
qed (simp add: lcm_zero)  | 
|
1125  | 
next  | 
|
1126  | 
fix x y  | 
|
1127  | 
show "lcm x y = lcm y x"  | 
|
1128  | 
by (simp add: lcm_gcd ac_simps)  | 
|
1129  | 
qed  | 
|
1130  | 
||
1131  | 
lemma dvd_lcm_D1:  | 
|
1132  | 
"lcm m n dvd k \<Longrightarrow> m dvd k"  | 
|
1133  | 
by (rule dvd_trans, rule lcm_dvd1, assumption)  | 
|
1134  | 
||
1135  | 
lemma dvd_lcm_D2:  | 
|
1136  | 
"lcm m n dvd k \<Longrightarrow> n dvd k"  | 
|
1137  | 
by (rule dvd_trans, rule lcm_dvd2, assumption)  | 
|
1138  | 
||
1139  | 
lemma gcd_dvd_lcm [simp]:  | 
|
1140  | 
"gcd a b dvd lcm a b"  | 
|
1141  | 
by (metis dvd_trans gcd_dvd2 lcm_dvd2)  | 
|
1142  | 
||
1143  | 
lemma lcm_1_iff:  | 
|
1144  | 
"lcm a b = 1 \<longleftrightarrow> is_unit a \<and> is_unit b"  | 
|
1145  | 
proof  | 
|
1146  | 
assume "lcm a b = 1"  | 
|
| 59061 | 1147  | 
then show "is_unit a \<and> is_unit b" by auto  | 
| 58023 | 1148  | 
next  | 
1149  | 
assume "is_unit a \<and> is_unit b"  | 
|
| 59061 | 1150  | 
hence "a dvd 1" and "b dvd 1" by simp_all  | 
1151  | 
hence "is_unit (lcm a b)" by (rule lcm_least)  | 
|
| 58023 | 1152  | 
hence "lcm a b = normalisation_factor (lcm a b)"  | 
1153  | 
by (subst normalisation_factor_unit, simp_all)  | 
|
| 59061 | 1154  | 
also have "\<dots> = 1" using `is_unit a \<and> is_unit b`  | 
1155  | 
by auto  | 
|
| 58023 | 1156  | 
finally show "lcm a b = 1" .  | 
1157  | 
qed  | 
|
1158  | 
||
1159  | 
lemma lcm_0_left [simp]:  | 
|
1160  | 
"lcm 0 x = 0"  | 
|
1161  | 
by (rule sym, rule lcmI, simp_all)  | 
|
1162  | 
||
1163  | 
lemma lcm_0 [simp]:  | 
|
1164  | 
"lcm x 0 = 0"  | 
|
1165  | 
by (rule sym, rule lcmI, simp_all)  | 
|
1166  | 
||
1167  | 
lemma lcm_unique:  | 
|
1168  | 
"a dvd d \<and> b dvd d \<and>  | 
|
1169  | 
normalisation_factor d = (if d = 0 then 0 else 1) \<and>  | 
|
1170  | 
(\<forall>e. a dvd e \<and> b dvd e \<longrightarrow> d dvd e) \<longleftrightarrow> d = lcm a b"  | 
|
1171  | 
by (rule, auto intro: lcmI simp: lcm_least lcm_zero)  | 
|
1172  | 
||
1173  | 
lemma dvd_lcm_I1 [simp]:  | 
|
1174  | 
"k dvd m \<Longrightarrow> k dvd lcm m n"  | 
|
1175  | 
by (metis lcm_dvd1 dvd_trans)  | 
|
1176  | 
||
1177  | 
lemma dvd_lcm_I2 [simp]:  | 
|
1178  | 
"k dvd n \<Longrightarrow> k dvd lcm m n"  | 
|
1179  | 
by (metis lcm_dvd2 dvd_trans)  | 
|
1180  | 
||
1181  | 
lemma lcm_1_left [simp]:  | 
|
1182  | 
"lcm 1 x = x div normalisation_factor x"  | 
|
1183  | 
by (cases "x = 0") (simp, rule sym, rule lcmI, simp_all)  | 
|
1184  | 
||
1185  | 
lemma lcm_1_right [simp]:  | 
|
1186  | 
"lcm x 1 = x div normalisation_factor x"  | 
|
1187  | 
by (simp add: ac_simps)  | 
|
1188  | 
||
1189  | 
lemma lcm_coprime:  | 
|
1190  | 
"gcd a b = 1 \<Longrightarrow> lcm a b = a * b div normalisation_factor (a*b)"  | 
|
1191  | 
by (subst lcm_gcd) simp  | 
|
1192  | 
||
1193  | 
lemma lcm_proj1_if_dvd:  | 
|
1194  | 
"y dvd x \<Longrightarrow> lcm x y = x div normalisation_factor x"  | 
|
1195  | 
by (cases "x = 0") (simp, rule sym, rule lcmI, simp_all)  | 
|
1196  | 
||
1197  | 
lemma lcm_proj2_if_dvd:  | 
|
1198  | 
"x dvd y \<Longrightarrow> lcm x y = y div normalisation_factor y"  | 
|
1199  | 
using lcm_proj1_if_dvd [of x y] by (simp add: ac_simps)  | 
|
1200  | 
||
1201  | 
lemma lcm_proj1_iff:  | 
|
1202  | 
"lcm m n = m div normalisation_factor m \<longleftrightarrow> n dvd m"  | 
|
1203  | 
proof  | 
|
1204  | 
assume A: "lcm m n = m div normalisation_factor m"  | 
|
1205  | 
show "n dvd m"  | 
|
1206  | 
proof (cases "m = 0")  | 
|
1207  | 
assume [simp]: "m \<noteq> 0"  | 
|
1208  | 
from A have B: "m = lcm m n * normalisation_factor m"  | 
|
1209  | 
by (simp add: unit_eq_div2)  | 
|
1210  | 
show ?thesis by (subst B, simp)  | 
|
1211  | 
qed simp  | 
|
1212  | 
next  | 
|
1213  | 
assume "n dvd m"  | 
|
1214  | 
then show "lcm m n = m div normalisation_factor m" by (rule lcm_proj1_if_dvd)  | 
|
1215  | 
qed  | 
|
1216  | 
||
1217  | 
lemma lcm_proj2_iff:  | 
|
1218  | 
"lcm m n = n div normalisation_factor n \<longleftrightarrow> m dvd n"  | 
|
1219  | 
using lcm_proj1_iff [of n m] by (simp add: ac_simps)  | 
|
1220  | 
||
1221  | 
lemma euclidean_size_lcm_le1:  | 
|
1222  | 
assumes "a \<noteq> 0" and "b \<noteq> 0"  | 
|
1223  | 
shows "euclidean_size a \<le> euclidean_size (lcm a b)"  | 
|
1224  | 
proof -  | 
|
1225  | 
have "a dvd lcm a b" by (rule lcm_dvd1)  | 
|
1226  | 
then obtain c where A: "lcm a b = a * c" unfolding dvd_def by blast  | 
|
1227  | 
with `a \<noteq> 0` and `b \<noteq> 0` have "c \<noteq> 0" by (auto simp: lcm_zero)  | 
|
1228  | 
then show ?thesis by (subst A, intro size_mult_mono)  | 
|
1229  | 
qed  | 
|
1230  | 
||
1231  | 
lemma euclidean_size_lcm_le2:  | 
|
1232  | 
"a \<noteq> 0 \<Longrightarrow> b \<noteq> 0 \<Longrightarrow> euclidean_size b \<le> euclidean_size (lcm a b)"  | 
|
1233  | 
using euclidean_size_lcm_le1 [of b a] by (simp add: ac_simps)  | 
|
1234  | 
||
1235  | 
lemma euclidean_size_lcm_less1:  | 
|
1236  | 
assumes "b \<noteq> 0" and "\<not>b dvd a"  | 
|
1237  | 
shows "euclidean_size a < euclidean_size (lcm a b)"  | 
|
1238  | 
proof (rule ccontr)  | 
|
1239  | 
from assms have "a \<noteq> 0" by auto  | 
|
1240  | 
assume "\<not>euclidean_size a < euclidean_size (lcm a b)"  | 
|
1241  | 
with `a \<noteq> 0` and `b \<noteq> 0` have "euclidean_size (lcm a b) = euclidean_size a"  | 
|
1242  | 
by (intro le_antisym, simp, intro euclidean_size_lcm_le1)  | 
|
1243  | 
with assms have "lcm a b dvd a"  | 
|
1244  | 
by (rule_tac dvd_euclidean_size_eq_imp_dvd) (auto simp: lcm_zero)  | 
|
1245  | 
hence "b dvd a" by (rule dvd_lcm_D2)  | 
|
1246  | 
with `\<not>b dvd a` show False by contradiction  | 
|
1247  | 
qed  | 
|
1248  | 
||
1249  | 
lemma euclidean_size_lcm_less2:  | 
|
1250  | 
assumes "a \<noteq> 0" and "\<not>a dvd b"  | 
|
1251  | 
shows "euclidean_size b < euclidean_size (lcm a b)"  | 
|
1252  | 
using assms euclidean_size_lcm_less1 [of a b] by (simp add: ac_simps)  | 
|
1253  | 
||
1254  | 
lemma lcm_mult_unit1:  | 
|
1255  | 
"is_unit a \<Longrightarrow> lcm (x*a) y = lcm x y"  | 
|
1256  | 
apply (rule lcmI)  | 
|
1257  | 
apply (rule dvd_trans[of _ "x*a"], simp, rule lcm_dvd1)  | 
|
1258  | 
apply (rule lcm_dvd2)  | 
|
1259  | 
apply (rule lcm_least, simp add: unit_simps, assumption)  | 
|
1260  | 
apply (subst normalisation_factor_lcm, simp add: lcm_zero)  | 
|
1261  | 
done  | 
|
1262  | 
||
1263  | 
lemma lcm_mult_unit2:  | 
|
1264  | 
"is_unit a \<Longrightarrow> lcm x (y*a) = lcm x y"  | 
|
1265  | 
using lcm_mult_unit1 [of a y x] by (simp add: ac_simps)  | 
|
1266  | 
||
1267  | 
lemma lcm_div_unit1:  | 
|
1268  | 
"is_unit a \<Longrightarrow> lcm (x div a) y = lcm x y"  | 
|
1269  | 
by (simp add: unit_ring_inv lcm_mult_unit1)  | 
|
1270  | 
||
1271  | 
lemma lcm_div_unit2:  | 
|
1272  | 
"is_unit a \<Longrightarrow> lcm x (y div a) = lcm x y"  | 
|
1273  | 
by (simp add: unit_ring_inv lcm_mult_unit2)  | 
|
1274  | 
||
1275  | 
lemma lcm_left_idem:  | 
|
1276  | 
"lcm p (lcm p q) = lcm p q"  | 
|
1277  | 
apply (rule lcmI)  | 
|
1278  | 
apply simp  | 
|
1279  | 
apply (subst lcm.assoc [symmetric], rule lcm_dvd2)  | 
|
1280  | 
apply (rule lcm_least, assumption)  | 
|
1281  | 
apply (erule (1) lcm_least)  | 
|
1282  | 
apply (auto simp: lcm_zero)  | 
|
1283  | 
done  | 
|
1284  | 
||
1285  | 
lemma lcm_right_idem:  | 
|
1286  | 
"lcm (lcm p q) q = lcm p q"  | 
|
1287  | 
apply (rule lcmI)  | 
|
1288  | 
apply (subst lcm.assoc, rule lcm_dvd1)  | 
|
1289  | 
apply (rule lcm_dvd2)  | 
|
1290  | 
apply (rule lcm_least, erule (1) lcm_least, assumption)  | 
|
1291  | 
apply (auto simp: lcm_zero)  | 
|
1292  | 
done  | 
|
1293  | 
||
1294  | 
lemma comp_fun_idem_lcm: "comp_fun_idem lcm"  | 
|
1295  | 
proof  | 
|
1296  | 
fix a b show "lcm a \<circ> lcm b = lcm b \<circ> lcm a"  | 
|
1297  | 
by (simp add: fun_eq_iff ac_simps)  | 
|
1298  | 
next  | 
|
1299  | 
fix a show "lcm a \<circ> lcm a = lcm a" unfolding o_def  | 
|
1300  | 
by (intro ext, simp add: lcm_left_idem)  | 
|
1301  | 
qed  | 
|
1302  | 
||
1303  | 
lemma dvd_Lcm [simp]: "x \<in> A \<Longrightarrow> x dvd Lcm A"  | 
|
1304  | 
and Lcm_dvd [simp]: "(\<forall>x\<in>A. x dvd l') \<Longrightarrow> Lcm A dvd l'"  | 
|
1305  | 
and normalisation_factor_Lcm [simp]:  | 
|
1306  | 
"normalisation_factor (Lcm A) = (if Lcm A = 0 then 0 else 1)"  | 
|
1307  | 
proof -  | 
|
1308  | 
have "(\<forall>x\<in>A. x dvd Lcm A) \<and> (\<forall>l'. (\<forall>x\<in>A. x dvd l') \<longrightarrow> Lcm A dvd l') \<and>  | 
|
1309  | 
normalisation_factor (Lcm A) = (if Lcm A = 0 then 0 else 1)" (is ?thesis)  | 
|
1310  | 
proof (cases "\<exists>l. l \<noteq> 0 \<and> (\<forall>x\<in>A. x dvd l)")  | 
|
1311  | 
case False  | 
|
1312  | 
hence "Lcm A = 0" by (auto simp: Lcm_Lcm_eucl Lcm_eucl_def)  | 
|
1313  | 
with False show ?thesis by auto  | 
|
1314  | 
next  | 
|
1315  | 
case True  | 
|
1316  | 
then obtain l\<^sub>0 where l\<^sub>0_props: "l\<^sub>0 \<noteq> 0 \<and> (\<forall>x\<in>A. x dvd l\<^sub>0)" by blast  | 
|
1317  | 
def n \<equiv> "LEAST n. \<exists>l. l \<noteq> 0 \<and> (\<forall>x\<in>A. x dvd l) \<and> euclidean_size l = n"  | 
|
1318  | 
def l \<equiv> "SOME l. l \<noteq> 0 \<and> (\<forall>x\<in>A. x dvd l) \<and> euclidean_size l = n"  | 
|
1319  | 
have "\<exists>l. l \<noteq> 0 \<and> (\<forall>x\<in>A. x dvd l) \<and> euclidean_size l = n"  | 
|
1320  | 
apply (subst n_def)  | 
|
1321  | 
apply (rule LeastI[of _ "euclidean_size l\<^sub>0"])  | 
|
1322  | 
apply (rule exI[of _ l\<^sub>0])  | 
|
1323  | 
apply (simp add: l\<^sub>0_props)  | 
|
1324  | 
done  | 
|
1325  | 
from someI_ex[OF this] have "l \<noteq> 0" and "\<forall>x\<in>A. x dvd l" and "euclidean_size l = n"  | 
|
1326  | 
unfolding l_def by simp_all  | 
|
1327  | 
    {
 | 
|
1328  | 
fix l' assume "\<forall>x\<in>A. x dvd l'"  | 
|
1329  | 
with `\<forall>x\<in>A. x dvd l` have "\<forall>x\<in>A. x dvd gcd l l'" by (auto intro: gcd_greatest)  | 
|
| 
59009
 
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
 
haftmann 
parents: 
58953 
diff
changeset
 | 
1330  | 
moreover from `l \<noteq> 0` have "gcd l l' \<noteq> 0" by simp  | 
| 58023 | 1331  | 
ultimately have "\<exists>b. b \<noteq> 0 \<and> (\<forall>x\<in>A. x dvd b) \<and> euclidean_size b = euclidean_size (gcd l l')"  | 
1332  | 
by (intro exI[of _ "gcd l l'"], auto)  | 
|
1333  | 
hence "euclidean_size (gcd l l') \<ge> n" by (subst n_def) (rule Least_le)  | 
|
1334  | 
moreover have "euclidean_size (gcd l l') \<le> n"  | 
|
1335  | 
proof -  | 
|
1336  | 
have "gcd l l' dvd l" by simp  | 
|
1337  | 
then obtain a where "l = gcd l l' * a" unfolding dvd_def by blast  | 
|
1338  | 
with `l \<noteq> 0` have "a \<noteq> 0" by auto  | 
|
1339  | 
hence "euclidean_size (gcd l l') \<le> euclidean_size (gcd l l' * a)"  | 
|
1340  | 
by (rule size_mult_mono)  | 
|
1341  | 
also have "gcd l l' * a = l" using `l = gcd l l' * a` ..  | 
|
1342  | 
also note `euclidean_size l = n`  | 
|
1343  | 
finally show "euclidean_size (gcd l l') \<le> n" .  | 
|
1344  | 
qed  | 
|
1345  | 
ultimately have "euclidean_size l = euclidean_size (gcd l l')"  | 
|
1346  | 
by (intro le_antisym, simp_all add: `euclidean_size l = n`)  | 
|
1347  | 
with `l \<noteq> 0` have "l dvd gcd l l'" by (blast intro: dvd_euclidean_size_eq_imp_dvd)  | 
|
1348  | 
hence "l dvd l'" by (blast dest: dvd_gcd_D2)  | 
|
1349  | 
}  | 
|
1350  | 
||
1351  | 
with `(\<forall>x\<in>A. x dvd l)` and normalisation_factor_is_unit[OF `l \<noteq> 0`] and `l \<noteq> 0`  | 
|
1352  | 
have "(\<forall>x\<in>A. x dvd l div normalisation_factor l) \<and>  | 
|
1353  | 
(\<forall>l'. (\<forall>x\<in>A. x dvd l') \<longrightarrow> l div normalisation_factor l dvd l') \<and>  | 
|
1354  | 
normalisation_factor (l div normalisation_factor l) =  | 
|
1355  | 
(if l div normalisation_factor l = 0 then 0 else 1)"  | 
|
1356  | 
by (auto simp: unit_simps)  | 
|
1357  | 
also from True have "l div normalisation_factor l = Lcm A"  | 
|
1358  | 
by (simp add: Lcm_Lcm_eucl Lcm_eucl_def Let_def n_def l_def)  | 
|
1359  | 
finally show ?thesis .  | 
|
1360  | 
qed  | 
|
1361  | 
note A = this  | 
|
1362  | 
||
1363  | 
  {fix x assume "x \<in> A" then show "x dvd Lcm A" using A by blast}
 | 
|
1364  | 
  {fix l' assume "\<forall>x\<in>A. x dvd l'" then show "Lcm A dvd l'" using A by blast}
 | 
|
1365  | 
from A show "normalisation_factor (Lcm A) = (if Lcm A = 0 then 0 else 1)" by blast  | 
|
1366  | 
qed  | 
|
1367  | 
||
1368  | 
lemma LcmI:  | 
|
1369  | 
"(\<And>x. x\<in>A \<Longrightarrow> x dvd l) \<Longrightarrow> (\<And>l'. (\<forall>x\<in>A. x dvd l') \<Longrightarrow> l dvd l') \<Longrightarrow>  | 
|
1370  | 
normalisation_factor l = (if l = 0 then 0 else 1) \<Longrightarrow> l = Lcm A"  | 
|
1371  | 
by (intro normed_associated_imp_eq)  | 
|
1372  | 
(auto intro: Lcm_dvd dvd_Lcm simp: associated_def)  | 
|
1373  | 
||
1374  | 
lemma Lcm_subset:  | 
|
1375  | 
"A \<subseteq> B \<Longrightarrow> Lcm A dvd Lcm B"  | 
|
1376  | 
by (blast intro: Lcm_dvd dvd_Lcm)  | 
|
1377  | 
||
1378  | 
lemma Lcm_Un:  | 
|
1379  | 
"Lcm (A \<union> B) = lcm (Lcm A) (Lcm B)"  | 
|
1380  | 
apply (rule lcmI)  | 
|
1381  | 
apply (blast intro: Lcm_subset)  | 
|
1382  | 
apply (blast intro: Lcm_subset)  | 
|
1383  | 
apply (intro Lcm_dvd ballI, elim UnE)  | 
|
1384  | 
apply (rule dvd_trans, erule dvd_Lcm, assumption)  | 
|
1385  | 
apply (rule dvd_trans, erule dvd_Lcm, assumption)  | 
|
1386  | 
apply simp  | 
|
1387  | 
done  | 
|
1388  | 
||
1389  | 
lemma Lcm_1_iff:  | 
|
1390  | 
"Lcm A = 1 \<longleftrightarrow> (\<forall>x\<in>A. is_unit x)"  | 
|
1391  | 
proof  | 
|
1392  | 
assume "Lcm A = 1"  | 
|
| 59061 | 1393  | 
then show "\<forall>x\<in>A. is_unit x" by auto  | 
| 58023 | 1394  | 
qed (rule LcmI [symmetric], auto)  | 
1395  | 
||
1396  | 
lemma Lcm_no_units:  | 
|
1397  | 
  "Lcm A = Lcm (A - {x. is_unit x})"
 | 
|
1398  | 
proof -  | 
|
1399  | 
  have "(A - {x. is_unit x}) \<union> {x\<in>A. is_unit x} = A" by blast
 | 
|
1400  | 
  hence "Lcm A = lcm (Lcm (A - {x. is_unit x})) (Lcm {x\<in>A. is_unit x})"
 | 
|
1401  | 
by (simp add: Lcm_Un[symmetric])  | 
|
1402  | 
  also have "Lcm {x\<in>A. is_unit x} = 1" by (simp add: Lcm_1_iff)
 | 
|
1403  | 
finally show ?thesis by simp  | 
|
1404  | 
qed  | 
|
1405  | 
||
1406  | 
lemma Lcm_empty [simp]:  | 
|
1407  | 
  "Lcm {} = 1"
 | 
|
1408  | 
by (simp add: Lcm_1_iff)  | 
|
1409  | 
||
1410  | 
lemma Lcm_eq_0 [simp]:  | 
|
1411  | 
"0 \<in> A \<Longrightarrow> Lcm A = 0"  | 
|
1412  | 
by (drule dvd_Lcm) simp  | 
|
1413  | 
||
1414  | 
lemma Lcm0_iff':  | 
|
1415  | 
"Lcm A = 0 \<longleftrightarrow> \<not>(\<exists>l. l \<noteq> 0 \<and> (\<forall>x\<in>A. x dvd l))"  | 
|
1416  | 
proof  | 
|
1417  | 
assume "Lcm A = 0"  | 
|
1418  | 
show "\<not>(\<exists>l. l \<noteq> 0 \<and> (\<forall>x\<in>A. x dvd l))"  | 
|
1419  | 
proof  | 
|
1420  | 
assume ex: "\<exists>l. l \<noteq> 0 \<and> (\<forall>x\<in>A. x dvd l)"  | 
|
1421  | 
then obtain l\<^sub>0 where l\<^sub>0_props: "l\<^sub>0 \<noteq> 0 \<and> (\<forall>x\<in>A. x dvd l\<^sub>0)" by blast  | 
|
1422  | 
def n \<equiv> "LEAST n. \<exists>l. l \<noteq> 0 \<and> (\<forall>x\<in>A. x dvd l) \<and> euclidean_size l = n"  | 
|
1423  | 
def l \<equiv> "SOME l. l \<noteq> 0 \<and> (\<forall>x\<in>A. x dvd l) \<and> euclidean_size l = n"  | 
|
1424  | 
have "\<exists>l. l \<noteq> 0 \<and> (\<forall>x\<in>A. x dvd l) \<and> euclidean_size l = n"  | 
|
1425  | 
apply (subst n_def)  | 
|
1426  | 
apply (rule LeastI[of _ "euclidean_size l\<^sub>0"])  | 
|
1427  | 
apply (rule exI[of _ l\<^sub>0])  | 
|
1428  | 
apply (simp add: l\<^sub>0_props)  | 
|
1429  | 
done  | 
|
1430  | 
from someI_ex[OF this] have "l \<noteq> 0" unfolding l_def by simp_all  | 
|
1431  | 
hence "l div normalisation_factor l \<noteq> 0" by simp  | 
|
1432  | 
also from ex have "l div normalisation_factor l = Lcm A"  | 
|
1433  | 
by (simp only: Lcm_Lcm_eucl Lcm_eucl_def n_def l_def if_True Let_def)  | 
|
1434  | 
finally show False using `Lcm A = 0` by contradiction  | 
|
1435  | 
qed  | 
|
1436  | 
qed (simp only: Lcm_Lcm_eucl Lcm_eucl_def if_False)  | 
|
1437  | 
||
1438  | 
lemma Lcm0_iff [simp]:  | 
|
1439  | 
"finite A \<Longrightarrow> Lcm A = 0 \<longleftrightarrow> 0 \<in> A"  | 
|
1440  | 
proof -  | 
|
1441  | 
assume "finite A"  | 
|
1442  | 
have "0 \<in> A \<Longrightarrow> Lcm A = 0" by (intro dvd_0_left dvd_Lcm)  | 
|
1443  | 
  moreover {
 | 
|
1444  | 
assume "0 \<notin> A"  | 
|
1445  | 
hence "\<Prod>A \<noteq> 0"  | 
|
1446  | 
apply (induct rule: finite_induct[OF `finite A`])  | 
|
1447  | 
apply simp  | 
|
1448  | 
apply (subst setprod.insert, assumption, assumption)  | 
|
1449  | 
apply (rule no_zero_divisors)  | 
|
1450  | 
apply blast+  | 
|
1451  | 
done  | 
|
| 59010 | 1452  | 
moreover from `finite A` have "\<forall>x\<in>A. x dvd \<Prod>A" by blast  | 
| 58023 | 1453  | 
ultimately have "\<exists>l. l \<noteq> 0 \<and> (\<forall>x\<in>A. x dvd l)" by blast  | 
1454  | 
with Lcm0_iff' have "Lcm A \<noteq> 0" by simp  | 
|
1455  | 
}  | 
|
1456  | 
ultimately show "Lcm A = 0 \<longleftrightarrow> 0 \<in> A" by blast  | 
|
1457  | 
qed  | 
|
1458  | 
||
1459  | 
lemma Lcm_no_multiple:  | 
|
1460  | 
"(\<forall>m. m \<noteq> 0 \<longrightarrow> (\<exists>x\<in>A. \<not>x dvd m)) \<Longrightarrow> Lcm A = 0"  | 
|
1461  | 
proof -  | 
|
1462  | 
assume "\<forall>m. m \<noteq> 0 \<longrightarrow> (\<exists>x\<in>A. \<not>x dvd m)"  | 
|
1463  | 
hence "\<not>(\<exists>l. l \<noteq> 0 \<and> (\<forall>x\<in>A. x dvd l))" by blast  | 
|
1464  | 
then show "Lcm A = 0" by (simp only: Lcm_Lcm_eucl Lcm_eucl_def if_False)  | 
|
1465  | 
qed  | 
|
1466  | 
||
1467  | 
lemma Lcm_insert [simp]:  | 
|
1468  | 
"Lcm (insert a A) = lcm a (Lcm A)"  | 
|
1469  | 
proof (rule lcmI)  | 
|
1470  | 
fix l assume "a dvd l" and "Lcm A dvd l"  | 
|
1471  | 
hence "\<forall>x\<in>A. x dvd l" by (blast intro: dvd_trans dvd_Lcm)  | 
|
1472  | 
with `a dvd l` show "Lcm (insert a A) dvd l" by (force intro: Lcm_dvd)  | 
|
1473  | 
qed (auto intro: Lcm_dvd dvd_Lcm)  | 
|
1474  | 
||
1475  | 
lemma Lcm_finite:  | 
|
1476  | 
assumes "finite A"  | 
|
1477  | 
shows "Lcm A = Finite_Set.fold lcm 1 A"  | 
|
1478  | 
by (induct rule: finite.induct[OF `finite A`])  | 
|
1479  | 
(simp_all add: comp_fun_idem.fold_insert_idem[OF comp_fun_idem_lcm])  | 
|
1480  | 
||
1481  | 
lemma Lcm_set [code, code_unfold]:  | 
|
1482  | 
"Lcm (set xs) = fold lcm xs 1"  | 
|
1483  | 
using comp_fun_idem.fold_set_fold[OF comp_fun_idem_lcm] Lcm_finite by (simp add: ac_simps)  | 
|
1484  | 
||
1485  | 
lemma Lcm_singleton [simp]:  | 
|
1486  | 
  "Lcm {a} = a div normalisation_factor a"
 | 
|
1487  | 
by simp  | 
|
1488  | 
||
1489  | 
lemma Lcm_2 [simp]:  | 
|
1490  | 
  "Lcm {a,b} = lcm a b"
 | 
|
1491  | 
by (simp only: Lcm_insert Lcm_empty lcm_1_right)  | 
|
1492  | 
(cases "b = 0", simp, rule lcm_div_unit2, simp)  | 
|
1493  | 
||
1494  | 
lemma Lcm_coprime:  | 
|
1495  | 
  assumes "finite A" and "A \<noteq> {}" 
 | 
|
1496  | 
assumes "\<And>a b. a \<in> A \<Longrightarrow> b \<in> A \<Longrightarrow> a \<noteq> b \<Longrightarrow> gcd a b = 1"  | 
|
1497  | 
shows "Lcm A = \<Prod>A div normalisation_factor (\<Prod>A)"  | 
|
1498  | 
using assms proof (induct rule: finite_ne_induct)  | 
|
1499  | 
case (insert a A)  | 
|
1500  | 
have "Lcm (insert a A) = lcm a (Lcm A)" by simp  | 
|
1501  | 
also from insert have "Lcm A = \<Prod>A div normalisation_factor (\<Prod>A)" by blast  | 
|
1502  | 
also have "lcm a \<dots> = lcm a (\<Prod>A)" by (cases "\<Prod>A = 0") (simp_all add: lcm_div_unit2)  | 
|
1503  | 
also from insert have "gcd a (\<Prod>A) = 1" by (subst gcd.commute, intro setprod_coprime) auto  | 
|
1504  | 
with insert have "lcm a (\<Prod>A) = \<Prod>(insert a A) div normalisation_factor (\<Prod>(insert a A))"  | 
|
1505  | 
by (simp add: lcm_coprime)  | 
|
1506  | 
finally show ?case .  | 
|
1507  | 
qed simp  | 
|
1508  | 
||
1509  | 
lemma Lcm_coprime':  | 
|
1510  | 
"card A \<noteq> 0 \<Longrightarrow> (\<And>a b. a \<in> A \<Longrightarrow> b \<in> A \<Longrightarrow> a \<noteq> b \<Longrightarrow> gcd a b = 1)  | 
|
1511  | 
\<Longrightarrow> Lcm A = \<Prod>A div normalisation_factor (\<Prod>A)"  | 
|
1512  | 
by (rule Lcm_coprime) (simp_all add: card_eq_0_iff)  | 
|
1513  | 
||
1514  | 
lemma Gcd_Lcm:  | 
|
1515  | 
  "Gcd A = Lcm {d. \<forall>x\<in>A. d dvd x}"
 | 
|
1516  | 
by (simp add: Gcd_Gcd_eucl Lcm_Lcm_eucl Gcd_eucl_def)  | 
|
1517  | 
||
1518  | 
lemma Gcd_dvd [simp]: "x \<in> A \<Longrightarrow> Gcd A dvd x"  | 
|
1519  | 
and dvd_Gcd [simp]: "(\<forall>x\<in>A. g' dvd x) \<Longrightarrow> g' dvd Gcd A"  | 
|
1520  | 
and normalisation_factor_Gcd [simp]:  | 
|
1521  | 
"normalisation_factor (Gcd A) = (if Gcd A = 0 then 0 else 1)"  | 
|
1522  | 
proof -  | 
|
1523  | 
fix x assume "x \<in> A"  | 
|
1524  | 
  hence "Lcm {d. \<forall>x\<in>A. d dvd x} dvd x" by (intro Lcm_dvd) blast
 | 
|
1525  | 
then show "Gcd A dvd x" by (simp add: Gcd_Lcm)  | 
|
1526  | 
next  | 
|
1527  | 
fix g' assume "\<forall>x\<in>A. g' dvd x"  | 
|
1528  | 
  hence "g' dvd Lcm {d. \<forall>x\<in>A. d dvd x}" by (intro dvd_Lcm) blast
 | 
|
1529  | 
then show "g' dvd Gcd A" by (simp add: Gcd_Lcm)  | 
|
1530  | 
next  | 
|
1531  | 
show "normalisation_factor (Gcd A) = (if Gcd A = 0 then 0 else 1)"  | 
|
| 
59009
 
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
 
haftmann 
parents: 
58953 
diff
changeset
 | 
1532  | 
by (simp add: Gcd_Lcm)  | 
| 58023 | 1533  | 
qed  | 
1534  | 
||
1535  | 
lemma GcdI:  | 
|
1536  | 
"(\<And>x. x\<in>A \<Longrightarrow> l dvd x) \<Longrightarrow> (\<And>l'. (\<forall>x\<in>A. l' dvd x) \<Longrightarrow> l' dvd l) \<Longrightarrow>  | 
|
1537  | 
normalisation_factor l = (if l = 0 then 0 else 1) \<Longrightarrow> l = Gcd A"  | 
|
1538  | 
by (intro normed_associated_imp_eq)  | 
|
1539  | 
(auto intro: Gcd_dvd dvd_Gcd simp: associated_def)  | 
|
1540  | 
||
1541  | 
lemma Lcm_Gcd:  | 
|
1542  | 
  "Lcm A = Gcd {m. \<forall>x\<in>A. x dvd m}"
 | 
|
1543  | 
by (rule LcmI[symmetric]) (auto intro: dvd_Gcd Gcd_dvd)  | 
|
1544  | 
||
1545  | 
lemma Gcd_0_iff:  | 
|
1546  | 
  "Gcd A = 0 \<longleftrightarrow> A \<subseteq> {0}"
 | 
|
1547  | 
apply (rule iffI)  | 
|
1548  | 
apply (rule subsetI, drule Gcd_dvd, simp)  | 
|
1549  | 
apply (auto intro: GcdI[symmetric])  | 
|
1550  | 
done  | 
|
1551  | 
||
1552  | 
lemma Gcd_empty [simp]:  | 
|
1553  | 
  "Gcd {} = 0"
 | 
|
1554  | 
by (simp add: Gcd_0_iff)  | 
|
1555  | 
||
1556  | 
lemma Gcd_1:  | 
|
1557  | 
"1 \<in> A \<Longrightarrow> Gcd A = 1"  | 
|
1558  | 
by (intro GcdI[symmetric]) (auto intro: Gcd_dvd dvd_Gcd)  | 
|
1559  | 
||
1560  | 
lemma Gcd_insert [simp]:  | 
|
1561  | 
"Gcd (insert a A) = gcd a (Gcd A)"  | 
|
1562  | 
proof (rule gcdI)  | 
|
1563  | 
fix l assume "l dvd a" and "l dvd Gcd A"  | 
|
1564  | 
hence "\<forall>x\<in>A. l dvd x" by (blast intro: dvd_trans Gcd_dvd)  | 
|
1565  | 
with `l dvd a` show "l dvd Gcd (insert a A)" by (force intro: Gcd_dvd)  | 
|
| 
59009
 
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
 
haftmann 
parents: 
58953 
diff
changeset
 | 
1566  | 
qed auto  | 
| 58023 | 1567  | 
|
1568  | 
lemma Gcd_finite:  | 
|
1569  | 
assumes "finite A"  | 
|
1570  | 
shows "Gcd A = Finite_Set.fold gcd 0 A"  | 
|
1571  | 
by (induct rule: finite.induct[OF `finite A`])  | 
|
1572  | 
(simp_all add: comp_fun_idem.fold_insert_idem[OF comp_fun_idem_gcd])  | 
|
1573  | 
||
1574  | 
lemma Gcd_set [code, code_unfold]:  | 
|
1575  | 
"Gcd (set xs) = fold gcd xs 0"  | 
|
1576  | 
using comp_fun_idem.fold_set_fold[OF comp_fun_idem_gcd] Gcd_finite by (simp add: ac_simps)  | 
|
1577  | 
||
1578  | 
lemma Gcd_singleton [simp]: "Gcd {a} = a div normalisation_factor a"
 | 
|
1579  | 
by (simp add: gcd_0)  | 
|
1580  | 
||
1581  | 
lemma Gcd_2 [simp]: "Gcd {a,b} = gcd a b"
 | 
|
1582  | 
by (simp only: Gcd_insert Gcd_empty gcd_0) (cases "b = 0", simp, rule gcd_div_unit2, simp)  | 
|
1583  | 
||
1584  | 
end  | 
|
1585  | 
||
1586  | 
text {*
 | 
|
1587  | 
A Euclidean ring is a Euclidean semiring with additive inverses. It provides a  | 
|
1588  | 
few more lemmas; in particular, Bezout's lemma holds for any Euclidean ring.  | 
|
1589  | 
*}  | 
|
1590  | 
||
1591  | 
class euclidean_ring = euclidean_semiring + idom  | 
|
1592  | 
||
1593  | 
class euclidean_ring_gcd = euclidean_semiring_gcd + idom  | 
|
1594  | 
begin  | 
|
1595  | 
||
1596  | 
subclass euclidean_ring ..  | 
|
1597  | 
||
1598  | 
lemma gcd_neg1 [simp]:  | 
|
1599  | 
"gcd (-x) y = gcd x y"  | 
|
| 
59009
 
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
 
haftmann 
parents: 
58953 
diff
changeset
 | 
1600  | 
by (rule sym, rule gcdI, simp_all add: gcd_greatest)  | 
| 58023 | 1601  | 
|
1602  | 
lemma gcd_neg2 [simp]:  | 
|
1603  | 
"gcd x (-y) = gcd x y"  | 
|
| 
59009
 
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
 
haftmann 
parents: 
58953 
diff
changeset
 | 
1604  | 
by (rule sym, rule gcdI, simp_all add: gcd_greatest)  | 
| 58023 | 1605  | 
|
1606  | 
lemma gcd_neg_numeral_1 [simp]:  | 
|
1607  | 
"gcd (- numeral n) x = gcd (numeral n) x"  | 
|
1608  | 
by (fact gcd_neg1)  | 
|
1609  | 
||
1610  | 
lemma gcd_neg_numeral_2 [simp]:  | 
|
1611  | 
"gcd x (- numeral n) = gcd x (numeral n)"  | 
|
1612  | 
by (fact gcd_neg2)  | 
|
1613  | 
||
1614  | 
lemma gcd_diff1: "gcd (m - n) n = gcd m n"  | 
|
1615  | 
by (subst diff_conv_add_uminus, subst gcd_neg2[symmetric], subst gcd_add1, simp)  | 
|
1616  | 
||
1617  | 
lemma gcd_diff2: "gcd (n - m) n = gcd m n"  | 
|
1618  | 
by (subst gcd_neg1[symmetric], simp only: minus_diff_eq gcd_diff1)  | 
|
1619  | 
||
1620  | 
lemma coprime_minus_one [simp]: "gcd (n - 1) n = 1"  | 
|
1621  | 
proof -  | 
|
1622  | 
have "gcd (n - 1) n = gcd n (n - 1)" by (fact gcd.commute)  | 
|
1623  | 
also have "\<dots> = gcd ((n - 1) + 1) (n - 1)" by simp  | 
|
1624  | 
also have "\<dots> = 1" by (rule coprime_plus_one)  | 
|
1625  | 
finally show ?thesis .  | 
|
1626  | 
qed  | 
|
1627  | 
||
1628  | 
lemma lcm_neg1 [simp]: "lcm (-x) y = lcm x y"  | 
|
1629  | 
by (rule sym, rule lcmI, simp_all add: lcm_least lcm_zero)  | 
|
1630  | 
||
1631  | 
lemma lcm_neg2 [simp]: "lcm x (-y) = lcm x y"  | 
|
1632  | 
by (rule sym, rule lcmI, simp_all add: lcm_least lcm_zero)  | 
|
1633  | 
||
1634  | 
lemma lcm_neg_numeral_1 [simp]: "lcm (- numeral n) x = lcm (numeral n) x"  | 
|
1635  | 
by (fact lcm_neg1)  | 
|
1636  | 
||
1637  | 
lemma lcm_neg_numeral_2 [simp]: "lcm x (- numeral n) = lcm x (numeral n)"  | 
|
1638  | 
by (fact lcm_neg2)  | 
|
1639  | 
||
1640  | 
function euclid_ext :: "'a \<Rightarrow> 'a \<Rightarrow> 'a \<times> 'a \<times> 'a" where  | 
|
1641  | 
"euclid_ext a b =  | 
|
1642  | 
(if b = 0 then  | 
|
1643  | 
let x = ring_inv (normalisation_factor a) in (x, 0, a * x)  | 
|
1644  | 
else  | 
|
1645  | 
case euclid_ext b (a mod b) of  | 
|
1646  | 
(s,t,c) \<Rightarrow> (t, s - t * (a div b), c))"  | 
|
1647  | 
by (pat_completeness, simp)  | 
|
1648  | 
termination by (relation "measure (euclidean_size \<circ> snd)", simp_all)  | 
|
1649  | 
||
1650  | 
declare euclid_ext.simps [simp del]  | 
|
1651  | 
||
1652  | 
lemma euclid_ext_0:  | 
|
1653  | 
"euclid_ext a 0 = (ring_inv (normalisation_factor a), 0, a * ring_inv (normalisation_factor a))"  | 
|
1654  | 
by (subst euclid_ext.simps, simp add: Let_def)  | 
|
1655  | 
||
1656  | 
lemma euclid_ext_non_0:  | 
|
1657  | 
"b \<noteq> 0 \<Longrightarrow> euclid_ext a b = (case euclid_ext b (a mod b) of  | 
|
1658  | 
(s,t,c) \<Rightarrow> (t, s - t * (a div b), c))"  | 
|
1659  | 
by (subst euclid_ext.simps, simp)  | 
|
1660  | 
||
1661  | 
definition euclid_ext' :: "'a \<Rightarrow> 'a \<Rightarrow> 'a \<times> 'a"  | 
|
1662  | 
where  | 
|
1663  | 
"euclid_ext' a b = (case euclid_ext a b of (s, t, _) \<Rightarrow> (s, t))"  | 
|
1664  | 
||
1665  | 
lemma euclid_ext_gcd [simp]:  | 
|
1666  | 
"(case euclid_ext a b of (_,_,t) \<Rightarrow> t) = gcd a b"  | 
|
1667  | 
proof (induct a b rule: euclid_ext.induct)  | 
|
1668  | 
case (1 a b)  | 
|
1669  | 
then show ?case  | 
|
1670  | 
proof (cases "b = 0")  | 
|
1671  | 
case True  | 
|
1672  | 
then show ?thesis by (cases "a = 0")  | 
|
1673  | 
(simp_all add: euclid_ext_0 unit_div mult_ac unit_simps gcd_0)  | 
|
1674  | 
next  | 
|
1675  | 
case False with 1 show ?thesis  | 
|
1676  | 
by (simp add: euclid_ext_non_0 ac_simps split: prod.split prod.split_asm)  | 
|
1677  | 
qed  | 
|
1678  | 
qed  | 
|
1679  | 
||
1680  | 
lemma euclid_ext_gcd' [simp]:  | 
|
1681  | 
"euclid_ext a b = (r, s, t) \<Longrightarrow> t = gcd a b"  | 
|
1682  | 
by (insert euclid_ext_gcd[of a b], drule (1) subst, simp)  | 
|
1683  | 
||
1684  | 
lemma euclid_ext_correct:  | 
|
1685  | 
"case euclid_ext x y of (s,t,c) \<Rightarrow> s*x + t*y = c"  | 
|
1686  | 
proof (induct x y rule: euclid_ext.induct)  | 
|
1687  | 
case (1 x y)  | 
|
1688  | 
show ?case  | 
|
1689  | 
proof (cases "y = 0")  | 
|
1690  | 
case True  | 
|
1691  | 
then show ?thesis by (simp add: euclid_ext_0 mult_ac)  | 
|
1692  | 
next  | 
|
1693  | 
case False  | 
|
1694  | 
obtain s t c where stc: "euclid_ext y (x mod y) = (s,t,c)"  | 
|
1695  | 
by (cases "euclid_ext y (x mod y)", blast)  | 
|
1696  | 
from 1 have "c = s * y + t * (x mod y)" by (simp add: stc False)  | 
|
1697  | 
also have "... = t*((x div y)*y + x mod y) + (s - t * (x div y))*y"  | 
|
1698  | 
by (simp add: algebra_simps)  | 
|
1699  | 
also have "(x div y)*y + x mod y = x" using mod_div_equality .  | 
|
1700  | 
finally show ?thesis  | 
|
1701  | 
by (subst euclid_ext.simps, simp add: False stc)  | 
|
1702  | 
qed  | 
|
1703  | 
qed  | 
|
1704  | 
||
1705  | 
lemma euclid_ext'_correct:  | 
|
1706  | 
"fst (euclid_ext' a b) * a + snd (euclid_ext' a b) * b = gcd a b"  | 
|
1707  | 
proof-  | 
|
1708  | 
obtain s t c where "euclid_ext a b = (s,t,c)"  | 
|
1709  | 
by (cases "euclid_ext a b", blast)  | 
|
1710  | 
with euclid_ext_correct[of a b] euclid_ext_gcd[of a b]  | 
|
1711  | 
show ?thesis unfolding euclid_ext'_def by simp  | 
|
1712  | 
qed  | 
|
1713  | 
||
1714  | 
lemma bezout: "\<exists>s t. s * x + t * y = gcd x y"  | 
|
1715  | 
using euclid_ext'_correct by blast  | 
|
1716  | 
||
1717  | 
lemma euclid_ext'_0 [simp]: "euclid_ext' x 0 = (ring_inv (normalisation_factor x), 0)"  | 
|
1718  | 
by (simp add: bezw_def euclid_ext'_def euclid_ext_0)  | 
|
1719  | 
||
1720  | 
lemma euclid_ext'_non_0: "y \<noteq> 0 \<Longrightarrow> euclid_ext' x y = (snd (euclid_ext' y (x mod y)),  | 
|
1721  | 
fst (euclid_ext' y (x mod y)) - snd (euclid_ext' y (x mod y)) * (x div y))"  | 
|
1722  | 
by (cases "euclid_ext y (x mod y)")  | 
|
1723  | 
(simp add: euclid_ext'_def euclid_ext_non_0)  | 
|
1724  | 
||
1725  | 
end  | 
|
1726  | 
||
1727  | 
instantiation nat :: euclidean_semiring  | 
|
1728  | 
begin  | 
|
1729  | 
||
1730  | 
definition [simp]:  | 
|
1731  | 
"euclidean_size_nat = (id :: nat \<Rightarrow> nat)"  | 
|
1732  | 
||
1733  | 
definition [simp]:  | 
|
1734  | 
"normalisation_factor_nat (n::nat) = (if n = 0 then 0 else 1 :: nat)"  | 
|
1735  | 
||
1736  | 
instance proof  | 
|
| 59061 | 1737  | 
qed simp_all  | 
| 58023 | 1738  | 
|
1739  | 
end  | 
|
1740  | 
||
1741  | 
instantiation int :: euclidean_ring  | 
|
1742  | 
begin  | 
|
1743  | 
||
1744  | 
definition [simp]:  | 
|
1745  | 
"euclidean_size_int = (nat \<circ> abs :: int \<Rightarrow> nat)"  | 
|
1746  | 
||
1747  | 
definition [simp]:  | 
|
1748  | 
"normalisation_factor_int = (sgn :: int \<Rightarrow> int)"  | 
|
1749  | 
||
1750  | 
instance proof  | 
|
1751  | 
case goal2 then show ?case by (auto simp add: abs_mult nat_mult_distrib)  | 
|
1752  | 
next  | 
|
| 59061 | 1753  | 
case goal3 then show ?case by (simp add: zsgn_def)  | 
| 58023 | 1754  | 
next  | 
| 59061 | 1755  | 
case goal5 then show ?case by (auto simp: zsgn_def)  | 
| 58023 | 1756  | 
next  | 
| 59061 | 1757  | 
case goal6 then show ?case by (auto split: abs_split simp: zsgn_def)  | 
| 58023 | 1758  | 
qed (auto simp: sgn_times split: abs_split)  | 
1759  | 
||
1760  | 
end  | 
|
1761  | 
||
1762  | 
end  | 
|
1763  |