src/HOL/Number_Theory/Euclidean_Algorithm.thy
 author haftmann Wed Jul 08 14:01:39 2015 +0200 (2015-07-08) changeset 60686 ea5bc46c11e6 parent 60685 cb21b7022b00 child 60687 33dbbcb6a8a3 permissions -rw-r--r--
more algebraic properties for gcd/lcm
1 (* Author: Manuel Eberl *)
3 section \<open>Abstract euclidean algorithm\<close>
5 theory Euclidean_Algorithm
6 imports Main "~~/src/HOL/GCD" "~~/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   \end{itemize}
17   The existence of these functions makes it possible to derive gcd and lcm functions
18   for any Euclidean semiring.
19 \<close>
20 class euclidean_semiring = semiring_div + normalization_semidom +
21   fixes euclidean_size :: "'a \<Rightarrow> nat"
22   assumes mod_size_less:
23     "b \<noteq> 0 \<Longrightarrow> euclidean_size (a mod b) < euclidean_size b"
24   assumes size_mult_mono:
25     "b \<noteq> 0 \<Longrightarrow> euclidean_size a \<le> euclidean_size (a * b)"
26 begin
28 lemma euclidean_division:
29   fixes a :: 'a and b :: 'a
30   assumes "b \<noteq> 0"
31   obtains s and t where "a = s * b + t"
32     and "euclidean_size t < euclidean_size b"
33 proof -
34   from div_mod_equality [of a b 0]
35      have "a = a div b * b + a mod b" by simp
36   with that and assms show ?thesis by (auto simp add: mod_size_less)
37 qed
39 lemma dvd_euclidean_size_eq_imp_dvd:
40   assumes "a \<noteq> 0" and b_dvd_a: "b dvd a" and size_eq: "euclidean_size a = euclidean_size b"
41   shows "a dvd b"
42 proof (rule ccontr)
43   assume "\<not> a dvd b"
44   then have "b mod a \<noteq> 0" by (simp add: mod_eq_0_iff_dvd)
45   from b_dvd_a have b_dvd_mod: "b dvd b mod a" by (simp add: dvd_mod_iff)
46   from b_dvd_mod obtain c where "b mod a = b * c" unfolding dvd_def by blast
47     with \<open>b mod a \<noteq> 0\<close> have "c \<noteq> 0" by auto
48   with \<open>b mod a = b * c\<close> have "euclidean_size (b mod a) \<ge> euclidean_size b"
49       using size_mult_mono by force
50   moreover from \<open>\<not> a dvd b\<close> and \<open>a \<noteq> 0\<close>
51   have "euclidean_size (b mod a) < euclidean_size a"
52       using mod_size_less by blast
53   ultimately show False using size_eq by simp
54 qed
56 function gcd_eucl :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"
57 where
58   "gcd_eucl a b = (if b = 0 then normalize a else gcd_eucl b (a mod b))"
59   by pat_completeness simp
60 termination
61   by (relation "measure (euclidean_size \<circ> snd)") (simp_all add: mod_size_less)
63 declare gcd_eucl.simps [simp del]
65 lemma gcd_eucl_induct [case_names zero mod]:
66   assumes H1: "\<And>b. P b 0"
67   and H2: "\<And>a b. b \<noteq> 0 \<Longrightarrow> P b (a mod b) \<Longrightarrow> P a b"
68   shows "P a b"
69 proof (induct a b rule: gcd_eucl.induct)
70   case ("1" a b)
71   show ?case
72   proof (cases "b = 0")
73     case True then show "P a b" by simp (rule H1)
74   next
75     case False
76     then have "P b (a mod b)"
77       by (rule "1.hyps")
78     with \<open>b \<noteq> 0\<close> show "P a b"
79       by (blast intro: H2)
80   qed
81 qed
83 definition lcm_eucl :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"
84 where
85   "lcm_eucl a b = normalize (a * b) div gcd_eucl a b"
87 definition Lcm_eucl :: "'a set \<Rightarrow> 'a" -- \<open>
88   Somewhat complicated definition of Lcm that has the advantage of working
89   for infinite sets as well\<close>
90 where
91   "Lcm_eucl A = (if \<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) then
92      let l = SOME l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l =
93        (LEAST n. \<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n)
94        in normalize l
95       else 0)"
97 definition Gcd_eucl :: "'a set \<Rightarrow> 'a"
98 where
99   "Gcd_eucl A = Lcm_eucl {d. \<forall>a\<in>A. d dvd a}"
101 lemma gcd_eucl_0:
102   "gcd_eucl a 0 = normalize a"
103   by (simp add: gcd_eucl.simps [of a 0])
105 lemma gcd_eucl_0_left:
106   "gcd_eucl 0 a = normalize a"
107   by (simp_all add: gcd_eucl_0 gcd_eucl.simps [of 0 a])
109 lemma gcd_eucl_non_0:
110   "b \<noteq> 0 \<Longrightarrow> gcd_eucl a b = gcd_eucl b (a mod b)"
111   by (simp add: gcd_eucl.simps [of a b] gcd_eucl.simps [of b 0])
113 end
115 class euclidean_ring = euclidean_semiring + idom
116 begin
118 function euclid_ext :: "'a \<Rightarrow> 'a \<Rightarrow> 'a \<times> 'a \<times> 'a" where
119   "euclid_ext a b =
120      (if b = 0 then
121         (1 div unit_factor a, 0, normalize a)
122       else
123         case euclid_ext b (a mod b) of
124             (s, t, c) \<Rightarrow> (t, s - t * (a div b), c))"
125   by pat_completeness simp
126 termination
127   by (relation "measure (euclidean_size \<circ> snd)") (simp_all add: mod_size_less)
129 declare euclid_ext.simps [simp del]
131 lemma euclid_ext_0:
132   "euclid_ext a 0 = (1 div unit_factor a, 0, normalize a)"
133   by (simp add: euclid_ext.simps [of a 0])
135 lemma euclid_ext_left_0:
136   "euclid_ext 0 a = (0, 1 div unit_factor a, normalize a)"
137   by (simp add: euclid_ext_0 euclid_ext.simps [of 0 a])
139 lemma euclid_ext_non_0:
140   "b \<noteq> 0 \<Longrightarrow> euclid_ext a b = (case euclid_ext b (a mod b) of
141     (s, t, c) \<Rightarrow> (t, s - t * (a div b), c))"
142   by (simp add: euclid_ext.simps [of a b] euclid_ext.simps [of b 0])
144 lemma euclid_ext_code [code]:
145   "euclid_ext a b = (if b = 0 then (1 div unit_factor a, 0, normalize a)
146     else let (s, t, c) = euclid_ext b (a mod b) in  (t, s - t * (a div b), c))"
147   by (simp add: euclid_ext.simps [of a b] euclid_ext.simps [of b 0])
149 lemma euclid_ext_correct:
150   "case euclid_ext a b of (s, t, c) \<Rightarrow> s * a + t * b = c"
151 proof (induct a b rule: gcd_eucl_induct)
152   case (zero a) then show ?case
153     by (simp add: euclid_ext_0 ac_simps)
154 next
155   case (mod a b)
156   obtain s t c where stc: "euclid_ext b (a mod b) = (s,t,c)"
157     by (cases "euclid_ext b (a mod b)") blast
158   with mod have "c = s * b + t * (a mod b)" by simp
159   also have "... = t * ((a div b) * b + a mod b) + (s - t * (a div b)) * b"
160     by (simp add: algebra_simps)
161   also have "(a div b) * b + a mod b = a" using mod_div_equality .
162   finally show ?case
163     by (subst euclid_ext.simps) (simp add: stc mod ac_simps)
164 qed
166 definition euclid_ext' :: "'a \<Rightarrow> 'a \<Rightarrow> 'a \<times> 'a"
167 where
168   "euclid_ext' a b = (case euclid_ext a b of (s, t, _) \<Rightarrow> (s, t))"
170 lemma euclid_ext'_0: "euclid_ext' a 0 = (1 div unit_factor a, 0)"
171   by (simp add: euclid_ext'_def euclid_ext_0)
173 lemma euclid_ext'_left_0: "euclid_ext' 0 a = (0, 1 div unit_factor a)"
174   by (simp add: euclid_ext'_def euclid_ext_left_0)
176 lemma euclid_ext'_non_0: "b \<noteq> 0 \<Longrightarrow> euclid_ext' a b = (snd (euclid_ext' b (a mod b)),
177   fst (euclid_ext' b (a mod b)) - snd (euclid_ext' b (a mod b)) * (a div b))"
178   by (simp add: euclid_ext'_def euclid_ext_non_0 split_def)
180 end
182 class euclidean_semiring_gcd = euclidean_semiring + gcd + Gcd +
183   assumes gcd_gcd_eucl: "gcd = gcd_eucl" and lcm_lcm_eucl: "lcm = lcm_eucl"
184   assumes Gcd_Gcd_eucl: "Gcd = Gcd_eucl" and Lcm_Lcm_eucl: "Lcm = Lcm_eucl"
185 begin
187 lemma gcd_0_left:
188   "gcd 0 a = normalize a"
189   unfolding gcd_gcd_eucl by (fact gcd_eucl_0_left)
191 lemma gcd_0:
192   "gcd a 0 = normalize a"
193   unfolding gcd_gcd_eucl by (fact gcd_eucl_0)
195 lemma gcd_non_0:
196   "b \<noteq> 0 \<Longrightarrow> gcd a b = gcd b (a mod b)"
197   unfolding gcd_gcd_eucl by (fact gcd_eucl_non_0)
199 lemma gcd_dvd1 [iff]: "gcd a b dvd a"
200   and gcd_dvd2 [iff]: "gcd a b dvd b"
201   by (induct a b rule: gcd_eucl_induct)
202     (simp_all add: gcd_0 gcd_non_0 dvd_mod_iff)
204 lemma dvd_gcd_D1: "k dvd gcd m n \<Longrightarrow> k dvd m"
205   by (rule dvd_trans, assumption, rule gcd_dvd1)
207 lemma dvd_gcd_D2: "k dvd gcd m n \<Longrightarrow> k dvd n"
208   by (rule dvd_trans, assumption, rule gcd_dvd2)
210 lemma gcd_greatest:
211   fixes k a b :: 'a
212   shows "k dvd a \<Longrightarrow> k dvd b \<Longrightarrow> k dvd gcd a b"
213 proof (induct a b rule: gcd_eucl_induct)
214   case (zero a) from zero(1) show ?case by (rule dvd_trans) (simp add: gcd_0)
215 next
216   case (mod a b)
217   then show ?case
218     by (simp add: gcd_non_0 dvd_mod_iff)
219 qed
221 lemma dvd_gcd_iff:
222   "k dvd gcd a b \<longleftrightarrow> k dvd a \<and> k dvd b"
223   by (blast intro!: gcd_greatest intro: dvd_trans)
225 lemmas gcd_greatest_iff = dvd_gcd_iff
227 lemma gcd_zero [simp]:
228   "gcd a b = 0 \<longleftrightarrow> a = 0 \<and> b = 0"
229   by (metis dvd_0_left dvd_refl gcd_dvd1 gcd_dvd2 gcd_greatest)+
231 lemma unit_factor_gcd [simp]:
232   "unit_factor (gcd a b) = (if a = 0 \<and> b = 0 then 0 else 1)" (is "?f a b = ?g a b")
233   by (induct a b rule: gcd_eucl_induct)
234     (auto simp add: gcd_0 gcd_non_0)
236 lemma gcdI:
237   assumes "c dvd a" and "c dvd b" and greatest: "\<And>d. d dvd a \<Longrightarrow> d dvd b \<Longrightarrow> d dvd c"
238     and "unit_factor c = (if c = 0 then 0 else 1)"
239   shows "c = gcd a b"
240   by (rule associated_eqI) (auto simp: assms associated_def intro: gcd_greatest)
242 sublocale gcd!: abel_semigroup gcd
243 proof
244   fix a b c
245   show "gcd (gcd a b) c = gcd a (gcd b c)"
246   proof (rule gcdI)
247     have "gcd (gcd a b) c dvd gcd a b" "gcd a b dvd a" by simp_all
248     then show "gcd (gcd a b) c dvd a" by (rule dvd_trans)
249     have "gcd (gcd a b) c dvd gcd a b" "gcd a b dvd b" by simp_all
250     hence "gcd (gcd a b) c dvd b" by (rule dvd_trans)
251     moreover have "gcd (gcd a b) c dvd c" by simp
252     ultimately show "gcd (gcd a b) c dvd gcd b c"
253       by (rule gcd_greatest)
254     show "unit_factor (gcd (gcd a b) c) =  (if gcd (gcd a b) c = 0 then 0 else 1)"
255       by auto
256     fix l assume "l dvd a" and "l dvd gcd b c"
257     with dvd_trans[OF _ gcd_dvd1] and dvd_trans[OF _ gcd_dvd2]
258       have "l dvd b" and "l dvd c" by blast+
259     with \<open>l dvd a\<close> show "l dvd gcd (gcd a b) c"
260       by (intro gcd_greatest)
261   qed
262 next
263   fix a b
264   show "gcd a b = gcd b a"
265     by (rule gcdI) (simp_all add: gcd_greatest)
266 qed
268 lemma gcd_unique: "d dvd a \<and> d dvd b \<and>
269     unit_factor d = (if d = 0 then 0 else 1) \<and>
270     (\<forall>e. e dvd a \<and> e dvd b \<longrightarrow> e dvd d) \<longleftrightarrow> d = gcd a b"
271   by (rule, auto intro: gcdI simp: gcd_greatest)
273 lemma gcd_dvd_prod: "gcd a b dvd k * b"
274   using mult_dvd_mono [of 1] by auto
276 lemma gcd_1_left [simp]: "gcd 1 a = 1"
277   by (rule sym, rule gcdI, simp_all)
279 lemma gcd_1 [simp]: "gcd a 1 = 1"
280   by (rule sym, rule gcdI, simp_all)
282 lemma gcd_proj2_if_dvd:
283   "b dvd a \<Longrightarrow> gcd a b = normalize b"
284   by (cases "b = 0", simp_all add: dvd_eq_mod_eq_0 gcd_non_0 gcd_0)
286 lemma gcd_proj1_if_dvd:
287   "a dvd b \<Longrightarrow> gcd a b = normalize a"
288   by (subst gcd.commute, simp add: gcd_proj2_if_dvd)
290 lemma gcd_proj1_iff: "gcd m n = normalize m \<longleftrightarrow> m dvd n"
291 proof
292   assume A: "gcd m n = normalize m"
293   show "m dvd n"
294   proof (cases "m = 0")
295     assume [simp]: "m \<noteq> 0"
296     from A have B: "m = gcd m n * unit_factor m"
297       by (simp add: unit_eq_div2)
298     show ?thesis by (subst B, simp add: mult_unit_dvd_iff)
299   qed (insert A, simp)
300 next
301   assume "m dvd n"
302   then show "gcd m n = normalize m" by (rule gcd_proj1_if_dvd)
303 qed
305 lemma gcd_proj2_iff: "gcd m n = normalize n \<longleftrightarrow> n dvd m"
306   using gcd_proj1_iff [of n m] by (simp add: ac_simps)
308 lemma gcd_mod1 [simp]:
309   "gcd (a mod b) b = gcd a b"
310   by (rule gcdI, metis dvd_mod_iff gcd_dvd1 gcd_dvd2, simp_all add: gcd_greatest dvd_mod_iff)
312 lemma gcd_mod2 [simp]:
313   "gcd a (b mod a) = gcd a b"
314   by (rule gcdI, simp, metis dvd_mod_iff gcd_dvd1 gcd_dvd2, simp_all add: gcd_greatest dvd_mod_iff)
316 lemma gcd_mult_distrib':
317   "normalize c * gcd a b = gcd (c * a) (c * b)"
318 proof (cases "c = 0")
319   case True then show ?thesis by (simp_all add: gcd_0)
320 next
321   case False then have [simp]: "is_unit (unit_factor c)" by simp
322   show ?thesis
323   proof (induct a b rule: gcd_eucl_induct)
324     case (zero a) show ?case
325     proof (cases "a = 0")
326       case True then show ?thesis by (simp add: gcd_0)
327     next
328       case False
329       then show ?thesis by (simp add: gcd_0 normalize_mult)
330     qed
331     case (mod a b)
332     then show ?case by (simp add: mult_mod_right gcd.commute)
333   qed
334 qed
336 lemma gcd_mult_distrib:
337   "k * gcd a b = gcd (k * a) (k * b) * unit_factor k"
338 proof-
339   have "normalize k * gcd a b = gcd (k * a) (k * b)"
340     by (simp add: gcd_mult_distrib')
341   then have "normalize k * gcd a b * unit_factor k = gcd (k * a) (k * b) * unit_factor k"
342     by simp
343   then have "normalize k * unit_factor k * gcd a b  = gcd (k * a) (k * b) * unit_factor k"
344     by (simp only: ac_simps)
345   then show ?thesis
346     by simp
347 qed
349 lemma euclidean_size_gcd_le1 [simp]:
350   assumes "a \<noteq> 0"
351   shows "euclidean_size (gcd a b) \<le> euclidean_size a"
352 proof -
353    have "gcd a b dvd a" by (rule gcd_dvd1)
354    then obtain c where A: "a = gcd a b * c" unfolding dvd_def by blast
355    with \<open>a \<noteq> 0\<close> show ?thesis by (subst (2) A, intro size_mult_mono) auto
356 qed
358 lemma euclidean_size_gcd_le2 [simp]:
359   "b \<noteq> 0 \<Longrightarrow> euclidean_size (gcd a b) \<le> euclidean_size b"
360   by (subst gcd.commute, rule euclidean_size_gcd_le1)
362 lemma euclidean_size_gcd_less1:
363   assumes "a \<noteq> 0" and "\<not>a dvd b"
364   shows "euclidean_size (gcd a b) < euclidean_size a"
365 proof (rule ccontr)
366   assume "\<not>euclidean_size (gcd a b) < euclidean_size a"
367   with \<open>a \<noteq> 0\<close> have "euclidean_size (gcd a b) = euclidean_size a"
368     by (intro le_antisym, simp_all)
369   with assms have "a dvd gcd a b" by (auto intro: dvd_euclidean_size_eq_imp_dvd)
370   hence "a dvd b" using dvd_gcd_D2 by blast
371   with \<open>\<not>a dvd b\<close> show False by contradiction
372 qed
374 lemma euclidean_size_gcd_less2:
375   assumes "b \<noteq> 0" and "\<not>b dvd a"
376   shows "euclidean_size (gcd a b) < euclidean_size b"
377   using assms by (subst gcd.commute, rule euclidean_size_gcd_less1)
379 lemma gcd_mult_unit1: "is_unit a \<Longrightarrow> gcd (b * a) c = gcd b c"
380   apply (rule gcdI)
381   apply (rule dvd_trans, rule gcd_dvd1, simp add: unit_simps)
382   apply (rule gcd_dvd2)
383   apply (rule gcd_greatest, simp add: unit_simps, assumption)
384   apply (subst unit_factor_gcd, simp add: gcd_0)
385   done
387 lemma gcd_mult_unit2: "is_unit a \<Longrightarrow> gcd b (c * a) = gcd b c"
388   by (subst gcd.commute, subst gcd_mult_unit1, assumption, rule gcd.commute)
390 lemma gcd_div_unit1: "is_unit a \<Longrightarrow> gcd (b div a) c = gcd b c"
391   by (erule is_unitE [of _ b]) (simp add: gcd_mult_unit1)
393 lemma gcd_div_unit2: "is_unit a \<Longrightarrow> gcd b (c div a) = gcd b c"
394   by (erule is_unitE [of _ c]) (simp add: gcd_mult_unit2)
396 lemma normalize_gcd_left [simp]:
397   "gcd (normalize a) b = gcd a b"
398 proof (cases "a = 0")
399   case True then show ?thesis
400     by simp
401 next
402   case False then have "is_unit (unit_factor a)"
403     by simp
404   moreover have "normalize a = a div unit_factor a"
405     by simp
406   ultimately show ?thesis
407     by (simp only: gcd_div_unit1)
408 qed
410 lemma normalize_gcd_right [simp]:
411   "gcd a (normalize b) = gcd a b"
412   using normalize_gcd_left [of b a] by (simp add: ac_simps)
414 lemma gcd_idem: "gcd a a = normalize a"
415   by (cases "a = 0") (simp add: gcd_0_left, rule sym, rule gcdI, simp_all)
417 lemma gcd_right_idem: "gcd (gcd a b) b = gcd a b"
418   apply (rule gcdI)
419   apply (simp add: ac_simps)
420   apply (rule gcd_dvd2)
421   apply (rule gcd_greatest, erule (1) gcd_greatest, assumption)
422   apply simp
423   done
425 lemma gcd_left_idem: "gcd a (gcd a b) = gcd a b"
426   apply (rule gcdI)
427   apply simp
428   apply (rule dvd_trans, rule gcd_dvd2, rule gcd_dvd2)
429   apply (rule gcd_greatest, assumption, erule gcd_greatest, assumption)
430   apply simp
431   done
433 lemma comp_fun_idem_gcd: "comp_fun_idem gcd"
434 proof
435   fix a b show "gcd a \<circ> gcd b = gcd b \<circ> gcd a"
436     by (simp add: fun_eq_iff ac_simps)
437 next
438   fix a show "gcd a \<circ> gcd a = gcd a"
439     by (simp add: fun_eq_iff gcd_left_idem)
440 qed
442 lemma coprime_dvd_mult:
443   assumes "gcd c b = 1" and "c dvd a * b"
444   shows "c dvd a"
445 proof -
446   let ?nf = "unit_factor"
447   from assms gcd_mult_distrib [of a c b]
448     have A: "a = gcd (a * c) (a * b) * ?nf a" by simp
449   from \<open>c dvd a * b\<close> show ?thesis by (subst A, simp_all add: gcd_greatest)
450 qed
452 lemma coprime_dvd_mult_iff:
453   "gcd c b = 1 \<Longrightarrow> (c dvd a * b) = (c dvd a)"
454   by (rule, rule coprime_dvd_mult, simp_all)
456 lemma gcd_dvd_antisym:
457   "gcd a b dvd gcd c d \<Longrightarrow> gcd c d dvd gcd a b \<Longrightarrow> gcd a b = gcd c d"
458 proof (rule gcdI)
459   assume A: "gcd a b dvd gcd c d" and B: "gcd c d dvd gcd a b"
460   have "gcd c d dvd c" by simp
461   with A show "gcd a b dvd c" by (rule dvd_trans)
462   have "gcd c d dvd d" by simp
463   with A show "gcd a b dvd d" by (rule dvd_trans)
464   show "unit_factor (gcd a b) = (if gcd a b = 0 then 0 else 1)"
465     by simp
466   fix l assume "l dvd c" and "l dvd d"
467   hence "l dvd gcd c d" by (rule gcd_greatest)
468   from this and B show "l dvd gcd a b" by (rule dvd_trans)
469 qed
471 lemma gcd_mult_cancel:
472   assumes "gcd k n = 1"
473   shows "gcd (k * m) n = gcd m n"
474 proof (rule gcd_dvd_antisym)
475   have "gcd (gcd (k * m) n) k = gcd (gcd k n) (k * m)" by (simp add: ac_simps)
476   also note \<open>gcd k n = 1\<close>
477   finally have "gcd (gcd (k * m) n) k = 1" by simp
478   hence "gcd (k * m) n dvd m" by (rule coprime_dvd_mult, simp add: ac_simps)
479   moreover have "gcd (k * m) n dvd n" by simp
480   ultimately show "gcd (k * m) n dvd gcd m n" by (rule gcd_greatest)
481   have "gcd m n dvd (k * m)" and "gcd m n dvd n" by simp_all
482   then show "gcd m n dvd gcd (k * m) n" by (rule gcd_greatest)
483 qed
485 lemma coprime_crossproduct:
486   assumes [simp]: "gcd a d = 1" "gcd b c = 1"
487   shows "associated (a * c) (b * d) \<longleftrightarrow> associated a b \<and> associated c d" (is "?lhs \<longleftrightarrow> ?rhs")
488 proof
489   assume ?rhs then show ?lhs unfolding associated_def by (fast intro: mult_dvd_mono)
490 next
491   assume ?lhs
492   from \<open>?lhs\<close> have "a dvd b * d" unfolding associated_def by (metis dvd_mult_left)
493   hence "a dvd b" by (simp add: coprime_dvd_mult_iff)
494   moreover from \<open>?lhs\<close> have "b dvd a * c" unfolding associated_def by (metis dvd_mult_left)
495   hence "b dvd a" by (simp add: coprime_dvd_mult_iff)
496   moreover from \<open>?lhs\<close> have "c dvd d * b"
497     unfolding associated_def by (auto dest: dvd_mult_right simp add: ac_simps)
498   hence "c dvd d" by (simp add: coprime_dvd_mult_iff gcd.commute)
499   moreover from \<open>?lhs\<close> have "d dvd c * a"
500     unfolding associated_def by (auto dest: dvd_mult_right simp add: ac_simps)
501   hence "d dvd c" by (simp add: coprime_dvd_mult_iff gcd.commute)
502   ultimately show ?rhs unfolding associated_def by simp
503 qed
505 lemma gcd_add1 [simp]:
506   "gcd (m + n) n = gcd m n"
507   by (cases "n = 0", simp_all add: gcd_non_0)
509 lemma gcd_add2 [simp]:
510   "gcd m (m + n) = gcd m n"
511   using gcd_add1 [of n m] by (simp add: ac_simps)
514   "gcd m (k * m + n) = gcd m n"
515 proof -
516   have "gcd m ((k * m + n) mod m) = gcd m (k * m + n)"
517     by (fact gcd_mod2)
518   then show ?thesis by simp
519 qed
521 lemma coprimeI: "(\<And>l. \<lbrakk>l dvd a; l dvd b\<rbrakk> \<Longrightarrow> l dvd 1) \<Longrightarrow> gcd a b = 1"
522   by (rule sym, rule gcdI, simp_all)
524 lemma coprime: "gcd a b = 1 \<longleftrightarrow> (\<forall>d. d dvd a \<and> d dvd b \<longleftrightarrow> is_unit d)"
525   by (auto intro: coprimeI gcd_greatest dvd_gcd_D1 dvd_gcd_D2)
527 lemma div_gcd_coprime:
528   assumes nz: "a \<noteq> 0 \<or> b \<noteq> 0"
529   defines [simp]: "d \<equiv> gcd a b"
530   defines [simp]: "a' \<equiv> a div d" and [simp]: "b' \<equiv> b div d"
531   shows "gcd a' b' = 1"
532 proof (rule coprimeI)
533   fix l assume "l dvd a'" "l dvd b'"
534   then obtain s t where "a' = l * s" "b' = l * t" unfolding dvd_def by blast
535   moreover have "a = a' * d" "b = b' * d" by simp_all
536   ultimately have "a = (l * d) * s" "b = (l * d) * t"
537     by (simp_all only: ac_simps)
538   hence "l*d dvd a" and "l*d dvd b" by (simp_all only: dvd_triv_left)
539   hence "l*d dvd d" by (simp add: gcd_greatest)
540   then obtain u where "d = l * d * u" ..
541   then have "d * (l * u) = d" by (simp add: ac_simps)
542   moreover from nz have "d \<noteq> 0" by simp
543   with div_mult_self1_is_id have "d * (l * u) div d = l * u" .
544   ultimately have "1 = l * u"
545     using \<open>d \<noteq> 0\<close> by simp
546   then show "l dvd 1" ..
547 qed
549 lemma coprime_mult:
550   assumes da: "gcd d a = 1" and db: "gcd d b = 1"
551   shows "gcd d (a * b) = 1"
552   apply (subst gcd.commute)
553   using da apply (subst gcd_mult_cancel)
554   apply (subst gcd.commute, assumption)
555   apply (subst gcd.commute, rule db)
556   done
558 lemma coprime_lmult:
559   assumes dab: "gcd d (a * b) = 1"
560   shows "gcd d a = 1"
561 proof (rule coprimeI)
562   fix l assume "l dvd d" and "l dvd a"
563   hence "l dvd a * b" by simp
564   with \<open>l dvd d\<close> and dab show "l dvd 1" by (auto intro: gcd_greatest)
565 qed
567 lemma coprime_rmult:
568   assumes dab: "gcd d (a * b) = 1"
569   shows "gcd d b = 1"
570 proof (rule coprimeI)
571   fix l assume "l dvd d" and "l dvd b"
572   hence "l dvd a * b" by simp
573   with \<open>l dvd d\<close> and dab show "l dvd 1" by (auto intro: gcd_greatest)
574 qed
576 lemma coprime_mul_eq: "gcd d (a * b) = 1 \<longleftrightarrow> gcd d a = 1 \<and> gcd d b = 1"
577   using coprime_rmult[of d a b] coprime_lmult[of d a b] coprime_mult[of d a b] by blast
579 lemma gcd_coprime:
580   assumes c: "gcd a b \<noteq> 0" and a: "a = a' * gcd a b" and b: "b = b' * gcd a b"
581   shows "gcd a' b' = 1"
582 proof -
583   from c have "a \<noteq> 0 \<or> b \<noteq> 0" by simp
584   with div_gcd_coprime have "gcd (a div gcd a b) (b div gcd a b) = 1" .
585   also from assms have "a div gcd a b = a'" by (metis div_mult_self2_is_id)+
586   also from assms have "b div gcd a b = b'" by (metis div_mult_self2_is_id)+
587   finally show ?thesis .
588 qed
590 lemma coprime_power:
591   assumes "0 < n"
592   shows "gcd a (b ^ n) = 1 \<longleftrightarrow> gcd a b = 1"
593 using assms proof (induct n)
594   case (Suc n) then show ?case
595     by (cases n) (simp_all add: coprime_mul_eq)
596 qed simp
598 lemma gcd_coprime_exists:
599   assumes nz: "gcd a b \<noteq> 0"
600   shows "\<exists>a' b'. a = a' * gcd a b \<and> b = b' * gcd a b \<and> gcd a' b' = 1"
601   apply (rule_tac x = "a div gcd a b" in exI)
602   apply (rule_tac x = "b div gcd a b" in exI)
603   apply (insert nz, auto intro: div_gcd_coprime)
604   done
606 lemma coprime_exp:
607   "gcd d a = 1 \<Longrightarrow> gcd d (a^n) = 1"
608   by (induct n, simp_all add: coprime_mult)
610 lemma coprime_exp2 [intro]:
611   "gcd a b = 1 \<Longrightarrow> gcd (a^n) (b^m) = 1"
612   apply (rule coprime_exp)
613   apply (subst gcd.commute)
614   apply (rule coprime_exp)
615   apply (subst gcd.commute)
616   apply assumption
617   done
619 lemma gcd_exp:
620   "gcd (a^n) (b^n) = (gcd a b) ^ n"
621 proof (cases "a = 0 \<and> b = 0")
622   assume "a = 0 \<and> b = 0"
623   then show ?thesis by (cases n, simp_all add: gcd_0_left)
624 next
625   assume A: "\<not>(a = 0 \<and> b = 0)"
626   hence "1 = gcd ((a div gcd a b)^n) ((b div gcd a b)^n)"
627     using div_gcd_coprime by (subst sym, auto simp: div_gcd_coprime)
628   hence "(gcd a b) ^ n = (gcd a b) ^ n * ..." by simp
629   also note gcd_mult_distrib
630   also have "unit_factor ((gcd a b)^n) = 1"
631     by (simp add: unit_factor_power A)
632   also have "(gcd a b)^n * (a div gcd a b)^n = a^n"
633     by (subst ac_simps, subst div_power, simp, rule dvd_div_mult_self, rule dvd_power_same, simp)
634   also have "(gcd a b)^n * (b div gcd a b)^n = b^n"
635     by (subst ac_simps, subst div_power, simp, rule dvd_div_mult_self, rule dvd_power_same, simp)
636   finally show ?thesis by simp
637 qed
639 lemma coprime_common_divisor:
640   "gcd a b = 1 \<Longrightarrow> a dvd a \<Longrightarrow> a dvd b \<Longrightarrow> is_unit a"
641   apply (subgoal_tac "a dvd gcd a b")
642   apply simp
643   apply (erule (1) gcd_greatest)
644   done
646 lemma division_decomp:
647   assumes dc: "a dvd b * c"
648   shows "\<exists>b' c'. a = b' * c' \<and> b' dvd b \<and> c' dvd c"
649 proof (cases "gcd a b = 0")
650   assume "gcd a b = 0"
651   hence "a = 0 \<and> b = 0" by simp
652   hence "a = 0 * c \<and> 0 dvd b \<and> c dvd c" by simp
653   then show ?thesis by blast
654 next
655   let ?d = "gcd a b"
656   assume "?d \<noteq> 0"
657   from gcd_coprime_exists[OF this]
658     obtain a' b' where ab': "a = a' * ?d" "b = b' * ?d" "gcd a' b' = 1"
659     by blast
660   from ab'(1) have "a' dvd a" unfolding dvd_def by blast
661   with dc have "a' dvd b*c" using dvd_trans[of a' a "b*c"] by simp
662   from dc ab'(1,2) have "a'*?d dvd (b'*?d) * c" by simp
663   hence "?d * a' dvd ?d * (b' * c)" by (simp add: mult_ac)
664   with \<open>?d \<noteq> 0\<close> have "a' dvd b' * c" by simp
665   with coprime_dvd_mult[OF ab'(3)]
666     have "a' dvd c" by (subst (asm) ac_simps, blast)
667   with ab'(1) have "a = ?d * a' \<and> ?d dvd b \<and> a' dvd c" by (simp add: mult_ac)
668   then show ?thesis by blast
669 qed
671 lemma pow_divs_pow:
672   assumes ab: "a ^ n dvd b ^ n" and n: "n \<noteq> 0"
673   shows "a dvd b"
674 proof (cases "gcd a b = 0")
675   assume "gcd a b = 0"
676   then show ?thesis by simp
677 next
678   let ?d = "gcd a b"
679   assume "?d \<noteq> 0"
680   from n obtain m where m: "n = Suc m" by (cases n, simp_all)
681   from \<open>?d \<noteq> 0\<close> have zn: "?d ^ n \<noteq> 0" by (rule power_not_zero)
682   from gcd_coprime_exists[OF \<open>?d \<noteq> 0\<close>]
683     obtain a' b' where ab': "a = a' * ?d" "b = b' * ?d" "gcd a' b' = 1"
684     by blast
685   from ab have "(a' * ?d) ^ n dvd (b' * ?d) ^ n"
686     by (simp add: ab'(1,2)[symmetric])
687   hence "?d^n * a'^n dvd ?d^n * b'^n"
688     by (simp only: power_mult_distrib ac_simps)
689   with zn have "a'^n dvd b'^n" by simp
690   hence "a' dvd b'^n" using dvd_trans[of a' "a'^n" "b'^n"] by (simp add: m)
691   hence "a' dvd b'^m * b'" by (simp add: m ac_simps)
692   with coprime_dvd_mult[OF coprime_exp[OF ab'(3), of m]]
693     have "a' dvd b'" by (subst (asm) ac_simps, blast)
694   hence "a'*?d dvd b'*?d" by (rule mult_dvd_mono, simp)
695   with ab'(1,2) show ?thesis by simp
696 qed
698 lemma pow_divs_eq [simp]:
699   "n \<noteq> 0 \<Longrightarrow> a ^ n dvd b ^ n \<longleftrightarrow> a dvd b"
700   by (auto intro: pow_divs_pow dvd_power_same)
702 lemma divs_mult:
703   assumes mr: "m dvd r" and nr: "n dvd r" and mn: "gcd m n = 1"
704   shows "m * n dvd r"
705 proof -
706   from mr nr obtain m' n' where m': "r = m*m'" and n': "r = n*n'"
707     unfolding dvd_def by blast
708   from mr n' have "m dvd n'*n" by (simp add: ac_simps)
709   hence "m dvd n'" using coprime_dvd_mult_iff[OF mn] by simp
710   then obtain k where k: "n' = m*k" unfolding dvd_def by blast
711   with n' have "r = m * n * k" by (simp add: mult_ac)
712   then show ?thesis unfolding dvd_def by blast
713 qed
715 lemma coprime_plus_one [simp]: "gcd (n + 1) n = 1"
716   by (subst add_commute, simp)
718 lemma setprod_coprime [rule_format]:
719   "(\<forall>i\<in>A. gcd (f i) a = 1) \<longrightarrow> gcd (\<Prod>i\<in>A. f i) a = 1"
720   apply (cases "finite A")
721   apply (induct set: finite)
722   apply (auto simp add: gcd_mult_cancel)
723   done
725 lemma coprime_divisors:
726   assumes "d dvd a" "e dvd b" "gcd a b = 1"
727   shows "gcd d e = 1"
728 proof -
729   from assms obtain k l where "a = d * k" "b = e * l"
730     unfolding dvd_def by blast
731   with assms have "gcd (d * k) (e * l) = 1" by simp
732   hence "gcd (d * k) e = 1" by (rule coprime_lmult)
733   also have "gcd (d * k) e = gcd e (d * k)" by (simp add: ac_simps)
734   finally have "gcd e d = 1" by (rule coprime_lmult)
735   then show ?thesis by (simp add: ac_simps)
736 qed
738 lemma invertible_coprime:
739   assumes "a * b mod m = 1"
740   shows "coprime a m"
741 proof -
742   from assms have "coprime m (a * b mod m)"
743     by simp
744   then have "coprime m (a * b)"
745     by simp
746   then have "coprime m a"
747     by (rule coprime_lmult)
748   then show ?thesis
749     by (simp add: ac_simps)
750 qed
752 lemma lcm_gcd:
753   "lcm a b = normalize (a * b) div gcd a b"
754   by (simp add: lcm_lcm_eucl gcd_gcd_eucl lcm_eucl_def)
756 lemma lcm_gcd_prod:
757   "lcm a b * gcd a b = normalize (a * b)"
758   by (simp add: lcm_gcd)
760 lemma lcm_dvd1 [iff]:
761   "a dvd lcm a b"
762 proof (cases "a*b = 0")
763   assume "a * b \<noteq> 0"
764   hence "gcd a b \<noteq> 0" by simp
765   let ?c = "1 div unit_factor (a * b)"
766   from \<open>a * b \<noteq> 0\<close> have [simp]: "is_unit (unit_factor (a * b))" by simp
767   from lcm_gcd_prod[of a b] have "lcm a b * gcd a b = a * ?c * b"
768     by (simp add: div_mult_swap unit_div_commute)
769   hence "lcm a b * gcd a b div gcd a b = a * ?c * b div gcd a b" by simp
770   with \<open>gcd a b \<noteq> 0\<close> have "lcm a b = a * ?c * b div gcd a b"
771     by (subst (asm) div_mult_self2_is_id, simp_all)
772   also have "... = a * (?c * b div gcd a b)"
773     by (metis div_mult_swap gcd_dvd2 mult_assoc)
774   finally show ?thesis by (rule dvdI)
775 qed (auto simp add: lcm_gcd)
777 lemma lcm_least:
778   "\<lbrakk>a dvd k; b dvd k\<rbrakk> \<Longrightarrow> lcm a b dvd k"
779 proof (cases "k = 0")
780   let ?nf = unit_factor
781   assume "k \<noteq> 0"
782   hence "is_unit (?nf k)" by simp
783   hence "?nf k \<noteq> 0" by (metis not_is_unit_0)
784   assume A: "a dvd k" "b dvd k"
785   hence "gcd a b \<noteq> 0" using \<open>k \<noteq> 0\<close> by auto
786   from A obtain r s where ar: "k = a * r" and bs: "k = b * s"
787     unfolding dvd_def by blast
788   with \<open>k \<noteq> 0\<close> have "r * s \<noteq> 0"
789     by auto (drule sym [of 0], simp)
790   hence "is_unit (?nf (r * s))" by simp
791   let ?c = "?nf k div ?nf (r*s)"
792   from \<open>is_unit (?nf k)\<close> and \<open>is_unit (?nf (r * s))\<close> have "is_unit ?c" by (rule unit_div)
793   hence "?c \<noteq> 0" using not_is_unit_0 by fast
794   from ar bs have "k * k * gcd s r = ?nf k * k * gcd (k * s) (k * r)"
795     by (subst mult_assoc, subst gcd_mult_distrib[of k s r], simp only: ac_simps)
796   also have "... = ?nf k * k * gcd ((r*s) * a) ((r*s) * b)"
797     by (subst (3) \<open>k = a * r\<close>, subst (3) \<open>k = b * s\<close>, simp add: algebra_simps)
798   also have "... = ?c * r*s * k * gcd a b" using \<open>r * s \<noteq> 0\<close>
799     by (subst gcd_mult_distrib'[symmetric], simp add: algebra_simps unit_simps)
800   finally have "(a*r) * (b*s) * gcd s r = ?c * k * r * s * gcd a b"
801     by (subst ar[symmetric], subst bs[symmetric], simp add: mult_ac)
802   hence "a * b * gcd s r * (r * s) = ?c * k * gcd a b * (r * s)"
803     by (simp add: algebra_simps)
804   hence "?c * k * gcd a b = a * b * gcd s r" using \<open>r * s \<noteq> 0\<close>
805     by (metis div_mult_self2_is_id)
806   also have "... = lcm a b * gcd a b * gcd s r * ?nf (a*b)"
807     by (subst lcm_gcd_prod[of a b], metis gcd_mult_distrib gcd_mult_distrib')
808   also have "... = lcm a b * gcd s r * ?nf (a*b) * gcd a b"
809     by (simp add: algebra_simps)
810   finally have "k * ?c = lcm a b * gcd s r * ?nf (a*b)" using \<open>gcd a b \<noteq> 0\<close>
811     by (metis mult.commute div_mult_self2_is_id)
812   hence "k = lcm a b * (gcd s r * ?nf (a*b)) div ?c" using \<open>?c \<noteq> 0\<close>
813     by (metis div_mult_self2_is_id mult_assoc)
814   also have "... = lcm a b * (gcd s r * ?nf (a*b) div ?c)" using \<open>is_unit ?c\<close>
815     by (simp add: unit_simps)
816   finally show ?thesis by (rule dvdI)
817 qed simp
819 lemma lcm_zero:
820   "lcm a b = 0 \<longleftrightarrow> a = 0 \<or> b = 0"
821 proof -
822   let ?nf = unit_factor
823   {
824     assume "a \<noteq> 0" "b \<noteq> 0"
825     hence "a * b div ?nf (a * b) \<noteq> 0" by (simp add: no_zero_divisors)
826     moreover from \<open>a \<noteq> 0\<close> and \<open>b \<noteq> 0\<close> have "gcd a b \<noteq> 0" by simp
827     ultimately have "lcm a b \<noteq> 0" using lcm_gcd_prod[of a b] by (intro notI, simp)
828   } moreover {
829     assume "a = 0 \<or> b = 0"
830     hence "lcm a b = 0" by (elim disjE, simp_all add: lcm_gcd)
831   }
832   ultimately show ?thesis by blast
833 qed
835 lemmas lcm_0_iff = lcm_zero
837 lemma gcd_lcm:
838   assumes "lcm a b \<noteq> 0"
839   shows "gcd a b = normalize (a * b) div lcm a b"
840 proof -
841   have "lcm a b * gcd a b = normalize (a * b)"
842     by (fact lcm_gcd_prod)
843   with assms show ?thesis
844     by (metis nonzero_mult_divide_cancel_left)
845 qed
847 lemma unit_factor_lcm [simp]:
848   "unit_factor (lcm a b) = (if a = 0 \<or> b = 0 then 0 else 1)"
849   by (simp add: dvd_unit_factor_div lcm_gcd)
851 lemma lcm_dvd2 [iff]: "b dvd lcm a b"
852   using lcm_dvd1 [of b a] by (simp add: lcm_gcd ac_simps)
854 lemma lcmI:
855   assumes "a dvd c" and "b dvd c" and "\<And>d. a dvd d \<Longrightarrow> b dvd d \<Longrightarrow> c dvd d"
856     and "unit_factor c = (if c = 0 then 0 else 1)"
857   shows "c = lcm a b"
858   by (rule associated_eqI)
859     (auto simp: assms associated_def intro: lcm_least, simp_all add: lcm_gcd)
861 sublocale lcm!: abel_semigroup lcm
862 proof
863   fix a b c
864   show "lcm (lcm a b) c = lcm a (lcm b c)"
865   proof (rule lcmI)
866     have "a dvd lcm a b" and "lcm a b dvd lcm (lcm a b) c" by simp_all
867     then show "a dvd lcm (lcm a b) c" by (rule dvd_trans)
869     have "b dvd lcm a b" and "lcm a b dvd lcm (lcm a b) c" by simp_all
870     hence "b dvd lcm (lcm a b) c" by (rule dvd_trans)
871     moreover have "c dvd lcm (lcm a b) c" by simp
872     ultimately show "lcm b c dvd lcm (lcm a b) c" by (rule lcm_least)
874     fix l assume "a dvd l" and "lcm b c dvd l"
875     have "b dvd lcm b c" by simp
876     from this and \<open>lcm b c dvd l\<close> have "b dvd l" by (rule dvd_trans)
877     have "c dvd lcm b c" by simp
878     from this and \<open>lcm b c dvd l\<close> have "c dvd l" by (rule dvd_trans)
879     from \<open>a dvd l\<close> and \<open>b dvd l\<close> have "lcm a b dvd l" by (rule lcm_least)
880     from this and \<open>c dvd l\<close> show "lcm (lcm a b) c dvd l" by (rule lcm_least)
881   qed (simp add: lcm_zero)
882 next
883   fix a b
884   show "lcm a b = lcm b a"
885     by (simp add: lcm_gcd ac_simps)
886 qed
888 lemma dvd_lcm_D1:
889   "lcm m n dvd k \<Longrightarrow> m dvd k"
890   by (rule dvd_trans, rule lcm_dvd1, assumption)
892 lemma dvd_lcm_D2:
893   "lcm m n dvd k \<Longrightarrow> n dvd k"
894   by (rule dvd_trans, rule lcm_dvd2, assumption)
896 lemma gcd_dvd_lcm [simp]:
897   "gcd a b dvd lcm a b"
898   by (metis dvd_trans gcd_dvd2 lcm_dvd2)
900 lemma lcm_1_iff:
901   "lcm a b = 1 \<longleftrightarrow> is_unit a \<and> is_unit b"
902 proof
903   assume "lcm a b = 1"
904   then show "is_unit a \<and> is_unit b" by auto
905 next
906   assume "is_unit a \<and> is_unit b"
907   hence "a dvd 1" and "b dvd 1" by simp_all
908   hence "is_unit (lcm a b)" by (rule lcm_least)
909   hence "lcm a b = unit_factor (lcm a b)"
910     by (blast intro: sym is_unit_unit_factor)
911   also have "\<dots> = 1" using \<open>is_unit a \<and> is_unit b\<close>
912     by auto
913   finally show "lcm a b = 1" .
914 qed
916 lemma lcm_0_left [simp]:
917   "lcm 0 a = 0"
918   by (rule sym, rule lcmI, simp_all)
920 lemma lcm_0 [simp]:
921   "lcm a 0 = 0"
922   by (rule sym, rule lcmI, simp_all)
924 lemma lcm_unique:
925   "a dvd d \<and> b dvd d \<and>
926   unit_factor d = (if d = 0 then 0 else 1) \<and>
927   (\<forall>e. a dvd e \<and> b dvd e \<longrightarrow> d dvd e) \<longleftrightarrow> d = lcm a b"
928   by (rule, auto intro: lcmI simp: lcm_least lcm_zero)
930 lemma dvd_lcm_I1 [simp]:
931   "k dvd m \<Longrightarrow> k dvd lcm m n"
932   by (metis lcm_dvd1 dvd_trans)
934 lemma dvd_lcm_I2 [simp]:
935   "k dvd n \<Longrightarrow> k dvd lcm m n"
936   by (metis lcm_dvd2 dvd_trans)
938 lemma lcm_1_left [simp]:
939   "lcm 1 a = normalize a"
940   by (cases "a = 0") (simp, rule sym, rule lcmI, simp_all)
942 lemma lcm_1_right [simp]:
943   "lcm a 1 = normalize a"
944   using lcm_1_left [of a] by (simp add: ac_simps)
946 lemma lcm_coprime:
947   "gcd a b = 1 \<Longrightarrow> lcm a b = normalize (a * b)"
948   by (subst lcm_gcd) simp
950 lemma lcm_proj1_if_dvd:
951   "b dvd a \<Longrightarrow> lcm a b = normalize a"
952   by (cases "a = 0") (simp, rule sym, rule lcmI, simp_all)
954 lemma lcm_proj2_if_dvd:
955   "a dvd b \<Longrightarrow> lcm a b = normalize b"
956   using lcm_proj1_if_dvd [of a b] by (simp add: ac_simps)
958 lemma lcm_proj1_iff:
959   "lcm m n = normalize m \<longleftrightarrow> n dvd m"
960 proof
961   assume A: "lcm m n = normalize m"
962   show "n dvd m"
963   proof (cases "m = 0")
964     assume [simp]: "m \<noteq> 0"
965     from A have B: "m = lcm m n * unit_factor m"
966       by (simp add: unit_eq_div2)
967     show ?thesis by (subst B, simp)
968   qed simp
969 next
970   assume "n dvd m"
971   then show "lcm m n = normalize m" by (rule lcm_proj1_if_dvd)
972 qed
974 lemma lcm_proj2_iff:
975   "lcm m n = normalize n \<longleftrightarrow> m dvd n"
976   using lcm_proj1_iff [of n m] by (simp add: ac_simps)
978 lemma euclidean_size_lcm_le1:
979   assumes "a \<noteq> 0" and "b \<noteq> 0"
980   shows "euclidean_size a \<le> euclidean_size (lcm a b)"
981 proof -
982   have "a dvd lcm a b" by (rule lcm_dvd1)
983   then obtain c where A: "lcm a b = a * c" unfolding dvd_def by blast
984   with \<open>a \<noteq> 0\<close> and \<open>b \<noteq> 0\<close> have "c \<noteq> 0" by (auto simp: lcm_zero)
985   then show ?thesis by (subst A, intro size_mult_mono)
986 qed
988 lemma euclidean_size_lcm_le2:
989   "a \<noteq> 0 \<Longrightarrow> b \<noteq> 0 \<Longrightarrow> euclidean_size b \<le> euclidean_size (lcm a b)"
990   using euclidean_size_lcm_le1 [of b a] by (simp add: ac_simps)
992 lemma euclidean_size_lcm_less1:
993   assumes "b \<noteq> 0" and "\<not>b dvd a"
994   shows "euclidean_size a < euclidean_size (lcm a b)"
995 proof (rule ccontr)
996   from assms have "a \<noteq> 0" by auto
997   assume "\<not>euclidean_size a < euclidean_size (lcm a b)"
998   with \<open>a \<noteq> 0\<close> and \<open>b \<noteq> 0\<close> have "euclidean_size (lcm a b) = euclidean_size a"
999     by (intro le_antisym, simp, intro euclidean_size_lcm_le1)
1000   with assms have "lcm a b dvd a"
1001     by (rule_tac dvd_euclidean_size_eq_imp_dvd) (auto simp: lcm_zero)
1002   hence "b dvd a" by (rule dvd_lcm_D2)
1003   with \<open>\<not>b dvd a\<close> show False by contradiction
1004 qed
1006 lemma euclidean_size_lcm_less2:
1007   assumes "a \<noteq> 0" and "\<not>a dvd b"
1008   shows "euclidean_size b < euclidean_size (lcm a b)"
1009   using assms euclidean_size_lcm_less1 [of a b] by (simp add: ac_simps)
1011 lemma lcm_mult_unit1:
1012   "is_unit a \<Longrightarrow> lcm (b * a) c = lcm b c"
1013   apply (rule lcmI)
1014   apply (rule dvd_trans[of _ "b * a"], simp, rule lcm_dvd1)
1015   apply (rule lcm_dvd2)
1016   apply (rule lcm_least, simp add: unit_simps, assumption)
1017   apply (subst unit_factor_lcm, simp add: lcm_zero)
1018   done
1020 lemma lcm_mult_unit2:
1021   "is_unit a \<Longrightarrow> lcm b (c * a) = lcm b c"
1022   using lcm_mult_unit1 [of a c b] by (simp add: ac_simps)
1024 lemma lcm_div_unit1:
1025   "is_unit a \<Longrightarrow> lcm (b div a) c = lcm b c"
1026   by (erule is_unitE [of _ b]) (simp add: lcm_mult_unit1)
1028 lemma lcm_div_unit2:
1029   "is_unit a \<Longrightarrow> lcm b (c div a) = lcm b c"
1030   by (erule is_unitE [of _ c]) (simp add: lcm_mult_unit2)
1032 lemma normalize_lcm_left [simp]:
1033   "lcm (normalize a) b = lcm a b"
1034 proof (cases "a = 0")
1035   case True then show ?thesis
1036     by simp
1037 next
1038   case False then have "is_unit (unit_factor a)"
1039     by simp
1040   moreover have "normalize a = a div unit_factor a"
1041     by simp
1042   ultimately show ?thesis
1043     by (simp only: lcm_div_unit1)
1044 qed
1046 lemma normalize_lcm_right [simp]:
1047   "lcm a (normalize b) = lcm a b"
1048   using normalize_lcm_left [of b a] by (simp add: ac_simps)
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_least: "(\<And>a. a \<in> A \<Longrightarrow> a dvd b) \<Longrightarrow> Lcm A dvd b"
1080   and unit_factor_Lcm [simp]:
1081           "unit_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     unit_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 unit_factor_is_unit[OF \<open>l \<noteq> 0\<close>] and \<open>l \<noteq> 0\<close>
1127       have "(\<forall>a\<in>A. a dvd normalize l) \<and>
1128         (\<forall>l'. (\<forall>a\<in>A. a dvd l') \<longrightarrow> normalize l dvd l') \<and>
1129         unit_factor (normalize l) =
1130         (if normalize l = 0 then 0 else 1)"
1131       by (auto simp: unit_simps)
1132     also from True have "normalize 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 b assume "\<And>a. a \<in> A \<Longrightarrow> a dvd b" then show "Lcm A dvd b" using A by blast}
1140   from A show "unit_factor (Lcm A) = (if Lcm A = 0 then 0 else 1)" by blast
1141 qed
1143 lemma normalize_Lcm [simp]:
1144   "normalize (Lcm A) = Lcm A"
1145   by (cases "Lcm A = 0") (auto intro: associated_eqI)
1147 lemma LcmI:
1148   assumes "\<And>a. a \<in> A \<Longrightarrow> a dvd b" and "\<And>c. (\<And>a. a \<in> A \<Longrightarrow> a dvd c) \<Longrightarrow> b dvd c"
1149     and "unit_factor b = (if b = 0 then 0 else 1)" shows "b = Lcm A"
1150   by (rule associated_eqI) (auto simp: assms associated_def intro: Lcm_least)
1152 lemma Lcm_subset:
1153   "A \<subseteq> B \<Longrightarrow> Lcm A dvd Lcm B"
1154   by (blast intro: Lcm_least dvd_Lcm)
1156 lemma Lcm_Un:
1157   "Lcm (A \<union> B) = lcm (Lcm A) (Lcm B)"
1158   apply (rule lcmI)
1159   apply (blast intro: Lcm_subset)
1160   apply (blast intro: Lcm_subset)
1161   apply (intro Lcm_least ballI, elim UnE)
1162   apply (rule dvd_trans, erule dvd_Lcm, assumption)
1163   apply (rule dvd_trans, erule dvd_Lcm, assumption)
1164   apply simp
1165   done
1167 lemma Lcm_1_iff:
1168   "Lcm A = 1 \<longleftrightarrow> (\<forall>a\<in>A. is_unit a)"
1169 proof
1170   assume "Lcm A = 1"
1171   then show "\<forall>a\<in>A. is_unit a" by auto
1172 qed (rule LcmI [symmetric], auto)
1174 lemma Lcm_no_units:
1175   "Lcm A = Lcm (A - {a. is_unit a})"
1176 proof -
1177   have "(A - {a. is_unit a}) \<union> {a\<in>A. is_unit a} = A" by blast
1178   hence "Lcm A = lcm (Lcm (A - {a. is_unit a})) (Lcm {a\<in>A. is_unit a})"
1179     by (simp add: Lcm_Un [symmetric])
1180   also have "Lcm {a\<in>A. is_unit a} = 1" by (simp add: Lcm_1_iff)
1181   finally show ?thesis by simp
1182 qed
1184 lemma Lcm_empty [simp]:
1185   "Lcm {} = 1"
1186   by (simp add: Lcm_1_iff)
1188 lemma Lcm_eq_0 [simp]:
1189   "0 \<in> A \<Longrightarrow> Lcm A = 0"
1190   by (drule dvd_Lcm) simp
1192 lemma Lcm0_iff':
1193   "Lcm A = 0 \<longleftrightarrow> \<not>(\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l))"
1194 proof
1195   assume "Lcm A = 0"
1196   show "\<not>(\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l))"
1197   proof
1198     assume ex: "\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l)"
1199     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
1200     def n \<equiv> "LEAST n. \<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n"
1201     def l \<equiv> "SOME l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n"
1202     have "\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n"
1203       apply (subst n_def)
1204       apply (rule LeastI[of _ "euclidean_size l\<^sub>0"])
1205       apply (rule exI[of _ l\<^sub>0])
1206       apply (simp add: l\<^sub>0_props)
1207       done
1208     from someI_ex[OF this] have "l \<noteq> 0" unfolding l_def by simp_all
1209     hence "normalize l \<noteq> 0" by simp
1210     also from ex have "normalize l = Lcm A"
1211        by (simp only: Lcm_Lcm_eucl Lcm_eucl_def n_def l_def if_True Let_def)
1212     finally show False using \<open>Lcm A = 0\<close> by contradiction
1213   qed
1214 qed (simp only: Lcm_Lcm_eucl Lcm_eucl_def if_False)
1216 lemma Lcm0_iff [simp]:
1217   "finite A \<Longrightarrow> Lcm A = 0 \<longleftrightarrow> 0 \<in> A"
1218 proof -
1219   assume "finite A"
1220   have "0 \<in> A \<Longrightarrow> Lcm A = 0"  by (intro dvd_0_left dvd_Lcm)
1221   moreover {
1222     assume "0 \<notin> A"
1223     hence "\<Prod>A \<noteq> 0"
1224       apply (induct rule: finite_induct[OF \<open>finite A\<close>])
1225       apply simp
1226       apply (subst setprod.insert, assumption, assumption)
1227       apply (rule no_zero_divisors)
1228       apply blast+
1229       done
1230     moreover from \<open>finite A\<close> have "\<forall>a\<in>A. a dvd \<Prod>A" by blast
1231     ultimately have "\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l)" by blast
1232     with Lcm0_iff' have "Lcm A \<noteq> 0" by simp
1233   }
1234   ultimately show "Lcm A = 0 \<longleftrightarrow> 0 \<in> A" by blast
1235 qed
1237 lemma Lcm_no_multiple:
1238   "(\<forall>m. m \<noteq> 0 \<longrightarrow> (\<exists>a\<in>A. \<not>a dvd m)) \<Longrightarrow> Lcm A = 0"
1239 proof -
1240   assume "\<forall>m. m \<noteq> 0 \<longrightarrow> (\<exists>a\<in>A. \<not>a dvd m)"
1241   hence "\<not>(\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l))" by blast
1242   then show "Lcm A = 0" by (simp only: Lcm_Lcm_eucl Lcm_eucl_def if_False)
1243 qed
1245 lemma Lcm_insert [simp]:
1246   "Lcm (insert a A) = lcm a (Lcm A)"
1247 proof (rule lcmI)
1248   fix l assume "a dvd l" and "Lcm A dvd l"
1249   hence "\<forall>a\<in>A. a dvd l" by (blast intro: dvd_trans dvd_Lcm)
1250   with \<open>a dvd l\<close> show "Lcm (insert a A) dvd l" by (force intro: Lcm_least)
1251 qed (auto intro: Lcm_least dvd_Lcm)
1253 lemma Lcm_finite:
1254   assumes "finite A"
1255   shows "Lcm A = Finite_Set.fold lcm 1 A"
1256   by (induct rule: finite.induct[OF \<open>finite A\<close>])
1257     (simp_all add: comp_fun_idem.fold_insert_idem[OF comp_fun_idem_lcm])
1259 lemma Lcm_set [code_unfold]:
1260   "Lcm (set xs) = fold lcm xs 1"
1261   using comp_fun_idem.fold_set_fold[OF comp_fun_idem_lcm] Lcm_finite by (simp add: ac_simps)
1263 lemma Lcm_singleton [simp]:
1264   "Lcm {a} = normalize a"
1265   by simp
1267 lemma Lcm_2 [simp]:
1268   "Lcm {a,b} = lcm a b"
1269   by simp
1271 lemma Lcm_coprime:
1272   assumes "finite A" and "A \<noteq> {}"
1273   assumes "\<And>a b. a \<in> A \<Longrightarrow> b \<in> A \<Longrightarrow> a \<noteq> b \<Longrightarrow> gcd a b = 1"
1274   shows "Lcm A = normalize (\<Prod>A)"
1275 using assms proof (induct rule: finite_ne_induct)
1276   case (insert a A)
1277   have "Lcm (insert a A) = lcm a (Lcm A)" by simp
1278   also from insert have "Lcm A = normalize (\<Prod>A)" by blast
1279   also have "lcm a \<dots> = lcm a (\<Prod>A)" by (cases "\<Prod>A = 0") (simp_all add: lcm_div_unit2)
1280   also from insert have "gcd a (\<Prod>A) = 1" by (subst gcd.commute, intro setprod_coprime) auto
1281   with insert have "lcm a (\<Prod>A) = normalize (\<Prod>(insert a A))"
1282     by (simp add: lcm_coprime)
1283   finally show ?case .
1284 qed simp
1286 lemma Lcm_coprime':
1287   "card A \<noteq> 0 \<Longrightarrow> (\<And>a b. a \<in> A \<Longrightarrow> b \<in> A \<Longrightarrow> a \<noteq> b \<Longrightarrow> gcd a b = 1)
1288     \<Longrightarrow> Lcm A = normalize (\<Prod>A)"
1289   by (rule Lcm_coprime) (simp_all add: card_eq_0_iff)
1291 lemma Gcd_Lcm:
1292   "Gcd A = Lcm {d. \<forall>a\<in>A. d dvd a}"
1293   by (simp add: Gcd_Gcd_eucl Lcm_Lcm_eucl Gcd_eucl_def)
1295 lemma Gcd_dvd [simp]: "a \<in> A \<Longrightarrow> Gcd A dvd a"
1296   and Gcd_greatest: "(\<And>a. a \<in> A \<Longrightarrow> b dvd a) \<Longrightarrow> b dvd Gcd A"
1297   and unit_factor_Gcd [simp]:
1298     "unit_factor (Gcd A) = (if Gcd A = 0 then 0 else 1)"
1299 proof -
1300   fix a assume "a \<in> A"
1301   hence "Lcm {d. \<forall>a\<in>A. d dvd a} dvd a" by (intro Lcm_least) blast
1302   then show "Gcd A dvd a" by (simp add: Gcd_Lcm)
1303 next
1304   fix g' assume "\<And>a. a \<in> A \<Longrightarrow> g' dvd a"
1305   hence "g' dvd Lcm {d. \<forall>a\<in>A. d dvd a}" by (intro dvd_Lcm) blast
1306   then show "g' dvd Gcd A" by (simp add: Gcd_Lcm)
1307 next
1308   show "unit_factor (Gcd A) = (if Gcd A = 0 then 0 else 1)"
1309     by (simp add: Gcd_Lcm)
1310 qed
1312 lemma normalize_Gcd [simp]:
1313   "normalize (Gcd A) = Gcd A"
1314   by (cases "Gcd A = 0") (auto intro: associated_eqI)
1316 lemma GcdI:
1317   assumes "\<And>a. a \<in> A \<Longrightarrow> b dvd a" and "\<And>c. (\<And>a. a \<in> A \<Longrightarrow> c dvd a) \<Longrightarrow> c dvd b"
1318     and "unit_factor b = (if b = 0 then 0 else 1)"
1319   shows "b = Gcd A"
1320   by (rule associated_eqI) (auto simp: assms associated_def intro: Gcd_greatest)
1322 lemma Lcm_Gcd:
1323   "Lcm A = Gcd {m. \<forall>a\<in>A. a dvd m}"
1324   by (rule LcmI[symmetric]) (auto intro: dvd_Gcd Gcd_greatest)
1326 lemma Gcd_0_iff:
1327   "Gcd A = 0 \<longleftrightarrow> A \<subseteq> {0}"
1328   apply (rule iffI)
1329   apply (rule subsetI, drule Gcd_dvd, simp)
1330   apply (auto intro: GcdI[symmetric])
1331   done
1333 lemma Gcd_empty [simp]:
1334   "Gcd {} = 0"
1335   by (simp add: Gcd_0_iff)
1337 lemma Gcd_1:
1338   "1 \<in> A \<Longrightarrow> Gcd A = 1"
1339   by (intro GcdI[symmetric]) (auto intro: Gcd_dvd dvd_Gcd)
1341 lemma Gcd_insert [simp]:
1342   "Gcd (insert a A) = gcd a (Gcd A)"
1343 proof (rule gcdI)
1344   fix l assume "l dvd a" and "l dvd Gcd A"
1345   hence "\<forall>a\<in>A. l dvd a" by (blast intro: dvd_trans Gcd_dvd)
1346   with \<open>l dvd a\<close> show "l dvd Gcd (insert a A)" by (force intro: Gcd_dvd Gcd_greatest)
1347 qed (auto intro: Gcd_greatest)
1349 lemma Gcd_finite:
1350   assumes "finite A"
1351   shows "Gcd A = Finite_Set.fold gcd 0 A"
1352   by (induct rule: finite.induct[OF \<open>finite A\<close>])
1353     (simp_all add: comp_fun_idem.fold_insert_idem[OF comp_fun_idem_gcd])
1355 lemma Gcd_set [code_unfold]:
1356   "Gcd (set xs) = fold gcd xs 0"
1357   using comp_fun_idem.fold_set_fold[OF comp_fun_idem_gcd] Gcd_finite by (simp add: ac_simps)
1359 lemma Gcd_singleton [simp]: "Gcd {a} = normalize a"
1360   by (simp add: gcd_0)
1362 lemma Gcd_2 [simp]: "Gcd {a,b} = gcd a b"
1363   by (simp add: gcd_0)
1365 subclass semiring_gcd
1366   apply standard
1367   using gcd_right_idem
1368   apply (simp_all add: lcm_gcd gcd_greatest_iff gcd_proj1_if_dvd)
1369   done
1371 subclass semiring_Gcd
1372   by standard (simp_all add: Gcd_greatest)
1374 subclass semiring_Lcm
1375   by standard (simp add: Lcm_Gcd)
1377 end
1379 text \<open>
1380   A Euclidean ring is a Euclidean semiring with additive inverses. It provides a
1381   few more lemmas; in particular, Bezout's lemma holds for any Euclidean ring.
1382 \<close>
1384 class euclidean_ring_gcd = euclidean_semiring_gcd + idom
1385 begin
1387 subclass euclidean_ring ..
1389 subclass ring_gcd ..
1391 lemma euclid_ext_gcd [simp]:
1392   "(case euclid_ext a b of (_, _ , t) \<Rightarrow> t) = gcd a b"
1393   by (induct a b rule: gcd_eucl_induct)
1394     (simp_all add: euclid_ext_0 euclid_ext_non_0 ac_simps split: prod.split prod.split_asm)
1396 lemma euclid_ext_gcd' [simp]:
1397   "euclid_ext a b = (r, s, t) \<Longrightarrow> t = gcd a b"
1398   by (insert euclid_ext_gcd[of a b], drule (1) subst, simp)
1400 lemma euclid_ext'_correct:
1401   "fst (euclid_ext' a b) * a + snd (euclid_ext' a b) * b = gcd a b"
1402 proof-
1403   obtain s t c where "euclid_ext a b = (s,t,c)"
1404     by (cases "euclid_ext a b", blast)
1405   with euclid_ext_correct[of a b] euclid_ext_gcd[of a b]
1406     show ?thesis unfolding euclid_ext'_def by simp
1407 qed
1409 lemma bezout: "\<exists>s t. s * a + t * b = gcd a b"
1410   using euclid_ext'_correct by blast
1412 lemma gcd_neg1 [simp]:
1413   "gcd (-a) b = gcd a b"
1414   by (rule sym, rule gcdI, simp_all add: gcd_greatest)
1416 lemma gcd_neg2 [simp]:
1417   "gcd a (-b) = gcd a b"
1418   by (rule sym, rule gcdI, simp_all add: gcd_greatest)
1420 lemma gcd_neg_numeral_1 [simp]:
1421   "gcd (- numeral n) a = gcd (numeral n) a"
1422   by (fact gcd_neg1)
1424 lemma gcd_neg_numeral_2 [simp]:
1425   "gcd a (- numeral n) = gcd a (numeral n)"
1426   by (fact gcd_neg2)
1428 lemma gcd_diff1: "gcd (m - n) n = gcd m n"
1429   by (subst diff_conv_add_uminus, subst gcd_neg2[symmetric],  subst gcd_add1, simp)
1431 lemma gcd_diff2: "gcd (n - m) n = gcd m n"
1432   by (subst gcd_neg1[symmetric], simp only: minus_diff_eq gcd_diff1)
1434 lemma coprime_minus_one [simp]: "gcd (n - 1) n = 1"
1435 proof -
1436   have "gcd (n - 1) n = gcd n (n - 1)" by (fact gcd.commute)
1437   also have "\<dots> = gcd ((n - 1) + 1) (n - 1)" by simp
1438   also have "\<dots> = 1" by (rule coprime_plus_one)
1439   finally show ?thesis .
1440 qed
1442 lemma lcm_neg1 [simp]: "lcm (-a) b = lcm a b"
1443   by (rule sym, rule lcmI, simp_all add: lcm_least lcm_zero)
1445 lemma lcm_neg2 [simp]: "lcm a (-b) = lcm a b"
1446   by (rule sym, rule lcmI, simp_all add: lcm_least lcm_zero)
1448 lemma lcm_neg_numeral_1 [simp]: "lcm (- numeral n) a = lcm (numeral n) a"
1449   by (fact lcm_neg1)
1451 lemma lcm_neg_numeral_2 [simp]: "lcm a (- numeral n) = lcm a (numeral n)"
1452   by (fact lcm_neg2)
1454 end
1457 subsection \<open>Typical instances\<close>
1459 instantiation nat :: euclidean_semiring
1460 begin
1462 definition [simp]:
1463   "euclidean_size_nat = (id :: nat \<Rightarrow> nat)"
1465 instance proof
1466 qed simp_all
1468 end
1470 instantiation int :: euclidean_ring
1471 begin
1473 definition [simp]:
1474   "euclidean_size_int = (nat \<circ> abs :: int \<Rightarrow> nat)"
1476 instance
1477 by standard (auto simp add: abs_mult nat_mult_distrib split: abs_split)
1479 end
1481 instantiation poly :: (field) euclidean_ring
1482 begin
1484 definition euclidean_size_poly :: "'a poly \<Rightarrow> nat"
1485   where "euclidean_size p = (if p = 0 then 0 else Suc (degree p))"
1487 lemma euclidenan_size_poly_minus_one_degree [simp]:
1488   "euclidean_size p - 1 = degree p"
1489   by (simp add: euclidean_size_poly_def)
1491 lemma euclidean_size_poly_0 [simp]:
1492   "euclidean_size (0::'a poly) = 0"
1493   by (simp add: euclidean_size_poly_def)
1495 lemma euclidean_size_poly_not_0 [simp]:
1496   "p \<noteq> 0 \<Longrightarrow> euclidean_size p = Suc (degree p)"
1497   by (simp add: euclidean_size_poly_def)
1499 instance
1500 proof
1501   fix p q :: "'a poly"
1502   assume "q \<noteq> 0"
1503   then have "p mod q = 0 \<or> degree (p mod q) < degree q"
1504     by (rule degree_mod_less [of q p])
1505   with \<open>q \<noteq> 0\<close> show "euclidean_size (p mod q) < euclidean_size q"
1506     by (cases "p mod q = 0") simp_all
1507 next
1508   fix p q :: "'a poly"
1509   assume "q \<noteq> 0"
1510   from \<open>q \<noteq> 0\<close> have "degree p \<le> degree (p * q)"
1511     by (rule degree_mult_right_le)
1512   with \<open>q \<noteq> 0\<close> show "euclidean_size p \<le> euclidean_size (p * q)"
1513     by (cases "p = 0") simp_all
1514 qed
1516 end
1518 end