src/HOL/Number_Theory/Euclidean_Algorithm.thy
 changeset 58023 62826b36ac5e child 58889 5b7a9633cfa8
1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/HOL/Number_Theory/Euclidean_Algorithm.thy	Fri Aug 22 08:43:14 2014 +0200
1.3 @@ -0,0 +1,1819 @@
1.4 +(* Author: Manuel Eberl *)
1.5 +
1.6 +header {* Abstract euclidean algorithm *}
1.7 +
1.8 +theory Euclidean_Algorithm
1.9 +imports Complex_Main
1.10 +begin
1.11 +
1.12 +lemma finite_int_set_iff_bounded_le:
1.13 +  "finite (N::int set) = (\<exists>m\<ge>0. \<forall>n\<in>N. abs n \<le> m)"
1.14 +proof
1.15 +  assume "finite (N::int set)"
1.16 +  hence "finite (nat  abs  N)" by (intro finite_imageI)
1.17 +  hence "\<exists>m. \<forall>n\<in>natabsN. n \<le> m" by (simp add: finite_nat_set_iff_bounded_le)
1.18 +  then obtain m :: nat where "\<forall>n\<in>N. nat (abs n) \<le> nat (int m)" by auto
1.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)
1.20 +next
1.21 +  assume "\<exists>m\<ge>0. \<forall>n\<in>N. abs n \<le> m"
1.22 +  then obtain m where "m \<ge> 0" and "\<forall>n\<in>N. abs n \<le> m" by blast
1.23 +  hence "\<forall>n\<in>N. nat (abs n) \<le> nat m" by (auto simp: nat_le_eq_zle)
1.24 +  hence "\<forall>n\<in>natabsN. n \<le> nat m" by (auto simp: nat_le_eq_zle)
1.25 +  hence A: "finite ((nat \<circ> abs)N)" unfolding o_def
1.26 +      by (subst finite_nat_set_iff_bounded_le) blast
1.27 +  {
1.28 +    assume "\<not>finite N"
1.29 +    from pigeonhole_infinite[OF this A] obtain x
1.30 +       where "x \<in> N" and B: "~finite {a\<in>N. nat (abs a) = nat (abs x)}"
1.31 +       unfolding o_def by blast
1.32 +    have "{a\<in>N. nat (abs a) = nat (abs x)} \<subseteq> {x, -x}" by auto
1.33 +    hence "finite {a\<in>N. nat (abs a) = nat (abs x)}" by (rule finite_subset) simp
1.34 +    with B have False by contradiction
1.35 +  }
1.36 +  then show "finite N" by blast
1.37 +qed
1.38 +
1.39 +context semiring_div
1.40 +begin
1.41 +
1.42 +lemma dvd_setprod [intro]:
1.43 +  assumes "finite A" and "x \<in> A"
1.44 +  shows "f x dvd setprod f A"
1.45 +proof
1.46 +  from finite A have "setprod f (insert x (A - {x})) = f x * setprod f (A - {x})"
1.47 +    by (intro setprod.insert) auto
1.48 +  also from x \<in> A have "insert x (A - {x}) = A" by blast
1.49 +  finally show "setprod f A = f x * setprod f (A - {x})" .
1.50 +qed
1.51 +
1.52 +lemma dvd_mult_cancel_left:
1.53 +  assumes "a \<noteq> 0" and "a * b dvd a * c"
1.54 +  shows "b dvd c"
1.55 +proof-
1.56 +  from assms(2) obtain k where "a * c = a * b * k" unfolding dvd_def by blast
1.57 +  hence "c * a = b * k * a" by (simp add: ac_simps)
1.58 +  hence "c * (a div a) = b * k * (a div a)" by (simp add: div_mult_swap)
1.59 +  also from a \<noteq> 0 have "a div a = 1" by simp
1.60 +  finally show ?thesis by simp
1.61 +qed
1.62 +
1.63 +lemma dvd_mult_cancel_right:
1.64 +  "a \<noteq> 0 \<Longrightarrow> b * a dvd c * a \<Longrightarrow> b dvd c"
1.65 +  by (subst (asm) (1 2) ac_simps, rule dvd_mult_cancel_left)
1.66 +
1.67 +lemma nonzero_pow_nonzero:
1.68 +  "a \<noteq> 0 \<Longrightarrow> a ^ n \<noteq> 0"
1.69 +  by (induct n) (simp_all add: no_zero_divisors)
1.70 +
1.71 +lemma zero_pow_zero: "n \<noteq> 0 \<Longrightarrow> 0 ^ n = 0"
1.72 +  by (cases n, simp_all)
1.73 +
1.74 +lemma pow_zero_iff:
1.75 +  "n \<noteq> 0 \<Longrightarrow> a^n = 0 \<longleftrightarrow> a = 0"
1.76 +  using nonzero_pow_nonzero zero_pow_zero by auto
1.77 +
1.78 +end
1.79 +
1.80 +context semiring_div
1.81 +begin
1.82 +
1.83 +definition ring_inv :: "'a \<Rightarrow> 'a"
1.84 +where
1.85 +  "ring_inv x = 1 div x"
1.86 +
1.87 +definition is_unit :: "'a \<Rightarrow> bool"
1.88 +where
1.89 +  "is_unit x \<longleftrightarrow> x dvd 1"
1.90 +
1.91 +definition associated :: "'a \<Rightarrow> 'a \<Rightarrow> bool"
1.92 +where
1.93 +  "associated x y \<longleftrightarrow> x dvd y \<and> y dvd x"
1.94 +
1.95 +lemma unit_prod [intro]:
1.96 +  "is_unit x \<Longrightarrow> is_unit y \<Longrightarrow> is_unit (x * y)"
1.97 +  unfolding is_unit_def by (subst mult_1_left [of 1, symmetric], rule mult_dvd_mono)
1.98 +
1.99 +lemma unit_ring_inv:
1.100 +  "is_unit y \<Longrightarrow> x div y = x * ring_inv y"
1.101 +  by (simp add: div_mult_swap ring_inv_def is_unit_def)
1.103 +lemma unit_ring_inv_ring_inv [simp]:
1.104 +  "is_unit x \<Longrightarrow> ring_inv (ring_inv x) = x"
1.105 +  unfolding is_unit_def ring_inv_def
1.106 +  by (metis div_mult_mult1_if div_mult_self1_is_id dvd_mult_div_cancel mult_1_right)
1.108 +lemma inv_imp_eq_ring_inv:
1.109 +  "a * b = 1 \<Longrightarrow> ring_inv a = b"
1.110 +  by (metis dvd_mult_div_cancel dvd_mult_right mult_1_right mult.left_commute one_dvd ring_inv_def)
1.112 +lemma ring_inv_is_inv1 [simp]:
1.113 +  "is_unit a \<Longrightarrow> a * ring_inv a = 1"
1.114 +  unfolding is_unit_def ring_inv_def by (simp add: dvd_mult_div_cancel)
1.116 +lemma ring_inv_is_inv2 [simp]:
1.117 +  "is_unit a \<Longrightarrow> ring_inv a * a = 1"
1.118 +  by (simp add: ac_simps)
1.120 +lemma unit_ring_inv_unit [simp, intro]:
1.121 +  assumes "is_unit x"
1.122 +  shows "is_unit (ring_inv x)"
1.123 +proof -
1.124 +  from assms have "1 = ring_inv x * x" by simp
1.125 +  then show "is_unit (ring_inv x)" unfolding is_unit_def by (rule dvdI)
1.126 +qed
1.128 +lemma mult_unit_dvd_iff:
1.129 +  "is_unit y \<Longrightarrow> x * y dvd z \<longleftrightarrow> x dvd z"
1.130 +proof
1.131 +  assume "is_unit y" "x * y dvd z"
1.132 +  then show "x dvd z" by (simp add: dvd_mult_left)
1.133 +next
1.134 +  assume "is_unit y" "x dvd z"
1.135 +  then obtain k where "z = x * k" unfolding dvd_def by blast
1.136 +  with is_unit y have "z = (x * y) * (ring_inv y * k)"
1.137 +      by (simp add: mult_ac)
1.138 +  then show "x * y dvd z" by (rule dvdI)
1.139 +qed
1.141 +lemma div_unit_dvd_iff:
1.142 +  "is_unit y \<Longrightarrow> x div y dvd z \<longleftrightarrow> x dvd z"
1.143 +  by (subst unit_ring_inv) (assumption, simp add: mult_unit_dvd_iff)
1.145 +lemma dvd_mult_unit_iff:
1.146 +  "is_unit y \<Longrightarrow> x dvd z * y \<longleftrightarrow> x dvd z"
1.147 +proof
1.148 +  assume "is_unit y" and "x dvd z * y"
1.149 +  have "z * y dvd z * (y * ring_inv y)" by (subst mult_assoc [symmetric]) simp
1.150 +  also from is_unit y have "y * ring_inv y = 1" by simp
1.151 +  finally have "z * y dvd z" by simp
1.152 +  with x dvd z * y show "x dvd z" by (rule dvd_trans)
1.153 +next
1.154 +  assume "x dvd z"
1.155 +  then show "x dvd z * y" by simp
1.156 +qed
1.158 +lemma dvd_div_unit_iff:
1.159 +  "is_unit y \<Longrightarrow> x dvd z div y \<longleftrightarrow> x dvd z"
1.160 +  by (subst unit_ring_inv) (assumption, simp add: dvd_mult_unit_iff)
1.162 +lemmas unit_dvd_iff = mult_unit_dvd_iff div_unit_dvd_iff dvd_mult_unit_iff dvd_div_unit_iff
1.164 +lemma unit_div [intro]:
1.165 +  "is_unit x \<Longrightarrow> is_unit y \<Longrightarrow> is_unit (x div y)"
1.166 +  by (subst unit_ring_inv) (assumption, rule unit_prod, simp_all)
1.168 +lemma unit_div_mult_swap:
1.169 +  "is_unit z \<Longrightarrow> x * (y div z) = x * y div z"
1.170 +  by (simp only: unit_ring_inv [of _ y] unit_ring_inv [of _ "x*y"] ac_simps)
1.172 +lemma unit_div_commute:
1.173 +  "is_unit y \<Longrightarrow> x div y * z = x * z div y"
1.174 +  by (simp only: unit_ring_inv [of _ x] unit_ring_inv [of _ "x*z"] ac_simps)
1.176 +lemma unit_imp_dvd [dest]:
1.177 +  "is_unit y \<Longrightarrow> y dvd x"
1.178 +  by (rule dvd_trans [of _ 1]) (simp_all add: is_unit_def)
1.180 +lemma dvd_unit_imp_unit:
1.181 +  "is_unit y \<Longrightarrow> x dvd y \<Longrightarrow> is_unit x"
1.182 +  by (unfold is_unit_def) (rule dvd_trans)
1.184 +lemma ring_inv_0 [simp]:
1.185 +  "ring_inv 0 = 0"
1.186 +  unfolding ring_inv_def by simp
1.188 +lemma unit_ring_inv'1:
1.189 +  assumes "is_unit y"
1.190 +  shows "x div (y * z) = x * ring_inv y div z"
1.191 +proof -
1.192 +  from assms have "x div (y * z) = x * (ring_inv y * y) div (y * z)"
1.193 +    by simp
1.194 +  also have "... = y * (x * ring_inv y) div (y * z)"
1.195 +    by (simp only: mult_ac)
1.196 +  also have "... = x * ring_inv y div z"
1.197 +    by (cases "y = 0", simp, rule div_mult_mult1)
1.198 +  finally show ?thesis .
1.199 +qed
1.201 +lemma associated_comm:
1.202 +  "associated x y \<Longrightarrow> associated y x"
1.203 +  by (simp add: associated_def)
1.205 +lemma associated_0 [simp]:
1.206 +  "associated 0 b \<longleftrightarrow> b = 0"
1.207 +  "associated a 0 \<longleftrightarrow> a = 0"
1.208 +  unfolding associated_def by simp_all
1.210 +lemma associated_unit:
1.211 +  "is_unit x \<Longrightarrow> associated x y \<Longrightarrow> is_unit y"
1.212 +  unfolding associated_def by (fast dest: dvd_unit_imp_unit)
1.214 +lemma is_unit_1 [simp]:
1.215 +  "is_unit 1"
1.216 +  unfolding is_unit_def by simp
1.218 +lemma not_is_unit_0 [simp]:
1.219 +  "\<not> is_unit 0"
1.220 +  unfolding is_unit_def by auto
1.222 +lemma unit_mult_left_cancel:
1.223 +  assumes "is_unit x"
1.224 +  shows "(x * y) = (x * z) \<longleftrightarrow> y = z"
1.225 +proof -
1.226 +  from assms have "x \<noteq> 0" by auto
1.227 +  then show ?thesis by (metis div_mult_self1_is_id)
1.228 +qed
1.230 +lemma unit_mult_right_cancel:
1.231 +  "is_unit x \<Longrightarrow> (y * x) = (z * x) \<longleftrightarrow> y = z"
1.232 +  by (simp add: ac_simps unit_mult_left_cancel)
1.234 +lemma unit_div_cancel:
1.235 +  "is_unit x \<Longrightarrow> (y div x) = (z div x) \<longleftrightarrow> y = z"
1.236 +  apply (subst unit_ring_inv[of _ y], assumption)
1.237 +  apply (subst unit_ring_inv[of _ z], assumption)
1.238 +  apply (rule unit_mult_right_cancel, erule unit_ring_inv_unit)
1.239 +  done
1.241 +lemma unit_eq_div1:
1.242 +  "is_unit y \<Longrightarrow> x div y = z \<longleftrightarrow> x = z * y"
1.243 +  apply (subst unit_ring_inv, assumption)
1.244 +  apply (subst unit_mult_right_cancel[symmetric], assumption)
1.245 +  apply (subst mult_assoc, subst ring_inv_is_inv2, assumption, simp)
1.246 +  done
1.248 +lemma unit_eq_div2:
1.249 +  "is_unit y \<Longrightarrow> x = z div y \<longleftrightarrow> x * y = z"
1.250 +  by (subst (1 2) eq_commute, simp add: unit_eq_div1, subst eq_commute, rule refl)
1.252 +lemma associated_iff_div_unit:
1.253 +  "associated x y \<longleftrightarrow> (\<exists>z. is_unit z \<and> x = z * y)"
1.254 +proof
1.255 +  assume "associated x y"
1.256 +  show "\<exists>z. is_unit z \<and> x = z * y"
1.257 +  proof (cases "x = 0")
1.258 +    assume "x = 0"
1.259 +    then show "\<exists>z. is_unit z \<and> x = z * y" using associated x y
1.260 +        by (intro exI[of _ 1], simp add: associated_def)
1.261 +  next
1.262 +    assume [simp]: "x \<noteq> 0"
1.263 +    hence [simp]: "x dvd y" "y dvd x" using associated x y
1.264 +        unfolding associated_def by simp_all
1.265 +    hence "1 = x div y * (y div x)"
1.266 +      by (simp add: div_mult_swap dvd_div_mult_self)
1.267 +    hence "is_unit (x div y)" unfolding is_unit_def by (rule dvdI)
1.268 +    moreover have "x = (x div y) * y" by (simp add: dvd_div_mult_self)
1.269 +    ultimately show ?thesis by blast
1.270 +  qed
1.271 +next
1.272 +  assume "\<exists>z. is_unit z \<and> x = z * y"
1.273 +  then obtain z where "is_unit z" and "x = z * y" by blast
1.274 +  hence "y = x * ring_inv z" by (simp add: algebra_simps)
1.275 +  hence "x dvd y" by simp
1.276 +  moreover from x = z * y have "y dvd x" by simp
1.277 +  ultimately show "associated x y" unfolding associated_def by simp
1.278 +qed
1.280 +lemmas unit_simps = mult_unit_dvd_iff div_unit_dvd_iff dvd_mult_unit_iff
1.281 +  dvd_div_unit_iff unit_div_mult_swap unit_div_commute
1.282 +  unit_mult_left_cancel unit_mult_right_cancel unit_div_cancel
1.283 +  unit_eq_div1 unit_eq_div2
1.285 +end
1.287 +context ring_div
1.288 +begin
1.290 +lemma is_unit_neg [simp]:
1.291 +  "is_unit (- x) \<Longrightarrow> is_unit x"
1.292 +  unfolding is_unit_def by simp
1.294 +lemma is_unit_neg_1 [simp]:
1.295 +  "is_unit (-1)"
1.296 +  unfolding is_unit_def by simp
1.298 +end
1.300 +lemma is_unit_nat [simp]:
1.301 +  "is_unit (x::nat) \<longleftrightarrow> x = 1"
1.302 +  unfolding is_unit_def by simp
1.304 +lemma is_unit_int:
1.305 +  "is_unit (x::int) \<longleftrightarrow> x = 1 \<or> x = -1"
1.306 +  unfolding is_unit_def by auto
1.308 +text {*
1.309 +  A Euclidean semiring is a semiring upon which the Euclidean algorithm can be
1.310 +  implemented. It must provide:
1.311 +  \begin{itemize}
1.312 +  \item division with remainder
1.313 +  \item a size function such that @{term "size (a mod b) < size b"}
1.314 +        for any @{term "b \<noteq> 0"}
1.315 +  \item a normalisation factor such that two associated numbers are equal iff
1.316 +        they are the same when divided by their normalisation factors.
1.317 +  \end{itemize}
1.318 +  The existence of these functions makes it possible to derive gcd and lcm functions
1.319 +  for any Euclidean semiring.
1.320 +*}
1.321 +class euclidean_semiring = semiring_div +
1.322 +  fixes euclidean_size :: "'a \<Rightarrow> nat"
1.323 +  fixes normalisation_factor :: "'a \<Rightarrow> 'a"
1.324 +  assumes mod_size_less [simp]:
1.325 +    "b \<noteq> 0 \<Longrightarrow> euclidean_size (a mod b) < euclidean_size b"
1.326 +  assumes size_mult_mono:
1.327 +    "b \<noteq> 0 \<Longrightarrow> euclidean_size (a * b) \<ge> euclidean_size a"
1.328 +  assumes normalisation_factor_is_unit [intro,simp]:
1.329 +    "a \<noteq> 0 \<Longrightarrow> is_unit (normalisation_factor a)"
1.330 +  assumes normalisation_factor_mult: "normalisation_factor (a * b) =
1.331 +    normalisation_factor a * normalisation_factor b"
1.332 +  assumes normalisation_factor_unit: "is_unit x \<Longrightarrow> normalisation_factor x = x"
1.333 +  assumes normalisation_factor_0 [simp]: "normalisation_factor 0 = 0"
1.334 +begin
1.336 +lemma normalisation_factor_dvd [simp]:
1.337 +  "a \<noteq> 0 \<Longrightarrow> normalisation_factor a dvd b"
1.338 +  by (rule unit_imp_dvd, simp)
1.340 +lemma normalisation_factor_1 [simp]:
1.341 +  "normalisation_factor 1 = 1"
1.342 +  by (simp add: normalisation_factor_unit)
1.344 +lemma normalisation_factor_0_iff [simp]:
1.345 +  "normalisation_factor x = 0 \<longleftrightarrow> x = 0"
1.346 +proof
1.347 +  assume "normalisation_factor x = 0"
1.348 +  hence "\<not> is_unit (normalisation_factor x)"
1.349 +    by (metis not_is_unit_0)
1.350 +  then show "x = 0" by force
1.351 +next
1.352 +  assume "x = 0"
1.353 +  then show "normalisation_factor x = 0" by simp
1.354 +qed
1.356 +lemma normalisation_factor_pow:
1.357 +  "normalisation_factor (x ^ n) = normalisation_factor x ^ n"
1.358 +  by (induct n) (simp_all add: normalisation_factor_mult power_Suc2)
1.360 +lemma normalisation_correct [simp]:
1.361 +  "normalisation_factor (x div normalisation_factor x) = (if x = 0 then 0 else 1)"
1.362 +proof (cases "x = 0", simp)
1.363 +  assume "x \<noteq> 0"
1.364 +  let ?nf = "normalisation_factor"
1.365 +  from normalisation_factor_is_unit[OF x \<noteq> 0] have "?nf x \<noteq> 0"
1.366 +    by (metis not_is_unit_0)
1.367 +  have "?nf (x div ?nf x) * ?nf (?nf x) = ?nf (x div ?nf x * ?nf x)"
1.368 +    by (simp add: normalisation_factor_mult)
1.369 +  also have "x div ?nf x * ?nf x = x" using x \<noteq> 0
1.370 +    by (simp add: dvd_div_mult_self)
1.371 +  also have "?nf (?nf x) = ?nf x" using x \<noteq> 0
1.372 +    normalisation_factor_is_unit normalisation_factor_unit by simp
1.373 +  finally show ?thesis using x \<noteq> 0 and ?nf x \<noteq> 0
1.374 +    by (metis div_mult_self2_is_id div_self)
1.375 +qed
1.377 +lemma normalisation_0_iff [simp]:
1.378 +  "x div normalisation_factor x = 0 \<longleftrightarrow> x = 0"
1.379 +  by (cases "x = 0", simp, subst unit_eq_div1, blast, simp)
1.381 +lemma associated_iff_normed_eq:
1.382 +  "associated a b \<longleftrightarrow> a div normalisation_factor a = b div normalisation_factor b"
1.383 +proof (cases "b = 0", simp, cases "a = 0", metis associated_0(1) normalisation_0_iff, rule iffI)
1.384 +  let ?nf = normalisation_factor
1.385 +  assume "a \<noteq> 0" "b \<noteq> 0" "a div ?nf a = b div ?nf b"
1.386 +  hence "a = b * (?nf a div ?nf b)"
1.387 +    apply (subst (asm) unit_eq_div1, blast, subst (asm) unit_div_commute, blast)
1.388 +    apply (subst div_mult_swap, simp, simp)
1.389 +    done
1.390 +  with a \<noteq> 0 b \<noteq> 0 have "\<exists>z. is_unit z \<and> a = z * b"
1.391 +    by (intro exI[of _ "?nf a div ?nf b"], force simp: mult_ac)
1.392 +  with associated_iff_div_unit show "associated a b" by simp
1.393 +next
1.394 +  let ?nf = normalisation_factor
1.395 +  assume "a \<noteq> 0" "b \<noteq> 0" "associated a b"
1.396 +  with associated_iff_div_unit obtain z where "is_unit z" and "a = z * b" by blast
1.397 +  then show "a div ?nf a = b div ?nf b"
1.398 +    apply (simp only: a = z * b normalisation_factor_mult normalisation_factor_unit)
1.399 +    apply (rule div_mult_mult1, force)
1.400 +    done
1.401 +  qed
1.403 +lemma normed_associated_imp_eq:
1.404 +  "associated a b \<Longrightarrow> normalisation_factor a \<in> {0, 1} \<Longrightarrow> normalisation_factor b \<in> {0, 1} \<Longrightarrow> a = b"
1.405 +  by (simp add: associated_iff_normed_eq, elim disjE, simp_all)
1.407 +lemmas normalisation_factor_dvd_iff [simp] =
1.408 +  unit_dvd_iff [OF normalisation_factor_is_unit]
1.410 +lemma euclidean_division:
1.411 +  fixes a :: 'a and b :: 'a
1.412 +  assumes "b \<noteq> 0"
1.413 +  obtains s and t where "a = s * b + t"
1.414 +    and "euclidean_size t < euclidean_size b"
1.415 +proof -
1.416 +  from div_mod_equality[of a b 0]
1.417 +     have "a = a div b * b + a mod b" by simp
1.418 +  with that and assms show ?thesis by force
1.419 +qed
1.421 +lemma dvd_euclidean_size_eq_imp_dvd:
1.422 +  assumes "a \<noteq> 0" and b_dvd_a: "b dvd a" and size_eq: "euclidean_size a = euclidean_size b"
1.423 +  shows "a dvd b"
1.424 +proof (subst dvd_eq_mod_eq_0, rule ccontr)
1.425 +  assume "b mod a \<noteq> 0"
1.426 +  from b_dvd_a have b_dvd_mod: "b dvd b mod a" by (simp add: dvd_mod_iff)
1.427 +  from b_dvd_mod obtain c where "b mod a = b * c" unfolding dvd_def by blast
1.428 +    with b mod a \<noteq> 0 have "c \<noteq> 0" by auto
1.429 +  with b mod a = b * c have "euclidean_size (b mod a) \<ge> euclidean_size b"
1.430 +      using size_mult_mono by force
1.431 +  moreover from a \<noteq> 0 have "euclidean_size (b mod a) < euclidean_size a"
1.432 +      using mod_size_less by blast
1.433 +  ultimately show False using size_eq by simp
1.434 +qed
1.436 +function gcd_eucl :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"
1.437 +where
1.438 +  "gcd_eucl a b = (if b = 0 then a div normalisation_factor a else gcd_eucl b (a mod b))"
1.439 +  by (pat_completeness, simp)
1.440 +termination by (relation "measure (euclidean_size \<circ> snd)", simp_all)
1.442 +declare gcd_eucl.simps [simp del]
1.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"
1.445 +proof (induct a b rule: gcd_eucl.induct)
1.446 +  case ("1" m n)
1.447 +    then show ?case by (cases "n = 0") auto
1.448 +qed
1.450 +definition lcm_eucl :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"
1.451 +where
1.452 +  "lcm_eucl a b = a * b div (gcd_eucl a b * normalisation_factor (a * b))"
1.454 +  (* Somewhat complicated definition of Lcm that has the advantage of working
1.455 +     for infinite sets as well *)
1.457 +definition Lcm_eucl :: "'a set \<Rightarrow> 'a"
1.458 +where
1.459 +  "Lcm_eucl A = (if \<exists>l. l \<noteq> 0 \<and> (\<forall>x\<in>A. x dvd l) then
1.460 +     let l = SOME l. l \<noteq> 0 \<and> (\<forall>x\<in>A. x dvd l) \<and> euclidean_size l =
1.461 +       (LEAST n. \<exists>l. l \<noteq> 0 \<and> (\<forall>x\<in>A. x dvd l) \<and> euclidean_size l = n)
1.462 +       in l div normalisation_factor l
1.463 +      else 0)"
1.465 +definition Gcd_eucl :: "'a set \<Rightarrow> 'a"
1.466 +where
1.467 +  "Gcd_eucl A = Lcm_eucl {d. \<forall>a\<in>A. d dvd a}"
1.469 +end
1.471 +class euclidean_semiring_gcd = euclidean_semiring + gcd + Gcd +
1.472 +  assumes gcd_gcd_eucl: "gcd = gcd_eucl" and lcm_lcm_eucl: "lcm = lcm_eucl"
1.473 +  assumes Gcd_Gcd_eucl: "Gcd = Gcd_eucl" and Lcm_Lcm_eucl: "Lcm = Lcm_eucl"
1.474 +begin
1.476 +lemma gcd_red:
1.477 +  "gcd x y = gcd y (x mod y)"
1.478 +  by (metis gcd_eucl.simps mod_0 mod_by_0 gcd_gcd_eucl)
1.480 +lemma gcd_non_0:
1.481 +  "y \<noteq> 0 \<Longrightarrow> gcd x y = gcd y (x mod y)"
1.482 +  by (rule gcd_red)
1.484 +lemma gcd_0_left:
1.485 +  "gcd 0 x = x div normalisation_factor x"
1.486 +   by (simp only: gcd_gcd_eucl, subst gcd_eucl.simps, subst gcd_eucl.simps, simp add: Let_def)
1.488 +lemma gcd_0:
1.489 +  "gcd x 0 = x div normalisation_factor x"
1.490 +  by (simp only: gcd_gcd_eucl, subst gcd_eucl.simps, simp add: Let_def)
1.492 +lemma gcd_dvd1 [iff]: "gcd x y dvd x"
1.493 +  and gcd_dvd2 [iff]: "gcd x y dvd y"
1.494 +proof (induct x y rule: gcd_eucl.induct)
1.495 +  fix x y :: 'a
1.496 +  assume IH1: "y \<noteq> 0 \<Longrightarrow> gcd y (x mod y) dvd y"
1.497 +  assume IH2: "y \<noteq> 0 \<Longrightarrow> gcd y (x mod y) dvd (x mod y)"
1.499 +  have "gcd x y dvd x \<and> gcd x y dvd y"
1.500 +  proof (cases "y = 0")
1.501 +    case True
1.502 +      then show ?thesis by (cases "x = 0", simp_all add: gcd_0)
1.503 +  next
1.504 +    case False
1.505 +      with IH1 and IH2 show ?thesis by (simp add: gcd_non_0 dvd_mod_iff)
1.506 +  qed
1.507 +  then show "gcd x y dvd x" "gcd x y dvd y" by simp_all
1.508 +qed
1.510 +lemma dvd_gcd_D1: "k dvd gcd m n \<Longrightarrow> k dvd m"
1.511 +  by (rule dvd_trans, assumption, rule gcd_dvd1)
1.513 +lemma dvd_gcd_D2: "k dvd gcd m n \<Longrightarrow> k dvd n"
1.514 +  by (rule dvd_trans, assumption, rule gcd_dvd2)
1.516 +lemma gcd_greatest:
1.517 +  fixes k x y :: 'a
1.518 +  shows "k dvd x \<Longrightarrow> k dvd y \<Longrightarrow> k dvd gcd x y"
1.519 +proof (induct x y rule: gcd_eucl.induct)
1.520 +  case (1 x y)
1.521 +  show ?case
1.522 +    proof (cases "y = 0")
1.523 +      assume "y = 0"
1.524 +      with 1 show ?thesis by (cases "x = 0", simp_all add: gcd_0)
1.525 +    next
1.526 +      assume "y \<noteq> 0"
1.527 +      with 1 show ?thesis by (simp add: gcd_non_0 dvd_mod_iff)
1.528 +    qed
1.529 +qed
1.531 +lemma dvd_gcd_iff:
1.532 +  "k dvd gcd x y \<longleftrightarrow> k dvd x \<and> k dvd y"
1.533 +  by (blast intro!: gcd_greatest intro: dvd_trans)
1.535 +lemmas gcd_greatest_iff = dvd_gcd_iff
1.537 +lemma gcd_zero [simp]:
1.538 +  "gcd x y = 0 \<longleftrightarrow> x = 0 \<and> y = 0"
1.539 +  by (metis dvd_0_left dvd_refl gcd_dvd1 gcd_dvd2 gcd_greatest)+
1.541 +lemma normalisation_factor_gcd [simp]:
1.542 +  "normalisation_factor (gcd x y) = (if x = 0 \<and> y = 0 then 0 else 1)" (is "?f x y = ?g x y")
1.543 +proof (induct x y rule: gcd_eucl.induct)
1.544 +  fix x y :: 'a
1.545 +  assume IH: "y \<noteq> 0 \<Longrightarrow> ?f y (x mod y) = ?g y (x mod y)"
1.546 +  then show "?f x y = ?g x y" by (cases "y = 0", auto simp: gcd_non_0 gcd_0)
1.547 +qed
1.549 +lemma gcdI:
1.550 +  "k dvd x \<Longrightarrow> k dvd y \<Longrightarrow> (\<And>l. l dvd x \<Longrightarrow> l dvd y \<Longrightarrow> l dvd k)
1.551 +    \<Longrightarrow> normalisation_factor k = (if k = 0 then 0 else 1) \<Longrightarrow> k = gcd x y"
1.552 +  by (intro normed_associated_imp_eq) (auto simp: associated_def intro: gcd_greatest)
1.554 +sublocale gcd!: abel_semigroup gcd
1.555 +proof
1.556 +  fix x y z
1.557 +  show "gcd (gcd x y) z = gcd x (gcd y z)"
1.558 +  proof (rule gcdI)
1.559 +    have "gcd (gcd x y) z dvd gcd x y" "gcd x y dvd x" by simp_all
1.560 +    then show "gcd (gcd x y) z dvd x" by (rule dvd_trans)
1.561 +    have "gcd (gcd x y) z dvd gcd x y" "gcd x y dvd y" by simp_all
1.562 +    hence "gcd (gcd x y) z dvd y" by (rule dvd_trans)
1.563 +    moreover have "gcd (gcd x y) z dvd z" by simp
1.564 +    ultimately show "gcd (gcd x y) z dvd gcd y z"
1.565 +      by (rule gcd_greatest)
1.566 +    show "normalisation_factor (gcd (gcd x y) z) =  (if gcd (gcd x y) z = 0 then 0 else 1)"
1.567 +      by auto
1.568 +    fix l assume "l dvd x" and "l dvd gcd y z"
1.569 +    with dvd_trans[OF _ gcd_dvd1] and dvd_trans[OF _ gcd_dvd2]
1.570 +      have "l dvd y" and "l dvd z" by blast+
1.571 +    with l dvd x show "l dvd gcd (gcd x y) z"
1.572 +      by (intro gcd_greatest)
1.573 +  qed
1.574 +next
1.575 +  fix x y
1.576 +  show "gcd x y = gcd y x"
1.577 +    by (rule gcdI) (simp_all add: gcd_greatest)
1.578 +qed
1.580 +lemma gcd_unique: "d dvd a \<and> d dvd b \<and>
1.581 +    normalisation_factor d = (if d = 0 then 0 else 1) \<and>
1.582 +    (\<forall>e. e dvd a \<and> e dvd b \<longrightarrow> e dvd d) \<longleftrightarrow> d = gcd a b"
1.583 +  by (rule, auto intro: gcdI simp: gcd_greatest)
1.585 +lemma gcd_dvd_prod: "gcd a b dvd k * b"
1.586 +  using mult_dvd_mono [of 1] by auto
1.588 +lemma gcd_1_left [simp]: "gcd 1 x = 1"
1.589 +  by (rule sym, rule gcdI, simp_all)
1.591 +lemma gcd_1 [simp]: "gcd x 1 = 1"
1.592 +  by (rule sym, rule gcdI, simp_all)
1.594 +lemma gcd_proj2_if_dvd:
1.595 +  "y dvd x \<Longrightarrow> gcd x y = y div normalisation_factor y"
1.596 +  by (cases "y = 0", simp_all add: dvd_eq_mod_eq_0 gcd_non_0 gcd_0)
1.598 +lemma gcd_proj1_if_dvd:
1.599 +  "x dvd y \<Longrightarrow> gcd x y = x div normalisation_factor x"
1.600 +  by (subst gcd.commute, simp add: gcd_proj2_if_dvd)
1.602 +lemma gcd_proj1_iff: "gcd m n = m div normalisation_factor m \<longleftrightarrow> m dvd n"
1.603 +proof
1.604 +  assume A: "gcd m n = m div normalisation_factor m"
1.605 +  show "m dvd n"
1.606 +  proof (cases "m = 0")
1.607 +    assume [simp]: "m \<noteq> 0"
1.608 +    from A have B: "m = gcd m n * normalisation_factor m"
1.609 +      by (simp add: unit_eq_div2)
1.610 +    show ?thesis by (subst B, simp add: mult_unit_dvd_iff)
1.611 +  qed (insert A, simp)
1.612 +next
1.613 +  assume "m dvd n"
1.614 +  then show "gcd m n = m div normalisation_factor m" by (rule gcd_proj1_if_dvd)
1.615 +qed
1.617 +lemma gcd_proj2_iff: "gcd m n = n div normalisation_factor n \<longleftrightarrow> n dvd m"
1.618 +  by (subst gcd.commute, simp add: gcd_proj1_iff)
1.620 +lemma gcd_mod1 [simp]:
1.621 +  "gcd (x mod y) y = gcd x y"
1.622 +  by (rule gcdI, metis dvd_mod_iff gcd_dvd1 gcd_dvd2, simp_all add: gcd_greatest dvd_mod_iff)
1.624 +lemma gcd_mod2 [simp]:
1.625 +  "gcd x (y mod x) = gcd x y"
1.626 +  by (rule gcdI, simp, metis dvd_mod_iff gcd_dvd1 gcd_dvd2, simp_all add: gcd_greatest dvd_mod_iff)
1.628 +lemma normalisation_factor_dvd' [simp]:
1.629 +  "normalisation_factor x dvd x"
1.630 +  by (cases "x = 0", simp_all)
1.632 +lemma gcd_mult_distrib':
1.633 +  "k div normalisation_factor k * gcd x y = gcd (k*x) (k*y)"
1.634 +proof (induct x y rule: gcd_eucl.induct)
1.635 +  case (1 x y)
1.636 +  show ?case
1.637 +  proof (cases "y = 0")
1.638 +    case True
1.639 +    then show ?thesis by (simp add: normalisation_factor_mult gcd_0 algebra_simps div_mult_div_if_dvd)
1.640 +  next
1.641 +    case False
1.642 +    hence "k div normalisation_factor k * gcd x y =  gcd (k * y) (k * (x mod y))"
1.643 +      using 1 by (subst gcd_red, simp)
1.644 +    also have "... = gcd (k * x) (k * y)"
1.645 +      by (simp add: mult_mod_right gcd.commute)
1.646 +    finally show ?thesis .
1.647 +  qed
1.648 +qed
1.650 +lemma gcd_mult_distrib:
1.651 +  "k * gcd x y = gcd (k*x) (k*y) * normalisation_factor k"
1.652 +proof-
1.653 +  let ?nf = "normalisation_factor"
1.654 +  from gcd_mult_distrib'
1.655 +    have "gcd (k*x) (k*y) = k div ?nf k * gcd x y" ..
1.656 +  also have "... = k * gcd x y div ?nf k"
1.657 +    by (metis dvd_div_mult dvd_eq_mod_eq_0 mod_0 normalisation_factor_dvd)
1.658 +  finally show ?thesis
1.659 +    by (simp add: ac_simps dvd_mult_div_cancel)
1.660 +qed
1.662 +lemma euclidean_size_gcd_le1 [simp]:
1.663 +  assumes "a \<noteq> 0"
1.664 +  shows "euclidean_size (gcd a b) \<le> euclidean_size a"
1.665 +proof -
1.666 +   have "gcd a b dvd a" by (rule gcd_dvd1)
1.667 +   then obtain c where A: "a = gcd a b * c" unfolding dvd_def by blast
1.668 +   with a \<noteq> 0 show ?thesis by (subst (2) A, intro size_mult_mono) auto
1.669 +qed
1.671 +lemma euclidean_size_gcd_le2 [simp]:
1.672 +  "b \<noteq> 0 \<Longrightarrow> euclidean_size (gcd a b) \<le> euclidean_size b"
1.673 +  by (subst gcd.commute, rule euclidean_size_gcd_le1)
1.675 +lemma euclidean_size_gcd_less1:
1.676 +  assumes "a \<noteq> 0" and "\<not>a dvd b"
1.677 +  shows "euclidean_size (gcd a b) < euclidean_size a"
1.678 +proof (rule ccontr)
1.679 +  assume "\<not>euclidean_size (gcd a b) < euclidean_size a"
1.680 +  with a \<noteq> 0 have "euclidean_size (gcd a b) = euclidean_size a"
1.681 +    by (intro le_antisym, simp_all)
1.682 +  with assms have "a dvd gcd a b" by (auto intro: dvd_euclidean_size_eq_imp_dvd)
1.683 +  hence "a dvd b" using dvd_gcd_D2 by blast
1.684 +  with \<not>a dvd b show False by contradiction
1.685 +qed
1.687 +lemma euclidean_size_gcd_less2:
1.688 +  assumes "b \<noteq> 0" and "\<not>b dvd a"
1.689 +  shows "euclidean_size (gcd a b) < euclidean_size b"
1.690 +  using assms by (subst gcd.commute, rule euclidean_size_gcd_less1)
1.692 +lemma gcd_mult_unit1: "is_unit a \<Longrightarrow> gcd (x*a) y = gcd x y"
1.693 +  apply (rule gcdI)
1.694 +  apply (rule dvd_trans, rule gcd_dvd1, simp add: unit_simps)
1.695 +  apply (rule gcd_dvd2)
1.696 +  apply (rule gcd_greatest, simp add: unit_simps, assumption)
1.697 +  apply (subst normalisation_factor_gcd, simp add: gcd_0)
1.698 +  done
1.700 +lemma gcd_mult_unit2: "is_unit a \<Longrightarrow> gcd x (y*a) = gcd x y"
1.701 +  by (subst gcd.commute, subst gcd_mult_unit1, assumption, rule gcd.commute)
1.703 +lemma gcd_div_unit1: "is_unit a \<Longrightarrow> gcd (x div a) y = gcd x y"
1.704 +  by (simp add: unit_ring_inv gcd_mult_unit1)
1.706 +lemma gcd_div_unit2: "is_unit a \<Longrightarrow> gcd x (y div a) = gcd x y"
1.707 +  by (simp add: unit_ring_inv gcd_mult_unit2)
1.709 +lemma gcd_idem: "gcd x x = x div normalisation_factor x"
1.710 +  by (cases "x = 0") (simp add: gcd_0_left, rule sym, rule gcdI, simp_all)
1.712 +lemma gcd_right_idem: "gcd (gcd p q) q = gcd p q"
1.713 +  apply (rule gcdI)
1.714 +  apply (simp add: ac_simps)
1.715 +  apply (rule gcd_dvd2)
1.716 +  apply (rule gcd_greatest, erule (1) gcd_greatest, assumption)
1.717 +  apply (simp add: gcd_zero)
1.718 +  done
1.720 +lemma gcd_left_idem: "gcd p (gcd p q) = gcd p q"
1.721 +  apply (rule gcdI)
1.722 +  apply simp
1.723 +  apply (rule dvd_trans, rule gcd_dvd2, rule gcd_dvd2)
1.724 +  apply (rule gcd_greatest, assumption, erule gcd_greatest, assumption)
1.725 +  apply (simp add: gcd_zero)
1.726 +  done
1.728 +lemma comp_fun_idem_gcd: "comp_fun_idem gcd"
1.729 +proof
1.730 +  fix a b show "gcd a \<circ> gcd b = gcd b \<circ> gcd a"
1.731 +    by (simp add: fun_eq_iff ac_simps)
1.732 +next
1.733 +  fix a show "gcd a \<circ> gcd a = gcd a"
1.734 +    by (simp add: fun_eq_iff gcd_left_idem)
1.735 +qed
1.737 +lemma coprime_dvd_mult:
1.738 +  assumes "gcd k n = 1" and "k dvd m * n"
1.739 +  shows "k dvd m"
1.740 +proof -
1.741 +  let ?nf = "normalisation_factor"
1.742 +  from assms gcd_mult_distrib [of m k n]
1.743 +    have A: "m = gcd (m * k) (m * n) * ?nf m" by simp
1.744 +  from k dvd m * n show ?thesis by (subst A, simp_all add: gcd_greatest)
1.745 +qed
1.747 +lemma coprime_dvd_mult_iff:
1.748 +  "gcd k n = 1 \<Longrightarrow> (k dvd m * n) = (k dvd m)"
1.749 +  by (rule, rule coprime_dvd_mult, simp_all)
1.751 +lemma gcd_dvd_antisym:
1.752 +  "gcd a b dvd gcd c d \<Longrightarrow> gcd c d dvd gcd a b \<Longrightarrow> gcd a b = gcd c d"
1.753 +proof (rule gcdI)
1.754 +  assume A: "gcd a b dvd gcd c d" and B: "gcd c d dvd gcd a b"
1.755 +  have "gcd c d dvd c" by simp
1.756 +  with A show "gcd a b dvd c" by (rule dvd_trans)
1.757 +  have "gcd c d dvd d" by simp
1.758 +  with A show "gcd a b dvd d" by (rule dvd_trans)
1.759 +  show "normalisation_factor (gcd a b) = (if gcd a b = 0 then 0 else 1)"
1.760 +    by (simp add: gcd_zero)
1.761 +  fix l assume "l dvd c" and "l dvd d"
1.762 +  hence "l dvd gcd c d" by (rule gcd_greatest)
1.763 +  from this and B show "l dvd gcd a b" by (rule dvd_trans)
1.764 +qed
1.766 +lemma gcd_mult_cancel:
1.767 +  assumes "gcd k n = 1"
1.768 +  shows "gcd (k * m) n = gcd m n"
1.769 +proof (rule gcd_dvd_antisym)
1.770 +  have "gcd (gcd (k * m) n) k = gcd (gcd k n) (k * m)" by (simp add: ac_simps)
1.771 +  also note gcd k n = 1
1.772 +  finally have "gcd (gcd (k * m) n) k = 1" by simp
1.773 +  hence "gcd (k * m) n dvd m" by (rule coprime_dvd_mult, simp add: ac_simps)
1.774 +  moreover have "gcd (k * m) n dvd n" by simp
1.775 +  ultimately show "gcd (k * m) n dvd gcd m n" by (rule gcd_greatest)
1.776 +  have "gcd m n dvd (k * m)" and "gcd m n dvd n" by simp_all
1.777 +  then show "gcd m n dvd gcd (k * m) n" by (rule gcd_greatest)
1.778 +qed
1.780 +lemma coprime_crossproduct:
1.781 +  assumes [simp]: "gcd a d = 1" "gcd b c = 1"
1.782 +  shows "associated (a * c) (b * d) \<longleftrightarrow> associated a b \<and> associated c d" (is "?lhs \<longleftrightarrow> ?rhs")
1.783 +proof
1.784 +  assume ?rhs then show ?lhs unfolding associated_def by (fast intro: mult_dvd_mono)
1.785 +next
1.786 +  assume ?lhs
1.787 +  from ?lhs have "a dvd b * d" unfolding associated_def by (metis dvd_mult_left)
1.788 +  hence "a dvd b" by (simp add: coprime_dvd_mult_iff)
1.789 +  moreover from ?lhs have "b dvd a * c" unfolding associated_def by (metis dvd_mult_left)
1.790 +  hence "b dvd a" by (simp add: coprime_dvd_mult_iff)
1.791 +  moreover from ?lhs have "c dvd d * b"
1.792 +    unfolding associated_def by (metis dvd_mult_right ac_simps)
1.793 +  hence "c dvd d" by (simp add: coprime_dvd_mult_iff gcd.commute)
1.794 +  moreover from ?lhs have "d dvd c * a"
1.795 +    unfolding associated_def by (metis dvd_mult_right ac_simps)
1.796 +  hence "d dvd c" by (simp add: coprime_dvd_mult_iff gcd.commute)
1.797 +  ultimately show ?rhs unfolding associated_def by simp
1.798 +qed
1.800 +lemma gcd_add1 [simp]:
1.801 +  "gcd (m + n) n = gcd m n"
1.802 +  by (cases "n = 0", simp_all add: gcd_non_0)
1.804 +lemma gcd_add2 [simp]:
1.805 +  "gcd m (m + n) = gcd m n"
1.806 +  using gcd_add1 [of n m] by (simp add: ac_simps)
1.808 +lemma gcd_add_mult: "gcd m (k * m + n) = gcd m n"
1.809 +  by (subst gcd.commute, subst gcd_red, simp)
1.811 +lemma coprimeI: "(\<And>l. \<lbrakk>l dvd x; l dvd y\<rbrakk> \<Longrightarrow> l dvd 1) \<Longrightarrow> gcd x y = 1"
1.812 +  by (rule sym, rule gcdI, simp_all)
1.814 +lemma coprime: "gcd a b = 1 \<longleftrightarrow> (\<forall>d. d dvd a \<and> d dvd b \<longleftrightarrow> is_unit d)"
1.815 +  by (auto simp: is_unit_def intro: coprimeI gcd_greatest dvd_gcd_D1 dvd_gcd_D2)
1.817 +lemma div_gcd_coprime:
1.818 +  assumes nz: "a \<noteq> 0 \<or> b \<noteq> 0"
1.819 +  defines [simp]: "d \<equiv> gcd a b"
1.820 +  defines [simp]: "a' \<equiv> a div d" and [simp]: "b' \<equiv> b div d"
1.821 +  shows "gcd a' b' = 1"
1.822 +proof (rule coprimeI)
1.823 +  fix l assume "l dvd a'" "l dvd b'"
1.824 +  then obtain s t where "a' = l * s" "b' = l * t" unfolding dvd_def by blast
1.825 +  moreover have "a = a' * d" "b = b' * d" by (simp_all add: dvd_div_mult_self)
1.826 +  ultimately have "a = (l * d) * s" "b = (l * d) * t"
1.827 +    by (metis ac_simps)+
1.828 +  hence "l*d dvd a" and "l*d dvd b" by (simp_all only: dvd_triv_left)
1.829 +  hence "l*d dvd d" by (simp add: gcd_greatest)
1.830 +  then obtain u where "u * l * d = d" unfolding dvd_def
1.831 +    by (metis ac_simps mult_assoc)
1.832 +  moreover from nz have "d \<noteq> 0" by (simp add: gcd_zero)
1.833 +  ultimately have "u * l = 1"
1.834 +    by (metis div_mult_self1_is_id div_self ac_simps)
1.835 +  then show "l dvd 1" by force
1.836 +qed
1.838 +lemma coprime_mult:
1.839 +  assumes da: "gcd d a = 1" and db: "gcd d b = 1"
1.840 +  shows "gcd d (a * b) = 1"
1.841 +  apply (subst gcd.commute)
1.842 +  using da apply (subst gcd_mult_cancel)
1.843 +  apply (subst gcd.commute, assumption)
1.844 +  apply (subst gcd.commute, rule db)
1.845 +  done
1.847 +lemma coprime_lmult:
1.848 +  assumes dab: "gcd d (a * b) = 1"
1.849 +  shows "gcd d a = 1"
1.850 +proof (rule coprimeI)
1.851 +  fix l assume "l dvd d" and "l dvd a"
1.852 +  hence "l dvd a * b" by simp
1.853 +  with l dvd d and dab show "l dvd 1" by (auto intro: gcd_greatest)
1.854 +qed
1.856 +lemma coprime_rmult:
1.857 +  assumes dab: "gcd d (a * b) = 1"
1.858 +  shows "gcd d b = 1"
1.859 +proof (rule coprimeI)
1.860 +  fix l assume "l dvd d" and "l dvd b"
1.861 +  hence "l dvd a * b" by simp
1.862 +  with l dvd d and dab show "l dvd 1" by (auto intro: gcd_greatest)
1.863 +qed
1.865 +lemma coprime_mul_eq: "gcd d (a * b) = 1 \<longleftrightarrow> gcd d a = 1 \<and> gcd d b = 1"
1.866 +  using coprime_rmult[of d a b] coprime_lmult[of d a b] coprime_mult[of d a b] by blast
1.868 +lemma gcd_coprime:
1.869 +  assumes z: "gcd a b \<noteq> 0" and a: "a = a' * gcd a b" and b: "b = b' * gcd a b"
1.870 +  shows "gcd a' b' = 1"
1.871 +proof -
1.872 +  from z have "a \<noteq> 0 \<or> b \<noteq> 0" by (simp add: gcd_zero)
1.873 +  with div_gcd_coprime have "gcd (a div gcd a b) (b div gcd a b) = 1" .
1.874 +  also from assms have "a div gcd a b = a'" by (metis div_mult_self2_is_id)+
1.875 +  also from assms have "b div gcd a b = b'" by (metis div_mult_self2_is_id)+
1.876 +  finally show ?thesis .
1.877 +qed
1.879 +lemma coprime_power:
1.880 +  assumes "0 < n"
1.881 +  shows "gcd a (b ^ n) = 1 \<longleftrightarrow> gcd a b = 1"
1.882 +using assms proof (induct n)
1.883 +  case (Suc n) then show ?case
1.884 +    by (cases n) (simp_all add: coprime_mul_eq)
1.885 +qed simp
1.887 +lemma gcd_coprime_exists:
1.888 +  assumes nz: "gcd a b \<noteq> 0"
1.889 +  shows "\<exists>a' b'. a = a' * gcd a b \<and> b = b' * gcd a b \<and> gcd a' b' = 1"
1.890 +  apply (rule_tac x = "a div gcd a b" in exI)
1.891 +  apply (rule_tac x = "b div gcd a b" in exI)
1.892 +  apply (insert nz, auto simp add: dvd_div_mult gcd_0_left  gcd_zero intro: div_gcd_coprime)
1.893 +  done
1.895 +lemma coprime_exp:
1.896 +  "gcd d a = 1 \<Longrightarrow> gcd d (a^n) = 1"
1.897 +  by (induct n, simp_all add: coprime_mult)
1.899 +lemma coprime_exp2 [intro]:
1.900 +  "gcd a b = 1 \<Longrightarrow> gcd (a^n) (b^m) = 1"
1.901 +  apply (rule coprime_exp)
1.902 +  apply (subst gcd.commute)
1.903 +  apply (rule coprime_exp)
1.904 +  apply (subst gcd.commute)
1.905 +  apply assumption
1.906 +  done
1.908 +lemma gcd_exp:
1.909 +  "gcd (a^n) (b^n) = (gcd a b) ^ n"
1.910 +proof (cases "a = 0 \<and> b = 0")
1.911 +  assume "a = 0 \<and> b = 0"
1.912 +  then show ?thesis by (cases n, simp_all add: gcd_0_left)
1.913 +next
1.914 +  assume A: "\<not>(a = 0 \<and> b = 0)"
1.915 +  hence "1 = gcd ((a div gcd a b)^n) ((b div gcd a b)^n)"
1.916 +    using div_gcd_coprime by (subst sym, auto simp: div_gcd_coprime)
1.917 +  hence "(gcd a b) ^ n = (gcd a b) ^ n * ..." by simp
1.918 +  also note gcd_mult_distrib
1.919 +  also have "normalisation_factor ((gcd a b)^n) = 1"
1.920 +    by (simp add: normalisation_factor_pow A)
1.921 +  also have "(gcd a b)^n * (a div gcd a b)^n = a^n"
1.922 +    by (subst ac_simps, subst div_power, simp, rule dvd_div_mult_self, rule dvd_power_same, simp)
1.923 +  also have "(gcd a b)^n * (b div gcd a b)^n = b^n"
1.924 +    by (subst ac_simps, subst div_power, simp, rule dvd_div_mult_self, rule dvd_power_same, simp)
1.925 +  finally show ?thesis by simp
1.926 +qed
1.928 +lemma coprime_common_divisor:
1.929 +  "gcd a b = 1 \<Longrightarrow> x dvd a \<Longrightarrow> x dvd b \<Longrightarrow> is_unit x"
1.930 +  apply (subgoal_tac "x dvd gcd a b")
1.931 +  apply (simp add: is_unit_def)
1.932 +  apply (erule (1) gcd_greatest)
1.933 +  done
1.935 +lemma division_decomp:
1.936 +  assumes dc: "a dvd b * c"
1.937 +  shows "\<exists>b' c'. a = b' * c' \<and> b' dvd b \<and> c' dvd c"
1.938 +proof (cases "gcd a b = 0")
1.939 +  assume "gcd a b = 0"
1.940 +  hence "a = 0 \<and> b = 0" by (simp add: gcd_zero)
1.941 +  hence "a = 0 * c \<and> 0 dvd b \<and> c dvd c" by simp
1.942 +  then show ?thesis by blast
1.943 +next
1.944 +  let ?d = "gcd a b"
1.945 +  assume "?d \<noteq> 0"
1.946 +  from gcd_coprime_exists[OF this]
1.947 +    obtain a' b' where ab': "a = a' * ?d" "b = b' * ?d" "gcd a' b' = 1"
1.948 +    by blast
1.949 +  from ab'(1) have "a' dvd a" unfolding dvd_def by blast
1.950 +  with dc have "a' dvd b*c" using dvd_trans[of a' a "b*c"] by simp
1.951 +  from dc ab'(1,2) have "a'*?d dvd (b'*?d) * c" by simp
1.952 +  hence "?d * a' dvd ?d * (b' * c)" by (simp add: mult_ac)
1.953 +  with ?d \<noteq> 0 have "a' dvd b' * c" by (rule dvd_mult_cancel_left)
1.954 +  with coprime_dvd_mult[OF ab'(3)]
1.955 +    have "a' dvd c" by (subst (asm) ac_simps, blast)
1.956 +  with ab'(1) have "a = ?d * a' \<and> ?d dvd b \<and> a' dvd c" by (simp add: mult_ac)
1.957 +  then show ?thesis by blast
1.958 +qed
1.960 +lemma pow_divides_pow:
1.961 +  assumes ab: "a ^ n dvd b ^ n" and n: "n \<noteq> 0"
1.962 +  shows "a dvd b"
1.963 +proof (cases "gcd a b = 0")
1.964 +  assume "gcd a b = 0"
1.965 +  then show ?thesis by (simp add: gcd_zero)
1.966 +next
1.967 +  let ?d = "gcd a b"
1.968 +  assume "?d \<noteq> 0"
1.969 +  from n obtain m where m: "n = Suc m" by (cases n, simp_all)
1.970 +  from ?d \<noteq> 0 have zn: "?d ^ n \<noteq> 0" by (rule nonzero_pow_nonzero)
1.971 +  from gcd_coprime_exists[OF ?d \<noteq> 0]
1.972 +    obtain a' b' where ab': "a = a' * ?d" "b = b' * ?d" "gcd a' b' = 1"
1.973 +    by blast
1.974 +  from ab have "(a' * ?d) ^ n dvd (b' * ?d) ^ n"
1.975 +    by (simp add: ab'(1,2)[symmetric])
1.976 +  hence "?d^n * a'^n dvd ?d^n * b'^n"
1.977 +    by (simp only: power_mult_distrib ac_simps)
1.978 +  with zn have "a'^n dvd b'^n" by (rule dvd_mult_cancel_left)
1.979 +  hence "a' dvd b'^n" using dvd_trans[of a' "a'^n" "b'^n"] by (simp add: m)
1.980 +  hence "a' dvd b'^m * b'" by (simp add: m ac_simps)
1.981 +  with coprime_dvd_mult[OF coprime_exp[OF ab'(3), of m]]
1.982 +    have "a' dvd b'" by (subst (asm) ac_simps, blast)
1.983 +  hence "a'*?d dvd b'*?d" by (rule mult_dvd_mono, simp)
1.984 +  with ab'(1,2) show ?thesis by simp
1.985 +qed
1.987 +lemma pow_divides_eq [simp]:
1.988 +  "n \<noteq> 0 \<Longrightarrow> a ^ n dvd b ^ n \<longleftrightarrow> a dvd b"
1.989 +  by (auto intro: pow_divides_pow dvd_power_same)
1.991 +lemma divides_mult:
1.992 +  assumes mr: "m dvd r" and nr: "n dvd r" and mn: "gcd m n = 1"
1.993 +  shows "m * n dvd r"
1.994 +proof -
1.995 +  from mr nr obtain m' n' where m': "r = m*m'" and n': "r = n*n'"
1.996 +    unfolding dvd_def by blast
1.997 +  from mr n' have "m dvd n'*n" by (simp add: ac_simps)
1.998 +  hence "m dvd n'" using coprime_dvd_mult_iff[OF mn] by simp
1.999 +  then obtain k where k: "n' = m*k" unfolding dvd_def by blast
1.1000 +  with n' have "r = m * n * k" by (simp add: mult_ac)
1.1001 +  then show ?thesis unfolding dvd_def by blast
1.1002 +qed
1.1004 +lemma coprime_plus_one [simp]: "gcd (n + 1) n = 1"
1.1005 +  by (subst add_commute, simp)
1.1007 +lemma setprod_coprime [rule_format]:
1.1008 +  "(\<forall>i\<in>A. gcd (f i) x = 1) \<longrightarrow> gcd (\<Prod>i\<in>A. f i) x = 1"
1.1009 +  apply (cases "finite A")
1.1010 +  apply (induct set: finite)
1.1011 +  apply (auto simp add: gcd_mult_cancel)
1.1012 +  done
1.1014 +lemma coprime_divisors:
1.1015 +  assumes "d dvd a" "e dvd b" "gcd a b = 1"
1.1016 +  shows "gcd d e = 1"
1.1017 +proof -
1.1018 +  from assms obtain k l where "a = d * k" "b = e * l"
1.1019 +    unfolding dvd_def by blast
1.1020 +  with assms have "gcd (d * k) (e * l) = 1" by simp
1.1021 +  hence "gcd (d * k) e = 1" by (rule coprime_lmult)
1.1022 +  also have "gcd (d * k) e = gcd e (d * k)" by (simp add: ac_simps)
1.1023 +  finally have "gcd e d = 1" by (rule coprime_lmult)
1.1024 +  then show ?thesis by (simp add: ac_simps)
1.1025 +qed
1.1027 +lemma invertible_coprime:
1.1028 +  "x * y mod m = 1 \<Longrightarrow> gcd x m = 1"
1.1029 +  by (metis coprime_lmult gcd_1 ac_simps gcd_red)
1.1031 +lemma lcm_gcd:
1.1032 +  "lcm a b = a * b div (gcd a b * normalisation_factor (a*b))"
1.1033 +  by (simp only: lcm_lcm_eucl gcd_gcd_eucl lcm_eucl_def)
1.1035 +lemma lcm_gcd_prod:
1.1036 +  "lcm a b * gcd a b = a * b div normalisation_factor (a*b)"
1.1037 +proof (cases "a * b = 0")
1.1038 +  let ?nf = normalisation_factor
1.1039 +  assume "a * b \<noteq> 0"
1.1040 +  hence "gcd a b \<noteq> 0" by (auto simp add: gcd_zero)
1.1041 +  from lcm_gcd have "lcm a b * gcd a b = gcd a b * (a * b div (?nf (a*b) * gcd a b))"
1.1042 +    by (simp add: mult_ac)
1.1043 +  also from a * b \<noteq> 0 have "... = a * b div ?nf (a*b)"
1.1044 +    by (simp_all add: unit_ring_inv'1 dvd_mult_div_cancel unit_ring_inv)
1.1045 +  finally show ?thesis .
1.1046 +qed (simp add: lcm_gcd)
1.1048 +lemma lcm_dvd1 [iff]:
1.1049 +  "x dvd lcm x y"
1.1050 +proof (cases "x*y = 0")
1.1051 +  assume "x * y \<noteq> 0"
1.1052 +  hence "gcd x y \<noteq> 0" by (auto simp: gcd_zero)
1.1053 +  let ?c = "ring_inv (normalisation_factor (x*y))"
1.1054 +  from x * y \<noteq> 0 have [simp]: "is_unit (normalisation_factor (x*y))" by simp
1.1055 +  from lcm_gcd_prod[of x y] have "lcm x y * gcd x y = x * ?c * y"
1.1056 +    by (simp add: mult_ac unit_ring_inv)
1.1057 +  hence "lcm x y * gcd x y div gcd x y = x * ?c * y div gcd x y" by simp
1.1058 +  with gcd x y \<noteq> 0 have "lcm x y = x * ?c * y div gcd x y"
1.1059 +    by (subst (asm) div_mult_self2_is_id, simp_all)
1.1060 +  also have "... = x * (?c * y div gcd x y)"
1.1061 +    by (metis div_mult_swap gcd_dvd2 mult_assoc)
1.1062 +  finally show ?thesis by (rule dvdI)
1.1063 +qed (simp add: lcm_gcd)
1.1065 +lemma lcm_least:
1.1066 +  "\<lbrakk>a dvd k; b dvd k\<rbrakk> \<Longrightarrow> lcm a b dvd k"
1.1067 +proof (cases "k = 0")
1.1068 +  let ?nf = normalisation_factor
1.1069 +  assume "k \<noteq> 0"
1.1070 +  hence "is_unit (?nf k)" by simp
1.1071 +  hence "?nf k \<noteq> 0" by (metis not_is_unit_0)
1.1072 +  assume A: "a dvd k" "b dvd k"
1.1073 +  hence "gcd a b \<noteq> 0" using k \<noteq> 0 by (auto simp add: gcd_zero)
1.1074 +  from A obtain r s where ar: "k = a * r" and bs: "k = b * s"
1.1075 +    unfolding dvd_def by blast
1.1076 +  with k \<noteq> 0 have "r * s \<noteq> 0"
1.1077 +    by (intro notI) (drule divisors_zero, elim disjE, simp_all)
1.1078 +  hence "is_unit (?nf (r * s))" by simp
1.1079 +  let ?c = "?nf k div ?nf (r*s)"
1.1080 +  from is_unit (?nf k) and is_unit (?nf (r * s)) have "is_unit ?c" by (rule unit_div)
1.1081 +  hence "?c \<noteq> 0" using not_is_unit_0 by fast
1.1082 +  from ar bs have "k * k * gcd s r = ?nf k * k * gcd (k * s) (k * r)"
1.1083 +    by (subst mult_assoc, subst gcd_mult_distrib[of k s r], simp only: ac_simps mult_assoc)
1.1084 +  also have "... = ?nf k * k * gcd ((r*s) * a) ((r*s) * b)"
1.1085 +    by (subst (3) k = a * r, subst (3) k = b * s, simp add: algebra_simps)
1.1086 +  also have "... = ?c * r*s * k * gcd a b" using r * s \<noteq> 0
1.1087 +    by (subst gcd_mult_distrib'[symmetric], simp add: algebra_simps unit_simps)
1.1088 +  finally have "(a*r) * (b*s) * gcd s r = ?c * k * r * s * gcd a b"
1.1089 +    by (subst ar[symmetric], subst bs[symmetric], simp add: mult_ac)
1.1090 +  hence "a * b * gcd s r * (r * s) = ?c * k * gcd a b * (r * s)"
1.1091 +    by (simp add: algebra_simps)
1.1092 +  hence "?c * k * gcd a b = a * b * gcd s r" using r * s \<noteq> 0
1.1093 +    by (metis div_mult_self2_is_id)
1.1094 +  also have "... = lcm a b * gcd a b * gcd s r * ?nf (a*b)"
1.1095 +    by (subst lcm_gcd_prod[of a b], metis gcd_mult_distrib gcd_mult_distrib')
1.1096 +  also have "... = lcm a b * gcd s r * ?nf (a*b) * gcd a b"
1.1097 +    by (simp add: algebra_simps)
1.1098 +  finally have "k * ?c = lcm a b * gcd s r * ?nf (a*b)" using gcd a b \<noteq> 0
1.1099 +    by (metis mult.commute div_mult_self2_is_id)
1.1100 +  hence "k = lcm a b * (gcd s r * ?nf (a*b)) div ?c" using ?c \<noteq> 0
1.1101 +    by (metis div_mult_self2_is_id mult_assoc)
1.1102 +  also have "... = lcm a b * (gcd s r * ?nf (a*b) div ?c)" using is_unit ?c
1.1103 +    by (simp add: unit_simps)
1.1104 +  finally show ?thesis by (rule dvdI)
1.1105 +qed simp
1.1107 +lemma lcm_zero:
1.1108 +  "lcm a b = 0 \<longleftrightarrow> a = 0 \<or> b = 0"
1.1109 +proof -
1.1110 +  let ?nf = normalisation_factor
1.1111 +  {
1.1112 +    assume "a \<noteq> 0" "b \<noteq> 0"
1.1113 +    hence "a * b div ?nf (a * b) \<noteq> 0" by (simp add: no_zero_divisors)
1.1114 +    moreover from a \<noteq> 0 and b \<noteq> 0 have "gcd a b \<noteq> 0" by (simp add: gcd_zero)
1.1115 +    ultimately have "lcm a b \<noteq> 0" using lcm_gcd_prod[of a b] by (intro notI, simp)
1.1116 +  } moreover {
1.1117 +    assume "a = 0 \<or> b = 0"
1.1118 +    hence "lcm a b = 0" by (elim disjE, simp_all add: lcm_gcd)
1.1119 +  }
1.1120 +  ultimately show ?thesis by blast
1.1121 +qed
1.1123 +lemmas lcm_0_iff = lcm_zero
1.1125 +lemma gcd_lcm:
1.1126 +  assumes "lcm a b \<noteq> 0"
1.1127 +  shows "gcd a b = a * b div (lcm a b * normalisation_factor (a * b))"
1.1128 +proof-
1.1129 +  from assms have "gcd a b \<noteq> 0" by (simp add: gcd_zero lcm_zero)
1.1130 +  let ?c = "normalisation_factor (a*b)"
1.1131 +  from lcm a b \<noteq> 0 have "?c \<noteq> 0" by (intro notI, simp add: lcm_zero no_zero_divisors)
1.1132 +  hence "is_unit ?c" by simp
1.1133 +  from lcm_gcd_prod [of a b] have "gcd a b = a * b div ?c div lcm a b"
1.1134 +    by (subst (2) div_mult_self2_is_id[OF lcm a b \<noteq> 0, symmetric], simp add: mult_ac)
1.1135 +  also from is_unit ?c have "... = a * b div (?c * lcm a b)"
1.1136 +    by (simp only: unit_ring_inv'1 unit_ring_inv)
1.1137 +  finally show ?thesis by (simp only: ac_simps)
1.1138 +qed
1.1140 +lemma normalisation_factor_lcm [simp]:
1.1141 +  "normalisation_factor (lcm a b) = (if a = 0 \<or> b = 0 then 0 else 1)"
1.1142 +proof (cases "a = 0 \<or> b = 0")
1.1143 +  case True then show ?thesis
1.1144 +    by (simp add: lcm_gcd) (metis div_0 ac_simps mult_zero_left normalisation_factor_0)
1.1145 +next
1.1146 +  case False
1.1147 +  let ?nf = normalisation_factor
1.1148 +  from lcm_gcd_prod[of a b]
1.1149 +    have "?nf (lcm a b) * ?nf (gcd a b) = ?nf (a*b) div ?nf (a*b)"
1.1150 +    by (metis div_by_0 div_self normalisation_correct normalisation_factor_0 normalisation_factor_mult)
1.1151 +  also have "... = (if a*b = 0 then 0 else 1)"
1.1152 +    by (cases "a*b = 0", simp, subst div_self, metis dvd_0_left normalisation_factor_dvd, simp)
1.1153 +  finally show ?thesis using False by (simp add: no_zero_divisors)
1.1154 +qed
1.1156 +lemma lcm_dvd2 [iff]: "y dvd lcm x y"
1.1157 +  using lcm_dvd1 [of y x] by (simp add: lcm_gcd ac_simps)
1.1159 +lemma lcmI:
1.1160 +  "\<lbrakk>x dvd k; y dvd k; \<And>l. x dvd l \<Longrightarrow> y dvd l \<Longrightarrow> k dvd l;
1.1161 +    normalisation_factor k = (if k = 0 then 0 else 1)\<rbrakk> \<Longrightarrow> k = lcm x y"
1.1162 +  by (intro normed_associated_imp_eq) (auto simp: associated_def intro: lcm_least)
1.1164 +sublocale lcm!: abel_semigroup lcm
1.1165 +proof
1.1166 +  fix x y z
1.1167 +  show "lcm (lcm x y) z = lcm x (lcm y z)"
1.1168 +  proof (rule lcmI)
1.1169 +    have "x dvd lcm x y" and "lcm x y dvd lcm (lcm x y) z" by simp_all
1.1170 +    then show "x dvd lcm (lcm x y) z" by (rule dvd_trans)
1.1172 +    have "y dvd lcm x y" and "lcm x y dvd lcm (lcm x y) z" by simp_all
1.1173 +    hence "y dvd lcm (lcm x y) z" by (rule dvd_trans)
1.1174 +    moreover have "z dvd lcm (lcm x y) z" by simp
1.1175 +    ultimately show "lcm y z dvd lcm (lcm x y) z" by (rule lcm_least)
1.1177 +    fix l assume "x dvd l" and "lcm y z dvd l"
1.1178 +    have "y dvd lcm y z" by simp
1.1179 +    from this and lcm y z dvd l have "y dvd l" by (rule dvd_trans)
1.1180 +    have "z dvd lcm y z" by simp
1.1181 +    from this and lcm y z dvd l have "z dvd l" by (rule dvd_trans)
1.1182 +    from x dvd l and y dvd l have "lcm x y dvd l" by (rule lcm_least)
1.1183 +    from this and z dvd l show "lcm (lcm x y) z dvd l" by (rule lcm_least)
1.1184 +  qed (simp add: lcm_zero)
1.1185 +next
1.1186 +  fix x y
1.1187 +  show "lcm x y = lcm y x"
1.1188 +    by (simp add: lcm_gcd ac_simps)
1.1189 +qed
1.1191 +lemma dvd_lcm_D1:
1.1192 +  "lcm m n dvd k \<Longrightarrow> m dvd k"
1.1193 +  by (rule dvd_trans, rule lcm_dvd1, assumption)
1.1195 +lemma dvd_lcm_D2:
1.1196 +  "lcm m n dvd k \<Longrightarrow> n dvd k"
1.1197 +  by (rule dvd_trans, rule lcm_dvd2, assumption)
1.1199 +lemma gcd_dvd_lcm [simp]:
1.1200 +  "gcd a b dvd lcm a b"
1.1201 +  by (metis dvd_trans gcd_dvd2 lcm_dvd2)
1.1203 +lemma lcm_1_iff:
1.1204 +  "lcm a b = 1 \<longleftrightarrow> is_unit a \<and> is_unit b"
1.1205 +proof
1.1206 +  assume "lcm a b = 1"
1.1207 +  then show "is_unit a \<and> is_unit b" unfolding is_unit_def by auto
1.1208 +next
1.1209 +  assume "is_unit a \<and> is_unit b"
1.1210 +  hence "a dvd 1" and "b dvd 1" unfolding is_unit_def by simp_all
1.1211 +  hence "is_unit (lcm a b)" unfolding is_unit_def by (rule lcm_least)
1.1212 +  hence "lcm a b = normalisation_factor (lcm a b)"
1.1213 +    by (subst normalisation_factor_unit, simp_all)
1.1214 +  also have "\<dots> = 1" using is_unit a \<and> is_unit b by (auto simp add: is_unit_def)
1.1215 +  finally show "lcm a b = 1" .
1.1216 +qed
1.1218 +lemma lcm_0_left [simp]:
1.1219 +  "lcm 0 x = 0"
1.1220 +  by (rule sym, rule lcmI, simp_all)
1.1222 +lemma lcm_0 [simp]:
1.1223 +  "lcm x 0 = 0"
1.1224 +  by (rule sym, rule lcmI, simp_all)
1.1226 +lemma lcm_unique:
1.1227 +  "a dvd d \<and> b dvd d \<and>
1.1228 +  normalisation_factor d = (if d = 0 then 0 else 1) \<and>
1.1229 +  (\<forall>e. a dvd e \<and> b dvd e \<longrightarrow> d dvd e) \<longleftrightarrow> d = lcm a b"
1.1230 +  by (rule, auto intro: lcmI simp: lcm_least lcm_zero)
1.1232 +lemma dvd_lcm_I1 [simp]:
1.1233 +  "k dvd m \<Longrightarrow> k dvd lcm m n"
1.1234 +  by (metis lcm_dvd1 dvd_trans)
1.1236 +lemma dvd_lcm_I2 [simp]:
1.1237 +  "k dvd n \<Longrightarrow> k dvd lcm m n"
1.1238 +  by (metis lcm_dvd2 dvd_trans)
1.1240 +lemma lcm_1_left [simp]:
1.1241 +  "lcm 1 x = x div normalisation_factor x"
1.1242 +  by (cases "x = 0") (simp, rule sym, rule lcmI, simp_all)
1.1244 +lemma lcm_1_right [simp]:
1.1245 +  "lcm x 1 = x div normalisation_factor x"
1.1246 +  by (simp add: ac_simps)
1.1248 +lemma lcm_coprime:
1.1249 +  "gcd a b = 1 \<Longrightarrow> lcm a b = a * b div normalisation_factor (a*b)"
1.1250 +  by (subst lcm_gcd) simp
1.1252 +lemma lcm_proj1_if_dvd:
1.1253 +  "y dvd x \<Longrightarrow> lcm x y = x div normalisation_factor x"
1.1254 +  by (cases "x = 0") (simp, rule sym, rule lcmI, simp_all)
1.1256 +lemma lcm_proj2_if_dvd:
1.1257 +  "x dvd y \<Longrightarrow> lcm x y = y div normalisation_factor y"
1.1258 +  using lcm_proj1_if_dvd [of x y] by (simp add: ac_simps)
1.1260 +lemma lcm_proj1_iff:
1.1261 +  "lcm m n = m div normalisation_factor m \<longleftrightarrow> n dvd m"
1.1262 +proof
1.1263 +  assume A: "lcm m n = m div normalisation_factor m"
1.1264 +  show "n dvd m"
1.1265 +  proof (cases "m = 0")
1.1266 +    assume [simp]: "m \<noteq> 0"
1.1267 +    from A have B: "m = lcm m n * normalisation_factor m"
1.1268 +      by (simp add: unit_eq_div2)
1.1269 +    show ?thesis by (subst B, simp)
1.1270 +  qed simp
1.1271 +next
1.1272 +  assume "n dvd m"
1.1273 +  then show "lcm m n = m div normalisation_factor m" by (rule lcm_proj1_if_dvd)
1.1274 +qed
1.1276 +lemma lcm_proj2_iff:
1.1277 +  "lcm m n = n div normalisation_factor n \<longleftrightarrow> m dvd n"
1.1278 +  using lcm_proj1_iff [of n m] by (simp add: ac_simps)
1.1280 +lemma euclidean_size_lcm_le1:
1.1281 +  assumes "a \<noteq> 0" and "b \<noteq> 0"
1.1282 +  shows "euclidean_size a \<le> euclidean_size (lcm a b)"
1.1283 +proof -
1.1284 +  have "a dvd lcm a b" by (rule lcm_dvd1)
1.1285 +  then obtain c where A: "lcm a b = a * c" unfolding dvd_def by blast
1.1286 +  with a \<noteq> 0 and b \<noteq> 0 have "c \<noteq> 0" by (auto simp: lcm_zero)
1.1287 +  then show ?thesis by (subst A, intro size_mult_mono)
1.1288 +qed
1.1290 +lemma euclidean_size_lcm_le2:
1.1291 +  "a \<noteq> 0 \<Longrightarrow> b \<noteq> 0 \<Longrightarrow> euclidean_size b \<le> euclidean_size (lcm a b)"
1.1292 +  using euclidean_size_lcm_le1 [of b a] by (simp add: ac_simps)
1.1294 +lemma euclidean_size_lcm_less1:
1.1295 +  assumes "b \<noteq> 0" and "\<not>b dvd a"
1.1296 +  shows "euclidean_size a < euclidean_size (lcm a b)"
1.1297 +proof (rule ccontr)
1.1298 +  from assms have "a \<noteq> 0" by auto
1.1299 +  assume "\<not>euclidean_size a < euclidean_size (lcm a b)"
1.1300 +  with a \<noteq> 0 and b \<noteq> 0 have "euclidean_size (lcm a b) = euclidean_size a"
1.1301 +    by (intro le_antisym, simp, intro euclidean_size_lcm_le1)
1.1302 +  with assms have "lcm a b dvd a"
1.1303 +    by (rule_tac dvd_euclidean_size_eq_imp_dvd) (auto simp: lcm_zero)
1.1304 +  hence "b dvd a" by (rule dvd_lcm_D2)
1.1305 +  with \<not>b dvd a show False by contradiction
1.1306 +qed
1.1308 +lemma euclidean_size_lcm_less2:
1.1309 +  assumes "a \<noteq> 0" and "\<not>a dvd b"
1.1310 +  shows "euclidean_size b < euclidean_size (lcm a b)"
1.1311 +  using assms euclidean_size_lcm_less1 [of a b] by (simp add: ac_simps)
1.1313 +lemma lcm_mult_unit1:
1.1314 +  "is_unit a \<Longrightarrow> lcm (x*a) y = lcm x y"
1.1315 +  apply (rule lcmI)
1.1316 +  apply (rule dvd_trans[of _ "x*a"], simp, rule lcm_dvd1)
1.1317 +  apply (rule lcm_dvd2)
1.1318 +  apply (rule lcm_least, simp add: unit_simps, assumption)
1.1319 +  apply (subst normalisation_factor_lcm, simp add: lcm_zero)
1.1320 +  done
1.1322 +lemma lcm_mult_unit2:
1.1323 +  "is_unit a \<Longrightarrow> lcm x (y*a) = lcm x y"
1.1324 +  using lcm_mult_unit1 [of a y x] by (simp add: ac_simps)
1.1326 +lemma lcm_div_unit1:
1.1327 +  "is_unit a \<Longrightarrow> lcm (x div a) y = lcm x y"
1.1328 +  by (simp add: unit_ring_inv lcm_mult_unit1)
1.1330 +lemma lcm_div_unit2:
1.1331 +  "is_unit a \<Longrightarrow> lcm x (y div a) = lcm x y"
1.1332 +  by (simp add: unit_ring_inv lcm_mult_unit2)
1.1334 +lemma lcm_left_idem:
1.1335 +  "lcm p (lcm p q) = lcm p q"
1.1336 +  apply (rule lcmI)
1.1337 +  apply simp
1.1338 +  apply (subst lcm.assoc [symmetric], rule lcm_dvd2)
1.1339 +  apply (rule lcm_least, assumption)
1.1340 +  apply (erule (1) lcm_least)
1.1341 +  apply (auto simp: lcm_zero)
1.1342 +  done
1.1344 +lemma lcm_right_idem:
1.1345 +  "lcm (lcm p q) q = lcm p q"
1.1346 +  apply (rule lcmI)
1.1347 +  apply (subst lcm.assoc, rule lcm_dvd1)
1.1348 +  apply (rule lcm_dvd2)
1.1349 +  apply (rule lcm_least, erule (1) lcm_least, assumption)
1.1350 +  apply (auto simp: lcm_zero)
1.1351 +  done
1.1353 +lemma comp_fun_idem_lcm: "comp_fun_idem lcm"
1.1354 +proof
1.1355 +  fix a b show "lcm a \<circ> lcm b = lcm b \<circ> lcm a"
1.1356 +    by (simp add: fun_eq_iff ac_simps)
1.1357 +next
1.1358 +  fix a show "lcm a \<circ> lcm a = lcm a" unfolding o_def
1.1359 +    by (intro ext, simp add: lcm_left_idem)
1.1360 +qed
1.1362 +lemma dvd_Lcm [simp]: "x \<in> A \<Longrightarrow> x dvd Lcm A"
1.1363 +  and Lcm_dvd [simp]: "(\<forall>x\<in>A. x dvd l') \<Longrightarrow> Lcm A dvd l'"
1.1364 +  and normalisation_factor_Lcm [simp]:
1.1365 +          "normalisation_factor (Lcm A) = (if Lcm A = 0 then 0 else 1)"
1.1366 +proof -
1.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>
1.1368 +    normalisation_factor (Lcm A) = (if Lcm A = 0 then 0 else 1)" (is ?thesis)
1.1369 +  proof (cases "\<exists>l. l \<noteq>  0 \<and> (\<forall>x\<in>A. x dvd l)")
1.1370 +    case False
1.1371 +    hence "Lcm A = 0" by (auto simp: Lcm_Lcm_eucl Lcm_eucl_def)
1.1372 +    with False show ?thesis by auto
1.1373 +  next
1.1374 +    case True
1.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
1.1376 +    def n \<equiv> "LEAST n. \<exists>l. l \<noteq> 0 \<and> (\<forall>x\<in>A. x dvd l) \<and> euclidean_size l = n"
1.1377 +    def l \<equiv> "SOME l. l \<noteq> 0 \<and> (\<forall>x\<in>A. x dvd l) \<and> euclidean_size l = n"
1.1378 +    have "\<exists>l. l \<noteq> 0 \<and> (\<forall>x\<in>A. x dvd l) \<and> euclidean_size l = n"
1.1379 +      apply (subst n_def)
1.1380 +      apply (rule LeastI[of _ "euclidean_size l\<^sub>0"])
1.1381 +      apply (rule exI[of _ l\<^sub>0])
1.1382 +      apply (simp add: l\<^sub>0_props)
1.1383 +      done
1.1384 +    from someI_ex[OF this] have "l \<noteq> 0" and "\<forall>x\<in>A. x dvd l" and "euclidean_size l = n"
1.1385 +      unfolding l_def by simp_all
1.1386 +    {
1.1387 +      fix l' assume "\<forall>x\<in>A. x dvd l'"
1.1388 +      with \<forall>x\<in>A. x dvd l have "\<forall>x\<in>A. x dvd gcd l l'" by (auto intro: gcd_greatest)
1.1389 +      moreover from l \<noteq> 0 have "gcd l l' \<noteq> 0" by (simp add: gcd_zero)
1.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')"
1.1391 +        by (intro exI[of _ "gcd l l'"], auto)
1.1392 +      hence "euclidean_size (gcd l l') \<ge> n" by (subst n_def) (rule Least_le)
1.1393 +      moreover have "euclidean_size (gcd l l') \<le> n"
1.1394 +      proof -
1.1395 +        have "gcd l l' dvd l" by simp
1.1396 +        then obtain a where "l = gcd l l' * a" unfolding dvd_def by blast
1.1397 +        with l \<noteq> 0 have "a \<noteq> 0" by auto
1.1398 +        hence "euclidean_size (gcd l l') \<le> euclidean_size (gcd l l' * a)"
1.1399 +          by (rule size_mult_mono)
1.1400 +        also have "gcd l l' * a = l" using l = gcd l l' * a ..
1.1401 +        also note euclidean_size l = n
1.1402 +        finally show "euclidean_size (gcd l l') \<le> n" .
1.1403 +      qed
1.1404 +      ultimately have "euclidean_size l = euclidean_size (gcd l l')"
1.1405 +        by (intro le_antisym, simp_all add: euclidean_size l = n)
1.1406 +      with l \<noteq> 0 have "l dvd gcd l l'" by (blast intro: dvd_euclidean_size_eq_imp_dvd)
1.1407 +      hence "l dvd l'" by (blast dest: dvd_gcd_D2)
1.1408 +    }
1.1410 +    with (\<forall>x\<in>A. x dvd l) and normalisation_factor_is_unit[OF l \<noteq> 0] and l \<noteq> 0
1.1411 +      have "(\<forall>x\<in>A. x dvd l div normalisation_factor l) \<and>
1.1412 +        (\<forall>l'. (\<forall>x\<in>A. x dvd l') \<longrightarrow> l div normalisation_factor l dvd l') \<and>
1.1413 +        normalisation_factor (l div normalisation_factor l) =
1.1414 +        (if l div normalisation_factor l = 0 then 0 else 1)"
1.1415 +      by (auto simp: unit_simps)
1.1416 +    also from True have "l div normalisation_factor l = Lcm A"
1.1417 +      by (simp add: Lcm_Lcm_eucl Lcm_eucl_def Let_def n_def l_def)
1.1418 +    finally show ?thesis .
1.1419 +  qed
1.1420 +  note A = this
1.1422 +  {fix x assume "x \<in> A" then show "x dvd Lcm A" using A by blast}
1.1423 +  {fix l' assume "\<forall>x\<in>A. x dvd l'" then show "Lcm A dvd l'" using A by blast}
1.1424 +  from A show "normalisation_factor (Lcm A) = (if Lcm A = 0 then 0 else 1)" by blast
1.1425 +qed
1.1427 +lemma LcmI:
1.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>
1.1429 +      normalisation_factor l = (if l = 0 then 0 else 1) \<Longrightarrow> l = Lcm A"
1.1430 +  by (intro normed_associated_imp_eq)
1.1431 +    (auto intro: Lcm_dvd dvd_Lcm simp: associated_def)
1.1433 +lemma Lcm_subset:
1.1434 +  "A \<subseteq> B \<Longrightarrow> Lcm A dvd Lcm B"
1.1435 +  by (blast intro: Lcm_dvd dvd_Lcm)
1.1437 +lemma Lcm_Un:
1.1438 +  "Lcm (A \<union> B) = lcm (Lcm A) (Lcm B)"
1.1439 +  apply (rule lcmI)
1.1440 +  apply (blast intro: Lcm_subset)
1.1441 +  apply (blast intro: Lcm_subset)
1.1442 +  apply (intro Lcm_dvd ballI, elim UnE)
1.1443 +  apply (rule dvd_trans, erule dvd_Lcm, assumption)
1.1444 +  apply (rule dvd_trans, erule dvd_Lcm, assumption)
1.1445 +  apply simp
1.1446 +  done
1.1448 +lemma Lcm_1_iff:
1.1449 +  "Lcm A = 1 \<longleftrightarrow> (\<forall>x\<in>A. is_unit x)"
1.1450 +proof
1.1451 +  assume "Lcm A = 1"
1.1452 +  then show "\<forall>x\<in>A. is_unit x" unfolding is_unit_def by auto
1.1453 +qed (rule LcmI [symmetric], auto)
1.1455 +lemma Lcm_no_units:
1.1456 +  "Lcm A = Lcm (A - {x. is_unit x})"
1.1457 +proof -
1.1458 +  have "(A - {x. is_unit x}) \<union> {x\<in>A. is_unit x} = A" by blast
1.1459 +  hence "Lcm A = lcm (Lcm (A - {x. is_unit x})) (Lcm {x\<in>A. is_unit x})"
1.1460 +    by (simp add: Lcm_Un[symmetric])
1.1461 +  also have "Lcm {x\<in>A. is_unit x} = 1" by (simp add: Lcm_1_iff)
1.1462 +  finally show ?thesis by simp
1.1463 +qed
1.1465 +lemma Lcm_empty [simp]:
1.1466 +  "Lcm {} = 1"
1.1467 +  by (simp add: Lcm_1_iff)
1.1469 +lemma Lcm_eq_0 [simp]:
1.1470 +  "0 \<in> A \<Longrightarrow> Lcm A = 0"
1.1471 +  by (drule dvd_Lcm) simp
1.1473 +lemma Lcm0_iff':
1.1474 +  "Lcm A = 0 \<longleftrightarrow> \<not>(\<exists>l. l \<noteq> 0 \<and> (\<forall>x\<in>A. x dvd l))"
1.1475 +proof
1.1476 +  assume "Lcm A = 0"
1.1477 +  show "\<not>(\<exists>l. l \<noteq> 0 \<and> (\<forall>x\<in>A. x dvd l))"
1.1478 +  proof
1.1479 +    assume ex: "\<exists>l. l \<noteq> 0 \<and> (\<forall>x\<in>A. x dvd l)"
1.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
1.1481 +    def n \<equiv> "LEAST n. \<exists>l. l \<noteq> 0 \<and> (\<forall>x\<in>A. x dvd l) \<and> euclidean_size l = n"
1.1482 +    def l \<equiv> "SOME l. l \<noteq> 0 \<and> (\<forall>x\<in>A. x dvd l) \<and> euclidean_size l = n"
1.1483 +    have "\<exists>l. l \<noteq> 0 \<and> (\<forall>x\<in>A. x dvd l) \<and> euclidean_size l = n"
1.1484 +      apply (subst n_def)
1.1485 +      apply (rule LeastI[of _ "euclidean_size l\<^sub>0"])
1.1486 +      apply (rule exI[of _ l\<^sub>0])
1.1487 +      apply (simp add: l\<^sub>0_props)
1.1488 +      done
1.1489 +    from someI_ex[OF this] have "l \<noteq> 0" unfolding l_def by simp_all
1.1490 +    hence "l div normalisation_factor l \<noteq> 0" by simp
1.1491 +    also from ex have "l div normalisation_factor l = Lcm A"
1.1492 +       by (simp only: Lcm_Lcm_eucl Lcm_eucl_def n_def l_def if_True Let_def)
1.1493 +    finally show False using Lcm A = 0 by contradiction
1.1494 +  qed
1.1495 +qed (simp only: Lcm_Lcm_eucl Lcm_eucl_def if_False)
1.1497 +lemma Lcm0_iff [simp]:
1.1498 +  "finite A \<Longrightarrow> Lcm A = 0 \<longleftrightarrow> 0 \<in> A"
1.1499 +proof -
1.1500 +  assume "finite A"
1.1501 +  have "0 \<in> A \<Longrightarrow> Lcm A = 0"  by (intro dvd_0_left dvd_Lcm)
1.1502 +  moreover {
1.1503 +    assume "0 \<notin> A"
1.1504 +    hence "\<Prod>A \<noteq> 0"
1.1505 +      apply (induct rule: finite_induct[OF finite A])
1.1506 +      apply simp
1.1507 +      apply (subst setprod.insert, assumption, assumption)
1.1508 +      apply (rule no_zero_divisors)
1.1509 +      apply blast+
1.1510 +      done
1.1511 +    moreover from finite A have "\<forall>x\<in>A. x dvd \<Prod>A" by (intro ballI dvd_setprod)
1.1512 +    ultimately have "\<exists>l. l \<noteq> 0 \<and> (\<forall>x\<in>A. x dvd l)" by blast
1.1513 +    with Lcm0_iff' have "Lcm A \<noteq> 0" by simp
1.1514 +  }
1.1515 +  ultimately show "Lcm A = 0 \<longleftrightarrow> 0 \<in> A" by blast
1.1516 +qed
1.1518 +lemma Lcm_no_multiple:
1.1519 +  "(\<forall>m. m \<noteq> 0 \<longrightarrow> (\<exists>x\<in>A. \<not>x dvd m)) \<Longrightarrow> Lcm A = 0"
1.1520 +proof -
1.1521 +  assume "\<forall>m. m \<noteq> 0 \<longrightarrow> (\<exists>x\<in>A. \<not>x dvd m)"
1.1522 +  hence "\<not>(\<exists>l. l \<noteq> 0 \<and> (\<forall>x\<in>A. x dvd l))" by blast
1.1523 +  then show "Lcm A = 0" by (simp only: Lcm_Lcm_eucl Lcm_eucl_def if_False)
1.1524 +qed
1.1526 +lemma Lcm_insert [simp]:
1.1527 +  "Lcm (insert a A) = lcm a (Lcm A)"
1.1528 +proof (rule lcmI)
1.1529 +  fix l assume "a dvd l" and "Lcm A dvd l"
1.1530 +  hence "\<forall>x\<in>A. x dvd l" by (blast intro: dvd_trans dvd_Lcm)
1.1531 +  with a dvd l show "Lcm (insert a A) dvd l" by (force intro: Lcm_dvd)
1.1532 +qed (auto intro: Lcm_dvd dvd_Lcm)
1.1534 +lemma Lcm_finite:
1.1535 +  assumes "finite A"
1.1536 +  shows "Lcm A = Finite_Set.fold lcm 1 A"
1.1537 +  by (induct rule: finite.induct[OF finite A])
1.1538 +    (simp_all add: comp_fun_idem.fold_insert_idem[OF comp_fun_idem_lcm])
1.1540 +lemma Lcm_set [code, code_unfold]:
1.1541 +  "Lcm (set xs) = fold lcm xs 1"
1.1542 +  using comp_fun_idem.fold_set_fold[OF comp_fun_idem_lcm] Lcm_finite by (simp add: ac_simps)
1.1544 +lemma Lcm_singleton [simp]:
1.1545 +  "Lcm {a} = a div normalisation_factor a"
1.1546 +  by simp
1.1548 +lemma Lcm_2 [simp]:
1.1549 +  "Lcm {a,b} = lcm a b"
1.1550 +  by (simp only: Lcm_insert Lcm_empty lcm_1_right)
1.1551 +    (cases "b = 0", simp, rule lcm_div_unit2, simp)
1.1553 +lemma Lcm_coprime:
1.1554 +  assumes "finite A" and "A \<noteq> {}"
1.1555 +  assumes "\<And>a b. a \<in> A \<Longrightarrow> b \<in> A \<Longrightarrow> a \<noteq> b \<Longrightarrow> gcd a b = 1"
1.1556 +  shows "Lcm A = \<Prod>A div normalisation_factor (\<Prod>A)"
1.1557 +using assms proof (induct rule: finite_ne_induct)
1.1558 +  case (insert a A)
1.1559 +  have "Lcm (insert a A) = lcm a (Lcm A)" by simp
1.1560 +  also from insert have "Lcm A = \<Prod>A div normalisation_factor (\<Prod>A)" by blast
1.1561 +  also have "lcm a \<dots> = lcm a (\<Prod>A)" by (cases "\<Prod>A = 0") (simp_all add: lcm_div_unit2)
1.1562 +  also from insert have "gcd a (\<Prod>A) = 1" by (subst gcd.commute, intro setprod_coprime) auto
1.1563 +  with insert have "lcm a (\<Prod>A) = \<Prod>(insert a A) div normalisation_factor (\<Prod>(insert a A))"
1.1564 +    by (simp add: lcm_coprime)
1.1565 +  finally show ?case .
1.1566 +qed simp
1.1568 +lemma Lcm_coprime':
1.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)
1.1570 +    \<Longrightarrow> Lcm A = \<Prod>A div normalisation_factor (\<Prod>A)"
1.1571 +  by (rule Lcm_coprime) (simp_all add: card_eq_0_iff)
1.1573 +lemma Gcd_Lcm:
1.1574 +  "Gcd A = Lcm {d. \<forall>x\<in>A. d dvd x}"
1.1575 +  by (simp add: Gcd_Gcd_eucl Lcm_Lcm_eucl Gcd_eucl_def)
1.1577 +lemma Gcd_dvd [simp]: "x \<in> A \<Longrightarrow> Gcd A dvd x"
1.1578 +  and dvd_Gcd [simp]: "(\<forall>x\<in>A. g' dvd x) \<Longrightarrow> g' dvd Gcd A"
1.1579 +  and normalisation_factor_Gcd [simp]:
1.1580 +    "normalisation_factor (Gcd A) = (if Gcd A = 0 then 0 else 1)"
1.1581 +proof -
1.1582 +  fix x assume "x \<in> A"
1.1583 +  hence "Lcm {d. \<forall>x\<in>A. d dvd x} dvd x" by (intro Lcm_dvd) blast
1.1584 +  then show "Gcd A dvd x" by (simp add: Gcd_Lcm)
1.1585 +next
1.1586 +  fix g' assume "\<forall>x\<in>A. g' dvd x"
1.1587 +  hence "g' dvd Lcm {d. \<forall>x\<in>A. d dvd x}" by (intro dvd_Lcm) blast
1.1588 +  then show "g' dvd Gcd A" by (simp add: Gcd_Lcm)
1.1589 +next
1.1590 +  show "normalisation_factor (Gcd A) = (if Gcd A = 0 then 0 else 1)"
1.1591 +    by (simp add: Gcd_Lcm normalisation_factor_Lcm)
1.1592 +qed
1.1594 +lemma GcdI:
1.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>
1.1596 +    normalisation_factor l = (if l = 0 then 0 else 1) \<Longrightarrow> l = Gcd A"
1.1597 +  by (intro normed_associated_imp_eq)
1.1598 +    (auto intro: Gcd_dvd dvd_Gcd simp: associated_def)
1.1600 +lemma Lcm_Gcd:
1.1601 +  "Lcm A = Gcd {m. \<forall>x\<in>A. x dvd m}"
1.1602 +  by (rule LcmI[symmetric]) (auto intro: dvd_Gcd Gcd_dvd)
1.1604 +lemma Gcd_0_iff:
1.1605 +  "Gcd A = 0 \<longleftrightarrow> A \<subseteq> {0}"
1.1606 +  apply (rule iffI)
1.1607 +  apply (rule subsetI, drule Gcd_dvd, simp)
1.1608 +  apply (auto intro: GcdI[symmetric])
1.1609 +  done
1.1611 +lemma Gcd_empty [simp]:
1.1612 +  "Gcd {} = 0"
1.1613 +  by (simp add: Gcd_0_iff)
1.1615 +lemma Gcd_1:
1.1616 +  "1 \<in> A \<Longrightarrow> Gcd A = 1"
1.1617 +  by (intro GcdI[symmetric]) (auto intro: Gcd_dvd dvd_Gcd)
1.1619 +lemma Gcd_insert [simp]:
1.1620 +  "Gcd (insert a A) = gcd a (Gcd A)"
1.1621 +proof (rule gcdI)
1.1622 +  fix l assume "l dvd a" and "l dvd Gcd A"
1.1623 +  hence "\<forall>x\<in>A. l dvd x" by (blast intro: dvd_trans Gcd_dvd)
1.1624 +  with l dvd a show "l dvd Gcd (insert a A)" by (force intro: Gcd_dvd)
1.1625 +qed (auto intro: Gcd_dvd dvd_Gcd simp: normalisation_factor_Gcd)
1.1627 +lemma Gcd_finite:
1.1628 +  assumes "finite A"
1.1629 +  shows "Gcd A = Finite_Set.fold gcd 0 A"
1.1630 +  by (induct rule: finite.induct[OF finite A`])
1.1631 +    (simp_all add: comp_fun_idem.fold_insert_idem[OF comp_fun_idem_gcd])
1.1633 +lemma Gcd_set [code, code_unfold]:
1.1634 +  "Gcd (set xs) = fold gcd xs 0"
1.1635 +  using comp_fun_idem.fold_set_fold[OF comp_fun_idem_gcd] Gcd_finite by (simp add: ac_simps)
1.1637 +lemma Gcd_singleton [simp]: "Gcd {a} = a div normalisation_factor a"
1.1638 +  by (simp add: gcd_0)
1.1640 +lemma Gcd_2 [simp]: "Gcd {a,b} = gcd a b"
1.1641 +  by (simp only: Gcd_insert Gcd_empty gcd_0) (cases "b = 0", simp, rule gcd_div_unit2, simp)
1.1643 +end
1.1645 +text {*
1.1646 +  A Euclidean ring is a Euclidean semiring with additive inverses. It provides a
1.1647 +  few more lemmas; in particular, Bezout's lemma holds for any Euclidean ring.
1.1648 +*}
1.1650 +class euclidean_ring = euclidean_semiring + idom
1.1652 +class euclidean_ring_gcd = euclidean_semiring_gcd + idom
1.1653 +begin
1.1655 +subclass euclidean_ring ..
1.1657 +lemma gcd_neg1 [simp]:
1.1658 +  "gcd (-x) y = gcd x y"
1.1659 +  by (rule sym, rule gcdI, simp_all add: gcd_greatest gcd_zero)
1.1661 +lemma gcd_neg2 [simp]:
1.1662 +  "gcd x (-y) = gcd x y"
1.1663 +  by (rule sym, rule gcdI, simp_all add: gcd_greatest gcd_zero)
1.1665 +lemma gcd_neg_numeral_1 [simp]:
1.1666 +  "gcd (- numeral n) x = gcd (numeral n) x"
1.1667 +  by (fact gcd_neg1)
1.1669 +lemma gcd_neg_numeral_2 [simp]:
1.1670 +  "gcd x (- numeral n) = gcd x (numeral n)"
1.1671 +  by (fact gcd_neg2)
1.1673 +lemma gcd_diff1: "gcd (m - n) n = gcd m n"
1.1674 +  by (subst diff_conv_add_uminus, subst gcd_neg2[symmetric],  subst gcd_add1, simp)
1.1676 +lemma gcd_diff2: "gcd (n - m) n = gcd m n"
1.1677 +  by (subst gcd_neg1[symmetric], simp only: minus_diff_eq gcd_diff1)
1.1679 +lemma coprime_minus_one [simp]: "gcd (n - 1) n = 1"
1.1680 +proof -
1.1681 +  have "gcd (n - 1) n = gcd n (n - 1)" by (fact gcd.commute)
1.1682 +  also have "\<dots> = gcd ((n - 1) + 1) (n - 1)" by simp
1.1683 +  also have "\<dots> = 1" by (rule coprime_plus_one)
1.1684 +  finally show ?thesis .
1.1685 +qed
1.1687 +lemma lcm_neg1 [simp]: "lcm (-x) y = lcm x y"
1.1688 +  by (rule sym, rule lcmI, simp_all add: lcm_least lcm_zero)
1.1690 +lemma lcm_neg2 [simp]: "lcm x (-y) = lcm x y"
1.1691 +  by (rule sym, rule lcmI, simp_all add: lcm_least lcm_zero)
1.1693 +lemma lcm_neg_numeral_1 [simp]: "lcm (- numeral n) x = lcm (numeral n) x"
1.1694 +  by (fact lcm_neg1)
1.1696 +lemma lcm_neg_numeral_2 [simp]: "lcm x (- numeral n) = lcm x (numeral n)"
1.1697 +  by (fact lcm_neg2)
1.1699 +function euclid_ext :: "'a \<Rightarrow> 'a \<Rightarrow> 'a \<times> 'a \<times> 'a" where
1.1700 +  "euclid_ext a b =
1.1701 +     (if b = 0 then
1.1702 +        let x = ring_inv (normalisation_factor a) in (x, 0, a * x)
1.1703 +      else
1.1704 +        case euclid_ext b (a mod b) of
1.1705 +            (s,t,c) \<Rightarrow> (t, s - t * (a div b), c))"
1.1706 +  by (pat_completeness, simp)
1.1707 +  termination by (relation "measure (euclidean_size \<circ> snd)", simp_all)
1.1709 +declare euclid_ext.simps [simp del]
1.1711 +lemma euclid_ext_0:
1.1712 +  "euclid_ext a 0 = (ring_inv (normalisation_factor a), 0, a * ring_inv (normalisation_factor a))"
1.1713 +  by (subst euclid_ext.simps, simp add: Let_def)
1.1715 +lemma euclid_ext_non_0:
1.1716 +  "b \<noteq> 0 \<Longrightarrow> euclid_ext a b = (case euclid_ext b (a mod b) of
1.1717 +    (s,t,c) \<Rightarrow> (t, s - t * (a div b), c))"
1.1718 +  by (subst euclid_ext.simps, simp)
1.1720 +definition euclid_ext' :: "'a \<Rightarrow> 'a \<Rightarrow> 'a \<times> 'a"
1.1721 +where
1.1722 +  "euclid_ext' a b = (case euclid_ext a b of (s, t, _) \<Rightarrow> (s, t))"
1.1724 +lemma euclid_ext_gcd [simp]:
1.1725 +  "(case euclid_ext a b of (_,_,t) \<Rightarrow> t) = gcd a b"
1.1726 +proof (induct a b rule: euclid_ext.induct)
1.1727 +  case (1 a b)
1.1728 +  then show ?case
1.1729 +  proof (cases "b = 0")
1.1730 +    case True
1.1731 +      then show ?thesis by (cases "a = 0")
1.1732 +        (simp_all add: euclid_ext_0 unit_div mult_ac unit_simps gcd_0)
1.1733 +    next
1.1734 +    case False with 1 show ?thesis
1.1735 +      by (simp add: euclid_ext_non_0 ac_simps split: prod.split prod.split_asm)
1.1736 +    qed
1.1737 +qed
1.1739 +lemma euclid_ext_gcd' [simp]:
1.1740 +  "euclid_ext a b = (r, s, t) \<Longrightarrow> t = gcd a b"
1.1741 +  by (insert euclid_ext_gcd[of a b], drule (1) subst, simp)
1.1743 +lemma euclid_ext_correct:
1.1744 +  "case euclid_ext x y of (s,t,c) \<Rightarrow> s*x + t*y = c"
1.1745 +proof (induct x y rule: euclid_ext.induct)
1.1746 +  case (1 x y)
1.1747 +  show ?case
1.1748 +  proof (cases "y = 0")
1.1749 +    case True
1.1750 +    then show ?thesis by (simp add: euclid_ext_0 mult_ac)
1.1751 +  next
1.1752 +    case False
1.1753 +    obtain s t c where stc: "euclid_ext y (x mod y) = (s,t,c)"
1.1754 +      by (cases "euclid_ext y (x mod y)", blast)
1.1755 +    from 1 have "c = s * y + t * (x mod y)" by (simp add: stc False)
1.1756 +    also have "... = t*((x div y)*y + x mod y) + (s - t * (x div y))*y"
1.1757 +      by (simp add: algebra_simps)
1.1758 +    also have "(x div y)*y + x mod y = x" using mod_div_equality .
1.1759 +    finally show ?thesis
1.1760 +      by (subst euclid_ext.simps, simp add: False stc)
1.1761 +    qed
1.1762 +qed
1.1764 +lemma euclid_ext'_correct:
1.1765 +  "fst (euclid_ext' a b) * a + snd (euclid_ext' a b) * b = gcd a b"
1.1766 +proof-
1.1767 +  obtain s t c where "euclid_ext a b = (s,t,c)"
1.1768 +    by (cases "euclid_ext a b", blast)
1.1769 +  with euclid_ext_correct[of a b] euclid_ext_gcd[of a b]
1.1770 +    show ?thesis unfolding euclid_ext'_def by simp
1.1771 +qed
1.1773 +lemma bezout: "\<exists>s t. s * x + t * y = gcd x y"
1.1774 +  using euclid_ext'_correct by blast
1.1776 +lemma euclid_ext'_0 [simp]: "euclid_ext' x 0 = (ring_inv (normalisation_factor x), 0)"
1.1777 +  by (simp add: bezw_def euclid_ext'_def euclid_ext_0)
1.1779 +lemma euclid_ext'_non_0: "y \<noteq> 0 \<Longrightarrow> euclid_ext' x y = (snd (euclid_ext' y (x mod y)),
1.1780 +  fst (euclid_ext' y (x mod y)) - snd (euclid_ext' y (x mod y)) * (x div y))"
1.1781 +  by (cases "euclid_ext y (x mod y)")
1.1782 +    (simp add: euclid_ext'_def euclid_ext_non_0)
1.1784 +end
1.1786 +instantiation nat :: euclidean_semiring
1.1787 +begin
1.1789 +definition [simp]:
1.1790 +  "euclidean_size_nat = (id :: nat \<Rightarrow> nat)"
1.1792 +definition [simp]:
1.1793 +  "normalisation_factor_nat (n::nat) = (if n = 0 then 0 else 1 :: nat)"
1.1795 +instance proof
1.1796 +qed (simp_all add: is_unit_def)
1.1798 +end
1.1800 +instantiation int :: euclidean_ring
1.1801 +begin
1.1803 +definition [simp]:
1.1804 +  "euclidean_size_int = (nat \<circ> abs :: int \<Rightarrow> nat)"
1.1806 +definition [simp]:
1.1807 +  "normalisation_factor_int = (sgn :: int \<Rightarrow> int)"
1.1809 +instance proof
1.1810 +  case goal2 then show ?case by (auto simp add: abs_mult nat_mult_distrib)
1.1811 +next
1.1812 +  case goal3 then show ?case by (simp add: zsgn_def is_unit_def)
1.1813 +next
1.1814 +  case goal5 then show ?case by (auto simp: zsgn_def is_unit_def)
1.1815 +next
1.1816 +  case goal6 then show ?case by (auto split: abs_split simp: zsgn_def is_unit_def)
1.1817 +qed (auto simp: sgn_times split: abs_split)
1.1819 +end
1.1821 +end