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