haftmann@58023: (* Author: Manuel Eberl *) haftmann@58023: wenzelm@60526: section \Abstract euclidean algorithm\ haftmann@58023: haftmann@58023: theory Euclidean_Algorithm haftmann@60634: imports Complex_Main "~~/src/HOL/Library/Polynomial" "~~/src/HOL/Number_Theory/Normalization_Semidom" haftmann@60634: begin haftmann@60634: haftmann@60634: lemma is_unit_polyE: haftmann@60634: assumes "is_unit p" haftmann@60634: obtains a where "p = monom a 0" and "a \ 0" haftmann@60634: proof - haftmann@60634: obtain a q where "p = pCons a q" by (cases p) haftmann@60634: with assms have "p = [:a:]" and "a \ 0" haftmann@60634: by (simp_all add: is_unit_pCons_iff) haftmann@60634: with that show thesis by (simp add: monom_0) haftmann@60634: qed haftmann@60634: haftmann@60634: instantiation poly :: (field) normalization_semidom haftmann@58023: begin haftmann@60634: haftmann@60634: definition normalize_poly :: "'a poly \ 'a poly" haftmann@60634: where "normalize_poly p = smult (1 / coeff p (degree p)) p" haftmann@60634: haftmann@60634: definition unit_factor_poly :: "'a poly \ 'a poly" haftmann@60634: where "unit_factor_poly p = monom (coeff p (degree p)) 0" haftmann@60634: haftmann@60634: instance haftmann@60634: proof haftmann@60634: fix p :: "'a poly" haftmann@60634: show "unit_factor p * normalize p = p" haftmann@60634: by (simp add: normalize_poly_def unit_factor_poly_def) haftmann@60634: (simp only: mult_smult_left [symmetric] smult_monom, simp) haftmann@60634: next haftmann@60634: show "normalize 0 = (0::'a poly)" haftmann@60634: by (simp add: normalize_poly_def) haftmann@60634: next haftmann@60634: show "unit_factor 0 = (0::'a poly)" haftmann@60634: by (simp add: unit_factor_poly_def) haftmann@60634: next haftmann@60634: fix p :: "'a poly" haftmann@60634: assume "is_unit p" haftmann@60634: then obtain a where "p = monom a 0" and "a \ 0" haftmann@60634: by (rule is_unit_polyE) haftmann@60634: then show "normalize p = 1" haftmann@60634: by (auto simp add: normalize_poly_def smult_monom degree_monom_eq) haftmann@60634: next haftmann@60634: fix p q :: "'a poly" haftmann@60634: assume "q \ 0" haftmann@60634: from \q \ 0\ have "is_unit (monom (coeff q (degree q)) 0)" haftmann@60634: by (auto intro: is_unit_monom_0) haftmann@60634: then show "is_unit (unit_factor q)" haftmann@60634: by (simp add: unit_factor_poly_def) haftmann@60634: next haftmann@60634: fix p q :: "'a poly" haftmann@60634: have "monom (coeff (p * q) (degree (p * q))) 0 = haftmann@60634: monom (coeff p (degree p)) 0 * monom (coeff q (degree q)) 0" haftmann@60634: by (simp add: monom_0 coeff_degree_mult) haftmann@60634: then show "unit_factor (p * q) = haftmann@60634: unit_factor p * unit_factor q" haftmann@60634: by (simp add: unit_factor_poly_def) haftmann@60634: qed haftmann@60634: haftmann@60634: end haftmann@60634: wenzelm@60526: text \ haftmann@58023: A Euclidean semiring is a semiring upon which the Euclidean algorithm can be haftmann@58023: implemented. It must provide: haftmann@58023: \begin{itemize} haftmann@58023: \item division with remainder haftmann@58023: \item a size function such that @{term "size (a mod b) < size b"} haftmann@58023: for any @{term "b \ 0"} haftmann@58023: \end{itemize} haftmann@58023: The existence of these functions makes it possible to derive gcd and lcm functions haftmann@58023: for any Euclidean semiring. wenzelm@60526: \ haftmann@60634: class euclidean_semiring = semiring_div + normalization_semidom + haftmann@58023: fixes euclidean_size :: "'a \ nat" haftmann@60569: assumes mod_size_less: haftmann@60600: "b \ 0 \ euclidean_size (a mod b) < euclidean_size b" haftmann@58023: assumes size_mult_mono: haftmann@60634: "b \ 0 \ euclidean_size a \ euclidean_size (a * b)" haftmann@58023: begin haftmann@58023: haftmann@58023: lemma euclidean_division: haftmann@58023: fixes a :: 'a and b :: 'a haftmann@60600: assumes "b \ 0" haftmann@58023: obtains s and t where "a = s * b + t" haftmann@58023: and "euclidean_size t < euclidean_size b" haftmann@58023: proof - haftmann@60569: from div_mod_equality [of a b 0] haftmann@58023: have "a = a div b * b + a mod b" by simp haftmann@60569: with that and assms show ?thesis by (auto simp add: mod_size_less) haftmann@58023: qed haftmann@58023: haftmann@58023: lemma dvd_euclidean_size_eq_imp_dvd: haftmann@58023: assumes "a \ 0" and b_dvd_a: "b dvd a" and size_eq: "euclidean_size a = euclidean_size b" haftmann@58023: shows "a dvd b" haftmann@60569: proof (rule ccontr) haftmann@60569: assume "\ a dvd b" haftmann@60569: then have "b mod a \ 0" by (simp add: mod_eq_0_iff_dvd) haftmann@58023: from b_dvd_a have b_dvd_mod: "b dvd b mod a" by (simp add: dvd_mod_iff) haftmann@58023: from b_dvd_mod obtain c where "b mod a = b * c" unfolding dvd_def by blast wenzelm@60526: with \b mod a \ 0\ have "c \ 0" by auto wenzelm@60526: with \b mod a = b * c\ have "euclidean_size (b mod a) \ euclidean_size b" haftmann@58023: using size_mult_mono by force haftmann@60569: moreover from \\ a dvd b\ and \a \ 0\ haftmann@60569: have "euclidean_size (b mod a) < euclidean_size a" haftmann@58023: using mod_size_less by blast haftmann@58023: ultimately show False using size_eq by simp haftmann@58023: qed haftmann@58023: haftmann@58023: function gcd_eucl :: "'a \ 'a \ 'a" haftmann@58023: where haftmann@60634: "gcd_eucl a b = (if b = 0 then normalize a else gcd_eucl b (a mod b))" haftmann@60572: by pat_completeness simp haftmann@60569: termination haftmann@60569: by (relation "measure (euclidean_size \ snd)") (simp_all add: mod_size_less) haftmann@58023: haftmann@58023: declare gcd_eucl.simps [simp del] haftmann@58023: haftmann@60569: lemma gcd_eucl_induct [case_names zero mod]: haftmann@60569: assumes H1: "\b. P b 0" haftmann@60569: and H2: "\a b. b \ 0 \ P b (a mod b) \ P a b" haftmann@60569: shows "P a b" haftmann@58023: proof (induct a b rule: gcd_eucl.induct) haftmann@60569: case ("1" a b) haftmann@60569: show ?case haftmann@60569: proof (cases "b = 0") haftmann@60569: case True then show "P a b" by simp (rule H1) haftmann@60569: next haftmann@60569: case False haftmann@60600: then have "P b (a mod b)" haftmann@60600: by (rule "1.hyps") haftmann@60569: with \b \ 0\ show "P a b" haftmann@60569: by (blast intro: H2) haftmann@60569: qed haftmann@58023: qed haftmann@58023: haftmann@58023: definition lcm_eucl :: "'a \ 'a \ 'a" haftmann@58023: where haftmann@60634: "lcm_eucl a b = normalize (a * b) div gcd_eucl a b" haftmann@58023: haftmann@60572: definition Lcm_eucl :: "'a set \ 'a" -- \ haftmann@60572: Somewhat complicated definition of Lcm that has the advantage of working haftmann@60572: for infinite sets as well\ haftmann@58023: where haftmann@60430: "Lcm_eucl A = (if \l. l \ 0 \ (\a\A. a dvd l) then haftmann@60430: let l = SOME l. l \ 0 \ (\a\A. a dvd l) \ euclidean_size l = haftmann@60430: (LEAST n. \l. l \ 0 \ (\a\A. a dvd l) \ euclidean_size l = n) haftmann@60634: in normalize l haftmann@58023: else 0)" haftmann@58023: haftmann@58023: definition Gcd_eucl :: "'a set \ 'a" haftmann@58023: where haftmann@58023: "Gcd_eucl A = Lcm_eucl {d. \a\A. d dvd a}" haftmann@58023: haftmann@60572: lemma gcd_eucl_0: haftmann@60634: "gcd_eucl a 0 = normalize a" haftmann@60572: by (simp add: gcd_eucl.simps [of a 0]) haftmann@60572: haftmann@60572: lemma gcd_eucl_0_left: haftmann@60634: "gcd_eucl 0 a = normalize a" haftmann@60600: by (simp_all add: gcd_eucl_0 gcd_eucl.simps [of 0 a]) haftmann@60572: haftmann@60572: lemma gcd_eucl_non_0: haftmann@60572: "b \ 0 \ gcd_eucl a b = gcd_eucl b (a mod b)" haftmann@60600: by (simp add: gcd_eucl.simps [of a b] gcd_eucl.simps [of b 0]) haftmann@60572: haftmann@58023: end haftmann@58023: haftmann@60598: class euclidean_ring = euclidean_semiring + idom haftmann@60598: begin haftmann@60598: haftmann@60598: function euclid_ext :: "'a \ 'a \ 'a \ 'a \ 'a" where haftmann@60598: "euclid_ext a b = haftmann@60598: (if b = 0 then haftmann@60634: (1 div unit_factor a, 0, normalize a) haftmann@60598: else haftmann@60598: case euclid_ext b (a mod b) of haftmann@60598: (s, t, c) \ (t, s - t * (a div b), c))" haftmann@60598: by pat_completeness simp haftmann@60598: termination haftmann@60598: by (relation "measure (euclidean_size \ snd)") (simp_all add: mod_size_less) haftmann@60598: haftmann@60598: declare euclid_ext.simps [simp del] haftmann@60598: haftmann@60598: lemma euclid_ext_0: haftmann@60634: "euclid_ext a 0 = (1 div unit_factor a, 0, normalize a)" haftmann@60598: by (simp add: euclid_ext.simps [of a 0]) haftmann@60598: haftmann@60598: lemma euclid_ext_left_0: haftmann@60634: "euclid_ext 0 a = (0, 1 div unit_factor a, normalize a)" haftmann@60600: by (simp add: euclid_ext_0 euclid_ext.simps [of 0 a]) haftmann@60598: haftmann@60598: lemma euclid_ext_non_0: haftmann@60598: "b \ 0 \ euclid_ext a b = (case euclid_ext b (a mod b) of haftmann@60598: (s, t, c) \ (t, s - t * (a div b), c))" haftmann@60600: by (simp add: euclid_ext.simps [of a b] euclid_ext.simps [of b 0]) haftmann@60598: haftmann@60598: lemma euclid_ext_code [code]: haftmann@60634: "euclid_ext a b = (if b = 0 then (1 div unit_factor a, 0, normalize a) haftmann@60598: else let (s, t, c) = euclid_ext b (a mod b) in (t, s - t * (a div b), c))" haftmann@60598: by (simp add: euclid_ext.simps [of a b] euclid_ext.simps [of b 0]) haftmann@60598: haftmann@60598: lemma euclid_ext_correct: haftmann@60598: "case euclid_ext a b of (s, t, c) \ s * a + t * b = c" haftmann@60598: proof (induct a b rule: gcd_eucl_induct) haftmann@60598: case (zero a) then show ?case haftmann@60598: by (simp add: euclid_ext_0 ac_simps) haftmann@60598: next haftmann@60598: case (mod a b) haftmann@60598: obtain s t c where stc: "euclid_ext b (a mod b) = (s,t,c)" haftmann@60598: by (cases "euclid_ext b (a mod b)") blast haftmann@60598: with mod have "c = s * b + t * (a mod b)" by simp haftmann@60598: also have "... = t * ((a div b) * b + a mod b) + (s - t * (a div b)) * b" haftmann@60598: by (simp add: algebra_simps) haftmann@60598: also have "(a div b) * b + a mod b = a" using mod_div_equality . haftmann@60598: finally show ?case haftmann@60598: by (subst euclid_ext.simps) (simp add: stc mod ac_simps) haftmann@60598: qed haftmann@60598: haftmann@60598: definition euclid_ext' :: "'a \ 'a \ 'a \ 'a" haftmann@60598: where haftmann@60598: "euclid_ext' a b = (case euclid_ext a b of (s, t, _) \ (s, t))" haftmann@60598: haftmann@60634: lemma euclid_ext'_0: "euclid_ext' a 0 = (1 div unit_factor a, 0)" haftmann@60598: by (simp add: euclid_ext'_def euclid_ext_0) haftmann@60598: haftmann@60634: lemma euclid_ext'_left_0: "euclid_ext' 0 a = (0, 1 div unit_factor a)" haftmann@60598: by (simp add: euclid_ext'_def euclid_ext_left_0) haftmann@60598: haftmann@60598: lemma euclid_ext'_non_0: "b \ 0 \ euclid_ext' a b = (snd (euclid_ext' b (a mod b)), haftmann@60598: fst (euclid_ext' b (a mod b)) - snd (euclid_ext' b (a mod b)) * (a div b))" haftmann@60598: by (simp add: euclid_ext'_def euclid_ext_non_0 split_def) haftmann@60598: haftmann@60598: end haftmann@60598: haftmann@58023: class euclidean_semiring_gcd = euclidean_semiring + gcd + Gcd + haftmann@58023: assumes gcd_gcd_eucl: "gcd = gcd_eucl" and lcm_lcm_eucl: "lcm = lcm_eucl" haftmann@58023: assumes Gcd_Gcd_eucl: "Gcd = Gcd_eucl" and Lcm_Lcm_eucl: "Lcm = Lcm_eucl" haftmann@58023: begin haftmann@58023: haftmann@58023: lemma gcd_0_left: haftmann@60634: "gcd 0 a = normalize a" haftmann@60572: unfolding gcd_gcd_eucl by (fact gcd_eucl_0_left) haftmann@58023: haftmann@58023: lemma gcd_0: haftmann@60634: "gcd a 0 = normalize a" haftmann@60572: unfolding gcd_gcd_eucl by (fact gcd_eucl_0) haftmann@58023: haftmann@58023: lemma gcd_non_0: haftmann@60430: "b \ 0 \ gcd a b = gcd b (a mod b)" haftmann@60572: unfolding gcd_gcd_eucl by (fact gcd_eucl_non_0) haftmann@58023: haftmann@60430: lemma gcd_dvd1 [iff]: "gcd a b dvd a" haftmann@60430: and gcd_dvd2 [iff]: "gcd a b dvd b" haftmann@60569: by (induct a b rule: gcd_eucl_induct) haftmann@60569: (simp_all add: gcd_0 gcd_non_0 dvd_mod_iff) haftmann@60569: haftmann@58023: lemma dvd_gcd_D1: "k dvd gcd m n \ k dvd m" haftmann@58023: by (rule dvd_trans, assumption, rule gcd_dvd1) haftmann@58023: haftmann@58023: lemma dvd_gcd_D2: "k dvd gcd m n \ k dvd n" haftmann@58023: by (rule dvd_trans, assumption, rule gcd_dvd2) haftmann@58023: haftmann@58023: lemma gcd_greatest: haftmann@60430: fixes k a b :: 'a haftmann@60430: shows "k dvd a \ k dvd b \ k dvd gcd a b" haftmann@60569: proof (induct a b rule: gcd_eucl_induct) haftmann@60569: case (zero a) from zero(1) show ?case by (rule dvd_trans) (simp add: gcd_0) haftmann@60569: next haftmann@60569: case (mod a b) haftmann@60569: then show ?case haftmann@60569: by (simp add: gcd_non_0 dvd_mod_iff) haftmann@58023: qed haftmann@58023: haftmann@58023: lemma dvd_gcd_iff: haftmann@60430: "k dvd gcd a b \ k dvd a \ k dvd b" haftmann@58023: by (blast intro!: gcd_greatest intro: dvd_trans) haftmann@58023: haftmann@58023: lemmas gcd_greatest_iff = dvd_gcd_iff haftmann@58023: haftmann@58023: lemma gcd_zero [simp]: haftmann@60430: "gcd a b = 0 \ a = 0 \ b = 0" haftmann@58023: by (metis dvd_0_left dvd_refl gcd_dvd1 gcd_dvd2 gcd_greatest)+ haftmann@58023: haftmann@60634: lemma unit_factor_gcd [simp]: haftmann@60634: "unit_factor (gcd a b) = (if a = 0 \ b = 0 then 0 else 1)" (is "?f a b = ?g a b") haftmann@60569: by (induct a b rule: gcd_eucl_induct) haftmann@60569: (auto simp add: gcd_0 gcd_non_0) haftmann@58023: haftmann@58023: lemma gcdI: haftmann@60634: assumes "c dvd a" and "c dvd b" and greatest: "\d. d dvd a \ d dvd b \ d dvd c" haftmann@60634: and "unit_factor c = (if c = 0 then 0 else 1)" haftmann@60634: shows "c = gcd a b" haftmann@60634: by (rule associated_eqI) (auto simp: assms associated_def intro: gcd_greatest) haftmann@58023: haftmann@58023: sublocale gcd!: abel_semigroup gcd haftmann@58023: proof haftmann@60430: fix a b c haftmann@60430: show "gcd (gcd a b) c = gcd a (gcd b c)" haftmann@58023: proof (rule gcdI) haftmann@60430: have "gcd (gcd a b) c dvd gcd a b" "gcd a b dvd a" by simp_all haftmann@60430: then show "gcd (gcd a b) c dvd a" by (rule dvd_trans) haftmann@60430: have "gcd (gcd a b) c dvd gcd a b" "gcd a b dvd b" by simp_all haftmann@60430: hence "gcd (gcd a b) c dvd b" by (rule dvd_trans) haftmann@60430: moreover have "gcd (gcd a b) c dvd c" by simp haftmann@60430: ultimately show "gcd (gcd a b) c dvd gcd b c" haftmann@58023: by (rule gcd_greatest) haftmann@60634: show "unit_factor (gcd (gcd a b) c) = (if gcd (gcd a b) c = 0 then 0 else 1)" haftmann@58023: by auto haftmann@60430: fix l assume "l dvd a" and "l dvd gcd b c" haftmann@58023: with dvd_trans[OF _ gcd_dvd1] and dvd_trans[OF _ gcd_dvd2] haftmann@60430: have "l dvd b" and "l dvd c" by blast+ wenzelm@60526: with \l dvd a\ show "l dvd gcd (gcd a b) c" haftmann@58023: by (intro gcd_greatest) haftmann@58023: qed haftmann@58023: next haftmann@60430: fix a b haftmann@60430: show "gcd a b = gcd b a" haftmann@58023: by (rule gcdI) (simp_all add: gcd_greatest) haftmann@58023: qed haftmann@58023: haftmann@58023: lemma gcd_unique: "d dvd a \ d dvd b \ haftmann@60634: unit_factor d = (if d = 0 then 0 else 1) \ haftmann@58023: (\e. e dvd a \ e dvd b \ e dvd d) \ d = gcd a b" haftmann@58023: by (rule, auto intro: gcdI simp: gcd_greatest) haftmann@58023: haftmann@58023: lemma gcd_dvd_prod: "gcd a b dvd k * b" haftmann@58023: using mult_dvd_mono [of 1] by auto haftmann@58023: haftmann@60430: lemma gcd_1_left [simp]: "gcd 1 a = 1" haftmann@58023: by (rule sym, rule gcdI, simp_all) haftmann@58023: haftmann@60430: lemma gcd_1 [simp]: "gcd a 1 = 1" haftmann@58023: by (rule sym, rule gcdI, simp_all) haftmann@58023: haftmann@58023: lemma gcd_proj2_if_dvd: haftmann@60634: "b dvd a \ gcd a b = normalize b" haftmann@60430: by (cases "b = 0", simp_all add: dvd_eq_mod_eq_0 gcd_non_0 gcd_0) haftmann@58023: haftmann@58023: lemma gcd_proj1_if_dvd: haftmann@60634: "a dvd b \ gcd a b = normalize a" haftmann@58023: by (subst gcd.commute, simp add: gcd_proj2_if_dvd) haftmann@58023: haftmann@60634: lemma gcd_proj1_iff: "gcd m n = normalize m \ m dvd n" haftmann@58023: proof haftmann@60634: assume A: "gcd m n = normalize m" haftmann@58023: show "m dvd n" haftmann@58023: proof (cases "m = 0") haftmann@58023: assume [simp]: "m \ 0" haftmann@60634: from A have B: "m = gcd m n * unit_factor m" haftmann@58023: by (simp add: unit_eq_div2) haftmann@58023: show ?thesis by (subst B, simp add: mult_unit_dvd_iff) haftmann@58023: qed (insert A, simp) haftmann@58023: next haftmann@58023: assume "m dvd n" haftmann@60634: then show "gcd m n = normalize m" by (rule gcd_proj1_if_dvd) haftmann@58023: qed haftmann@58023: haftmann@60634: lemma gcd_proj2_iff: "gcd m n = normalize n \ n dvd m" haftmann@60634: using gcd_proj1_iff [of n m] by (simp add: ac_simps) haftmann@58023: haftmann@58023: lemma gcd_mod1 [simp]: haftmann@60430: "gcd (a mod b) b = gcd a b" haftmann@58023: by (rule gcdI, metis dvd_mod_iff gcd_dvd1 gcd_dvd2, simp_all add: gcd_greatest dvd_mod_iff) haftmann@58023: haftmann@58023: lemma gcd_mod2 [simp]: haftmann@60430: "gcd a (b mod a) = gcd a b" haftmann@58023: by (rule gcdI, simp, metis dvd_mod_iff gcd_dvd1 gcd_dvd2, simp_all add: gcd_greatest dvd_mod_iff) haftmann@58023: haftmann@58023: lemma gcd_mult_distrib': haftmann@60634: "normalize c * gcd a b = gcd (c * a) (c * b)" haftmann@60569: proof (cases "c = 0") haftmann@60569: case True then show ?thesis by (simp_all add: gcd_0) haftmann@60569: next haftmann@60634: case False then have [simp]: "is_unit (unit_factor c)" by simp haftmann@60569: show ?thesis haftmann@60569: proof (induct a b rule: gcd_eucl_induct) haftmann@60569: case (zero a) show ?case haftmann@60569: proof (cases "a = 0") haftmann@60569: case True then show ?thesis by (simp add: gcd_0) haftmann@60569: next haftmann@60634: case False haftmann@60634: then show ?thesis by (simp add: gcd_0 normalize_mult) haftmann@60569: qed haftmann@60569: case (mod a b) haftmann@60569: then show ?case by (simp add: mult_mod_right gcd.commute) haftmann@58023: qed haftmann@58023: qed haftmann@58023: haftmann@58023: lemma gcd_mult_distrib: haftmann@60634: "k * gcd a b = gcd (k * a) (k * b) * unit_factor k" haftmann@58023: proof- haftmann@60634: have "normalize k * gcd a b = gcd (k * a) (k * b)" haftmann@60634: by (simp add: gcd_mult_distrib') haftmann@60634: then have "normalize k * gcd a b * unit_factor k = gcd (k * a) (k * b) * unit_factor k" haftmann@60634: by simp haftmann@60634: then have "normalize k * unit_factor k * gcd a b = gcd (k * a) (k * b) * unit_factor k" haftmann@60634: by (simp only: ac_simps) haftmann@60634: then show ?thesis haftmann@59009: by simp haftmann@58023: qed haftmann@58023: haftmann@58023: lemma euclidean_size_gcd_le1 [simp]: haftmann@58023: assumes "a \ 0" haftmann@58023: shows "euclidean_size (gcd a b) \ euclidean_size a" haftmann@58023: proof - haftmann@58023: have "gcd a b dvd a" by (rule gcd_dvd1) haftmann@58023: then obtain c where A: "a = gcd a b * c" unfolding dvd_def by blast wenzelm@60526: with \a \ 0\ show ?thesis by (subst (2) A, intro size_mult_mono) auto haftmann@58023: qed haftmann@58023: haftmann@58023: lemma euclidean_size_gcd_le2 [simp]: haftmann@58023: "b \ 0 \ euclidean_size (gcd a b) \ euclidean_size b" haftmann@58023: by (subst gcd.commute, rule euclidean_size_gcd_le1) haftmann@58023: haftmann@58023: lemma euclidean_size_gcd_less1: haftmann@58023: assumes "a \ 0" and "\a dvd b" haftmann@58023: shows "euclidean_size (gcd a b) < euclidean_size a" haftmann@58023: proof (rule ccontr) haftmann@58023: assume "\euclidean_size (gcd a b) < euclidean_size a" wenzelm@60526: with \a \ 0\ have "euclidean_size (gcd a b) = euclidean_size a" haftmann@58023: by (intro le_antisym, simp_all) haftmann@58023: with assms have "a dvd gcd a b" by (auto intro: dvd_euclidean_size_eq_imp_dvd) haftmann@58023: hence "a dvd b" using dvd_gcd_D2 by blast wenzelm@60526: with \\a dvd b\ show False by contradiction haftmann@58023: qed haftmann@58023: haftmann@58023: lemma euclidean_size_gcd_less2: haftmann@58023: assumes "b \ 0" and "\b dvd a" haftmann@58023: shows "euclidean_size (gcd a b) < euclidean_size b" haftmann@58023: using assms by (subst gcd.commute, rule euclidean_size_gcd_less1) haftmann@58023: haftmann@60430: lemma gcd_mult_unit1: "is_unit a \ gcd (b * a) c = gcd b c" haftmann@58023: apply (rule gcdI) haftmann@58023: apply (rule dvd_trans, rule gcd_dvd1, simp add: unit_simps) haftmann@58023: apply (rule gcd_dvd2) haftmann@58023: apply (rule gcd_greatest, simp add: unit_simps, assumption) haftmann@60634: apply (subst unit_factor_gcd, simp add: gcd_0) haftmann@58023: done haftmann@58023: haftmann@60430: lemma gcd_mult_unit2: "is_unit a \ gcd b (c * a) = gcd b c" haftmann@58023: by (subst gcd.commute, subst gcd_mult_unit1, assumption, rule gcd.commute) haftmann@58023: haftmann@60430: lemma gcd_div_unit1: "is_unit a \ gcd (b div a) c = gcd b c" haftmann@60433: by (erule is_unitE [of _ b]) (simp add: gcd_mult_unit1) haftmann@58023: haftmann@60430: lemma gcd_div_unit2: "is_unit a \ gcd b (c div a) = gcd b c" haftmann@60433: by (erule is_unitE [of _ c]) (simp add: gcd_mult_unit2) haftmann@58023: haftmann@60634: lemma normalize_gcd_left [simp]: haftmann@60634: "gcd (normalize a) b = gcd a b" haftmann@60634: proof (cases "a = 0") haftmann@60634: case True then show ?thesis haftmann@60634: by simp haftmann@60634: next haftmann@60634: case False then have "is_unit (unit_factor a)" haftmann@60634: by simp haftmann@60634: moreover have "normalize a = a div unit_factor a" haftmann@60634: by simp haftmann@60634: ultimately show ?thesis haftmann@60634: by (simp only: gcd_div_unit1) haftmann@60634: qed haftmann@60634: haftmann@60634: lemma normalize_gcd_right [simp]: haftmann@60634: "gcd a (normalize b) = gcd a b" haftmann@60634: using normalize_gcd_left [of b a] by (simp add: ac_simps) haftmann@60634: haftmann@60634: lemma gcd_idem: "gcd a a = normalize a" haftmann@60430: by (cases "a = 0") (simp add: gcd_0_left, rule sym, rule gcdI, simp_all) haftmann@58023: haftmann@60430: lemma gcd_right_idem: "gcd (gcd a b) b = gcd a b" haftmann@58023: apply (rule gcdI) haftmann@58023: apply (simp add: ac_simps) haftmann@58023: apply (rule gcd_dvd2) haftmann@58023: apply (rule gcd_greatest, erule (1) gcd_greatest, assumption) haftmann@59009: apply simp haftmann@58023: done haftmann@58023: haftmann@60430: lemma gcd_left_idem: "gcd a (gcd a b) = gcd a b" haftmann@58023: apply (rule gcdI) haftmann@58023: apply simp haftmann@58023: apply (rule dvd_trans, rule gcd_dvd2, rule gcd_dvd2) haftmann@58023: apply (rule gcd_greatest, assumption, erule gcd_greatest, assumption) haftmann@59009: apply simp haftmann@58023: done haftmann@58023: haftmann@58023: lemma comp_fun_idem_gcd: "comp_fun_idem gcd" haftmann@58023: proof haftmann@58023: fix a b show "gcd a \ gcd b = gcd b \ gcd a" haftmann@58023: by (simp add: fun_eq_iff ac_simps) haftmann@58023: next haftmann@58023: fix a show "gcd a \ gcd a = gcd a" haftmann@58023: by (simp add: fun_eq_iff gcd_left_idem) haftmann@58023: qed haftmann@58023: haftmann@58023: lemma coprime_dvd_mult: haftmann@60430: assumes "gcd c b = 1" and "c dvd a * b" haftmann@60430: shows "c dvd a" haftmann@58023: proof - haftmann@60634: let ?nf = "unit_factor" haftmann@60430: from assms gcd_mult_distrib [of a c b] haftmann@60430: have A: "a = gcd (a * c) (a * b) * ?nf a" by simp wenzelm@60526: from \c dvd a * b\ show ?thesis by (subst A, simp_all add: gcd_greatest) haftmann@58023: qed haftmann@58023: haftmann@58023: lemma coprime_dvd_mult_iff: haftmann@60430: "gcd c b = 1 \ (c dvd a * b) = (c dvd a)" haftmann@58023: by (rule, rule coprime_dvd_mult, simp_all) haftmann@58023: haftmann@58023: lemma gcd_dvd_antisym: haftmann@58023: "gcd a b dvd gcd c d \ gcd c d dvd gcd a b \ gcd a b = gcd c d" haftmann@58023: proof (rule gcdI) haftmann@58023: assume A: "gcd a b dvd gcd c d" and B: "gcd c d dvd gcd a b" haftmann@58023: have "gcd c d dvd c" by simp haftmann@58023: with A show "gcd a b dvd c" by (rule dvd_trans) haftmann@58023: have "gcd c d dvd d" by simp haftmann@58023: with A show "gcd a b dvd d" by (rule dvd_trans) haftmann@60634: show "unit_factor (gcd a b) = (if gcd a b = 0 then 0 else 1)" haftmann@59009: by simp haftmann@58023: fix l assume "l dvd c" and "l dvd d" haftmann@58023: hence "l dvd gcd c d" by (rule gcd_greatest) haftmann@58023: from this and B show "l dvd gcd a b" by (rule dvd_trans) haftmann@58023: qed haftmann@58023: haftmann@58023: lemma gcd_mult_cancel: haftmann@58023: assumes "gcd k n = 1" haftmann@58023: shows "gcd (k * m) n = gcd m n" haftmann@58023: proof (rule gcd_dvd_antisym) haftmann@58023: have "gcd (gcd (k * m) n) k = gcd (gcd k n) (k * m)" by (simp add: ac_simps) wenzelm@60526: also note \gcd k n = 1\ haftmann@58023: finally have "gcd (gcd (k * m) n) k = 1" by simp haftmann@58023: hence "gcd (k * m) n dvd m" by (rule coprime_dvd_mult, simp add: ac_simps) haftmann@58023: moreover have "gcd (k * m) n dvd n" by simp haftmann@58023: ultimately show "gcd (k * m) n dvd gcd m n" by (rule gcd_greatest) haftmann@58023: have "gcd m n dvd (k * m)" and "gcd m n dvd n" by simp_all haftmann@58023: then show "gcd m n dvd gcd (k * m) n" by (rule gcd_greatest) haftmann@58023: qed haftmann@58023: haftmann@58023: lemma coprime_crossproduct: haftmann@58023: assumes [simp]: "gcd a d = 1" "gcd b c = 1" haftmann@58023: shows "associated (a * c) (b * d) \ associated a b \ associated c d" (is "?lhs \ ?rhs") haftmann@58023: proof haftmann@58023: assume ?rhs then show ?lhs unfolding associated_def by (fast intro: mult_dvd_mono) haftmann@58023: next haftmann@58023: assume ?lhs wenzelm@60526: from \?lhs\ have "a dvd b * d" unfolding associated_def by (metis dvd_mult_left) haftmann@58023: hence "a dvd b" by (simp add: coprime_dvd_mult_iff) wenzelm@60526: moreover from \?lhs\ have "b dvd a * c" unfolding associated_def by (metis dvd_mult_left) haftmann@58023: hence "b dvd a" by (simp add: coprime_dvd_mult_iff) wenzelm@60526: moreover from \?lhs\ have "c dvd d * b" haftmann@59009: unfolding associated_def by (auto dest: dvd_mult_right simp add: ac_simps) haftmann@58023: hence "c dvd d" by (simp add: coprime_dvd_mult_iff gcd.commute) wenzelm@60526: moreover from \?lhs\ have "d dvd c * a" haftmann@59009: unfolding associated_def by (auto dest: dvd_mult_right simp add: ac_simps) haftmann@58023: hence "d dvd c" by (simp add: coprime_dvd_mult_iff gcd.commute) haftmann@58023: ultimately show ?rhs unfolding associated_def by simp haftmann@58023: qed haftmann@58023: haftmann@58023: lemma gcd_add1 [simp]: haftmann@58023: "gcd (m + n) n = gcd m n" haftmann@58023: by (cases "n = 0", simp_all add: gcd_non_0) haftmann@58023: haftmann@58023: lemma gcd_add2 [simp]: haftmann@58023: "gcd m (m + n) = gcd m n" haftmann@58023: using gcd_add1 [of n m] by (simp add: ac_simps) haftmann@58023: haftmann@60572: lemma gcd_add_mult: haftmann@60572: "gcd m (k * m + n) = gcd m n" haftmann@60572: proof - haftmann@60572: have "gcd m ((k * m + n) mod m) = gcd m (k * m + n)" haftmann@60572: by (fact gcd_mod2) haftmann@60572: then show ?thesis by simp haftmann@60572: qed haftmann@58023: haftmann@60430: lemma coprimeI: "(\l. \l dvd a; l dvd b\ \ l dvd 1) \ gcd a b = 1" haftmann@58023: by (rule sym, rule gcdI, simp_all) haftmann@58023: haftmann@58023: lemma coprime: "gcd a b = 1 \ (\d. d dvd a \ d dvd b \ is_unit d)" haftmann@59061: by (auto intro: coprimeI gcd_greatest dvd_gcd_D1 dvd_gcd_D2) haftmann@58023: haftmann@58023: lemma div_gcd_coprime: haftmann@58023: assumes nz: "a \ 0 \ b \ 0" haftmann@58023: defines [simp]: "d \ gcd a b" haftmann@58023: defines [simp]: "a' \ a div d" and [simp]: "b' \ b div d" haftmann@58023: shows "gcd a' b' = 1" haftmann@58023: proof (rule coprimeI) haftmann@58023: fix l assume "l dvd a'" "l dvd b'" haftmann@58023: then obtain s t where "a' = l * s" "b' = l * t" unfolding dvd_def by blast haftmann@59009: moreover have "a = a' * d" "b = b' * d" by simp_all haftmann@58023: ultimately have "a = (l * d) * s" "b = (l * d) * t" haftmann@59009: by (simp_all only: ac_simps) haftmann@58023: hence "l*d dvd a" and "l*d dvd b" by (simp_all only: dvd_triv_left) haftmann@58023: hence "l*d dvd d" by (simp add: gcd_greatest) haftmann@59009: then obtain u where "d = l * d * u" .. haftmann@59009: then have "d * (l * u) = d" by (simp add: ac_simps) haftmann@59009: moreover from nz have "d \ 0" by simp haftmann@59009: with div_mult_self1_is_id have "d * (l * u) div d = l * u" . haftmann@59009: ultimately have "1 = l * u" wenzelm@60526: using \d \ 0\ by simp haftmann@59009: then show "l dvd 1" .. haftmann@58023: qed haftmann@58023: haftmann@58023: lemma coprime_mult: haftmann@58023: assumes da: "gcd d a = 1" and db: "gcd d b = 1" haftmann@58023: shows "gcd d (a * b) = 1" haftmann@58023: apply (subst gcd.commute) haftmann@58023: using da apply (subst gcd_mult_cancel) haftmann@58023: apply (subst gcd.commute, assumption) haftmann@58023: apply (subst gcd.commute, rule db) haftmann@58023: done haftmann@58023: haftmann@58023: lemma coprime_lmult: haftmann@58023: assumes dab: "gcd d (a * b) = 1" haftmann@58023: shows "gcd d a = 1" haftmann@58023: proof (rule coprimeI) haftmann@58023: fix l assume "l dvd d" and "l dvd a" haftmann@58023: hence "l dvd a * b" by simp wenzelm@60526: with \l dvd d\ and dab show "l dvd 1" by (auto intro: gcd_greatest) haftmann@58023: qed haftmann@58023: haftmann@58023: lemma coprime_rmult: haftmann@58023: assumes dab: "gcd d (a * b) = 1" haftmann@58023: shows "gcd d b = 1" haftmann@58023: proof (rule coprimeI) haftmann@58023: fix l assume "l dvd d" and "l dvd b" haftmann@58023: hence "l dvd a * b" by simp wenzelm@60526: with \l dvd d\ and dab show "l dvd 1" by (auto intro: gcd_greatest) haftmann@58023: qed haftmann@58023: haftmann@58023: lemma coprime_mul_eq: "gcd d (a * b) = 1 \ gcd d a = 1 \ gcd d b = 1" haftmann@58023: using coprime_rmult[of d a b] coprime_lmult[of d a b] coprime_mult[of d a b] by blast haftmann@58023: haftmann@58023: lemma gcd_coprime: haftmann@60430: assumes c: "gcd a b \ 0" and a: "a = a' * gcd a b" and b: "b = b' * gcd a b" haftmann@58023: shows "gcd a' b' = 1" haftmann@58023: proof - haftmann@60430: from c have "a \ 0 \ b \ 0" by simp haftmann@58023: with div_gcd_coprime have "gcd (a div gcd a b) (b div gcd a b) = 1" . haftmann@58023: also from assms have "a div gcd a b = a'" by (metis div_mult_self2_is_id)+ haftmann@58023: also from assms have "b div gcd a b = b'" by (metis div_mult_self2_is_id)+ haftmann@58023: finally show ?thesis . haftmann@58023: qed haftmann@58023: haftmann@58023: lemma coprime_power: haftmann@58023: assumes "0 < n" haftmann@58023: shows "gcd a (b ^ n) = 1 \ gcd a b = 1" haftmann@58023: using assms proof (induct n) haftmann@58023: case (Suc n) then show ?case haftmann@58023: by (cases n) (simp_all add: coprime_mul_eq) haftmann@58023: qed simp haftmann@58023: haftmann@58023: lemma gcd_coprime_exists: haftmann@58023: assumes nz: "gcd a b \ 0" haftmann@58023: shows "\a' b'. a = a' * gcd a b \ b = b' * gcd a b \ gcd a' b' = 1" haftmann@58023: apply (rule_tac x = "a div gcd a b" in exI) haftmann@58023: apply (rule_tac x = "b div gcd a b" in exI) haftmann@59009: apply (insert nz, auto intro: div_gcd_coprime) haftmann@58023: done haftmann@58023: haftmann@58023: lemma coprime_exp: haftmann@58023: "gcd d a = 1 \ gcd d (a^n) = 1" haftmann@58023: by (induct n, simp_all add: coprime_mult) haftmann@58023: haftmann@58023: lemma coprime_exp2 [intro]: haftmann@58023: "gcd a b = 1 \ gcd (a^n) (b^m) = 1" haftmann@58023: apply (rule coprime_exp) haftmann@58023: apply (subst gcd.commute) haftmann@58023: apply (rule coprime_exp) haftmann@58023: apply (subst gcd.commute) haftmann@58023: apply assumption haftmann@58023: done haftmann@58023: haftmann@58023: lemma gcd_exp: haftmann@58023: "gcd (a^n) (b^n) = (gcd a b) ^ n" haftmann@58023: proof (cases "a = 0 \ b = 0") haftmann@58023: assume "a = 0 \ b = 0" haftmann@58023: then show ?thesis by (cases n, simp_all add: gcd_0_left) haftmann@58023: next haftmann@58023: assume A: "$$a = 0 \ b = 0)" haftmann@58023: hence "1 = gcd ((a div gcd a b)^n) ((b div gcd a b)^n)" haftmann@58023: using div_gcd_coprime by (subst sym, auto simp: div_gcd_coprime) haftmann@58023: hence "(gcd a b) ^ n = (gcd a b) ^ n * ..." by simp haftmann@58023: also note gcd_mult_distrib haftmann@60634: also have "unit_factor ((gcd a b)^n) = 1" haftmann@60634: by (simp add: unit_factor_power A) haftmann@58023: also have "(gcd a b)^n * (a div gcd a b)^n = a^n" haftmann@58023: by (subst ac_simps, subst div_power, simp, rule dvd_div_mult_self, rule dvd_power_same, simp) haftmann@58023: also have "(gcd a b)^n * (b div gcd a b)^n = b^n" haftmann@58023: by (subst ac_simps, subst div_power, simp, rule dvd_div_mult_self, rule dvd_power_same, simp) haftmann@58023: finally show ?thesis by simp haftmann@58023: qed haftmann@58023: haftmann@58023: lemma coprime_common_divisor: haftmann@60430: "gcd a b = 1 \ a dvd a \ a dvd b \ is_unit a" haftmann@60430: apply (subgoal_tac "a dvd gcd a b") haftmann@59061: apply simp haftmann@58023: apply (erule (1) gcd_greatest) haftmann@58023: done haftmann@58023: haftmann@58023: lemma division_decomp: haftmann@58023: assumes dc: "a dvd b * c" haftmann@58023: shows "\b' c'. a = b' * c' \ b' dvd b \ c' dvd c" haftmann@58023: proof (cases "gcd a b = 0") haftmann@58023: assume "gcd a b = 0" haftmann@59009: hence "a = 0 \ b = 0" by simp haftmann@58023: hence "a = 0 * c \ 0 dvd b \ c dvd c" by simp haftmann@58023: then show ?thesis by blast haftmann@58023: next haftmann@58023: let ?d = "gcd a b" haftmann@58023: assume "?d \ 0" haftmann@58023: from gcd_coprime_exists[OF this] haftmann@58023: obtain a' b' where ab': "a = a' * ?d" "b = b' * ?d" "gcd a' b' = 1" haftmann@58023: by blast haftmann@58023: from ab'(1) have "a' dvd a" unfolding dvd_def by blast haftmann@58023: with dc have "a' dvd b*c" using dvd_trans[of a' a "b*c"] by simp haftmann@58023: from dc ab'(1,2) have "a'*?d dvd (b'*?d) * c" by simp haftmann@58023: hence "?d * a' dvd ?d * (b' * c)" by (simp add: mult_ac) wenzelm@60526: with \?d \ 0\ have "a' dvd b' * c" by simp haftmann@58023: with coprime_dvd_mult[OF ab'(3)] haftmann@58023: have "a' dvd c" by (subst (asm) ac_simps, blast) haftmann@58023: with ab'(1) have "a = ?d * a' \ ?d dvd b \ a' dvd c" by (simp add: mult_ac) haftmann@58023: then show ?thesis by blast haftmann@58023: qed haftmann@58023: haftmann@60433: lemma pow_divs_pow: haftmann@58023: assumes ab: "a ^ n dvd b ^ n" and n: "n \ 0" haftmann@58023: shows "a dvd b" haftmann@58023: proof (cases "gcd a b = 0") haftmann@58023: assume "gcd a b = 0" haftmann@59009: then show ?thesis by simp haftmann@58023: next haftmann@58023: let ?d = "gcd a b" haftmann@58023: assume "?d \ 0" haftmann@58023: from n obtain m where m: "n = Suc m" by (cases n, simp_all) wenzelm@60526: from \?d \ 0\ have zn: "?d ^ n \ 0" by (rule power_not_zero) wenzelm@60526: from gcd_coprime_exists[OF \?d \ 0\] haftmann@58023: obtain a' b' where ab': "a = a' * ?d" "b = b' * ?d" "gcd a' b' = 1" haftmann@58023: by blast haftmann@58023: from ab have "(a' * ?d) ^ n dvd (b' * ?d) ^ n" haftmann@58023: by (simp add: ab'(1,2)[symmetric]) haftmann@58023: hence "?d^n * a'^n dvd ?d^n * b'^n" haftmann@58023: by (simp only: power_mult_distrib ac_simps) haftmann@59009: with zn have "a'^n dvd b'^n" by simp haftmann@58023: hence "a' dvd b'^n" using dvd_trans[of a' "a'^n" "b'^n"] by (simp add: m) haftmann@58023: hence "a' dvd b'^m * b'" by (simp add: m ac_simps) haftmann@58023: with coprime_dvd_mult[OF coprime_exp[OF ab'(3), of m]] haftmann@58023: have "a' dvd b'" by (subst (asm) ac_simps, blast) haftmann@58023: hence "a'*?d dvd b'*?d" by (rule mult_dvd_mono, simp) haftmann@58023: with ab'(1,2) show ?thesis by simp haftmann@58023: qed haftmann@58023: haftmann@60433: lemma pow_divs_eq [simp]: haftmann@58023: "n \ 0 \ a ^ n dvd b ^ n \ a dvd b" haftmann@60433: by (auto intro: pow_divs_pow dvd_power_same) haftmann@58023: haftmann@60433: lemma divs_mult: haftmann@58023: assumes mr: "m dvd r" and nr: "n dvd r" and mn: "gcd m n = 1" haftmann@58023: shows "m * n dvd r" haftmann@58023: proof - haftmann@58023: from mr nr obtain m' n' where m': "r = m*m'" and n': "r = n*n'" haftmann@58023: unfolding dvd_def by blast haftmann@58023: from mr n' have "m dvd n'*n" by (simp add: ac_simps) haftmann@58023: hence "m dvd n'" using coprime_dvd_mult_iff[OF mn] by simp haftmann@58023: then obtain k where k: "n' = m*k" unfolding dvd_def by blast haftmann@58023: with n' have "r = m * n * k" by (simp add: mult_ac) haftmann@58023: then show ?thesis unfolding dvd_def by blast haftmann@58023: qed haftmann@58023: haftmann@58023: lemma coprime_plus_one [simp]: "gcd (n + 1) n = 1" haftmann@58023: by (subst add_commute, simp) haftmann@58023: haftmann@58023: lemma setprod_coprime [rule_format]: haftmann@60430: "(\i\A. gcd (f i) a = 1) \ gcd (\i\A. f i) a = 1" haftmann@58023: apply (cases "finite A") haftmann@58023: apply (induct set: finite) haftmann@58023: apply (auto simp add: gcd_mult_cancel) haftmann@58023: done haftmann@58023: haftmann@58023: lemma coprime_divisors: haftmann@58023: assumes "d dvd a" "e dvd b" "gcd a b = 1" haftmann@58023: shows "gcd d e = 1" haftmann@58023: proof - haftmann@58023: from assms obtain k l where "a = d * k" "b = e * l" haftmann@58023: unfolding dvd_def by blast haftmann@58023: with assms have "gcd (d * k) (e * l) = 1" by simp haftmann@58023: hence "gcd (d * k) e = 1" by (rule coprime_lmult) haftmann@58023: also have "gcd (d * k) e = gcd e (d * k)" by (simp add: ac_simps) haftmann@58023: finally have "gcd e d = 1" by (rule coprime_lmult) haftmann@58023: then show ?thesis by (simp add: ac_simps) haftmann@58023: qed haftmann@58023: haftmann@58023: lemma invertible_coprime: haftmann@60430: assumes "a * b mod m = 1" haftmann@60430: shows "coprime a m" haftmann@59009: proof - haftmann@60430: from assms have "coprime m (a * b mod m)" haftmann@59009: by simp haftmann@60430: then have "coprime m (a * b)" haftmann@59009: by simp haftmann@60430: then have "coprime m a" haftmann@59009: by (rule coprime_lmult) haftmann@59009: then show ?thesis haftmann@59009: by (simp add: ac_simps) haftmann@59009: qed haftmann@58023: haftmann@58023: lemma lcm_gcd: haftmann@60634: "lcm a b = normalize (a * b) div gcd a b" haftmann@60634: by (simp add: lcm_lcm_eucl gcd_gcd_eucl lcm_eucl_def) haftmann@58023: haftmann@58023: lemma lcm_gcd_prod: haftmann@60634: "lcm a b * gcd a b = normalize (a * b)" haftmann@60634: by (simp add: lcm_gcd) haftmann@58023: haftmann@58023: lemma lcm_dvd1 [iff]: haftmann@60430: "a dvd lcm a b" haftmann@60430: proof (cases "a*b = 0") haftmann@60430: assume "a * b \ 0" haftmann@60430: hence "gcd a b \ 0" by simp haftmann@60634: let ?c = "1 div unit_factor (a * b)" haftmann@60634: from \a * b \ 0\ have [simp]: "is_unit (unit_factor (a * b))" by simp haftmann@60430: from lcm_gcd_prod[of a b] have "lcm a b * gcd a b = a * ?c * b" haftmann@60432: by (simp add: div_mult_swap unit_div_commute) haftmann@60430: hence "lcm a b * gcd a b div gcd a b = a * ?c * b div gcd a b" by simp wenzelm@60526: with \gcd a b \ 0\ have "lcm a b = a * ?c * b div gcd a b" haftmann@58023: by (subst (asm) div_mult_self2_is_id, simp_all) haftmann@60430: also have "... = a * (?c * b div gcd a b)" haftmann@58023: by (metis div_mult_swap gcd_dvd2 mult_assoc) haftmann@58023: finally show ?thesis by (rule dvdI) haftmann@58953: qed (auto simp add: lcm_gcd) haftmann@58023: haftmann@58023: lemma lcm_least: haftmann@58023: "\a dvd k; b dvd k\ \ lcm a b dvd k" haftmann@58023: proof (cases "k = 0") haftmann@60634: let ?nf = unit_factor haftmann@58023: assume "k \ 0" haftmann@58023: hence "is_unit (?nf k)" by simp haftmann@58023: hence "?nf k \ 0" by (metis not_is_unit_0) haftmann@58023: assume A: "a dvd k" "b dvd k" wenzelm@60526: hence "gcd a b \ 0" using \k \ 0\ by auto haftmann@58023: from A obtain r s where ar: "k = a * r" and bs: "k = b * s" haftmann@58023: unfolding dvd_def by blast wenzelm@60526: with \k \ 0\ have "r * s \ 0" haftmann@58953: by auto (drule sym [of 0], simp) haftmann@58023: hence "is_unit (?nf (r * s))" by simp haftmann@58023: let ?c = "?nf k div ?nf (r*s)" wenzelm@60526: from \is_unit (?nf k)\ and \is_unit (?nf (r * s))\ have "is_unit ?c" by (rule unit_div) haftmann@58023: hence "?c \ 0" using not_is_unit_0 by fast haftmann@58023: from ar bs have "k * k * gcd s r = ?nf k * k * gcd (k * s) (k * r)" haftmann@58953: by (subst mult_assoc, subst gcd_mult_distrib[of k s r], simp only: ac_simps) haftmann@58023: also have "... = ?nf k * k * gcd ((r*s) * a) ((r*s) * b)" wenzelm@60526: by (subst (3) \k = a * r\, subst (3) \k = b * s\, simp add: algebra_simps) wenzelm@60526: also have "... = ?c * r*s * k * gcd a b" using \r * s \ 0\ haftmann@58023: by (subst gcd_mult_distrib'[symmetric], simp add: algebra_simps unit_simps) haftmann@58023: finally have "(a*r) * (b*s) * gcd s r = ?c * k * r * s * gcd a b" haftmann@58023: by (subst ar[symmetric], subst bs[symmetric], simp add: mult_ac) haftmann@58023: hence "a * b * gcd s r * (r * s) = ?c * k * gcd a b * (r * s)" haftmann@58023: by (simp add: algebra_simps) wenzelm@60526: hence "?c * k * gcd a b = a * b * gcd s r" using \r * s \ 0\ haftmann@58023: by (metis div_mult_self2_is_id) haftmann@58023: also have "... = lcm a b * gcd a b * gcd s r * ?nf (a*b)" haftmann@58023: by (subst lcm_gcd_prod[of a b], metis gcd_mult_distrib gcd_mult_distrib') haftmann@58023: also have "... = lcm a b * gcd s r * ?nf (a*b) * gcd a b" haftmann@58023: by (simp add: algebra_simps) wenzelm@60526: finally have "k * ?c = lcm a b * gcd s r * ?nf (a*b)" using \gcd a b \ 0\ haftmann@58023: by (metis mult.commute div_mult_self2_is_id) wenzelm@60526: hence "k = lcm a b * (gcd s r * ?nf (a*b)) div ?c" using \?c \ 0\ haftmann@58023: by (metis div_mult_self2_is_id mult_assoc) wenzelm@60526: also have "... = lcm a b * (gcd s r * ?nf (a*b) div ?c)" using \is_unit ?c\ haftmann@58023: by (simp add: unit_simps) haftmann@58023: finally show ?thesis by (rule dvdI) haftmann@58023: qed simp haftmann@58023: haftmann@58023: lemma lcm_zero: haftmann@58023: "lcm a b = 0 \ a = 0 \ b = 0" haftmann@58023: proof - haftmann@60634: let ?nf = unit_factor haftmann@58023: { haftmann@58023: assume "a \ 0" "b \ 0" haftmann@58023: hence "a * b div ?nf (a * b) \ 0" by (simp add: no_zero_divisors) wenzelm@60526: moreover from \a \ 0\ and \b \ 0\ have "gcd a b \ 0" by simp haftmann@58023: ultimately have "lcm a b \ 0" using lcm_gcd_prod[of a b] by (intro notI, simp) haftmann@58023: } moreover { haftmann@58023: assume "a = 0 \ b = 0" haftmann@58023: hence "lcm a b = 0" by (elim disjE, simp_all add: lcm_gcd) haftmann@58023: } haftmann@58023: ultimately show ?thesis by blast haftmann@58023: qed haftmann@58023: haftmann@58023: lemmas lcm_0_iff = lcm_zero haftmann@58023: haftmann@58023: lemma gcd_lcm: haftmann@58023: assumes "lcm a b \ 0" haftmann@60634: shows "gcd a b = normalize (a * b) div lcm a b" haftmann@60634: proof - haftmann@60634: have "lcm a b * gcd a b = normalize (a * b)" haftmann@60634: by (fact lcm_gcd_prod) haftmann@60634: with assms show ?thesis haftmann@60634: by (metis nonzero_mult_divide_cancel_left) haftmann@58023: qed haftmann@58023: haftmann@60634: lemma unit_factor_lcm [simp]: haftmann@60634: "unit_factor (lcm a b) = (if a = 0 \ b = 0 then 0 else 1)" haftmann@60634: by (simp add: dvd_unit_factor_div lcm_gcd) haftmann@58023: haftmann@60430: lemma lcm_dvd2 [iff]: "b dvd lcm a b" haftmann@60430: using lcm_dvd1 [of b a] by (simp add: lcm_gcd ac_simps) haftmann@58023: haftmann@58023: lemma lcmI: haftmann@60634: assumes "a dvd c" and "b dvd c" and "\d. a dvd d \ b dvd d \ c dvd d" haftmann@60634: and "unit_factor c = (if c = 0 then 0 else 1)" haftmann@60634: shows "c = lcm a b" haftmann@60634: by (rule associated_eqI) (auto simp: assms associated_def intro: lcm_least) haftmann@58023: haftmann@58023: sublocale lcm!: abel_semigroup lcm haftmann@58023: proof haftmann@60430: fix a b c haftmann@60430: show "lcm (lcm a b) c = lcm a (lcm b c)" haftmann@58023: proof (rule lcmI) haftmann@60430: have "a dvd lcm a b" and "lcm a b dvd lcm (lcm a b) c" by simp_all haftmann@60430: then show "a dvd lcm (lcm a b) c" by (rule dvd_trans) haftmann@58023: haftmann@60430: have "b dvd lcm a b" and "lcm a b dvd lcm (lcm a b) c" by simp_all haftmann@60430: hence "b dvd lcm (lcm a b) c" by (rule dvd_trans) haftmann@60430: moreover have "c dvd lcm (lcm a b) c" by simp haftmann@60430: ultimately show "lcm b c dvd lcm (lcm a b) c" by (rule lcm_least) haftmann@58023: haftmann@60430: fix l assume "a dvd l" and "lcm b c dvd l" haftmann@60430: have "b dvd lcm b c" by simp wenzelm@60526: from this and \lcm b c dvd l\ have "b dvd l" by (rule dvd_trans) haftmann@60430: have "c dvd lcm b c" by simp wenzelm@60526: from this and \lcm b c dvd l\ have "c dvd l" by (rule dvd_trans) wenzelm@60526: from \a dvd l\ and \b dvd l\ have "lcm a b dvd l" by (rule lcm_least) wenzelm@60526: from this and \c dvd l\ show "lcm (lcm a b) c dvd l" by (rule lcm_least) haftmann@58023: qed (simp add: lcm_zero) haftmann@58023: next haftmann@60430: fix a b haftmann@60430: show "lcm a b = lcm b a" haftmann@58023: by (simp add: lcm_gcd ac_simps) haftmann@58023: qed haftmann@58023: haftmann@58023: lemma dvd_lcm_D1: haftmann@58023: "lcm m n dvd k \ m dvd k" haftmann@58023: by (rule dvd_trans, rule lcm_dvd1, assumption) haftmann@58023: haftmann@58023: lemma dvd_lcm_D2: haftmann@58023: "lcm m n dvd k \ n dvd k" haftmann@58023: by (rule dvd_trans, rule lcm_dvd2, assumption) haftmann@58023: haftmann@58023: lemma gcd_dvd_lcm [simp]: haftmann@58023: "gcd a b dvd lcm a b" haftmann@58023: by (metis dvd_trans gcd_dvd2 lcm_dvd2) haftmann@58023: haftmann@58023: lemma lcm_1_iff: haftmann@58023: "lcm a b = 1 \ is_unit a \ is_unit b" haftmann@58023: proof haftmann@58023: assume "lcm a b = 1" haftmann@59061: then show "is_unit a \ is_unit b" by auto haftmann@58023: next haftmann@58023: assume "is_unit a \ is_unit b" haftmann@59061: hence "a dvd 1" and "b dvd 1" by simp_all haftmann@59061: hence "is_unit (lcm a b)" by (rule lcm_least) haftmann@60634: hence "lcm a b = unit_factor (lcm a b)" haftmann@60634: by (blast intro: sym is_unit_unit_factor) wenzelm@60526: also have "\ = 1" using \is_unit a \ is_unit b\ haftmann@59061: by auto haftmann@58023: finally show "lcm a b = 1" . haftmann@58023: qed haftmann@58023: haftmann@58023: lemma lcm_0_left [simp]: haftmann@60430: "lcm 0 a = 0" haftmann@58023: by (rule sym, rule lcmI, simp_all) haftmann@58023: haftmann@58023: lemma lcm_0 [simp]: haftmann@60430: "lcm a 0 = 0" haftmann@58023: by (rule sym, rule lcmI, simp_all) haftmann@58023: haftmann@58023: lemma lcm_unique: haftmann@58023: "a dvd d \ b dvd d \ haftmann@60634: unit_factor d = (if d = 0 then 0 else 1) \ haftmann@58023: (\e. a dvd e \ b dvd e \ d dvd e) \ d = lcm a b" haftmann@58023: by (rule, auto intro: lcmI simp: lcm_least lcm_zero) haftmann@58023: haftmann@58023: lemma dvd_lcm_I1 [simp]: haftmann@58023: "k dvd m \ k dvd lcm m n" haftmann@58023: by (metis lcm_dvd1 dvd_trans) haftmann@58023: haftmann@58023: lemma dvd_lcm_I2 [simp]: haftmann@58023: "k dvd n \ k dvd lcm m n" haftmann@58023: by (metis lcm_dvd2 dvd_trans) haftmann@58023: haftmann@58023: lemma lcm_1_left [simp]: haftmann@60634: "lcm 1 a = normalize a" haftmann@60430: by (cases "a = 0") (simp, rule sym, rule lcmI, simp_all) haftmann@58023: haftmann@58023: lemma lcm_1_right [simp]: haftmann@60634: "lcm a 1 = normalize a" haftmann@60430: using lcm_1_left [of a] by (simp add: ac_simps) haftmann@58023: haftmann@58023: lemma lcm_coprime: haftmann@60634: "gcd a b = 1 \ lcm a b = normalize (a * b)" haftmann@58023: by (subst lcm_gcd) simp haftmann@58023: haftmann@58023: lemma lcm_proj1_if_dvd: haftmann@60634: "b dvd a \ lcm a b = normalize a" haftmann@60430: by (cases "a = 0") (simp, rule sym, rule lcmI, simp_all) haftmann@58023: haftmann@58023: lemma lcm_proj2_if_dvd: haftmann@60634: "a dvd b \ lcm a b = normalize b" haftmann@60430: using lcm_proj1_if_dvd [of a b] by (simp add: ac_simps) haftmann@58023: haftmann@58023: lemma lcm_proj1_iff: haftmann@60634: "lcm m n = normalize m \ n dvd m" haftmann@58023: proof haftmann@60634: assume A: "lcm m n = normalize m" haftmann@58023: show "n dvd m" haftmann@58023: proof (cases "m = 0") haftmann@58023: assume [simp]: "m \ 0" haftmann@60634: from A have B: "m = lcm m n * unit_factor m" haftmann@58023: by (simp add: unit_eq_div2) haftmann@58023: show ?thesis by (subst B, simp) haftmann@58023: qed simp haftmann@58023: next haftmann@58023: assume "n dvd m" haftmann@60634: then show "lcm m n = normalize m" by (rule lcm_proj1_if_dvd) haftmann@58023: qed haftmann@58023: haftmann@58023: lemma lcm_proj2_iff: haftmann@60634: "lcm m n = normalize n \ m dvd n" haftmann@58023: using lcm_proj1_iff [of n m] by (simp add: ac_simps) haftmann@58023: haftmann@58023: lemma euclidean_size_lcm_le1: haftmann@58023: assumes "a \ 0" and "b \ 0" haftmann@58023: shows "euclidean_size a \ euclidean_size (lcm a b)" haftmann@58023: proof - haftmann@58023: have "a dvd lcm a b" by (rule lcm_dvd1) haftmann@58023: then obtain c where A: "lcm a b = a * c" unfolding dvd_def by blast wenzelm@60526: with \a \ 0\ and \b \ 0\ have "c \ 0" by (auto simp: lcm_zero) haftmann@58023: then show ?thesis by (subst A, intro size_mult_mono) haftmann@58023: qed haftmann@58023: haftmann@58023: lemma euclidean_size_lcm_le2: haftmann@58023: "a \ 0 \ b \ 0 \ euclidean_size b \ euclidean_size (lcm a b)" haftmann@58023: using euclidean_size_lcm_le1 [of b a] by (simp add: ac_simps) haftmann@58023: haftmann@58023: lemma euclidean_size_lcm_less1: haftmann@58023: assumes "b \ 0" and "\b dvd a" haftmann@58023: shows "euclidean_size a < euclidean_size (lcm a b)" haftmann@58023: proof (rule ccontr) haftmann@58023: from assms have "a \ 0" by auto haftmann@58023: assume "\euclidean_size a < euclidean_size (lcm a b)" wenzelm@60526: with \a \ 0\ and \b \ 0\ have "euclidean_size (lcm a b) = euclidean_size a" haftmann@58023: by (intro le_antisym, simp, intro euclidean_size_lcm_le1) haftmann@58023: with assms have "lcm a b dvd a" haftmann@58023: by (rule_tac dvd_euclidean_size_eq_imp_dvd) (auto simp: lcm_zero) haftmann@58023: hence "b dvd a" by (rule dvd_lcm_D2) wenzelm@60526: with \\b dvd a\ show False by contradiction haftmann@58023: qed haftmann@58023: haftmann@58023: lemma euclidean_size_lcm_less2: haftmann@58023: assumes "a \ 0" and "\a dvd b" haftmann@58023: shows "euclidean_size b < euclidean_size (lcm a b)" haftmann@58023: using assms euclidean_size_lcm_less1 [of a b] by (simp add: ac_simps) haftmann@58023: haftmann@58023: lemma lcm_mult_unit1: haftmann@60430: "is_unit a \ lcm (b * a) c = lcm b c" haftmann@58023: apply (rule lcmI) haftmann@60430: apply (rule dvd_trans[of _ "b * a"], simp, rule lcm_dvd1) haftmann@58023: apply (rule lcm_dvd2) haftmann@58023: apply (rule lcm_least, simp add: unit_simps, assumption) haftmann@60634: apply (subst unit_factor_lcm, simp add: lcm_zero) haftmann@58023: done haftmann@58023: haftmann@58023: lemma lcm_mult_unit2: haftmann@60430: "is_unit a \ lcm b (c * a) = lcm b c" haftmann@60430: using lcm_mult_unit1 [of a c b] by (simp add: ac_simps) haftmann@58023: haftmann@58023: lemma lcm_div_unit1: haftmann@60430: "is_unit a \ lcm (b div a) c = lcm b c" haftmann@60433: by (erule is_unitE [of _ b]) (simp add: lcm_mult_unit1) haftmann@58023: haftmann@58023: lemma lcm_div_unit2: haftmann@60430: "is_unit a \ lcm b (c div a) = lcm b c" haftmann@60433: by (erule is_unitE [of _ c]) (simp add: lcm_mult_unit2) haftmann@58023: haftmann@60634: lemma normalize_lcm_left [simp]: haftmann@60634: "lcm (normalize a) b = lcm a b" haftmann@60634: proof (cases "a = 0") haftmann@60634: case True then show ?thesis haftmann@60634: by simp haftmann@60634: next haftmann@60634: case False then have "is_unit (unit_factor a)" haftmann@60634: by simp haftmann@60634: moreover have "normalize a = a div unit_factor a" haftmann@60634: by simp haftmann@60634: ultimately show ?thesis haftmann@60634: by (simp only: lcm_div_unit1) haftmann@60634: qed haftmann@60634: haftmann@60634: lemma normalize_lcm_right [simp]: haftmann@60634: "lcm a (normalize b) = lcm a b" haftmann@60634: using normalize_lcm_left [of b a] by (simp add: ac_simps) haftmann@60634: haftmann@58023: lemma lcm_left_idem: haftmann@60430: "lcm a (lcm a b) = lcm a b" haftmann@58023: apply (rule lcmI) haftmann@58023: apply simp haftmann@58023: apply (subst lcm.assoc [symmetric], rule lcm_dvd2) haftmann@58023: apply (rule lcm_least, assumption) haftmann@58023: apply (erule (1) lcm_least) haftmann@58023: apply (auto simp: lcm_zero) haftmann@58023: done haftmann@58023: haftmann@58023: lemma lcm_right_idem: haftmann@60430: "lcm (lcm a b) b = lcm a b" haftmann@58023: apply (rule lcmI) haftmann@58023: apply (subst lcm.assoc, rule lcm_dvd1) haftmann@58023: apply (rule lcm_dvd2) haftmann@58023: apply (rule lcm_least, erule (1) lcm_least, assumption) haftmann@58023: apply (auto simp: lcm_zero) haftmann@58023: done haftmann@58023: haftmann@58023: lemma comp_fun_idem_lcm: "comp_fun_idem lcm" haftmann@58023: proof haftmann@58023: fix a b show "lcm a \ lcm b = lcm b \ lcm a" haftmann@58023: by (simp add: fun_eq_iff ac_simps) haftmann@58023: next haftmann@58023: fix a show "lcm a \ lcm a = lcm a" unfolding o_def haftmann@58023: by (intro ext, simp add: lcm_left_idem) haftmann@58023: qed haftmann@58023: haftmann@60430: lemma dvd_Lcm [simp]: "a \ A \ a dvd Lcm A" haftmann@60634: and Lcm_least: "(\a. a \ A \ a dvd b) \ Lcm A dvd b" haftmann@60634: and unit_factor_Lcm [simp]: haftmann@60634: "unit_factor (Lcm A) = (if Lcm A = 0 then 0 else 1)" haftmann@58023: proof - haftmann@60430: have "(\a\A. a dvd Lcm A) \ (\l'. (\a\A. a dvd l') \ Lcm A dvd l') \ haftmann@60634: unit_factor (Lcm A) = (if Lcm A = 0 then 0 else 1)" (is ?thesis) haftmann@60430: proof (cases "\l. l \ 0 \ (\a\A. a dvd l)") haftmann@58023: case False haftmann@58023: hence "Lcm A = 0" by (auto simp: Lcm_Lcm_eucl Lcm_eucl_def) haftmann@58023: with False show ?thesis by auto haftmann@58023: next haftmann@58023: case True haftmann@60430: then obtain l\<^sub>0 where l\<^sub>0_props: "l\<^sub>0 \ 0 \ (\a\A. a dvd l\<^sub>0)" by blast haftmann@60430: def n \ "LEAST n. \l. l \ 0 \ (\a\A. a dvd l) \ euclidean_size l = n" haftmann@60430: def l \ "SOME l. l \ 0 \ (\a\A. a dvd l) \ euclidean_size l = n" haftmann@60430: have "\l. l \ 0 \ (\a\A. a dvd l) \ euclidean_size l = n" haftmann@58023: apply (subst n_def) haftmann@58023: apply (rule LeastI[of _ "euclidean_size l\<^sub>0"]) haftmann@58023: apply (rule exI[of _ l\<^sub>0]) haftmann@58023: apply (simp add: l\<^sub>0_props) haftmann@58023: done haftmann@60430: from someI_ex[OF this] have "l \ 0" and "\a\A. a dvd l" and "euclidean_size l = n" haftmann@58023: unfolding l_def by simp_all haftmann@58023: { haftmann@60430: fix l' assume "\a\A. a dvd l'" wenzelm@60526: with \\a\A. a dvd l\ have "\a\A. a dvd gcd l l'" by (auto intro: gcd_greatest) wenzelm@60526: moreover from \l \ 0\ have "gcd l l' \ 0" by simp haftmann@60430: ultimately have "\b. b \ 0 \ (\a\A. a dvd b) \ euclidean_size b = euclidean_size (gcd l l')" haftmann@58023: by (intro exI[of _ "gcd l l'"], auto) haftmann@58023: hence "euclidean_size (gcd l l') \ n" by (subst n_def) (rule Least_le) haftmann@58023: moreover have "euclidean_size (gcd l l') \ n" haftmann@58023: proof - haftmann@58023: have "gcd l l' dvd l" by simp haftmann@58023: then obtain a where "l = gcd l l' * a" unfolding dvd_def by blast wenzelm@60526: with \l \ 0\ have "a \ 0" by auto haftmann@58023: hence "euclidean_size (gcd l l') \ euclidean_size (gcd l l' * a)" haftmann@58023: by (rule size_mult_mono) wenzelm@60526: also have "gcd l l' * a = l" using \l = gcd l l' * a\ .. wenzelm@60526: also note \euclidean_size l = n\ haftmann@58023: finally show "euclidean_size (gcd l l') \ n" . haftmann@58023: qed haftmann@58023: ultimately have "euclidean_size l = euclidean_size (gcd l l')" wenzelm@60526: by (intro le_antisym, simp_all add: \euclidean_size l = n$$ wenzelm@60526: with \l \ 0\ have "l dvd gcd l l'" by (blast intro: dvd_euclidean_size_eq_imp_dvd) haftmann@58023: hence "l dvd l'" by (blast dest: dvd_gcd_D2) haftmann@58023: } haftmann@58023: haftmann@60634: with \(\a\A. a dvd l)\ and unit_factor_is_unit[OF \l \ 0\] and \l \ 0\ haftmann@60634: have "(\a\A. a dvd normalize l) \ haftmann@60634: (\l'. (\a\A. a dvd l') \ normalize l dvd l') \ haftmann@60634: unit_factor (normalize l) = haftmann@60634: (if normalize l = 0 then 0 else 1)" haftmann@58023: by (auto simp: unit_simps) haftmann@60634: also from True have "normalize l = Lcm A" haftmann@58023: by (simp add: Lcm_Lcm_eucl Lcm_eucl_def Let_def n_def l_def) haftmann@58023: finally show ?thesis . haftmann@58023: qed haftmann@58023: note A = this haftmann@58023: haftmann@60430: {fix a assume "a \ A" then show "a dvd Lcm A" using A by blast} haftmann@60634: {fix b assume "\a. a \ A \ a dvd b" then show "Lcm A dvd b" using A by blast} haftmann@60634: from A show "unit_factor (Lcm A) = (if Lcm A = 0 then 0 else 1)" by blast haftmann@58023: qed haftmann@60634: haftmann@60634: lemma normalize_Lcm [simp]: haftmann@60634: "normalize (Lcm A) = Lcm A" haftmann@60634: by (cases "Lcm A = 0") (auto intro: associated_eqI) haftmann@60634: haftmann@58023: lemma LcmI: haftmann@60634: assumes "\a. a \ A \ a dvd b" and "\c. (\a. a \ A \ a dvd c) \ b dvd c" haftmann@60634: and "unit_factor b = (if b = 0 then 0 else 1)" shows "b = Lcm A" haftmann@60634: by (rule associated_eqI) (auto simp: assms associated_def intro: Lcm_least) haftmann@58023: haftmann@58023: lemma Lcm_subset: haftmann@58023: "A \ B \ Lcm A dvd Lcm B" haftmann@60634: by (blast intro: Lcm_least dvd_Lcm) haftmann@58023: haftmann@58023: lemma Lcm_Un: haftmann@58023: "Lcm (A \ B) = lcm (Lcm A) (Lcm B)" haftmann@58023: apply (rule lcmI) haftmann@58023: apply (blast intro: Lcm_subset) haftmann@58023: apply (blast intro: Lcm_subset) haftmann@60634: apply (intro Lcm_least ballI, elim UnE) haftmann@58023: apply (rule dvd_trans, erule dvd_Lcm, assumption) haftmann@58023: apply (rule dvd_trans, erule dvd_Lcm, assumption) haftmann@58023: apply simp haftmann@58023: done haftmann@58023: haftmann@58023: lemma Lcm_1_iff: haftmann@60430: "Lcm A = 1 \ (\a\A. is_unit a)" haftmann@58023: proof haftmann@58023: assume "Lcm A = 1" haftmann@60430: then show "\a\A. is_unit a" by auto haftmann@58023: qed (rule LcmI [symmetric], auto) haftmann@58023: haftmann@58023: lemma Lcm_no_units: haftmann@60430: "Lcm A = Lcm (A - {a. is_unit a})" haftmann@58023: proof - haftmann@60430: have "(A - {a. is_unit a}) \ {a\A. is_unit a} = A" by blast haftmann@60430: hence "Lcm A = lcm (Lcm (A - {a. is_unit a})) (Lcm {a\A. is_unit a})" haftmann@60634: by (simp add: Lcm_Un [symmetric]) haftmann@60430: also have "Lcm {a\A. is_unit a} = 1" by (simp add: Lcm_1_iff) haftmann@58023: finally show ?thesis by simp haftmann@58023: qed haftmann@58023: haftmann@58023: lemma Lcm_empty [simp]: haftmann@58023: "Lcm {} = 1" haftmann@58023: by (simp add: Lcm_1_iff) haftmann@58023: haftmann@58023: lemma Lcm_eq_0 [simp]: haftmann@58023: "0 \ A \ Lcm A = 0" haftmann@58023: by (drule dvd_Lcm) simp haftmann@58023: haftmann@58023: lemma Lcm0_iff': haftmann@60430: "Lcm A = 0 \ \(\l. l \ 0 \ (\a\A. a dvd l))" haftmann@58023: proof haftmann@58023: assume "Lcm A = 0" haftmann@60430: show "\(\l. l \ 0 \ (\a\A. a dvd l))" haftmann@58023: proof haftmann@60430: assume ex: "\l. l \ 0 \ (\a\A. a dvd l)" haftmann@60430: then obtain l\<^sub>0 where l\<^sub>0_props: "l\<^sub>0 \ 0 \ (\a\A. a dvd l\<^sub>0)" by blast haftmann@60430: def n \ "LEAST n. \l. l \ 0 \ (\a\A. a dvd l) \ euclidean_size l = n" haftmann@60430: def l \ "SOME l. l \ 0 \ (\a\A. a dvd l) \ euclidean_size l = n" haftmann@60430: have "\l. l \ 0 \ (\a\A. a dvd l) \ euclidean_size l = n" haftmann@58023: apply (subst n_def) haftmann@58023: apply (rule LeastI[of _ "euclidean_size l\<^sub>0"]) haftmann@58023: apply (rule exI[of _ l\<^sub>0]) haftmann@58023: apply (simp add: l\<^sub>0_props) haftmann@58023: done haftmann@58023: from someI_ex[OF this] have "l \ 0" unfolding l_def by simp_all haftmann@60634: hence "normalize l \ 0" by simp haftmann@60634: also from ex have "normalize l = Lcm A" haftmann@58023: by (simp only: Lcm_Lcm_eucl Lcm_eucl_def n_def l_def if_True Let_def) wenzelm@60526: finally show False using \Lcm A = 0\ by contradiction haftmann@58023: qed haftmann@58023: qed (simp only: Lcm_Lcm_eucl Lcm_eucl_def if_False) haftmann@58023: haftmann@58023: lemma Lcm0_iff [simp]: haftmann@58023: "finite A \ Lcm A = 0 \ 0 \ A" haftmann@58023: proof - haftmann@58023: assume "finite A" haftmann@58023: have "0 \ A \ Lcm A = 0" by (intro dvd_0_left dvd_Lcm) haftmann@58023: moreover { haftmann@58023: assume "0 \ A" haftmann@58023: hence "\A \ 0" wenzelm@60526: apply (induct rule: finite_induct[OF \finite A\]) haftmann@58023: apply simp haftmann@58023: apply (subst setprod.insert, assumption, assumption) haftmann@58023: apply (rule no_zero_divisors) haftmann@58023: apply blast+ haftmann@58023: done wenzelm@60526: moreover from \finite A\ have "\a\A. a dvd \A" by blast haftmann@60430: ultimately have "\l. l \ 0 \ (\a\A. a dvd l)" by blast haftmann@58023: with Lcm0_iff' have "Lcm A \ 0" by simp haftmann@58023: } haftmann@58023: ultimately show "Lcm A = 0 \ 0 \ A" by blast haftmann@58023: qed haftmann@58023: haftmann@58023: lemma Lcm_no_multiple: haftmann@60430: "(\m. m \ 0 \ (\a\A. \a dvd m)) \ Lcm A = 0" haftmann@58023: proof - haftmann@60430: assume "\m. m \ 0 \ (\a\A. \a dvd m)" haftmann@60430: hence "\(\l. l \ 0 \ (\a\A. a dvd l))" by blast haftmann@58023: then show "Lcm A = 0" by (simp only: Lcm_Lcm_eucl Lcm_eucl_def if_False) haftmann@58023: qed haftmann@58023: haftmann@58023: lemma Lcm_insert [simp]: haftmann@58023: "Lcm (insert a A) = lcm a (Lcm A)" haftmann@58023: proof (rule lcmI) haftmann@58023: fix l assume "a dvd l" and "Lcm A dvd l" haftmann@60430: hence "\a\A. a dvd l" by (blast intro: dvd_trans dvd_Lcm) haftmann@60634: with \a dvd l\ show "Lcm (insert a A) dvd l" by (force intro: Lcm_least) haftmann@60634: qed (auto intro: Lcm_least dvd_Lcm) haftmann@58023: haftmann@58023: lemma Lcm_finite: haftmann@58023: assumes "finite A" haftmann@58023: shows "Lcm A = Finite_Set.fold lcm 1 A" wenzelm@60526: by (induct rule: finite.induct[OF \finite A\]) haftmann@58023: (simp_all add: comp_fun_idem.fold_insert_idem[OF comp_fun_idem_lcm]) haftmann@58023: haftmann@60431: lemma Lcm_set [code_unfold]: haftmann@58023: "Lcm (set xs) = fold lcm xs 1" haftmann@58023: using comp_fun_idem.fold_set_fold[OF comp_fun_idem_lcm] Lcm_finite by (simp add: ac_simps) haftmann@58023: haftmann@58023: lemma Lcm_singleton [simp]: haftmann@60634: "Lcm {a} = normalize a" haftmann@58023: by simp haftmann@58023: haftmann@58023: lemma Lcm_2 [simp]: haftmann@58023: "Lcm {a,b} = lcm a b" haftmann@60634: by simp haftmann@58023: haftmann@58023: lemma Lcm_coprime: haftmann@58023: assumes "finite A" and "A \ {}" haftmann@58023: assumes "\a b. a \ A \ b \ A \ a \ b \ gcd a b = 1" haftmann@60634: shows "Lcm A = normalize (\A)" haftmann@58023: using assms proof (induct rule: finite_ne_induct) haftmann@58023: case (insert a A) haftmann@58023: have "Lcm (insert a A) = lcm a (Lcm A)" by simp haftmann@60634: also from insert have "Lcm A = normalize (\A)" by blast haftmann@58023: also have "lcm a \ = lcm a (\A)" by (cases "\A = 0") (simp_all add: lcm_div_unit2) haftmann@58023: also from insert have "gcd a (\A) = 1" by (subst gcd.commute, intro setprod_coprime) auto haftmann@60634: with insert have "lcm a (\A) = normalize (\(insert a A))" haftmann@58023: by (simp add: lcm_coprime) haftmann@58023: finally show ?case . haftmann@58023: qed simp haftmann@58023: haftmann@58023: lemma Lcm_coprime': haftmann@58023: "card A \ 0 \ (\a b. a \ A \ b \ A \ a \ b \ gcd a b = 1) haftmann@60634: \ Lcm A = normalize (\A)" haftmann@58023: by (rule Lcm_coprime) (simp_all add: card_eq_0_iff) haftmann@58023: haftmann@58023: lemma Gcd_Lcm: haftmann@60430: "Gcd A = Lcm {d. \a\A. d dvd a}" haftmann@58023: by (simp add: Gcd_Gcd_eucl Lcm_Lcm_eucl Gcd_eucl_def) haftmann@58023: haftmann@60430: lemma Gcd_dvd [simp]: "a \ A \ Gcd A dvd a" haftmann@60634: and Gcd_greatest: "(\a. a \ A \ b dvd a) \ b dvd Gcd A" haftmann@60634: and unit_factor_Gcd [simp]: haftmann@60634: "unit_factor (Gcd A) = (if Gcd A = 0 then 0 else 1)" haftmann@58023: proof - haftmann@60430: fix a assume "a \ A" haftmann@60634: hence "Lcm {d. \a\A. d dvd a} dvd a" by (intro Lcm_least) blast haftmann@60430: then show "Gcd A dvd a" by (simp add: Gcd_Lcm) haftmann@58023: next haftmann@60634: fix g' assume "\a. a \ A \ g' dvd a" haftmann@60430: hence "g' dvd Lcm {d. \a\A. d dvd a}" by (intro dvd_Lcm) blast haftmann@58023: then show "g' dvd Gcd A" by (simp add: Gcd_Lcm) haftmann@58023: next haftmann@60634: show "unit_factor (Gcd A) = (if Gcd A = 0 then 0 else 1)" haftmann@59009: by (simp add: Gcd_Lcm) haftmann@58023: qed haftmann@58023: haftmann@60634: lemma normalize_Gcd [simp]: haftmann@60634: "normalize (Gcd A) = Gcd A" haftmann@60634: by (cases "Gcd A = 0") (auto intro: associated_eqI) haftmann@60634: haftmann@58023: lemma GcdI: haftmann@60634: assumes "\a. a \ A \ b dvd a" and "\c. (\a. a \ A \ c dvd a) \ c dvd b" haftmann@60634: and "unit_factor b = (if b = 0 then 0 else 1)" haftmann@60634: shows "b = Gcd A" haftmann@60634: by (rule associated_eqI) (auto simp: assms associated_def intro: Gcd_greatest) haftmann@58023: haftmann@58023: lemma Lcm_Gcd: haftmann@60430: "Lcm A = Gcd {m. \a\A. a dvd m}" haftmann@60634: by (rule LcmI[symmetric]) (auto intro: dvd_Gcd Gcd_greatest) haftmann@58023: haftmann@58023: lemma Gcd_0_iff: haftmann@58023: "Gcd A = 0 \ A \ {0}" haftmann@58023: apply (rule iffI) haftmann@58023: apply (rule subsetI, drule Gcd_dvd, simp) haftmann@58023: apply (auto intro: GcdI[symmetric]) haftmann@58023: done haftmann@58023: haftmann@58023: lemma Gcd_empty [simp]: haftmann@58023: "Gcd {} = 0" haftmann@58023: by (simp add: Gcd_0_iff) haftmann@58023: haftmann@58023: lemma Gcd_1: haftmann@58023: "1 \ A \ Gcd A = 1" haftmann@58023: by (intro GcdI[symmetric]) (auto intro: Gcd_dvd dvd_Gcd) haftmann@58023: haftmann@58023: lemma Gcd_insert [simp]: haftmann@58023: "Gcd (insert a A) = gcd a (Gcd A)" haftmann@58023: proof (rule gcdI) haftmann@58023: fix l assume "l dvd a" and "l dvd Gcd A" haftmann@60430: hence "\a\A. l dvd a" by (blast intro: dvd_trans Gcd_dvd) haftmann@60634: with \l dvd a\ show "l dvd Gcd (insert a A)" by (force intro: Gcd_dvd Gcd_greatest) haftmann@60634: qed (auto intro: Gcd_greatest) haftmann@58023: haftmann@58023: lemma Gcd_finite: haftmann@58023: assumes "finite A" haftmann@58023: shows "Gcd A = Finite_Set.fold gcd 0 A" wenzelm@60526: by (induct rule: finite.induct[OF \finite A\]) haftmann@58023: (simp_all add: comp_fun_idem.fold_insert_idem[OF comp_fun_idem_gcd]) haftmann@58023: haftmann@60431: lemma Gcd_set [code_unfold]: haftmann@58023: "Gcd (set xs) = fold gcd xs 0" haftmann@58023: using comp_fun_idem.fold_set_fold[OF comp_fun_idem_gcd] Gcd_finite by (simp add: ac_simps) haftmann@58023: haftmann@60634: lemma Gcd_singleton [simp]: "Gcd {a} = normalize a" haftmann@58023: by (simp add: gcd_0) haftmann@58023: haftmann@58023: lemma Gcd_2 [simp]: "Gcd {a,b} = gcd a b" haftmann@60634: by (simp add: gcd_0) haftmann@58023: haftmann@60439: subclass semiring_gcd haftmann@60439: by unfold_locales (simp_all add: gcd_greatest_iff) haftmann@60439: haftmann@58023: end haftmann@58023: wenzelm@60526: text \ haftmann@58023: A Euclidean ring is a Euclidean semiring with additive inverses. It provides a haftmann@58023: few more lemmas; in particular, Bezout's lemma holds for any Euclidean ring. wenzelm@60526: \ haftmann@58023: haftmann@58023: class euclidean_ring_gcd = euclidean_semiring_gcd + idom haftmann@58023: begin haftmann@58023: haftmann@58023: subclass euclidean_ring .. haftmann@58023: haftmann@60439: subclass ring_gcd .. haftmann@60439: haftmann@60572: lemma euclid_ext_gcd [simp]: haftmann@60572: "(case euclid_ext a b of (_, _ , t) \ t) = gcd a b" haftmann@60572: by (induct a b rule: gcd_eucl_induct) haftmann@60572: (simp_all add: euclid_ext_0 gcd_0 euclid_ext_non_0 ac_simps split: prod.split prod.split_asm) haftmann@60572: haftmann@60572: lemma euclid_ext_gcd' [simp]: haftmann@60572: "euclid_ext a b = (r, s, t) \ t = gcd a b" haftmann@60572: by (insert euclid_ext_gcd[of a b], drule (1) subst, simp) haftmann@60572: haftmann@60572: lemma euclid_ext'_correct: haftmann@60572: "fst (euclid_ext' a b) * a + snd (euclid_ext' a b) * b = gcd a b" haftmann@60572: proof- haftmann@60572: obtain s t c where "euclid_ext a b = (s,t,c)" haftmann@60572: by (cases "euclid_ext a b", blast) haftmann@60572: with euclid_ext_correct[of a b] euclid_ext_gcd[of a b] haftmann@60572: show ?thesis unfolding euclid_ext'_def by simp haftmann@60572: qed haftmann@60572: haftmann@60572: lemma bezout: "\s t. s * a + t * b = gcd a b" haftmann@60572: using euclid_ext'_correct by blast haftmann@60572: haftmann@58023: lemma gcd_neg1 [simp]: haftmann@60430: "gcd (-a) b = gcd a b" haftmann@59009: by (rule sym, rule gcdI, simp_all add: gcd_greatest) haftmann@58023: haftmann@58023: lemma gcd_neg2 [simp]: haftmann@60430: "gcd a (-b) = gcd a b" haftmann@59009: by (rule sym, rule gcdI, simp_all add: gcd_greatest) haftmann@58023: haftmann@58023: lemma gcd_neg_numeral_1 [simp]: haftmann@60430: "gcd (- numeral n) a = gcd (numeral n) a" haftmann@58023: by (fact gcd_neg1) haftmann@58023: haftmann@58023: lemma gcd_neg_numeral_2 [simp]: haftmann@60430: "gcd a (- numeral n) = gcd a (numeral n)" haftmann@58023: by (fact gcd_neg2) haftmann@58023: haftmann@58023: lemma gcd_diff1: "gcd (m - n) n = gcd m n" haftmann@58023: by (subst diff_conv_add_uminus, subst gcd_neg2[symmetric], subst gcd_add1, simp) haftmann@58023: haftmann@58023: lemma gcd_diff2: "gcd (n - m) n = gcd m n" haftmann@58023: by (subst gcd_neg1[symmetric], simp only: minus_diff_eq gcd_diff1) haftmann@58023: haftmann@58023: lemma coprime_minus_one [simp]: "gcd (n - 1) n = 1" haftmann@58023: proof - haftmann@58023: have "gcd (n - 1) n = gcd n (n - 1)" by (fact gcd.commute) haftmann@58023: also have "\ = gcd ((n - 1) + 1) (n - 1)" by simp haftmann@58023: also have "\ = 1" by (rule coprime_plus_one) haftmann@58023: finally show ?thesis . haftmann@58023: qed haftmann@58023: haftmann@60430: lemma lcm_neg1 [simp]: "lcm (-a) b = lcm a b" haftmann@58023: by (rule sym, rule lcmI, simp_all add: lcm_least lcm_zero) haftmann@58023: haftmann@60430: lemma lcm_neg2 [simp]: "lcm a (-b) = lcm a b" haftmann@58023: by (rule sym, rule lcmI, simp_all add: lcm_least lcm_zero) haftmann@58023: haftmann@60430: lemma lcm_neg_numeral_1 [simp]: "lcm (- numeral n) a = lcm (numeral n) a" haftmann@58023: by (fact lcm_neg1) haftmann@58023: haftmann@60430: lemma lcm_neg_numeral_2 [simp]: "lcm a (- numeral n) = lcm a (numeral n)" haftmann@58023: by (fact lcm_neg2) haftmann@58023: haftmann@60572: end haftmann@58023: haftmann@58023: haftmann@60572: subsection \Typical instances\ haftmann@58023: haftmann@58023: instantiation nat :: euclidean_semiring haftmann@58023: begin haftmann@58023: haftmann@58023: definition [simp]: haftmann@58023: "euclidean_size_nat = (id :: nat \ nat)" haftmann@58023: haftmann@58023: definition [simp]: haftmann@60634: "unit_factor_nat (n::nat) = (if n = 0 then 0 else 1 :: nat)" haftmann@58023: haftmann@58023: instance proof haftmann@59061: qed simp_all haftmann@58023: haftmann@58023: end haftmann@58023: haftmann@58023: instantiation int :: euclidean_ring haftmann@58023: begin haftmann@58023: haftmann@58023: definition [simp]: haftmann@58023: "euclidean_size_int = (nat \ abs :: int \ nat)" haftmann@58023: haftmann@58023: definition [simp]: haftmann@60634: "unit_factor_int = (sgn :: int \ int)" haftmann@58023: wenzelm@60580: instance haftmann@60634: by standard (auto simp add: abs_mult nat_mult_distrib sgn_times split: abs_split) haftmann@58023: haftmann@58023: end haftmann@58023: haftmann@60572: instantiation poly :: (field) euclidean_ring haftmann@60571: begin haftmann@60571: haftmann@60571: definition euclidean_size_poly :: "'a poly \ nat" haftmann@60600: where "euclidean_size p = (if p = 0 then 0 else Suc (degree p))" haftmann@60571: haftmann@60634: lemma euclidenan_size_poly_minus_one_degree [simp]: haftmann@60634: "euclidean_size p - 1 = degree p" haftmann@60634: by (simp add: euclidean_size_poly_def) haftmann@60571: haftmann@60600: lemma euclidean_size_poly_0 [simp]: haftmann@60600: "euclidean_size (0::'a poly) = 0" haftmann@60600: by (simp add: euclidean_size_poly_def) haftmann@60600: haftmann@60600: lemma euclidean_size_poly_not_0 [simp]: haftmann@60600: "p \ 0 \ euclidean_size p = Suc (degree p)" haftmann@60600: by (simp add: euclidean_size_poly_def) haftmann@60600: haftmann@60571: instance haftmann@60600: proof haftmann@60571: fix p q :: "'a poly" haftmann@60600: assume "q \ 0" haftmann@60600: then have "p mod q = 0 \ degree (p mod q) < degree q" haftmann@60600: by (rule degree_mod_less [of q p]) haftmann@60600: with \q \ 0\ show "euclidean_size (p mod q) < euclidean_size q" haftmann@60600: by (cases "p mod q = 0") simp_all haftmann@60571: next haftmann@60571: fix p q :: "'a poly" haftmann@60571: assume "q \ 0" haftmann@60600: from \q \ 0\ have "degree p \ degree (p * q)" haftmann@60571: by (rule degree_mult_right_le) haftmann@60600: with \q \ 0\ show "euclidean_size p \ euclidean_size (p * q)" haftmann@60600: by (cases "p = 0") simp_all haftmann@60571: qed haftmann@60571: haftmann@58023: end haftmann@60571: haftmann@60571: end