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