src/HOL/Number_Theory/Euclidean_Algorithm.thy
 changeset 60687 33dbbcb6a8a3 parent 60686 ea5bc46c11e6 child 60688 01488b559910
```     1.1 --- a/src/HOL/Number_Theory/Euclidean_Algorithm.thy	Wed Jul 08 14:01:39 2015 +0200
1.2 +++ b/src/HOL/Number_Theory/Euclidean_Algorithm.thy	Wed Jul 08 14:01:40 2015 +0200
1.3 @@ -753,84 +753,19 @@
1.4    "lcm a b = normalize (a * b) div gcd a b"
1.5    by (simp add: lcm_lcm_eucl gcd_gcd_eucl lcm_eucl_def)
1.6
1.7 +subclass semiring_gcd
1.8 +  apply standard
1.9 +  using gcd_right_idem
1.10 +  apply (simp_all add: lcm_gcd gcd_greatest_iff gcd_proj1_if_dvd)
1.11 +  done
1.12 +
1.13  lemma lcm_gcd_prod:
1.14    "lcm a b * gcd a b = normalize (a * b)"
1.16
1.17 -lemma lcm_dvd1 [iff]:
1.18 -  "a dvd lcm a b"
1.19 -proof (cases "a*b = 0")
1.20 -  assume "a * b \<noteq> 0"
1.21 -  hence "gcd a b \<noteq> 0" by simp
1.22 -  let ?c = "1 div unit_factor (a * b)"
1.23 -  from \<open>a * b \<noteq> 0\<close> have [simp]: "is_unit (unit_factor (a * b))" by simp
1.24 -  from lcm_gcd_prod[of a b] have "lcm a b * gcd a b = a * ?c * b"
1.25 -    by (simp add: div_mult_swap unit_div_commute)
1.26 -  hence "lcm a b * gcd a b div gcd a b = a * ?c * b div gcd a b" by simp
1.27 -  with \<open>gcd a b \<noteq> 0\<close> have "lcm a b = a * ?c * b div gcd a b"
1.28 -    by (subst (asm) div_mult_self2_is_id, simp_all)
1.29 -  also have "... = a * (?c * b div gcd a b)"
1.30 -    by (metis div_mult_swap gcd_dvd2 mult_assoc)
1.31 -  finally show ?thesis by (rule dvdI)
1.32 -qed (auto simp add: lcm_gcd)
1.33 -
1.34 -lemma lcm_least:
1.35 -  "\<lbrakk>a dvd k; b dvd k\<rbrakk> \<Longrightarrow> lcm a b dvd k"
1.36 -proof (cases "k = 0")
1.37 -  let ?nf = unit_factor
1.38 -  assume "k \<noteq> 0"
1.39 -  hence "is_unit (?nf k)" by simp
1.40 -  hence "?nf k \<noteq> 0" by (metis not_is_unit_0)
1.41 -  assume A: "a dvd k" "b dvd k"
1.42 -  hence "gcd a b \<noteq> 0" using \<open>k \<noteq> 0\<close> by auto
1.43 -  from A obtain r s where ar: "k = a * r" and bs: "k = b * s"
1.44 -    unfolding dvd_def by blast
1.45 -  with \<open>k \<noteq> 0\<close> have "r * s \<noteq> 0"
1.46 -    by auto (drule sym [of 0], simp)
1.47 -  hence "is_unit (?nf (r * s))" by simp
1.48 -  let ?c = "?nf k div ?nf (r*s)"
1.49 -  from \<open>is_unit (?nf k)\<close> and \<open>is_unit (?nf (r * s))\<close> have "is_unit ?c" by (rule unit_div)
1.50 -  hence "?c \<noteq> 0" using not_is_unit_0 by fast
1.51 -  from ar bs have "k * k * gcd s r = ?nf k * k * gcd (k * s) (k * r)"
1.52 -    by (subst mult_assoc, subst gcd_mult_distrib[of k s r], simp only: ac_simps)
1.53 -  also have "... = ?nf k * k * gcd ((r*s) * a) ((r*s) * b)"
1.54 -    by (subst (3) \<open>k = a * r\<close>, subst (3) \<open>k = b * s\<close>, simp add: algebra_simps)
1.55 -  also have "... = ?c * r*s * k * gcd a b" using \<open>r * s \<noteq> 0\<close>
1.56 -    by (subst gcd_mult_distrib'[symmetric], simp add: algebra_simps unit_simps)
1.57 -  finally have "(a*r) * (b*s) * gcd s r = ?c * k * r * s * gcd a b"
1.58 -    by (subst ar[symmetric], subst bs[symmetric], simp add: mult_ac)
1.59 -  hence "a * b * gcd s r * (r * s) = ?c * k * gcd a b * (r * s)"
1.60 -    by (simp add: algebra_simps)
1.61 -  hence "?c * k * gcd a b = a * b * gcd s r" using \<open>r * s \<noteq> 0\<close>
1.62 -    by (metis div_mult_self2_is_id)
1.63 -  also have "... = lcm a b * gcd a b * gcd s r * ?nf (a*b)"
1.64 -    by (subst lcm_gcd_prod[of a b], metis gcd_mult_distrib gcd_mult_distrib')
1.65 -  also have "... = lcm a b * gcd s r * ?nf (a*b) * gcd a b"
1.66 -    by (simp add: algebra_simps)
1.67 -  finally have "k * ?c = lcm a b * gcd s r * ?nf (a*b)" using \<open>gcd a b \<noteq> 0\<close>
1.68 -    by (metis mult.commute div_mult_self2_is_id)
1.69 -  hence "k = lcm a b * (gcd s r * ?nf (a*b)) div ?c" using \<open>?c \<noteq> 0\<close>
1.70 -    by (metis div_mult_self2_is_id mult_assoc)
1.71 -  also have "... = lcm a b * (gcd s r * ?nf (a*b) div ?c)" using \<open>is_unit ?c\<close>
1.72 -    by (simp add: unit_simps)
1.73 -  finally show ?thesis by (rule dvdI)
1.74 -qed simp
1.75 -
1.76  lemma lcm_zero:
1.77    "lcm a b = 0 \<longleftrightarrow> a = 0 \<or> b = 0"
1.78 -proof -
1.79 -  let ?nf = unit_factor
1.80 -  {
1.81 -    assume "a \<noteq> 0" "b \<noteq> 0"
1.82 -    hence "a * b div ?nf (a * b) \<noteq> 0" by (simp add: no_zero_divisors)
1.83 -    moreover from \<open>a \<noteq> 0\<close> and \<open>b \<noteq> 0\<close> have "gcd a b \<noteq> 0" by simp
1.84 -    ultimately have "lcm a b \<noteq> 0" using lcm_gcd_prod[of a b] by (intro notI, simp)
1.85 -  } moreover {
1.86 -    assume "a = 0 \<or> b = 0"
1.87 -    hence "lcm a b = 0" by (elim disjE, simp_all add: lcm_gcd)
1.88 -  }
1.89 -  ultimately show ?thesis by blast
1.90 -qed
1.91 +  by (fact lcm_eq_0_iff)
1.92
1.93  lemmas lcm_0_iff = lcm_zero
1.94
1.95 @@ -844,46 +779,15 @@
1.96      by (metis nonzero_mult_divide_cancel_left)
1.97  qed
1.98
1.99 -lemma unit_factor_lcm [simp]:
1.100 -  "unit_factor (lcm a b) = (if a = 0 \<or> b = 0 then 0 else 1)"
1.101 -  by (simp add: dvd_unit_factor_div lcm_gcd)
1.102 -
1.103 -lemma lcm_dvd2 [iff]: "b dvd lcm a b"
1.104 -  using lcm_dvd1 [of b a] by (simp add: lcm_gcd ac_simps)
1.105 +declare unit_factor_lcm [simp]
1.106
1.107  lemma lcmI:
1.108    assumes "a dvd c" and "b dvd c" and "\<And>d. a dvd d \<Longrightarrow> b dvd d \<Longrightarrow> c dvd d"
1.109      and "unit_factor c = (if c = 0 then 0 else 1)"
1.110    shows "c = lcm a b"
1.111 -  by (rule associated_eqI)
1.112 -    (auto simp: assms associated_def intro: lcm_least, simp_all add: lcm_gcd)
1.113 +  by (rule associated_eqI) (auto simp: assms intro: associatedI lcm_least)
1.114
1.115 -sublocale lcm!: abel_semigroup lcm
1.116 -proof
1.117 -  fix a b c
1.118 -  show "lcm (lcm a b) c = lcm a (lcm b c)"
1.119 -  proof (rule lcmI)
1.120 -    have "a dvd lcm a b" and "lcm a b dvd lcm (lcm a b) c" by simp_all
1.121 -    then show "a dvd lcm (lcm a b) c" by (rule dvd_trans)
1.122 -
1.123 -    have "b dvd lcm a b" and "lcm a b dvd lcm (lcm a b) c" by simp_all
1.124 -    hence "b dvd lcm (lcm a b) c" by (rule dvd_trans)
1.125 -    moreover have "c dvd lcm (lcm a b) c" by simp
1.126 -    ultimately show "lcm b c dvd lcm (lcm a b) c" by (rule lcm_least)
1.127 -
1.128 -    fix l assume "a dvd l" and "lcm b c dvd l"
1.129 -    have "b dvd lcm b c" by simp
1.130 -    from this and \<open>lcm b c dvd l\<close> have "b dvd l" by (rule dvd_trans)
1.131 -    have "c dvd lcm b c" by simp
1.132 -    from this and \<open>lcm b c dvd l\<close> have "c dvd l" by (rule dvd_trans)
1.133 -    from \<open>a dvd l\<close> and \<open>b dvd l\<close> have "lcm a b dvd l" by (rule lcm_least)
1.134 -    from this and \<open>c dvd l\<close> show "lcm (lcm a b) c dvd l" by (rule lcm_least)
1.135 -  qed (simp add: lcm_zero)
1.136 -next
1.137 -  fix a b
1.138 -  show "lcm a b = lcm b a"
1.139 -    by (simp add: lcm_gcd ac_simps)
1.140 -qed
1.141 +sublocale lcm!: abel_semigroup lcm ..
1.142
1.143  lemma dvd_lcm_D1:
1.144    "lcm m n dvd k \<Longrightarrow> m dvd k"
1.145 @@ -913,13 +817,9 @@
1.146    finally show "lcm a b = 1" .
1.147  qed
1.148
1.149 -lemma lcm_0_left [simp]:
1.150 -  "lcm 0 a = 0"
1.151 -  by (rule sym, rule lcmI, simp_all)
1.152 -
1.153 -lemma lcm_0 [simp]:
1.154 +lemma lcm_0:
1.155    "lcm a 0 = 0"
1.156 -  by (rule sym, rule lcmI, simp_all)
1.157 +  by (fact lcm_0_right)
1.158
1.159  lemma lcm_unique:
1.160    "a dvd d \<and> b dvd d \<and>
1.161 @@ -935,14 +835,6 @@
1.162    "k dvd n \<Longrightarrow> k dvd lcm m n"
1.163    by (metis lcm_dvd2 dvd_trans)
1.164
1.165 -lemma lcm_1_left [simp]:
1.166 -  "lcm 1 a = normalize a"
1.167 -  by (cases "a = 0") (simp, rule sym, rule lcmI, simp_all)
1.168 -
1.169 -lemma lcm_1_right [simp]:
1.170 -  "lcm a 1 = normalize a"
1.171 -  using lcm_1_left [of a] by (simp add: ac_simps)
1.172 -
1.173  lemma lcm_coprime:
1.174    "gcd a b = 1 \<Longrightarrow> lcm a b = normalize (a * b)"
1.175    by (subst lcm_gcd) simp
1.176 @@ -1119,7 +1011,8 @@
1.177        qed
1.178        ultimately have "euclidean_size l = euclidean_size (gcd l l')"
1.179          by (intro le_antisym, simp_all add: \<open>euclidean_size l = n\<close>)
1.180 -      with \<open>l \<noteq> 0\<close> have "l dvd gcd l l'" by (blast intro: dvd_euclidean_size_eq_imp_dvd)
1.181 +      with \<open>l \<noteq> 0\<close> have "l dvd gcd l l'"
1.182 +        using dvd_euclidean_size_eq_imp_dvd by auto
1.183        hence "l dvd l'" by (blast dest: dvd_gcd_D2)
1.184      }
1.185
1.186 @@ -1246,7 +1139,7 @@
1.187    "Lcm (insert a A) = lcm a (Lcm A)"
1.188  proof (rule lcmI)
1.189    fix l assume "a dvd l" and "Lcm A dvd l"
1.190 -  hence "\<forall>a\<in>A. a dvd l" by (blast intro: dvd_trans dvd_Lcm)
1.191 +  then have "\<forall>a\<in>A. a dvd l" by (auto intro: dvd_trans [of _ "Lcm A" l])
1.192    with \<open>a dvd l\<close> show "Lcm (insert a A) dvd l" by (force intro: Lcm_least)
1.193  qed (auto intro: Lcm_least dvd_Lcm)
1.194
1.195 @@ -1313,6 +1206,9 @@
1.196    "normalize (Gcd A) = Gcd A"
1.197    by (cases "Gcd A = 0") (auto intro: associated_eqI)
1.198
1.199 +subclass semiring_Gcd
1.200 +  by standard (simp_all add: Gcd_greatest)
1.201 +
1.202  lemma GcdI:
1.203    assumes "\<And>a. a \<in> A \<Longrightarrow> b dvd a" and "\<And>c. (\<And>a. a \<in> A \<Longrightarrow> c dvd a) \<Longrightarrow> c dvd b"
1.204      and "unit_factor b = (if b = 0 then 0 else 1)"
1.205 @@ -1323,28 +1219,12 @@
1.206    "Lcm A = Gcd {m. \<forall>a\<in>A. a dvd m}"
1.207    by (rule LcmI[symmetric]) (auto intro: dvd_Gcd Gcd_greatest)
1.208
1.209 -lemma Gcd_0_iff:
1.210 -  "Gcd A = 0 \<longleftrightarrow> A \<subseteq> {0}"
1.211 -  apply (rule iffI)
1.212 -  apply (rule subsetI, drule Gcd_dvd, simp)
1.213 -  apply (auto intro: GcdI[symmetric])
1.214 -  done
1.215 -
1.216 -lemma Gcd_empty [simp]:
1.217 -  "Gcd {} = 0"
1.218 -  by (simp add: Gcd_0_iff)
1.219 +subclass semiring_Lcm
1.220 +  by standard (simp add: Lcm_Gcd)
1.221
1.222  lemma Gcd_1:
1.223    "1 \<in> A \<Longrightarrow> Gcd A = 1"
1.224 -  by (intro GcdI[symmetric]) (auto intro: Gcd_dvd dvd_Gcd)
1.225 -
1.226 -lemma Gcd_insert [simp]:
1.227 -  "Gcd (insert a A) = gcd a (Gcd A)"
1.228 -proof (rule gcdI)
1.229 -  fix l assume "l dvd a" and "l dvd Gcd A"
1.230 -  hence "\<forall>a\<in>A. l dvd a" by (blast intro: dvd_trans Gcd_dvd)
1.231 -  with \<open>l dvd a\<close> show "l dvd Gcd (insert a A)" by (force intro: Gcd_dvd Gcd_greatest)
1.232 -qed (auto intro: Gcd_greatest)
1.233 +  by (auto intro!: Gcd_eq_1_I)
1.234
1.235  lemma Gcd_finite:
1.236    assumes "finite A"
1.237 @@ -1357,22 +1237,10 @@
1.238    using comp_fun_idem.fold_set_fold[OF comp_fun_idem_gcd] Gcd_finite by (simp add: ac_simps)
1.239
1.240  lemma Gcd_singleton [simp]: "Gcd {a} = normalize a"
1.241 -  by (simp add: gcd_0)
1.242 +  by simp
1.243
1.244  lemma Gcd_2 [simp]: "Gcd {a,b} = gcd a b"
1.245 -  by (simp add: gcd_0)
1.246 -
1.247 -subclass semiring_gcd
1.248 -  apply standard
1.249 -  using gcd_right_idem
1.250 -  apply (simp_all add: lcm_gcd gcd_greatest_iff gcd_proj1_if_dvd)
1.251 -  done
1.252 -
1.253 -subclass semiring_Gcd
1.254 -  by standard (simp_all add: Gcd_greatest)
1.255 -
1.256 -subclass semiring_Lcm
1.257 -  by standard (simp add: Lcm_Gcd)
1.258 +  by simp
1.259
1.260  end
1.261
1.262 @@ -1515,4 +1383,19 @@
1.263
1.264  end
1.265
1.266 +(*instance nat :: euclidean_semiring_gcd
1.267 +proof (standard, auto intro!: ext)
1.268 +  fix m n :: nat
1.269 +  show *: "gcd m n = gcd_eucl m n"
1.270 +  proof (induct m n rule: gcd_eucl_induct)
1.271 +    case zero then show ?case by (simp add: gcd_eucl_0)
1.272 +  next
1.273 +    case (mod m n)
1.274 +    with gcd_eucl_non_0 [of n m, symmetric]
1.275 +    show ?case by (simp add: gcd_non_0_nat)
1.276 +  qed
1.277 +  show "lcm m n = lcm_eucl m n"
1.278 +    by (simp add: lcm_eucl_def lcm_gcd * [symmetric] lcm_nat_def)
1.279 +qed*)
1.280 +
1.281  end
```