src/HOL/Number_Theory/Euclidean_Algorithm.thy
changeset 64786 340db65fd2c1
parent 64785 ae0bbc8e45ad
child 64848 c50db2128048
     1.1 --- a/src/HOL/Number_Theory/Euclidean_Algorithm.thy	Wed Jan 04 21:28:29 2017 +0100
     1.2 +++ b/src/HOL/Number_Theory/Euclidean_Algorithm.thy	Wed Jan 04 21:28:29 2017 +0100
     1.3 @@ -9,24 +9,28 @@
     1.4      "~~/src/HOL/Number_Theory/Factorial_Ring"
     1.5  begin
     1.6  
     1.7 +subsection \<open>Generic construction of the (simple) euclidean algorithm\<close>
     1.8 +  
     1.9  context euclidean_semiring
    1.10  begin
    1.11  
    1.12 -function gcd_eucl :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"
    1.13 -where
    1.14 -  "gcd_eucl a b = (if b = 0 then normalize a else gcd_eucl b (a mod b))"
    1.15 +context
    1.16 +begin
    1.17 +
    1.18 +qualified function gcd :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"
    1.19 +  where "gcd a b = (if b = 0 then normalize a else gcd b (a mod b))"
    1.20    by pat_completeness simp
    1.21  termination
    1.22    by (relation "measure (euclidean_size \<circ> snd)") (simp_all add: mod_size_less)
    1.23  
    1.24 -declare gcd_eucl.simps [simp del]
    1.25 +declare gcd.simps [simp del]
    1.26  
    1.27 -lemma gcd_eucl_induct [case_names zero mod]:
    1.28 +lemma eucl_induct [case_names zero mod]:
    1.29    assumes H1: "\<And>b. P b 0"
    1.30    and H2: "\<And>a b. b \<noteq> 0 \<Longrightarrow> P b (a mod b) \<Longrightarrow> P a b"
    1.31    shows "P a b"
    1.32 -proof (induct a b rule: gcd_eucl.induct)
    1.33 -  case ("1" a b)
    1.34 +proof (induct a b rule: gcd.induct)
    1.35 +  case (1 a b)
    1.36    show ?case
    1.37    proof (cases "b = 0")
    1.38      case True then show "P a b" by simp (rule H1)
    1.39 @@ -38,425 +42,305 @@
    1.40        by (blast intro: H2)
    1.41    qed
    1.42  qed
    1.43 -
    1.44 -definition lcm_eucl :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"
    1.45 -where
    1.46 -  "lcm_eucl a b = normalize (a * b) div gcd_eucl a b"
    1.47 +  
    1.48 +qualified lemma gcd_0:
    1.49 +  "gcd a 0 = normalize a"
    1.50 +  by (simp add: gcd.simps [of a 0])
    1.51 +  
    1.52 +qualified lemma gcd_mod:
    1.53 +  "a \<noteq> 0 \<Longrightarrow> gcd a (b mod a) = gcd b a"
    1.54 +  by (simp add: gcd.simps [of b 0] gcd.simps [of b a])
    1.55  
    1.56 -definition Lcm_eucl :: "'a set \<Rightarrow> 'a" \<comment> \<open>
    1.57 -  Somewhat complicated definition of Lcm that has the advantage of working
    1.58 -  for infinite sets as well\<close>
    1.59 -where
    1.60 -  "Lcm_eucl A = (if \<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) then
    1.61 +qualified definition lcm :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"
    1.62 +  where "lcm a b = normalize (a * b) div gcd a b"
    1.63 +
    1.64 +qualified definition Lcm :: "'a set \<Rightarrow> 'a" \<comment>
    1.65 +    \<open>Somewhat complicated definition of Lcm that has the advantage of working
    1.66 +    for infinite sets as well\<close>
    1.67 +  where
    1.68 +  [code del]: "Lcm A = (if \<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) then
    1.69       let l = SOME l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l =
    1.70         (LEAST n. \<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n)
    1.71         in normalize l 
    1.72        else 0)"
    1.73  
    1.74 -definition Gcd_eucl :: "'a set \<Rightarrow> 'a"
    1.75 -where
    1.76 -  "Gcd_eucl A = Lcm_eucl {d. \<forall>a\<in>A. d dvd a}"
    1.77 -
    1.78 -declare Lcm_eucl_def Gcd_eucl_def [code del]
    1.79 +qualified definition Gcd :: "'a set \<Rightarrow> 'a"
    1.80 +  where [code del]: "Gcd A = Lcm {d. \<forall>a\<in>A. d dvd a}"
    1.81  
    1.82 -lemma gcd_eucl_0:
    1.83 -  "gcd_eucl a 0 = normalize a"
    1.84 -  by (simp add: gcd_eucl.simps [of a 0])
    1.85 -
    1.86 -lemma gcd_eucl_0_left:
    1.87 -  "gcd_eucl 0 a = normalize a"
    1.88 -  by (simp_all add: gcd_eucl_0 gcd_eucl.simps [of 0 a])
    1.89 +end    
    1.90  
    1.91 -lemma gcd_eucl_non_0:
    1.92 -  "b \<noteq> 0 \<Longrightarrow> gcd_eucl a b = gcd_eucl b (a mod b)"
    1.93 -  by (simp add: gcd_eucl.simps [of a b] gcd_eucl.simps [of b 0])
    1.94 -
    1.95 -lemma gcd_eucl_dvd1 [iff]: "gcd_eucl a b dvd a"
    1.96 -  and gcd_eucl_dvd2 [iff]: "gcd_eucl a b dvd b"
    1.97 -  by (induct a b rule: gcd_eucl_induct)
    1.98 -     (simp_all add: gcd_eucl_0 gcd_eucl_non_0 dvd_mod_iff)
    1.99 -
   1.100 -lemma normalize_gcd_eucl [simp]:
   1.101 -  "normalize (gcd_eucl a b) = gcd_eucl a b"
   1.102 -  by (induct a b rule: gcd_eucl_induct) (simp_all add: gcd_eucl_0 gcd_eucl_non_0)
   1.103 -     
   1.104 -lemma gcd_eucl_greatest:
   1.105 -  fixes k a b :: 'a
   1.106 -  shows "k dvd a \<Longrightarrow> k dvd b \<Longrightarrow> k dvd gcd_eucl a b"
   1.107 -proof (induct a b rule: gcd_eucl_induct)
   1.108 -  case (zero a) from zero(1) show ?case by (rule dvd_trans) (simp add: gcd_eucl_0)
   1.109 +lemma semiring_gcd:
   1.110 +  "class.semiring_gcd one zero times gcd lcm
   1.111 +    divide plus minus normalize unit_factor"
   1.112 +proof
   1.113 +  show "gcd a b dvd a"
   1.114 +    and "gcd a b dvd b" for a b
   1.115 +    by (induct a b rule: eucl_induct)
   1.116 +      (simp_all add: local.gcd_0 local.gcd_mod dvd_mod_iff)
   1.117  next
   1.118 -  case (mod a b)
   1.119 -  then show ?case
   1.120 -    by (simp add: gcd_eucl_non_0 dvd_mod_iff)
   1.121 -qed
   1.122 -
   1.123 -lemma gcd_euclI:
   1.124 -  fixes gcd :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"
   1.125 -  assumes "d dvd a" "d dvd b" "normalize d = d"
   1.126 -          "\<And>k. k dvd a \<Longrightarrow> k dvd b \<Longrightarrow> k dvd d"
   1.127 -  shows   "gcd_eucl a b = d"
   1.128 -  by (rule associated_eqI) (simp_all add: gcd_eucl_greatest assms)
   1.129 -
   1.130 -lemma eq_gcd_euclI:
   1.131 -  fixes gcd :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"
   1.132 -  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"
   1.133 -          "\<And>a b k. k dvd a \<Longrightarrow> k dvd b \<Longrightarrow> k dvd gcd a b"
   1.134 -  shows   "gcd = gcd_eucl"
   1.135 -  by (intro ext, rule associated_eqI) (simp_all add: gcd_eucl_greatest assms)
   1.136 -
   1.137 -lemma gcd_eucl_zero [simp]:
   1.138 -  "gcd_eucl a b = 0 \<longleftrightarrow> a = 0 \<and> b = 0"
   1.139 -  by (metis dvd_0_left dvd_refl gcd_eucl_dvd1 gcd_eucl_dvd2 gcd_eucl_greatest)+
   1.140 -
   1.141 -  
   1.142 -lemma dvd_Lcm_eucl [simp]: "a \<in> A \<Longrightarrow> a dvd Lcm_eucl A"
   1.143 -  and Lcm_eucl_least: "(\<And>a. a \<in> A \<Longrightarrow> a dvd b) \<Longrightarrow> Lcm_eucl A dvd b"
   1.144 -  and unit_factor_Lcm_eucl [simp]: 
   1.145 -          "unit_factor (Lcm_eucl A) = (if Lcm_eucl A = 0 then 0 else 1)"
   1.146 -proof -
   1.147 -  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>
   1.148 -    unit_factor (Lcm_eucl A) = (if Lcm_eucl A = 0 then 0 else 1)" (is ?thesis)
   1.149 -  proof (cases "\<exists>l. l \<noteq>  0 \<and> (\<forall>a\<in>A. a dvd l)")
   1.150 -    case False
   1.151 -    hence "Lcm_eucl A = 0" by (auto simp: Lcm_eucl_def)
   1.152 -    with False show ?thesis by auto
   1.153 +  show "c dvd a \<Longrightarrow> c dvd b \<Longrightarrow> c dvd gcd a b" for a b c
   1.154 +  proof (induct a b rule: eucl_induct)
   1.155 +    case (zero a) from \<open>c dvd a\<close> show ?case
   1.156 +      by (rule dvd_trans) (simp add: local.gcd_0)
   1.157    next
   1.158 -    case True
   1.159 -    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
   1.160 -    define n where "n = (LEAST n. \<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n)"
   1.161 -    define l where "l = (SOME l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n)"
   1.162 -    have "\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n"
   1.163 -      apply (subst n_def)
   1.164 -      apply (rule LeastI[of _ "euclidean_size l\<^sub>0"])
   1.165 -      apply (rule exI[of _ l\<^sub>0])
   1.166 -      apply (simp add: l\<^sub>0_props)
   1.167 -      done
   1.168 -    from someI_ex[OF this] have "l \<noteq> 0" and "\<forall>a\<in>A. a dvd l" and "euclidean_size l = n" 
   1.169 -      unfolding l_def by simp_all
   1.170 -    {
   1.171 -      fix l' assume "\<forall>a\<in>A. a dvd l'"
   1.172 -      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)
   1.173 -      moreover from \<open>l \<noteq> 0\<close> have "gcd_eucl l l' \<noteq> 0" by simp
   1.174 -      ultimately have "\<exists>b. b \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd b) \<and> 
   1.175 -                          euclidean_size b = euclidean_size (gcd_eucl l l')"
   1.176 -        by (intro exI[of _ "gcd_eucl l l'"], auto)
   1.177 -      hence "euclidean_size (gcd_eucl l l') \<ge> n" by (subst n_def) (rule Least_le)
   1.178 -      moreover have "euclidean_size (gcd_eucl l l') \<le> n"
   1.179 -      proof -
   1.180 -        have "gcd_eucl l l' dvd l" by simp
   1.181 -        then obtain a where "l = gcd_eucl l l' * a" unfolding dvd_def by blast
   1.182 -        with \<open>l \<noteq> 0\<close> have "a \<noteq> 0" by auto
   1.183 -        hence "euclidean_size (gcd_eucl l l') \<le> euclidean_size (gcd_eucl l l' * a)"
   1.184 -          by (rule size_mult_mono)
   1.185 -        also have "gcd_eucl l l' * a = l" using \<open>l = gcd_eucl l l' * a\<close> ..
   1.186 -        also note \<open>euclidean_size l = n\<close>
   1.187 -        finally show "euclidean_size (gcd_eucl l l') \<le> n" .
   1.188 -      qed
   1.189 -      ultimately have *: "euclidean_size l = euclidean_size (gcd_eucl l l')" 
   1.190 -        by (intro le_antisym, simp_all add: \<open>euclidean_size l = n\<close>)
   1.191 -      from \<open>l \<noteq> 0\<close> have "l dvd gcd_eucl l l'"
   1.192 -        by (rule dvd_euclidean_size_eq_imp_dvd) (auto simp add: *)
   1.193 -      hence "l dvd l'" by (rule dvd_trans[OF _ gcd_eucl_dvd2])
   1.194 -    }
   1.195 -
   1.196 -    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>
   1.197 -      have "(\<forall>a\<in>A. a dvd normalize l) \<and> 
   1.198 -        (\<forall>l'. (\<forall>a\<in>A. a dvd l') \<longrightarrow> normalize l dvd l') \<and>
   1.199 -        unit_factor (normalize l) = 
   1.200 -        (if normalize l = 0 then 0 else 1)"
   1.201 -      by (auto simp: unit_simps)
   1.202 -    also from True have "normalize l = Lcm_eucl A"
   1.203 -      by (simp add: Lcm_eucl_def Let_def n_def l_def)
   1.204 -    finally show ?thesis .
   1.205 +    case (mod a b)
   1.206 +    then show ?case
   1.207 +      by (simp add: local.gcd_mod dvd_mod_iff)
   1.208    qed
   1.209 -  note A = this
   1.210 -
   1.211 -  {fix a assume "a \<in> A" then show "a dvd Lcm_eucl A" using A by blast}
   1.212 -  {fix b assume "\<And>a. a \<in> A \<Longrightarrow> a dvd b" then show "Lcm_eucl A dvd b" using A by blast}
   1.213 -  from A show "unit_factor (Lcm_eucl A) = (if Lcm_eucl A = 0 then 0 else 1)" by blast
   1.214 -qed
   1.215 -
   1.216 -lemma normalize_Lcm_eucl [simp]:
   1.217 -  "normalize (Lcm_eucl A) = Lcm_eucl A"
   1.218 -proof (cases "Lcm_eucl A = 0")
   1.219 -  case True then show ?thesis by simp
   1.220  next
   1.221 -  case False
   1.222 -  have "unit_factor (Lcm_eucl A) * normalize (Lcm_eucl A) = Lcm_eucl A"
   1.223 -    by (fact unit_factor_mult_normalize)
   1.224 -  with False show ?thesis by simp
   1.225 -qed
   1.226 -
   1.227 -lemma eq_Lcm_euclI:
   1.228 -  fixes lcm :: "'a set \<Rightarrow> 'a"
   1.229 -  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"
   1.230 -          "\<And>A. normalize (lcm A) = lcm A" shows "lcm = Lcm_eucl"
   1.231 -  by (intro ext, rule associated_eqI) (auto simp: assms intro: Lcm_eucl_least)  
   1.232 -
   1.233 -lemma Gcd_eucl_dvd: "a \<in> A \<Longrightarrow> Gcd_eucl A dvd a"
   1.234 -  unfolding Gcd_eucl_def by (auto intro: Lcm_eucl_least)
   1.235 -
   1.236 -lemma Gcd_eucl_greatest: "(\<And>x. x \<in> A \<Longrightarrow> d dvd x) \<Longrightarrow> d dvd Gcd_eucl A"
   1.237 -  unfolding Gcd_eucl_def by auto
   1.238 -
   1.239 -lemma normalize_Gcd_eucl [simp]: "normalize (Gcd_eucl A) = Gcd_eucl A"
   1.240 -  by (simp add: Gcd_eucl_def)
   1.241 -
   1.242 -lemma Lcm_euclI:
   1.243 -  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"
   1.244 -  shows   "Lcm_eucl A = d"
   1.245 -proof -
   1.246 -  have "normalize (Lcm_eucl A) = normalize d"
   1.247 -    by (intro associatedI) (auto intro: dvd_Lcm_eucl Lcm_eucl_least assms)
   1.248 -  thus ?thesis by (simp add: assms)
   1.249 +  show "normalize (gcd a b) = gcd a b" for a b
   1.250 +    by (induct a b rule: eucl_induct)
   1.251 +      (simp_all add: local.gcd_0 local.gcd_mod)
   1.252 +next
   1.253 +  show "lcm a b = normalize (a * b) div gcd a b" for a b
   1.254 +    by (fact local.lcm_def)
   1.255  qed
   1.256  
   1.257 -lemma Gcd_euclI:
   1.258 -  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"
   1.259 -  shows   "Gcd_eucl A = d"
   1.260 -proof -
   1.261 -  have "normalize (Gcd_eucl A) = normalize d"
   1.262 -    by (intro associatedI) (auto intro: Gcd_eucl_dvd Gcd_eucl_greatest assms)
   1.263 -  thus ?thesis by (simp add: assms)
   1.264 -qed
   1.265 +interpretation semiring_gcd one zero times gcd lcm
   1.266 +  divide plus minus normalize unit_factor
   1.267 +  by (fact semiring_gcd)
   1.268    
   1.269 -lemmas lcm_gcd_eucl_facts = 
   1.270 -  gcd_eucl_dvd1 gcd_eucl_dvd2 gcd_eucl_greatest normalize_gcd_eucl lcm_eucl_def
   1.271 -  Gcd_eucl_def Gcd_eucl_dvd Gcd_eucl_greatest normalize_Gcd_eucl
   1.272 -  dvd_Lcm_eucl Lcm_eucl_least normalize_Lcm_eucl
   1.273 -
   1.274 -lemma normalized_factors_product:
   1.275 -  "{p. p dvd a * b \<and> normalize p = p} = 
   1.276 -     (\<lambda>(x,y). x * y) ` ({p. p dvd a \<and> normalize p = p} \<times> {p. p dvd b \<and> normalize p = p})"
   1.277 -proof safe
   1.278 -  fix p assume p: "p dvd a * b" "normalize p = p"
   1.279 -  interpret semiring_gcd 1 0 "op *" gcd_eucl lcm_eucl "op div" "op +" "op -" normalize unit_factor
   1.280 -    by standard (rule lcm_gcd_eucl_facts; assumption)+
   1.281 -  from dvd_productE[OF p(1)] guess x y . note xy = this
   1.282 -  define x' y' where "x' = normalize x" and "y' = normalize y"
   1.283 -  have "p = x' * y'"
   1.284 -    by (subst p(2) [symmetric]) (simp add: xy x'_def y'_def normalize_mult)
   1.285 -  moreover from xy have "normalize x' = x'" "normalize y' = y'" "x' dvd a" "y' dvd b" 
   1.286 -    by (simp_all add: x'_def y'_def)
   1.287 -  ultimately show "p \<in> (\<lambda>(x, y). x * y) ` 
   1.288 -                     ({p. p dvd a \<and> normalize p = p} \<times> {p. p dvd b \<and> normalize p = p})"
   1.289 -    by blast
   1.290 -qed (auto simp: normalize_mult mult_dvd_mono)
   1.291 -
   1.292 -
   1.293 -subclass factorial_semiring
   1.294 -proof (standard, rule factorial_semiring_altI_aux)
   1.295 -  fix x assume "x \<noteq> 0"
   1.296 -  thus "finite {p. p dvd x \<and> normalize p = p}"
   1.297 -  proof (induction "euclidean_size x" arbitrary: x rule: less_induct)
   1.298 -    case (less x)
   1.299 -    show ?case
   1.300 -    proof (cases "\<exists>y. y dvd x \<and> \<not>x dvd y \<and> \<not>is_unit y")
   1.301 +lemma semiring_Gcd:
   1.302 +  "class.semiring_Gcd one zero times gcd lcm Gcd Lcm
   1.303 +    divide plus minus normalize unit_factor"
   1.304 +proof -
   1.305 +  show ?thesis
   1.306 +  proof
   1.307 +    have "(\<forall>a\<in>A. a dvd Lcm A) \<and> (\<forall>b. (\<forall>a\<in>A. a dvd b) \<longrightarrow> Lcm A dvd b)" for A
   1.308 +    proof (cases "\<exists>l. l \<noteq>  0 \<and> (\<forall>a\<in>A. a dvd l)")
   1.309        case False
   1.310 -      have "{p. p dvd x \<and> normalize p = p} \<subseteq> {1, normalize x}"
   1.311 -      proof
   1.312 -        fix p assume p: "p \<in> {p. p dvd x \<and> normalize p = p}"
   1.313 -        with False have "is_unit p \<or> x dvd p" by blast
   1.314 -        thus "p \<in> {1, normalize x}"
   1.315 -        proof (elim disjE)
   1.316 -          assume "is_unit p"
   1.317 -          hence "normalize p = 1" by (simp add: is_unit_normalize)
   1.318 -          with p show ?thesis by simp
   1.319 -        next
   1.320 -          assume "x dvd p"
   1.321 -          with p have "normalize p = normalize x" by (intro associatedI) simp_all
   1.322 -          with p show ?thesis by simp
   1.323 -        qed
   1.324 -      qed
   1.325 -      moreover have "finite \<dots>" by simp
   1.326 -      ultimately show ?thesis by (rule finite_subset)
   1.327 -      
   1.328 +      then have "Lcm A = 0"
   1.329 +        by (auto simp add: local.Lcm_def)
   1.330 +      with False show ?thesis
   1.331 +        by auto
   1.332      next
   1.333        case True
   1.334 -      then obtain y where y: "y dvd x" "\<not>x dvd y" "\<not>is_unit y" by blast
   1.335 -      define z where "z = x div y"
   1.336 -      let ?fctrs = "\<lambda>x. {p. p dvd x \<and> normalize p = p}"
   1.337 -      from y have x: "x = y * z" by (simp add: z_def)
   1.338 -      with less.prems have "y \<noteq> 0" "z \<noteq> 0" by auto
   1.339 -      from x y have "\<not>is_unit z" by (auto simp: mult_unit_dvd_iff)
   1.340 -      have "?fctrs x = (\<lambda>(p,p'). p * p') ` (?fctrs y \<times> ?fctrs z)"
   1.341 -        by (subst x) (rule normalized_factors_product)
   1.342 -      also have "\<not>y * z dvd y * 1" "\<not>y * z dvd 1 * z"
   1.343 -        by (subst dvd_times_left_cancel_iff dvd_times_right_cancel_iff; fact)+
   1.344 -      hence "finite ((\<lambda>(p,p'). p * p') ` (?fctrs y \<times> ?fctrs z))"
   1.345 -        by (intro finite_imageI finite_cartesian_product less dvd_proper_imp_size_less)
   1.346 -           (auto simp: x)
   1.347 +      then obtain l\<^sub>0 where l\<^sub>0_props: "l\<^sub>0 \<noteq> 0" "\<forall>a\<in>A. a dvd l\<^sub>0" by blast
   1.348 +      define n where "n = (LEAST n. \<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n)"
   1.349 +      define l where "l = (SOME l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n)"
   1.350 +      have "\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n"
   1.351 +        apply (subst n_def)
   1.352 +        apply (rule LeastI [of _ "euclidean_size l\<^sub>0"])
   1.353 +        apply (rule exI [of _ l\<^sub>0])
   1.354 +        apply (simp add: l\<^sub>0_props)
   1.355 +        done
   1.356 +      from someI_ex [OF this] have "l \<noteq> 0" and "\<forall>a\<in>A. a dvd l"
   1.357 +        and "euclidean_size l = n" 
   1.358 +        unfolding l_def by simp_all
   1.359 +      {
   1.360 +        fix l' assume "\<forall>a\<in>A. a dvd l'"
   1.361 +        with \<open>\<forall>a\<in>A. a dvd l\<close> have "\<forall>a\<in>A. a dvd gcd l l'"
   1.362 +          by (auto intro: gcd_greatest)
   1.363 +        moreover from \<open>l \<noteq> 0\<close> have "gcd l l' \<noteq> 0"
   1.364 +          by simp
   1.365 +        ultimately have "\<exists>b. b \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd b) \<and> 
   1.366 +          euclidean_size b = euclidean_size (gcd l l')"
   1.367 +          by (intro exI [of _ "gcd l l'"], auto)
   1.368 +        then have "euclidean_size (gcd l l') \<ge> n"
   1.369 +          by (subst n_def) (rule Least_le)
   1.370 +        moreover have "euclidean_size (gcd l l') \<le> n"
   1.371 +        proof -
   1.372 +          have "gcd l l' dvd l"
   1.373 +            by simp
   1.374 +          then obtain a where "l = gcd l l' * a" ..
   1.375 +          with \<open>l \<noteq> 0\<close> have "a \<noteq> 0"
   1.376 +            by auto
   1.377 +          hence "euclidean_size (gcd l l') \<le> euclidean_size (gcd l l' * a)"
   1.378 +            by (rule size_mult_mono)
   1.379 +          also have "gcd l l' * a = l" using \<open>l = gcd l l' * a\<close> ..
   1.380 +          also note \<open>euclidean_size l = n\<close>
   1.381 +          finally show "euclidean_size (gcd l l') \<le> n" .
   1.382 +        qed
   1.383 +        ultimately have *: "euclidean_size l = euclidean_size (gcd l l')" 
   1.384 +          by (intro le_antisym, simp_all add: \<open>euclidean_size l = n\<close>)
   1.385 +        from \<open>l \<noteq> 0\<close> have "l dvd gcd l l'"
   1.386 +          by (rule dvd_euclidean_size_eq_imp_dvd) (auto simp add: *)
   1.387 +        hence "l dvd l'" by (rule dvd_trans [OF _ gcd_dvd2])
   1.388 +      }
   1.389 +      with \<open>\<forall>a\<in>A. a dvd l\<close> and \<open>l \<noteq> 0\<close>
   1.390 +        have "(\<forall>a\<in>A. a dvd normalize l) \<and> 
   1.391 +          (\<forall>l'. (\<forall>a\<in>A. a dvd l') \<longrightarrow> normalize l dvd l')"
   1.392 +        by auto
   1.393 +      also from True have "normalize l = Lcm A"
   1.394 +        by (simp add: local.Lcm_def Let_def n_def l_def)
   1.395        finally show ?thesis .
   1.396      qed
   1.397 -  qed
   1.398 -next
   1.399 -  interpret semiring_gcd 1 0 "op *" gcd_eucl lcm_eucl "op div" "op +" "op -" normalize unit_factor
   1.400 -    by standard (rule lcm_gcd_eucl_facts; assumption)+
   1.401 -  fix p assume p: "irreducible p"
   1.402 -  thus "prime_elem p" by (rule irreducible_imp_prime_elem_gcd)
   1.403 -qed
   1.404 -
   1.405 -lemma gcd_eucl_eq_gcd_factorial: "gcd_eucl = gcd_factorial"
   1.406 -  by (intro ext gcd_euclI gcd_lcm_factorial)
   1.407 -
   1.408 -lemma lcm_eucl_eq_lcm_factorial: "lcm_eucl = lcm_factorial"
   1.409 -  by (intro ext) (simp add: lcm_eucl_def lcm_factorial_gcd_factorial gcd_eucl_eq_gcd_factorial)
   1.410 -
   1.411 -lemma Gcd_eucl_eq_Gcd_factorial: "Gcd_eucl = Gcd_factorial"
   1.412 -  by (intro ext Gcd_euclI gcd_lcm_factorial)
   1.413 -
   1.414 -lemma Lcm_eucl_eq_Lcm_factorial: "Lcm_eucl = Lcm_factorial"
   1.415 -  by (intro ext Lcm_euclI gcd_lcm_factorial)
   1.416 -
   1.417 -lemmas eucl_eq_factorial = 
   1.418 -  gcd_eucl_eq_gcd_factorial lcm_eucl_eq_lcm_factorial 
   1.419 -  Gcd_eucl_eq_Gcd_factorial Lcm_eucl_eq_Lcm_factorial
   1.420 -  
   1.421 -end
   1.422 -
   1.423 -context euclidean_ring
   1.424 -begin
   1.425 -
   1.426 -function euclid_ext_aux :: "'a \<Rightarrow> _" where
   1.427 -  "euclid_ext_aux r' r s' s t' t = (
   1.428 -     if r = 0 then let c = 1 div unit_factor r' in (s' * c, t' * c, normalize r')
   1.429 -     else let q = r' div r
   1.430 -          in  euclid_ext_aux r (r' mod r) s (s' - q * s) t (t' - q * t))"
   1.431 -by auto
   1.432 -termination by (relation "measure (\<lambda>(_,b,_,_,_,_). euclidean_size b)") (simp_all add: mod_size_less)
   1.433 -
   1.434 -declare euclid_ext_aux.simps [simp del]
   1.435 -
   1.436 -lemma euclid_ext_aux_correct:
   1.437 -  assumes "gcd_eucl r' r = gcd_eucl a b"
   1.438 -  assumes "s' * a + t' * b = r'"
   1.439 -  assumes "s * a + t * b = r"
   1.440 -  shows   "case euclid_ext_aux r' r s' s t' t of (x,y,c) \<Rightarrow>
   1.441 -             x * a + y * b = c \<and> c = gcd_eucl a b" (is "?P (euclid_ext_aux r' r s' s t' t)")
   1.442 -using assms
   1.443 -proof (induction r' r s' s t' t rule: euclid_ext_aux.induct)
   1.444 -  case (1 r' r s' s t' t)
   1.445 -  show ?case
   1.446 -  proof (cases "r = 0")
   1.447 -    case True
   1.448 -    hence "euclid_ext_aux r' r s' s t' t = 
   1.449 -             (s' div unit_factor r', t' div unit_factor r', normalize r')"
   1.450 -      by (subst euclid_ext_aux.simps) (simp add: Let_def)
   1.451 -    also have "?P \<dots>"
   1.452 -    proof safe
   1.453 -      have "s' div unit_factor r' * a + t' div unit_factor r' * b = 
   1.454 -                (s' * a + t' * b) div unit_factor r'"
   1.455 -        by (cases "r' = 0") (simp_all add: unit_div_commute)
   1.456 -      also have "s' * a + t' * b = r'" by fact
   1.457 -      also have "\<dots> div unit_factor r' = normalize r'" by simp
   1.458 -      finally show "s' div unit_factor r' * a + t' div unit_factor r' * b = normalize r'" .
   1.459 -    next
   1.460 -      from "1.prems" True show "normalize r' = gcd_eucl a b" by (simp add: gcd_eucl_0)
   1.461 -    qed
   1.462 -    finally show ?thesis .
   1.463 -  next
   1.464 -    case False
   1.465 -    hence "euclid_ext_aux r' r s' s t' t = 
   1.466 -             euclid_ext_aux r (r' mod r) s (s' - r' div r * s) t (t' - r' div r * t)"
   1.467 -      by (subst euclid_ext_aux.simps) (simp add: Let_def)
   1.468 -    also from "1.prems" False have "?P \<dots>"
   1.469 -    proof (intro "1.IH")
   1.470 -      have "(s' - r' div r * s) * a + (t' - r' div r * t) * b =
   1.471 -              (s' * a + t' * b) - r' div r * (s * a + t * b)" by (simp add: algebra_simps)
   1.472 -      also have "s' * a + t' * b = r'" by fact
   1.473 -      also have "s * a + t * b = r" by fact
   1.474 -      also have "r' - r' div r * r = r' mod r" using div_mult_mod_eq [of r' r]
   1.475 -        by (simp add: algebra_simps)
   1.476 -      finally show "(s' - r' div r * s) * a + (t' - r' div r * t) * b = r' mod r" .
   1.477 -    qed (auto simp: gcd_eucl_non_0 algebra_simps minus_mod_eq_div_mult [symmetric])
   1.478 -    finally show ?thesis .
   1.479 +    then show dvd_Lcm: "a \<in> A \<Longrightarrow> a dvd Lcm A"
   1.480 +      and Lcm_least: "(\<And>a. a \<in> A \<Longrightarrow> a dvd b) \<Longrightarrow> Lcm A dvd b" for A and a b
   1.481 +      by auto
   1.482 +    show "a \<in> A \<Longrightarrow> Gcd A dvd a" for A and a
   1.483 +      by (auto simp add: local.Gcd_def intro: Lcm_least)
   1.484 +    show "(\<And>a. a \<in> A \<Longrightarrow> b dvd a) \<Longrightarrow> b dvd Gcd A" for A and b
   1.485 +      by (auto simp add: local.Gcd_def intro: dvd_Lcm)
   1.486 +    show [simp]: "normalize (Lcm A) = Lcm A" for A
   1.487 +      by (simp add: local.Lcm_def)
   1.488 +    show "normalize (Gcd A) = Gcd A" for A
   1.489 +      by (simp add: local.Gcd_def)
   1.490    qed
   1.491  qed
   1.492  
   1.493 -definition euclid_ext where
   1.494 -  "euclid_ext a b = euclid_ext_aux a b 1 0 0 1"
   1.495 -
   1.496 -lemma euclid_ext_0: 
   1.497 -  "euclid_ext a 0 = (1 div unit_factor a, 0, normalize a)"
   1.498 -  by (simp add: euclid_ext_def euclid_ext_aux.simps)
   1.499 -
   1.500 -lemma euclid_ext_left_0: 
   1.501 -  "euclid_ext 0 a = (0, 1 div unit_factor a, normalize a)"
   1.502 -  by (simp add: euclid_ext_def euclid_ext_aux.simps)
   1.503 -
   1.504 -lemma euclid_ext_correct':
   1.505 -  "case euclid_ext a b of (x,y,c) \<Rightarrow> x * a + y * b = c \<and> c = gcd_eucl a b"
   1.506 -  unfolding euclid_ext_def by (rule euclid_ext_aux_correct) simp_all
   1.507 +interpretation semiring_Gcd one zero times gcd lcm Gcd Lcm
   1.508 +    divide plus minus normalize unit_factor
   1.509 +  by (fact semiring_Gcd)
   1.510  
   1.511 -lemma euclid_ext_gcd_eucl:
   1.512 -  "(case euclid_ext a b of (x,y,c) \<Rightarrow> c) = gcd_eucl a b"
   1.513 -  using euclid_ext_correct'[of a b] by (simp add: case_prod_unfold)
   1.514 -
   1.515 -definition euclid_ext' where
   1.516 -  "euclid_ext' a b = (case euclid_ext a b of (x, y, _) \<Rightarrow> (x, y))"
   1.517 +subclass factorial_semiring
   1.518 +proof -
   1.519 +  show "class.factorial_semiring divide plus minus zero times one
   1.520 +     normalize unit_factor"
   1.521 +  proof (standard, rule factorial_semiring_altI_aux) -- \<open>FIXME rule\<close>
   1.522 +    fix x assume "x \<noteq> 0"
   1.523 +    thus "finite {p. p dvd x \<and> normalize p = p}"
   1.524 +    proof (induction "euclidean_size x" arbitrary: x rule: less_induct)
   1.525 +      case (less x)
   1.526 +      show ?case
   1.527 +      proof (cases "\<exists>y. y dvd x \<and> \<not>x dvd y \<and> \<not>is_unit y")
   1.528 +        case False
   1.529 +        have "{p. p dvd x \<and> normalize p = p} \<subseteq> {1, normalize x}"
   1.530 +        proof
   1.531 +          fix p assume p: "p \<in> {p. p dvd x \<and> normalize p = p}"
   1.532 +          with False have "is_unit p \<or> x dvd p" by blast
   1.533 +          thus "p \<in> {1, normalize x}"
   1.534 +          proof (elim disjE)
   1.535 +            assume "is_unit p"
   1.536 +            hence "normalize p = 1" by (simp add: is_unit_normalize)
   1.537 +            with p show ?thesis by simp
   1.538 +          next
   1.539 +            assume "x dvd p"
   1.540 +            with p have "normalize p = normalize x" by (intro associatedI) simp_all
   1.541 +            with p show ?thesis by simp
   1.542 +          qed
   1.543 +        qed
   1.544 +        moreover have "finite \<dots>" by simp
   1.545 +        ultimately show ?thesis by (rule finite_subset)
   1.546 +      next
   1.547 +        case True
   1.548 +        then obtain y where y: "y dvd x" "\<not>x dvd y" "\<not>is_unit y" by blast
   1.549 +        define z where "z = x div y"
   1.550 +        let ?fctrs = "\<lambda>x. {p. p dvd x \<and> normalize p = p}"
   1.551 +        from y have x: "x = y * z" by (simp add: z_def)
   1.552 +        with less.prems have "y \<noteq> 0" "z \<noteq> 0" by auto
   1.553 +        have normalized_factors_product:
   1.554 +          "{p. p dvd a * b \<and> normalize p = p} = 
   1.555 +             (\<lambda>(x,y). x * y) ` ({p. p dvd a \<and> normalize p = p} \<times> {p. p dvd b \<and> normalize p = p})" for a b
   1.556 +        proof safe
   1.557 +          fix p assume p: "p dvd a * b" "normalize p = p"
   1.558 +          from dvd_productE[OF p(1)] guess x y . note xy = this
   1.559 +          define x' y' where "x' = normalize x" and "y' = normalize y"
   1.560 +          have "p = x' * y'"
   1.561 +            by (subst p(2) [symmetric]) (simp add: xy x'_def y'_def normalize_mult)
   1.562 +          moreover from xy have "normalize x' = x'" "normalize y' = y'" "x' dvd a" "y' dvd b" 
   1.563 +            by (simp_all add: x'_def y'_def)
   1.564 +          ultimately show "p \<in> (\<lambda>(x, y). x * y) ` 
   1.565 +            ({p. p dvd a \<and> normalize p = p} \<times> {p. p dvd b \<and> normalize p = p})"
   1.566 +            by blast
   1.567 +        qed (auto simp: normalize_mult mult_dvd_mono)
   1.568 +        from x y have "\<not>is_unit z" by (auto simp: mult_unit_dvd_iff)
   1.569 +        have "?fctrs x = (\<lambda>(p,p'). p * p') ` (?fctrs y \<times> ?fctrs z)"
   1.570 +          by (subst x) (rule normalized_factors_product)
   1.571 +        also have "\<not>y * z dvd y * 1" "\<not>y * z dvd 1 * z"
   1.572 +          by (subst dvd_times_left_cancel_iff dvd_times_right_cancel_iff; fact)+
   1.573 +        hence "finite ((\<lambda>(p,p'). p * p') ` (?fctrs y \<times> ?fctrs z))"
   1.574 +          by (intro finite_imageI finite_cartesian_product less dvd_proper_imp_size_less)
   1.575 +             (auto simp: x)
   1.576 +        finally show ?thesis .
   1.577 +      qed
   1.578 +    qed
   1.579 +  next
   1.580 +    fix p
   1.581 +    assume "irreducible p"
   1.582 +    then show "prime_elem p"
   1.583 +      by (rule irreducible_imp_prime_elem_gcd)
   1.584 +  qed
   1.585 +qed
   1.586  
   1.587 -lemma euclid_ext'_correct':
   1.588 -  "case euclid_ext' a b of (x,y) \<Rightarrow> x * a + y * b = gcd_eucl a b"
   1.589 -  using euclid_ext_correct'[of a b] by (simp add: case_prod_unfold euclid_ext'_def)
   1.590 +lemma Gcd_eucl_set [code]:
   1.591 +  "Gcd (set xs) = foldl gcd 0 xs"
   1.592 +  by (fact local.Gcd_set)
   1.593  
   1.594 -lemma euclid_ext'_0: "euclid_ext' a 0 = (1 div unit_factor a, 0)" 
   1.595 -  by (simp add: euclid_ext'_def euclid_ext_0)
   1.596 -
   1.597 -lemma euclid_ext'_left_0: "euclid_ext' 0 a = (0, 1 div unit_factor a)" 
   1.598 -  by (simp add: euclid_ext'_def euclid_ext_left_0)
   1.599 -
   1.600 +lemma Lcm_eucl_set [code]:
   1.601 +  "Lcm (set xs) = foldl lcm 1 xs"
   1.602 +  by (fact local.Lcm_set)
   1.603 + 
   1.604  end
   1.605  
   1.606 +hide_const (open) gcd lcm Gcd Lcm
   1.607 +
   1.608 +lemma prime_elem_int_abs_iff [simp]:
   1.609 +  fixes p :: int
   1.610 +  shows "prime_elem \<bar>p\<bar> \<longleftrightarrow> prime_elem p"
   1.611 +  using prime_elem_normalize_iff [of p] by simp
   1.612 +  
   1.613 +lemma prime_elem_int_minus_iff [simp]:
   1.614 +  fixes p :: int
   1.615 +  shows "prime_elem (- p) \<longleftrightarrow> prime_elem p"
   1.616 +  using prime_elem_normalize_iff [of "- p"] by simp
   1.617 +
   1.618 +lemma prime_int_iff:
   1.619 +  fixes p :: int
   1.620 +  shows "prime p \<longleftrightarrow> p > 0 \<and> prime_elem p"
   1.621 +  by (auto simp add: prime_def dest: prime_elem_not_zeroI)
   1.622 +  
   1.623 +  
   1.624 +subsection \<open>The (simple) euclidean algorithm as gcd computation\<close>
   1.625 +  
   1.626  class euclidean_semiring_gcd = euclidean_semiring + gcd + Gcd +
   1.627 -  assumes gcd_gcd_eucl: "gcd = gcd_eucl" and lcm_lcm_eucl: "lcm = lcm_eucl"
   1.628 -  assumes Gcd_Gcd_eucl: "Gcd = Gcd_eucl" and Lcm_Lcm_eucl: "Lcm = Lcm_eucl"
   1.629 +  assumes gcd_eucl: "Euclidean_Algorithm.gcd = GCD.gcd"
   1.630 +    and lcm_eucl: "Euclidean_Algorithm.lcm = GCD.lcm"
   1.631 +  assumes Gcd_eucl: "Euclidean_Algorithm.Gcd = GCD.Gcd"
   1.632 +    and Lcm_eucl: "Euclidean_Algorithm.Lcm = GCD.Lcm"
   1.633  begin
   1.634  
   1.635  subclass semiring_gcd
   1.636 -  by standard (simp_all add: gcd_gcd_eucl gcd_eucl_greatest lcm_lcm_eucl lcm_eucl_def)
   1.637 +  unfolding gcd_eucl [symmetric] lcm_eucl [symmetric]
   1.638 +  by (fact semiring_gcd)
   1.639  
   1.640  subclass semiring_Gcd
   1.641 -  by standard (auto simp: Gcd_Gcd_eucl Lcm_Lcm_eucl Gcd_eucl_def intro: Lcm_eucl_least)
   1.642 +  unfolding  gcd_eucl [symmetric] lcm_eucl [symmetric]
   1.643 +    Gcd_eucl [symmetric] Lcm_eucl [symmetric]
   1.644 +  by (fact semiring_Gcd)
   1.645  
   1.646  subclass factorial_semiring_gcd
   1.647  proof
   1.648 -  fix a b
   1.649 -  show "gcd a b = gcd_factorial a b"
   1.650 -    by (rule sym, rule gcdI) (rule gcd_lcm_factorial; assumption)+
   1.651 -  thus "lcm a b = lcm_factorial a b"
   1.652 +  show "gcd a b = gcd_factorial a b" for a b
   1.653 +    apply (rule sym)
   1.654 +    apply (rule gcdI)
   1.655 +       apply (fact gcd_lcm_factorial)+
   1.656 +    done
   1.657 +  then show "lcm a b = lcm_factorial a b" for a b
   1.658      by (simp add: lcm_factorial_gcd_factorial lcm_gcd)
   1.659 -next
   1.660 -  fix A 
   1.661 -  show "Gcd A = Gcd_factorial A"
   1.662 -    by (rule sym, rule GcdI) (rule gcd_lcm_factorial; assumption)+
   1.663 -  show "Lcm A = Lcm_factorial A"
   1.664 -    by (rule sym, rule LcmI) (rule gcd_lcm_factorial; assumption)+
   1.665 +  show "Gcd A = Gcd_factorial A" for A
   1.666 +    apply (rule sym)
   1.667 +    apply (rule GcdI)
   1.668 +       apply (fact gcd_lcm_factorial)+
   1.669 +    done
   1.670 +  show "Lcm A = Lcm_factorial A" for A
   1.671 +    apply (rule sym)
   1.672 +    apply (rule LcmI)
   1.673 +       apply (fact gcd_lcm_factorial)+
   1.674 +    done
   1.675  qed
   1.676  
   1.677 -lemma gcd_non_0:
   1.678 -  "b \<noteq> 0 \<Longrightarrow> gcd a b = gcd b (a mod b)"
   1.679 -  unfolding gcd_gcd_eucl by (fact gcd_eucl_non_0)
   1.680 -
   1.681 -lemmas gcd_0 = gcd_0_right
   1.682 -lemmas dvd_gcd_iff = gcd_greatest_iff
   1.683 -lemmas gcd_greatest_iff = dvd_gcd_iff
   1.684 +lemma gcd_mod_right [simp]:
   1.685 +  "a \<noteq> 0 \<Longrightarrow> gcd a (b mod a) = gcd a b"
   1.686 +  unfolding gcd.commute [of a b]
   1.687 +  by (simp add: gcd_eucl [symmetric] local.gcd_mod)
   1.688  
   1.689 -lemma gcd_mod1 [simp]:
   1.690 -  "gcd (a mod b) b = gcd a b"
   1.691 -  by (rule gcdI, metis dvd_mod_iff gcd_dvd1 gcd_dvd2, simp_all add: gcd_greatest dvd_mod_iff)
   1.692 +lemma gcd_mod_left [simp]:
   1.693 +  "b \<noteq> 0 \<Longrightarrow> gcd (a mod b) b = gcd a b"
   1.694 +  by (drule gcd_mod_right [of _ a]) (simp add: gcd.commute)
   1.695  
   1.696 -lemma gcd_mod2 [simp]:
   1.697 -  "gcd a (b mod a) = gcd a b"
   1.698 -  by (rule gcdI, simp, metis dvd_mod_iff gcd_dvd1 gcd_dvd2, simp_all add: gcd_greatest dvd_mod_iff)
   1.699 -         
   1.700  lemma euclidean_size_gcd_le1 [simp]:
   1.701    assumes "a \<noteq> 0"
   1.702    shows "euclidean_size (gcd a b) \<le> euclidean_size a"
   1.703  proof -
   1.704 -   have "gcd a b dvd a" by (rule gcd_dvd1)
   1.705 -   then obtain c where A: "a = gcd a b * c" unfolding dvd_def by blast
   1.706 -   with \<open>a \<noteq> 0\<close> show ?thesis by (subst (2) A, intro size_mult_mono) auto
   1.707 +  from gcd_dvd1 obtain c where A: "a = gcd a b * c" ..
   1.708 +  with assms have "c \<noteq> 0"
   1.709 +    by auto
   1.710 +  moreover from this
   1.711 +  have "euclidean_size (gcd a b) \<le> euclidean_size (gcd a b * c)"
   1.712 +    by (rule size_mult_mono)
   1.713 +  with A show ?thesis
   1.714 +    by simp
   1.715  qed
   1.716  
   1.717  lemma euclidean_size_gcd_le2 [simp]:
   1.718 @@ -464,7 +348,7 @@
   1.719    by (subst gcd.commute, rule euclidean_size_gcd_le1)
   1.720  
   1.721  lemma euclidean_size_gcd_less1:
   1.722 -  assumes "a \<noteq> 0" and "\<not>a dvd b"
   1.723 +  assumes "a \<noteq> 0" and "\<not> a dvd b"
   1.724    shows "euclidean_size (gcd a b) < euclidean_size a"
   1.725  proof (rule ccontr)
   1.726    assume "\<not>euclidean_size (gcd a b) < euclidean_size a"
   1.727 @@ -473,11 +357,11 @@
   1.728    have "a dvd gcd a b"
   1.729      by (rule dvd_euclidean_size_eq_imp_dvd) (simp_all add: assms A)
   1.730    hence "a dvd b" using dvd_gcdD2 by blast
   1.731 -  with \<open>\<not>a dvd b\<close> show False by contradiction
   1.732 +  with \<open>\<not> a dvd b\<close> show False by contradiction
   1.733  qed
   1.734  
   1.735  lemma euclidean_size_gcd_less2:
   1.736 -  assumes "b \<noteq> 0" and "\<not>b dvd a"
   1.737 +  assumes "b \<noteq> 0" and "\<not> b dvd a"
   1.738    shows "euclidean_size (gcd a b) < euclidean_size b"
   1.739    using assms by (subst gcd.commute, rule euclidean_size_gcd_less1)
   1.740  
   1.741 @@ -496,7 +380,7 @@
   1.742    using euclidean_size_lcm_le1 [of b a] by (simp add: ac_simps)
   1.743  
   1.744  lemma euclidean_size_lcm_less1:
   1.745 -  assumes "b \<noteq> 0" and "\<not>b dvd a"
   1.746 +  assumes "b \<noteq> 0" and "\<not> b dvd a"
   1.747    shows "euclidean_size a < euclidean_size (lcm a b)"
   1.748  proof (rule ccontr)
   1.749    from assms have "a \<noteq> 0" by auto
   1.750 @@ -510,26 +394,49 @@
   1.751  qed
   1.752  
   1.753  lemma euclidean_size_lcm_less2:
   1.754 -  assumes "a \<noteq> 0" and "\<not>a dvd b"
   1.755 +  assumes "a \<noteq> 0" and "\<not> a dvd b"
   1.756    shows "euclidean_size b < euclidean_size (lcm a b)"
   1.757    using assms euclidean_size_lcm_less1 [of a b] by (simp add: ac_simps)
   1.758  
   1.759 -lemma Lcm_eucl_set [code]:
   1.760 -  "Lcm_eucl (set xs) = foldl lcm_eucl 1 xs"
   1.761 -  by (simp add: Lcm_Lcm_eucl [symmetric] lcm_lcm_eucl Lcm_set)
   1.762 -
   1.763 -lemma Gcd_eucl_set [code]:
   1.764 -  "Gcd_eucl (set xs) = foldl gcd_eucl 0 xs"
   1.765 -  by (simp add: Gcd_Gcd_eucl [symmetric] gcd_gcd_eucl Gcd_set)
   1.766 -
   1.767  end
   1.768  
   1.769 +lemma factorial_euclidean_semiring_gcdI:
   1.770 +  "OFCLASS('a::{factorial_semiring_gcd, euclidean_semiring}, euclidean_semiring_gcd_class)"
   1.771 +proof
   1.772 +  interpret semiring_Gcd 1 0 times
   1.773 +    Euclidean_Algorithm.gcd Euclidean_Algorithm.lcm
   1.774 +    Euclidean_Algorithm.Gcd Euclidean_Algorithm.Lcm
   1.775 +    divide plus minus normalize unit_factor
   1.776 +    rewrites "dvd.dvd op * = Rings.dvd"
   1.777 +    by (fact semiring_Gcd) (simp add: dvd.dvd_def dvd_def fun_eq_iff)
   1.778 +  show [simp]: "Euclidean_Algorithm.gcd = (gcd :: 'a \<Rightarrow> _)"
   1.779 +  proof (rule ext)+
   1.780 +    fix a b :: 'a
   1.781 +    show "Euclidean_Algorithm.gcd a b = gcd a b"
   1.782 +    proof (induct a b rule: eucl_induct)
   1.783 +      case zero
   1.784 +      then show ?case
   1.785 +        by simp
   1.786 +    next
   1.787 +      case (mod a b)
   1.788 +      moreover have "gcd b (a mod b) = gcd b a"
   1.789 +        using GCD.gcd_add_mult [of b "a div b" "a mod b", symmetric]
   1.790 +          by (simp add: div_mult_mod_eq)
   1.791 +      ultimately show ?case
   1.792 +        by (simp add: Euclidean_Algorithm.gcd_mod ac_simps)
   1.793 +    qed
   1.794 +  qed
   1.795 +  show [simp]: "Euclidean_Algorithm.Lcm = (Lcm :: 'a set \<Rightarrow> _)"
   1.796 +    by (auto intro!: Lcm_eqI GCD.dvd_Lcm GCD.Lcm_least)
   1.797 +  show "Euclidean_Algorithm.lcm = (lcm :: 'a \<Rightarrow> _)"
   1.798 +    by (simp add: fun_eq_iff Euclidean_Algorithm.lcm_def semiring_gcd_class.lcm_gcd)
   1.799 +  show "Euclidean_Algorithm.Gcd = (Gcd :: 'a set \<Rightarrow> _)"
   1.800 +    by (simp add: fun_eq_iff Euclidean_Algorithm.Gcd_def semiring_Gcd_class.Gcd_Lcm)
   1.801 +qed
   1.802  
   1.803 -text \<open>
   1.804 -  A Euclidean ring is a Euclidean semiring with additive inverses. It provides a 
   1.805 -  few more lemmas; in particular, Bezout's lemma holds for any Euclidean ring.
   1.806 -\<close>
   1.807  
   1.808 +subsection \<open>The extended euclidean algorithm\<close>
   1.809 +  
   1.810  class euclidean_ring_gcd = euclidean_semiring_gcd + idom
   1.811  begin
   1.812  
   1.813 @@ -537,26 +444,109 @@
   1.814  subclass ring_gcd ..
   1.815  subclass factorial_ring_gcd ..
   1.816  
   1.817 -lemma euclid_ext_gcd [simp]:
   1.818 -  "(case euclid_ext a b of (_, _ , t) \<Rightarrow> t) = gcd a b"
   1.819 -  using euclid_ext_correct'[of a b] by (simp add: case_prod_unfold Let_def gcd_gcd_eucl)
   1.820 -
   1.821 -lemma euclid_ext_gcd' [simp]:
   1.822 -  "euclid_ext a b = (r, s, t) \<Longrightarrow> t = gcd a b"
   1.823 -  by (insert euclid_ext_gcd[of a b], drule (1) subst, simp)
   1.824 +function euclid_ext_aux :: "'a \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> ('a \<times> 'a) \<times> 'a"
   1.825 +  where "euclid_ext_aux s' s t' t r' r = (
   1.826 +     if r = 0 then let c = 1 div unit_factor r' in ((s' * c, t' * c), normalize r')
   1.827 +     else let q = r' div r
   1.828 +          in euclid_ext_aux s (s' - q * s) t (t' - q * t) r (r' mod r))"
   1.829 +  by auto
   1.830 +termination
   1.831 +  by (relation "measure (\<lambda>(_, _, _, _, _, b). euclidean_size b)")
   1.832 +    (simp_all add: mod_size_less)
   1.833  
   1.834 -lemma euclid_ext_correct:
   1.835 -  "case euclid_ext a b of (x,y,c) \<Rightarrow> x * a + y * b = c \<and> c = gcd a b"
   1.836 -  using euclid_ext_correct'[of a b]
   1.837 -  by (simp add: gcd_gcd_eucl case_prod_unfold)
   1.838 -  
   1.839 -lemma euclid_ext'_correct:
   1.840 -  "fst (euclid_ext' a b) * a + snd (euclid_ext' a b) * b = gcd a b"
   1.841 -  using euclid_ext_correct'[of a b]
   1.842 -  by (simp add: gcd_gcd_eucl case_prod_unfold euclid_ext'_def)
   1.843 +abbreviation (input) euclid_ext :: "'a \<Rightarrow> 'a \<Rightarrow> ('a \<times> 'a) \<times> 'a"
   1.844 +  where "euclid_ext \<equiv> euclid_ext_aux 1 0 0 1"
   1.845 +    
   1.846 +lemma
   1.847 +  assumes "gcd r' r = gcd a b"
   1.848 +  assumes "s' * a + t' * b = r'"
   1.849 +  assumes "s * a + t * b = r"
   1.850 +  assumes "euclid_ext_aux s' s t' t r' r = ((x, y), c)"
   1.851 +  shows euclid_ext_aux_eq_gcd: "c = gcd a b"
   1.852 +    and euclid_ext_aux_bezout: "x * a + y * b = gcd a b"
   1.853 +proof -
   1.854 +  have "case euclid_ext_aux s' s t' t r' r of ((x, y), c) \<Rightarrow> 
   1.855 +    x * a + y * b = c \<and> c = gcd a b" (is "?P (euclid_ext_aux s' s t' t r' r)")
   1.856 +    using assms(1-3)
   1.857 +  proof (induction s' s t' t r' r rule: euclid_ext_aux.induct)
   1.858 +    case (1 s' s t' t r' r)
   1.859 +    show ?case
   1.860 +    proof (cases "r = 0")
   1.861 +      case True
   1.862 +      hence "euclid_ext_aux s' s t' t r' r = 
   1.863 +               ((s' div unit_factor r', t' div unit_factor r'), normalize r')"
   1.864 +        by (subst euclid_ext_aux.simps) (simp add: Let_def)
   1.865 +      also have "?P \<dots>"
   1.866 +      proof safe
   1.867 +        have "s' div unit_factor r' * a + t' div unit_factor r' * b = 
   1.868 +                (s' * a + t' * b) div unit_factor r'"
   1.869 +          by (cases "r' = 0") (simp_all add: unit_div_commute)
   1.870 +        also have "s' * a + t' * b = r'" by fact
   1.871 +        also have "\<dots> div unit_factor r' = normalize r'" by simp
   1.872 +        finally show "s' div unit_factor r' * a + t' div unit_factor r' * b = normalize r'" .
   1.873 +      next
   1.874 +        from "1.prems" True show "normalize r' = gcd a b"
   1.875 +          by simp
   1.876 +      qed
   1.877 +      finally show ?thesis .
   1.878 +    next
   1.879 +      case False
   1.880 +      hence "euclid_ext_aux s' s t' t r' r = 
   1.881 +             euclid_ext_aux s (s' - r' div r * s) t (t' - r' div r * t) r (r' mod r)"
   1.882 +        by (subst euclid_ext_aux.simps) (simp add: Let_def)
   1.883 +      also from "1.prems" False have "?P \<dots>"
   1.884 +      proof (intro "1.IH")
   1.885 +        have "(s' - r' div r * s) * a + (t' - r' div r * t) * b =
   1.886 +              (s' * a + t' * b) - r' div r * (s * a + t * b)" by (simp add: algebra_simps)
   1.887 +        also have "s' * a + t' * b = r'" by fact
   1.888 +        also have "s * a + t * b = r" by fact
   1.889 +        also have "r' - r' div r * r = r' mod r" using div_mult_mod_eq [of r' r]
   1.890 +          by (simp add: algebra_simps)
   1.891 +        finally show "(s' - r' div r * s) * a + (t' - r' div r * t) * b = r' mod r" .
   1.892 +      qed (auto simp: gcd_mod_right algebra_simps minus_mod_eq_div_mult [symmetric] gcd.commute)
   1.893 +      finally show ?thesis .
   1.894 +    qed
   1.895 +  qed
   1.896 +  with assms(4) show "c = gcd a b" "x * a + y * b = gcd a b"
   1.897 +    by simp_all
   1.898 +qed
   1.899  
   1.900 -lemma bezout: "\<exists>s t. s * a + t * b = gcd a b"
   1.901 -  using euclid_ext'_correct by blast
   1.902 +declare euclid_ext_aux.simps [simp del]
   1.903 +
   1.904 +definition bezout_coefficients :: "'a \<Rightarrow> 'a \<Rightarrow> 'a \<times> 'a"
   1.905 +  where [code]: "bezout_coefficients a b = fst (euclid_ext a b)"
   1.906 +
   1.907 +lemma bezout_coefficients_0: 
   1.908 +  "bezout_coefficients a 0 = (1 div unit_factor a, 0)"
   1.909 +  by (simp add: bezout_coefficients_def euclid_ext_aux.simps)
   1.910 +
   1.911 +lemma bezout_coefficients_left_0: 
   1.912 +  "bezout_coefficients 0 a = (0, 1 div unit_factor a)"
   1.913 +  by (simp add: bezout_coefficients_def euclid_ext_aux.simps)
   1.914 +
   1.915 +lemma bezout_coefficients:
   1.916 +  assumes "bezout_coefficients a b = (x, y)"
   1.917 +  shows "x * a + y * b = gcd a b"
   1.918 +  using assms by (simp add: bezout_coefficients_def
   1.919 +    euclid_ext_aux_bezout [of a b a b 1 0 0 1 x y] prod_eq_iff)
   1.920 +
   1.921 +lemma bezout_coefficients_fst_snd:
   1.922 +  "fst (bezout_coefficients a b) * a + snd (bezout_coefficients a b) * b = gcd a b"
   1.923 +  by (rule bezout_coefficients) simp
   1.924 +
   1.925 +lemma euclid_ext_eq [simp]:
   1.926 +  "euclid_ext a b = (bezout_coefficients a b, gcd a b)" (is "?p = ?q")
   1.927 +proof
   1.928 +  show "fst ?p = fst ?q"
   1.929 +    by (simp add: bezout_coefficients_def)
   1.930 +  have "snd (euclid_ext_aux 1 0 0 1 a b) = gcd a b"
   1.931 +    by (rule euclid_ext_aux_eq_gcd [of a b a b 1 0 0 1])
   1.932 +      (simp_all add: prod_eq_iff)
   1.933 +  then show "snd ?p = snd ?q"
   1.934 +    by simp
   1.935 +qed
   1.936 +
   1.937 +declare euclid_ext_eq [symmetric, code_unfold]
   1.938  
   1.939  end
   1.940  
   1.941 @@ -565,19 +555,78 @@
   1.942  
   1.943  instance nat :: euclidean_semiring_gcd
   1.944  proof
   1.945 -  show [simp]: "gcd = (gcd_eucl :: nat \<Rightarrow> _)" "Lcm = (Lcm_eucl :: nat set \<Rightarrow> _)"
   1.946 -    by (simp_all add: eq_gcd_euclI eq_Lcm_euclI)
   1.947 -  show "lcm = (lcm_eucl :: nat \<Rightarrow> _)" "Gcd = (Gcd_eucl :: nat set \<Rightarrow> _)"
   1.948 -    by (intro ext, simp add: lcm_eucl_def lcm_nat_def Gcd_nat_def Gcd_eucl_def)+
   1.949 +  interpret semiring_Gcd 1 0 times
   1.950 +    "Euclidean_Algorithm.gcd" "Euclidean_Algorithm.lcm"
   1.951 +    "Euclidean_Algorithm.Gcd" "Euclidean_Algorithm.Lcm"
   1.952 +    divide plus minus normalize unit_factor
   1.953 +    rewrites "dvd.dvd op * = Rings.dvd"
   1.954 +    by (fact semiring_Gcd) (simp add: dvd.dvd_def dvd_def fun_eq_iff)
   1.955 +  show [simp]: "(Euclidean_Algorithm.gcd :: nat \<Rightarrow> _) = gcd"
   1.956 +  proof (rule ext)+
   1.957 +    fix m n :: nat
   1.958 +    show "Euclidean_Algorithm.gcd m n = gcd m n"
   1.959 +    proof (induct m n rule: eucl_induct)
   1.960 +      case zero
   1.961 +      then show ?case
   1.962 +        by simp
   1.963 +    next
   1.964 +      case (mod m n)
   1.965 +      then have "gcd n (m mod n) = gcd n m"
   1.966 +        using gcd_nat.simps [of m n] by (simp add: ac_simps)
   1.967 +      with mod show ?case
   1.968 +        by (simp add: Euclidean_Algorithm.gcd_mod ac_simps)
   1.969 +    qed
   1.970 +  qed
   1.971 +  show [simp]: "(Euclidean_Algorithm.Lcm :: nat set \<Rightarrow> _) = Lcm"
   1.972 +    by (auto intro!: ext Lcm_eqI)
   1.973 +  show "(Euclidean_Algorithm.lcm :: nat \<Rightarrow> _) = lcm"
   1.974 +    by (simp add: fun_eq_iff Euclidean_Algorithm.lcm_def semiring_gcd_class.lcm_gcd)
   1.975 +  show "(Euclidean_Algorithm.Gcd :: nat set \<Rightarrow> _) = Gcd"
   1.976 +    by (simp add: fun_eq_iff Euclidean_Algorithm.Gcd_def semiring_Gcd_class.Gcd_Lcm)
   1.977  qed
   1.978  
   1.979  instance int :: euclidean_ring_gcd
   1.980  proof
   1.981 -  show [simp]: "gcd = (gcd_eucl :: int \<Rightarrow> _)" "Lcm = (Lcm_eucl :: int set \<Rightarrow> _)"
   1.982 -    by (simp_all add: eq_gcd_euclI eq_Lcm_euclI)
   1.983 -  show "lcm = (lcm_eucl :: int \<Rightarrow> _)" "Gcd = (Gcd_eucl :: int set \<Rightarrow> _)"
   1.984 -    by (intro ext, simp add: lcm_eucl_def lcm_altdef_int 
   1.985 -          semiring_Gcd_class.Gcd_Lcm Gcd_eucl_def abs_mult)+
   1.986 +  interpret semiring_Gcd 1 0 times
   1.987 +    "Euclidean_Algorithm.gcd" "Euclidean_Algorithm.lcm"
   1.988 +    "Euclidean_Algorithm.Gcd" "Euclidean_Algorithm.Lcm"
   1.989 +    divide plus minus normalize unit_factor
   1.990 +    rewrites "dvd.dvd op * = Rings.dvd"
   1.991 +    by (fact semiring_Gcd) (simp add: dvd.dvd_def dvd_def fun_eq_iff)
   1.992 +  show [simp]: "(Euclidean_Algorithm.gcd :: int \<Rightarrow> _) = gcd"
   1.993 +  proof (rule ext)+
   1.994 +    fix k l :: int
   1.995 +    show "Euclidean_Algorithm.gcd k l = gcd k l"
   1.996 +    proof (induct k l rule: eucl_induct)
   1.997 +      case zero
   1.998 +      then show ?case
   1.999 +        by simp
  1.1000 +    next
  1.1001 +      case (mod k l)
  1.1002 +      have "gcd l (k mod l) = gcd l k"
  1.1003 +      proof (cases l "0::int" rule: linorder_cases)
  1.1004 +        case less
  1.1005 +        then show ?thesis
  1.1006 +          using gcd_non_0_int [of "- l" "- k"] by (simp add: ac_simps)
  1.1007 +      next
  1.1008 +        case equal
  1.1009 +        with mod show ?thesis
  1.1010 +          by simp
  1.1011 +      next
  1.1012 +        case greater
  1.1013 +        then show ?thesis
  1.1014 +          using gcd_non_0_int [of l k] by (simp add: ac_simps)
  1.1015 +      qed
  1.1016 +      with mod show ?case
  1.1017 +        by (simp add: Euclidean_Algorithm.gcd_mod ac_simps)
  1.1018 +    qed
  1.1019 +  qed
  1.1020 +  show [simp]: "(Euclidean_Algorithm.Lcm :: int set \<Rightarrow> _) = Lcm"
  1.1021 +    by (auto intro!: ext Lcm_eqI)
  1.1022 +  show "(Euclidean_Algorithm.lcm :: int \<Rightarrow> _) = lcm"
  1.1023 +    by (simp add: fun_eq_iff Euclidean_Algorithm.lcm_def semiring_gcd_class.lcm_gcd)
  1.1024 +  show "(Euclidean_Algorithm.Gcd :: int set \<Rightarrow> _) = Gcd"
  1.1025 +    by (simp add: fun_eq_iff Euclidean_Algorithm.Gcd_def semiring_Gcd_class.Gcd_Lcm)
  1.1026  qed
  1.1027  
  1.1028  end