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