src/HOL/Number_Theory/Factorial_Ring.thy
changeset 60804 080a979a985b
child 62348 9a5f43dac883
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOL/Number_Theory/Factorial_Ring.thy	Mon Jul 27 22:44:02 2015 +0200
     1.3 @@ -0,0 +1,370 @@
     1.4 +(*  Title:      HOL/Number_Theory/Factorial_Ring.thy
     1.5 +    Author:     Florian Haftmann, TU Muenchen
     1.6 +*)
     1.7 +
     1.8 +section \<open>Factorial (semi)rings\<close>
     1.9 +
    1.10 +theory Factorial_Ring
    1.11 +imports Main Primes "~~/src/HOL/Library/Multiset" (*"~~/src/HOL/Library/Polynomial"*)
    1.12 +begin
    1.13 +
    1.14 +context algebraic_semidom
    1.15 +begin
    1.16 +
    1.17 +lemma is_unit_mult_iff:
    1.18 +  "is_unit (a * b) \<longleftrightarrow> is_unit a \<and> is_unit b" (is "?P \<longleftrightarrow> ?Q")
    1.19 +  by (auto dest: dvd_mult_left dvd_mult_right)
    1.20 +
    1.21 +lemma is_unit_power_iff:
    1.22 +  "is_unit (a ^ n) \<longleftrightarrow> is_unit a \<or> n = 0"
    1.23 +  by (induct n) (auto simp add: is_unit_mult_iff)
    1.24 +  
    1.25 +lemma subset_divisors_dvd:
    1.26 +  "{c. c dvd a} \<subseteq> {c. c dvd b} \<longleftrightarrow> a dvd b"
    1.27 +  by (auto simp add: subset_iff intro: dvd_trans)
    1.28 +
    1.29 +lemma strict_subset_divisors_dvd:
    1.30 +  "{c. c dvd a} \<subset> {c. c dvd b} \<longleftrightarrow> a dvd b \<and> \<not> b dvd a"
    1.31 +  by (auto simp add: subset_iff intro: dvd_trans)
    1.32 +
    1.33 +end
    1.34 +
    1.35 +class factorial_semiring = normalization_semidom +
    1.36 +  assumes finite_divisors: "a \<noteq> 0 \<Longrightarrow> finite {b. b dvd a \<and> normalize b = b}"
    1.37 +  fixes is_prime :: "'a \<Rightarrow> bool"
    1.38 +  assumes not_is_prime_zero [simp]: "\<not> is_prime 0"
    1.39 +    and is_prime_not_unit: "is_prime p \<Longrightarrow> \<not> is_unit p"
    1.40 +    and no_prime_divisorsI: "(\<And>b. b dvd a \<Longrightarrow> \<not> is_prime b) \<Longrightarrow> is_unit a"
    1.41 +  assumes is_primeI: "p \<noteq> 0 \<Longrightarrow> \<not> is_unit p \<Longrightarrow> (\<And>a. a dvd p \<Longrightarrow> \<not> is_unit a \<Longrightarrow> p dvd a) \<Longrightarrow> is_prime p"
    1.42 +    and is_primeD: "is_prime p \<Longrightarrow> p dvd a * b \<Longrightarrow> p dvd a \<or> p dvd b"
    1.43 +begin
    1.44 +
    1.45 +lemma not_is_prime_one [simp]:
    1.46 +  "\<not> is_prime 1"
    1.47 +  by (auto dest: is_prime_not_unit)
    1.48 +
    1.49 +lemma is_prime_not_zeroI:
    1.50 +  assumes "is_prime p"
    1.51 +  shows "p \<noteq> 0"
    1.52 +  using assms by (auto intro: ccontr)
    1.53 +
    1.54 +lemma is_prime_multD:
    1.55 +  assumes "is_prime (a * b)"
    1.56 +  shows "is_unit a \<or> is_unit b"
    1.57 +proof -
    1.58 +  from assms have "a \<noteq> 0" "b \<noteq> 0" by auto
    1.59 +  moreover from assms is_primeD [of "a * b"] have "a * b dvd a \<or> a * b dvd b"
    1.60 +    by auto
    1.61 +  ultimately show ?thesis
    1.62 +    using dvd_times_left_cancel_iff [of a b 1]
    1.63 +      dvd_times_right_cancel_iff [of b a 1]
    1.64 +    by auto
    1.65 +qed
    1.66 +
    1.67 +lemma is_primeD2:
    1.68 +  assumes "is_prime p" and "a dvd p" and "\<not> is_unit a"
    1.69 +  shows "p dvd a"
    1.70 +proof -
    1.71 +  from \<open>a dvd p\<close> obtain b where "p = a * b" ..
    1.72 +  with \<open>is_prime p\<close> is_prime_multD \<open>\<not> is_unit a\<close> have "is_unit b" by auto
    1.73 +  with \<open>p = a * b\<close> show ?thesis
    1.74 +    by (auto simp add: mult_unit_dvd_iff)
    1.75 +qed
    1.76 +
    1.77 +lemma is_prime_mult_unit_left:
    1.78 +  assumes "is_prime p"
    1.79 +    and "is_unit a"
    1.80 +  shows "is_prime (a * p)"
    1.81 +proof (rule is_primeI)
    1.82 +  from assms show "a * p \<noteq> 0" "\<not> is_unit (a * p)"
    1.83 +    by (auto simp add: is_unit_mult_iff is_prime_not_unit)
    1.84 +  show "a * p dvd b" if "b dvd a * p" "\<not> is_unit b" for b
    1.85 +  proof -
    1.86 +    from that \<open>is_unit a\<close> have "b dvd p"
    1.87 +      using dvd_mult_unit_iff [of a b p] by (simp add: ac_simps)
    1.88 +    with \<open>is_prime p\<close> \<open>\<not> is_unit b\<close> have "p dvd b" 
    1.89 +      using is_primeD2 [of p b] by auto
    1.90 +    with \<open>is_unit a\<close> show ?thesis
    1.91 +      using mult_unit_dvd_iff [of a p b] by (simp add: ac_simps)
    1.92 +  qed
    1.93 +qed
    1.94 +
    1.95 +lemma is_primeI2:
    1.96 +  assumes "p \<noteq> 0"
    1.97 +  assumes "\<not> is_unit p"
    1.98 +  assumes P: "\<And>a b. p dvd a * b \<Longrightarrow> p dvd a \<or> p dvd b"
    1.99 +  shows "is_prime p"
   1.100 +using \<open>p \<noteq> 0\<close> \<open>\<not> is_unit p\<close> proof (rule is_primeI)
   1.101 +  fix a
   1.102 +  assume "a dvd p"
   1.103 +  then obtain b where "p = a * b" ..
   1.104 +  with \<open>p \<noteq> 0\<close> have "b \<noteq> 0" by simp
   1.105 +  moreover from \<open>p = a * b\<close> P have "p dvd a \<or> p dvd b" by auto
   1.106 +  moreover assume "\<not> is_unit a"
   1.107 +  ultimately show "p dvd a"
   1.108 +    using dvd_times_right_cancel_iff [of b a 1] \<open>p = a * b\<close> by auto
   1.109 +qed    
   1.110 +
   1.111 +lemma not_is_prime_divisorE:
   1.112 +  assumes "a \<noteq> 0" and "\<not> is_unit a" and "\<not> is_prime a"
   1.113 +  obtains b where "b dvd a" and "\<not> is_unit b" and "\<not> a dvd b"
   1.114 +proof -
   1.115 +  from assms have "\<exists>b. b dvd a \<and> \<not> is_unit b \<and> \<not> a dvd b"
   1.116 +    by (auto intro: is_primeI)
   1.117 +  with that show thesis by blast
   1.118 +qed
   1.119 +
   1.120 +lemma prime_divisorE:
   1.121 +  assumes "a \<noteq> 0" and "\<not> is_unit a" 
   1.122 +  obtains p where "is_prime p" and "p dvd a"
   1.123 +  using assms no_prime_divisorsI [of a] by blast
   1.124 +
   1.125 +lemma prime_dvd_mult_iff:  
   1.126 +  assumes "is_prime p"
   1.127 +  shows "p dvd a * b \<longleftrightarrow> p dvd a \<or> p dvd b"
   1.128 +  using assms by (auto dest: is_primeD)
   1.129 +
   1.130 +lemma prime_dvd_power_iff:
   1.131 +  assumes "is_prime p"
   1.132 +  shows "p dvd a ^ n \<longleftrightarrow> p dvd a \<and> n > 0"
   1.133 +  using assms by (induct n) (auto dest: is_prime_not_unit is_primeD)
   1.134 +
   1.135 +lemma is_prime_normalize_iff [simp]:
   1.136 +  "is_prime (normalize p) \<longleftrightarrow> is_prime p" (is "?P \<longleftrightarrow> ?Q")
   1.137 +proof
   1.138 +  assume ?Q show ?P
   1.139 +    by (rule is_primeI) (insert \<open>?Q\<close>, simp_all add: is_prime_not_zeroI is_prime_not_unit is_primeD2)
   1.140 +next
   1.141 +  assume ?P show ?Q
   1.142 +    by (rule is_primeI)
   1.143 +      (insert is_prime_not_zeroI [of "normalize p"] is_prime_not_unit [of "normalize p"] is_primeD2 [of "normalize p"] \<open>?P\<close>, simp_all)
   1.144 +qed  
   1.145 +
   1.146 +lemma is_prime_inj_power:
   1.147 +  assumes "is_prime p"
   1.148 +  shows "inj (op ^ p)"
   1.149 +proof (rule injI, rule ccontr)
   1.150 +  fix m n :: nat
   1.151 +  have [simp]: "p ^ q = 1 \<longleftrightarrow> q = 0" (is "?P \<longleftrightarrow> ?Q") for q
   1.152 +  proof
   1.153 +    assume ?Q then show ?P by simp
   1.154 +  next
   1.155 +    assume ?P then have "is_unit (p ^ q)" by simp
   1.156 +    with assms show ?Q by (auto simp add: is_unit_power_iff is_prime_not_unit)
   1.157 +  qed
   1.158 +  have *: False if "p ^ m = p ^ n" and "m > n" for m n
   1.159 +  proof -
   1.160 +    from assms have "p \<noteq> 0"
   1.161 +      by (rule is_prime_not_zeroI)
   1.162 +    then have "p ^ n \<noteq> 0"
   1.163 +      by (induct n) simp_all
   1.164 +    from that have "m = n + (m - n)" and "m - n > 0" by arith+
   1.165 +    then obtain q where "m = n + q" and "q > 0" ..
   1.166 +    with that have "p ^ n * p ^ q = p ^ n * 1" by (simp add: power_add)
   1.167 +    with \<open>p ^ n \<noteq> 0\<close> have "p ^ q = 1"
   1.168 +      using mult_left_cancel [of "p ^ n" "p ^ q" 1] by simp
   1.169 +    with \<open>q > 0\<close> show ?thesis by simp
   1.170 +  qed 
   1.171 +  assume "m \<noteq> n"
   1.172 +  then have "m > n \<or> m < n" by arith
   1.173 +  moreover assume "p ^ m = p ^ n"
   1.174 +  ultimately show False using * [of m n] * [of n m] by auto
   1.175 +qed
   1.176 +
   1.177 +lemma prime_unique:
   1.178 +  assumes "is_prime q" and "is_prime p" and "q dvd p"
   1.179 +  shows "normalize q = normalize p"
   1.180 +proof -
   1.181 +  from assms have "p dvd q"
   1.182 +    by (auto dest: is_primeD2 [of p] is_prime_not_unit [of q])
   1.183 +  with assms show ?thesis
   1.184 +    by (auto intro: associatedI)
   1.185 +qed  
   1.186 +
   1.187 +lemma exists_factorization:
   1.188 +  assumes "a \<noteq> 0"
   1.189 +  obtains P where "\<And>p. p \<in># P \<Longrightarrow> is_prime p" "msetprod P = normalize a"
   1.190 +proof -
   1.191 +  let ?prime_factors = "\<lambda>a. {p. p dvd a \<and> is_prime p \<and> normalize p = p}"
   1.192 +  have "?prime_factors a \<subseteq> {b. b dvd a \<and> normalize b = b}" by auto
   1.193 +  moreover from assms have "finite {b. b dvd a \<and> normalize b = b}"
   1.194 +    by (rule finite_divisors)
   1.195 +  ultimately have "finite (?prime_factors a)" by (rule finite_subset)
   1.196 +  then show thesis using \<open>a \<noteq> 0\<close> that proof (induct "?prime_factors a" arbitrary: thesis a)
   1.197 +    case empty then have
   1.198 +      P: "\<And>b. is_prime b \<Longrightarrow> b dvd a \<Longrightarrow> normalize b \<noteq> b" and "a \<noteq> 0"
   1.199 +      by auto
   1.200 +    have "is_unit a"
   1.201 +    proof (rule no_prime_divisorsI)
   1.202 +      fix b
   1.203 +      assume "b dvd a"
   1.204 +      then show "\<not> is_prime b"
   1.205 +        using P [of "normalize b"] by auto
   1.206 +    qed
   1.207 +    then have "\<And>p. p \<in># {#} \<Longrightarrow> is_prime p" and "msetprod {#} = normalize a"
   1.208 +      by (simp_all add: is_unit_normalize)
   1.209 +    with empty show thesis by blast
   1.210 +  next
   1.211 +    case (insert p P)
   1.212 +    from \<open>insert p P = ?prime_factors a\<close>
   1.213 +    have "p dvd a" and "is_prime p" and "normalize p = p"
   1.214 +      by auto
   1.215 +    obtain n where "n > 0" and "p ^ n dvd a" and "\<not> p ^ Suc n dvd a" 
   1.216 +    proof (rule that)
   1.217 +      def N \<equiv> "{n. p ^ n dvd a}"
   1.218 +      from is_prime_inj_power \<open>is_prime p\<close> have "inj (op ^ p)" .
   1.219 +      then have "inj_on (op ^ p) U" for U
   1.220 +        by (rule subset_inj_on) simp
   1.221 +      moreover have "op ^ p ` N \<subseteq> {b. b dvd a \<and> normalize b = b}"
   1.222 +        by (auto simp add: normalize_power \<open>normalize p = p\<close> N_def)
   1.223 +      ultimately have "finite N"
   1.224 +        by (rule inj_on_finite) (simp add: finite_divisors \<open>a \<noteq> 0\<close>)
   1.225 +      from N_def \<open>a \<noteq> 0\<close> have "0 \<in> N" by (simp add: N_def)
   1.226 +      then have "N \<noteq> {}" by blast
   1.227 +      note * = \<open>finite N\<close> \<open>N \<noteq> {}\<close>
   1.228 +      from N_def \<open>p dvd a\<close> have "1 \<in> N" by simp
   1.229 +      with * show "Max N > 0"
   1.230 +        by (auto simp add: Max_gr_iff)
   1.231 +      from * have "Max N \<in> N" by (rule Max_in)
   1.232 +      then show "p ^ Max N dvd a" by (simp add: N_def)
   1.233 +      from * have "\<forall>n\<in>N. n \<le> Max N"
   1.234 +        by (simp add: Max_le_iff [symmetric])
   1.235 +      then have "p ^ Suc (Max N) dvd a \<Longrightarrow> Suc (Max N) \<le> Max N"
   1.236 +        by (rule bspec) (simp add: N_def)
   1.237 +      then show "\<not> p ^ Suc (Max N) dvd a"
   1.238 +        by auto
   1.239 +    qed
   1.240 +    from \<open>p ^ n dvd a\<close> obtain c where "a = p ^ n * c" ..
   1.241 +    with \<open>\<not> p ^ Suc n dvd a\<close> have "\<not> p dvd c"
   1.242 +      by (auto elim: dvdE simp add: ac_simps)
   1.243 +    have "?prime_factors a - {p} = ?prime_factors c" (is "?A = ?B")
   1.244 +    proof (rule set_eqI)
   1.245 +      fix q
   1.246 +      show "q \<in> ?A \<longleftrightarrow> q \<in> ?B"
   1.247 +      using \<open>normalize p = p\<close> \<open>is_prime p\<close> \<open>a = p ^ n * c\<close> \<open>\<not> p dvd c\<close>
   1.248 +        prime_unique [of q p]
   1.249 +        by (auto simp add: prime_dvd_power_iff prime_dvd_mult_iff)
   1.250 +    qed
   1.251 +    moreover from insert have "P = ?prime_factors a - {p}"
   1.252 +      by auto
   1.253 +    ultimately have "P = ?prime_factors c"
   1.254 +      by simp
   1.255 +    moreover from \<open>a \<noteq> 0\<close> \<open>a = p ^ n * c\<close> have "c \<noteq> 0"
   1.256 +      by auto
   1.257 +    ultimately obtain P where "\<And>p. p \<in># P \<Longrightarrow> is_prime p" "msetprod P = normalize c"
   1.258 +      using insert(3) by blast 
   1.259 +    with \<open>is_prime p\<close> \<open>a = p ^ n * c\<close> \<open>normalize p = p\<close>
   1.260 +    have "\<And>q. q \<in># P + (replicate_mset n p) \<longrightarrow> is_prime q" "msetprod (P + replicate_mset n p) = normalize a"
   1.261 +      by (auto simp add: ac_simps normalize_mult normalize_power)
   1.262 +    with insert(6) show thesis by blast
   1.263 +  qed
   1.264 +qed
   1.265 +  
   1.266 +end
   1.267 +
   1.268 +instantiation nat :: factorial_semiring
   1.269 +begin
   1.270 +
   1.271 +definition is_prime_nat :: "nat \<Rightarrow> bool"
   1.272 +where
   1.273 +  "is_prime_nat p \<longleftrightarrow> (1 < p \<and> (\<forall>n. n dvd p \<longrightarrow> n = 1 \<or> n = p))"
   1.274 +
   1.275 +lemma is_prime_eq_prime:
   1.276 +  "is_prime = prime"
   1.277 +  by (simp add: fun_eq_iff prime_def is_prime_nat_def)
   1.278 +
   1.279 +instance proof
   1.280 +  show "\<not> is_prime (0::nat)" by (simp add: is_prime_nat_def)
   1.281 +  show "\<not> is_unit p" if "is_prime p" for p :: nat
   1.282 +    using that by (simp add: is_prime_nat_def)
   1.283 +next
   1.284 +  fix p :: nat
   1.285 +  assume "p \<noteq> 0" and "\<not> is_unit p"
   1.286 +  then have "p > 1" by simp
   1.287 +  assume P: "\<And>n. n dvd p \<Longrightarrow> \<not> is_unit n \<Longrightarrow> p dvd n"
   1.288 +  have "n = 1" if "n dvd p" "n \<noteq> p" for n
   1.289 +  proof (rule ccontr)
   1.290 +    assume "n \<noteq> 1"
   1.291 +    with that P have "p dvd n" by auto
   1.292 +    with \<open>n dvd p\<close> have "n = p" by (rule dvd_antisym)
   1.293 +    with that show False by simp
   1.294 +  qed
   1.295 +  with \<open>p > 1\<close> show "is_prime p" by (auto simp add: is_prime_nat_def)
   1.296 +next
   1.297 +  fix p m n :: nat
   1.298 +  assume "is_prime p"
   1.299 +  then have "prime p" by (simp add: is_prime_eq_prime)
   1.300 +  moreover assume "p dvd m * n"
   1.301 +  ultimately show "p dvd m \<or> p dvd n"
   1.302 +    by (rule prime_dvd_mult_nat)
   1.303 +next
   1.304 +  fix n :: nat
   1.305 +  show "is_unit n" if "\<And>m. m dvd n \<Longrightarrow> \<not> is_prime m"
   1.306 +    using that prime_factor_nat by (auto simp add: is_prime_eq_prime)
   1.307 +qed simp
   1.308 +
   1.309 +end
   1.310 +
   1.311 +instantiation int :: factorial_semiring
   1.312 +begin
   1.313 +
   1.314 +definition is_prime_int :: "int \<Rightarrow> bool"
   1.315 +where
   1.316 +  "is_prime_int p \<longleftrightarrow> is_prime (nat \<bar>p\<bar>)"
   1.317 +
   1.318 +lemma is_prime_int_iff [simp]:
   1.319 +  "is_prime (int n) \<longleftrightarrow> is_prime n"
   1.320 +  by (simp add: is_prime_int_def)
   1.321 +
   1.322 +lemma is_prime_nat_abs_iff [simp]:
   1.323 +  "is_prime (nat \<bar>k\<bar>) \<longleftrightarrow> is_prime k"
   1.324 +  by (simp add: is_prime_int_def)
   1.325 +
   1.326 +instance proof
   1.327 +  show "\<not> is_prime (0::int)" by (simp add: is_prime_int_def)
   1.328 +  show "\<not> is_unit p" if "is_prime p" for p :: int
   1.329 +    using that is_prime_not_unit [of "nat \<bar>p\<bar>"] by simp
   1.330 +next
   1.331 +  fix p :: int
   1.332 +  assume P: "\<And>k. k dvd p \<Longrightarrow> \<not> is_unit k \<Longrightarrow> p dvd k"
   1.333 +  have "nat \<bar>p\<bar> dvd n" if "n dvd nat \<bar>p\<bar>" and "n \<noteq> Suc 0" for n :: nat
   1.334 +  proof -
   1.335 +    from that have "int n dvd p" by (simp add: int_dvd_iff)
   1.336 +    moreover from that have "\<not> is_unit (int n)" by simp
   1.337 +    ultimately have "p dvd int n" by (rule P)
   1.338 +    with that have "p dvd int n" by auto
   1.339 +    then show ?thesis by (simp add: dvd_int_iff)
   1.340 +  qed
   1.341 +  moreover assume "p \<noteq> 0" and "\<not> is_unit p"
   1.342 +  ultimately have "is_prime (nat \<bar>p\<bar>)" by (intro is_primeI) auto
   1.343 +  then show "is_prime p" by simp
   1.344 +next
   1.345 +  fix p k l :: int
   1.346 +  assume "is_prime p"
   1.347 +  then have *: "is_prime (nat \<bar>p\<bar>)" by simp
   1.348 +  assume "p dvd k * l"
   1.349 +  then have "nat \<bar>p\<bar> dvd nat \<bar>k * l\<bar>"
   1.350 +    by (simp add: dvd_int_iff)
   1.351 +  then have "nat \<bar>p\<bar> dvd nat \<bar>k\<bar> * nat \<bar>l\<bar>"
   1.352 +    by (simp add: abs_mult nat_mult_distrib)
   1.353 +  with * have "nat \<bar>p\<bar> dvd nat \<bar>k\<bar> \<or> nat \<bar>p\<bar> dvd nat \<bar>l\<bar>"
   1.354 +    using is_primeD [of "nat \<bar>p\<bar>"] by auto
   1.355 +  then show "p dvd k \<or> p dvd l"
   1.356 +    by (simp add: dvd_int_iff)
   1.357 +next
   1.358 +  fix k :: int
   1.359 +  assume P: "\<And>l. l dvd k \<Longrightarrow> \<not> is_prime l"
   1.360 +  have "is_unit (nat \<bar>k\<bar>)"
   1.361 +  proof (rule no_prime_divisorsI)
   1.362 +    fix m
   1.363 +    assume "m dvd nat \<bar>k\<bar>"
   1.364 +    then have "int m dvd k" by (simp add: int_dvd_iff)
   1.365 +    then have "\<not> is_prime (int m)" by (rule P)
   1.366 +    then show "\<not> is_prime m" by simp
   1.367 +  qed
   1.368 +  then show "is_unit k" by simp
   1.369 +qed simp
   1.370 +
   1.371 +end
   1.372 +
   1.373 +end