src/HOL/GCD.thy
changeset 28952 15a4b2cf8c34
parent 28562 4e74209f113e
child 29655 ac31940cfb69
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOL/GCD.thy	Wed Dec 03 15:58:44 2008 +0100
     1.3 @@ -0,0 +1,783 @@
     1.4 +(*  Title:      HOL/GCD.thy
     1.5 +    ID:         $Id$
     1.6 +    Author:     Christophe Tabacznyj and Lawrence C Paulson
     1.7 +    Copyright   1996  University of Cambridge
     1.8 +*)
     1.9 +
    1.10 +header {* The Greatest Common Divisor *}
    1.11 +
    1.12 +theory GCD
    1.13 +imports Plain Presburger
    1.14 +begin
    1.15 +
    1.16 +text {*
    1.17 +  See \cite{davenport92}. \bigskip
    1.18 +*}
    1.19 +
    1.20 +subsection {* Specification of GCD on nats *}
    1.21 +
    1.22 +definition
    1.23 +  is_gcd :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> bool" where -- {* @{term gcd} as a relation *}
    1.24 +  [code del]: "is_gcd m n p \<longleftrightarrow> p dvd m \<and> p dvd n \<and>
    1.25 +    (\<forall>d. d dvd m \<longrightarrow> d dvd n \<longrightarrow> d dvd p)"
    1.26 +
    1.27 +text {* Uniqueness *}
    1.28 +
    1.29 +lemma is_gcd_unique: "is_gcd a b m \<Longrightarrow> is_gcd a b n \<Longrightarrow> m = n"
    1.30 +  by (simp add: is_gcd_def) (blast intro: dvd_anti_sym)
    1.31 +
    1.32 +text {* Connection to divides relation *}
    1.33 +
    1.34 +lemma is_gcd_dvd: "is_gcd a b m \<Longrightarrow> k dvd a \<Longrightarrow> k dvd b \<Longrightarrow> k dvd m"
    1.35 +  by (auto simp add: is_gcd_def)
    1.36 +
    1.37 +text {* Commutativity *}
    1.38 +
    1.39 +lemma is_gcd_commute: "is_gcd m n k = is_gcd n m k"
    1.40 +  by (auto simp add: is_gcd_def)
    1.41 +
    1.42 +
    1.43 +subsection {* GCD on nat by Euclid's algorithm *}
    1.44 +
    1.45 +fun
    1.46 +  gcd  :: "nat => nat => nat"
    1.47 +where
    1.48 +  "gcd m n = (if n = 0 then m else gcd n (m mod n))"
    1.49 +lemma gcd_induct [case_names "0" rec]:
    1.50 +  fixes m n :: nat
    1.51 +  assumes "\<And>m. P m 0"
    1.52 +    and "\<And>m n. 0 < n \<Longrightarrow> P n (m mod n) \<Longrightarrow> P m n"
    1.53 +  shows "P m n"
    1.54 +proof (induct m n rule: gcd.induct)
    1.55 +  case (1 m n) with assms show ?case by (cases "n = 0") simp_all
    1.56 +qed
    1.57 +
    1.58 +lemma gcd_0 [simp, algebra]: "gcd m 0 = m"
    1.59 +  by simp
    1.60 +
    1.61 +lemma gcd_0_left [simp,algebra]: "gcd 0 m = m"
    1.62 +  by simp
    1.63 +
    1.64 +lemma gcd_non_0: "n > 0 \<Longrightarrow> gcd m n = gcd n (m mod n)"
    1.65 +  by simp
    1.66 +
    1.67 +lemma gcd_1 [simp, algebra]: "gcd m (Suc 0) = 1"
    1.68 +  by simp
    1.69 +
    1.70 +declare gcd.simps [simp del]
    1.71 +
    1.72 +text {*
    1.73 +  \medskip @{term "gcd m n"} divides @{text m} and @{text n}.  The
    1.74 +  conjunctions don't seem provable separately.
    1.75 +*}
    1.76 +
    1.77 +lemma gcd_dvd1 [iff, algebra]: "gcd m n dvd m"
    1.78 +  and gcd_dvd2 [iff, algebra]: "gcd m n dvd n"
    1.79 +  apply (induct m n rule: gcd_induct)
    1.80 +     apply (simp_all add: gcd_non_0)
    1.81 +  apply (blast dest: dvd_mod_imp_dvd)
    1.82 +  done
    1.83 +
    1.84 +text {*
    1.85 +  \medskip Maximality: for all @{term m}, @{term n}, @{term k}
    1.86 +  naturals, if @{term k} divides @{term m} and @{term k} divides
    1.87 +  @{term n} then @{term k} divides @{term "gcd m n"}.
    1.88 +*}
    1.89 +
    1.90 +lemma gcd_greatest: "k dvd m \<Longrightarrow> k dvd n \<Longrightarrow> k dvd gcd m n"
    1.91 +  by (induct m n rule: gcd_induct) (simp_all add: gcd_non_0 dvd_mod)
    1.92 +
    1.93 +text {*
    1.94 +  \medskip Function gcd yields the Greatest Common Divisor.
    1.95 +*}
    1.96 +
    1.97 +lemma is_gcd: "is_gcd m n (gcd m n) "
    1.98 +  by (simp add: is_gcd_def gcd_greatest)
    1.99 +
   1.100 +
   1.101 +subsection {* Derived laws for GCD *}
   1.102 +
   1.103 +lemma gcd_greatest_iff [iff, algebra]: "k dvd gcd m n \<longleftrightarrow> k dvd m \<and> k dvd n"
   1.104 +  by (blast intro!: gcd_greatest intro: dvd_trans)
   1.105 +
   1.106 +lemma gcd_zero[algebra]: "gcd m n = 0 \<longleftrightarrow> m = 0 \<and> n = 0"
   1.107 +  by (simp only: dvd_0_left_iff [symmetric] gcd_greatest_iff)
   1.108 +
   1.109 +lemma gcd_commute: "gcd m n = gcd n m"
   1.110 +  apply (rule is_gcd_unique)
   1.111 +   apply (rule is_gcd)
   1.112 +  apply (subst is_gcd_commute)
   1.113 +  apply (simp add: is_gcd)
   1.114 +  done
   1.115 +
   1.116 +lemma gcd_assoc: "gcd (gcd k m) n = gcd k (gcd m n)"
   1.117 +  apply (rule is_gcd_unique)
   1.118 +   apply (rule is_gcd)
   1.119 +  apply (simp add: is_gcd_def)
   1.120 +  apply (blast intro: dvd_trans)
   1.121 +  done
   1.122 +
   1.123 +lemma gcd_1_left [simp, algebra]: "gcd (Suc 0) m = 1"
   1.124 +  by (simp add: gcd_commute)
   1.125 +
   1.126 +text {*
   1.127 +  \medskip Multiplication laws
   1.128 +*}
   1.129 +
   1.130 +lemma gcd_mult_distrib2: "k * gcd m n = gcd (k * m) (k * n)"
   1.131 +    -- {* \cite[page 27]{davenport92} *}
   1.132 +  apply (induct m n rule: gcd_induct)
   1.133 +   apply simp
   1.134 +  apply (case_tac "k = 0")
   1.135 +   apply (simp_all add: mod_geq gcd_non_0 mod_mult_distrib2)
   1.136 +  done
   1.137 +
   1.138 +lemma gcd_mult [simp, algebra]: "gcd k (k * n) = k"
   1.139 +  apply (rule gcd_mult_distrib2 [of k 1 n, simplified, symmetric])
   1.140 +  done
   1.141 +
   1.142 +lemma gcd_self [simp, algebra]: "gcd k k = k"
   1.143 +  apply (rule gcd_mult [of k 1, simplified])
   1.144 +  done
   1.145 +
   1.146 +lemma relprime_dvd_mult: "gcd k n = 1 ==> k dvd m * n ==> k dvd m"
   1.147 +  apply (insert gcd_mult_distrib2 [of m k n])
   1.148 +  apply simp
   1.149 +  apply (erule_tac t = m in ssubst)
   1.150 +  apply simp
   1.151 +  done
   1.152 +
   1.153 +lemma relprime_dvd_mult_iff: "gcd k n = 1 ==> (k dvd m * n) = (k dvd m)"
   1.154 +  by (auto intro: relprime_dvd_mult dvd_mult2)
   1.155 +
   1.156 +lemma gcd_mult_cancel: "gcd k n = 1 ==> gcd (k * m) n = gcd m n"
   1.157 +  apply (rule dvd_anti_sym)
   1.158 +   apply (rule gcd_greatest)
   1.159 +    apply (rule_tac n = k in relprime_dvd_mult)
   1.160 +     apply (simp add: gcd_assoc)
   1.161 +     apply (simp add: gcd_commute)
   1.162 +    apply (simp_all add: mult_commute)
   1.163 +  apply (blast intro: dvd_mult)
   1.164 +  done
   1.165 +
   1.166 +
   1.167 +text {* \medskip Addition laws *}
   1.168 +
   1.169 +lemma gcd_add1 [simp, algebra]: "gcd (m + n) n = gcd m n"
   1.170 +  by (cases "n = 0") (auto simp add: gcd_non_0)
   1.171 +
   1.172 +lemma gcd_add2 [simp, algebra]: "gcd m (m + n) = gcd m n"
   1.173 +proof -
   1.174 +  have "gcd m (m + n) = gcd (m + n) m" by (rule gcd_commute)
   1.175 +  also have "... = gcd (n + m) m" by (simp add: add_commute)
   1.176 +  also have "... = gcd n m" by simp
   1.177 +  also have  "... = gcd m n" by (rule gcd_commute)
   1.178 +  finally show ?thesis .
   1.179 +qed
   1.180 +
   1.181 +lemma gcd_add2' [simp, algebra]: "gcd m (n + m) = gcd m n"
   1.182 +  apply (subst add_commute)
   1.183 +  apply (rule gcd_add2)
   1.184 +  done
   1.185 +
   1.186 +lemma gcd_add_mult[algebra]: "gcd m (k * m + n) = gcd m n"
   1.187 +  by (induct k) (simp_all add: add_assoc)
   1.188 +
   1.189 +lemma gcd_dvd_prod: "gcd m n dvd m * n" 
   1.190 +  using mult_dvd_mono [of 1] by auto
   1.191 +
   1.192 +text {*
   1.193 +  \medskip Division by gcd yields rrelatively primes.
   1.194 +*}
   1.195 +
   1.196 +lemma div_gcd_relprime:
   1.197 +  assumes nz: "a \<noteq> 0 \<or> b \<noteq> 0"
   1.198 +  shows "gcd (a div gcd a b) (b div gcd a b) = 1"
   1.199 +proof -
   1.200 +  let ?g = "gcd a b"
   1.201 +  let ?a' = "a div ?g"
   1.202 +  let ?b' = "b div ?g"
   1.203 +  let ?g' = "gcd ?a' ?b'"
   1.204 +  have dvdg: "?g dvd a" "?g dvd b" by simp_all
   1.205 +  have dvdg': "?g' dvd ?a'" "?g' dvd ?b'" by simp_all
   1.206 +  from dvdg dvdg' obtain ka kb ka' kb' where
   1.207 +      kab: "a = ?g * ka" "b = ?g * kb" "?a' = ?g' * ka'" "?b' = ?g' * kb'"
   1.208 +    unfolding dvd_def by blast
   1.209 +  then have "?g * ?a' = (?g * ?g') * ka'" "?g * ?b' = (?g * ?g') * kb'" by simp_all
   1.210 +  then have dvdgg':"?g * ?g' dvd a" "?g* ?g' dvd b"
   1.211 +    by (auto simp add: dvd_mult_div_cancel [OF dvdg(1)]
   1.212 +      dvd_mult_div_cancel [OF dvdg(2)] dvd_def)
   1.213 +  have "?g \<noteq> 0" using nz by (simp add: gcd_zero)
   1.214 +  then have gp: "?g > 0" by simp
   1.215 +  from gcd_greatest [OF dvdgg'] have "?g * ?g' dvd ?g" .
   1.216 +  with dvd_mult_cancel1 [OF gp] show "?g' = 1" by simp
   1.217 +qed
   1.218 +
   1.219 +
   1.220 +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"
   1.221 +proof(auto)
   1.222 +  assume H: "d dvd a" "d dvd b" "\<forall>e. e dvd a \<and> e dvd b \<longrightarrow> e dvd d"
   1.223 +  from H(3)[rule_format] gcd_dvd1[of a b] gcd_dvd2[of a b] 
   1.224 +  have th: "gcd a b dvd d" by blast
   1.225 +  from dvd_anti_sym[OF th gcd_greatest[OF H(1,2)]]  show "d = gcd a b" by blast 
   1.226 +qed
   1.227 +
   1.228 +lemma gcd_eq: assumes H: "\<forall>d. d dvd x \<and> d dvd y \<longleftrightarrow> d dvd u \<and> d dvd v"
   1.229 +  shows "gcd x y = gcd u v"
   1.230 +proof-
   1.231 +  from H have "\<forall>d. d dvd x \<and> d dvd y \<longleftrightarrow> d dvd gcd u v" by simp
   1.232 +  with gcd_unique[of "gcd u v" x y]  show ?thesis by auto
   1.233 +qed
   1.234 +
   1.235 +lemma ind_euclid: 
   1.236 +  assumes c: " \<forall>a b. P (a::nat) b \<longleftrightarrow> P b a" and z: "\<forall>a. P a 0" 
   1.237 +  and add: "\<forall>a b. P a b \<longrightarrow> P a (a + b)" 
   1.238 +  shows "P a b"
   1.239 +proof(induct n\<equiv>"a+b" arbitrary: a b rule: nat_less_induct)
   1.240 +  fix n a b
   1.241 +  assume H: "\<forall>m < n. \<forall>a b. m = a + b \<longrightarrow> P a b" "n = a + b"
   1.242 +  have "a = b \<or> a < b \<or> b < a" by arith
   1.243 +  moreover {assume eq: "a= b"
   1.244 +    from add[rule_format, OF z[rule_format, of a]] have "P a b" using eq by simp}
   1.245 +  moreover
   1.246 +  {assume lt: "a < b"
   1.247 +    hence "a + b - a < n \<or> a = 0"  using H(2) by arith
   1.248 +    moreover
   1.249 +    {assume "a =0" with z c have "P a b" by blast }
   1.250 +    moreover
   1.251 +    {assume ab: "a + b - a < n"
   1.252 +      have th0: "a + b - a = a + (b - a)" using lt by arith
   1.253 +      from add[rule_format, OF H(1)[rule_format, OF ab th0]]
   1.254 +      have "P a b" by (simp add: th0[symmetric])}
   1.255 +    ultimately have "P a b" by blast}
   1.256 +  moreover
   1.257 +  {assume lt: "a > b"
   1.258 +    hence "b + a - b < n \<or> b = 0"  using H(2) by arith
   1.259 +    moreover
   1.260 +    {assume "b =0" with z c have "P a b" by blast }
   1.261 +    moreover
   1.262 +    {assume ab: "b + a - b < n"
   1.263 +      have th0: "b + a - b = b + (a - b)" using lt by arith
   1.264 +      from add[rule_format, OF H(1)[rule_format, OF ab th0]]
   1.265 +      have "P b a" by (simp add: th0[symmetric])
   1.266 +      hence "P a b" using c by blast }
   1.267 +    ultimately have "P a b" by blast}
   1.268 +ultimately  show "P a b" by blast
   1.269 +qed
   1.270 +
   1.271 +lemma bezout_lemma: 
   1.272 +  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)"
   1.273 +  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)"
   1.274 +using ex
   1.275 +apply clarsimp
   1.276 +apply (rule_tac x="d" in exI, simp add: dvd_add)
   1.277 +apply (case_tac "a * x = b * y + d" , simp_all)
   1.278 +apply (rule_tac x="x + y" in exI)
   1.279 +apply (rule_tac x="y" in exI)
   1.280 +apply algebra
   1.281 +apply (rule_tac x="x" in exI)
   1.282 +apply (rule_tac x="x + y" in exI)
   1.283 +apply algebra
   1.284 +done
   1.285 +
   1.286 +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)"
   1.287 +apply(induct a b rule: ind_euclid)
   1.288 +apply blast
   1.289 +apply clarify
   1.290 +apply (rule_tac x="a" in exI, simp add: dvd_add)
   1.291 +apply clarsimp
   1.292 +apply (rule_tac x="d" in exI)
   1.293 +apply (case_tac "a * x = b * y + d", simp_all add: dvd_add)
   1.294 +apply (rule_tac x="x+y" in exI)
   1.295 +apply (rule_tac x="y" in exI)
   1.296 +apply algebra
   1.297 +apply (rule_tac x="x" in exI)
   1.298 +apply (rule_tac x="x+y" in exI)
   1.299 +apply algebra
   1.300 +done
   1.301 +
   1.302 +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)"
   1.303 +using bezout_add[of a b]
   1.304 +apply clarsimp
   1.305 +apply (rule_tac x="d" in exI, simp)
   1.306 +apply (rule_tac x="x" in exI)
   1.307 +apply (rule_tac x="y" in exI)
   1.308 +apply auto
   1.309 +done
   1.310 +
   1.311 +
   1.312 +text {* We can get a stronger version with a nonzeroness assumption. *}
   1.313 +lemma divides_le: "m dvd n ==> m <= n \<or> n = (0::nat)" by (auto simp add: dvd_def)
   1.314 +
   1.315 +lemma bezout_add_strong: assumes nz: "a \<noteq> (0::nat)"
   1.316 +  shows "\<exists>d x y. d dvd a \<and> d dvd b \<and> a * x = b * y + d"
   1.317 +proof-
   1.318 +  from nz have ap: "a > 0" by simp
   1.319 + from bezout_add[of a b] 
   1.320 + 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
   1.321 + moreover
   1.322 + {fix d x y assume H: "d dvd a" "d dvd b" "a * x = b * y + d"
   1.323 +   from H have ?thesis by blast }
   1.324 + moreover
   1.325 + {fix d x y assume H: "d dvd a" "d dvd b" "b * x = a * y + d"
   1.326 +   {assume b0: "b = 0" with H  have ?thesis by simp}
   1.327 +   moreover 
   1.328 +   {assume b: "b \<noteq> 0" hence bp: "b > 0" by simp
   1.329 +     from divides_le[OF H(2)] b have "d < b \<or> d = b" using le_less by blast
   1.330 +     moreover
   1.331 +     {assume db: "d=b"
   1.332 +       from prems have ?thesis apply simp
   1.333 +	 apply (rule exI[where x = b], simp)
   1.334 +	 apply (rule exI[where x = b])
   1.335 +	by (rule exI[where x = "a - 1"], simp add: diff_mult_distrib2)}
   1.336 +    moreover
   1.337 +    {assume db: "d < b" 
   1.338 +	{assume "x=0" hence ?thesis  using prems by simp }
   1.339 +	moreover
   1.340 +	{assume x0: "x \<noteq> 0" hence xp: "x > 0" by simp
   1.341 +	  
   1.342 +	  from db have "d \<le> b - 1" by simp
   1.343 +	  hence "d*b \<le> b*(b - 1)" by simp
   1.344 +	  with xp mult_mono[of "1" "x" "d*b" "b*(b - 1)"]
   1.345 +	  have dble: "d*b \<le> x*b*(b - 1)" using bp by simp
   1.346 +	  from H (3) have "a * ((b - 1) * y) + d * (b - 1 + 1) = d + x*b*(b - 1)" by algebra
   1.347 +	  hence "a * ((b - 1) * y) = d + x*b*(b - 1) - d*b" using bp by simp
   1.348 +	  hence "a * ((b - 1) * y) = d + (x*b*(b - 1) - d*b)" 
   1.349 +	    by (simp only: diff_add_assoc[OF dble, of d, symmetric])
   1.350 +	  hence "a * ((b - 1) * y) = b*(x*(b - 1) - d) + d"
   1.351 +	    by (simp only: diff_mult_distrib2 add_commute mult_ac)
   1.352 +	  hence ?thesis using H(1,2)
   1.353 +	    apply -
   1.354 +	    apply (rule exI[where x=d], simp)
   1.355 +	    apply (rule exI[where x="(b - 1) * y"])
   1.356 +	    by (rule exI[where x="x*(b - 1) - d"], simp)}
   1.357 +	ultimately have ?thesis by blast}
   1.358 +    ultimately have ?thesis by blast}
   1.359 +  ultimately have ?thesis by blast}
   1.360 + ultimately show ?thesis by blast
   1.361 +qed
   1.362 +
   1.363 +
   1.364 +lemma bezout_gcd: "\<exists>x y. a * x - b * y = gcd a b \<or> b * x - a * y = gcd a b"
   1.365 +proof-
   1.366 +  let ?g = "gcd a b"
   1.367 +  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
   1.368 +  from d(1,2) have "d dvd ?g" by simp
   1.369 +  then obtain k where k: "?g = d*k" unfolding dvd_def by blast
   1.370 +  from d(3) have "(a * x - b * y)*k = d*k \<or> (b * x - a * y)*k = d*k" by blast 
   1.371 +  hence "a * x * k - b * y*k = d*k \<or> b * x * k - a * y*k = d*k" 
   1.372 +    by (algebra add: diff_mult_distrib)
   1.373 +  hence "a * (x * k) - b * (y*k) = ?g \<or> b * (x * k) - a * (y*k) = ?g" 
   1.374 +    by (simp add: k mult_assoc)
   1.375 +  thus ?thesis by blast
   1.376 +qed
   1.377 +
   1.378 +lemma bezout_gcd_strong: assumes a: "a \<noteq> 0" 
   1.379 +  shows "\<exists>x y. a * x = b * y + gcd a b"
   1.380 +proof-
   1.381 +  let ?g = "gcd a b"
   1.382 +  from bezout_add_strong[OF a, of b]
   1.383 +  obtain d x y where d: "d dvd a" "d dvd b" "a * x = b * y + d" by blast
   1.384 +  from d(1,2) have "d dvd ?g" by simp
   1.385 +  then obtain k where k: "?g = d*k" unfolding dvd_def by blast
   1.386 +  from d(3) have "a * x * k = (b * y + d) *k " by algebra
   1.387 +  hence "a * (x * k) = b * (y*k) + ?g" by (algebra add: k)
   1.388 +  thus ?thesis by blast
   1.389 +qed
   1.390 +
   1.391 +lemma gcd_mult_distrib: "gcd(a * c) (b * c) = c * gcd a b"
   1.392 +by(simp add: gcd_mult_distrib2 mult_commute)
   1.393 +
   1.394 +lemma gcd_bezout: "(\<exists>x y. a * x - b * y = d \<or> b * x - a * y = d) \<longleftrightarrow> gcd a b dvd d"
   1.395 +  (is "?lhs \<longleftrightarrow> ?rhs")
   1.396 +proof-
   1.397 +  let ?g = "gcd a b"
   1.398 +  {assume H: ?rhs then obtain k where k: "d = ?g*k" unfolding dvd_def by blast
   1.399 +    from bezout_gcd[of a b] obtain x y where xy: "a * x - b * y = ?g \<or> b * x - a * y = ?g"
   1.400 +      by blast
   1.401 +    hence "(a * x - b * y)*k = ?g*k \<or> (b * x - a * y)*k = ?g*k" by auto
   1.402 +    hence "a * x*k - b * y*k = ?g*k \<or> b * x * k - a * y*k = ?g*k" 
   1.403 +      by (simp only: diff_mult_distrib)
   1.404 +    hence "a * (x*k) - b * (y*k) = d \<or> b * (x * k) - a * (y*k) = d"
   1.405 +      by (simp add: k[symmetric] mult_assoc)
   1.406 +    hence ?lhs by blast}
   1.407 +  moreover
   1.408 +  {fix x y assume H: "a * x - b * y = d \<or> b * x - a * y = d"
   1.409 +    have dv: "?g dvd a*x" "?g dvd b * y" "?g dvd b*x" "?g dvd a * y"
   1.410 +      using dvd_mult2[OF gcd_dvd1[of a b]] dvd_mult2[OF gcd_dvd2[of a b]] by simp_all
   1.411 +    from dvd_diff[OF dv(1,2)] dvd_diff[OF dv(3,4)] H
   1.412 +    have ?rhs by auto}
   1.413 +  ultimately show ?thesis by blast
   1.414 +qed
   1.415 +
   1.416 +lemma gcd_bezout_sum: assumes H:"a * x + b * y = d" shows "gcd a b dvd d"
   1.417 +proof-
   1.418 +  let ?g = "gcd a b"
   1.419 +    have dv: "?g dvd a*x" "?g dvd b * y" 
   1.420 +      using dvd_mult2[OF gcd_dvd1[of a b]] dvd_mult2[OF gcd_dvd2[of a b]] by simp_all
   1.421 +    from dvd_add[OF dv] H
   1.422 +    show ?thesis by auto
   1.423 +qed
   1.424 +
   1.425 +lemma gcd_mult': "gcd b (a * b) = b"
   1.426 +by (simp add: gcd_mult mult_commute[of a b]) 
   1.427 +
   1.428 +lemma gcd_add: "gcd(a + b) b = gcd a b" 
   1.429 +  "gcd(b + a) b = gcd a b" "gcd a (a + b) = gcd a b" "gcd a (b + a) = gcd a b"
   1.430 +apply (simp_all add: gcd_add1)
   1.431 +by (simp add: gcd_commute gcd_add1)
   1.432 +
   1.433 +lemma gcd_sub: "b <= a ==> gcd(a - b) b = gcd a b" "a <= b ==> gcd a (b - a) = gcd a b"
   1.434 +proof-
   1.435 +  {fix a b assume H: "b \<le> (a::nat)"
   1.436 +    hence th: "a - b + b = a" by arith
   1.437 +    from gcd_add(1)[of "a - b" b] th  have "gcd(a - b) b = gcd a b" by simp}
   1.438 +  note th = this
   1.439 +{
   1.440 +  assume ab: "b \<le> a"
   1.441 +  from th[OF ab] show "gcd (a - b)  b = gcd a b" by blast
   1.442 +next
   1.443 +  assume ab: "a \<le> b"
   1.444 +  from th[OF ab] show "gcd a (b - a) = gcd a b" 
   1.445 +    by (simp add: gcd_commute)}
   1.446 +qed
   1.447 +
   1.448 +
   1.449 +subsection {* LCM defined by GCD *}
   1.450 +
   1.451 +
   1.452 +definition
   1.453 +  lcm :: "nat \<Rightarrow> nat \<Rightarrow> nat"
   1.454 +where
   1.455 +  lcm_def: "lcm m n = m * n div gcd m n"
   1.456 +
   1.457 +lemma prod_gcd_lcm:
   1.458 +  "m * n = gcd m n * lcm m n"
   1.459 +  unfolding lcm_def by (simp add: dvd_mult_div_cancel [OF gcd_dvd_prod])
   1.460 +
   1.461 +lemma lcm_0 [simp]: "lcm m 0 = 0"
   1.462 +  unfolding lcm_def by simp
   1.463 +
   1.464 +lemma lcm_1 [simp]: "lcm m 1 = m"
   1.465 +  unfolding lcm_def by simp
   1.466 +
   1.467 +lemma lcm_0_left [simp]: "lcm 0 n = 0"
   1.468 +  unfolding lcm_def by simp
   1.469 +
   1.470 +lemma lcm_1_left [simp]: "lcm 1 m = m"
   1.471 +  unfolding lcm_def by simp
   1.472 +
   1.473 +lemma dvd_pos:
   1.474 +  fixes n m :: nat
   1.475 +  assumes "n > 0" and "m dvd n"
   1.476 +  shows "m > 0"
   1.477 +using assms by (cases m) auto
   1.478 +
   1.479 +lemma lcm_least:
   1.480 +  assumes "m dvd k" and "n dvd k"
   1.481 +  shows "lcm m n dvd k"
   1.482 +proof (cases k)
   1.483 +  case 0 then show ?thesis by auto
   1.484 +next
   1.485 +  case (Suc _) then have pos_k: "k > 0" by auto
   1.486 +  from assms dvd_pos [OF this] have pos_mn: "m > 0" "n > 0" by auto
   1.487 +  with gcd_zero [of m n] have pos_gcd: "gcd m n > 0" by simp
   1.488 +  from assms obtain p where k_m: "k = m * p" using dvd_def by blast
   1.489 +  from assms obtain q where k_n: "k = n * q" using dvd_def by blast
   1.490 +  from pos_k k_m have pos_p: "p > 0" by auto
   1.491 +  from pos_k k_n have pos_q: "q > 0" by auto
   1.492 +  have "k * k * gcd q p = k * gcd (k * q) (k * p)"
   1.493 +    by (simp add: mult_ac gcd_mult_distrib2)
   1.494 +  also have "\<dots> = k * gcd (m * p * q) (n * q * p)"
   1.495 +    by (simp add: k_m [symmetric] k_n [symmetric])
   1.496 +  also have "\<dots> = k * p * q * gcd m n"
   1.497 +    by (simp add: mult_ac gcd_mult_distrib2)
   1.498 +  finally have "(m * p) * (n * q) * gcd q p = k * p * q * gcd m n"
   1.499 +    by (simp only: k_m [symmetric] k_n [symmetric])
   1.500 +  then have "p * q * m * n * gcd q p = p * q * k * gcd m n"
   1.501 +    by (simp add: mult_ac)
   1.502 +  with pos_p pos_q have "m * n * gcd q p = k * gcd m n"
   1.503 +    by simp
   1.504 +  with prod_gcd_lcm [of m n]
   1.505 +  have "lcm m n * gcd q p * gcd m n = k * gcd m n"
   1.506 +    by (simp add: mult_ac)
   1.507 +  with pos_gcd have "lcm m n * gcd q p = k" by simp
   1.508 +  then show ?thesis using dvd_def by auto
   1.509 +qed
   1.510 +
   1.511 +lemma lcm_dvd1 [iff]:
   1.512 +  "m dvd lcm m n"
   1.513 +proof (cases m)
   1.514 +  case 0 then show ?thesis by simp
   1.515 +next
   1.516 +  case (Suc _)
   1.517 +  then have mpos: "m > 0" by simp
   1.518 +  show ?thesis
   1.519 +  proof (cases n)
   1.520 +    case 0 then show ?thesis by simp
   1.521 +  next
   1.522 +    case (Suc _)
   1.523 +    then have npos: "n > 0" by simp
   1.524 +    have "gcd m n dvd n" by simp
   1.525 +    then obtain k where "n = gcd m n * k" using dvd_def by auto
   1.526 +    then have "m * n div gcd m n = m * (gcd m n * k) div gcd m n" by (simp add: mult_ac)
   1.527 +    also have "\<dots> = m * k" using mpos npos gcd_zero by simp
   1.528 +    finally show ?thesis by (simp add: lcm_def)
   1.529 +  qed
   1.530 +qed
   1.531 +
   1.532 +lemma lcm_dvd2 [iff]: 
   1.533 +  "n dvd lcm m n"
   1.534 +proof (cases n)
   1.535 +  case 0 then show ?thesis by simp
   1.536 +next
   1.537 +  case (Suc _)
   1.538 +  then have npos: "n > 0" by simp
   1.539 +  show ?thesis
   1.540 +  proof (cases m)
   1.541 +    case 0 then show ?thesis by simp
   1.542 +  next
   1.543 +    case (Suc _)
   1.544 +    then have mpos: "m > 0" by simp
   1.545 +    have "gcd m n dvd m" by simp
   1.546 +    then obtain k where "m = gcd m n * k" using dvd_def by auto
   1.547 +    then have "m * n div gcd m n = (gcd m n * k) * n div gcd m n" by (simp add: mult_ac)
   1.548 +    also have "\<dots> = n * k" using mpos npos gcd_zero by simp
   1.549 +    finally show ?thesis by (simp add: lcm_def)
   1.550 +  qed
   1.551 +qed
   1.552 +
   1.553 +lemma gcd_add1_eq: "gcd (m + k) k = gcd (m + k) m"
   1.554 +  by (simp add: gcd_commute)
   1.555 +
   1.556 +lemma gcd_diff2: "m \<le> n ==> gcd n (n - m) = gcd n m"
   1.557 +  apply (subgoal_tac "n = m + (n - m)")
   1.558 +  apply (erule ssubst, rule gcd_add1_eq, simp)  
   1.559 +  done
   1.560 +
   1.561 +
   1.562 +subsection {* GCD and LCM on integers *}
   1.563 +
   1.564 +definition
   1.565 +  zgcd :: "int \<Rightarrow> int \<Rightarrow> int" where
   1.566 +  "zgcd i j = int (gcd (nat (abs i)) (nat (abs j)))"
   1.567 +
   1.568 +lemma zgcd_zdvd1 [iff,simp, algebra]: "zgcd i j dvd i"
   1.569 +  by (simp add: zgcd_def int_dvd_iff)
   1.570 +
   1.571 +lemma zgcd_zdvd2 [iff,simp, algebra]: "zgcd i j dvd j"
   1.572 +  by (simp add: zgcd_def int_dvd_iff)
   1.573 +
   1.574 +lemma zgcd_pos: "zgcd i j \<ge> 0"
   1.575 +  by (simp add: zgcd_def)
   1.576 +
   1.577 +lemma zgcd0 [simp,algebra]: "(zgcd i j = 0) = (i = 0 \<and> j = 0)"
   1.578 +  by (simp add: zgcd_def gcd_zero) arith
   1.579 +
   1.580 +lemma zgcd_commute: "zgcd i j = zgcd j i"
   1.581 +  unfolding zgcd_def by (simp add: gcd_commute)
   1.582 +
   1.583 +lemma zgcd_zminus [simp, algebra]: "zgcd (- i) j = zgcd i j"
   1.584 +  unfolding zgcd_def by simp
   1.585 +
   1.586 +lemma zgcd_zminus2 [simp, algebra]: "zgcd i (- j) = zgcd i j"
   1.587 +  unfolding zgcd_def by simp
   1.588 +
   1.589 +  (* should be solved by algebra*)
   1.590 +lemma zrelprime_dvd_mult: "zgcd i j = 1 \<Longrightarrow> i dvd k * j \<Longrightarrow> i dvd k"
   1.591 +  unfolding zgcd_def
   1.592 +proof -
   1.593 +  assume "int (gcd (nat \<bar>i\<bar>) (nat \<bar>j\<bar>)) = 1" "i dvd k * j"
   1.594 +  then have g: "gcd (nat \<bar>i\<bar>) (nat \<bar>j\<bar>) = 1" by simp
   1.595 +  from `i dvd k * j` obtain h where h: "k*j = i*h" unfolding dvd_def by blast
   1.596 +  have th: "nat \<bar>i\<bar> dvd nat \<bar>k\<bar> * nat \<bar>j\<bar>"
   1.597 +    unfolding dvd_def
   1.598 +    by (rule_tac x= "nat \<bar>h\<bar>" in exI, simp add: h nat_abs_mult_distrib [symmetric])
   1.599 +  from relprime_dvd_mult [OF g th] obtain h' where h': "nat \<bar>k\<bar> = nat \<bar>i\<bar> * h'"
   1.600 +    unfolding dvd_def by blast
   1.601 +  from h' have "int (nat \<bar>k\<bar>) = int (nat \<bar>i\<bar> * h')" by simp
   1.602 +  then have "\<bar>k\<bar> = \<bar>i\<bar> * int h'" by (simp add: int_mult)
   1.603 +  then show ?thesis
   1.604 +    apply (subst zdvd_abs1 [symmetric])
   1.605 +    apply (subst zdvd_abs2 [symmetric])
   1.606 +    apply (unfold dvd_def)
   1.607 +    apply (rule_tac x = "int h'" in exI, simp)
   1.608 +    done
   1.609 +qed
   1.610 +
   1.611 +lemma int_nat_abs: "int (nat (abs x)) = abs x" by arith
   1.612 +
   1.613 +lemma zgcd_greatest:
   1.614 +  assumes "k dvd m" and "k dvd n"
   1.615 +  shows "k dvd zgcd m n"
   1.616 +proof -
   1.617 +  let ?k' = "nat \<bar>k\<bar>"
   1.618 +  let ?m' = "nat \<bar>m\<bar>"
   1.619 +  let ?n' = "nat \<bar>n\<bar>"
   1.620 +  from `k dvd m` and `k dvd n` have dvd': "?k' dvd ?m'" "?k' dvd ?n'"
   1.621 +    unfolding zdvd_int by (simp_all only: int_nat_abs zdvd_abs1 zdvd_abs2)
   1.622 +  from gcd_greatest [OF dvd'] have "int (nat \<bar>k\<bar>) dvd zgcd m n"
   1.623 +    unfolding zgcd_def by (simp only: zdvd_int)
   1.624 +  then have "\<bar>k\<bar> dvd zgcd m n" by (simp only: int_nat_abs)
   1.625 +  then show "k dvd zgcd m n" by (simp add: zdvd_abs1)
   1.626 +qed
   1.627 +
   1.628 +lemma div_zgcd_relprime:
   1.629 +  assumes nz: "a \<noteq> 0 \<or> b \<noteq> 0"
   1.630 +  shows "zgcd (a div (zgcd a b)) (b div (zgcd a b)) = 1"
   1.631 +proof -
   1.632 +  from nz have nz': "nat \<bar>a\<bar> \<noteq> 0 \<or> nat \<bar>b\<bar> \<noteq> 0" by arith 
   1.633 +  let ?g = "zgcd a b"
   1.634 +  let ?a' = "a div ?g"
   1.635 +  let ?b' = "b div ?g"
   1.636 +  let ?g' = "zgcd ?a' ?b'"
   1.637 +  have dvdg: "?g dvd a" "?g dvd b" by (simp_all add: zgcd_zdvd1 zgcd_zdvd2)
   1.638 +  have dvdg': "?g' dvd ?a'" "?g' dvd ?b'" by (simp_all add: zgcd_zdvd1 zgcd_zdvd2)
   1.639 +  from dvdg dvdg' obtain ka kb ka' kb' where
   1.640 +   kab: "a = ?g*ka" "b = ?g*kb" "?a' = ?g'*ka'" "?b' = ?g' * kb'"
   1.641 +    unfolding dvd_def by blast
   1.642 +  then have "?g* ?a' = (?g * ?g') * ka'" "?g* ?b' = (?g * ?g') * kb'" by simp_all
   1.643 +  then have dvdgg':"?g * ?g' dvd a" "?g* ?g' dvd b"
   1.644 +    by (auto simp add: zdvd_mult_div_cancel [OF dvdg(1)]
   1.645 +      zdvd_mult_div_cancel [OF dvdg(2)] dvd_def)
   1.646 +  have "?g \<noteq> 0" using nz by simp
   1.647 +  then have gp: "?g \<noteq> 0" using zgcd_pos[where i="a" and j="b"] by arith
   1.648 +  from zgcd_greatest [OF dvdgg'] have "?g * ?g' dvd ?g" .
   1.649 +  with zdvd_mult_cancel1 [OF gp] have "\<bar>?g'\<bar> = 1" by simp
   1.650 +  with zgcd_pos show "?g' = 1" by simp
   1.651 +qed
   1.652 +
   1.653 +lemma zgcd_0 [simp, algebra]: "zgcd m 0 = abs m"
   1.654 +  by (simp add: zgcd_def abs_if)
   1.655 +
   1.656 +lemma zgcd_0_left [simp, algebra]: "zgcd 0 m = abs m"
   1.657 +  by (simp add: zgcd_def abs_if)
   1.658 +
   1.659 +lemma zgcd_non_0: "0 < n ==> zgcd m n = zgcd n (m mod n)"
   1.660 +  apply (frule_tac b = n and a = m in pos_mod_sign)
   1.661 +  apply (simp del: pos_mod_sign add: zgcd_def abs_if nat_mod_distrib)
   1.662 +  apply (auto simp add: gcd_non_0 nat_mod_distrib [symmetric] zmod_zminus1_eq_if)
   1.663 +  apply (frule_tac a = m in pos_mod_bound)
   1.664 +  apply (simp del: pos_mod_bound add: nat_diff_distrib gcd_diff2 nat_le_eq_zle)
   1.665 +  done
   1.666 +
   1.667 +lemma zgcd_eq: "zgcd m n = zgcd n (m mod n)"
   1.668 +  apply (case_tac "n = 0", simp add: DIVISION_BY_ZERO)
   1.669 +  apply (auto simp add: linorder_neq_iff zgcd_non_0)
   1.670 +  apply (cut_tac m = "-m" and n = "-n" in zgcd_non_0, auto)
   1.671 +  done
   1.672 +
   1.673 +lemma zgcd_1 [simp, algebra]: "zgcd m 1 = 1"
   1.674 +  by (simp add: zgcd_def abs_if)
   1.675 +
   1.676 +lemma zgcd_0_1_iff [simp, algebra]: "zgcd 0 m = 1 \<longleftrightarrow> \<bar>m\<bar> = 1"
   1.677 +  by (simp add: zgcd_def abs_if)
   1.678 +
   1.679 +lemma zgcd_greatest_iff[algebra]: "k dvd zgcd m n = (k dvd m \<and> k dvd n)"
   1.680 +  by (simp add: zgcd_def abs_if int_dvd_iff dvd_int_iff nat_dvd_iff)
   1.681 +
   1.682 +lemma zgcd_1_left [simp, algebra]: "zgcd 1 m = 1"
   1.683 +  by (simp add: zgcd_def gcd_1_left)
   1.684 +
   1.685 +lemma zgcd_assoc: "zgcd (zgcd k m) n = zgcd k (zgcd m n)"
   1.686 +  by (simp add: zgcd_def gcd_assoc)
   1.687 +
   1.688 +lemma zgcd_left_commute: "zgcd k (zgcd m n) = zgcd m (zgcd k n)"
   1.689 +  apply (rule zgcd_commute [THEN trans])
   1.690 +  apply (rule zgcd_assoc [THEN trans])
   1.691 +  apply (rule zgcd_commute [THEN arg_cong])
   1.692 +  done
   1.693 +
   1.694 +lemmas zgcd_ac = zgcd_assoc zgcd_commute zgcd_left_commute
   1.695 +  -- {* addition is an AC-operator *}
   1.696 +
   1.697 +lemma zgcd_zmult_distrib2: "0 \<le> k ==> k * zgcd m n = zgcd (k * m) (k * n)"
   1.698 +  by (simp del: minus_mult_right [symmetric]
   1.699 +      add: minus_mult_right nat_mult_distrib zgcd_def abs_if
   1.700 +          mult_less_0_iff gcd_mult_distrib2 [symmetric] zmult_int [symmetric])
   1.701 +
   1.702 +lemma zgcd_zmult_distrib2_abs: "zgcd (k * m) (k * n) = abs k * zgcd m n"
   1.703 +  by (simp add: abs_if zgcd_zmult_distrib2)
   1.704 +
   1.705 +lemma zgcd_self [simp]: "0 \<le> m ==> zgcd m m = m"
   1.706 +  by (cut_tac k = m and m = 1 and n = 1 in zgcd_zmult_distrib2, simp_all)
   1.707 +
   1.708 +lemma zgcd_zmult_eq_self [simp]: "0 \<le> k ==> zgcd k (k * n) = k"
   1.709 +  by (cut_tac k = k and m = 1 and n = n in zgcd_zmult_distrib2, simp_all)
   1.710 +
   1.711 +lemma zgcd_zmult_eq_self2 [simp]: "0 \<le> k ==> zgcd (k * n) k = k"
   1.712 +  by (cut_tac k = k and m = n and n = 1 in zgcd_zmult_distrib2, simp_all)
   1.713 +
   1.714 +
   1.715 +definition "zlcm i j = int (lcm(nat(abs i)) (nat(abs j)))"
   1.716 +
   1.717 +lemma dvd_zlcm_self1[simp, algebra]: "i dvd zlcm i j"
   1.718 +by(simp add:zlcm_def dvd_int_iff)
   1.719 +
   1.720 +lemma dvd_zlcm_self2[simp, algebra]: "j dvd zlcm i j"
   1.721 +by(simp add:zlcm_def dvd_int_iff)
   1.722 +
   1.723 +
   1.724 +lemma dvd_imp_dvd_zlcm1:
   1.725 +  assumes "k dvd i" shows "k dvd (zlcm i j)"
   1.726 +proof -
   1.727 +  have "nat(abs k) dvd nat(abs i)" using `k dvd i`
   1.728 +    by(simp add:int_dvd_iff[symmetric] dvd_int_iff[symmetric] zdvd_abs1)
   1.729 +  thus ?thesis by(simp add:zlcm_def dvd_int_iff)(blast intro: dvd_trans)
   1.730 +qed
   1.731 +
   1.732 +lemma dvd_imp_dvd_zlcm2:
   1.733 +  assumes "k dvd j" shows "k dvd (zlcm i j)"
   1.734 +proof -
   1.735 +  have "nat(abs k) dvd nat(abs j)" using `k dvd j`
   1.736 +    by(simp add:int_dvd_iff[symmetric] dvd_int_iff[symmetric] zdvd_abs1)
   1.737 +  thus ?thesis by(simp add:zlcm_def dvd_int_iff)(blast intro: dvd_trans)
   1.738 +qed
   1.739 +
   1.740 +
   1.741 +lemma zdvd_self_abs1: "(d::int) dvd (abs d)"
   1.742 +by (case_tac "d <0", simp_all)
   1.743 +
   1.744 +lemma zdvd_self_abs2: "(abs (d::int)) dvd d"
   1.745 +by (case_tac "d<0", simp_all)
   1.746 +
   1.747 +(* lcm a b is positive for positive a and b *)
   1.748 +
   1.749 +lemma lcm_pos: 
   1.750 +  assumes mpos: "m > 0"
   1.751 +  and npos: "n>0"
   1.752 +  shows "lcm m n > 0"
   1.753 +proof(rule ccontr, simp add: lcm_def gcd_zero)
   1.754 +assume h:"m*n div gcd m n = 0"
   1.755 +from mpos npos have "gcd m n \<noteq> 0" using gcd_zero by simp
   1.756 +hence gcdp: "gcd m n > 0" by simp
   1.757 +with h
   1.758 +have "m*n < gcd m n"
   1.759 +  by (cases "m * n < gcd m n") (auto simp add: div_if[OF gcdp, where m="m*n"])
   1.760 +moreover 
   1.761 +have "gcd m n dvd m" by simp
   1.762 + with mpos dvd_imp_le have t1:"gcd m n \<le> m" by simp
   1.763 + with npos have t1:"gcd m n *n \<le> m*n" by simp
   1.764 + have "gcd m n \<le> gcd m n*n" using npos by simp
   1.765 + with t1 have "gcd m n \<le> m*n" by arith
   1.766 +ultimately show "False" by simp
   1.767 +qed
   1.768 +
   1.769 +lemma zlcm_pos: 
   1.770 +  assumes anz: "a \<noteq> 0"
   1.771 +  and bnz: "b \<noteq> 0" 
   1.772 +  shows "0 < zlcm a b"
   1.773 +proof-
   1.774 +  let ?na = "nat (abs a)"
   1.775 +  let ?nb = "nat (abs b)"
   1.776 +  have nap: "?na >0" using anz by simp
   1.777 +  have nbp: "?nb >0" using bnz by simp
   1.778 +  have "0 < lcm ?na ?nb" by (rule lcm_pos[OF nap nbp])
   1.779 +  thus ?thesis by (simp add: zlcm_def)
   1.780 +qed
   1.781 +
   1.782 +lemma zgcd_code [code]:
   1.783 +  "zgcd k l = \<bar>if l = 0 then k else zgcd l (\<bar>k\<bar> mod \<bar>l\<bar>)\<bar>"
   1.784 +  by (simp add: zgcd_def gcd.simps [of "nat \<bar>k\<bar>"] nat_mod_distrib)
   1.785 +
   1.786 +end