src/HOL/Number_Theory/Euclidean_Algorithm.thy
 author wenzelm Fri Jun 19 21:41:33 2015 +0200 (2015-06-19) changeset 60526 fad653acf58f parent 60517 f16e4fb20652 child 60569 f2f1f6860959 child 60580 7e741e22d7fc permissions -rw-r--r--
isabelle update_cartouches;
1 (* Author: Manuel Eberl *)
3 section \<open>Abstract euclidean algorithm\<close>
5 theory Euclidean_Algorithm
6 imports Complex_Main
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 [simp]:
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"
86 proof (cases "b = 0", simp, cases "a = 0", metis associated_0(1) normalization_0_iff, rule iffI)
87   let ?nf = normalization_factor
88   assume "a \<noteq> 0" "b \<noteq> 0" "a div ?nf a = b div ?nf b"
89   hence "a = b * (?nf a div ?nf b)"
90     apply (subst (asm) unit_eq_div1, blast, subst (asm) unit_div_commute, blast)
91     apply (subst div_mult_swap, simp, simp)
92     done
93   with \<open>a \<noteq> 0\<close> \<open>b \<noteq> 0\<close> have "\<exists>c. is_unit c \<and> a = c * b"
94     by (intro exI[of _ "?nf a div ?nf b"], force simp: mult_ac)
95   then obtain c where "is_unit c" and "a = c * b" by blast
96   then show "associated a b" by (rule is_unit_associatedI)
97 next
98   let ?nf = normalization_factor
99   assume "a \<noteq> 0" "b \<noteq> 0" "associated a b"
100   then obtain c where "is_unit c" and "a = c * b" by (blast elim: associated_is_unitE)
101   then show "a div ?nf a = b div ?nf b"
102     apply (simp only: \<open>a = c * b\<close> normalization_factor_mult normalization_factor_unit)
103     apply (rule div_mult_mult1, force)
104     done
105   qed
107 lemma normed_associated_imp_eq:
108   "associated a b \<Longrightarrow> normalization_factor a \<in> {0, 1} \<Longrightarrow> normalization_factor b \<in> {0, 1} \<Longrightarrow> a = b"
109   by (simp add: associated_iff_normed_eq, elim disjE, simp_all)
111 lemmas normalization_factor_dvd_iff [simp] =
112   unit_dvd_iff [OF normalization_factor_is_unit]
114 lemma euclidean_division:
115   fixes a :: 'a and b :: 'a
116   assumes "b \<noteq> 0"
117   obtains s and t where "a = s * b + t"
118     and "euclidean_size t < euclidean_size b"
119 proof -
120   from div_mod_equality[of a b 0]
121      have "a = a div b * b + a mod b" by simp
122   with that and assms show ?thesis by force
123 qed
125 lemma dvd_euclidean_size_eq_imp_dvd:
126   assumes "a \<noteq> 0" and b_dvd_a: "b dvd a" and size_eq: "euclidean_size a = euclidean_size b"
127   shows "a dvd b"
128 proof (subst dvd_eq_mod_eq_0, rule ccontr)
129   assume "b mod a \<noteq> 0"
130   from b_dvd_a have b_dvd_mod: "b dvd b mod a" by (simp add: dvd_mod_iff)
131   from b_dvd_mod obtain c where "b mod a = b * c" unfolding dvd_def by blast
132     with \<open>b mod a \<noteq> 0\<close> have "c \<noteq> 0" by auto
133   with \<open>b mod a = b * c\<close> have "euclidean_size (b mod a) \<ge> euclidean_size b"
134       using size_mult_mono by force
135   moreover from \<open>a \<noteq> 0\<close> have "euclidean_size (b mod a) < euclidean_size a"
136       using mod_size_less by blast
137   ultimately show False using size_eq by simp
138 qed
140 function gcd_eucl :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"
141 where
142   "gcd_eucl a b = (if b = 0 then a div normalization_factor a else gcd_eucl b (a mod b))"
143   by (pat_completeness, simp)
144 termination by (relation "measure (euclidean_size \<circ> snd)", simp_all)
146 declare gcd_eucl.simps [simp del]
148 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"
149 proof (induct a b rule: gcd_eucl.induct)
150   case ("1" m n)
151     then show ?case by (cases "n = 0") auto
152 qed
154 definition lcm_eucl :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"
155 where
156   "lcm_eucl a b = a * b div (gcd_eucl a b * normalization_factor (a * b))"
158   (* Somewhat complicated definition of Lcm that has the advantage of working
159      for infinite sets as well *)
161 definition Lcm_eucl :: "'a set \<Rightarrow> 'a"
162 where
163   "Lcm_eucl A = (if \<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) then
164      let l = SOME l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l =
165        (LEAST n. \<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n)
166        in l div normalization_factor l
167       else 0)"
169 definition Gcd_eucl :: "'a set \<Rightarrow> 'a"
170 where
171   "Gcd_eucl A = Lcm_eucl {d. \<forall>a\<in>A. d dvd a}"
173 end
175 class euclidean_semiring_gcd = euclidean_semiring + gcd + Gcd +
176   assumes gcd_gcd_eucl: "gcd = gcd_eucl" and lcm_lcm_eucl: "lcm = lcm_eucl"
177   assumes Gcd_Gcd_eucl: "Gcd = Gcd_eucl" and Lcm_Lcm_eucl: "Lcm = Lcm_eucl"
178 begin
180 lemma gcd_red:
181   "gcd a b = gcd b (a mod b)"
182   by (metis gcd_eucl.simps mod_0 mod_by_0 gcd_gcd_eucl)
184 lemma gcd_non_0:
185   "b \<noteq> 0 \<Longrightarrow> gcd a b = gcd b (a mod b)"
186   by (rule gcd_red)
188 lemma gcd_0_left:
189   "gcd 0 a = a div normalization_factor a"
190    by (simp only: gcd_gcd_eucl, subst gcd_eucl.simps, subst gcd_eucl.simps, simp add: Let_def)
192 lemma gcd_0:
193   "gcd a 0 = a div normalization_factor a"
194   by (simp only: gcd_gcd_eucl, subst gcd_eucl.simps, simp add: Let_def)
196 lemma gcd_dvd1 [iff]: "gcd a b dvd a"
197   and gcd_dvd2 [iff]: "gcd a b dvd b"
198 proof (induct a b rule: gcd_eucl.induct)
199   fix a b :: 'a
200   assume IH1: "b \<noteq> 0 \<Longrightarrow> gcd b (a mod b) dvd b"
201   assume IH2: "b \<noteq> 0 \<Longrightarrow> gcd b (a mod b) dvd (a mod b)"
203   have "gcd a b dvd a \<and> gcd a b dvd b"
204   proof (cases "b = 0")
205     case True
206       then show ?thesis by (cases "a = 0", simp_all add: gcd_0)
207   next
208     case False
209       with IH1 and IH2 show ?thesis by (simp add: gcd_non_0 dvd_mod_iff)
210   qed
211   then show "gcd a b dvd a" "gcd a b dvd b" by simp_all
212 qed
214 lemma dvd_gcd_D1: "k dvd gcd m n \<Longrightarrow> k dvd m"
215   by (rule dvd_trans, assumption, rule gcd_dvd1)
217 lemma dvd_gcd_D2: "k dvd gcd m n \<Longrightarrow> k dvd n"
218   by (rule dvd_trans, assumption, rule gcd_dvd2)
220 lemma gcd_greatest:
221   fixes k a b :: 'a
222   shows "k dvd a \<Longrightarrow> k dvd b \<Longrightarrow> k dvd gcd a b"
223 proof (induct a b rule: gcd_eucl.induct)
224   case (1 a b)
225   show ?case
226     proof (cases "b = 0")
227       assume "b = 0"
228       with 1 show ?thesis by (cases "a = 0", simp_all add: gcd_0)
229     next
230       assume "b \<noteq> 0"
231       with 1 show ?thesis by (simp add: gcd_non_0 dvd_mod_iff)
232     qed
233 qed
235 lemma dvd_gcd_iff:
236   "k dvd gcd a b \<longleftrightarrow> k dvd a \<and> k dvd b"
237   by (blast intro!: gcd_greatest intro: dvd_trans)
239 lemmas gcd_greatest_iff = dvd_gcd_iff
241 lemma gcd_zero [simp]:
242   "gcd a b = 0 \<longleftrightarrow> a = 0 \<and> b = 0"
243   by (metis dvd_0_left dvd_refl gcd_dvd1 gcd_dvd2 gcd_greatest)+
245 lemma normalization_factor_gcd [simp]:
246   "normalization_factor (gcd a b) = (if a = 0 \<and> b = 0 then 0 else 1)" (is "?f a b = ?g a b")
247 proof (induct a b rule: gcd_eucl.induct)
248   fix a b :: 'a
249   assume IH: "b \<noteq> 0 \<Longrightarrow> ?f b (a mod b) = ?g b (a mod b)"
250   then show "?f a b = ?g a b" by (cases "b = 0", auto simp: gcd_non_0 gcd_0)
251 qed
253 lemma gcdI:
254   "k dvd a \<Longrightarrow> k dvd b \<Longrightarrow> (\<And>l. l dvd a \<Longrightarrow> l dvd b \<Longrightarrow> l dvd k)
255     \<Longrightarrow> normalization_factor k = (if k = 0 then 0 else 1) \<Longrightarrow> k = gcd a b"
256   by (intro normed_associated_imp_eq) (auto simp: associated_def intro: gcd_greatest)
258 sublocale gcd!: abel_semigroup gcd
259 proof
260   fix a b c
261   show "gcd (gcd a b) c = gcd a (gcd b c)"
262   proof (rule gcdI)
263     have "gcd (gcd a b) c dvd gcd a b" "gcd a b dvd a" by simp_all
264     then show "gcd (gcd a b) c dvd a" by (rule dvd_trans)
265     have "gcd (gcd a b) c dvd gcd a b" "gcd a b dvd b" by simp_all
266     hence "gcd (gcd a b) c dvd b" by (rule dvd_trans)
267     moreover have "gcd (gcd a b) c dvd c" by simp
268     ultimately show "gcd (gcd a b) c dvd gcd b c"
269       by (rule gcd_greatest)
270     show "normalization_factor (gcd (gcd a b) c) =  (if gcd (gcd a b) c = 0 then 0 else 1)"
271       by auto
272     fix l assume "l dvd a" and "l dvd gcd b c"
273     with dvd_trans[OF _ gcd_dvd1] and dvd_trans[OF _ gcd_dvd2]
274       have "l dvd b" and "l dvd c" by blast+
275     with \<open>l dvd a\<close> show "l dvd gcd (gcd a b) c"
276       by (intro gcd_greatest)
277   qed
278 next
279   fix a b
280   show "gcd a b = gcd b a"
281     by (rule gcdI) (simp_all add: gcd_greatest)
282 qed
284 lemma gcd_unique: "d dvd a \<and> d dvd b \<and>
285     normalization_factor d = (if d = 0 then 0 else 1) \<and>
286     (\<forall>e. e dvd a \<and> e dvd b \<longrightarrow> e dvd d) \<longleftrightarrow> d = gcd a b"
287   by (rule, auto intro: gcdI simp: gcd_greatest)
289 lemma gcd_dvd_prod: "gcd a b dvd k * b"
290   using mult_dvd_mono [of 1] by auto
292 lemma gcd_1_left [simp]: "gcd 1 a = 1"
293   by (rule sym, rule gcdI, simp_all)
295 lemma gcd_1 [simp]: "gcd a 1 = 1"
296   by (rule sym, rule gcdI, simp_all)
298 lemma gcd_proj2_if_dvd:
299   "b dvd a \<Longrightarrow> gcd a b = b div normalization_factor b"
300   by (cases "b = 0", simp_all add: dvd_eq_mod_eq_0 gcd_non_0 gcd_0)
302 lemma gcd_proj1_if_dvd:
303   "a dvd b \<Longrightarrow> gcd a b = a div normalization_factor a"
304   by (subst gcd.commute, simp add: gcd_proj2_if_dvd)
306 lemma gcd_proj1_iff: "gcd m n = m div normalization_factor m \<longleftrightarrow> m dvd n"
307 proof
308   assume A: "gcd m n = m div normalization_factor m"
309   show "m dvd n"
310   proof (cases "m = 0")
311     assume [simp]: "m \<noteq> 0"
312     from A have B: "m = gcd m n * normalization_factor m"
313       by (simp add: unit_eq_div2)
314     show ?thesis by (subst B, simp add: mult_unit_dvd_iff)
315   qed (insert A, simp)
316 next
317   assume "m dvd n"
318   then show "gcd m n = m div normalization_factor m" by (rule gcd_proj1_if_dvd)
319 qed
321 lemma gcd_proj2_iff: "gcd m n = n div normalization_factor n \<longleftrightarrow> n dvd m"
322   by (subst gcd.commute, simp add: gcd_proj1_iff)
324 lemma gcd_mod1 [simp]:
325   "gcd (a mod b) b = gcd a b"
326   by (rule gcdI, metis dvd_mod_iff gcd_dvd1 gcd_dvd2, simp_all add: gcd_greatest dvd_mod_iff)
328 lemma gcd_mod2 [simp]:
329   "gcd a (b mod a) = gcd a b"
330   by (rule gcdI, simp, metis dvd_mod_iff gcd_dvd1 gcd_dvd2, simp_all add: gcd_greatest dvd_mod_iff)
332 lemma normalization_factor_dvd' [simp]:
333   "normalization_factor a dvd a"
334   by (cases "a = 0", simp_all)
336 lemma gcd_mult_distrib':
337   "k div normalization_factor k * gcd a b = gcd (k*a) (k*b)"
338 proof (induct a b rule: gcd_eucl.induct)
339   case (1 a b)
340   show ?case
341   proof (cases "b = 0")
342     case True
343     then show ?thesis by (simp add: normalization_factor_mult gcd_0 algebra_simps div_mult_div_if_dvd)
344   next
345     case False
346     hence "k div normalization_factor k * gcd a b =  gcd (k * b) (k * (a mod b))"
347       using 1 by (subst gcd_red, simp)
348     also have "... = gcd (k * a) (k * b)"
349       by (simp add: mult_mod_right gcd.commute)
350     finally show ?thesis .
351   qed
352 qed
354 lemma gcd_mult_distrib:
355   "k * gcd a b = gcd (k*a) (k*b) * normalization_factor k"
356 proof-
357   let ?nf = "normalization_factor"
358   from gcd_mult_distrib'
359     have "gcd (k*a) (k*b) = k div ?nf k * gcd a b" ..
360   also have "... = k * gcd a b div ?nf k"
361     by (metis dvd_div_mult dvd_eq_mod_eq_0 mod_0 normalization_factor_dvd)
362   finally show ?thesis
363     by simp
364 qed
366 lemma euclidean_size_gcd_le1 [simp]:
367   assumes "a \<noteq> 0"
368   shows "euclidean_size (gcd a b) \<le> euclidean_size a"
369 proof -
370    have "gcd a b dvd a" by (rule gcd_dvd1)
371    then obtain c where A: "a = gcd a b * c" unfolding dvd_def by blast
372    with \<open>a \<noteq> 0\<close> show ?thesis by (subst (2) A, intro size_mult_mono) auto
373 qed
375 lemma euclidean_size_gcd_le2 [simp]:
376   "b \<noteq> 0 \<Longrightarrow> euclidean_size (gcd a b) \<le> euclidean_size b"
377   by (subst gcd.commute, rule euclidean_size_gcd_le1)
379 lemma euclidean_size_gcd_less1:
380   assumes "a \<noteq> 0" and "\<not>a dvd b"
381   shows "euclidean_size (gcd a b) < euclidean_size a"
382 proof (rule ccontr)
383   assume "\<not>euclidean_size (gcd a b) < euclidean_size a"
384   with \<open>a \<noteq> 0\<close> have "euclidean_size (gcd a b) = euclidean_size a"
385     by (intro le_antisym, simp_all)
386   with assms have "a dvd gcd a b" by (auto intro: dvd_euclidean_size_eq_imp_dvd)
387   hence "a dvd b" using dvd_gcd_D2 by blast
388   with \<open>\<not>a dvd b\<close> show False by contradiction
389 qed
391 lemma euclidean_size_gcd_less2:
392   assumes "b \<noteq> 0" and "\<not>b dvd a"
393   shows "euclidean_size (gcd a b) < euclidean_size b"
394   using assms by (subst gcd.commute, rule euclidean_size_gcd_less1)
396 lemma gcd_mult_unit1: "is_unit a \<Longrightarrow> gcd (b * a) c = gcd b c"
397   apply (rule gcdI)
398   apply (rule dvd_trans, rule gcd_dvd1, simp add: unit_simps)
399   apply (rule gcd_dvd2)
400   apply (rule gcd_greatest, simp add: unit_simps, assumption)
401   apply (subst normalization_factor_gcd, simp add: gcd_0)
402   done
404 lemma gcd_mult_unit2: "is_unit a \<Longrightarrow> gcd b (c * a) = gcd b c"
405   by (subst gcd.commute, subst gcd_mult_unit1, assumption, rule gcd.commute)
407 lemma gcd_div_unit1: "is_unit a \<Longrightarrow> gcd (b div a) c = gcd b c"
408   by (erule is_unitE [of _ b]) (simp add: gcd_mult_unit1)
410 lemma gcd_div_unit2: "is_unit a \<Longrightarrow> gcd b (c div a) = gcd b c"
411   by (erule is_unitE [of _ c]) (simp add: gcd_mult_unit2)
413 lemma gcd_idem: "gcd a a = a div normalization_factor a"
414   by (cases "a = 0") (simp add: gcd_0_left, rule sym, rule gcdI, simp_all)
416 lemma gcd_right_idem: "gcd (gcd a b) b = gcd a b"
417   apply (rule gcdI)
418   apply (simp add: ac_simps)
419   apply (rule gcd_dvd2)
420   apply (rule gcd_greatest, erule (1) gcd_greatest, assumption)
421   apply simp
422   done
424 lemma gcd_left_idem: "gcd a (gcd a b) = gcd a b"
425   apply (rule gcdI)
426   apply simp
427   apply (rule dvd_trans, rule gcd_dvd2, rule gcd_dvd2)
428   apply (rule gcd_greatest, assumption, erule gcd_greatest, assumption)
429   apply simp
430   done
432 lemma comp_fun_idem_gcd: "comp_fun_idem gcd"
433 proof
434   fix a b show "gcd a \<circ> gcd b = gcd b \<circ> gcd a"
435     by (simp add: fun_eq_iff ac_simps)
436 next
437   fix a show "gcd a \<circ> gcd a = gcd a"
438     by (simp add: fun_eq_iff gcd_left_idem)
439 qed
441 lemma coprime_dvd_mult:
442   assumes "gcd c b = 1" and "c dvd a * b"
443   shows "c dvd a"
444 proof -
445   let ?nf = "normalization_factor"
446   from assms gcd_mult_distrib [of a c b]
447     have A: "a = gcd (a * c) (a * b) * ?nf a" by simp
448   from \<open>c dvd a * b\<close> show ?thesis by (subst A, simp_all add: gcd_greatest)
449 qed
451 lemma coprime_dvd_mult_iff:
452   "gcd c b = 1 \<Longrightarrow> (c dvd a * b) = (c dvd a)"
453   by (rule, rule coprime_dvd_mult, simp_all)
455 lemma gcd_dvd_antisym:
456   "gcd a b dvd gcd c d \<Longrightarrow> gcd c d dvd gcd a b \<Longrightarrow> gcd a b = gcd c d"
457 proof (rule gcdI)
458   assume A: "gcd a b dvd gcd c d" and B: "gcd c d dvd gcd a b"
459   have "gcd c d dvd c" by simp
460   with A show "gcd a b dvd c" by (rule dvd_trans)
461   have "gcd c d dvd d" by simp
462   with A show "gcd a b dvd d" by (rule dvd_trans)
463   show "normalization_factor (gcd a b) = (if gcd a b = 0 then 0 else 1)"
464     by simp
465   fix l assume "l dvd c" and "l dvd d"
466   hence "l dvd gcd c d" by (rule gcd_greatest)
467   from this and B show "l dvd gcd a b" by (rule dvd_trans)
468 qed
470 lemma gcd_mult_cancel:
471   assumes "gcd k n = 1"
472   shows "gcd (k * m) n = gcd m n"
473 proof (rule gcd_dvd_antisym)
474   have "gcd (gcd (k * m) n) k = gcd (gcd k n) (k * m)" by (simp add: ac_simps)
475   also note \<open>gcd k n = 1\<close>
476   finally have "gcd (gcd (k * m) n) k = 1" by simp
477   hence "gcd (k * m) n dvd m" by (rule coprime_dvd_mult, simp add: ac_simps)
478   moreover have "gcd (k * m) n dvd n" by simp
479   ultimately show "gcd (k * m) n dvd gcd m n" by (rule gcd_greatest)
480   have "gcd m n dvd (k * m)" and "gcd m n dvd n" by simp_all
481   then show "gcd m n dvd gcd (k * m) n" by (rule gcd_greatest)
482 qed
484 lemma coprime_crossproduct:
485   assumes [simp]: "gcd a d = 1" "gcd b c = 1"
486   shows "associated (a * c) (b * d) \<longleftrightarrow> associated a b \<and> associated c d" (is "?lhs \<longleftrightarrow> ?rhs")
487 proof
488   assume ?rhs then show ?lhs unfolding associated_def by (fast intro: mult_dvd_mono)
489 next
490   assume ?lhs
491   from \<open>?lhs\<close> have "a dvd b * d" unfolding associated_def by (metis dvd_mult_left)
492   hence "a dvd b" by (simp add: coprime_dvd_mult_iff)
493   moreover from \<open>?lhs\<close> have "b dvd a * c" unfolding associated_def by (metis dvd_mult_left)
494   hence "b dvd a" by (simp add: coprime_dvd_mult_iff)
495   moreover from \<open>?lhs\<close> have "c dvd d * b"
496     unfolding associated_def by (auto dest: dvd_mult_right simp add: ac_simps)
497   hence "c dvd d" by (simp add: coprime_dvd_mult_iff gcd.commute)
498   moreover from \<open>?lhs\<close> have "d dvd c * a"
499     unfolding associated_def by (auto dest: dvd_mult_right simp add: ac_simps)
500   hence "d dvd c" by (simp add: coprime_dvd_mult_iff gcd.commute)
501   ultimately show ?rhs unfolding associated_def by simp
502 qed
504 lemma gcd_add1 [simp]:
505   "gcd (m + n) n = gcd m n"
506   by (cases "n = 0", simp_all add: gcd_non_0)
508 lemma gcd_add2 [simp]:
509   "gcd m (m + n) = gcd m n"
510   using gcd_add1 [of n m] by (simp add: ac_simps)
512 lemma gcd_add_mult: "gcd m (k * m + n) = gcd m n"
513   by (subst gcd.commute, subst gcd_red, simp)
515 lemma coprimeI: "(\<And>l. \<lbrakk>l dvd a; l dvd b\<rbrakk> \<Longrightarrow> l dvd 1) \<Longrightarrow> gcd a b = 1"
516   by (rule sym, rule gcdI, simp_all)
518 lemma coprime: "gcd a b = 1 \<longleftrightarrow> (\<forall>d. d dvd a \<and> d dvd b \<longleftrightarrow> is_unit d)"
519   by (auto intro: coprimeI gcd_greatest dvd_gcd_D1 dvd_gcd_D2)
521 lemma div_gcd_coprime:
522   assumes nz: "a \<noteq> 0 \<or> b \<noteq> 0"
523   defines [simp]: "d \<equiv> gcd a b"
524   defines [simp]: "a' \<equiv> a div d" and [simp]: "b' \<equiv> b div d"
525   shows "gcd a' b' = 1"
526 proof (rule coprimeI)
527   fix l assume "l dvd a'" "l dvd b'"
528   then obtain s t where "a' = l * s" "b' = l * t" unfolding dvd_def by blast
529   moreover have "a = a' * d" "b = b' * d" by simp_all
530   ultimately have "a = (l * d) * s" "b = (l * d) * t"
531     by (simp_all only: ac_simps)
532   hence "l*d dvd a" and "l*d dvd b" by (simp_all only: dvd_triv_left)
533   hence "l*d dvd d" by (simp add: gcd_greatest)
534   then obtain u where "d = l * d * u" ..
535   then have "d * (l * u) = d" by (simp add: ac_simps)
536   moreover from nz have "d \<noteq> 0" by simp
537   with div_mult_self1_is_id have "d * (l * u) div d = l * u" .
538   ultimately have "1 = l * u"
539     using \<open>d \<noteq> 0\<close> by simp
540   then show "l dvd 1" ..
541 qed
543 lemma coprime_mult:
544   assumes da: "gcd d a = 1" and db: "gcd d b = 1"
545   shows "gcd d (a * b) = 1"
546   apply (subst gcd.commute)
547   using da apply (subst gcd_mult_cancel)
548   apply (subst gcd.commute, assumption)
549   apply (subst gcd.commute, rule db)
550   done
552 lemma coprime_lmult:
553   assumes dab: "gcd d (a * b) = 1"
554   shows "gcd d a = 1"
555 proof (rule coprimeI)
556   fix l assume "l dvd d" and "l dvd a"
557   hence "l dvd a * b" by simp
558   with \<open>l dvd d\<close> and dab show "l dvd 1" by (auto intro: gcd_greatest)
559 qed
561 lemma coprime_rmult:
562   assumes dab: "gcd d (a * b) = 1"
563   shows "gcd d b = 1"
564 proof (rule coprimeI)
565   fix l assume "l dvd d" and "l dvd b"
566   hence "l dvd a * b" by simp
567   with \<open>l dvd d\<close> and dab show "l dvd 1" by (auto intro: gcd_greatest)
568 qed
570 lemma coprime_mul_eq: "gcd d (a * b) = 1 \<longleftrightarrow> gcd d a = 1 \<and> gcd d b = 1"
571   using coprime_rmult[of d a b] coprime_lmult[of d a b] coprime_mult[of d a b] by blast
573 lemma gcd_coprime:
574   assumes c: "gcd a b \<noteq> 0" and a: "a = a' * gcd a b" and b: "b = b' * gcd a b"
575   shows "gcd a' b' = 1"
576 proof -
577   from c have "a \<noteq> 0 \<or> b \<noteq> 0" by simp
578   with div_gcd_coprime have "gcd (a div gcd a b) (b div gcd a b) = 1" .
579   also from assms have "a div gcd a b = a'" by (metis div_mult_self2_is_id)+
580   also from assms have "b div gcd a b = b'" by (metis div_mult_self2_is_id)+
581   finally show ?thesis .
582 qed
584 lemma coprime_power:
585   assumes "0 < n"
586   shows "gcd a (b ^ n) = 1 \<longleftrightarrow> gcd a b = 1"
587 using assms proof (induct n)
588   case (Suc n) then show ?case
589     by (cases n) (simp_all add: coprime_mul_eq)
590 qed simp
592 lemma gcd_coprime_exists:
593   assumes nz: "gcd a b \<noteq> 0"
594   shows "\<exists>a' b'. a = a' * gcd a b \<and> b = b' * gcd a b \<and> gcd a' b' = 1"
595   apply (rule_tac x = "a div gcd a b" in exI)
596   apply (rule_tac x = "b div gcd a b" in exI)
597   apply (insert nz, auto intro: div_gcd_coprime)
598   done
600 lemma coprime_exp:
601   "gcd d a = 1 \<Longrightarrow> gcd d (a^n) = 1"
602   by (induct n, simp_all add: coprime_mult)
604 lemma coprime_exp2 [intro]:
605   "gcd a b = 1 \<Longrightarrow> gcd (a^n) (b^m) = 1"
606   apply (rule coprime_exp)
607   apply (subst gcd.commute)
608   apply (rule coprime_exp)
609   apply (subst gcd.commute)
610   apply assumption
611   done
613 lemma gcd_exp:
614   "gcd (a^n) (b^n) = (gcd a b) ^ n"
615 proof (cases "a = 0 \<and> b = 0")
616   assume "a = 0 \<and> b = 0"
617   then show ?thesis by (cases n, simp_all add: gcd_0_left)
618 next
619   assume A: "\<not>(a = 0 \<and> b = 0)"
620   hence "1 = gcd ((a div gcd a b)^n) ((b div gcd a b)^n)"
621     using div_gcd_coprime by (subst sym, auto simp: div_gcd_coprime)
622   hence "(gcd a b) ^ n = (gcd a b) ^ n * ..." by simp
623   also note gcd_mult_distrib
624   also have "normalization_factor ((gcd a b)^n) = 1"
625     by (simp add: normalization_factor_pow A)
626   also have "(gcd a b)^n * (a div gcd a b)^n = a^n"
627     by (subst ac_simps, subst div_power, simp, rule dvd_div_mult_self, rule dvd_power_same, simp)
628   also have "(gcd a b)^n * (b div gcd a b)^n = b^n"
629     by (subst ac_simps, subst div_power, simp, rule dvd_div_mult_self, rule dvd_power_same, simp)
630   finally show ?thesis by simp
631 qed
633 lemma coprime_common_divisor:
634   "gcd a b = 1 \<Longrightarrow> a dvd a \<Longrightarrow> a dvd b \<Longrightarrow> is_unit a"
635   apply (subgoal_tac "a dvd gcd a b")
636   apply simp
637   apply (erule (1) gcd_greatest)
638   done
640 lemma division_decomp:
641   assumes dc: "a dvd b * c"
642   shows "\<exists>b' c'. a = b' * c' \<and> b' dvd b \<and> c' dvd c"
643 proof (cases "gcd a b = 0")
644   assume "gcd a b = 0"
645   hence "a = 0 \<and> b = 0" by simp
646   hence "a = 0 * c \<and> 0 dvd b \<and> c dvd c" by simp
647   then show ?thesis by blast
648 next
649   let ?d = "gcd a b"
650   assume "?d \<noteq> 0"
651   from gcd_coprime_exists[OF this]
652     obtain a' b' where ab': "a = a' * ?d" "b = b' * ?d" "gcd a' b' = 1"
653     by blast
654   from ab'(1) have "a' dvd a" unfolding dvd_def by blast
655   with dc have "a' dvd b*c" using dvd_trans[of a' a "b*c"] by simp
656   from dc ab'(1,2) have "a'*?d dvd (b'*?d) * c" by simp
657   hence "?d * a' dvd ?d * (b' * c)" by (simp add: mult_ac)
658   with \<open>?d \<noteq> 0\<close> have "a' dvd b' * c" by simp
659   with coprime_dvd_mult[OF ab'(3)]
660     have "a' dvd c" by (subst (asm) ac_simps, blast)
661   with ab'(1) have "a = ?d * a' \<and> ?d dvd b \<and> a' dvd c" by (simp add: mult_ac)
662   then show ?thesis by blast
663 qed
665 lemma pow_divs_pow:
666   assumes ab: "a ^ n dvd b ^ n" and n: "n \<noteq> 0"
667   shows "a dvd b"
668 proof (cases "gcd a b = 0")
669   assume "gcd a b = 0"
670   then show ?thesis by simp
671 next
672   let ?d = "gcd a b"
673   assume "?d \<noteq> 0"
674   from n obtain m where m: "n = Suc m" by (cases n, simp_all)
675   from \<open>?d \<noteq> 0\<close> have zn: "?d ^ n \<noteq> 0" by (rule power_not_zero)
676   from gcd_coprime_exists[OF \<open>?d \<noteq> 0\<close>]
677     obtain a' b' where ab': "a = a' * ?d" "b = b' * ?d" "gcd a' b' = 1"
678     by blast
679   from ab have "(a' * ?d) ^ n dvd (b' * ?d) ^ n"
680     by (simp add: ab'(1,2)[symmetric])
681   hence "?d^n * a'^n dvd ?d^n * b'^n"
682     by (simp only: power_mult_distrib ac_simps)
683   with zn have "a'^n dvd b'^n" by simp
684   hence "a' dvd b'^n" using dvd_trans[of a' "a'^n" "b'^n"] by (simp add: m)
685   hence "a' dvd b'^m * b'" by (simp add: m ac_simps)
686   with coprime_dvd_mult[OF coprime_exp[OF ab'(3), of m]]
687     have "a' dvd b'" by (subst (asm) ac_simps, blast)
688   hence "a'*?d dvd b'*?d" by (rule mult_dvd_mono, simp)
689   with ab'(1,2) show ?thesis by simp
690 qed
692 lemma pow_divs_eq [simp]:
693   "n \<noteq> 0 \<Longrightarrow> a ^ n dvd b ^ n \<longleftrightarrow> a dvd b"
694   by (auto intro: pow_divs_pow dvd_power_same)
696 lemma divs_mult:
697   assumes mr: "m dvd r" and nr: "n dvd r" and mn: "gcd m n = 1"
698   shows "m * n dvd r"
699 proof -
700   from mr nr obtain m' n' where m': "r = m*m'" and n': "r = n*n'"
701     unfolding dvd_def by blast
702   from mr n' have "m dvd n'*n" by (simp add: ac_simps)
703   hence "m dvd n'" using coprime_dvd_mult_iff[OF mn] by simp
704   then obtain k where k: "n' = m*k" unfolding dvd_def by blast
705   with n' have "r = m * n * k" by (simp add: mult_ac)
706   then show ?thesis unfolding dvd_def by blast
707 qed
709 lemma coprime_plus_one [simp]: "gcd (n + 1) n = 1"
710   by (subst add_commute, simp)
712 lemma setprod_coprime [rule_format]:
713   "(\<forall>i\<in>A. gcd (f i) a = 1) \<longrightarrow> gcd (\<Prod>i\<in>A. f i) a = 1"
714   apply (cases "finite A")
715   apply (induct set: finite)
716   apply (auto simp add: gcd_mult_cancel)
717   done
719 lemma coprime_divisors:
720   assumes "d dvd a" "e dvd b" "gcd a b = 1"
721   shows "gcd d e = 1"
722 proof -
723   from assms obtain k l where "a = d * k" "b = e * l"
724     unfolding dvd_def by blast
725   with assms have "gcd (d * k) (e * l) = 1" by simp
726   hence "gcd (d * k) e = 1" by (rule coprime_lmult)
727   also have "gcd (d * k) e = gcd e (d * k)" by (simp add: ac_simps)
728   finally have "gcd e d = 1" by (rule coprime_lmult)
729   then show ?thesis by (simp add: ac_simps)
730 qed
732 lemma invertible_coprime:
733   assumes "a * b mod m = 1"
734   shows "coprime a m"
735 proof -
736   from assms have "coprime m (a * b mod m)"
737     by simp
738   then have "coprime m (a * b)"
739     by simp
740   then have "coprime m a"
741     by (rule coprime_lmult)
742   then show ?thesis
743     by (simp add: ac_simps)
744 qed
746 lemma lcm_gcd:
747   "lcm a b = a * b div (gcd a b * normalization_factor (a*b))"
748   by (simp only: lcm_lcm_eucl gcd_gcd_eucl lcm_eucl_def)
750 lemma lcm_gcd_prod:
751   "lcm a b * gcd a b = a * b div normalization_factor (a*b)"
752 proof (cases "a * b = 0")
753   let ?nf = normalization_factor
754   assume "a * b \<noteq> 0"
755   hence "gcd a b \<noteq> 0" by simp
756   from lcm_gcd have "lcm a b * gcd a b = gcd a b * (a * b div (?nf (a*b) * gcd a b))"
757     by (simp add: mult_ac)
758   also from \<open>a * b \<noteq> 0\<close> have "... = a * b div ?nf (a*b)"
759     by (simp add: div_mult_swap mult.commute)
760   finally show ?thesis .
761 qed (auto simp add: lcm_gcd)
763 lemma lcm_dvd1 [iff]:
764   "a dvd lcm a b"
765 proof (cases "a*b = 0")
766   assume "a * b \<noteq> 0"
767   hence "gcd a b \<noteq> 0" by simp
768   let ?c = "1 div normalization_factor (a * b)"
769   from \<open>a * b \<noteq> 0\<close> have [simp]: "is_unit (normalization_factor (a * b))" by simp
770   from lcm_gcd_prod[of a b] have "lcm a b * gcd a b = a * ?c * b"
771     by (simp add: div_mult_swap unit_div_commute)
772   hence "lcm a b * gcd a b div gcd a b = a * ?c * b div gcd a b" by simp
773   with \<open>gcd a b \<noteq> 0\<close> have "lcm a b = a * ?c * b div gcd a b"
774     by (subst (asm) div_mult_self2_is_id, simp_all)
775   also have "... = a * (?c * b div gcd a b)"
776     by (metis div_mult_swap gcd_dvd2 mult_assoc)
777   finally show ?thesis by (rule dvdI)
778 qed (auto simp add: lcm_gcd)
780 lemma lcm_least:
781   "\<lbrakk>a dvd k; b dvd k\<rbrakk> \<Longrightarrow> lcm a b dvd k"
782 proof (cases "k = 0")
783   let ?nf = normalization_factor
784   assume "k \<noteq> 0"
785   hence "is_unit (?nf k)" by simp
786   hence "?nf k \<noteq> 0" by (metis not_is_unit_0)
787   assume A: "a dvd k" "b dvd k"
788   hence "gcd a b \<noteq> 0" using \<open>k \<noteq> 0\<close> by auto
789   from A obtain r s where ar: "k = a * r" and bs: "k = b * s"
790     unfolding dvd_def by blast
791   with \<open>k \<noteq> 0\<close> have "r * s \<noteq> 0"
792     by auto (drule sym [of 0], simp)
793   hence "is_unit (?nf (r * s))" by simp
794   let ?c = "?nf k div ?nf (r*s)"
795   from \<open>is_unit (?nf k)\<close> and \<open>is_unit (?nf (r * s))\<close> have "is_unit ?c" by (rule unit_div)
796   hence "?c \<noteq> 0" using not_is_unit_0 by fast
797   from ar bs have "k * k * gcd s r = ?nf k * k * gcd (k * s) (k * r)"
798     by (subst mult_assoc, subst gcd_mult_distrib[of k s r], simp only: ac_simps)
799   also have "... = ?nf k * k * gcd ((r*s) * a) ((r*s) * b)"
800     by (subst (3) \<open>k = a * r\<close>, subst (3) \<open>k = b * s\<close>, simp add: algebra_simps)
801   also have "... = ?c * r*s * k * gcd a b" using \<open>r * s \<noteq> 0\<close>
802     by (subst gcd_mult_distrib'[symmetric], simp add: algebra_simps unit_simps)
803   finally have "(a*r) * (b*s) * gcd s r = ?c * k * r * s * gcd a b"
804     by (subst ar[symmetric], subst bs[symmetric], simp add: mult_ac)
805   hence "a * b * gcd s r * (r * s) = ?c * k * gcd a b * (r * s)"
806     by (simp add: algebra_simps)
807   hence "?c * k * gcd a b = a * b * gcd s r" using \<open>r * s \<noteq> 0\<close>
808     by (metis div_mult_self2_is_id)
809   also have "... = lcm a b * gcd a b * gcd s r * ?nf (a*b)"
810     by (subst lcm_gcd_prod[of a b], metis gcd_mult_distrib gcd_mult_distrib')
811   also have "... = lcm a b * gcd s r * ?nf (a*b) * gcd a b"
812     by (simp add: algebra_simps)
813   finally have "k * ?c = lcm a b * gcd s r * ?nf (a*b)" using \<open>gcd a b \<noteq> 0\<close>
814     by (metis mult.commute div_mult_self2_is_id)
815   hence "k = lcm a b * (gcd s r * ?nf (a*b)) div ?c" using \<open>?c \<noteq> 0\<close>
816     by (metis div_mult_self2_is_id mult_assoc)
817   also have "... = lcm a b * (gcd s r * ?nf (a*b) div ?c)" using \<open>is_unit ?c\<close>
818     by (simp add: unit_simps)
819   finally show ?thesis by (rule dvdI)
820 qed simp
822 lemma lcm_zero:
823   "lcm a b = 0 \<longleftrightarrow> a = 0 \<or> b = 0"
824 proof -
825   let ?nf = normalization_factor
826   {
827     assume "a \<noteq> 0" "b \<noteq> 0"
828     hence "a * b div ?nf (a * b) \<noteq> 0" by (simp add: no_zero_divisors)
829     moreover from \<open>a \<noteq> 0\<close> and \<open>b \<noteq> 0\<close> have "gcd a b \<noteq> 0" by simp
830     ultimately have "lcm a b \<noteq> 0" using lcm_gcd_prod[of a b] by (intro notI, simp)
831   } moreover {
832     assume "a = 0 \<or> b = 0"
833     hence "lcm a b = 0" by (elim disjE, simp_all add: lcm_gcd)
834   }
835   ultimately show ?thesis by blast
836 qed
838 lemmas lcm_0_iff = lcm_zero
840 lemma gcd_lcm:
841   assumes "lcm a b \<noteq> 0"
842   shows "gcd a b = a * b div (lcm a b * normalization_factor (a * b))"
843 proof-
844   from assms have "gcd a b \<noteq> 0" by (simp add: lcm_zero)
845   let ?c = "normalization_factor (a * b)"
846   from \<open>lcm a b \<noteq> 0\<close> have "?c \<noteq> 0" by (intro notI, simp add: lcm_zero no_zero_divisors)
847   hence "is_unit ?c" by simp
848   from lcm_gcd_prod [of a b] have "gcd a b = a * b div ?c div lcm a b"
849     by (subst (2) div_mult_self2_is_id[OF \<open>lcm a b \<noteq> 0\<close>, symmetric], simp add: mult_ac)
850   also from \<open>is_unit ?c\<close> have "... = a * b div (lcm a b * ?c)"
851     by (metis \<open>?c \<noteq> 0\<close> div_mult_mult1 dvd_mult_div_cancel mult_commute normalization_factor_dvd')
852   finally show ?thesis .
853 qed
855 lemma normalization_factor_lcm [simp]:
856   "normalization_factor (lcm a b) = (if a = 0 \<or> b = 0 then 0 else 1)"
857 proof (cases "a = 0 \<or> b = 0")
858   case True then show ?thesis
859     by (auto simp add: lcm_gcd)
860 next
861   case False
862   let ?nf = normalization_factor
863   from lcm_gcd_prod[of a b]
864     have "?nf (lcm a b) * ?nf (gcd a b) = ?nf (a*b) div ?nf (a*b)"
865     by (metis div_by_0 div_self normalization_correct normalization_factor_0 normalization_factor_mult)
866   also have "... = (if a*b = 0 then 0 else 1)"
867     by simp
868   finally show ?thesis using False by simp
869 qed
871 lemma lcm_dvd2 [iff]: "b dvd lcm a b"
872   using lcm_dvd1 [of b a] by (simp add: lcm_gcd ac_simps)
874 lemma lcmI:
875   "\<lbrakk>a dvd k; b dvd k; \<And>l. a dvd l \<Longrightarrow> b dvd l \<Longrightarrow> k dvd l;
876     normalization_factor k = (if k = 0 then 0 else 1)\<rbrakk> \<Longrightarrow> k = lcm a b"
877   by (intro normed_associated_imp_eq) (auto simp: associated_def intro: lcm_least)
879 sublocale lcm!: abel_semigroup lcm
880 proof
881   fix a b c
882   show "lcm (lcm a b) c = lcm a (lcm b c)"
883   proof (rule lcmI)
884     have "a dvd lcm a b" and "lcm a b dvd lcm (lcm a b) c" by simp_all
885     then show "a dvd lcm (lcm a b) c" by (rule dvd_trans)
887     have "b dvd lcm a b" and "lcm a b dvd lcm (lcm a b) c" by simp_all
888     hence "b dvd lcm (lcm a b) c" by (rule dvd_trans)
889     moreover have "c dvd lcm (lcm a b) c" by simp
890     ultimately show "lcm b c dvd lcm (lcm a b) c" by (rule lcm_least)
892     fix l assume "a dvd l" and "lcm b c dvd l"
893     have "b dvd lcm b c" by simp
894     from this and \<open>lcm b c dvd l\<close> have "b dvd l" by (rule dvd_trans)
895     have "c dvd lcm b c" by simp
896     from this and \<open>lcm b c dvd l\<close> have "c dvd l" by (rule dvd_trans)
897     from \<open>a dvd l\<close> and \<open>b dvd l\<close> have "lcm a b dvd l" by (rule lcm_least)
898     from this and \<open>c dvd l\<close> show "lcm (lcm a b) c dvd l" by (rule lcm_least)
899   qed (simp add: lcm_zero)
900 next
901   fix a b
902   show "lcm a b = lcm b a"
903     by (simp add: lcm_gcd ac_simps)
904 qed
906 lemma dvd_lcm_D1:
907   "lcm m n dvd k \<Longrightarrow> m dvd k"
908   by (rule dvd_trans, rule lcm_dvd1, assumption)
910 lemma dvd_lcm_D2:
911   "lcm m n dvd k \<Longrightarrow> n dvd k"
912   by (rule dvd_trans, rule lcm_dvd2, assumption)
914 lemma gcd_dvd_lcm [simp]:
915   "gcd a b dvd lcm a b"
916   by (metis dvd_trans gcd_dvd2 lcm_dvd2)
918 lemma lcm_1_iff:
919   "lcm a b = 1 \<longleftrightarrow> is_unit a \<and> is_unit b"
920 proof
921   assume "lcm a b = 1"
922   then show "is_unit a \<and> is_unit b" by auto
923 next
924   assume "is_unit a \<and> is_unit b"
925   hence "a dvd 1" and "b dvd 1" by simp_all
926   hence "is_unit (lcm a b)" by (rule lcm_least)
927   hence "lcm a b = normalization_factor (lcm a b)"
928     by (subst normalization_factor_unit, simp_all)
929   also have "\<dots> = 1" using \<open>is_unit a \<and> is_unit b\<close>
930     by auto
931   finally show "lcm a b = 1" .
932 qed
934 lemma lcm_0_left [simp]:
935   "lcm 0 a = 0"
936   by (rule sym, rule lcmI, simp_all)
938 lemma lcm_0 [simp]:
939   "lcm a 0 = 0"
940   by (rule sym, rule lcmI, simp_all)
942 lemma lcm_unique:
943   "a dvd d \<and> b dvd d \<and>
944   normalization_factor d = (if d = 0 then 0 else 1) \<and>
945   (\<forall>e. a dvd e \<and> b dvd e \<longrightarrow> d dvd e) \<longleftrightarrow> d = lcm a b"
946   by (rule, auto intro: lcmI simp: lcm_least lcm_zero)
948 lemma dvd_lcm_I1 [simp]:
949   "k dvd m \<Longrightarrow> k dvd lcm m n"
950   by (metis lcm_dvd1 dvd_trans)
952 lemma dvd_lcm_I2 [simp]:
953   "k dvd n \<Longrightarrow> k dvd lcm m n"
954   by (metis lcm_dvd2 dvd_trans)
956 lemma lcm_1_left [simp]:
957   "lcm 1 a = a div normalization_factor a"
958   by (cases "a = 0") (simp, rule sym, rule lcmI, simp_all)
960 lemma lcm_1_right [simp]:
961   "lcm a 1 = a div normalization_factor a"
962   using lcm_1_left [of a] by (simp add: ac_simps)
964 lemma lcm_coprime:
965   "gcd a b = 1 \<Longrightarrow> lcm a b = a * b div normalization_factor (a*b)"
966   by (subst lcm_gcd) simp
968 lemma lcm_proj1_if_dvd:
969   "b dvd a \<Longrightarrow> lcm a b = a div normalization_factor a"
970   by (cases "a = 0") (simp, rule sym, rule lcmI, simp_all)
972 lemma lcm_proj2_if_dvd:
973   "a dvd b \<Longrightarrow> lcm a b = b div normalization_factor b"
974   using lcm_proj1_if_dvd [of a b] by (simp add: ac_simps)
976 lemma lcm_proj1_iff:
977   "lcm m n = m div normalization_factor m \<longleftrightarrow> n dvd m"
978 proof
979   assume A: "lcm m n = m div normalization_factor m"
980   show "n dvd m"
981   proof (cases "m = 0")
982     assume [simp]: "m \<noteq> 0"
983     from A have B: "m = lcm m n * normalization_factor m"
984       by (simp add: unit_eq_div2)
985     show ?thesis by (subst B, simp)
986   qed simp
987 next
988   assume "n dvd m"
989   then show "lcm m n = m div normalization_factor m" by (rule lcm_proj1_if_dvd)
990 qed
992 lemma lcm_proj2_iff:
993   "lcm m n = n div normalization_factor n \<longleftrightarrow> m dvd n"
994   using lcm_proj1_iff [of n m] by (simp add: ac_simps)
996 lemma euclidean_size_lcm_le1:
997   assumes "a \<noteq> 0" and "b \<noteq> 0"
998   shows "euclidean_size a \<le> euclidean_size (lcm a b)"
999 proof -
1000   have "a dvd lcm a b" by (rule lcm_dvd1)
1001   then obtain c where A: "lcm a b = a * c" unfolding dvd_def by blast
1002   with \<open>a \<noteq> 0\<close> and \<open>b \<noteq> 0\<close> have "c \<noteq> 0" by (auto simp: lcm_zero)
1003   then show ?thesis by (subst A, intro size_mult_mono)
1004 qed
1006 lemma euclidean_size_lcm_le2:
1007   "a \<noteq> 0 \<Longrightarrow> b \<noteq> 0 \<Longrightarrow> euclidean_size b \<le> euclidean_size (lcm a b)"
1008   using euclidean_size_lcm_le1 [of b a] by (simp add: ac_simps)
1010 lemma euclidean_size_lcm_less1:
1011   assumes "b \<noteq> 0" and "\<not>b dvd a"
1012   shows "euclidean_size a < euclidean_size (lcm a b)"
1013 proof (rule ccontr)
1014   from assms have "a \<noteq> 0" by auto
1015   assume "\<not>euclidean_size a < euclidean_size (lcm a b)"
1016   with \<open>a \<noteq> 0\<close> and \<open>b \<noteq> 0\<close> have "euclidean_size (lcm a b) = euclidean_size a"
1017     by (intro le_antisym, simp, intro euclidean_size_lcm_le1)
1018   with assms have "lcm a b dvd a"
1019     by (rule_tac dvd_euclidean_size_eq_imp_dvd) (auto simp: lcm_zero)
1020   hence "b dvd a" by (rule dvd_lcm_D2)
1021   with \<open>\<not>b dvd a\<close> show False by contradiction
1022 qed
1024 lemma euclidean_size_lcm_less2:
1025   assumes "a \<noteq> 0" and "\<not>a dvd b"
1026   shows "euclidean_size b < euclidean_size (lcm a b)"
1027   using assms euclidean_size_lcm_less1 [of a b] by (simp add: ac_simps)
1029 lemma lcm_mult_unit1:
1030   "is_unit a \<Longrightarrow> lcm (b * a) c = lcm b c"
1031   apply (rule lcmI)
1032   apply (rule dvd_trans[of _ "b * a"], simp, rule lcm_dvd1)
1033   apply (rule lcm_dvd2)
1034   apply (rule lcm_least, simp add: unit_simps, assumption)
1035   apply (subst normalization_factor_lcm, simp add: lcm_zero)
1036   done
1038 lemma lcm_mult_unit2:
1039   "is_unit a \<Longrightarrow> lcm b (c * a) = lcm b c"
1040   using lcm_mult_unit1 [of a c b] by (simp add: ac_simps)
1042 lemma lcm_div_unit1:
1043   "is_unit a \<Longrightarrow> lcm (b div a) c = lcm b c"
1044   by (erule is_unitE [of _ b]) (simp add: lcm_mult_unit1)
1046 lemma lcm_div_unit2:
1047   "is_unit a \<Longrightarrow> lcm b (c div a) = lcm b c"
1048   by (erule is_unitE [of _ c]) (simp add: lcm_mult_unit2)
1050 lemma lcm_left_idem:
1051   "lcm a (lcm a b) = lcm a b"
1052   apply (rule lcmI)
1053   apply simp
1054   apply (subst lcm.assoc [symmetric], rule lcm_dvd2)
1055   apply (rule lcm_least, assumption)
1056   apply (erule (1) lcm_least)
1057   apply (auto simp: lcm_zero)
1058   done
1060 lemma lcm_right_idem:
1061   "lcm (lcm a b) b = lcm a b"
1062   apply (rule lcmI)
1063   apply (subst lcm.assoc, rule lcm_dvd1)
1064   apply (rule lcm_dvd2)
1065   apply (rule lcm_least, erule (1) lcm_least, assumption)
1066   apply (auto simp: lcm_zero)
1067   done
1069 lemma comp_fun_idem_lcm: "comp_fun_idem lcm"
1070 proof
1071   fix a b show "lcm a \<circ> lcm b = lcm b \<circ> lcm a"
1072     by (simp add: fun_eq_iff ac_simps)
1073 next
1074   fix a show "lcm a \<circ> lcm a = lcm a" unfolding o_def
1075     by (intro ext, simp add: lcm_left_idem)
1076 qed
1078 lemma dvd_Lcm [simp]: "a \<in> A \<Longrightarrow> a dvd Lcm A"
1079   and Lcm_dvd [simp]: "(\<forall>a\<in>A. a dvd l') \<Longrightarrow> Lcm A dvd l'"
1080   and normalization_factor_Lcm [simp]:
1081           "normalization_factor (Lcm A) = (if Lcm A = 0 then 0 else 1)"
1082 proof -
1083   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>
1084     normalization_factor (Lcm A) = (if Lcm A = 0 then 0 else 1)" (is ?thesis)
1085   proof (cases "\<exists>l. l \<noteq>  0 \<and> (\<forall>a\<in>A. a dvd l)")
1086     case False
1087     hence "Lcm A = 0" by (auto simp: Lcm_Lcm_eucl Lcm_eucl_def)
1088     with False show ?thesis by auto
1089   next
1090     case True
1091     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
1092     def n \<equiv> "LEAST n. \<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n"
1093     def l \<equiv> "SOME l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n"
1094     have "\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n"
1095       apply (subst n_def)
1096       apply (rule LeastI[of _ "euclidean_size l\<^sub>0"])
1097       apply (rule exI[of _ l\<^sub>0])
1098       apply (simp add: l\<^sub>0_props)
1099       done
1100     from someI_ex[OF this] have "l \<noteq> 0" and "\<forall>a\<in>A. a dvd l" and "euclidean_size l = n"
1101       unfolding l_def by simp_all
1102     {
1103       fix l' assume "\<forall>a\<in>A. a dvd l'"
1104       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)
1105       moreover from \<open>l \<noteq> 0\<close> have "gcd l l' \<noteq> 0" by simp
1106       ultimately have "\<exists>b. b \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd b) \<and> euclidean_size b = euclidean_size (gcd l l')"
1107         by (intro exI[of _ "gcd l l'"], auto)
1108       hence "euclidean_size (gcd l l') \<ge> n" by (subst n_def) (rule Least_le)
1109       moreover have "euclidean_size (gcd l l') \<le> n"
1110       proof -
1111         have "gcd l l' dvd l" by simp
1112         then obtain a where "l = gcd l l' * a" unfolding dvd_def by blast
1113         with \<open>l \<noteq> 0\<close> have "a \<noteq> 0" by auto
1114         hence "euclidean_size (gcd l l') \<le> euclidean_size (gcd l l' * a)"
1115           by (rule size_mult_mono)
1116         also have "gcd l l' * a = l" using \<open>l = gcd l l' * a\<close> ..
1117         also note \<open>euclidean_size l = n\<close>
1118         finally show "euclidean_size (gcd l l') \<le> n" .
1119       qed
1120       ultimately have "euclidean_size l = euclidean_size (gcd l l')"
1121         by (intro le_antisym, simp_all add: \<open>euclidean_size l = n\<close>)
1122       with \<open>l \<noteq> 0\<close> have "l dvd gcd l l'" by (blast intro: dvd_euclidean_size_eq_imp_dvd)
1123       hence "l dvd l'" by (blast dest: dvd_gcd_D2)
1124     }
1126     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>
1127       have "(\<forall>a\<in>A. a dvd l div normalization_factor l) \<and>
1128         (\<forall>l'. (\<forall>a\<in>A. a dvd l') \<longrightarrow> l div normalization_factor l dvd l') \<and>
1129         normalization_factor (l div normalization_factor l) =
1130         (if l div normalization_factor l = 0 then 0 else 1)"
1131       by (auto simp: unit_simps)
1132     also from True have "l div normalization_factor l = Lcm A"
1133       by (simp add: Lcm_Lcm_eucl Lcm_eucl_def Let_def n_def l_def)
1134     finally show ?thesis .
1135   qed
1136   note A = this
1138   {fix a assume "a \<in> A" then show "a dvd Lcm A" using A by blast}
1139   {fix l' assume "\<forall>a\<in>A. a dvd l'" then show "Lcm A dvd l'" using A by blast}
1140   from A show "normalization_factor (Lcm A) = (if Lcm A = 0 then 0 else 1)" by blast
1141 qed
1143 lemma LcmI:
1144   "(\<And>a. a\<in>A \<Longrightarrow> a dvd l) \<Longrightarrow> (\<And>l'. (\<forall>a\<in>A. a dvd l') \<Longrightarrow> l dvd l') \<Longrightarrow>
1145       normalization_factor l = (if l = 0 then 0 else 1) \<Longrightarrow> l = Lcm A"
1146   by (intro normed_associated_imp_eq)
1147     (auto intro: Lcm_dvd dvd_Lcm simp: associated_def)
1149 lemma Lcm_subset:
1150   "A \<subseteq> B \<Longrightarrow> Lcm A dvd Lcm B"
1151   by (blast intro: Lcm_dvd dvd_Lcm)
1153 lemma Lcm_Un:
1154   "Lcm (A \<union> B) = lcm (Lcm A) (Lcm B)"
1155   apply (rule lcmI)
1156   apply (blast intro: Lcm_subset)
1157   apply (blast intro: Lcm_subset)
1158   apply (intro Lcm_dvd ballI, elim UnE)
1159   apply (rule dvd_trans, erule dvd_Lcm, assumption)
1160   apply (rule dvd_trans, erule dvd_Lcm, assumption)
1161   apply simp
1162   done
1164 lemma Lcm_1_iff:
1165   "Lcm A = 1 \<longleftrightarrow> (\<forall>a\<in>A. is_unit a)"
1166 proof
1167   assume "Lcm A = 1"
1168   then show "\<forall>a\<in>A. is_unit a" by auto
1169 qed (rule LcmI [symmetric], auto)
1171 lemma Lcm_no_units:
1172   "Lcm A = Lcm (A - {a. is_unit a})"
1173 proof -
1174   have "(A - {a. is_unit a}) \<union> {a\<in>A. is_unit a} = A" by blast
1175   hence "Lcm A = lcm (Lcm (A - {a. is_unit a})) (Lcm {a\<in>A. is_unit a})"
1176     by (simp add: Lcm_Un[symmetric])
1177   also have "Lcm {a\<in>A. is_unit a} = 1" by (simp add: Lcm_1_iff)
1178   finally show ?thesis by simp
1179 qed
1181 lemma Lcm_empty [simp]:
1182   "Lcm {} = 1"
1183   by (simp add: Lcm_1_iff)
1185 lemma Lcm_eq_0 [simp]:
1186   "0 \<in> A \<Longrightarrow> Lcm A = 0"
1187   by (drule dvd_Lcm) simp
1189 lemma Lcm0_iff':
1190   "Lcm A = 0 \<longleftrightarrow> \<not>(\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l))"
1191 proof
1192   assume "Lcm A = 0"
1193   show "\<not>(\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l))"
1194   proof
1195     assume ex: "\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l)"
1196     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
1197     def n \<equiv> "LEAST n. \<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n"
1198     def l \<equiv> "SOME l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n"
1199     have "\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n"
1200       apply (subst n_def)
1201       apply (rule LeastI[of _ "euclidean_size l\<^sub>0"])
1202       apply (rule exI[of _ l\<^sub>0])
1203       apply (simp add: l\<^sub>0_props)
1204       done
1205     from someI_ex[OF this] have "l \<noteq> 0" unfolding l_def by simp_all
1206     hence "l div normalization_factor l \<noteq> 0" by simp
1207     also from ex have "l div normalization_factor l = Lcm A"
1208        by (simp only: Lcm_Lcm_eucl Lcm_eucl_def n_def l_def if_True Let_def)
1209     finally show False using \<open>Lcm A = 0\<close> by contradiction
1210   qed
1211 qed (simp only: Lcm_Lcm_eucl Lcm_eucl_def if_False)
1213 lemma Lcm0_iff [simp]:
1214   "finite A \<Longrightarrow> Lcm A = 0 \<longleftrightarrow> 0 \<in> A"
1215 proof -
1216   assume "finite A"
1217   have "0 \<in> A \<Longrightarrow> Lcm A = 0"  by (intro dvd_0_left dvd_Lcm)
1218   moreover {
1219     assume "0 \<notin> A"
1220     hence "\<Prod>A \<noteq> 0"
1221       apply (induct rule: finite_induct[OF \<open>finite A\<close>])
1222       apply simp
1223       apply (subst setprod.insert, assumption, assumption)
1224       apply (rule no_zero_divisors)
1225       apply blast+
1226       done
1227     moreover from \<open>finite A\<close> have "\<forall>a\<in>A. a dvd \<Prod>A" by blast
1228     ultimately have "\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l)" by blast
1229     with Lcm0_iff' have "Lcm A \<noteq> 0" by simp
1230   }
1231   ultimately show "Lcm A = 0 \<longleftrightarrow> 0 \<in> A" by blast
1232 qed
1234 lemma Lcm_no_multiple:
1235   "(\<forall>m. m \<noteq> 0 \<longrightarrow> (\<exists>a\<in>A. \<not>a dvd m)) \<Longrightarrow> Lcm A = 0"
1236 proof -
1237   assume "\<forall>m. m \<noteq> 0 \<longrightarrow> (\<exists>a\<in>A. \<not>a dvd m)"
1238   hence "\<not>(\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l))" by blast
1239   then show "Lcm A = 0" by (simp only: Lcm_Lcm_eucl Lcm_eucl_def if_False)
1240 qed
1242 lemma Lcm_insert [simp]:
1243   "Lcm (insert a A) = lcm a (Lcm A)"
1244 proof (rule lcmI)
1245   fix l assume "a dvd l" and "Lcm A dvd l"
1246   hence "\<forall>a\<in>A. a dvd l" by (blast intro: dvd_trans dvd_Lcm)
1247   with \<open>a dvd l\<close> show "Lcm (insert a A) dvd l" by (force intro: Lcm_dvd)
1248 qed (auto intro: Lcm_dvd dvd_Lcm)
1250 lemma Lcm_finite:
1251   assumes "finite A"
1252   shows "Lcm A = Finite_Set.fold lcm 1 A"
1253   by (induct rule: finite.induct[OF \<open>finite A\<close>])
1254     (simp_all add: comp_fun_idem.fold_insert_idem[OF comp_fun_idem_lcm])
1256 lemma Lcm_set [code_unfold]:
1257   "Lcm (set xs) = fold lcm xs 1"
1258   using comp_fun_idem.fold_set_fold[OF comp_fun_idem_lcm] Lcm_finite by (simp add: ac_simps)
1260 lemma Lcm_singleton [simp]:
1261   "Lcm {a} = a div normalization_factor a"
1262   by simp
1264 lemma Lcm_2 [simp]:
1265   "Lcm {a,b} = lcm a b"
1266   by (simp only: Lcm_insert Lcm_empty lcm_1_right)
1267     (cases "b = 0", simp, rule lcm_div_unit2, simp)
1269 lemma Lcm_coprime:
1270   assumes "finite A" and "A \<noteq> {}"
1271   assumes "\<And>a b. a \<in> A \<Longrightarrow> b \<in> A \<Longrightarrow> a \<noteq> b \<Longrightarrow> gcd a b = 1"
1272   shows "Lcm A = \<Prod>A div normalization_factor (\<Prod>A)"
1273 using assms proof (induct rule: finite_ne_induct)
1274   case (insert a A)
1275   have "Lcm (insert a A) = lcm a (Lcm A)" by simp
1276   also from insert have "Lcm A = \<Prod>A div normalization_factor (\<Prod>A)" by blast
1277   also have "lcm a \<dots> = lcm a (\<Prod>A)" by (cases "\<Prod>A = 0") (simp_all add: lcm_div_unit2)
1278   also from insert have "gcd a (\<Prod>A) = 1" by (subst gcd.commute, intro setprod_coprime) auto
1279   with insert have "lcm a (\<Prod>A) = \<Prod>(insert a A) div normalization_factor (\<Prod>(insert a A))"
1280     by (simp add: lcm_coprime)
1281   finally show ?case .
1282 qed simp
1284 lemma Lcm_coprime':
1285   "card A \<noteq> 0 \<Longrightarrow> (\<And>a b. a \<in> A \<Longrightarrow> b \<in> A \<Longrightarrow> a \<noteq> b \<Longrightarrow> gcd a b = 1)
1286     \<Longrightarrow> Lcm A = \<Prod>A div normalization_factor (\<Prod>A)"
1287   by (rule Lcm_coprime) (simp_all add: card_eq_0_iff)
1289 lemma Gcd_Lcm:
1290   "Gcd A = Lcm {d. \<forall>a\<in>A. d dvd a}"
1291   by (simp add: Gcd_Gcd_eucl Lcm_Lcm_eucl Gcd_eucl_def)
1293 lemma Gcd_dvd [simp]: "a \<in> A \<Longrightarrow> Gcd A dvd a"
1294   and dvd_Gcd [simp]: "(\<forall>a\<in>A. g' dvd a) \<Longrightarrow> g' dvd Gcd A"
1295   and normalization_factor_Gcd [simp]:
1296     "normalization_factor (Gcd A) = (if Gcd A = 0 then 0 else 1)"
1297 proof -
1298   fix a assume "a \<in> A"
1299   hence "Lcm {d. \<forall>a\<in>A. d dvd a} dvd a" by (intro Lcm_dvd) blast
1300   then show "Gcd A dvd a" by (simp add: Gcd_Lcm)
1301 next
1302   fix g' assume "\<forall>a\<in>A. g' dvd a"
1303   hence "g' dvd Lcm {d. \<forall>a\<in>A. d dvd a}" by (intro dvd_Lcm) blast
1304   then show "g' dvd Gcd A" by (simp add: Gcd_Lcm)
1305 next
1306   show "normalization_factor (Gcd A) = (if Gcd A = 0 then 0 else 1)"
1307     by (simp add: Gcd_Lcm)
1308 qed
1310 lemma GcdI:
1311   "(\<And>a. a\<in>A \<Longrightarrow> l dvd a) \<Longrightarrow> (\<And>l'. (\<forall>a\<in>A. l' dvd a) \<Longrightarrow> l' dvd l) \<Longrightarrow>
1312     normalization_factor l = (if l = 0 then 0 else 1) \<Longrightarrow> l = Gcd A"
1313   by (intro normed_associated_imp_eq)
1314     (auto intro: Gcd_dvd dvd_Gcd simp: associated_def)
1316 lemma Lcm_Gcd:
1317   "Lcm A = Gcd {m. \<forall>a\<in>A. a dvd m}"
1318   by (rule LcmI[symmetric]) (auto intro: dvd_Gcd Gcd_dvd)
1320 lemma Gcd_0_iff:
1321   "Gcd A = 0 \<longleftrightarrow> A \<subseteq> {0}"
1322   apply (rule iffI)
1323   apply (rule subsetI, drule Gcd_dvd, simp)
1324   apply (auto intro: GcdI[symmetric])
1325   done
1327 lemma Gcd_empty [simp]:
1328   "Gcd {} = 0"
1329   by (simp add: Gcd_0_iff)
1331 lemma Gcd_1:
1332   "1 \<in> A \<Longrightarrow> Gcd A = 1"
1333   by (intro GcdI[symmetric]) (auto intro: Gcd_dvd dvd_Gcd)
1335 lemma Gcd_insert [simp]:
1336   "Gcd (insert a A) = gcd a (Gcd A)"
1337 proof (rule gcdI)
1338   fix l assume "l dvd a" and "l dvd Gcd A"
1339   hence "\<forall>a\<in>A. l dvd a" by (blast intro: dvd_trans Gcd_dvd)
1340   with \<open>l dvd a\<close> show "l dvd Gcd (insert a A)" by (force intro: Gcd_dvd)
1341 qed auto
1343 lemma Gcd_finite:
1344   assumes "finite A"
1345   shows "Gcd A = Finite_Set.fold gcd 0 A"
1346   by (induct rule: finite.induct[OF \<open>finite A\<close>])
1347     (simp_all add: comp_fun_idem.fold_insert_idem[OF comp_fun_idem_gcd])
1349 lemma Gcd_set [code_unfold]:
1350   "Gcd (set xs) = fold gcd xs 0"
1351   using comp_fun_idem.fold_set_fold[OF comp_fun_idem_gcd] Gcd_finite by (simp add: ac_simps)
1353 lemma Gcd_singleton [simp]: "Gcd {a} = a div normalization_factor a"
1354   by (simp add: gcd_0)
1356 lemma Gcd_2 [simp]: "Gcd {a,b} = gcd a b"
1357   by (simp only: Gcd_insert Gcd_empty gcd_0) (cases "b = 0", simp, rule gcd_div_unit2, simp)
1359 subclass semiring_gcd
1360   by unfold_locales (simp_all add: gcd_greatest_iff)
1362 end
1364 text \<open>
1365   A Euclidean ring is a Euclidean semiring with additive inverses. It provides a
1366   few more lemmas; in particular, Bezout's lemma holds for any Euclidean ring.
1367 \<close>
1369 class euclidean_ring = euclidean_semiring + idom
1371 class euclidean_ring_gcd = euclidean_semiring_gcd + idom
1372 begin
1374 subclass euclidean_ring ..
1376 subclass ring_gcd ..
1378 lemma gcd_neg1 [simp]:
1379   "gcd (-a) b = gcd a b"
1380   by (rule sym, rule gcdI, simp_all add: gcd_greatest)
1382 lemma gcd_neg2 [simp]:
1383   "gcd a (-b) = gcd a b"
1384   by (rule sym, rule gcdI, simp_all add: gcd_greatest)
1386 lemma gcd_neg_numeral_1 [simp]:
1387   "gcd (- numeral n) a = gcd (numeral n) a"
1388   by (fact gcd_neg1)
1390 lemma gcd_neg_numeral_2 [simp]:
1391   "gcd a (- numeral n) = gcd a (numeral n)"
1392   by (fact gcd_neg2)
1394 lemma gcd_diff1: "gcd (m - n) n = gcd m n"
1395   by (subst diff_conv_add_uminus, subst gcd_neg2[symmetric],  subst gcd_add1, simp)
1397 lemma gcd_diff2: "gcd (n - m) n = gcd m n"
1398   by (subst gcd_neg1[symmetric], simp only: minus_diff_eq gcd_diff1)
1400 lemma coprime_minus_one [simp]: "gcd (n - 1) n = 1"
1401 proof -
1402   have "gcd (n - 1) n = gcd n (n - 1)" by (fact gcd.commute)
1403   also have "\<dots> = gcd ((n - 1) + 1) (n - 1)" by simp
1404   also have "\<dots> = 1" by (rule coprime_plus_one)
1405   finally show ?thesis .
1406 qed
1408 lemma lcm_neg1 [simp]: "lcm (-a) b = lcm a b"
1409   by (rule sym, rule lcmI, simp_all add: lcm_least lcm_zero)
1411 lemma lcm_neg2 [simp]: "lcm a (-b) = lcm a b"
1412   by (rule sym, rule lcmI, simp_all add: lcm_least lcm_zero)
1414 lemma lcm_neg_numeral_1 [simp]: "lcm (- numeral n) a = lcm (numeral n) a"
1415   by (fact lcm_neg1)
1417 lemma lcm_neg_numeral_2 [simp]: "lcm a (- numeral n) = lcm a (numeral n)"
1418   by (fact lcm_neg2)
1420 function euclid_ext :: "'a \<Rightarrow> 'a \<Rightarrow> 'a \<times> 'a \<times> 'a" where
1421   "euclid_ext a b =
1422      (if b = 0 then
1423         let c = 1 div normalization_factor a in (c, 0, a * c)
1424       else
1425         case euclid_ext b (a mod b) of
1426             (s,t,c) \<Rightarrow> (t, s - t * (a div b), c))"
1427   by (pat_completeness, simp)
1428   termination by (relation "measure (euclidean_size \<circ> snd)", simp_all)
1430 declare euclid_ext.simps [simp del]
1432 lemma euclid_ext_0:
1433   "euclid_ext a 0 = (1 div normalization_factor a, 0, a div normalization_factor a)"
1434   by (subst euclid_ext.simps) (simp add: Let_def)
1436 lemma euclid_ext_non_0:
1437   "b \<noteq> 0 \<Longrightarrow> euclid_ext a b = (case euclid_ext b (a mod b) of
1438     (s,t,c) \<Rightarrow> (t, s - t * (a div b), c))"
1439   by (subst euclid_ext.simps) simp
1441 definition euclid_ext' :: "'a \<Rightarrow> 'a \<Rightarrow> 'a \<times> 'a"
1442 where
1443   "euclid_ext' a b = (case euclid_ext a b of (s, t, _) \<Rightarrow> (s, t))"
1445 lemma euclid_ext_gcd [simp]:
1446   "(case euclid_ext a b of (_,_,t) \<Rightarrow> t) = gcd a b"
1447 proof (induct a b rule: euclid_ext.induct)
1448   case (1 a b)
1449   then show ?case
1450   proof (cases "b = 0")
1451     case True
1452       then show ?thesis by
1453         (simp add: euclid_ext_0 unit_div mult_ac unit_simps gcd_0)
1454     next
1455     case False with 1 show ?thesis
1456       by (simp add: euclid_ext_non_0 ac_simps split: prod.split prod.split_asm)
1457     qed
1458 qed
1460 lemma euclid_ext_gcd' [simp]:
1461   "euclid_ext a b = (r, s, t) \<Longrightarrow> t = gcd a b"
1462   by (insert euclid_ext_gcd[of a b], drule (1) subst, simp)
1464 lemma euclid_ext_correct:
1465   "case euclid_ext a b of (s,t,c) \<Rightarrow> s*a + t*b = c"
1466 proof (induct a b rule: euclid_ext.induct)
1467   case (1 a b)
1468   show ?case
1469   proof (cases "b = 0")
1470     case True
1471     then show ?thesis by (simp add: euclid_ext_0 mult_ac)
1472   next
1473     case False
1474     obtain s t c where stc: "euclid_ext b (a mod b) = (s,t,c)"
1475       by (cases "euclid_ext b (a mod b)", blast)
1476     from 1 have "c = s * b + t * (a mod b)" by (simp add: stc False)
1477     also have "... = t*((a div b)*b + a mod b) + (s - t * (a div b))*b"
1478       by (simp add: algebra_simps)
1479     also have "(a div b)*b + a mod b = a" using mod_div_equality .
1480     finally show ?thesis
1481       by (subst euclid_ext.simps, simp add: False stc)
1482     qed
1483 qed
1485 lemma euclid_ext'_correct:
1486   "fst (euclid_ext' a b) * a + snd (euclid_ext' a b) * b = gcd a b"
1487 proof-
1488   obtain s t c where "euclid_ext a b = (s,t,c)"
1489     by (cases "euclid_ext a b", blast)
1490   with euclid_ext_correct[of a b] euclid_ext_gcd[of a b]
1491     show ?thesis unfolding euclid_ext'_def by simp
1492 qed
1494 lemma bezout: "\<exists>s t. s * a + t * b = gcd a b"
1495   using euclid_ext'_correct by blast
1497 lemma euclid_ext'_0 [simp]: "euclid_ext' a 0 = (1 div normalization_factor a, 0)"
1498   by (simp add: bezw_def euclid_ext'_def euclid_ext_0)
1500 lemma euclid_ext'_non_0: "b \<noteq> 0 \<Longrightarrow> euclid_ext' a b = (snd (euclid_ext' b (a mod b)),
1501   fst (euclid_ext' b (a mod b)) - snd (euclid_ext' b (a mod b)) * (a div b))"
1502   by (cases "euclid_ext b (a mod b)")
1503     (simp add: euclid_ext'_def euclid_ext_non_0)
1505 end
1507 instantiation nat :: euclidean_semiring
1508 begin
1510 definition [simp]:
1511   "euclidean_size_nat = (id :: nat \<Rightarrow> nat)"
1513 definition [simp]:
1514   "normalization_factor_nat (n::nat) = (if n = 0 then 0 else 1 :: nat)"
1516 instance proof
1517 qed simp_all
1519 end
1521 instantiation int :: euclidean_ring
1522 begin
1524 definition [simp]:
1525   "euclidean_size_int = (nat \<circ> abs :: int \<Rightarrow> nat)"
1527 definition [simp]:
1528   "normalization_factor_int = (sgn :: int \<Rightarrow> int)"
1530 instance proof
1531   case goal2 then show ?case by (auto simp add: abs_mult nat_mult_distrib)
1532 next
1533   case goal3 then show ?case by (simp add: zsgn_def)
1534 next
1535   case goal5 then show ?case by (auto simp: zsgn_def)
1536 next
1537   case goal6 then show ?case by (auto split: abs_split simp: zsgn_def)
1538 qed (auto simp: sgn_times split: abs_split)
1540 end
1542 end