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