author  haftmann 
Mon, 17 Nov 2014 14:55:33 +0100  
changeset 59009  348561aa3869 
parent 58953  2e19b392d9e3 
child 59010  ec2b4270a502 
permissions  rwrr 
58023  1 
(* Author: Manuel Eberl *) 
2 

58889  3 
section {* Abstract euclidean algorithm *} 
58023  4 

5 
theory Euclidean_Algorithm 

6 
imports Complex_Main 

7 
begin 

8 

9 
context semiring_div 

10 
begin 

11 

12 
lemma dvd_setprod [intro]: 

13 
assumes "finite A" and "x \<in> A" 

14 
shows "f x dvd setprod f A" 

15 
proof 

16 
from `finite A` have "setprod f (insert x (A  {x})) = f x * setprod f (A  {x})" 

17 
by (intro setprod.insert) auto 

18 
also from `x \<in> A` have "insert x (A  {x}) = A" by blast 

19 
finally show "setprod f A = f x * setprod f (A  {x})" . 

20 
qed 

21 

22 
end 

23 

24 
context semiring_div 

25 
begin 

26 

27 
definition ring_inv :: "'a \<Rightarrow> 'a" 

28 
where 

29 
"ring_inv x = 1 div x" 

30 

31 
definition is_unit :: "'a \<Rightarrow> bool" 

32 
where 

33 
"is_unit x \<longleftrightarrow> x dvd 1" 

34 

35 
definition associated :: "'a \<Rightarrow> 'a \<Rightarrow> bool" 

36 
where 

37 
"associated x y \<longleftrightarrow> x dvd y \<and> y dvd x" 

38 

39 
lemma unit_prod [intro]: 

40 
"is_unit x \<Longrightarrow> is_unit y \<Longrightarrow> is_unit (x * y)" 

41 
unfolding is_unit_def by (subst mult_1_left [of 1, symmetric], rule mult_dvd_mono) 

42 

43 
lemma unit_ring_inv: 

44 
"is_unit y \<Longrightarrow> x div y = x * ring_inv y" 

45 
by (simp add: div_mult_swap ring_inv_def is_unit_def) 

46 

47 
lemma unit_ring_inv_ring_inv [simp]: 

48 
"is_unit x \<Longrightarrow> ring_inv (ring_inv x) = x" 

49 
unfolding is_unit_def ring_inv_def 

50 
by (metis div_mult_mult1_if div_mult_self1_is_id dvd_mult_div_cancel mult_1_right) 

51 

52 
lemma inv_imp_eq_ring_inv: 

53 
"a * b = 1 \<Longrightarrow> ring_inv a = b" 

54 
by (metis dvd_mult_div_cancel dvd_mult_right mult_1_right mult.left_commute one_dvd ring_inv_def) 

55 

56 
lemma ring_inv_is_inv1 [simp]: 

57 
"is_unit a \<Longrightarrow> a * ring_inv a = 1" 

58953  58 
unfolding is_unit_def ring_inv_def by simp 
58023  59 

60 
lemma ring_inv_is_inv2 [simp]: 

61 
"is_unit a \<Longrightarrow> ring_inv a * a = 1" 

62 
by (simp add: ac_simps) 

63 

64 
lemma unit_ring_inv_unit [simp, intro]: 

65 
assumes "is_unit x" 

66 
shows "is_unit (ring_inv x)" 

67 
proof  

68 
from assms have "1 = ring_inv x * x" by simp 

69 
then show "is_unit (ring_inv x)" unfolding is_unit_def by (rule dvdI) 

70 
qed 

71 

72 
lemma mult_unit_dvd_iff: 

73 
"is_unit y \<Longrightarrow> x * y dvd z \<longleftrightarrow> x dvd z" 

74 
proof 

75 
assume "is_unit y" "x * y dvd z" 

76 
then show "x dvd z" by (simp add: dvd_mult_left) 

77 
next 

78 
assume "is_unit y" "x dvd z" 

79 
then obtain k where "z = x * k" unfolding dvd_def by blast 

80 
with `is_unit y` have "z = (x * y) * (ring_inv y * k)" 

81 
by (simp add: mult_ac) 

82 
then show "x * y dvd z" by (rule dvdI) 

83 
qed 

84 

85 
lemma div_unit_dvd_iff: 

86 
"is_unit y \<Longrightarrow> x div y dvd z \<longleftrightarrow> x dvd z" 

87 
by (subst unit_ring_inv) (assumption, simp add: mult_unit_dvd_iff) 

88 

89 
lemma dvd_mult_unit_iff: 

90 
"is_unit y \<Longrightarrow> x dvd z * y \<longleftrightarrow> x dvd z" 

91 
proof 

92 
assume "is_unit y" and "x dvd z * y" 

93 
have "z * y dvd z * (y * ring_inv y)" by (subst mult_assoc [symmetric]) simp 

94 
also from `is_unit y` have "y * ring_inv y = 1" by simp 

95 
finally have "z * y dvd z" by simp 

96 
with `x dvd z * y` show "x dvd z" by (rule dvd_trans) 

97 
next 

98 
assume "x dvd z" 

99 
then show "x dvd z * y" by simp 

100 
qed 

101 

102 
lemma dvd_div_unit_iff: 

103 
"is_unit y \<Longrightarrow> x dvd z div y \<longleftrightarrow> x dvd z" 

104 
by (subst unit_ring_inv) (assumption, simp add: dvd_mult_unit_iff) 

105 

106 
lemmas unit_dvd_iff = mult_unit_dvd_iff div_unit_dvd_iff dvd_mult_unit_iff dvd_div_unit_iff 

107 

108 
lemma unit_div [intro]: 

109 
"is_unit x \<Longrightarrow> is_unit y \<Longrightarrow> is_unit (x div y)" 

110 
by (subst unit_ring_inv) (assumption, rule unit_prod, simp_all) 

111 

112 
lemma unit_div_mult_swap: 

113 
"is_unit z \<Longrightarrow> x * (y div z) = x * y div z" 

114 
by (simp only: unit_ring_inv [of _ y] unit_ring_inv [of _ "x*y"] ac_simps) 

115 

116 
lemma unit_div_commute: 

117 
"is_unit y \<Longrightarrow> x div y * z = x * z div y" 

118 
by (simp only: unit_ring_inv [of _ x] unit_ring_inv [of _ "x*z"] ac_simps) 

119 

120 
lemma unit_imp_dvd [dest]: 

121 
"is_unit y \<Longrightarrow> y dvd x" 

122 
by (rule dvd_trans [of _ 1]) (simp_all add: is_unit_def) 

123 

124 
lemma dvd_unit_imp_unit: 

125 
"is_unit y \<Longrightarrow> x dvd y \<Longrightarrow> is_unit x" 

126 
by (unfold is_unit_def) (rule dvd_trans) 

127 

128 
lemma ring_inv_0 [simp]: 

129 
"ring_inv 0 = 0" 

130 
unfolding ring_inv_def by simp 

131 

132 
lemma unit_ring_inv'1: 

133 
assumes "is_unit y" 

134 
shows "x div (y * z) = x * ring_inv y div z" 

135 
proof  

136 
from assms have "x div (y * z) = x * (ring_inv y * y) div (y * z)" 

137 
by simp 

138 
also have "... = y * (x * ring_inv y) div (y * z)" 

139 
by (simp only: mult_ac) 

140 
also have "... = x * ring_inv y div z" 

141 
by (cases "y = 0", simp, rule div_mult_mult1) 

142 
finally show ?thesis . 

143 
qed 

144 

145 
lemma associated_comm: 

146 
"associated x y \<Longrightarrow> associated y x" 

147 
by (simp add: associated_def) 

148 

149 
lemma associated_0 [simp]: 

150 
"associated 0 b \<longleftrightarrow> b = 0" 

151 
"associated a 0 \<longleftrightarrow> a = 0" 

152 
unfolding associated_def by simp_all 

153 

154 
lemma associated_unit: 

155 
"is_unit x \<Longrightarrow> associated x y \<Longrightarrow> is_unit y" 

156 
unfolding associated_def by (fast dest: dvd_unit_imp_unit) 

157 

158 
lemma is_unit_1 [simp]: 

159 
"is_unit 1" 

160 
unfolding is_unit_def by simp 

161 

162 
lemma not_is_unit_0 [simp]: 

163 
"\<not> is_unit 0" 

164 
unfolding is_unit_def by auto 

165 

166 
lemma unit_mult_left_cancel: 

167 
assumes "is_unit x" 

168 
shows "(x * y) = (x * z) \<longleftrightarrow> y = z" 

169 
proof  

170 
from assms have "x \<noteq> 0" by auto 

171 
then show ?thesis by (metis div_mult_self1_is_id) 

172 
qed 

173 

174 
lemma unit_mult_right_cancel: 

175 
"is_unit x \<Longrightarrow> (y * x) = (z * x) \<longleftrightarrow> y = z" 

176 
by (simp add: ac_simps unit_mult_left_cancel) 

177 

178 
lemma unit_div_cancel: 

179 
"is_unit x \<Longrightarrow> (y div x) = (z div x) \<longleftrightarrow> y = z" 

180 
apply (subst unit_ring_inv[of _ y], assumption) 

181 
apply (subst unit_ring_inv[of _ z], assumption) 

182 
apply (rule unit_mult_right_cancel, erule unit_ring_inv_unit) 

183 
done 

184 

185 
lemma unit_eq_div1: 

186 
"is_unit y \<Longrightarrow> x div y = z \<longleftrightarrow> x = z * y" 

187 
apply (subst unit_ring_inv, assumption) 

188 
apply (subst unit_mult_right_cancel[symmetric], assumption) 

189 
apply (subst mult_assoc, subst ring_inv_is_inv2, assumption, simp) 

190 
done 

191 

192 
lemma unit_eq_div2: 

193 
"is_unit y \<Longrightarrow> x = z div y \<longleftrightarrow> x * y = z" 

194 
by (subst (1 2) eq_commute, simp add: unit_eq_div1, subst eq_commute, rule refl) 

195 

196 
lemma associated_iff_div_unit: 

197 
"associated x y \<longleftrightarrow> (\<exists>z. is_unit z \<and> x = z * y)" 

198 
proof 

199 
assume "associated x y" 

200 
show "\<exists>z. is_unit z \<and> x = z * y" 

201 
proof (cases "x = 0") 

202 
assume "x = 0" 

203 
then show "\<exists>z. is_unit z \<and> x = z * y" using `associated x y` 

204 
by (intro exI[of _ 1], simp add: associated_def) 

205 
next 

206 
assume [simp]: "x \<noteq> 0" 

207 
hence [simp]: "x dvd y" "y dvd x" using `associated x y` 

208 
unfolding associated_def by simp_all 

209 
hence "1 = x div y * (y div x)" 

59009
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset

210 
by (simp add: div_mult_swap) 
58023  211 
hence "is_unit (x div y)" unfolding is_unit_def by (rule dvdI) 
59009
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset

212 
moreover have "x = (x div y) * y" by simp 
58023  213 
ultimately show ?thesis by blast 
214 
qed 

215 
next 

216 
assume "\<exists>z. is_unit z \<and> x = z * y" 

217 
then obtain z where "is_unit z" and "x = z * y" by blast 

218 
hence "y = x * ring_inv z" by (simp add: algebra_simps) 

219 
hence "x dvd y" by simp 

220 
moreover from `x = z * y` have "y dvd x" by simp 

221 
ultimately show "associated x y" unfolding associated_def by simp 

222 
qed 

223 

224 
lemmas unit_simps = mult_unit_dvd_iff div_unit_dvd_iff dvd_mult_unit_iff 

225 
dvd_div_unit_iff unit_div_mult_swap unit_div_commute 

226 
unit_mult_left_cancel unit_mult_right_cancel unit_div_cancel 

227 
unit_eq_div1 unit_eq_div2 

228 

229 
end 

230 

231 
context ring_div 

232 
begin 

233 

234 
lemma is_unit_neg [simp]: 

235 
"is_unit ( x) \<Longrightarrow> is_unit x" 

236 
unfolding is_unit_def by simp 

237 

238 
lemma is_unit_neg_1 [simp]: 

239 
"is_unit (1)" 

240 
unfolding is_unit_def by simp 

241 

242 
end 

243 

244 
lemma is_unit_nat [simp]: 

245 
"is_unit (x::nat) \<longleftrightarrow> x = 1" 

246 
unfolding is_unit_def by simp 

247 

248 
lemma is_unit_int: 

249 
"is_unit (x::int) \<longleftrightarrow> x = 1 \<or> x = 1" 

250 
unfolding is_unit_def by auto 

251 

252 
text {* 

253 
A Euclidean semiring is a semiring upon which the Euclidean algorithm can be 

254 
implemented. It must provide: 

255 
\begin{itemize} 

256 
\item division with remainder 

257 
\item a size function such that @{term "size (a mod b) < size b"} 

258 
for any @{term "b \<noteq> 0"} 

259 
\item a normalisation factor such that two associated numbers are equal iff 

260 
they are the same when divided by their normalisation factors. 

261 
\end{itemize} 

262 
The existence of these functions makes it possible to derive gcd and lcm functions 

263 
for any Euclidean semiring. 

264 
*} 

265 
class euclidean_semiring = semiring_div + 

266 
fixes euclidean_size :: "'a \<Rightarrow> nat" 

267 
fixes normalisation_factor :: "'a \<Rightarrow> 'a" 

268 
assumes mod_size_less [simp]: 

269 
"b \<noteq> 0 \<Longrightarrow> euclidean_size (a mod b) < euclidean_size b" 

270 
assumes size_mult_mono: 

271 
"b \<noteq> 0 \<Longrightarrow> euclidean_size (a * b) \<ge> euclidean_size a" 

272 
assumes normalisation_factor_is_unit [intro,simp]: 

273 
"a \<noteq> 0 \<Longrightarrow> is_unit (normalisation_factor a)" 

274 
assumes normalisation_factor_mult: "normalisation_factor (a * b) = 

275 
normalisation_factor a * normalisation_factor b" 

276 
assumes normalisation_factor_unit: "is_unit x \<Longrightarrow> normalisation_factor x = x" 

277 
assumes normalisation_factor_0 [simp]: "normalisation_factor 0 = 0" 

278 
begin 

279 

280 
lemma normalisation_factor_dvd [simp]: 

281 
"a \<noteq> 0 \<Longrightarrow> normalisation_factor a dvd b" 

282 
by (rule unit_imp_dvd, simp) 

283 

284 
lemma normalisation_factor_1 [simp]: 

285 
"normalisation_factor 1 = 1" 

286 
by (simp add: normalisation_factor_unit) 

287 

288 
lemma normalisation_factor_0_iff [simp]: 

289 
"normalisation_factor x = 0 \<longleftrightarrow> x = 0" 

290 
proof 

291 
assume "normalisation_factor x = 0" 

292 
hence "\<not> is_unit (normalisation_factor x)" 

293 
by (metis not_is_unit_0) 

294 
then show "x = 0" by force 

295 
next 

296 
assume "x = 0" 

297 
then show "normalisation_factor x = 0" by simp 

298 
qed 

299 

300 
lemma normalisation_factor_pow: 

301 
"normalisation_factor (x ^ n) = normalisation_factor x ^ n" 

302 
by (induct n) (simp_all add: normalisation_factor_mult power_Suc2) 

303 

304 
lemma normalisation_correct [simp]: 

305 
"normalisation_factor (x div normalisation_factor x) = (if x = 0 then 0 else 1)" 

306 
proof (cases "x = 0", simp) 

307 
assume "x \<noteq> 0" 

308 
let ?nf = "normalisation_factor" 

309 
from normalisation_factor_is_unit[OF `x \<noteq> 0`] have "?nf x \<noteq> 0" 

310 
by (metis not_is_unit_0) 

311 
have "?nf (x div ?nf x) * ?nf (?nf x) = ?nf (x div ?nf x * ?nf x)" 

312 
by (simp add: normalisation_factor_mult) 

313 
also have "x div ?nf x * ?nf x = x" using `x \<noteq> 0` 

59009
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset

314 
by simp 
58023  315 
also have "?nf (?nf x) = ?nf x" using `x \<noteq> 0` 
316 
normalisation_factor_is_unit normalisation_factor_unit by simp 

317 
finally show ?thesis using `x \<noteq> 0` and `?nf x \<noteq> 0` 

318 
by (metis div_mult_self2_is_id div_self) 

319 
qed 

320 

321 
lemma normalisation_0_iff [simp]: 

322 
"x div normalisation_factor x = 0 \<longleftrightarrow> x = 0" 

323 
by (cases "x = 0", simp, subst unit_eq_div1, blast, simp) 

324 

325 
lemma associated_iff_normed_eq: 

326 
"associated a b \<longleftrightarrow> a div normalisation_factor a = b div normalisation_factor b" 

327 
proof (cases "b = 0", simp, cases "a = 0", metis associated_0(1) normalisation_0_iff, rule iffI) 

328 
let ?nf = normalisation_factor 

329 
assume "a \<noteq> 0" "b \<noteq> 0" "a div ?nf a = b div ?nf b" 

330 
hence "a = b * (?nf a div ?nf b)" 

331 
apply (subst (asm) unit_eq_div1, blast, subst (asm) unit_div_commute, blast) 

332 
apply (subst div_mult_swap, simp, simp) 

333 
done 

334 
with `a \<noteq> 0` `b \<noteq> 0` have "\<exists>z. is_unit z \<and> a = z * b" 

335 
by (intro exI[of _ "?nf a div ?nf b"], force simp: mult_ac) 

336 
with associated_iff_div_unit show "associated a b" by simp 

337 
next 

338 
let ?nf = normalisation_factor 

339 
assume "a \<noteq> 0" "b \<noteq> 0" "associated a b" 

340 
with associated_iff_div_unit obtain z where "is_unit z" and "a = z * b" by blast 

341 
then show "a div ?nf a = b div ?nf b" 

342 
apply (simp only: `a = z * b` normalisation_factor_mult normalisation_factor_unit) 

343 
apply (rule div_mult_mult1, force) 

344 
done 

345 
qed 

346 

347 
lemma normed_associated_imp_eq: 

348 
"associated a b \<Longrightarrow> normalisation_factor a \<in> {0, 1} \<Longrightarrow> normalisation_factor b \<in> {0, 1} \<Longrightarrow> a = b" 

349 
by (simp add: associated_iff_normed_eq, elim disjE, simp_all) 

350 

351 
lemmas normalisation_factor_dvd_iff [simp] = 

352 
unit_dvd_iff [OF normalisation_factor_is_unit] 

353 

354 
lemma euclidean_division: 

355 
fixes a :: 'a and b :: 'a 

356 
assumes "b \<noteq> 0" 

357 
obtains s and t where "a = s * b + t" 

358 
and "euclidean_size t < euclidean_size b" 

359 
proof  

360 
from div_mod_equality[of a b 0] 

361 
have "a = a div b * b + a mod b" by simp 

362 
with that and assms show ?thesis by force 

363 
qed 

364 

365 
lemma dvd_euclidean_size_eq_imp_dvd: 

366 
assumes "a \<noteq> 0" and b_dvd_a: "b dvd a" and size_eq: "euclidean_size a = euclidean_size b" 

367 
shows "a dvd b" 

368 
proof (subst dvd_eq_mod_eq_0, rule ccontr) 

369 
assume "b mod a \<noteq> 0" 

370 
from b_dvd_a have b_dvd_mod: "b dvd b mod a" by (simp add: dvd_mod_iff) 

371 
from b_dvd_mod obtain c where "b mod a = b * c" unfolding dvd_def by blast 

372 
with `b mod a \<noteq> 0` have "c \<noteq> 0" by auto 

373 
with `b mod a = b * c` have "euclidean_size (b mod a) \<ge> euclidean_size b" 

374 
using size_mult_mono by force 

375 
moreover from `a \<noteq> 0` have "euclidean_size (b mod a) < euclidean_size a" 

376 
using mod_size_less by blast 

377 
ultimately show False using size_eq by simp 

378 
qed 

379 

380 
function gcd_eucl :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" 

381 
where 

382 
"gcd_eucl a b = (if b = 0 then a div normalisation_factor a else gcd_eucl b (a mod b))" 

383 
by (pat_completeness, simp) 

384 
termination by (relation "measure (euclidean_size \<circ> snd)", simp_all) 

385 

386 
declare gcd_eucl.simps [simp del] 

387 

388 
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" 

389 
proof (induct a b rule: gcd_eucl.induct) 

390 
case ("1" m n) 

391 
then show ?case by (cases "n = 0") auto 

392 
qed 

393 

394 
definition lcm_eucl :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" 

395 
where 

396 
"lcm_eucl a b = a * b div (gcd_eucl a b * normalisation_factor (a * b))" 

397 

398 
(* Somewhat complicated definition of Lcm that has the advantage of working 

399 
for infinite sets as well *) 

400 

401 
definition Lcm_eucl :: "'a set \<Rightarrow> 'a" 

402 
where 

403 
"Lcm_eucl A = (if \<exists>l. l \<noteq> 0 \<and> (\<forall>x\<in>A. x dvd l) then 

404 
let l = SOME l. l \<noteq> 0 \<and> (\<forall>x\<in>A. x dvd l) \<and> euclidean_size l = 

405 
(LEAST n. \<exists>l. l \<noteq> 0 \<and> (\<forall>x\<in>A. x dvd l) \<and> euclidean_size l = n) 

406 
in l div normalisation_factor l 

407 
else 0)" 

408 

409 
definition Gcd_eucl :: "'a set \<Rightarrow> 'a" 

410 
where 

411 
"Gcd_eucl A = Lcm_eucl {d. \<forall>a\<in>A. d dvd a}" 

412 

413 
end 

414 

415 
class euclidean_semiring_gcd = euclidean_semiring + gcd + Gcd + 

416 
assumes gcd_gcd_eucl: "gcd = gcd_eucl" and lcm_lcm_eucl: "lcm = lcm_eucl" 

417 
assumes Gcd_Gcd_eucl: "Gcd = Gcd_eucl" and Lcm_Lcm_eucl: "Lcm = Lcm_eucl" 

418 
begin 

419 

420 
lemma gcd_red: 

421 
"gcd x y = gcd y (x mod y)" 

422 
by (metis gcd_eucl.simps mod_0 mod_by_0 gcd_gcd_eucl) 

423 

424 
lemma gcd_non_0: 

425 
"y \<noteq> 0 \<Longrightarrow> gcd x y = gcd y (x mod y)" 

426 
by (rule gcd_red) 

427 

428 
lemma gcd_0_left: 

429 
"gcd 0 x = x div normalisation_factor x" 

430 
by (simp only: gcd_gcd_eucl, subst gcd_eucl.simps, subst gcd_eucl.simps, simp add: Let_def) 

431 

432 
lemma gcd_0: 

433 
"gcd x 0 = x div normalisation_factor x" 

434 
by (simp only: gcd_gcd_eucl, subst gcd_eucl.simps, simp add: Let_def) 

435 

436 
lemma gcd_dvd1 [iff]: "gcd x y dvd x" 

437 
and gcd_dvd2 [iff]: "gcd x y dvd y" 

438 
proof (induct x y rule: gcd_eucl.induct) 

439 
fix x y :: 'a 

440 
assume IH1: "y \<noteq> 0 \<Longrightarrow> gcd y (x mod y) dvd y" 

441 
assume IH2: "y \<noteq> 0 \<Longrightarrow> gcd y (x mod y) dvd (x mod y)" 

442 

443 
have "gcd x y dvd x \<and> gcd x y dvd y" 

444 
proof (cases "y = 0") 

445 
case True 

446 
then show ?thesis by (cases "x = 0", simp_all add: gcd_0) 

447 
next 

448 
case False 

449 
with IH1 and IH2 show ?thesis by (simp add: gcd_non_0 dvd_mod_iff) 

450 
qed 

451 
then show "gcd x y dvd x" "gcd x y dvd y" by simp_all 

452 
qed 

453 

454 
lemma dvd_gcd_D1: "k dvd gcd m n \<Longrightarrow> k dvd m" 

455 
by (rule dvd_trans, assumption, rule gcd_dvd1) 

456 

457 
lemma dvd_gcd_D2: "k dvd gcd m n \<Longrightarrow> k dvd n" 

458 
by (rule dvd_trans, assumption, rule gcd_dvd2) 

459 

460 
lemma gcd_greatest: 

461 
fixes k x y :: 'a 

462 
shows "k dvd x \<Longrightarrow> k dvd y \<Longrightarrow> k dvd gcd x y" 

463 
proof (induct x y rule: gcd_eucl.induct) 

464 
case (1 x y) 

465 
show ?case 

466 
proof (cases "y = 0") 

467 
assume "y = 0" 

468 
with 1 show ?thesis by (cases "x = 0", simp_all add: gcd_0) 

469 
next 

470 
assume "y \<noteq> 0" 

471 
with 1 show ?thesis by (simp add: gcd_non_0 dvd_mod_iff) 

472 
qed 

473 
qed 

474 

475 
lemma dvd_gcd_iff: 

476 
"k dvd gcd x y \<longleftrightarrow> k dvd x \<and> k dvd y" 

477 
by (blast intro!: gcd_greatest intro: dvd_trans) 

478 

479 
lemmas gcd_greatest_iff = dvd_gcd_iff 

480 

481 
lemma gcd_zero [simp]: 

482 
"gcd x y = 0 \<longleftrightarrow> x = 0 \<and> y = 0" 

483 
by (metis dvd_0_left dvd_refl gcd_dvd1 gcd_dvd2 gcd_greatest)+ 

484 

485 
lemma normalisation_factor_gcd [simp]: 

486 
"normalisation_factor (gcd x y) = (if x = 0 \<and> y = 0 then 0 else 1)" (is "?f x y = ?g x y") 

487 
proof (induct x y rule: gcd_eucl.induct) 

488 
fix x y :: 'a 

489 
assume IH: "y \<noteq> 0 \<Longrightarrow> ?f y (x mod y) = ?g y (x mod y)" 

490 
then show "?f x y = ?g x y" by (cases "y = 0", auto simp: gcd_non_0 gcd_0) 

491 
qed 

492 

493 
lemma gcdI: 

494 
"k dvd x \<Longrightarrow> k dvd y \<Longrightarrow> (\<And>l. l dvd x \<Longrightarrow> l dvd y \<Longrightarrow> l dvd k) 

495 
\<Longrightarrow> normalisation_factor k = (if k = 0 then 0 else 1) \<Longrightarrow> k = gcd x y" 

496 
by (intro normed_associated_imp_eq) (auto simp: associated_def intro: gcd_greatest) 

497 

498 
sublocale gcd!: abel_semigroup gcd 

499 
proof 

500 
fix x y z 

501 
show "gcd (gcd x y) z = gcd x (gcd y z)" 

502 
proof (rule gcdI) 

503 
have "gcd (gcd x y) z dvd gcd x y" "gcd x y dvd x" by simp_all 

504 
then show "gcd (gcd x y) z dvd x" by (rule dvd_trans) 

505 
have "gcd (gcd x y) z dvd gcd x y" "gcd x y dvd y" by simp_all 

506 
hence "gcd (gcd x y) z dvd y" by (rule dvd_trans) 

507 
moreover have "gcd (gcd x y) z dvd z" by simp 

508 
ultimately show "gcd (gcd x y) z dvd gcd y z" 

509 
by (rule gcd_greatest) 

510 
show "normalisation_factor (gcd (gcd x y) z) = (if gcd (gcd x y) z = 0 then 0 else 1)" 

511 
by auto 

512 
fix l assume "l dvd x" and "l dvd gcd y z" 

513 
with dvd_trans[OF _ gcd_dvd1] and dvd_trans[OF _ gcd_dvd2] 

514 
have "l dvd y" and "l dvd z" by blast+ 

515 
with `l dvd x` show "l dvd gcd (gcd x y) z" 

516 
by (intro gcd_greatest) 

517 
qed 

518 
next 

519 
fix x y 

520 
show "gcd x y = gcd y x" 

521 
by (rule gcdI) (simp_all add: gcd_greatest) 

522 
qed 

523 

524 
lemma gcd_unique: "d dvd a \<and> d dvd b \<and> 

525 
normalisation_factor d = (if d = 0 then 0 else 1) \<and> 

526 
(\<forall>e. e dvd a \<and> e dvd b \<longrightarrow> e dvd d) \<longleftrightarrow> d = gcd a b" 

527 
by (rule, auto intro: gcdI simp: gcd_greatest) 

528 

529 
lemma gcd_dvd_prod: "gcd a b dvd k * b" 

530 
using mult_dvd_mono [of 1] by auto 

531 

532 
lemma gcd_1_left [simp]: "gcd 1 x = 1" 

533 
by (rule sym, rule gcdI, simp_all) 

534 

535 
lemma gcd_1 [simp]: "gcd x 1 = 1" 

536 
by (rule sym, rule gcdI, simp_all) 

537 

538 
lemma gcd_proj2_if_dvd: 

539 
"y dvd x \<Longrightarrow> gcd x y = y div normalisation_factor y" 

540 
by (cases "y = 0", simp_all add: dvd_eq_mod_eq_0 gcd_non_0 gcd_0) 

541 

542 
lemma gcd_proj1_if_dvd: 

543 
"x dvd y \<Longrightarrow> gcd x y = x div normalisation_factor x" 

544 
by (subst gcd.commute, simp add: gcd_proj2_if_dvd) 

545 

546 
lemma gcd_proj1_iff: "gcd m n = m div normalisation_factor m \<longleftrightarrow> m dvd n" 

547 
proof 

548 
assume A: "gcd m n = m div normalisation_factor m" 

549 
show "m dvd n" 

550 
proof (cases "m = 0") 

551 
assume [simp]: "m \<noteq> 0" 

552 
from A have B: "m = gcd m n * normalisation_factor m" 

553 
by (simp add: unit_eq_div2) 

554 
show ?thesis by (subst B, simp add: mult_unit_dvd_iff) 

555 
qed (insert A, simp) 

556 
next 

557 
assume "m dvd n" 

558 
then show "gcd m n = m div normalisation_factor m" by (rule gcd_proj1_if_dvd) 

559 
qed 

560 

561 
lemma gcd_proj2_iff: "gcd m n = n div normalisation_factor n \<longleftrightarrow> n dvd m" 

562 
by (subst gcd.commute, simp add: gcd_proj1_iff) 

563 

564 
lemma gcd_mod1 [simp]: 

565 
"gcd (x mod y) y = gcd x y" 

566 
by (rule gcdI, metis dvd_mod_iff gcd_dvd1 gcd_dvd2, simp_all add: gcd_greatest dvd_mod_iff) 

567 

568 
lemma gcd_mod2 [simp]: 

569 
"gcd x (y mod x) = gcd x y" 

570 
by (rule gcdI, simp, metis dvd_mod_iff gcd_dvd1 gcd_dvd2, simp_all add: gcd_greatest dvd_mod_iff) 

571 

572 
lemma normalisation_factor_dvd' [simp]: 

573 
"normalisation_factor x dvd x" 

574 
by (cases "x = 0", simp_all) 

575 

576 
lemma gcd_mult_distrib': 

577 
"k div normalisation_factor k * gcd x y = gcd (k*x) (k*y)" 

578 
proof (induct x y rule: gcd_eucl.induct) 

579 
case (1 x y) 

580 
show ?case 

581 
proof (cases "y = 0") 

582 
case True 

583 
then show ?thesis by (simp add: normalisation_factor_mult gcd_0 algebra_simps div_mult_div_if_dvd) 

584 
next 

585 
case False 

586 
hence "k div normalisation_factor k * gcd x y = gcd (k * y) (k * (x mod y))" 

587 
using 1 by (subst gcd_red, simp) 

588 
also have "... = gcd (k * x) (k * y)" 

589 
by (simp add: mult_mod_right gcd.commute) 

590 
finally show ?thesis . 

591 
qed 

592 
qed 

593 

594 
lemma gcd_mult_distrib: 

595 
"k * gcd x y = gcd (k*x) (k*y) * normalisation_factor k" 

596 
proof 

597 
let ?nf = "normalisation_factor" 

598 
from gcd_mult_distrib' 

599 
have "gcd (k*x) (k*y) = k div ?nf k * gcd x y" .. 

600 
also have "... = k * gcd x y div ?nf k" 

601 
by (metis dvd_div_mult dvd_eq_mod_eq_0 mod_0 normalisation_factor_dvd) 

602 
finally show ?thesis 

59009
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset

603 
by simp 
58023  604 
qed 
605 

606 
lemma euclidean_size_gcd_le1 [simp]: 

607 
assumes "a \<noteq> 0" 

608 
shows "euclidean_size (gcd a b) \<le> euclidean_size a" 

609 
proof  

610 
have "gcd a b dvd a" by (rule gcd_dvd1) 

611 
then obtain c where A: "a = gcd a b * c" unfolding dvd_def by blast 

612 
with `a \<noteq> 0` show ?thesis by (subst (2) A, intro size_mult_mono) auto 

613 
qed 

614 

615 
lemma euclidean_size_gcd_le2 [simp]: 

616 
"b \<noteq> 0 \<Longrightarrow> euclidean_size (gcd a b) \<le> euclidean_size b" 

617 
by (subst gcd.commute, rule euclidean_size_gcd_le1) 

618 

619 
lemma euclidean_size_gcd_less1: 

620 
assumes "a \<noteq> 0" and "\<not>a dvd b" 

621 
shows "euclidean_size (gcd a b) < euclidean_size a" 

622 
proof (rule ccontr) 

623 
assume "\<not>euclidean_size (gcd a b) < euclidean_size a" 

624 
with `a \<noteq> 0` have "euclidean_size (gcd a b) = euclidean_size a" 

625 
by (intro le_antisym, simp_all) 

626 
with assms have "a dvd gcd a b" by (auto intro: dvd_euclidean_size_eq_imp_dvd) 

627 
hence "a dvd b" using dvd_gcd_D2 by blast 

628 
with `\<not>a dvd b` show False by contradiction 

629 
qed 

630 

631 
lemma euclidean_size_gcd_less2: 

632 
assumes "b \<noteq> 0" and "\<not>b dvd a" 

633 
shows "euclidean_size (gcd a b) < euclidean_size b" 

634 
using assms by (subst gcd.commute, rule euclidean_size_gcd_less1) 

635 

636 
lemma gcd_mult_unit1: "is_unit a \<Longrightarrow> gcd (x*a) y = gcd x y" 

637 
apply (rule gcdI) 

638 
apply (rule dvd_trans, rule gcd_dvd1, simp add: unit_simps) 

639 
apply (rule gcd_dvd2) 

640 
apply (rule gcd_greatest, simp add: unit_simps, assumption) 

641 
apply (subst normalisation_factor_gcd, simp add: gcd_0) 

642 
done 

643 

644 
lemma gcd_mult_unit2: "is_unit a \<Longrightarrow> gcd x (y*a) = gcd x y" 

645 
by (subst gcd.commute, subst gcd_mult_unit1, assumption, rule gcd.commute) 

646 

647 
lemma gcd_div_unit1: "is_unit a \<Longrightarrow> gcd (x div a) y = gcd x y" 

648 
by (simp add: unit_ring_inv gcd_mult_unit1) 

649 

650 
lemma gcd_div_unit2: "is_unit a \<Longrightarrow> gcd x (y div a) = gcd x y" 

651 
by (simp add: unit_ring_inv gcd_mult_unit2) 

652 

653 
lemma gcd_idem: "gcd x x = x div normalisation_factor x" 

654 
by (cases "x = 0") (simp add: gcd_0_left, rule sym, rule gcdI, simp_all) 

655 

656 
lemma gcd_right_idem: "gcd (gcd p q) q = gcd p q" 

657 
apply (rule gcdI) 

658 
apply (simp add: ac_simps) 

659 
apply (rule gcd_dvd2) 

660 
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

661 
apply simp 
58023  662 
done 
663 

664 
lemma gcd_left_idem: "gcd p (gcd p q) = gcd p q" 

665 
apply (rule gcdI) 

666 
apply simp 

667 
apply (rule dvd_trans, rule gcd_dvd2, rule gcd_dvd2) 

668 
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

669 
apply simp 
58023  670 
done 
671 

672 
lemma comp_fun_idem_gcd: "comp_fun_idem gcd" 

673 
proof 

674 
fix a b show "gcd a \<circ> gcd b = gcd b \<circ> gcd a" 

675 
by (simp add: fun_eq_iff ac_simps) 

676 
next 

677 
fix a show "gcd a \<circ> gcd a = gcd a" 

678 
by (simp add: fun_eq_iff gcd_left_idem) 

679 
qed 

680 

681 
lemma coprime_dvd_mult: 

682 
assumes "gcd k n = 1" and "k dvd m * n" 

683 
shows "k dvd m" 

684 
proof  

685 
let ?nf = "normalisation_factor" 

686 
from assms gcd_mult_distrib [of m k n] 

687 
have A: "m = gcd (m * k) (m * n) * ?nf m" by simp 

688 
from `k dvd m * n` show ?thesis by (subst A, simp_all add: gcd_greatest) 

689 
qed 

690 

691 
lemma coprime_dvd_mult_iff: 

692 
"gcd k n = 1 \<Longrightarrow> (k dvd m * n) = (k dvd m)" 

693 
by (rule, rule coprime_dvd_mult, simp_all) 

694 

695 
lemma gcd_dvd_antisym: 

696 
"gcd a b dvd gcd c d \<Longrightarrow> gcd c d dvd gcd a b \<Longrightarrow> gcd a b = gcd c d" 

697 
proof (rule gcdI) 

698 
assume A: "gcd a b dvd gcd c d" and B: "gcd c d dvd gcd a b" 

699 
have "gcd c d dvd c" by simp 

700 
with A show "gcd a b dvd c" by (rule dvd_trans) 

701 
have "gcd c d dvd d" by simp 

702 
with A show "gcd a b dvd d" by (rule dvd_trans) 

703 
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

704 
by simp 
58023  705 
fix l assume "l dvd c" and "l dvd d" 
706 
hence "l dvd gcd c d" by (rule gcd_greatest) 

707 
from this and B show "l dvd gcd a b" by (rule dvd_trans) 

708 
qed 

709 

710 
lemma gcd_mult_cancel: 

711 
assumes "gcd k n = 1" 

712 
shows "gcd (k * m) n = gcd m n" 

713 
proof (rule gcd_dvd_antisym) 

714 
have "gcd (gcd (k * m) n) k = gcd (gcd k n) (k * m)" by (simp add: ac_simps) 

715 
also note `gcd k n = 1` 

716 
finally have "gcd (gcd (k * m) n) k = 1" by simp 

717 
hence "gcd (k * m) n dvd m" by (rule coprime_dvd_mult, simp add: ac_simps) 

718 
moreover have "gcd (k * m) n dvd n" by simp 

719 
ultimately show "gcd (k * m) n dvd gcd m n" by (rule gcd_greatest) 

720 
have "gcd m n dvd (k * m)" and "gcd m n dvd n" by simp_all 

721 
then show "gcd m n dvd gcd (k * m) n" by (rule gcd_greatest) 

722 
qed 

723 

724 
lemma coprime_crossproduct: 

725 
assumes [simp]: "gcd a d = 1" "gcd b c = 1" 

726 
shows "associated (a * c) (b * d) \<longleftrightarrow> associated a b \<and> associated c d" (is "?lhs \<longleftrightarrow> ?rhs") 

727 
proof 

728 
assume ?rhs then show ?lhs unfolding associated_def by (fast intro: mult_dvd_mono) 

729 
next 

730 
assume ?lhs 

731 
from `?lhs` have "a dvd b * d" unfolding associated_def by (metis dvd_mult_left) 

732 
hence "a dvd b" by (simp add: coprime_dvd_mult_iff) 

733 
moreover from `?lhs` have "b dvd a * c" unfolding associated_def by (metis dvd_mult_left) 

734 
hence "b dvd a" by (simp add: coprime_dvd_mult_iff) 

735 
moreover from `?lhs` have "c dvd d * b" 

59009
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset

736 
unfolding associated_def by (auto dest: dvd_mult_right simp add: ac_simps) 
58023  737 
hence "c dvd d" by (simp add: coprime_dvd_mult_iff gcd.commute) 
738 
moreover from `?lhs` have "d dvd c * a" 

59009
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset

739 
unfolding associated_def by (auto dest: dvd_mult_right simp add: ac_simps) 
58023  740 
hence "d dvd c" by (simp add: coprime_dvd_mult_iff gcd.commute) 
741 
ultimately show ?rhs unfolding associated_def by simp 

742 
qed 

743 

744 
lemma gcd_add1 [simp]: 

745 
"gcd (m + n) n = gcd m n" 

746 
by (cases "n = 0", simp_all add: gcd_non_0) 

747 

748 
lemma gcd_add2 [simp]: 

749 
"gcd m (m + n) = gcd m n" 

750 
using gcd_add1 [of n m] by (simp add: ac_simps) 

751 

752 
lemma gcd_add_mult: "gcd m (k * m + n) = gcd m n" 

753 
by (subst gcd.commute, subst gcd_red, simp) 

754 

755 
lemma coprimeI: "(\<And>l. \<lbrakk>l dvd x; l dvd y\<rbrakk> \<Longrightarrow> l dvd 1) \<Longrightarrow> gcd x y = 1" 

756 
by (rule sym, rule gcdI, simp_all) 

757 

758 
lemma coprime: "gcd a b = 1 \<longleftrightarrow> (\<forall>d. d dvd a \<and> d dvd b \<longleftrightarrow> is_unit d)" 

759 
by (auto simp: is_unit_def intro: coprimeI gcd_greatest dvd_gcd_D1 dvd_gcd_D2) 

760 

761 
lemma div_gcd_coprime: 

762 
assumes nz: "a \<noteq> 0 \<or> b \<noteq> 0" 

763 
defines [simp]: "d \<equiv> gcd a b" 

764 
defines [simp]: "a' \<equiv> a div d" and [simp]: "b' \<equiv> b div d" 

765 
shows "gcd a' b' = 1" 

766 
proof (rule coprimeI) 

767 
fix l assume "l dvd a'" "l dvd b'" 

768 
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

769 
moreover have "a = a' * d" "b = b' * d" by simp_all 
58023  770 
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

771 
by (simp_all only: ac_simps) 
58023  772 
hence "l*d dvd a" and "l*d dvd b" by (simp_all only: dvd_triv_left) 
773 
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

774 
then obtain u where "d = l * d * u" .. 
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset

775 
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

776 
moreover from nz have "d \<noteq> 0" by simp 
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset

777 
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

778 
ultimately have "1 = l * u" 
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset

779 
using `d \<noteq> 0` by simp 
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset

780 
then show "l dvd 1" .. 
58023  781 
qed 
782 

783 
lemma coprime_mult: 

784 
assumes da: "gcd d a = 1" and db: "gcd d b = 1" 

785 
shows "gcd d (a * b) = 1" 

786 
apply (subst gcd.commute) 

787 
using da apply (subst gcd_mult_cancel) 

788 
apply (subst gcd.commute, assumption) 

789 
apply (subst gcd.commute, rule db) 

790 
done 

791 

792 
lemma coprime_lmult: 

793 
assumes dab: "gcd d (a * b) = 1" 

794 
shows "gcd d a = 1" 

795 
proof (rule coprimeI) 

796 
fix l assume "l dvd d" and "l dvd a" 

797 
hence "l dvd a * b" by simp 

798 
with `l dvd d` and dab show "l dvd 1" by (auto intro: gcd_greatest) 

799 
qed 

800 

801 
lemma coprime_rmult: 

802 
assumes dab: "gcd d (a * b) = 1" 

803 
shows "gcd d b = 1" 

804 
proof (rule coprimeI) 

805 
fix l assume "l dvd d" and "l dvd b" 

806 
hence "l dvd a * b" by simp 

807 
with `l dvd d` and dab show "l dvd 1" by (auto intro: gcd_greatest) 

808 
qed 

809 

810 
lemma coprime_mul_eq: "gcd d (a * b) = 1 \<longleftrightarrow> gcd d a = 1 \<and> gcd d b = 1" 

811 
using coprime_rmult[of d a b] coprime_lmult[of d a b] coprime_mult[of d a b] by blast 

812 

813 
lemma gcd_coprime: 

814 
assumes z: "gcd a b \<noteq> 0" and a: "a = a' * gcd a b" and b: "b = b' * gcd a b" 

815 
shows "gcd a' b' = 1" 

816 
proof  

59009
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset

817 
from z have "a \<noteq> 0 \<or> b \<noteq> 0" by simp 
58023  818 
with div_gcd_coprime have "gcd (a div gcd a b) (b div gcd a b) = 1" . 
819 
also from assms have "a div gcd a b = a'" by (metis div_mult_self2_is_id)+ 

820 
also from assms have "b div gcd a b = b'" by (metis div_mult_self2_is_id)+ 

821 
finally show ?thesis . 

822 
qed 

823 

824 
lemma coprime_power: 

825 
assumes "0 < n" 

826 
shows "gcd a (b ^ n) = 1 \<longleftrightarrow> gcd a b = 1" 

827 
using assms proof (induct n) 

828 
case (Suc n) then show ?case 

829 
by (cases n) (simp_all add: coprime_mul_eq) 

830 
qed simp 

831 

832 
lemma gcd_coprime_exists: 

833 
assumes nz: "gcd a b \<noteq> 0" 

834 
shows "\<exists>a' b'. a = a' * gcd a b \<and> b = b' * gcd a b \<and> gcd a' b' = 1" 

835 
apply (rule_tac x = "a div gcd a b" in exI) 

836 
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

837 
apply (insert nz, auto intro: div_gcd_coprime) 
58023  838 
done 
839 

840 
lemma coprime_exp: 

841 
"gcd d a = 1 \<Longrightarrow> gcd d (a^n) = 1" 

842 
by (induct n, simp_all add: coprime_mult) 

843 

844 
lemma coprime_exp2 [intro]: 

845 
"gcd a b = 1 \<Longrightarrow> gcd (a^n) (b^m) = 1" 

846 
apply (rule coprime_exp) 

847 
apply (subst gcd.commute) 

848 
apply (rule coprime_exp) 

849 
apply (subst gcd.commute) 

850 
apply assumption 

851 
done 

852 

853 
lemma gcd_exp: 

854 
"gcd (a^n) (b^n) = (gcd a b) ^ n" 

855 
proof (cases "a = 0 \<and> b = 0") 

856 
assume "a = 0 \<and> b = 0" 

857 
then show ?thesis by (cases n, simp_all add: gcd_0_left) 

858 
next 

859 
assume A: "\<not>(a = 0 \<and> b = 0)" 

860 
hence "1 = gcd ((a div gcd a b)^n) ((b div gcd a b)^n)" 

861 
using div_gcd_coprime by (subst sym, auto simp: div_gcd_coprime) 

862 
hence "(gcd a b) ^ n = (gcd a b) ^ n * ..." by simp 

863 
also note gcd_mult_distrib 

864 
also have "normalisation_factor ((gcd a b)^n) = 1" 

865 
by (simp add: normalisation_factor_pow A) 

866 
also have "(gcd a b)^n * (a div gcd a b)^n = a^n" 

867 
by (subst ac_simps, subst div_power, simp, rule dvd_div_mult_self, rule dvd_power_same, simp) 

868 
also have "(gcd a b)^n * (b div gcd a b)^n = b^n" 

869 
by (subst ac_simps, subst div_power, simp, rule dvd_div_mult_self, rule dvd_power_same, simp) 

870 
finally show ?thesis by simp 

871 
qed 

872 

873 
lemma coprime_common_divisor: 

874 
"gcd a b = 1 \<Longrightarrow> x dvd a \<Longrightarrow> x dvd b \<Longrightarrow> is_unit x" 

875 
apply (subgoal_tac "x dvd gcd a b") 

876 
apply (simp add: is_unit_def) 

877 
apply (erule (1) gcd_greatest) 

878 
done 

879 

880 
lemma division_decomp: 

881 
assumes dc: "a dvd b * c" 

882 
shows "\<exists>b' c'. a = b' * c' \<and> b' dvd b \<and> c' dvd c" 

883 
proof (cases "gcd a b = 0") 

884 
assume "gcd a b = 0" 

59009
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset

885 
hence "a = 0 \<and> b = 0" by simp 
58023  886 
hence "a = 0 * c \<and> 0 dvd b \<and> c dvd c" by simp 
887 
then show ?thesis by blast 

888 
next 

889 
let ?d = "gcd a b" 

890 
assume "?d \<noteq> 0" 

891 
from gcd_coprime_exists[OF this] 

892 
obtain a' b' where ab': "a = a' * ?d" "b = b' * ?d" "gcd a' b' = 1" 

893 
by blast 

894 
from ab'(1) have "a' dvd a" unfolding dvd_def by blast 

895 
with dc have "a' dvd b*c" using dvd_trans[of a' a "b*c"] by simp 

896 
from dc ab'(1,2) have "a'*?d dvd (b'*?d) * c" by simp 

897 
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

898 
with `?d \<noteq> 0` have "a' dvd b' * c" by simp 
58023  899 
with coprime_dvd_mult[OF ab'(3)] 
900 
have "a' dvd c" by (subst (asm) ac_simps, blast) 

901 
with ab'(1) have "a = ?d * a' \<and> ?d dvd b \<and> a' dvd c" by (simp add: mult_ac) 

902 
then show ?thesis by blast 

903 
qed 

904 

905 
lemma pow_divides_pow: 

906 
assumes ab: "a ^ n dvd b ^ n" and n: "n \<noteq> 0" 

907 
shows "a dvd b" 

908 
proof (cases "gcd a b = 0") 

909 
assume "gcd a b = 0" 

59009
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset

910 
then show ?thesis by simp 
58023  911 
next 
912 
let ?d = "gcd a b" 

913 
assume "?d \<noteq> 0" 

914 
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

915 
from `?d \<noteq> 0` have zn: "?d ^ n \<noteq> 0" by (rule power_not_zero) 
58023  916 
from gcd_coprime_exists[OF `?d \<noteq> 0`] 
917 
obtain a' b' where ab': "a = a' * ?d" "b = b' * ?d" "gcd a' b' = 1" 

918 
by blast 

919 
from ab have "(a' * ?d) ^ n dvd (b' * ?d) ^ n" 

920 
by (simp add: ab'(1,2)[symmetric]) 

921 
hence "?d^n * a'^n dvd ?d^n * b'^n" 

922 
by (simp only: power_mult_distrib ac_simps) 

59009
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset

923 
with zn have "a'^n dvd b'^n" by simp 
58023  924 
hence "a' dvd b'^n" using dvd_trans[of a' "a'^n" "b'^n"] by (simp add: m) 
925 
hence "a' dvd b'^m * b'" by (simp add: m ac_simps) 

926 
with coprime_dvd_mult[OF coprime_exp[OF ab'(3), of m]] 

927 
have "a' dvd b'" by (subst (asm) ac_simps, blast) 

928 
hence "a'*?d dvd b'*?d" by (rule mult_dvd_mono, simp) 

929 
with ab'(1,2) show ?thesis by simp 

930 
qed 

931 

932 
lemma pow_divides_eq [simp]: 

933 
"n \<noteq> 0 \<Longrightarrow> a ^ n dvd b ^ n \<longleftrightarrow> a dvd b" 

934 
by (auto intro: pow_divides_pow dvd_power_same) 

935 

936 
lemma divides_mult: 

937 
assumes mr: "m dvd r" and nr: "n dvd r" and mn: "gcd m n = 1" 

938 
shows "m * n dvd r" 

939 
proof  

940 
from mr nr obtain m' n' where m': "r = m*m'" and n': "r = n*n'" 

941 
unfolding dvd_def by blast 

942 
from mr n' have "m dvd n'*n" by (simp add: ac_simps) 

943 
hence "m dvd n'" using coprime_dvd_mult_iff[OF mn] by simp 

944 
then obtain k where k: "n' = m*k" unfolding dvd_def by blast 

945 
with n' have "r = m * n * k" by (simp add: mult_ac) 

946 
then show ?thesis unfolding dvd_def by blast 

947 
qed 

948 

949 
lemma coprime_plus_one [simp]: "gcd (n + 1) n = 1" 

950 
by (subst add_commute, simp) 

951 

952 
lemma setprod_coprime [rule_format]: 

953 
"(\<forall>i\<in>A. gcd (f i) x = 1) \<longrightarrow> gcd (\<Prod>i\<in>A. f i) x = 1" 

954 
apply (cases "finite A") 

955 
apply (induct set: finite) 

956 
apply (auto simp add: gcd_mult_cancel) 

957 
done 

958 

959 
lemma coprime_divisors: 

960 
assumes "d dvd a" "e dvd b" "gcd a b = 1" 

961 
shows "gcd d e = 1" 

962 
proof  

963 
from assms obtain k l where "a = d * k" "b = e * l" 

964 
unfolding dvd_def by blast 

965 
with assms have "gcd (d * k) (e * l) = 1" by simp 

966 
hence "gcd (d * k) e = 1" by (rule coprime_lmult) 

967 
also have "gcd (d * k) e = gcd e (d * k)" by (simp add: ac_simps) 

968 
finally have "gcd e d = 1" by (rule coprime_lmult) 

969 
then show ?thesis by (simp add: ac_simps) 

970 
qed 

971 

972 
lemma invertible_coprime: 

59009
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset

973 
assumes "x * y mod m = 1" 
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset

974 
shows "coprime x m" 
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset

975 
proof  
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset

976 
from assms have "coprime m (x * y mod m)" 
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset

977 
by simp 
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset

978 
then have "coprime m (x * y)" 
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset

979 
by simp 
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset

980 
then have "coprime m x" 
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset

981 
by (rule coprime_lmult) 
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset

982 
then show ?thesis 
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset

983 
by (simp add: ac_simps) 
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset

984 
qed 
58023  985 

986 
lemma lcm_gcd: 

987 
"lcm a b = a * b div (gcd a b * normalisation_factor (a*b))" 

988 
by (simp only: lcm_lcm_eucl gcd_gcd_eucl lcm_eucl_def) 

989 

990 
lemma lcm_gcd_prod: 

991 
"lcm a b * gcd a b = a * b div normalisation_factor (a*b)" 

992 
proof (cases "a * b = 0") 

993 
let ?nf = normalisation_factor 

994 
assume "a * b \<noteq> 0" 

58953  995 
hence "gcd a b \<noteq> 0" by simp 
58023  996 
from lcm_gcd have "lcm a b * gcd a b = gcd a b * (a * b div (?nf (a*b) * gcd a b))" 
997 
by (simp add: mult_ac) 

998 
also from `a * b \<noteq> 0` have "... = a * b div ?nf (a*b)" 

58953  999 
by (simp_all add: unit_ring_inv'1 unit_ring_inv) 
58023  1000 
finally show ?thesis . 
58953  1001 
qed (auto simp add: lcm_gcd) 
58023  1002 

1003 
lemma lcm_dvd1 [iff]: 

1004 
"x dvd lcm x y" 

1005 
proof (cases "x*y = 0") 

1006 
assume "x * y \<noteq> 0" 

58953  1007 
hence "gcd x y \<noteq> 0" by simp 
58023  1008 
let ?c = "ring_inv (normalisation_factor (x*y))" 
1009 
from `x * y \<noteq> 0` have [simp]: "is_unit (normalisation_factor (x*y))" by simp 

1010 
from lcm_gcd_prod[of x y] have "lcm x y * gcd x y = x * ?c * y" 

1011 
by (simp add: mult_ac unit_ring_inv) 

1012 
hence "lcm x y * gcd x y div gcd x y = x * ?c * y div gcd x y" by simp 

1013 
with `gcd x y \<noteq> 0` have "lcm x y = x * ?c * y div gcd x y" 

1014 
by (subst (asm) div_mult_self2_is_id, simp_all) 

1015 
also have "... = x * (?c * y div gcd x y)" 

1016 
by (metis div_mult_swap gcd_dvd2 mult_assoc) 

1017 
finally show ?thesis by (rule dvdI) 

58953  1018 
qed (auto simp add: lcm_gcd) 
58023  1019 

1020 
lemma lcm_least: 

1021 
"\<lbrakk>a dvd k; b dvd k\<rbrakk> \<Longrightarrow> lcm a b dvd k" 

1022 
proof (cases "k = 0") 

1023 
let ?nf = normalisation_factor 

1024 
assume "k \<noteq> 0" 

1025 
hence "is_unit (?nf k)" by simp 

1026 
hence "?nf k \<noteq> 0" by (metis not_is_unit_0) 

1027 
assume A: "a dvd k" "b dvd k" 

58953  1028 
hence "gcd a b \<noteq> 0" using `k \<noteq> 0` by auto 
58023  1029 
from A obtain r s where ar: "k = a * r" and bs: "k = b * s" 
1030 
unfolding dvd_def by blast 

58953  1031 
with `k \<noteq> 0` have "r * s \<noteq> 0" 
1032 
by auto (drule sym [of 0], simp) 

58023  1033 
hence "is_unit (?nf (r * s))" by simp 
1034 
let ?c = "?nf k div ?nf (r*s)" 

1035 
from `is_unit (?nf k)` and `is_unit (?nf (r * s))` have "is_unit ?c" by (rule unit_div) 

1036 
hence "?c \<noteq> 0" using not_is_unit_0 by fast 

1037 
from ar bs have "k * k * gcd s r = ?nf k * k * gcd (k * s) (k * r)" 

58953  1038 
by (subst mult_assoc, subst gcd_mult_distrib[of k s r], simp only: ac_simps) 
58023  1039 
also have "... = ?nf k * k * gcd ((r*s) * a) ((r*s) * b)" 
1040 
by (subst (3) `k = a * r`, subst (3) `k = b * s`, simp add: algebra_simps) 

1041 
also have "... = ?c * r*s * k * gcd a b" using `r * s \<noteq> 0` 

1042 
by (subst gcd_mult_distrib'[symmetric], simp add: algebra_simps unit_simps) 

1043 
finally have "(a*r) * (b*s) * gcd s r = ?c * k * r * s * gcd a b" 

1044 
by (subst ar[symmetric], subst bs[symmetric], simp add: mult_ac) 

1045 
hence "a * b * gcd s r * (r * s) = ?c * k * gcd a b * (r * s)" 

1046 
by (simp add: algebra_simps) 

1047 
hence "?c * k * gcd a b = a * b * gcd s r" using `r * s \<noteq> 0` 

1048 
by (metis div_mult_self2_is_id) 

1049 
also have "... = lcm a b * gcd a b * gcd s r * ?nf (a*b)" 

1050 
by (subst lcm_gcd_prod[of a b], metis gcd_mult_distrib gcd_mult_distrib') 

1051 
also have "... = lcm a b * gcd s r * ?nf (a*b) * gcd a b" 

1052 
by (simp add: algebra_simps) 

1053 
finally have "k * ?c = lcm a b * gcd s r * ?nf (a*b)" using `gcd a b \<noteq> 0` 

1054 
by (metis mult.commute div_mult_self2_is_id) 

1055 
hence "k = lcm a b * (gcd s r * ?nf (a*b)) div ?c" using `?c \<noteq> 0` 

1056 
by (metis div_mult_self2_is_id mult_assoc) 

1057 
also have "... = lcm a b * (gcd s r * ?nf (a*b) div ?c)" using `is_unit ?c` 

1058 
by (simp add: unit_simps) 

1059 
finally show ?thesis by (rule dvdI) 

1060 
qed simp 

1061 

1062 
lemma lcm_zero: 

1063 
"lcm a b = 0 \<longleftrightarrow> a = 0 \<or> b = 0" 

1064 
proof  

1065 
let ?nf = normalisation_factor 

1066 
{ 

1067 
assume "a \<noteq> 0" "b \<noteq> 0" 

1068 
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

1069 
moreover from `a \<noteq> 0` and `b \<noteq> 0` have "gcd a b \<noteq> 0" by simp 
58023  1070 
ultimately have "lcm a b \<noteq> 0" using lcm_gcd_prod[of a b] by (intro notI, simp) 
1071 
} moreover { 

1072 
assume "a = 0 \<or> b = 0" 

1073 
hence "lcm a b = 0" by (elim disjE, simp_all add: lcm_gcd) 

1074 
} 

1075 
ultimately show ?thesis by blast 

1076 
qed 

1077 

1078 
lemmas lcm_0_iff = lcm_zero 

1079 

1080 
lemma gcd_lcm: 

1081 
assumes "lcm a b \<noteq> 0" 

1082 
shows "gcd a b = a * b div (lcm a b * normalisation_factor (a * b))" 

1083 
proof 

59009
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset

1084 
from assms have "gcd a b \<noteq> 0" by (simp add: lcm_zero) 
58023  1085 
let ?c = "normalisation_factor (a*b)" 
1086 
from `lcm a b \<noteq> 0` have "?c \<noteq> 0" by (intro notI, simp add: lcm_zero no_zero_divisors) 

1087 
hence "is_unit ?c" by simp 

1088 
from lcm_gcd_prod [of a b] have "gcd a b = a * b div ?c div lcm a b" 

1089 
by (subst (2) div_mult_self2_is_id[OF `lcm a b \<noteq> 0`, symmetric], simp add: mult_ac) 

1090 
also from `is_unit ?c` have "... = a * b div (?c * lcm a b)" 

1091 
by (simp only: unit_ring_inv'1 unit_ring_inv) 

1092 
finally show ?thesis by (simp only: ac_simps) 

1093 
qed 

1094 

1095 
lemma normalisation_factor_lcm [simp]: 

1096 
"normalisation_factor (lcm a b) = (if a = 0 \<or> b = 0 then 0 else 1)" 

1097 
proof (cases "a = 0 \<or> b = 0") 

1098 
case True then show ?thesis 

58953  1099 
by (auto simp add: lcm_gcd) 
58023  1100 
next 
1101 
case False 

1102 
let ?nf = normalisation_factor 

1103 
from lcm_gcd_prod[of a b] 

1104 
have "?nf (lcm a b) * ?nf (gcd a b) = ?nf (a*b) div ?nf (a*b)" 

1105 
by (metis div_by_0 div_self normalisation_correct normalisation_factor_0 normalisation_factor_mult) 

1106 
also have "... = (if a*b = 0 then 0 else 1)" 

58953  1107 
by simp 
1108 
finally show ?thesis using False by simp 

58023  1109 
qed 
1110 

1111 
lemma lcm_dvd2 [iff]: "y dvd lcm x y" 

1112 
using lcm_dvd1 [of y x] by (simp add: lcm_gcd ac_simps) 

1113 

1114 
lemma lcmI: 

1115 
"\<lbrakk>x dvd k; y dvd k; \<And>l. x dvd l \<Longrightarrow> y dvd l \<Longrightarrow> k dvd l; 

1116 
normalisation_factor k = (if k = 0 then 0 else 1)\<rbrakk> \<Longrightarrow> k = lcm x y" 

1117 
by (intro normed_associated_imp_eq) (auto simp: associated_def intro: lcm_least) 

1118 

1119 
sublocale lcm!: abel_semigroup lcm 

1120 
proof 

1121 
fix x y z 

1122 
show "lcm (lcm x y) z = lcm x (lcm y z)" 

1123 
proof (rule lcmI) 

1124 
have "x dvd lcm x y" and "lcm x y dvd lcm (lcm x y) z" by simp_all 

1125 
then show "x dvd lcm (lcm x y) z" by (rule dvd_trans) 

1126 

1127 
have "y dvd lcm x y" and "lcm x y dvd lcm (lcm x y) z" by simp_all 

1128 
hence "y dvd lcm (lcm x y) z" by (rule dvd_trans) 

1129 
moreover have "z dvd lcm (lcm x y) z" by simp 

1130 
ultimately show "lcm y z dvd lcm (lcm x y) z" by (rule lcm_least) 

1131 

1132 
fix l assume "x dvd l" and "lcm y z dvd l" 

1133 
have "y dvd lcm y z" by simp 

1134 
from this and `lcm y z dvd l` have "y dvd l" by (rule dvd_trans) 

1135 
have "z dvd lcm y z" by simp 

1136 
from this and `lcm y z dvd l` have "z dvd l" by (rule dvd_trans) 

1137 
from `x dvd l` and `y dvd l` have "lcm x y dvd l" by (rule lcm_least) 

1138 
from this and `z dvd l` show "lcm (lcm x y) z dvd l" by (rule lcm_least) 

1139 
qed (simp add: lcm_zero) 

1140 
next 

1141 
fix x y 

1142 
show "lcm x y = lcm y x" 

1143 
by (simp add: lcm_gcd ac_simps) 

1144 
qed 

1145 

1146 
lemma dvd_lcm_D1: 

1147 
"lcm m n dvd k \<Longrightarrow> m dvd k" 

1148 
by (rule dvd_trans, rule lcm_dvd1, assumption) 

1149 

1150 
lemma dvd_lcm_D2: 

1151 
"lcm m n dvd k \<Longrightarrow> n dvd k" 

1152 
by (rule dvd_trans, rule lcm_dvd2, assumption) 

1153 

1154 
lemma gcd_dvd_lcm [simp]: 

1155 
"gcd a b dvd lcm a b" 

1156 
by (metis dvd_trans gcd_dvd2 lcm_dvd2) 

1157 

1158 
lemma lcm_1_iff: 

1159 
"lcm a b = 1 \<longleftrightarrow> is_unit a \<and> is_unit b" 

1160 
proof 

1161 
assume "lcm a b = 1" 

1162 
then show "is_unit a \<and> is_unit b" unfolding is_unit_def by auto 

1163 
next 

1164 
assume "is_unit a \<and> is_unit b" 

1165 
hence "a dvd 1" and "b dvd 1" unfolding is_unit_def by simp_all 

1166 
hence "is_unit (lcm a b)" unfolding is_unit_def by (rule lcm_least) 

1167 
hence "lcm a b = normalisation_factor (lcm a b)" 

1168 
by (subst normalisation_factor_unit, simp_all) 

1169 
also have "\<dots> = 1" using `is_unit a \<and> is_unit b` by (auto simp add: is_unit_def) 

1170 
finally show "lcm a b = 1" . 

1171 
qed 

1172 

1173 
lemma lcm_0_left [simp]: 

1174 
"lcm 0 x = 0" 

1175 
by (rule sym, rule lcmI, simp_all) 

1176 

1177 
lemma lcm_0 [simp]: 

1178 
"lcm x 0 = 0" 

1179 
by (rule sym, rule lcmI, simp_all) 

1180 

1181 
lemma lcm_unique: 

1182 
"a dvd d \<and> b dvd d \<and> 

1183 
normalisation_factor d = (if d = 0 then 0 else 1) \<and> 

1184 
(\<forall>e. a dvd e \<and> b dvd e \<longrightarrow> d dvd e) \<longleftrightarrow> d = lcm a b" 

1185 
by (rule, auto intro: lcmI simp: lcm_least lcm_zero) 

1186 

1187 
lemma dvd_lcm_I1 [simp]: 

1188 
"k dvd m \<Longrightarrow> k dvd lcm m n" 

1189 
by (metis lcm_dvd1 dvd_trans) 

1190 

1191 
lemma dvd_lcm_I2 [simp]: 

1192 
"k dvd n \<Longrightarrow> k dvd lcm m n" 

1193 
by (metis lcm_dvd2 dvd_trans) 

1194 

1195 
lemma lcm_1_left [simp]: 

1196 
"lcm 1 x = x div normalisation_factor x" 

1197 
by (cases "x = 0") (simp, rule sym, rule lcmI, simp_all) 

1198 

1199 
lemma lcm_1_right [simp]: 

1200 
"lcm x 1 = x div normalisation_factor x" 

1201 
by (simp add: ac_simps) 

1202 

1203 
lemma lcm_coprime: 

1204 
"gcd a b = 1 \<Longrightarrow> lcm a b = a * b div normalisation_factor (a*b)" 

1205 
by (subst lcm_gcd) simp 

1206 

1207 
lemma lcm_proj1_if_dvd: 

1208 
"y dvd x \<Longrightarrow> lcm x y = x div normalisation_factor x" 

1209 
by (cases "x = 0") (simp, rule sym, rule lcmI, simp_all) 

1210 

1211 
lemma lcm_proj2_if_dvd: 

1212 
"x dvd y \<Longrightarrow> lcm x y = y div normalisation_factor y" 

1213 
using lcm_proj1_if_dvd [of x y] by (simp add: ac_simps) 

1214 

1215 
lemma lcm_proj1_iff: 

1216 
"lcm m n = m div normalisation_factor m \<longleftrightarrow> n dvd m" 

1217 
proof 

1218 
assume A: "lcm m n = m div normalisation_factor m" 

1219 
show "n dvd m" 

1220 
proof (cases "m = 0") 

1221 
assume [simp]: "m \<noteq> 0" 

1222 
from A have B: "m = lcm m n * normalisation_factor m" 

1223 
by (simp add: unit_eq_div2) 

1224 
show ?thesis by (subst B, simp) 

1225 
qed simp 

1226 
next 

1227 
assume "n dvd m" 

1228 
then show "lcm m n = m div normalisation_factor m" by (rule lcm_proj1_if_dvd) 

1229 
qed 

1230 

1231 
lemma lcm_proj2_iff: 

1232 
"lcm m n = n div normalisation_factor n \<longleftrightarrow> m dvd n" 

1233 
using lcm_proj1_iff [of n m] by (simp add: ac_simps) 

1234 

1235 
lemma euclidean_size_lcm_le1: 

1236 
assumes "a \<noteq> 0" and "b \<noteq> 0" 

1237 
shows "euclidean_size a \<le> euclidean_size (lcm a b)" 

1238 
proof  

1239 
have "a dvd lcm a b" by (rule lcm_dvd1) 

1240 
then obtain c where A: "lcm a b = a * c" unfolding dvd_def by blast 

1241 
with `a \<noteq> 0` and `b \<noteq> 0` have "c \<noteq> 0" by (auto simp: lcm_zero) 

1242 
then show ?thesis by (subst A, intro size_mult_mono) 

1243 
qed 

1244 

1245 
lemma euclidean_size_lcm_le2: 

1246 
"a \<noteq> 0 \<Longrightarrow> b \<noteq> 0 \<Longrightarrow> euclidean_size b \<le> euclidean_size (lcm a b)" 

1247 
using euclidean_size_lcm_le1 [of b a] by (simp add: ac_simps) 

1248 

1249 
lemma euclidean_size_lcm_less1: 

1250 
assumes "b \<noteq> 0" and "\<not>b dvd a" 

1251 
shows "euclidean_size a < euclidean_size (lcm a b)" 

1252 
proof (rule ccontr) 

1253 
from assms have "a \<noteq> 0" by auto 

1254 
assume "\<not>euclidean_size a < euclidean_size (lcm a b)" 

1255 
with `a \<noteq> 0` and `b \<noteq> 0` have "euclidean_size (lcm a b) = euclidean_size a" 

1256 
by (intro le_antisym, simp, intro euclidean_size_lcm_le1) 

1257 
with assms have "lcm a b dvd a" 

1258 
by (rule_tac dvd_euclidean_size_eq_imp_dvd) (auto simp: lcm_zero) 

1259 
hence "b dvd a" by (rule dvd_lcm_D2) 

1260 
with `\<not>b dvd a` show False by contradiction 

1261 
qed 

1262 

1263 
lemma euclidean_size_lcm_less2: 

1264 
assumes "a \<noteq> 0" and "\<not>a dvd b" 

1265 
shows "euclidean_size b < euclidean_size (lcm a b)" 

1266 
using assms euclidean_size_lcm_less1 [of a b] by (simp add: ac_simps) 

1267 

1268 
lemma lcm_mult_unit1: 

1269 
"is_unit a \<Longrightarrow> lcm (x*a) y = lcm x y" 

1270 
apply (rule lcmI) 

1271 
apply (rule dvd_trans[of _ "x*a"], simp, rule lcm_dvd1) 

1272 
apply (rule lcm_dvd2) 

1273 
apply (rule lcm_least, simp add: unit_simps, assumption) 

1274 
apply (subst normalisation_factor_lcm, simp add: lcm_zero) 

1275 
done 

1276 

1277 
lemma lcm_mult_unit2: 

1278 
"is_unit a \<Longrightarrow> lcm x (y*a) = lcm x y" 

1279 
using lcm_mult_unit1 [of a y x] by (simp add: ac_simps) 

1280 

1281 
lemma lcm_div_unit1: 

1282 
"is_unit a \<Longrightarrow> lcm (x div a) y = lcm x y" 

1283 
by (simp add: unit_ring_inv lcm_mult_unit1) 

1284 

1285 
lemma lcm_div_unit2: 

1286 
"is_unit a \<Longrightarrow> lcm x (y div a) = lcm x y" 

1287 
by (simp add: unit_ring_inv lcm_mult_unit2) 

1288 

1289 
lemma lcm_left_idem: 

1290 
"lcm p (lcm p q) = lcm p q" 

1291 
apply (rule lcmI) 

1292 
apply simp 

1293 
apply (subst lcm.assoc [symmetric], rule lcm_dvd2) 

1294 
apply (rule lcm_least, assumption) 

1295 
apply (erule (1) lcm_least) 

1296 
apply (auto simp: lcm_zero) 

1297 
done 

1298 

1299 
lemma lcm_right_idem: 

1300 
"lcm (lcm p q) q = lcm p q" 

1301 
apply (rule lcmI) 

1302 
apply (subst lcm.assoc, rule lcm_dvd1) 

1303 
apply (rule lcm_dvd2) 

1304 
apply (rule lcm_least, erule (1) lcm_least, assumption) 

1305 
apply (auto simp: lcm_zero) 

1306 
done 

1307 

1308 
lemma comp_fun_idem_lcm: "comp_fun_idem lcm" 

1309 
proof 

1310 
fix a b show "lcm a \<circ> lcm b = lcm b \<circ> lcm a" 

1311 
by (simp add: fun_eq_iff ac_simps) 

1312 
next 

1313 
fix a show "lcm a \<circ> lcm a = lcm a" unfolding o_def 

1314 
by (intro ext, simp add: lcm_left_idem) 

1315 
qed 

1316 

1317 
lemma dvd_Lcm [simp]: "x \<in> A \<Longrightarrow> x dvd Lcm A" 

1318 
and Lcm_dvd [simp]: "(\<forall>x\<in>A. x dvd l') \<Longrightarrow> Lcm A dvd l'" 

1319 
and normalisation_factor_Lcm [simp]: 

1320 
"normalisation_factor (Lcm A) = (if Lcm A = 0 then 0 else 1)" 

1321 
proof  

1322 
have "(\<forall>x\<in>A. x dvd Lcm A) \<and> (\<forall>l'. (\<forall>x\<in>A. x dvd l') \<longrightarrow> Lcm A dvd l') \<and> 

1323 
normalisation_factor (Lcm A) = (if Lcm A = 0 then 0 else 1)" (is ?thesis) 

1324 
proof (cases "\<exists>l. l \<noteq> 0 \<and> (\<forall>x\<in>A. x dvd l)") 

1325 
case False 

1326 
hence "Lcm A = 0" by (auto simp: Lcm_Lcm_eucl Lcm_eucl_def) 

1327 
with False show ?thesis by auto 

1328 
next 

1329 
case True 

1330 
then obtain l\<^sub>0 where l\<^sub>0_props: "l\<^sub>0 \<noteq> 0 \<and> (\<forall>x\<in>A. x dvd l\<^sub>0)" by blast 

1331 
def n \<equiv> "LEAST n. \<exists>l. l \<noteq> 0 \<and> (\<forall>x\<in>A. x dvd l) \<and> euclidean_size l = n" 

1332 
def l \<equiv> "SOME l. l \<noteq> 0 \<and> (\<forall>x\<in>A. x dvd l) \<and> euclidean_size l = n" 

1333 
have "\<exists>l. l \<noteq> 0 \<and> (\<forall>x\<in>A. x dvd l) \<and> euclidean_size l = n" 

1334 
apply (subst n_def) 

1335 
apply (rule LeastI[of _ "euclidean_size l\<^sub>0"]) 

1336 
apply (rule exI[of _ l\<^sub>0]) 

1337 
apply (simp add: l\<^sub>0_props) 

1338 
done 

1339 
from someI_ex[OF this] have "l \<noteq> 0" and "\<forall>x\<in>A. x dvd l" and "euclidean_size l = n" 

1340 
unfolding l_def by simp_all 

1341 
{ 

1342 
fix l' assume "\<forall>x\<in>A. x dvd l'" 

1343 
with `\<forall>x\<in>A. x dvd l` have "\<forall>x\<in>A. x dvd gcd l l'" by (auto intro: gcd_greatest) 

59009
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset

1344 
moreover from `l \<noteq> 0` have "gcd l l' \<noteq> 0" by simp 
58023  1345 
ultimately have "\<exists>b. b \<noteq> 0 \<and> (\<forall>x\<in>A. x dvd b) \<and> euclidean_size b = euclidean_size (gcd l l')" 
1346 
by (intro exI[of _ "gcd l l'"], auto) 

1347 
hence "euclidean_size (gcd l l') \<ge> n" by (subst n_def) (rule Least_le) 

1348 
moreover have "euclidean_size (gcd l l') \<le> n" 

1349 
proof  

1350 
have "gcd l l' dvd l" by simp 

1351 
then obtain a where "l = gcd l l' * a" unfolding dvd_def by blast 

1352 
with `l \<noteq> 0` have "a \<noteq> 0" by auto 

1353 
hence "euclidean_size (gcd l l') \<le> euclidean_size (gcd l l' * a)" 

1354 
by (rule size_mult_mono) 

1355 
also have "gcd l l' * a = l" using `l = gcd l l' * a` .. 

1356 
also note `euclidean_size l = n` 

1357 
finally show "euclidean_size (gcd l l') \<le> n" . 

1358 
qed 

1359 
ultimately have "euclidean_size l = euclidean_size (gcd l l')" 

1360 
by (intro le_antisym, simp_all add: `euclidean_size l = n`) 

1361 
with `l \<noteq> 0` have "l dvd gcd l l'" by (blast intro: dvd_euclidean_size_eq_imp_dvd) 

1362 
hence "l dvd l'" by (blast dest: dvd_gcd_D2) 

1363 
} 

62826b36ac5e
generic euclidean algorithm (due to Manu 