src/HOL/Old_Number_Theory/Legacy_GCD.thy
author wenzelm
Sun Nov 02 18:21:45 2014 +0100 (2014-11-02)
changeset 58889 5b7a9633cfa8
parent 58834 773b378d9313
child 61382 efac889fccbc
permissions -rw-r--r--
modernized header uniformly as section;
     1 (*  Title:      HOL/Old_Number_Theory/Legacy_GCD.thy
     2     Author:     Christophe Tabacznyj and Lawrence C Paulson
     3     Copyright   1996  University of Cambridge
     4 *)
     5 
     6 section {* The Greatest Common Divisor *}
     7 
     8 theory Legacy_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   "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_antisym)
    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 gcd :: "nat => nat => nat"
    42   where "gcd m n = (if n = 0 then m else gcd n (m mod n))"
    43 
    44 lemma gcd_induct [case_names "0" rec]:
    45   fixes m n :: nat
    46   assumes "\<And>m. P m 0"
    47     and "\<And>m n. 0 < n \<Longrightarrow> P n (m mod n) \<Longrightarrow> P m n"
    48   shows "P m n"
    49 proof (induct m n rule: gcd.induct)
    50   case (1 m n)
    51   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 \<open>page 27\<close> davenport92} *}
   134   apply (induct m n rule: gcd_induct)
   135    apply simp
   136   apply (case_tac "k = 0")
   137    apply (simp_all add: gcd_non_0)
   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_antisym)
   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   from this(3-4) [symmetric] have "?g * ?a' = (?g * ?g') * ka'" "?g * ?b' = (?g * ?g') * kb'"
   211     by (simp_all only: ac_simps mult.left_commute [of _ "gcd a b"])
   212   then have dvdgg':"?g * ?g' dvd a" "?g* ?g' dvd b"
   213     by (auto simp add: dvd_mult_div_cancel [OF dvdg(1)]
   214       dvd_mult_div_cancel [OF dvdg(2)] dvd_def)
   215   have "?g \<noteq> 0" using nz by (simp add: gcd_zero)
   216   then have gp: "?g > 0" by simp
   217   from gcd_greatest [OF dvdgg'] have "?g * ?g' dvd ?g" .
   218   with dvd_mult_cancel1 [OF gp] show "?g' = 1" by simp
   219 qed
   220 
   221 
   222 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"
   223 proof(auto)
   224   assume H: "d dvd a" "d dvd b" "\<forall>e. e dvd a \<and> e dvd b \<longrightarrow> e dvd d"
   225   from H(3)[rule_format] gcd_dvd1[of a b] gcd_dvd2[of a b] 
   226   have th: "gcd a b dvd d" by blast
   227   from dvd_antisym[OF th gcd_greatest[OF H(1,2)]]  show "d = gcd a b" by blast 
   228 qed
   229 
   230 lemma gcd_eq: assumes H: "\<forall>d. d dvd x \<and> d dvd y \<longleftrightarrow> d dvd u \<and> d dvd v"
   231   shows "gcd x y = gcd u v"
   232 proof-
   233   from H have "\<forall>d. d dvd x \<and> d dvd y \<longleftrightarrow> d dvd gcd u v" by simp
   234   with gcd_unique[of "gcd u v" x y]  show ?thesis by auto
   235 qed
   236 
   237 lemma ind_euclid:
   238   assumes c: " \<forall>a b. P (a::nat) b \<longleftrightarrow> P b a" and z: "\<forall>a. P a 0"
   239   and add: "\<forall>a b. P a b \<longrightarrow> P a (a + b)"
   240   shows "P a b"
   241 proof(induct "a + b" arbitrary: a b rule: less_induct)
   242   case less
   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
   246     by simp}
   247   moreover
   248   {assume lt: "a < b"
   249     hence "a + b - a < a + b \<or> a = 0" by arith
   250     moreover
   251     {assume "a =0" with z c have "P a b" by blast }
   252     moreover
   253     {assume "a + b - a < a + b"
   254       also have th0: "a + b - a = a + (b - a)" using lt by arith
   255       finally have "a + (b - a) < a + b" .
   256       then have "P a (a + (b - a))" by (rule add[rule_format, OF less])
   257       then have "P a b" by (simp add: th0[symmetric])}
   258     ultimately have "P a b" by blast}
   259   moreover
   260   {assume lt: "a > b"
   261     hence "b + a - b < a + b \<or> b = 0" by arith
   262     moreover
   263     {assume "b =0" with z c have "P a b" by blast }
   264     moreover
   265     {assume "b + a - b < a + b"
   266       also have th0: "b + a - b = b + (a - b)" using lt by arith
   267       finally have "b + (a - b) < a + b" .
   268       then have "P b (b + (a - b))" by (rule add[rule_format, OF less])
   269       then have "P b a" by (simp add: th0[symmetric])
   270       hence "P a b" using c by blast }
   271     ultimately have "P a b" by blast}
   272 ultimately  show "P a b" by blast
   273 qed
   274 
   275 lemma bezout_lemma: 
   276   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)"
   277   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)"
   278 using ex
   279 apply clarsimp
   280 apply (rule_tac x="d" in exI, simp)
   281 apply (case_tac "a * x = b * y + d" , simp_all)
   282 apply (rule_tac x="x + y" in exI)
   283 apply (rule_tac x="y" in exI)
   284 apply algebra
   285 apply (rule_tac x="x" in exI)
   286 apply (rule_tac x="x + y" in exI)
   287 apply algebra
   288 done
   289 
   290 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)"
   291 apply(induct a b rule: ind_euclid)
   292 apply blast
   293 apply clarify
   294 apply (rule_tac x="a" in exI, simp)
   295 apply clarsimp
   296 apply (rule_tac x="d" in exI)
   297 apply (case_tac "a * x = b * y + d", simp_all)
   298 apply (rule_tac x="x+y" in exI)
   299 apply (rule_tac x="y" in exI)
   300 apply algebra
   301 apply (rule_tac x="x" in exI)
   302 apply (rule_tac x="x+y" in exI)
   303 apply algebra
   304 done
   305 
   306 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)"
   307 using bezout_add[of a b]
   308 apply clarsimp
   309 apply (rule_tac x="d" in exI, simp)
   310 apply (rule_tac x="x" in exI)
   311 apply (rule_tac x="y" in exI)
   312 apply auto
   313 done
   314 
   315 
   316 text {* We can get a stronger version with a nonzeroness assumption. *}
   317 lemma divides_le: "m dvd n ==> m <= n \<or> n = (0::nat)" by (auto simp add: dvd_def)
   318 
   319 lemma bezout_add_strong: assumes nz: "a \<noteq> (0::nat)"
   320   shows "\<exists>d x y. d dvd a \<and> d dvd b \<and> a * x = b * y + d"
   321 proof-
   322   from nz have ap: "a > 0" by simp
   323  from bezout_add[of a b] 
   324  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
   325  moreover
   326  {fix d x y assume H: "d dvd a" "d dvd b" "a * x = b * y + d"
   327    from H have ?thesis by blast }
   328  moreover
   329  {fix d x y assume H: "d dvd a" "d dvd b" "b * x = a * y + d"
   330    {assume b0: "b = 0" with H  have ?thesis by simp}
   331    moreover 
   332    {assume b: "b \<noteq> 0" hence bp: "b > 0" by simp
   333      from divides_le[OF H(2)] b have "d < b \<or> d = b" using le_less by blast
   334      moreover
   335      {assume db: "d=b"
   336        from nz H db have ?thesis apply simp
   337          apply (rule exI[where x = b], simp)
   338          apply (rule exI[where x = b])
   339         by (rule exI[where x = "a - 1"], simp add: diff_mult_distrib2)}
   340     moreover
   341     {assume db: "d < b" 
   342         {assume "x=0" hence ?thesis using nz H by simp }
   343         moreover
   344         {assume x0: "x \<noteq> 0" hence xp: "x > 0" by simp
   345           
   346           from db have "d \<le> b - 1" by simp
   347           hence "d*b \<le> b*(b - 1)" by simp
   348           with xp mult_mono[of "1" "x" "d*b" "b*(b - 1)"]
   349           have dble: "d*b \<le> x*b*(b - 1)" using bp by simp
   350           from H (3) have "a * ((b - 1) * y) + d * (b - 1 + 1) = d + x*b*(b - 1)" by algebra
   351           hence "a * ((b - 1) * y) = d + x*b*(b - 1) - d*b" using bp by simp
   352           hence "a * ((b - 1) * y) = d + (x*b*(b - 1) - d*b)" 
   353             by (simp only: diff_add_assoc[OF dble, of d, symmetric])
   354           hence "a * ((b - 1) * y) = b*(x*(b - 1) - d) + d"
   355             by (simp only: diff_mult_distrib2 ac_simps)
   356           hence ?thesis using H(1,2)
   357             apply -
   358             apply (rule exI[where x=d], simp)
   359             apply (rule exI[where x="(b - 1) * y"])
   360             by (rule exI[where x="x*(b - 1) - d"], simp)}
   361         ultimately have ?thesis by blast}
   362     ultimately have ?thesis by blast}
   363   ultimately have ?thesis by blast}
   364  ultimately show ?thesis by blast
   365 qed
   366 
   367 
   368 lemma bezout_gcd: "\<exists>x y. a * x - b * y = gcd a b \<or> b * x - a * y = gcd a b"
   369 proof-
   370   let ?g = "gcd a b"
   371   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
   372   from d(1,2) have "d dvd ?g" by simp
   373   then obtain k where k: "?g = d*k" unfolding dvd_def by blast
   374   from d(3) have "(a * x - b * y)*k = d*k \<or> (b * x - a * y)*k = d*k" by blast 
   375   hence "a * x * k - b * y*k = d*k \<or> b * x * k - a * y*k = d*k" 
   376     by (algebra add: diff_mult_distrib)
   377   hence "a * (x * k) - b * (y*k) = ?g \<or> b * (x * k) - a * (y*k) = ?g" 
   378     by (simp add: k mult.assoc)
   379   thus ?thesis by blast
   380 qed
   381 
   382 lemma bezout_gcd_strong: assumes a: "a \<noteq> 0" 
   383   shows "\<exists>x y. a * x = b * y + gcd a b"
   384 proof-
   385   let ?g = "gcd a b"
   386   from bezout_add_strong[OF a, of b]
   387   obtain d x y where d: "d dvd a" "d dvd b" "a * x = b * y + d" by blast
   388   from d(1,2) have "d dvd ?g" by simp
   389   then obtain k where k: "?g = d*k" unfolding dvd_def by blast
   390   from d(3) have "a * x * k = (b * y + d) *k " by algebra
   391   hence "a * (x * k) = b * (y*k) + ?g" by (algebra add: k)
   392   thus ?thesis by blast
   393 qed
   394 
   395 lemma gcd_mult_distrib: "gcd(a * c) (b * c) = c * gcd a b"
   396 by(simp add: gcd_mult_distrib2 mult.commute)
   397 
   398 lemma gcd_bezout: "(\<exists>x y. a * x - b * y = d \<or> b * x - a * y = d) \<longleftrightarrow> gcd a b dvd d"
   399   (is "?lhs \<longleftrightarrow> ?rhs")
   400 proof-
   401   let ?g = "gcd a b"
   402   {assume H: ?rhs then obtain k where k: "d = ?g*k" unfolding dvd_def by blast
   403     from bezout_gcd[of a b] obtain x y where xy: "a * x - b * y = ?g \<or> b * x - a * y = ?g"
   404       by blast
   405     hence "(a * x - b * y)*k = ?g*k \<or> (b * x - a * y)*k = ?g*k" by auto
   406     hence "a * x*k - b * y*k = ?g*k \<or> b * x * k - a * y*k = ?g*k" 
   407       by (simp only: diff_mult_distrib)
   408     hence "a * (x*k) - b * (y*k) = d \<or> b * (x * k) - a * (y*k) = d"
   409       by (simp add: k[symmetric] mult.assoc)
   410     hence ?lhs by blast}
   411   moreover
   412   {fix x y assume H: "a * x - b * y = d \<or> b * x - a * y = d"
   413     have dv: "?g dvd a*x" "?g dvd b * y" "?g dvd b*x" "?g dvd a * y"
   414       using dvd_mult2[OF gcd_dvd1[of a b]] dvd_mult2[OF gcd_dvd2[of a b]] by simp_all
   415     from dvd_diff_nat[OF dv(1,2)] dvd_diff_nat[OF dv(3,4)] H
   416     have ?rhs by auto}
   417   ultimately show ?thesis by blast
   418 qed
   419 
   420 lemma gcd_bezout_sum: assumes H:"a * x + b * y = d" shows "gcd a b dvd d"
   421 proof-
   422   let ?g = "gcd a b"
   423     have dv: "?g dvd a*x" "?g dvd b * y" 
   424       using dvd_mult2[OF gcd_dvd1[of a b]] dvd_mult2[OF gcd_dvd2[of a b]] by simp_all
   425     from dvd_add[OF dv] H
   426     show ?thesis by auto
   427 qed
   428 
   429 lemma gcd_mult': "gcd b (a * b) = b"
   430 by (simp add: mult.commute[of a b]) 
   431 
   432 lemma gcd_add: "gcd(a + b) b = gcd a b" 
   433   "gcd(b + a) b = gcd a b" "gcd a (a + b) = gcd a b" "gcd a (b + a) = gcd a b"
   434 by (simp_all add: gcd_commute)
   435 
   436 lemma gcd_sub: "b <= a ==> gcd(a - b) b = gcd a b" "a <= b ==> gcd a (b - a) = gcd a b"
   437 proof-
   438   {fix a b assume H: "b \<le> (a::nat)"
   439     hence th: "a - b + b = a" by arith
   440     from gcd_add(1)[of "a - b" b] th  have "gcd(a - b) b = gcd a b" by simp}
   441   note th = this
   442 {
   443   assume ab: "b \<le> a"
   444   from th[OF ab] show "gcd (a - b)  b = gcd a b" by blast
   445 next
   446   assume ab: "a \<le> b"
   447   from th[OF ab] show "gcd a (b - a) = gcd a b" 
   448     by (simp add: gcd_commute)}
   449 qed
   450 
   451 
   452 subsection {* LCM defined by GCD *}
   453 
   454 
   455 definition
   456   lcm :: "nat \<Rightarrow> nat \<Rightarrow> nat"
   457 where
   458   lcm_def: "lcm m n = m * n div gcd m n"
   459 
   460 lemma prod_gcd_lcm:
   461   "m * n = gcd m n * lcm m n"
   462   unfolding lcm_def by (simp add: dvd_mult_div_cancel [OF gcd_dvd_prod])
   463 
   464 lemma lcm_0 [simp]: "lcm m 0 = 0"
   465   unfolding lcm_def by simp
   466 
   467 lemma lcm_1 [simp]: "lcm m 1 = m"
   468   unfolding lcm_def by simp
   469 
   470 lemma lcm_0_left [simp]: "lcm 0 n = 0"
   471   unfolding lcm_def by simp
   472 
   473 lemma lcm_1_left [simp]: "lcm 1 m = m"
   474   unfolding lcm_def by simp
   475 
   476 lemma dvd_pos:
   477   fixes n m :: nat
   478   assumes "n > 0" and "m dvd n"
   479   shows "m > 0"
   480 using assms by (cases m) auto
   481 
   482 lemma lcm_least:
   483   assumes "m dvd k" and "n dvd k"
   484   shows "lcm m n dvd k"
   485 proof (cases k)
   486   case 0 then show ?thesis by auto
   487 next
   488   case (Suc _) then have pos_k: "k > 0" by auto
   489   from assms dvd_pos [OF this] have pos_mn: "m > 0" "n > 0" by auto
   490   with gcd_zero [of m n] have pos_gcd: "gcd m n > 0" by simp
   491   from assms obtain p where k_m: "k = m * p" using dvd_def by blast
   492   from assms obtain q where k_n: "k = n * q" using dvd_def by blast
   493   from pos_k k_m have pos_p: "p > 0" by auto
   494   from pos_k k_n have pos_q: "q > 0" by auto
   495   have "k * k * gcd q p = k * gcd (k * q) (k * p)"
   496     by (simp add: ac_simps gcd_mult_distrib2)
   497   also have "\<dots> = k * gcd (m * p * q) (n * q * p)"
   498     by (simp add: k_m [symmetric] k_n [symmetric])
   499   also have "\<dots> = k * p * q * gcd m n"
   500     by (simp add: ac_simps gcd_mult_distrib2)
   501   finally have "(m * p) * (n * q) * gcd q p = k * p * q * gcd m n"
   502     by (simp only: k_m [symmetric] k_n [symmetric])
   503   then have "p * q * m * n * gcd q p = p * q * k * gcd m n"
   504     by (simp add: ac_simps)
   505   with pos_p pos_q have "m * n * gcd q p = k * gcd m n"
   506     by simp
   507   with prod_gcd_lcm [of m n]
   508   have "lcm m n * gcd q p * gcd m n = k * gcd m n"
   509     by (simp add: ac_simps)
   510   with pos_gcd have "lcm m n * gcd q p = k" by simp
   511   then show ?thesis using dvd_def by auto
   512 qed
   513 
   514 lemma lcm_dvd1 [iff]:
   515   "m dvd lcm m n"
   516 proof (cases m)
   517   case 0 then show ?thesis by simp
   518 next
   519   case (Suc _)
   520   then have mpos: "m > 0" by simp
   521   show ?thesis
   522   proof (cases n)
   523     case 0 then show ?thesis by simp
   524   next
   525     case (Suc _)
   526     then have npos: "n > 0" by simp
   527     have "gcd m n dvd n" by simp
   528     then obtain k where "n = gcd m n * k" using dvd_def by auto
   529     then have "m * n div gcd m n = m * (gcd m n * k) div gcd m n" by (simp add: ac_simps)
   530     also have "\<dots> = m * k" using mpos npos gcd_zero by simp
   531     finally show ?thesis by (simp add: lcm_def)
   532   qed
   533 qed
   534 
   535 lemma lcm_dvd2 [iff]: 
   536   "n dvd lcm m n"
   537 proof (cases n)
   538   case 0 then show ?thesis by simp
   539 next
   540   case (Suc _)
   541   then have npos: "n > 0" by simp
   542   show ?thesis
   543   proof (cases m)
   544     case 0 then show ?thesis by simp
   545   next
   546     case (Suc _)
   547     then have mpos: "m > 0" by simp
   548     have "gcd m n dvd m" by simp
   549     then obtain k where "m = gcd m n * k" using dvd_def by auto
   550     then have "m * n div gcd m n = (gcd m n * k) * n div gcd m n" by (simp add: ac_simps)
   551     also have "\<dots> = n * k" using mpos npos gcd_zero by simp
   552     finally show ?thesis by (simp add: lcm_def)
   553   qed
   554 qed
   555 
   556 lemma gcd_add1_eq: "gcd (m + k) k = gcd (m + k) m"
   557   by (simp add: gcd_commute)
   558 
   559 lemma gcd_diff2: "m \<le> n ==> gcd n (n - m) = gcd n m"
   560   apply (subgoal_tac "n = m + (n - m)")
   561   apply (erule ssubst, rule gcd_add1_eq, simp)  
   562   done
   563 
   564 
   565 subsection {* GCD and LCM on integers *}
   566 
   567 definition
   568   zgcd :: "int \<Rightarrow> int \<Rightarrow> int" where
   569   "zgcd i j = int (gcd (nat (abs i)) (nat (abs j)))"
   570 
   571 lemma zgcd_zdvd1 [iff, algebra]: "zgcd i j dvd i"
   572 by (simp add: zgcd_def int_dvd_iff)
   573 
   574 lemma zgcd_zdvd2 [iff, algebra]: "zgcd i j dvd j"
   575 by (simp add: zgcd_def int_dvd_iff)
   576 
   577 lemma zgcd_pos: "zgcd i j \<ge> 0"
   578 by (simp add: zgcd_def)
   579 
   580 lemma zgcd0 [simp,algebra]: "(zgcd i j = 0) = (i = 0 \<and> j = 0)"
   581 by (simp add: zgcd_def gcd_zero)
   582 
   583 lemma zgcd_commute: "zgcd i j = zgcd j i"
   584 unfolding zgcd_def by (simp add: gcd_commute)
   585 
   586 lemma zgcd_zminus [simp, algebra]: "zgcd (- i) j = zgcd i j"
   587 unfolding zgcd_def by simp
   588 
   589 lemma zgcd_zminus2 [simp, algebra]: "zgcd i (- j) = zgcd i j"
   590 unfolding zgcd_def by simp
   591 
   592   (* should be solved by algebra*)
   593 lemma zrelprime_dvd_mult: "zgcd i j = 1 \<Longrightarrow> i dvd k * j \<Longrightarrow> i dvd k"
   594   unfolding zgcd_def
   595 proof -
   596   assume "int (gcd (nat \<bar>i\<bar>) (nat \<bar>j\<bar>)) = 1" "i dvd k * j"
   597   then have g: "gcd (nat \<bar>i\<bar>) (nat \<bar>j\<bar>) = 1" by simp
   598   from `i dvd k * j` obtain h where h: "k*j = i*h" unfolding dvd_def by blast
   599   have th: "nat \<bar>i\<bar> dvd nat \<bar>k\<bar> * nat \<bar>j\<bar>"
   600     unfolding dvd_def
   601     by (rule_tac x= "nat \<bar>h\<bar>" in exI, simp add: h nat_abs_mult_distrib [symmetric])
   602   from relprime_dvd_mult [OF g th] obtain h' where h': "nat \<bar>k\<bar> = nat \<bar>i\<bar> * h'"
   603     unfolding dvd_def by blast
   604   from h' have "int (nat \<bar>k\<bar>) = int (nat \<bar>i\<bar> * h')" by simp
   605   then have "\<bar>k\<bar> = \<bar>i\<bar> * int h'" by (simp add: int_mult)
   606   then show ?thesis
   607     apply (subst abs_dvd_iff [symmetric])
   608     apply (subst dvd_abs_iff [symmetric])
   609     apply (unfold dvd_def)
   610     apply (rule_tac x = "int h'" in exI, simp)
   611     done
   612 qed
   613 
   614 lemma int_nat_abs: "int (nat (abs x)) = abs x" by arith
   615 
   616 lemma zgcd_greatest:
   617   assumes "k dvd m" and "k dvd n"
   618   shows "k dvd zgcd m n"
   619 proof -
   620   let ?k' = "nat \<bar>k\<bar>"
   621   let ?m' = "nat \<bar>m\<bar>"
   622   let ?n' = "nat \<bar>n\<bar>"
   623   from `k dvd m` and `k dvd n` have dvd': "?k' dvd ?m'" "?k' dvd ?n'"
   624     unfolding zdvd_int by (simp_all only: int_nat_abs abs_dvd_iff dvd_abs_iff)
   625   from gcd_greatest [OF dvd'] have "int (nat \<bar>k\<bar>) dvd zgcd m n"
   626     unfolding zgcd_def by (simp only: zdvd_int)
   627   then have "\<bar>k\<bar> dvd zgcd m n" by (simp only: int_nat_abs)
   628   then show "k dvd zgcd m n" by simp
   629 qed
   630 
   631 lemma div_zgcd_relprime:
   632   assumes nz: "a \<noteq> 0 \<or> b \<noteq> 0"
   633   shows "zgcd (a div (zgcd a b)) (b div (zgcd a b)) = 1"
   634 proof -
   635   from nz have nz': "nat \<bar>a\<bar> \<noteq> 0 \<or> nat \<bar>b\<bar> \<noteq> 0" by arith 
   636   let ?g = "zgcd a b"
   637   let ?a' = "a div ?g"
   638   let ?b' = "b div ?g"
   639   let ?g' = "zgcd ?a' ?b'"
   640   have dvdg: "?g dvd a" "?g dvd b" by simp_all
   641   have dvdg': "?g' dvd ?a'" "?g' dvd ?b'" by simp_all
   642   from dvdg dvdg' obtain ka kb ka' kb' where
   643    kab: "a = ?g*ka" "b = ?g*kb" "?a' = ?g'*ka'" "?b' = ?g' * kb'"
   644     unfolding dvd_def by blast
   645   from this(3-4) [symmetric] have "?g* ?a' = (?g * ?g') * ka'" "?g* ?b' = (?g * ?g') * kb'"
   646     by (simp_all only: ac_simps mult.left_commute [of _ "zgcd a b"])
   647   then have dvdgg':"?g * ?g' dvd a" "?g* ?g' dvd b"
   648     by (auto simp add: dvd_mult_div_cancel [OF dvdg(1)]
   649       dvd_mult_div_cancel [OF dvdg(2)] dvd_def)
   650   have "?g \<noteq> 0" using nz by simp
   651   then have gp: "?g \<noteq> 0" using zgcd_pos[where i="a" and j="b"] by arith
   652   from zgcd_greatest [OF dvdgg'] have "?g * ?g' dvd ?g" .
   653   with zdvd_mult_cancel1 [OF gp] have "\<bar>?g'\<bar> = 1" by simp
   654   with zgcd_pos show "?g' = 1" by simp
   655 qed
   656 
   657 lemma zgcd_0 [simp, algebra]: "zgcd m 0 = abs m"
   658   by (simp add: zgcd_def abs_if)
   659 
   660 lemma zgcd_0_left [simp, algebra]: "zgcd 0 m = abs m"
   661   by (simp add: zgcd_def abs_if)
   662 
   663 lemma zgcd_non_0: "0 < n ==> zgcd m n = zgcd n (m mod n)"
   664   apply (frule_tac b = n and a = m in pos_mod_sign)
   665   apply (simp del: pos_mod_sign add: zgcd_def abs_if nat_mod_distrib)
   666   apply (auto simp add: gcd_non_0 nat_mod_distrib [symmetric] zmod_zminus1_eq_if)
   667   apply (frule_tac a = m in pos_mod_bound)
   668   apply (simp del: pos_mod_bound add: nat_diff_distrib gcd_diff2 nat_le_eq_zle)
   669   done
   670 
   671 lemma zgcd_eq: "zgcd m n = zgcd n (m mod n)"
   672   apply (cases "n = 0", simp)
   673   apply (auto simp add: linorder_neq_iff zgcd_non_0)
   674   apply (cut_tac m = "-m" and n = "-n" in zgcd_non_0, auto)
   675   done
   676 
   677 lemma zgcd_1 [simp, algebra]: "zgcd m 1 = 1"
   678   by (simp add: zgcd_def abs_if)
   679 
   680 lemma zgcd_0_1_iff [simp, algebra]: "zgcd 0 m = 1 \<longleftrightarrow> \<bar>m\<bar> = 1"
   681   by (simp add: zgcd_def abs_if)
   682 
   683 lemma zgcd_greatest_iff[algebra]: "k dvd zgcd m n = (k dvd m \<and> k dvd n)"
   684   by (simp add: zgcd_def abs_if int_dvd_iff dvd_int_iff nat_dvd_iff)
   685 
   686 lemma zgcd_1_left [simp, algebra]: "zgcd 1 m = 1"
   687   by (simp add: zgcd_def)
   688 
   689 lemma zgcd_assoc: "zgcd (zgcd k m) n = zgcd k (zgcd m n)"
   690   by (simp add: zgcd_def gcd_assoc)
   691 
   692 lemma zgcd_left_commute: "zgcd k (zgcd m n) = zgcd m (zgcd k n)"
   693   apply (rule zgcd_commute [THEN trans])
   694   apply (rule zgcd_assoc [THEN trans])
   695   apply (rule zgcd_commute [THEN arg_cong])
   696   done
   697 
   698 lemmas zgcd_ac = zgcd_assoc zgcd_commute zgcd_left_commute
   699   -- {* addition is an AC-operator *}
   700 
   701 lemma zgcd_zmult_distrib2: "0 \<le> k ==> k * zgcd m n = zgcd (k * m) (k * n)"
   702   by (simp del: minus_mult_right [symmetric]
   703       add: minus_mult_right nat_mult_distrib zgcd_def abs_if
   704           mult_less_0_iff gcd_mult_distrib2 [symmetric] of_nat_mult)
   705 
   706 lemma zgcd_zmult_distrib2_abs: "zgcd (k * m) (k * n) = abs k * zgcd m n"
   707   by (simp add: abs_if zgcd_zmult_distrib2)
   708 
   709 lemma zgcd_self [simp]: "0 \<le> m ==> zgcd m m = m"
   710   by (cut_tac k = m and m = 1 and n = 1 in zgcd_zmult_distrib2, simp_all)
   711 
   712 lemma zgcd_zmult_eq_self [simp]: "0 \<le> k ==> zgcd k (k * n) = k"
   713   by (cut_tac k = k and m = 1 and n = n in zgcd_zmult_distrib2, simp_all)
   714 
   715 lemma zgcd_zmult_eq_self2 [simp]: "0 \<le> k ==> zgcd (k * n) k = k"
   716   by (cut_tac k = k and m = n and n = 1 in zgcd_zmult_distrib2, simp_all)
   717 
   718 
   719 definition "zlcm i j = int (lcm(nat(abs i)) (nat(abs j)))"
   720 
   721 lemma dvd_zlcm_self1[simp, algebra]: "i dvd zlcm i j"
   722 by(simp add:zlcm_def dvd_int_iff)
   723 
   724 lemma dvd_zlcm_self2[simp, algebra]: "j dvd zlcm i j"
   725 by(simp add:zlcm_def dvd_int_iff)
   726 
   727 
   728 lemma dvd_imp_dvd_zlcm1:
   729   assumes "k dvd i" shows "k dvd (zlcm i j)"
   730 proof -
   731   have "nat(abs k) dvd nat(abs i)" using `k dvd i`
   732     by(simp add:int_dvd_iff[symmetric] dvd_int_iff[symmetric])
   733   thus ?thesis by(simp add:zlcm_def dvd_int_iff)(blast intro: dvd_trans)
   734 qed
   735 
   736 lemma dvd_imp_dvd_zlcm2:
   737   assumes "k dvd j" shows "k dvd (zlcm i j)"
   738 proof -
   739   have "nat(abs k) dvd nat(abs j)" using `k dvd j`
   740     by(simp add:int_dvd_iff[symmetric] dvd_int_iff[symmetric])
   741   thus ?thesis by(simp add:zlcm_def dvd_int_iff)(blast intro: dvd_trans)
   742 qed
   743 
   744 
   745 lemma zdvd_self_abs1: "(d::int) dvd (abs d)"
   746 by (case_tac "d <0", simp_all)
   747 
   748 lemma zdvd_self_abs2: "(abs (d::int)) dvd d"
   749 by (case_tac "d<0", simp_all)
   750 
   751 (* lcm a b is positive for positive a and b *)
   752 
   753 lemma lcm_pos: 
   754   assumes mpos: "m > 0"
   755     and npos: "n>0"
   756   shows "lcm m n > 0"
   757 proof (rule ccontr, simp add: lcm_def gcd_zero)
   758   assume h:"m*n div gcd m n = 0"
   759   from mpos npos have "gcd m n \<noteq> 0" using gcd_zero by simp
   760   hence gcdp: "gcd m n > 0" by simp
   761   with h
   762   have "m*n < gcd m n"
   763     by (cases "m * n < gcd m n") (auto simp add: div_if[OF gcdp, where m="m*n"])
   764   moreover 
   765   have "gcd m n dvd m" by simp
   766   with mpos dvd_imp_le have t1:"gcd m n \<le> m" by simp
   767   with npos have t1:"gcd m n *n \<le> m*n" by simp
   768   have "gcd m n \<le> gcd m n*n" using npos by simp
   769   with t1 have "gcd m n \<le> m*n" by arith
   770   ultimately show "False" by simp
   771 qed
   772 
   773 lemma zlcm_pos: 
   774   assumes anz: "a \<noteq> 0"
   775   and bnz: "b \<noteq> 0" 
   776   shows "0 < zlcm a b"
   777 proof-
   778   let ?na = "nat (abs a)"
   779   let ?nb = "nat (abs b)"
   780   have nap: "?na >0" using anz by simp
   781   have nbp: "?nb >0" using bnz by simp
   782   have "0 < lcm ?na ?nb" by (rule lcm_pos[OF nap nbp])
   783   thus ?thesis by (simp add: zlcm_def)
   784 qed
   785 
   786 lemma zgcd_code [code]:
   787   "zgcd k l = \<bar>if l = 0 then k else zgcd l (\<bar>k\<bar> mod \<bar>l\<bar>)\<bar>"
   788   by (simp add: zgcd_def gcd.simps [of "nat \<bar>k\<bar>"] nat_mod_distrib)
   789 
   790 end