src/HOL/Number_Theory/Euclidean_Algorithm.thy
 author haftmann Sat Jun 27 20:26:33 2015 +0200 (2015-06-27) changeset 60600 87fbfea0bd0a parent 60599 f8bb070dc98b child 60634 e3b6e516608b permissions -rw-r--r--
simplified termination criterion for euclidean algorithm (again)
1 (* Author: Manuel Eberl *)
3 section \<open>Abstract euclidean algorithm\<close>
5 theory Euclidean_Algorithm
6 imports Complex_Main "~~/src/HOL/Library/Polynomial"
7 begin
9 text \<open>
10   A Euclidean semiring is a semiring upon which the Euclidean algorithm can be
11   implemented. It must provide:
12   \begin{itemize}
13   \item division with remainder
14   \item a size function such that @{term "size (a mod b) < size b"}
15         for any @{term "b \<noteq> 0"}
16   \item a normalization factor such that two associated numbers are equal iff
17         they are the same when divd by their normalization factors.
18   \end{itemize}
19   The existence of these functions makes it possible to derive gcd and lcm functions
20   for any Euclidean semiring.
21 \<close>
22 class euclidean_semiring = semiring_div +
23   fixes euclidean_size :: "'a \<Rightarrow> nat"
24   fixes normalization_factor :: "'a \<Rightarrow> 'a"
25   assumes mod_size_less:
26     "b \<noteq> 0 \<Longrightarrow> euclidean_size (a mod b) < euclidean_size b"
27   assumes size_mult_mono:
28     "b \<noteq> 0 \<Longrightarrow> euclidean_size (a * b) \<ge> euclidean_size a"
29   assumes normalization_factor_is_unit [intro,simp]:
30     "a \<noteq> 0 \<Longrightarrow> is_unit (normalization_factor a)"
31   assumes normalization_factor_mult: "normalization_factor (a * b) =
32     normalization_factor a * normalization_factor b"
33   assumes normalization_factor_unit: "is_unit a \<Longrightarrow> normalization_factor a = a"
34   assumes normalization_factor_0 [simp]: "normalization_factor 0 = 0"
35 begin
37 lemma normalization_factor_dvd [simp]:
38   "a \<noteq> 0 \<Longrightarrow> normalization_factor a dvd b"
39   by (rule unit_imp_dvd, simp)
41 lemma normalization_factor_1 [simp]:
42   "normalization_factor 1 = 1"
43   by (simp add: normalization_factor_unit)
45 lemma normalization_factor_0_iff [simp]:
46   "normalization_factor a = 0 \<longleftrightarrow> a = 0"
47 proof
48   assume "normalization_factor a = 0"
49   hence "\<not> is_unit (normalization_factor a)"
50     by simp
51   then show "a = 0" by auto
52 qed simp
54 lemma normalization_factor_pow:
55   "normalization_factor (a ^ n) = normalization_factor a ^ n"
56   by (induct n) (simp_all add: normalization_factor_mult power_Suc2)
58 lemma normalization_correct [simp]:
59   "normalization_factor (a div normalization_factor a) = (if a = 0 then 0 else 1)"
60 proof (cases "a = 0", simp)
61   assume "a \<noteq> 0"
62   let ?nf = "normalization_factor"
63   from normalization_factor_is_unit[OF \<open>a \<noteq> 0\<close>] have "?nf a \<noteq> 0"
64     by auto
65   have "?nf (a div ?nf a) * ?nf (?nf a) = ?nf (a div ?nf a * ?nf a)"
66     by (simp add: normalization_factor_mult)
67   also have "a div ?nf a * ?nf a = a" using \<open>a \<noteq> 0\<close>
68     by simp
69   also have "?nf (?nf a) = ?nf a" using \<open>a \<noteq> 0\<close>
70     normalization_factor_is_unit normalization_factor_unit by simp
71   finally have "normalization_factor (a div normalization_factor a) = 1"
72     using \<open>?nf a \<noteq> 0\<close> by (metis div_mult_self2_is_id div_self)
73   with \<open>a \<noteq> 0\<close> show ?thesis by simp
74 qed
76 lemma normalization_0_iff [simp]:
77   "a div normalization_factor a = 0 \<longleftrightarrow> a = 0"
78   by (cases "a = 0", simp, subst unit_eq_div1, blast, simp)
80 lemma mult_div_normalization [simp]:
81   "b * (1 div normalization_factor a) = b div normalization_factor a"
82   by (cases "a = 0") simp_all
84 lemma associated_iff_normed_eq:
85   "associated a b \<longleftrightarrow> a div normalization_factor a = b div normalization_factor b" (is "?P \<longleftrightarrow> ?Q")
86 proof (cases "a = 0 \<or> b = 0")
87   case True then show ?thesis by (auto dest: sym)
88 next
89   case False then have "a \<noteq> 0" and "b \<noteq> 0" by auto
90   show ?thesis
91   proof
92     assume ?Q
93     from \<open>a \<noteq> 0\<close> \<open>b \<noteq> 0\<close>
94     have "is_unit (normalization_factor a div normalization_factor b)"
95       by auto
96     moreover from \<open>?Q\<close> \<open>a \<noteq> 0\<close> \<open>b \<noteq> 0\<close>
97     have "a = (normalization_factor a div normalization_factor b) * b"
98       by (simp add: ac_simps div_mult_swap unit_eq_div1)
99     ultimately show "associated a b" by (rule is_unit_associatedI)
100   next
101     assume ?P
102     then obtain c where "is_unit c" and "a = c * b"
103       by (blast elim: associated_is_unitE)
104     then show ?Q
105       by (auto simp add: normalization_factor_mult normalization_factor_unit)
106   qed
107 qed
109 lemma normed_associated_imp_eq:
110   "associated a b \<Longrightarrow> normalization_factor a \<in> {0, 1} \<Longrightarrow> normalization_factor b \<in> {0, 1} \<Longrightarrow> a = b"
111   by (simp add: associated_iff_normed_eq, elim disjE, simp_all)
113 lemma normed_dvd [iff]:
114   "a div normalization_factor a dvd a"
115 proof (cases "a = 0")
116   case True then show ?thesis by simp
117 next
118   case False
119   then have "a = a div normalization_factor a * normalization_factor a"
120     by (auto intro: unit_div_mult_self)
121   then show ?thesis ..
122 qed
124 lemma dvd_normed [iff]:
125   "a dvd a div normalization_factor a"
126 proof (cases "a = 0")
127   case True then show ?thesis by simp
128 next
129   case False
130   then have "a div normalization_factor a = a * (1 div normalization_factor a)"
131     by (auto intro: unit_mult_div_div)
132   then show ?thesis ..
133 qed
135 lemma associated_normed:
136   "associated (a div normalization_factor a) a"
137   by (rule associatedI) simp_all
139 lemma normalization_factor_dvd' [simp]:
140   "normalization_factor a dvd a"
141   by (cases "a = 0", simp_all)
143 lemmas normalization_factor_dvd_iff [simp] =
144   unit_dvd_iff [OF normalization_factor_is_unit]
146 lemma euclidean_division:
147   fixes a :: 'a and b :: 'a
148   assumes "b \<noteq> 0"
149   obtains s and t where "a = s * b + t"
150     and "euclidean_size t < euclidean_size b"
151 proof -
152   from div_mod_equality [of a b 0]
153      have "a = a div b * b + a mod b" by simp
154   with that and assms show ?thesis by (auto simp add: mod_size_less)
155 qed
157 lemma dvd_euclidean_size_eq_imp_dvd:
158   assumes "a \<noteq> 0" and b_dvd_a: "b dvd a" and size_eq: "euclidean_size a = euclidean_size b"
159   shows "a dvd b"
160 proof (rule ccontr)
161   assume "\<not> a dvd b"
162   then have "b mod a \<noteq> 0" by (simp add: mod_eq_0_iff_dvd)
163   from b_dvd_a have b_dvd_mod: "b dvd b mod a" by (simp add: dvd_mod_iff)
164   from b_dvd_mod obtain c where "b mod a = b * c" unfolding dvd_def by blast
165     with \<open>b mod a \<noteq> 0\<close> have "c \<noteq> 0" by auto
166   with \<open>b mod a = b * c\<close> have "euclidean_size (b mod a) \<ge> euclidean_size b"
167       using size_mult_mono by force
168   moreover from \<open>\<not> a dvd b\<close> and \<open>a \<noteq> 0\<close>
169   have "euclidean_size (b mod a) < euclidean_size a"
170       using mod_size_less by blast
171   ultimately show False using size_eq by simp
172 qed
174 function gcd_eucl :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"
175 where
176   "gcd_eucl a b = (if b = 0 then a div normalization_factor a
177     else gcd_eucl b (a mod b))"
178   by pat_completeness simp
179 termination
180   by (relation "measure (euclidean_size \<circ> snd)") (simp_all add: mod_size_less)
182 declare gcd_eucl.simps [simp del]
184 lemma gcd_eucl_induct [case_names zero mod]:
185   assumes H1: "\<And>b. P b 0"
186   and H2: "\<And>a b. b \<noteq> 0 \<Longrightarrow> P b (a mod b) \<Longrightarrow> P a b"
187   shows "P a b"
188 proof (induct a b rule: gcd_eucl.induct)
189   case ("1" a b)
190   show ?case
191   proof (cases "b = 0")
192     case True then show "P a b" by simp (rule H1)
193   next
194     case False
195     then have "P b (a mod b)"
196       by (rule "1.hyps")
197     with \<open>b \<noteq> 0\<close> show "P a b"
198       by (blast intro: H2)
199   qed
200 qed
202 definition lcm_eucl :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"
203 where
204   "lcm_eucl a b = a * b div (gcd_eucl a b * normalization_factor (a * b))"
206 definition Lcm_eucl :: "'a set \<Rightarrow> 'a" -- \<open>
207   Somewhat complicated definition of Lcm that has the advantage of working
208   for infinite sets as well\<close>
209 where
210   "Lcm_eucl A = (if \<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) then
211      let l = SOME l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l =
212        (LEAST n. \<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n)
213        in l div normalization_factor l
214       else 0)"
216 definition Gcd_eucl :: "'a set \<Rightarrow> 'a"
217 where
218   "Gcd_eucl A = Lcm_eucl {d. \<forall>a\<in>A. d dvd a}"
220 lemma gcd_eucl_0:
221   "gcd_eucl a 0 = a div normalization_factor a"
222   by (simp add: gcd_eucl.simps [of a 0])
224 lemma gcd_eucl_0_left:
225   "gcd_eucl 0 a = a div normalization_factor a"
226   by (simp_all add: gcd_eucl_0 gcd_eucl.simps [of 0 a])
228 lemma gcd_eucl_non_0:
229   "b \<noteq> 0 \<Longrightarrow> gcd_eucl a b = gcd_eucl b (a mod b)"
230   by (simp add: gcd_eucl.simps [of a b] gcd_eucl.simps [of b 0])
232 end
234 class euclidean_ring = euclidean_semiring + idom
235 begin
237 function euclid_ext :: "'a \<Rightarrow> 'a \<Rightarrow> 'a \<times> 'a \<times> 'a" where
238   "euclid_ext a b =
239      (if b = 0 then
240         let c = 1 div normalization_factor a in (c, 0, a * c)
241       else
242         case euclid_ext b (a mod b) of
243             (s, t, c) \<Rightarrow> (t, s - t * (a div b), c))"
244   by pat_completeness simp
245 termination
246   by (relation "measure (euclidean_size \<circ> snd)") (simp_all add: mod_size_less)
248 declare euclid_ext.simps [simp del]
250 lemma euclid_ext_0:
251   "euclid_ext a 0 = (1 div normalization_factor a, 0, a div normalization_factor a)"
252   by (simp add: euclid_ext.simps [of a 0])
254 lemma euclid_ext_left_0:
255   "euclid_ext 0 a = (0, 1 div normalization_factor a, a div normalization_factor a)"
256   by (simp add: euclid_ext_0 euclid_ext.simps [of 0 a])
258 lemma euclid_ext_non_0:
259   "b \<noteq> 0 \<Longrightarrow> euclid_ext a b = (case euclid_ext b (a mod b) of
260     (s, t, c) \<Rightarrow> (t, s - t * (a div b), c))"
261   by (simp add: euclid_ext.simps [of a b] euclid_ext.simps [of b 0])
263 lemma euclid_ext_code [code]:
264   "euclid_ext a b = (if b = 0 then (1 div normalization_factor a, 0, a div normalization_factor a)
265     else let (s, t, c) = euclid_ext b (a mod b) in  (t, s - t * (a div b), c))"
266   by (simp add: euclid_ext.simps [of a b] euclid_ext.simps [of b 0])
268 lemma euclid_ext_correct:
269   "case euclid_ext a b of (s, t, c) \<Rightarrow> s * a + t * b = c"
270 proof (induct a b rule: gcd_eucl_induct)
271   case (zero a) then show ?case
272     by (simp add: euclid_ext_0 ac_simps)
273 next
274   case (mod a b)
275   obtain s t c where stc: "euclid_ext b (a mod b) = (s,t,c)"
276     by (cases "euclid_ext b (a mod b)") blast
277   with mod have "c = s * b + t * (a mod b)" by simp
278   also have "... = t * ((a div b) * b + a mod b) + (s - t * (a div b)) * b"
279     by (simp add: algebra_simps)
280   also have "(a div b) * b + a mod b = a" using mod_div_equality .
281   finally show ?case
282     by (subst euclid_ext.simps) (simp add: stc mod ac_simps)
283 qed
285 definition euclid_ext' :: "'a \<Rightarrow> 'a \<Rightarrow> 'a \<times> 'a"
286 where
287   "euclid_ext' a b = (case euclid_ext a b of (s, t, _) \<Rightarrow> (s, t))"
289 lemma euclid_ext'_0: "euclid_ext' a 0 = (1 div normalization_factor a, 0)"
290   by (simp add: euclid_ext'_def euclid_ext_0)
292 lemma euclid_ext'_left_0: "euclid_ext' 0 a = (0, 1 div normalization_factor a)"
293   by (simp add: euclid_ext'_def euclid_ext_left_0)
295 lemma euclid_ext'_non_0: "b \<noteq> 0 \<Longrightarrow> euclid_ext' a b = (snd (euclid_ext' b (a mod b)),
296   fst (euclid_ext' b (a mod b)) - snd (euclid_ext' b (a mod b)) * (a div b))"
297   by (simp add: euclid_ext'_def euclid_ext_non_0 split_def)
299 end
301 class euclidean_semiring_gcd = euclidean_semiring + gcd + Gcd +
302   assumes gcd_gcd_eucl: "gcd = gcd_eucl" and lcm_lcm_eucl: "lcm = lcm_eucl"
303   assumes Gcd_Gcd_eucl: "Gcd = Gcd_eucl" and Lcm_Lcm_eucl: "Lcm = Lcm_eucl"
304 begin
306 lemma gcd_0_left:
307   "gcd 0 a = a div normalization_factor a"
308   unfolding gcd_gcd_eucl by (fact gcd_eucl_0_left)
310 lemma gcd_0:
311   "gcd a 0 = a div normalization_factor a"
312   unfolding gcd_gcd_eucl by (fact gcd_eucl_0)
314 lemma gcd_non_0:
315   "b \<noteq> 0 \<Longrightarrow> gcd a b = gcd b (a mod b)"
316   unfolding gcd_gcd_eucl by (fact gcd_eucl_non_0)
318 lemma gcd_dvd1 [iff]: "gcd a b dvd a"
319   and gcd_dvd2 [iff]: "gcd a b dvd b"
320   by (induct a b rule: gcd_eucl_induct)
321     (simp_all add: gcd_0 gcd_non_0 dvd_mod_iff)
323 lemma dvd_gcd_D1: "k dvd gcd m n \<Longrightarrow> k dvd m"
324   by (rule dvd_trans, assumption, rule gcd_dvd1)
326 lemma dvd_gcd_D2: "k dvd gcd m n \<Longrightarrow> k dvd n"
327   by (rule dvd_trans, assumption, rule gcd_dvd2)
329 lemma gcd_greatest:
330   fixes k a b :: 'a
331   shows "k dvd a \<Longrightarrow> k dvd b \<Longrightarrow> k dvd gcd a b"
332 proof (induct a b rule: gcd_eucl_induct)
333   case (zero a) from zero(1) show ?case by (rule dvd_trans) (simp add: gcd_0)
334 next
335   case (mod a b)
336   then show ?case
337     by (simp add: gcd_non_0 dvd_mod_iff)
338 qed
340 lemma dvd_gcd_iff:
341   "k dvd gcd a b \<longleftrightarrow> k dvd a \<and> k dvd b"
342   by (blast intro!: gcd_greatest intro: dvd_trans)
344 lemmas gcd_greatest_iff = dvd_gcd_iff
346 lemma gcd_zero [simp]:
347   "gcd a b = 0 \<longleftrightarrow> a = 0 \<and> b = 0"
348   by (metis dvd_0_left dvd_refl gcd_dvd1 gcd_dvd2 gcd_greatest)+
350 lemma normalization_factor_gcd [simp]:
351   "normalization_factor (gcd a b) = (if a = 0 \<and> b = 0 then 0 else 1)" (is "?f a b = ?g a b")
352   by (induct a b rule: gcd_eucl_induct)
353     (auto simp add: gcd_0 gcd_non_0)
355 lemma gcdI:
356   "k dvd a \<Longrightarrow> k dvd b \<Longrightarrow> (\<And>l. l dvd a \<Longrightarrow> l dvd b \<Longrightarrow> l dvd k)
357     \<Longrightarrow> normalization_factor k = (if k = 0 then 0 else 1) \<Longrightarrow> k = gcd a b"
358   by (intro normed_associated_imp_eq) (auto simp: associated_def intro: gcd_greatest)
360 sublocale gcd!: abel_semigroup gcd
361 proof
362   fix a b c
363   show "gcd (gcd a b) c = gcd a (gcd b c)"
364   proof (rule gcdI)
365     have "gcd (gcd a b) c dvd gcd a b" "gcd a b dvd a" by simp_all
366     then show "gcd (gcd a b) c dvd a" by (rule dvd_trans)
367     have "gcd (gcd a b) c dvd gcd a b" "gcd a b dvd b" by simp_all
368     hence "gcd (gcd a b) c dvd b" by (rule dvd_trans)
369     moreover have "gcd (gcd a b) c dvd c" by simp
370     ultimately show "gcd (gcd a b) c dvd gcd b c"
371       by (rule gcd_greatest)
372     show "normalization_factor (gcd (gcd a b) c) =  (if gcd (gcd a b) c = 0 then 0 else 1)"
373       by auto
374     fix l assume "l dvd a" and "l dvd gcd b c"
375     with dvd_trans[OF _ gcd_dvd1] and dvd_trans[OF _ gcd_dvd2]
376       have "l dvd b" and "l dvd c" by blast+
377     with \<open>l dvd a\<close> show "l dvd gcd (gcd a b) c"
378       by (intro gcd_greatest)
379   qed
380 next
381   fix a b
382   show "gcd a b = gcd b a"
383     by (rule gcdI) (simp_all add: gcd_greatest)
384 qed
386 lemma gcd_unique: "d dvd a \<and> d dvd b \<and>
387     normalization_factor d = (if d = 0 then 0 else 1) \<and>
388     (\<forall>e. e dvd a \<and> e dvd b \<longrightarrow> e dvd d) \<longleftrightarrow> d = gcd a b"
389   by (rule, auto intro: gcdI simp: gcd_greatest)
391 lemma gcd_dvd_prod: "gcd a b dvd k * b"
392   using mult_dvd_mono [of 1] by auto
394 lemma gcd_1_left [simp]: "gcd 1 a = 1"
395   by (rule sym, rule gcdI, simp_all)
397 lemma gcd_1 [simp]: "gcd a 1 = 1"
398   by (rule sym, rule gcdI, simp_all)
400 lemma gcd_proj2_if_dvd:
401   "b dvd a \<Longrightarrow> gcd a b = b div normalization_factor b"
402   by (cases "b = 0", simp_all add: dvd_eq_mod_eq_0 gcd_non_0 gcd_0)
404 lemma gcd_proj1_if_dvd:
405   "a dvd b \<Longrightarrow> gcd a b = a div normalization_factor a"
406   by (subst gcd.commute, simp add: gcd_proj2_if_dvd)
408 lemma gcd_proj1_iff: "gcd m n = m div normalization_factor m \<longleftrightarrow> m dvd n"
409 proof
410   assume A: "gcd m n = m div normalization_factor m"
411   show "m dvd n"
412   proof (cases "m = 0")
413     assume [simp]: "m \<noteq> 0"
414     from A have B: "m = gcd m n * normalization_factor m"
415       by (simp add: unit_eq_div2)
416     show ?thesis by (subst B, simp add: mult_unit_dvd_iff)
417   qed (insert A, simp)
418 next
419   assume "m dvd n"
420   then show "gcd m n = m div normalization_factor m" by (rule gcd_proj1_if_dvd)
421 qed
423 lemma gcd_proj2_iff: "gcd m n = n div normalization_factor n \<longleftrightarrow> n dvd m"
424   by (subst gcd.commute, simp add: gcd_proj1_iff)
426 lemma gcd_mod1 [simp]:
427   "gcd (a mod b) b = gcd a b"
428   by (rule gcdI, metis dvd_mod_iff gcd_dvd1 gcd_dvd2, simp_all add: gcd_greatest dvd_mod_iff)
430 lemma gcd_mod2 [simp]:
431   "gcd a (b mod a) = gcd a b"
432   by (rule gcdI, simp, metis dvd_mod_iff gcd_dvd1 gcd_dvd2, simp_all add: gcd_greatest dvd_mod_iff)
434 lemma gcd_mult_distrib':
435   "c div normalization_factor c * gcd a b = gcd (c * a) (c * b)"
436 proof (cases "c = 0")
437   case True then show ?thesis by (simp_all add: gcd_0)
438 next
439   case False then have [simp]: "is_unit (normalization_factor c)" by simp
440   show ?thesis
441   proof (induct a b rule: gcd_eucl_induct)
442     case (zero a) show ?case
443     proof (cases "a = 0")
444       case True then show ?thesis by (simp add: gcd_0)
445     next
446       case False then have "is_unit (normalization_factor a)" by simp
447       then show ?thesis
448         by (simp add: gcd_0 unit_div_commute unit_div_mult_swap normalization_factor_mult is_unit_div_mult2_eq)
449     qed
450     case (mod a b)
451     then show ?case by (simp add: mult_mod_right gcd.commute)
452   qed
453 qed
455 lemma gcd_mult_distrib:
456   "k * gcd a b = gcd (k*a) (k*b) * normalization_factor k"
457 proof-
458   let ?nf = "normalization_factor"
459   from gcd_mult_distrib'
460     have "gcd (k*a) (k*b) = k div ?nf k * gcd a b" ..
461   also have "... = k * gcd a b div ?nf k"
462     by (metis dvd_div_mult dvd_eq_mod_eq_0 mod_0 normalization_factor_dvd)
463   finally show ?thesis
464     by simp
465 qed
467 lemma euclidean_size_gcd_le1 [simp]:
468   assumes "a \<noteq> 0"
469   shows "euclidean_size (gcd a b) \<le> euclidean_size a"
470 proof -
471    have "gcd a b dvd a" by (rule gcd_dvd1)
472    then obtain c where A: "a = gcd a b * c" unfolding dvd_def by blast
473    with \<open>a \<noteq> 0\<close> show ?thesis by (subst (2) A, intro size_mult_mono) auto
474 qed
476 lemma euclidean_size_gcd_le2 [simp]:
477   "b \<noteq> 0 \<Longrightarrow> euclidean_size (gcd a b) \<le> euclidean_size b"
478   by (subst gcd.commute, rule euclidean_size_gcd_le1)
480 lemma euclidean_size_gcd_less1:
481   assumes "a \<noteq> 0" and "\<not>a dvd b"
482   shows "euclidean_size (gcd a b) < euclidean_size a"
483 proof (rule ccontr)
484   assume "\<not>euclidean_size (gcd a b) < euclidean_size a"
485   with \<open>a \<noteq> 0\<close> have "euclidean_size (gcd a b) = euclidean_size a"
486     by (intro le_antisym, simp_all)
487   with assms have "a dvd gcd a b" by (auto intro: dvd_euclidean_size_eq_imp_dvd)
488   hence "a dvd b" using dvd_gcd_D2 by blast
489   with \<open>\<not>a dvd b\<close> show False by contradiction
490 qed
492 lemma euclidean_size_gcd_less2:
493   assumes "b \<noteq> 0" and "\<not>b dvd a"
494   shows "euclidean_size (gcd a b) < euclidean_size b"
495   using assms by (subst gcd.commute, rule euclidean_size_gcd_less1)
497 lemma gcd_mult_unit1: "is_unit a \<Longrightarrow> gcd (b * a) c = gcd b c"
498   apply (rule gcdI)
499   apply (rule dvd_trans, rule gcd_dvd1, simp add: unit_simps)
500   apply (rule gcd_dvd2)
501   apply (rule gcd_greatest, simp add: unit_simps, assumption)
502   apply (subst normalization_factor_gcd, simp add: gcd_0)
503   done
505 lemma gcd_mult_unit2: "is_unit a \<Longrightarrow> gcd b (c * a) = gcd b c"
506   by (subst gcd.commute, subst gcd_mult_unit1, assumption, rule gcd.commute)
508 lemma gcd_div_unit1: "is_unit a \<Longrightarrow> gcd (b div a) c = gcd b c"
509   by (erule is_unitE [of _ b]) (simp add: gcd_mult_unit1)
511 lemma gcd_div_unit2: "is_unit a \<Longrightarrow> gcd b (c div a) = gcd b c"
512   by (erule is_unitE [of _ c]) (simp add: gcd_mult_unit2)
514 lemma gcd_idem: "gcd a a = a div normalization_factor a"
515   by (cases "a = 0") (simp add: gcd_0_left, rule sym, rule gcdI, simp_all)
517 lemma gcd_right_idem: "gcd (gcd a b) b = gcd a b"
518   apply (rule gcdI)
519   apply (simp add: ac_simps)
520   apply (rule gcd_dvd2)
521   apply (rule gcd_greatest, erule (1) gcd_greatest, assumption)
522   apply simp
523   done
525 lemma gcd_left_idem: "gcd a (gcd a b) = gcd a b"
526   apply (rule gcdI)
527   apply simp
528   apply (rule dvd_trans, rule gcd_dvd2, rule gcd_dvd2)
529   apply (rule gcd_greatest, assumption, erule gcd_greatest, assumption)
530   apply simp
531   done
533 lemma comp_fun_idem_gcd: "comp_fun_idem gcd"
534 proof
535   fix a b show "gcd a \<circ> gcd b = gcd b \<circ> gcd a"
536     by (simp add: fun_eq_iff ac_simps)
537 next
538   fix a show "gcd a \<circ> gcd a = gcd a"
539     by (simp add: fun_eq_iff gcd_left_idem)
540 qed
542 lemma coprime_dvd_mult:
543   assumes "gcd c b = 1" and "c dvd a * b"
544   shows "c dvd a"
545 proof -
546   let ?nf = "normalization_factor"
547   from assms gcd_mult_distrib [of a c b]
548     have A: "a = gcd (a * c) (a * b) * ?nf a" by simp
549   from \<open>c dvd a * b\<close> show ?thesis by (subst A, simp_all add: gcd_greatest)
550 qed
552 lemma coprime_dvd_mult_iff:
553   "gcd c b = 1 \<Longrightarrow> (c dvd a * b) = (c dvd a)"
554   by (rule, rule coprime_dvd_mult, simp_all)
556 lemma gcd_dvd_antisym:
557   "gcd a b dvd gcd c d \<Longrightarrow> gcd c d dvd gcd a b \<Longrightarrow> gcd a b = gcd c d"
558 proof (rule gcdI)
559   assume A: "gcd a b dvd gcd c d" and B: "gcd c d dvd gcd a b"
560   have "gcd c d dvd c" by simp
561   with A show "gcd a b dvd c" by (rule dvd_trans)
562   have "gcd c d dvd d" by simp
563   with A show "gcd a b dvd d" by (rule dvd_trans)
564   show "normalization_factor (gcd a b) = (if gcd a b = 0 then 0 else 1)"
565     by simp
566   fix l assume "l dvd c" and "l dvd d"
567   hence "l dvd gcd c d" by (rule gcd_greatest)
568   from this and B show "l dvd gcd a b" by (rule dvd_trans)
569 qed
571 lemma gcd_mult_cancel:
572   assumes "gcd k n = 1"
573   shows "gcd (k * m) n = gcd m n"
574 proof (rule gcd_dvd_antisym)
575   have "gcd (gcd (k * m) n) k = gcd (gcd k n) (k * m)" by (simp add: ac_simps)
576   also note \<open>gcd k n = 1\<close>
577   finally have "gcd (gcd (k * m) n) k = 1" by simp
578   hence "gcd (k * m) n dvd m" by (rule coprime_dvd_mult, simp add: ac_simps)
579   moreover have "gcd (k * m) n dvd n" by simp
580   ultimately show "gcd (k * m) n dvd gcd m n" by (rule gcd_greatest)
581   have "gcd m n dvd (k * m)" and "gcd m n dvd n" by simp_all
582   then show "gcd m n dvd gcd (k * m) n" by (rule gcd_greatest)
583 qed
585 lemma coprime_crossproduct:
586   assumes [simp]: "gcd a d = 1" "gcd b c = 1"
587   shows "associated (a * c) (b * d) \<longleftrightarrow> associated a b \<and> associated c d" (is "?lhs \<longleftrightarrow> ?rhs")
588 proof
589   assume ?rhs then show ?lhs unfolding associated_def by (fast intro: mult_dvd_mono)
590 next
591   assume ?lhs
592   from \<open>?lhs\<close> have "a dvd b * d" unfolding associated_def by (metis dvd_mult_left)
593   hence "a dvd b" by (simp add: coprime_dvd_mult_iff)
594   moreover from \<open>?lhs\<close> have "b dvd a * c" unfolding associated_def by (metis dvd_mult_left)
595   hence "b dvd a" by (simp add: coprime_dvd_mult_iff)
596   moreover from \<open>?lhs\<close> have "c dvd d * b"
597     unfolding associated_def by (auto dest: dvd_mult_right simp add: ac_simps)
598   hence "c dvd d" by (simp add: coprime_dvd_mult_iff gcd.commute)
599   moreover from \<open>?lhs\<close> have "d dvd c * a"
600     unfolding associated_def by (auto dest: dvd_mult_right simp add: ac_simps)
601   hence "d dvd c" by (simp add: coprime_dvd_mult_iff gcd.commute)
602   ultimately show ?rhs unfolding associated_def by simp
603 qed
605 lemma gcd_add1 [simp]:
606   "gcd (m + n) n = gcd m n"
607   by (cases "n = 0", simp_all add: gcd_non_0)
609 lemma gcd_add2 [simp]:
610   "gcd m (m + n) = gcd m n"
611   using gcd_add1 [of n m] by (simp add: ac_simps)
614   "gcd m (k * m + n) = gcd m n"
615 proof -
616   have "gcd m ((k * m + n) mod m) = gcd m (k * m + n)"
617     by (fact gcd_mod2)
618   then show ?thesis by simp
619 qed
621 lemma coprimeI: "(\<And>l. \<lbrakk>l dvd a; l dvd b\<rbrakk> \<Longrightarrow> l dvd 1) \<Longrightarrow> gcd a b = 1"
622   by (rule sym, rule gcdI, simp_all)
624 lemma coprime: "gcd a b = 1 \<longleftrightarrow> (\<forall>d. d dvd a \<and> d dvd b \<longleftrightarrow> is_unit d)"
625   by (auto intro: coprimeI gcd_greatest dvd_gcd_D1 dvd_gcd_D2)
627 lemma div_gcd_coprime:
628   assumes nz: "a \<noteq> 0 \<or> b \<noteq> 0"
629   defines [simp]: "d \<equiv> gcd a b"
630   defines [simp]: "a' \<equiv> a div d" and [simp]: "b' \<equiv> b div d"
631   shows "gcd a' b' = 1"
632 proof (rule coprimeI)
633   fix l assume "l dvd a'" "l dvd b'"
634   then obtain s t where "a' = l * s" "b' = l * t" unfolding dvd_def by blast
635   moreover have "a = a' * d" "b = b' * d" by simp_all
636   ultimately have "a = (l * d) * s" "b = (l * d) * t"
637     by (simp_all only: ac_simps)
638   hence "l*d dvd a" and "l*d dvd b" by (simp_all only: dvd_triv_left)
639   hence "l*d dvd d" by (simp add: gcd_greatest)
640   then obtain u where "d = l * d * u" ..
641   then have "d * (l * u) = d" by (simp add: ac_simps)
642   moreover from nz have "d \<noteq> 0" by simp
643   with div_mult_self1_is_id have "d * (l * u) div d = l * u" .
644   ultimately have "1 = l * u"
645     using \<open>d \<noteq> 0\<close> by simp
646   then show "l dvd 1" ..
647 qed
649 lemma coprime_mult:
650   assumes da: "gcd d a = 1" and db: "gcd d b = 1"
651   shows "gcd d (a * b) = 1"
652   apply (subst gcd.commute)
653   using da apply (subst gcd_mult_cancel)
654   apply (subst gcd.commute, assumption)
655   apply (subst gcd.commute, rule db)
656   done
658 lemma coprime_lmult:
659   assumes dab: "gcd d (a * b) = 1"
660   shows "gcd d a = 1"
661 proof (rule coprimeI)
662   fix l assume "l dvd d" and "l dvd a"
663   hence "l dvd a * b" by simp
664   with \<open>l dvd d\<close> and dab show "l dvd 1" by (auto intro: gcd_greatest)
665 qed
667 lemma coprime_rmult:
668   assumes dab: "gcd d (a * b) = 1"
669   shows "gcd d b = 1"
670 proof (rule coprimeI)
671   fix l assume "l dvd d" and "l dvd b"
672   hence "l dvd a * b" by simp
673   with \<open>l dvd d\<close> and dab show "l dvd 1" by (auto intro: gcd_greatest)
674 qed
676 lemma coprime_mul_eq: "gcd d (a * b) = 1 \<longleftrightarrow> gcd d a = 1 \<and> gcd d b = 1"
677   using coprime_rmult[of d a b] coprime_lmult[of d a b] coprime_mult[of d a b] by blast
679 lemma gcd_coprime:
680   assumes c: "gcd a b \<noteq> 0" and a: "a = a' * gcd a b" and b: "b = b' * gcd a b"
681   shows "gcd a' b' = 1"
682 proof -
683   from c have "a \<noteq> 0 \<or> b \<noteq> 0" by simp
684   with div_gcd_coprime have "gcd (a div gcd a b) (b div gcd a b) = 1" .
685   also from assms have "a div gcd a b = a'" by (metis div_mult_self2_is_id)+
686   also from assms have "b div gcd a b = b'" by (metis div_mult_self2_is_id)+
687   finally show ?thesis .
688 qed
690 lemma coprime_power:
691   assumes "0 < n"
692   shows "gcd a (b ^ n) = 1 \<longleftrightarrow> gcd a b = 1"
693 using assms proof (induct n)
694   case (Suc n) then show ?case
695     by (cases n) (simp_all add: coprime_mul_eq)
696 qed simp
698 lemma gcd_coprime_exists:
699   assumes nz: "gcd a b \<noteq> 0"
700   shows "\<exists>a' b'. a = a' * gcd a b \<and> b = b' * gcd a b \<and> gcd a' b' = 1"
701   apply (rule_tac x = "a div gcd a b" in exI)
702   apply (rule_tac x = "b div gcd a b" in exI)
703   apply (insert nz, auto intro: div_gcd_coprime)
704   done
706 lemma coprime_exp:
707   "gcd d a = 1 \<Longrightarrow> gcd d (a^n) = 1"
708   by (induct n, simp_all add: coprime_mult)
710 lemma coprime_exp2 [intro]:
711   "gcd a b = 1 \<Longrightarrow> gcd (a^n) (b^m) = 1"
712   apply (rule coprime_exp)
713   apply (subst gcd.commute)
714   apply (rule coprime_exp)
715   apply (subst gcd.commute)
716   apply assumption
717   done
719 lemma gcd_exp:
720   "gcd (a^n) (b^n) = (gcd a b) ^ n"
721 proof (cases "a = 0 \<and> b = 0")
722   assume "a = 0 \<and> b = 0"
723   then show ?thesis by (cases n, simp_all add: gcd_0_left)
724 next
725   assume A: "\<not>(a = 0 \<and> b = 0)"
726   hence "1 = gcd ((a div gcd a b)^n) ((b div gcd a b)^n)"
727     using div_gcd_coprime by (subst sym, auto simp: div_gcd_coprime)
728   hence "(gcd a b) ^ n = (gcd a b) ^ n * ..." by simp
729   also note gcd_mult_distrib
730   also have "normalization_factor ((gcd a b)^n) = 1"
731     by (simp add: normalization_factor_pow A)
732   also have "(gcd a b)^n * (a div gcd a b)^n = a^n"
733     by (subst ac_simps, subst div_power, simp, rule dvd_div_mult_self, rule dvd_power_same, simp)
734   also have "(gcd a b)^n * (b div gcd a b)^n = b^n"
735     by (subst ac_simps, subst div_power, simp, rule dvd_div_mult_self, rule dvd_power_same, simp)
736   finally show ?thesis by simp
737 qed
739 lemma coprime_common_divisor:
740   "gcd a b = 1 \<Longrightarrow> a dvd a \<Longrightarrow> a dvd b \<Longrightarrow> is_unit a"
741   apply (subgoal_tac "a dvd gcd a b")
742   apply simp
743   apply (erule (1) gcd_greatest)
744   done
746 lemma division_decomp:
747   assumes dc: "a dvd b * c"
748   shows "\<exists>b' c'. a = b' * c' \<and> b' dvd b \<and> c' dvd c"
749 proof (cases "gcd a b = 0")
750   assume "gcd a b = 0"
751   hence "a = 0 \<and> b = 0" by simp
752   hence "a = 0 * c \<and> 0 dvd b \<and> c dvd c" by simp
753   then show ?thesis by blast
754 next
755   let ?d = "gcd a b"
756   assume "?d \<noteq> 0"
757   from gcd_coprime_exists[OF this]
758     obtain a' b' where ab': "a = a' * ?d" "b = b' * ?d" "gcd a' b' = 1"
759     by blast
760   from ab'(1) have "a' dvd a" unfolding dvd_def by blast
761   with dc have "a' dvd b*c" using dvd_trans[of a' a "b*c"] by simp
762   from dc ab'(1,2) have "a'*?d dvd (b'*?d) * c" by simp
763   hence "?d * a' dvd ?d * (b' * c)" by (simp add: mult_ac)
764   with \<open>?d \<noteq> 0\<close> have "a' dvd b' * c" by simp
765   with coprime_dvd_mult[OF ab'(3)]
766     have "a' dvd c" by (subst (asm) ac_simps, blast)
767   with ab'(1) have "a = ?d * a' \<and> ?d dvd b \<and> a' dvd c" by (simp add: mult_ac)
768   then show ?thesis by blast
769 qed
771 lemma pow_divs_pow:
772   assumes ab: "a ^ n dvd b ^ n" and n: "n \<noteq> 0"
773   shows "a dvd b"
774 proof (cases "gcd a b = 0")
775   assume "gcd a b = 0"
776   then show ?thesis by simp
777 next
778   let ?d = "gcd a b"
779   assume "?d \<noteq> 0"
780   from n obtain m where m: "n = Suc m" by (cases n, simp_all)
781   from \<open>?d \<noteq> 0\<close> have zn: "?d ^ n \<noteq> 0" by (rule power_not_zero)
782   from gcd_coprime_exists[OF \<open>?d \<noteq> 0\<close>]
783     obtain a' b' where ab': "a = a' * ?d" "b = b' * ?d" "gcd a' b' = 1"
784     by blast
785   from ab have "(a' * ?d) ^ n dvd (b' * ?d) ^ n"
786     by (simp add: ab'(1,2)[symmetric])
787   hence "?d^n * a'^n dvd ?d^n * b'^n"
788     by (simp only: power_mult_distrib ac_simps)
789   with zn have "a'^n dvd b'^n" by simp
790   hence "a' dvd b'^n" using dvd_trans[of a' "a'^n" "b'^n"] by (simp add: m)
791   hence "a' dvd b'^m * b'" by (simp add: m ac_simps)
792   with coprime_dvd_mult[OF coprime_exp[OF ab'(3), of m]]
793     have "a' dvd b'" by (subst (asm) ac_simps, blast)
794   hence "a'*?d dvd b'*?d" by (rule mult_dvd_mono, simp)
795   with ab'(1,2) show ?thesis by simp
796 qed
798 lemma pow_divs_eq [simp]:
799   "n \<noteq> 0 \<Longrightarrow> a ^ n dvd b ^ n \<longleftrightarrow> a dvd b"
800   by (auto intro: pow_divs_pow dvd_power_same)
802 lemma divs_mult:
803   assumes mr: "m dvd r" and nr: "n dvd r" and mn: "gcd m n = 1"
804   shows "m * n dvd r"
805 proof -
806   from mr nr obtain m' n' where m': "r = m*m'" and n': "r = n*n'"
807     unfolding dvd_def by blast
808   from mr n' have "m dvd n'*n" by (simp add: ac_simps)
809   hence "m dvd n'" using coprime_dvd_mult_iff[OF mn] by simp
810   then obtain k where k: "n' = m*k" unfolding dvd_def by blast
811   with n' have "r = m * n * k" by (simp add: mult_ac)
812   then show ?thesis unfolding dvd_def by blast
813 qed
815 lemma coprime_plus_one [simp]: "gcd (n + 1) n = 1"
816   by (subst add_commute, simp)
818 lemma setprod_coprime [rule_format]:
819   "(\<forall>i\<in>A. gcd (f i) a = 1) \<longrightarrow> gcd (\<Prod>i\<in>A. f i) a = 1"
820   apply (cases "finite A")
821   apply (induct set: finite)
822   apply (auto simp add: gcd_mult_cancel)
823   done
825 lemma coprime_divisors:
826   assumes "d dvd a" "e dvd b" "gcd a b = 1"
827   shows "gcd d e = 1"
828 proof -
829   from assms obtain k l where "a = d * k" "b = e * l"
830     unfolding dvd_def by blast
831   with assms have "gcd (d * k) (e * l) = 1" by simp
832   hence "gcd (d * k) e = 1" by (rule coprime_lmult)
833   also have "gcd (d * k) e = gcd e (d * k)" by (simp add: ac_simps)
834   finally have "gcd e d = 1" by (rule coprime_lmult)
835   then show ?thesis by (simp add: ac_simps)
836 qed
838 lemma invertible_coprime:
839   assumes "a * b mod m = 1"
840   shows "coprime a m"
841 proof -
842   from assms have "coprime m (a * b mod m)"
843     by simp
844   then have "coprime m (a * b)"
845     by simp
846   then have "coprime m a"
847     by (rule coprime_lmult)
848   then show ?thesis
849     by (simp add: ac_simps)
850 qed
852 lemma lcm_gcd:
853   "lcm a b = a * b div (gcd a b * normalization_factor (a*b))"
854   by (simp only: lcm_lcm_eucl gcd_gcd_eucl lcm_eucl_def)
856 lemma lcm_gcd_prod:
857   "lcm a b * gcd a b = a * b div normalization_factor (a*b)"
858 proof (cases "a * b = 0")
859   let ?nf = normalization_factor
860   assume "a * b \<noteq> 0"
861   hence "gcd a b \<noteq> 0" by simp
862   from lcm_gcd have "lcm a b * gcd a b = gcd a b * (a * b div (?nf (a*b) * gcd a b))"
863     by (simp add: mult_ac)
864   also from \<open>a * b \<noteq> 0\<close> have "... = a * b div ?nf (a*b)"
865     by (simp add: div_mult_swap mult.commute)
866   finally show ?thesis .
867 qed (auto simp add: lcm_gcd)
869 lemma lcm_dvd1 [iff]:
870   "a dvd lcm a b"
871 proof (cases "a*b = 0")
872   assume "a * b \<noteq> 0"
873   hence "gcd a b \<noteq> 0" by simp
874   let ?c = "1 div normalization_factor (a * b)"
875   from \<open>a * b \<noteq> 0\<close> have [simp]: "is_unit (normalization_factor (a * b))" by simp
876   from lcm_gcd_prod[of a b] have "lcm a b * gcd a b = a * ?c * b"
877     by (simp add: div_mult_swap unit_div_commute)
878   hence "lcm a b * gcd a b div gcd a b = a * ?c * b div gcd a b" by simp
879   with \<open>gcd a b \<noteq> 0\<close> have "lcm a b = a * ?c * b div gcd a b"
880     by (subst (asm) div_mult_self2_is_id, simp_all)
881   also have "... = a * (?c * b div gcd a b)"
882     by (metis div_mult_swap gcd_dvd2 mult_assoc)
883   finally show ?thesis by (rule dvdI)
884 qed (auto simp add: lcm_gcd)
886 lemma lcm_least:
887   "\<lbrakk>a dvd k; b dvd k\<rbrakk> \<Longrightarrow> lcm a b dvd k"
888 proof (cases "k = 0")
889   let ?nf = normalization_factor
890   assume "k \<noteq> 0"
891   hence "is_unit (?nf k)" by simp
892   hence "?nf k \<noteq> 0" by (metis not_is_unit_0)
893   assume A: "a dvd k" "b dvd k"
894   hence "gcd a b \<noteq> 0" using \<open>k \<noteq> 0\<close> by auto
895   from A obtain r s where ar: "k = a * r" and bs: "k = b * s"
896     unfolding dvd_def by blast
897   with \<open>k \<noteq> 0\<close> have "r * s \<noteq> 0"
898     by auto (drule sym [of 0], simp)
899   hence "is_unit (?nf (r * s))" by simp
900   let ?c = "?nf k div ?nf (r*s)"
901   from \<open>is_unit (?nf k)\<close> and \<open>is_unit (?nf (r * s))\<close> have "is_unit ?c" by (rule unit_div)
902   hence "?c \<noteq> 0" using not_is_unit_0 by fast
903   from ar bs have "k * k * gcd s r = ?nf k * k * gcd (k * s) (k * r)"
904     by (subst mult_assoc, subst gcd_mult_distrib[of k s r], simp only: ac_simps)
905   also have "... = ?nf k * k * gcd ((r*s) * a) ((r*s) * b)"
906     by (subst (3) \<open>k = a * r\<close>, subst (3) \<open>k = b * s\<close>, simp add: algebra_simps)
907   also have "... = ?c * r*s * k * gcd a b" using \<open>r * s \<noteq> 0\<close>
908     by (subst gcd_mult_distrib'[symmetric], simp add: algebra_simps unit_simps)
909   finally have "(a*r) * (b*s) * gcd s r = ?c * k * r * s * gcd a b"
910     by (subst ar[symmetric], subst bs[symmetric], simp add: mult_ac)
911   hence "a * b * gcd s r * (r * s) = ?c * k * gcd a b * (r * s)"
912     by (simp add: algebra_simps)
913   hence "?c * k * gcd a b = a * b * gcd s r" using \<open>r * s \<noteq> 0\<close>
914     by (metis div_mult_self2_is_id)
915   also have "... = lcm a b * gcd a b * gcd s r * ?nf (a*b)"
916     by (subst lcm_gcd_prod[of a b], metis gcd_mult_distrib gcd_mult_distrib')
917   also have "... = lcm a b * gcd s r * ?nf (a*b) * gcd a b"
918     by (simp add: algebra_simps)
919   finally have "k * ?c = lcm a b * gcd s r * ?nf (a*b)" using \<open>gcd a b \<noteq> 0\<close>
920     by (metis mult.commute div_mult_self2_is_id)
921   hence "k = lcm a b * (gcd s r * ?nf (a*b)) div ?c" using \<open>?c \<noteq> 0\<close>
922     by (metis div_mult_self2_is_id mult_assoc)
923   also have "... = lcm a b * (gcd s r * ?nf (a*b) div ?c)" using \<open>is_unit ?c\<close>
924     by (simp add: unit_simps)
925   finally show ?thesis by (rule dvdI)
926 qed simp
928 lemma lcm_zero:
929   "lcm a b = 0 \<longleftrightarrow> a = 0 \<or> b = 0"
930 proof -
931   let ?nf = normalization_factor
932   {
933     assume "a \<noteq> 0" "b \<noteq> 0"
934     hence "a * b div ?nf (a * b) \<noteq> 0" by (simp add: no_zero_divisors)
935     moreover from \<open>a \<noteq> 0\<close> and \<open>b \<noteq> 0\<close> have "gcd a b \<noteq> 0" by simp
936     ultimately have "lcm a b \<noteq> 0" using lcm_gcd_prod[of a b] by (intro notI, simp)
937   } moreover {
938     assume "a = 0 \<or> b = 0"
939     hence "lcm a b = 0" by (elim disjE, simp_all add: lcm_gcd)
940   }
941   ultimately show ?thesis by blast
942 qed
944 lemmas lcm_0_iff = lcm_zero
946 lemma gcd_lcm:
947   assumes "lcm a b \<noteq> 0"
948   shows "gcd a b = a * b div (lcm a b * normalization_factor (a * b))"
949 proof-
950   from assms have "gcd a b \<noteq> 0" by (simp add: lcm_zero)
951   let ?c = "normalization_factor (a * b)"
952   from \<open>lcm a b \<noteq> 0\<close> have "?c \<noteq> 0" by (intro notI, simp add: lcm_zero no_zero_divisors)
953   hence "is_unit ?c" by simp
954   from lcm_gcd_prod [of a b] have "gcd a b = a * b div ?c div lcm a b"
955     by (subst (2) div_mult_self2_is_id[OF \<open>lcm a b \<noteq> 0\<close>, symmetric], simp add: mult_ac)
956   also from \<open>is_unit ?c\<close> have "... = a * b div (lcm a b * ?c)"
957     by (metis \<open>?c \<noteq> 0\<close> div_mult_mult1 dvd_mult_div_cancel mult_commute normalization_factor_dvd')
958   finally show ?thesis .
959 qed
961 lemma normalization_factor_lcm [simp]:
962   "normalization_factor (lcm a b) = (if a = 0 \<or> b = 0 then 0 else 1)"
963 proof (cases "a = 0 \<or> b = 0")
964   case True then show ?thesis
965     by (auto simp add: lcm_gcd)
966 next
967   case False
968   let ?nf = normalization_factor
969   from lcm_gcd_prod[of a b]
970     have "?nf (lcm a b) * ?nf (gcd a b) = ?nf (a*b) div ?nf (a*b)"
971     by (metis div_by_0 div_self normalization_correct normalization_factor_0 normalization_factor_mult)
972   also have "... = (if a*b = 0 then 0 else 1)"
973     by simp
974   finally show ?thesis using False by simp
975 qed
977 lemma lcm_dvd2 [iff]: "b dvd lcm a b"
978   using lcm_dvd1 [of b a] by (simp add: lcm_gcd ac_simps)
980 lemma lcmI:
981   "\<lbrakk>a dvd k; b dvd k; \<And>l. a dvd l \<Longrightarrow> b dvd l \<Longrightarrow> k dvd l;
982     normalization_factor k = (if k = 0 then 0 else 1)\<rbrakk> \<Longrightarrow> k = lcm a b"
983   by (intro normed_associated_imp_eq) (auto simp: associated_def intro: lcm_least)
985 sublocale lcm!: abel_semigroup lcm
986 proof
987   fix a b c
988   show "lcm (lcm a b) c = lcm a (lcm b c)"
989   proof (rule lcmI)
990     have "a dvd lcm a b" and "lcm a b dvd lcm (lcm a b) c" by simp_all
991     then show "a dvd lcm (lcm a b) c" by (rule dvd_trans)
993     have "b dvd lcm a b" and "lcm a b dvd lcm (lcm a b) c" by simp_all
994     hence "b dvd lcm (lcm a b) c" by (rule dvd_trans)
995     moreover have "c dvd lcm (lcm a b) c" by simp
996     ultimately show "lcm b c dvd lcm (lcm a b) c" by (rule lcm_least)
998     fix l assume "a dvd l" and "lcm b c dvd l"
999     have "b dvd lcm b c" by simp
1000     from this and \<open>lcm b c dvd l\<close> have "b dvd l" by (rule dvd_trans)
1001     have "c dvd lcm b c" by simp
1002     from this and \<open>lcm b c dvd l\<close> have "c dvd l" by (rule dvd_trans)
1003     from \<open>a dvd l\<close> and \<open>b dvd l\<close> have "lcm a b dvd l" by (rule lcm_least)
1004     from this and \<open>c dvd l\<close> show "lcm (lcm a b) c dvd l" by (rule lcm_least)
1005   qed (simp add: lcm_zero)
1006 next
1007   fix a b
1008   show "lcm a b = lcm b a"
1009     by (simp add: lcm_gcd ac_simps)
1010 qed
1012 lemma dvd_lcm_D1:
1013   "lcm m n dvd k \<Longrightarrow> m dvd k"
1014   by (rule dvd_trans, rule lcm_dvd1, assumption)
1016 lemma dvd_lcm_D2:
1017   "lcm m n dvd k \<Longrightarrow> n dvd k"
1018   by (rule dvd_trans, rule lcm_dvd2, assumption)
1020 lemma gcd_dvd_lcm [simp]:
1021   "gcd a b dvd lcm a b"
1022   by (metis dvd_trans gcd_dvd2 lcm_dvd2)
1024 lemma lcm_1_iff:
1025   "lcm a b = 1 \<longleftrightarrow> is_unit a \<and> is_unit b"
1026 proof
1027   assume "lcm a b = 1"
1028   then show "is_unit a \<and> is_unit b" by auto
1029 next
1030   assume "is_unit a \<and> is_unit b"
1031   hence "a dvd 1" and "b dvd 1" by simp_all
1032   hence "is_unit (lcm a b)" by (rule lcm_least)
1033   hence "lcm a b = normalization_factor (lcm a b)"
1034     by (subst normalization_factor_unit, simp_all)
1035   also have "\<dots> = 1" using \<open>is_unit a \<and> is_unit b\<close>
1036     by auto
1037   finally show "lcm a b = 1" .
1038 qed
1040 lemma lcm_0_left [simp]:
1041   "lcm 0 a = 0"
1042   by (rule sym, rule lcmI, simp_all)
1044 lemma lcm_0 [simp]:
1045   "lcm a 0 = 0"
1046   by (rule sym, rule lcmI, simp_all)
1048 lemma lcm_unique:
1049   "a dvd d \<and> b dvd d \<and>
1050   normalization_factor d = (if d = 0 then 0 else 1) \<and>
1051   (\<forall>e. a dvd e \<and> b dvd e \<longrightarrow> d dvd e) \<longleftrightarrow> d = lcm a b"
1052   by (rule, auto intro: lcmI simp: lcm_least lcm_zero)
1054 lemma dvd_lcm_I1 [simp]:
1055   "k dvd m \<Longrightarrow> k dvd lcm m n"
1056   by (metis lcm_dvd1 dvd_trans)
1058 lemma dvd_lcm_I2 [simp]:
1059   "k dvd n \<Longrightarrow> k dvd lcm m n"
1060   by (metis lcm_dvd2 dvd_trans)
1062 lemma lcm_1_left [simp]:
1063   "lcm 1 a = a div normalization_factor a"
1064   by (cases "a = 0") (simp, rule sym, rule lcmI, simp_all)
1066 lemma lcm_1_right [simp]:
1067   "lcm a 1 = a div normalization_factor a"
1068   using lcm_1_left [of a] by (simp add: ac_simps)
1070 lemma lcm_coprime:
1071   "gcd a b = 1 \<Longrightarrow> lcm a b = a * b div normalization_factor (a*b)"
1072   by (subst lcm_gcd) simp
1074 lemma lcm_proj1_if_dvd:
1075   "b dvd a \<Longrightarrow> lcm a b = a div normalization_factor a"
1076   by (cases "a = 0") (simp, rule sym, rule lcmI, simp_all)
1078 lemma lcm_proj2_if_dvd:
1079   "a dvd b \<Longrightarrow> lcm a b = b div normalization_factor b"
1080   using lcm_proj1_if_dvd [of a b] by (simp add: ac_simps)
1082 lemma lcm_proj1_iff:
1083   "lcm m n = m div normalization_factor m \<longleftrightarrow> n dvd m"
1084 proof
1085   assume A: "lcm m n = m div normalization_factor m"
1086   show "n dvd m"
1087   proof (cases "m = 0")
1088     assume [simp]: "m \<noteq> 0"
1089     from A have B: "m = lcm m n * normalization_factor m"
1090       by (simp add: unit_eq_div2)
1091     show ?thesis by (subst B, simp)
1092   qed simp
1093 next
1094   assume "n dvd m"
1095   then show "lcm m n = m div normalization_factor m" by (rule lcm_proj1_if_dvd)
1096 qed
1098 lemma lcm_proj2_iff:
1099   "lcm m n = n div normalization_factor n \<longleftrightarrow> m dvd n"
1100   using lcm_proj1_iff [of n m] by (simp add: ac_simps)
1102 lemma euclidean_size_lcm_le1:
1103   assumes "a \<noteq> 0" and "b \<noteq> 0"
1104   shows "euclidean_size a \<le> euclidean_size (lcm a b)"
1105 proof -
1106   have "a dvd lcm a b" by (rule lcm_dvd1)
1107   then obtain c where A: "lcm a b = a * c" unfolding dvd_def by blast
1108   with \<open>a \<noteq> 0\<close> and \<open>b \<noteq> 0\<close> have "c \<noteq> 0" by (auto simp: lcm_zero)
1109   then show ?thesis by (subst A, intro size_mult_mono)
1110 qed
1112 lemma euclidean_size_lcm_le2:
1113   "a \<noteq> 0 \<Longrightarrow> b \<noteq> 0 \<Longrightarrow> euclidean_size b \<le> euclidean_size (lcm a b)"
1114   using euclidean_size_lcm_le1 [of b a] by (simp add: ac_simps)
1116 lemma euclidean_size_lcm_less1:
1117   assumes "b \<noteq> 0" and "\<not>b dvd a"
1118   shows "euclidean_size a < euclidean_size (lcm a b)"
1119 proof (rule ccontr)
1120   from assms have "a \<noteq> 0" by auto
1121   assume "\<not>euclidean_size a < euclidean_size (lcm a b)"
1122   with \<open>a \<noteq> 0\<close> and \<open>b \<noteq> 0\<close> have "euclidean_size (lcm a b) = euclidean_size a"
1123     by (intro le_antisym, simp, intro euclidean_size_lcm_le1)
1124   with assms have "lcm a b dvd a"
1125     by (rule_tac dvd_euclidean_size_eq_imp_dvd) (auto simp: lcm_zero)
1126   hence "b dvd a" by (rule dvd_lcm_D2)
1127   with \<open>\<not>b dvd a\<close> show False by contradiction
1128 qed
1130 lemma euclidean_size_lcm_less2:
1131   assumes "a \<noteq> 0" and "\<not>a dvd b"
1132   shows "euclidean_size b < euclidean_size (lcm a b)"
1133   using assms euclidean_size_lcm_less1 [of a b] by (simp add: ac_simps)
1135 lemma lcm_mult_unit1:
1136   "is_unit a \<Longrightarrow> lcm (b * a) c = lcm b c"
1137   apply (rule lcmI)
1138   apply (rule dvd_trans[of _ "b * a"], simp, rule lcm_dvd1)
1139   apply (rule lcm_dvd2)
1140   apply (rule lcm_least, simp add: unit_simps, assumption)
1141   apply (subst normalization_factor_lcm, simp add: lcm_zero)
1142   done
1144 lemma lcm_mult_unit2:
1145   "is_unit a \<Longrightarrow> lcm b (c * a) = lcm b c"
1146   using lcm_mult_unit1 [of a c b] by (simp add: ac_simps)
1148 lemma lcm_div_unit1:
1149   "is_unit a \<Longrightarrow> lcm (b div a) c = lcm b c"
1150   by (erule is_unitE [of _ b]) (simp add: lcm_mult_unit1)
1152 lemma lcm_div_unit2:
1153   "is_unit a \<Longrightarrow> lcm b (c div a) = lcm b c"
1154   by (erule is_unitE [of _ c]) (simp add: lcm_mult_unit2)
1156 lemma lcm_left_idem:
1157   "lcm a (lcm a b) = lcm a b"
1158   apply (rule lcmI)
1159   apply simp
1160   apply (subst lcm.assoc [symmetric], rule lcm_dvd2)
1161   apply (rule lcm_least, assumption)
1162   apply (erule (1) lcm_least)
1163   apply (auto simp: lcm_zero)
1164   done
1166 lemma lcm_right_idem:
1167   "lcm (lcm a b) b = lcm a b"
1168   apply (rule lcmI)
1169   apply (subst lcm.assoc, rule lcm_dvd1)
1170   apply (rule lcm_dvd2)
1171   apply (rule lcm_least, erule (1) lcm_least, assumption)
1172   apply (auto simp: lcm_zero)
1173   done
1175 lemma comp_fun_idem_lcm: "comp_fun_idem lcm"
1176 proof
1177   fix a b show "lcm a \<circ> lcm b = lcm b \<circ> lcm a"
1178     by (simp add: fun_eq_iff ac_simps)
1179 next
1180   fix a show "lcm a \<circ> lcm a = lcm a" unfolding o_def
1181     by (intro ext, simp add: lcm_left_idem)
1182 qed
1184 lemma dvd_Lcm [simp]: "a \<in> A \<Longrightarrow> a dvd Lcm A"
1185   and Lcm_dvd [simp]: "(\<forall>a\<in>A. a dvd l') \<Longrightarrow> Lcm A dvd l'"
1186   and normalization_factor_Lcm [simp]:
1187           "normalization_factor (Lcm A) = (if Lcm A = 0 then 0 else 1)"
1188 proof -
1189   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>
1190     normalization_factor (Lcm A) = (if Lcm A = 0 then 0 else 1)" (is ?thesis)
1191   proof (cases "\<exists>l. l \<noteq>  0 \<and> (\<forall>a\<in>A. a dvd l)")
1192     case False
1193     hence "Lcm A = 0" by (auto simp: Lcm_Lcm_eucl Lcm_eucl_def)
1194     with False show ?thesis by auto
1195   next
1196     case True
1197     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
1198     def n \<equiv> "LEAST n. \<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n"
1199     def l \<equiv> "SOME l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n"
1200     have "\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n"
1201       apply (subst n_def)
1202       apply (rule LeastI[of _ "euclidean_size l\<^sub>0"])
1203       apply (rule exI[of _ l\<^sub>0])
1204       apply (simp add: l\<^sub>0_props)
1205       done
1206     from someI_ex[OF this] have "l \<noteq> 0" and "\<forall>a\<in>A. a dvd l" and "euclidean_size l = n"
1207       unfolding l_def by simp_all
1208     {
1209       fix l' assume "\<forall>a\<in>A. a dvd l'"
1210       with \<open>\<forall>a\<in>A. a dvd l\<close> have "\<forall>a\<in>A. a dvd gcd l l'" by (auto intro: gcd_greatest)
1211       moreover from \<open>l \<noteq> 0\<close> have "gcd l l' \<noteq> 0" by simp
1212       ultimately have "\<exists>b. b \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd b) \<and> euclidean_size b = euclidean_size (gcd l l')"
1213         by (intro exI[of _ "gcd l l'"], auto)
1214       hence "euclidean_size (gcd l l') \<ge> n" by (subst n_def) (rule Least_le)
1215       moreover have "euclidean_size (gcd l l') \<le> n"
1216       proof -
1217         have "gcd l l' dvd l" by simp
1218         then obtain a where "l = gcd l l' * a" unfolding dvd_def by blast
1219         with \<open>l \<noteq> 0\<close> have "a \<noteq> 0" by auto
1220         hence "euclidean_size (gcd l l') \<le> euclidean_size (gcd l l' * a)"
1221           by (rule size_mult_mono)
1222         also have "gcd l l' * a = l" using \<open>l = gcd l l' * a\<close> ..
1223         also note \<open>euclidean_size l = n\<close>
1224         finally show "euclidean_size (gcd l l') \<le> n" .
1225       qed
1226       ultimately have "euclidean_size l = euclidean_size (gcd l l')"
1227         by (intro le_antisym, simp_all add: \<open>euclidean_size l = n\<close>)
1228       with \<open>l \<noteq> 0\<close> have "l dvd gcd l l'" by (blast intro: dvd_euclidean_size_eq_imp_dvd)
1229       hence "l dvd l'" by (blast dest: dvd_gcd_D2)
1230     }
1232     with \<open>(\<forall>a\<in>A. a dvd l)\<close> and normalization_factor_is_unit[OF \<open>l \<noteq> 0\<close>] and \<open>l \<noteq> 0\<close>
1233       have "(\<forall>a\<in>A. a dvd l div normalization_factor l) \<and>
1234         (\<forall>l'. (\<forall>a\<in>A. a dvd l') \<longrightarrow> l div normalization_factor l dvd l') \<and>
1235         normalization_factor (l div normalization_factor l) =
1236         (if l div normalization_factor l = 0 then 0 else 1)"
1237       by (auto simp: unit_simps)
1238     also from True have "l div normalization_factor l = Lcm A"
1239       by (simp add: Lcm_Lcm_eucl Lcm_eucl_def Let_def n_def l_def)
1240     finally show ?thesis .
1241   qed
1242   note A = this
1244   {fix a assume "a \<in> A" then show "a dvd Lcm A" using A by blast}
1245   {fix l' assume "\<forall>a\<in>A. a dvd l'" then show "Lcm A dvd l'" using A by blast}
1246   from A show "normalization_factor (Lcm A) = (if Lcm A = 0 then 0 else 1)" by blast
1247 qed
1249 lemma LcmI:
1250   "(\<And>a. a\<in>A \<Longrightarrow> a dvd l) \<Longrightarrow> (\<And>l'. (\<forall>a\<in>A. a dvd l') \<Longrightarrow> l dvd l') \<Longrightarrow>
1251       normalization_factor l = (if l = 0 then 0 else 1) \<Longrightarrow> l = Lcm A"
1252   by (intro normed_associated_imp_eq)
1253     (auto intro: Lcm_dvd dvd_Lcm simp: associated_def)
1255 lemma Lcm_subset:
1256   "A \<subseteq> B \<Longrightarrow> Lcm A dvd Lcm B"
1257   by (blast intro: Lcm_dvd dvd_Lcm)
1259 lemma Lcm_Un:
1260   "Lcm (A \<union> B) = lcm (Lcm A) (Lcm B)"
1261   apply (rule lcmI)
1262   apply (blast intro: Lcm_subset)
1263   apply (blast intro: Lcm_subset)
1264   apply (intro Lcm_dvd ballI, elim UnE)
1265   apply (rule dvd_trans, erule dvd_Lcm, assumption)
1266   apply (rule dvd_trans, erule dvd_Lcm, assumption)
1267   apply simp
1268   done
1270 lemma Lcm_1_iff:
1271   "Lcm A = 1 \<longleftrightarrow> (\<forall>a\<in>A. is_unit a)"
1272 proof
1273   assume "Lcm A = 1"
1274   then show "\<forall>a\<in>A. is_unit a" by auto
1275 qed (rule LcmI [symmetric], auto)
1277 lemma Lcm_no_units:
1278   "Lcm A = Lcm (A - {a. is_unit a})"
1279 proof -
1280   have "(A - {a. is_unit a}) \<union> {a\<in>A. is_unit a} = A" by blast
1281   hence "Lcm A = lcm (Lcm (A - {a. is_unit a})) (Lcm {a\<in>A. is_unit a})"
1282     by (simp add: Lcm_Un[symmetric])
1283   also have "Lcm {a\<in>A. is_unit a} = 1" by (simp add: Lcm_1_iff)
1284   finally show ?thesis by simp
1285 qed
1287 lemma Lcm_empty [simp]:
1288   "Lcm {} = 1"
1289   by (simp add: Lcm_1_iff)
1291 lemma Lcm_eq_0 [simp]:
1292   "0 \<in> A \<Longrightarrow> Lcm A = 0"
1293   by (drule dvd_Lcm) simp
1295 lemma Lcm0_iff':
1296   "Lcm A = 0 \<longleftrightarrow> \<not>(\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l))"
1297 proof
1298   assume "Lcm A = 0"
1299   show "\<not>(\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l))"
1300   proof
1301     assume ex: "\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l)"
1302     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
1303     def n \<equiv> "LEAST n. \<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n"
1304     def l \<equiv> "SOME l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n"
1305     have "\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n"
1306       apply (subst n_def)
1307       apply (rule LeastI[of _ "euclidean_size l\<^sub>0"])
1308       apply (rule exI[of _ l\<^sub>0])
1309       apply (simp add: l\<^sub>0_props)
1310       done
1311     from someI_ex[OF this] have "l \<noteq> 0" unfolding l_def by simp_all
1312     hence "l div normalization_factor l \<noteq> 0" by simp
1313     also from ex have "l div normalization_factor l = Lcm A"
1314        by (simp only: Lcm_Lcm_eucl Lcm_eucl_def n_def l_def if_True Let_def)
1315     finally show False using \<open>Lcm A = 0\<close> by contradiction
1316   qed
1317 qed (simp only: Lcm_Lcm_eucl Lcm_eucl_def if_False)
1319 lemma Lcm0_iff [simp]:
1320   "finite A \<Longrightarrow> Lcm A = 0 \<longleftrightarrow> 0 \<in> A"
1321 proof -
1322   assume "finite A"
1323   have "0 \<in> A \<Longrightarrow> Lcm A = 0"  by (intro dvd_0_left dvd_Lcm)
1324   moreover {
1325     assume "0 \<notin> A"
1326     hence "\<Prod>A \<noteq> 0"
1327       apply (induct rule: finite_induct[OF \<open>finite A\<close>])
1328       apply simp
1329       apply (subst setprod.insert, assumption, assumption)
1330       apply (rule no_zero_divisors)
1331       apply blast+
1332       done
1333     moreover from \<open>finite A\<close> have "\<forall>a\<in>A. a dvd \<Prod>A" by blast
1334     ultimately have "\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l)" by blast
1335     with Lcm0_iff' have "Lcm A \<noteq> 0" by simp
1336   }
1337   ultimately show "Lcm A = 0 \<longleftrightarrow> 0 \<in> A" by blast
1338 qed
1340 lemma Lcm_no_multiple:
1341   "(\<forall>m. m \<noteq> 0 \<longrightarrow> (\<exists>a\<in>A. \<not>a dvd m)) \<Longrightarrow> Lcm A = 0"
1342 proof -
1343   assume "\<forall>m. m \<noteq> 0 \<longrightarrow> (\<exists>a\<in>A. \<not>a dvd m)"
1344   hence "\<not>(\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l))" by blast
1345   then show "Lcm A = 0" by (simp only: Lcm_Lcm_eucl Lcm_eucl_def if_False)
1346 qed
1348 lemma Lcm_insert [simp]:
1349   "Lcm (insert a A) = lcm a (Lcm A)"
1350 proof (rule lcmI)
1351   fix l assume "a dvd l" and "Lcm A dvd l"
1352   hence "\<forall>a\<in>A. a dvd l" by (blast intro: dvd_trans dvd_Lcm)
1353   with \<open>a dvd l\<close> show "Lcm (insert a A) dvd l" by (force intro: Lcm_dvd)
1354 qed (auto intro: Lcm_dvd dvd_Lcm)
1356 lemma Lcm_finite:
1357   assumes "finite A"
1358   shows "Lcm A = Finite_Set.fold lcm 1 A"
1359   by (induct rule: finite.induct[OF \<open>finite A\<close>])
1360     (simp_all add: comp_fun_idem.fold_insert_idem[OF comp_fun_idem_lcm])
1362 lemma Lcm_set [code_unfold]:
1363   "Lcm (set xs) = fold lcm xs 1"
1364   using comp_fun_idem.fold_set_fold[OF comp_fun_idem_lcm] Lcm_finite by (simp add: ac_simps)
1366 lemma Lcm_singleton [simp]:
1367   "Lcm {a} = a div normalization_factor a"
1368   by simp
1370 lemma Lcm_2 [simp]:
1371   "Lcm {a,b} = lcm a b"
1372   by (simp only: Lcm_insert Lcm_empty lcm_1_right)
1373     (cases "b = 0", simp, rule lcm_div_unit2, simp)
1375 lemma Lcm_coprime:
1376   assumes "finite A" and "A \<noteq> {}"
1377   assumes "\<And>a b. a \<in> A \<Longrightarrow> b \<in> A \<Longrightarrow> a \<noteq> b \<Longrightarrow> gcd a b = 1"
1378   shows "Lcm A = \<Prod>A div normalization_factor (\<Prod>A)"
1379 using assms proof (induct rule: finite_ne_induct)
1380   case (insert a A)
1381   have "Lcm (insert a A) = lcm a (Lcm A)" by simp
1382   also from insert have "Lcm A = \<Prod>A div normalization_factor (\<Prod>A)" by blast
1383   also have "lcm a \<dots> = lcm a (\<Prod>A)" by (cases "\<Prod>A = 0") (simp_all add: lcm_div_unit2)
1384   also from insert have "gcd a (\<Prod>A) = 1" by (subst gcd.commute, intro setprod_coprime) auto
1385   with insert have "lcm a (\<Prod>A) = \<Prod>(insert a A) div normalization_factor (\<Prod>(insert a A))"
1386     by (simp add: lcm_coprime)
1387   finally show ?case .
1388 qed simp
1390 lemma Lcm_coprime':
1391   "card A \<noteq> 0 \<Longrightarrow> (\<And>a b. a \<in> A \<Longrightarrow> b \<in> A \<Longrightarrow> a \<noteq> b \<Longrightarrow> gcd a b = 1)
1392     \<Longrightarrow> Lcm A = \<Prod>A div normalization_factor (\<Prod>A)"
1393   by (rule Lcm_coprime) (simp_all add: card_eq_0_iff)
1395 lemma Gcd_Lcm:
1396   "Gcd A = Lcm {d. \<forall>a\<in>A. d dvd a}"
1397   by (simp add: Gcd_Gcd_eucl Lcm_Lcm_eucl Gcd_eucl_def)
1399 lemma Gcd_dvd [simp]: "a \<in> A \<Longrightarrow> Gcd A dvd a"
1400   and dvd_Gcd [simp]: "(\<forall>a\<in>A. g' dvd a) \<Longrightarrow> g' dvd Gcd A"
1401   and normalization_factor_Gcd [simp]:
1402     "normalization_factor (Gcd A) = (if Gcd A = 0 then 0 else 1)"
1403 proof -
1404   fix a assume "a \<in> A"
1405   hence "Lcm {d. \<forall>a\<in>A. d dvd a} dvd a" by (intro Lcm_dvd) blast
1406   then show "Gcd A dvd a" by (simp add: Gcd_Lcm)
1407 next
1408   fix g' assume "\<forall>a\<in>A. g' dvd a"
1409   hence "g' dvd Lcm {d. \<forall>a\<in>A. d dvd a}" by (intro dvd_Lcm) blast
1410   then show "g' dvd Gcd A" by (simp add: Gcd_Lcm)
1411 next
1412   show "normalization_factor (Gcd A) = (if Gcd A = 0 then 0 else 1)"
1413     by (simp add: Gcd_Lcm)
1414 qed
1416 lemma GcdI:
1417   "(\<And>a. a\<in>A \<Longrightarrow> l dvd a) \<Longrightarrow> (\<And>l'. (\<forall>a\<in>A. l' dvd a) \<Longrightarrow> l' dvd l) \<Longrightarrow>
1418     normalization_factor l = (if l = 0 then 0 else 1) \<Longrightarrow> l = Gcd A"
1419   by (intro normed_associated_imp_eq)
1420     (auto intro: Gcd_dvd dvd_Gcd simp: associated_def)
1422 lemma Lcm_Gcd:
1423   "Lcm A = Gcd {m. \<forall>a\<in>A. a dvd m}"
1424   by (rule LcmI[symmetric]) (auto intro: dvd_Gcd Gcd_dvd)
1426 lemma Gcd_0_iff:
1427   "Gcd A = 0 \<longleftrightarrow> A \<subseteq> {0}"
1428   apply (rule iffI)
1429   apply (rule subsetI, drule Gcd_dvd, simp)
1430   apply (auto intro: GcdI[symmetric])
1431   done
1433 lemma Gcd_empty [simp]:
1434   "Gcd {} = 0"
1435   by (simp add: Gcd_0_iff)
1437 lemma Gcd_1:
1438   "1 \<in> A \<Longrightarrow> Gcd A = 1"
1439   by (intro GcdI[symmetric]) (auto intro: Gcd_dvd dvd_Gcd)
1441 lemma Gcd_insert [simp]:
1442   "Gcd (insert a A) = gcd a (Gcd A)"
1443 proof (rule gcdI)
1444   fix l assume "l dvd a" and "l dvd Gcd A"
1445   hence "\<forall>a\<in>A. l dvd a" by (blast intro: dvd_trans Gcd_dvd)
1446   with \<open>l dvd a\<close> show "l dvd Gcd (insert a A)" by (force intro: Gcd_dvd)
1447 qed auto
1449 lemma Gcd_finite:
1450   assumes "finite A"
1451   shows "Gcd A = Finite_Set.fold gcd 0 A"
1452   by (induct rule: finite.induct[OF \<open>finite A\<close>])
1453     (simp_all add: comp_fun_idem.fold_insert_idem[OF comp_fun_idem_gcd])
1455 lemma Gcd_set [code_unfold]:
1456   "Gcd (set xs) = fold gcd xs 0"
1457   using comp_fun_idem.fold_set_fold[OF comp_fun_idem_gcd] Gcd_finite by (simp add: ac_simps)
1459 lemma Gcd_singleton [simp]: "Gcd {a} = a div normalization_factor a"
1460   by (simp add: gcd_0)
1462 lemma Gcd_2 [simp]: "Gcd {a,b} = gcd a b"
1463   by (simp only: Gcd_insert Gcd_empty gcd_0) (cases "b = 0", simp, rule gcd_div_unit2, simp)
1465 subclass semiring_gcd
1466   by unfold_locales (simp_all add: gcd_greatest_iff)
1468 end
1470 text \<open>
1471   A Euclidean ring is a Euclidean semiring with additive inverses. It provides a
1472   few more lemmas; in particular, Bezout's lemma holds for any Euclidean ring.
1473 \<close>
1475 class euclidean_ring_gcd = euclidean_semiring_gcd + idom
1476 begin
1478 subclass euclidean_ring ..
1480 subclass ring_gcd ..
1482 lemma euclid_ext_gcd [simp]:
1483   "(case euclid_ext a b of (_, _ , t) \<Rightarrow> t) = gcd a b"
1484   by (induct a b rule: gcd_eucl_induct)
1485     (simp_all add: euclid_ext_0 gcd_0 euclid_ext_non_0 ac_simps split: prod.split prod.split_asm)
1487 lemma euclid_ext_gcd' [simp]:
1488   "euclid_ext a b = (r, s, t) \<Longrightarrow> t = gcd a b"
1489   by (insert euclid_ext_gcd[of a b], drule (1) subst, simp)
1491 lemma euclid_ext'_correct:
1492   "fst (euclid_ext' a b) * a + snd (euclid_ext' a b) * b = gcd a b"
1493 proof-
1494   obtain s t c where "euclid_ext a b = (s,t,c)"
1495     by (cases "euclid_ext a b", blast)
1496   with euclid_ext_correct[of a b] euclid_ext_gcd[of a b]
1497     show ?thesis unfolding euclid_ext'_def by simp
1498 qed
1500 lemma bezout: "\<exists>s t. s * a + t * b = gcd a b"
1501   using euclid_ext'_correct by blast
1503 lemma gcd_neg1 [simp]:
1504   "gcd (-a) b = gcd a b"
1505   by (rule sym, rule gcdI, simp_all add: gcd_greatest)
1507 lemma gcd_neg2 [simp]:
1508   "gcd a (-b) = gcd a b"
1509   by (rule sym, rule gcdI, simp_all add: gcd_greatest)
1511 lemma gcd_neg_numeral_1 [simp]:
1512   "gcd (- numeral n) a = gcd (numeral n) a"
1513   by (fact gcd_neg1)
1515 lemma gcd_neg_numeral_2 [simp]:
1516   "gcd a (- numeral n) = gcd a (numeral n)"
1517   by (fact gcd_neg2)
1519 lemma gcd_diff1: "gcd (m - n) n = gcd m n"
1520   by (subst diff_conv_add_uminus, subst gcd_neg2[symmetric],  subst gcd_add1, simp)
1522 lemma gcd_diff2: "gcd (n - m) n = gcd m n"
1523   by (subst gcd_neg1[symmetric], simp only: minus_diff_eq gcd_diff1)
1525 lemma coprime_minus_one [simp]: "gcd (n - 1) n = 1"
1526 proof -
1527   have "gcd (n - 1) n = gcd n (n - 1)" by (fact gcd.commute)
1528   also have "\<dots> = gcd ((n - 1) + 1) (n - 1)" by simp
1529   also have "\<dots> = 1" by (rule coprime_plus_one)
1530   finally show ?thesis .
1531 qed
1533 lemma lcm_neg1 [simp]: "lcm (-a) b = lcm a b"
1534   by (rule sym, rule lcmI, simp_all add: lcm_least lcm_zero)
1536 lemma lcm_neg2 [simp]: "lcm a (-b) = lcm a b"
1537   by (rule sym, rule lcmI, simp_all add: lcm_least lcm_zero)
1539 lemma lcm_neg_numeral_1 [simp]: "lcm (- numeral n) a = lcm (numeral n) a"
1540   by (fact lcm_neg1)
1542 lemma lcm_neg_numeral_2 [simp]: "lcm a (- numeral n) = lcm a (numeral n)"
1543   by (fact lcm_neg2)
1545 end
1548 subsection \<open>Typical instances\<close>
1550 instantiation nat :: euclidean_semiring
1551 begin
1553 definition [simp]:
1554   "euclidean_size_nat = (id :: nat \<Rightarrow> nat)"
1556 definition [simp]:
1557   "normalization_factor_nat (n::nat) = (if n = 0 then 0 else 1 :: nat)"
1559 instance proof
1560 qed simp_all
1562 end
1564 instantiation int :: euclidean_ring
1565 begin
1567 definition [simp]:
1568   "euclidean_size_int = (nat \<circ> abs :: int \<Rightarrow> nat)"
1570 definition [simp]:
1571   "normalization_factor_int = (sgn :: int \<Rightarrow> int)"
1573 instance
1574 proof (default, goals)
1575   case 2
1576   then show ?case by (auto simp add: abs_mult nat_mult_distrib)
1577 next
1578   case 3
1579   then show ?case by (simp add: zsgn_def)
1580 next
1581   case 5
1582   then show ?case by (auto simp: zsgn_def)
1583 next
1584   case 6
1585   then show ?case by (auto split: abs_split simp: zsgn_def)
1586 qed (auto simp: sgn_times split: abs_split)
1588 end
1590 instantiation poly :: (field) euclidean_ring
1591 begin
1593 definition euclidean_size_poly :: "'a poly \<Rightarrow> nat"
1594   where "euclidean_size p = (if p = 0 then 0 else Suc (degree p))"
1596 definition normalization_factor_poly :: "'a poly \<Rightarrow> 'a poly"
1597   where "normalization_factor p = monom (coeff p (degree p)) 0"
1599 lemma euclidean_size_poly_0 [simp]:
1600   "euclidean_size (0::'a poly) = 0"
1601   by (simp add: euclidean_size_poly_def)
1603 lemma euclidean_size_poly_not_0 [simp]:
1604   "p \<noteq> 0 \<Longrightarrow> euclidean_size p = Suc (degree p)"
1605   by (simp add: euclidean_size_poly_def)
1607 instance
1608 proof
1609   fix p q :: "'a poly"
1610   assume "q \<noteq> 0"
1611   then have "p mod q = 0 \<or> degree (p mod q) < degree q"
1612     by (rule degree_mod_less [of q p])
1613   with \<open>q \<noteq> 0\<close> show "euclidean_size (p mod q) < euclidean_size q"
1614     by (cases "p mod q = 0") simp_all
1615 next
1616   fix p q :: "'a poly"
1617   assume "q \<noteq> 0"
1618   from \<open>q \<noteq> 0\<close> have "degree p \<le> degree (p * q)"
1619     by (rule degree_mult_right_le)
1620   with \<open>q \<noteq> 0\<close> show "euclidean_size p \<le> euclidean_size (p * q)"
1621     by (cases "p = 0") simp_all
1622   from \<open>q \<noteq> 0\<close> have "is_unit (monom (coeff q (degree q)) 0)"
1623     by (auto intro: is_unit_monom_0)
1624   then show "is_unit (normalization_factor q)"
1625     by (simp add: normalization_factor_poly_def)
1626 next
1627   fix p :: "'a poly"
1628   assume "is_unit p"
1629   then have "monom (coeff p (degree p)) 0 = p"
1630     by (fact is_unit_monom_trival)
1631   then show "normalization_factor p = p"
1632     by (simp add: normalization_factor_poly_def)
1633 next
1634   fix p q :: "'a poly"
1635   have "monom (coeff (p * q) (degree (p * q))) 0 =
1636     monom (coeff p (degree p)) 0 * monom (coeff q (degree q)) 0"
1637     by (simp add: monom_0 coeff_degree_mult)
1638   then show "normalization_factor (p * q) =
1639     normalization_factor p * normalization_factor q"
1640     by (simp add: normalization_factor_poly_def)
1641 next
1642   have "monom (coeff 0 (degree 0)) 0 = 0"
1643     by simp
1644   then show "normalization_factor 0 = (0::'a poly)"
1645     by (simp add: normalization_factor_poly_def)
1646 qed
1648 end
1650 end