src/HOL/Number_Theory/Euclidean_Algorithm.thy
 author Manuel Eberl Fri Feb 26 22:15:09 2016 +0100 (2016-02-26) changeset 62429 25271ff79171 parent 62428 4d5fbec92bb1 child 62442 26e4be6a680f permissions -rw-r--r--
Tuned Euclidean Rings/GCD rings
1 (* Author: Manuel Eberl *)
3 section \<open>Abstract euclidean algorithm\<close>
5 theory Euclidean_Algorithm
6 imports "~~/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 size_0 [simp]: "euclidean_size 0 = 0"
23   assumes mod_size_less:
24     "b \<noteq> 0 \<Longrightarrow> euclidean_size (a mod b) < euclidean_size b"
25   assumes size_mult_mono:
26     "b \<noteq> 0 \<Longrightarrow> euclidean_size a \<le> euclidean_size (a * b)"
27 begin
29 lemma euclidean_division:
30   fixes a :: 'a and b :: 'a
31   assumes "b \<noteq> 0"
32   obtains s and t where "a = s * b + t"
33     and "euclidean_size t < euclidean_size b"
34 proof -
35   from div_mod_equality [of a b 0]
36      have "a = a div b * b + a mod b" by simp
37   with that and assms show ?thesis by (auto simp add: mod_size_less)
38 qed
40 lemma dvd_euclidean_size_eq_imp_dvd:
41   assumes "a \<noteq> 0" and b_dvd_a: "b dvd a" and size_eq: "euclidean_size a = euclidean_size b"
42   shows "a dvd b"
43 proof (rule ccontr)
44   assume "\<not> a dvd b"
45   then have "b mod a \<noteq> 0" by (simp add: mod_eq_0_iff_dvd)
46   from b_dvd_a have b_dvd_mod: "b dvd b mod a" by (simp add: dvd_mod_iff)
47   from b_dvd_mod obtain c where "b mod a = b * c" unfolding dvd_def by blast
48     with \<open>b mod a \<noteq> 0\<close> have "c \<noteq> 0" by auto
49   with \<open>b mod a = b * c\<close> have "euclidean_size (b mod a) \<ge> euclidean_size b"
50       using size_mult_mono by force
51   moreover from \<open>\<not> a dvd b\<close> and \<open>a \<noteq> 0\<close>
52   have "euclidean_size (b mod a) < euclidean_size a"
53       using mod_size_less by blast
54   ultimately show False using size_eq by simp
55 qed
57 function gcd_eucl :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"
58 where
59   "gcd_eucl a b = (if b = 0 then normalize a else gcd_eucl b (a mod b))"
60   by pat_completeness simp
61 termination
62   by (relation "measure (euclidean_size \<circ> snd)") (simp_all add: mod_size_less)
64 declare gcd_eucl.simps [simp del]
66 lemma gcd_eucl_induct [case_names zero mod]:
67   assumes H1: "\<And>b. P b 0"
68   and H2: "\<And>a b. b \<noteq> 0 \<Longrightarrow> P b (a mod b) \<Longrightarrow> P a b"
69   shows "P a b"
70 proof (induct a b rule: gcd_eucl.induct)
71   case ("1" a b)
72   show ?case
73   proof (cases "b = 0")
74     case True then show "P a b" by simp (rule H1)
75   next
76     case False
77     then have "P b (a mod b)"
78       by (rule "1.hyps")
79     with \<open>b \<noteq> 0\<close> show "P a b"
80       by (blast intro: H2)
81   qed
82 qed
84 definition lcm_eucl :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"
85 where
86   "lcm_eucl a b = normalize (a * b) div gcd_eucl a b"
88 definition Lcm_eucl :: "'a set \<Rightarrow> 'a" -- \<open>
89   Somewhat complicated definition of Lcm that has the advantage of working
90   for infinite sets as well\<close>
91 where
92   "Lcm_eucl A = (if \<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) then
93      let l = SOME l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l =
94        (LEAST n. \<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n)
95        in normalize l
96       else 0)"
98 definition Gcd_eucl :: "'a set \<Rightarrow> 'a"
99 where
100   "Gcd_eucl A = Lcm_eucl {d. \<forall>a\<in>A. d dvd a}"
102 declare Lcm_eucl_def Gcd_eucl_def [code del]
104 lemma gcd_eucl_0:
105   "gcd_eucl a 0 = normalize a"
106   by (simp add: gcd_eucl.simps [of a 0])
108 lemma gcd_eucl_0_left:
109   "gcd_eucl 0 a = normalize a"
110   by (simp_all add: gcd_eucl_0 gcd_eucl.simps [of 0 a])
112 lemma gcd_eucl_non_0:
113   "b \<noteq> 0 \<Longrightarrow> gcd_eucl a b = gcd_eucl b (a mod b)"
114   by (simp add: gcd_eucl.simps [of a b] gcd_eucl.simps [of b 0])
116 lemma gcd_eucl_dvd1 [iff]: "gcd_eucl a b dvd a"
117   and gcd_eucl_dvd2 [iff]: "gcd_eucl a b dvd b"
118   by (induct a b rule: gcd_eucl_induct)
119      (simp_all add: gcd_eucl_0 gcd_eucl_non_0 dvd_mod_iff)
121 lemma normalize_gcd_eucl [simp]:
122   "normalize (gcd_eucl a b) = gcd_eucl a b"
123   by (induct a b rule: gcd_eucl_induct) (simp_all add: gcd_eucl_0 gcd_eucl_non_0)
125 lemma gcd_eucl_greatest:
126   fixes k a b :: 'a
127   shows "k dvd a \<Longrightarrow> k dvd b \<Longrightarrow> k dvd gcd_eucl a b"
128 proof (induct a b rule: gcd_eucl_induct)
129   case (zero a) from zero(1) show ?case by (rule dvd_trans) (simp add: gcd_eucl_0)
130 next
131   case (mod a b)
132   then show ?case
133     by (simp add: gcd_eucl_non_0 dvd_mod_iff)
134 qed
136 lemma eq_gcd_euclI:
137   fixes gcd :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"
138   assumes "\<And>a b. gcd a b dvd a" "\<And>a b. gcd a b dvd b" "\<And>a b. normalize (gcd a b) = gcd a b"
139           "\<And>a b k. k dvd a \<Longrightarrow> k dvd b \<Longrightarrow> k dvd gcd a b"
140   shows   "gcd = gcd_eucl"
141   by (intro ext, rule associated_eqI) (simp_all add: gcd_eucl_greatest assms)
143 lemma gcd_eucl_zero [simp]:
144   "gcd_eucl a b = 0 \<longleftrightarrow> a = 0 \<and> b = 0"
145   by (metis dvd_0_left dvd_refl gcd_eucl_dvd1 gcd_eucl_dvd2 gcd_eucl_greatest)+
148 lemma dvd_Lcm_eucl [simp]: "a \<in> A \<Longrightarrow> a dvd Lcm_eucl A"
149   and Lcm_eucl_least: "(\<And>a. a \<in> A \<Longrightarrow> a dvd b) \<Longrightarrow> Lcm_eucl A dvd b"
150   and unit_factor_Lcm_eucl [simp]:
151           "unit_factor (Lcm_eucl A) = (if Lcm_eucl A = 0 then 0 else 1)"
152 proof -
153   have "(\<forall>a\<in>A. a dvd Lcm_eucl A) \<and> (\<forall>l'. (\<forall>a\<in>A. a dvd l') \<longrightarrow> Lcm_eucl A dvd l') \<and>
154     unit_factor (Lcm_eucl A) = (if Lcm_eucl A = 0 then 0 else 1)" (is ?thesis)
155   proof (cases "\<exists>l. l \<noteq>  0 \<and> (\<forall>a\<in>A. a dvd l)")
156     case False
157     hence "Lcm_eucl A = 0" by (auto simp: Lcm_eucl_def)
158     with False show ?thesis by auto
159   next
160     case True
161     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
162     def n \<equiv> "LEAST n. \<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n"
163     def l \<equiv> "SOME l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n"
164     have "\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n"
165       apply (subst n_def)
166       apply (rule LeastI[of _ "euclidean_size l\<^sub>0"])
167       apply (rule exI[of _ l\<^sub>0])
168       apply (simp add: l\<^sub>0_props)
169       done
170     from someI_ex[OF this] have "l \<noteq> 0" and "\<forall>a\<in>A. a dvd l" and "euclidean_size l = n"
171       unfolding l_def by simp_all
172     {
173       fix l' assume "\<forall>a\<in>A. a dvd l'"
174       with \<open>\<forall>a\<in>A. a dvd l\<close> have "\<forall>a\<in>A. a dvd gcd_eucl l l'" by (auto intro: gcd_eucl_greatest)
175       moreover from \<open>l \<noteq> 0\<close> have "gcd_eucl l l' \<noteq> 0" by simp
176       ultimately have "\<exists>b. b \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd b) \<and>
177                           euclidean_size b = euclidean_size (gcd_eucl l l')"
178         by (intro exI[of _ "gcd_eucl l l'"], auto)
179       hence "euclidean_size (gcd_eucl l l') \<ge> n" by (subst n_def) (rule Least_le)
180       moreover have "euclidean_size (gcd_eucl l l') \<le> n"
181       proof -
182         have "gcd_eucl l l' dvd l" by simp
183         then obtain a where "l = gcd_eucl l l' * a" unfolding dvd_def by blast
184         with \<open>l \<noteq> 0\<close> have "a \<noteq> 0" by auto
185         hence "euclidean_size (gcd_eucl l l') \<le> euclidean_size (gcd_eucl l l' * a)"
186           by (rule size_mult_mono)
187         also have "gcd_eucl l l' * a = l" using \<open>l = gcd_eucl l l' * a\<close> ..
188         also note \<open>euclidean_size l = n\<close>
189         finally show "euclidean_size (gcd_eucl l l') \<le> n" .
190       qed
191       ultimately have *: "euclidean_size l = euclidean_size (gcd_eucl l l')"
192         by (intro le_antisym, simp_all add: \<open>euclidean_size l = n\<close>)
193       from \<open>l \<noteq> 0\<close> have "l dvd gcd_eucl l l'"
194         by (rule dvd_euclidean_size_eq_imp_dvd) (auto simp add: *)
195       hence "l dvd l'" by (rule dvd_trans[OF _ gcd_eucl_dvd2])
196     }
198     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>
199       have "(\<forall>a\<in>A. a dvd normalize l) \<and>
200         (\<forall>l'. (\<forall>a\<in>A. a dvd l') \<longrightarrow> normalize l dvd l') \<and>
201         unit_factor (normalize l) =
202         (if normalize l = 0 then 0 else 1)"
203       by (auto simp: unit_simps)
204     also from True have "normalize l = Lcm_eucl A"
205       by (simp add: Lcm_eucl_def Let_def n_def l_def)
206     finally show ?thesis .
207   qed
208   note A = this
210   {fix a assume "a \<in> A" then show "a dvd Lcm_eucl A" using A by blast}
211   {fix b assume "\<And>a. a \<in> A \<Longrightarrow> a dvd b" then show "Lcm_eucl A dvd b" using A by blast}
212   from A show "unit_factor (Lcm_eucl A) = (if Lcm_eucl A = 0 then 0 else 1)" by blast
213 qed
215 lemma normalize_Lcm_eucl [simp]:
216   "normalize (Lcm_eucl A) = Lcm_eucl A"
217 proof (cases "Lcm_eucl A = 0")
218   case True then show ?thesis by simp
219 next
220   case False
221   have "unit_factor (Lcm_eucl A) * normalize (Lcm_eucl A) = Lcm_eucl A"
222     by (fact unit_factor_mult_normalize)
223   with False show ?thesis by simp
224 qed
226 lemma eq_Lcm_euclI:
227   fixes lcm :: "'a set \<Rightarrow> 'a"
228   assumes "\<And>A a. a \<in> A \<Longrightarrow> a dvd lcm A" and "\<And>A c. (\<And>a. a \<in> A \<Longrightarrow> a dvd c) \<Longrightarrow> lcm A dvd c"
229           "\<And>A. normalize (lcm A) = lcm A" shows "lcm = Lcm_eucl"
230   by (intro ext, rule associated_eqI) (auto simp: assms intro: Lcm_eucl_least)
232 end
234 class euclidean_ring = euclidean_semiring + idom
235 begin
237 function euclid_ext :: "'a \<Rightarrow> 'a \<Rightarrow> 'a \<times> 'a \<times> 'a" where
238   "euclid_ext a b =
239      (if b = 0 then
240         (1 div unit_factor a, 0, normalize a)
241       else
242         case euclid_ext b (a mod b) of
243             (s, t, c) \<Rightarrow> (t, s - t * (a div b), c))"
244   by pat_completeness simp
245 termination
246   by (relation "measure (euclidean_size \<circ> snd)") (simp_all add: mod_size_less)
248 declare euclid_ext.simps [simp del]
250 lemma euclid_ext_0:
251   "euclid_ext a 0 = (1 div unit_factor a, 0, normalize a)"
252   by (simp add: euclid_ext.simps [of a 0])
254 lemma euclid_ext_left_0:
255   "euclid_ext 0 a = (0, 1 div unit_factor a, normalize a)"
256   by (simp add: euclid_ext_0 euclid_ext.simps [of 0 a])
258 lemma euclid_ext_non_0:
259   "b \<noteq> 0 \<Longrightarrow> euclid_ext a b = (case euclid_ext b (a mod b) of
260     (s, t, c) \<Rightarrow> (t, s - t * (a div b), c))"
261   by (simp add: euclid_ext.simps [of a b] euclid_ext.simps [of b 0])
263 lemma euclid_ext_code [code]:
264   "euclid_ext a b = (if b = 0 then (1 div unit_factor a, 0, normalize a)
265     else let (s, t, c) = euclid_ext b (a mod b) in  (t, s - t * (a div b), c))"
266   by (simp add: euclid_ext.simps [of a b] euclid_ext.simps [of b 0])
268 lemma euclid_ext_correct:
269   "case euclid_ext a b of (s, t, c) \<Rightarrow> s * a + t * b = c"
270 proof (induct a b rule: gcd_eucl_induct)
271   case (zero a) then show ?case
272     by (simp add: euclid_ext_0 ac_simps)
273 next
274   case (mod a b)
275   obtain s t c where stc: "euclid_ext b (a mod b) = (s,t,c)"
276     by (cases "euclid_ext b (a mod b)") blast
277   with mod have "c = s * b + t * (a mod b)" by simp
278   also have "... = t * ((a div b) * b + a mod b) + (s - t * (a div b)) * b"
279     by (simp add: algebra_simps)
280   also have "(a div b) * b + a mod b = a" using mod_div_equality .
281   finally show ?case
282     by (subst euclid_ext.simps) (simp add: stc mod ac_simps)
283 qed
285 definition euclid_ext' :: "'a \<Rightarrow> 'a \<Rightarrow> 'a \<times> 'a"
286 where
287   "euclid_ext' a b = (case euclid_ext a b of (s, t, _) \<Rightarrow> (s, t))"
289 lemma euclid_ext'_0: "euclid_ext' a 0 = (1 div unit_factor a, 0)"
290   by (simp add: euclid_ext'_def euclid_ext_0)
292 lemma euclid_ext'_left_0: "euclid_ext' 0 a = (0, 1 div unit_factor a)"
293   by (simp add: euclid_ext'_def euclid_ext_left_0)
295 lemma euclid_ext'_non_0: "b \<noteq> 0 \<Longrightarrow> euclid_ext' a b = (snd (euclid_ext' b (a mod b)),
296   fst (euclid_ext' b (a mod b)) - snd (euclid_ext' b (a mod b)) * (a div b))"
297   by (simp add: euclid_ext'_def euclid_ext_non_0 split_def)
299 end
301 class euclidean_semiring_gcd = euclidean_semiring + gcd + Gcd +
302   assumes gcd_gcd_eucl: "gcd = gcd_eucl" and lcm_lcm_eucl: "lcm = lcm_eucl"
303   assumes Gcd_Gcd_eucl: "Gcd = Gcd_eucl" and Lcm_Lcm_eucl: "Lcm = Lcm_eucl"
304 begin
306 subclass semiring_gcd
307   by standard (simp_all add: gcd_gcd_eucl gcd_eucl_greatest lcm_lcm_eucl lcm_eucl_def)
309 subclass semiring_Gcd
310   by standard (auto simp: Gcd_Gcd_eucl Lcm_Lcm_eucl Gcd_eucl_def intro: Lcm_eucl_least)
312 lemma gcd_non_0:
313   "b \<noteq> 0 \<Longrightarrow> gcd a b = gcd b (a mod b)"
314   unfolding gcd_gcd_eucl by (fact gcd_eucl_non_0)
316 lemmas gcd_0 = gcd_0_right
317 lemmas dvd_gcd_iff = gcd_greatest_iff
318 lemmas gcd_greatest_iff = dvd_gcd_iff
320 lemma gcd_mod1 [simp]:
321   "gcd (a mod b) b = gcd a b"
322   by (rule gcdI, metis dvd_mod_iff gcd_dvd1 gcd_dvd2, simp_all add: gcd_greatest dvd_mod_iff)
324 lemma gcd_mod2 [simp]:
325   "gcd a (b mod a) = gcd a b"
326   by (rule gcdI, simp, metis dvd_mod_iff gcd_dvd1 gcd_dvd2, simp_all add: gcd_greatest dvd_mod_iff)
328 lemma euclidean_size_gcd_le1 [simp]:
329   assumes "a \<noteq> 0"
330   shows "euclidean_size (gcd a b) \<le> euclidean_size a"
331 proof -
332    have "gcd a b dvd a" by (rule gcd_dvd1)
333    then obtain c where A: "a = gcd a b * c" unfolding dvd_def by blast
334    with \<open>a \<noteq> 0\<close> show ?thesis by (subst (2) A, intro size_mult_mono) auto
335 qed
337 lemma euclidean_size_gcd_le2 [simp]:
338   "b \<noteq> 0 \<Longrightarrow> euclidean_size (gcd a b) \<le> euclidean_size b"
339   by (subst gcd.commute, rule euclidean_size_gcd_le1)
341 lemma euclidean_size_gcd_less1:
342   assumes "a \<noteq> 0" and "\<not>a dvd b"
343   shows "euclidean_size (gcd a b) < euclidean_size a"
344 proof (rule ccontr)
345   assume "\<not>euclidean_size (gcd a b) < euclidean_size a"
346   with \<open>a \<noteq> 0\<close> have A: "euclidean_size (gcd a b) = euclidean_size a"
347     by (intro le_antisym, simp_all)
348   have "a dvd gcd a b"
349     by (rule dvd_euclidean_size_eq_imp_dvd) (simp_all add: assms A)
350   hence "a dvd b" using dvd_gcdD2 by blast
351   with \<open>\<not>a dvd b\<close> show False by contradiction
352 qed
354 lemma euclidean_size_gcd_less2:
355   assumes "b \<noteq> 0" and "\<not>b dvd a"
356   shows "euclidean_size (gcd a b) < euclidean_size b"
357   using assms by (subst gcd.commute, rule euclidean_size_gcd_less1)
359 lemma euclidean_size_lcm_le1:
360   assumes "a \<noteq> 0" and "b \<noteq> 0"
361   shows "euclidean_size a \<le> euclidean_size (lcm a b)"
362 proof -
363   have "a dvd lcm a b" by (rule dvd_lcm1)
364   then obtain c where A: "lcm a b = a * c" ..
365   with \<open>a \<noteq> 0\<close> and \<open>b \<noteq> 0\<close> have "c \<noteq> 0" by (auto simp: lcm_eq_0_iff)
366   then show ?thesis by (subst A, intro size_mult_mono)
367 qed
369 lemma euclidean_size_lcm_le2:
370   "a \<noteq> 0 \<Longrightarrow> b \<noteq> 0 \<Longrightarrow> euclidean_size b \<le> euclidean_size (lcm a b)"
371   using euclidean_size_lcm_le1 [of b a] by (simp add: ac_simps)
373 lemma euclidean_size_lcm_less1:
374   assumes "b \<noteq> 0" and "\<not>b dvd a"
375   shows "euclidean_size a < euclidean_size (lcm a b)"
376 proof (rule ccontr)
377   from assms have "a \<noteq> 0" by auto
378   assume "\<not>euclidean_size a < euclidean_size (lcm a b)"
379   with \<open>a \<noteq> 0\<close> and \<open>b \<noteq> 0\<close> have "euclidean_size (lcm a b) = euclidean_size a"
380     by (intro le_antisym, simp, intro euclidean_size_lcm_le1)
381   with assms have "lcm a b dvd a"
382     by (rule_tac dvd_euclidean_size_eq_imp_dvd) (auto simp: lcm_eq_0_iff)
383   hence "b dvd a" by (rule lcm_dvdD2)
384   with \<open>\<not>b dvd a\<close> show False by contradiction
385 qed
387 lemma euclidean_size_lcm_less2:
388   assumes "a \<noteq> 0" and "\<not>a dvd b"
389   shows "euclidean_size b < euclidean_size (lcm a b)"
390   using assms euclidean_size_lcm_less1 [of a b] by (simp add: ac_simps)
392 lemma Lcm_eucl_set [code]:
393   "Lcm_eucl (set xs) = foldl lcm_eucl 1 xs"
394   by (simp add: Lcm_Lcm_eucl [symmetric] lcm_lcm_eucl Lcm_set)
396 lemma Gcd_eucl_set [code]:
397   "Gcd_eucl (set xs) = foldl gcd_eucl 0 xs"
398   by (simp add: Gcd_Gcd_eucl [symmetric] gcd_gcd_eucl Gcd_set)
400 end
402 text \<open>
403   A Euclidean ring is a Euclidean semiring with additive inverses. It provides a
404   few more lemmas; in particular, Bezout's lemma holds for any Euclidean ring.
405 \<close>
407 class euclidean_ring_gcd = euclidean_semiring_gcd + idom
408 begin
410 subclass euclidean_ring ..
411 subclass ring_gcd ..
413 lemma euclid_ext_gcd [simp]:
414   "(case euclid_ext a b of (_, _ , t) \<Rightarrow> t) = gcd a b"
415   by (induct a b rule: gcd_eucl_induct)
416     (simp_all add: euclid_ext_0 euclid_ext_non_0 ac_simps split: prod.split prod.split_asm)
418 lemma euclid_ext_gcd' [simp]:
419   "euclid_ext a b = (r, s, t) \<Longrightarrow> t = gcd a b"
420   by (insert euclid_ext_gcd[of a b], drule (1) subst, simp)
422 lemma euclid_ext'_correct:
423   "fst (euclid_ext' a b) * a + snd (euclid_ext' a b) * b = gcd a b"
424 proof-
425   obtain s t c where "euclid_ext a b = (s,t,c)"
426     by (cases "euclid_ext a b", blast)
427   with euclid_ext_correct[of a b] euclid_ext_gcd[of a b]
428     show ?thesis unfolding euclid_ext'_def by simp
429 qed
431 lemma bezout: "\<exists>s t. s * a + t * b = gcd a b"
432   using euclid_ext'_correct by blast
434 end
437 subsection \<open>Typical instances\<close>
439 instantiation nat :: euclidean_semiring
440 begin
442 definition [simp]:
443   "euclidean_size_nat = (id :: nat \<Rightarrow> nat)"
445 instance proof
446 qed simp_all
448 end
451 instantiation int :: euclidean_ring
452 begin
454 definition [simp]:
455   "euclidean_size_int = (nat \<circ> abs :: int \<Rightarrow> nat)"
457 instance
458 by standard (auto simp add: abs_mult nat_mult_distrib split: abs_split)
460 end
463 instantiation poly :: (field) euclidean_ring
464 begin
466 definition euclidean_size_poly :: "'a poly \<Rightarrow> nat"
467   where "euclidean_size p = (if p = 0 then 0 else 2 ^ degree p)"
469 lemma euclidean_size_poly_0 [simp]:
470   "euclidean_size (0::'a poly) = 0"
471   by (simp add: euclidean_size_poly_def)
473 lemma euclidean_size_poly_not_0 [simp]:
474   "p \<noteq> 0 \<Longrightarrow> euclidean_size p = 2 ^ degree p"
475   by (simp add: euclidean_size_poly_def)
477 instance
478 proof
479   fix p q :: "'a poly"
480   assume "q \<noteq> 0"
481   then have "p mod q = 0 \<or> degree (p mod q) < degree q"
482     by (rule degree_mod_less [of q p])
483   with \<open>q \<noteq> 0\<close> show "euclidean_size (p mod q) < euclidean_size q"
484     by (cases "p mod q = 0") simp_all
485 next
486   fix p q :: "'a poly"
487   assume "q \<noteq> 0"
488   from \<open>q \<noteq> 0\<close> have "degree p \<le> degree (p * q)"
489     by (rule degree_mult_right_le)
490   with \<open>q \<noteq> 0\<close> show "euclidean_size p \<le> euclidean_size (p * q)"
491     by (cases "p = 0") simp_all
492 qed simp
494 end
497 instance nat :: euclidean_semiring_gcd
498 proof
499   show [simp]: "gcd = (gcd_eucl :: nat \<Rightarrow> _)" "Lcm = (Lcm_eucl :: nat set \<Rightarrow> _)"
500     by (simp_all add: eq_gcd_euclI eq_Lcm_euclI)
501   show "lcm = (lcm_eucl :: nat \<Rightarrow> _)" "Gcd = (Gcd_eucl :: nat set \<Rightarrow> _)"
502     by (intro ext, simp add: lcm_eucl_def lcm_nat_def Gcd_nat_def Gcd_eucl_def)+
503 qed
505 instance int :: euclidean_ring_gcd
506 proof
507   show [simp]: "gcd = (gcd_eucl :: int \<Rightarrow> _)" "Lcm = (Lcm_eucl :: int set \<Rightarrow> _)"
508     by (simp_all add: eq_gcd_euclI eq_Lcm_euclI)
509   show "lcm = (lcm_eucl :: int \<Rightarrow> _)" "Gcd = (Gcd_eucl :: int set \<Rightarrow> _)"
510     by (intro ext, simp add: lcm_eucl_def lcm_altdef_int
511           semiring_Gcd_class.Gcd_Lcm Gcd_eucl_def abs_mult)+
512 qed
515 instantiation poly :: (field) euclidean_ring_gcd
516 begin
518 definition gcd_poly :: "'a poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly" where
519   "gcd_poly = gcd_eucl"
521 definition lcm_poly :: "'a poly \<Rightarrow> 'a poly \<Rightarrow> 'a poly" where
522   "lcm_poly = lcm_eucl"
524 definition Gcd_poly :: "'a poly set \<Rightarrow> 'a poly" where
525   "Gcd_poly = Gcd_eucl"
527 definition Lcm_poly :: "'a poly set \<Rightarrow> 'a poly" where
528   "Lcm_poly = Lcm_eucl"
530 instance by standard (simp_all only: gcd_poly_def lcm_poly_def Gcd_poly_def Lcm_poly_def)
531 end
533 lemma poly_gcd_monic:
534   "lead_coeff (gcd x y) = (if x = 0 \<and> y = 0 then 0 else 1)"
535   using unit_factor_gcd[of x y]
536   by (simp add: unit_factor_poly_def monom_0 one_poly_def lead_coeff_def split: if_split_asm)
538 lemma poly_dvd_antisym:
539   fixes p q :: "'a::idom poly"
540   assumes coeff: "coeff p (degree p) = coeff q (degree q)"
541   assumes dvd1: "p dvd q" and dvd2: "q dvd p" shows "p = q"
542 proof (cases "p = 0")
543   case True with coeff show "p = q" by simp
544 next
545   case False with coeff have "q \<noteq> 0" by auto
546   have degree: "degree p = degree q"
547     using \<open>p dvd q\<close> \<open>q dvd p\<close> \<open>p \<noteq> 0\<close> \<open>q \<noteq> 0\<close>
548     by (intro order_antisym dvd_imp_degree_le)
550   from \<open>p dvd q\<close> obtain a where a: "q = p * a" ..
551   with \<open>q \<noteq> 0\<close> have "a \<noteq> 0" by auto
552   with degree a \<open>p \<noteq> 0\<close> have "degree a = 0"
553     by (simp add: degree_mult_eq)
554   with coeff a show "p = q"
555     by (cases a, auto split: if_splits)
556 qed
558 lemma poly_gcd_unique:
559   fixes d x y :: "_ poly"
560   assumes dvd1: "d dvd x" and dvd2: "d dvd y"
561     and greatest: "\<And>k. k dvd x \<Longrightarrow> k dvd y \<Longrightarrow> k dvd d"
562     and monic: "coeff d (degree d) = (if x = 0 \<and> y = 0 then 0 else 1)"
563   shows "d = gcd x y"
564   using assms by (intro gcdI) (auto simp: normalize_poly_def split: if_split_asm)
566 lemma poly_gcd_code [code]:
567   "gcd x y = (if y = 0 then normalize x else gcd y (x mod (y :: _ poly)))"
568   by (simp add: gcd_0 gcd_non_0)
570 end