author  haftmann 
Fri, 12 Jun 2015 21:52:48 +0200  
changeset 60436  77e694c0c919 
parent 60433  720f210c5b1d 
child 60437  63edc650cf67 
permissions  rwrr 
58023  1 
(* Author: Manuel Eberl *) 
2 

58889  3 
section {* Abstract euclidean algorithm *} 
58023  4 

5 
theory Euclidean_Algorithm 

6 
imports Complex_Main 

7 
begin 

8 

60436
77e694c0c919
simplified relationship between associated and is_unit
haftmann
parents:
60433
diff
changeset

9 
context semidom_divide 
77e694c0c919
simplified relationship between associated and is_unit
haftmann
parents:
60433
diff
changeset

10 
begin 
77e694c0c919
simplified relationship between associated and is_unit
haftmann
parents:
60433
diff
changeset

11 

77e694c0c919
simplified relationship between associated and is_unit
haftmann
parents:
60433
diff
changeset

12 
lemma mult_cancel_right [simp]: 
77e694c0c919
simplified relationship between associated and is_unit
haftmann
parents:
60433
diff
changeset

13 
"a * c = b * c \<longleftrightarrow> c = 0 \<or> a = b" 
77e694c0c919
simplified relationship between associated and is_unit
haftmann
parents:
60433
diff
changeset

14 
proof (cases "c = 0") 
77e694c0c919
simplified relationship between associated and is_unit
haftmann
parents:
60433
diff
changeset

15 
case True then show ?thesis by simp 
77e694c0c919
simplified relationship between associated and is_unit
haftmann
parents:
60433
diff
changeset

16 
next 
77e694c0c919
simplified relationship between associated and is_unit
haftmann
parents:
60433
diff
changeset

17 
case False 
77e694c0c919
simplified relationship between associated and is_unit
haftmann
parents:
60433
diff
changeset

18 
{ assume "a * c = b * c" 
77e694c0c919
simplified relationship between associated and is_unit
haftmann
parents:
60433
diff
changeset

19 
then have "a * c div c = b * c div c" 
77e694c0c919
simplified relationship between associated and is_unit
haftmann
parents:
60433
diff
changeset

20 
by simp 
77e694c0c919
simplified relationship between associated and is_unit
haftmann
parents:
60433
diff
changeset

21 
with False have "a = b" 
77e694c0c919
simplified relationship between associated and is_unit
haftmann
parents:
60433
diff
changeset

22 
by simp 
77e694c0c919
simplified relationship between associated and is_unit
haftmann
parents:
60433
diff
changeset

23 
} then show ?thesis by auto 
77e694c0c919
simplified relationship between associated and is_unit
haftmann
parents:
60433
diff
changeset

24 
qed 
77e694c0c919
simplified relationship between associated and is_unit
haftmann
parents:
60433
diff
changeset

25 

77e694c0c919
simplified relationship between associated and is_unit
haftmann
parents:
60433
diff
changeset

26 
lemma mult_cancel_left [simp]: 
77e694c0c919
simplified relationship between associated and is_unit
haftmann
parents:
60433
diff
changeset

27 
"c * a = c * b \<longleftrightarrow> c = 0 \<or> a = b" 
77e694c0c919
simplified relationship between associated and is_unit
haftmann
parents:
60433
diff
changeset

28 
using mult_cancel_right [of a c b] by (simp add: ac_simps) 
77e694c0c919
simplified relationship between associated and is_unit
haftmann
parents:
60433
diff
changeset

29 

77e694c0c919
simplified relationship between associated and is_unit
haftmann
parents:
60433
diff
changeset

30 
end 
77e694c0c919
simplified relationship between associated and is_unit
haftmann
parents:
60433
diff
changeset

31 

58023  32 
context semiring_div 
33 
begin 

34 

60433  35 
text \<open>Units: invertible elements in a ring\<close> 
36 

59061  37 
abbreviation is_unit :: "'a \<Rightarrow> bool" 
58023  38 
where 
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

39 
"is_unit a \<equiv> a dvd 1" 
58023  40 

60433  41 
lemma not_is_unit_0 [simp]: 
42 
"\<not> is_unit 0" 

43 
by simp 

44 

45 
lemma unit_imp_dvd [dest]: 

46 
"is_unit b \<Longrightarrow> b dvd a" 

47 
by (rule dvd_trans [of _ 1]) simp_all 

48 

49 
lemma unit_dvdE: 

50 
assumes "is_unit a" 

51 
obtains c where "a \<noteq> 0" and "b = a * c" 

52 
proof  

53 
from assms have "a dvd b" by auto 

54 
then obtain c where "b = a * c" .. 

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

56 
ultimately show thesis using that by blast 

57 
qed 

58 

59 
lemma dvd_unit_imp_unit: 

60 
"a dvd b \<Longrightarrow> is_unit b \<Longrightarrow> is_unit a" 

61 
by (rule dvd_trans) 

62 

63 
lemma unit_div_1_unit [simp, intro]: 

64 
assumes "is_unit a" 

65 
shows "is_unit (1 div a)" 

66 
proof  

67 
from assms have "1 = 1 div a * a" by simp 

68 
then show "is_unit (1 div a)" by (rule dvdI) 

69 
qed 

70 

71 
lemma is_unitE [elim?]: 

72 
assumes "is_unit a" 

73 
obtains b where "a \<noteq> 0" and "b \<noteq> 0" 

74 
and "is_unit b" and "1 div a = b" and "1 div b = a" 

75 
and "a * b = 1" and "c div a = c * b" 

76 
proof (rule that) 

77 
def b \<equiv> "1 div a" 

78 
then show "1 div a = b" by simp 

79 
from b_def `is_unit a` show "is_unit b" by simp 

80 
from `is_unit a` and `is_unit b` show "a \<noteq> 0" and "b \<noteq> 0" by auto 

81 
from b_def `is_unit a` show "a * b = 1" by simp 

82 
then have "1 = a * b" .. 

83 
with b_def `b \<noteq> 0` show "1 div b = a" by simp 

84 
from `is_unit a` have "a dvd c" .. 

85 
then obtain d where "c = a * d" .. 

86 
with `a \<noteq> 0` `a * b = 1` show "c div a = c * b" 

87 
by (simp add: mult.assoc mult.left_commute [of a]) 

88 
qed 

58023  89 

90 
lemma unit_prod [intro]: 

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

91 
"is_unit a \<Longrightarrow> is_unit b \<Longrightarrow> is_unit (a * b)" 
60433  92 
by (subst mult_1_left [of 1, symmetric]) (rule mult_dvd_mono) 
93 

94 
lemma unit_div [intro]: 

95 
"is_unit a \<Longrightarrow> is_unit b \<Longrightarrow> is_unit (a div b)" 

96 
by (erule is_unitE [of b a]) (simp add: ac_simps unit_prod) 

58023  97 

98 
lemma mult_unit_dvd_iff: 

60433  99 
assumes "is_unit b" 
100 
shows "a * b dvd c \<longleftrightarrow> a dvd c" 

58023  101 
proof 
60433  102 
assume "a * b dvd c" 
103 
with assms show "a dvd c" 

104 
by (simp add: dvd_mult_left) 

58023  105 
next 
60433  106 
assume "a dvd c" 
107 
then obtain k where "c = a * k" .. 

108 
with assms have "c = (a * b) * (1 div b * k)" 

109 
by (simp add: mult_ac) 

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

110 
then show "a * b dvd c" by (rule dvdI) 
58023  111 
qed 
112 

113 
lemma dvd_mult_unit_iff: 

60433  114 
assumes "is_unit b" 
115 
shows "a dvd c * b \<longleftrightarrow> a dvd c" 

58023  116 
proof 
60433  117 
assume "a dvd c * b" 
118 
with assms have "c * b dvd c * (b * (1 div b))" 

119 
by (subst mult_assoc [symmetric]) simp 

120 
also from `is_unit b` have "b * (1 div b) = 1" by (rule is_unitE) simp 

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

121 
finally have "c * b dvd c" by simp 
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

122 
with `a dvd c * b` show "a dvd c" by (rule dvd_trans) 
58023  123 
next 
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

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

125 
then show "a dvd c * b" by simp 
58023  126 
qed 
127 

60433  128 
lemma div_unit_dvd_iff: 
129 
"is_unit b \<Longrightarrow> a div b dvd c \<longleftrightarrow> a dvd c" 

130 
by (erule is_unitE [of _ a]) (auto simp add: mult_unit_dvd_iff) 

131 

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

133 
"is_unit b \<Longrightarrow> a dvd c div b \<longleftrightarrow> a dvd c" 
60433  134 
by (erule is_unitE [of _ c]) (simp add: dvd_mult_unit_iff) 
58023  135 

60433  136 
lemmas unit_dvd_iff = mult_unit_dvd_iff div_unit_dvd_iff 
137 
dvd_mult_unit_iff dvd_div_unit_iff  \<open>FIXME consider fact collection\<close> 

58023  138 

60433  139 
lemma unit_mult_div_div [simp]: 
140 
"is_unit a \<Longrightarrow> b * (1 div a) = b div a" 

141 
by (erule is_unitE [of _ b]) simp 

142 

143 
lemma unit_div_mult_self [simp]: 

144 
"is_unit a \<Longrightarrow> b div a * a = b" 

145 
by (rule dvd_div_mult_self) auto 

146 

147 
lemma unit_div_1_div_1 [simp]: 

148 
"is_unit a \<Longrightarrow> 1 div (1 div a) = a" 

149 
by (erule is_unitE) simp 

58023  150 

151 
lemma unit_div_mult_swap: 

60433  152 
"is_unit c \<Longrightarrow> a * (b div c) = (a * b) div c" 
153 
by (erule unit_dvdE [of _ b]) (simp add: mult.left_commute [of _ c]) 

58023  154 

155 
lemma unit_div_commute: 

60433  156 
"is_unit b \<Longrightarrow> (a div b) * c = (a * c) div b" 
157 
using unit_div_mult_swap [of b c a] by (simp add: ac_simps) 

58023  158 

60433  159 
lemma unit_eq_div1: 
160 
"is_unit b \<Longrightarrow> a div b = c \<longleftrightarrow> a = c * b" 

161 
by (auto elim: is_unitE) 

58023  162 

60433  163 
lemma unit_eq_div2: 
164 
"is_unit b \<Longrightarrow> a = c div b \<longleftrightarrow> a * b = c" 

165 
using unit_eq_div1 [of b c a] by auto 

166 

167 
lemma unit_mult_left_cancel: 

168 
assumes "is_unit a" 

169 
shows "a * b = a * c \<longleftrightarrow> b = c" (is "?P \<longleftrightarrow> ?Q") 

60436
77e694c0c919
simplified relationship between associated and is_unit
haftmann
parents:
60433
diff
changeset

170 
using assms mult_cancel_left [of a b c] by auto 
58023  171 

60433  172 
lemma unit_mult_right_cancel: 
173 
"is_unit a \<Longrightarrow> b * a = c * a \<longleftrightarrow> b = c" 

174 
using unit_mult_left_cancel [of a b c] by (auto simp add: ac_simps) 

175 

176 
lemma unit_div_cancel: 

177 
assumes "is_unit a" 

178 
shows "b div a = c div a \<longleftrightarrow> b = c" 

179 
proof  

180 
from assms have "is_unit (1 div a)" by simp 

181 
then have "b * (1 div a) = c * (1 div a) \<longleftrightarrow> b = c" 

182 
by (rule unit_mult_right_cancel) 

183 
with assms show ?thesis by simp 

184 
qed 

185 

186 

187 
text \<open>Associated elements in a ring â€“ an equivalence relation induced by the quasiorder divisibility \<close> 

188 

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

190 
where 

191 
"associated a b \<longleftrightarrow> a dvd b \<and> b dvd a" 

192 

193 
lemma associatedI: 

194 
"a dvd b \<Longrightarrow> b dvd a \<Longrightarrow> associated a b" 

195 
by (simp add: associated_def) 

196 

197 
lemma associatedD1: 

198 
"associated a b \<Longrightarrow> a dvd b" 

58023  199 
by (simp add: associated_def) 
200 

60433  201 
lemma associatedD2: 
202 
"associated a b \<Longrightarrow> b dvd a" 

203 
by (simp add: associated_def) 

204 

205 
lemma associated_refl [simp]: 

206 
"associated a a" 

207 
by (auto intro: associatedI) 

208 

209 
lemma associated_sym: 

210 
"associated b a \<longleftrightarrow> associated a b" 

211 
by (auto intro: associatedI dest: associatedD1 associatedD2) 

212 

213 
lemma associated_trans: 

214 
"associated a b \<Longrightarrow> associated b c \<Longrightarrow> associated a c" 

215 
by (auto intro: associatedI dvd_trans dest: associatedD1 associatedD2) 

216 

217 
lemma equivp_associated: 

218 
"equivp associated" 

219 
proof (rule equivpI) 

220 
show "reflp associated" 

221 
by (rule reflpI) simp 

222 
show "symp associated" 

223 
by (rule sympI) (simp add: associated_sym) 

224 
show "transp associated" 

225 
by (rule transpI) (fact associated_trans) 

226 
qed 

227 

58023  228 
lemma associated_0 [simp]: 
229 
"associated 0 b \<longleftrightarrow> b = 0" 

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

60433  231 
by (auto dest: associatedD1 associatedD2) 
58023  232 

233 
lemma associated_unit: 

60433  234 
"associated a b \<Longrightarrow> is_unit a \<Longrightarrow> is_unit b" 
235 
using dvd_unit_imp_unit by (auto dest!: associatedD1 associatedD2) 

58023  236 

60436
77e694c0c919
simplified relationship between associated and is_unit
haftmann
parents:
60433
diff
changeset

237 
lemma is_unit_associatedI: 
77e694c0c919
simplified relationship between associated and is_unit
haftmann
parents:
60433
diff
changeset

238 
assumes "is_unit c" and "a = c * b" 
77e694c0c919
simplified relationship between associated and is_unit
haftmann
parents:
60433
diff
changeset

239 
shows "associated a b" 
77e694c0c919
simplified relationship between associated and is_unit
haftmann
parents:
60433
diff
changeset

240 
proof (rule associatedI) 
77e694c0c919
simplified relationship between associated and is_unit
haftmann
parents:
60433
diff
changeset

241 
from `a = c * b` show "b dvd a" by auto 
77e694c0c919
simplified relationship between associated and is_unit
haftmann
parents:
60433
diff
changeset

242 
from `is_unit c` obtain d where "c * d = 1" by (rule is_unitE) 
77e694c0c919
simplified relationship between associated and is_unit
haftmann
parents:
60433
diff
changeset

243 
moreover from `a = c * b` have "d * a = d * (c * b)" by simp 
77e694c0c919
simplified relationship between associated and is_unit
haftmann
parents:
60433
diff
changeset

244 
ultimately have "b = a * d" by (simp add: ac_simps) 
77e694c0c919
simplified relationship between associated and is_unit
haftmann
parents:
60433
diff
changeset

245 
then show "a dvd b" .. 
58023  246 
qed 
247 

60436
77e694c0c919
simplified relationship between associated and is_unit
haftmann
parents:
60433
diff
changeset

248 
lemma associated_is_unitE: 
77e694c0c919
simplified relationship between associated and is_unit
haftmann
parents:
60433
diff
changeset

249 
assumes "associated a b" 
77e694c0c919
simplified relationship between associated and is_unit
haftmann
parents:
60433
diff
changeset

250 
obtains c where "is_unit c" and "a = c * b" 
77e694c0c919
simplified relationship between associated and is_unit
haftmann
parents:
60433
diff
changeset

251 
proof (cases "b = 0") 
77e694c0c919
simplified relationship between associated and is_unit
haftmann
parents:
60433
diff
changeset

252 
case True with assms have "is_unit 1" and "a = 1 * b" by simp_all 
77e694c0c919
simplified relationship between associated and is_unit
haftmann
parents:
60433
diff
changeset

253 
with that show thesis . 
77e694c0c919
simplified relationship between associated and is_unit
haftmann
parents:
60433
diff
changeset

254 
next 
77e694c0c919
simplified relationship between associated and is_unit
haftmann
parents:
60433
diff
changeset

255 
case False 
77e694c0c919
simplified relationship between associated and is_unit
haftmann
parents:
60433
diff
changeset

256 
from assms have "a dvd b" and "b dvd a" by (auto dest: associatedD1 associatedD2) 
77e694c0c919
simplified relationship between associated and is_unit
haftmann
parents:
60433
diff
changeset

257 
then obtain c d where "b = a * d" and "a = b * c" by (blast elim: dvdE) 
77e694c0c919
simplified relationship between associated and is_unit
haftmann
parents:
60433
diff
changeset

258 
then have "a = c * b" and "(c * d) * b = 1 * b" by (simp_all add: ac_simps) 
77e694c0c919
simplified relationship between associated and is_unit
haftmann
parents:
60433
diff
changeset

259 
with False have "c * d = 1" using mult_cancel_right [of "c * d" b 1] by simp 
77e694c0c919
simplified relationship between associated and is_unit
haftmann
parents:
60433
diff
changeset

260 
then have "is_unit c" by auto 
77e694c0c919
simplified relationship between associated and is_unit
haftmann
parents:
60433
diff
changeset

261 
with `a = c * b` that show thesis by blast 
77e694c0c919
simplified relationship between associated and is_unit
haftmann
parents:
60433
diff
changeset

262 
qed 
77e694c0c919
simplified relationship between associated and is_unit
haftmann
parents:
60433
diff
changeset

263 

58023  264 
lemmas unit_simps = mult_unit_dvd_iff div_unit_dvd_iff dvd_mult_unit_iff 
265 
dvd_div_unit_iff unit_div_mult_swap unit_div_commute 

266 
unit_mult_left_cancel unit_mult_right_cancel unit_div_cancel 

267 
unit_eq_div1 unit_eq_div2 

268 

269 
end 

270 

271 
lemma is_unit_int: 

60433  272 
"is_unit (k::int) \<longleftrightarrow> k = 1 \<or> k =  1" 
59061  273 
by auto 
58023  274 

60433  275 

58023  276 
text {* 
277 
A Euclidean semiring is a semiring upon which the Euclidean algorithm can be 

278 
implemented. It must provide: 

279 
\begin{itemize} 

280 
\item division with remainder 

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

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

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

60433  284 
they are the same when divd by their normalisation factors. 
58023  285 
\end{itemize} 
286 
The existence of these functions makes it possible to derive gcd and lcm functions 

287 
for any Euclidean semiring. 

288 
*} 

289 
class euclidean_semiring = semiring_div + 

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

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

292 
assumes mod_size_less [simp]: 

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

294 
assumes size_mult_mono: 

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

296 
assumes normalisation_factor_is_unit [intro,simp]: 

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

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

299 
normalisation_factor a * normalisation_factor b" 

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

300 
assumes normalisation_factor_unit: "is_unit a \<Longrightarrow> normalisation_factor a = a" 
58023  301 
assumes normalisation_factor_0 [simp]: "normalisation_factor 0 = 0" 
302 
begin 

303 

304 
lemma normalisation_factor_dvd [simp]: 

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

306 
by (rule unit_imp_dvd, simp) 

307 

308 
lemma normalisation_factor_1 [simp]: 

309 
"normalisation_factor 1 = 1" 

310 
by (simp add: normalisation_factor_unit) 

311 

312 
lemma normalisation_factor_0_iff [simp]: 

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

313 
"normalisation_factor a = 0 \<longleftrightarrow> a = 0" 
58023  314 
proof 
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

315 
assume "normalisation_factor a = 0" 
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

316 
hence "\<not> is_unit (normalisation_factor a)" 
60433  317 
by simp 
318 
then show "a = 0" by auto 

319 
qed simp 

58023  320 

321 
lemma normalisation_factor_pow: 

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

322 
"normalisation_factor (a ^ n) = normalisation_factor a ^ n" 
58023  323 
by (induct n) (simp_all add: normalisation_factor_mult power_Suc2) 
324 

325 
lemma normalisation_correct [simp]: 

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

326 
"normalisation_factor (a div normalisation_factor a) = (if a = 0 then 0 else 1)" 
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

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

328 
assume "a \<noteq> 0" 
58023  329 
let ?nf = "normalisation_factor" 
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

330 
from normalisation_factor_is_unit[OF `a \<noteq> 0`] have "?nf a \<noteq> 0" 
60433  331 
by auto 
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

332 
have "?nf (a div ?nf a) * ?nf (?nf a) = ?nf (a div ?nf a * ?nf a)" 
58023  333 
by (simp add: normalisation_factor_mult) 
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

334 
also have "a div ?nf a * ?nf a = a" using `a \<noteq> 0` 
59009
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset

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

336 
also have "?nf (?nf a) = ?nf a" using `a \<noteq> 0` 
58023  337 
normalisation_factor_is_unit normalisation_factor_unit by simp 
60433  338 
finally have "normalisation_factor (a div normalisation_factor a) = 1" 
339 
using `?nf a \<noteq> 0` by (metis div_mult_self2_is_id div_self) 

340 
with `a \<noteq> 0` show ?thesis by simp 

58023  341 
qed 
342 

343 
lemma normalisation_0_iff [simp]: 

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

344 
"a div normalisation_factor a = 0 \<longleftrightarrow> a = 0" 
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

345 
by (cases "a = 0", simp, subst unit_eq_div1, blast, simp) 
58023  346 

60433  347 
lemma mult_div_normalisation [simp]: 
348 
"b * (1 div normalisation_factor a) = b div normalisation_factor a" 

349 
by (cases "a = 0") simp_all 

350 

58023  351 
lemma associated_iff_normed_eq: 
352 
"associated a b \<longleftrightarrow> a div normalisation_factor a = b div normalisation_factor b" 

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

354 
let ?nf = normalisation_factor 

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

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

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

358 
apply (subst div_mult_swap, simp, simp) 

359 
done 

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

360 
with `a \<noteq> 0` `b \<noteq> 0` have "\<exists>c. is_unit c \<and> a = c * b" 
58023  361 
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

362 
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

363 
then show "associated a b" by (rule is_unit_associatedI) 
58023  364 
next 
365 
let ?nf = normalisation_factor 

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

60436
77e694c0c919
simplified relationship between associated and is_unit
haftmann
parents:
60433
diff
changeset

367 
then obtain c where "is_unit c" and "a = c * b" by (blast elim: associated_is_unitE) 
58023  368 
then show "a div ?nf a = b div ?nf b" 
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

369 
apply (simp only: `a = c * b` normalisation_factor_mult normalisation_factor_unit) 
58023  370 
apply (rule div_mult_mult1, force) 
371 
done 

372 
qed 

373 

374 
lemma normed_associated_imp_eq: 

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

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

377 

378 
lemmas normalisation_factor_dvd_iff [simp] = 

379 
unit_dvd_iff [OF normalisation_factor_is_unit] 

380 

381 
lemma euclidean_division: 

382 
fixes a :: 'a and b :: 'a 

383 
assumes "b \<noteq> 0" 

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

385 
and "euclidean_size t < euclidean_size b" 

386 
proof  

387 
from div_mod_equality[of a b 0] 

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

389 
with that and assms show ?thesis by force 

390 
qed 

391 

392 
lemma dvd_euclidean_size_eq_imp_dvd: 

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

394 
shows "a dvd b" 

395 
proof (subst dvd_eq_mod_eq_0, rule ccontr) 

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

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

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

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

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

401 
using size_mult_mono by force 

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

403 
using mod_size_less by blast 

404 
ultimately show False using size_eq by simp 

405 
qed 

406 

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

408 
where 

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

410 
by (pat_completeness, simp) 

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

412 

413 
declare gcd_eucl.simps [simp del] 

414 

415 
lemma gcd_induct: "\<lbrakk>\<And>b. P b 0; \<And>a b. 0 \<noteq> b \<Longrightarrow> P b (a mod b) \<Longrightarrow> P a b\<rbrakk> \<Longrightarrow> P a b" 

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

417 
case ("1" m n) 

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

419 
qed 

420 

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

422 
where 

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

424 

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

426 
for infinite sets as well *) 

427 

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

429 
where 

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

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

431 
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

432 
(LEAST n. \<exists>l. l \<noteq> 0 \<and> (\<forall>a\<in>A. a dvd l) \<and> euclidean_size l = n) 
58023  433 
in l div normalisation_factor l 
434 
else 0)" 

435 

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

437 
where 

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

439 

440 
end 

441 

442 
class euclidean_semiring_gcd = euclidean_semiring + gcd + Gcd + 

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

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

445 
begin 

446 

447 
lemma gcd_red: 

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

448 
"gcd a b = gcd b (a mod b)" 
58023  449 
by (metis gcd_eucl.simps mod_0 mod_by_0 gcd_gcd_eucl) 
450 

451 
lemma gcd_non_0: 

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

452 
"b \<noteq> 0 \<Longrightarrow> gcd a b = gcd b (a mod b)" 
58023  453 
by (rule gcd_red) 
454 

455 
lemma gcd_0_left: 

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

456 
"gcd 0 a = a div normalisation_factor a" 
58023  457 
by (simp only: gcd_gcd_eucl, subst gcd_eucl.simps, subst gcd_eucl.simps, simp add: Let_def) 
458 

459 
lemma gcd_0: 

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

460 
"gcd a 0 = a div normalisation_factor a" 
58023  461 
by (simp only: gcd_gcd_eucl, subst gcd_eucl.simps, simp add: Let_def) 
462 

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

463 
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

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

465 
proof (induct a b rule: gcd_eucl.induct) 
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

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

467 
assume IH1: "b \<noteq> 0 \<Longrightarrow> gcd b (a mod b) dvd b" 
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

468 
assume IH2: "b \<noteq> 0 \<Longrightarrow> gcd b (a mod b) dvd (a mod b)" 
58023  469 

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

470 
have "gcd a b dvd a \<and> gcd a b dvd b" 
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

471 
proof (cases "b = 0") 
58023  472 
case True 
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

473 
then show ?thesis by (cases "a = 0", simp_all add: gcd_0) 
58023  474 
next 
475 
case False 

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

477 
qed 

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

478 
then show "gcd a b dvd a" "gcd a b dvd b" by simp_all 
58023  479 
qed 
480 

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

482 
by (rule dvd_trans, assumption, rule gcd_dvd1) 

483 

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

485 
by (rule dvd_trans, assumption, rule gcd_dvd2) 

486 

487 
lemma gcd_greatest: 

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

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

489 
shows "k dvd a \<Longrightarrow> k dvd b \<Longrightarrow> k dvd gcd a b" 
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

490 
proof (induct a b rule: gcd_eucl.induct) 
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

491 
case (1 a b) 
58023  492 
show ?case 
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

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

494 
assume "b = 0" 
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

495 
with 1 show ?thesis by (cases "a = 0", simp_all add: gcd_0) 
58023  496 
next 
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

497 
assume "b \<noteq> 0" 
58023  498 
with 1 show ?thesis by (simp add: gcd_non_0 dvd_mod_iff) 
499 
qed 

500 
qed 

501 

502 
lemma dvd_gcd_iff: 

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

503 
"k dvd gcd a b \<longleftrightarrow> k dvd a \<and> k dvd b" 
58023  504 
by (blast intro!: gcd_greatest intro: dvd_trans) 
505 

506 
lemmas gcd_greatest_iff = dvd_gcd_iff 

507 

508 
lemma gcd_zero [simp]: 

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

509 
"gcd a b = 0 \<longleftrightarrow> a = 0 \<and> b = 0" 
58023  510 
by (metis dvd_0_left dvd_refl gcd_dvd1 gcd_dvd2 gcd_greatest)+ 
511 

512 
lemma normalisation_factor_gcd [simp]: 

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

513 
"normalisation_factor (gcd a b) = (if a = 0 \<and> b = 0 then 0 else 1)" (is "?f a b = ?g a b") 
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

514 
proof (induct a b rule: gcd_eucl.induct) 
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

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

516 
assume IH: "b \<noteq> 0 \<Longrightarrow> ?f b (a mod b) = ?g b (a mod b)" 
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

517 
then show "?f a b = ?g a b" by (cases "b = 0", auto simp: gcd_non_0 gcd_0) 
58023  518 
qed 
519 

520 
lemma gcdI: 

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

521 
"k dvd a \<Longrightarrow> k dvd b \<Longrightarrow> (\<And>l. l dvd a \<Longrightarrow> l dvd b \<Longrightarrow> l dvd k) 
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

522 
\<Longrightarrow> normalisation_factor k = (if k = 0 then 0 else 1) \<Longrightarrow> k = gcd a b" 
58023  523 
by (intro normed_associated_imp_eq) (auto simp: associated_def intro: gcd_greatest) 
524 

525 
sublocale gcd!: abel_semigroup gcd 

526 
proof 

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

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

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

530 
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

531 
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

532 
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

533 
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

534 
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

535 
ultimately show "gcd (gcd a b) c dvd gcd b c" 
58023  536 
by (rule gcd_greatest) 
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

537 
show "normalisation_factor (gcd (gcd a b) c) = (if gcd (gcd a b) c = 0 then 0 else 1)" 
58023  538 
by auto 
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

539 
fix l assume "l dvd a" and "l dvd gcd b c" 
58023  540 
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

541 
have "l dvd b" and "l dvd c" by blast+ 
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

542 
with `l dvd a` show "l dvd gcd (gcd a b) c" 
58023  543 
by (intro gcd_greatest) 
544 
qed 

545 
next 

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

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

547 
show "gcd a b = gcd b a" 
58023  548 
by (rule gcdI) (simp_all add: gcd_greatest) 
549 
qed 

550 

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

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

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

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

555 

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

557 
using mult_dvd_mono [of 1] by auto 

558 

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

559 
lemma gcd_1_left [simp]: "gcd 1 a = 1" 
58023  560 
by (rule sym, rule gcdI, simp_all) 
561 

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

562 
lemma gcd_1 [simp]: "gcd a 1 = 1" 
58023  563 
by (rule sym, rule gcdI, simp_all) 
564 

565 
lemma gcd_proj2_if_dvd: 

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

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

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

569 
lemma gcd_proj1_if_dvd: 

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

570 
"a dvd b \<Longrightarrow> gcd a b = a div normalisation_factor a" 
58023  571 
by (subst gcd.commute, simp add: gcd_proj2_if_dvd) 
572 

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

574 
proof 

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

576 
show "m dvd n" 

577 
proof (cases "m = 0") 

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

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

580 
by (simp add: unit_eq_div2) 

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

582 
qed (insert A, simp) 

583 
next 

584 
assume "m dvd n" 

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

586 
qed 

587 

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

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

590 

591 
lemma gcd_mod1 [simp]: 

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

592 
"gcd (a mod b) b = gcd a b" 
58023  593 
by (rule gcdI, metis dvd_mod_iff gcd_dvd1 gcd_dvd2, simp_all add: gcd_greatest dvd_mod_iff) 
594 

595 
lemma gcd_mod2 [simp]: 

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

596 
"gcd a (b mod a) = gcd a b" 
58023  597 
by (rule gcdI, simp, metis dvd_mod_iff gcd_dvd1 gcd_dvd2, simp_all add: gcd_greatest dvd_mod_iff) 
598 

599 
lemma normalisation_factor_dvd' [simp]: 

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

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

601 
by (cases "a = 0", simp_all) 
58023  602 

603 
lemma gcd_mult_distrib': 

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

604 
"k div normalisation_factor k * gcd a b = gcd (k*a) (k*b)" 
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

605 
proof (induct a b rule: gcd_eucl.induct) 
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

606 
case (1 a b) 
58023  607 
show ?case 
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

608 
proof (cases "b = 0") 
58023  609 
case True 
610 
then show ?thesis by (simp add: normalisation_factor_mult gcd_0 algebra_simps div_mult_div_if_dvd) 

611 
next 

612 
case False 

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

613 
hence "k div normalisation_factor k * gcd a b = gcd (k * b) (k * (a mod b))" 
58023  614 
using 1 by (subst gcd_red, simp) 
60430
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

615 
also have "... = gcd (k * a) (k * b)" 
58023  616 
by (simp add: mult_mod_right gcd.commute) 
617 
finally show ?thesis . 

618 
qed 

619 
qed 

620 

621 
lemma gcd_mult_distrib: 

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

622 
"k * gcd a b = gcd (k*a) (k*b) * normalisation_factor k" 
58023  623 
proof 
624 
let ?nf = "normalisation_factor" 

625 
from gcd_mult_distrib' 

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

626 
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

627 
also have "... = k * gcd a b div ?nf k" 
58023  628 
by (metis dvd_div_mult dvd_eq_mod_eq_0 mod_0 normalisation_factor_dvd) 
629 
finally show ?thesis 

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

630 
by simp 
58023  631 
qed 
632 

633 
lemma euclidean_size_gcd_le1 [simp]: 

634 
assumes "a \<noteq> 0" 

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

636 
proof  

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

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

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

640 
qed 

641 

642 
lemma euclidean_size_gcd_le2 [simp]: 

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

644 
by (subst gcd.commute, rule euclidean_size_gcd_le1) 

645 

646 
lemma euclidean_size_gcd_less1: 

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

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

649 
proof (rule ccontr) 

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

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

652 
by (intro le_antisym, simp_all) 

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

654 
hence "a dvd b" using dvd_gcd_D2 by blast 

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

656 
qed 

657 

658 
lemma euclidean_size_gcd_less2: 

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

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

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

662 

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

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

666 
apply (rule gcd_dvd2) 

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

668 
apply (subst normalisation_factor_gcd, simp add: gcd_0) 

669 
done 

670 

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

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

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

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

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

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

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

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

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

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

683 
lemma gcd_right_idem: "gcd (gcd a b) b = gcd a b" 
58023  684 
apply (rule gcdI) 
685 
apply (simp add: ac_simps) 

686 
apply (rule gcd_dvd2) 

687 
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

688 
apply simp 
58023  689 
done 
690 

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

691 
lemma gcd_left_idem: "gcd a (gcd a b) = gcd a b" 
58023  692 
apply (rule gcdI) 
693 
apply simp 

694 
apply (rule dvd_trans, rule gcd_dvd2, rule gcd_dvd2) 

695 
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

696 
apply simp 
58023  697 
done 
698 

699 
lemma comp_fun_idem_gcd: "comp_fun_idem gcd" 

700 
proof 

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

702 
by (simp add: fun_eq_iff ac_simps) 

703 
next 

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

705 
by (simp add: fun_eq_iff gcd_left_idem) 

706 
qed 

707 

708 
lemma coprime_dvd_mult: 

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

709 
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

710 
shows "c dvd a" 
58023  711 
proof  
712 
let ?nf = "normalisation_factor" 

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

713 
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

714 
have A: "a = gcd (a * c) (a * b) * ?nf a" by simp 
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

715 
from `c dvd a * b` show ?thesis by (subst A, simp_all add: gcd_greatest) 
58023  716 
qed 
717 

718 
lemma coprime_dvd_mult_iff: 

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

719 
"gcd c b = 1 \<Longrightarrow> (c dvd a * b) = (c dvd a)" 
58023  720 
by (rule, rule coprime_dvd_mult, simp_all) 
721 

722 
lemma gcd_dvd_antisym: 

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

724 
proof (rule gcdI) 

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

726 
have "gcd c d dvd c" by simp 

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

728 
have "gcd c d dvd d" by simp 

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

730 
show "normalisation_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

731 
by simp 
58023  732 
fix l assume "l dvd c" and "l dvd d" 
733 
hence "l dvd gcd c d" by (rule gcd_greatest) 

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

735 
qed 

736 

737 
lemma gcd_mult_cancel: 

738 
assumes "gcd k n = 1" 

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

740 
proof (rule gcd_dvd_antisym) 

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

742 
also note `gcd k n = 1` 

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

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

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

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

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

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

749 
qed 

750 

751 
lemma coprime_crossproduct: 

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

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

754 
proof 

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

756 
next 

757 
assume ?lhs 

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

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

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

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

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

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

763 
unfolding associated_def by (auto dest: dvd_mult_right simp add: ac_simps) 
58023  764 
hence "c dvd d" by (simp add: coprime_dvd_mult_iff gcd.commute) 
765 
moreover from `?lhs` have "d dvd c * a" 

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

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

769 
qed 

770 

771 
lemma gcd_add1 [simp]: 

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

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

774 

775 
lemma gcd_add2 [simp]: 

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

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

778 

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

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

781 

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

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

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

59061  786 
by (auto intro: coprimeI gcd_greatest dvd_gcd_D1 dvd_gcd_D2) 
58023  787 

788 
lemma div_gcd_coprime: 

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

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

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

792 
shows "gcd a' b' = 1" 

793 
proof (rule coprimeI) 

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

795 
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

796 
moreover have "a = a' * d" "b = b' * d" by simp_all 
58023  797 
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

798 
by (simp_all only: ac_simps) 
58023  799 
hence "l*d dvd a" and "l*d dvd b" by (simp_all only: dvd_triv_left) 
800 
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

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

802 
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

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

804 
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

805 
ultimately have "1 = l * u" 
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset

806 
using `d \<noteq> 0` by simp 
348561aa3869
generalized lemmas (particularly concerning dvd) as far as appropriate
haftmann
parents:
58953
diff
changeset

807 
then show "l dvd 1" .. 
58023  808 
qed 
809 

810 
lemma coprime_mult: 

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

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

813 
apply (subst gcd.commute) 

814 
using da apply (subst gcd_mult_cancel) 

815 
apply (subst gcd.commute, assumption) 

816 
apply (subst gcd.commute, rule db) 

817 
done 

818 

819 
lemma coprime_lmult: 

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

821 
shows "gcd d a = 1" 

822 
proof (rule coprimeI) 

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

824 
hence "l dvd a * b" by simp 

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

826 
qed 

827 

828 
lemma coprime_rmult: 

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

830 
shows "gcd d b = 1" 

831 
proof (rule coprimeI) 

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

833 
hence "l dvd a * b" by simp 

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

835 
qed 

836 

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

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

839 

840 
lemma gcd_coprime: 

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

841 
assumes c: "gcd a b \<noteq> 0" and a: "a = a' * gcd a b" and b: "b = b' * gcd a b" 
58023  842 
shows "gcd a' b' = 1" 
843 
proof  

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

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

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

848 
finally show ?thesis . 

849 
qed 

850 

851 
lemma coprime_power: 

852 
assumes "0 < n" 

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

854 
using assms proof (induct n) 

855 
case (Suc n) then show ?case 

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

857 
qed simp 

858 

859 
lemma gcd_coprime_exists: 

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

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

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

863 
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

864 
apply (insert nz, auto intro: div_gcd_coprime) 
58023  865 
done 
866 

867 
lemma coprime_exp: 

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

869 
by (induct n, simp_all add: coprime_mult) 

870 

871 
lemma coprime_exp2 [intro]: 

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

873 
apply (rule coprime_exp) 

874 
apply (subst gcd.commute) 

875 
apply (rule coprime_exp) 

876 
apply (subst gcd.commute) 

877 
apply assumption 

878 
done 

879 

880 
lemma gcd_exp: 

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

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

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

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

885 
next 

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

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

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

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

890 
also note gcd_mult_distrib 

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

892 
by (simp add: normalisation_factor_pow A) 

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

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

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

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

897 
finally show ?thesis by simp 

898 
qed 

899 

900 
lemma coprime_common_divisor: 

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

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

902 
apply (subgoal_tac "a dvd gcd a b") 
59061  903 
apply simp 
58023  904 
apply (erule (1) gcd_greatest) 
905 
done 

906 

907 
lemma division_decomp: 

908 
assumes dc: "a dvd b * c" 

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

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

911 
assume "gcd a b = 0" 

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

912 
hence "a = 0 \<and> b = 0" by simp 
58023  913 
hence "a = 0 * c \<and> 0 dvd b \<and> c dvd c" by simp 
914 
then show ?thesis by blast 

915 
next 

916 
let ?d = "gcd a b" 

917 
assume "?d \<noteq> 0" 

918 
from gcd_coprime_exists[OF this] 

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

920 
by blast 

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

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

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

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

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

925 
with `?d \<noteq> 0` have "a' dvd b' * c" by simp 
58023  926 
with coprime_dvd_mult[OF ab'(3)] 
927 
have "a' dvd c" by (subst (asm) ac_simps, blast) 

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

929 
then show ?thesis by blast 

930 
qed 

931 

60433  932 
lemma pow_divs_pow: 
58023  933 
assumes ab: "a ^ n dvd b ^ n" and n: "n \<noteq> 0" 
934 
shows "a dvd b" 

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

936 
assume "gcd a b = 0" 

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

937 
then show ?thesis by simp 
58023  938 
next 
939 
let ?d = "gcd a b" 

940 
assume "?d \<noteq> 0" 

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

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

942 
from `?d \<noteq> 0` have zn: "?d ^ n \<noteq> 0" by (rule power_not_zero) 
58023  943 
from gcd_coprime_exists[OF `?d \<noteq> 0`] 
944 
obtain a' b' where ab': "a = a' * ?d" "b = b' * ?d" "gcd a' b' = 1" 

945 
by blast 

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

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

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

949 
by (simp only: power_mult_distrib ac_simps) 

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

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

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

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

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

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

957 
qed 

958 

60433  959 
lemma pow_divs_eq [simp]: 
58023  960 
"n \<noteq> 0 \<Longrightarrow> a ^ n dvd b ^ n \<longleftrightarrow> a dvd b" 
60433  961 
by (auto intro: pow_divs_pow dvd_power_same) 
58023  962 

60433  963 
lemma divs_mult: 
58023  964 
assumes mr: "m dvd r" and nr: "n dvd r" and mn: "gcd m n = 1" 
965 
shows "m * n dvd r" 

966 
proof  

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

968 
unfolding dvd_def by blast 

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

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

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

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

973 
then show ?thesis unfolding dvd_def by blast 

974 
qed 

975 

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

977 
by (subst add_commute, simp) 

978 

979 
lemma setprod_coprime [rule_format]: 

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

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

983 
apply (auto simp add: gcd_mult_cancel) 

984 
done 

985 

986 
lemma coprime_divisors: 

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

988 
shows "gcd d e = 1" 

989 
proof  

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

991 
unfolding dvd_def by blast 

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

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

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

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

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

997 
qed 

998 

999 
lemma invertible_coprime: 

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

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

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

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

1003 
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

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

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

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

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

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

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

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

1011 
qed 
58023  1012 

1013 
lemma lcm_gcd: 

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

1015 
by (simp only: lcm_lcm_eucl gcd_gcd_eucl lcm_eucl_def) 

1016 

1017 
lemma lcm_gcd_prod: 

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

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

1020 
let ?nf = normalisation_factor 

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

58953  1022 
hence "gcd a b \<noteq> 0" by simp 
58023  1023 
from lcm_gcd have "lcm a b * gcd a b = gcd a b * (a * b div (?nf (a*b) * gcd a b))" 
1024 
by (simp add: mult_ac) 

60432  1025 
also from `a * b \<noteq> 0` have "... = a * b div ?nf (a*b)" 
1026 
by (simp add: div_mult_swap mult.commute) 

58023  1027 
finally show ?thesis . 
58953  1028 
qed (auto simp add: lcm_gcd) 
58023  1029 

1030 
lemma lcm_dvd1 [iff]: 

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

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

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

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

1034 
hence "gcd a b \<noteq> 0" by simp 
60433  1035 
let ?c = "1 div normalisation_factor (a * b)" 
1036 
from `a * b \<noteq> 0` have [simp]: "is_unit (normalisation_factor (a * b))" by simp 

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

1037 
from lcm_gcd_prod[of a b] have "lcm a b * gcd a b = a * ?c * b" 
60432  1038 
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

1039 
hence "lcm a b * gcd a b div gcd a b = a * ?c * b div gcd a b" by simp 
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

1040 
with `gcd a b \<noteq> 0` have "lcm a b = a * ?c * b div gcd a b" 
58023  1041 
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

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

58953  1045 
qed (auto simp add: lcm_gcd) 
58023  1046 

1047 
lemma lcm_least: 

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

1049 
proof (cases "k = 0") 

1050 
let ?nf = normalisation_factor 

1051 
assume "k \<noteq> 0" 

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

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

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

58953  1055 
hence "gcd a b \<noteq> 0" using `k \<noteq> 0` by auto 
58023  1056 
from A obtain r s where ar: "k = a * r" and bs: "k = b * s" 
1057 
unfolding dvd_def by blast 

58953  1058 
with `k \<noteq> 0` have "r * s \<noteq> 0" 
1059 
by auto (drule sym [of 0], simp) 

58023  1060 
hence "is_unit (?nf (r * s))" by simp 
1061 
let ?c = "?nf k div ?nf (r*s)" 

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

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

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

58953  1065 
by (subst mult_assoc, subst gcd_mult_distrib[of k s r], simp only: ac_simps) 
58023  1066 
also have "... = ?nf k * k * gcd ((r*s) * a) ((r*s) * b)" 
1067 
by (subst (3) `k = a * r`, subst (3) `k = b * s`, simp add: algebra_simps) 

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

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

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

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

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

1073 
by (simp add: algebra_simps) 

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

1075 
by (metis div_mult_self2_is_id) 

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

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

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

1079 
by (simp add: algebra_simps) 

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

1081 
by (metis mult.commute div_mult_self2_is_id) 

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

1083 
by (metis div_mult_self2_is_id mult_assoc) 

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

1085 
by (simp add: unit_simps) 

1086 
finally show ?thesis by (rule dvdI) 

1087 
qed simp 

1088 

1089 
lemma lcm_zero: 

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

1091 
proof  

1092 
let ?nf = normalisation_factor 

1093 
{ 

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

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

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

1096 
moreover from `a \<noteq> 0` and `b \<noteq> 0` have "gcd a b \<noteq> 0" by simp 
58023  1097 
ultimately have "lcm a b \<noteq> 0" using lcm_gcd_prod[of a b] by (intro notI, simp) 
1098 
} moreover { 

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

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

1101 
} 

1102 
ultimately show ?thesis by blast 

1103 
qed 

1104 

1105 
lemmas lcm_0_iff = lcm_zero 

1106 

1107 
lemma gcd_lcm: 

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

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

1110 
proof 

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

1111 
from assms have "gcd a b \<noteq> 0" by (simp add: lcm_zero) 
60433  1112 
let ?c = "normalisation_factor (a * b)" 
58023  1113 
from `lcm a b \<noteq> 0` have "?c \<noteq> 0" by (intro notI, simp add: lcm_zero no_zero_divisors) 
1114 
hence "is_unit ?c" by simp 

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

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

60433  1117 
also from `is_unit ?c` have "... = a * b div (lcm a b * ?c)" 
1118 
by (metis `?c \<noteq> 0` div_mult_mult1 dvd_mult_div_cancel mult_commute normalisation_factor_dvd') 

1119 
finally show ?thesis . 

58023  1120 
qed 
1121 

1122 
lemma normalisation_factor_lcm [simp]: 

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

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

1125 
case True then show ?thesis 

58953  1126 
by (auto simp add: lcm_gcd) 
58023  1127 
next 
1128 
case False 

1129 
let ?nf = normalisation_factor 

1130 
from lcm_gcd_prod[of a b] 

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

1132 
by (metis div_by_0 div_self normalisation_correct normalisation_factor_0 normalisation_factor_mult) 

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

58953  1134 
by simp 
1135 
finally show ?thesis using False by simp 

58023  1136 
qed 
1137 

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

1138 
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

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

1141 
lemma lcmI: 

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

1142 
"\<lbrakk>a dvd k; b dvd k; \<And>l. a dvd l \<Longrightarrow> b dvd l \<Longrightarrow> k dvd l; 
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

1143 
normalisation_factor k = (if k = 0 then 0 else 1)\<rbrakk> \<Longrightarrow> k = lcm a b" 
58023  1144 
by (intro normed_associated_imp_eq) (auto simp: associated_def intro: lcm_least) 
1145 

1146 
sublocale lcm!: abel_semigroup lcm 

1147 
proof 

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

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

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

1151 
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

1152 
then show "a dvd lcm (lcm a b) c" by (rule dvd_trans) 
58023  1153 

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

1154 
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

1155 
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

1156 
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

1157 
ultimately show "lcm b c dvd lcm (lcm a b) c" by (rule lcm_least) 
58023  1158 

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

1159 
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

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

1161 
from this and `lcm b c dvd l` have "b dvd l" by (rule dvd_trans) 
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

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

1163 
from this and `lcm b c dvd l` have "c dvd l" by (rule dvd_trans) 
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

1164 
from `a dvd l` and `b dvd l` have "lcm a b dvd l" by (rule lcm_least) 
ce559c850a27
standardized algebraic conventions: prefer a, b, c over x, y, z
haftmann
parents:
59061
diff
changeset

1165 
from this and `c dvd l` show "lcm (lcm a b) c dvd l" by (rule lcm_least) 
58023  1166 
qed (simp add: lcm_zero) 
1167 
next 

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

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

1169 
show "lcm a b = lcm b a" 
58023  1170 
by (simp add: lcm_gcd ac_simps) 
1171 
qed 

1172 

1173 
lemma dvd_lcm_D1: 

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

1175 
by (rule dvd_trans, rule lcm_dvd1, assumption) 

1176 

1177 
lemma dvd_lcm_D2: 

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

1179 
by (rule dvd_trans, rule lcm_dvd2, assumption) 

1180 

1181 
lemma gcd_dvd_lcm [simp]: 

1182 
"gcd a b dvd lcm a b" 

1183 
by (metis dvd_trans gcd_dvd2 lcm_dvd2) 

1184 

1185 
lemma lcm_1_iff: 

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

1187 
proof 

1188 
assume "lcm a b = 1" 

59061  1189 
then show "is_unit a \<and> is_unit b" by auto 
58023  1190 
next 
1191 
assume "is_unit a \<and> is_unit b" 

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

58023  1194 
hence "lcm a b = normalisation_factor (lcm a b)" 
1195 
by (subst normalisation_factor_unit, simp_all) 

59061  1196 
also have "\<dots> = 1" using `is_unit a \<and> is_unit b` 
1197 
by auto 

58023  1198 
finally show "lcm a b = 1" . 
1199 
qed 

1200 

1201 
lemma lcm_0_left [simp]: 

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

1202 
"lcm 0 a = 0" 
58023  1203 
by (rule sym, rule lcmI, simp_all) 
1204 

1205 
lemma lcm_0 [simp]: 

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

1206 
"lcm a 0 = 0" 
58023  1207 
by (rule sym, rule lcmI, simp_all) 
1208 

1209 
lemma lcm_unique: 

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

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

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

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

1214 

1215 
lemma dvd_lcm_I1 [simp]: 

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

1217 
by (metis lcm_dvd1 dvd_trans) 

1218 

1219 
lemma dvd_lcm_I2 [simp]: 

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

1221 
by (metis lcm_dvd2 dvd_trans) 

1222 

1223 
lemma lcm_1_left [simp]: 

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

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

1225 
by (cases "a = 0") (simp, rule sym, rule lcmI, simp_all) 
58023  1226 

1227 
lemma lcm_1_right [simp]: 

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

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

1229 
using lcm_1_left [of a] by (simp add: ac_simps) 
58023  1230 

1231 
lemma lcm_coprime: 

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

1233 
by (subst lcm_gcd) simp 

1234 

1235 
lemma lcm_proj1_if_dvd: 

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

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

1237 
by (cases "a = 0") (simp, rule sym, rule lcmI, simp_all) 
58023  1238 

1239 
lemma lcm_proj2_if_dvd: 

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

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

1241 
using lcm_proj1_if_dvd [of a b] by (simp add: ac_simps) 
58023  1242 

1243 
lemma lcm_proj1_iff: 

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

1245 
proof 

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

1247 
show "n dvd m" 

1248 
proof (cases "m = 0") 

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

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

1251 
by (simp add: unit_eq_div2) 

1252 
show ?thesis by (subst B, simp) 

1253 
qed simp 

1254 
next 

1255 
assume "n dvd m" 

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

1257 
qed 

1258 

1259 
lemma lcm_proj2_iff: 

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

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

1262 

1263 
lemma euclidean_size_lcm_le1: 

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

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

1266 
proof  

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

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

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

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

1271 
qed 

1272 

1273 
lemma euclidean_size_lcm_le2: 

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

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

1276 

1277 
lemma euclidean_size_lcm_less1: 

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

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