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