src/HOL/GCD.thy
author haftmann
Wed Dec 03 15:58:44 2008 +0100 (2008-12-03)
changeset 28952 15a4b2cf8c34
parent 28562 src/HOL/Library/GCD.thy@4e74209f113e
child 29655 ac31940cfb69
permissions -rw-r--r--
made repository layout more coherent with logical distribution structure; stripped some $Id$s
     1 (*  Title:      HOL/GCD.thy
     2     ID:         $Id$
     3     Author:     Christophe Tabacznyj and Lawrence C Paulson
     4     Copyright   1996  University of Cambridge
     5 *)
     6 
     7 header {* The Greatest Common Divisor *}
     8 
     9 theory GCD
    10 imports Plain Presburger
    11 begin
    12 
    13 text {*
    14   See \cite{davenport92}. \bigskip
    15 *}
    16 
    17 subsection {* Specification of GCD on nats *}
    18 
    19 definition
    20   is_gcd :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> bool" where -- {* @{term gcd} as a relation *}
    21   [code del]: "is_gcd m n p \<longleftrightarrow> p dvd m \<and> p dvd n \<and>
    22     (\<forall>d. d dvd m \<longrightarrow> d dvd n \<longrightarrow> d dvd p)"
    23 
    24 text {* Uniqueness *}
    25 
    26 lemma is_gcd_unique: "is_gcd a b m \<Longrightarrow> is_gcd a b n \<Longrightarrow> m = n"
    27   by (simp add: is_gcd_def) (blast intro: dvd_anti_sym)
    28 
    29 text {* Connection to divides relation *}
    30 
    31 lemma is_gcd_dvd: "is_gcd a b m \<Longrightarrow> k dvd a \<Longrightarrow> k dvd b \<Longrightarrow> k dvd m"
    32   by (auto simp add: is_gcd_def)
    33 
    34 text {* Commutativity *}
    35 
    36 lemma is_gcd_commute: "is_gcd m n k = is_gcd n m k"
    37   by (auto simp add: is_gcd_def)
    38 
    39 
    40 subsection {* GCD on nat by Euclid's algorithm *}
    41 
    42 fun
    43   gcd  :: "nat => nat => nat"
    44 where
    45   "gcd m n = (if n = 0 then m else gcd n (m mod n))"
    46 lemma gcd_induct [case_names "0" rec]:
    47   fixes m n :: nat
    48   assumes "\<And>m. P m 0"
    49     and "\<And>m n. 0 < n \<Longrightarrow> P n (m mod n) \<Longrightarrow> P m n"
    50   shows "P m n"
    51 proof (induct m n rule: gcd.induct)
    52   case (1 m n) with assms show ?case by (cases "n = 0") simp_all
    53 qed
    54 
    55 lemma gcd_0 [simp, algebra]: "gcd m 0 = m"
    56   by simp
    57 
    58 lemma gcd_0_left [simp,algebra]: "gcd 0 m = m"
    59   by simp
    60 
    61 lemma gcd_non_0: "n > 0 \<Longrightarrow> gcd m n = gcd n (m mod n)"
    62   by simp
    63 
    64 lemma gcd_1 [simp, algebra]: "gcd m (Suc 0) = 1"
    65   by simp
    66 
    67 declare gcd.simps [simp del]
    68 
    69 text {*
    70   \medskip @{term "gcd m n"} divides @{text m} and @{text n}.  The
    71   conjunctions don't seem provable separately.
    72 *}
    73 
    74 lemma gcd_dvd1 [iff, algebra]: "gcd m n dvd m"
    75   and gcd_dvd2 [iff, algebra]: "gcd m n dvd n"
    76   apply (induct m n rule: gcd_induct)
    77      apply (simp_all add: gcd_non_0)
    78   apply (blast dest: dvd_mod_imp_dvd)
    79   done
    80 
    81 text {*
    82   \medskip Maximality: for all @{term m}, @{term n}, @{term k}
    83   naturals, if @{term k} divides @{term m} and @{term k} divides
    84   @{term n} then @{term k} divides @{term "gcd m n"}.
    85 *}
    86 
    87 lemma gcd_greatest: "k dvd m \<Longrightarrow> k dvd n \<Longrightarrow> k dvd gcd m n"
    88   by (induct m n rule: gcd_induct) (simp_all add: gcd_non_0 dvd_mod)
    89 
    90 text {*
    91   \medskip Function gcd yields the Greatest Common Divisor.
    92 *}
    93 
    94 lemma is_gcd: "is_gcd m n (gcd m n) "
    95   by (simp add: is_gcd_def gcd_greatest)
    96 
    97 
    98 subsection {* Derived laws for GCD *}
    99 
   100 lemma gcd_greatest_iff [iff, algebra]: "k dvd gcd m n \<longleftrightarrow> k dvd m \<and> k dvd n"
   101   by (blast intro!: gcd_greatest intro: dvd_trans)
   102 
   103 lemma gcd_zero[algebra]: "gcd m n = 0 \<longleftrightarrow> m = 0 \<and> n = 0"
   104   by (simp only: dvd_0_left_iff [symmetric] gcd_greatest_iff)
   105 
   106 lemma gcd_commute: "gcd m n = gcd n m"
   107   apply (rule is_gcd_unique)
   108    apply (rule is_gcd)
   109   apply (subst is_gcd_commute)
   110   apply (simp add: is_gcd)
   111   done
   112 
   113 lemma gcd_assoc: "gcd (gcd k m) n = gcd k (gcd m n)"
   114   apply (rule is_gcd_unique)
   115    apply (rule is_gcd)
   116   apply (simp add: is_gcd_def)
   117   apply (blast intro: dvd_trans)
   118   done
   119 
   120 lemma gcd_1_left [simp, algebra]: "gcd (Suc 0) m = 1"
   121   by (simp add: gcd_commute)
   122 
   123 text {*
   124   \medskip Multiplication laws
   125 *}
   126 
   127 lemma gcd_mult_distrib2: "k * gcd m n = gcd (k * m) (k * n)"
   128     -- {* \cite[page 27]{davenport92} *}
   129   apply (induct m n rule: gcd_induct)
   130    apply simp
   131   apply (case_tac "k = 0")
   132    apply (simp_all add: mod_geq gcd_non_0 mod_mult_distrib2)
   133   done
   134 
   135 lemma gcd_mult [simp, algebra]: "gcd k (k * n) = k"
   136   apply (rule gcd_mult_distrib2 [of k 1 n, simplified, symmetric])
   137   done
   138 
   139 lemma gcd_self [simp, algebra]: "gcd k k = k"
   140   apply (rule gcd_mult [of k 1, simplified])
   141   done
   142 
   143 lemma relprime_dvd_mult: "gcd k n = 1 ==> k dvd m * n ==> k dvd m"
   144   apply (insert gcd_mult_distrib2 [of m k n])
   145   apply simp
   146   apply (erule_tac t = m in ssubst)
   147   apply simp
   148   done
   149 
   150 lemma relprime_dvd_mult_iff: "gcd k n = 1 ==> (k dvd m * n) = (k dvd m)"
   151   by (auto intro: relprime_dvd_mult dvd_mult2)
   152 
   153 lemma gcd_mult_cancel: "gcd k n = 1 ==> gcd (k * m) n = gcd m n"
   154   apply (rule dvd_anti_sym)
   155    apply (rule gcd_greatest)
   156     apply (rule_tac n = k in relprime_dvd_mult)
   157      apply (simp add: gcd_assoc)
   158      apply (simp add: gcd_commute)
   159     apply (simp_all add: mult_commute)
   160   apply (blast intro: dvd_mult)
   161   done
   162 
   163 
   164 text {* \medskip Addition laws *}
   165 
   166 lemma gcd_add1 [simp, algebra]: "gcd (m + n) n = gcd m n"
   167   by (cases "n = 0") (auto simp add: gcd_non_0)
   168 
   169 lemma gcd_add2 [simp, algebra]: "gcd m (m + n) = gcd m n"
   170 proof -
   171   have "gcd m (m + n) = gcd (m + n) m" by (rule gcd_commute)
   172   also have "... = gcd (n + m) m" by (simp add: add_commute)
   173   also have "... = gcd n m" by simp
   174   also have  "... = gcd m n" by (rule gcd_commute)
   175   finally show ?thesis .
   176 qed
   177 
   178 lemma gcd_add2' [simp, algebra]: "gcd m (n + m) = gcd m n"
   179   apply (subst add_commute)
   180   apply (rule gcd_add2)
   181   done
   182 
   183 lemma gcd_add_mult[algebra]: "gcd m (k * m + n) = gcd m n"
   184   by (induct k) (simp_all add: add_assoc)
   185 
   186 lemma gcd_dvd_prod: "gcd m n dvd m * n" 
   187   using mult_dvd_mono [of 1] by auto
   188 
   189 text {*
   190   \medskip Division by gcd yields rrelatively primes.
   191 *}
   192 
   193 lemma div_gcd_relprime:
   194   assumes nz: "a \<noteq> 0 \<or> b \<noteq> 0"
   195   shows "gcd (a div gcd a b) (b div gcd a b) = 1"
   196 proof -
   197   let ?g = "gcd a b"
   198   let ?a' = "a div ?g"
   199   let ?b' = "b div ?g"
   200   let ?g' = "gcd ?a' ?b'"
   201   have dvdg: "?g dvd a" "?g dvd b" by simp_all
   202   have dvdg': "?g' dvd ?a'" "?g' dvd ?b'" by simp_all
   203   from dvdg dvdg' obtain ka kb ka' kb' where
   204       kab: "a = ?g * ka" "b = ?g * kb" "?a' = ?g' * ka'" "?b' = ?g' * kb'"
   205     unfolding dvd_def by blast
   206   then have "?g * ?a' = (?g * ?g') * ka'" "?g * ?b' = (?g * ?g') * kb'" by simp_all
   207   then have dvdgg':"?g * ?g' dvd a" "?g* ?g' dvd b"
   208     by (auto simp add: dvd_mult_div_cancel [OF dvdg(1)]
   209       dvd_mult_div_cancel [OF dvdg(2)] dvd_def)
   210   have "?g \<noteq> 0" using nz by (simp add: gcd_zero)
   211   then have gp: "?g > 0" by simp
   212   from gcd_greatest [OF dvdgg'] have "?g * ?g' dvd ?g" .
   213   with dvd_mult_cancel1 [OF gp] show "?g' = 1" by simp
   214 qed
   215 
   216 
   217 lemma gcd_unique: "d dvd a\<and>d dvd b \<and> (\<forall>e. e dvd a \<and> e dvd b \<longrightarrow> e dvd d) \<longleftrightarrow> d = gcd a b"
   218 proof(auto)
   219   assume H: "d dvd a" "d dvd b" "\<forall>e. e dvd a \<and> e dvd b \<longrightarrow> e dvd d"
   220   from H(3)[rule_format] gcd_dvd1[of a b] gcd_dvd2[of a b] 
   221   have th: "gcd a b dvd d" by blast
   222   from dvd_anti_sym[OF th gcd_greatest[OF H(1,2)]]  show "d = gcd a b" by blast 
   223 qed
   224 
   225 lemma gcd_eq: assumes H: "\<forall>d. d dvd x \<and> d dvd y \<longleftrightarrow> d dvd u \<and> d dvd v"
   226   shows "gcd x y = gcd u v"
   227 proof-
   228   from H have "\<forall>d. d dvd x \<and> d dvd y \<longleftrightarrow> d dvd gcd u v" by simp
   229   with gcd_unique[of "gcd u v" x y]  show ?thesis by auto
   230 qed
   231 
   232 lemma ind_euclid: 
   233   assumes c: " \<forall>a b. P (a::nat) b \<longleftrightarrow> P b a" and z: "\<forall>a. P a 0" 
   234   and add: "\<forall>a b. P a b \<longrightarrow> P a (a + b)" 
   235   shows "P a b"
   236 proof(induct n\<equiv>"a+b" arbitrary: a b rule: nat_less_induct)
   237   fix n a b
   238   assume H: "\<forall>m < n. \<forall>a b. m = a + b \<longrightarrow> P a b" "n = a + b"
   239   have "a = b \<or> a < b \<or> b < a" by arith
   240   moreover {assume eq: "a= b"
   241     from add[rule_format, OF z[rule_format, of a]] have "P a b" using eq by simp}
   242   moreover
   243   {assume lt: "a < b"
   244     hence "a + b - a < n \<or> a = 0"  using H(2) by arith
   245     moreover
   246     {assume "a =0" with z c have "P a b" by blast }
   247     moreover
   248     {assume ab: "a + b - a < n"
   249       have th0: "a + b - a = a + (b - a)" using lt by arith
   250       from add[rule_format, OF H(1)[rule_format, OF ab th0]]
   251       have "P a b" by (simp add: th0[symmetric])}
   252     ultimately have "P a b" by blast}
   253   moreover
   254   {assume lt: "a > b"
   255     hence "b + a - b < n \<or> b = 0"  using H(2) by arith
   256     moreover
   257     {assume "b =0" with z c have "P a b" by blast }
   258     moreover
   259     {assume ab: "b + a - b < n"
   260       have th0: "b + a - b = b + (a - b)" using lt by arith
   261       from add[rule_format, OF H(1)[rule_format, OF ab th0]]
   262       have "P b a" by (simp add: th0[symmetric])
   263       hence "P a b" using c by blast }
   264     ultimately have "P a b" by blast}
   265 ultimately  show "P a b" by blast
   266 qed
   267 
   268 lemma bezout_lemma: 
   269   assumes ex: "\<exists>(d::nat) x y. d dvd a \<and> d dvd b \<and> (a * x = b * y + d \<or> b * x = a * y + d)"
   270   shows "\<exists>d x y. d dvd a \<and> d dvd a + b \<and> (a * x = (a + b) * y + d \<or> (a + b) * x = a * y + d)"
   271 using ex
   272 apply clarsimp
   273 apply (rule_tac x="d" in exI, simp add: dvd_add)
   274 apply (case_tac "a * x = b * y + d" , simp_all)
   275 apply (rule_tac x="x + y" in exI)
   276 apply (rule_tac x="y" in exI)
   277 apply algebra
   278 apply (rule_tac x="x" in exI)
   279 apply (rule_tac x="x + y" in exI)
   280 apply algebra
   281 done
   282 
   283 lemma bezout_add: "\<exists>(d::nat) x y. d dvd a \<and> d dvd b \<and> (a * x = b * y + d \<or> b * x = a * y + d)"
   284 apply(induct a b rule: ind_euclid)
   285 apply blast
   286 apply clarify
   287 apply (rule_tac x="a" in exI, simp add: dvd_add)
   288 apply clarsimp
   289 apply (rule_tac x="d" in exI)
   290 apply (case_tac "a * x = b * y + d", simp_all add: dvd_add)
   291 apply (rule_tac x="x+y" in exI)
   292 apply (rule_tac x="y" in exI)
   293 apply algebra
   294 apply (rule_tac x="x" in exI)
   295 apply (rule_tac x="x+y" in exI)
   296 apply algebra
   297 done
   298 
   299 lemma bezout: "\<exists>(d::nat) x y. d dvd a \<and> d dvd b \<and> (a * x - b * y = d \<or> b * x - a * y = d)"
   300 using bezout_add[of a b]
   301 apply clarsimp
   302 apply (rule_tac x="d" in exI, simp)
   303 apply (rule_tac x="x" in exI)
   304 apply (rule_tac x="y" in exI)
   305 apply auto
   306 done
   307 
   308 
   309 text {* We can get a stronger version with a nonzeroness assumption. *}
   310 lemma divides_le: "m dvd n ==> m <= n \<or> n = (0::nat)" by (auto simp add: dvd_def)
   311 
   312 lemma bezout_add_strong: assumes nz: "a \<noteq> (0::nat)"
   313   shows "\<exists>d x y. d dvd a \<and> d dvd b \<and> a * x = b * y + d"
   314 proof-
   315   from nz have ap: "a > 0" by simp
   316  from bezout_add[of a b] 
   317  have "(\<exists>d x y. d dvd a \<and> d dvd b \<and> a * x = b * y + d) \<or> (\<exists>d x y. d dvd a \<and> d dvd b \<and> b * x = a * y + d)" by blast
   318  moreover
   319  {fix d x y assume H: "d dvd a" "d dvd b" "a * x = b * y + d"
   320    from H have ?thesis by blast }
   321  moreover
   322  {fix d x y assume H: "d dvd a" "d dvd b" "b * x = a * y + d"
   323    {assume b0: "b = 0" with H  have ?thesis by simp}
   324    moreover 
   325    {assume b: "b \<noteq> 0" hence bp: "b > 0" by simp
   326      from divides_le[OF H(2)] b have "d < b \<or> d = b" using le_less by blast
   327      moreover
   328      {assume db: "d=b"
   329        from prems have ?thesis apply simp
   330 	 apply (rule exI[where x = b], simp)
   331 	 apply (rule exI[where x = b])
   332 	by (rule exI[where x = "a - 1"], simp add: diff_mult_distrib2)}
   333     moreover
   334     {assume db: "d < b" 
   335 	{assume "x=0" hence ?thesis  using prems by simp }
   336 	moreover
   337 	{assume x0: "x \<noteq> 0" hence xp: "x > 0" by simp
   338 	  
   339 	  from db have "d \<le> b - 1" by simp
   340 	  hence "d*b \<le> b*(b - 1)" by simp
   341 	  with xp mult_mono[of "1" "x" "d*b" "b*(b - 1)"]
   342 	  have dble: "d*b \<le> x*b*(b - 1)" using bp by simp
   343 	  from H (3) have "a * ((b - 1) * y) + d * (b - 1 + 1) = d + x*b*(b - 1)" by algebra
   344 	  hence "a * ((b - 1) * y) = d + x*b*(b - 1) - d*b" using bp by simp
   345 	  hence "a * ((b - 1) * y) = d + (x*b*(b - 1) - d*b)" 
   346 	    by (simp only: diff_add_assoc[OF dble, of d, symmetric])
   347 	  hence "a * ((b - 1) * y) = b*(x*(b - 1) - d) + d"
   348 	    by (simp only: diff_mult_distrib2 add_commute mult_ac)
   349 	  hence ?thesis using H(1,2)
   350 	    apply -
   351 	    apply (rule exI[where x=d], simp)
   352 	    apply (rule exI[where x="(b - 1) * y"])
   353 	    by (rule exI[where x="x*(b - 1) - d"], simp)}
   354 	ultimately have ?thesis by blast}
   355     ultimately have ?thesis by blast}
   356   ultimately have ?thesis by blast}
   357  ultimately show ?thesis by blast
   358 qed
   359 
   360 
   361 lemma bezout_gcd: "\<exists>x y. a * x - b * y = gcd a b \<or> b * x - a * y = gcd a b"
   362 proof-
   363   let ?g = "gcd a b"
   364   from bezout[of a b] obtain d x y where d: "d dvd a" "d dvd b" "a * x - b * y = d \<or> b * x - a * y = d" by blast
   365   from d(1,2) have "d dvd ?g" by simp
   366   then obtain k where k: "?g = d*k" unfolding dvd_def by blast
   367   from d(3) have "(a * x - b * y)*k = d*k \<or> (b * x - a * y)*k = d*k" by blast 
   368   hence "a * x * k - b * y*k = d*k \<or> b * x * k - a * y*k = d*k" 
   369     by (algebra add: diff_mult_distrib)
   370   hence "a * (x * k) - b * (y*k) = ?g \<or> b * (x * k) - a * (y*k) = ?g" 
   371     by (simp add: k mult_assoc)
   372   thus ?thesis by blast
   373 qed
   374 
   375 lemma bezout_gcd_strong: assumes a: "a \<noteq> 0" 
   376   shows "\<exists>x y. a * x = b * y + gcd a b"
   377 proof-
   378   let ?g = "gcd a b"
   379   from bezout_add_strong[OF a, of b]
   380   obtain d x y where d: "d dvd a" "d dvd b" "a * x = b * y + d" by blast
   381   from d(1,2) have "d dvd ?g" by simp
   382   then obtain k where k: "?g = d*k" unfolding dvd_def by blast
   383   from d(3) have "a * x * k = (b * y + d) *k " by algebra
   384   hence "a * (x * k) = b * (y*k) + ?g" by (algebra add: k)
   385   thus ?thesis by blast
   386 qed
   387 
   388 lemma gcd_mult_distrib: "gcd(a * c) (b * c) = c * gcd a b"
   389 by(simp add: gcd_mult_distrib2 mult_commute)
   390 
   391 lemma gcd_bezout: "(\<exists>x y. a * x - b * y = d \<or> b * x - a * y = d) \<longleftrightarrow> gcd a b dvd d"
   392   (is "?lhs \<longleftrightarrow> ?rhs")
   393 proof-
   394   let ?g = "gcd a b"
   395   {assume H: ?rhs then obtain k where k: "d = ?g*k" unfolding dvd_def by blast
   396     from bezout_gcd[of a b] obtain x y where xy: "a * x - b * y = ?g \<or> b * x - a * y = ?g"
   397       by blast
   398     hence "(a * x - b * y)*k = ?g*k \<or> (b * x - a * y)*k = ?g*k" by auto
   399     hence "a * x*k - b * y*k = ?g*k \<or> b * x * k - a * y*k = ?g*k" 
   400       by (simp only: diff_mult_distrib)
   401     hence "a * (x*k) - b * (y*k) = d \<or> b * (x * k) - a * (y*k) = d"
   402       by (simp add: k[symmetric] mult_assoc)
   403     hence ?lhs by blast}
   404   moreover
   405   {fix x y assume H: "a * x - b * y = d \<or> b * x - a * y = d"
   406     have dv: "?g dvd a*x" "?g dvd b * y" "?g dvd b*x" "?g dvd a * y"
   407       using dvd_mult2[OF gcd_dvd1[of a b]] dvd_mult2[OF gcd_dvd2[of a b]] by simp_all
   408     from dvd_diff[OF dv(1,2)] dvd_diff[OF dv(3,4)] H
   409     have ?rhs by auto}
   410   ultimately show ?thesis by blast
   411 qed
   412 
   413 lemma gcd_bezout_sum: assumes H:"a * x + b * y = d" shows "gcd a b dvd d"
   414 proof-
   415   let ?g = "gcd a b"
   416     have dv: "?g dvd a*x" "?g dvd b * y" 
   417       using dvd_mult2[OF gcd_dvd1[of a b]] dvd_mult2[OF gcd_dvd2[of a b]] by simp_all
   418     from dvd_add[OF dv] H
   419     show ?thesis by auto
   420 qed
   421 
   422 lemma gcd_mult': "gcd b (a * b) = b"
   423 by (simp add: gcd_mult mult_commute[of a b]) 
   424 
   425 lemma gcd_add: "gcd(a + b) b = gcd a b" 
   426   "gcd(b + a) b = gcd a b" "gcd a (a + b) = gcd a b" "gcd a (b + a) = gcd a b"
   427 apply (simp_all add: gcd_add1)
   428 by (simp add: gcd_commute gcd_add1)
   429 
   430 lemma gcd_sub: "b <= a ==> gcd(a - b) b = gcd a b" "a <= b ==> gcd a (b - a) = gcd a b"
   431 proof-
   432   {fix a b assume H: "b \<le> (a::nat)"
   433     hence th: "a - b + b = a" by arith
   434     from gcd_add(1)[of "a - b" b] th  have "gcd(a - b) b = gcd a b" by simp}
   435   note th = this
   436 {
   437   assume ab: "b \<le> a"
   438   from th[OF ab] show "gcd (a - b)  b = gcd a b" by blast
   439 next
   440   assume ab: "a \<le> b"
   441   from th[OF ab] show "gcd a (b - a) = gcd a b" 
   442     by (simp add: gcd_commute)}
   443 qed
   444 
   445 
   446 subsection {* LCM defined by GCD *}
   447 
   448 
   449 definition
   450   lcm :: "nat \<Rightarrow> nat \<Rightarrow> nat"
   451 where
   452   lcm_def: "lcm m n = m * n div gcd m n"
   453 
   454 lemma prod_gcd_lcm:
   455   "m * n = gcd m n * lcm m n"
   456   unfolding lcm_def by (simp add: dvd_mult_div_cancel [OF gcd_dvd_prod])
   457 
   458 lemma lcm_0 [simp]: "lcm m 0 = 0"
   459   unfolding lcm_def by simp
   460 
   461 lemma lcm_1 [simp]: "lcm m 1 = m"
   462   unfolding lcm_def by simp
   463 
   464 lemma lcm_0_left [simp]: "lcm 0 n = 0"
   465   unfolding lcm_def by simp
   466 
   467 lemma lcm_1_left [simp]: "lcm 1 m = m"
   468   unfolding lcm_def by simp
   469 
   470 lemma dvd_pos:
   471   fixes n m :: nat
   472   assumes "n > 0" and "m dvd n"
   473   shows "m > 0"
   474 using assms by (cases m) auto
   475 
   476 lemma lcm_least:
   477   assumes "m dvd k" and "n dvd k"
   478   shows "lcm m n dvd k"
   479 proof (cases k)
   480   case 0 then show ?thesis by auto
   481 next
   482   case (Suc _) then have pos_k: "k > 0" by auto
   483   from assms dvd_pos [OF this] have pos_mn: "m > 0" "n > 0" by auto
   484   with gcd_zero [of m n] have pos_gcd: "gcd m n > 0" by simp
   485   from assms obtain p where k_m: "k = m * p" using dvd_def by blast
   486   from assms obtain q where k_n: "k = n * q" using dvd_def by blast
   487   from pos_k k_m have pos_p: "p > 0" by auto
   488   from pos_k k_n have pos_q: "q > 0" by auto
   489   have "k * k * gcd q p = k * gcd (k * q) (k * p)"
   490     by (simp add: mult_ac gcd_mult_distrib2)
   491   also have "\<dots> = k * gcd (m * p * q) (n * q * p)"
   492     by (simp add: k_m [symmetric] k_n [symmetric])
   493   also have "\<dots> = k * p * q * gcd m n"
   494     by (simp add: mult_ac gcd_mult_distrib2)
   495   finally have "(m * p) * (n * q) * gcd q p = k * p * q * gcd m n"
   496     by (simp only: k_m [symmetric] k_n [symmetric])
   497   then have "p * q * m * n * gcd q p = p * q * k * gcd m n"
   498     by (simp add: mult_ac)
   499   with pos_p pos_q have "m * n * gcd q p = k * gcd m n"
   500     by simp
   501   with prod_gcd_lcm [of m n]
   502   have "lcm m n * gcd q p * gcd m n = k * gcd m n"
   503     by (simp add: mult_ac)
   504   with pos_gcd have "lcm m n * gcd q p = k" by simp
   505   then show ?thesis using dvd_def by auto
   506 qed
   507 
   508 lemma lcm_dvd1 [iff]:
   509   "m dvd lcm m n"
   510 proof (cases m)
   511   case 0 then show ?thesis by simp
   512 next
   513   case (Suc _)
   514   then have mpos: "m > 0" by simp
   515   show ?thesis
   516   proof (cases n)
   517     case 0 then show ?thesis by simp
   518   next
   519     case (Suc _)
   520     then have npos: "n > 0" by simp
   521     have "gcd m n dvd n" by simp
   522     then obtain k where "n = gcd m n * k" using dvd_def by auto
   523     then have "m * n div gcd m n = m * (gcd m n * k) div gcd m n" by (simp add: mult_ac)
   524     also have "\<dots> = m * k" using mpos npos gcd_zero by simp
   525     finally show ?thesis by (simp add: lcm_def)
   526   qed
   527 qed
   528 
   529 lemma lcm_dvd2 [iff]: 
   530   "n dvd lcm m n"
   531 proof (cases n)
   532   case 0 then show ?thesis by simp
   533 next
   534   case (Suc _)
   535   then have npos: "n > 0" by simp
   536   show ?thesis
   537   proof (cases m)
   538     case 0 then show ?thesis by simp
   539   next
   540     case (Suc _)
   541     then have mpos: "m > 0" by simp
   542     have "gcd m n dvd m" by simp
   543     then obtain k where "m = gcd m n * k" using dvd_def by auto
   544     then have "m * n div gcd m n = (gcd m n * k) * n div gcd m n" by (simp add: mult_ac)
   545     also have "\<dots> = n * k" using mpos npos gcd_zero by simp
   546     finally show ?thesis by (simp add: lcm_def)
   547   qed
   548 qed
   549 
   550 lemma gcd_add1_eq: "gcd (m + k) k = gcd (m + k) m"
   551   by (simp add: gcd_commute)
   552 
   553 lemma gcd_diff2: "m \<le> n ==> gcd n (n - m) = gcd n m"
   554   apply (subgoal_tac "n = m + (n - m)")
   555   apply (erule ssubst, rule gcd_add1_eq, simp)  
   556   done
   557 
   558 
   559 subsection {* GCD and LCM on integers *}
   560 
   561 definition
   562   zgcd :: "int \<Rightarrow> int \<Rightarrow> int" where
   563   "zgcd i j = int (gcd (nat (abs i)) (nat (abs j)))"
   564 
   565 lemma zgcd_zdvd1 [iff,simp, algebra]: "zgcd i j dvd i"
   566   by (simp add: zgcd_def int_dvd_iff)
   567 
   568 lemma zgcd_zdvd2 [iff,simp, algebra]: "zgcd i j dvd j"
   569   by (simp add: zgcd_def int_dvd_iff)
   570 
   571 lemma zgcd_pos: "zgcd i j \<ge> 0"
   572   by (simp add: zgcd_def)
   573 
   574 lemma zgcd0 [simp,algebra]: "(zgcd i j = 0) = (i = 0 \<and> j = 0)"
   575   by (simp add: zgcd_def gcd_zero) arith
   576 
   577 lemma zgcd_commute: "zgcd i j = zgcd j i"
   578   unfolding zgcd_def by (simp add: gcd_commute)
   579 
   580 lemma zgcd_zminus [simp, algebra]: "zgcd (- i) j = zgcd i j"
   581   unfolding zgcd_def by simp
   582 
   583 lemma zgcd_zminus2 [simp, algebra]: "zgcd i (- j) = zgcd i j"
   584   unfolding zgcd_def by simp
   585 
   586   (* should be solved by algebra*)
   587 lemma zrelprime_dvd_mult: "zgcd i j = 1 \<Longrightarrow> i dvd k * j \<Longrightarrow> i dvd k"
   588   unfolding zgcd_def
   589 proof -
   590   assume "int (gcd (nat \<bar>i\<bar>) (nat \<bar>j\<bar>)) = 1" "i dvd k * j"
   591   then have g: "gcd (nat \<bar>i\<bar>) (nat \<bar>j\<bar>) = 1" by simp
   592   from `i dvd k * j` obtain h where h: "k*j = i*h" unfolding dvd_def by blast
   593   have th: "nat \<bar>i\<bar> dvd nat \<bar>k\<bar> * nat \<bar>j\<bar>"
   594     unfolding dvd_def
   595     by (rule_tac x= "nat \<bar>h\<bar>" in exI, simp add: h nat_abs_mult_distrib [symmetric])
   596   from relprime_dvd_mult [OF g th] obtain h' where h': "nat \<bar>k\<bar> = nat \<bar>i\<bar> * h'"
   597     unfolding dvd_def by blast
   598   from h' have "int (nat \<bar>k\<bar>) = int (nat \<bar>i\<bar> * h')" by simp
   599   then have "\<bar>k\<bar> = \<bar>i\<bar> * int h'" by (simp add: int_mult)
   600   then show ?thesis
   601     apply (subst zdvd_abs1 [symmetric])
   602     apply (subst zdvd_abs2 [symmetric])
   603     apply (unfold dvd_def)
   604     apply (rule_tac x = "int h'" in exI, simp)
   605     done
   606 qed
   607 
   608 lemma int_nat_abs: "int (nat (abs x)) = abs x" by arith
   609 
   610 lemma zgcd_greatest:
   611   assumes "k dvd m" and "k dvd n"
   612   shows "k dvd zgcd m n"
   613 proof -
   614   let ?k' = "nat \<bar>k\<bar>"
   615   let ?m' = "nat \<bar>m\<bar>"
   616   let ?n' = "nat \<bar>n\<bar>"
   617   from `k dvd m` and `k dvd n` have dvd': "?k' dvd ?m'" "?k' dvd ?n'"
   618     unfolding zdvd_int by (simp_all only: int_nat_abs zdvd_abs1 zdvd_abs2)
   619   from gcd_greatest [OF dvd'] have "int (nat \<bar>k\<bar>) dvd zgcd m n"
   620     unfolding zgcd_def by (simp only: zdvd_int)
   621   then have "\<bar>k\<bar> dvd zgcd m n" by (simp only: int_nat_abs)
   622   then show "k dvd zgcd m n" by (simp add: zdvd_abs1)
   623 qed
   624 
   625 lemma div_zgcd_relprime:
   626   assumes nz: "a \<noteq> 0 \<or> b \<noteq> 0"
   627   shows "zgcd (a div (zgcd a b)) (b div (zgcd a b)) = 1"
   628 proof -
   629   from nz have nz': "nat \<bar>a\<bar> \<noteq> 0 \<or> nat \<bar>b\<bar> \<noteq> 0" by arith 
   630   let ?g = "zgcd a b"
   631   let ?a' = "a div ?g"
   632   let ?b' = "b div ?g"
   633   let ?g' = "zgcd ?a' ?b'"
   634   have dvdg: "?g dvd a" "?g dvd b" by (simp_all add: zgcd_zdvd1 zgcd_zdvd2)
   635   have dvdg': "?g' dvd ?a'" "?g' dvd ?b'" by (simp_all add: zgcd_zdvd1 zgcd_zdvd2)
   636   from dvdg dvdg' obtain ka kb ka' kb' where
   637    kab: "a = ?g*ka" "b = ?g*kb" "?a' = ?g'*ka'" "?b' = ?g' * kb'"
   638     unfolding dvd_def by blast
   639   then have "?g* ?a' = (?g * ?g') * ka'" "?g* ?b' = (?g * ?g') * kb'" by simp_all
   640   then have dvdgg':"?g * ?g' dvd a" "?g* ?g' dvd b"
   641     by (auto simp add: zdvd_mult_div_cancel [OF dvdg(1)]
   642       zdvd_mult_div_cancel [OF dvdg(2)] dvd_def)
   643   have "?g \<noteq> 0" using nz by simp
   644   then have gp: "?g \<noteq> 0" using zgcd_pos[where i="a" and j="b"] by arith
   645   from zgcd_greatest [OF dvdgg'] have "?g * ?g' dvd ?g" .
   646   with zdvd_mult_cancel1 [OF gp] have "\<bar>?g'\<bar> = 1" by simp
   647   with zgcd_pos show "?g' = 1" by simp
   648 qed
   649 
   650 lemma zgcd_0 [simp, algebra]: "zgcd m 0 = abs m"
   651   by (simp add: zgcd_def abs_if)
   652 
   653 lemma zgcd_0_left [simp, algebra]: "zgcd 0 m = abs m"
   654   by (simp add: zgcd_def abs_if)
   655 
   656 lemma zgcd_non_0: "0 < n ==> zgcd m n = zgcd n (m mod n)"
   657   apply (frule_tac b = n and a = m in pos_mod_sign)
   658   apply (simp del: pos_mod_sign add: zgcd_def abs_if nat_mod_distrib)
   659   apply (auto simp add: gcd_non_0 nat_mod_distrib [symmetric] zmod_zminus1_eq_if)
   660   apply (frule_tac a = m in pos_mod_bound)
   661   apply (simp del: pos_mod_bound add: nat_diff_distrib gcd_diff2 nat_le_eq_zle)
   662   done
   663 
   664 lemma zgcd_eq: "zgcd m n = zgcd n (m mod n)"
   665   apply (case_tac "n = 0", simp add: DIVISION_BY_ZERO)
   666   apply (auto simp add: linorder_neq_iff zgcd_non_0)
   667   apply (cut_tac m = "-m" and n = "-n" in zgcd_non_0, auto)
   668   done
   669 
   670 lemma zgcd_1 [simp, algebra]: "zgcd m 1 = 1"
   671   by (simp add: zgcd_def abs_if)
   672 
   673 lemma zgcd_0_1_iff [simp, algebra]: "zgcd 0 m = 1 \<longleftrightarrow> \<bar>m\<bar> = 1"
   674   by (simp add: zgcd_def abs_if)
   675 
   676 lemma zgcd_greatest_iff[algebra]: "k dvd zgcd m n = (k dvd m \<and> k dvd n)"
   677   by (simp add: zgcd_def abs_if int_dvd_iff dvd_int_iff nat_dvd_iff)
   678 
   679 lemma zgcd_1_left [simp, algebra]: "zgcd 1 m = 1"
   680   by (simp add: zgcd_def gcd_1_left)
   681 
   682 lemma zgcd_assoc: "zgcd (zgcd k m) n = zgcd k (zgcd m n)"
   683   by (simp add: zgcd_def gcd_assoc)
   684 
   685 lemma zgcd_left_commute: "zgcd k (zgcd m n) = zgcd m (zgcd k n)"
   686   apply (rule zgcd_commute [THEN trans])
   687   apply (rule zgcd_assoc [THEN trans])
   688   apply (rule zgcd_commute [THEN arg_cong])
   689   done
   690 
   691 lemmas zgcd_ac = zgcd_assoc zgcd_commute zgcd_left_commute
   692   -- {* addition is an AC-operator *}
   693 
   694 lemma zgcd_zmult_distrib2: "0 \<le> k ==> k * zgcd m n = zgcd (k * m) (k * n)"
   695   by (simp del: minus_mult_right [symmetric]
   696       add: minus_mult_right nat_mult_distrib zgcd_def abs_if
   697           mult_less_0_iff gcd_mult_distrib2 [symmetric] zmult_int [symmetric])
   698 
   699 lemma zgcd_zmult_distrib2_abs: "zgcd (k * m) (k * n) = abs k * zgcd m n"
   700   by (simp add: abs_if zgcd_zmult_distrib2)
   701 
   702 lemma zgcd_self [simp]: "0 \<le> m ==> zgcd m m = m"
   703   by (cut_tac k = m and m = 1 and n = 1 in zgcd_zmult_distrib2, simp_all)
   704 
   705 lemma zgcd_zmult_eq_self [simp]: "0 \<le> k ==> zgcd k (k * n) = k"
   706   by (cut_tac k = k and m = 1 and n = n in zgcd_zmult_distrib2, simp_all)
   707 
   708 lemma zgcd_zmult_eq_self2 [simp]: "0 \<le> k ==> zgcd (k * n) k = k"
   709   by (cut_tac k = k and m = n and n = 1 in zgcd_zmult_distrib2, simp_all)
   710 
   711 
   712 definition "zlcm i j = int (lcm(nat(abs i)) (nat(abs j)))"
   713 
   714 lemma dvd_zlcm_self1[simp, algebra]: "i dvd zlcm i j"
   715 by(simp add:zlcm_def dvd_int_iff)
   716 
   717 lemma dvd_zlcm_self2[simp, algebra]: "j dvd zlcm i j"
   718 by(simp add:zlcm_def dvd_int_iff)
   719 
   720 
   721 lemma dvd_imp_dvd_zlcm1:
   722   assumes "k dvd i" shows "k dvd (zlcm i j)"
   723 proof -
   724   have "nat(abs k) dvd nat(abs i)" using `k dvd i`
   725     by(simp add:int_dvd_iff[symmetric] dvd_int_iff[symmetric] zdvd_abs1)
   726   thus ?thesis by(simp add:zlcm_def dvd_int_iff)(blast intro: dvd_trans)
   727 qed
   728 
   729 lemma dvd_imp_dvd_zlcm2:
   730   assumes "k dvd j" shows "k dvd (zlcm i j)"
   731 proof -
   732   have "nat(abs k) dvd nat(abs j)" using `k dvd j`
   733     by(simp add:int_dvd_iff[symmetric] dvd_int_iff[symmetric] zdvd_abs1)
   734   thus ?thesis by(simp add:zlcm_def dvd_int_iff)(blast intro: dvd_trans)
   735 qed
   736 
   737 
   738 lemma zdvd_self_abs1: "(d::int) dvd (abs d)"
   739 by (case_tac "d <0", simp_all)
   740 
   741 lemma zdvd_self_abs2: "(abs (d::int)) dvd d"
   742 by (case_tac "d<0", simp_all)
   743 
   744 (* lcm a b is positive for positive a and b *)
   745 
   746 lemma lcm_pos: 
   747   assumes mpos: "m > 0"
   748   and npos: "n>0"
   749   shows "lcm m n > 0"
   750 proof(rule ccontr, simp add: lcm_def gcd_zero)
   751 assume h:"m*n div gcd m n = 0"
   752 from mpos npos have "gcd m n \<noteq> 0" using gcd_zero by simp
   753 hence gcdp: "gcd m n > 0" by simp
   754 with h
   755 have "m*n < gcd m n"
   756   by (cases "m * n < gcd m n") (auto simp add: div_if[OF gcdp, where m="m*n"])
   757 moreover 
   758 have "gcd m n dvd m" by simp
   759  with mpos dvd_imp_le have t1:"gcd m n \<le> m" by simp
   760  with npos have t1:"gcd m n *n \<le> m*n" by simp
   761  have "gcd m n \<le> gcd m n*n" using npos by simp
   762  with t1 have "gcd m n \<le> m*n" by arith
   763 ultimately show "False" by simp
   764 qed
   765 
   766 lemma zlcm_pos: 
   767   assumes anz: "a \<noteq> 0"
   768   and bnz: "b \<noteq> 0" 
   769   shows "0 < zlcm a b"
   770 proof-
   771   let ?na = "nat (abs a)"
   772   let ?nb = "nat (abs b)"
   773   have nap: "?na >0" using anz by simp
   774   have nbp: "?nb >0" using bnz by simp
   775   have "0 < lcm ?na ?nb" by (rule lcm_pos[OF nap nbp])
   776   thus ?thesis by (simp add: zlcm_def)
   777 qed
   778 
   779 lemma zgcd_code [code]:
   780   "zgcd k l = \<bar>if l = 0 then k else zgcd l (\<bar>k\<bar> mod \<bar>l\<bar>)\<bar>"
   781   by (simp add: zgcd_def gcd.simps [of "nat \<bar>k\<bar>"] nat_mod_distrib)
   782 
   783 end