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