src/HOL/Number_Theory/Euclidean_Algorithm.thy
 author haftmann Mon Nov 17 14:55:34 2014 +0100 (2014-11-17) changeset 59010 ec2b4270a502 parent 59009 348561aa3869 child 59061 67771d267ff2 permissions -rw-r--r--
generalized lemmas and tuned proofs
 haftmann@58023  1 (* Author: Manuel Eberl *)  haftmann@58023  2 wenzelm@58889  3 section {* Abstract euclidean algorithm *}  haftmann@58023  4 haftmann@58023  5 theory Euclidean_Algorithm  haftmann@58023  6 imports Complex_Main  haftmann@58023  7 begin  haftmann@58023  8 haftmann@58023  9 context semiring_div  haftmann@58023  10 begin  haftmann@58023  11 haftmann@58023  12 definition ring_inv :: "'a \ 'a"  haftmann@58023  13 where  haftmann@58023  14  "ring_inv x = 1 div x"  haftmann@58023  15 haftmann@58023  16 definition is_unit :: "'a \ bool"  haftmann@58023  17 where  haftmann@58023  18  "is_unit x \ x dvd 1"  haftmann@58023  19 haftmann@58023  20 definition associated :: "'a \ 'a \ bool"  haftmann@58023  21 where  haftmann@58023  22  "associated x y \ x dvd y \ y dvd x"  haftmann@58023  23 haftmann@58023  24 lemma unit_prod [intro]:  haftmann@58023  25  "is_unit x \ is_unit y \ is_unit (x * y)"  haftmann@58023  26  unfolding is_unit_def by (subst mult_1_left [of 1, symmetric], rule mult_dvd_mono)  haftmann@58023  27 haftmann@58023  28 lemma unit_ring_inv:  haftmann@58023  29  "is_unit y \ x div y = x * ring_inv y"  haftmann@58023  30  by (simp add: div_mult_swap ring_inv_def is_unit_def)  haftmann@58023  31 haftmann@58023  32 lemma unit_ring_inv_ring_inv [simp]:  haftmann@58023  33  "is_unit x \ ring_inv (ring_inv x) = x"  haftmann@58023  34  unfolding is_unit_def ring_inv_def  haftmann@58023  35  by (metis div_mult_mult1_if div_mult_self1_is_id dvd_mult_div_cancel mult_1_right)  haftmann@58023  36 haftmann@58023  37 lemma inv_imp_eq_ring_inv:  haftmann@58023  38  "a * b = 1 \ ring_inv a = b"  haftmann@58023  39  by (metis dvd_mult_div_cancel dvd_mult_right mult_1_right mult.left_commute one_dvd ring_inv_def)  haftmann@58023  40 haftmann@58023  41 lemma ring_inv_is_inv1 [simp]:  haftmann@58023  42  "is_unit a \ a * ring_inv a = 1"  haftmann@58953  43  unfolding is_unit_def ring_inv_def by simp  haftmann@58023  44 haftmann@58023  45 lemma ring_inv_is_inv2 [simp]:  haftmann@58023  46  "is_unit a \ ring_inv a * a = 1"  haftmann@58023  47  by (simp add: ac_simps)  haftmann@58023  48 haftmann@58023  49 lemma unit_ring_inv_unit [simp, intro]:  haftmann@58023  50  assumes "is_unit x"  haftmann@58023  51  shows "is_unit (ring_inv x)"  haftmann@58023  52 proof -  haftmann@58023  53  from assms have "1 = ring_inv x * x" by simp  haftmann@58023  54  then show "is_unit (ring_inv x)" unfolding is_unit_def by (rule dvdI)  haftmann@58023  55 qed  haftmann@58023  56 haftmann@58023  57 lemma mult_unit_dvd_iff:  haftmann@58023  58  "is_unit y \ x * y dvd z \ x dvd z"  haftmann@58023  59 proof  haftmann@58023  60  assume "is_unit y" "x * y dvd z"  haftmann@58023  61  then show "x dvd z" by (simp add: dvd_mult_left)  haftmann@58023  62 next  haftmann@58023  63  assume "is_unit y" "x dvd z"  haftmann@58023  64  then obtain k where "z = x * k" unfolding dvd_def by blast  haftmann@58023  65  with is_unit y have "z = (x * y) * (ring_inv y * k)"  haftmann@58023  66  by (simp add: mult_ac)  haftmann@58023  67  then show "x * y dvd z" by (rule dvdI)  haftmann@58023  68 qed  haftmann@58023  69 haftmann@58023  70 lemma div_unit_dvd_iff:  haftmann@58023  71  "is_unit y \ x div y dvd z \ x dvd z"  haftmann@58023  72  by (subst unit_ring_inv) (assumption, simp add: mult_unit_dvd_iff)  haftmann@58023  73 haftmann@58023  74 lemma dvd_mult_unit_iff:  haftmann@58023  75  "is_unit y \ x dvd z * y \ x dvd z"  haftmann@58023  76 proof  haftmann@58023  77  assume "is_unit y" and "x dvd z * y"  haftmann@58023  78  have "z * y dvd z * (y * ring_inv y)" by (subst mult_assoc [symmetric]) simp  haftmann@58023  79  also from is_unit y have "y * ring_inv y = 1" by simp  haftmann@58023  80  finally have "z * y dvd z" by simp  haftmann@58023  81  with x dvd z * y show "x dvd z" by (rule dvd_trans)  haftmann@58023  82 next  haftmann@58023  83  assume "x dvd z"  haftmann@58023  84  then show "x dvd z * y" by simp  haftmann@58023  85 qed  haftmann@58023  86 haftmann@58023  87 lemma dvd_div_unit_iff:  haftmann@58023  88  "is_unit y \ x dvd z div y \ x dvd z"  haftmann@58023  89  by (subst unit_ring_inv) (assumption, simp add: dvd_mult_unit_iff)  haftmann@58023  90 haftmann@58023  91 lemmas unit_dvd_iff = mult_unit_dvd_iff div_unit_dvd_iff dvd_mult_unit_iff dvd_div_unit_iff  haftmann@58023  92 haftmann@58023  93 lemma unit_div [intro]:  haftmann@58023  94  "is_unit x \ is_unit y \ is_unit (x div y)"  haftmann@58023  95  by (subst unit_ring_inv) (assumption, rule unit_prod, simp_all)  haftmann@58023  96 haftmann@58023  97 lemma unit_div_mult_swap:  haftmann@58023  98  "is_unit z \ x * (y div z) = x * y div z"  haftmann@58023  99  by (simp only: unit_ring_inv [of _ y] unit_ring_inv [of _ "x*y"] ac_simps)  haftmann@58023  100 haftmann@58023  101 lemma unit_div_commute:  haftmann@58023  102  "is_unit y \ x div y * z = x * z div y"  haftmann@58023  103  by (simp only: unit_ring_inv [of _ x] unit_ring_inv [of _ "x*z"] ac_simps)  haftmann@58023  104 haftmann@58023  105 lemma unit_imp_dvd [dest]:  haftmann@58023  106  "is_unit y \ y dvd x"  haftmann@58023  107  by (rule dvd_trans [of _ 1]) (simp_all add: is_unit_def)  haftmann@58023  108 haftmann@58023  109 lemma dvd_unit_imp_unit:  haftmann@58023  110  "is_unit y \ x dvd y \ is_unit x"  haftmann@58023  111  by (unfold is_unit_def) (rule dvd_trans)  haftmann@58023  112 haftmann@58023  113 lemma ring_inv_0 [simp]:  haftmann@58023  114  "ring_inv 0 = 0"  haftmann@58023  115  unfolding ring_inv_def by simp  haftmann@58023  116 haftmann@58023  117 lemma unit_ring_inv'1:  haftmann@58023  118  assumes "is_unit y"  haftmann@58023  119  shows "x div (y * z) = x * ring_inv y div z"  haftmann@58023  120 proof -  haftmann@58023  121  from assms have "x div (y * z) = x * (ring_inv y * y) div (y * z)"  haftmann@58023  122  by simp  haftmann@58023  123  also have "... = y * (x * ring_inv y) div (y * z)"  haftmann@58023  124  by (simp only: mult_ac)  haftmann@58023  125  also have "... = x * ring_inv y div z"  haftmann@58023  126  by (cases "y = 0", simp, rule div_mult_mult1)  haftmann@58023  127  finally show ?thesis .  haftmann@58023  128 qed  haftmann@58023  129 haftmann@58023  130 lemma associated_comm:  haftmann@58023  131  "associated x y \ associated y x"  haftmann@58023  132  by (simp add: associated_def)  haftmann@58023  133 haftmann@58023  134 lemma associated_0 [simp]:  haftmann@58023  135  "associated 0 b \ b = 0"  haftmann@58023  136  "associated a 0 \ a = 0"  haftmann@58023  137  unfolding associated_def by simp_all  haftmann@58023  138 haftmann@58023  139 lemma associated_unit:  haftmann@58023  140  "is_unit x \ associated x y \ is_unit y"  haftmann@58023  141  unfolding associated_def by (fast dest: dvd_unit_imp_unit)  haftmann@58023  142 haftmann@58023  143 lemma is_unit_1 [simp]:  haftmann@58023  144  "is_unit 1"  haftmann@58023  145  unfolding is_unit_def by simp  haftmann@58023  146 haftmann@58023  147 lemma not_is_unit_0 [simp]:  haftmann@58023  148  "\ is_unit 0"  haftmann@58023  149  unfolding is_unit_def by auto  haftmann@58023  150 haftmann@58023  151 lemma unit_mult_left_cancel:  haftmann@58023  152  assumes "is_unit x"  haftmann@58023  153  shows "(x * y) = (x * z) \ y = z"  haftmann@58023  154 proof -  haftmann@58023  155  from assms have "x \ 0" by auto  haftmann@58023  156  then show ?thesis by (metis div_mult_self1_is_id)  haftmann@58023  157 qed  haftmann@58023  158 haftmann@58023  159 lemma unit_mult_right_cancel:  haftmann@58023  160  "is_unit x \ (y * x) = (z * x) \ y = z"  haftmann@58023  161  by (simp add: ac_simps unit_mult_left_cancel)  haftmann@58023  162 haftmann@58023  163 lemma unit_div_cancel:  haftmann@58023  164  "is_unit x \ (y div x) = (z div x) \ y = z"  haftmann@58023  165  apply (subst unit_ring_inv[of _ y], assumption)  haftmann@58023  166  apply (subst unit_ring_inv[of _ z], assumption)  haftmann@58023  167  apply (rule unit_mult_right_cancel, erule unit_ring_inv_unit)  haftmann@58023  168  done  haftmann@58023  169 haftmann@58023  170 lemma unit_eq_div1:  haftmann@58023  171  "is_unit y \ x div y = z \ x = z * y"  haftmann@58023  172  apply (subst unit_ring_inv, assumption)  haftmann@58023  173  apply (subst unit_mult_right_cancel[symmetric], assumption)  haftmann@58023  174  apply (subst mult_assoc, subst ring_inv_is_inv2, assumption, simp)  haftmann@58023  175  done  haftmann@58023  176 haftmann@58023  177 lemma unit_eq_div2:  haftmann@58023  178  "is_unit y \ x = z div y \ x * y = z"  haftmann@58023  179  by (subst (1 2) eq_commute, simp add: unit_eq_div1, subst eq_commute, rule refl)  haftmann@58023  180 haftmann@58023  181 lemma associated_iff_div_unit:  haftmann@58023  182  "associated x y \ (\z. is_unit z \ x = z * y)"  haftmann@58023  183 proof  haftmann@58023  184  assume "associated x y"  haftmann@58023  185  show "\z. is_unit z \ x = z * y"  haftmann@58023  186  proof (cases "x = 0")  haftmann@58023  187  assume "x = 0"  haftmann@58023  188  then show "\z. is_unit z \ x = z * y" using associated x y  haftmann@58023  189  by (intro exI[of _ 1], simp add: associated_def)  haftmann@58023  190  next  haftmann@58023  191  assume [simp]: "x \ 0"  haftmann@58023  192  hence [simp]: "x dvd y" "y dvd x" using associated x y  haftmann@58023  193  unfolding associated_def by simp_all  haftmann@58023  194  hence "1 = x div y * (y div x)"  haftmann@59009  195  by (simp add: div_mult_swap)  haftmann@58023  196  hence "is_unit (x div y)" unfolding is_unit_def by (rule dvdI)  haftmann@59009  197  moreover have "x = (x div y) * y" by simp  haftmann@58023  198  ultimately show ?thesis by blast  haftmann@58023  199  qed  haftmann@58023  200 next  haftmann@58023  201  assume "\z. is_unit z \ x = z * y"  haftmann@58023  202  then obtain z where "is_unit z" and "x = z * y" by blast  haftmann@58023  203  hence "y = x * ring_inv z" by (simp add: algebra_simps)  haftmann@58023  204  hence "x dvd y" by simp  haftmann@58023  205  moreover from x = z * y have "y dvd x" by simp  haftmann@58023  206  ultimately show "associated x y" unfolding associated_def by simp  haftmann@58023  207 qed  haftmann@58023  208 haftmann@58023  209 lemmas unit_simps = mult_unit_dvd_iff div_unit_dvd_iff dvd_mult_unit_iff  haftmann@58023  210  dvd_div_unit_iff unit_div_mult_swap unit_div_commute  haftmann@58023  211  unit_mult_left_cancel unit_mult_right_cancel unit_div_cancel  haftmann@58023  212  unit_eq_div1 unit_eq_div2  haftmann@58023  213 haftmann@58023  214 end  haftmann@58023  215 haftmann@58023  216 context ring_div  haftmann@58023  217 begin  haftmann@58023  218 haftmann@58023  219 lemma is_unit_neg [simp]:  haftmann@58023  220  "is_unit (- x) \ is_unit x"  haftmann@58023  221  unfolding is_unit_def by simp  haftmann@58023  222 haftmann@58023  223 lemma is_unit_neg_1 [simp]:  haftmann@58023  224  "is_unit (-1)"  haftmann@58023  225  unfolding is_unit_def by simp  haftmann@58023  226 haftmann@58023  227 end  haftmann@58023  228 haftmann@58023  229 lemma is_unit_nat [simp]:  haftmann@58023  230  "is_unit (x::nat) \ x = 1"  haftmann@58023  231  unfolding is_unit_def by simp  haftmann@58023  232 haftmann@58023  233 lemma is_unit_int:  haftmann@58023  234  "is_unit (x::int) \ x = 1 \ x = -1"  haftmann@58023  235  unfolding is_unit_def by auto  haftmann@58023  236 haftmann@58023  237 text {*  haftmann@58023  238  A Euclidean semiring is a semiring upon which the Euclidean algorithm can be  haftmann@58023  239  implemented. It must provide:  haftmann@58023  240  \begin{itemize}  haftmann@58023  241  \item division with remainder  haftmann@58023  242  \item a size function such that @{term "size (a mod b) < size b"}  haftmann@58023  243  for any @{term "b \ 0"}  haftmann@58023  244  \item a normalisation factor such that two associated numbers are equal iff  haftmann@58023  245  they are the same when divided by their normalisation factors.  haftmann@58023  246  \end{itemize}  haftmann@58023  247  The existence of these functions makes it possible to derive gcd and lcm functions  haftmann@58023  248  for any Euclidean semiring.  haftmann@58023  249 *}  haftmann@58023  250 class euclidean_semiring = semiring_div +  haftmann@58023  251  fixes euclidean_size :: "'a \ nat"  haftmann@58023  252  fixes normalisation_factor :: "'a \ 'a"  haftmann@58023  253  assumes mod_size_less [simp]:  haftmann@58023  254  "b \ 0 \ euclidean_size (a mod b) < euclidean_size b"  haftmann@58023  255  assumes size_mult_mono:  haftmann@58023  256  "b \ 0 \ euclidean_size (a * b) \ euclidean_size a"  haftmann@58023  257  assumes normalisation_factor_is_unit [intro,simp]:  haftmann@58023  258  "a \ 0 \ is_unit (normalisation_factor a)"  haftmann@58023  259  assumes normalisation_factor_mult: "normalisation_factor (a * b) =  haftmann@58023  260  normalisation_factor a * normalisation_factor b"  haftmann@58023  261  assumes normalisation_factor_unit: "is_unit x \ normalisation_factor x = x"  haftmann@58023  262  assumes normalisation_factor_0 [simp]: "normalisation_factor 0 = 0"  haftmann@58023  263 begin  haftmann@58023  264 haftmann@58023  265 lemma normalisation_factor_dvd [simp]:  haftmann@58023  266  "a \ 0 \ normalisation_factor a dvd b"  haftmann@58023  267  by (rule unit_imp_dvd, simp)  haftmann@58023  268   haftmann@58023  269 lemma normalisation_factor_1 [simp]:  haftmann@58023  270  "normalisation_factor 1 = 1"  haftmann@58023  271  by (simp add: normalisation_factor_unit)  haftmann@58023  272 haftmann@58023  273 lemma normalisation_factor_0_iff [simp]:  haftmann@58023  274  "normalisation_factor x = 0 \ x = 0"  haftmann@58023  275 proof  haftmann@58023  276  assume "normalisation_factor x = 0"  haftmann@58023  277  hence "\ is_unit (normalisation_factor x)"  haftmann@58023  278  by (metis not_is_unit_0)  haftmann@58023  279  then show "x = 0" by force  haftmann@58023  280 next  haftmann@58023  281  assume "x = 0"  haftmann@58023  282  then show "normalisation_factor x = 0" by simp  haftmann@58023  283 qed  haftmann@58023  284 haftmann@58023  285 lemma normalisation_factor_pow:  haftmann@58023  286  "normalisation_factor (x ^ n) = normalisation_factor x ^ n"  haftmann@58023  287  by (induct n) (simp_all add: normalisation_factor_mult power_Suc2)  haftmann@58023  288 haftmann@58023  289 lemma normalisation_correct [simp]:  haftmann@58023  290  "normalisation_factor (x div normalisation_factor x) = (if x = 0 then 0 else 1)"  haftmann@58023  291 proof (cases "x = 0", simp)  haftmann@58023  292  assume "x \ 0"  haftmann@58023  293  let ?nf = "normalisation_factor"  haftmann@58023  294  from normalisation_factor_is_unit[OF x \ 0] have "?nf x \ 0"  haftmann@58023  295  by (metis not_is_unit_0)  haftmann@58023  296  have "?nf (x div ?nf x) * ?nf (?nf x) = ?nf (x div ?nf x * ?nf x)"  haftmann@58023  297  by (simp add: normalisation_factor_mult)  haftmann@58023  298  also have "x div ?nf x * ?nf x = x" using x \ 0  haftmann@59009  299  by simp  haftmann@58023  300  also have "?nf (?nf x) = ?nf x" using x \ 0  haftmann@58023  301  normalisation_factor_is_unit normalisation_factor_unit by simp  haftmann@58023  302  finally show ?thesis using x \ 0 and ?nf x \ 0  haftmann@58023  303  by (metis div_mult_self2_is_id div_self)  haftmann@58023  304 qed  haftmann@58023  305 haftmann@58023  306 lemma normalisation_0_iff [simp]:  haftmann@58023  307  "x div normalisation_factor x = 0 \ x = 0"  haftmann@58023  308  by (cases "x = 0", simp, subst unit_eq_div1, blast, simp)  haftmann@58023  309 haftmann@58023  310 lemma associated_iff_normed_eq:  haftmann@58023  311  "associated a b \ a div normalisation_factor a = b div normalisation_factor b"  haftmann@58023  312 proof (cases "b = 0", simp, cases "a = 0", metis associated_0(1) normalisation_0_iff, rule iffI)  haftmann@58023  313  let ?nf = normalisation_factor  haftmann@58023  314  assume "a \ 0" "b \ 0" "a div ?nf a = b div ?nf b"  haftmann@58023  315  hence "a = b * (?nf a div ?nf b)"  haftmann@58023  316  apply (subst (asm) unit_eq_div1, blast, subst (asm) unit_div_commute, blast)  haftmann@58023  317  apply (subst div_mult_swap, simp, simp)  haftmann@58023  318  done  haftmann@58023  319  with a \ 0 b \ 0 have "\z. is_unit z \ a = z * b"  haftmann@58023  320  by (intro exI[of _ "?nf a div ?nf b"], force simp: mult_ac)  haftmann@58023  321  with associated_iff_div_unit show "associated a b" by simp  haftmann@58023  322 next  haftmann@58023  323  let ?nf = normalisation_factor  haftmann@58023  324  assume "a \ 0" "b \ 0" "associated a b"  haftmann@58023  325  with associated_iff_div_unit obtain z where "is_unit z" and "a = z * b" by blast  haftmann@58023  326  then show "a div ?nf a = b div ?nf b"  haftmann@58023  327  apply (simp only: a = z * b normalisation_factor_mult normalisation_factor_unit)  haftmann@58023  328  apply (rule div_mult_mult1, force)  haftmann@58023  329  done  haftmann@58023  330  qed  haftmann@58023  331 haftmann@58023  332 lemma normed_associated_imp_eq:  haftmann@58023  333  "associated a b \ normalisation_factor a \ {0, 1} \ normalisation_factor b \ {0, 1} \ a = b"  haftmann@58023  334  by (simp add: associated_iff_normed_eq, elim disjE, simp_all)  haftmann@58023  335   haftmann@58023  336 lemmas normalisation_factor_dvd_iff [simp] =  haftmann@58023  337  unit_dvd_iff [OF normalisation_factor_is_unit]  haftmann@58023  338 haftmann@58023  339 lemma euclidean_division:  haftmann@58023  340  fixes a :: 'a and b :: 'a  haftmann@58023  341  assumes "b \ 0"  haftmann@58023  342  obtains s and t where "a = s * b + t"  haftmann@58023  343  and "euclidean_size t < euclidean_size b"  haftmann@58023  344 proof -  haftmann@58023  345  from div_mod_equality[of a b 0]  haftmann@58023  346  have "a = a div b * b + a mod b" by simp  haftmann@58023  347  with that and assms show ?thesis by force  haftmann@58023  348 qed  haftmann@58023  349 haftmann@58023  350 lemma dvd_euclidean_size_eq_imp_dvd:  haftmann@58023  351  assumes "a \ 0" and b_dvd_a: "b dvd a" and size_eq: "euclidean_size a = euclidean_size b"  haftmann@58023  352  shows "a dvd b"  haftmann@58023  353 proof (subst dvd_eq_mod_eq_0, rule ccontr)  haftmann@58023  354  assume "b mod a \ 0"  haftmann@58023  355  from b_dvd_a have b_dvd_mod: "b dvd b mod a" by (simp add: dvd_mod_iff)  haftmann@58023  356  from b_dvd_mod obtain c where "b mod a = b * c" unfolding dvd_def by blast  haftmann@58023  357  with b mod a \ 0 have "c \ 0" by auto  haftmann@58023  358  with b mod a = b * c have "euclidean_size (b mod a) \ euclidean_size b"  haftmann@58023  359  using size_mult_mono by force  haftmann@58023  360  moreover from a \ 0 have "euclidean_size (b mod a) < euclidean_size a"  haftmann@58023  361  using mod_size_less by blast  haftmann@58023  362  ultimately show False using size_eq by simp  haftmann@58023  363 qed  haftmann@58023  364 haftmann@58023  365 function gcd_eucl :: "'a \ 'a \ 'a"  haftmann@58023  366 where  haftmann@58023  367  "gcd_eucl a b = (if b = 0 then a div normalisation_factor a else gcd_eucl b (a mod b))"  haftmann@58023  368  by (pat_completeness, simp)  haftmann@58023  369 termination by (relation "measure (euclidean_size \ snd)", simp_all)  haftmann@58023  370 haftmann@58023  371 declare gcd_eucl.simps [simp del]  haftmann@58023  372 haftmann@58023  373 lemma gcd_induct: "\\b. P b 0; \a b. 0 \ b \ P b (a mod b) \ P a b\ \ P a b"  haftmann@58023  374 proof (induct a b rule: gcd_eucl.induct)  haftmann@58023  375  case ("1" m n)  haftmann@58023  376  then show ?case by (cases "n = 0") auto  haftmann@58023  377 qed  haftmann@58023  378 haftmann@58023  379 definition lcm_eucl :: "'a \ 'a \ 'a"  haftmann@58023  380 where  haftmann@58023  381  "lcm_eucl a b = a * b div (gcd_eucl a b * normalisation_factor (a * b))"  haftmann@58023  382 haftmann@58023  383  (* Somewhat complicated definition of Lcm that has the advantage of working  haftmann@58023  384  for infinite sets as well *)  haftmann@58023  385 haftmann@58023  386 definition Lcm_eucl :: "'a set \ 'a"  haftmann@58023  387 where  haftmann@58023  388  "Lcm_eucl A = (if \l. l \ 0 \ (\x\A. x dvd l) then  haftmann@58023  389  let l = SOME l. l \ 0 \ (\x\A. x dvd l) \ euclidean_size l =  haftmann@58023  390  (LEAST n. \l. l \ 0 \ (\x\A. x dvd l) \ euclidean_size l = n)  haftmann@58023  391  in l div normalisation_factor l  haftmann@58023  392  else 0)"  haftmann@58023  393 haftmann@58023  394 definition Gcd_eucl :: "'a set \ 'a"  haftmann@58023  395 where  haftmann@58023  396  "Gcd_eucl A = Lcm_eucl {d. \a\A. d dvd a}"  haftmann@58023  397 haftmann@58023  398 end  haftmann@58023  399 haftmann@58023  400 class euclidean_semiring_gcd = euclidean_semiring + gcd + Gcd +  haftmann@58023  401  assumes gcd_gcd_eucl: "gcd = gcd_eucl" and lcm_lcm_eucl: "lcm = lcm_eucl"  haftmann@58023  402  assumes Gcd_Gcd_eucl: "Gcd = Gcd_eucl" and Lcm_Lcm_eucl: "Lcm = Lcm_eucl"  haftmann@58023  403 begin  haftmann@58023  404 haftmann@58023  405 lemma gcd_red:  haftmann@58023  406  "gcd x y = gcd y (x mod y)"  haftmann@58023  407  by (metis gcd_eucl.simps mod_0 mod_by_0 gcd_gcd_eucl)  haftmann@58023  408 haftmann@58023  409 lemma gcd_non_0:  haftmann@58023  410  "y \ 0 \ gcd x y = gcd y (x mod y)"  haftmann@58023  411  by (rule gcd_red)  haftmann@58023  412 haftmann@58023  413 lemma gcd_0_left:  haftmann@58023  414  "gcd 0 x = x div normalisation_factor x"  haftmann@58023  415  by (simp only: gcd_gcd_eucl, subst gcd_eucl.simps, subst gcd_eucl.simps, simp add: Let_def)  haftmann@58023  416 haftmann@58023  417 lemma gcd_0:  haftmann@58023  418  "gcd x 0 = x div normalisation_factor x"  haftmann@58023  419  by (simp only: gcd_gcd_eucl, subst gcd_eucl.simps, simp add: Let_def)  haftmann@58023  420 haftmann@58023  421 lemma gcd_dvd1 [iff]: "gcd x y dvd x"  haftmann@58023  422  and gcd_dvd2 [iff]: "gcd x y dvd y"  haftmann@58023  423 proof (induct x y rule: gcd_eucl.induct)  haftmann@58023  424  fix x y :: 'a  haftmann@58023  425  assume IH1: "y \ 0 \ gcd y (x mod y) dvd y"  haftmann@58023  426  assume IH2: "y \ 0 \ gcd y (x mod y) dvd (x mod y)"  haftmann@58023  427   haftmann@58023  428  have "gcd x y dvd x \ gcd x y dvd y"  haftmann@58023  429  proof (cases "y = 0")  haftmann@58023  430  case True  haftmann@58023  431  then show ?thesis by (cases "x = 0", simp_all add: gcd_0)  haftmann@58023  432  next  haftmann@58023  433  case False  haftmann@58023  434  with IH1 and IH2 show ?thesis by (simp add: gcd_non_0 dvd_mod_iff)  haftmann@58023  435  qed  haftmann@58023  436  then show "gcd x y dvd x" "gcd x y dvd y" by simp_all  haftmann@58023  437 qed  haftmann@58023  438 haftmann@58023  439 lemma dvd_gcd_D1: "k dvd gcd m n \ k dvd m"  haftmann@58023  440  by (rule dvd_trans, assumption, rule gcd_dvd1)  haftmann@58023  441 haftmann@58023  442 lemma dvd_gcd_D2: "k dvd gcd m n \ k dvd n"  haftmann@58023  443  by (rule dvd_trans, assumption, rule gcd_dvd2)  haftmann@58023  444 haftmann@58023  445 lemma gcd_greatest:  haftmann@58023  446  fixes k x y :: 'a  haftmann@58023  447  shows "k dvd x \ k dvd y \ k dvd gcd x y"  haftmann@58023  448 proof (induct x y rule: gcd_eucl.induct)  haftmann@58023  449  case (1 x y)  haftmann@58023  450  show ?case  haftmann@58023  451  proof (cases "y = 0")  haftmann@58023  452  assume "y = 0"  haftmann@58023  453  with 1 show ?thesis by (cases "x = 0", simp_all add: gcd_0)  haftmann@58023  454  next  haftmann@58023  455  assume "y \ 0"  haftmann@58023  456  with 1 show ?thesis by (simp add: gcd_non_0 dvd_mod_iff)  haftmann@58023  457  qed  haftmann@58023  458 qed  haftmann@58023  459 haftmann@58023  460 lemma dvd_gcd_iff:  haftmann@58023  461  "k dvd gcd x y \ k dvd x \ k dvd y"  haftmann@58023  462  by (blast intro!: gcd_greatest intro: dvd_trans)  haftmann@58023  463 haftmann@58023  464 lemmas gcd_greatest_iff = dvd_gcd_iff  haftmann@58023  465 haftmann@58023  466 lemma gcd_zero [simp]:  haftmann@58023  467  "gcd x y = 0 \ x = 0 \ y = 0"  haftmann@58023  468  by (metis dvd_0_left dvd_refl gcd_dvd1 gcd_dvd2 gcd_greatest)+  haftmann@58023  469 haftmann@58023  470 lemma normalisation_factor_gcd [simp]:  haftmann@58023  471  "normalisation_factor (gcd x y) = (if x = 0 \ y = 0 then 0 else 1)" (is "?f x y = ?g x y")  haftmann@58023  472 proof (induct x y rule: gcd_eucl.induct)  haftmann@58023  473  fix x y :: 'a  haftmann@58023  474  assume IH: "y \ 0 \ ?f y (x mod y) = ?g y (x mod y)"  haftmann@58023  475  then show "?f x y = ?g x y" by (cases "y = 0", auto simp: gcd_non_0 gcd_0)  haftmann@58023  476 qed  haftmann@58023  477 haftmann@58023  478 lemma gcdI:  haftmann@58023  479  "k dvd x \ k dvd y \ (\l. l dvd x \ l dvd y \ l dvd k)  haftmann@58023  480  \ normalisation_factor k = (if k = 0 then 0 else 1) \ k = gcd x y"  haftmann@58023  481  by (intro normed_associated_imp_eq) (auto simp: associated_def intro: gcd_greatest)  haftmann@58023  482 haftmann@58023  483 sublocale gcd!: abel_semigroup gcd  haftmann@58023  484 proof  haftmann@58023  485  fix x y z  haftmann@58023  486  show "gcd (gcd x y) z = gcd x (gcd y z)"  haftmann@58023  487  proof (rule gcdI)  haftmann@58023  488  have "gcd (gcd x y) z dvd gcd x y" "gcd x y dvd x" by simp_all  haftmann@58023  489  then show "gcd (gcd x y) z dvd x" by (rule dvd_trans)  haftmann@58023  490  have "gcd (gcd x y) z dvd gcd x y" "gcd x y dvd y" by simp_all  haftmann@58023  491  hence "gcd (gcd x y) z dvd y" by (rule dvd_trans)  haftmann@58023  492  moreover have "gcd (gcd x y) z dvd z" by simp  haftmann@58023  493  ultimately show "gcd (gcd x y) z dvd gcd y z"  haftmann@58023  494  by (rule gcd_greatest)  haftmann@58023  495  show "normalisation_factor (gcd (gcd x y) z) = (if gcd (gcd x y) z = 0 then 0 else 1)"  haftmann@58023  496  by auto  haftmann@58023  497  fix l assume "l dvd x" and "l dvd gcd y z"  haftmann@58023  498  with dvd_trans[OF _ gcd_dvd1] and dvd_trans[OF _ gcd_dvd2]  haftmann@58023  499  have "l dvd y" and "l dvd z" by blast+  haftmann@58023  500  with l dvd x show "l dvd gcd (gcd x y) z"  haftmann@58023  501  by (intro gcd_greatest)  haftmann@58023  502  qed  haftmann@58023  503 next  haftmann@58023  504  fix x y  haftmann@58023  505  show "gcd x y = gcd y x"  haftmann@58023  506  by (rule gcdI) (simp_all add: gcd_greatest)  haftmann@58023  507 qed  haftmann@58023  508 haftmann@58023  509 lemma gcd_unique: "d dvd a \ d dvd b \  haftmann@58023  510  normalisation_factor d = (if d = 0 then 0 else 1) \  haftmann@58023  511  (\e. e dvd a \ e dvd b \ e dvd d) \ d = gcd a b"  haftmann@58023  512  by (rule, auto intro: gcdI simp: gcd_greatest)  haftmann@58023  513 haftmann@58023  514 lemma gcd_dvd_prod: "gcd a b dvd k * b"  haftmann@58023  515  using mult_dvd_mono [of 1] by auto  haftmann@58023  516 haftmann@58023  517 lemma gcd_1_left [simp]: "gcd 1 x = 1"  haftmann@58023  518  by (rule sym, rule gcdI, simp_all)  haftmann@58023  519 haftmann@58023  520 lemma gcd_1 [simp]: "gcd x 1 = 1"  haftmann@58023  521  by (rule sym, rule gcdI, simp_all)  haftmann@58023  522 haftmann@58023  523 lemma gcd_proj2_if_dvd:  haftmann@58023  524  "y dvd x \ gcd x y = y div normalisation_factor y"  haftmann@58023  525  by (cases "y = 0", simp_all add: dvd_eq_mod_eq_0 gcd_non_0 gcd_0)  haftmann@58023  526 haftmann@58023  527 lemma gcd_proj1_if_dvd:  haftmann@58023  528  "x dvd y \ gcd x y = x div normalisation_factor x"  haftmann@58023  529  by (subst gcd.commute, simp add: gcd_proj2_if_dvd)  haftmann@58023  530 haftmann@58023  531 lemma gcd_proj1_iff: "gcd m n = m div normalisation_factor m \ m dvd n"  haftmann@58023  532 proof  haftmann@58023  533  assume A: "gcd m n = m div normalisation_factor m"  haftmann@58023  534  show "m dvd n"  haftmann@58023  535  proof (cases "m = 0")  haftmann@58023  536  assume [simp]: "m \ 0"  haftmann@58023  537  from A have B: "m = gcd m n * normalisation_factor m"  haftmann@58023  538  by (simp add: unit_eq_div2)  haftmann@58023  539  show ?thesis by (subst B, simp add: mult_unit_dvd_iff)  haftmann@58023  540  qed (insert A, simp)  haftmann@58023  541 next  haftmann@58023  542  assume "m dvd n"  haftmann@58023  543  then show "gcd m n = m div normalisation_factor m" by (rule gcd_proj1_if_dvd)  haftmann@58023  544 qed  haftmann@58023  545   haftmann@58023  546 lemma gcd_proj2_iff: "gcd m n = n div normalisation_factor n \ n dvd m"  haftmann@58023  547  by (subst gcd.commute, simp add: gcd_proj1_iff)  haftmann@58023  548 haftmann@58023  549 lemma gcd_mod1 [simp]:  haftmann@58023  550  "gcd (x mod y) y = gcd x y"  haftmann@58023  551  by (rule gcdI, metis dvd_mod_iff gcd_dvd1 gcd_dvd2, simp_all add: gcd_greatest dvd_mod_iff)  haftmann@58023  552 haftmann@58023  553 lemma gcd_mod2 [simp]:  haftmann@58023  554  "gcd x (y mod x) = gcd x y"  haftmann@58023  555  by (rule gcdI, simp, metis dvd_mod_iff gcd_dvd1 gcd_dvd2, simp_all add: gcd_greatest dvd_mod_iff)  haftmann@58023  556   haftmann@58023  557 lemma normalisation_factor_dvd' [simp]:  haftmann@58023  558  "normalisation_factor x dvd x"  haftmann@58023  559  by (cases "x = 0", simp_all)  haftmann@58023  560 haftmann@58023  561 lemma gcd_mult_distrib':  haftmann@58023  562  "k div normalisation_factor k * gcd x y = gcd (k*x) (k*y)"  haftmann@58023  563 proof (induct x y rule: gcd_eucl.induct)  haftmann@58023  564  case (1 x y)  haftmann@58023  565  show ?case  haftmann@58023  566  proof (cases "y = 0")  haftmann@58023  567  case True  haftmann@58023  568  then show ?thesis by (simp add: normalisation_factor_mult gcd_0 algebra_simps div_mult_div_if_dvd)  haftmann@58023  569  next  haftmann@58023  570  case False  haftmann@58023  571  hence "k div normalisation_factor k * gcd x y = gcd (k * y) (k * (x mod y))"  haftmann@58023  572  using 1 by (subst gcd_red, simp)  haftmann@58023  573  also have "... = gcd (k * x) (k * y)"  haftmann@58023  574  by (simp add: mult_mod_right gcd.commute)  haftmann@58023  575  finally show ?thesis .  haftmann@58023  576  qed  haftmann@58023  577 qed  haftmann@58023  578 haftmann@58023  579 lemma gcd_mult_distrib:  haftmann@58023  580  "k * gcd x y = gcd (k*x) (k*y) * normalisation_factor k"  haftmann@58023  581 proof-  haftmann@58023  582  let ?nf = "normalisation_factor"  haftmann@58023  583  from gcd_mult_distrib'  haftmann@58023  584  have "gcd (k*x) (k*y) = k div ?nf k * gcd x y" ..  haftmann@58023  585  also have "... = k * gcd x y div ?nf k"  haftmann@58023  586  by (metis dvd_div_mult dvd_eq_mod_eq_0 mod_0 normalisation_factor_dvd)  haftmann@58023  587  finally show ?thesis  haftmann@59009  588  by simp  haftmann@58023  589 qed  haftmann@58023  590 haftmann@58023  591 lemma euclidean_size_gcd_le1 [simp]:  haftmann@58023  592  assumes "a \ 0"  haftmann@58023  593  shows "euclidean_size (gcd a b) \ euclidean_size a"  haftmann@58023  594 proof -  haftmann@58023  595  have "gcd a b dvd a" by (rule gcd_dvd1)  haftmann@58023  596  then obtain c where A: "a = gcd a b * c" unfolding dvd_def by blast  haftmann@58023  597  with a \ 0 show ?thesis by (subst (2) A, intro size_mult_mono) auto  haftmann@58023  598 qed  haftmann@58023  599 haftmann@58023  600 lemma euclidean_size_gcd_le2 [simp]:  haftmann@58023  601  "b \ 0 \ euclidean_size (gcd a b) \ euclidean_size b"  haftmann@58023  602  by (subst gcd.commute, rule euclidean_size_gcd_le1)  haftmann@58023  603 haftmann@58023  604 lemma euclidean_size_gcd_less1:  haftmann@58023  605  assumes "a \ 0" and "\a dvd b"  haftmann@58023  606  shows "euclidean_size (gcd a b) < euclidean_size a"  haftmann@58023  607 proof (rule ccontr)  haftmann@58023  608  assume "\euclidean_size (gcd a b) < euclidean_size a"  haftmann@58023  609  with a \ 0 have "euclidean_size (gcd a b) = euclidean_size a"  haftmann@58023  610  by (intro le_antisym, simp_all)  haftmann@58023  611  with assms have "a dvd gcd a b" by (auto intro: dvd_euclidean_size_eq_imp_dvd)  haftmann@58023  612  hence "a dvd b" using dvd_gcd_D2 by blast  haftmann@58023  613  with \a dvd b show False by contradiction  haftmann@58023  614 qed  haftmann@58023  615 haftmann@58023  616 lemma euclidean_size_gcd_less2:  haftmann@58023  617  assumes "b \ 0" and "\b dvd a"  haftmann@58023  618  shows "euclidean_size (gcd a b) < euclidean_size b"  haftmann@58023  619  using assms by (subst gcd.commute, rule euclidean_size_gcd_less1)  haftmann@58023  620 haftmann@58023  621 lemma gcd_mult_unit1: "is_unit a \ gcd (x*a) y = gcd x y"  haftmann@58023  622  apply (rule gcdI)  haftmann@58023  623  apply (rule dvd_trans, rule gcd_dvd1, simp add: unit_simps)  haftmann@58023  624  apply (rule gcd_dvd2)  haftmann@58023  625  apply (rule gcd_greatest, simp add: unit_simps, assumption)  haftmann@58023  626  apply (subst normalisation_factor_gcd, simp add: gcd_0)  haftmann@58023  627  done  haftmann@58023  628 haftmann@58023  629 lemma gcd_mult_unit2: "is_unit a \ gcd x (y*a) = gcd x y"  haftmann@58023  630  by (subst gcd.commute, subst gcd_mult_unit1, assumption, rule gcd.commute)  haftmann@58023  631 haftmann@58023  632 lemma gcd_div_unit1: "is_unit a \ gcd (x div a) y = gcd x y"  haftmann@58023  633  by (simp add: unit_ring_inv gcd_mult_unit1)  haftmann@58023  634 haftmann@58023  635 lemma gcd_div_unit2: "is_unit a \ gcd x (y div a) = gcd x y"  haftmann@58023  636  by (simp add: unit_ring_inv gcd_mult_unit2)  haftmann@58023  637 haftmann@58023  638 lemma gcd_idem: "gcd x x = x div normalisation_factor x"  haftmann@58023  639  by (cases "x = 0") (simp add: gcd_0_left, rule sym, rule gcdI, simp_all)  haftmann@58023  640 haftmann@58023  641 lemma gcd_right_idem: "gcd (gcd p q) q = gcd p q"  haftmann@58023  642  apply (rule gcdI)  haftmann@58023  643  apply (simp add: ac_simps)  haftmann@58023  644  apply (rule gcd_dvd2)  haftmann@58023  645  apply (rule gcd_greatest, erule (1) gcd_greatest, assumption)  haftmann@59009  646  apply simp  haftmann@58023  647  done  haftmann@58023  648 haftmann@58023  649 lemma gcd_left_idem: "gcd p (gcd p q) = gcd p q"  haftmann@58023  650  apply (rule gcdI)  haftmann@58023  651  apply simp  haftmann@58023  652  apply (rule dvd_trans, rule gcd_dvd2, rule gcd_dvd2)  haftmann@58023  653  apply (rule gcd_greatest, assumption, erule gcd_greatest, assumption)  haftmann@59009  654  apply simp  haftmann@58023  655  done  haftmann@58023  656 haftmann@58023  657 lemma comp_fun_idem_gcd: "comp_fun_idem gcd"  haftmann@58023  658 proof  haftmann@58023  659  fix a b show "gcd a \ gcd b = gcd b \ gcd a"  haftmann@58023  660  by (simp add: fun_eq_iff ac_simps)  haftmann@58023  661 next  haftmann@58023  662  fix a show "gcd a \ gcd a = gcd a"  haftmann@58023  663  by (simp add: fun_eq_iff gcd_left_idem)  haftmann@58023  664 qed  haftmann@58023  665 haftmann@58023  666 lemma coprime_dvd_mult:  haftmann@58023  667  assumes "gcd k n = 1" and "k dvd m * n"  haftmann@58023  668  shows "k dvd m"  haftmann@58023  669 proof -  haftmann@58023  670  let ?nf = "normalisation_factor"  haftmann@58023  671  from assms gcd_mult_distrib [of m k n]  haftmann@58023  672  have A: "m = gcd (m * k) (m * n) * ?nf m" by simp  haftmann@58023  673  from k dvd m * n show ?thesis by (subst A, simp_all add: gcd_greatest)  haftmann@58023  674 qed  haftmann@58023  675 haftmann@58023  676 lemma coprime_dvd_mult_iff:  haftmann@58023  677  "gcd k n = 1 \ (k dvd m * n) = (k dvd m)"  haftmann@58023  678  by (rule, rule coprime_dvd_mult, simp_all)  haftmann@58023  679 haftmann@58023  680 lemma gcd_dvd_antisym:  haftmann@58023  681  "gcd a b dvd gcd c d \ gcd c d dvd gcd a b \ gcd a b = gcd c d"  haftmann@58023  682 proof (rule gcdI)  haftmann@58023  683  assume A: "gcd a b dvd gcd c d" and B: "gcd c d dvd gcd a b"  haftmann@58023  684  have "gcd c d dvd c" by simp  haftmann@58023  685  with A show "gcd a b dvd c" by (rule dvd_trans)  haftmann@58023  686  have "gcd c d dvd d" by simp  haftmann@58023  687  with A show "gcd a b dvd d" by (rule dvd_trans)  haftmann@58023  688  show "normalisation_factor (gcd a b) = (if gcd a b = 0 then 0 else 1)"  haftmann@59009  689  by simp  haftmann@58023  690  fix l assume "l dvd c" and "l dvd d"  haftmann@58023  691  hence "l dvd gcd c d" by (rule gcd_greatest)  haftmann@58023  692  from this and B show "l dvd gcd a b" by (rule dvd_trans)  haftmann@58023  693 qed  haftmann@58023  694 haftmann@58023  695 lemma gcd_mult_cancel:  haftmann@58023  696  assumes "gcd k n = 1"  haftmann@58023  697  shows "gcd (k * m) n = gcd m n"  haftmann@58023  698 proof (rule gcd_dvd_antisym)  haftmann@58023  699  have "gcd (gcd (k * m) n) k = gcd (gcd k n) (k * m)" by (simp add: ac_simps)  haftmann@58023  700  also note gcd k n = 1  haftmann@58023  701  finally have "gcd (gcd (k * m) n) k = 1" by simp  haftmann@58023  702  hence "gcd (k * m) n dvd m" by (rule coprime_dvd_mult, simp add: ac_simps)  haftmann@58023  703  moreover have "gcd (k * m) n dvd n" by simp  haftmann@58023  704  ultimately show "gcd (k * m) n dvd gcd m n" by (rule gcd_greatest)  haftmann@58023  705  have "gcd m n dvd (k * m)" and "gcd m n dvd n" by simp_all  haftmann@58023  706  then show "gcd m n dvd gcd (k * m) n" by (rule gcd_greatest)  haftmann@58023  707 qed  haftmann@58023  708 haftmann@58023  709 lemma coprime_crossproduct:  haftmann@58023  710  assumes [simp]: "gcd a d = 1" "gcd b c = 1"  haftmann@58023  711  shows "associated (a * c) (b * d) \ associated a b \ associated c d" (is "?lhs \ ?rhs")  haftmann@58023  712 proof  haftmann@58023  713  assume ?rhs then show ?lhs unfolding associated_def by (fast intro: mult_dvd_mono)  haftmann@58023  714 next  haftmann@58023  715  assume ?lhs  haftmann@58023  716  from ?lhs have "a dvd b * d" unfolding associated_def by (metis dvd_mult_left)  haftmann@58023  717  hence "a dvd b" by (simp add: coprime_dvd_mult_iff)  haftmann@58023  718  moreover from ?lhs have "b dvd a * c" unfolding associated_def by (metis dvd_mult_left)  haftmann@58023  719  hence "b dvd a" by (simp add: coprime_dvd_mult_iff)  haftmann@58023  720  moreover from ?lhs have "c dvd d * b"  haftmann@59009  721  unfolding associated_def by (auto dest: dvd_mult_right simp add: ac_simps)  haftmann@58023  722  hence "c dvd d" by (simp add: coprime_dvd_mult_iff gcd.commute)  haftmann@58023  723  moreover from ?lhs have "d dvd c * a"  haftmann@59009  724  unfolding associated_def by (auto dest: dvd_mult_right simp add: ac_simps)  haftmann@58023  725  hence "d dvd c" by (simp add: coprime_dvd_mult_iff gcd.commute)  haftmann@58023  726  ultimately show ?rhs unfolding associated_def by simp  haftmann@58023  727 qed  haftmann@58023  728 haftmann@58023  729 lemma gcd_add1 [simp]:  haftmann@58023  730  "gcd (m + n) n = gcd m n"  haftmann@58023  731  by (cases "n = 0", simp_all add: gcd_non_0)  haftmann@58023  732 haftmann@58023  733 lemma gcd_add2 [simp]:  haftmann@58023  734  "gcd m (m + n) = gcd m n"  haftmann@58023  735  using gcd_add1 [of n m] by (simp add: ac_simps)  haftmann@58023  736 haftmann@58023  737 lemma gcd_add_mult: "gcd m (k * m + n) = gcd m n"  haftmann@58023  738  by (subst gcd.commute, subst gcd_red, simp)  haftmann@58023  739 haftmann@58023  740 lemma coprimeI: "(\l. \l dvd x; l dvd y\ \ l dvd 1) \ gcd x y = 1"  haftmann@58023  741  by (rule sym, rule gcdI, simp_all)  haftmann@58023  742 haftmann@58023  743 lemma coprime: "gcd a b = 1 \ (\d. d dvd a \ d dvd b \ is_unit d)"  haftmann@58023  744  by (auto simp: is_unit_def intro: coprimeI gcd_greatest dvd_gcd_D1 dvd_gcd_D2)  haftmann@58023  745 haftmann@58023  746 lemma div_gcd_coprime:  haftmann@58023  747  assumes nz: "a \ 0 \ b \ 0"  haftmann@58023  748  defines [simp]: "d \ gcd a b"  haftmann@58023  749  defines [simp]: "a' \ a div d" and [simp]: "b' \ b div d"  haftmann@58023  750  shows "gcd a' b' = 1"  haftmann@58023  751 proof (rule coprimeI)  haftmann@58023  752  fix l assume "l dvd a'" "l dvd b'"  haftmann@58023  753  then obtain s t where "a' = l * s" "b' = l * t" unfolding dvd_def by blast  haftmann@59009  754  moreover have "a = a' * d" "b = b' * d" by simp_all  haftmann@58023  755  ultimately have "a = (l * d) * s" "b = (l * d) * t"  haftmann@59009  756  by (simp_all only: ac_simps)  haftmann@58023  757  hence "l*d dvd a" and "l*d dvd b" by (simp_all only: dvd_triv_left)  haftmann@58023  758  hence "l*d dvd d" by (simp add: gcd_greatest)  haftmann@59009  759  then obtain u where "d = l * d * u" ..  haftmann@59009  760  then have "d * (l * u) = d" by (simp add: ac_simps)  haftmann@59009  761  moreover from nz have "d \ 0" by simp  haftmann@59009  762  with div_mult_self1_is_id have "d * (l * u) div d = l * u" .  haftmann@59009  763  ultimately have "1 = l * u"  haftmann@59009  764  using d \ 0 by simp  haftmann@59009  765  then show "l dvd 1" ..  haftmann@58023  766 qed  haftmann@58023  767 haftmann@58023  768 lemma coprime_mult:  haftmann@58023  769  assumes da: "gcd d a = 1" and db: "gcd d b = 1"  haftmann@58023  770  shows "gcd d (a * b) = 1"  haftmann@58023  771  apply (subst gcd.commute)  haftmann@58023  772  using da apply (subst gcd_mult_cancel)  haftmann@58023  773  apply (subst gcd.commute, assumption)  haftmann@58023  774  apply (subst gcd.commute, rule db)  haftmann@58023  775  done  haftmann@58023  776 haftmann@58023  777 lemma coprime_lmult:  haftmann@58023  778  assumes dab: "gcd d (a * b) = 1"  haftmann@58023  779  shows "gcd d a = 1"  haftmann@58023  780 proof (rule coprimeI)  haftmann@58023  781  fix l assume "l dvd d" and "l dvd a"  haftmann@58023  782  hence "l dvd a * b" by simp  haftmann@58023  783  with l dvd d and dab show "l dvd 1" by (auto intro: gcd_greatest)  haftmann@58023  784 qed  haftmann@58023  785 haftmann@58023  786 lemma coprime_rmult:  haftmann@58023  787  assumes dab: "gcd d (a * b) = 1"  haftmann@58023  788  shows "gcd d b = 1"  haftmann@58023  789 proof (rule coprimeI)  haftmann@58023  790  fix l assume "l dvd d" and "l dvd b"  haftmann@58023  791  hence "l dvd a * b" by simp  haftmann@58023  792  with l dvd d and dab show "l dvd 1" by (auto intro: gcd_greatest)  haftmann@58023  793 qed  haftmann@58023  794 haftmann@58023  795 lemma coprime_mul_eq: "gcd d (a * b) = 1 \ gcd d a = 1 \ gcd d b = 1"  haftmann@58023  796  using coprime_rmult[of d a b] coprime_lmult[of d a b] coprime_mult[of d a b] by blast  haftmann@58023  797 haftmann@58023  798 lemma gcd_coprime:  haftmann@58023  799  assumes z: "gcd a b \ 0" and a: "a = a' * gcd a b" and b: "b = b' * gcd a b"  haftmann@58023  800  shows "gcd a' b' = 1"  haftmann@58023  801 proof -  haftmann@59009  802  from z have "a \ 0 \ b \ 0" by simp  haftmann@58023  803  with div_gcd_coprime have "gcd (a div gcd a b) (b div gcd a b) = 1" .  haftmann@58023  804  also from assms have "a div gcd a b = a'" by (metis div_mult_self2_is_id)+  haftmann@58023  805  also from assms have "b div gcd a b = b'" by (metis div_mult_self2_is_id)+  haftmann@58023  806  finally show ?thesis .  haftmann@58023  807 qed  haftmann@58023  808 haftmann@58023  809 lemma coprime_power:  haftmann@58023  810  assumes "0 < n"  haftmann@58023  811  shows "gcd a (b ^ n) = 1 \ gcd a b = 1"  haftmann@58023  812 using assms proof (induct n)  haftmann@58023  813  case (Suc n) then show ?case  haftmann@58023  814  by (cases n) (simp_all add: coprime_mul_eq)  haftmann@58023  815 qed simp  haftmann@58023  816 haftmann@58023  817 lemma gcd_coprime_exists:  haftmann@58023  818  assumes nz: "gcd a b \ 0"  haftmann@58023  819  shows "\a' b'. a = a' * gcd a b \ b = b' * gcd a b \ gcd a' b' = 1"  haftmann@58023  820  apply (rule_tac x = "a div gcd a b" in exI)  haftmann@58023  821  apply (rule_tac x = "b div gcd a b" in exI)  haftmann@59009  822  apply (insert nz, auto intro: div_gcd_coprime)  haftmann@58023  823  done  haftmann@58023  824 haftmann@58023  825 lemma coprime_exp:  haftmann@58023  826  "gcd d a = 1 \ gcd d (a^n) = 1"  haftmann@58023  827  by (induct n, simp_all add: coprime_mult)  haftmann@58023  828 haftmann@58023  829 lemma coprime_exp2 [intro]:  haftmann@58023  830  "gcd a b = 1 \ gcd (a^n) (b^m) = 1"  haftmann@58023  831  apply (rule coprime_exp)  haftmann@58023  832  apply (subst gcd.commute)  haftmann@58023  833  apply (rule coprime_exp)  haftmann@58023  834  apply (subst gcd.commute)  haftmann@58023  835  apply assumption  haftmann@58023  836  done  haftmann@58023  837 haftmann@58023  838 lemma gcd_exp:  haftmann@58023  839  "gcd (a^n) (b^n) = (gcd a b) ^ n"  haftmann@58023  840 proof (cases "a = 0 \ b = 0")  haftmann@58023  841  assume "a = 0 \ b = 0"  haftmann@58023  842  then show ?thesis by (cases n, simp_all add: gcd_0_left)  haftmann@58023  843 next  haftmann@58023  844  assume A: "\(a = 0 \ b = 0)"  haftmann@58023  845  hence "1 = gcd ((a div gcd a b)^n) ((b div gcd a b)^n)"  haftmann@58023  846  using div_gcd_coprime by (subst sym, auto simp: div_gcd_coprime)  haftmann@58023  847  hence "(gcd a b) ^ n = (gcd a b) ^ n * ..." by simp  haftmann@58023  848  also note gcd_mult_distrib  haftmann@58023  849  also have "normalisation_factor ((gcd a b)^n) = 1"  haftmann@58023  850  by (simp add: normalisation_factor_pow A)  haftmann@58023  851  also have "(gcd a b)^n * (a div gcd a b)^n = a^n"  haftmann@58023  852  by (subst ac_simps, subst div_power, simp, rule dvd_div_mult_self, rule dvd_power_same, simp)  haftmann@58023  853  also have "(gcd a b)^n * (b div gcd a b)^n = b^n"  haftmann@58023  854  by (subst ac_simps, subst div_power, simp, rule dvd_div_mult_self, rule dvd_power_same, simp)  haftmann@58023  855  finally show ?thesis by simp  haftmann@58023  856 qed  haftmann@58023  857 haftmann@58023  858 lemma coprime_common_divisor:  haftmann@58023  859  "gcd a b = 1 \ x dvd a \ x dvd b \ is_unit x"  haftmann@58023  860  apply (subgoal_tac "x dvd gcd a b")  haftmann@58023  861  apply (simp add: is_unit_def)  haftmann@58023  862  apply (erule (1) gcd_greatest)  haftmann@58023  863  done  haftmann@58023  864 haftmann@58023  865 lemma division_decomp:  haftmann@58023  866  assumes dc: "a dvd b * c"  haftmann@58023  867  shows "\b' c'. a = b' * c' \ b' dvd b \ c' dvd c"  haftmann@58023  868 proof (cases "gcd a b = 0")  haftmann@58023  869  assume "gcd a b = 0"  haftmann@59009  870  hence "a = 0 \ b = 0" by simp  haftmann@58023  871  hence "a = 0 * c \ 0 dvd b \ c dvd c" by simp  haftmann@58023  872  then show ?thesis by blast  haftmann@58023  873 next  haftmann@58023  874  let ?d = "gcd a b"  haftmann@58023  875  assume "?d \ 0"  haftmann@58023  876  from gcd_coprime_exists[OF this]  haftmann@58023  877  obtain a' b' where ab': "a = a' * ?d" "b = b' * ?d" "gcd a' b' = 1"  haftmann@58023  878  by blast  haftmann@58023  879  from ab'(1) have "a' dvd a" unfolding dvd_def by blast  haftmann@58023  880  with dc have "a' dvd b*c" using dvd_trans[of a' a "b*c"] by simp  haftmann@58023  881  from dc ab'(1,2) have "a'*?d dvd (b'*?d) * c" by simp  haftmann@58023  882  hence "?d * a' dvd ?d * (b' * c)" by (simp add: mult_ac)  haftmann@59009  883  with ?d \ 0 have "a' dvd b' * c" by simp  haftmann@58023  884  with coprime_dvd_mult[OF ab'(3)]  haftmann@58023  885  have "a' dvd c" by (subst (asm) ac_simps, blast)  haftmann@58023  886  with ab'(1) have "a = ?d * a' \ ?d dvd b \ a' dvd c" by (simp add: mult_ac)  haftmann@58023  887  then show ?thesis by blast  haftmann@58023  888 qed  haftmann@58023  889 haftmann@58023  890 lemma pow_divides_pow:  haftmann@58023  891  assumes ab: "a ^ n dvd b ^ n" and n: "n \ 0"  haftmann@58023  892  shows "a dvd b"  haftmann@58023  893 proof (cases "gcd a b = 0")  haftmann@58023  894  assume "gcd a b = 0"  haftmann@59009  895  then show ?thesis by simp  haftmann@58023  896 next  haftmann@58023  897  let ?d = "gcd a b"  haftmann@58023  898  assume "?d \ 0"  haftmann@58023  899  from n obtain m where m: "n = Suc m" by (cases n, simp_all)  haftmann@59009  900  from ?d \ 0 have zn: "?d ^ n \ 0" by (rule power_not_zero)  haftmann@58023  901  from gcd_coprime_exists[OF ?d \ 0]  haftmann@58023  902  obtain a' b' where ab': "a = a' * ?d" "b = b' * ?d" "gcd a' b' = 1"  haftmann@58023  903  by blast  haftmann@58023  904  from ab have "(a' * ?d) ^ n dvd (b' * ?d) ^ n"  haftmann@58023  905  by (simp add: ab'(1,2)[symmetric])  haftmann@58023  906  hence "?d^n * a'^n dvd ?d^n * b'^n"  haftmann@58023  907  by (simp only: power_mult_distrib ac_simps)  haftmann@59009  908  with zn have "a'^n dvd b'^n" by simp  haftmann@58023  909  hence "a' dvd b'^n" using dvd_trans[of a' "a'^n" "b'^n"] by (simp add: m)  haftmann@58023  910  hence "a' dvd b'^m * b'" by (simp add: m ac_simps)  haftmann@58023  911  with coprime_dvd_mult[OF coprime_exp[OF ab'(3), of m]]  haftmann@58023  912  have "a' dvd b'" by (subst (asm) ac_simps, blast)  haftmann@58023  913  hence "a'*?d dvd b'*?d" by (rule mult_dvd_mono, simp)  haftmann@58023  914  with ab'(1,2) show ?thesis by simp  haftmann@58023  915 qed  haftmann@58023  916 haftmann@58023  917 lemma pow_divides_eq [simp]:  haftmann@58023  918  "n \ 0 \ a ^ n dvd b ^ n \ a dvd b"  haftmann@58023  919  by (auto intro: pow_divides_pow dvd_power_same)  haftmann@58023  920 haftmann@58023  921 lemma divides_mult:  haftmann@58023  922  assumes mr: "m dvd r" and nr: "n dvd r" and mn: "gcd m n = 1"  haftmann@58023  923  shows "m * n dvd r"  haftmann@58023  924 proof -  haftmann@58023  925  from mr nr obtain m' n' where m': "r = m*m'" and n': "r = n*n'"  haftmann@58023  926  unfolding dvd_def by blast  haftmann@58023  927  from mr n' have "m dvd n'*n" by (simp add: ac_simps)  haftmann@58023  928  hence "m dvd n'" using coprime_dvd_mult_iff[OF mn] by simp  haftmann@58023  929  then obtain k where k: "n' = m*k" unfolding dvd_def by blast  haftmann@58023  930  with n' have "r = m * n * k" by (simp add: mult_ac)  haftmann@58023  931  then show ?thesis unfolding dvd_def by blast  haftmann@58023  932 qed  haftmann@58023  933 haftmann@58023  934 lemma coprime_plus_one [simp]: "gcd (n + 1) n = 1"  haftmann@58023  935  by (subst add_commute, simp)  haftmann@58023  936 haftmann@58023  937 lemma setprod_coprime [rule_format]:  haftmann@58023  938  "(\i\A. gcd (f i) x = 1) \ gcd (\i\A. f i) x = 1"  haftmann@58023  939  apply (cases "finite A")  haftmann@58023  940  apply (induct set: finite)  haftmann@58023  941  apply (auto simp add: gcd_mult_cancel)  haftmann@58023  942  done  haftmann@58023  943 haftmann@58023  944 lemma coprime_divisors:  haftmann@58023  945  assumes "d dvd a" "e dvd b" "gcd a b = 1"  haftmann@58023  946  shows "gcd d e = 1"  haftmann@58023  947 proof -  haftmann@58023  948  from assms obtain k l where "a = d * k" "b = e * l"  haftmann@58023  949  unfolding dvd_def by blast  haftmann@58023  950  with assms have "gcd (d * k) (e * l) = 1" by simp  haftmann@58023  951  hence "gcd (d * k) e = 1" by (rule coprime_lmult)  haftmann@58023  952  also have "gcd (d * k) e = gcd e (d * k)" by (simp add: ac_simps)  haftmann@58023  953  finally have "gcd e d = 1" by (rule coprime_lmult)  haftmann@58023  954  then show ?thesis by (simp add: ac_simps)  haftmann@58023  955 qed  haftmann@58023  956 haftmann@58023  957 lemma invertible_coprime:  haftmann@59009  958  assumes "x * y mod m = 1"  haftmann@59009  959  shows "coprime x m"  haftmann@59009  960 proof -  haftmann@59009  961  from assms have "coprime m (x * y mod m)"  haftmann@59009  962  by simp  haftmann@59009  963  then have "coprime m (x * y)"  haftmann@59009  964  by simp  haftmann@59009  965  then have "coprime m x"  haftmann@59009  966  by (rule coprime_lmult)  haftmann@59009  967  then show ?thesis  haftmann@59009  968  by (simp add: ac_simps)  haftmann@59009  969 qed  haftmann@58023  970 haftmann@58023  971 lemma lcm_gcd:  haftmann@58023  972  "lcm a b = a * b div (gcd a b * normalisation_factor (a*b))"  haftmann@58023  973  by (simp only: lcm_lcm_eucl gcd_gcd_eucl lcm_eucl_def)  haftmann@58023  974 haftmann@58023  975 lemma lcm_gcd_prod:  haftmann@58023  976  "lcm a b * gcd a b = a * b div normalisation_factor (a*b)"  haftmann@58023  977 proof (cases "a * b = 0")  haftmann@58023  978  let ?nf = normalisation_factor  haftmann@58023  979  assume "a * b \ 0"  haftmann@58953  980  hence "gcd a b \ 0" by simp  haftmann@58023  981  from lcm_gcd have "lcm a b * gcd a b = gcd a b * (a * b div (?nf (a*b) * gcd a b))"  haftmann@58023  982  by (simp add: mult_ac)  haftmann@58023  983  also from a * b \ 0 have "... = a * b div ?nf (a*b)"  haftmann@58953  984  by (simp_all add: unit_ring_inv'1 unit_ring_inv)  haftmann@58023  985  finally show ?thesis .  haftmann@58953  986 qed (auto simp add: lcm_gcd)  haftmann@58023  987 haftmann@58023  988 lemma lcm_dvd1 [iff]:  haftmann@58023  989  "x dvd lcm x y"  haftmann@58023  990 proof (cases "x*y = 0")  haftmann@58023  991  assume "x * y \ 0"  haftmann@58953  992  hence "gcd x y \ 0" by simp  haftmann@58023  993  let ?c = "ring_inv (normalisation_factor (x*y))"  haftmann@58023  994  from x * y \ 0 have [simp]: "is_unit (normalisation_factor (x*y))" by simp  haftmann@58023  995  from lcm_gcd_prod[of x y] have "lcm x y * gcd x y = x * ?c * y"  haftmann@58023  996  by (simp add: mult_ac unit_ring_inv)  haftmann@58023  997  hence "lcm x y * gcd x y div gcd x y = x * ?c * y div gcd x y" by simp  haftmann@58023  998  with gcd x y \ 0 have "lcm x y = x * ?c * y div gcd x y"  haftmann@58023  999  by (subst (asm) div_mult_self2_is_id, simp_all)  haftmann@58023  1000  also have "... = x * (?c * y div gcd x y)"  haftmann@58023  1001  by (metis div_mult_swap gcd_dvd2 mult_assoc)  haftmann@58023  1002  finally show ?thesis by (rule dvdI)  haftmann@58953  1003 qed (auto simp add: lcm_gcd)  haftmann@58023  1004 haftmann@58023  1005 lemma lcm_least:  haftmann@58023  1006  "\a dvd k; b dvd k\ \ lcm a b dvd k"  haftmann@58023  1007 proof (cases "k = 0")  haftmann@58023  1008  let ?nf = normalisation_factor  haftmann@58023  1009  assume "k \ 0"  haftmann@58023  1010  hence "is_unit (?nf k)" by simp  haftmann@58023  1011  hence "?nf k \ 0" by (metis not_is_unit_0)  haftmann@58023  1012  assume A: "a dvd k" "b dvd k"  haftmann@58953  1013  hence "gcd a b \ 0" using k \ 0 by auto  haftmann@58023  1014  from A obtain r s where ar: "k = a * r" and bs: "k = b * s"  haftmann@58023  1015  unfolding dvd_def by blast  haftmann@58953  1016  with k \ 0 have "r * s \ 0"  haftmann@58953  1017  by auto (drule sym [of 0], simp)  haftmann@58023  1018  hence "is_unit (?nf (r * s))" by simp  haftmann@58023  1019  let ?c = "?nf k div ?nf (r*s)"  haftmann@58023  1020  from is_unit (?nf k) and is_unit (?nf (r * s)) have "is_unit ?c" by (rule unit_div)  haftmann@58023  1021  hence "?c \ 0" using not_is_unit_0 by fast  haftmann@58023  1022  from ar bs have "k * k * gcd s r = ?nf k * k * gcd (k * s) (k * r)"  haftmann@58953  1023  by (subst mult_assoc, subst gcd_mult_distrib[of k s r], simp only: ac_simps)  haftmann@58023  1024  also have "... = ?nf k * k * gcd ((r*s) * a) ((r*s) * b)"  haftmann@58023  1025  by (subst (3) k = a * r, subst (3) k = b * s, simp add: algebra_simps)  haftmann@58023  1026  also have "... = ?c * r*s * k * gcd a b" using r * s \ 0  haftmann@58023  1027  by (subst gcd_mult_distrib'[symmetric], simp add: algebra_simps unit_simps)  haftmann@58023  1028  finally have "(a*r) * (b*s) * gcd s r = ?c * k * r * s * gcd a b"  haftmann@58023  1029  by (subst ar[symmetric], subst bs[symmetric], simp add: mult_ac)  haftmann@58023  1030  hence "a * b * gcd s r * (r * s) = ?c * k * gcd a b * (r * s)"  haftmann@58023  1031  by (simp add: algebra_simps)  haftmann@58023  1032  hence "?c * k * gcd a b = a * b * gcd s r" using r * s \ 0  haftmann@58023  1033  by (metis div_mult_self2_is_id)  haftmann@58023  1034  also have "... = lcm a b * gcd a b * gcd s r * ?nf (a*b)"  haftmann@58023  1035  by (subst lcm_gcd_prod[of a b], metis gcd_mult_distrib gcd_mult_distrib')  haftmann@58023  1036  also have "... = lcm a b * gcd s r * ?nf (a*b) * gcd a b"  haftmann@58023  1037  by (simp add: algebra_simps)  haftmann@58023  1038  finally have "k * ?c = lcm a b * gcd s r * ?nf (a*b)" using gcd a b \ 0  haftmann@58023  1039  by (metis mult.commute div_mult_self2_is_id)  haftmann@58023  1040  hence "k = lcm a b * (gcd s r * ?nf (a*b)) div ?c" using ?c \ 0  haftmann@58023  1041  by (metis div_mult_self2_is_id mult_assoc)  haftmann@58023  1042  also have "... = lcm a b * (gcd s r * ?nf (a*b) div ?c)" using is_unit ?c  haftmann@58023  1043  by (simp add: unit_simps)  haftmann@58023  1044  finally show ?thesis by (rule dvdI)  haftmann@58023  1045 qed simp  haftmann@58023  1046 haftmann@58023  1047 lemma lcm_zero:  haftmann@58023  1048  "lcm a b = 0 \ a = 0 \ b = 0"  haftmann@58023  1049 proof -  haftmann@58023  1050  let ?nf = normalisation_factor  haftmann@58023  1051  {  haftmann@58023  1052  assume "a \ 0" "b \ 0"  haftmann@58023  1053  hence "a * b div ?nf (a * b) \ 0" by (simp add: no_zero_divisors)  haftmann@59009  1054  moreover from a \ 0 and b \ 0 have "gcd a b \ 0" by simp  haftmann@58023  1055  ultimately have "lcm a b \ 0" using lcm_gcd_prod[of a b] by (intro notI, simp)  haftmann@58023  1056  } moreover {  haftmann@58023  1057  assume "a = 0 \ b = 0"  haftmann@58023  1058  hence "lcm a b = 0" by (elim disjE, simp_all add: lcm_gcd)  haftmann@58023  1059  }  haftmann@58023  1060  ultimately show ?thesis by blast  haftmann@58023  1061 qed  haftmann@58023  1062 haftmann@58023  1063 lemmas lcm_0_iff = lcm_zero  haftmann@58023  1064 haftmann@58023  1065 lemma gcd_lcm:  haftmann@58023  1066  assumes "lcm a b \ 0"  haftmann@58023  1067  shows "gcd a b = a * b div (lcm a b * normalisation_factor (a * b))"  haftmann@58023  1068 proof-  haftmann@59009  1069  from assms have "gcd a b \ 0" by (simp add: lcm_zero)  haftmann@58023  1070  let ?c = "normalisation_factor (a*b)"  haftmann@58023  1071  from lcm a b \ 0 have "?c \ 0" by (intro notI, simp add: lcm_zero no_zero_divisors)  haftmann@58023  1072  hence "is_unit ?c" by simp  haftmann@58023  1073  from lcm_gcd_prod [of a b] have "gcd a b = a * b div ?c div lcm a b"  haftmann@58023  1074  by (subst (2) div_mult_self2_is_id[OF lcm a b \ 0, symmetric], simp add: mult_ac)  haftmann@58023  1075  also from is_unit ?c have "... = a * b div (?c * lcm a b)"  haftmann@58023  1076  by (simp only: unit_ring_inv'1 unit_ring_inv)  haftmann@58023  1077  finally show ?thesis by (simp only: ac_simps)  haftmann@58023  1078 qed  haftmann@58023  1079 haftmann@58023  1080 lemma normalisation_factor_lcm [simp]:  haftmann@58023  1081  "normalisation_factor (lcm a b) = (if a = 0 \ b = 0 then 0 else 1)"  haftmann@58023  1082 proof (cases "a = 0 \ b = 0")  haftmann@58023  1083  case True then show ?thesis  haftmann@58953  1084  by (auto simp add: lcm_gcd)  haftmann@58023  1085 next  haftmann@58023  1086  case False  haftmann@58023  1087  let ?nf = normalisation_factor  haftmann@58023  1088  from lcm_gcd_prod[of a b]  haftmann@58023  1089  have "?nf (lcm a b) * ?nf (gcd a b) = ?nf (a*b) div ?nf (a*b)"  haftmann@58023  1090  by (metis div_by_0 div_self normalisation_correct normalisation_factor_0 normalisation_factor_mult)  haftmann@58023  1091  also have "... = (if a*b = 0 then 0 else 1)"  haftmann@58953  1092  by simp  haftmann@58953  1093  finally show ?thesis using False by simp  haftmann@58023  1094 qed  haftmann@58023  1095 haftmann@58023  1096 lemma lcm_dvd2 [iff]: "y dvd lcm x y"  haftmann@58023  1097  using lcm_dvd1 [of y x] by (simp add: lcm_gcd ac_simps)  haftmann@58023  1098 haftmann@58023  1099 lemma lcmI:  haftmann@58023  1100  "\x dvd k; y dvd k; \l. x dvd l \ y dvd l \ k dvd l;  haftmann@58023  1101  normalisation_factor k = (if k = 0 then 0 else 1)\ \ k = lcm x y"  haftmann@58023  1102  by (intro normed_associated_imp_eq) (auto simp: associated_def intro: lcm_least)  haftmann@58023  1103 haftmann@58023  1104 sublocale lcm!: abel_semigroup lcm  haftmann@58023  1105 proof  haftmann@58023  1106  fix x y z  haftmann@58023  1107  show "lcm (lcm x y) z = lcm x (lcm y z)"  haftmann@58023  1108  proof (rule lcmI)  haftmann@58023  1109  have "x dvd lcm x y" and "lcm x y dvd lcm (lcm x y) z" by simp_all  haftmann@58023  1110  then show "x dvd lcm (lcm x y) z" by (rule dvd_trans)  haftmann@58023  1111   haftmann@58023  1112  have "y dvd lcm x y" and "lcm x y dvd lcm (lcm x y) z" by simp_all  haftmann@58023  1113  hence "y dvd lcm (lcm x y) z" by (rule dvd_trans)  haftmann@58023  1114  moreover have "z dvd lcm (lcm x y) z" by simp  haftmann@58023  1115  ultimately show "lcm y z dvd lcm (lcm x y) z" by (rule lcm_least)  haftmann@58023  1116 haftmann@58023  1117  fix l assume "x dvd l" and "lcm y z dvd l"  haftmann@58023  1118  have "y dvd lcm y z" by simp  haftmann@58023  1119  from this and lcm y z dvd l have "y dvd l" by (rule dvd_trans)  haftmann@58023  1120  have "z dvd lcm y z" by simp  haftmann@58023  1121  from this and lcm y z dvd l have "z dvd l" by (rule dvd_trans)  haftmann@58023  1122  from x dvd l and y dvd l have "lcm x y dvd l" by (rule lcm_least)  haftmann@58023  1123  from this and z dvd l show "lcm (lcm x y) z dvd l" by (rule lcm_least)  haftmann@58023  1124  qed (simp add: lcm_zero)  haftmann@58023  1125 next  haftmann@58023  1126  fix x y  haftmann@58023  1127  show "lcm x y = lcm y x"  haftmann@58023  1128  by (simp add: lcm_gcd ac_simps)  haftmann@58023  1129 qed  haftmann@58023  1130 haftmann@58023  1131 lemma dvd_lcm_D1:  haftmann@58023  1132  "lcm m n dvd k \ m dvd k"  haftmann@58023  1133  by (rule dvd_trans, rule lcm_dvd1, assumption)  haftmann@58023  1134 haftmann@58023  1135 lemma dvd_lcm_D2:  haftmann@58023  1136  "lcm m n dvd k \ n dvd k"  haftmann@58023  1137  by (rule dvd_trans, rule lcm_dvd2, assumption)  haftmann@58023  1138 haftmann@58023  1139 lemma gcd_dvd_lcm [simp]:  haftmann@58023  1140  "gcd a b dvd lcm a b"  haftmann@58023  1141  by (metis dvd_trans gcd_dvd2 lcm_dvd2)  haftmann@58023  1142 haftmann@58023  1143 lemma lcm_1_iff:  haftmann@58023  1144  "lcm a b = 1 \ is_unit a \ is_unit b"  haftmann@58023  1145 proof  haftmann@58023  1146  assume "lcm a b = 1"  haftmann@58023  1147  then show "is_unit a \ is_unit b" unfolding is_unit_def by auto  haftmann@58023  1148 next  haftmann@58023  1149  assume "is_unit a \ is_unit b"  haftmann@58023  1150  hence "a dvd 1" and "b dvd 1" unfolding is_unit_def by simp_all  haftmann@58023  1151  hence "is_unit (lcm a b)" unfolding is_unit_def by (rule lcm_least)  haftmann@58023  1152  hence "lcm a b = normalisation_factor (lcm a b)"  haftmann@58023  1153  by (subst normalisation_factor_unit, simp_all)  haftmann@58023  1154  also have "\ = 1" using is_unit a \ is_unit b by (auto simp add: is_unit_def)  haftmann@58023  1155  finally show "lcm a b = 1" .  haftmann@58023  1156 qed  haftmann@58023  1157 haftmann@58023  1158 lemma lcm_0_left [simp]:  haftmann@58023  1159  "lcm 0 x = 0"  haftmann@58023  1160  by (rule sym, rule lcmI, simp_all)  haftmann@58023  1161 haftmann@58023  1162 lemma lcm_0 [simp]:  haftmann@58023  1163  "lcm x 0 = 0"  haftmann@58023  1164  by (rule sym, rule lcmI, simp_all)  haftmann@58023  1165 haftmann@58023  1166 lemma lcm_unique:  haftmann@58023  1167  "a dvd d \ b dvd d \  haftmann@58023  1168  normalisation_factor d = (if d = 0 then 0 else 1) \  haftmann@58023  1169  (\e. a dvd e \ b dvd e \ d dvd e) \ d = lcm a b"  haftmann@58023  1170  by (rule, auto intro: lcmI simp: lcm_least lcm_zero)  haftmann@58023  1171 haftmann@58023  1172 lemma dvd_lcm_I1 [simp]:  haftmann@58023  1173  "k dvd m \ k dvd lcm m n"  haftmann@58023  1174  by (metis lcm_dvd1 dvd_trans)  haftmann@58023  1175 haftmann@58023  1176 lemma dvd_lcm_I2 [simp]:  haftmann@58023  1177  "k dvd n \ k dvd lcm m n"  haftmann@58023  1178  by (metis lcm_dvd2 dvd_trans)  haftmann@58023  1179 haftmann@58023  1180 lemma lcm_1_left [simp]:  haftmann@58023  1181  "lcm 1 x = x div normalisation_factor x"  haftmann@58023  1182  by (cases "x = 0") (simp, rule sym, rule lcmI, simp_all)  haftmann@58023  1183 haftmann@58023  1184 lemma lcm_1_right [simp]:  haftmann@58023  1185  "lcm x 1 = x div normalisation_factor x"  haftmann@58023  1186  by (simp add: ac_simps)  haftmann@58023  1187 haftmann@58023  1188 lemma lcm_coprime:  haftmann@58023  1189  "gcd a b = 1 \ lcm a b = a * b div normalisation_factor (a*b)"  haftmann@58023  1190  by (subst lcm_gcd) simp  haftmann@58023  1191 haftmann@58023  1192 lemma lcm_proj1_if_dvd:  haftmann@58023  1193  "y dvd x \ lcm x y = x div normalisation_factor x"  haftmann@58023  1194  by (cases "x = 0") (simp, rule sym, rule lcmI, simp_all)  haftmann@58023  1195 haftmann@58023  1196 lemma lcm_proj2_if_dvd:  haftmann@58023  1197  "x dvd y \ lcm x y = y div normalisation_factor y"  haftmann@58023  1198  using lcm_proj1_if_dvd [of x y] by (simp add: ac_simps)  haftmann@58023  1199 haftmann@58023  1200 lemma lcm_proj1_iff:  haftmann@58023  1201  "lcm m n = m div normalisation_factor m \ n dvd m"  haftmann@58023  1202 proof  haftmann@58023  1203  assume A: "lcm m n = m div normalisation_factor m"  haftmann@58023  1204  show "n dvd m"  haftmann@58023  1205  proof (cases "m = 0")  haftmann@58023  1206  assume [simp]: "m \ 0"  haftmann@58023  1207  from A have B: "m = lcm m n * normalisation_factor m"  haftmann@58023  1208  by (simp add: unit_eq_div2)  haftmann@58023  1209  show ?thesis by (subst B, simp)  haftmann@58023  1210  qed simp  haftmann@58023  1211 next  haftmann@58023  1212  assume "n dvd m"  haftmann@58023  1213  then show "lcm m n = m div normalisation_factor m" by (rule lcm_proj1_if_dvd)  haftmann@58023  1214 qed  haftmann@58023  1215 haftmann@58023  1216 lemma lcm_proj2_iff:  haftmann@58023  1217  "lcm m n = n div normalisation_factor n \ m dvd n"  haftmann@58023  1218  using lcm_proj1_iff [of n m] by (simp add: ac_simps)  haftmann@58023  1219 haftmann@58023  1220 lemma euclidean_size_lcm_le1:  haftmann@58023  1221  assumes "a \ 0" and "b \ 0"  haftmann@58023  1222  shows "euclidean_size a \ euclidean_size (lcm a b)"  haftmann@58023  1223 proof -  haftmann@58023  1224  have "a dvd lcm a b" by (rule lcm_dvd1)  haftmann@58023  1225  then obtain c where A: "lcm a b = a * c" unfolding dvd_def by blast  haftmann@58023  1226  with a \ 0 and b \ 0 have "c \ 0" by (auto simp: lcm_zero)  haftmann@58023  1227  then show ?thesis by (subst A, intro size_mult_mono)  haftmann@58023  1228 qed  haftmann@58023  1229 haftmann@58023  1230 lemma euclidean_size_lcm_le2:  haftmann@58023  1231  "a \ 0 \ b \ 0 \ euclidean_size b \ euclidean_size (lcm a b)"  haftmann@58023  1232  using euclidean_size_lcm_le1 [of b a] by (simp add: ac_simps)  haftmann@58023  1233 haftmann@58023  1234 lemma euclidean_size_lcm_less1:  haftmann@58023  1235  assumes "b \ 0" and "\b dvd a"  haftmann@58023  1236  shows "euclidean_size a < euclidean_size (lcm a b)"  haftmann@58023  1237 proof (rule ccontr)  haftmann@58023  1238  from assms have "a \ 0" by auto  haftmann@58023  1239  assume "\euclidean_size a < euclidean_size (lcm a b)"  haftmann@58023  1240  with a \ 0 and b \ 0 have "euclidean_size (lcm a b) = euclidean_size a"  haftmann@58023  1241  by (intro le_antisym, simp, intro euclidean_size_lcm_le1)  haftmann@58023  1242  with assms have "lcm a b dvd a"  haftmann@58023  1243  by (rule_tac dvd_euclidean_size_eq_imp_dvd) (auto simp: lcm_zero)  haftmann@58023  1244  hence "b dvd a" by (rule dvd_lcm_D2)  haftmann@58023  1245  with \b dvd a show False by contradiction  haftmann@58023  1246 qed  haftmann@58023  1247 haftmann@58023  1248 lemma euclidean_size_lcm_less2:  haftmann@58023  1249  assumes "a \ 0" and "\a dvd b"  haftmann@58023  1250  shows "euclidean_size b < euclidean_size (lcm a b)"  haftmann@58023  1251  using assms euclidean_size_lcm_less1 [of a b] by (simp add: ac_simps)  haftmann@58023  1252 haftmann@58023  1253 lemma lcm_mult_unit1:  haftmann@58023  1254  "is_unit a \ lcm (x*a) y = lcm x y"  haftmann@58023  1255  apply (rule lcmI)  haftmann@58023  1256  apply (rule dvd_trans[of _ "x*a"], simp, rule lcm_dvd1)  haftmann@58023  1257  apply (rule lcm_dvd2)  haftmann@58023  1258  apply (rule lcm_least, simp add: unit_simps, assumption)  haftmann@58023  1259  apply (subst normalisation_factor_lcm, simp add: lcm_zero)  haftmann@58023  1260  done  haftmann@58023  1261 haftmann@58023  1262 lemma lcm_mult_unit2:  haftmann@58023  1263  "is_unit a \ lcm x (y*a) = lcm x y"  haftmann@58023  1264  using lcm_mult_unit1 [of a y x] by (simp add: ac_simps)  haftmann@58023  1265 haftmann@58023  1266 lemma lcm_div_unit1:  haftmann@58023  1267  "is_unit a \ lcm (x div a) y = lcm x y"  haftmann@58023  1268  by (simp add: unit_ring_inv lcm_mult_unit1)  haftmann@58023  1269 haftmann@58023  1270 lemma lcm_div_unit2:  haftmann@58023  1271  "is_unit a \ lcm x (y div a) = lcm x y"  haftmann@58023  1272  by (simp add: unit_ring_inv lcm_mult_unit2)  haftmann@58023  1273 haftmann@58023  1274 lemma lcm_left_idem:  haftmann@58023  1275  "lcm p (lcm p q) = lcm p q"  haftmann@58023  1276  apply (rule lcmI)  haftmann@58023  1277  apply simp  haftmann@58023  1278  apply (subst lcm.assoc [symmetric], rule lcm_dvd2)  haftmann@58023  1279  apply (rule lcm_least, assumption)  haftmann@58023  1280  apply (erule (1) lcm_least)  haftmann@58023  1281  apply (auto simp: lcm_zero)  haftmann@58023  1282  done  haftmann@58023  1283 haftmann@58023  1284 lemma lcm_right_idem:  haftmann@58023  1285  "lcm (lcm p q) q = lcm p q"  haftmann@58023  1286  apply (rule lcmI)  haftmann@58023  1287  apply (subst lcm.assoc, rule lcm_dvd1)  haftmann@58023  1288  apply (rule lcm_dvd2)  haftmann@58023  1289  apply (rule lcm_least, erule (1) lcm_least, assumption)  haftmann@58023  1290  apply (auto simp: lcm_zero)  haftmann@58023  1291  done  haftmann@58023  1292 haftmann@58023  1293 lemma comp_fun_idem_lcm: "comp_fun_idem lcm"  haftmann@58023  1294 proof  haftmann@58023  1295  fix a b show "lcm a \ lcm b = lcm b \ lcm a"  haftmann@58023  1296  by (simp add: fun_eq_iff ac_simps)  haftmann@58023  1297 next  haftmann@58023  1298  fix a show "lcm a \ lcm a = lcm a" unfolding o_def  haftmann@58023  1299  by (intro ext, simp add: lcm_left_idem)  haftmann@58023  1300 qed  haftmann@58023  1301 haftmann@58023  1302 lemma dvd_Lcm [simp]: "x \ A \ x dvd Lcm A"  haftmann@58023  1303  and Lcm_dvd [simp]: "(\x\A. x dvd l') \ Lcm A dvd l'"  haftmann@58023  1304  and normalisation_factor_Lcm [simp]:  haftmann@58023  1305  "normalisation_factor (Lcm A) = (if Lcm A = 0 then 0 else 1)"  haftmann@58023  1306 proof -  haftmann@58023  1307  have "(\x\A. x dvd Lcm A) \ (\l'. (\x\A. x dvd l') \ Lcm A dvd l') \  haftmann@58023  1308  normalisation_factor (Lcm A) = (if Lcm A = 0 then 0 else 1)" (is ?thesis)  haftmann@58023  1309  proof (cases "\l. l \ 0 \ (\x\A. x dvd l)")  haftmann@58023  1310  case False  haftmann@58023  1311  hence "Lcm A = 0" by (auto simp: Lcm_Lcm_eucl Lcm_eucl_def)  haftmann@58023  1312  with False show ?thesis by auto  haftmann@58023  1313  next  haftmann@58023  1314  case True  haftmann@58023  1315  then obtain l\<^sub>0 where l\<^sub>0_props: "l\<^sub>0 \ 0 \ (\x\A. x dvd l\<^sub>0)" by blast  haftmann@58023  1316  def n \ "LEAST n. \l. l \ 0 \ (\x\A. x dvd l) \ euclidean_size l = n"  haftmann@58023  1317  def l \ "SOME l. l \ 0 \ (\x\A. x dvd l) \ euclidean_size l = n"  haftmann@58023  1318  have "\l. l \ 0 \ (\x\A. x dvd l) \ euclidean_size l = n"  haftmann@58023  1319  apply (subst n_def)  haftmann@58023  1320  apply (rule LeastI[of _ "euclidean_size l\<^sub>0"])  haftmann@58023  1321  apply (rule exI[of _ l\<^sub>0])  haftmann@58023  1322  apply (simp add: l\<^sub>0_props)  haftmann@58023  1323  done  haftmann@58023  1324  from someI_ex[OF this] have "l \ 0" and "\x\A. x dvd l" and "euclidean_size l = n"  haftmann@58023  1325  unfolding l_def by simp_all  haftmann@58023  1326  {  haftmann@58023  1327  fix l' assume "\x\A. x dvd l'"  haftmann@58023  1328  with \x\A. x dvd l have "\x\A. x dvd gcd l l'" by (auto intro: gcd_greatest)  haftmann@59009  1329  moreover from l \ 0 have "gcd l l' \ 0" by simp  haftmann@58023  1330  ultimately have "\b. b \ 0 \ (\x\A. x dvd b) \ euclidean_size b = euclidean_size (gcd l l')"  haftmann@58023  1331  by (intro exI[of _ "gcd l l'"], auto)  haftmann@58023  1332  hence "euclidean_size (gcd l l') \ n" by (subst n_def) (rule Least_le)  haftmann@58023  1333  moreover have "euclidean_size (gcd l l') \ n"  haftmann@58023  1334  proof -  haftmann@58023  1335  have "gcd l l' dvd l" by simp  haftmann@58023  1336  then obtain a where "l = gcd l l' * a" unfolding dvd_def by blast  haftmann@58023  1337  with l \ 0 have "a \ 0" by auto  haftmann@58023  1338  hence "euclidean_size (gcd l l') \ euclidean_size (gcd l l' * a)"  haftmann@58023  1339  by (rule size_mult_mono)  haftmann@58023  1340  also have "gcd l l' * a = l" using l = gcd l l' * a ..  haftmann@58023  1341  also note euclidean_size l = n  haftmann@58023  1342  finally show "euclidean_size (gcd l l') \ n" .  haftmann@58023  1343  qed  haftmann@58023  1344  ultimately have "euclidean_size l = euclidean_size (gcd l l')"  haftmann@58023  1345  by (intro le_antisym, simp_all add: euclidean_size l = n)  haftmann@58023  1346  with l \ 0 have "l dvd gcd l l'" by (blast intro: dvd_euclidean_size_eq_imp_dvd)  haftmann@58023  1347  hence "l dvd l'" by (blast dest: dvd_gcd_D2)  haftmann@58023  1348  }  haftmann@58023  1349 haftmann@58023  1350  with (\x\A. x dvd l) and normalisation_factor_is_unit[OF l \ 0] and l \ 0  haftmann@58023  1351  have "(\x\A. x dvd l div normalisation_factor l) \  haftmann@58023  1352  (\l'. (\x\A. x dvd l') \ l div normalisation_factor l dvd l') \  haftmann@58023  1353  normalisation_factor (l div normalisation_factor l) =  haftmann@58023  1354  (if l div normalisation_factor l = 0 then 0 else 1)"  haftmann@58023  1355  by (auto simp: unit_simps)  haftmann@58023  1356  also from True have "l div normalisation_factor l = Lcm A"  haftmann@58023  1357  by (simp add: Lcm_Lcm_eucl Lcm_eucl_def Let_def n_def l_def)  haftmann@58023  1358  finally show ?thesis .  haftmann@58023  1359  qed  haftmann@58023  1360  note A = this  haftmann@58023  1361 haftmann@58023  1362  {fix x assume "x \ A" then show "x dvd Lcm A" using A by blast}  haftmann@58023  1363  {fix l' assume "\x\A. x dvd l'" then show "Lcm A dvd l'" using A by blast}  haftmann@58023  1364  from A show "normalisation_factor (Lcm A) = (if Lcm A = 0 then 0 else 1)" by blast  haftmann@58023  1365 qed  haftmann@58023  1366   haftmann@58023  1367 lemma LcmI:  haftmann@58023  1368  "(\x. x\A \ x dvd l) \ (\l'. (\x\A. x dvd l') \ l dvd l') \  haftmann@58023  1369  normalisation_factor l = (if l = 0 then 0 else 1) \ l = Lcm A"  haftmann@58023  1370  by (intro normed_associated_imp_eq)  haftmann@58023  1371  (auto intro: Lcm_dvd dvd_Lcm simp: associated_def)  haftmann@58023  1372 haftmann@58023  1373 lemma Lcm_subset:  haftmann@58023  1374  "A \ B \ Lcm A dvd Lcm B"  haftmann@58023  1375  by (blast intro: Lcm_dvd dvd_Lcm)  haftmann@58023  1376 haftmann@58023  1377 lemma Lcm_Un:  haftmann@58023  1378  "Lcm (A \ B) = lcm (Lcm A) (Lcm B)"  haftmann@58023  1379  apply (rule lcmI)  haftmann@58023  1380  apply (blast intro: Lcm_subset)  haftmann@58023  1381  apply (blast intro: Lcm_subset)  haftmann@58023  1382  apply (intro Lcm_dvd ballI, elim UnE)  haftmann@58023  1383  apply (rule dvd_trans, erule dvd_Lcm, assumption)  haftmann@58023  1384  apply (rule dvd_trans, erule dvd_Lcm, assumption)  haftmann@58023  1385  apply simp  haftmann@58023  1386  done  haftmann@58023  1387 haftmann@58023  1388 lemma Lcm_1_iff:  haftmann@58023  1389  "Lcm A = 1 \ (\x\A. is_unit x)"  haftmann@58023  1390 proof  haftmann@58023  1391  assume "Lcm A = 1"  haftmann@58023  1392  then show "\x\A. is_unit x" unfolding is_unit_def by auto  haftmann@58023  1393 qed (rule LcmI [symmetric], auto)  haftmann@58023  1394 haftmann@58023  1395 lemma Lcm_no_units:  haftmann@58023  1396  "Lcm A = Lcm (A - {x. is_unit x})"  haftmann@58023  1397 proof -  haftmann@58023  1398  have "(A - {x. is_unit x}) \ {x\A. is_unit x} = A" by blast  haftmann@58023  1399  hence "Lcm A = lcm (Lcm (A - {x. is_unit x})) (Lcm {x\A. is_unit x})"  haftmann@58023  1400  by (simp add: Lcm_Un[symmetric])  haftmann@58023  1401  also have "Lcm {x\A. is_unit x} = 1" by (simp add: Lcm_1_iff)  haftmann@58023  1402  finally show ?thesis by simp  haftmann@58023  1403 qed  haftmann@58023  1404 haftmann@58023  1405 lemma Lcm_empty [simp]:  haftmann@58023  1406  "Lcm {} = 1"  haftmann@58023  1407  by (simp add: Lcm_1_iff)  haftmann@58023  1408 haftmann@58023  1409 lemma Lcm_eq_0 [simp]:  haftmann@58023  1410  "0 \ A \ Lcm A = 0"  haftmann@58023  1411  by (drule dvd_Lcm) simp  haftmann@58023  1412 haftmann@58023  1413 lemma Lcm0_iff':  haftmann@58023  1414  "Lcm A = 0 \ \(\l. l \ 0 \ (\x\A. x dvd l))"  haftmann@58023  1415 proof  haftmann@58023  1416  assume "Lcm A = 0"  haftmann@58023  1417  show "\(\l. l \ 0 \ (\x\A. x dvd l))"  haftmann@58023  1418  proof  haftmann@58023  1419  assume ex: "\l. l \ 0 \ (\x\A. x dvd l)"  haftmann@58023  1420  then obtain l\<^sub>0 where l\<^sub>0_props: "l\<^sub>0 \ 0 \ (\x\A. x dvd l\<^sub>0)" by blast  haftmann@58023  1421  def n \ "LEAST n. \l. l \ 0 \ (\x\A. x dvd l) \ euclidean_size l = n"  haftmann@58023  1422  def l \ "SOME l. l \ 0 \ (\x\A. x dvd l) \ euclidean_size l = n"  haftmann@58023  1423  have "\l. l \ 0 \ (\x\A. x dvd l) \ euclidean_size l = n"  haftmann@58023  1424  apply (subst n_def)  haftmann@58023  1425  apply (rule LeastI[of _ "euclidean_size l\<^sub>0"])  haftmann@58023  1426  apply (rule exI[of _ l\<^sub>0])  haftmann@58023  1427  apply (simp add: l\<^sub>0_props)  haftmann@58023  1428  done  haftmann@58023  1429  from someI_ex[OF this] have "l \ 0" unfolding l_def by simp_all  haftmann@58023  1430  hence "l div normalisation_factor l \ 0" by simp  haftmann@58023  1431  also from ex have "l div normalisation_factor l = Lcm A"  haftmann@58023  1432  by (simp only: Lcm_Lcm_eucl Lcm_eucl_def n_def l_def if_True Let_def)  haftmann@58023  1433  finally show False using Lcm A = 0 by contradiction  haftmann@58023  1434  qed  haftmann@58023  1435 qed (simp only: Lcm_Lcm_eucl Lcm_eucl_def if_False)  haftmann@58023  1436 haftmann@58023  1437 lemma Lcm0_iff [simp]:  haftmann@58023  1438  "finite A \ Lcm A = 0 \ 0 \ A"  haftmann@58023  1439 proof -  haftmann@58023  1440  assume "finite A"  haftmann@58023  1441  have "0 \ A \ Lcm A = 0" by (intro dvd_0_left dvd_Lcm)  haftmann@58023  1442  moreover {  haftmann@58023  1443  assume "0 \ A"  haftmann@58023  1444  hence "\A \ 0"  haftmann@58023  1445  apply (induct rule: finite_induct[OF finite A])  haftmann@58023  1446  apply simp  haftmann@58023  1447  apply (subst setprod.insert, assumption, assumption)  haftmann@58023  1448  apply (rule no_zero_divisors)  haftmann@58023  1449  apply blast+  haftmann@58023  1450  done  haftmann@59010  1451  moreover from finite A have "\x\A. x dvd \A" by blast  haftmann@58023  1452  ultimately have "\l. l \ 0 \ (\x\A. x dvd l)" by blast  haftmann@58023  1453  with Lcm0_iff' have "Lcm A \ 0" by simp  haftmann@58023  1454  }  haftmann@58023  1455  ultimately show "Lcm A = 0 \ 0 \ A" by blast  haftmann@58023  1456 qed  haftmann@58023  1457 haftmann@58023  1458 lemma Lcm_no_multiple:  haftmann@58023  1459  "(\m. m \ 0 \ (\x\A. \x dvd m)) \ Lcm A = 0"  haftmann@58023  1460 proof -  haftmann@58023  1461  assume "\m. m \ 0 \ (\x\A. \x dvd m)"  haftmann@58023  1462  hence "\(\l. l \ 0 \ (\x\A. x dvd l))" by blast  haftmann@58023  1463  then show "Lcm A = 0" by (simp only: Lcm_Lcm_eucl Lcm_eucl_def if_False)  haftmann@58023  1464 qed  haftmann@58023  1465 haftmann@58023  1466 lemma Lcm_insert [simp]:  haftmann@58023  1467  "Lcm (insert a A) = lcm a (Lcm A)"  haftmann@58023  1468 proof (rule lcmI)  haftmann@58023  1469  fix l assume "a dvd l" and "Lcm A dvd l"  haftmann@58023  1470  hence "\x\A. x dvd l" by (blast intro: dvd_trans dvd_Lcm)  haftmann@58023  1471  with a dvd l show "Lcm (insert a A) dvd l" by (force intro: Lcm_dvd)  haftmann@58023  1472 qed (auto intro: Lcm_dvd dvd_Lcm)  haftmann@58023  1473   haftmann@58023  1474 lemma Lcm_finite:  haftmann@58023  1475  assumes "finite A"  haftmann@58023  1476  shows "Lcm A = Finite_Set.fold lcm 1 A"  haftmann@58023  1477  by (induct rule: finite.induct[OF finite A])  haftmann@58023  1478  (simp_all add: comp_fun_idem.fold_insert_idem[OF comp_fun_idem_lcm])  haftmann@58023  1479 haftmann@58023  1480 lemma Lcm_set [code, code_unfold]:  haftmann@58023  1481  "Lcm (set xs) = fold lcm xs 1"  haftmann@58023  1482  using comp_fun_idem.fold_set_fold[OF comp_fun_idem_lcm] Lcm_finite by (simp add: ac_simps)  haftmann@58023  1483 haftmann@58023  1484 lemma Lcm_singleton [simp]:  haftmann@58023  1485  "Lcm {a} = a div normalisation_factor a"  haftmann@58023  1486  by simp  haftmann@58023  1487 haftmann@58023  1488 lemma Lcm_2 [simp]:  haftmann@58023  1489  "Lcm {a,b} = lcm a b"  haftmann@58023  1490  by (simp only: Lcm_insert Lcm_empty lcm_1_right)  haftmann@58023  1491  (cases "b = 0", simp, rule lcm_div_unit2, simp)  haftmann@58023  1492 haftmann@58023  1493 lemma Lcm_coprime:  haftmann@58023  1494  assumes "finite A" and "A \ {}"  haftmann@58023  1495  assumes "\a b. a \ A \ b \ A \ a \ b \ gcd a b = 1"  haftmann@58023  1496  shows "Lcm A = \A div normalisation_factor (\A)"  haftmann@58023  1497 using assms proof (induct rule: finite_ne_induct)  haftmann@58023  1498  case (insert a A)  haftmann@58023  1499  have "Lcm (insert a A) = lcm a (Lcm A)" by simp  haftmann@58023  1500  also from insert have "Lcm A = \A div normalisation_factor (\A)" by blast  haftmann@58023  1501  also have "lcm a \ = lcm a (\A)" by (cases "\A = 0") (simp_all add: lcm_div_unit2)  haftmann@58023  1502  also from insert have "gcd a (\A) = 1" by (subst gcd.commute, intro setprod_coprime) auto  haftmann@58023  1503  with insert have "lcm a (\A) = \(insert a A) div normalisation_factor (\(insert a A))"  haftmann@58023  1504  by (simp add: lcm_coprime)  haftmann@58023  1505  finally show ?case .  haftmann@58023  1506 qed simp  haftmann@58023  1507   haftmann@58023  1508 lemma Lcm_coprime':  haftmann@58023  1509  "card A \ 0 \ (\a b. a \ A \ b \ A \ a \ b \ gcd a b = 1)  haftmann@58023  1510  \ Lcm A = \A div normalisation_factor (\A)"  haftmann@58023  1511  by (rule Lcm_coprime) (simp_all add: card_eq_0_iff)  haftmann@58023  1512 haftmann@58023  1513 lemma Gcd_Lcm:  haftmann@58023  1514  "Gcd A = Lcm {d. \x\A. d dvd x}"  haftmann@58023  1515  by (simp add: Gcd_Gcd_eucl Lcm_Lcm_eucl Gcd_eucl_def)  haftmann@58023  1516 haftmann@58023  1517 lemma Gcd_dvd [simp]: "x \ A \ Gcd A dvd x"  haftmann@58023  1518  and dvd_Gcd [simp]: "(\x\A. g' dvd x) \ g' dvd Gcd A"  haftmann@58023  1519  and normalisation_factor_Gcd [simp]:  haftmann@58023  1520  "normalisation_factor (Gcd A) = (if Gcd A = 0 then 0 else 1)"  haftmann@58023  1521 proof -  haftmann@58023  1522  fix x assume "x \ A"  haftmann@58023  1523  hence "Lcm {d. \x\A. d dvd x} dvd x" by (intro Lcm_dvd) blast  haftmann@58023  1524  then show "Gcd A dvd x" by (simp add: Gcd_Lcm)  haftmann@58023  1525 next  haftmann@58023  1526  fix g' assume "\x\A. g' dvd x"  haftmann@58023  1527  hence "g' dvd Lcm {d. \x\A. d dvd x}" by (intro dvd_Lcm) blast  haftmann@58023  1528  then show "g' dvd Gcd A" by (simp add: Gcd_Lcm)  haftmann@58023  1529 next  haftmann@58023  1530  show "normalisation_factor (Gcd A) = (if Gcd A = 0 then 0 else 1)"  haftmann@59009  1531  by (simp add: Gcd_Lcm)  haftmann@58023  1532 qed  haftmann@58023  1533 haftmann@58023  1534 lemma GcdI:  haftmann@58023  1535  "(\x. x\A \ l dvd x) \ (\l'. (\x\A. l' dvd x) \ l' dvd l) \  haftmann@58023  1536  normalisation_factor l = (if l = 0 then 0 else 1) \ l = Gcd A"  haftmann@58023  1537  by (intro normed_associated_imp_eq)  haftmann@58023  1538  (auto intro: Gcd_dvd dvd_Gcd simp: associated_def)  haftmann@58023  1539 haftmann@58023  1540 lemma Lcm_Gcd:  haftmann@58023  1541  "Lcm A = Gcd {m. \x\A. x dvd m}"  haftmann@58023  1542  by (rule LcmI[symmetric]) (auto intro: dvd_Gcd Gcd_dvd)  haftmann@58023  1543 haftmann@58023  1544 lemma Gcd_0_iff:  haftmann@58023  1545  "Gcd A = 0 \ A \ {0}"  haftmann@58023  1546  apply (rule iffI)  haftmann@58023  1547  apply (rule subsetI, drule Gcd_dvd, simp)  haftmann@58023  1548  apply (auto intro: GcdI[symmetric])  haftmann@58023  1549  done  haftmann@58023  1550 haftmann@58023  1551 lemma Gcd_empty [simp]:  haftmann@58023  1552  "Gcd {} = 0"  haftmann@58023  1553  by (simp add: Gcd_0_iff)  haftmann@58023  1554 haftmann@58023  1555 lemma Gcd_1:  haftmann@58023  1556  "1 \ A \ Gcd A = 1"  haftmann@58023  1557  by (intro GcdI[symmetric]) (auto intro: Gcd_dvd dvd_Gcd)  haftmann@58023  1558 haftmann@58023  1559 lemma Gcd_insert [simp]:  haftmann@58023  1560  "Gcd (insert a A) = gcd a (Gcd A)"  haftmann@58023  1561 proof (rule gcdI)  haftmann@58023  1562  fix l assume "l dvd a" and "l dvd Gcd A"  haftmann@58023  1563  hence "\x\A. l dvd x" by (blast intro: dvd_trans Gcd_dvd)  haftmann@58023  1564  with l dvd a show "l dvd Gcd (insert a A)" by (force intro: Gcd_dvd)  haftmann@59009  1565 qed auto  haftmann@58023  1566 haftmann@58023  1567 lemma Gcd_finite:  haftmann@58023  1568  assumes "finite A"  haftmann@58023  1569  shows "Gcd A = Finite_Set.fold gcd 0 A"  haftmann@58023  1570  by (induct rule: finite.induct[OF finite A])  haftmann@58023  1571  (simp_all add: comp_fun_idem.fold_insert_idem[OF comp_fun_idem_gcd])  haftmann@58023  1572 haftmann@58023  1573 lemma Gcd_set [code, code_unfold]:  haftmann@58023  1574  "Gcd (set xs) = fold gcd xs 0"  haftmann@58023  1575  using comp_fun_idem.fold_set_fold[OF comp_fun_idem_gcd] Gcd_finite by (simp add: ac_simps)  haftmann@58023  1576 haftmann@58023  1577 lemma Gcd_singleton [simp]: "Gcd {a} = a div normalisation_factor a"  haftmann@58023  1578  by (simp add: gcd_0)  haftmann@58023  1579 haftmann@58023  1580 lemma Gcd_2 [simp]: "Gcd {a,b} = gcd a b"  haftmann@58023  1581  by (simp only: Gcd_insert Gcd_empty gcd_0) (cases "b = 0", simp, rule gcd_div_unit2, simp)  haftmann@58023  1582 haftmann@58023  1583 end  haftmann@58023  1584 haftmann@58023  1585 text {*  haftmann@58023  1586  A Euclidean ring is a Euclidean semiring with additive inverses. It provides a  haftmann@58023  1587  few more lemmas; in particular, Bezout's lemma holds for any Euclidean ring.  haftmann@58023  1588 *}  haftmann@58023  1589 haftmann@58023  1590 class euclidean_ring = euclidean_semiring + idom  haftmann@58023  1591 haftmann@58023  1592 class euclidean_ring_gcd = euclidean_semiring_gcd + idom  haftmann@58023  1593 begin  haftmann@58023  1594 haftmann@58023  1595 subclass euclidean_ring ..  haftmann@58023  1596 haftmann@58023  1597 lemma gcd_neg1 [simp]:  haftmann@58023  1598  "gcd (-x) y = gcd x y"  haftmann@59009  1599  by (rule sym, rule gcdI, simp_all add: gcd_greatest)  haftmann@58023  1600 haftmann@58023  1601 lemma gcd_neg2 [simp]:  haftmann@58023  1602  "gcd x (-y) = gcd x y"  haftmann@59009  1603  by (rule sym, rule gcdI, simp_all add: gcd_greatest)  haftmann@58023  1604 haftmann@58023  1605 lemma gcd_neg_numeral_1 [simp]:  haftmann@58023  1606  "gcd (- numeral n) x = gcd (numeral n) x"  haftmann@58023  1607  by (fact gcd_neg1)  haftmann@58023  1608 haftmann@58023  1609 lemma gcd_neg_numeral_2 [simp]:  haftmann@58023  1610  "gcd x (- numeral n) = gcd x (numeral n)"  haftmann@58023  1611  by (fact gcd_neg2)  haftmann@58023  1612 haftmann@58023  1613 lemma gcd_diff1: "gcd (m - n) n = gcd m n"  haftmann@58023  1614  by (subst diff_conv_add_uminus, subst gcd_neg2[symmetric], subst gcd_add1, simp)  haftmann@58023  1615 haftmann@58023  1616 lemma gcd_diff2: "gcd (n - m) n = gcd m n"  haftmann@58023  1617  by (subst gcd_neg1[symmetric], simp only: minus_diff_eq gcd_diff1)  haftmann@58023  1618 haftmann@58023  1619 lemma coprime_minus_one [simp]: "gcd (n - 1) n = 1"  haftmann@58023  1620 proof -  haftmann@58023  1621  have "gcd (n - 1) n = gcd n (n - 1)" by (fact gcd.commute)  haftmann@58023  1622  also have "\ = gcd ((n - 1) + 1) (n - 1)" by simp  haftmann@58023  1623  also have "\ = 1" by (rule coprime_plus_one)  haftmann@58023  1624  finally show ?thesis .  haftmann@58023  1625 qed  haftmann@58023  1626 haftmann@58023  1627 lemma lcm_neg1 [simp]: "lcm (-x) y = lcm x y"  haftmann@58023  1628  by (rule sym, rule lcmI, simp_all add: lcm_least lcm_zero)  haftmann@58023  1629 haftmann@58023  1630 lemma lcm_neg2 [simp]: "lcm x (-y) = lcm x y"  haftmann@58023  1631  by (rule sym, rule lcmI, simp_all add: lcm_least lcm_zero)  haftmann@58023  1632 haftmann@58023  1633 lemma lcm_neg_numeral_1 [simp]: "lcm (- numeral n) x = lcm (numeral n) x"  haftmann@58023  1634  by (fact lcm_neg1)  haftmann@58023  1635 haftmann@58023  1636 lemma lcm_neg_numeral_2 [simp]: "lcm x (- numeral n) = lcm x (numeral n)"  haftmann@58023  1637  by (fact lcm_neg2)  haftmann@58023  1638 haftmann@58023  1639 function euclid_ext :: "'a \ 'a \ 'a \ 'a \ 'a" where  haftmann@58023  1640  "euclid_ext a b =  haftmann@58023  1641  (if b = 0 then  haftmann@58023  1642  let x = ring_inv (normalisation_factor a) in (x, 0, a * x)  haftmann@58023  1643  else  haftmann@58023  1644  case euclid_ext b (a mod b) of  haftmann@58023  1645  (s,t,c) \ (t, s - t * (a div b), c))"  haftmann@58023  1646  by (pat_completeness, simp)  haftmann@58023  1647  termination by (relation "measure (euclidean_size \ snd)", simp_all)  haftmann@58023  1648 haftmann@58023  1649 declare euclid_ext.simps [simp del]  haftmann@58023  1650 haftmann@58023  1651 lemma euclid_ext_0:  haftmann@58023  1652  "euclid_ext a 0 = (ring_inv (normalisation_factor a), 0, a * ring_inv (normalisation_factor a))"  haftmann@58023  1653  by (subst euclid_ext.simps, simp add: Let_def)  haftmann@58023  1654 haftmann@58023  1655 lemma euclid_ext_non_0:  haftmann@58023  1656  "b \ 0 \ euclid_ext a b = (case euclid_ext b (a mod b) of  haftmann@58023  1657  (s,t,c) \ (t, s - t * (a div b), c))"  haftmann@58023  1658  by (subst euclid_ext.simps, simp)  haftmann@58023  1659 haftmann@58023  1660 definition euclid_ext' :: "'a \ 'a \ 'a \ 'a"  haftmann@58023  1661 where  haftmann@58023  1662  "euclid_ext' a b = (case euclid_ext a b of (s, t, _) \ (s, t))"  haftmann@58023  1663 haftmann@58023  1664 lemma euclid_ext_gcd [simp]:  haftmann@58023  1665  "(case euclid_ext a b of (_,_,t) \ t) = gcd a b"  haftmann@58023  1666 proof (induct a b rule: euclid_ext.induct)  haftmann@58023  1667  case (1 a b)  haftmann@58023  1668  then show ?case  haftmann@58023  1669  proof (cases "b = 0")  haftmann@58023  1670  case True  haftmann@58023  1671  then show ?thesis by (cases "a = 0")  haftmann@58023  1672  (simp_all add: euclid_ext_0 unit_div mult_ac unit_simps gcd_0)  haftmann@58023  1673  next  haftmann@58023  1674  case False with 1 show ?thesis  haftmann@58023  1675  by (simp add: euclid_ext_non_0 ac_simps split: prod.split prod.split_asm)  haftmann@58023  1676  qed  haftmann@58023  1677 qed  haftmann@58023  1678 haftmann@58023  1679 lemma euclid_ext_gcd' [simp]:  haftmann@58023  1680  "euclid_ext a b = (r, s, t) \ t = gcd a b"  haftmann@58023  1681  by (insert euclid_ext_gcd[of a b], drule (1) subst, simp)  haftmann@58023  1682 haftmann@58023  1683 lemma euclid_ext_correct:  haftmann@58023  1684  "case euclid_ext x y of (s,t,c) \ s*x + t*y = c"  haftmann@58023  1685 proof (induct x y rule: euclid_ext.induct)  haftmann@58023  1686  case (1 x y)  haftmann@58023  1687  show ?case  haftmann@58023  1688  proof (cases "y = 0")  haftmann@58023  1689  case True  haftmann@58023  1690  then show ?thesis by (simp add: euclid_ext_0 mult_ac)  haftmann@58023  1691  next  haftmann@58023  1692  case False  haftmann@58023  1693  obtain s t c where stc: "euclid_ext y (x mod y) = (s,t,c)"  haftmann@58023  1694  by (cases "euclid_ext y (x mod y)", blast)  haftmann@58023  1695  from 1 have "c = s * y + t * (x mod y)" by (simp add: stc False)  haftmann@58023  1696  also have "... = t*((x div y)*y + x mod y) + (s - t * (x div y))*y"  haftmann@58023  1697  by (simp add: algebra_simps)  haftmann@58023  1698  also have "(x div y)*y + x mod y = x" using mod_div_equality .  haftmann@58023  1699  finally show ?thesis  haftmann@58023  1700  by (subst euclid_ext.simps, simp add: False stc)  haftmann@58023  1701  qed  haftmann@58023  1702 qed  haftmann@58023  1703 haftmann@58023  1704 lemma euclid_ext'_correct:  haftmann@58023  1705  "fst (euclid_ext' a b) * a + snd (euclid_ext' a b) * b = gcd a b"  haftmann@58023  1706 proof-  haftmann@58023  1707  obtain s t c where "euclid_ext a b = (s,t,c)"  haftmann@58023  1708  by (cases "euclid_ext a b", blast)  haftmann@58023  1709  with euclid_ext_correct[of a b] euclid_ext_gcd[of a b]  haftmann@58023  1710  show ?thesis unfolding euclid_ext'_def by simp  haftmann@58023  1711 qed  haftmann@58023  1712 haftmann@58023  1713 lemma bezout: "\s t. s * x + t * y = gcd x y"  haftmann@58023  1714  using euclid_ext'_correct by blast  haftmann@58023  1715 haftmann@58023  1716 lemma euclid_ext'_0 [simp]: "euclid_ext' x 0 = (ring_inv (normalisation_factor x), 0)"  haftmann@58023  1717  by (simp add: bezw_def euclid_ext'_def euclid_ext_0)  haftmann@58023  1718 haftmann@58023  1719 lemma euclid_ext'_non_0: "y \ 0 \ euclid_ext' x y = (snd (euclid_ext' y (x mod y)),  haftmann@58023  1720  fst (euclid_ext' y (x mod y)) - snd (euclid_ext' y (x mod y)) * (x div y))"  haftmann@58023  1721  by (cases "euclid_ext y (x mod y)")  haftmann@58023  1722  (simp add: euclid_ext'_def euclid_ext_non_0)  haftmann@58023  1723   haftmann@58023  1724 end  haftmann@58023  1725 haftmann@58023  1726 instantiation nat :: euclidean_semiring  haftmann@58023  1727 begin  haftmann@58023  1728 haftmann@58023  1729 definition [simp]:  haftmann@58023  1730  "euclidean_size_nat = (id :: nat \ nat)"  haftmann@58023  1731 haftmann@58023  1732 definition [simp]:  haftmann@58023  1733  "normalisation_factor_nat (n::nat) = (if n = 0 then 0 else 1 :: nat)"  haftmann@58023  1734 haftmann@58023  1735 instance proof  haftmann@58023  1736 qed (simp_all add: is_unit_def)  haftmann@58023  1737 haftmann@58023  1738 end  haftmann@58023  1739 haftmann@58023  1740 instantiation int :: euclidean_ring  haftmann@58023  1741 begin  haftmann@58023  1742 haftmann@58023  1743 definition [simp]:  haftmann@58023  1744  "euclidean_size_int = (nat \ abs :: int \ nat)"  haftmann@58023  1745 haftmann@58023  1746 definition [simp]:  haftmann@58023  1747  "normalisation_factor_int = (sgn :: int \ int)"  haftmann@58023  1748 haftmann@58023  1749 instance proof  haftmann@58023  1750  case goal2 then show ?case by (auto simp add: abs_mult nat_mult_distrib)  haftmann@58023  1751 next  haftmann@58023  1752  case goal3 then show ?case by (simp add: zsgn_def is_unit_def)  haftmann@58023  1753 next  haftmann@58023  1754  case goal5 then show ?case by (auto simp: zsgn_def is_unit_def)  haftmann@58023  1755 next  haftmann@58023  1756  case goal6 then show ?case by (auto split: abs_split simp: zsgn_def is_unit_def)  haftmann@58023  1757 qed (auto simp: sgn_times split: abs_split)  haftmann@58023  1758 haftmann@58023  1759 end  haftmann@58023  1760 haftmann@58023  1761 end  haftmann@58023  1762