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