author haftmann Fri Aug 22 08:43:14 2014 +0200 (2014-08-22) changeset 58023 62826b36ac5e parent 58022 464c1815fde9 child 58031 b2b93903ab6b
generic euclidean algorithm (due to Manuel Eberl)
 CONTRIBUTORS file | annotate | diff | revisions src/HOL/Number_Theory/Euclidean_Algorithm.thy file | annotate | diff | revisions src/HOL/ROOT file | annotate | diff | revisions
     1.1 --- a/CONTRIBUTORS	Thu Aug 21 14:41:08 2014 +0200
1.2 +++ b/CONTRIBUTORS	Fri Aug 22 08:43:14 2014 +0200
1.3 @@ -6,6 +6,9 @@
1.4  Contributions to this Isabelle version
1.5  --------------------------------------
1.6
1.7 +* August 2014: Manuel Eberl, TUM
1.8 +  Generic euclidean algorithms for gcd et al.
1.9 +
1.10
1.11  Contributions to Isabelle2014
1.12  -----------------------------

     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/src/HOL/Number_Theory/Euclidean_Algorithm.thy	Fri Aug 22 08:43:14 2014 +0200
2.3 @@ -0,0 +1,1819 @@
2.4 +(* Author: Manuel Eberl *)
2.5 +
2.6 +header {* Abstract euclidean algorithm *}
2.7 +
2.8 +theory Euclidean_Algorithm
2.9 +imports Complex_Main
2.10 +begin
2.11 +
2.12 +lemma finite_int_set_iff_bounded_le:
2.13 +  "finite (N::int set) = (\<exists>m\<ge>0. \<forall>n\<in>N. abs n \<le> m)"
2.14 +proof
2.15 +  assume "finite (N::int set)"
2.16 +  hence "finite (nat  abs  N)" by (intro finite_imageI)
2.17 +  hence "\<exists>m. \<forall>n\<in>natabsN. n \<le> m" by (simp add: finite_nat_set_iff_bounded_le)
2.18 +  then obtain m :: nat where "\<forall>n\<in>N. nat (abs n) \<le> nat (int m)" by auto
2.19 +  then show "\<exists>m\<ge>0. \<forall>n\<in>N. abs n \<le> m" by (intro exI[of _ "int m"]) (auto simp: nat_le_eq_zle)
2.20 +next
2.21 +  assume "\<exists>m\<ge>0. \<forall>n\<in>N. abs n \<le> m"
2.22 +  then obtain m where "m \<ge> 0" and "\<forall>n\<in>N. abs n \<le> m" by blast
2.23 +  hence "\<forall>n\<in>N. nat (abs n) \<le> nat m" by (auto simp: nat_le_eq_zle)
2.24 +  hence "\<forall>n\<in>natabsN. n \<le> nat m" by (auto simp: nat_le_eq_zle)
2.25 +  hence A: "finite ((nat \<circ> abs)N)" unfolding o_def
2.26 +      by (subst finite_nat_set_iff_bounded_le) blast
2.27 +  {
2.28 +    assume "\<not>finite N"
2.29 +    from pigeonhole_infinite[OF this A] obtain x
2.30 +       where "x \<in> N" and B: "~finite {a\<in>N. nat (abs a) = nat (abs x)}"
2.31 +       unfolding o_def by blast
2.32 +    have "{a\<in>N. nat (abs a) = nat (abs x)} \<subseteq> {x, -x}" by auto
2.33 +    hence "finite {a\<in>N. nat (abs a) = nat (abs x)}" by (rule finite_subset) simp
2.34 +    with B have False by contradiction
2.35 +  }
2.36 +  then show "finite N" by blast
2.37 +qed
2.38 +
2.39 +context semiring_div
2.40 +begin
2.41 +
2.42 +lemma dvd_setprod [intro]:
2.43 +  assumes "finite A" and "x \<in> A"
2.44 +  shows "f x dvd setprod f A"
2.45 +proof
2.46 +  from finite A have "setprod f (insert x (A - {x})) = f x * setprod f (A - {x})"
2.47 +    by (intro setprod.insert) auto
2.48 +  also from x \<in> A have "insert x (A - {x}) = A" by blast
2.49 +  finally show "setprod f A = f x * setprod f (A - {x})" .
2.50 +qed
2.51 +
2.52 +lemma dvd_mult_cancel_left:
2.53 +  assumes "a \<noteq> 0" and "a * b dvd a * c"
2.54 +  shows "b dvd c"
2.55 +proof-
2.56 +  from assms(2) obtain k where "a * c = a * b * k" unfolding dvd_def by blast
2.57 +  hence "c * a = b * k * a" by (simp add: ac_simps)
2.58 +  hence "c * (a div a) = b * k * (a div a)" by (simp add: div_mult_swap)
2.59 +  also from a \<noteq> 0 have "a div a = 1" by simp
2.60 +  finally show ?thesis by simp
2.61 +qed
2.62 +
2.63 +lemma dvd_mult_cancel_right:
2.64 +  "a \<noteq> 0 \<Longrightarrow> b * a dvd c * a \<Longrightarrow> b dvd c"
2.65 +  by (subst (asm) (1 2) ac_simps, rule dvd_mult_cancel_left)
2.66 +
2.67 +lemma nonzero_pow_nonzero:
2.68 +  "a \<noteq> 0 \<Longrightarrow> a ^ n \<noteq> 0"
2.69 +  by (induct n) (simp_all add: no_zero_divisors)
2.70 +
2.71 +lemma zero_pow_zero: "n \<noteq> 0 \<Longrightarrow> 0 ^ n = 0"
2.72 +  by (cases n, simp_all)
2.73 +
2.74 +lemma pow_zero_iff:
2.75 +  "n \<noteq> 0 \<Longrightarrow> a^n = 0 \<longleftrightarrow> a = 0"
2.76 +  using nonzero_pow_nonzero zero_pow_zero by auto
2.77 +
2.78 +end
2.79 +
2.80 +context semiring_div
2.81 +begin
2.82 +
2.83 +definition ring_inv :: "'a \<Rightarrow> 'a"
2.84 +where
2.85 +  "ring_inv x = 1 div x"
2.86 +
2.87 +definition is_unit :: "'a \<Rightarrow> bool"
2.88 +where
2.89 +  "is_unit x \<longleftrightarrow> x dvd 1"
2.90 +
2.91 +definition associated :: "'a \<Rightarrow> 'a \<Rightarrow> bool"
2.92 +where
2.93 +  "associated x y \<longleftrightarrow> x dvd y \<and> y dvd x"
2.94 +
2.95 +lemma unit_prod [intro]:
2.96 +  "is_unit x \<Longrightarrow> is_unit y \<Longrightarrow> is_unit (x * y)"
2.97 +  unfolding is_unit_def by (subst mult_1_left [of 1, symmetric], rule mult_dvd_mono)
2.98 +
2.99 +lemma unit_ring_inv:
2.100 +  "is_unit y \<Longrightarrow> x div y = x * ring_inv y"
2.101 +  by (simp add: div_mult_swap ring_inv_def is_unit_def)
2.102 +
2.103 +lemma unit_ring_inv_ring_inv [simp]:
2.104 +  "is_unit x \<Longrightarrow> ring_inv (ring_inv x) = x"
2.105 +  unfolding is_unit_def ring_inv_def
2.106 +  by (metis div_mult_mult1_if div_mult_self1_is_id dvd_mult_div_cancel mult_1_right)
2.107 +
2.108 +lemma inv_imp_eq_ring_inv:
2.109 +  "a * b = 1 \<Longrightarrow> ring_inv a = b"
2.110 +  by (metis dvd_mult_div_cancel dvd_mult_right mult_1_right mult.left_commute one_dvd ring_inv_def)
2.111 +
2.112 +lemma ring_inv_is_inv1 [simp]:
2.113 +  "is_unit a \<Longrightarrow> a * ring_inv a = 1"
2.114 +  unfolding is_unit_def ring_inv_def by (simp add: dvd_mult_div_cancel)
2.115 +
2.116 +lemma ring_inv_is_inv2 [simp]:
2.117 +  "is_unit a \<Longrightarrow> ring_inv a * a = 1"
2.118 +  by (simp add: ac_simps)
2.119 +
2.120 +lemma unit_ring_inv_unit [simp, intro]:
2.121 +  assumes "is_unit x"
2.122 +  shows "is_unit (ring_inv x)"
2.123 +proof -
2.124 +  from assms have "1 = ring_inv x * x" by simp
2.125 +  then show "is_unit (ring_inv x)" unfolding is_unit_def by (rule dvdI)
2.126 +qed
2.127 +
2.128 +lemma mult_unit_dvd_iff:
2.129 +  "is_unit y \<Longrightarrow> x * y dvd z \<longleftrightarrow> x dvd z"
2.130 +proof
2.131 +  assume "is_unit y" "x * y dvd z"
2.132 +  then show "x dvd z" by (simp add: dvd_mult_left)
2.133 +next
2.134 +  assume "is_unit y" "x dvd z"
2.135 +  then obtain k where "z = x * k" unfolding dvd_def by blast
2.136 +  with is_unit y have "z = (x * y) * (ring_inv y * k)"
2.137 +      by (simp add: mult_ac)
2.138 +  then show "x * y dvd z" by (rule dvdI)
2.139 +qed
2.140 +
2.141 +lemma div_unit_dvd_iff:
2.142 +  "is_unit y \<Longrightarrow> x div y dvd z \<longleftrightarrow> x dvd z"
2.143 +  by (subst unit_ring_inv) (assumption, simp add: mult_unit_dvd_iff)
2.144 +
2.145 +lemma dvd_mult_unit_iff:
2.146 +  "is_unit y \<Longrightarrow> x dvd z * y \<longleftrightarrow> x dvd z"
2.147 +proof
2.148 +  assume "is_unit y" and "x dvd z * y"
2.149 +  have "z * y dvd z * (y * ring_inv y)" by (subst mult_assoc [symmetric]) simp
2.150 +  also from is_unit y have "y * ring_inv y = 1" by simp
2.151 +  finally have "z * y dvd z" by simp
2.152 +  with x dvd z * y show "x dvd z" by (rule dvd_trans)
2.153 +next
2.154 +  assume "x dvd z"
2.155 +  then show "x dvd z * y" by simp
2.156 +qed
2.157 +
2.158 +lemma dvd_div_unit_iff:
2.159 +  "is_unit y \<Longrightarrow> x dvd z div y \<longleftrightarrow> x dvd z"
2.160 +  by (subst unit_ring_inv) (assumption, simp add: dvd_mult_unit_iff)
2.161 +
2.162 +lemmas unit_dvd_iff = mult_unit_dvd_iff div_unit_dvd_iff dvd_mult_unit_iff dvd_div_unit_iff
2.163 +
2.164 +lemma unit_div [intro]:
2.165 +  "is_unit x \<Longrightarrow> is_unit y \<Longrightarrow> is_unit (x div y)"
2.166 +  by (subst unit_ring_inv) (assumption, rule unit_prod, simp_all)
2.167 +
2.168 +lemma unit_div_mult_swap:
2.169 +  "is_unit z \<Longrightarrow> x * (y div z) = x * y div z"
2.170 +  by (simp only: unit_ring_inv [of _ y] unit_ring_inv [of _ "x*y"] ac_simps)
2.171 +
2.172 +lemma unit_div_commute:
2.173 +  "is_unit y \<Longrightarrow> x div y * z = x * z div y"
2.174 +  by (simp only: unit_ring_inv [of _ x] unit_ring_inv [of _ "x*z"] ac_simps)
2.175 +
2.176 +lemma unit_imp_dvd [dest]:
2.177 +  "is_unit y \<Longrightarrow> y dvd x"
2.178 +  by (rule dvd_trans [of _ 1]) (simp_all add: is_unit_def)
2.179 +
2.180 +lemma dvd_unit_imp_unit:
2.181 +  "is_unit y \<Longrightarrow> x dvd y \<Longrightarrow> is_unit x"
2.182 +  by (unfold is_unit_def) (rule dvd_trans)
2.183 +
2.184 +lemma ring_inv_0 [simp]:
2.185 +  "ring_inv 0 = 0"
2.186 +  unfolding ring_inv_def by simp
2.187 +
2.188 +lemma unit_ring_inv'1:
2.189 +  assumes "is_unit y"
2.190 +  shows "x div (y * z) = x * ring_inv y div z"
2.191 +proof -
2.192 +  from assms have "x div (y * z) = x * (ring_inv y * y) div (y * z)"
2.193 +    by simp
2.194 +  also have "... = y * (x * ring_inv y) div (y * z)"
2.195 +    by (simp only: mult_ac)
2.196 +  also have "... = x * ring_inv y div z"
2.197 +    by (cases "y = 0", simp, rule div_mult_mult1)
2.198 +  finally show ?thesis .
2.199 +qed
2.200 +
2.201 +lemma associated_comm:
2.202 +  "associated x y \<Longrightarrow> associated y x"
2.203 +  by (simp add: associated_def)
2.204 +
2.205 +lemma associated_0 [simp]:
2.206 +  "associated 0 b \<longleftrightarrow> b = 0"
2.207 +  "associated a 0 \<longleftrightarrow> a = 0"
2.208 +  unfolding associated_def by simp_all
2.209 +
2.210 +lemma associated_unit:
2.211 +  "is_unit x \<Longrightarrow> associated x y \<Longrightarrow> is_unit y"
2.212 +  unfolding associated_def by (fast dest: dvd_unit_imp_unit)
2.213 +
2.214 +lemma is_unit_1 [simp]:
2.215 +  "is_unit 1"
2.216 +  unfolding is_unit_def by simp
2.217 +
2.218 +lemma not_is_unit_0 [simp]:
2.219 +  "\<not> is_unit 0"
2.220 +  unfolding is_unit_def by auto
2.221 +
2.222 +lemma unit_mult_left_cancel:
2.223 +  assumes "is_unit x"
2.224 +  shows "(x * y) = (x * z) \<longleftrightarrow> y = z"
2.225 +proof -
2.226 +  from assms have "x \<noteq> 0" by auto
2.227 +  then show ?thesis by (metis div_mult_self1_is_id)
2.228 +qed
2.229 +
2.230 +lemma unit_mult_right_cancel:
2.231 +  "is_unit x \<Longrightarrow> (y * x) = (z * x) \<longleftrightarrow> y = z"
2.232 +  by (simp add: ac_simps unit_mult_left_cancel)
2.233 +
2.234 +lemma unit_div_cancel:
2.235 +  "is_unit x \<Longrightarrow> (y div x) = (z div x) \<longleftrightarrow> y = z"
2.236 +  apply (subst unit_ring_inv[of _ y], assumption)
2.237 +  apply (subst unit_ring_inv[of _ z], assumption)
2.238 +  apply (rule unit_mult_right_cancel, erule unit_ring_inv_unit)
2.239 +  done
2.240 +
2.241 +lemma unit_eq_div1:
2.242 +  "is_unit y \<Longrightarrow> x div y = z \<longleftrightarrow> x = z * y"
2.243 +  apply (subst unit_ring_inv, assumption)
2.244 +  apply (subst unit_mult_right_cancel[symmetric], assumption)
2.245 +  apply (subst mult_assoc, subst ring_inv_is_inv2, assumption, simp)
2.246 +  done
2.247 +
2.248 +lemma unit_eq_div2:
2.249 +  "is_unit y \<Longrightarrow> x = z div y \<longleftrightarrow> x * y = z"
2.250 +  by (subst (1 2) eq_commute, simp add: unit_eq_div1, subst eq_commute, rule refl)
2.251 +
2.252 +lemma associated_iff_div_unit:
2.253 +  "associated x y \<longleftrightarrow> (\<exists>z. is_unit z \<and> x = z * y)"
2.254 +proof
2.255 +  assume "associated x y"
2.256 +  show "\<exists>z. is_unit z \<and> x = z * y"
2.257 +  proof (cases "x = 0")
2.258 +    assume "x = 0"
2.259 +    then show "\<exists>z. is_unit z \<and> x = z * y" using associated x y
2.260 +        by (intro exI[of _ 1], simp add: associated_def)
2.261 +  next
2.262 +    assume [simp]: "x \<noteq> 0"
2.263 +    hence [simp]: "x dvd y" "y dvd x" using associated x y
2.264 +        unfolding associated_def by simp_all
2.265 +    hence "1 = x div y * (y div x)"
2.266 +      by (simp add: div_mult_swap dvd_div_mult_self)
2.267 +    hence "is_unit (x div y)" unfolding is_unit_def by (rule dvdI)
2.268 +    moreover have "x = (x div y) * y" by (simp add: dvd_div_mult_self)
2.269 +    ultimately show ?thesis by blast
2.270 +  qed
2.271 +next
2.272 +  assume "\<exists>z. is_unit z \<and> x = z * y"
2.273 +  then obtain z where "is_unit z" and "x = z * y" by blast
2.274 +  hence "y = x * ring_inv z" by (simp add: algebra_simps)
2.275 +  hence "x dvd y" by simp
2.276 +  moreover from x = z * y have "y dvd x" by simp
2.277 +  ultimately show "associated x y" unfolding associated_def by simp
2.278 +qed
2.279 +
2.280 +lemmas unit_simps = mult_unit_dvd_iff div_unit_dvd_iff dvd_mult_unit_iff
2.281 +  dvd_div_unit_iff unit_div_mult_swap unit_div_commute
2.282 +  unit_mult_left_cancel unit_mult_right_cancel unit_div_cancel
2.283 +  unit_eq_div1 unit_eq_div2
2.284 +
2.285 +end
2.286 +
2.287 +context ring_div
2.288 +begin
2.289 +
2.290 +lemma is_unit_neg [simp]:
2.291 +  "is_unit (- x) \<Longrightarrow> is_unit x"
2.292 +  unfolding is_unit_def by simp
2.293 +
2.294 +lemma is_unit_neg_1 [simp]:
2.295 +  "is_unit (-1)"
2.296 +  unfolding is_unit_def by simp
2.297 +
2.298 +end
2.299 +
2.300 +lemma is_unit_nat [simp]:
2.301 +  "is_unit (x::nat) \<longleftrightarrow> x = 1"
2.302 +  unfolding is_unit_def by simp
2.303 +
2.304 +lemma is_unit_int:
2.305 +  "is_unit (x::int) \<longleftrightarrow> x = 1 \<or> x = -1"
2.306 +  unfolding is_unit_def by auto
2.307 +
2.308 +text {*
2.309 +  A Euclidean semiring is a semiring upon which the Euclidean algorithm can be
2.310 +  implemented. It must provide:
2.311 +  \begin{itemize}
2.312 +  \item division with remainder
2.313 +  \item a size function such that @{term "size (a mod b) < size b"}
2.314 +        for any @{term "b \<noteq> 0"}
2.315 +  \item a normalisation factor such that two associated numbers are equal iff
2.316 +        they are the same when divided by their normalisation factors.
2.317 +  \end{itemize}
2.318 +  The existence of these functions makes it possible to derive gcd and lcm functions
2.319 +  for any Euclidean semiring.
2.320 +*}
2.321 +class euclidean_semiring = semiring_div +
2.322 +  fixes euclidean_size :: "'a \<Rightarrow> nat"
2.323 +  fixes normalisation_factor :: "'a \<Rightarrow> 'a"
2.324 +  assumes mod_size_less [simp]:
2.325 +    "b \<noteq> 0 \<Longrightarrow> euclidean_size (a mod b) < euclidean_size b"
2.326 +  assumes size_mult_mono:
2.327 +    "b \<noteq> 0 \<Longrightarrow> euclidean_size (a * b) \<ge> euclidean_size a"
2.328 +  assumes normalisation_factor_is_unit [intro,simp]:
2.329 +    "a \<noteq> 0 \<Longrightarrow> is_unit (normalisation_factor a)"
2.330 +  assumes normalisation_factor_mult: "normalisation_factor (a * b) =
2.331 +    normalisation_factor a * normalisation_factor b"
2.332 +  assumes normalisation_factor_unit: "is_unit x \<Longrightarrow> normalisation_factor x = x"
2.333 +  assumes normalisation_factor_0 [simp]: "normalisation_factor 0 = 0"
2.334 +begin
2.335 +
2.336 +lemma normalisation_factor_dvd [simp]:
2.337 +  "a \<noteq> 0 \<Longrightarrow> normalisation_factor a dvd b"
2.338 +  by (rule unit_imp_dvd, simp)
2.339 +
2.340 +lemma normalisation_factor_1 [simp]:
2.341 +  "normalisation_factor 1 = 1"
2.342 +  by (simp add: normalisation_factor_unit)
2.343 +
2.344 +lemma normalisation_factor_0_iff [simp]:
2.345 +  "normalisation_factor x = 0 \<longleftrightarrow> x = 0"
2.346 +proof
2.347 +  assume "normalisation_factor x = 0"
2.348 +  hence "\<not> is_unit (normalisation_factor x)"
2.349 +    by (metis not_is_unit_0)
2.350 +  then show "x = 0" by force
2.351 +next
2.352 +  assume "x = 0"
2.353 +  then show "normalisation_factor x = 0" by simp
2.354 +qed
2.355 +
2.356 +lemma normalisation_factor_pow:
2.357 +  "normalisation_factor (x ^ n) = normalisation_factor x ^ n"
2.358 +  by (induct n) (simp_all add: normalisation_factor_mult power_Suc2)
2.359 +
2.360 +lemma normalisation_correct [simp]:
2.361 +  "normalisation_factor (x div normalisation_factor x) = (if x = 0 then 0 else 1)"
2.362 +proof (cases "x = 0", simp)
2.363 +  assume "x \<noteq> 0"
2.364 +  let ?nf = "normalisation_factor"
2.365 +  from normalisation_factor_is_unit[OF x \<noteq> 0] have "?nf x \<noteq> 0"
2.366 +    by (metis not_is_unit_0)
2.367 +  have "?nf (x div ?nf x) * ?nf (?nf x) = ?nf (x div ?nf x * ?nf x)"
2.368 +    by (simp add: normalisation_factor_mult)
2.369 +  also have "x div ?nf x * ?nf x = x" using x \<noteq> 0
2.370 +    by (simp add: dvd_div_mult_self)
2.371 +  also have "?nf (?nf x) = ?nf x" using x \<noteq> 0
2.372 +    normalisation_factor_is_unit normalisation_factor_unit by simp
2.373 +  finally show ?thesis using x \<noteq> 0 and ?nf x \<noteq> 0
2.374 +    by (metis div_mult_self2_is_id div_self)
2.375 +qed
2.376 +
2.377 +lemma normalisation_0_iff [simp]:
2.378 +  "x div normalisation_factor x = 0 \<longleftrightarrow> x = 0"
2.379 +  by (cases "x = 0", simp, subst unit_eq_div1, blast, simp)
2.380 +
2.381 +lemma associated_iff_normed_eq:
2.382 +  "associated a b \<longleftrightarrow> a div normalisation_factor a = b div normalisation_factor b"
2.383 +proof (cases "b = 0", simp, cases "a = 0", metis associated_0(1) normalisation_0_iff, rule iffI)
2.384 +  let ?nf = normalisation_factor
2.385 +  assume "a \<noteq> 0" "b \<noteq> 0" "a div ?nf a = b div ?nf b"
2.386 +  hence "a = b * (?nf a div ?nf b)"
2.387 +    apply (subst (asm) unit_eq_div1, blast, subst (asm) unit_div_commute, blast)
2.388 +    apply (subst div_mult_swap, simp, simp)
2.389 +    done
2.390 +  with a \<noteq> 0 b \<noteq> 0 have "\<exists>z. is_unit z \<and> a = z * b"
2.391 +    by (intro exI[of _ "?nf a div ?nf b"], force simp: mult_ac)
2.392 +  with associated_iff_div_unit show "associated a b" by simp
2.393 +next
2.394 +  let ?nf = normalisation_factor
2.395 +  assume "a \<noteq> 0" "b \<noteq> 0" "associated a b"
2.396 +  with associated_iff_div_unit obtain z where "is_unit z" and "a = z * b" by blast
2.397 +  then show "a div ?nf a = b div ?nf b"
2.398 +    apply (simp only: a = z * b normalisation_factor_mult normalisation_factor_unit)
2.399 +    apply (rule div_mult_mult1, force)
2.400 +    done
2.401 +  qed
2.402 +
2.403 +lemma normed_associated_imp_eq:
2.404 +  "associated a b \<Longrightarrow> normalisation_factor a \<in> {0, 1} \<Longrightarrow> normalisation_factor b \<in> {0, 1} \<Longrightarrow> a = b"
2.405 +  by (simp add: associated_iff_normed_eq, elim disjE, simp_all)
2.406 +
2.407 +lemmas normalisation_factor_dvd_iff [simp] =
2.408 +  unit_dvd_iff [OF normalisation_factor_is_unit]
2.409 +
2.410 +lemma euclidean_division:
2.411 +  fixes a :: 'a and b :: 'a
2.412 +  assumes "b \<noteq> 0"
2.413 +  obtains s and t where "a = s * b + t"
2.414 +    and "euclidean_size t < euclidean_size b"
2.415 +proof -
2.416 +  from div_mod_equality[of a b 0]
2.417 +     have "a = a div b * b + a mod b" by simp
2.418 +  with that and assms show ?thesis by force
2.419 +qed
2.420 +
2.421 +lemma dvd_euclidean_size_eq_imp_dvd:
2.422 +  assumes "a \<noteq> 0" and b_dvd_a: "b dvd a" and size_eq: "euclidean_size a = euclidean_size b"
2.423 +  shows "a dvd b"
2.424 +proof (subst dvd_eq_mod_eq_0, rule ccontr)
2.425 +  assume "b mod a \<noteq> 0"
2.426 +  from b_dvd_a have b_dvd_mod: "b dvd b mod a" by (simp add: dvd_mod_iff)
2.427 +  from b_dvd_mod obtain c where "b mod a = b * c" unfolding dvd_def by blast
2.428 +    with b mod a \<noteq> 0 have "c \<noteq> 0" by auto
2.429 +  with b mod a = b * c have "euclidean_size (b mod a) \<ge> euclidean_size b"
2.430 +      using size_mult_mono by force
2.431 +  moreover from a \<noteq> 0 have "euclidean_size (b mod a) < euclidean_size a"
2.432 +      using mod_size_less by blast
2.433 +  ultimately show False using size_eq by simp
2.434 +qed
2.435 +
2.436 +function gcd_eucl :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"
2.437 +where
2.438 +  "gcd_eucl a b = (if b = 0 then a div normalisation_factor a else gcd_eucl b (a mod b))"
2.439 +  by (pat_completeness, simp)
2.440 +termination by (relation "measure (euclidean_size \<circ> snd)", simp_all)
2.441 +
2.442 +declare gcd_eucl.simps [simp del]
2.443 +
2.444 +lemma gcd_induct: "\<lbrakk>\<And>b. P b 0; \<And>a b. 0 \<noteq> b \<Longrightarrow> P b (a mod b) \<Longrightarrow> P a b\<rbrakk> \<Longrightarrow> P a b"
2.445 +proof (induct a b rule: gcd_eucl.induct)
2.446 +  case ("1" m n)
2.447 +    then show ?case by (cases "n = 0") auto
2.448 +qed
2.449 +
2.450 +definition lcm_eucl :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"
2.451 +where
2.452 +  "lcm_eucl a b = a * b div (gcd_eucl a b * normalisation_factor (a * b))"
2.453 +
2.454 +  (* Somewhat complicated definition of Lcm that has the advantage of working
2.455 +     for infinite sets as well *)
2.456 +
2.457 +definition Lcm_eucl :: "'a set \<Rightarrow> 'a"
2.458 +where
2.459 +  "Lcm_eucl A = (if \<exists>l. l \<noteq> 0 \<and> (\<forall>x\<in>A. x dvd l) then
2.460 +     let l = SOME l. l \<noteq> 0 \<and> (\<forall>x\<in>A. x dvd l) \<and> euclidean_size l =
2.461 +       (LEAST n. \<exists>l. l \<noteq> 0 \<and> (\<forall>x\<in>A. x dvd l) \<and> euclidean_size l = n)
2.462 +       in l div normalisation_factor l
2.463 +      else 0)"
2.464 +
2.465 +definition Gcd_eucl :: "'a set \<Rightarrow> 'a"
2.466 +where
2.467 +  "Gcd_eucl A = Lcm_eucl {d. \<forall>a\<in>A. d dvd a}"
2.468 +
2.469 +end
2.470 +
2.471 +class euclidean_semiring_gcd = euclidean_semiring + gcd + Gcd +
2.472 +  assumes gcd_gcd_eucl: "gcd = gcd_eucl" and lcm_lcm_eucl: "lcm = lcm_eucl"
2.473 +  assumes Gcd_Gcd_eucl: "Gcd = Gcd_eucl" and Lcm_Lcm_eucl: "Lcm = Lcm_eucl"
2.474 +begin
2.475 +
2.476 +lemma gcd_red:
2.477 +  "gcd x y = gcd y (x mod y)"
2.478 +  by (metis gcd_eucl.simps mod_0 mod_by_0 gcd_gcd_eucl)
2.479 +
2.480 +lemma gcd_non_0:
2.481 +  "y \<noteq> 0 \<Longrightarrow> gcd x y = gcd y (x mod y)"
2.482 +  by (rule gcd_red)
2.483 +
2.484 +lemma gcd_0_left:
2.485 +  "gcd 0 x = x div normalisation_factor x"
2.486 +   by (simp only: gcd_gcd_eucl, subst gcd_eucl.simps, subst gcd_eucl.simps, simp add: Let_def)
2.487 +
2.488 +lemma gcd_0:
2.489 +  "gcd x 0 = x div normalisation_factor x"
2.490 +  by (simp only: gcd_gcd_eucl, subst gcd_eucl.simps, simp add: Let_def)
2.491 +
2.492 +lemma gcd_dvd1 [iff]: "gcd x y dvd x"
2.493 +  and gcd_dvd2 [iff]: "gcd x y dvd y"
2.494 +proof (induct x y rule: gcd_eucl.induct)
2.495 +  fix x y :: 'a
2.496 +  assume IH1: "y \<noteq> 0 \<Longrightarrow> gcd y (x mod y) dvd y"
2.497 +  assume IH2: "y \<noteq> 0 \<Longrightarrow> gcd y (x mod y) dvd (x mod y)"
2.498 +
2.499 +  have "gcd x y dvd x \<and> gcd x y dvd y"
2.500 +  proof (cases "y = 0")
2.501 +    case True
2.502 +      then show ?thesis by (cases "x = 0", simp_all add: gcd_0)
2.503 +  next
2.504 +    case False
2.505 +      with IH1 and IH2 show ?thesis by (simp add: gcd_non_0 dvd_mod_iff)
2.506 +  qed
2.507 +  then show "gcd x y dvd x" "gcd x y dvd y" by simp_all
2.508 +qed
2.509 +
2.510 +lemma dvd_gcd_D1: "k dvd gcd m n \<Longrightarrow> k dvd m"
2.511 +  by (rule dvd_trans, assumption, rule gcd_dvd1)
2.512 +
2.513 +lemma dvd_gcd_D2: "k dvd gcd m n \<Longrightarrow> k dvd n"
2.514 +  by (rule dvd_trans, assumption, rule gcd_dvd2)
2.515 +
2.516 +lemma gcd_greatest:
2.517 +  fixes k x y :: 'a
2.518 +  shows "k dvd x \<Longrightarrow> k dvd y \<Longrightarrow> k dvd gcd x y"
2.519 +proof (induct x y rule: gcd_eucl.induct)
2.520 +  case (1 x y)
2.521 +  show ?case
2.522 +    proof (cases "y = 0")
2.523 +      assume "y = 0"
2.524 +      with 1 show ?thesis by (cases "x = 0", simp_all add: gcd_0)
2.525 +    next
2.526 +      assume "y \<noteq> 0"
2.527 +      with 1 show ?thesis by (simp add: gcd_non_0 dvd_mod_iff)
2.528 +    qed
2.529 +qed
2.530 +
2.531 +lemma dvd_gcd_iff:
2.532 +  "k dvd gcd x y \<longleftrightarrow> k dvd x \<and> k dvd y"
2.533 +  by (blast intro!: gcd_greatest intro: dvd_trans)
2.534 +
2.535 +lemmas gcd_greatest_iff = dvd_gcd_iff
2.536 +
2.537 +lemma gcd_zero [simp]:
2.538 +  "gcd x y = 0 \<longleftrightarrow> x = 0 \<and> y = 0"
2.539 +  by (metis dvd_0_left dvd_refl gcd_dvd1 gcd_dvd2 gcd_greatest)+
2.540 +
2.541 +lemma normalisation_factor_gcd [simp]:
2.542 +  "normalisation_factor (gcd x y) = (if x = 0 \<and> y = 0 then 0 else 1)" (is "?f x y = ?g x y")
2.543 +proof (induct x y rule: gcd_eucl.induct)
2.544 +  fix x y :: 'a
2.545 +  assume IH: "y \<noteq> 0 \<Longrightarrow> ?f y (x mod y) = ?g y (x mod y)"
2.546 +  then show "?f x y = ?g x y" by (cases "y = 0", auto simp: gcd_non_0 gcd_0)
2.547 +qed
2.548 +
2.549 +lemma gcdI:
2.550 +  "k dvd x \<Longrightarrow> k dvd y \<Longrightarrow> (\<And>l. l dvd x \<Longrightarrow> l dvd y \<Longrightarrow> l dvd k)
2.551 +    \<Longrightarrow> normalisation_factor k = (if k = 0 then 0 else 1) \<Longrightarrow> k = gcd x y"
2.552 +  by (intro normed_associated_imp_eq) (auto simp: associated_def intro: gcd_greatest)
2.553 +
2.554 +sublocale gcd!: abel_semigroup gcd
2.555 +proof
2.556 +  fix x y z
2.557 +  show "gcd (gcd x y) z = gcd x (gcd y z)"
2.558 +  proof (rule gcdI)
2.559 +    have "gcd (gcd x y) z dvd gcd x y" "gcd x y dvd x" by simp_all
2.560 +    then show "gcd (gcd x y) z dvd x" by (rule dvd_trans)
2.561 +    have "gcd (gcd x y) z dvd gcd x y" "gcd x y dvd y" by simp_all
2.562 +    hence "gcd (gcd x y) z dvd y" by (rule dvd_trans)
2.563 +    moreover have "gcd (gcd x y) z dvd z" by simp
2.564 +    ultimately show "gcd (gcd x y) z dvd gcd y z"
2.565 +      by (rule gcd_greatest)
2.566 +    show "normalisation_factor (gcd (gcd x y) z) =  (if gcd (gcd x y) z = 0 then 0 else 1)"
2.567 +      by auto
2.568 +    fix l assume "l dvd x" and "l dvd gcd y z"
2.569 +    with dvd_trans[OF _ gcd_dvd1] and dvd_trans[OF _ gcd_dvd2]
2.570 +      have "l dvd y" and "l dvd z" by blast+
2.571 +    with l dvd x show "l dvd gcd (gcd x y) z"
2.572 +      by (intro gcd_greatest)
2.573 +  qed
2.574 +next
2.575 +  fix x y
2.576 +  show "gcd x y = gcd y x"
2.577 +    by (rule gcdI) (simp_all add: gcd_greatest)
2.578 +qed
2.579 +
2.580 +lemma gcd_unique: "d dvd a \<and> d dvd b \<and>
2.581 +    normalisation_factor d = (if d = 0 then 0 else 1) \<and>
2.582 +    (\<forall>e. e dvd a \<and> e dvd b \<longrightarrow> e dvd d) \<longleftrightarrow> d = gcd a b"
2.583 +  by (rule, auto intro: gcdI simp: gcd_greatest)
2.584 +
2.585 +lemma gcd_dvd_prod: "gcd a b dvd k * b"
2.586 +  using mult_dvd_mono [of 1] by auto
2.587 +
2.588 +lemma gcd_1_left [simp]: "gcd 1 x = 1"
2.589 +  by (rule sym, rule gcdI, simp_all)
2.590 +
2.591 +lemma gcd_1 [simp]: "gcd x 1 = 1"
2.592 +  by (rule sym, rule gcdI, simp_all)
2.593 +
2.594 +lemma gcd_proj2_if_dvd:
2.595 +  "y dvd x \<Longrightarrow> gcd x y = y div normalisation_factor y"
2.596 +  by (cases "y = 0", simp_all add: dvd_eq_mod_eq_0 gcd_non_0 gcd_0)
2.597 +
2.598 +lemma gcd_proj1_if_dvd:
2.599 +  "x dvd y \<Longrightarrow> gcd x y = x div normalisation_factor x"
2.600 +  by (subst gcd.commute, simp add: gcd_proj2_if_dvd)
2.601 +
2.602 +lemma gcd_proj1_iff: "gcd m n = m div normalisation_factor m \<longleftrightarrow> m dvd n"
2.603 +proof
2.604 +  assume A: "gcd m n = m div normalisation_factor m"
2.605 +  show "m dvd n"
2.606 +  proof (cases "m = 0")
2.607 +    assume [simp]: "m \<noteq> 0"
2.608 +    from A have B: "m = gcd m n * normalisation_factor m"
2.609 +      by (simp add: unit_eq_div2)
2.610 +    show ?thesis by (subst B, simp add: mult_unit_dvd_iff)
2.611 +  qed (insert A, simp)
2.612 +next
2.613 +  assume "m dvd n"
2.614 +  then show "gcd m n = m div normalisation_factor m" by (rule gcd_proj1_if_dvd)
2.615 +qed
2.616 +
2.617 +lemma gcd_proj2_iff: "gcd m n = n div normalisation_factor n \<longleftrightarrow> n dvd m"
2.618 +  by (subst gcd.commute, simp add: gcd_proj1_iff)
2.619 +
2.620 +lemma gcd_mod1 [simp]:
2.621 +  "gcd (x mod y) y = gcd x y"
2.622 +  by (rule gcdI, metis dvd_mod_iff gcd_dvd1 gcd_dvd2, simp_all add: gcd_greatest dvd_mod_iff)
2.623 +
2.624 +lemma gcd_mod2 [simp]:
2.625 +  "gcd x (y mod x) = gcd x y"
2.626 +  by (rule gcdI, simp, metis dvd_mod_iff gcd_dvd1 gcd_dvd2, simp_all add: gcd_greatest dvd_mod_iff)
2.627 +
2.628 +lemma normalisation_factor_dvd' [simp]:
2.629 +  "normalisation_factor x dvd x"
2.630 +  by (cases "x = 0", simp_all)
2.631 +
2.632 +lemma gcd_mult_distrib':
2.633 +  "k div normalisation_factor k * gcd x y = gcd (k*x) (k*y)"
2.634 +proof (induct x y rule: gcd_eucl.induct)
2.635 +  case (1 x y)
2.636 +  show ?case
2.637 +  proof (cases "y = 0")
2.638 +    case True
2.639 +    then show ?thesis by (simp add: normalisation_factor_mult gcd_0 algebra_simps div_mult_div_if_dvd)
2.640 +  next
2.641 +    case False
2.642 +    hence "k div normalisation_factor k * gcd x y =  gcd (k * y) (k * (x mod y))"
2.643 +      using 1 by (subst gcd_red, simp)
2.644 +    also have "... = gcd (k * x) (k * y)"
2.645 +      by (simp add: mult_mod_right gcd.commute)
2.646 +    finally show ?thesis .
2.647 +  qed
2.648 +qed
2.649 +
2.650 +lemma gcd_mult_distrib:
2.651 +  "k * gcd x y = gcd (k*x) (k*y) * normalisation_factor k"
2.652 +proof-
2.653 +  let ?nf = "normalisation_factor"
2.654 +  from gcd_mult_distrib'
2.655 +    have "gcd (k*x) (k*y) = k div ?nf k * gcd x y" ..
2.656 +  also have "... = k * gcd x y div ?nf k"
2.657 +    by (metis dvd_div_mult dvd_eq_mod_eq_0 mod_0 normalisation_factor_dvd)
2.658 +  finally show ?thesis
2.659 +    by (simp add: ac_simps dvd_mult_div_cancel)
2.660 +qed
2.661 +
2.662 +lemma euclidean_size_gcd_le1 [simp]:
2.663 +  assumes "a \<noteq> 0"
2.664 +  shows "euclidean_size (gcd a b) \<le> euclidean_size a"
2.665 +proof -
2.666 +   have "gcd a b dvd a" by (rule gcd_dvd1)
2.667 +   then obtain c where A: "a = gcd a b * c" unfolding dvd_def by blast
2.668 +   with a \<noteq> 0 show ?thesis by (subst (2) A, intro size_mult_mono) auto
2.669 +qed
2.670 +
2.671 +lemma euclidean_size_gcd_le2 [simp]:
2.672 +  "b \<noteq> 0 \<Longrightarrow> euclidean_size (gcd a b) \<le> euclidean_size b"
2.673 +  by (subst gcd.commute, rule euclidean_size_gcd_le1)
2.674 +
2.675 +lemma euclidean_size_gcd_less1:
2.676 +  assumes "a \<noteq> 0" and "\<not>a dvd b"
2.677 +  shows "euclidean_size (gcd a b) < euclidean_size a"
2.678 +proof (rule ccontr)
2.679 +  assume "\<not>euclidean_size (gcd a b) < euclidean_size a"
2.680 +  with a \<noteq> 0 have "euclidean_size (gcd a b) = euclidean_size a"
2.681 +    by (intro le_antisym, simp_all)
2.682 +  with assms have "a dvd gcd a b" by (auto intro: dvd_euclidean_size_eq_imp_dvd)
2.683 +  hence "a dvd b" using dvd_gcd_D2 by blast
2.684 +  with \<not>a dvd b show False by contradiction
2.685 +qed
2.686 +
2.687 +lemma euclidean_size_gcd_less2:
2.688 +  assumes "b \<noteq> 0" and "\<not>b dvd a"
2.689 +  shows "euclidean_size (gcd a b) < euclidean_size b"
2.690 +  using assms by (subst gcd.commute, rule euclidean_size_gcd_less1)
2.691 +
2.692 +lemma gcd_mult_unit1: "is_unit a \<Longrightarrow> gcd (x*a) y = gcd x y"
2.693 +  apply (rule gcdI)
2.694 +  apply (rule dvd_trans, rule gcd_dvd1, simp add: unit_simps)
2.695 +  apply (rule gcd_dvd2)
2.696 +  apply (rule gcd_greatest, simp add: unit_simps, assumption)
2.697 +  apply (subst normalisation_factor_gcd, simp add: gcd_0)
2.698 +  done
2.699 +
2.700 +lemma gcd_mult_unit2: "is_unit a \<Longrightarrow> gcd x (y*a) = gcd x y"
2.701 +  by (subst gcd.commute, subst gcd_mult_unit1, assumption, rule gcd.commute)
2.702 +
2.703 +lemma gcd_div_unit1: "is_unit a \<Longrightarrow> gcd (x div a) y = gcd x y"
2.704 +  by (simp add: unit_ring_inv gcd_mult_unit1)
2.705 +
2.706 +lemma gcd_div_unit2: "is_unit a \<Longrightarrow> gcd x (y div a) = gcd x y"
2.707 +  by (simp add: unit_ring_inv gcd_mult_unit2)
2.708 +
2.709 +lemma gcd_idem: "gcd x x = x div normalisation_factor x"
2.710 +  by (cases "x = 0") (simp add: gcd_0_left, rule sym, rule gcdI, simp_all)
2.711 +
2.712 +lemma gcd_right_idem: "gcd (gcd p q) q = gcd p q"
2.713 +  apply (rule gcdI)
2.714 +  apply (simp add: ac_simps)
2.715 +  apply (rule gcd_dvd2)
2.716 +  apply (rule gcd_greatest, erule (1) gcd_greatest, assumption)
2.717 +  apply (simp add: gcd_zero)
2.718 +  done
2.719 +
2.720 +lemma gcd_left_idem: "gcd p (gcd p q) = gcd p q"
2.721 +  apply (rule gcdI)
2.722 +  apply simp
2.723 +  apply (rule dvd_trans, rule gcd_dvd2, rule gcd_dvd2)
2.724 +  apply (rule gcd_greatest, assumption, erule gcd_greatest, assumption)
2.725 +  apply (simp add: gcd_zero)
2.726 +  done
2.727 +
2.728 +lemma comp_fun_idem_gcd: "comp_fun_idem gcd"
2.729 +proof
2.730 +  fix a b show "gcd a \<circ> gcd b = gcd b \<circ> gcd a"
2.731 +    by (simp add: fun_eq_iff ac_simps)
2.732 +next
2.733 +  fix a show "gcd a \<circ> gcd a = gcd a"
2.734 +    by (simp add: fun_eq_iff gcd_left_idem)
2.735 +qed
2.736 +
2.737 +lemma coprime_dvd_mult:
2.738 +  assumes "gcd k n = 1" and "k dvd m * n"
2.739 +  shows "k dvd m"
2.740 +proof -
2.741 +  let ?nf = "normalisation_factor"
2.742 +  from assms gcd_mult_distrib [of m k n]
2.743 +    have A: "m = gcd (m * k) (m * n) * ?nf m" by simp
2.744 +  from k dvd m * n show ?thesis by (subst A, simp_all add: gcd_greatest)
2.745 +qed
2.746 +
2.747 +lemma coprime_dvd_mult_iff:
2.748 +  "gcd k n = 1 \<Longrightarrow> (k dvd m * n) = (k dvd m)"
2.749 +  by (rule, rule coprime_dvd_mult, simp_all)
2.750 +
2.751 +lemma gcd_dvd_antisym:
2.752 +  "gcd a b dvd gcd c d \<Longrightarrow> gcd c d dvd gcd a b \<Longrightarrow> gcd a b = gcd c d"
2.753 +proof (rule gcdI)
2.754 +  assume A: "gcd a b dvd gcd c d" and B: "gcd c d dvd gcd a b"
2.755 +  have "gcd c d dvd c" by simp
2.756 +  with A show "gcd a b dvd c" by (rule dvd_trans)
2.757 +  have "gcd c d dvd d" by simp
2.758 +  with A show "gcd a b dvd d" by (rule dvd_trans)
2.759 +  show "normalisation_factor (gcd a b) = (if gcd a b = 0 then 0 else 1)"
2.760 +    by (simp add: gcd_zero)
2.761 +  fix l assume "l dvd c" and "l dvd d"
2.762 +  hence "l dvd gcd c d" by (rule gcd_greatest)
2.763 +  from this and B show "l dvd gcd a b" by (rule dvd_trans)
2.764 +qed
2.765 +
2.766 +lemma gcd_mult_cancel:
2.767 +  assumes "gcd k n = 1"
2.768 +  shows "gcd (k * m) n = gcd m n"
2.769 +proof (rule gcd_dvd_antisym)
2.770 +  have "gcd (gcd (k * m) n) k = gcd (gcd k n) (k * m)" by (simp add: ac_simps)
2.771 +  also note gcd k n = 1
2.772 +  finally have "gcd (gcd (k * m) n) k = 1" by simp
2.773 +  hence "gcd (k * m) n dvd m" by (rule coprime_dvd_mult, simp add: ac_simps)
2.774 +  moreover have "gcd (k * m) n dvd n" by simp
2.775 +  ultimately show "gcd (k * m) n dvd gcd m n" by (rule gcd_greatest)
2.776 +  have "gcd m n dvd (k * m)" and "gcd m n dvd n" by simp_all
2.777 +  then show "gcd m n dvd gcd (k * m) n" by (rule gcd_greatest)
2.778 +qed
2.779 +
2.780 +lemma coprime_crossproduct:
2.781 +  assumes [simp]: "gcd a d = 1" "gcd b c = 1"
2.782 +  shows "associated (a * c) (b * d) \<longleftrightarrow> associated a b \<and> associated c d" (is "?lhs \<longleftrightarrow> ?rhs")
2.783 +proof
2.784 +  assume ?rhs then show ?lhs unfolding associated_def by (fast intro: mult_dvd_mono)
2.785 +next
2.786 +  assume ?lhs
2.787 +  from ?lhs have "a dvd b * d" unfolding associated_def by (metis dvd_mult_left)
2.788 +  hence "a dvd b" by (simp add: coprime_dvd_mult_iff)
2.789 +  moreover from ?lhs have "b dvd a * c" unfolding associated_def by (metis dvd_mult_left)
2.790 +  hence "b dvd a" by (simp add: coprime_dvd_mult_iff)
2.791 +  moreover from ?lhs have "c dvd d * b"
2.792 +    unfolding associated_def by (metis dvd_mult_right ac_simps)
2.793 +  hence "c dvd d" by (simp add: coprime_dvd_mult_iff gcd.commute)
2.794 +  moreover from ?lhs have "d dvd c * a"
2.795 +    unfolding associated_def by (metis dvd_mult_right ac_simps)
2.796 +  hence "d dvd c" by (simp add: coprime_dvd_mult_iff gcd.commute)
2.797 +  ultimately show ?rhs unfolding associated_def by simp
2.798 +qed
2.799 +
2.800 +lemma gcd_add1 [simp]:
2.801 +  "gcd (m + n) n = gcd m n"
2.802 +  by (cases "n = 0", simp_all add: gcd_non_0)
2.803 +
2.804 +lemma gcd_add2 [simp]:
2.805 +  "gcd m (m + n) = gcd m n"
2.806 +  using gcd_add1 [of n m] by (simp add: ac_simps)
2.807 +
2.808 +lemma gcd_add_mult: "gcd m (k * m + n) = gcd m n"
2.809 +  by (subst gcd.commute, subst gcd_red, simp)
2.810 +
2.811 +lemma coprimeI: "(\<And>l. \<lbrakk>l dvd x; l dvd y\<rbrakk> \<Longrightarrow> l dvd 1) \<Longrightarrow> gcd x y = 1"
2.812 +  by (rule sym, rule gcdI, simp_all)
2.813 +
2.814 +lemma coprime: "gcd a b = 1 \<longleftrightarrow> (\<forall>d. d dvd a \<and> d dvd b \<longleftrightarrow> is_unit d)"
2.815 +  by (auto simp: is_unit_def intro: coprimeI gcd_greatest dvd_gcd_D1 dvd_gcd_D2)
2.816 +
2.817 +lemma div_gcd_coprime:
2.818 +  assumes nz: "a \<noteq> 0 \<or> b \<noteq> 0"
2.819 +  defines [simp]: "d \<equiv> gcd a b"
2.820 +  defines [simp]: "a' \<equiv> a div d" and [simp]: "b' \<equiv> b div d"
2.821 +  shows "gcd a' b' = 1"
2.822 +proof (rule coprimeI)
2.823 +  fix l assume "l dvd a'" "l dvd b'"
2.824 +  then obtain s t where "a' = l * s" "b' = l * t" unfolding dvd_def by blast
2.825 +  moreover have "a = a' * d" "b = b' * d" by (simp_all add: dvd_div_mult_self)
2.826 +  ultimately have "a = (l * d) * s" "b = (l * d) * t"
2.827 +    by (metis ac_simps)+
2.828 +  hence "l*d dvd a" and "l*d dvd b" by (simp_all only: dvd_triv_left)
2.829 +  hence "l*d dvd d" by (simp add: gcd_greatest)
2.830 +  then obtain u where "u * l * d = d" unfolding dvd_def
2.831 +    by (metis ac_simps mult_assoc)
2.832 +  moreover from nz have "d \<noteq> 0" by (simp add: gcd_zero)
2.833 +  ultimately have "u * l = 1"
2.834 +    by (metis div_mult_self1_is_id div_self ac_simps)
2.835 +  then show "l dvd 1" by force
2.836 +qed
2.837 +
2.838 +lemma coprime_mult:
2.839 +  assumes da: "gcd d a = 1" and db: "gcd d b = 1"
2.840 +  shows "gcd d (a * b) = 1"
2.841 +  apply (subst gcd.commute)
2.842 +  using da apply (subst gcd_mult_cancel)
2.843 +  apply (subst gcd.commute, assumption)
2.844 +  apply (subst gcd.commute, rule db)
2.845 +  done
2.846 +
2.847 +lemma coprime_lmult:
2.848 +  assumes dab: "gcd d (a * b) = 1"
2.849 +  shows "gcd d a = 1"
2.850 +proof (rule coprimeI)
2.851 +  fix l assume "l dvd d" and "l dvd a"
2.852 +  hence "l dvd a * b" by simp
2.853 +  with l dvd d and dab show "l dvd 1" by (auto intro: gcd_greatest)
2.854 +qed
2.855 +
2.856 +lemma coprime_rmult:
2.857 +  assumes dab: "gcd d (a * b) = 1"
2.858 +  shows "gcd d b = 1"
2.859 +proof (rule coprimeI)
2.860 +  fix l assume "l dvd d" and "l dvd b"
2.861 +  hence "l dvd a * b" by simp
2.862 +  with l dvd d and dab show "l dvd 1" by (auto intro: gcd_greatest)
2.863 +qed
2.864 +
2.865 +lemma coprime_mul_eq: "gcd d (a * b) = 1 \<longleftrightarrow> gcd d a = 1 \<and> gcd d b = 1"
2.866 +  using coprime_rmult[of d a b] coprime_lmult[of d a b] coprime_mult[of d a b] by blast
2.867 +
2.868 +lemma gcd_coprime:
2.869 +  assumes z: "gcd a b \<noteq> 0" and a: "a = a' * gcd a b" and b: "b = b' * gcd a b"
2.870 +  shows "gcd a' b' = 1"
2.871 +proof -
2.872 +  from z have "a \<noteq> 0 \<or> b \<noteq> 0" by (simp add: gcd_zero)
2.873 +  with div_gcd_coprime have "gcd (a div gcd a b) (b div gcd a b) = 1" .
2.874 +  also from assms have "a div gcd a b = a'" by (metis div_mult_self2_is_id)+
2.875 +  also from assms have "b div gcd a b = b'" by (metis div_mult_self2_is_id)+
2.876 +  finally show ?thesis .
2.877 +qed
2.878 +
2.879 +lemma coprime_power:
2.880 +  assumes "0 < n"
2.881 +  shows "gcd a (b ^ n) = 1 \<longleftrightarrow> gcd a b = 1"
2.882 +using assms proof (induct n)
2.883 +  case (Suc n) then show ?case
2.884 +    by (cases n) (simp_all add: coprime_mul_eq)
2.885 +qed simp
2.886 +
2.887 +lemma gcd_coprime_exists:
2.888 +  assumes nz: "gcd a b \<noteq> 0"
2.889 +  shows "\<exists>a' b'. a = a' * gcd a b \<and> b = b' * gcd a b \<and> gcd a' b' = 1"
2.890 +  apply (rule_tac x = "a div gcd a b" in exI)
2.891 +  apply (rule_tac x = "b div gcd a b" in exI)
2.892 +  apply (insert nz, auto simp add: dvd_div_mult gcd_0_left  gcd_zero intro: div_gcd_coprime)
2.893 +  done
2.894 +
2.895 +lemma coprime_exp:
2.896 +  "gcd d a = 1 \<Longrightarrow> gcd d (a^n) = 1"
2.897 +  by (induct n, simp_all add: coprime_mult)
2.898 +
2.899 +lemma coprime_exp2 [intro]:
2.900 +  "gcd a b = 1 \<Longrightarrow> gcd (a^n) (b^m) = 1"
2.901 +  apply (rule coprime_exp)
2.902 +  apply (subst gcd.commute)
2.903 +  apply (rule coprime_exp)
2.904 +  apply (subst gcd.commute)
2.905 +  apply assumption
2.906 +  done
2.907 +
2.908 +lemma gcd_exp:
2.909 +  "gcd (a^n) (b^n) = (gcd a b) ^ n"
2.910 +proof (cases "a = 0 \<and> b = 0")
2.911 +  assume "a = 0 \<and> b = 0"
2.912 +  then show ?thesis by (cases n, simp_all add: gcd_0_left)
2.913 +next
2.914 +  assume A: "\<not>(a = 0 \<and> b = 0)"
2.915 +  hence "1 = gcd ((a div gcd a b)^n) ((b div gcd a b)^n)"
2.916 +    using div_gcd_coprime by (subst sym, auto simp: div_gcd_coprime)
2.917 +  hence "(gcd a b) ^ n = (gcd a b) ^ n * ..." by simp
2.918 +  also note gcd_mult_distrib
2.919 +  also have "normalisation_factor ((gcd a b)^n) = 1"
2.920 +    by (simp add: normalisation_factor_pow A)
2.921 +  also have "(gcd a b)^n * (a div gcd a b)^n = a^n"
2.922 +    by (subst ac_simps, subst div_power, simp, rule dvd_div_mult_self, rule dvd_power_same, simp)
2.923 +  also have "(gcd a b)^n * (b div gcd a b)^n = b^n"
2.924 +    by (subst ac_simps, subst div_power, simp, rule dvd_div_mult_self, rule dvd_power_same, simp)
2.925 +  finally show ?thesis by simp
2.926 +qed
2.927 +
2.928 +lemma coprime_common_divisor:
2.929 +  "gcd a b = 1 \<Longrightarrow> x dvd a \<Longrightarrow> x dvd b \<Longrightarrow> is_unit x"
2.930 +  apply (subgoal_tac "x dvd gcd a b")
2.931 +  apply (simp add: is_unit_def)
2.932 +  apply (erule (1) gcd_greatest)
2.933 +  done
2.934 +
2.935 +lemma division_decomp:
2.936 +  assumes dc: "a dvd b * c"
2.937 +  shows "\<exists>b' c'. a = b' * c' \<and> b' dvd b \<and> c' dvd c"
2.938 +proof (cases "gcd a b = 0")
2.939 +  assume "gcd a b = 0"
2.940 +  hence "a = 0 \<and> b = 0" by (simp add: gcd_zero)
2.941 +  hence "a = 0 * c \<and> 0 dvd b \<and> c dvd c" by simp
2.942 +  then show ?thesis by blast
2.943 +next
2.944 +  let ?d = "gcd a b"
2.945 +  assume "?d \<noteq> 0"
2.946 +  from gcd_coprime_exists[OF this]
2.947 +    obtain a' b' where ab': "a = a' * ?d" "b = b' * ?d" "gcd a' b' = 1"
2.948 +    by blast
2.949 +  from ab'(1) have "a' dvd a" unfolding dvd_def by blast
2.950 +  with dc have "a' dvd b*c" using dvd_trans[of a' a "b*c"] by simp
2.951 +  from dc ab'(1,2) have "a'*?d dvd (b'*?d) * c" by simp
2.952 +  hence "?d * a' dvd ?d * (b' * c)" by (simp add: mult_ac)
2.953 +  with ?d \<noteq> 0 have "a' dvd b' * c" by (rule dvd_mult_cancel_left)
2.954 +  with coprime_dvd_mult[OF ab'(3)]
2.955 +    have "a' dvd c" by (subst (asm) ac_simps, blast)
2.956 +  with ab'(1) have "a = ?d * a' \<and> ?d dvd b \<and> a' dvd c" by (simp add: mult_ac)
2.957 +  then show ?thesis by blast
2.958 +qed
2.959 +
2.960 +lemma pow_divides_pow:
2.961 +  assumes ab: "a ^ n dvd b ^ n" and n: "n \<noteq> 0"
2.962 +  shows "a dvd b"
2.963 +proof (cases "gcd a b = 0")
2.964 +  assume "gcd a b = 0"
2.965 +  then show ?thesis by (simp add: gcd_zero)
2.966 +next
2.967 +  let ?d = "gcd a b"
2.968 +  assume "?d \<noteq> 0"
2.969 +  from n obtain m where m: "n = Suc m" by (cases n, simp_all)
2.970 +  from ?d \<noteq> 0 have zn: "?d ^ n \<noteq> 0" by (rule nonzero_pow_nonzero)
2.971 +  from gcd_coprime_exists[OF ?d \<noteq> 0]
2.972 +    obtain a' b' where ab': "a = a' * ?d" "b = b' * ?d" "gcd a' b' = 1"
2.973 +    by blast
2.974 +  from ab have "(a' * ?d) ^ n dvd (b' * ?d) ^ n"
2.975 +    by (simp add: ab'(1,2)[symmetric])
2.976 +  hence "?d^n * a'^n dvd ?d^n * b'^n"
2.977 +    by (simp only: power_mult_distrib ac_simps)
2.978 +  with zn have "a'^n dvd b'^n" by (rule dvd_mult_cancel_left)
2.979 +  hence "a' dvd b'^n" using dvd_trans[of a' "a'^n" "b'^n"] by (simp add: m)
2.980 +  hence "a' dvd b'^m * b'" by (simp add: m ac_simps)
2.981 +  with coprime_dvd_mult[OF coprime_exp[OF ab'(3), of m]]
2.982 +    have "a' dvd b'" by (subst (asm) ac_simps, blast)
2.983 +  hence "a'*?d dvd b'*?d" by (rule mult_dvd_mono, simp)
2.984 +  with ab'(1,2) show ?thesis by simp
2.985 +qed
2.986 +
2.987 +lemma pow_divides_eq [simp]:
2.988 +  "n \<noteq> 0 \<Longrightarrow> a ^ n dvd b ^ n \<longleftrightarrow> a dvd b"
2.989 +  by (auto intro: pow_divides_pow dvd_power_same)
2.990 +
2.991 +lemma divides_mult:
2.992 +  assumes mr: "m dvd r" and nr: "n dvd r" and mn: "gcd m n = 1"
2.993 +  shows "m * n dvd r"
2.994 +proof -
2.995 +  from mr nr obtain m' n' where m': "r = m*m'" and n': "r = n*n'"
2.996 +    unfolding dvd_def by blast
2.997 +  from mr n' have "m dvd n'*n" by (simp add: ac_simps)
2.998 +  hence "m dvd n'" using coprime_dvd_mult_iff[OF mn] by simp
2.999 +  then obtain k where k: "n' = m*k" unfolding dvd_def by blast
2.1000 +  with n' have "r = m * n * k" by (simp add: mult_ac)
2.1001 +  then show ?thesis unfolding dvd_def by blast
2.1002 +qed
2.1003 +
2.1004 +lemma coprime_plus_one [simp]: "gcd (n + 1) n = 1"
2.1005 +  by (subst add_commute, simp)
2.1006 +
2.1007 +lemma setprod_coprime [rule_format]:
2.1008 +  "(\<forall>i\<in>A. gcd (f i) x = 1) \<longrightarrow> gcd (\<Prod>i\<in>A. f i) x = 1"
2.1009 +  apply (cases "finite A")
2.1010 +  apply (induct set: finite)
2.1011 +  apply (auto simp add: gcd_mult_cancel)
2.1012 +  done
2.1013 +
2.1014 +lemma coprime_divisors:
2.1015 +  assumes "d dvd a" "e dvd b" "gcd a b = 1"
2.1016 +  shows "gcd d e = 1"
2.1017 +proof -
2.1018 +  from assms obtain k l where "a = d * k" "b = e * l"
2.1019 +    unfolding dvd_def by blast
2.1020 +  with assms have "gcd (d * k) (e * l) = 1" by simp
2.1021 +  hence "gcd (d * k) e = 1" by (rule coprime_lmult)
2.1022 +  also have "gcd (d * k) e = gcd e (d * k)" by (simp add: ac_simps)
2.1023 +  finally have "gcd e d = 1" by (rule coprime_lmult)
2.1024 +  then show ?thesis by (simp add: ac_simps)
2.1025 +qed
2.1026 +
2.1027 +lemma invertible_coprime:
2.1028 +  "x * y mod m = 1 \<Longrightarrow> gcd x m = 1"
2.1029 +  by (metis coprime_lmult gcd_1 ac_simps gcd_red)
2.1030 +
2.1031 +lemma lcm_gcd:
2.1032 +  "lcm a b = a * b div (gcd a b * normalisation_factor (a*b))"
2.1033 +  by (simp only: lcm_lcm_eucl gcd_gcd_eucl lcm_eucl_def)
2.1034 +
2.1035 +lemma lcm_gcd_prod:
2.1036 +  "lcm a b * gcd a b = a * b div normalisation_factor (a*b)"
2.1037 +proof (cases "a * b = 0")
2.1038 +  let ?nf = normalisation_factor
2.1039 +  assume "a * b \<noteq> 0"
2.1040 +  hence "gcd a b \<noteq> 0" by (auto simp add: gcd_zero)
2.1041 +  from lcm_gcd have "lcm a b * gcd a b = gcd a b * (a * b div (?nf (a*b) * gcd a b))"
2.1042 +    by (simp add: mult_ac)
2.1043 +  also from a * b \<noteq> 0 have "... = a * b div ?nf (a*b)"
2.1044 +    by (simp_all add: unit_ring_inv'1 dvd_mult_div_cancel unit_ring_inv)
2.1045 +  finally show ?thesis .
2.1046 +qed (simp add: lcm_gcd)
2.1047 +
2.1048 +lemma lcm_dvd1 [iff]:
2.1049 +  "x dvd lcm x y"
2.1050 +proof (cases "x*y = 0")
2.1051 +  assume "x * y \<noteq> 0"
2.1052 +  hence "gcd x y \<noteq> 0" by (auto simp: gcd_zero)
2.1053 +  let ?c = "ring_inv (normalisation_factor (x*y))"
2.1054 +  from x * y \<noteq> 0 have [simp]: "is_unit (normalisation_factor (x*y))" by simp
2.1055 +  from lcm_gcd_prod[of x y] have "lcm x y * gcd x y = x * ?c * y"
2.1056 +    by (simp add: mult_ac unit_ring_inv)
2.1057 +  hence "lcm x y * gcd x y div gcd x y = x * ?c * y div gcd x y" by simp
2.1058 +  with gcd x y \<noteq> 0 have "lcm x y = x * ?c * y div gcd x y"
2.1059 +    by (subst (asm) div_mult_self2_is_id, simp_all)
2.1060 +  also have "... = x * (?c * y div gcd x y)"
2.1061 +    by (metis div_mult_swap gcd_dvd2 mult_assoc)
2.1062 +  finally show ?thesis by (rule dvdI)
2.1063 +qed (simp add: lcm_gcd)
2.1064 +
2.1065 +lemma lcm_least:
2.1066 +  "\<lbrakk>a dvd k; b dvd k\<rbrakk> \<Longrightarrow> lcm a b dvd k"
2.1067 +proof (cases "k = 0")
2.1068 +  let ?nf = normalisation_factor
2.1069 +  assume "k \<noteq> 0"
2.1070 +  hence "is_unit (?nf k)" by simp
2.1071 +  hence "?nf k \<noteq> 0" by (metis not_is_unit_0)
2.1072 +  assume A: "a dvd k" "b dvd k"
2.1073 +  hence "gcd a b \<noteq> 0" using k \<noteq> 0 by (auto simp add: gcd_zero)
2.1074 +  from A obtain r s where ar: "k = a * r" and bs: "k = b * s"
2.1075 +    unfolding dvd_def by blast
2.1076 +  with k \<noteq> 0 have "r * s \<noteq> 0"
2.1077 +    by (intro notI) (drule divisors_zero, elim disjE, simp_all)
2.1078 +  hence "is_unit (?nf (r * s))" by simp
2.1079 +  let ?c = "?nf k div ?nf (r*s)"
2.1080 +  from is_unit (?nf k) and is_unit (?nf (r * s)) have "is_unit ?c" by (rule unit_div)
2.1081 +  hence "?c \<noteq> 0" using not_is_unit_0 by fast
2.1082 +  from ar bs have "k * k * gcd s r = ?nf k * k * gcd (k * s) (k * r)"
2.1083 +    by (subst mult_assoc, subst gcd_mult_distrib[of k s r], simp only: ac_simps mult_assoc)
2.1084 +  also have "... = ?nf k * k * gcd ((r*s) * a) ((r*s) * b)"
2.1085 +    by (subst (3) k = a * r, subst (3) k = b * s, simp add: algebra_simps)
2.1086 +  also have "... = ?c * r*s * k * gcd a b" using r * s \<noteq> 0
2.1087 +    by (subst gcd_mult_distrib'[symmetric], simp add: algebra_simps unit_simps)
2.1088 +  finally have "(a*r) * (b*s) * gcd s r = ?c * k * r * s * gcd a b"
2.1089 +    by (subst ar[symmetric], subst bs[symmetric], simp add: mult_ac)
2.1090 +  hence "a * b * gcd s r * (r * s) = ?c * k * gcd a b * (r * s)"
2.1091 +    by (simp add: algebra_simps)
2.1092 +  hence "?c * k * gcd a b = a * b * gcd s r" using r * s \<noteq> 0
2.1093 +    by (metis div_mult_self2_is_id)
2.1094 +  also have "... = lcm a b * gcd a b * gcd s r * ?nf (a*b)"
2.1095 +    by (subst lcm_gcd_prod[of a b], metis gcd_mult_distrib gcd_mult_distrib')
2.1096 +  also have "... = lcm a b * gcd s r * ?nf (a*b) * gcd a b"
2.1097 +    by (simp add: algebra_simps)
2.1098 +  finally have "k * ?c = lcm a b * gcd s r * ?nf (a*b)" using gcd a b \<noteq> 0
2.1099 +    by (metis mult.commute div_mult_self2_is_id)
2.1100 +  hence "k = lcm a b * (gcd s r * ?nf (a*b)) div ?c" using ?c \<noteq> 0
2.1101 +    by (metis div_mult_self2_is_id mult_assoc)
2.1102 +  also have "... = lcm a b * (gcd s r * ?nf (a*b) div ?c)" using is_unit ?c
2.1103 +    by (simp add: unit_simps)
2.1104 +  finally show ?thesis by (rule dvdI)
2.1105 +qed simp
2.1106 +
2.1107 +lemma lcm_zero:
2.1108 +  "lcm a b = 0 \<longleftrightarrow> a = 0 \<or> b = 0"
2.1109 +proof -
2.1110 +  let ?nf = normalisation_factor
2.1111 +  {
2.1112 +    assume "a \<noteq> 0" "b \<noteq> 0"
2.1113 +    hence "a * b div ?nf (a * b) \<noteq> 0" by (simp add: no_zero_divisors)
2.1114 +    moreover from a \<noteq> 0 and b \<noteq> 0 have "gcd a b \<noteq> 0" by (simp add: gcd_zero)
2.1115 +    ultimately have "lcm a b \<noteq> 0" using lcm_gcd_prod[of a b] by (intro notI, simp)
2.1116 +  } moreover {
2.1117 +    assume "a = 0 \<or> b = 0"
2.1118 +    hence "lcm a b = 0" by (elim disjE, simp_all add: lcm_gcd)
2.1119 +  }
2.1120 +  ultimately show ?thesis by blast
2.1121 +qed
2.1122 +
2.1123 +lemmas lcm_0_iff = lcm_zero
2.1124 +
2.1125 +lemma gcd_lcm:
2.1126 +  assumes "lcm a b \<noteq> 0"
2.1127 +  shows "gcd a b = a * b div (lcm a b * normalisation_factor (a * b))"
2.1128 +proof-
2.1129 +  from assms have "gcd a b \<noteq> 0" by (simp add: gcd_zero lcm_zero)
2.1130 +  let ?c = "normalisation_factor (a*b)"
2.1131 +  from lcm a b \<noteq> 0 have "?c \<noteq> 0" by (intro notI, simp add: lcm_zero no_zero_divisors)
2.1132 +  hence "is_unit ?c" by simp
2.1133 +  from lcm_gcd_prod [of a b] have "gcd a b = a * b div ?c div lcm a b"
2.1134 +    by (subst (2) div_mult_self2_is_id[OF lcm a b \<noteq> 0, symmetric], simp add: mult_ac)
2.1135 +  also from is_unit ?c have "... = a * b div (?c * lcm a b)"
2.1136 +    by (simp only: unit_ring_inv'1 unit_ring_inv)
2.1137 +  finally show ?thesis by (simp only: ac_simps)
2.1138 +qed
2.1139 +
2.1140 +lemma normalisation_factor_lcm [simp]:
2.1141 +  "normalisation_factor (lcm a b) = (if a = 0 \<or> b = 0 then 0 else 1)"
2.1142 +proof (cases "a = 0 \<or> b = 0")
2.1143 +  case True then show ?thesis
2.1144 +    by (simp add: lcm_gcd) (metis div_0 ac_simps mult_zero_left normalisation_factor_0)
2.1145 +next
2.1146 +  case False
2.1147 +  let ?nf = normalisation_factor
2.1148 +  from lcm_gcd_prod[of a b]
2.1149 +    have "?nf (lcm a b) * ?nf (gcd a b) = ?nf (a*b) div ?nf (a*b)"
2.1150 +    by (metis div_by_0 div_self normalisation_correct normalisation_factor_0 normalisation_factor_mult)
2.1151 +  also have "... = (if a*b = 0 then 0 else 1)"
2.1152 +    by (cases "a*b = 0", simp, subst div_self, metis dvd_0_left normalisation_factor_dvd, simp)
2.1153 +  finally show ?thesis using False by (simp add: no_zero_divisors)
2.1154 +qed
2.1155 +
2.1156 +lemma lcm_dvd2 [iff]: "y dvd lcm x y"
2.1157 +  using lcm_dvd1 [of y x] by (simp add: lcm_gcd ac_simps)
2.1158 +
2.1159 +lemma lcmI:
2.1160 +  "\<lbrakk>x dvd k; y dvd k; \<And>l. x dvd l \<Longrightarrow> y dvd l \<Longrightarrow> k dvd l;
2.1161 +    normalisation_factor k = (if k = 0 then 0 else 1)\<rbrakk> \<Longrightarrow> k = lcm x y"
2.1162 +  by (intro normed_associated_imp_eq) (auto simp: associated_def intro: lcm_least)
2.1163 +
2.1164 +sublocale lcm!: abel_semigroup lcm
2.1165 +proof
2.1166 +  fix x y z
2.1167 +  show "lcm (lcm x y) z = lcm x (lcm y z)"
2.1168 +  proof (rule lcmI)
2.1169 +    have "x dvd lcm x y" and "lcm x y dvd lcm (lcm x y) z" by simp_all
2.1170 +    then show "x dvd lcm (lcm x y) z" by (rule dvd_trans)
2.1171 +
2.1172 +    have "y dvd lcm x y" and "lcm x y dvd lcm (lcm x y) z" by simp_all
2.1173 +    hence "y dvd lcm (lcm x y) z" by (rule dvd_trans)
2.1174 +    moreover have "z dvd lcm (lcm x y) z" by simp
2.1175 +    ultimately show "lcm y z dvd lcm (lcm x y) z" by (rule lcm_least)
2.1176 +
2.1177 +    fix l assume "x dvd l" and "lcm y z dvd l"
2.1178 +    have "y dvd lcm y z" by simp
2.1179 +    from this and lcm y z dvd l have "y dvd l" by (rule dvd_trans)
2.1180 +    have "z dvd lcm y z" by simp
2.1181 +    from this and lcm y z dvd l have "z dvd l" by (rule dvd_trans)
2.1182 +    from x dvd l and y dvd l have "lcm x y dvd l" by (rule lcm_least)
2.1183 +    from this and z dvd l show "lcm (lcm x y) z dvd l" by (rule lcm_least)
2.1184 +  qed (simp add: lcm_zero)
2.1185 +next
2.1186 +  fix x y
2.1187 +  show "lcm x y = lcm y x"
2.1188 +    by (simp add: lcm_gcd ac_simps)
2.1189 +qed
2.1190 +
2.1191 +lemma dvd_lcm_D1:
2.1192 +  "lcm m n dvd k \<Longrightarrow> m dvd k"
2.1193 +  by (rule dvd_trans, rule lcm_dvd1, assumption)
2.1194 +
2.1195 +lemma dvd_lcm_D2:
2.1196 +  "lcm m n dvd k \<Longrightarrow> n dvd k"
2.1197 +  by (rule dvd_trans, rule lcm_dvd2, assumption)
2.1198 +
2.1199 +lemma gcd_dvd_lcm [simp]:
2.1200 +  "gcd a b dvd lcm a b"
2.1201 +  by (metis dvd_trans gcd_dvd2 lcm_dvd2)
2.1202 +
2.1203 +lemma lcm_1_iff:
2.1204 +  "lcm a b = 1 \<longleftrightarrow> is_unit a \<and> is_unit b"
2.1205 +proof
2.1206 +  assume "lcm a b = 1"
2.1207 +  then show "is_unit a \<and> is_unit b" unfolding is_unit_def by auto
2.1208 +next
2.1209 +  assume "is_unit a \<and> is_unit b"
2.1210 +  hence "a dvd 1" and "b dvd 1" unfolding is_unit_def by simp_all
2.1211 +  hence "is_unit (lcm a b)" unfolding is_unit_def by (rule lcm_least)
2.1212 +  hence "lcm a b = normalisation_factor (lcm a b)"
2.1213 +    by (subst normalisation_factor_unit, simp_all)
2.1214 +  also have "\<dots> = 1" using is_unit a \<and> is_unit b by (auto simp add: is_unit_def)
2.1215 +  finally show "lcm a b = 1" .
2.1216 +qed
2.1217 +
2.1218 +lemma lcm_0_left [simp]:
2.1219 +  "lcm 0 x = 0"
2.1220 +  by (rule sym, rule lcmI, simp_all)
2.1221 +
2.1222 +lemma lcm_0 [simp]:
2.1223 +  "lcm x 0 = 0"
2.1224 +  by (rule sym, rule lcmI, simp_all)
2.1225 +
2.1226 +lemma lcm_unique:
2.1227 +  "a dvd d \<and> b dvd d \<and>
2.1228 +  normalisation_factor d = (if d = 0 then 0 else 1) \<and>
2.1229 +  (\<forall>e. a dvd e \<and> b dvd e \<longrightarrow> d dvd e) \<longleftrightarrow> d = lcm a b"
2.1230 +  by (rule, auto intro: lcmI simp: lcm_least lcm_zero)
2.1231 +
2.1232 +lemma dvd_lcm_I1 [simp]:
2.1233 +  "k dvd m \<Longrightarrow> k dvd lcm m n"
2.1234 +  by (metis lcm_dvd1 dvd_trans)
2.1235 +
2.1236 +lemma dvd_lcm_I2 [simp]:
2.1237 +  "k dvd n \<Longrightarrow> k dvd lcm m n"
2.1238 +  by (metis lcm_dvd2 dvd_trans)
2.1239 +
2.1240 +lemma lcm_1_left [simp]:
2.1241 +  "lcm 1 x = x div normalisation_factor x"
2.1242 +  by (cases "x = 0") (simp, rule sym, rule lcmI, simp_all)
2.1243 +
2.1244 +lemma lcm_1_right [simp]:
2.1245 +  "lcm x 1 = x div normalisation_factor x"
2.1246 +  by (simp add: ac_simps)
2.1247 +
2.1248 +lemma lcm_coprime:
2.1249 +  "gcd a b = 1 \<Longrightarrow> lcm a b = a * b div normalisation_factor (a*b)"
2.1250 +  by (subst lcm_gcd) simp
2.1251 +
2.1252 +lemma lcm_proj1_if_dvd:
2.1253 +  "y dvd x \<Longrightarrow> lcm x y = x div normalisation_factor x"
2.1254 +  by (cases "x = 0") (simp, rule sym, rule lcmI, simp_all)
2.1255 +
2.1256 +lemma lcm_proj2_if_dvd:
2.1257 +  "x dvd y \<Longrightarrow> lcm x y = y div normalisation_factor y"
2.1258 +  using lcm_proj1_if_dvd [of x y] by (simp add: ac_simps)
2.1259 +
2.1260 +lemma lcm_proj1_iff:
2.1261 +  "lcm m n = m div normalisation_factor m \<longleftrightarrow> n dvd m"
2.1262 +proof
2.1263 +  assume A: "lcm m n = m div normalisation_factor m"
2.1264 +  show "n dvd m"
2.1265 +  proof (cases "m = 0")
2.1266 +    assume [simp]: "m \<noteq> 0"
2.1267 +    from A have B: "m = lcm m n * normalisation_factor m"
2.1268 +      by (simp add: unit_eq_div2)
2.1269 +    show ?thesis by (subst B, simp)
2.1270 +  qed simp
2.1271 +next
2.1272 +  assume "n dvd m"
2.1273 +  then show "lcm m n = m div normalisation_factor m" by (rule lcm_proj1_if_dvd)
2.1274 +qed
2.1275 +
2.1276 +lemma lcm_proj2_iff:
2.1277 +  "lcm m n = n div normalisation_factor n \<longleftrightarrow> m dvd n"
2.1278 +  using lcm_proj1_iff [of n m] by (simp add: ac_simps)
2.1279 +
2.1280 +lemma euclidean_size_lcm_le1:
2.1281 +  assumes "a \<noteq> 0" and "b \<noteq> 0"
2.1282 +  shows "euclidean_size a \<le> euclidean_size (lcm a b)"
2.1283 +proof -
2.1284 +  have "a dvd lcm a b" by (rule lcm_dvd1)
2.1285 +  then obtain c where A: "lcm a b = a * c" unfolding dvd_def by blast
2.1286 +  with a \<noteq> 0 and b \<noteq> 0 have "c \<noteq> 0" by (auto simp: lcm_zero)
2.1287 +  then show ?thesis by (subst A, intro size_mult_mono)
2.1288 +qed
2.1289 +
2.1290 +lemma euclidean_size_lcm_le2:
2.1291 +  "a \<noteq> 0 \<Longrightarrow> b \<noteq> 0 \<Longrightarrow> euclidean_size b \<le> euclidean_size (lcm a b)"
2.1292 +  using euclidean_size_lcm_le1 [of b a] by (simp add: ac_simps)
2.1293 +
2.1294 +lemma euclidean_size_lcm_less1:
2.1295 +  assumes "b \<noteq> 0" and "\<not>b dvd a"
2.1296 +  shows "euclidean_size a < euclidean_size (lcm a b)"
2.1297 +proof (rule ccontr)
2.1298 +  from assms have "a \<noteq> 0" by auto
2.1299 +  assume "\<not>euclidean_size a < euclidean_size (lcm a b)"
2.1300 +  with a \<noteq> 0 and b \<noteq> 0 have "euclidean_size (lcm a b) = euclidean_size a"
2.1301 +    by (intro le_antisym, simp, intro euclidean_size_lcm_le1)
2.1302 +  with assms have "lcm a b dvd a"
2.1303 +    by (rule_tac dvd_euclidean_size_eq_imp_dvd) (auto simp: lcm_zero)
2.1304 +  hence "b dvd a" by (rule dvd_lcm_D2)
2.1305 +  with \<not>b dvd a show False by contradiction
2.1306 +qed
2.1307 +
2.1308 +lemma euclidean_size_lcm_less2:
2.1309 +  assumes "a \<noteq> 0" and "\<not>a dvd b"
2.1310 +  shows "euclidean_size b < euclidean_size (lcm a b)"
2.1311 +  using assms euclidean_size_lcm_less1 [of a b] by (simp add: ac_simps)
2.1312 +
2.1313 +lemma lcm_mult_unit1:
2.1314 +  "is_unit a \<Longrightarrow> lcm (x*a) y = lcm x y"
2.1315 +  apply (rule lcmI)
2.1316 +  apply (rule dvd_trans[of _ "x*a"], simp, rule lcm_dvd1)
2.1317 +  apply (rule lcm_dvd2)
2.1318 +  apply (rule lcm_least, simp add: unit_simps, assumption)
2.1319 +  apply (subst normalisation_factor_lcm, simp add: lcm_zero)
2.1320 +  done
2.1321 +
2.1322 +lemma lcm_mult_unit2:
2.1323 +  "is_unit a \<Longrightarrow> lcm x (y*a) = lcm x y"
2.1324 +  using lcm_mult_unit1 [of a y x] by (simp add: ac_simps)
2.1325 +
2.1326 +lemma lcm_div_unit1:
2.1327 +  "is_unit a \<Longrightarrow> lcm (x div a) y = lcm x y"
2.1328 +  by (simp add: unit_ring_inv lcm_mult_unit1)
2.1329 +
2.1330 +lemma lcm_div_unit2:
2.1331 +  "is_unit a \<Longrightarrow> lcm x (y div a) = lcm x y"
2.1332 +  by (simp add: unit_ring_inv lcm_mult_unit2)
2.1333 +
2.1334 +lemma lcm_left_idem:
2.1335 +  "lcm p (lcm p q) = lcm p q"
2.1336 +  apply (rule lcmI)
2.1337 +  apply simp
2.1338 +  apply (subst lcm.assoc [symmetric], rule lcm_dvd2)
2.1339 +  apply (rule lcm_least, assumption)
2.1340 +  apply (erule (1) lcm_least)
2.1341 +  apply (auto simp: lcm_zero)
2.1342 +  done
2.1343 +
2.1344 +lemma lcm_right_idem:
2.1345 +  "lcm (lcm p q) q = lcm p q"
2.1346 +  apply (rule lcmI)
2.1347 +  apply (subst lcm.assoc, rule lcm_dvd1)
2.1348 +  apply (rule lcm_dvd2)
2.1349 +  apply (rule lcm_least, erule (1) lcm_least, assumption)
2.1350 +  apply (auto simp: lcm_zero)
2.1351 +  done
2.1352 +
2.1353 +lemma comp_fun_idem_lcm: "comp_fun_idem lcm"
2.1354 +proof
2.1355 +  fix a b show "lcm a \<circ> lcm b = lcm b \<circ> lcm a"
2.1356 +    by (simp add: fun_eq_iff ac_simps)
2.1357 +next
2.1358 +  fix a show "lcm a \<circ> lcm a = lcm a" unfolding o_def
2.1359 +    by (intro ext, simp add: lcm_left_idem)
2.1360 +qed
2.1361 +
2.1362 +lemma dvd_Lcm [simp]: "x \<in> A \<Longrightarrow> x dvd Lcm A"
2.1363 +  and Lcm_dvd [simp]: "(\<forall>x\<in>A. x dvd l') \<Longrightarrow> Lcm A dvd l'"
2.1364 +  and normalisation_factor_Lcm [simp]:
2.1365 +          "normalisation_factor (Lcm A) = (if Lcm A = 0 then 0 else 1)"
2.1366 +proof -
2.1367 +  have "(\<forall>x\<in>A. x dvd Lcm A) \<and> (\<forall>l'. (\<forall>x\<in>A. x dvd l') \<longrightarrow> Lcm A dvd l') \<and>
2.1368 +    normalisation_factor (Lcm A) = (if Lcm A = 0 then 0 else 1)" (is ?thesis)
2.1369 +  proof (cases "\<exists>l. l \<noteq>  0 \<and> (\<forall>x\<in>A. x dvd l)")
2.1370 +    case False
2.1371 +    hence "Lcm A = 0" by (auto simp: Lcm_Lcm_eucl Lcm_eucl_def)
2.1372 +    with False show ?thesis by auto
2.1373 +  next
2.1374 +    case True
2.1375 +    then obtain l\<^sub>0 where l\<^sub>0_props: "l\<^sub>0 \<noteq> 0 \<and> (\<forall>x\<in>A. x dvd l\<^sub>0)" by blast
2.1376 +    def n \<equiv> "LEAST n. \<exists>l. l \<noteq> 0 \<and> (\<forall>x\<in>A. x dvd l) \<and> euclidean_size l = n"
2.1377 +    def l \<equiv> "SOME l. l \<noteq> 0 \<and> (\<forall>x\<in>A. x dvd l) \<and> euclidean_size l = n"
2.1378 +    have "\<exists>l. l \<noteq> 0 \<and> (\<forall>x\<in>A. x dvd l) \<and> euclidean_size l = n"
2.1379 +      apply (subst n_def)
2.1380 +      apply (rule LeastI[of _ "euclidean_size l\<^sub>0"])
2.1381 +      apply (rule exI[of _ l\<^sub>0])
2.1382 +      apply (simp add: l\<^sub>0_props)
2.1383 +      done
2.1384 +    from someI_ex[OF this] have "l \<noteq> 0" and "\<forall>x\<in>A. x dvd l" and "euclidean_size l = n"
2.1385 +      unfolding l_def by simp_all
2.1386 +    {
2.1387 +      fix l' assume "\<forall>x\<in>A. x dvd l'"
2.1388 +      with \<forall>x\<in>A. x dvd l have "\<forall>x\<in>A. x dvd gcd l l'" by (auto intro: gcd_greatest)
2.1389 +      moreover from l \<noteq> 0 have "gcd l l' \<noteq> 0" by (simp add: gcd_zero)
2.1390 +      ultimately have "\<exists>b. b \<noteq> 0 \<and> (\<forall>x\<in>A. x dvd b) \<and> euclidean_size b = euclidean_size (gcd l l')"
2.1391 +        by (intro exI[of _ "gcd l l'"], auto)
2.1392 +      hence "euclidean_size (gcd l l') \<ge> n" by (subst n_def) (rule Least_le)
2.1393 +      moreover have "euclidean_size (gcd l l') \<le> n"
2.1394 +      proof -
2.1395 +        have "gcd l l' dvd l" by simp
2.1396 +        then obtain a where "l = gcd l l' * a" unfolding dvd_def by blast
2.1397 +        with l \<noteq> 0 have "a \<noteq> 0" by auto
2.1398 +        hence "euclidean_size (gcd l l') \<le> euclidean_size (gcd l l' * a)"
2.1399 +          by (rule size_mult_mono)
2.1400 +        also have "gcd l l' * a = l" using l = gcd l l' * a ..
2.1401 +        also note euclidean_size l = n
2.1402 +        finally show "euclidean_size (gcd l l') \<le> n" .
2.1403 +      qed
2.1404 +      ultimately have "euclidean_size l = euclidean_size (gcd l l')"
2.1405 +        by (intro le_antisym, simp_all add: euclidean_size l = n)
2.1406 +      with l \<noteq> 0 have "l dvd gcd l l'" by (blast intro: dvd_euclidean_size_eq_imp_dvd)
2.1407 +      hence "l dvd l'" by (blast dest: dvd_gcd_D2)
2.1408 +    }
2.1409 +
2.1410 +    with (\<forall>x\<in>A. x dvd l) and normalisation_factor_is_unit[OF l \<noteq> 0] and l \<noteq> 0
2.1411 +      have "(\<forall>x\<in>A. x dvd l div normalisation_factor l) \<and>
2.1412 +        (\<forall>l'. (\<forall>x\<in>A. x dvd l') \<longrightarrow> l div normalisation_factor l dvd l') \<and>
2.1413 +        normalisation_factor (l div normalisation_factor l) =
2.1414 +        (if l div normalisation_factor l = 0 then 0 else 1)"
2.1415 +      by (auto simp: unit_simps)
2.1416 +    also from True have "l div normalisation_factor l = Lcm A"
2.1417 +      by (simp add: Lcm_Lcm_eucl Lcm_eucl_def Let_def n_def l_def)
2.1418 +    finally show ?thesis .
2.1419 +  qed
2.1420 +  note A = this
2.1421 +
2.1422 +  {fix x assume "x \<in> A" then show "x dvd Lcm A" using A by blast}
2.1423 +  {fix l' assume "\<forall>x\<in>A. x dvd l'" then show "Lcm A dvd l'" using A by blast}
2.1424 +  from A show "normalisation_factor (Lcm A) = (if Lcm A = 0 then 0 else 1)" by blast
2.1425 +qed
2.1426 +
2.1427 +lemma LcmI:
2.1428 +  "(\<And>x. x\<in>A \<Longrightarrow> x dvd l) \<Longrightarrow> (\<And>l'. (\<forall>x\<in>A. x dvd l') \<Longrightarrow> l dvd l') \<Longrightarrow>
2.1429 +      normalisation_factor l = (if l = 0 then 0 else 1) \<Longrightarrow> l = Lcm A"
2.1430 +  by (intro normed_associated_imp_eq)
2.1431 +    (auto intro: Lcm_dvd dvd_Lcm simp: associated_def)
2.1432 +
2.1433 +lemma Lcm_subset:
2.1434 +  "A \<subseteq> B \<Longrightarrow> Lcm A dvd Lcm B"
2.1435 +  by (blast intro: Lcm_dvd dvd_Lcm)
2.1436 +
2.1437 +lemma Lcm_Un:
2.1438 +  "Lcm (A \<union> B) = lcm (Lcm A) (Lcm B)"
2.1439 +  apply (rule lcmI)
2.1440 +  apply (blast intro: Lcm_subset)
2.1441 +  apply (blast intro: Lcm_subset)
2.1442 +  apply (intro Lcm_dvd ballI, elim UnE)
2.1443 +  apply (rule dvd_trans, erule dvd_Lcm, assumption)
2.1444 +  apply (rule dvd_trans, erule dvd_Lcm, assumption)
2.1445 +  apply simp
2.1446 +  done
2.1447 +
2.1448 +lemma Lcm_1_iff:
2.1449 +  "Lcm A = 1 \<longleftrightarrow> (\<forall>x\<in>A. is_unit x)"
2.1450 +proof
2.1451 +  assume "Lcm A = 1"
2.1452 +  then show "\<forall>x\<in>A. is_unit x" unfolding is_unit_def by auto
2.1453 +qed (rule LcmI [symmetric], auto)
2.1454 +
2.1455 +lemma Lcm_no_units:
2.1456 +  "Lcm A = Lcm (A - {x. is_unit x})"
2.1457 +proof -
2.1458 +  have "(A - {x. is_unit x}) \<union> {x\<in>A. is_unit x} = A" by blast
2.1459 +  hence "Lcm A = lcm (Lcm (A - {x. is_unit x})) (Lcm {x\<in>A. is_unit x})"
2.1460 +    by (simp add: Lcm_Un[symmetric])
2.1461 +  also have "Lcm {x\<in>A. is_unit x} = 1" by (simp add: Lcm_1_iff)
2.1462 +  finally show ?thesis by simp
2.1463 +qed
2.1464 +
2.1465 +lemma Lcm_empty [simp]:
2.1466 +  "Lcm {} = 1"
2.1467 +  by (simp add: Lcm_1_iff)
2.1468 +
2.1469 +lemma Lcm_eq_0 [simp]:
2.1470 +  "0 \<in> A \<Longrightarrow> Lcm A = 0"
2.1471 +  by (drule dvd_Lcm) simp
2.1472 +
2.1473 +lemma Lcm0_iff':
2.1474 +  "Lcm A = 0 \<longleftrightarrow> \<not>(\<exists>l. l \<noteq> 0 \<and> (\<forall>x\<in>A. x dvd l))"
2.1475 +proof
2.1476 +  assume "Lcm A = 0"
2.1477 +  show "\<not>(\<exists>l. l \<noteq> 0 \<and> (\<forall>x\<in>A. x dvd l))"
2.1478 +  proof
2.1479 +    assume ex: "\<exists>l. l \<noteq> 0 \<and> (\<forall>x\<in>A. x dvd l)"
2.1480 +    then obtain l\<^sub>0 where l\<^sub>0_props: "l\<^sub>0 \<noteq> 0 \<and> (\<forall>x\<in>A. x dvd l\<^sub>0)" by blast
2.1481 +    def n \<equiv> "LEAST n. \<exists>l. l \<noteq> 0 \<and> (\<forall>x\<in>A. x dvd l) \<and> euclidean_size l = n"
2.1482 +    def l \<equiv> "SOME l. l \<noteq> 0 \<and> (\<forall>x\<in>A. x dvd l) \<and> euclidean_size l = n"
2.1483 +    have "\<exists>l. l \<noteq> 0 \<and> (\<forall>x\<in>A. x dvd l) \<and> euclidean_size l = n"
2.1484 +      apply (subst n_def)
2.1485 +      apply (rule LeastI[of _ "euclidean_size l\<^sub>0"])
2.1486 +      apply (rule exI[of _ l\<^sub>0])
2.1487 +      apply (simp add: l\<^sub>0_props)
2.1488 +      done
2.1489 +    from someI_ex[OF this] have "l \<noteq> 0" unfolding l_def by simp_all
2.1490 +    hence "l div normalisation_factor l \<noteq> 0" by simp
2.1491 +    also from ex have "l div normalisation_factor l = Lcm A"
2.1492 +       by (simp only: Lcm_Lcm_eucl Lcm_eucl_def n_def l_def if_True Let_def)
2.1493 +    finally show False using Lcm A = 0 by contradiction
2.1494 +  qed
2.1495 +qed (simp only: Lcm_Lcm_eucl Lcm_eucl_def if_False)
2.1496 +
2.1497 +lemma Lcm0_iff [simp]:
2.1498 +  "finite A \<Longrightarrow> Lcm A = 0 \<longleftrightarrow> 0 \<in> A"
2.1499 +proof -
2.1500 +  assume "finite A"
2.1501 +  have "0 \<in> A \<Longrightarrow> Lcm A = 0"  by (intro dvd_0_left dvd_Lcm)
2.1502 +  moreover {
2.1503 +    assume "0 \<notin> A"
2.1504 +    hence "\<Prod>A \<noteq> 0"
2.1505 +      apply (induct rule: finite_induct[OF finite A])
2.1506 +      apply simp
2.1507 +      apply (subst setprod.insert, assumption, assumption)
2.1508 +      apply (rule no_zero_divisors)
2.1509 +      apply blast+
2.1510 +      done
2.1511 +    moreover from finite A have "\<forall>x\<in>A. x dvd \<Prod>A" by (intro ballI dvd_setprod)
2.1512 +    ultimately have "\<exists>l. l \<noteq> 0 \<and> (\<forall>x\<in>A. x dvd l)" by blast
2.1513 +    with Lcm0_iff' have "Lcm A \<noteq> 0" by simp
2.1514 +  }
2.1515 +  ultimately show "Lcm A = 0 \<longleftrightarrow> 0 \<in> A" by blast
2.1516 +qed
2.1517 +
2.1518 +lemma Lcm_no_multiple:
2.1519 +  "(\<forall>m. m \<noteq> 0 \<longrightarrow> (\<exists>x\<in>A. \<not>x dvd m)) \<Longrightarrow> Lcm A = 0"
2.1520 +proof -
2.1521 +  assume "\<forall>m. m \<noteq> 0 \<longrightarrow> (\<exists>x\<in>A. \<not>x dvd m)"
2.1522 +  hence "\<not>(\<exists>l. l \<noteq> 0 \<and> (\<forall>x\<in>A. x dvd l))" by blast
2.1523 +  then show "Lcm A = 0" by (simp only: Lcm_Lcm_eucl Lcm_eucl_def if_False)
2.1524 +qed
2.1525 +
2.1526 +lemma Lcm_insert [simp]:
2.1527 +  "Lcm (insert a A) = lcm a (Lcm A)"
2.1528 +proof (rule lcmI)
2.1529 +  fix l assume "a dvd l" and "Lcm A dvd l"
2.1530 +  hence "\<forall>x\<in>A. x dvd l" by (blast intro: dvd_trans dvd_Lcm)
2.1531 +  with a dvd l show "Lcm (insert a A) dvd l" by (force intro: Lcm_dvd)
2.1532 +qed (auto intro: Lcm_dvd dvd_Lcm)
2.1533 +
2.1534 +lemma Lcm_finite:
2.1535 +  assumes "finite A"
2.1536 +  shows "Lcm A = Finite_Set.fold lcm 1 A"
2.1537 +  by (induct rule: finite.induct[OF finite A])
2.1538 +    (simp_all add: comp_fun_idem.fold_insert_idem[OF comp_fun_idem_lcm])
2.1539 +
2.1540 +lemma Lcm_set [code, code_unfold]:
2.1541 +  "Lcm (set xs) = fold lcm xs 1"
2.1542 +  using comp_fun_idem.fold_set_fold[OF comp_fun_idem_lcm] Lcm_finite by (simp add: ac_simps)
2.1543 +
2.1544 +lemma Lcm_singleton [simp]:
2.1545 +  "Lcm {a} = a div normalisation_factor a"
2.1546 +  by simp
2.1547 +
2.1548 +lemma Lcm_2 [simp]:
2.1549 +  "Lcm {a,b} = lcm a b"
2.1550 +  by (simp only: Lcm_insert Lcm_empty lcm_1_right)
2.1551 +    (cases "b = 0", simp, rule lcm_div_unit2, simp)
2.1552 +
2.1553 +lemma Lcm_coprime:
2.1554 +  assumes "finite A" and "A \<noteq> {}"
2.1555 +  assumes "\<And>a b. a \<in> A \<Longrightarrow> b \<in> A \<Longrightarrow> a \<noteq> b \<Longrightarrow> gcd a b = 1"
2.1556 +  shows "Lcm A = \<Prod>A div normalisation_factor (\<Prod>A)"
2.1557 +using assms proof (induct rule: finite_ne_induct)
2.1558 +  case (insert a A)
2.1559 +  have "Lcm (insert a A) = lcm a (Lcm A)" by simp
2.1560 +  also from insert have "Lcm A = \<Prod>A div normalisation_factor (\<Prod>A)" by blast
2.1561 +  also have "lcm a \<dots> = lcm a (\<Prod>A)" by (cases "\<Prod>A = 0") (simp_all add: lcm_div_unit2)
2.1562 +  also from insert have "gcd a (\<Prod>A) = 1" by (subst gcd.commute, intro setprod_coprime) auto
2.1563 +  with insert have "lcm a (\<Prod>A) = \<Prod>(insert a A) div normalisation_factor (\<Prod>(insert a A))"
2.1564 +    by (simp add: lcm_coprime)
2.1565 +  finally show ?case .
2.1566 +qed simp
2.1567 +
2.1568 +lemma Lcm_coprime':
2.1569 +  "card A \<noteq> 0 \<Longrightarrow> (\<And>a b. a \<in> A \<Longrightarrow> b \<in> A \<Longrightarrow> a \<noteq> b \<Longrightarrow> gcd a b = 1)
2.1570 +    \<Longrightarrow> Lcm A = \<Prod>A div normalisation_factor (\<Prod>A)"
2.1571 +  by (rule Lcm_coprime) (simp_all add: card_eq_0_iff)
2.1572 +
2.1573 +lemma Gcd_Lcm:
2.1574 +  "Gcd A = Lcm {d. \<forall>x\<in>A. d dvd x}"
2.1575 +  by (simp add: Gcd_Gcd_eucl Lcm_Lcm_eucl Gcd_eucl_def)
2.1576 +
2.1577 +lemma Gcd_dvd [simp]: "x \<in> A \<Longrightarrow> Gcd A dvd x"
2.1578 +  and dvd_Gcd [simp]: "(\<forall>x\<in>A. g' dvd x) \<Longrightarrow> g' dvd Gcd A"
2.1579 +  and normalisation_factor_Gcd [simp]:
2.1580 +    "normalisation_factor (Gcd A) = (if Gcd A = 0 then 0 else 1)"
2.1581 +proof -
2.1582 +  fix x assume "x \<in> A"
2.1583 +  hence "Lcm {d. \<forall>x\<in>A. d dvd x} dvd x" by (intro Lcm_dvd) blast
2.1584 +  then show "Gcd A dvd x" by (simp add: Gcd_Lcm)
2.1585 +next
2.1586 +  fix g' assume "\<forall>x\<in>A. g' dvd x"
2.1587 +  hence "g' dvd Lcm {d. \<forall>x\<in>A. d dvd x}" by (intro dvd_Lcm) blast
2.1588 +  then show "g' dvd Gcd A" by (simp add: Gcd_Lcm)
2.1589 +next
2.1590 +  show "normalisation_factor (Gcd A) = (if Gcd A = 0 then 0 else 1)"
2.1591 +    by (simp add: Gcd_Lcm normalisation_factor_Lcm)
2.1592 +qed
2.1593 +
2.1594 +lemma GcdI:
2.1595 +  "(\<And>x. x\<in>A \<Longrightarrow> l dvd x) \<Longrightarrow> (\<And>l'. (\<forall>x\<in>A. l' dvd x) \<Longrightarrow> l' dvd l) \<Longrightarrow>
2.1596 +    normalisation_factor l = (if l = 0 then 0 else 1) \<Longrightarrow> l = Gcd A"
2.1597 +  by (intro normed_associated_imp_eq)
2.1598 +    (auto intro: Gcd_dvd dvd_Gcd simp: associated_def)
2.1599 +
2.1600 +lemma Lcm_Gcd:
2.1601 +  "Lcm A = Gcd {m. \<forall>x\<in>A. x dvd m}"
2.1602 +  by (rule LcmI[symmetric]) (auto intro: dvd_Gcd Gcd_dvd)
2.1603 +
2.1604 +lemma Gcd_0_iff:
2.1605 +  "Gcd A = 0 \<longleftrightarrow> A \<subseteq> {0}"
2.1606 +  apply (rule iffI)
2.1607 +  apply (rule subsetI, drule Gcd_dvd, simp)
2.1608 +  apply (auto intro: GcdI[symmetric])
2.1609 +  done
2.1610 +
2.1611 +lemma Gcd_empty [simp]:
2.1612 +  "Gcd {} = 0"
2.1613 +  by (simp add: Gcd_0_iff)
2.1614 +
2.1615 +lemma Gcd_1:
2.1616 +  "1 \<in> A \<Longrightarrow> Gcd A = 1"
2.1617 +  by (intro GcdI[symmetric]) (auto intro: Gcd_dvd dvd_Gcd)
2.1618 +
2.1619 +lemma Gcd_insert [simp]:
2.1620 +  "Gcd (insert a A) = gcd a (Gcd A)"
2.1621 +proof (rule gcdI)
2.1622 +  fix l assume "l dvd a" and "l dvd Gcd A"
2.1623 +  hence "\<forall>x\<in>A. l dvd x" by (blast intro: dvd_trans Gcd_dvd)
2.1624 +  with l dvd a show "l dvd Gcd (insert a A)" by (force intro: Gcd_dvd)
2.1625 +qed (auto intro: Gcd_dvd dvd_Gcd simp: normalisation_factor_Gcd)
2.1626 +
2.1627 +lemma Gcd_finite:
2.1628 +  assumes "finite A"
2.1629 +  shows "Gcd A = Finite_Set.fold gcd 0 A"
2.1630 +  by (induct rule: finite.induct[OF finite A])
2.1631 +    (simp_all add: comp_fun_idem.fold_insert_idem[OF comp_fun_idem_gcd])
2.1632 +
2.1633 +lemma Gcd_set [code, code_unfold]:
2.1634 +  "Gcd (set xs) = fold gcd xs 0"
2.1635 +  using comp_fun_idem.fold_set_fold[OF comp_fun_idem_gcd] Gcd_finite by (simp add: ac_simps)
2.1636 +
2.1637 +lemma Gcd_singleton [simp]: "Gcd {a} = a div normalisation_factor a"
2.1638 +  by (simp add: gcd_0)
2.1639 +
2.1640 +lemma Gcd_2 [simp]: "Gcd {a,b} = gcd a b"
2.1641 +  by (simp only: Gcd_insert Gcd_empty gcd_0) (cases "b = 0", simp, rule gcd_div_unit2, simp)
2.1642 +
2.1643 +end
2.1644 +
2.1645 +text {*
2.1646 +  A Euclidean ring is a Euclidean semiring with additive inverses. It provides a
2.1647 +  few more lemmas; in particular, Bezout's lemma holds for any Euclidean ring.
2.1648 +*}
2.1649 +
2.1650 +class euclidean_ring = euclidean_semiring + idom
2.1651 +
2.1652 +class euclidean_ring_gcd = euclidean_semiring_gcd + idom
2.1653 +begin
2.1654 +
2.1655 +subclass euclidean_ring ..
2.1656 +
2.1657 +lemma gcd_neg1 [simp]:
2.1658 +  "gcd (-x) y = gcd x y"
2.1659 +  by (rule sym, rule gcdI, simp_all add: gcd_greatest gcd_zero)
2.1660 +
2.1661 +lemma gcd_neg2 [simp]:
2.1662 +  "gcd x (-y) = gcd x y"
2.1663 +  by (rule sym, rule gcdI, simp_all add: gcd_greatest gcd_zero)
2.1664 +
2.1665 +lemma gcd_neg_numeral_1 [simp]:
2.1666 +  "gcd (- numeral n) x = gcd (numeral n) x"
2.1667 +  by (fact gcd_neg1)
2.1668 +
2.1669 +lemma gcd_neg_numeral_2 [simp]:
2.1670 +  "gcd x (- numeral n) = gcd x (numeral n)"
2.1671 +  by (fact gcd_neg2)
2.1672 +
2.1673 +lemma gcd_diff1: "gcd (m - n) n = gcd m n"
2.1674 +  by (subst diff_conv_add_uminus, subst gcd_neg2[symmetric],  subst gcd_add1, simp)
2.1675 +
2.1676 +lemma gcd_diff2: "gcd (n - m) n = gcd m n"
2.1677 +  by (subst gcd_neg1[symmetric], simp only: minus_diff_eq gcd_diff1)
2.1678 +
2.1679 +lemma coprime_minus_one [simp]: "gcd (n - 1) n = 1"
2.1680 +proof -
2.1681 +  have "gcd (n - 1) n = gcd n (n - 1)" by (fact gcd.commute)
2.1682 +  also have "\<dots> = gcd ((n - 1) + 1) (n - 1)" by simp
2.1683 +  also have "\<dots> = 1" by (rule coprime_plus_one)
2.1684 +  finally show ?thesis .
2.1685 +qed
2.1686 +
2.1687 +lemma lcm_neg1 [simp]: "lcm (-x) y = lcm x y"
2.1688 +  by (rule sym, rule lcmI, simp_all add: lcm_least lcm_zero)
2.1689 +
2.1690 +lemma lcm_neg2 [simp]: "lcm x (-y) = lcm x y"
2.1691 +  by (rule sym, rule lcmI, simp_all add: lcm_least lcm_zero)
2.1692 +
2.1693 +lemma lcm_neg_numeral_1 [simp]: "lcm (- numeral n) x = lcm (numeral n) x"
2.1694 +  by (fact lcm_neg1)
2.1695 +
2.1696 +lemma lcm_neg_numeral_2 [simp]: "lcm x (- numeral n) = lcm x (numeral n)"
2.1697 +  by (fact lcm_neg2)
2.1698 +
2.1699 +function euclid_ext :: "'a \<Rightarrow> 'a \<Rightarrow> 'a \<times> 'a \<times> 'a" where
2.1700 +  "euclid_ext a b =
2.1701 +     (if b = 0 then
2.1702 +        let x = ring_inv (normalisation_factor a) in (x, 0, a * x)
2.1703 +      else
2.1704 +        case euclid_ext b (a mod b) of
2.1705 +            (s,t,c) \<Rightarrow> (t, s - t * (a div b), c))"
2.1706 +  by (pat_completeness, simp)
2.1707 +  termination by (relation "measure (euclidean_size \<circ> snd)", simp_all)
2.1708 +
2.1709 +declare euclid_ext.simps [simp del]
2.1710 +
2.1711 +lemma euclid_ext_0:
2.1712 +  "euclid_ext a 0 = (ring_inv (normalisation_factor a), 0, a * ring_inv (normalisation_factor a))"
2.1713 +  by (subst euclid_ext.simps, simp add: Let_def)
2.1714 +
2.1715 +lemma euclid_ext_non_0:
2.1716 +  "b \<noteq> 0 \<Longrightarrow> euclid_ext a b = (case euclid_ext b (a mod b) of
2.1717 +    (s,t,c) \<Rightarrow> (t, s - t * (a div b), c))"
2.1718 +  by (subst euclid_ext.simps, simp)
2.1719 +
2.1720 +definition euclid_ext' :: "'a \<Rightarrow> 'a \<Rightarrow> 'a \<times> 'a"
2.1721 +where
2.1722 +  "euclid_ext' a b = (case euclid_ext a b of (s, t, _) \<Rightarrow> (s, t))"
2.1723 +
2.1724 +lemma euclid_ext_gcd [simp]:
2.1725 +  "(case euclid_ext a b of (_,_,t) \<Rightarrow> t) = gcd a b"
2.1726 +proof (induct a b rule: euclid_ext.induct)
2.1727 +  case (1 a b)
2.1728 +  then show ?case
2.1729 +  proof (cases "b = 0")
2.1730 +    case True
2.1731 +      then show ?thesis by (cases "a = 0")
2.1732 +        (simp_all add: euclid_ext_0 unit_div mult_ac unit_simps gcd_0)
2.1733 +    next
2.1734 +    case False with 1 show ?thesis
2.1735 +      by (simp add: euclid_ext_non_0 ac_simps split: prod.split prod.split_asm)
2.1736 +    qed
2.1737 +qed
2.1738 +
2.1739 +lemma euclid_ext_gcd' [simp]:
2.1740 +  "euclid_ext a b = (r, s, t) \<Longrightarrow> t = gcd a b"
2.1741 +  by (insert euclid_ext_gcd[of a b], drule (1) subst, simp)
2.1742 +
2.1743 +lemma euclid_ext_correct:
2.1744 +  "case euclid_ext x y of (s,t,c) \<Rightarrow> s*x + t*y = c"
2.1745 +proof (induct x y rule: euclid_ext.induct)
2.1746 +  case (1 x y)
2.1747 +  show ?case
2.1748 +  proof (cases "y = 0")
2.1749 +    case True
2.1750 +    then show ?thesis by (simp add: euclid_ext_0 mult_ac)
2.1751 +  next
2.1752 +    case False
2.1753 +    obtain s t c where stc: "euclid_ext y (x mod y) = (s,t,c)"
2.1754 +      by (cases "euclid_ext y (x mod y)", blast)
2.1755 +    from 1 have "c = s * y + t * (x mod y)" by (simp add: stc False)
2.1756 +    also have "... = t*((x div y)*y + x mod y) + (s - t * (x div y))*y"
2.1757 +      by (simp add: algebra_simps)
2.1758 +    also have "(x div y)*y + x mod y = x" using mod_div_equality .
2.1759 +    finally show ?thesis
2.1760 +      by (subst euclid_ext.simps, simp add: False stc)
2.1761 +    qed
2.1762 +qed
2.1763 +
2.1764 +lemma euclid_ext'_correct:
2.1765 +  "fst (euclid_ext' a b) * a + snd (euclid_ext' a b) * b = gcd a b"
2.1766 +proof-
2.1767 +  obtain s t c where "euclid_ext a b = (s,t,c)"
2.1768 +    by (cases "euclid_ext a b", blast)
2.1769 +  with euclid_ext_correct[of a b] euclid_ext_gcd[of a b]
2.1770 +    show ?thesis unfolding euclid_ext'_def by simp
2.1771 +qed
2.1772 +
2.1773 +lemma bezout: "\<exists>s t. s * x + t * y = gcd x y"
2.1774 +  using euclid_ext'_correct by blast
2.1775 +
2.1776 +lemma euclid_ext'_0 [simp]: "euclid_ext' x 0 = (ring_inv (normalisation_factor x), 0)"
2.1777 +  by (simp add: bezw_def euclid_ext'_def euclid_ext_0)
2.1778 +
2.1779 +lemma euclid_ext'_non_0: "y \<noteq> 0 \<Longrightarrow> euclid_ext' x y = (snd (euclid_ext' y (x mod y)),
2.1780 +  fst (euclid_ext' y (x mod y)) - snd (euclid_ext' y (x mod y)) * (x div y))"
2.1781 +  by (cases "euclid_ext y (x mod y)")
2.1782 +    (simp add: euclid_ext'_def euclid_ext_non_0)
2.1783 +
2.1784 +end
2.1785 +
2.1786 +instantiation nat :: euclidean_semiring
2.1787 +begin
2.1788 +
2.1789 +definition [simp]:
2.1790 +  "euclidean_size_nat = (id :: nat \<Rightarrow> nat)"
2.1791 +
2.1792 +definition [simp]:
2.1793 +  "normalisation_factor_nat (n::nat) = (if n = 0 then 0 else 1 :: nat)"
2.1794 +
2.1795 +instance proof
2.1796 +qed (simp_all add: is_unit_def)
2.1797 +
2.1798 +end
2.1799 +
2.1800 +instantiation int :: euclidean_ring
2.1801 +begin
2.1802 +
2.1803 +definition [simp]:
2.1804 +  "euclidean_size_int = (nat \<circ> abs :: int \<Rightarrow> nat)"
2.1805 +
2.1806 +definition [simp]:
2.1807 +  "normalisation_factor_int = (sgn :: int \<Rightarrow> int)"
2.1808 +
2.1809 +instance proof
2.1810 +  case goal2 then show ?case by (auto simp add: abs_mult nat_mult_distrib)
2.1811 +next
2.1812 +  case goal3 then show ?case by (simp add: zsgn_def is_unit_def)
2.1813 +next
2.1814 +  case goal5 then show ?case by (auto simp: zsgn_def is_unit_def)
2.1815 +next
2.1816 +  case goal6 then show ?case by (auto split: abs_split simp: zsgn_def is_unit_def)
2.1817 +qed (auto simp: sgn_times split: abs_split)
2.1818 +
2.1819 +end
2.1820 +
2.1821 +end
2.1822 +

     3.1 --- a/src/HOL/ROOT	Thu Aug 21 14:41:08 2014 +0200
3.2 +++ b/src/HOL/ROOT	Fri Aug 22 08:43:14 2014 +0200
3.3 @@ -188,6 +188,7 @@
3.4      Pocklington
3.5      Gauss
3.6      Number_Theory
3.7 +    Euclidean_Algorithm
3.8    document_files
3.9      "root.tex"
3.10
`