src/HOL/Number_Theory/Euclidean_Algorithm.thy
 author haftmann Sun Oct 16 09:31:05 2016 +0200 (2016-10-16) changeset 64243 aee949f6642d parent 64242 93c6f0da5c70 child 64592 7759f1766189 permissions -rw-r--r--
eliminated irregular aliasses
 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@63498  6 imports "~~/src/HOL/GCD" Factorial_Ring  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@64164  20 class euclidean_semiring = semiring_modulo + 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@64240  29 lemma mod_0 [simp]: "0 mod a = 0"  haftmann@64242  30  using div_mult_mod_eq [of 0 a] by simp  haftmann@64164  31 haftmann@64164  32 lemma dvd_mod_iff:  haftmann@64164  33  assumes "k dvd n"  haftmann@64164  34  shows "(k dvd m mod n) = (k dvd m)"  haftmann@64164  35 proof -  haftmann@64164  36  from assms have "(k dvd m mod n) \ (k dvd ((m div n) * n + m mod n))"  haftmann@64164  37  by (simp add: dvd_add_right_iff)  haftmann@64164  38  also have "(m div n) * n + m mod n = m"  haftmann@64242  39  using div_mult_mod_eq [of m n] by simp  haftmann@64164  40  finally show ?thesis .  haftmann@64164  41 qed  haftmann@64164  42 haftmann@64164  43 lemma mod_0_imp_dvd:  haftmann@64164  44  assumes "a mod b = 0"  haftmann@64164  45  shows "b dvd a"  haftmann@64164  46 proof -  haftmann@64164  47  have "b dvd ((a div b) * b)" by simp  haftmann@64164  48  also have "(a div b) * b = a"  haftmann@64242  49  using div_mult_mod_eq [of a b] by (simp add: assms)  haftmann@64164  50  finally show ?thesis .  haftmann@64164  51 qed  haftmann@64164  52 haftmann@63947  53 lemma euclidean_size_normalize [simp]:  haftmann@63947  54  "euclidean_size (normalize a) = euclidean_size a"  haftmann@63947  55 proof (cases "a = 0")  haftmann@63947  56  case True  haftmann@63947  57  then show ?thesis  haftmann@63947  58  by simp  haftmann@63947  59 next  haftmann@63947  60  case [simp]: False  haftmann@63947  61  have "euclidean_size (normalize a) \ euclidean_size (normalize a * unit_factor a)"  haftmann@63947  62  by (rule size_mult_mono) simp  haftmann@63947  63  moreover have "euclidean_size a \ euclidean_size (a * (1 div unit_factor a))"  haftmann@63947  64  by (rule size_mult_mono) simp  haftmann@63947  65  ultimately show ?thesis  haftmann@63947  66  by simp  haftmann@63947  67 qed  haftmann@63947  68 haftmann@58023  69 lemma euclidean_division:  haftmann@58023  70  fixes a :: 'a and b :: 'a  haftmann@60600  71  assumes "b \ 0"  haftmann@58023  72  obtains s and t where "a = s * b + t"  haftmann@58023  73  and "euclidean_size t < euclidean_size b"  haftmann@58023  74 proof -  haftmann@64242  75  from div_mult_mod_eq [of a b]  haftmann@58023  76  have "a = a div b * b + a mod b" by simp  haftmann@60569  77  with that and assms show ?thesis by (auto simp add: mod_size_less)  haftmann@58023  78 qed  haftmann@58023  79 haftmann@58023  80 lemma dvd_euclidean_size_eq_imp_dvd:  haftmann@58023  81  assumes "a \ 0" and b_dvd_a: "b dvd a" and size_eq: "euclidean_size a = euclidean_size b"  haftmann@58023  82  shows "a dvd b"  haftmann@60569  83 proof (rule ccontr)  haftmann@60569  84  assume "\ a dvd b"  haftmann@64163  85  hence "b mod a \ 0" using mod_0_imp_dvd[of b a] by blast  haftmann@60569  86  then have "b mod a \ 0" by (simp add: mod_eq_0_iff_dvd)  haftmann@64164  87  from b_dvd_a have b_dvd_mod: "b dvd b mod a" by (simp add: dvd_mod_iff)  haftmann@58023  88  from b_dvd_mod obtain c where "b mod a = b * c" unfolding dvd_def by blast  wenzelm@60526  89  with \b mod a \ 0\ have "c \ 0" by auto  wenzelm@60526  90  with \b mod a = b * c\ have "euclidean_size (b mod a) \ euclidean_size b"  haftmann@58023  91  using size_mult_mono by force  haftmann@60569  92  moreover from \\ a dvd b\ and \a \ 0\  haftmann@60569  93  have "euclidean_size (b mod a) < euclidean_size a"  haftmann@58023  94  using mod_size_less by blast  haftmann@58023  95  ultimately show False using size_eq by simp  haftmann@58023  96 qed  haftmann@58023  97 eberlm@63498  98 lemma size_mult_mono': "b \ 0 \ euclidean_size a \ euclidean_size (b * a)"  eberlm@63498  99  by (subst mult.commute) (rule size_mult_mono)  eberlm@63498  100 eberlm@63498  101 lemma euclidean_size_times_unit:  eberlm@63498  102  assumes "is_unit a"  eberlm@63498  103  shows "euclidean_size (a * b) = euclidean_size b"  eberlm@63498  104 proof (rule antisym)  eberlm@63498  105  from assms have [simp]: "a \ 0" by auto  eberlm@63498  106  thus "euclidean_size (a * b) \ euclidean_size b" by (rule size_mult_mono')  eberlm@63498  107  from assms have "is_unit (1 div a)" by simp  eberlm@63498  108  hence "1 div a \ 0" by (intro notI) simp_all  eberlm@63498  109  hence "euclidean_size (a * b) \ euclidean_size ((1 div a) * (a * b))"  eberlm@63498  110  by (rule size_mult_mono')  eberlm@63498  111  also from assms have "(1 div a) * (a * b) = b"  eberlm@63498  112  by (simp add: algebra_simps unit_div_mult_swap)  eberlm@63498  113  finally show "euclidean_size (a * b) \ euclidean_size b" .  eberlm@63498  114 qed  eberlm@63498  115 haftmann@64177  116 lemma euclidean_size_unit: "is_unit a \ euclidean_size a = euclidean_size 1"  haftmann@64177  117  using euclidean_size_times_unit[of a 1] by simp  eberlm@63498  118 eberlm@63498  119 lemma unit_iff_euclidean_size:  haftmann@64177  120  "is_unit a \ euclidean_size a = euclidean_size 1 \ a \ 0"  eberlm@63498  121 proof safe  haftmann@64177  122  assume A: "a \ 0" and B: "euclidean_size a = euclidean_size 1"  haftmann@64177  123  show "is_unit a" by (rule dvd_euclidean_size_eq_imp_dvd[OF A _ B]) simp_all  eberlm@63498  124 qed (auto intro: euclidean_size_unit)  eberlm@63498  125 eberlm@63498  126 lemma euclidean_size_times_nonunit:  eberlm@63498  127  assumes "a \ 0" "b \ 0" "\is_unit a"  eberlm@63498  128  shows "euclidean_size b < euclidean_size (a * b)"  eberlm@63498  129 proof (rule ccontr)  eberlm@63498  130  assume "\euclidean_size b < euclidean_size (a * b)"  eberlm@63498  131  with size_mult_mono'[OF assms(1), of b]  eberlm@63498  132  have eq: "euclidean_size (a * b) = euclidean_size b" by simp  eberlm@63498  133  have "a * b dvd b"  eberlm@63498  134  by (rule dvd_euclidean_size_eq_imp_dvd[OF _ _ eq]) (insert assms, simp_all)  eberlm@63498  135  hence "a * b dvd 1 * b" by simp  eberlm@63498  136  with \b \ 0\ have "is_unit a" by (subst (asm) dvd_times_right_cancel_iff)  eberlm@63498  137  with assms(3) show False by contradiction  eberlm@63498  138 qed  eberlm@63498  139 eberlm@63498  140 lemma dvd_imp_size_le:  haftmann@64177  141  assumes "a dvd b" "b \ 0"  haftmann@64177  142  shows "euclidean_size a \ euclidean_size b"  eberlm@63498  143  using assms by (auto elim!: dvdE simp: size_mult_mono)  eberlm@63498  144 eberlm@63498  145 lemma dvd_proper_imp_size_less:  haftmann@64177  146  assumes "a dvd b" "\b dvd a" "b \ 0"  haftmann@64177  147  shows "euclidean_size a < euclidean_size b"  eberlm@63498  148 proof -  haftmann@64177  149  from assms(1) obtain c where "b = a * c" by (erule dvdE)  haftmann@64177  150  hence z: "b = c * a" by (simp add: mult.commute)  haftmann@64177  151  from z assms have "\is_unit c" by (auto simp: mult.commute mult_unit_dvd_iff)  eberlm@63498  152  with z assms show ?thesis  eberlm@63498  153  by (auto intro!: euclidean_size_times_nonunit simp: )  eberlm@63498  154 qed  eberlm@63498  155 haftmann@58023  156 function gcd_eucl :: "'a \ 'a \ 'a"  haftmann@58023  157 where  haftmann@60634  158  "gcd_eucl a b = (if b = 0 then normalize a else gcd_eucl b (a mod b))"  haftmann@60572  159  by pat_completeness simp  haftmann@60569  160 termination  haftmann@60569  161  by (relation "measure (euclidean_size \ snd)") (simp_all add: mod_size_less)  haftmann@58023  162 haftmann@58023  163 declare gcd_eucl.simps [simp del]  haftmann@58023  164 haftmann@60569  165 lemma gcd_eucl_induct [case_names zero mod]:  haftmann@60569  166  assumes H1: "\b. P b 0"  haftmann@60569  167  and H2: "\a b. b \ 0 \ P b (a mod b) \ P a b"  haftmann@60569  168  shows "P a b"  haftmann@58023  169 proof (induct a b rule: gcd_eucl.induct)  haftmann@60569  170  case ("1" a b)  haftmann@60569  171  show ?case  haftmann@60569  172  proof (cases "b = 0")  haftmann@60569  173  case True then show "P a b" by simp (rule H1)  haftmann@60569  174  next  haftmann@60569  175  case False  haftmann@60600  176  then have "P b (a mod b)"  haftmann@60600  177  by (rule "1.hyps")  haftmann@60569  178  with \b \ 0\ show "P a b"  haftmann@60569  179  by (blast intro: H2)  haftmann@60569  180  qed  haftmann@58023  181 qed  haftmann@58023  182 haftmann@58023  183 definition lcm_eucl :: "'a \ 'a \ 'a"  haftmann@58023  184 where  haftmann@60634  185  "lcm_eucl a b = normalize (a * b) div gcd_eucl a b"  haftmann@58023  186 wenzelm@63167  187 definition Lcm_eucl :: "'a set \ 'a" \ \  haftmann@60572  188  Somewhat complicated definition of Lcm that has the advantage of working  haftmann@60572  189  for infinite sets as well\  haftmann@58023  190 where  haftmann@60430  191  "Lcm_eucl A = (if \l. l \ 0 \ (\a\A. a dvd l) then  haftmann@60430  192  let l = SOME l. l \ 0 \ (\a\A. a dvd l) \ euclidean_size l =  haftmann@60430  193  (LEAST n. \l. l \ 0 \ (\a\A. a dvd l) \ euclidean_size l = n)  haftmann@60634  194  in normalize l  haftmann@58023  195  else 0)"  haftmann@58023  196 haftmann@58023  197 definition Gcd_eucl :: "'a set \ 'a"  haftmann@58023  198 where  haftmann@58023  199  "Gcd_eucl A = Lcm_eucl {d. \a\A. d dvd a}"  haftmann@58023  200 eberlm@62428  201 declare Lcm_eucl_def Gcd_eucl_def [code del]  eberlm@62428  202 haftmann@60572  203 lemma gcd_eucl_0:  haftmann@60634  204  "gcd_eucl a 0 = normalize a"  haftmann@60572  205  by (simp add: gcd_eucl.simps [of a 0])  haftmann@60572  206 haftmann@60572  207 lemma gcd_eucl_0_left:  haftmann@60634  208  "gcd_eucl 0 a = normalize a"  haftmann@60600  209  by (simp_all add: gcd_eucl_0 gcd_eucl.simps [of 0 a])  haftmann@60572  210 haftmann@60572  211 lemma gcd_eucl_non_0:  haftmann@60572  212  "b \ 0 \ gcd_eucl a b = gcd_eucl b (a mod b)"  haftmann@60600  213  by (simp add: gcd_eucl.simps [of a b] gcd_eucl.simps [of b 0])  haftmann@60572  214 eberlm@62422  215 lemma gcd_eucl_dvd1 [iff]: "gcd_eucl a b dvd a"  eberlm@62422  216  and gcd_eucl_dvd2 [iff]: "gcd_eucl a b dvd b"  eberlm@62422  217  by (induct a b rule: gcd_eucl_induct)  eberlm@62422  218  (simp_all add: gcd_eucl_0 gcd_eucl_non_0 dvd_mod_iff)  eberlm@62422  219 eberlm@62422  220 lemma normalize_gcd_eucl [simp]:  eberlm@62422  221  "normalize (gcd_eucl a b) = gcd_eucl a b"  eberlm@62422  222  by (induct a b rule: gcd_eucl_induct) (simp_all add: gcd_eucl_0 gcd_eucl_non_0)  eberlm@62422  223   eberlm@62422  224 lemma gcd_eucl_greatest:  eberlm@62422  225  fixes k a b :: 'a  eberlm@62422  226  shows "k dvd a \ k dvd b \ k dvd gcd_eucl a b"  eberlm@62422  227 proof (induct a b rule: gcd_eucl_induct)  eberlm@62422  228  case (zero a) from zero(1) show ?case by (rule dvd_trans) (simp add: gcd_eucl_0)  eberlm@62422  229 next  eberlm@62422  230  case (mod a b)  eberlm@62422  231  then show ?case  eberlm@62422  232  by (simp add: gcd_eucl_non_0 dvd_mod_iff)  eberlm@62422  233 qed  eberlm@62422  234 eberlm@63498  235 lemma gcd_euclI:  eberlm@63498  236  fixes gcd :: "'a \ 'a \ 'a"  eberlm@63498  237  assumes "d dvd a" "d dvd b" "normalize d = d"  eberlm@63498  238  "\k. k dvd a \ k dvd b \ k dvd d"  eberlm@63498  239  shows "gcd_eucl a b = d"  eberlm@63498  240  by (rule associated_eqI) (simp_all add: gcd_eucl_greatest assms)  eberlm@63498  241 eberlm@62422  242 lemma eq_gcd_euclI:  eberlm@62422  243  fixes gcd :: "'a \ 'a \ 'a"  eberlm@62422  244  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  245  "\a b k. k dvd a \ k dvd b \ k dvd gcd a b"  eberlm@62422  246  shows "gcd = gcd_eucl"  eberlm@62422  247  by (intro ext, rule associated_eqI) (simp_all add: gcd_eucl_greatest assms)  eberlm@62422  248 eberlm@62422  249 lemma gcd_eucl_zero [simp]:  eberlm@62422  250  "gcd_eucl a b = 0 \ a = 0 \ b = 0"  eberlm@62422  251  by (metis dvd_0_left dvd_refl gcd_eucl_dvd1 gcd_eucl_dvd2 gcd_eucl_greatest)+  eberlm@62422  252 eberlm@62422  253   eberlm@62422  254 lemma dvd_Lcm_eucl [simp]: "a \ A \ a dvd Lcm_eucl A"  eberlm@62422  255  and Lcm_eucl_least: "(\a. a \ A \ a dvd b) \ Lcm_eucl A dvd b"  eberlm@62422  256  and unit_factor_Lcm_eucl [simp]:  eberlm@62422  257  "unit_factor (Lcm_eucl A) = (if Lcm_eucl A = 0 then 0 else 1)"  eberlm@62422  258 proof -  eberlm@62422  259  have "(\a\A. a dvd Lcm_eucl A) \ (\l'. (\a\A. a dvd l') \ Lcm_eucl A dvd l') \  eberlm@62422  260  unit_factor (Lcm_eucl A) = (if Lcm_eucl A = 0 then 0 else 1)" (is ?thesis)  eberlm@62422  261  proof (cases "\l. l \ 0 \ (\a\A. a dvd l)")  eberlm@62422  262  case False  eberlm@62422  263  hence "Lcm_eucl A = 0" by (auto simp: Lcm_eucl_def)  eberlm@62422  264  with False show ?thesis by auto  eberlm@62422  265  next  eberlm@62422  266  case True  eberlm@62422  267  then obtain l\<^sub>0 where l\<^sub>0_props: "l\<^sub>0 \ 0 \ (\a\A. a dvd l\<^sub>0)" by blast  wenzelm@63040  268  define n where "n = (LEAST n. \l. l \ 0 \ (\a\A. a dvd l) \ euclidean_size l = n)"  wenzelm@63040  269  define l where "l = (SOME l. l \ 0 \ (\a\A. a dvd l) \ euclidean_size l = n)"  eberlm@62422  270  have "\l. l \ 0 \ (\a\A. a dvd l) \ euclidean_size l = n"  eberlm@62422  271  apply (subst n_def)  eberlm@62422  272  apply (rule LeastI[of _ "euclidean_size l\<^sub>0"])  eberlm@62422  273  apply (rule exI[of _ l\<^sub>0])  eberlm@62422  274  apply (simp add: l\<^sub>0_props)  eberlm@62422  275  done  eberlm@62422  276  from someI_ex[OF this] have "l \ 0" and "\a\A. a dvd l" and "euclidean_size l = n"  eberlm@62422  277  unfolding l_def by simp_all  eberlm@62422  278  {  eberlm@62422  279  fix l' assume "\a\A. a dvd l'"  eberlm@62422  280  with \\a\A. a dvd l\ have "\a\A. a dvd gcd_eucl l l'" by (auto intro: gcd_eucl_greatest)  eberlm@62422  281  moreover from \l \ 0\ have "gcd_eucl l l' \ 0" by simp  eberlm@62422  282  ultimately have "\b. b \ 0 \ (\a\A. a dvd b) \  eberlm@62422  283  euclidean_size b = euclidean_size (gcd_eucl l l')"  eberlm@62422  284  by (intro exI[of _ "gcd_eucl l l'"], auto)  eberlm@62422  285  hence "euclidean_size (gcd_eucl l l') \ n" by (subst n_def) (rule Least_le)  eberlm@62422  286  moreover have "euclidean_size (gcd_eucl l l') \ n"  eberlm@62422  287  proof -  eberlm@62422  288  have "gcd_eucl l l' dvd l" by simp  eberlm@62422  289  then obtain a where "l = gcd_eucl l l' * a" unfolding dvd_def by blast  eberlm@62422  290  with \l \ 0\ have "a \ 0" by auto  eberlm@62422  291  hence "euclidean_size (gcd_eucl l l') \ euclidean_size (gcd_eucl l l' * a)"  eberlm@62422  292  by (rule size_mult_mono)  eberlm@62422  293  also have "gcd_eucl l l' * a = l" using \l = gcd_eucl l l' * a\ ..  eberlm@62422  294  also note \euclidean_size l = n\  eberlm@62422  295  finally show "euclidean_size (gcd_eucl l l') \ n" .  eberlm@62422  296  qed  eberlm@62422  297  ultimately have *: "euclidean_size l = euclidean_size (gcd_eucl l l')"  eberlm@62422  298  by (intro le_antisym, simp_all add: \euclidean_size l = n\)  eberlm@62422  299  from \l \ 0\ have "l dvd gcd_eucl l l'"  eberlm@62422  300  by (rule dvd_euclidean_size_eq_imp_dvd) (auto simp add: *)  eberlm@62422  301  hence "l dvd l'" by (rule dvd_trans[OF _ gcd_eucl_dvd2])  eberlm@62422  302  }  eberlm@62422  303 eberlm@62422  304  with \(\a\A. a dvd l)\ and unit_factor_is_unit[OF \l \ 0\] and \l \ 0\  eberlm@62422  305  have "(\a\A. a dvd normalize l) \  eberlm@62422  306  (\l'. (\a\A. a dvd l') \ normalize l dvd l') \  eberlm@62422  307  unit_factor (normalize l) =  eberlm@62422  308  (if normalize l = 0 then 0 else 1)"  eberlm@62422  309  by (auto simp: unit_simps)  eberlm@62422  310  also from True have "normalize l = Lcm_eucl A"  eberlm@62422  311  by (simp add: Lcm_eucl_def Let_def n_def l_def)  eberlm@62422  312  finally show ?thesis .  eberlm@62422  313  qed  eberlm@62422  314  note A = this  eberlm@62422  315 eberlm@62422  316  {fix a assume "a \ A" then show "a dvd Lcm_eucl A" using A by blast}  eberlm@62422  317  {fix b assume "\a. a \ A \ a dvd b" then show "Lcm_eucl A dvd b" using A by blast}  eberlm@62422  318  from A show "unit_factor (Lcm_eucl A) = (if Lcm_eucl A = 0 then 0 else 1)" by blast  eberlm@62422  319 qed  eberlm@63498  320 eberlm@62422  321 lemma normalize_Lcm_eucl [simp]:  eberlm@62422  322  "normalize (Lcm_eucl A) = Lcm_eucl A"  eberlm@62422  323 proof (cases "Lcm_eucl A = 0")  eberlm@62422  324  case True then show ?thesis by simp  eberlm@62422  325 next  eberlm@62422  326  case False  eberlm@62422  327  have "unit_factor (Lcm_eucl A) * normalize (Lcm_eucl A) = Lcm_eucl A"  eberlm@62422  328  by (fact unit_factor_mult_normalize)  eberlm@62422  329  with False show ?thesis by simp  eberlm@62422  330 qed  eberlm@62422  331 eberlm@62422  332 lemma eq_Lcm_euclI:  eberlm@62422  333  fixes lcm :: "'a set \ 'a"  eberlm@62422  334  assumes "\A a. a \ A \ a dvd lcm A" and "\A c. (\a. a \ A \ a dvd c) \ lcm A dvd c"  eberlm@62422  335  "\A. normalize (lcm A) = lcm A" shows "lcm = Lcm_eucl"  eberlm@62422  336  by (intro ext, rule associated_eqI) (auto simp: assms intro: Lcm_eucl_least)  eberlm@62422  337 haftmann@64177  338 lemma Gcd_eucl_dvd: "a \ A \ Gcd_eucl A dvd a"  eberlm@63498  339  unfolding Gcd_eucl_def by (auto intro: Lcm_eucl_least)  eberlm@63498  340 eberlm@63498  341 lemma Gcd_eucl_greatest: "(\x. x \ A \ d dvd x) \ d dvd Gcd_eucl A"  eberlm@63498  342  unfolding Gcd_eucl_def by auto  eberlm@63498  343 eberlm@63498  344 lemma normalize_Gcd_eucl [simp]: "normalize (Gcd_eucl A) = Gcd_eucl A"  eberlm@63498  345  by (simp add: Gcd_eucl_def)  eberlm@63498  346 eberlm@63498  347 lemma Lcm_euclI:  eberlm@63498  348  assumes "\x. x \ A \ x dvd d" "\d'. (\x. x \ A \ x dvd d') \ d dvd d'" "normalize d = d"  eberlm@63498  349  shows "Lcm_eucl A = d"  eberlm@63498  350 proof -  eberlm@63498  351  have "normalize (Lcm_eucl A) = normalize d"  eberlm@63498  352  by (intro associatedI) (auto intro: dvd_Lcm_eucl Lcm_eucl_least assms)  eberlm@63498  353  thus ?thesis by (simp add: assms)  eberlm@63498  354 qed  eberlm@63498  355 eberlm@63498  356 lemma Gcd_euclI:  eberlm@63498  357  assumes "\x. x \ A \ d dvd x" "\d'. (\x. x \ A \ d' dvd x) \ d' dvd d" "normalize d = d"  eberlm@63498  358  shows "Gcd_eucl A = d"  eberlm@63498  359 proof -  eberlm@63498  360  have "normalize (Gcd_eucl A) = normalize d"  eberlm@63498  361  by (intro associatedI) (auto intro: Gcd_eucl_dvd Gcd_eucl_greatest assms)  eberlm@63498  362  thus ?thesis by (simp add: assms)  eberlm@63498  363 qed  eberlm@63498  364   eberlm@63498  365 lemmas lcm_gcd_eucl_facts =  eberlm@63498  366  gcd_eucl_dvd1 gcd_eucl_dvd2 gcd_eucl_greatest normalize_gcd_eucl lcm_eucl_def  eberlm@63498  367  Gcd_eucl_def Gcd_eucl_dvd Gcd_eucl_greatest normalize_Gcd_eucl  eberlm@63498  368  dvd_Lcm_eucl Lcm_eucl_least normalize_Lcm_eucl  eberlm@63498  369 eberlm@63498  370 lemma normalized_factors_product:  eberlm@63498  371  "{p. p dvd a * b \ normalize p = p} =  eberlm@63498  372  (\(x,y). x * y)  ({p. p dvd a \ normalize p = p} \ {p. p dvd b \ normalize p = p})"  eberlm@63498  373 proof safe  eberlm@63498  374  fix p assume p: "p dvd a * b" "normalize p = p"  eberlm@63498  375  interpret semiring_gcd 1 0 "op *" gcd_eucl lcm_eucl "op div" "op +" "op -" normalize unit_factor  eberlm@63498  376  by standard (rule lcm_gcd_eucl_facts; assumption)+  eberlm@63498  377  from dvd_productE[OF p(1)] guess x y . note xy = this  eberlm@63498  378  define x' y' where "x' = normalize x" and "y' = normalize y"  eberlm@63498  379  have "p = x' * y'"  eberlm@63498  380  by (subst p(2) [symmetric]) (simp add: xy x'_def y'_def normalize_mult)  eberlm@63498  381  moreover from xy have "normalize x' = x'" "normalize y' = y'" "x' dvd a" "y' dvd b"  eberlm@63498  382  by (simp_all add: x'_def y'_def)  eberlm@63498  383  ultimately show "p \ (\(x, y). x * y)   eberlm@63498  384  ({p. p dvd a \ normalize p = p} \ {p. p dvd b \ normalize p = p})"  eberlm@63498  385  by blast  eberlm@63498  386 qed (auto simp: normalize_mult mult_dvd_mono)  eberlm@63498  387 eberlm@63498  388 eberlm@63498  389 subclass factorial_semiring  eberlm@63498  390 proof (standard, rule factorial_semiring_altI_aux)  eberlm@63498  391  fix x assume "x \ 0"  eberlm@63498  392  thus "finite {p. p dvd x \ normalize p = p}"  eberlm@63498  393  proof (induction "euclidean_size x" arbitrary: x rule: less_induct)  eberlm@63498  394  case (less x)  eberlm@63498  395  show ?case  eberlm@63498  396  proof (cases "\y. y dvd x \ \x dvd y \ \is_unit y")  eberlm@63498  397  case False  eberlm@63498  398  have "{p. p dvd x \ normalize p = p} \ {1, normalize x}"  eberlm@63498  399  proof  eberlm@63498  400  fix p assume p: "p \ {p. p dvd x \ normalize p = p}"  eberlm@63498  401  with False have "is_unit p \ x dvd p" by blast  eberlm@63498  402  thus "p \ {1, normalize x}"  eberlm@63498  403  proof (elim disjE)  eberlm@63498  404  assume "is_unit p"  eberlm@63498  405  hence "normalize p = 1" by (simp add: is_unit_normalize)  eberlm@63498  406  with p show ?thesis by simp  eberlm@63498  407  next  eberlm@63498  408  assume "x dvd p"  eberlm@63498  409  with p have "normalize p = normalize x" by (intro associatedI) simp_all  eberlm@63498  410  with p show ?thesis by simp  eberlm@63498  411  qed  eberlm@63498  412  qed  eberlm@63498  413  moreover have "finite \" by simp  eberlm@63498  414  ultimately show ?thesis by (rule finite_subset)  eberlm@63498  415   eberlm@63498  416  next  eberlm@63498  417  case True  eberlm@63498  418  then obtain y where y: "y dvd x" "\x dvd y" "\is_unit y" by blast  eberlm@63498  419  define z where "z = x div y"  eberlm@63498  420  let ?fctrs = "\x. {p. p dvd x \ normalize p = p}"  eberlm@63498  421  from y have x: "x = y * z" by (simp add: z_def)  eberlm@63498  422  with less.prems have "y \ 0" "z \ 0" by auto  eberlm@63498  423  from x y have "\is_unit z" by (auto simp: mult_unit_dvd_iff)  eberlm@63498  424  have "?fctrs x = (\(p,p'). p * p')  (?fctrs y \ ?fctrs z)"  eberlm@63498  425  by (subst x) (rule normalized_factors_product)  eberlm@63498  426  also have "\y * z dvd y * 1" "\y * z dvd 1 * z"  eberlm@63498  427  by (subst dvd_times_left_cancel_iff dvd_times_right_cancel_iff; fact)+  eberlm@63498  428  hence "finite ((\(p,p'). p * p')  (?fctrs y \ ?fctrs z))"  eberlm@63498  429  by (intro finite_imageI finite_cartesian_product less dvd_proper_imp_size_less)  eberlm@63498  430  (auto simp: x)  eberlm@63498  431  finally show ?thesis .  eberlm@63498  432  qed  eberlm@63498  433  qed  eberlm@63498  434 next  eberlm@63498  435  interpret semiring_gcd 1 0 "op *" gcd_eucl lcm_eucl "op div" "op +" "op -" normalize unit_factor  eberlm@63498  436  by standard (rule lcm_gcd_eucl_facts; assumption)+  eberlm@63498  437  fix p assume p: "irreducible p"  eberlm@63633  438  thus "prime_elem p" by (rule irreducible_imp_prime_elem_gcd)  eberlm@63498  439 qed  eberlm@63498  440 eberlm@63498  441 lemma gcd_eucl_eq_gcd_factorial: "gcd_eucl = gcd_factorial"  eberlm@63498  442  by (intro ext gcd_euclI gcd_lcm_factorial)  eberlm@63498  443 eberlm@63498  444 lemma lcm_eucl_eq_lcm_factorial: "lcm_eucl = lcm_factorial"  eberlm@63498  445  by (intro ext) (simp add: lcm_eucl_def lcm_factorial_gcd_factorial gcd_eucl_eq_gcd_factorial)  eberlm@63498  446 eberlm@63498  447 lemma Gcd_eucl_eq_Gcd_factorial: "Gcd_eucl = Gcd_factorial"  eberlm@63498  448  by (intro ext Gcd_euclI gcd_lcm_factorial)  eberlm@63498  449 eberlm@63498  450 lemma Lcm_eucl_eq_Lcm_factorial: "Lcm_eucl = Lcm_factorial"  eberlm@63498  451  by (intro ext Lcm_euclI gcd_lcm_factorial)  eberlm@63498  452 eberlm@63498  453 lemmas eucl_eq_factorial =  eberlm@63498  454  gcd_eucl_eq_gcd_factorial lcm_eucl_eq_lcm_factorial  eberlm@63498  455  Gcd_eucl_eq_Gcd_factorial Lcm_eucl_eq_Lcm_factorial  eberlm@63498  456   haftmann@58023  457 end  haftmann@58023  458 haftmann@60598  459 class euclidean_ring = euclidean_semiring + idom  haftmann@60598  460 begin  haftmann@60598  461 eberlm@62442  462 function euclid_ext_aux :: "'a \ _" where  eberlm@62442  463  "euclid_ext_aux r' r s' s t' t = (  eberlm@62442  464  if r = 0 then let c = 1 div unit_factor r' in (s' * c, t' * c, normalize r')  eberlm@62442  465  else let q = r' div r  eberlm@62442  466  in euclid_ext_aux r (r' mod r) s (s' - q * s) t (t' - q * t))"  eberlm@62442  467 by auto  eberlm@62442  468 termination by (relation "measure (\(_,b,_,_,_,_). euclidean_size b)") (simp_all add: mod_size_less)  eberlm@62442  469 eberlm@62442  470 declare euclid_ext_aux.simps [simp del]  haftmann@60598  471 eberlm@62442  472 lemma euclid_ext_aux_correct:  haftmann@64177  473  assumes "gcd_eucl r' r = gcd_eucl a b"  haftmann@64177  474  assumes "s' * a + t' * b = r'"  haftmann@64177  475  assumes "s * a + t * b = r"  haftmann@64177  476  shows "case euclid_ext_aux r' r s' s t' t of (x,y,c) \  haftmann@64177  477  x * a + y * b = c \ c = gcd_eucl a b" (is "?P (euclid_ext_aux r' r s' s t' t)")  eberlm@62442  478 using assms  eberlm@62442  479 proof (induction r' r s' s t' t rule: euclid_ext_aux.induct)  eberlm@62442  480  case (1 r' r s' s t' t)  eberlm@62442  481  show ?case  eberlm@62442  482  proof (cases "r = 0")  eberlm@62442  483  case True  eberlm@62442  484  hence "euclid_ext_aux r' r s' s t' t =  eberlm@62442  485  (s' div unit_factor r', t' div unit_factor r', normalize r')"  eberlm@62442  486  by (subst euclid_ext_aux.simps) (simp add: Let_def)  eberlm@62442  487  also have "?P \"  eberlm@62442  488  proof safe  haftmann@64177  489  have "s' div unit_factor r' * a + t' div unit_factor r' * b =  haftmann@64177  490  (s' * a + t' * b) div unit_factor r'"  eberlm@62442  491  by (cases "r' = 0") (simp_all add: unit_div_commute)  haftmann@64177  492  also have "s' * a + t' * b = r'" by fact  eberlm@62442  493  also have "\ div unit_factor r' = normalize r'" by simp  haftmann@64177  494  finally show "s' div unit_factor r' * a + t' div unit_factor r' * b = normalize r'" .  eberlm@62442  495  next  haftmann@64177  496  from "1.prems" True show "normalize r' = gcd_eucl a b" by (simp add: gcd_eucl_0)  eberlm@62442  497  qed  eberlm@62442  498  finally show ?thesis .  eberlm@62442  499  next  eberlm@62442  500  case False  eberlm@62442  501  hence "euclid_ext_aux r' r s' s t' t =  eberlm@62442  502  euclid_ext_aux r (r' mod r) s (s' - r' div r * s) t (t' - r' div r * t)"  eberlm@62442  503  by (subst euclid_ext_aux.simps) (simp add: Let_def)  eberlm@62442  504  also from "1.prems" False have "?P \"  eberlm@62442  505  proof (intro "1.IH")  haftmann@64177  506  have "(s' - r' div r * s) * a + (t' - r' div r * t) * b =  haftmann@64177  507  (s' * a + t' * b) - r' div r * (s * a + t * b)" by (simp add: algebra_simps)  haftmann@64177  508  also have "s' * a + t' * b = r'" by fact  haftmann@64177  509  also have "s * a + t * b = r" by fact  haftmann@64242  510  also have "r' - r' div r * r = r' mod r" using div_mult_mod_eq [of r' r]  eberlm@62442  511  by (simp add: algebra_simps)  haftmann@64177  512  finally show "(s' - r' div r * s) * a + (t' - r' div r * t) * b = r' mod r" .  haftmann@64243  513  qed (auto simp: gcd_eucl_non_0 algebra_simps minus_mod_eq_div_mult [symmetric])  eberlm@62442  514  finally show ?thesis .  eberlm@62442  515  qed  eberlm@62442  516 qed  eberlm@62442  517 eberlm@62442  518 definition euclid_ext where  eberlm@62442  519  "euclid_ext a b = euclid_ext_aux a b 1 0 0 1"  haftmann@60598  520 haftmann@60598  521 lemma euclid_ext_0:  haftmann@60634  522  "euclid_ext a 0 = (1 div unit_factor a, 0, normalize a)"  eberlm@62442  523  by (simp add: euclid_ext_def euclid_ext_aux.simps)  haftmann@60598  524 haftmann@60598  525 lemma euclid_ext_left_0:  haftmann@60634  526  "euclid_ext 0 a = (0, 1 div unit_factor a, normalize a)"  eberlm@62442  527  by (simp add: euclid_ext_def euclid_ext_aux.simps)  haftmann@60598  528 eberlm@62442  529 lemma euclid_ext_correct':  haftmann@64177  530  "case euclid_ext a b of (x,y,c) \ x * a + y * b = c \ c = gcd_eucl a b"  eberlm@62442  531  unfolding euclid_ext_def by (rule euclid_ext_aux_correct) simp_all  haftmann@60598  532 eberlm@62457  533 lemma euclid_ext_gcd_eucl:  haftmann@64177  534  "(case euclid_ext a b of (x,y,c) \ c) = gcd_eucl a b"  haftmann@64177  535  using euclid_ext_correct'[of a b] by (simp add: case_prod_unfold)  eberlm@62457  536 eberlm@62442  537 definition euclid_ext' where  haftmann@64177  538  "euclid_ext' a b = (case euclid_ext a b of (x, y, _) \ (x, y))"  haftmann@60598  539 eberlm@62442  540 lemma euclid_ext'_correct':  haftmann@64177  541  "case euclid_ext' a b of (x,y) \ x * a + y * b = gcd_eucl a b"  haftmann@64177  542  using euclid_ext_correct'[of a b] by (simp add: case_prod_unfold euclid_ext'_def)  haftmann@60598  543 haftmann@60634  544 lemma euclid_ext'_0: "euclid_ext' a 0 = (1 div unit_factor a, 0)"  haftmann@60598  545  by (simp add: euclid_ext'_def euclid_ext_0)  haftmann@60598  546 haftmann@60634  547 lemma euclid_ext'_left_0: "euclid_ext' 0 a = (0, 1 div unit_factor a)"  haftmann@60598  548  by (simp add: euclid_ext'_def euclid_ext_left_0)  haftmann@60598  549 haftmann@60598  550 end  haftmann@60598  551 haftmann@58023  552 class euclidean_semiring_gcd = euclidean_semiring + gcd + Gcd +  haftmann@58023  553  assumes gcd_gcd_eucl: "gcd = gcd_eucl" and lcm_lcm_eucl: "lcm = lcm_eucl"  haftmann@58023  554  assumes Gcd_Gcd_eucl: "Gcd = Gcd_eucl" and Lcm_Lcm_eucl: "Lcm = Lcm_eucl"  haftmann@58023  555 begin  haftmann@58023  556 eberlm@62422  557 subclass semiring_gcd  eberlm@62422  558  by standard (simp_all add: gcd_gcd_eucl gcd_eucl_greatest lcm_lcm_eucl lcm_eucl_def)  haftmann@58023  559 eberlm@62422  560 subclass semiring_Gcd  eberlm@62422  561  by standard (auto simp: Gcd_Gcd_eucl Lcm_Lcm_eucl Gcd_eucl_def intro: Lcm_eucl_least)  eberlm@63498  562 eberlm@63498  563 subclass factorial_semiring_gcd  eberlm@63498  564 proof  eberlm@63498  565  fix a b  eberlm@63498  566  show "gcd a b = gcd_factorial a b"  eberlm@63498  567  by (rule sym, rule gcdI) (rule gcd_lcm_factorial; assumption)+  eberlm@63498  568  thus "lcm a b = lcm_factorial a b"  eberlm@63498  569  by (simp add: lcm_factorial_gcd_factorial lcm_gcd)  eberlm@63498  570 next  eberlm@63498  571  fix A  eberlm@63498  572  show "Gcd A = Gcd_factorial A"  eberlm@63498  573  by (rule sym, rule GcdI) (rule gcd_lcm_factorial; assumption)+  eberlm@63498  574  show "Lcm A = Lcm_factorial A"  eberlm@63498  575  by (rule sym, rule LcmI) (rule gcd_lcm_factorial; assumption)+  eberlm@63498  576 qed  eberlm@63498  577 haftmann@58023  578 lemma gcd_non_0:  haftmann@60430  579  "b \ 0 \ gcd a b = gcd b (a mod b)"  haftmann@60572  580  unfolding gcd_gcd_eucl by (fact gcd_eucl_non_0)  haftmann@58023  581 eberlm@62422  582 lemmas gcd_0 = gcd_0_right  eberlm@62422  583 lemmas dvd_gcd_iff = gcd_greatest_iff  haftmann@58023  584 lemmas gcd_greatest_iff = dvd_gcd_iff  haftmann@58023  585 haftmann@58023  586 lemma gcd_mod1 [simp]:  haftmann@60430  587  "gcd (a mod b) b = gcd a b"  haftmann@58023  588  by (rule gcdI, metis dvd_mod_iff gcd_dvd1 gcd_dvd2, simp_all add: gcd_greatest dvd_mod_iff)  haftmann@58023  589 haftmann@58023  590 lemma gcd_mod2 [simp]:  haftmann@60430  591  "gcd a (b mod a) = gcd a b"  haftmann@58023  592  by (rule gcdI, simp, metis dvd_mod_iff gcd_dvd1 gcd_dvd2, simp_all add: gcd_greatest dvd_mod_iff)  haftmann@58023  593   haftmann@58023  594 lemma euclidean_size_gcd_le1 [simp]:  haftmann@58023  595  assumes "a \ 0"  haftmann@58023  596  shows "euclidean_size (gcd a b) \ euclidean_size a"  haftmann@58023  597 proof -  haftmann@58023  598  have "gcd a b dvd a" by (rule gcd_dvd1)  haftmann@58023  599  then obtain c where A: "a = gcd a b * c" unfolding dvd_def by blast  wenzelm@60526  600  with \a \ 0\ show ?thesis by (subst (2) A, intro size_mult_mono) auto  haftmann@58023  601 qed  haftmann@58023  602 haftmann@58023  603 lemma euclidean_size_gcd_le2 [simp]:  haftmann@58023  604  "b \ 0 \ euclidean_size (gcd a b) \ euclidean_size b"  haftmann@58023  605  by (subst gcd.commute, rule euclidean_size_gcd_le1)  haftmann@58023  606 haftmann@58023  607 lemma euclidean_size_gcd_less1:  haftmann@58023  608  assumes "a \ 0" and "\a dvd b"  haftmann@58023  609  shows "euclidean_size (gcd a b) < euclidean_size a"  haftmann@58023  610 proof (rule ccontr)  haftmann@58023  611  assume "\euclidean_size (gcd a b) < euclidean_size a"  eberlm@62422  612  with \a \ 0\ have A: "euclidean_size (gcd a b) = euclidean_size a"  haftmann@58023  613  by (intro le_antisym, simp_all)  eberlm@62422  614  have "a dvd gcd a b"  eberlm@62422  615  by (rule dvd_euclidean_size_eq_imp_dvd) (simp_all add: assms A)  eberlm@62422  616  hence "a dvd b" using dvd_gcdD2 by blast  wenzelm@60526  617  with \\a dvd b\ show False by contradiction  haftmann@58023  618 qed  haftmann@58023  619 haftmann@58023  620 lemma euclidean_size_gcd_less2:  haftmann@58023  621  assumes "b \ 0" and "\b dvd a"  haftmann@58023  622  shows "euclidean_size (gcd a b) < euclidean_size b"  haftmann@58023  623  using assms by (subst gcd.commute, rule euclidean_size_gcd_less1)  haftmann@58023  624 haftmann@58023  625 lemma euclidean_size_lcm_le1:  haftmann@58023  626  assumes "a \ 0" and "b \ 0"  haftmann@58023  627  shows "euclidean_size a \ euclidean_size (lcm a b)"  haftmann@58023  628 proof -  haftmann@60690  629  have "a dvd lcm a b" by (rule dvd_lcm1)  haftmann@60690  630  then obtain c where A: "lcm a b = a * c" ..  eberlm@62429  631  with \a \ 0\ and \b \ 0\ have "c \ 0" by (auto simp: lcm_eq_0_iff)  haftmann@58023  632  then show ?thesis by (subst A, intro size_mult_mono)  haftmann@58023  633 qed  haftmann@58023  634 haftmann@58023  635 lemma euclidean_size_lcm_le2:  haftmann@58023  636  "a \ 0 \ b \ 0 \ euclidean_size b \ euclidean_size (lcm a b)"  haftmann@58023  637  using euclidean_size_lcm_le1 [of b a] by (simp add: ac_simps)  haftmann@58023  638 haftmann@58023  639 lemma euclidean_size_lcm_less1:  haftmann@58023  640  assumes "b \ 0" and "\b dvd a"  haftmann@58023  641  shows "euclidean_size a < euclidean_size (lcm a b)"  haftmann@58023  642 proof (rule ccontr)  haftmann@58023  643  from assms have "a \ 0" by auto  haftmann@58023  644  assume "\euclidean_size a < euclidean_size (lcm a b)"  wenzelm@60526  645  with \a \ 0\ and \b \ 0\ have "euclidean_size (lcm a b) = euclidean_size a"  haftmann@58023  646  by (intro le_antisym, simp, intro euclidean_size_lcm_le1)  haftmann@58023  647  with assms have "lcm a b dvd a"  eberlm@62429  648  by (rule_tac dvd_euclidean_size_eq_imp_dvd) (auto simp: lcm_eq_0_iff)  eberlm@62422  649  hence "b dvd a" by (rule lcm_dvdD2)  wenzelm@60526  650  with \\b dvd a\ show False by contradiction  haftmann@58023  651 qed  haftmann@58023  652 haftmann@58023  653 lemma euclidean_size_lcm_less2:  haftmann@58023  654  assumes "a \ 0" and "\a dvd b"  haftmann@58023  655  shows "euclidean_size b < euclidean_size (lcm a b)"  haftmann@58023  656  using assms euclidean_size_lcm_less1 [of a b] by (simp add: ac_simps)  haftmann@58023  657 eberlm@62428  658 lemma Lcm_eucl_set [code]:  eberlm@62428  659  "Lcm_eucl (set xs) = foldl lcm_eucl 1 xs"  eberlm@62428  660  by (simp add: Lcm_Lcm_eucl [symmetric] lcm_lcm_eucl Lcm_set)  haftmann@58023  661 eberlm@62428  662 lemma Gcd_eucl_set [code]:  eberlm@62428  663  "Gcd_eucl (set xs) = foldl gcd_eucl 0 xs"  eberlm@62428  664  by (simp add: Gcd_Gcd_eucl [symmetric] gcd_gcd_eucl Gcd_set)  haftmann@58023  665 haftmann@58023  666 end  haftmann@58023  667 eberlm@63498  668 wenzelm@60526  669 text \  haftmann@58023  670  A Euclidean ring is a Euclidean semiring with additive inverses. It provides a  haftmann@58023  671  few more lemmas; in particular, Bezout's lemma holds for any Euclidean ring.  wenzelm@60526  672 \  haftmann@58023  673 haftmann@58023  674 class euclidean_ring_gcd = euclidean_semiring_gcd + idom  haftmann@58023  675 begin  haftmann@58023  676 haftmann@58023  677 subclass euclidean_ring ..  haftmann@60439  678 subclass ring_gcd ..  eberlm@63498  679 subclass factorial_ring_gcd ..  haftmann@60439  680 haftmann@60572  681 lemma euclid_ext_gcd [simp]:  haftmann@60572  682  "(case euclid_ext a b of (_, _ , t) \ t) = gcd a b"  eberlm@62442  683  using euclid_ext_correct'[of a b] by (simp add: case_prod_unfold Let_def gcd_gcd_eucl)  haftmann@60572  684 haftmann@60572  685 lemma euclid_ext_gcd' [simp]:  haftmann@60572  686  "euclid_ext a b = (r, s, t) \ t = gcd a b"  haftmann@60572  687  by (insert euclid_ext_gcd[of a b], drule (1) subst, simp)  eberlm@62442  688 eberlm@62442  689 lemma euclid_ext_correct:  haftmann@64177  690  "case euclid_ext a b of (x,y,c) \ x * a + y * b = c \ c = gcd a b"  haftmann@64177  691  using euclid_ext_correct'[of a b]  eberlm@62442  692  by (simp add: gcd_gcd_eucl case_prod_unfold)  haftmann@60572  693   haftmann@60572  694 lemma euclid_ext'_correct:  haftmann@60572  695  "fst (euclid_ext' a b) * a + snd (euclid_ext' a b) * b = gcd a b"  eberlm@62442  696  using euclid_ext_correct'[of a b]  eberlm@62442  697  by (simp add: gcd_gcd_eucl case_prod_unfold euclid_ext'_def)  haftmann@60572  698 haftmann@60572  699 lemma bezout: "\s t. s * a + t * b = gcd a b"  haftmann@60572  700  using euclid_ext'_correct by blast  haftmann@60572  701 haftmann@60572  702 end  haftmann@58023  703 haftmann@58023  704 haftmann@60572  705 subsection \Typical instances\  haftmann@58023  706 haftmann@58023  707 instantiation nat :: euclidean_semiring  haftmann@58023  708 begin  haftmann@58023  709 haftmann@58023  710 definition [simp]:  haftmann@58023  711  "euclidean_size_nat = (id :: nat \ nat)"  haftmann@58023  712 eberlm@63498  713 instance by standard simp_all  haftmann@58023  714 haftmann@58023  715 end  haftmann@58023  716 eberlm@62422  717 haftmann@58023  718 instantiation int :: euclidean_ring  haftmann@58023  719 begin  haftmann@58023  720 haftmann@58023  721 definition [simp]:  haftmann@58023  722  "euclidean_size_int = (nat \ abs :: int \ nat)"  haftmann@58023  723 eberlm@63498  724 instance by standard (auto simp add: abs_mult nat_mult_distrib split: abs_split)  haftmann@58023  725 haftmann@58023  726 end  haftmann@58023  727 eberlm@62422  728 instance nat :: euclidean_semiring_gcd  eberlm@62422  729 proof  eberlm@62422  730  show [simp]: "gcd = (gcd_eucl :: nat \ _)" "Lcm = (Lcm_eucl :: nat set \ _)"  eberlm@62422  731  by (simp_all add: eq_gcd_euclI eq_Lcm_euclI)  eberlm@62422  732  show "lcm = (lcm_eucl :: nat \ _)" "Gcd = (Gcd_eucl :: nat set \ _)"  eberlm@62422  733  by (intro ext, simp add: lcm_eucl_def lcm_nat_def Gcd_nat_def Gcd_eucl_def)+  eberlm@62422  734 qed  eberlm@62422  735 eberlm@62422  736 instance int :: euclidean_ring_gcd  eberlm@62422  737 proof  eberlm@62422  738  show [simp]: "gcd = (gcd_eucl :: int \ _)" "Lcm = (Lcm_eucl :: int set \ _)"  eberlm@62422  739  by (simp_all add: eq_gcd_euclI eq_Lcm_euclI)  eberlm@62422  740  show "lcm = (lcm_eucl :: int \ _)" "Gcd = (Gcd_eucl :: int set \ _)"  eberlm@62422  741  by (intro ext, simp add: lcm_eucl_def lcm_altdef_int  eberlm@62422  742  semiring_Gcd_class.Gcd_Lcm Gcd_eucl_def abs_mult)+  eberlm@62422  743 qed  eberlm@62422  744 haftmann@63924  745 end