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