src/HOL/Number_Theory/Euclidean_Algorithm.thy
 author haftmann Wed Jan 04 21:28:29 2017 +0100 (2017-01-04) changeset 64785 ae0bbc8e45ad parent 64784 5cb5e7ecb284 child 64786 340db65fd2c1 permissions -rw-r--r--
moved euclidean ring to HOL
```     1 (*  Title:      HOL/Number_Theory/Euclidean_Algorithm.thy
```
```     2     Author:     Manuel Eberl, TU Muenchen
```
```     3 *)
```
```     4
```
```     5 section \<open>Abstract euclidean algorithm in euclidean (semi)rings\<close>
```
```     6
```
```     7 theory Euclidean_Algorithm
```
```     8   imports "~~/src/HOL/GCD"
```
```     9     "~~/src/HOL/Number_Theory/Factorial_Ring"
```
```    10 begin
```
```    11
```
```    12 context euclidean_semiring
```
```    13 begin
```
```    14
```
```    15 function gcd_eucl :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"
```
```    16 where
```
```    17   "gcd_eucl a b = (if b = 0 then normalize a else gcd_eucl b (a mod b))"
```
```    18   by pat_completeness simp
```
```    19 termination
```
```    20   by (relation "measure (euclidean_size \<circ> snd)") (simp_all add: mod_size_less)
```
```    21
```
```    22 declare gcd_eucl.simps [simp del]
```
```    23
```
```    24 lemma gcd_eucl_induct [case_names zero mod]:
```
```    25   assumes H1: "\<And>b. P b 0"
```
```    26   and H2: "\<And>a b. b \<noteq> 0 \<Longrightarrow> P b (a mod b) \<Longrightarrow> P a b"
```
```    27   shows "P a b"
```
```    28 proof (induct a b rule: gcd_eucl.induct)
```
```    29   case ("1" a b)
```
```    30   show ?case
```
```    31   proof (cases "b = 0")
```
```    32     case True then show "P a b" by simp (rule H1)
```
```    33   next
```
```    34     case False
```
```    35     then have "P b (a mod b)"
```
```    36       by (rule "1.hyps")
```
```    37     with \<open>b \<noteq> 0\<close> show "P a b"
```
```    38       by (blast intro: H2)
```
```    39   qed
```
```    40 qed
```
```    41
```
```    42 definition lcm_eucl :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"
```
```    43 where
```
```    44   "lcm_eucl a b = normalize (a * b) div gcd_eucl a b"
```
```    45
```
```    46 definition Lcm_eucl :: "'a set \<Rightarrow> 'a" \<comment> \<open>
```
```    47   Somewhat complicated definition of Lcm that has the advantage of working
```
```    48   for infinite sets as well\<close>
```
```    49 where
```
```    50   "Lcm_eucl A = (if \<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) then
```
```    51      let l = SOME l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l =
```
```    52        (LEAST n. \<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n)
```
```    53        in normalize l
```
```    54       else 0)"
```
```    55
```
```    56 definition Gcd_eucl :: "'a set \<Rightarrow> 'a"
```
```    57 where
```
```    58   "Gcd_eucl A = Lcm_eucl {d. \<forall>a\<in>A. d dvd a}"
```
```    59
```
```    60 declare Lcm_eucl_def Gcd_eucl_def [code del]
```
```    61
```
```    62 lemma gcd_eucl_0:
```
```    63   "gcd_eucl a 0 = normalize a"
```
```    64   by (simp add: gcd_eucl.simps [of a 0])
```
```    65
```
```    66 lemma gcd_eucl_0_left:
```
```    67   "gcd_eucl 0 a = normalize a"
```
```    68   by (simp_all add: gcd_eucl_0 gcd_eucl.simps [of 0 a])
```
```    69
```
```    70 lemma gcd_eucl_non_0:
```
```    71   "b \<noteq> 0 \<Longrightarrow> gcd_eucl a b = gcd_eucl b (a mod b)"
```
```    72   by (simp add: gcd_eucl.simps [of a b] gcd_eucl.simps [of b 0])
```
```    73
```
```    74 lemma gcd_eucl_dvd1 [iff]: "gcd_eucl a b dvd a"
```
```    75   and gcd_eucl_dvd2 [iff]: "gcd_eucl a b dvd b"
```
```    76   by (induct a b rule: gcd_eucl_induct)
```
```    77      (simp_all add: gcd_eucl_0 gcd_eucl_non_0 dvd_mod_iff)
```
```    78
```
```    79 lemma normalize_gcd_eucl [simp]:
```
```    80   "normalize (gcd_eucl a b) = gcd_eucl a b"
```
```    81   by (induct a b rule: gcd_eucl_induct) (simp_all add: gcd_eucl_0 gcd_eucl_non_0)
```
```    82
```
```    83 lemma gcd_eucl_greatest:
```
```    84   fixes k a b :: 'a
```
```    85   shows "k dvd a \<Longrightarrow> k dvd b \<Longrightarrow> k dvd gcd_eucl a b"
```
```    86 proof (induct a b rule: gcd_eucl_induct)
```
```    87   case (zero a) from zero(1) show ?case by (rule dvd_trans) (simp add: gcd_eucl_0)
```
```    88 next
```
```    89   case (mod a b)
```
```    90   then show ?case
```
```    91     by (simp add: gcd_eucl_non_0 dvd_mod_iff)
```
```    92 qed
```
```    93
```
```    94 lemma gcd_euclI:
```
```    95   fixes gcd :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"
```
```    96   assumes "d dvd a" "d dvd b" "normalize d = d"
```
```    97           "\<And>k. k dvd a \<Longrightarrow> k dvd b \<Longrightarrow> k dvd d"
```
```    98   shows   "gcd_eucl a b = d"
```
```    99   by (rule associated_eqI) (simp_all add: gcd_eucl_greatest assms)
```
```   100
```
```   101 lemma eq_gcd_euclI:
```
```   102   fixes gcd :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"
```
```   103   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"
```
```   104           "\<And>a b k. k dvd a \<Longrightarrow> k dvd b \<Longrightarrow> k dvd gcd a b"
```
```   105   shows   "gcd = gcd_eucl"
```
```   106   by (intro ext, rule associated_eqI) (simp_all add: gcd_eucl_greatest assms)
```
```   107
```
```   108 lemma gcd_eucl_zero [simp]:
```
```   109   "gcd_eucl a b = 0 \<longleftrightarrow> a = 0 \<and> b = 0"
```
```   110   by (metis dvd_0_left dvd_refl gcd_eucl_dvd1 gcd_eucl_dvd2 gcd_eucl_greatest)+
```
```   111
```
```   112
```
```   113 lemma dvd_Lcm_eucl [simp]: "a \<in> A \<Longrightarrow> a dvd Lcm_eucl A"
```
```   114   and Lcm_eucl_least: "(\<And>a. a \<in> A \<Longrightarrow> a dvd b) \<Longrightarrow> Lcm_eucl A dvd b"
```
```   115   and unit_factor_Lcm_eucl [simp]:
```
```   116           "unit_factor (Lcm_eucl A) = (if Lcm_eucl A = 0 then 0 else 1)"
```
```   117 proof -
```
```   118   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>
```
```   119     unit_factor (Lcm_eucl A) = (if Lcm_eucl A = 0 then 0 else 1)" (is ?thesis)
```
```   120   proof (cases "\<exists>l. l \<noteq>  0 \<and> (\<forall>a\<in>A. a dvd l)")
```
```   121     case False
```
```   122     hence "Lcm_eucl A = 0" by (auto simp: Lcm_eucl_def)
```
```   123     with False show ?thesis by auto
```
```   124   next
```
```   125     case True
```
```   126     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
```
```   127     define n where "n = (LEAST n. \<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n)"
```
```   128     define l where "l = (SOME l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n)"
```
```   129     have "\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n"
```
```   130       apply (subst n_def)
```
```   131       apply (rule LeastI[of _ "euclidean_size l\<^sub>0"])
```
```   132       apply (rule exI[of _ l\<^sub>0])
```
```   133       apply (simp add: l\<^sub>0_props)
```
```   134       done
```
```   135     from someI_ex[OF this] have "l \<noteq> 0" and "\<forall>a\<in>A. a dvd l" and "euclidean_size l = n"
```
```   136       unfolding l_def by simp_all
```
```   137     {
```
```   138       fix l' assume "\<forall>a\<in>A. a dvd l'"
```
```   139       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)
```
```   140       moreover from \<open>l \<noteq> 0\<close> have "gcd_eucl l l' \<noteq> 0" by simp
```
```   141       ultimately have "\<exists>b. b \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd b) \<and>
```
```   142                           euclidean_size b = euclidean_size (gcd_eucl l l')"
```
```   143         by (intro exI[of _ "gcd_eucl l l'"], auto)
```
```   144       hence "euclidean_size (gcd_eucl l l') \<ge> n" by (subst n_def) (rule Least_le)
```
```   145       moreover have "euclidean_size (gcd_eucl l l') \<le> n"
```
```   146       proof -
```
```   147         have "gcd_eucl l l' dvd l" by simp
```
```   148         then obtain a where "l = gcd_eucl l l' * a" unfolding dvd_def by blast
```
```   149         with \<open>l \<noteq> 0\<close> have "a \<noteq> 0" by auto
```
```   150         hence "euclidean_size (gcd_eucl l l') \<le> euclidean_size (gcd_eucl l l' * a)"
```
```   151           by (rule size_mult_mono)
```
```   152         also have "gcd_eucl l l' * a = l" using \<open>l = gcd_eucl l l' * a\<close> ..
```
```   153         also note \<open>euclidean_size l = n\<close>
```
```   154         finally show "euclidean_size (gcd_eucl l l') \<le> n" .
```
```   155       qed
```
```   156       ultimately have *: "euclidean_size l = euclidean_size (gcd_eucl l l')"
```
```   157         by (intro le_antisym, simp_all add: \<open>euclidean_size l = n\<close>)
```
```   158       from \<open>l \<noteq> 0\<close> have "l dvd gcd_eucl l l'"
```
```   159         by (rule dvd_euclidean_size_eq_imp_dvd) (auto simp add: *)
```
```   160       hence "l dvd l'" by (rule dvd_trans[OF _ gcd_eucl_dvd2])
```
```   161     }
```
```   162
```
```   163     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>
```
```   164       have "(\<forall>a\<in>A. a dvd normalize l) \<and>
```
```   165         (\<forall>l'. (\<forall>a\<in>A. a dvd l') \<longrightarrow> normalize l dvd l') \<and>
```
```   166         unit_factor (normalize l) =
```
```   167         (if normalize l = 0 then 0 else 1)"
```
```   168       by (auto simp: unit_simps)
```
```   169     also from True have "normalize l = Lcm_eucl A"
```
```   170       by (simp add: Lcm_eucl_def Let_def n_def l_def)
```
```   171     finally show ?thesis .
```
```   172   qed
```
```   173   note A = this
```
```   174
```
```   175   {fix a assume "a \<in> A" then show "a dvd Lcm_eucl A" using A by blast}
```
```   176   {fix b assume "\<And>a. a \<in> A \<Longrightarrow> a dvd b" then show "Lcm_eucl A dvd b" using A by blast}
```
```   177   from A show "unit_factor (Lcm_eucl A) = (if Lcm_eucl A = 0 then 0 else 1)" by blast
```
```   178 qed
```
```   179
```
```   180 lemma normalize_Lcm_eucl [simp]:
```
```   181   "normalize (Lcm_eucl A) = Lcm_eucl A"
```
```   182 proof (cases "Lcm_eucl A = 0")
```
```   183   case True then show ?thesis by simp
```
```   184 next
```
```   185   case False
```
```   186   have "unit_factor (Lcm_eucl A) * normalize (Lcm_eucl A) = Lcm_eucl A"
```
```   187     by (fact unit_factor_mult_normalize)
```
```   188   with False show ?thesis by simp
```
```   189 qed
```
```   190
```
```   191 lemma eq_Lcm_euclI:
```
```   192   fixes lcm :: "'a set \<Rightarrow> 'a"
```
```   193   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"
```
```   194           "\<And>A. normalize (lcm A) = lcm A" shows "lcm = Lcm_eucl"
```
```   195   by (intro ext, rule associated_eqI) (auto simp: assms intro: Lcm_eucl_least)
```
```   196
```
```   197 lemma Gcd_eucl_dvd: "a \<in> A \<Longrightarrow> Gcd_eucl A dvd a"
```
```   198   unfolding Gcd_eucl_def by (auto intro: Lcm_eucl_least)
```
```   199
```
```   200 lemma Gcd_eucl_greatest: "(\<And>x. x \<in> A \<Longrightarrow> d dvd x) \<Longrightarrow> d dvd Gcd_eucl A"
```
```   201   unfolding Gcd_eucl_def by auto
```
```   202
```
```   203 lemma normalize_Gcd_eucl [simp]: "normalize (Gcd_eucl A) = Gcd_eucl A"
```
```   204   by (simp add: Gcd_eucl_def)
```
```   205
```
```   206 lemma Lcm_euclI:
```
```   207   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"
```
```   208   shows   "Lcm_eucl A = d"
```
```   209 proof -
```
```   210   have "normalize (Lcm_eucl A) = normalize d"
```
```   211     by (intro associatedI) (auto intro: dvd_Lcm_eucl Lcm_eucl_least assms)
```
```   212   thus ?thesis by (simp add: assms)
```
```   213 qed
```
```   214
```
```   215 lemma Gcd_euclI:
```
```   216   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"
```
```   217   shows   "Gcd_eucl A = d"
```
```   218 proof -
```
```   219   have "normalize (Gcd_eucl A) = normalize d"
```
```   220     by (intro associatedI) (auto intro: Gcd_eucl_dvd Gcd_eucl_greatest assms)
```
```   221   thus ?thesis by (simp add: assms)
```
```   222 qed
```
```   223
```
```   224 lemmas lcm_gcd_eucl_facts =
```
```   225   gcd_eucl_dvd1 gcd_eucl_dvd2 gcd_eucl_greatest normalize_gcd_eucl lcm_eucl_def
```
```   226   Gcd_eucl_def Gcd_eucl_dvd Gcd_eucl_greatest normalize_Gcd_eucl
```
```   227   dvd_Lcm_eucl Lcm_eucl_least normalize_Lcm_eucl
```
```   228
```
```   229 lemma normalized_factors_product:
```
```   230   "{p. p dvd a * b \<and> normalize p = p} =
```
```   231      (\<lambda>(x,y). x * y) ` ({p. p dvd a \<and> normalize p = p} \<times> {p. p dvd b \<and> normalize p = p})"
```
```   232 proof safe
```
```   233   fix p assume p: "p dvd a * b" "normalize p = p"
```
```   234   interpret semiring_gcd 1 0 "op *" gcd_eucl lcm_eucl "op div" "op +" "op -" normalize unit_factor
```
```   235     by standard (rule lcm_gcd_eucl_facts; assumption)+
```
```   236   from dvd_productE[OF p(1)] guess x y . note xy = this
```
```   237   define x' y' where "x' = normalize x" and "y' = normalize y"
```
```   238   have "p = x' * y'"
```
```   239     by (subst p(2) [symmetric]) (simp add: xy x'_def y'_def normalize_mult)
```
```   240   moreover from xy have "normalize x' = x'" "normalize y' = y'" "x' dvd a" "y' dvd b"
```
```   241     by (simp_all add: x'_def y'_def)
```
```   242   ultimately show "p \<in> (\<lambda>(x, y). x * y) `
```
```   243                      ({p. p dvd a \<and> normalize p = p} \<times> {p. p dvd b \<and> normalize p = p})"
```
```   244     by blast
```
```   245 qed (auto simp: normalize_mult mult_dvd_mono)
```
```   246
```
```   247
```
```   248 subclass factorial_semiring
```
```   249 proof (standard, rule factorial_semiring_altI_aux)
```
```   250   fix x assume "x \<noteq> 0"
```
```   251   thus "finite {p. p dvd x \<and> normalize p = p}"
```
```   252   proof (induction "euclidean_size x" arbitrary: x rule: less_induct)
```
```   253     case (less x)
```
```   254     show ?case
```
```   255     proof (cases "\<exists>y. y dvd x \<and> \<not>x dvd y \<and> \<not>is_unit y")
```
```   256       case False
```
```   257       have "{p. p dvd x \<and> normalize p = p} \<subseteq> {1, normalize x}"
```
```   258       proof
```
```   259         fix p assume p: "p \<in> {p. p dvd x \<and> normalize p = p}"
```
```   260         with False have "is_unit p \<or> x dvd p" by blast
```
```   261         thus "p \<in> {1, normalize x}"
```
```   262         proof (elim disjE)
```
```   263           assume "is_unit p"
```
```   264           hence "normalize p = 1" by (simp add: is_unit_normalize)
```
```   265           with p show ?thesis by simp
```
```   266         next
```
```   267           assume "x dvd p"
```
```   268           with p have "normalize p = normalize x" by (intro associatedI) simp_all
```
```   269           with p show ?thesis by simp
```
```   270         qed
```
```   271       qed
```
```   272       moreover have "finite \<dots>" by simp
```
```   273       ultimately show ?thesis by (rule finite_subset)
```
```   274
```
```   275     next
```
```   276       case True
```
```   277       then obtain y where y: "y dvd x" "\<not>x dvd y" "\<not>is_unit y" by blast
```
```   278       define z where "z = x div y"
```
```   279       let ?fctrs = "\<lambda>x. {p. p dvd x \<and> normalize p = p}"
```
```   280       from y have x: "x = y * z" by (simp add: z_def)
```
```   281       with less.prems have "y \<noteq> 0" "z \<noteq> 0" by auto
```
```   282       from x y have "\<not>is_unit z" by (auto simp: mult_unit_dvd_iff)
```
```   283       have "?fctrs x = (\<lambda>(p,p'). p * p') ` (?fctrs y \<times> ?fctrs z)"
```
```   284         by (subst x) (rule normalized_factors_product)
```
```   285       also have "\<not>y * z dvd y * 1" "\<not>y * z dvd 1 * z"
```
```   286         by (subst dvd_times_left_cancel_iff dvd_times_right_cancel_iff; fact)+
```
```   287       hence "finite ((\<lambda>(p,p'). p * p') ` (?fctrs y \<times> ?fctrs z))"
```
```   288         by (intro finite_imageI finite_cartesian_product less dvd_proper_imp_size_less)
```
```   289            (auto simp: x)
```
```   290       finally show ?thesis .
```
```   291     qed
```
```   292   qed
```
```   293 next
```
```   294   interpret semiring_gcd 1 0 "op *" gcd_eucl lcm_eucl "op div" "op +" "op -" normalize unit_factor
```
```   295     by standard (rule lcm_gcd_eucl_facts; assumption)+
```
```   296   fix p assume p: "irreducible p"
```
```   297   thus "prime_elem p" by (rule irreducible_imp_prime_elem_gcd)
```
```   298 qed
```
```   299
```
```   300 lemma gcd_eucl_eq_gcd_factorial: "gcd_eucl = gcd_factorial"
```
```   301   by (intro ext gcd_euclI gcd_lcm_factorial)
```
```   302
```
```   303 lemma lcm_eucl_eq_lcm_factorial: "lcm_eucl = lcm_factorial"
```
```   304   by (intro ext) (simp add: lcm_eucl_def lcm_factorial_gcd_factorial gcd_eucl_eq_gcd_factorial)
```
```   305
```
```   306 lemma Gcd_eucl_eq_Gcd_factorial: "Gcd_eucl = Gcd_factorial"
```
```   307   by (intro ext Gcd_euclI gcd_lcm_factorial)
```
```   308
```
```   309 lemma Lcm_eucl_eq_Lcm_factorial: "Lcm_eucl = Lcm_factorial"
```
```   310   by (intro ext Lcm_euclI gcd_lcm_factorial)
```
```   311
```
```   312 lemmas eucl_eq_factorial =
```
```   313   gcd_eucl_eq_gcd_factorial lcm_eucl_eq_lcm_factorial
```
```   314   Gcd_eucl_eq_Gcd_factorial Lcm_eucl_eq_Lcm_factorial
```
```   315
```
```   316 end
```
```   317
```
```   318 context euclidean_ring
```
```   319 begin
```
```   320
```
```   321 function euclid_ext_aux :: "'a \<Rightarrow> _" where
```
```   322   "euclid_ext_aux r' r s' s t' t = (
```
```   323      if r = 0 then let c = 1 div unit_factor r' in (s' * c, t' * c, normalize r')
```
```   324      else let q = r' div r
```
```   325           in  euclid_ext_aux r (r' mod r) s (s' - q * s) t (t' - q * t))"
```
```   326 by auto
```
```   327 termination by (relation "measure (\<lambda>(_,b,_,_,_,_). euclidean_size b)") (simp_all add: mod_size_less)
```
```   328
```
```   329 declare euclid_ext_aux.simps [simp del]
```
```   330
```
```   331 lemma euclid_ext_aux_correct:
```
```   332   assumes "gcd_eucl r' r = gcd_eucl a b"
```
```   333   assumes "s' * a + t' * b = r'"
```
```   334   assumes "s * a + t * b = r"
```
```   335   shows   "case euclid_ext_aux r' r s' s t' t of (x,y,c) \<Rightarrow>
```
```   336              x * a + y * b = c \<and> c = gcd_eucl a b" (is "?P (euclid_ext_aux r' r s' s t' t)")
```
```   337 using assms
```
```   338 proof (induction r' r s' s t' t rule: euclid_ext_aux.induct)
```
```   339   case (1 r' r s' s t' t)
```
```   340   show ?case
```
```   341   proof (cases "r = 0")
```
```   342     case True
```
```   343     hence "euclid_ext_aux r' r s' s t' t =
```
```   344              (s' div unit_factor r', t' div unit_factor r', normalize r')"
```
```   345       by (subst euclid_ext_aux.simps) (simp add: Let_def)
```
```   346     also have "?P \<dots>"
```
```   347     proof safe
```
```   348       have "s' div unit_factor r' * a + t' div unit_factor r' * b =
```
```   349                 (s' * a + t' * b) div unit_factor r'"
```
```   350         by (cases "r' = 0") (simp_all add: unit_div_commute)
```
```   351       also have "s' * a + t' * b = r'" by fact
```
```   352       also have "\<dots> div unit_factor r' = normalize r'" by simp
```
```   353       finally show "s' div unit_factor r' * a + t' div unit_factor r' * b = normalize r'" .
```
```   354     next
```
```   355       from "1.prems" True show "normalize r' = gcd_eucl a b" by (simp add: gcd_eucl_0)
```
```   356     qed
```
```   357     finally show ?thesis .
```
```   358   next
```
```   359     case False
```
```   360     hence "euclid_ext_aux r' r s' s t' t =
```
```   361              euclid_ext_aux r (r' mod r) s (s' - r' div r * s) t (t' - r' div r * t)"
```
```   362       by (subst euclid_ext_aux.simps) (simp add: Let_def)
```
```   363     also from "1.prems" False have "?P \<dots>"
```
```   364     proof (intro "1.IH")
```
```   365       have "(s' - r' div r * s) * a + (t' - r' div r * t) * b =
```
```   366               (s' * a + t' * b) - r' div r * (s * a + t * b)" by (simp add: algebra_simps)
```
```   367       also have "s' * a + t' * b = r'" by fact
```
```   368       also have "s * a + t * b = r" by fact
```
```   369       also have "r' - r' div r * r = r' mod r" using div_mult_mod_eq [of r' r]
```
```   370         by (simp add: algebra_simps)
```
```   371       finally show "(s' - r' div r * s) * a + (t' - r' div r * t) * b = r' mod r" .
```
```   372     qed (auto simp: gcd_eucl_non_0 algebra_simps minus_mod_eq_div_mult [symmetric])
```
```   373     finally show ?thesis .
```
```   374   qed
```
```   375 qed
```
```   376
```
```   377 definition euclid_ext where
```
```   378   "euclid_ext a b = euclid_ext_aux a b 1 0 0 1"
```
```   379
```
```   380 lemma euclid_ext_0:
```
```   381   "euclid_ext a 0 = (1 div unit_factor a, 0, normalize a)"
```
```   382   by (simp add: euclid_ext_def euclid_ext_aux.simps)
```
```   383
```
```   384 lemma euclid_ext_left_0:
```
```   385   "euclid_ext 0 a = (0, 1 div unit_factor a, normalize a)"
```
```   386   by (simp add: euclid_ext_def euclid_ext_aux.simps)
```
```   387
```
```   388 lemma euclid_ext_correct':
```
```   389   "case euclid_ext a b of (x,y,c) \<Rightarrow> x * a + y * b = c \<and> c = gcd_eucl a b"
```
```   390   unfolding euclid_ext_def by (rule euclid_ext_aux_correct) simp_all
```
```   391
```
```   392 lemma euclid_ext_gcd_eucl:
```
```   393   "(case euclid_ext a b of (x,y,c) \<Rightarrow> c) = gcd_eucl a b"
```
```   394   using euclid_ext_correct'[of a b] by (simp add: case_prod_unfold)
```
```   395
```
```   396 definition euclid_ext' where
```
```   397   "euclid_ext' a b = (case euclid_ext a b of (x, y, _) \<Rightarrow> (x, y))"
```
```   398
```
```   399 lemma euclid_ext'_correct':
```
```   400   "case euclid_ext' a b of (x,y) \<Rightarrow> x * a + y * b = gcd_eucl a b"
```
```   401   using euclid_ext_correct'[of a b] by (simp add: case_prod_unfold euclid_ext'_def)
```
```   402
```
```   403 lemma euclid_ext'_0: "euclid_ext' a 0 = (1 div unit_factor a, 0)"
```
```   404   by (simp add: euclid_ext'_def euclid_ext_0)
```
```   405
```
```   406 lemma euclid_ext'_left_0: "euclid_ext' 0 a = (0, 1 div unit_factor a)"
```
```   407   by (simp add: euclid_ext'_def euclid_ext_left_0)
```
```   408
```
```   409 end
```
```   410
```
```   411 class euclidean_semiring_gcd = euclidean_semiring + gcd + Gcd +
```
```   412   assumes gcd_gcd_eucl: "gcd = gcd_eucl" and lcm_lcm_eucl: "lcm = lcm_eucl"
```
```   413   assumes Gcd_Gcd_eucl: "Gcd = Gcd_eucl" and Lcm_Lcm_eucl: "Lcm = Lcm_eucl"
```
```   414 begin
```
```   415
```
```   416 subclass semiring_gcd
```
```   417   by standard (simp_all add: gcd_gcd_eucl gcd_eucl_greatest lcm_lcm_eucl lcm_eucl_def)
```
```   418
```
```   419 subclass semiring_Gcd
```
```   420   by standard (auto simp: Gcd_Gcd_eucl Lcm_Lcm_eucl Gcd_eucl_def intro: Lcm_eucl_least)
```
```   421
```
```   422 subclass factorial_semiring_gcd
```
```   423 proof
```
```   424   fix a b
```
```   425   show "gcd a b = gcd_factorial a b"
```
```   426     by (rule sym, rule gcdI) (rule gcd_lcm_factorial; assumption)+
```
```   427   thus "lcm a b = lcm_factorial a b"
```
```   428     by (simp add: lcm_factorial_gcd_factorial lcm_gcd)
```
```   429 next
```
```   430   fix A
```
```   431   show "Gcd A = Gcd_factorial A"
```
```   432     by (rule sym, rule GcdI) (rule gcd_lcm_factorial; assumption)+
```
```   433   show "Lcm A = Lcm_factorial A"
```
```   434     by (rule sym, rule LcmI) (rule gcd_lcm_factorial; assumption)+
```
```   435 qed
```
```   436
```
```   437 lemma gcd_non_0:
```
```   438   "b \<noteq> 0 \<Longrightarrow> gcd a b = gcd b (a mod b)"
```
```   439   unfolding gcd_gcd_eucl by (fact gcd_eucl_non_0)
```
```   440
```
```   441 lemmas gcd_0 = gcd_0_right
```
```   442 lemmas dvd_gcd_iff = gcd_greatest_iff
```
```   443 lemmas gcd_greatest_iff = dvd_gcd_iff
```
```   444
```
```   445 lemma gcd_mod1 [simp]:
```
```   446   "gcd (a mod b) b = gcd a b"
```
```   447   by (rule gcdI, metis dvd_mod_iff gcd_dvd1 gcd_dvd2, simp_all add: gcd_greatest dvd_mod_iff)
```
```   448
```
```   449 lemma gcd_mod2 [simp]:
```
```   450   "gcd a (b mod a) = gcd a b"
```
```   451   by (rule gcdI, simp, metis dvd_mod_iff gcd_dvd1 gcd_dvd2, simp_all add: gcd_greatest dvd_mod_iff)
```
```   452
```
```   453 lemma euclidean_size_gcd_le1 [simp]:
```
```   454   assumes "a \<noteq> 0"
```
```   455   shows "euclidean_size (gcd a b) \<le> euclidean_size a"
```
```   456 proof -
```
```   457    have "gcd a b dvd a" by (rule gcd_dvd1)
```
```   458    then obtain c where A: "a = gcd a b * c" unfolding dvd_def by blast
```
```   459    with \<open>a \<noteq> 0\<close> show ?thesis by (subst (2) A, intro size_mult_mono) auto
```
```   460 qed
```
```   461
```
```   462 lemma euclidean_size_gcd_le2 [simp]:
```
```   463   "b \<noteq> 0 \<Longrightarrow> euclidean_size (gcd a b) \<le> euclidean_size b"
```
```   464   by (subst gcd.commute, rule euclidean_size_gcd_le1)
```
```   465
```
```   466 lemma euclidean_size_gcd_less1:
```
```   467   assumes "a \<noteq> 0" and "\<not>a dvd b"
```
```   468   shows "euclidean_size (gcd a b) < euclidean_size a"
```
```   469 proof (rule ccontr)
```
```   470   assume "\<not>euclidean_size (gcd a b) < euclidean_size a"
```
```   471   with \<open>a \<noteq> 0\<close> have A: "euclidean_size (gcd a b) = euclidean_size a"
```
```   472     by (intro le_antisym, simp_all)
```
```   473   have "a dvd gcd a b"
```
```   474     by (rule dvd_euclidean_size_eq_imp_dvd) (simp_all add: assms A)
```
```   475   hence "a dvd b" using dvd_gcdD2 by blast
```
```   476   with \<open>\<not>a dvd b\<close> show False by contradiction
```
```   477 qed
```
```   478
```
```   479 lemma euclidean_size_gcd_less2:
```
```   480   assumes "b \<noteq> 0" and "\<not>b dvd a"
```
```   481   shows "euclidean_size (gcd a b) < euclidean_size b"
```
```   482   using assms by (subst gcd.commute, rule euclidean_size_gcd_less1)
```
```   483
```
```   484 lemma euclidean_size_lcm_le1:
```
```   485   assumes "a \<noteq> 0" and "b \<noteq> 0"
```
```   486   shows "euclidean_size a \<le> euclidean_size (lcm a b)"
```
```   487 proof -
```
```   488   have "a dvd lcm a b" by (rule dvd_lcm1)
```
```   489   then obtain c where A: "lcm a b = a * c" ..
```
```   490   with \<open>a \<noteq> 0\<close> and \<open>b \<noteq> 0\<close> have "c \<noteq> 0" by (auto simp: lcm_eq_0_iff)
```
```   491   then show ?thesis by (subst A, intro size_mult_mono)
```
```   492 qed
```
```   493
```
```   494 lemma euclidean_size_lcm_le2:
```
```   495   "a \<noteq> 0 \<Longrightarrow> b \<noteq> 0 \<Longrightarrow> euclidean_size b \<le> euclidean_size (lcm a b)"
```
```   496   using euclidean_size_lcm_le1 [of b a] by (simp add: ac_simps)
```
```   497
```
```   498 lemma euclidean_size_lcm_less1:
```
```   499   assumes "b \<noteq> 0" and "\<not>b dvd a"
```
```   500   shows "euclidean_size a < euclidean_size (lcm a b)"
```
```   501 proof (rule ccontr)
```
```   502   from assms have "a \<noteq> 0" by auto
```
```   503   assume "\<not>euclidean_size a < euclidean_size (lcm a b)"
```
```   504   with \<open>a \<noteq> 0\<close> and \<open>b \<noteq> 0\<close> have "euclidean_size (lcm a b) = euclidean_size a"
```
```   505     by (intro le_antisym, simp, intro euclidean_size_lcm_le1)
```
```   506   with assms have "lcm a b dvd a"
```
```   507     by (rule_tac dvd_euclidean_size_eq_imp_dvd) (auto simp: lcm_eq_0_iff)
```
```   508   hence "b dvd a" by (rule lcm_dvdD2)
```
```   509   with \<open>\<not>b dvd a\<close> show False by contradiction
```
```   510 qed
```
```   511
```
```   512 lemma euclidean_size_lcm_less2:
```
```   513   assumes "a \<noteq> 0" and "\<not>a dvd b"
```
```   514   shows "euclidean_size b < euclidean_size (lcm a b)"
```
```   515   using assms euclidean_size_lcm_less1 [of a b] by (simp add: ac_simps)
```
```   516
```
```   517 lemma Lcm_eucl_set [code]:
```
```   518   "Lcm_eucl (set xs) = foldl lcm_eucl 1 xs"
```
```   519   by (simp add: Lcm_Lcm_eucl [symmetric] lcm_lcm_eucl Lcm_set)
```
```   520
```
```   521 lemma Gcd_eucl_set [code]:
```
```   522   "Gcd_eucl (set xs) = foldl gcd_eucl 0 xs"
```
```   523   by (simp add: Gcd_Gcd_eucl [symmetric] gcd_gcd_eucl Gcd_set)
```
```   524
```
```   525 end
```
```   526
```
```   527
```
```   528 text \<open>
```
```   529   A Euclidean ring is a Euclidean semiring with additive inverses. It provides a
```
```   530   few more lemmas; in particular, Bezout's lemma holds for any Euclidean ring.
```
```   531 \<close>
```
```   532
```
```   533 class euclidean_ring_gcd = euclidean_semiring_gcd + idom
```
```   534 begin
```
```   535
```
```   536 subclass euclidean_ring ..
```
```   537 subclass ring_gcd ..
```
```   538 subclass factorial_ring_gcd ..
```
```   539
```
```   540 lemma euclid_ext_gcd [simp]:
```
```   541   "(case euclid_ext a b of (_, _ , t) \<Rightarrow> t) = gcd a b"
```
```   542   using euclid_ext_correct'[of a b] by (simp add: case_prod_unfold Let_def gcd_gcd_eucl)
```
```   543
```
```   544 lemma euclid_ext_gcd' [simp]:
```
```   545   "euclid_ext a b = (r, s, t) \<Longrightarrow> t = gcd a b"
```
```   546   by (insert euclid_ext_gcd[of a b], drule (1) subst, simp)
```
```   547
```
```   548 lemma euclid_ext_correct:
```
```   549   "case euclid_ext a b of (x,y,c) \<Rightarrow> x * a + y * b = c \<and> c = gcd a b"
```
```   550   using euclid_ext_correct'[of a b]
```
```   551   by (simp add: gcd_gcd_eucl case_prod_unfold)
```
```   552
```
```   553 lemma euclid_ext'_correct:
```
```   554   "fst (euclid_ext' a b) * a + snd (euclid_ext' a b) * b = gcd a b"
```
```   555   using euclid_ext_correct'[of a b]
```
```   556   by (simp add: gcd_gcd_eucl case_prod_unfold euclid_ext'_def)
```
```   557
```
```   558 lemma bezout: "\<exists>s t. s * a + t * b = gcd a b"
```
```   559   using euclid_ext'_correct by blast
```
```   560
```
```   561 end
```
```   562
```
```   563
```
```   564 subsection \<open>Typical instances\<close>
```
```   565
```
```   566 instance nat :: euclidean_semiring_gcd
```
```   567 proof
```
```   568   show [simp]: "gcd = (gcd_eucl :: nat \<Rightarrow> _)" "Lcm = (Lcm_eucl :: nat set \<Rightarrow> _)"
```
```   569     by (simp_all add: eq_gcd_euclI eq_Lcm_euclI)
```
```   570   show "lcm = (lcm_eucl :: nat \<Rightarrow> _)" "Gcd = (Gcd_eucl :: nat set \<Rightarrow> _)"
```
```   571     by (intro ext, simp add: lcm_eucl_def lcm_nat_def Gcd_nat_def Gcd_eucl_def)+
```
```   572 qed
```
```   573
```
```   574 instance int :: euclidean_ring_gcd
```
```   575 proof
```
```   576   show [simp]: "gcd = (gcd_eucl :: int \<Rightarrow> _)" "Lcm = (Lcm_eucl :: int set \<Rightarrow> _)"
```
```   577     by (simp_all add: eq_gcd_euclI eq_Lcm_euclI)
```
```   578   show "lcm = (lcm_eucl :: int \<Rightarrow> _)" "Gcd = (Gcd_eucl :: int set \<Rightarrow> _)"
```
```   579     by (intro ext, simp add: lcm_eucl_def lcm_altdef_int
```
```   580           semiring_Gcd_class.Gcd_Lcm Gcd_eucl_def abs_mult)+
```
```   581 qed
```
```   582
```
```   583 end
```