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