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