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