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