author  haftmann 
Sat, 27 Jun 2015 20:20:34 +0200  
changeset 60598  78ca5674c66a 
parent 60582  d694f217ee41 
child 60599  f8bb070dc98b 
permissions  rwrr 
58023  1 
(* Author: Manuel Eberl *) 
2 

60526  3 
section \<open>Abstract euclidean algorithm\<close> 
58023  4 

5 
theory Euclidean_Algorithm 

60571  6 
imports Complex_Main "~~/src/HOL/Library/Polynomial" 
58023  7 
begin 
60433  8 

60526  9 
text \<open> 
58023  10 
A Euclidean semiring is a semiring upon which the Euclidean algorithm can be 
11 
implemented. It must provide: 

12 
\begin{itemize} 

13 
\item division with remainder 

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

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

60438  16 
\item a normalization factor such that two associated numbers are equal iff 
17 
they are the same when divd by their normalization factors. 

58023  18 
\end{itemize} 
19 
The existence of these functions makes it possible to derive gcd and lcm functions 

20 
for any Euclidean semiring. 

60526  21 
\<close> 
58023  22 
class euclidean_semiring = semiring_div + 
23 
fixes euclidean_size :: "'a \<Rightarrow> nat" 

60438  24 
fixes normalization_factor :: "'a \<Rightarrow> 'a" 
60569
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

25 
assumes mod_size_less: 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

26 
"b \<noteq> 0 \<Longrightarrow> \<not> b dvd a \<Longrightarrow> euclidean_size (a mod b) < euclidean_size b" 
58023  27 
assumes size_mult_mono: 
28 
"b \<noteq> 0 \<Longrightarrow> euclidean_size (a * b) \<ge> euclidean_size a" 

60438  29 
assumes normalization_factor_is_unit [intro,simp]: 
30 
"a \<noteq> 0 \<Longrightarrow> is_unit (normalization_factor a)" 

31 
assumes normalization_factor_mult: "normalization_factor (a * b) = 

32 
normalization_factor a * normalization_factor b" 

33 
assumes normalization_factor_unit: "is_unit a \<Longrightarrow> normalization_factor a = a" 

34 
assumes normalization_factor_0 [simp]: "normalization_factor 0 = 0" 

58023  35 
begin 
36 

60438  37 
lemma normalization_factor_dvd [simp]: 
38 
"a \<noteq> 0 \<Longrightarrow> normalization_factor a dvd b" 

58023  39 
by (rule unit_imp_dvd, simp) 
40 

60438  41 
lemma normalization_factor_1 [simp]: 
42 
"normalization_factor 1 = 1" 

43 
by (simp add: normalization_factor_unit) 

58023  44 

60438  45 
lemma normalization_factor_0_iff [simp]: 
46 
"normalization_factor a = 0 \<longleftrightarrow> a = 0" 

58023  47 
proof 
60438  48 
assume "normalization_factor a = 0" 
49 
hence "\<not> is_unit (normalization_factor a)" 

60433  50 
by simp 
51 
then show "a = 0" by auto 

52 
qed simp 

58023  53 

60438  54 
lemma normalization_factor_pow: 
55 
"normalization_factor (a ^ n) = normalization_factor a ^ n" 

56 
by (induct n) (simp_all add: normalization_factor_mult power_Suc2) 

58023  57 

60438  58 
lemma normalization_correct [simp]: 
59 
"normalization_factor (a div normalization_factor a) = (if a = 0 then 0 else 1)" 

60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

60 
proof (cases "a = 0", simp) 
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

61 
assume "a \<noteq> 0" 
60438  62 
let ?nf = "normalization_factor" 
60526  63 
from normalization_factor_is_unit[OF \<open>a \<noteq> 0\<close>] have "?nf a \<noteq> 0" 
60433  64 
by auto 
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

65 
have "?nf (a div ?nf a) * ?nf (?nf a) = ?nf (a div ?nf a * ?nf a)" 
60438  66 
by (simp add: normalization_factor_mult) 
60526  67 
also have "a div ?nf a * ?nf a = a" using \<open>a \<noteq> 0\<close> 
59009
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset

68 
by simp 
60526  69 
also have "?nf (?nf a) = ?nf a" using \<open>a \<noteq> 0\<close> 
60438  70 
normalization_factor_is_unit normalization_factor_unit by simp 
71 
finally have "normalization_factor (a div normalization_factor a) = 1" 

60526  72 
using \<open>?nf a \<noteq> 0\<close> by (metis div_mult_self2_is_id div_self) 
73 
with \<open>a \<noteq> 0\<close> show ?thesis by simp 

58023  74 
qed 
75 

60438  76 
lemma normalization_0_iff [simp]: 
77 
"a div normalization_factor a = 0 \<longleftrightarrow> a = 0" 

60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

78 
by (cases "a = 0", simp, subst unit_eq_div1, blast, simp) 
58023  79 

60438  80 
lemma mult_div_normalization [simp]: 
81 
"b * (1 div normalization_factor a) = b div normalization_factor a" 

60433  82 
by (cases "a = 0") simp_all 
83 

58023  84 
lemma associated_iff_normed_eq: 
60438  85 
"associated a b \<longleftrightarrow> a div normalization_factor a = b div normalization_factor b" 
86 
proof (cases "b = 0", simp, cases "a = 0", metis associated_0(1) normalization_0_iff, rule iffI) 

87 
let ?nf = normalization_factor 

58023  88 
assume "a \<noteq> 0" "b \<noteq> 0" "a div ?nf a = b div ?nf b" 
89 
hence "a = b * (?nf a div ?nf b)" 

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

91 
apply (subst div_mult_swap, simp, simp) 

92 
done 

60526  93 
with \<open>a \<noteq> 0\<close> \<open>b \<noteq> 0\<close> have "\<exists>c. is_unit c \<and> a = c * b" 
58023  94 
by (intro exI[of _ "?nf a div ?nf b"], force simp: mult_ac) 
60436
77e694c0c919
simplified relationship between associated and is_unit
haftmann
parents:
60433
diff
changeset

95 
then obtain c where "is_unit c" and "a = c * b" by blast 
77e694c0c919
simplified relationship between associated and is_unit
haftmann
parents:
60433
diff
changeset

96 
then show "associated a b" by (rule is_unit_associatedI) 
58023  97 
next 
60438  98 
let ?nf = normalization_factor 
58023  99 
assume "a \<noteq> 0" "b \<noteq> 0" "associated a b" 
60436
77e694c0c919
simplified relationship between associated and is_unit
haftmann
parents:
60433
diff
changeset

100 
then obtain c where "is_unit c" and "a = c * b" by (blast elim: associated_is_unitE) 
58023  101 
then show "a div ?nf a = b div ?nf b" 
60526  102 
apply (simp only: \<open>a = c * b\<close> normalization_factor_mult normalization_factor_unit) 
58023  103 
apply (rule div_mult_mult1, force) 
104 
done 

105 
qed 

106 

107 
lemma normed_associated_imp_eq: 

60438  108 
"associated a b \<Longrightarrow> normalization_factor a \<in> {0, 1} \<Longrightarrow> normalization_factor b \<in> {0, 1} \<Longrightarrow> a = b" 
58023  109 
by (simp add: associated_iff_normed_eq, elim disjE, simp_all) 
60569
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

110 

f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

111 
lemma normed_dvd [iff]: 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

112 
"a div normalization_factor a dvd a" 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

113 
proof (cases "a = 0") 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

114 
case True then show ?thesis by simp 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

115 
next 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

116 
case False 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

117 
then have "a = a div normalization_factor a * normalization_factor a" 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

118 
by (auto intro: unit_div_mult_self) 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

119 
then show ?thesis .. 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

120 
qed 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

121 

f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

122 
lemma dvd_normed [iff]: 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

123 
"a dvd a div normalization_factor a" 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

124 
proof (cases "a = 0") 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

125 
case True then show ?thesis by simp 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

126 
next 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

127 
case False 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

128 
then have "a div normalization_factor a = a * (1 div normalization_factor a)" 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

129 
by (auto intro: unit_mult_div_div) 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

130 
then show ?thesis .. 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

131 
qed 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

132 

f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

133 
lemma associated_normed: 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

134 
"associated (a div normalization_factor a) a" 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

135 
by (rule associatedI) simp_all 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

136 

f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

137 
lemma normalization_factor_dvd' [simp]: 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

138 
"normalization_factor a dvd a" 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

139 
by (cases "a = 0", simp_all) 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

140 

60438  141 
lemmas normalization_factor_dvd_iff [simp] = 
142 
unit_dvd_iff [OF normalization_factor_is_unit] 

58023  143 

144 
lemma euclidean_division: 

145 
fixes a :: 'a and b :: 'a 

60569
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

146 
assumes "b \<noteq> 0" and "\<not> b dvd a" 
58023  147 
obtains s and t where "a = s * b + t" 
148 
and "euclidean_size t < euclidean_size b" 

149 
proof  

60569
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

150 
from div_mod_equality [of a b 0] 
58023  151 
have "a = a div b * b + a mod b" by simp 
60569
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

152 
with that and assms show ?thesis by (auto simp add: mod_size_less) 
58023  153 
qed 
154 

155 
lemma dvd_euclidean_size_eq_imp_dvd: 

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

157 
shows "a dvd b" 

60569
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

158 
proof (rule ccontr) 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

159 
assume "\<not> a dvd b" 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

160 
then have "b mod a \<noteq> 0" by (simp add: mod_eq_0_iff_dvd) 
58023  161 
from b_dvd_a have b_dvd_mod: "b dvd b mod a" by (simp add: dvd_mod_iff) 
162 
from b_dvd_mod obtain c where "b mod a = b * c" unfolding dvd_def by blast 

60526  163 
with \<open>b mod a \<noteq> 0\<close> have "c \<noteq> 0" by auto 
164 
with \<open>b mod a = b * c\<close> have "euclidean_size (b mod a) \<ge> euclidean_size b" 

58023  165 
using size_mult_mono by force 
60569
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

166 
moreover from \<open>\<not> a dvd b\<close> and \<open>a \<noteq> 0\<close> 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

167 
have "euclidean_size (b mod a) < euclidean_size a" 
58023  168 
using mod_size_less by blast 
169 
ultimately show False using size_eq by simp 

170 
qed 

171 

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

173 
where 

60569
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

174 
"gcd_eucl a b = (if b = 0 then a div normalization_factor a 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

175 
else if b dvd a then b div normalization_factor b 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

176 
else gcd_eucl b (a mod b))" 
60572
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset

177 
by pat_completeness simp 
60569
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

178 
termination 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

179 
by (relation "measure (euclidean_size \<circ> snd)") (simp_all add: mod_size_less) 
58023  180 

181 
declare gcd_eucl.simps [simp del] 

182 

60569
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

183 
lemma gcd_eucl_induct [case_names zero mod]: 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

184 
assumes H1: "\<And>b. P b 0" 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

185 
and H2: "\<And>a b. b \<noteq> 0 \<Longrightarrow> P b (a mod b) \<Longrightarrow> P a b" 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

186 
shows "P a b" 
58023  187 
proof (induct a b rule: gcd_eucl.induct) 
60569
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

188 
case ("1" a b) 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

189 
show ?case 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

190 
proof (cases "b = 0") 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

191 
case True then show "P a b" by simp (rule H1) 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

192 
next 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

193 
case False 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

194 
have "P b (a mod b)" 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

195 
proof (cases "b dvd a") 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

196 
case False with \<open>b \<noteq> 0\<close> show "P b (a mod b)" 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

197 
by (rule "1.hyps") 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

198 
next 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

199 
case True then have "a mod b = 0" 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

200 
by (simp add: mod_eq_0_iff_dvd) 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

201 
then show "P b (a mod b)" by simp (rule H1) 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

202 
qed 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

203 
with \<open>b \<noteq> 0\<close> show "P a b" 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

204 
by (blast intro: H2) 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

205 
qed 
58023  206 
qed 
207 

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

209 
where 

60438  210 
"lcm_eucl a b = a * b div (gcd_eucl a b * normalization_factor (a * b))" 
58023  211 

60572
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset

212 
definition Lcm_eucl :: "'a set \<Rightarrow> 'a"  \<open> 
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset

213 
Somewhat complicated definition of Lcm that has the advantage of working 
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset

214 
for infinite sets as well\<close> 
58023  215 
where 
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

216 
"Lcm_eucl A = (if \<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) then 
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

217 
let l = SOME l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = 
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

218 
(LEAST n. \<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n) 
60438  219 
in l div normalization_factor l 
58023  220 
else 0)" 
221 

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

223 
where 

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

225 

60572
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset

226 
lemma gcd_eucl_0: 
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset

227 
"gcd_eucl a 0 = a div normalization_factor a" 
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset

228 
by (simp add: gcd_eucl.simps [of a 0]) 
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset

229 

718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset

230 
lemma gcd_eucl_0_left: 
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset

231 
"gcd_eucl 0 a = a div normalization_factor a" 
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset

232 
by (simp add: gcd_eucl.simps [of 0 a]) 
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset

233 

718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset

234 
lemma gcd_eucl_non_0: 
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset

235 
"b \<noteq> 0 \<Longrightarrow> gcd_eucl a b = gcd_eucl b (a mod b)" 
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset

236 
by (cases "b dvd a") 
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset

237 
(simp_all add: gcd_eucl.simps [of a b] gcd_eucl.simps [of b 0]) 
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset

238 

718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset

239 
lemma gcd_eucl_code [code]: 
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset

240 
"gcd_eucl a b = (if b = 0 then a div normalization_factor a else gcd_eucl b (a mod b))" 
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset

241 
by (auto simp add: gcd_eucl_non_0 gcd_eucl_0 gcd_eucl_0_left) 
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset

242 

58023  243 
end 
244 

60598
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset

245 
class euclidean_ring = euclidean_semiring + idom 
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset

246 
begin 
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset

247 

78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset

248 
function euclid_ext :: "'a \<Rightarrow> 'a \<Rightarrow> 'a \<times> 'a \<times> 'a" where 
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset

249 
"euclid_ext a b = 
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset

250 
(if b = 0 then 
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset

251 
let c = 1 div normalization_factor a in (c, 0, a * c) 
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset

252 
else if b dvd a then 
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset

253 
let c = 1 div normalization_factor b in (0, c, b * c) 
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset

254 
else 
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset

255 
case euclid_ext b (a mod b) of 
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset

256 
(s, t, c) \<Rightarrow> (t, s  t * (a div b), c))" 
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset

257 
by pat_completeness simp 
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset

258 
termination 
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset

259 
by (relation "measure (euclidean_size \<circ> snd)") (simp_all add: mod_size_less) 
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset

260 

78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset

261 
declare euclid_ext.simps [simp del] 
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset

262 

78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset

263 
lemma euclid_ext_0: 
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset

264 
"euclid_ext a 0 = (1 div normalization_factor a, 0, a div normalization_factor a)" 
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset

265 
by (simp add: euclid_ext.simps [of a 0]) 
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset

266 

78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset

267 
lemma euclid_ext_left_0: 
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset

268 
"euclid_ext 0 a = (0, 1 div normalization_factor a, a div normalization_factor a)" 
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset

269 
by (simp add: euclid_ext.simps [of 0 a]) 
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset

270 

78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset

271 
lemma euclid_ext_non_0: 
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset

272 
"b \<noteq> 0 \<Longrightarrow> euclid_ext a b = (case euclid_ext b (a mod b) of 
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset

273 
(s, t, c) \<Rightarrow> (t, s  t * (a div b), c))" 
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset

274 
by (cases "b dvd a") 
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset

275 
(simp_all add: euclid_ext.simps [of a b] euclid_ext.simps [of b 0]) 
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset

276 

78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset

277 
lemma euclid_ext_code [code]: 
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset

278 
"euclid_ext a b = (if b = 0 then (1 div normalization_factor a, 0, a div normalization_factor a) 
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset

279 
else let (s, t, c) = euclid_ext b (a mod b) in (t, s  t * (a div b), c))" 
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset

280 
by (simp add: euclid_ext.simps [of a b] euclid_ext.simps [of b 0]) 
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset

281 

78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset

282 
lemma euclid_ext_correct: 
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset

283 
"case euclid_ext a b of (s, t, c) \<Rightarrow> s * a + t * b = c" 
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset

284 
proof (induct a b rule: gcd_eucl_induct) 
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset

285 
case (zero a) then show ?case 
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset

286 
by (simp add: euclid_ext_0 ac_simps) 
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset

287 
next 
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset

288 
case (mod a b) 
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset

289 
obtain s t c where stc: "euclid_ext b (a mod b) = (s,t,c)" 
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset

290 
by (cases "euclid_ext b (a mod b)") blast 
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset

291 
with mod have "c = s * b + t * (a mod b)" by simp 
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset

292 
also have "... = t * ((a div b) * b + a mod b) + (s  t * (a div b)) * b" 
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset

293 
by (simp add: algebra_simps) 
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset

294 
also have "(a div b) * b + a mod b = a" using mod_div_equality . 
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset

295 
finally show ?case 
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset

296 
by (subst euclid_ext.simps) (simp add: stc mod ac_simps) 
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset

297 
qed 
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset

298 

78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset

299 
definition euclid_ext' :: "'a \<Rightarrow> 'a \<Rightarrow> 'a \<times> 'a" 
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset

300 
where 
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset

301 
"euclid_ext' a b = (case euclid_ext a b of (s, t, _) \<Rightarrow> (s, t))" 
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset

302 

78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset

303 
lemma euclid_ext'_0: "euclid_ext' a 0 = (1 div normalization_factor a, 0)" 
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset

304 
by (simp add: euclid_ext'_def euclid_ext_0) 
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset

305 

78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset

306 
lemma euclid_ext'_left_0: "euclid_ext' 0 a = (0, 1 div normalization_factor a)" 
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset

307 
by (simp add: euclid_ext'_def euclid_ext_left_0) 
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset

308 

78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset

309 
lemma euclid_ext'_non_0: "b \<noteq> 0 \<Longrightarrow> euclid_ext' a b = (snd (euclid_ext' b (a mod b)), 
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset

310 
fst (euclid_ext' b (a mod b))  snd (euclid_ext' b (a mod b)) * (a div b))" 
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset

311 
by (simp add: euclid_ext'_def euclid_ext_non_0 split_def) 
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset

312 

78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset

313 
end 
78ca5674c66a
rings follow immediately their corresponding semirings
haftmann
parents:
60582
diff
changeset

314 

58023  315 
class euclidean_semiring_gcd = euclidean_semiring + gcd + Gcd + 
316 
assumes gcd_gcd_eucl: "gcd = gcd_eucl" and lcm_lcm_eucl: "lcm = lcm_eucl" 

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

318 
begin 

319 

320 
lemma gcd_0_left: 

60438  321 
"gcd 0 a = a div normalization_factor a" 
60572
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset

322 
unfolding gcd_gcd_eucl by (fact gcd_eucl_0_left) 
58023  323 

324 
lemma gcd_0: 

60438  325 
"gcd a 0 = a div normalization_factor a" 
60572
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset

326 
unfolding gcd_gcd_eucl by (fact gcd_eucl_0) 
58023  327 

328 
lemma gcd_non_0: 

60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

329 
"b \<noteq> 0 \<Longrightarrow> gcd a b = gcd b (a mod b)" 
60572
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset

330 
unfolding gcd_gcd_eucl by (fact gcd_eucl_non_0) 
58023  331 

60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

332 
lemma gcd_dvd1 [iff]: "gcd a b dvd a" 
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

333 
and gcd_dvd2 [iff]: "gcd a b dvd b" 
60569
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

334 
by (induct a b rule: gcd_eucl_induct) 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

335 
(simp_all add: gcd_0 gcd_non_0 dvd_mod_iff) 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

336 

58023  337 
lemma dvd_gcd_D1: "k dvd gcd m n \<Longrightarrow> k dvd m" 
338 
by (rule dvd_trans, assumption, rule gcd_dvd1) 

339 

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

341 
by (rule dvd_trans, assumption, rule gcd_dvd2) 

342 

343 
lemma gcd_greatest: 

60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

344 
fixes k a b :: 'a 
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

345 
shows "k dvd a \<Longrightarrow> k dvd b \<Longrightarrow> k dvd gcd a b" 
60569
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

346 
proof (induct a b rule: gcd_eucl_induct) 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

347 
case (zero a) from zero(1) show ?case by (rule dvd_trans) (simp add: gcd_0) 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

348 
next 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

349 
case (mod a b) 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

350 
then show ?case 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

351 
by (simp add: gcd_non_0 dvd_mod_iff) 
58023  352 
qed 
353 

354 
lemma dvd_gcd_iff: 

60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

355 
"k dvd gcd a b \<longleftrightarrow> k dvd a \<and> k dvd b" 
58023  356 
by (blast intro!: gcd_greatest intro: dvd_trans) 
357 

358 
lemmas gcd_greatest_iff = dvd_gcd_iff 

359 

360 
lemma gcd_zero [simp]: 

60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

361 
"gcd a b = 0 \<longleftrightarrow> a = 0 \<and> b = 0" 
58023  362 
by (metis dvd_0_left dvd_refl gcd_dvd1 gcd_dvd2 gcd_greatest)+ 
363 

60438  364 
lemma normalization_factor_gcd [simp]: 
365 
"normalization_factor (gcd a b) = (if a = 0 \<and> b = 0 then 0 else 1)" (is "?f a b = ?g a b") 

60569
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

366 
by (induct a b rule: gcd_eucl_induct) 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

367 
(auto simp add: gcd_0 gcd_non_0) 
58023  368 

369 
lemma gcdI: 

60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

370 
"k dvd a \<Longrightarrow> k dvd b \<Longrightarrow> (\<And>l. l dvd a \<Longrightarrow> l dvd b \<Longrightarrow> l dvd k) 
60438  371 
\<Longrightarrow> normalization_factor k = (if k = 0 then 0 else 1) \<Longrightarrow> k = gcd a b" 
58023  372 
by (intro normed_associated_imp_eq) (auto simp: associated_def intro: gcd_greatest) 
373 

374 
sublocale gcd!: abel_semigroup gcd 

375 
proof 

60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

376 
fix a b c 
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

377 
show "gcd (gcd a b) c = gcd a (gcd b c)" 
58023  378 
proof (rule gcdI) 
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

379 
have "gcd (gcd a b) c dvd gcd a b" "gcd a b dvd a" by simp_all 
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

380 
then show "gcd (gcd a b) c dvd a" by (rule dvd_trans) 
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

381 
have "gcd (gcd a b) c dvd gcd a b" "gcd a b dvd b" by simp_all 
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

382 
hence "gcd (gcd a b) c dvd b" by (rule dvd_trans) 
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

383 
moreover have "gcd (gcd a b) c dvd c" by simp 
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

384 
ultimately show "gcd (gcd a b) c dvd gcd b c" 
58023  385 
by (rule gcd_greatest) 
60438  386 
show "normalization_factor (gcd (gcd a b) c) = (if gcd (gcd a b) c = 0 then 0 else 1)" 
58023  387 
by auto 
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

388 
fix l assume "l dvd a" and "l dvd gcd b c" 
58023  389 
with dvd_trans[OF _ gcd_dvd1] and dvd_trans[OF _ gcd_dvd2] 
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

390 
have "l dvd b" and "l dvd c" by blast+ 
60526  391 
with \<open>l dvd a\<close> show "l dvd gcd (gcd a b) c" 
58023  392 
by (intro gcd_greatest) 
393 
qed 

394 
next 

60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

395 
fix a b 
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

396 
show "gcd a b = gcd b a" 
58023  397 
by (rule gcdI) (simp_all add: gcd_greatest) 
398 
qed 

399 

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

60438  401 
normalization_factor d = (if d = 0 then 0 else 1) \<and> 
58023  402 
(\<forall>e. e dvd a \<and> e dvd b \<longrightarrow> e dvd d) \<longleftrightarrow> d = gcd a b" 
403 
by (rule, auto intro: gcdI simp: gcd_greatest) 

404 

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

406 
using mult_dvd_mono [of 1] by auto 

407 

60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

408 
lemma gcd_1_left [simp]: "gcd 1 a = 1" 
58023  409 
by (rule sym, rule gcdI, simp_all) 
410 

60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

411 
lemma gcd_1 [simp]: "gcd a 1 = 1" 
58023  412 
by (rule sym, rule gcdI, simp_all) 
413 

414 
lemma gcd_proj2_if_dvd: 

60438  415 
"b dvd a \<Longrightarrow> gcd a b = b div normalization_factor b" 
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

416 
by (cases "b = 0", simp_all add: dvd_eq_mod_eq_0 gcd_non_0 gcd_0) 
58023  417 

418 
lemma gcd_proj1_if_dvd: 

60438  419 
"a dvd b \<Longrightarrow> gcd a b = a div normalization_factor a" 
58023  420 
by (subst gcd.commute, simp add: gcd_proj2_if_dvd) 
421 

60438  422 
lemma gcd_proj1_iff: "gcd m n = m div normalization_factor m \<longleftrightarrow> m dvd n" 
58023  423 
proof 
60438  424 
assume A: "gcd m n = m div normalization_factor m" 
58023  425 
show "m dvd n" 
426 
proof (cases "m = 0") 

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

60438  428 
from A have B: "m = gcd m n * normalization_factor m" 
58023  429 
by (simp add: unit_eq_div2) 
430 
show ?thesis by (subst B, simp add: mult_unit_dvd_iff) 

431 
qed (insert A, simp) 

432 
next 

433 
assume "m dvd n" 

60438  434 
then show "gcd m n = m div normalization_factor m" by (rule gcd_proj1_if_dvd) 
58023  435 
qed 
436 

60438  437 
lemma gcd_proj2_iff: "gcd m n = n div normalization_factor n \<longleftrightarrow> n dvd m" 
58023  438 
by (subst gcd.commute, simp add: gcd_proj1_iff) 
439 

440 
lemma gcd_mod1 [simp]: 

60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

441 
"gcd (a mod b) b = gcd a b" 
58023  442 
by (rule gcdI, metis dvd_mod_iff gcd_dvd1 gcd_dvd2, simp_all add: gcd_greatest dvd_mod_iff) 
443 

444 
lemma gcd_mod2 [simp]: 

60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

445 
"gcd a (b mod a) = gcd a b" 
58023  446 
by (rule gcdI, simp, metis dvd_mod_iff gcd_dvd1 gcd_dvd2, simp_all add: gcd_greatest dvd_mod_iff) 
447 

448 
lemma gcd_mult_distrib': 

60569
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

449 
"c div normalization_factor c * gcd a b = gcd (c * a) (c * b)" 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

450 
proof (cases "c = 0") 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

451 
case True then show ?thesis by (simp_all add: gcd_0) 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

452 
next 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

453 
case False then have [simp]: "is_unit (normalization_factor c)" by simp 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

454 
show ?thesis 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

455 
proof (induct a b rule: gcd_eucl_induct) 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

456 
case (zero a) show ?case 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

457 
proof (cases "a = 0") 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

458 
case True then show ?thesis by (simp add: gcd_0) 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

459 
next 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

460 
case False then have "is_unit (normalization_factor a)" by simp 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

461 
then show ?thesis 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

462 
by (simp add: gcd_0 unit_div_commute unit_div_mult_swap normalization_factor_mult is_unit_div_mult2_eq) 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

463 
qed 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

464 
case (mod a b) 
f2f1f6860959
generalized to definition from literature, which covers also polynomials
haftmann
parents:
60526
diff
changeset

465 
then show ?case by (simp add: mult_mod_right gcd.commute) 
58023  466 
qed 
467 
qed 

468 

469 
lemma gcd_mult_distrib: 

60438  470 
"k * gcd a b = gcd (k*a) (k*b) * normalization_factor k" 
58023  471 
proof 
60438  472 
let ?nf = "normalization_factor" 
58023  473 
from gcd_mult_distrib' 
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

474 
have "gcd (k*a) (k*b) = k div ?nf k * gcd a b" .. 
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

475 
also have "... = k * gcd a b div ?nf k" 
60438  476 
by (metis dvd_div_mult dvd_eq_mod_eq_0 mod_0 normalization_factor_dvd) 
58023  477 
finally show ?thesis 
59009
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset

478 
by simp 
58023  479 
qed 
480 

481 
lemma euclidean_size_gcd_le1 [simp]: 

482 
assumes "a \<noteq> 0" 

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

484 
proof  

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

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

60526  487 
with \<open>a \<noteq> 0\<close> show ?thesis by (subst (2) A, intro size_mult_mono) auto 
58023  488 
qed 
489 

490 
lemma euclidean_size_gcd_le2 [simp]: 

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

492 
by (subst gcd.commute, rule euclidean_size_gcd_le1) 

493 

494 
lemma euclidean_size_gcd_less1: 

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

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

497 
proof (rule ccontr) 

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

60526  499 
with \<open>a \<noteq> 0\<close> have "euclidean_size (gcd a b) = euclidean_size a" 
58023  500 
by (intro le_antisym, simp_all) 
501 
with assms have "a dvd gcd a b" by (auto intro: dvd_euclidean_size_eq_imp_dvd) 

502 
hence "a dvd b" using dvd_gcd_D2 by blast 

60526  503 
with \<open>\<not>a dvd b\<close> show False by contradiction 
58023  504 
qed 
505 

506 
lemma euclidean_size_gcd_less2: 

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

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

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

510 

60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

511 
lemma gcd_mult_unit1: "is_unit a \<Longrightarrow> gcd (b * a) c = gcd b c" 
58023  512 
apply (rule gcdI) 
513 
apply (rule dvd_trans, rule gcd_dvd1, simp add: unit_simps) 

514 
apply (rule gcd_dvd2) 

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

60438  516 
apply (subst normalization_factor_gcd, simp add: gcd_0) 
58023  517 
done 
518 

60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

519 
lemma gcd_mult_unit2: "is_unit a \<Longrightarrow> gcd b (c * a) = gcd b c" 
58023  520 
by (subst gcd.commute, subst gcd_mult_unit1, assumption, rule gcd.commute) 
521 

60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

522 
lemma gcd_div_unit1: "is_unit a \<Longrightarrow> gcd (b div a) c = gcd b c" 
60433  523 
by (erule is_unitE [of _ b]) (simp add: gcd_mult_unit1) 
58023  524 

60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

525 
lemma gcd_div_unit2: "is_unit a \<Longrightarrow> gcd b (c div a) = gcd b c" 
60433  526 
by (erule is_unitE [of _ c]) (simp add: gcd_mult_unit2) 
58023  527 

60438  528 
lemma gcd_idem: "gcd a a = a div normalization_factor a" 
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

529 
by (cases "a = 0") (simp add: gcd_0_left, rule sym, rule gcdI, simp_all) 
58023  530 

60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

531 
lemma gcd_right_idem: "gcd (gcd a b) b = gcd a b" 
58023  532 
apply (rule gcdI) 
533 
apply (simp add: ac_simps) 

534 
apply (rule gcd_dvd2) 

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

59009
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset

536 
apply simp 
58023  537 
done 
538 

60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

539 
lemma gcd_left_idem: "gcd a (gcd a b) = gcd a b" 
58023  540 
apply (rule gcdI) 
541 
apply simp 

542 
apply (rule dvd_trans, rule gcd_dvd2, rule gcd_dvd2) 

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

59009
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset

544 
apply simp 
58023  545 
done 
546 

547 
lemma comp_fun_idem_gcd: "comp_fun_idem gcd" 

548 
proof 

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

550 
by (simp add: fun_eq_iff ac_simps) 

551 
next 

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

553 
by (simp add: fun_eq_iff gcd_left_idem) 

554 
qed 

555 

556 
lemma coprime_dvd_mult: 

60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

557 
assumes "gcd c b = 1" and "c dvd a * b" 
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

558 
shows "c dvd a" 
58023  559 
proof  
60438  560 
let ?nf = "normalization_factor" 
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

561 
from assms gcd_mult_distrib [of a c b] 
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

562 
have A: "a = gcd (a * c) (a * b) * ?nf a" by simp 
60526  563 
from \<open>c dvd a * b\<close> show ?thesis by (subst A, simp_all add: gcd_greatest) 
58023  564 
qed 
565 

566 
lemma coprime_dvd_mult_iff: 

60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

567 
"gcd c b = 1 \<Longrightarrow> (c dvd a * b) = (c dvd a)" 
58023  568 
by (rule, rule coprime_dvd_mult, simp_all) 
569 

570 
lemma gcd_dvd_antisym: 

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

572 
proof (rule gcdI) 

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

574 
have "gcd c d dvd c" by simp 

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

576 
have "gcd c d dvd d" by simp 

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

60438  578 
show "normalization_factor (gcd a b) = (if gcd a b = 0 then 0 else 1)" 
59009
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset

579 
by simp 
58023  580 
fix l assume "l dvd c" and "l dvd d" 
581 
hence "l dvd gcd c d" by (rule gcd_greatest) 

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

583 
qed 

584 

585 
lemma gcd_mult_cancel: 

586 
assumes "gcd k n = 1" 

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

588 
proof (rule gcd_dvd_antisym) 

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

60526  590 
also note \<open>gcd k n = 1\<close> 
58023  591 
finally have "gcd (gcd (k * m) n) k = 1" by simp 
592 
hence "gcd (k * m) n dvd m" by (rule coprime_dvd_mult, simp add: ac_simps) 

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

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

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

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

597 
qed 

598 

599 
lemma coprime_crossproduct: 

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

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

602 
proof 

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

604 
next 

605 
assume ?lhs 

60526  606 
from \<open>?lhs\<close> have "a dvd b * d" unfolding associated_def by (metis dvd_mult_left) 
58023  607 
hence "a dvd b" by (simp add: coprime_dvd_mult_iff) 
60526  608 
moreover from \<open>?lhs\<close> have "b dvd a * c" unfolding associated_def by (metis dvd_mult_left) 
58023  609 
hence "b dvd a" by (simp add: coprime_dvd_mult_iff) 
60526  610 
moreover from \<open>?lhs\<close> have "c dvd d * b" 
59009
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset

611 
unfolding associated_def by (auto dest: dvd_mult_right simp add: ac_simps) 
58023  612 
hence "c dvd d" by (simp add: coprime_dvd_mult_iff gcd.commute) 
60526  613 
moreover from \<open>?lhs\<close> have "d dvd c * a" 
59009
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset

614 
unfolding associated_def by (auto dest: dvd_mult_right simp add: ac_simps) 
58023  615 
hence "d dvd c" by (simp add: coprime_dvd_mult_iff gcd.commute) 
616 
ultimately show ?rhs unfolding associated_def by simp 

617 
qed 

618 

619 
lemma gcd_add1 [simp]: 

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

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

622 

623 
lemma gcd_add2 [simp]: 

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

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

626 

60572
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset

627 
lemma gcd_add_mult: 
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset

628 
"gcd m (k * m + n) = gcd m n" 
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset

629 
proof  
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset

630 
have "gcd m ((k * m + n) mod m) = gcd m (k * m + n)" 
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset

631 
by (fact gcd_mod2) 
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset

632 
then show ?thesis by simp 
718b1ba06429
streamlined definitions and primitive lemma of euclidean algorithm, including code generation
haftmann
parents:
60571
diff
changeset

633 
qed 
58023  634 

60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

635 
lemma coprimeI: "(\<And>l. \<lbrakk>l dvd a; l dvd b\<rbrakk> \<Longrightarrow> l dvd 1) \<Longrightarrow> gcd a b = 1" 
58023  636 
by (rule sym, rule gcdI, simp_all) 
637 

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

59061  639 
by (auto intro: coprimeI gcd_greatest dvd_gcd_D1 dvd_gcd_D2) 
58023  640 

641 
lemma div_gcd_coprime: 

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

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

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

645 
shows "gcd a' b' = 1" 

646 
proof (rule coprimeI) 

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

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

59009
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset

649 
moreover have "a = a' * d" "b = b' * d" by simp_all 
58023  650 
ultimately have "a = (l * d) * s" "b = (l * d) * t" 
59009
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset

651 
by (simp_all only: ac_simps) 
58023  652 
hence "l*d dvd a" and "l*d dvd b" by (simp_all only: dvd_triv_left) 
653 
hence "l*d dvd d" by (simp add: gcd_greatest) 

59009
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset

654 
then obtain u where "d = l * d * u" .. 
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset

655 
then have "d * (l * u) = d" by (simp add: ac_simps) 
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset

656 
moreover from nz have "d \<noteq> 0" by simp 
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset

657 
with div_mult_self1_is_id have "d * (l * u) div d = l * u" . 
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset

658 
ultimately have "1 = l * u" 
60526  659 
using \<open>d \<noteq> 0\<close> by simp 
59009
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset

660 
then show "l dvd 1" .. 
58023  661 
qed 
662 

663 
lemma coprime_mult: 

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

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

666 
apply (subst gcd.commute) 

667 
using da apply (subst gcd_mult_cancel) 

668 
apply (subst gcd.commute, assumption) 

669 
apply (subst gcd.commute, rule db) 

670 
done 

671 

672 
lemma coprime_lmult: 

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

674 
shows "gcd d a = 1" 

675 
proof (rule coprimeI) 

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

677 
hence "l dvd a * b" by simp 

60526  678 
with \<open>l dvd d\<close> and dab show "l dvd 1" by (auto intro: gcd_greatest) 
58023  679 
qed 
680 

681 
lemma coprime_rmult: 

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

683 
shows "gcd d b = 1" 

684 
proof (rule coprimeI) 

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

686 
hence "l dvd a * b" by simp 

60526  687 
with \<open>l dvd d\<close> and dab show "l dvd 1" by (auto intro: gcd_greatest) 
58023  688 
qed 
689 

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

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

692 

693 
lemma gcd_coprime: 

60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

694 
assumes c: "gcd a b \<noteq> 0" and a: "a = a' * gcd a b" and b: "b = b' * gcd a b" 
58023  695 
shows "gcd a' b' = 1" 
696 
proof  

60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

697 
from c have "a \<noteq> 0 \<or> b \<noteq> 0" by simp 
58023  698 
with div_gcd_coprime have "gcd (a div gcd a b) (b div gcd a b) = 1" . 
699 
also from assms have "a div gcd a b = a'" by (metis div_mult_self2_is_id)+ 

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

701 
finally show ?thesis . 

702 
qed 

703 

704 
lemma coprime_power: 

705 
assumes "0 < n" 

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

707 
using assms proof (induct n) 

708 
case (Suc n) then show ?case 

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

710 
qed simp 

711 

712 
lemma gcd_coprime_exists: 

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

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

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

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

59009
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset

717 
apply (insert nz, auto intro: div_gcd_coprime) 
58023  718 
done 
719 

720 
lemma coprime_exp: 

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

722 
by (induct n, simp_all add: coprime_mult) 

723 

724 
lemma coprime_exp2 [intro]: 

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

726 
apply (rule coprime_exp) 

727 
apply (subst gcd.commute) 

728 
apply (rule coprime_exp) 

729 
apply (subst gcd.commute) 

730 
apply assumption 

731 
done 

732 

733 
lemma gcd_exp: 

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

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

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

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

738 
next 

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

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

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

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

743 
also note gcd_mult_distrib 

60438  744 
also have "normalization_factor ((gcd a b)^n) = 1" 
745 
by (simp add: normalization_factor_pow A) 

58023  746 
also have "(gcd a b)^n * (a div gcd a b)^n = a^n" 
747 
by (subst ac_simps, subst div_power, simp, rule dvd_div_mult_self, rule dvd_power_same, simp) 

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

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

750 
finally show ?thesis by simp 

751 
qed 

752 

753 
lemma coprime_common_divisor: 

60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

754 
"gcd a b = 1 \<Longrightarrow> a dvd a \<Longrightarrow> a dvd b \<Longrightarrow> is_unit a" 
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

755 
apply (subgoal_tac "a dvd gcd a b") 
59061  756 
apply simp 
58023  757 
apply (erule (1) gcd_greatest) 
758 
done 

759 

760 
lemma division_decomp: 

761 
assumes dc: "a dvd b * c" 

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

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

764 
assume "gcd a b = 0" 

59009
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset

765 
hence "a = 0 \<and> b = 0" by simp 
58023  766 
hence "a = 0 * c \<and> 0 dvd b \<and> c dvd c" by simp 
767 
then show ?thesis by blast 

768 
next 

769 
let ?d = "gcd a b" 

770 
assume "?d \<noteq> 0" 

771 
from gcd_coprime_exists[OF this] 

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

773 
by blast 

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

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

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

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

60526  778 
with \<open>?d \<noteq> 0\<close> have "a' dvd b' * c" by simp 
58023  779 
with coprime_dvd_mult[OF ab'(3)] 
780 
have "a' dvd c" by (subst (asm) ac_simps, blast) 

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

782 
then show ?thesis by blast 

783 
qed 

784 

60433  785 
lemma pow_divs_pow: 
58023  786 
assumes ab: "a ^ n dvd b ^ n" and n: "n \<noteq> 0" 
787 
shows "a dvd b" 

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

789 
assume "gcd a b = 0" 

59009
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset

790 
then show ?thesis by simp 
58023  791 
next 
792 
let ?d = "gcd a b" 

793 
assume "?d \<noteq> 0" 

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

60526  795 
from \<open>?d \<noteq> 0\<close> have zn: "?d ^ n \<noteq> 0" by (rule power_not_zero) 
796 
from gcd_coprime_exists[OF \<open>?d \<noteq> 0\<close>] 

58023  797 
obtain a' b' where ab': "a = a' * ?d" "b = b' * ?d" "gcd a' b' = 1" 
798 
by blast 

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

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

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

802 
by (simp only: power_mult_distrib ac_simps) 

59009
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset

803 
with zn have "a'^n dvd b'^n" by simp 
58023  804 
hence "a' dvd b'^n" using dvd_trans[of a' "a'^n" "b'^n"] by (simp add: m) 
805 
hence "a' dvd b'^m * b'" by (simp add: m ac_simps) 

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

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

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

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

810 
qed 

811 

60433  812 
lemma pow_divs_eq [simp]: 
58023  813 
"n \<noteq> 0 \<Longrightarrow> a ^ n dvd b ^ n \<longleftrightarrow> a dvd b" 
60433  814 
by (auto intro: pow_divs_pow dvd_power_same) 
58023  815 

60433  816 
lemma divs_mult: 
58023  817 
assumes mr: "m dvd r" and nr: "n dvd r" and mn: "gcd m n = 1" 
818 
shows "m * n dvd r" 

819 
proof  

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

821 
unfolding dvd_def by blast 

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

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

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

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

826 
then show ?thesis unfolding dvd_def by blast 

827 
qed 

828 

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

830 
by (subst add_commute, simp) 

831 

832 
lemma setprod_coprime [rule_format]: 

60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

833 
"(\<forall>i\<in>A. gcd (f i) a = 1) \<longrightarrow> gcd (\<Prod>i\<in>A. f i) a = 1" 
58023  834 
apply (cases "finite A") 
835 
apply (induct set: finite) 

836 
apply (auto simp add: gcd_mult_cancel) 

837 
done 

838 

839 
lemma coprime_divisors: 

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

841 
shows "gcd d e = 1" 

842 
proof  

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

844 
unfolding dvd_def by blast 

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

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

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

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

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

850 
qed 

851 

852 
lemma invertible_coprime: 

60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

853 
assumes "a * b mod m = 1" 
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

854 
shows "coprime a m" 
59009
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset

855 
proof  
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

856 
from assms have "coprime m (a * b mod m)" 
59009
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset

857 
by simp 
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

858 
then have "coprime m (a * b)" 
59009
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset

859 
by simp 
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

860 
then have "coprime m a" 
59009
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset

861 
by (rule coprime_lmult) 
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset

862 
then show ?thesis 
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset

863 
by (simp add: ac_simps) 
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset

864 
qed 
58023  865 

866 
lemma lcm_gcd: 

60438  867 
"lcm a b = a * b div (gcd a b * normalization_factor (a*b))" 
58023  868 
by (simp only: lcm_lcm_eucl gcd_gcd_eucl lcm_eucl_def) 
869 

870 
lemma lcm_gcd_prod: 

60438  871 
"lcm a b * gcd a b = a * b div normalization_factor (a*b)" 
58023  872 
proof (cases "a * b = 0") 
60438  873 
let ?nf = normalization_factor 
58023  874 
assume "a * b \<noteq> 0" 
58953  875 
hence "gcd a b \<noteq> 0" by simp 
58023  876 
from lcm_gcd have "lcm a b * gcd a b = gcd a b * (a * b div (?nf (a*b) * gcd a b))" 
877 
by (simp add: mult_ac) 

60526  878 
also from \<open>a * b \<noteq> 0\<close> have "... = a * b div ?nf (a*b)" 
60432  879 
by (simp add: div_mult_swap mult.commute) 
58023  880 
finally show ?thesis . 
58953  881 
qed (auto simp add: lcm_gcd) 
58023  882 

883 
lemma lcm_dvd1 [iff]: 

60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

884 
"a dvd lcm a b" 
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

885 
proof (cases "a*b = 0") 
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

886 
assume "a * b \<noteq> 0" 
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

887 
hence "gcd a b \<noteq> 0" by simp 
60438  888 
let ?c = "1 div normalization_factor (a * b)" 
60526  889 
from \<open>a * b \<noteq> 0\<close> have [simp]: "is_unit (normalization_factor (a * b))" by simp 
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

890 
from lcm_gcd_prod[of a b] have "lcm a b * gcd a b = a * ?c * b" 
60432  891 
by (simp add: div_mult_swap unit_div_commute) 
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

892 
hence "lcm a b * gcd a b div gcd a b = a * ?c * b div gcd a b" by simp 
60526  893 
with \<open>gcd a b \<noteq> 0\<close> have "lcm a b = a * ?c * b div gcd a b" 
58023  894 
by (subst (asm) div_mult_self2_is_id, simp_all) 
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

895 
also have "... = a * (?c * b div gcd a b)" 
58023  896 
by (metis div_mult_swap gcd_dvd2 mult_assoc) 
897 
finally show ?thesis by (rule dvdI) 

58953  898 
qed (auto simp add: lcm_gcd) 
58023  899 

900 
lemma lcm_least: 

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

902 
proof (cases "k = 0") 

60438  903 
let ?nf = normalization_factor 
58023  904 
assume "k \<noteq> 0" 
905 
hence "is_unit (?nf k)" by simp 

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

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

60526  908 
hence "gcd a b \<noteq> 0" using \<open>k \<noteq> 0\<close> by auto 
58023  909 
from A obtain r s where ar: "k = a * r" and bs: "k = b * s" 
910 
unfolding dvd_def by blast 

60526  911 
with \<open>k \<noteq> 0\<close> have "r * s \<noteq> 0" 
58953  912 
by auto (drule sym [of 0], simp) 
58023  913 
hence "is_unit (?nf (r * s))" by simp 
914 
let ?c = "?nf k div ?nf (r*s)" 

60526  915 
from \<open>is_unit (?nf k)\<close> and \<open>is_unit (?nf (r * s))\<close> have "is_unit ?c" by (rule unit_div) 
58023  916 
hence "?c \<noteq> 0" using not_is_unit_0 by fast 
917 
from ar bs have "k * k * gcd s r = ?nf k * k * gcd (k * s) (k * r)" 

58953  918 
by (subst mult_assoc, subst gcd_mult_distrib[of k s r], simp only: ac_simps) 
58023  919 
also have "... = ?nf k * k * gcd ((r*s) * a) ((r*s) * b)" 
60526  920 
by (subst (3) \<open>k = a * r\<close>, subst (3) \<open>k = b * s\<close>, simp add: algebra_simps) 
921 
also have "... = ?c * r*s * k * gcd a b" using \<open>r * s \<noteq> 0\<close> 

58023  922 
by (subst gcd_mult_distrib'[symmetric], simp add: algebra_simps unit_simps) 
923 
finally have "(a*r) * (b*s) * gcd s r = ?c * k * r * s * gcd a b" 

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

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

926 
by (simp add: algebra_simps) 

60526  927 
hence "?c * k * gcd a b = a * b * gcd s r" using \<open>r * s \<noteq> 0\<close> 
58023  928 
by (metis div_mult_self2_is_id) 
929 
also have "... = lcm a b * gcd a b * gcd s r * ?nf (a*b)" 

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

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

932 
by (simp add: algebra_simps) 

60526  933 
finally have "k * ?c = lcm a b * gcd s r * ?nf (a*b)" using \<open>gcd a b \<noteq> 0\<close> 
58023  934 
by (metis mult.commute div_mult_self2_is_id) 
60526  935 
hence "k = lcm a b * (gcd s r * ?nf (a*b)) div ?c" using \<open>?c \<noteq> 0\<close> 
58023  936 
by (metis div_mult_self2_is_id mult_assoc) 
60526  937 
also have "... = lcm a b * (gcd s r * ?nf (a*b) div ?c)" using \<open>is_unit ?c\<close> 
58023  938 
by (simp add: unit_simps) 
939 
finally show ?thesis by (rule dvdI) 

940 
qed simp 

941 

942 
lemma lcm_zero: 

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

944 
proof  

60438  945 
let ?nf = normalization_factor 
58023  946 
{ 
947 
assume "a \<noteq> 0" "b \<noteq> 0" 

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

60526  949 
moreover from \<open>a \<noteq> 0\<close> and \<open>b \<noteq> 0\<close> have "gcd a b \<noteq> 0" by simp 
58023  950 
ultimately have "lcm a b \<noteq> 0" using lcm_gcd_prod[of a b] by (intro notI, simp) 
951 
} moreover { 

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

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

954 
} 

955 
ultimately show ?thesis by blast 

956 
qed 

957 

958 
lemmas lcm_0_iff = lcm_zero 

959 

960 
lemma gcd_lcm: 

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

60438  962 
shows "gcd a b = a * b div (lcm a b * normalization_factor (a * b))" 
58023  963 
proof 
59009
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset

964 
from assms have "gcd a b \<noteq> 0" by (simp add: lcm_zero) 
60438  965 
let ?c = "normalization_factor (a * b)" 
60526  966 
from \<open>lcm a b \<noteq> 0\<close> have "?c \<noteq> 0" by (intro notI, simp add: lcm_zero no_zero_divisors) 
58023  967 
hence "is_unit ?c" by simp 
968 
from lcm_gcd_prod [of a b] have "gcd a b = a * b div ?c div lcm a b" 

60526  969 
by (subst (2) div_mult_self2_is_id[OF \<open>lcm a b \<noteq> 0\<close>, symmetric], simp add: mult_ac) 
970 
also from \<open>is_unit ?c\<close> have "... = a * b div (lcm a b * ?c)" 

971 
by (metis \<open>?c \<noteq> 0\<close> div_mult_mult1 dvd_mult_div_cancel mult_commute normalization_factor_dvd') 

60433  972 
finally show ?thesis . 
58023  973 
qed 
974 

60438  975 
lemma normalization_factor_lcm [simp]: 
976 
"normalization_factor (lcm a b) = (if a = 0 \<or> b = 0 then 0 else 1)" 

58023  977 
proof (cases "a = 0 \<or> b = 0") 
978 
case True then show ?thesis 

58953  979 
by (auto simp add: lcm_gcd) 
58023  980 
next 
981 
case False 

60438  982 
let ?nf = normalization_factor 
58023  983 
from lcm_gcd_prod[of a b] 
984 
have "?nf (lcm a b) * ?nf (gcd a b) = ?nf (a*b) div ?nf (a*b)" 

60438  985 
by (metis div_by_0 div_self normalization_correct normalization_factor_0 normalization_factor_mult) 
58023  986 
also have "... = (if a*b = 0 then 0 else 1)" 
58953  987 
by simp 
988 
finally show ?thesis using False by simp 

58023  989 
qed 
990 

60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

991 
lemma lcm_dvd2 [iff]: "b dvd lcm a b" 
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

992 
using lcm_dvd1 [of b a] by (simp add: lcm_gcd ac_simps) 
58023  993 

994 
lemma lcmI: 

60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

995 
"\<lbrakk>a dvd k; b dvd k; \<And>l. a dvd l \<Longrightarrow> b dvd l \<Longrightarrow> k dvd l; 
60438  996 
normalization_factor k = (if k = 0 then 0 else 1)\<rbrakk> \<Longrightarrow> k = lcm a b" 
58023  997 
by (intro normed_associated_imp_eq) (auto simp: associated_def intro: lcm_least) 
998 

999 
sublocale lcm!: abel_semigroup lcm 

1000 
proof 

60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

1001 
fix a b c 
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

1002 
show "lcm (lcm a b) c = lcm a (lcm b c)" 
58023  1003 
proof (rule lcmI) 
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

1004 
have "a dvd lcm a b" and "lcm a b dvd lcm (lcm a b) c" by simp_all 
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

1005 
then show "a dvd lcm (lcm a b) c" by (rule dvd_trans) 
58023  1006 

60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

1007 
have "b dvd lcm a b" and "lcm a b dvd lcm (lcm a b) c" by simp_all 
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

1008 
hence "b dvd lcm (lcm a b) c" by (rule dvd_trans) 
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

1009 
moreover have "c dvd lcm (lcm a b) c" by simp 
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

1010 
ultimately show "lcm b c dvd lcm (lcm a b) c" by (rule lcm_least) 
58023  1011 

60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

1012 
fix l assume "a dvd l" and "lcm b c dvd l" 
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

1013 
have "b dvd lcm b c" by simp 
60526  1014 
from this and \<open>lcm b c dvd l\<close> have "b dvd l" by (rule dvd_trans) 
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

1015 
have "c dvd lcm b c" by simp 
60526  1016 
from this and \<open>lcm b c dvd l\<close> have "c dvd l" by (rule dvd_trans) 
1017 
from \<open>a dvd l\<close> and \<open>b dvd l\<close> have "lcm a b dvd l" by (rule lcm_least) 

1018 
from this and \<open>c dvd l\<close> show "lcm (lcm a b) c dvd l" by (rule lcm_least) 

58023  1019 
qed (simp add: lcm_zero) 
1020 
next 

60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

1021 
fix a b 
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

1022 
show "lcm a b = lcm b a" 
58023  1023 
by (simp add: lcm_gcd ac_simps) 
1024 
qed 

1025 

1026 
lemma dvd_lcm_D1: 

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

1028 
by (rule dvd_trans, rule lcm_dvd1, assumption) 

1029 

1030 
lemma dvd_lcm_D2: 

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

1032 
by (rule dvd_trans, rule lcm_dvd2, assumption) 

1033 

1034 
lemma gcd_dvd_lcm [simp]: 

1035 
"gcd a b dvd lcm a b" 

1036 
by (metis dvd_trans gcd_dvd2 lcm_dvd2) 

1037 

1038 
lemma lcm_1_iff: 

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

1040 
proof 

1041 
assume "lcm a b = 1" 

59061  1042 
then show "is_unit a \<and> is_unit b" by auto 
58023  1043 
next 
1044 
assume "is_unit a \<and> is_unit b" 

59061  1045 
hence "a dvd 1" and "b dvd 1" by simp_all 
1046 
hence "is_unit (lcm a b)" by (rule lcm_least) 

60438  1047 
hence "lcm a b = normalization_factor (lcm a b)" 
1048 
by (subst normalization_factor_unit, simp_all) 

60526  1049 
also have "\<dots> = 1" using \<open>is_unit a \<and> is_unit b\<close> 
59061  1050 
by auto 
58023  1051 
finally show "lcm a b = 1" . 
1052 
qed 

1053 

1054 
lemma lcm_0_left [simp]: 

60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

1055 
"lcm 0 a = 0" 
58023  1056 
by (rule sym, rule lcmI, simp_all) 
1057 

1058 
lemma lcm_0 [simp]: 

60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

1059 
"lcm a 0 = 0" 
58023  1060 
by (rule sym, rule lcmI, simp_all) 
1061 

1062 
lemma lcm_unique: 

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

60438  1064 
normalization_factor d = (if d = 0 then 0 else 1) \<and> 
58023  1065 
(\<forall>e. a dvd e \<and> b dvd e \<longrightarrow> d dvd e) \<longleftrightarrow> d = lcm a b" 
1066 
by (rule, auto intro: lcmI simp: lcm_least lcm_zero) 

1067 

1068 
lemma dvd_lcm_I1 [simp]: 

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

1070 
by (metis lcm_dvd1 dvd_trans) 

1071 

1072 
lemma dvd_lcm_I2 [simp]: 

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

1074 
by (metis lcm_dvd2 dvd_trans) 

1075 

1076 
lemma lcm_1_left [simp]: 

60438  1077 
"lcm 1 a = a div normalization_factor a" 
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

1078 
by (cases "a = 0") (simp, rule sym, rule lcmI, simp_all) 
58023  1079 

1080 
lemma lcm_1_right [simp]: 

60438  1081 
"lcm a 1 = a div normalization_factor a" 
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

1082 
using lcm_1_left [of a] by (simp add: ac_simps) 
58023  1083 

1084 
lemma lcm_coprime: 

60438  1085 
"gcd a b = 1 \<Longrightarrow> lcm a b = a * b div normalization_factor (a*b)" 
58023  1086 
by (subst lcm_gcd) simp 
1087 

1088 
lemma lcm_proj1_if_dvd: 

60438  1089 
"b dvd a \<Longrightarrow> lcm a b = a div normalization_factor a" 
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

1090 
by (cases "a = 0") (simp, rule sym, rule lcmI, simp_all) 
58023  1091 

1092 
lemma lcm_proj2_if_dvd: 

60438  1093 
"a dvd b \<Longrightarrow> lcm a b = b div normalization_factor b" 
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

1094 
using lcm_proj1_if_dvd [of a b] by (simp add: ac_simps) 
58023  1095 

1096 
lemma lcm_proj1_iff: 

60438  1097 
"lcm m n = m div normalization_factor m \<longleftrightarrow> n dvd m" 
58023  1098 
proof 
60438  1099 
assume A: "lcm m n = m div normalization_factor m" 
58023  1100 
show "n dvd m" 
1101 
proof (cases "m = 0") 

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

60438  1103 
from A have B: "m = lcm m n * normalization_factor m" 
58023  1104 
by (simp add: unit_eq_div2) 
1105 
show ?thesis by (subst B, simp) 

1106 
qed simp 

1107 
next 

1108 
assume "n dvd m" 

60438  1109 
then show "lcm m n = m div normalization_factor m" by (rule lcm_proj1_if_dvd) 
58023  1110 
qed 
1111 

1112 
lemma lcm_proj2_iff: 

60438  1113 
"lcm m n = n div normalization_factor n \<longleftrightarrow> m dvd n" 
58023  1114 
using lcm_proj1_iff [of n m] by (simp add: ac_simps) 
1115 

1116 
lemma euclidean_size_lcm_le1: 

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

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

1119 
proof  

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

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

60526  1122 
with \<open>a \<noteq> 0\<close> and \<open>b \<noteq> 0\<close> have "c \<noteq> 0" by (auto simp: lcm_zero) 
58023  1123 
then show ?thesis by (subst A, intro size_mult_mono) 
1124 
qed 

1125 

1126 
lemma euclidean_size_lcm_le2: 

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

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

1129 

1130 
lemma euclidean_size_lcm_less1: 

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

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

1133 
proof (rule ccontr) 

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

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

60526  1136 
with \<open>a \<noteq> 0\<close> and \<open>b \<noteq> 0\<close> have "euclidean_size (lcm a b) = euclidean_size a" 
58023  1137 
by (intro le_antisym, simp, intro euclidean_size_lcm_le1) 
1138 
with assms have "lcm a b dvd a" 

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

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

60526  1141 
with \<open>\<not>b dvd a\<close> show False by contradiction 
58023  1142 
qed 
1143 

1144 
lemma euclidean_size_lcm_less2: 

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

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

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

1148 

1149 
lemma lcm_mult_unit1: 

60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

1150 
"is_unit a \<Longrightarrow> lcm (b * a) c = lcm b c" 
58023  1151 
apply (rule lcmI) 
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

1152 
apply (rule dvd_trans[of _ "b * a"], simp, rule lcm_dvd1) 
58023  1153 
apply (rule lcm_dvd2) 
1154 
apply (rule lcm_least, simp add: unit_simps, assumption) 

60438  1155 
apply (subst normalization_factor_lcm, simp add: lcm_zero) 
58023  1156 
done 
1157 

1158 
lemma lcm_mult_unit2: 

60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

1159 
"is_unit a \<Longrightarrow> lcm b (c * a) = lcm b c" 
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

1160 
using lcm_mult_unit1 [of a c b] by (simp add: ac_simps) 
58023  1161 

1162 
lemma lcm_div_unit1: 

60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

1163 
"is_unit a \<Longrightarrow> lcm (b div a) c = lcm b c" 
60433  1164 
by (erule is_unitE [of _ b]) (simp add: lcm_mult_unit1) 
58023  1165 

1166 
lemma lcm_div_unit2: 

60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

1167 
"is_unit a \<Longrightarrow> lcm b (c div a) = lcm b c" 
60433  1168 
by (erule is_unitE [of _ c]) (simp add: lcm_mult_unit2) 
58023  1169 

1170 
lemma lcm_left_idem: 

60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

1171 
"lcm a (lcm a b) = lcm a b" 
58023  1172 
apply (rule lcmI) 
1173 
apply simp 

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

1175 
apply (rule lcm_least, assumption) 

1176 
apply (erule (1) lcm_least) 

1177 
apply (auto simp: lcm_zero) 

1178 
done 

1179 

1180 
lemma lcm_right_idem: 

60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

1181 
"lcm (lcm a b) b = lcm a b" 
58023  1182 
apply (rule lcmI) 
1183 
apply (subst lcm.assoc, rule lcm_dvd1) 

1184 
apply (rule lcm_dvd2) 

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

1186 
apply (auto simp: lcm_zero) 

1187 
done 

1188 

1189 
lemma comp_fun_idem_lcm: "comp_fun_idem lcm" 

1190 
proof 

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

1192 
by (simp add: fun_eq_iff ac_simps) 

1193 
next 

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

1195 
by (intro ext, simp add: lcm_left_idem) 

1196 
qed 

1197 

60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

1198 
lemma dvd_Lcm [simp]: "a \<in> A \<Longrightarrow> a dvd Lcm A" 
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

1199 
and Lcm_dvd [simp]: "(\<forall>a\<in>A. a dvd l') \<Longrightarrow> Lcm A dvd l'" 
60438  1200 
and normalization_factor_Lcm [simp]: 
1201 
"normalization_factor (Lcm A) = (if Lcm A = 0 then 0 else 1)" 

58023  1202 
proof  
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

1203 
have "(\<forall>a\<in>A. a dvd Lcm A) \<and> (\<forall>l'. (\<forall>a\<in>A. a dvd l') \<longrightarrow> Lcm A dvd l') \<and> 
60438  1204 
normalization_factor (Lcm A) = (if Lcm A = 0 then 0 else 1)" (is ?thesis) 
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

1205 
proof (cases "\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l)") 
58023  1206 
case False 
1207 
hence "Lcm A = 0" by (auto simp: Lcm_Lcm_eucl Lcm_eucl_def) 

1208 
with False show ?thesis by auto 

1209 
next 

1210 
case True 

60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

1211 
then obtain l\<^sub>0 where l\<^sub>0_props: "l\<^sub>0 \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l\<^sub>0)" by blast 
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

1212 
def n \<equiv> "LEAST n. \<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n" 
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

1213 
def l \<equiv> "SOME l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n" 
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

1214 
have "\<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n" 
58023  1215 
apply (subst n_def) 
1216 
apply (rule LeastI[of _ "euclidean_size l\<^sub>0"]) 

1217 
apply (rule exI[of _ l\<^sub>0]) 

1218 
apply (simp add: l\<^sub>0_props) 

1219 
done 

60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

1220 
from someI_ex[OF this] have "l \<noteq> 0" and "\<forall>a\<in>A. a dvd l" and "euclidean_size l = n" 
58023  1221 
unfolding l_def by simp_all 
1222 
{ 

60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

1223 
fix l' assume "\<forall>a\<in>A. a dvd l'" 
60526  1224 
with \<open>\<forall>a\<in>A. a dvd l\<close> have "\<forall>a\<in>A. a dvd gcd l l'" by (auto intro: gcd_greatest) 
1225 
moreover from \<open>l \<noteq> 0\<close> have "gcd l l' \<noteq> 0" by simp 

60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

1226 
ultimately have "\<exists>b. b \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd b) \<and> euclidean_size b = euclidean_size (gcd l l')" 
58023  1227 
by (intro exI[of _ "gcd l l'"], auto) 
1228 
hence "euclidean_size (gcd l l') \<ge> n" by (subst n_def) (rule Least_le) 

1229 
moreover have "euclidean_size (gcd l l') \<le> n" 

1230 
proof  

1231 
have "gcd l l' dvd l" by simp 

1232 
then obtain a where "l = gcd l l' * a" unfolding dvd_def by blast 

60526  1233 
with \<open>l \<noteq> 0\<close> have "a \<noteq> 0" by auto 
58023  1234 
hence "euclidean_size (gcd l l') \<le> euclidean_size (gcd l l' * a)" 
1235 
by (rule size_mult_mono) 

60526  1236 
also have "gcd l l' * a = l" using \<open>l = gcd l l' * a\<close> .. 
1237 
also note \<open>euclidean_size l = n\<close> 

58023  1238 
finally show "euclidean_size (gcd l l') \<le> n" . 
1239 
qed 