src/HOL/Number_Theory/Factorial_Ring.thy
 changeset 62499 4a5b81ff5992 parent 62366 95c6cf433c91 child 63040 eb4ddd18d635
```     1.1 --- a/src/HOL/Number_Theory/Factorial_Ring.thy	Wed Mar 02 19:43:31 2016 +0100
1.2 +++ b/src/HOL/Number_Theory/Factorial_Ring.thy	Thu Mar 03 08:33:55 2016 +0100
1.3 @@ -5,15 +5,31 @@
1.4  section \<open>Factorial (semi)rings\<close>
1.5
1.6  theory Factorial_Ring
1.7 -imports Main Primes "~~/src/HOL/Library/Multiset" (*"~~/src/HOL/Library/Polynomial"*)
1.8 +imports Main Primes "~~/src/HOL/Library/Multiset"
1.9 +begin
1.10 +
1.11 +context algebraic_semidom
1.12  begin
1.13
1.14 +lemma dvd_mult_imp_div:
1.15 +  assumes "a * c dvd b"
1.16 +  shows "a dvd b div c"
1.17 +proof (cases "c = 0")
1.18 +  case True then show ?thesis by simp
1.19 +next
1.20 +  case False
1.21 +  from \<open>a * c dvd b\<close> obtain d where "b = a * c * d" ..
1.22 +  with False show ?thesis by (simp add: mult.commute [of a] mult.assoc)
1.23 +qed
1.24 +
1.25 +end
1.26 +
1.27  class factorial_semiring = normalization_semidom +
1.28    assumes finite_divisors: "a \<noteq> 0 \<Longrightarrow> finite {b. b dvd a \<and> normalize b = b}"
1.29    fixes is_prime :: "'a \<Rightarrow> bool"
1.30    assumes not_is_prime_zero [simp]: "\<not> is_prime 0"
1.31      and is_prime_not_unit: "is_prime p \<Longrightarrow> \<not> is_unit p"
1.32 -    and no_prime_divisorsI: "(\<And>b. b dvd a \<Longrightarrow> \<not> is_prime b) \<Longrightarrow> is_unit a"
1.33 +    and no_prime_divisorsI2: "(\<And>b. b dvd a \<Longrightarrow> \<not> is_prime b) \<Longrightarrow> is_unit a"
1.34    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.35      and is_primeD: "is_prime p \<Longrightarrow> p dvd a * b \<Longrightarrow> p dvd a \<or> p dvd b"
1.36  begin
1.37 @@ -93,21 +109,6 @@
1.38    with that show thesis by blast
1.39  qed
1.40
1.41 -lemma prime_divisorE:
1.42 -  assumes "a \<noteq> 0" and "\<not> is_unit a"
1.43 -  obtains p where "is_prime p" and "p dvd a"
1.44 -  using assms no_prime_divisorsI [of a] by blast
1.45 -
1.46 -lemma prime_dvd_mult_iff:
1.47 -  assumes "is_prime p"
1.48 -  shows "p dvd a * b \<longleftrightarrow> p dvd a \<or> p dvd b"
1.49 -  using assms by (auto dest: is_primeD)
1.50 -
1.51 -lemma prime_dvd_power_iff:
1.52 -  assumes "is_prime p"
1.53 -  shows "p dvd a ^ n \<longleftrightarrow> p dvd a \<and> n > 0"
1.54 -  using assms by (induct n) (auto dest: is_prime_not_unit is_primeD)
1.55 -
1.56  lemma is_prime_normalize_iff [simp]:
1.57    "is_prime (normalize p) \<longleftrightarrow> is_prime p" (is "?P \<longleftrightarrow> ?Q")
1.58  proof
1.59 @@ -119,6 +120,157 @@
1.60        (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.61  qed
1.62
1.63 +lemma no_prime_divisorsI:
1.64 +  assumes "\<And>b. b dvd a \<Longrightarrow> normalize b = b \<Longrightarrow> \<not> is_prime b"
1.65 +  shows "is_unit a"
1.66 +proof (rule no_prime_divisorsI2)
1.67 +  fix b
1.68 +  assume "b dvd a"
1.69 +  then have "normalize b dvd a"
1.70 +    by simp
1.71 +  moreover have "normalize (normalize b) = normalize b"
1.72 +    by simp
1.73 +  ultimately have "\<not> is_prime (normalize b)"
1.74 +    by (rule assms)
1.75 +  then show "\<not> is_prime b"
1.76 +    by simp
1.77 +qed
1.78 +
1.79 +lemma prime_divisorE:
1.80 +  assumes "a \<noteq> 0" and "\<not> is_unit a"
1.81 +  obtains p where "is_prime p" and "p dvd a"
1.82 +  using assms no_prime_divisorsI [of a] by blast
1.83 +
1.84 +lemma is_prime_associated:
1.85 +  assumes "is_prime p" and "is_prime q" and "q dvd p"
1.86 +  shows "normalize q = normalize p"
1.87 +using \<open>q dvd p\<close> proof (rule associatedI)
1.88 +  from \<open>is_prime q\<close> have "\<not> is_unit q"
1.89 +    by (simp add: is_prime_not_unit)
1.90 +  with \<open>is_prime p\<close> \<open>q dvd p\<close> show "p dvd q"
1.91 +    by (blast intro: is_primeD2)
1.92 +qed
1.93 +
1.94 +lemma prime_dvd_mult_iff:
1.95 +  assumes "is_prime p"
1.96 +  shows "p dvd a * b \<longleftrightarrow> p dvd a \<or> p dvd b"
1.97 +  using assms by (auto dest: is_primeD)
1.98 +
1.99 +lemma prime_dvd_msetprod:
1.100 +  assumes "is_prime p"
1.101 +  assumes dvd: "p dvd msetprod A"
1.102 +  obtains a where "a \<in># A" and "p dvd a"
1.103 +proof -
1.104 +  from dvd have "\<exists>a. a \<in># A \<and> p dvd a"
1.105 +  proof (induct A)
1.106 +    case empty then show ?case
1.107 +    using \<open>is_prime p\<close> by (simp add: is_prime_not_unit)
1.108 +  next
1.109 +    case (add A a)
1.110 +    then have "p dvd msetprod A * a" by simp
1.111 +    with \<open>is_prime p\<close> consider (A) "p dvd msetprod A" | (B) "p dvd a"
1.112 +      by (blast dest: is_primeD)
1.113 +    then show ?case proof cases
1.114 +      case B then show ?thesis by auto
1.115 +    next
1.116 +      case A
1.117 +      with add.hyps obtain b where "b \<in># A" "p dvd b"
1.118 +        by auto
1.119 +      then show ?thesis by auto
1.120 +    qed
1.121 +  qed
1.122 +  with that show thesis by blast
1.123 +qed
1.124 +
1.125 +lemma msetprod_eq_iff:
1.126 +  assumes "\<forall>p\<in>set_mset P. is_prime p \<and> normalize p = p" and "\<forall>p\<in>set_mset Q. is_prime p \<and> normalize p = p"
1.127 +  shows "msetprod P = msetprod Q \<longleftrightarrow> P = Q" (is "?R \<longleftrightarrow> ?S")
1.128 +proof
1.129 +  assume ?S then show ?R by simp
1.130 +next
1.131 +  assume ?R then show ?S using assms proof (induct P arbitrary: Q)
1.132 +    case empty then have Q: "msetprod Q = 1" by simp
1.133 +    have "Q = {#}"
1.134 +    proof (rule ccontr)
1.135 +      assume "Q \<noteq> {#}"
1.136 +      then obtain r R where "Q = R + {#r#}"
1.137 +        using multi_nonempty_split by blast
1.138 +      moreover with empty have "is_prime r" by simp
1.139 +      ultimately have "msetprod Q = msetprod R * r"
1.140 +        by simp
1.141 +      with Q have "msetprod R * r = 1"
1.142 +        by simp
1.143 +      then have "is_unit r"
1.144 +        by (metis local.dvd_triv_right)
1.145 +      with \<open>is_prime r\<close> show False by (simp add: is_prime_not_unit)
1.146 +    qed
1.147 +    then show ?case by simp
1.148 +  next
1.149 +    case (add P p)
1.150 +    then have "is_prime p" and "normalize p = p"
1.151 +      and "msetprod Q = msetprod P * p" and "p dvd msetprod Q"
1.152 +      by auto (metis local.dvd_triv_right)
1.153 +    with prime_dvd_msetprod
1.154 +      obtain q where "q \<in># Q" and "p dvd q"
1.155 +      by blast
1.156 +    with add.prems have "is_prime q" and "normalize q = q"
1.157 +      by simp_all
1.158 +    from \<open>is_prime p\<close> have "p \<noteq> 0"
1.159 +      by auto
1.160 +    from \<open>is_prime q\<close> \<open>is_prime p\<close> \<open>p dvd q\<close>
1.161 +      have "normalize p = normalize q"
1.162 +      by (rule is_prime_associated)
1.163 +    from \<open>normalize p = p\<close> \<open>normalize q = q\<close> have "p = q"
1.164 +      unfolding \<open>normalize p = normalize q\<close> by simp
1.165 +    with \<open>q \<in># Q\<close> have "p \<in># Q" by simp
1.166 +    have "msetprod P = msetprod (Q - {#p#})"
1.167 +      using \<open>p \<in># Q\<close> \<open>p \<noteq> 0\<close> \<open>msetprod Q = msetprod P * p\<close>
1.168 +      by (simp add: msetprod_minus)
1.169 +    then have "P = Q - {#p#}"
1.171 +      by (auto intro: add.hyps dest: in_diffD)
1.172 +    with \<open>p \<in># Q\<close> show ?case by simp
1.173 +  qed
1.174 +qed
1.175 +
1.176 +lemma prime_dvd_power_iff:
1.177 +  assumes "is_prime p"
1.178 +  shows "p dvd a ^ n \<longleftrightarrow> p dvd a \<and> n > 0"
1.179 +  using assms by (induct n) (auto dest: is_prime_not_unit is_primeD)
1.180 +
1.181 +lemma prime_power_dvd_multD:
1.182 +  assumes "is_prime p"
1.183 +  assumes "p ^ n dvd a * b" and "n > 0" and "\<not> p dvd a"
1.184 +  shows "p ^ n dvd b"
1.185 +using \<open>p ^ n dvd a * b\<close> and \<open>n > 0\<close> proof (induct n arbitrary: b)
1.186 +  case 0 then show ?case by simp
1.187 +next
1.188 +  case (Suc n) show ?case
1.189 +  proof (cases "n = 0")
1.190 +    case True with Suc \<open>is_prime p\<close> \<open>\<not> p dvd a\<close> show ?thesis
1.191 +      by (simp add: prime_dvd_mult_iff)
1.192 +  next
1.193 +    case False then have "n > 0" by simp
1.194 +    from \<open>is_prime p\<close> have "p \<noteq> 0" by auto
1.195 +    from Suc.prems have *: "p * p ^ n dvd a * b"
1.196 +      by simp
1.197 +    then have "p dvd a * b"
1.198 +      by (rule dvd_mult_left)
1.199 +    with Suc \<open>is_prime p\<close> \<open>\<not> p dvd a\<close> have "p dvd b"
1.200 +      by (simp add: prime_dvd_mult_iff)
1.201 +    moreover def c \<equiv> "b div p"
1.202 +    ultimately have b: "b = p * c" by simp
1.203 +    with * have "p * p ^ n dvd p * (a * c)"
1.204 +      by (simp add: ac_simps)
1.205 +    with \<open>p \<noteq> 0\<close> have "p ^ n dvd a * c"
1.206 +      by simp
1.207 +    with Suc.hyps \<open>n > 0\<close> have "p ^ n dvd c"
1.208 +      by blast
1.209 +    with \<open>p \<noteq> 0\<close> show ?thesis
1.210 +      by (simp add: b)
1.211 +  qed
1.212 +qed
1.213 +
1.214  lemma is_prime_inj_power:
1.215    assumes "is_prime p"
1.216    shows "inj (op ^ p)"
1.217 @@ -150,47 +302,80 @@
1.218    ultimately show False using * [of m n] * [of n m] by auto
1.219  qed
1.220
1.221 -lemma prime_unique:
1.222 -  assumes "is_prime q" and "is_prime p" and "q dvd p"
1.223 -  shows "normalize q = normalize p"
1.224 -proof -
1.225 -  from assms have "p dvd q"
1.226 -    by (auto dest: is_primeD2 [of p] is_prime_not_unit [of q])
1.227 -  with assms show ?thesis
1.228 -    by (auto intro: associatedI)
1.229 -qed
1.230 +definition factorization :: "'a \<Rightarrow> 'a multiset option"
1.231 +  where "factorization a = (if a = 0 then None
1.232 +    else Some (setsum (\<lambda>p. replicate_mset (Max {n. p ^ n dvd a}) p)
1.233 +      {p. p dvd a \<and> is_prime p \<and> normalize p = p}))"
1.234 +
1.235 +lemma factorization_normalize [simp]:
1.236 +  "factorization (normalize a) = factorization a"
1.237 +  by (simp add: factorization_def)
1.238 +
1.239 +lemma factorization_0 [simp]:
1.240 +  "factorization 0 = None"
1.241 +  by (simp add: factorization_def)
1.242 +
1.243 +lemma factorization_eq_None_iff [simp]:
1.244 +  "factorization a = None \<longleftrightarrow> a = 0"
1.245 +  by (simp add: factorization_def)
1.246
1.247 -lemma exists_factorization:
1.248 -  assumes "a \<noteq> 0"
1.249 -  obtains P where "\<And>p. p \<in># P \<Longrightarrow> is_prime p" "msetprod P = normalize a"
1.250 -proof -
1.251 +lemma factorization_eq_Some_iff:
1.252 +  "factorization a = Some P \<longleftrightarrow>
1.253 +   normalize a = msetprod P \<and> 0 \<notin># P \<and> (\<forall>p \<in> set_mset P. is_prime p \<and> normalize p = p)"
1.254 +proof (cases "a = 0")
1.255 +  have [simp]: "0 = msetprod P \<longleftrightarrow> 0 \<in># P"
1.256 +    using msetprod_zero_iff [of P] by blast
1.257 +  case True
1.258 +  then show ?thesis by auto
1.259 +next
1.260 +  case False
1.261    let ?prime_factors = "\<lambda>a. {p. p dvd a \<and> is_prime p \<and> normalize p = p}"
1.262 -  have "?prime_factors a \<subseteq> {b. b dvd a \<and> normalize b = b}" by auto
1.263 -  moreover from assms have "finite {b. b dvd a \<and> normalize b = b}"
1.264 +  have "?prime_factors a \<subseteq> {b. b dvd a \<and> normalize b = b}"
1.265 +    by auto
1.266 +  moreover from \<open>a \<noteq> 0\<close> have "finite {b. b dvd a \<and> normalize b = b}"
1.267      by (rule finite_divisors)
1.268 -  ultimately have "finite (?prime_factors a)" by (rule finite_subset)
1.269 -  then show thesis using \<open>a \<noteq> 0\<close> that proof (induct "?prime_factors a" arbitrary: thesis a)
1.270 +  ultimately have "finite (?prime_factors a)"
1.271 +    by (rule finite_subset)
1.272 +  then show ?thesis using \<open>a \<noteq> 0\<close>
1.273 +  proof (induct "?prime_factors a" arbitrary: a P)
1.274      case empty then have
1.275 -      P: "\<And>b. is_prime b \<Longrightarrow> b dvd a \<Longrightarrow> normalize b \<noteq> b" and "a \<noteq> 0"
1.276 +      *: "{p. p dvd a \<and> is_prime p \<and> normalize p = p} = {}"
1.277 +        and "a \<noteq> 0"
1.278        by auto
1.279 -    have "is_unit a"
1.280 -    proof (rule no_prime_divisorsI)
1.281 -      fix b
1.282 -      assume "b dvd a"
1.283 -      then show "\<not> is_prime b"
1.284 -        using P [of "normalize b"] by auto
1.285 +    from \<open>a \<noteq> 0\<close> have "factorization a = Some {#}"
1.286 +      by (simp only: factorization_def *) simp
1.287 +    from * have "normalize a = 1"
1.288 +      by (auto intro: is_unit_normalize no_prime_divisorsI)
1.289 +    show ?case (is "?lhs \<longleftrightarrow> ?rhs") proof
1.290 +      assume ?lhs with \<open>factorization a = Some {#}\<close> \<open>normalize a = 1\<close>
1.291 +      show ?rhs by simp
1.292 +    next
1.293 +      assume ?rhs have "P = {#}"
1.294 +      proof (rule ccontr)
1.295 +        assume "P \<noteq> {#}"
1.296 +        then obtain q Q where "P = Q + {#q#}"
1.297 +          using multi_nonempty_split by blast
1.298 +        with \<open>?rhs\<close> \<open>normalize a = 1\<close>
1.299 +        have "1 = q * msetprod Q" and "is_prime q"
1.300 +          by (simp_all add: ac_simps)
1.301 +        then have "is_unit q" by (auto intro: dvdI)
1.302 +        with \<open>is_prime q\<close> show False
1.303 +          using is_prime_not_unit by blast
1.304 +      qed
1.305 +      with \<open>factorization a = Some {#}\<close> show ?lhs by simp
1.306      qed
1.307 -    then have "\<And>p. p \<in># {#} \<Longrightarrow> is_prime p" and "msetprod {#} = normalize a"
1.308 -      by (simp_all add: is_unit_normalize)
1.309 -    with empty show thesis by blast
1.310    next
1.311 -    case (insert p P)
1.312 -    from \<open>insert p P = ?prime_factors a\<close>
1.313 -    have "p dvd a" and "is_prime p" and "normalize p = p"
1.314 -      by auto
1.315 -    obtain n where "n > 0" and "p ^ n dvd a" and "\<not> p ^ Suc n dvd a"
1.316 -    proof (rule that)
1.317 +    case (insert p F)
1.318 +    from \<open>insert p F = ?prime_factors a\<close>
1.319 +    have "?prime_factors a = insert p F"
1.320 +      by simp
1.321 +    then have "p dvd a" and "is_prime p" and "normalize p = p" and "p \<noteq> 0"
1.322 +      by (auto intro!: is_prime_not_zeroI)
1.323 +    def n \<equiv> "Max {n. p ^ n dvd a}"
1.324 +    then have "n > 0" and "p ^ n dvd a" and "\<not> p ^ Suc n dvd a"
1.325 +    proof -
1.326        def N \<equiv> "{n. p ^ n dvd a}"
1.327 +      then have n_M: "n = Max N" by (simp add: n_def)
1.328        from is_prime_inj_power \<open>is_prime p\<close> have "inj (op ^ p)" .
1.329        then have "inj_on (op ^ p) U" for U
1.330          by (rule subset_inj_on) simp
1.331 @@ -202,43 +387,275 @@
1.332        then have "N \<noteq> {}" by blast
1.333        note * = \<open>finite N\<close> \<open>N \<noteq> {}\<close>
1.334        from N_def \<open>p dvd a\<close> have "1 \<in> N" by simp
1.335 -      with * show "Max N > 0"
1.336 +      with * have "Max N > 0"
1.337          by (auto simp add: Max_gr_iff)
1.338 +      then show "n > 0" by (simp add: n_M)
1.339        from * have "Max N \<in> N" by (rule Max_in)
1.340 -      then show "p ^ Max N dvd a" by (simp add: N_def)
1.341 +      then have "p ^ Max N dvd a" by (simp add: N_def)
1.342 +      then show "p ^ n dvd a" by (simp add: n_M)
1.343        from * have "\<forall>n\<in>N. n \<le> Max N"
1.344          by (simp add: Max_le_iff [symmetric])
1.345        then have "p ^ Suc (Max N) dvd a \<Longrightarrow> Suc (Max N) \<le> Max N"
1.346          by (rule bspec) (simp add: N_def)
1.347 -      then show "\<not> p ^ Suc (Max N) dvd a"
1.348 +      then have "\<not> p ^ Suc (Max N) dvd a"
1.349          by auto
1.350 +      then show "\<not> p ^ Suc n dvd a"
1.351 +        by (simp add: n_M)
1.352      qed
1.353 -    from \<open>p ^ n dvd a\<close> obtain c where "a = p ^ n * c" ..
1.354 -    with \<open>\<not> p ^ Suc n dvd a\<close> have "\<not> p dvd c"
1.355 +    def b \<equiv> "a div p ^ n"
1.356 +    with \<open>p ^ n dvd a\<close> have a: "a = p ^ n * b"
1.357 +      by simp
1.358 +    with \<open>\<not> p ^ Suc n dvd a\<close> have "\<not> p dvd b" and "b \<noteq> 0"
1.359        by (auto elim: dvdE simp add: ac_simps)
1.360 -    have "?prime_factors a - {p} = ?prime_factors c" (is "?A = ?B")
1.361 +    have "?prime_factors a = insert p (?prime_factors b)"
1.362      proof (rule set_eqI)
1.363        fix q
1.364 -      show "q \<in> ?A \<longleftrightarrow> q \<in> ?B"
1.365 -      using \<open>normalize p = p\<close> \<open>is_prime p\<close> \<open>a = p ^ n * c\<close> \<open>\<not> p dvd c\<close>
1.366 -        prime_unique [of q p]
1.367 -        by (auto simp add: prime_dvd_power_iff prime_dvd_mult_iff)
1.368 +      show "q \<in> ?prime_factors a \<longleftrightarrow> q \<in> insert p (?prime_factors b)"
1.369 +      using \<open>is_prime p\<close> \<open>normalize p = p\<close> \<open>n > 0\<close>
1.370 +        by (auto simp add: a prime_dvd_mult_iff prime_dvd_power_iff)
1.371 +          (auto dest: is_prime_associated)
1.372      qed
1.373 -    moreover from insert have "P = ?prime_factors a - {p}"
1.374 +    with \<open>\<not> p dvd b\<close> have "?prime_factors a - {p} = ?prime_factors b"
1.375 +      by auto
1.376 +    with insert.hyps have "F = ?prime_factors b"
1.377        by auto
1.378 -    ultimately have "P = ?prime_factors c"
1.379 +    then have "?prime_factors b = F"
1.380 +      by simp
1.381 +    with \<open>?prime_factors a = insert p (?prime_factors b)\<close> have "?prime_factors a = insert p F"
1.382        by simp
1.383 -    moreover from \<open>a \<noteq> 0\<close> \<open>a = p ^ n * c\<close> have "c \<noteq> 0"
1.384 -      by auto
1.385 -    ultimately obtain P where "\<And>p. p \<in># P \<Longrightarrow> is_prime p" "msetprod P = normalize c"
1.386 -      using insert(3) by blast
1.387 -    with \<open>is_prime p\<close> \<open>a = p ^ n * c\<close> \<open>normalize p = p\<close>
1.388 -    have "\<And>q. q \<in># P + (replicate_mset n p) \<longrightarrow> is_prime q" "msetprod (P + replicate_mset n p) = normalize a"
1.389 -      by (auto simp add: ac_simps normalize_mult normalize_power)
1.390 -    with insert(6) show thesis by blast
1.391 +    have equiv: "(\<Sum>p\<in>F. replicate_mset (Max {n. p ^ n dvd a}) p) =
1.392 +      (\<Sum>p\<in>F. replicate_mset (Max {n. p ^ n dvd b}) p)"
1.393 +    using refl proof (rule Groups_Big.setsum.cong)
1.394 +      fix q
1.395 +      assume "q \<in> F"
1.396 +      have "{n. q ^ n dvd a} = {n. q ^ n dvd b}"
1.397 +      proof -
1.398 +        have "q ^ m dvd a \<longleftrightarrow> q ^ m dvd b" (is "?R \<longleftrightarrow> ?S")
1.399 +          for m
1.400 +        proof (cases "m = 0")
1.401 +          case True then show ?thesis by simp
1.402 +        next
1.403 +          case False then have "m > 0" by simp
1.404 +          show ?thesis
1.405 +          proof
1.406 +            assume ?S then show ?R by (simp add: a)
1.407 +          next
1.408 +            assume ?R
1.409 +            then have *: "q ^ m dvd p ^ n * b" by (simp add: a)
1.410 +            from insert.hyps \<open>q \<in> F\<close>
1.411 +            have "is_prime q" "normalize q = q" "p \<noteq> q" "q dvd p ^ n * b"
1.412 +              by (auto simp add: a)
1.413 +            from \<open>is_prime q\<close> * \<open>m > 0\<close> show ?S
1.414 +            proof (rule prime_power_dvd_multD)
1.415 +              have "\<not> q dvd p"
1.416 +              proof
1.417 +                assume "q dvd p"
1.418 +                with \<open>is_prime q\<close> \<open>is_prime p\<close> have "normalize q = normalize p"
1.419 +                  by (blast intro: is_prime_associated)
1.420 +                with \<open>normalize p = p\<close> \<open>normalize q = q\<close> \<open>p \<noteq> q\<close> show False
1.421 +                  by simp
1.422 +              qed
1.423 +              with \<open>is_prime q\<close> show "\<not> q dvd p ^ n"
1.424 +                by (simp add: prime_dvd_power_iff)
1.425 +            qed
1.426 +          qed
1.427 +        qed
1.428 +        then show ?thesis by auto
1.429 +      qed
1.430 +      then show
1.431 +        "replicate_mset (Max {n. q ^ n dvd a}) q = replicate_mset (Max {n. q ^ n dvd b}) q"
1.432 +        by simp
1.433 +    qed
1.434 +    def Q \<equiv> "the (factorization b)"
1.435 +    with \<open>b \<noteq> 0\<close> have [simp]: "factorization b = Some Q"
1.436 +      by simp
1.437 +    from \<open>a \<noteq> 0\<close> have "factorization a =
1.438 +      Some (\<Sum>p\<in>?prime_factors a. replicate_mset (Max {n. p ^ n dvd a}) p)"
1.439 +      by (simp add: factorization_def)
1.440 +    also have "\<dots> =
1.441 +      Some (\<Sum>p\<in>insert p F. replicate_mset (Max {n. p ^ n dvd a}) p)"
1.442 +      by (simp add: \<open>?prime_factors a = insert p F\<close>)
1.443 +    also have "\<dots> =
1.444 +      Some (replicate_mset n p + (\<Sum>p\<in>F. replicate_mset (Max {n. p ^ n dvd a}) p))"
1.445 +      using \<open>finite F\<close> \<open>p \<notin> F\<close> n_def by simp
1.446 +    also have "\<dots> =
1.447 +      Some (replicate_mset n p + (\<Sum>p\<in>F. replicate_mset (Max {n. p ^ n dvd b}) p))"
1.448 +      using equiv by simp
1.449 +    also have "\<dots> = Some (replicate_mset n p + the (factorization b))"
1.450 +      using \<open>b \<noteq> 0\<close> by (simp add: factorization_def \<open>?prime_factors a = insert p F\<close> \<open>?prime_factors b = F\<close>)
1.451 +    finally have fact_a: "factorization a =
1.452 +      Some (replicate_mset n p + Q)"
1.453 +      by simp
1.454 +    moreover have "factorization b = Some Q \<longleftrightarrow>
1.455 +      normalize b = msetprod Q \<and>
1.456 +      0 \<notin># Q \<and>
1.457 +      (\<forall>p\<in>#Q. is_prime p \<and> normalize p = p)"
1.458 +      using \<open>F = ?prime_factors b\<close> \<open>b \<noteq> 0\<close> by (rule insert.hyps)
1.459 +    ultimately have
1.460 +      norm_a: "normalize a = msetprod (replicate_mset n p + Q)" and
1.461 +      prime_Q: "\<forall>p\<in>set_mset Q. is_prime p \<and> normalize p = p"
1.462 +      by (simp_all add: a normalize_mult normalize_power \<open>normalize p = p\<close>)
1.463 +    show ?case (is "?lhs \<longleftrightarrow> ?rhs") proof
1.464 +      assume ?lhs with fact_a
1.465 +      have "P = replicate_mset n p + Q" by simp
1.466 +      with \<open>n > 0\<close> \<open>is_prime p\<close> \<open>normalize p = p\<close> prime_Q
1.467 +        show ?rhs by (auto simp add: norm_a dest: is_prime_not_zeroI)
1.468 +    next
1.469 +      assume ?rhs
1.470 +      with \<open>n > 0\<close> \<open>is_prime p\<close> \<open>normalize p = p\<close> \<open>n > 0\<close> prime_Q
1.471 +      have "msetprod P = msetprod (replicate_mset n p + Q)"
1.472 +        and "\<forall>p\<in>set_mset P. is_prime p \<and> normalize p = p"
1.473 +        and "\<forall>p\<in>set_mset (replicate_mset n p + Q). is_prime p \<and> normalize p = p"
1.474 +        by (simp_all add: norm_a)
1.475 +      then have "P = replicate_mset n p + Q"
1.476 +        by (simp only: msetprod_eq_iff)
1.477 +      then show ?lhs
1.478 +        by (simp add: fact_a)
1.479 +    qed
1.480    qed
1.481  qed
1.482 -
1.483 +
1.484 +lemma factorization_cases [case_names 0 factorization]:
1.485 +  assumes "0": "a = 0 \<Longrightarrow> P"
1.486 +  assumes factorization: "\<And>A. a \<noteq> 0 \<Longrightarrow> factorization a = Some A \<Longrightarrow> msetprod A = normalize a
1.487 +    \<Longrightarrow> 0 \<notin># A \<Longrightarrow> (\<And>p. p \<in># A \<Longrightarrow> normalize p = p) \<Longrightarrow> (\<And>p. p \<in># A \<Longrightarrow> is_prime p) \<Longrightarrow> P"
1.488 +  shows P
1.489 +proof (cases "a = 0")
1.490 +  case True with 0 show P .
1.491 +next
1.492 +  case False
1.493 +  then have "factorization a \<noteq> None" by simp
1.494 +  then obtain A where "factorization a = Some A" by blast
1.495 +  moreover from this have "msetprod A = normalize a"
1.496 +    "0 \<notin># A" "\<And>p. p \<in># A \<Longrightarrow> normalize p = p" "\<And>p. p \<in># A \<Longrightarrow> is_prime p"
1.497 +    by (auto simp add: factorization_eq_Some_iff)
1.498 +  ultimately show P using \<open>a \<noteq> 0\<close> factorization by blast
1.499 +qed
1.500 +
1.501 +lemma factorizationE:
1.502 +  assumes "a \<noteq> 0"
1.503 +  obtains A u where "factorization a = Some A" "normalize a = msetprod A"
1.504 +    "0 \<notin># A" "\<And>p. p \<in># A \<Longrightarrow> is_prime p" "\<And>p. p \<in># A \<Longrightarrow> normalize p = p"
1.505 +  using assms by (cases a rule: factorization_cases) simp_all
1.506 +
1.507 +lemma prime_dvd_mset_prod_iff:
1.508 +  assumes "is_prime p" "normalize p = p" "\<And>p. p \<in># A \<Longrightarrow> is_prime p" "\<And>p. p \<in># A \<Longrightarrow> normalize p = p"
1.509 +  shows "p dvd msetprod A \<longleftrightarrow> p \<in># A"
1.510 +using assms proof (induct A)
1.511 +  case empty then show ?case by (auto dest: is_prime_not_unit)
1.512 +next
1.513 +  case (add A q) then show ?case
1.514 +    using is_prime_associated [of q p]
1.515 +    by (simp_all add: prime_dvd_mult_iff, safe, simp_all)
1.516 +qed
1.517 +
1.518 +end
1.519 +
1.520 +class factorial_semiring_gcd = factorial_semiring + gcd +
1.521 +  assumes gcd_unfold: "gcd a b =
1.522 +    (if a = 0 then normalize b
1.523 +     else if b = 0 then normalize a
1.524 +     else msetprod (the (factorization a) #\<inter> the (factorization b)))"
1.525 +  and lcm_unfold: "lcm a b =
1.526 +    (if a = 0 \<or> b = 0 then 0
1.527 +     else msetprod (the (factorization a) #\<union> the (factorization b)))"
1.528 +begin
1.529 +
1.530 +subclass semiring_gcd
1.531 +proof
1.532 +  fix a b
1.533 +  have comm: "gcd a b = gcd b a" for a b
1.534 +   by (simp add: gcd_unfold ac_simps)
1.535 +  have "gcd a b dvd a" for a b
1.536 +  proof (cases a rule: factorization_cases)
1.537 +    case 0 then show ?thesis by simp
1.538 +  next
1.539 +    case (factorization A) note fact_A = this
1.540 +    then have non_zero: "\<And>p. p \<in>#A \<Longrightarrow> p \<noteq> 0"
1.541 +      using normalize_0 not_is_prime_zero by blast
1.542 +    show ?thesis
1.543 +    proof (cases b rule: factorization_cases)
1.544 +      case 0 then show ?thesis by (simp add: gcd_unfold)
1.545 +    next
1.546 +      case (factorization B) note fact_B = this
1.547 +      have "msetprod (A #\<inter> B) dvd msetprod A"
1.548 +      using non_zero proof (induct B arbitrary: A)
1.549 +        case empty show ?case by simp
1.550 +      next
1.551 +        case (add B p) show ?case
1.552 +        proof (cases "p \<in># A")
1.553 +          case True then obtain C where "A = C + {#p#}"
1.554 +            by (metis insert_DiffM2)
1.555 +          moreover with True add have "p \<noteq> 0" and "\<And>p. p \<in># C \<Longrightarrow> p \<noteq> 0"
1.556 +            by auto
1.557 +          ultimately show ?thesis
1.558 +            using True add.hyps [of C]
1.559 +            by (simp add: inter_union_distrib_left [symmetric])
1.560 +        next
1.563 +        qed
1.564 +      qed
1.565 +      with fact_A fact_B show ?thesis by (simp add: gcd_unfold)
1.566 +    qed
1.567 +  qed
1.568 +  then have "gcd a b dvd a" and "gcd b a dvd b"
1.569 +    by simp_all
1.570 +  then show "gcd a b dvd a" and "gcd a b dvd b"
1.571 +    by (simp_all add: comm)
1.572 +  show "c dvd gcd a b" if "c dvd a" and "c dvd b" for c
1.573 +  proof (cases "a = 0 \<or> b = 0 \<or> c = 0")
1.574 +    case True with that show ?thesis by (auto simp add: gcd_unfold)
1.575 +  next
1.576 +    case False then have "a \<noteq> 0" and "b \<noteq> 0" and "c \<noteq> 0"
1.577 +      by simp_all
1.578 +    then obtain A B C where fact:
1.579 +      "factorization a = Some A" "factorization b = Some B" "factorization c = Some C"
1.580 +      and norm: "normalize a = msetprod A" "normalize b = msetprod B" "normalize c = msetprod C"
1.581 +      and A: "0 \<notin># A" "\<And>p. p \<in># A \<Longrightarrow> normalize p = p" "\<And>p. p \<in># A \<Longrightarrow> is_prime p"
1.582 +      and B: "0 \<notin># B" "\<And>p. p \<in># B \<Longrightarrow> normalize p = p" "\<And>p. p \<in># B \<Longrightarrow> is_prime p"
1.583 +      and C: "0 \<notin># C" "\<And>p. p \<in># C \<Longrightarrow> normalize p = p" "\<And>p. p \<in># C \<Longrightarrow> is_prime p"
1.584 +      by (blast elim!: factorizationE)
1.585 +    moreover from that have "normalize c dvd normalize a" and "normalize c dvd normalize b"
1.586 +      by simp_all
1.587 +    ultimately have "msetprod C dvd msetprod A" and "msetprod C dvd msetprod B"
1.588 +      by simp_all
1.589 +    with A B C have "msetprod C dvd msetprod (A #\<inter> B)"
1.590 +    proof (induct C arbitrary: A B)
1.591 +      case empty then show ?case by simp
1.592 +    next
1.595 +        have p: "p \<noteq> 0" "is_prime p" "normalize p = p" by auto
1.596 +      from add.prems have prems: "msetprod C * p dvd msetprod A" "msetprod C * p dvd msetprod B"
1.597 +        by simp_all
1.598 +      then have "p dvd msetprod A" "p dvd msetprod B"
1.599 +        by (auto dest: dvd_mult_imp_div dvd_mult_right)
1.600 +      with p add.prems have "p \<in># A" "p \<in># B"
1.601 +        by (simp_all add: prime_dvd_mset_prod_iff)
1.602 +      then obtain A' B' where ABp: "A = {#p#} + A'" "B = {#p#} + B'"
1.603 +        by (auto dest!: multi_member_split simp add: ac_simps)
1.604 +      with add.prems prems p have "msetprod C dvd msetprod (A' #\<inter> B')"
1.606 +      with p have "msetprod ({#p#} + C) dvd msetprod (({#p#} + A') #\<inter> ({#p#} + B'))"
1.607 +        by (simp add: inter_union_distrib_right [symmetric])
1.608 +      then show ?case by (simp add: ABp ac_simps)
1.609 +    qed
1.610 +    with \<open>a \<noteq> 0\<close> \<open>b \<noteq> 0\<close> that fact have "normalize c dvd gcd a b"
1.611 +      by (simp add: norm [symmetric] gcd_unfold fact)
1.612 +    then show ?thesis by simp
1.613 +  qed
1.614 +  show "normalize (gcd a b) = gcd a b"
1.615 +    apply (simp add: gcd_unfold)
1.616 +    apply safe
1.617 +    apply (rule normalized_msetprodI)
1.618 +    apply (auto elim: factorizationE)
1.619 +    done
1.620 +  show "lcm a b = normalize (a * b) div gcd a b"
1.621 +    by (auto elim!: factorizationE simp add: gcd_unfold lcm_unfold normalize_mult
1.622 +      union_diff_inter_eq_sup [symmetric] msetprod_diff inter_subset_eq_union)
1.623 +qed
1.624 +
1.625  end
1.626
1.627  instantiation nat :: factorial_semiring
```