58023

1 
(* Author: Manuel Eberl *)


2 

58889

3 
section {* Abstract euclidean algorithm *}

58023

4 


5 
theory Euclidean_Algorithm


6 
imports Complex_Main


7 
begin


8 


9 
lemma finite_int_set_iff_bounded_le:


10 
"finite (N::int set) = (\<exists>m\<ge>0. \<forall>n\<in>N. abs n \<le> m)"


11 
proof


12 
assume "finite (N::int set)"


13 
hence "finite (nat ` abs ` N)" by (intro finite_imageI)


14 
hence "\<exists>m. \<forall>n\<in>nat`abs`N. n \<le> m" by (simp add: finite_nat_set_iff_bounded_le)


15 
then obtain m :: nat where "\<forall>n\<in>N. nat (abs n) \<le> nat (int m)" by auto


16 
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)


17 
next


18 
assume "\<exists>m\<ge>0. \<forall>n\<in>N. abs n \<le> m"


19 
then obtain m where "m \<ge> 0" and "\<forall>n\<in>N. abs n \<le> m" by blast


20 
hence "\<forall>n\<in>N. nat (abs n) \<le> nat m" by (auto simp: nat_le_eq_zle)


21 
hence "\<forall>n\<in>nat`abs`N. n \<le> nat m" by (auto simp: nat_le_eq_zle)


22 
hence A: "finite ((nat \<circ> abs)`N)" unfolding o_def


23 
by (subst finite_nat_set_iff_bounded_le) blast


24 
{


25 
assume "\<not>finite N"


26 
from pigeonhole_infinite[OF this A] obtain x


27 
where "x \<in> N" and B: "~finite {a\<in>N. nat (abs a) = nat (abs x)}"


28 
unfolding o_def by blast


29 
have "{a\<in>N. nat (abs a) = nat (abs x)} \<subseteq> {x, x}" by auto


30 
hence "finite {a\<in>N. nat (abs a) = nat (abs x)}" by (rule finite_subset) simp


31 
with B have False by contradiction


32 
}


33 
then show "finite N" by blast


34 
qed


35 


36 
context semiring_div


37 
begin


38 


39 
lemma dvd_setprod [intro]:


40 
assumes "finite A" and "x \<in> A"


41 
shows "f x dvd setprod f A"


42 
proof


43 
from `finite A` have "setprod f (insert x (A  {x})) = f x * setprod f (A  {x})"


44 
by (intro setprod.insert) auto


45 
also from `x \<in> A` have "insert x (A  {x}) = A" by blast


46 
finally show "setprod f A = f x * setprod f (A  {x})" .


47 
qed


48 


49 
lemma dvd_mult_cancel_left:


50 
assumes "a \<noteq> 0" and "a * b dvd a * c"


51 
shows "b dvd c"


52 
proof


53 
from assms(2) obtain k where "a * c = a * b * k" unfolding dvd_def by blast


54 
hence "c * a = b * k * a" by (simp add: ac_simps)


55 
hence "c * (a div a) = b * k * (a div a)" by (simp add: div_mult_swap)


56 
also from `a \<noteq> 0` have "a div a = 1" by simp


57 
finally show ?thesis by simp


58 
qed


59 


60 
lemma dvd_mult_cancel_right:


61 
"a \<noteq> 0 \<Longrightarrow> b * a dvd c * a \<Longrightarrow> b dvd c"


62 
by (subst (asm) (1 2) ac_simps, rule dvd_mult_cancel_left)


63 


64 
lemma nonzero_pow_nonzero:


65 
"a \<noteq> 0 \<Longrightarrow> a ^ n \<noteq> 0"


66 
by (induct n) (simp_all add: no_zero_divisors)


67 


68 
lemma zero_pow_zero: "n \<noteq> 0 \<Longrightarrow> 0 ^ n = 0"


69 
by (cases n, simp_all)


70 


71 
lemma pow_zero_iff:


72 
"n \<noteq> 0 \<Longrightarrow> a^n = 0 \<longleftrightarrow> a = 0"


73 
using nonzero_pow_nonzero zero_pow_zero by auto


74 


75 
end


76 


77 
context semiring_div


78 
begin


79 


80 
definition ring_inv :: "'a \<Rightarrow> 'a"


81 
where


82 
"ring_inv x = 1 div x"


83 


84 
definition is_unit :: "'a \<Rightarrow> bool"


85 
where


86 
"is_unit x \<longleftrightarrow> x dvd 1"


87 


88 
definition associated :: "'a \<Rightarrow> 'a \<Rightarrow> bool"


89 
where


90 
"associated x y \<longleftrightarrow> x dvd y \<and> y dvd x"


91 


92 
lemma unit_prod [intro]:


93 
"is_unit x \<Longrightarrow> is_unit y \<Longrightarrow> is_unit (x * y)"


94 
unfolding is_unit_def by (subst mult_1_left [of 1, symmetric], rule mult_dvd_mono)


95 


96 
lemma unit_ring_inv:


97 
"is_unit y \<Longrightarrow> x div y = x * ring_inv y"


98 
by (simp add: div_mult_swap ring_inv_def is_unit_def)


99 


100 
lemma unit_ring_inv_ring_inv [simp]:


101 
"is_unit x \<Longrightarrow> ring_inv (ring_inv x) = x"


102 
unfolding is_unit_def ring_inv_def


103 
by (metis div_mult_mult1_if div_mult_self1_is_id dvd_mult_div_cancel mult_1_right)


104 


105 
lemma inv_imp_eq_ring_inv:


106 
"a * b = 1 \<Longrightarrow> ring_inv a = b"


107 
by (metis dvd_mult_div_cancel dvd_mult_right mult_1_right mult.left_commute one_dvd ring_inv_def)


108 


109 
lemma ring_inv_is_inv1 [simp]:


110 
"is_unit a \<Longrightarrow> a * ring_inv a = 1"

58953

111 
unfolding is_unit_def ring_inv_def by simp

58023

112 


113 
lemma ring_inv_is_inv2 [simp]:


114 
"is_unit a \<Longrightarrow> ring_inv a * a = 1"


115 
by (simp add: ac_simps)


116 


117 
lemma unit_ring_inv_unit [simp, intro]:


118 
assumes "is_unit x"


119 
shows "is_unit (ring_inv x)"


120 
proof 


121 
from assms have "1 = ring_inv x * x" by simp


122 
then show "is_unit (ring_inv x)" unfolding is_unit_def by (rule dvdI)


123 
qed


124 


125 
lemma mult_unit_dvd_iff:


126 
"is_unit y \<Longrightarrow> x * y dvd z \<longleftrightarrow> x dvd z"


127 
proof


128 
assume "is_unit y" "x * y dvd z"


129 
then show "x dvd z" by (simp add: dvd_mult_left)


130 
next


131 
assume "is_unit y" "x dvd z"


132 
then obtain k where "z = x * k" unfolding dvd_def by blast


133 
with `is_unit y` have "z = (x * y) * (ring_inv y * k)"


134 
by (simp add: mult_ac)


135 
then show "x * y dvd z" by (rule dvdI)


136 
qed


137 


138 
lemma div_unit_dvd_iff:


139 
"is_unit y \<Longrightarrow> x div y dvd z \<longleftrightarrow> x dvd z"


140 
by (subst unit_ring_inv) (assumption, simp add: mult_unit_dvd_iff)


141 


142 
lemma dvd_mult_unit_iff:


143 
"is_unit y \<Longrightarrow> x dvd z * y \<longleftrightarrow> x dvd z"


144 
proof


145 
assume "is_unit y" and "x dvd z * y"


146 
have "z * y dvd z * (y * ring_inv y)" by (subst mult_assoc [symmetric]) simp


147 
also from `is_unit y` have "y * ring_inv y = 1" by simp


148 
finally have "z * y dvd z" by simp


149 
with `x dvd z * y` show "x dvd z" by (rule dvd_trans)


150 
next


151 
assume "x dvd z"


152 
then show "x dvd z * y" by simp


153 
qed


154 


155 
lemma dvd_div_unit_iff:


156 
"is_unit y \<Longrightarrow> x dvd z div y \<longleftrightarrow> x dvd z"


157 
by (subst unit_ring_inv) (assumption, simp add: dvd_mult_unit_iff)


158 


159 
lemmas unit_dvd_iff = mult_unit_dvd_iff div_unit_dvd_iff dvd_mult_unit_iff dvd_div_unit_iff


160 


161 
lemma unit_div [intro]:


162 
"is_unit x \<Longrightarrow> is_unit y \<Longrightarrow> is_unit (x div y)"


163 
by (subst unit_ring_inv) (assumption, rule unit_prod, simp_all)


164 


165 
lemma unit_div_mult_swap:


166 
"is_unit z \<Longrightarrow> x * (y div z) = x * y div z"


167 
by (simp only: unit_ring_inv [of _ y] unit_ring_inv [of _ "x*y"] ac_simps)


168 


169 
lemma unit_div_commute:


170 
"is_unit y \<Longrightarrow> x div y * z = x * z div y"


171 
by (simp only: unit_ring_inv [of _ x] unit_ring_inv [of _ "x*z"] ac_simps)


172 


173 
lemma unit_imp_dvd [dest]:


174 
"is_unit y \<Longrightarrow> y dvd x"


175 
by (rule dvd_trans [of _ 1]) (simp_all add: is_unit_def)


176 


177 
lemma dvd_unit_imp_unit:


178 
"is_unit y \<Longrightarrow> x dvd y \<Longrightarrow> is_unit x"


179 
by (unfold is_unit_def) (rule dvd_trans)


180 


181 
lemma ring_inv_0 [simp]:


182 
"ring_inv 0 = 0"


183 
unfolding ring_inv_def by simp


184 


185 
lemma unit_ring_inv'1:


186 
assumes "is_unit y"


187 
shows "x div (y * z) = x * ring_inv y div z"


188 
proof 


189 
from assms have "x div (y * z) = x * (ring_inv y * y) div (y * z)"


190 
by simp


191 
also have "... = y * (x * ring_inv y) div (y * z)"


192 
by (simp only: mult_ac)


193 
also have "... = x * ring_inv y div z"


194 
by (cases "y = 0", simp, rule div_mult_mult1)


195 
finally show ?thesis .


196 
qed


197 


198 
lemma associated_comm:


199 
"associated x y \<Longrightarrow> associated y x"


200 
by (simp add: associated_def)


201 


202 
lemma associated_0 [simp]:


203 
"associated 0 b \<longleftrightarrow> b = 0"


204 
"associated a 0 \<longleftrightarrow> a = 0"


205 
unfolding associated_def by simp_all


206 


207 
lemma associated_unit:


208 
"is_unit x \<Longrightarrow> associated x y \<Longrightarrow> is_unit y"


209 
unfolding associated_def by (fast dest: dvd_unit_imp_unit)


210 


211 
lemma is_unit_1 [simp]:


212 
"is_unit 1"


213 
unfolding is_unit_def by simp


214 


215 
lemma not_is_unit_0 [simp]:


216 
"\<not> is_unit 0"


217 
unfolding is_unit_def by auto


218 


219 
lemma unit_mult_left_cancel:


220 
assumes "is_unit x"


221 
shows "(x * y) = (x * z) \<longleftrightarrow> y = z"


222 
proof 


223 
from assms have "x \<noteq> 0" by auto


224 
then show ?thesis by (metis div_mult_self1_is_id)


225 
qed


226 


227 
lemma unit_mult_right_cancel:


228 
"is_unit x \<Longrightarrow> (y * x) = (z * x) \<longleftrightarrow> y = z"


229 
by (simp add: ac_simps unit_mult_left_cancel)


230 


231 
lemma unit_div_cancel:


232 
"is_unit x \<Longrightarrow> (y div x) = (z div x) \<longleftrightarrow> y = z"


233 
apply (subst unit_ring_inv[of _ y], assumption)


234 
apply (subst unit_ring_inv[of _ z], assumption)


235 
apply (rule unit_mult_right_cancel, erule unit_ring_inv_unit)


236 
done


237 


238 
lemma unit_eq_div1:


239 
"is_unit y \<Longrightarrow> x div y = z \<longleftrightarrow> x = z * y"


240 
apply (subst unit_ring_inv, assumption)


241 
apply (subst unit_mult_right_cancel[symmetric], assumption)


242 
apply (subst mult_assoc, subst ring_inv_is_inv2, assumption, simp)


243 
done


244 


245 
lemma unit_eq_div2:


246 
"is_unit y \<Longrightarrow> x = z div y \<longleftrightarrow> x * y = z"


247 
by (subst (1 2) eq_commute, simp add: unit_eq_div1, subst eq_commute, rule refl)


248 


249 
lemma associated_iff_div_unit:


250 
"associated x y \<longleftrightarrow> (\<exists>z. is_unit z \<and> x = z * y)"


251 
proof


252 
assume "associated x y"


253 
show "\<exists>z. is_unit z \<and> x = z * y"


254 
proof (cases "x = 0")


255 
assume "x = 0"


256 
then show "\<exists>z. is_unit z \<and> x = z * y" using `associated x y`


257 
by (intro exI[of _ 1], simp add: associated_def)


258 
next


259 
assume [simp]: "x \<noteq> 0"


260 
hence [simp]: "x dvd y" "y dvd x" using `associated x y`


261 
unfolding associated_def by simp_all


262 
hence "1 = x div y * (y div x)"


263 
by (simp add: div_mult_swap dvd_div_mult_self)


264 
hence "is_unit (x div y)" unfolding is_unit_def by (rule dvdI)


265 
moreover have "x = (x div y) * y" by (simp add: dvd_div_mult_self)


266 
ultimately show ?thesis by blast


267 
qed


268 
next


269 
assume "\<exists>z. is_unit z \<and> x = z * y"


270 
then obtain z where "is_unit z" and "x = z * y" by blast


271 
hence "y = x * ring_inv z" by (simp add: algebra_simps)


272 
hence "x dvd y" by simp


273 
moreover from `x = z * y` have "y dvd x" by simp


274 
ultimately show "associated x y" unfolding associated_def by simp


275 
qed


276 


277 
lemmas unit_simps = mult_unit_dvd_iff div_unit_dvd_iff dvd_mult_unit_iff


278 
dvd_div_unit_iff unit_div_mult_swap unit_div_commute


279 
unit_mult_left_cancel unit_mult_right_cancel unit_div_cancel


280 
unit_eq_div1 unit_eq_div2


281 


282 
end


283 


284 
context ring_div


285 
begin


286 


287 
lemma is_unit_neg [simp]:


288 
"is_unit ( x) \<Longrightarrow> is_unit x"


289 
unfolding is_unit_def by simp


290 


291 
lemma is_unit_neg_1 [simp]:


292 
"is_unit (1)"


293 
unfolding is_unit_def by simp


294 


295 
end


296 


297 
lemma is_unit_nat [simp]:


298 
"is_unit (x::nat) \<longleftrightarrow> x = 1"


299 
unfolding is_unit_def by simp


300 


301 
lemma is_unit_int:


302 
"is_unit (x::int) \<longleftrightarrow> x = 1 \<or> x = 1"


303 
unfolding is_unit_def by auto


304 


305 
text {*


306 
A Euclidean semiring is a semiring upon which the Euclidean algorithm can be


307 
implemented. It must provide:


308 
\begin{itemize}


309 
\item division with remainder


310 
\item a size function such that @{term "size (a mod b) < size b"}


311 
for any @{term "b \<noteq> 0"}


312 
\item a normalisation factor such that two associated numbers are equal iff


313 
they are the same when divided by their normalisation factors.


314 
\end{itemize}


315 
The existence of these functions makes it possible to derive gcd and lcm functions


316 
for any Euclidean semiring.


317 
*}


318 
class euclidean_semiring = semiring_div +


319 
fixes euclidean_size :: "'a \<Rightarrow> nat"


320 
fixes normalisation_factor :: "'a \<Rightarrow> 'a"


321 
assumes mod_size_less [simp]:


322 
"b \<noteq> 0 \<Longrightarrow> euclidean_size (a mod b) < euclidean_size b"


323 
assumes size_mult_mono:


324 
"b \<noteq> 0 \<Longrightarrow> euclidean_size (a * b) \<ge> euclidean_size a"


325 
assumes normalisation_factor_is_unit [intro,simp]:


326 
"a \<noteq> 0 \<Longrightarrow> is_unit (normalisation_factor a)"


327 
assumes normalisation_factor_mult: "normalisation_factor (a * b) =


328 
normalisation_factor a * normalisation_factor b"


329 
assumes normalisation_factor_unit: "is_unit x \<Longrightarrow> normalisation_factor x = x"


330 
assumes normalisation_factor_0 [simp]: "normalisation_factor 0 = 0"


331 
begin


332 


333 
lemma normalisation_factor_dvd [simp]:


334 
"a \<noteq> 0 \<Longrightarrow> normalisation_factor a dvd b"


335 
by (rule unit_imp_dvd, simp)


336 


337 
lemma normalisation_factor_1 [simp]:


338 
"normalisation_factor 1 = 1"


339 
by (simp add: normalisation_factor_unit)


340 


341 
lemma normalisation_factor_0_iff [simp]:


342 
"normalisation_factor x = 0 \<longleftrightarrow> x = 0"


343 
proof


344 
assume "normalisation_factor x = 0"


345 
hence "\<not> is_unit (normalisation_factor x)"


346 
by (metis not_is_unit_0)


347 
then show "x = 0" by force


348 
next


349 
assume "x = 0"


350 
then show "normalisation_factor x = 0" by simp


351 
qed


352 


353 
lemma normalisation_factor_pow:


354 
"normalisation_factor (x ^ n) = normalisation_factor x ^ n"


355 
by (induct n) (simp_all add: normalisation_factor_mult power_Suc2)


356 


357 
lemma normalisation_correct [simp]:


358 
"normalisation_factor (x div normalisation_factor x) = (if x = 0 then 0 else 1)"


359 
proof (cases "x = 0", simp)


360 
assume "x \<noteq> 0"


361 
let ?nf = "normalisation_factor"


362 
from normalisation_factor_is_unit[OF `x \<noteq> 0`] have "?nf x \<noteq> 0"


363 
by (metis not_is_unit_0)


364 
have "?nf (x div ?nf x) * ?nf (?nf x) = ?nf (x div ?nf x * ?nf x)"


365 
by (simp add: normalisation_factor_mult)


366 
also have "x div ?nf x * ?nf x = x" using `x \<noteq> 0`


367 
by (simp add: dvd_div_mult_self)


368 
also have "?nf (?nf x) = ?nf x" using `x \<noteq> 0`


369 
normalisation_factor_is_unit normalisation_factor_unit by simp


370 
finally show ?thesis using `x \<noteq> 0` and `?nf x \<noteq> 0`


371 
by (metis div_mult_self2_is_id div_self)


372 
qed


373 


374 
lemma normalisation_0_iff [simp]:


375 
"x div normalisation_factor x = 0 \<longleftrightarrow> x = 0"


376 
by (cases "x = 0", simp, subst unit_eq_div1, blast, simp)


377 


378 
lemma associated_iff_normed_eq:


379 
"associated a b \<longleftrightarrow> a div normalisation_factor a = b div normalisation_factor b"


380 
proof (cases "b = 0", simp, cases "a = 0", metis associated_0(1) normalisation_0_iff, rule iffI)


381 
let ?nf = normalisation_factor


382 
assume "a \<noteq> 0" "b \<noteq> 0" "a div ?nf a = b div ?nf b"


383 
hence "a = b * (?nf a div ?nf b)"


384 
apply (subst (asm) unit_eq_div1, blast, subst (asm) unit_div_commute, blast)


385 
apply (subst div_mult_swap, simp, simp)


386 
done


387 
with `a \<noteq> 0` `b \<noteq> 0` have "\<exists>z. is_unit z \<and> a = z * b"


388 
by (intro exI[of _ "?nf a div ?nf b"], force simp: mult_ac)


389 
with associated_iff_div_unit show "associated a b" by simp


390 
next


391 
let ?nf = normalisation_factor


392 
assume "a \<noteq> 0" "b \<noteq> 0" "associated a b"


393 
with associated_iff_div_unit obtain z where "is_unit z" and "a = z * b" by blast


394 
then show "a div ?nf a = b div ?nf b"


395 
apply (simp only: `a = z * b` normalisation_factor_mult normalisation_factor_unit)


396 
apply (rule div_mult_mult1, force)


397 
done


398 
qed


399 


400 
lemma normed_associated_imp_eq:


401 
"associated a b \<Longrightarrow> normalisation_factor a \<in> {0, 1} \<Longrightarrow> normalisation_factor b \<in> {0, 1} \<Longrightarrow> a = b"


402 
by (simp add: associated_iff_normed_eq, elim disjE, simp_all)


403 


404 
lemmas normalisation_factor_dvd_iff [simp] =


405 
unit_dvd_iff [OF normalisation_factor_is_unit]


406 


407 
lemma euclidean_division:


408 
fixes a :: 'a and b :: 'a


409 
assumes "b \<noteq> 0"


410 
obtains s and t where "a = s * b + t"


411 
and "euclidean_size t < euclidean_size b"


412 
proof 


413 
from div_mod_equality[of a b 0]


414 
have "a = a div b * b + a mod b" by simp


415 
with that and assms show ?thesis by force


416 
qed


417 


418 
lemma dvd_euclidean_size_eq_imp_dvd:


419 
assumes "a \<noteq> 0" and b_dvd_a: "b dvd a" and size_eq: "euclidean_size a = euclidean_size b"


420 
shows "a dvd b"


421 
proof (subst dvd_eq_mod_eq_0, rule ccontr)


422 
assume "b mod a \<noteq> 0"


423 
from b_dvd_a have b_dvd_mod: "b dvd b mod a" by (simp add: dvd_mod_iff)


424 
from b_dvd_mod obtain c where "b mod a = b * c" unfolding dvd_def by blast


425 
with `b mod a \<noteq> 0` have "c \<noteq> 0" by auto


426 
with `b mod a = b * c` have "euclidean_size (b mod a) \<ge> euclidean_size b"


427 
using size_mult_mono by force


428 
moreover from `a \<noteq> 0` have "euclidean_size (b mod a) < euclidean_size a"


429 
using mod_size_less by blast


430 
ultimately show False using size_eq by simp


431 
qed


432 


433 
function gcd_eucl :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"


434 
where


435 
"gcd_eucl a b = (if b = 0 then a div normalisation_factor a else gcd_eucl b (a mod b))"


436 
by (pat_completeness, simp)


437 
termination by (relation "measure (euclidean_size \<circ> snd)", simp_all)


438 


439 
declare gcd_eucl.simps [simp del]


440 


441 
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"


442 
proof (induct a b rule: gcd_eucl.induct)


443 
case ("1" m n)


444 
then show ?case by (cases "n = 0") auto


445 
qed


446 


447 
definition lcm_eucl :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"


448 
where


449 
"lcm_eucl a b = a * b div (gcd_eucl a b * normalisation_factor (a * b))"


450 


451 
(* Somewhat complicated definition of Lcm that has the advantage of working


452 
for infinite sets as well *)


453 


454 
definition Lcm_eucl :: "'a set \<Rightarrow> 'a"


455 
where


456 
"Lcm_eucl A = (if \<exists>l. l \<noteq> 0 \<and> (\<forall>x\<in>A. x dvd l) then


457 
let l = SOME l. l \<noteq> 0 \<and> (\<forall>x\<in>A. x dvd l) \<and> euclidean_size l =


458 
(LEAST n. \<exists>l. l \<noteq> 0 \<and> (\<forall>x\<in>A. x dvd l) \<and> euclidean_size l = n)


459 
in l div normalisation_factor l


460 
else 0)"


461 


462 
definition Gcd_eucl :: "'a set \<Rightarrow> 'a"


463 
where


464 
"Gcd_eucl A = Lcm_eucl {d. \<forall>a\<in>A. d dvd a}"


465 


466 
end


467 


468 
class euclidean_semiring_gcd = euclidean_semiring + gcd + Gcd +


469 
assumes gcd_gcd_eucl: "gcd = gcd_eucl" and lcm_lcm_eucl: "lcm = lcm_eucl"


470 
assumes Gcd_Gcd_eucl: "Gcd = Gcd_eucl" and Lcm_Lcm_eucl: "Lcm = Lcm_eucl"


471 
begin


472 


473 
lemma gcd_red:


474 
"gcd x y = gcd y (x mod y)"


475 
by (metis gcd_eucl.simps mod_0 mod_by_0 gcd_gcd_eucl)


476 


477 
lemma gcd_non_0:


478 
"y \<noteq> 0 \<Longrightarrow> gcd x y = gcd y (x mod y)"


479 
by (rule gcd_red)


480 


481 
lemma gcd_0_left:


482 
"gcd 0 x = x div normalisation_factor x"


483 
by (simp only: gcd_gcd_eucl, subst gcd_eucl.simps, subst gcd_eucl.simps, simp add: Let_def)


484 


485 
lemma gcd_0:


486 
"gcd x 0 = x div normalisation_factor x"


487 
by (simp only: gcd_gcd_eucl, subst gcd_eucl.simps, simp add: Let_def)


488 


489 
lemma gcd_dvd1 [iff]: "gcd x y dvd x"


490 
and gcd_dvd2 [iff]: "gcd x y dvd y"


491 
proof (induct x y rule: gcd_eucl.induct)


492 
fix x y :: 'a


493 
assume IH1: "y \<noteq> 0 \<Longrightarrow> gcd y (x mod y) dvd y"


494 
assume IH2: "y \<noteq> 0 \<Longrightarrow> gcd y (x mod y) dvd (x mod y)"


495 


496 
have "gcd x y dvd x \<and> gcd x y dvd y"


497 
proof (cases "y = 0")


498 
case True


499 
then show ?thesis by (cases "x = 0", simp_all add: gcd_0)


500 
next


501 
case False


502 
with IH1 and IH2 show ?thesis by (simp add: gcd_non_0 dvd_mod_iff)


503 
qed


504 
then show "gcd x y dvd x" "gcd x y dvd y" by simp_all


505 
qed


506 


507 
lemma dvd_gcd_D1: "k dvd gcd m n \<Longrightarrow> k dvd m"


508 
by (rule dvd_trans, assumption, rule gcd_dvd1)


509 


510 
lemma dvd_gcd_D2: "k dvd gcd m n \<Longrightarrow> k dvd n"


511 
by (rule dvd_trans, assumption, rule gcd_dvd2)


512 


513 
lemma gcd_greatest:


514 
fixes k x y :: 'a


515 
shows "k dvd x \<Longrightarrow> k dvd y \<Longrightarrow> k dvd gcd x y"


516 
proof (induct x y rule: gcd_eucl.induct)


517 
case (1 x y)


518 
show ?case


519 
proof (cases "y = 0")


520 
assume "y = 0"


521 
with 1 show ?thesis by (cases "x = 0", simp_all add: gcd_0)


522 
next


523 
assume "y \<noteq> 0"


524 
with 1 show ?thesis by (simp add: gcd_non_0 dvd_mod_iff)


525 
qed


526 
qed


527 


528 
lemma dvd_gcd_iff:


529 
"k dvd gcd x y \<longleftrightarrow> k dvd x \<and> k dvd y"


530 
by (blast intro!: gcd_greatest intro: dvd_trans)


531 


532 
lemmas gcd_greatest_iff = dvd_gcd_iff


533 


534 
lemma gcd_zero [simp]:


535 
"gcd x y = 0 \<longleftrightarrow> x = 0 \<and> y = 0"


536 
by (metis dvd_0_left dvd_refl gcd_dvd1 gcd_dvd2 gcd_greatest)+


537 


538 
lemma normalisation_factor_gcd [simp]:


539 
"normalisation_factor (gcd x y) = (if x = 0 \<and> y = 0 then 0 else 1)" (is "?f x y = ?g x y")


540 
proof (induct x y rule: gcd_eucl.induct)


541 
fix x y :: 'a


542 
assume IH: "y \<noteq> 0 \<Longrightarrow> ?f y (x mod y) = ?g y (x mod y)"


543 
then show "?f x y = ?g x y" by (cases "y = 0", auto simp: gcd_non_0 gcd_0)


544 
qed


545 


546 
lemma gcdI:


547 
"k dvd x \<Longrightarrow> k dvd y \<Longrightarrow> (\<And>l. l dvd x \<Longrightarrow> l dvd y \<Longrightarrow> l dvd k)


548 
\<Longrightarrow> normalisation_factor k = (if k = 0 then 0 else 1) \<Longrightarrow> k = gcd x y"


549 
by (intro normed_associated_imp_eq) (auto simp: associated_def intro: gcd_greatest)


550 


551 
sublocale gcd!: abel_semigroup gcd


552 
proof


553 
fix x y z


554 
show "gcd (gcd x y) z = gcd x (gcd y z)"


555 
proof (rule gcdI)


556 
have "gcd (gcd x y) z dvd gcd x y" "gcd x y dvd x" by simp_all


557 
then show "gcd (gcd x y) z dvd x" by (rule dvd_trans)


558 
have "gcd (gcd x y) z dvd gcd x y" "gcd x y dvd y" by simp_all


559 
hence "gcd (gcd x y) z dvd y" by (rule dvd_trans)


560 
moreover have "gcd (gcd x y) z dvd z" by simp


561 
ultimately show "gcd (gcd x y) z dvd gcd y z"


562 
by (rule gcd_greatest)


563 
show "normalisation_factor (gcd (gcd x y) z) = (if gcd (gcd x y) z = 0 then 0 else 1)"


564 
by auto


565 
fix l assume "l dvd x" and "l dvd gcd y z"


566 
with dvd_trans[OF _ gcd_dvd1] and dvd_trans[OF _ gcd_dvd2]


567 
have "l dvd y" and "l dvd z" by blast+


568 
with `l dvd x` show "l dvd gcd (gcd x y) z"


569 
by (intro gcd_greatest)


570 
qed


571 
next


572 
fix x y


573 
show "gcd x y = gcd y x"


574 
by (rule gcdI) (simp_all add: gcd_greatest)


575 
qed


576 


577 
lemma gcd_unique: "d dvd a \<and> d dvd b \<and>


578 
normalisation_factor d = (if d = 0 then 0 else 1) \<and>


579 
(\<forall>e. e dvd a \<and> e dvd b \<longrightarrow> e dvd d) \<longleftrightarrow> d = gcd a b"


580 
by (rule, auto intro: gcdI simp: gcd_greatest)


581 


582 
lemma gcd_dvd_prod: "gcd a b dvd k * b"


583 
using mult_dvd_mono [of 1] by auto


584 


585 
lemma gcd_1_left [simp]: "gcd 1 x = 1"


586 
by (rule sym, rule gcdI, simp_all)


587 


588 
lemma gcd_1 [simp]: "gcd x 1 = 1"


589 
by (rule sym, rule gcdI, simp_all)


590 


591 
lemma gcd_proj2_if_dvd:


592 
"y dvd x \<Longrightarrow> gcd x y = y div normalisation_factor y"


593 
by (cases "y = 0", simp_all add: dvd_eq_mod_eq_0 gcd_non_0 gcd_0)


594 


595 
lemma gcd_proj1_if_dvd:


596 
"x dvd y \<Longrightarrow> gcd x y = x div normalisation_factor x"


597 
by (subst gcd.commute, simp add: gcd_proj2_if_dvd)


598 


599 
lemma gcd_proj1_iff: "gcd m n = m div normalisation_factor m \<longleftrightarrow> m dvd n"


600 
proof


601 
assume A: "gcd m n = m div normalisation_factor m"


602 
show "m dvd n"


603 
proof (cases "m = 0")


604 
assume [simp]: "m \<noteq> 0"


605 
from A have B: "m = gcd m n * normalisation_factor m"


606 
by (simp add: unit_eq_div2)


607 
show ?thesis by (subst B, simp add: mult_unit_dvd_iff)


608 
qed (insert A, simp)


609 
next


610 
assume "m dvd n"


611 
then show "gcd m n = m div normalisation_factor m" by (rule gcd_proj1_if_dvd)


612 
qed


613 


614 
lemma gcd_proj2_iff: "gcd m n = n div normalisation_factor n \<longleftrightarrow> n dvd m"


615 
by (subst gcd.commute, simp add: gcd_proj1_iff)


616 


617 
lemma gcd_mod1 [simp]:


618 
"gcd (x mod y) y = gcd x y"


619 
by (rule gcdI, metis dvd_mod_iff gcd_dvd1 gcd_dvd2, simp_all add: gcd_greatest dvd_mod_iff)


620 


621 
lemma gcd_mod2 [simp]:


622 
"gcd x (y mod x) = gcd x y"


623 
by (rule gcdI, simp, metis dvd_mod_iff gcd_dvd1 gcd_dvd2, simp_all add: gcd_greatest dvd_mod_iff)


624 


625 
lemma normalisation_factor_dvd' [simp]:


626 
"normalisation_factor x dvd x"


627 
by (cases "x = 0", simp_all)


628 


629 
lemma gcd_mult_distrib':


630 
"k div normalisation_factor k * gcd x y = gcd (k*x) (k*y)"


631 
proof (induct x y rule: gcd_eucl.induct)


632 
case (1 x y)


633 
show ?case


634 
proof (cases "y = 0")


635 
case True


636 
then show ?thesis by (simp add: normalisation_factor_mult gcd_0 algebra_simps div_mult_div_if_dvd)


637 
next


638 
case False


639 
hence "k div normalisation_factor k * gcd x y = gcd (k * y) (k * (x mod y))"


640 
using 1 by (subst gcd_red, simp)


641 
also have "... = gcd (k * x) (k * y)"


642 
by (simp add: mult_mod_right gcd.commute)


643 
finally show ?thesis .


644 
qed


645 
qed


646 


647 
lemma gcd_mult_distrib:


648 
"k * gcd x y = gcd (k*x) (k*y) * normalisation_factor k"


649 
proof


650 
let ?nf = "normalisation_factor"


651 
from gcd_mult_distrib'


652 
have "gcd (k*x) (k*y) = k div ?nf k * gcd x y" ..


653 
also have "... = k * gcd x y div ?nf k"


654 
by (metis dvd_div_mult dvd_eq_mod_eq_0 mod_0 normalisation_factor_dvd)


655 
finally show ?thesis


656 
by (simp add: ac_simps dvd_mult_div_cancel)


657 
qed


658 


659 
lemma euclidean_size_gcd_le1 [simp]:


660 
assumes "a \<noteq> 0"


661 
shows "euclidean_size (gcd a b) \<le> euclidean_size a"


662 
proof 


663 
have "gcd a b dvd a" by (rule gcd_dvd1)


664 
then obtain c where A: "a = gcd a b * c" unfolding dvd_def by blast


665 
with `a \<noteq> 0` show ?thesis by (subst (2) A, intro size_mult_mono) auto


666 
qed


667 


668 
lemma euclidean_size_gcd_le2 [simp]:


669 
"b \<noteq> 0 \<Longrightarrow> euclidean_size (gcd a b) \<le> euclidean_size b"


670 
by (subst gcd.commute, rule euclidean_size_gcd_le1)


671 


672 
lemma euclidean_size_gcd_less1:


673 
assumes "a \<noteq> 0" and "\<not>a dvd b"


674 
shows "euclidean_size (gcd a b) < euclidean_size a"


675 
proof (rule ccontr)


676 
assume "\<not>euclidean_size (gcd a b) < euclidean_size a"


677 
with `a \<noteq> 0` have "euclidean_size (gcd a b) = euclidean_size a"


678 
by (intro le_antisym, simp_all)


679 
with assms have "a dvd gcd a b" by (auto intro: dvd_euclidean_size_eq_imp_dvd)


680 
hence "a dvd b" using dvd_gcd_D2 by blast


681 
with `\<not>a dvd b` show False by contradiction


682 
qed


683 


684 
lemma euclidean_size_gcd_less2:


685 
assumes "b \<noteq> 0" and "\<not>b dvd a"


686 
shows "euclidean_size (gcd a b) < euclidean_size b"


687 
using assms by (subst gcd.commute, rule euclidean_size_gcd_less1)


688 


689 
lemma gcd_mult_unit1: "is_unit a \<Longrightarrow> gcd (x*a) y = gcd x y"


690 
apply (rule gcdI)


691 
apply (rule dvd_trans, rule gcd_dvd1, simp add: unit_simps)


692 
apply (rule gcd_dvd2)


693 
apply (rule gcd_greatest, simp add: unit_simps, assumption)


694 
apply (subst normalisation_factor_gcd, simp add: gcd_0)


695 
done


696 


697 
lemma gcd_mult_unit2: "is_unit a \<Longrightarrow> gcd x (y*a) = gcd x y"


698 
by (subst gcd.commute, subst gcd_mult_unit1, assumption, rule gcd.commute)


699 


700 
lemma gcd_div_unit1: "is_unit a \<Longrightarrow> gcd (x div a) y = gcd x y"


701 
by (simp add: unit_ring_inv gcd_mult_unit1)


702 


703 
lemma gcd_div_unit2: "is_unit a \<Longrightarrow> gcd x (y div a) = gcd x y"


704 
by (simp add: unit_ring_inv gcd_mult_unit2)


705 


706 
lemma gcd_idem: "gcd x x = x div normalisation_factor x"


707 
by (cases "x = 0") (simp add: gcd_0_left, rule sym, rule gcdI, simp_all)


708 


709 
lemma gcd_right_idem: "gcd (gcd p q) q = gcd p q"


710 
apply (rule gcdI)


711 
apply (simp add: ac_simps)


712 
apply (rule gcd_dvd2)


713 
apply (rule gcd_greatest, erule (1) gcd_greatest, assumption)


714 
apply (simp add: gcd_zero)


715 
done


716 


717 
lemma gcd_left_idem: "gcd p (gcd p q) = gcd p q"


718 
apply (rule gcdI)


719 
apply simp


720 
apply (rule dvd_trans, rule gcd_dvd2, rule gcd_dvd2)


721 
apply (rule gcd_greatest, assumption, erule gcd_greatest, assumption)


722 
apply (simp add: gcd_zero)


723 
done


724 


725 
lemma comp_fun_idem_gcd: "comp_fun_idem gcd"


726 
proof


727 
fix a b show "gcd a \<circ> gcd b = gcd b \<circ> gcd a"


728 
by (simp add: fun_eq_iff ac_simps)


729 
next


730 
fix a show "gcd a \<circ> gcd a = gcd a"


731 
by (simp add: fun_eq_iff gcd_left_idem)


732 
qed


733 


734 
lemma coprime_dvd_mult:


735 
assumes "gcd k n = 1" and "k dvd m * n"


736 
shows "k dvd m"


737 
proof 


738 
let ?nf = "normalisation_factor"


739 
from assms gcd_mult_distrib [of m k n]


740 
have A: "m = gcd (m * k) (m * n) * ?nf m" by simp


741 
from `k dvd m * n` show ?thesis by (subst A, simp_all add: gcd_greatest)


742 
qed


743 


744 
lemma coprime_dvd_mult_iff:


745 
"gcd k n = 1 \<Longrightarrow> (k dvd m * n) = (k dvd m)"


746 
by (rule, rule coprime_dvd_mult, simp_all)


747 


748 
lemma gcd_dvd_antisym:


749 
"gcd a b dvd gcd c d \<Longrightarrow> gcd c d dvd gcd a b \<Longrightarrow> gcd a b = gcd c d"


750 
proof (rule gcdI)


751 
assume A: "gcd a b dvd gcd c d" and B: "gcd c d dvd gcd a b"


752 
have "gcd c d dvd c" by simp


753 
with A show "gcd a b dvd c" by (rule dvd_trans)


754 
have "gcd c d dvd d" by simp


755 
with A show "gcd a b dvd d" by (rule dvd_trans)


756 
show "normalisation_factor (gcd a b) = (if gcd a b = 0 then 0 else 1)"


757 
by (simp add: gcd_zero)


758 
fix l assume "l dvd c" and "l dvd d"


759 
hence "l dvd gcd c d" by (rule gcd_greatest)


760 
from this and B show "l dvd gcd a b" by (rule dvd_trans)


761 
qed


762 


763 
lemma gcd_mult_cancel:


764 
assumes "gcd k n = 1"


765 
shows "gcd (k * m) n = gcd m n"


766 
proof (rule gcd_dvd_antisym)


767 
have "gcd (gcd (k * m) n) k = gcd (gcd k n) (k * m)" by (simp add: ac_simps)


768 
also note `gcd k n = 1`


769 
finally have "gcd (gcd (k * m) n) k = 1" by simp


770 
hence "gcd (k * m) n dvd m" by (rule coprime_dvd_mult, simp add: ac_simps)


771 
moreover have "gcd (k * m) n dvd n" by simp


772 
ultimately show "gcd (k * m) n dvd gcd m n" by (rule gcd_greatest)


773 
have "gcd m n dvd (k * m)" and "gcd m n dvd n" by simp_all


774 
then show "gcd m n dvd gcd (k * m) n" by (rule gcd_greatest)


775 
qed


776 


777 
lemma coprime_crossproduct:


778 
assumes [simp]: "gcd a d = 1" "gcd b c = 1"


779 
shows "associated (a * c) (b * d) \<longleftrightarrow> associated a b \<and> associated c d" (is "?lhs \<longleftrightarrow> ?rhs")


780 
proof


781 
assume ?rhs then show ?lhs unfolding associated_def by (fast intro: mult_dvd_mono)


782 
next


783 
assume ?lhs


784 
from `?lhs` have "a dvd b * d" unfolding associated_def by (metis dvd_mult_left)


785 
hence "a dvd b" by (simp add: coprime_dvd_mult_iff)


786 
moreover from `?lhs` have "b dvd a * c" unfolding associated_def by (metis dvd_mult_left)


787 
hence "b dvd a" by (simp add: coprime_dvd_mult_iff)


788 
moreover from `?lhs` have "c dvd d * b"


789 
unfolding associated_def by (metis dvd_mult_right ac_simps)


790 
hence "c dvd d" by (simp add: coprime_dvd_mult_iff gcd.commute)


791 
moreover from `?lhs` have "d dvd c * a"


792 
unfolding associated_def by (metis dvd_mult_right ac_simps)


793 
hence "d dvd c" by (simp add: coprime_dvd_mult_iff gcd.commute)


794 
ultimately show ?rhs unfolding associated_def by simp


795 
qed


796 


797 
lemma gcd_add1 [simp]:


798 
"gcd (m + n) n = gcd m n"


799 
by (cases "n = 0", simp_all add: gcd_non_0)


800 


801 
lemma gcd_add2 [simp]:


802 
"gcd m (m + n) = gcd m n"


803 
using gcd_add1 [of n m] by (simp add: ac_simps)


804 


805 
lemma gcd_add_mult: "gcd m (k * m + n) = gcd m n"


806 
by (subst gcd.commute, subst gcd_red, simp)


807 


808 
lemma coprimeI: "(\<And>l. \<lbrakk>l dvd x; l dvd y\<rbrakk> \<Longrightarrow> l dvd 1) \<Longrightarrow> gcd x y = 1"


809 
by (rule sym, rule gcdI, simp_all)


810 


811 
lemma coprime: "gcd a b = 1 \<longleftrightarrow> (\<forall>d. d dvd a \<and> d dvd b \<longleftrightarrow> is_unit d)"


812 
by (auto simp: is_unit_def intro: coprimeI gcd_greatest dvd_gcd_D1 dvd_gcd_D2)


813 


814 
lemma div_gcd_coprime:


815 
assumes nz: "a \<noteq> 0 \<or> b \<noteq> 0"


816 
defines [simp]: "d \<equiv> gcd a b"


817 
defines [simp]: "a' \<equiv> a div d" and [simp]: "b' \<equiv> b div d"


818 
shows "gcd a' b' = 1"


819 
proof (rule coprimeI)


820 
fix l assume "l dvd a'" "l dvd b'"


821 
then obtain s t where "a' = l * s" "b' = l * t" unfolding dvd_def by blast


822 
moreover have "a = a' * d" "b = b' * d" by (simp_all add: dvd_div_mult_self)


823 
ultimately have "a = (l * d) * s" "b = (l * d) * t"


824 
by (metis ac_simps)+


825 
hence "l*d dvd a" and "l*d dvd b" by (simp_all only: dvd_triv_left)


826 
hence "l*d dvd d" by (simp add: gcd_greatest)


827 
then obtain u where "u * l * d = d" unfolding dvd_def


828 
by (metis ac_simps mult_assoc)


829 
moreover from nz have "d \<noteq> 0" by (simp add: gcd_zero)


830 
ultimately have "u * l = 1"


831 
by (metis div_mult_self1_is_id div_self ac_simps)


832 
then show "l dvd 1" by force


833 
qed


834 


835 
lemma coprime_mult:


836 
assumes da: "gcd d a = 1" and db: "gcd d b = 1"


837 
shows "gcd d (a * b) = 1"


838 
apply (subst gcd.commute)


839 
using da apply (subst gcd_mult_cancel)


840 
apply (subst gcd.commute, assumption)


841 
apply (subst gcd.commute, rule db)


842 
done


843 


844 
lemma coprime_lmult:


845 
assumes dab: "gcd d (a * b) = 1"


846 
shows "gcd d a = 1"


847 
proof (rule coprimeI)


848 
fix l assume "l dvd d" and "l dvd a"


849 
hence "l dvd a * b" by simp


850 
with `l dvd d` and dab show "l dvd 1" by (auto intro: gcd_greatest)


851 
qed


852 


853 
lemma coprime_rmult:


854 
assumes dab: "gcd d (a * b) = 1"


855 
shows "gcd d b = 1"


856 
proof (rule coprimeI)


857 
fix l assume "l dvd d" and "l dvd b"


858 
hence "l dvd a * b" by simp


859 
with `l dvd d` and dab show "l dvd 1" by (auto intro: gcd_greatest)


860 
qed


861 


862 
lemma coprime_mul_eq: "gcd d (a * b) = 1 \<longleftrightarrow> gcd d a = 1 \<and> gcd d b = 1"


863 
using coprime_rmult[of d a b] coprime_lmult[of d a b] coprime_mult[of d a b] by blast


864 


865 
lemma gcd_coprime:


866 
assumes z: "gcd a b \<noteq> 0" and a: "a = a' * gcd a b" and b: "b = b' * gcd a b"


867 
shows "gcd a' b' = 1"


868 
proof 


869 
from z have "a \<noteq> 0 \<or> b \<noteq> 0" by (simp add: gcd_zero)


870 
with div_gcd_coprime have "gcd (a div gcd a b) (b div gcd a b) = 1" .


871 
also from assms have "a div gcd a b = a'" by (metis div_mult_self2_is_id)+


872 
also from assms have "b div gcd a b = b'" by (metis div_mult_self2_is_id)+


873 
finally show ?thesis .


874 
qed


875 


876 
lemma coprime_power:


877 
assumes "0 < n"


878 
shows "gcd a (b ^ n) = 1 \<longleftrightarrow> gcd a b = 1"


879 
using assms proof (induct n)


880 
case (Suc n) then show ?case


881 
by (cases n) (simp_all add: coprime_mul_eq)


882 
qed simp


883 


884 
lemma gcd_coprime_exists:


885 
assumes nz: "gcd a b \<noteq> 0"


886 
shows "\<exists>a' b'. a = a' * gcd a b \<and> b = b' * gcd a b \<and> gcd a' b' = 1"


887 
apply (rule_tac x = "a div gcd a b" in exI)


888 
apply (rule_tac x = "b div gcd a b" in exI)


889 
apply (insert nz, auto simp add: dvd_div_mult gcd_0_left gcd_zero intro: div_gcd_coprime)


890 
done


891 


892 
lemma coprime_exp:


893 
"gcd d a = 1 \<Longrightarrow> gcd d (a^n) = 1"


894 
by (induct n, simp_all add: coprime_mult)


895 


896 
lemma coprime_exp2 [intro]:


897 
"gcd a b = 1 \<Longrightarrow> gcd (a^n) (b^m) = 1"


898 
apply (rule coprime_exp)


899 
apply (subst gcd.commute)


900 
apply (rule coprime_exp)


901 
apply (subst gcd.commute)


902 
apply assumption


903 
done


904 


905 
lemma gcd_exp:


906 
"gcd (a^n) (b^n) = (gcd a b) ^ n"


907 
proof (cases "a = 0 \<and> b = 0")


908 
assume "a = 0 \<and> b = 0"


909 
then show ?thesis by (cases n, simp_all add: gcd_0_left)


910 
next


911 
assume A: "\<not>(a = 0 \<and> b = 0)"


912 
hence "1 = gcd ((a div gcd a b)^n) ((b div gcd a b)^n)"


913 
using div_gcd_coprime by (subst sym, auto simp: div_gcd_coprime)


914 
hence "(gcd a b) ^ n = (gcd a b) ^ n * ..." by simp


915 
also note gcd_mult_distrib


916 
also have "normalisation_factor ((gcd a b)^n) = 1"


917 
by (simp add: normalisation_factor_pow A)


918 
also have "(gcd a b)^n * (a div gcd a b)^n = a^n"


919 
by (subst ac_simps, subst div_power, simp, rule dvd_div_mult_self, rule dvd_power_same, simp)


920 
also have "(gcd a b)^n * (b div gcd a b)^n = b^n"


921 
by (subst ac_simps, subst div_power, simp, rule dvd_div_mult_self, rule dvd_power_same, simp)


922 
finally show ?thesis by simp


923 
qed


924 


925 
lemma coprime_common_divisor:


926 
"gcd a b = 1 \<Longrightarrow> x dvd a \<Longrightarrow> x dvd b \<Longrightarrow> is_unit x"


927 
apply (subgoal_tac "x dvd gcd a b")


928 
apply (simp add: is_unit_def)


929 
apply (erule (1) gcd_greatest)


930 
done


931 


932 
lemma division_decomp:


933 
assumes dc: "a dvd b * c"


934 
shows "\<exists>b' c'. a = b' * c' \<and> b' dvd b \<and> c' dvd c"


935 
proof (cases "gcd a b = 0")


936 
assume "gcd a b = 0"


937 
hence "a = 0 \<and> b = 0" by (simp add: gcd_zero)


938 
hence "a = 0 * c \<and> 0 dvd b \<and> c dvd c" by simp


939 
then show ?thesis by blast


940 
next


941 
let ?d = "gcd a b"


942 
assume "?d \<noteq> 0"


943 
from gcd_coprime_exists[OF this]


944 
obtain a' b' where ab': "a = a' * ?d" "b = b' * ?d" "gcd a' b' = 1"


945 
by blast


946 
from ab'(1) have "a' dvd a" unfolding dvd_def by blast


947 
with dc have "a' dvd b*c" using dvd_trans[of a' a "b*c"] by simp


948 
from dc ab'(1,2) have "a'*?d dvd (b'*?d) * c" by simp


949 
hence "?d * a' dvd ?d * (b' * c)" by (simp add: mult_ac)


950 
with `?d \<noteq> 0` have "a' dvd b' * c" by (rule dvd_mult_cancel_left)


951 
with coprime_dvd_mult[OF ab'(3)]


952 
have "a' dvd c" by (subst (asm) ac_simps, blast)


953 
with ab'(1) have "a = ?d * a' \<and> ?d dvd b \<and> a' dvd c" by (simp add: mult_ac)


954 
then show ?thesis by blast


955 
qed


956 


957 
lemma pow_divides_pow:


958 
assumes ab: "a ^ n dvd b ^ n" and n: "n \<noteq> 0"


959 
shows "a dvd b"


960 
proof (cases "gcd a b = 0")


961 
assume "gcd a b = 0"


962 
then show ?thesis by (simp add: gcd_zero)


963 
next


964 
let ?d = "gcd a b"


965 
assume "?d \<noteq> 0"


966 
from n obtain m where m: "n = Suc m" by (cases n, simp_all)


967 
from `?d \<noteq> 0` have zn: "?d ^ n \<noteq> 0" by (rule nonzero_pow_nonzero)


968 
from gcd_coprime_exists[OF `?d \<noteq> 0`]


969 
obtain a' b' where ab': "a = a' * ?d" "b = b' * ?d" "gcd a' b' = 1"


970 
by blast


971 
from ab have "(a' * ?d) ^ n dvd (b' * ?d) ^ n"


972 
by (simp add: ab'(1,2)[symmetric])


973 
hence "?d^n * a'^n dvd ?d^n * b'^n"


974 
by (simp only: power_mult_distrib ac_simps)


975 
with zn have "a'^n dvd b'^n" by (rule dvd_mult_cancel_left)


976 
hence "a' dvd b'^n" using dvd_trans[of a' "a'^n" "b'^n"] by (simp add: m)


977 
hence "a' dvd b'^m * b'" by (simp add: m ac_simps)


978 
with coprime_dvd_mult[OF coprime_exp[OF ab'(3), of m]]


979 
have "a' dvd b'" by (subst (asm) ac_simps, blast)


980 
hence "a'*?d dvd b'*?d" by (rule mult_dvd_mono, simp)


981 
with ab'(1,2) show ?thesis by simp


982 
qed


983 


984 
lemma pow_divides_eq [simp]:


985 
"n \<noteq> 0 \<Longrightarrow> a ^ n dvd b ^ n \<longleftrightarrow> a dvd b"


986 
by (auto intro: pow_divides_pow dvd_power_same)


987 


988 
lemma divides_mult:


989 
assumes mr: "m dvd r" and nr: "n dvd r" and mn: "gcd m n = 1"


990 
shows "m * n dvd r"


991 
proof 


992 
from mr nr obtain m' n' where m': "r = m*m'" and n': "r = n*n'"


993 
unfolding dvd_def by blast


994 
from mr n' have "m dvd n'*n" by (simp add: ac_simps)


995 
hence "m dvd n'" using coprime_dvd_mult_iff[OF mn] by simp


996 
then obtain k where k: "n' = m*k" unfolding dvd_def by blast


997 
with n' have "r = m * n * k" by (simp add: mult_ac)


998 
then show ?thesis unfolding dvd_def by blast


999 
qed


1000 


1001 
lemma coprime_plus_one [simp]: "gcd (n + 1) n = 1"


1002 
by (subst add_commute, simp)


1003 


1004 
lemma setprod_coprime [rule_format]:


1005 
"(\<forall>i\<in>A. gcd (f i) x = 1) \<longrightarrow> gcd (\<Prod>i\<in>A. f i) x = 1"


1006 
apply (cases "finite A")


1007 
apply (induct set: finite)


1008 
apply (auto simp add: gcd_mult_cancel)


1009 
done


1010 


1011 
lemma coprime_divisors:


1012 
assumes "d dvd a" "e dvd b" "gcd a b = 1"


1013 
shows "gcd d e = 1"


1014 
proof 


1015 
from assms obtain k l where "a = d * k" "b = e * l"


1016 
unfolding dvd_def by blast


1017 
with assms have "gcd (d * k) (e * l) = 1" by simp


1018 
hence "gcd (d * k) e = 1" by (rule coprime_lmult)


1019 
also have "gcd (d * k) e = gcd e (d * k)" by (simp add: ac_simps)


1020 
finally have "gcd e d = 1" by (rule coprime_lmult)


1021 
then show ?thesis by (simp add: ac_simps)


1022 
qed


1023 


1024 
lemma invertible_coprime:


1025 
"x * y mod m = 1 \<Longrightarrow> gcd x m = 1"


1026 
by (metis coprime_lmult gcd_1 ac_simps gcd_red)


1027 


1028 
lemma lcm_gcd:


1029 
"lcm a b = a * b div (gcd a b * normalisation_factor (a*b))"


1030 
by (simp only: lcm_lcm_eucl gcd_gcd_eucl lcm_eucl_def)


1031 


1032 
lemma lcm_gcd_prod:


1033 
"lcm a b * gcd a b = a * b div normalisation_factor (a*b)"


1034 
proof (cases "a * b = 0")


1035 
let ?nf = normalisation_factor


1036 
assume "a * b \<noteq> 0"

58953

1037 
hence "gcd a b \<noteq> 0" by simp

58023

1038 
from lcm_gcd have "lcm a b * gcd a b = gcd a b * (a * b div (?nf (a*b) * gcd a b))"


1039 
by (simp add: mult_ac)


1040 
also from `a * b \<noteq> 0` have "... = a * b div ?nf (a*b)"

58953

1041 
by (simp_all add: unit_ring_inv'1 unit_ring_inv)

58023

1042 
finally show ?thesis .

58953

1043 
qed (auto simp add: lcm_gcd)

58023

1044 


1045 
lemma lcm_dvd1 [iff]:


1046 
"x dvd lcm x y"


1047 
proof (cases "x*y = 0")


1048 
assume "x * y \<noteq> 0"

58953

1049 
hence "gcd x y \<noteq> 0" by simp

58023

1050 
let ?c = "ring_inv (normalisation_factor (x*y))"


1051 
from `x * y \<noteq> 0` have [simp]: "is_unit (normalisation_factor (x*y))" by simp


1052 
from lcm_gcd_prod[of x y] have "lcm x y * gcd x y = x * ?c * y"


1053 
by (simp add: mult_ac unit_ring_inv)


1054 
hence "lcm x y * gcd x y div gcd x y = x * ?c * y div gcd x y" by simp


1055 
with `gcd x y \<noteq> 0` have "lcm x y = x * ?c * y div gcd x y"


1056 
by (subst (asm) div_mult_self2_is_id, simp_all)


1057 
also have "... = x * (?c * y div gcd x y)"


1058 
by (metis div_mult_swap gcd_dvd2 mult_assoc)


1059 
finally show ?thesis by (rule dvdI)

58953

1060 
qed (auto simp add: lcm_gcd)

58023

1061 


1062 
lemma lcm_least:


1063 
"\<lbrakk>a dvd k; b dvd k\<rbrakk> \<Longrightarrow> lcm a b dvd k"


1064 
proof (cases "k = 0")


1065 
let ?nf = normalisation_factor


1066 
assume "k \<noteq> 0"


1067 
hence "is_unit (?nf k)" by simp


1068 
hence "?nf k \<noteq> 0" by (metis not_is_unit_0)


1069 
assume A: "a dvd k" "b dvd k"

58953

1070 
hence "gcd a b \<noteq> 0" using `k \<noteq> 0` by auto

58023

1071 
from A obtain r s where ar: "k = a * r" and bs: "k = b * s"


1072 
unfolding dvd_def by blast

58953

1073 
with `k \<noteq> 0` have "r * s \<noteq> 0"


1074 
by auto (drule sym [of 0], simp)

58023

1075 
hence "is_unit (?nf (r * s))" by simp


1076 
let ?c = "?nf k div ?nf (r*s)"


1077 
from `is_unit (?nf k)` and `is_unit (?nf (r * s))` have "is_unit ?c" by (rule unit_div)


1078 
hence "?c \<noteq> 0" using not_is_unit_0 by fast


1079 
from ar bs have "k * k * gcd s r = ?nf k * k * gcd (k * s) (k * r)"

58953

1080 
by (subst mult_assoc, subst gcd_mult_distrib[of k s r], simp only: ac_simps)

58023

1081 
also have "... = ?nf k * k * gcd ((r*s) * a) ((r*s) * b)"


1082 
by (subst (3) `k = a * r`, subst (3) `k = b * s`, simp add: algebra_simps)


1083 
also have "... = ?c * r*s * k * gcd a b" using `r * s \<noteq> 0`


1084 
by (subst gcd_mult_distrib'[symmetric], simp add: algebra_simps unit_simps)


1085 
finally have "(a*r) * (b*s) * gcd s r = ?c * k * r * s * gcd a b"


1086 
by (subst ar[symmetric], subst bs[symmetric], simp add: mult_ac)


1087 
hence "a * b * gcd s r * (r * s) = ?c * k * gcd a b * (r * s)"


1088 
by (simp add: algebra_simps)


1089 
hence "?c * k * gcd a b = a * b * gcd s r" using `r * s \<noteq> 0`


1090 
by (metis div_mult_self2_is_id)


1091 
also have "... = lcm a b * gcd a b * gcd s r * ?nf (a*b)"


1092 
by (subst lcm_gcd_prod[of a b], metis gcd_mult_distrib gcd_mult_distrib')


1093 
also have "... = lcm a b * gcd s r * ?nf (a*b) * gcd a b"


1094 
by (simp add: algebra_simps)


1095 
finally have "k * ?c = lcm a b * gcd s r * ?nf (a*b)" using `gcd a b \<noteq> 0`


1096 
by (metis mult.commute div_mult_self2_is_id)


1097 
hence "k = lcm a b * (gcd s r * ?nf (a*b)) div ?c" using `?c \<noteq> 0`


1098 
by (metis div_mult_self2_is_id mult_assoc)


1099 
also have "... = lcm a b * (gcd s r * ?nf (a*b) div ?c)" using `is_unit ?c`


1100 
by (simp add: unit_simps)


1101 
finally show ?thesis by (rule dvdI)


1102 
qed simp


1103 


1104 
lemma lcm_zero:


1105 
"lcm a b = 0 \<longleftrightarrow> a = 0 \<or> b = 0"


1106 
proof 


1107 
let ?nf = normalisation_factor


1108 
{


1109 
assume "a \<noteq> 0" "b \<noteq> 0"


1110 
hence "a * b div ?nf (a * b) \<noteq> 0" by (simp add: no_zero_divisors)


1111 
moreover from `a \<noteq> 0` and `b \<noteq> 0` have "gcd a b \<noteq> 0" by (simp add: gcd_zero)


1112 
ultimately have "lcm a b \<noteq> 0" using lcm_gcd_prod[of a b] by (intro notI, simp)


1113 
} moreover {


1114 
assume "a = 0 \<or> b = 0"


1115 
hence "lcm a b = 0" by (elim disjE, simp_all add: lcm_gcd)


1116 
}


1117 
ultimately show ?thesis by blast


1118 
qed


1119 


1120 
lemmas lcm_0_iff = lcm_zero


1121 


1122 
lemma gcd_lcm:


1123 
assumes "lcm a b \<noteq> 0"


1124 
shows "gcd a b = a * b div (lcm a b * normalisation_factor (a * b))"


1125 
proof


1126 
from assms have "gcd a b \<noteq> 0" by (simp add: gcd_zero lcm_zero)


1127 
let ?c = "normalisation_factor (a*b)"


1128 
from `lcm a b \<noteq> 0` have "?c \<noteq> 0" by (intro notI, simp add: lcm_zero no_zero_divisors)


1129 
hence "is_unit ?c" by simp


1130 
from lcm_gcd_prod [of a b] have "gcd a b = a * b div ?c div lcm a b"


1131 
by (subst (2) div_mult_self2_is_id[OF `lcm a b \<noteq> 0`, symmetric], simp add: mult_ac)


1132 
also from `is_unit ?c` have "... = a * b div (?c * lcm a b)"


1133 
by (simp only: unit_ring_inv'1 unit_ring_inv)


1134 
finally show ?thesis by (simp only: ac_simps)


1135 
qed


1136 


1137 
lemma normalisation_factor_lcm [simp]:


1138 
"normalisation_factor (lcm a b) = (if a = 0 \<or> b = 0 then 0 else 1)"


1139 
proof (cases "a = 0 \<or> b = 0")


1140 
case True then show ?thesis

58953

1141 
by (auto simp add: lcm_gcd)

58023

1142 
next


1143 
case False


1144 
let ?nf = normalisation_factor


1145 
from lcm_gcd_prod[of a b]


1146 
have "?nf (lcm a b) * ?nf (gcd a b) = ?nf (a*b) div ?nf (a*b)"


1147 
by (metis div_by_0 div_self normalisation_correct normalisation_factor_0 normalisation_factor_mult)


1148 
also have "... = (if a*b = 0 then 0 else 1)"

58953

1149 
by simp


1150 
finally show ?thesis using False by simp

58023

1151 
qed


1152 


1153 
lemma lcm_dvd2 [iff]: "y dvd lcm x y"


1154 
using lcm_dvd1 [of y x] by (simp add: lcm_gcd ac_simps)


1155 


1156 
lemma lcmI:


1157 
"\<lbrakk>x dvd k; y dvd k; \<And>l. x dvd l \<Longrightarrow> y dvd l \<Longrightarrow> k dvd l;


1158 
normalisation_factor k = (if k = 0 then 0 else 1)\<rbrakk> \<Longrightarrow> k = lcm x y"


1159 
by (intro normed_associated_imp_eq) (auto simp: associated_def intro: lcm_least)


1160 


1161 
sublocale lcm!: abel_semigroup lcm


1162 
proof


1163 
fix x y z


1164 
show "lcm (lcm x y) z = lcm x (lcm y z)"


1165 
proof (rule lcmI)


1166 
have "x dvd lcm x y" and "lcm x y dvd lcm (lcm x y) z" by simp_all


1167 
then show "x dvd lcm (lcm x y) z" by (rule dvd_trans)


1168 


1169 
have "y dvd lcm x y" and "lcm x y dvd lcm (lcm x y) z" by simp_all


1170 
hence "y dvd lcm (lcm x y) z" by (rule dvd_trans)


1171 
moreover have "z dvd lcm (lcm x y) z" by simp


1172 
ultimately show "lcm y z dvd lcm (lcm x y) z" by (rule lcm_least)


1173 


1174 
fix l assume "x dvd l" and "lcm y z dvd l"


1175 
have "y dvd lcm y z" by simp


1176 
from this and `lcm y z dvd l` have "y dvd l" by (rule dvd_trans)


1177 
have "z dvd lcm y z" by simp


1178 
from this and `lcm y z dvd l` have "z dvd l" by (rule dvd_trans)


1179 
from `x dvd l` and `y dvd l` have "lcm x y dvd l" by (rule lcm_least)


1180 
from this and `z dvd l` show "lcm (lcm x y) z dvd l" by (rule lcm_least)


1181 
qed (simp add: lcm_zero)


1182 
next


1183 
fix x y


1184 
show "lcm x y = lcm y x"


1185 
by (simp add: lcm_gcd ac_simps)


1186 
qed


1187 


1188 
lemma dvd_lcm_D1:


1189 
"lcm m n dvd k \<Longrightarrow> m dvd k"


1190 
by (rule dvd_trans, rule lcm_dvd1, assumption)


1191 


1192 
lemma dvd_lcm_D2:


1193 
"lcm m n dvd k \<Longrightarrow> n dvd k"


1194 
by (rule dvd_trans, rule lcm_dvd2, assumption)


1195 


1196 
lemma gcd_dvd_lcm [simp]:


1197 
"gcd a b dvd lcm a b"


1198 
by (metis dvd_trans gcd_dvd2 lcm_dvd2)


1199 


1200 
lemma lcm_1_iff:


1201 
"lcm a b = 1 \<longleftrightarrow> is_unit a \<and> is_unit b"


1202 
proof


1203 
assume "lcm a b = 1"


1204 
then show "is_unit a \<and> is_unit b" unfolding is_unit_def by auto


1205 
next


1206 
assume "is_unit a \<and> is_unit b"


1207 
hence "a dvd 1" and "b dvd 1" unfolding is_unit_def by simp_all


1208 
hence "is_unit (lcm a b)" unfolding is_unit_def by (rule lcm_least)


1209 
hence "lcm a b = normalisation_factor (lcm a b)"


1210 
by (subst normalisation_factor_unit, simp_all)


1211 
also have "\<dots> = 1" using `is_unit a \<and> is_unit b` by (auto simp add: is_unit_def)


1212 
finally show "lcm a b = 1" .


1213 
qed


1214 


1215 
lemma lcm_0_left [simp]:


1216 
"lcm 0 x = 0"


1217 
by (rule sym, rule lcmI, simp_all)


1218 


1219 
lemma lcm_0 [simp]:


1220 
"lcm x 0 = 0"


1221 
by (rule sym, rule lcmI, simp_all)


1222 


1223 
lemma lcm_unique:


1224 
"a dvd d \<and> b dvd d \<and>


1225 
normalisation_factor d = (if d = 0 then 0 else 1) \<and>


1226 
(\<forall>e. a dvd e \<and> b dvd e \<longrightarrow> d dvd e) \<longleftrightarrow> d = lcm a b"


1227 
by (rule, auto intro: lcmI simp: lcm_least lcm_zero)


1228 


1229 
lemma dvd_lcm_I1 [simp]:


1230 
"k dvd m \<Longrightarrow> k dvd lcm m n"


1231 
by (metis lcm_dvd1 dvd_trans)


1232 


1233 
lemma dvd_lcm_I2 [simp]:


1234 
"k dvd n \<Longrightarrow> k dvd lcm m n"


1235 
by (metis lcm_dvd2 dvd_trans)


1236 


1237 
lemma lcm_1_left [simp]:


1238 
"lcm 1 x = x div normalisation_factor x"


1239 
by (cases "x = 0") (simp, rule sym, rule lcmI, simp_all)


1240 


1241 
lemma lcm_1_right [simp]:


1242 
"lcm x 1 = x div normalisation_factor x"


1243 
by (simp add: ac_simps)


1244 


1245 
lemma lcm_coprime:


1246 
"gcd a b = 1 \<Longrightarrow> lcm a b = a * b div normalisation_factor (a*b)"


1247 
by (subst lcm_gcd) simp


1248 


1249 
lemma lcm_proj1_if_dvd:


1250 
"y dvd x \<Longrightarrow> lcm x y = x div normalisation_factor x"


1251 
by (cases "x = 0") (simp, rule sym, rule lcmI, simp_all)


1252 


1253 
lemma lcm_proj2_if_dvd:


1254 
"x dvd y \<Longrightarrow> lcm x y = y div normalisation_factor y"


1255 
using lcm_proj1_if_dvd [of x y] by (simp add: ac_simps)


1256 


1257 
lemma lcm_proj1_iff:


1258 
"lcm m n = m div normalisation_factor m \<longleftrightarrow> n dvd m"


1259 
proof


1260 
assume A: "lcm m n = m div normalisation_factor m"


1261 
show "n dvd m"


1262 
proof (cases "m = 0")


1263 
assume [simp]: "m \<noteq> 0"


1264 
from A have B: "m = lcm m n * normalisation_factor m"


1265 
by (simp add: unit_eq_div2)


1266 
show ?thesis by (subst B, simp)


1267 
qed simp


1268 
next


1269 
assume "n dvd m"


1270 
then show "lcm m n = m div normalisation_factor m" by (rule lcm_proj1_if_dvd)


1271 
qed


1272 


1273 
lemma lcm_proj2_iff:


1274 
"lcm m n = n div normalisation_factor n \<longleftrightarrow> m dvd n"


1275 
using lcm_proj1_iff [of n m] by (simp add: ac_simps)


1276 


1277 
lemma euclidean_size_lcm_le1:


1278 
assumes "a \<noteq> 0" and "b \<noteq> 0"


1279 
shows "euclidean_size a \<le> euclidean_size (lcm a b)"


1280 
proof 


1281 
have "a dvd lcm a b" by (rule lcm_dvd1)


1282 
then obtain c where A: "lcm a b = a * c" unfolding dvd_def by blast


1283 
with `a \<noteq> 0` and `b \<noteq> 0` have "c \<noteq> 0" by (auto simp: lcm_zero)


1284 
then show ?thesis by (subst A, intro size_mult_mono)


1285 
qed


1286 


1287 
lemma euclidean_size_lcm_le2:


1288 
"a \<noteq> 0 \<Longrightarrow> b \<noteq> 0 \<Longrightarrow> euclidean_size b \<le> euclidean_size (lcm a b)"


1289 
using euclidean_size_lcm_le1 [of b a] by (simp add: ac_simps)


1290 


1291 
lemma euclidean_size_lcm_less1:


1292 
assumes "b \<noteq> 0" and "\<not>b dvd a"


1293 
shows "euclidean_size a < euclidean_size (lcm a b)"


1294 
proof (rule ccontr)


1295 
from assms have "a \<noteq> 0" by auto


1296 
assume "\<not>euclidean_size a < euclidean_size (lcm a b)"


1297 
with `a \<noteq> 0` and `b \<noteq> 0` have "euclidean_size (lcm a b) = euclidean_size a"


1298 
by (intro le_antisym, simp, intro euclidean_size_lcm_le1)


1299 
with assms have "lcm a b dvd a"


1300 
by (rule_tac dvd_euclidean_size_eq_imp_dvd) (auto simp: lcm_zero)


1301 
hence "b dvd a" by (rule dvd_lcm_D2)


1302 
with `\<not>b dvd a` show False by contradiction


1303 
qed


1304 


1305 
lemma euclidean_size_lcm_less2:


1306 
assumes "a \<noteq> 0" and "\<not>a dvd b"


1307 
shows "euclidean_size b < euclidean_size (lcm a b)"


1308 
using assms euclidean_size_lcm_less1 [of a b] by (simp add: ac_simps)


1309 


1310 
lemma lcm_mult_unit1:


1311 
"is_unit a \<Longrightarrow> lcm (x*a) y = lcm x y"


1312 
apply (rule lcmI)


1313 
apply (rule dvd_trans[of _ "x*a"], simp, rule lcm_dvd1)


1314 
apply (rule lcm_dvd2)


1315 
apply (rule lcm_least, simp add: unit_simps, assumption)


1316 
apply (subst normalisation_factor_lcm, simp add: lcm_zero)


1317 
done


1318 


1319 
lemma lcm_mult_unit2:


1320 
"is_unit a \<Longrightarrow> lcm x (y*a) = lcm x y"


1321 
using lcm_mult_unit1 [of a y x] by (simp add: ac_simps)


1322 


1323 
lemma lcm_div_unit1:


1324 
"is_unit a \<Longrightarrow> lcm (x div a) y = lcm x y"


1325 
by (simp add: unit_ring_inv lcm_mult_unit1)


1326 


1327 
lemma lcm_div_unit2:


1328 
"is_unit a \<Longrightarrow> lcm x (y div a) = lcm x y"


1329 
by (simp add: unit_ring_inv lcm_mult_unit2)


1330 


1331 
lemma lcm_left_idem:


1332 
"lcm p (lcm p q) = lcm p q"


1333 
apply (rule lcmI)


1334 
apply simp


1335 
apply (subst lcm.assoc [symmetric], rule lcm_dvd2)


1336 
apply (rule lcm_least, assumption)


1337 
apply (erule (1) lcm_least)


1338 
apply (auto simp: lcm_zero)


1339 
done


1340 


1341 
lemma lcm_right_idem:


1342 
"lcm (lcm p q) q = lcm p q"


1343 
apply (rule lcmI)


1344 
apply (subst lcm.assoc, rule lcm_dvd1)


1345 
apply (rule lcm_dvd2)


1346 
apply (rule lcm_least, erule (1) lcm_least, assumption)


1347 
apply (auto simp: lcm_zero)


1348 
done


1349 


1350 
lemma comp_fun_idem_lcm: "comp_fun_idem lcm"


1351 
proof


1352 
fix a b show "lcm a \<circ> lcm b = lcm b \<circ> lcm a"


1353 
by (simp add: fun_eq_iff ac_simps)


1354 
next


1355 
fix a show "lcm a \<circ> lcm a = lcm a" unfolding o_def


1356 
by (intro ext, simp add: lcm_left_idem)


1357 
qed


1358 


1359 
lemma dvd_Lcm [simp]: "x \<in> A \<Longrightarrow> x dvd Lcm A"


1360 
and Lcm_dvd [simp]: "(\<forall>x\<in>A. x dvd l') \<Longrightarrow> Lcm A dvd l'"


1361 
and normalisation_factor_Lcm [simp]:


1362 
"normalisation_factor (Lcm A) = (if Lcm A = 0 then 0 else 1)"


1363 
proof 


1364 
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>


1365 
normalisation_factor (Lcm A) = (if Lcm A = 0 then 0 else 1)" (is ?thesis)


1366 
proof (cases "\<exists>l. l \<noteq> 0 \<and> (\<forall>x\<in>A. x dvd l)")


1367 
case False


1368 
hence "Lcm A = 0" by (auto simp: Lcm_Lcm_eucl Lcm_eucl_def)


1369 
with False show ?thesis by auto


1370 
next


1371 
case True


1372 
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


1373 
def n \<equiv> "LEAST n. \<exists>l. l \<noteq> 0 \<and> (\<forall>x\<in>A. x dvd l) \<and> euclidean_size l = n"


1374 
def l \<equiv> "SOME l. l \<noteq> 0 \<and> (\<forall>x\<in>A. x dvd l) \<and> euclidean_size l = n"


1375 
have "\<exists>l. l \<noteq> 0 \<and> (\<forall>x\<in>A. x dvd l) \<and> euclidean_size l = n"


1376 
apply (subst n_def)

62826b36ac5e
generic euclidean algorithm (due to Manuel Eberl)
haftmann
parents:
