src/HOL/GCD.thy
changeset 30240 5b25fee0362c
parent 29700 22faf21db3df
child 30242 aea5d7fa7ef5
equal deleted inserted replaced
30239:179ff9cb160b 30240:5b25fee0362c
    58   by simp
    58   by simp
    59 
    59 
    60 lemma gcd_non_0: "n > 0 \<Longrightarrow> gcd m n = gcd n (m mod n)"
    60 lemma gcd_non_0: "n > 0 \<Longrightarrow> gcd m n = gcd n (m mod n)"
    61   by simp
    61   by simp
    62 
    62 
    63 lemma gcd_1 [simp, algebra]: "gcd m (Suc 0) = 1"
    63 lemma gcd_1 [simp, algebra]: "gcd m (Suc 0) = Suc 0"
    64   by simp
    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)
    65 
    68 
    66 declare gcd.simps [simp del]
    69 declare gcd.simps [simp del]
    67 
    70 
    68 text {*
    71 text {*
    69   \medskip @{term "gcd m n"} divides @{text m} and @{text n}.  The
    72   \medskip @{term "gcd m n"} divides @{text m} and @{text n}.  The
   114    apply (rule is_gcd)
   117    apply (rule is_gcd)
   115   apply (simp add: is_gcd_def)
   118   apply (simp add: is_gcd_def)
   116   apply (blast intro: dvd_trans)
   119   apply (blast intro: dvd_trans)
   117   done
   120   done
   118 
   121 
   119 lemma gcd_1_left [simp, algebra]: "gcd (Suc 0) m = 1"
   122 lemma gcd_1_left [simp, algebra]: "gcd (Suc 0) m = Suc 0"
   120   by (simp add: gcd_commute)
   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)
   121 
   127 
   122 text {*
   128 text {*
   123   \medskip Multiplication laws
   129   \medskip Multiplication laws
   124 *}
   130 *}
   125 
   131 
   154    apply (rule gcd_greatest)
   160    apply (rule gcd_greatest)
   155     apply (rule_tac n = k in relprime_dvd_mult)
   161     apply (rule_tac n = k in relprime_dvd_mult)
   156      apply (simp add: gcd_assoc)
   162      apply (simp add: gcd_assoc)
   157      apply (simp add: gcd_commute)
   163      apply (simp add: gcd_commute)
   158     apply (simp_all add: mult_commute)
   164     apply (simp_all add: mult_commute)
   159   apply (blast intro: dvd_mult)
       
   160   done
   165   done
   161 
   166 
   162 
   167 
   163 text {* \medskip Addition laws *}
   168 text {* \medskip Addition laws *}
   164 
   169 
   402     hence ?lhs by blast}
   407     hence ?lhs by blast}
   403   moreover
   408   moreover
   404   {fix x y assume H: "a * x - b * y = d \<or> b * x - a * y = d"
   409   {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"
   410     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
   411       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
   412     from nat_dvd_diff[OF dv(1,2)] nat_dvd_diff[OF dv(3,4)] H
   408     have ?rhs by auto}
   413     have ?rhs by auto}
   409   ultimately show ?thesis by blast
   414   ultimately show ?thesis by blast
   410 qed
   415 qed
   411 
   416 
   412 lemma gcd_bezout_sum: assumes H:"a * x + b * y = d" shows "gcd a b dvd d"
   417 lemma gcd_bezout_sum: assumes H:"a * x + b * y = d" shows "gcd a b dvd d"
   595   from relprime_dvd_mult [OF g th] obtain h' where h': "nat \<bar>k\<bar> = nat \<bar>i\<bar> * h'"
   600   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
   601     unfolding dvd_def by blast
   597   from h' have "int (nat \<bar>k\<bar>) = int (nat \<bar>i\<bar> * h')" by simp
   602   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)
   603   then have "\<bar>k\<bar> = \<bar>i\<bar> * int h'" by (simp add: int_mult)
   599   then show ?thesis
   604   then show ?thesis
   600     apply (subst zdvd_abs1 [symmetric])
   605     apply (subst abs_dvd_iff [symmetric])
   601     apply (subst zdvd_abs2 [symmetric])
   606     apply (subst dvd_abs_iff [symmetric])
   602     apply (unfold dvd_def)
   607     apply (unfold dvd_def)
   603     apply (rule_tac x = "int h'" in exI, simp)
   608     apply (rule_tac x = "int h'" in exI, simp)
   604     done
   609     done
   605 qed
   610 qed
   606 
   611 
   612 proof -
   617 proof -
   613   let ?k' = "nat \<bar>k\<bar>"
   618   let ?k' = "nat \<bar>k\<bar>"
   614   let ?m' = "nat \<bar>m\<bar>"
   619   let ?m' = "nat \<bar>m\<bar>"
   615   let ?n' = "nat \<bar>n\<bar>"
   620   let ?n' = "nat \<bar>n\<bar>"
   616   from `k dvd m` and `k dvd n` have dvd': "?k' dvd ?m'" "?k' dvd ?n'"
   621   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)
   622     unfolding zdvd_int by (simp_all only: int_nat_abs abs_dvd_iff dvd_abs_iff)
   618   from gcd_greatest [OF dvd'] have "int (nat \<bar>k\<bar>) dvd zgcd m n"
   623   from gcd_greatest [OF dvd'] have "int (nat \<bar>k\<bar>) dvd zgcd m n"
   619     unfolding zgcd_def by (simp only: zdvd_int)
   624     unfolding zgcd_def by (simp only: zdvd_int)
   620   then have "\<bar>k\<bar> dvd zgcd m n" by (simp only: int_nat_abs)
   625   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)
   626   then show "k dvd zgcd m n" by simp
   622 qed
   627 qed
   623 
   628 
   624 lemma div_zgcd_relprime:
   629 lemma div_zgcd_relprime:
   625   assumes nz: "a \<noteq> 0 \<or> b \<noteq> 0"
   630   assumes nz: "a \<noteq> 0 \<or> b \<noteq> 0"
   626   shows "zgcd (a div (zgcd a b)) (b div (zgcd a b)) = 1"
   631   shows "zgcd (a div (zgcd a b)) (b div (zgcd a b)) = 1"
   719 
   724 
   720 lemma dvd_imp_dvd_zlcm1:
   725 lemma dvd_imp_dvd_zlcm1:
   721   assumes "k dvd i" shows "k dvd (zlcm i j)"
   726   assumes "k dvd i" shows "k dvd (zlcm i j)"
   722 proof -
   727 proof -
   723   have "nat(abs k) dvd nat(abs i)" using `k dvd i`
   728   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)
   729     by(simp add:int_dvd_iff[symmetric] dvd_int_iff[symmetric])
   725   thus ?thesis by(simp add:zlcm_def dvd_int_iff)(blast intro: dvd_trans)
   730   thus ?thesis by(simp add:zlcm_def dvd_int_iff)(blast intro: dvd_trans)
   726 qed
   731 qed
   727 
   732 
   728 lemma dvd_imp_dvd_zlcm2:
   733 lemma dvd_imp_dvd_zlcm2:
   729   assumes "k dvd j" shows "k dvd (zlcm i j)"
   734   assumes "k dvd j" shows "k dvd (zlcm i j)"
   730 proof -
   735 proof -
   731   have "nat(abs k) dvd nat(abs j)" using `k dvd j`
   736   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)
   737     by(simp add:int_dvd_iff[symmetric] dvd_int_iff[symmetric])
   733   thus ?thesis by(simp add:zlcm_def dvd_int_iff)(blast intro: dvd_trans)
   738   thus ?thesis by(simp add:zlcm_def dvd_int_iff)(blast intro: dvd_trans)
   734 qed
   739 qed
   735 
   740 
   736 
   741 
   737 lemma zdvd_self_abs1: "(d::int) dvd (abs d)"
   742 lemma zdvd_self_abs1: "(d::int) dvd (abs d)"