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