src/HOL/Computational_Algebra/Factorial_Ring.thy
author wenzelm
Wed Nov 01 20:46:23 2017 +0100 (22 months ago)
changeset 66983 df83b66f1d94
parent 66938 c78ff0aeba4c
child 67051 e7e54a0b9197
permissions -rw-r--r--
proper merge (amending fb46c031c841);
wenzelm@65435
     1
(*  Title:      HOL/Computational_Algebra/Factorial_Ring.thy
haftmann@63924
     2
    Author:     Manuel Eberl, TU Muenchen
haftmann@60804
     3
    Author:     Florian Haftmann, TU Muenchen
haftmann@60804
     4
*)
haftmann@60804
     5
haftmann@60804
     6
section \<open>Factorial (semi)rings\<close>
haftmann@60804
     7
haftmann@60804
     8
theory Factorial_Ring
wenzelm@65552
     9
imports
eberlm@63534
    10
  Main
wenzelm@66453
    11
  "HOL-Library.Multiset"
eberlm@63498
    12
begin
eberlm@63498
    13
haftmann@63924
    14
subsection \<open>Irreducible and prime elements\<close>
eberlm@63498
    15
eberlm@66276
    16
(* TODO: Move ? *)
eberlm@66276
    17
lemma (in semiring_gcd) prod_coprime' [rule_format]: 
eberlm@66276
    18
    "(\<forall>i\<in>A. gcd a (f i) = 1) \<longrightarrow> gcd a (\<Prod>i\<in>A. f i) = 1"
eberlm@66276
    19
  using prod_coprime[of A f a] by (simp add: gcd.commute)
eberlm@66276
    20
eberlm@66276
    21
eberlm@63498
    22
context comm_semiring_1
haftmann@62499
    23
begin
haftmann@62499
    24
eberlm@63498
    25
definition irreducible :: "'a \<Rightarrow> bool" where
eberlm@63498
    26
  "irreducible p \<longleftrightarrow> p \<noteq> 0 \<and> \<not>p dvd 1 \<and> (\<forall>a b. p = a * b \<longrightarrow> a dvd 1 \<or> b dvd 1)"
eberlm@63498
    27
eberlm@63498
    28
lemma not_irreducible_zero [simp]: "\<not>irreducible 0"
eberlm@63498
    29
  by (simp add: irreducible_def)
eberlm@63498
    30
eberlm@63498
    31
lemma irreducible_not_unit: "irreducible p \<Longrightarrow> \<not>p dvd 1"
eberlm@63498
    32
  by (simp add: irreducible_def)
eberlm@63498
    33
eberlm@63498
    34
lemma not_irreducible_one [simp]: "\<not>irreducible 1"
eberlm@63498
    35
  by (simp add: irreducible_def)
eberlm@63498
    36
eberlm@63498
    37
lemma irreducibleI:
eberlm@63498
    38
  "p \<noteq> 0 \<Longrightarrow> \<not>p dvd 1 \<Longrightarrow> (\<And>a b. p = a * b \<Longrightarrow> a dvd 1 \<or> b dvd 1) \<Longrightarrow> irreducible p"
eberlm@63498
    39
  by (simp add: irreducible_def)
eberlm@63498
    40
eberlm@63498
    41
lemma irreducibleD: "irreducible p \<Longrightarrow> p = a * b \<Longrightarrow> a dvd 1 \<or> b dvd 1"
eberlm@63498
    42
  by (simp add: irreducible_def)
eberlm@63498
    43
eberlm@63633
    44
definition prime_elem :: "'a \<Rightarrow> bool" where
eberlm@63633
    45
  "prime_elem p \<longleftrightarrow> p \<noteq> 0 \<and> \<not>p dvd 1 \<and> (\<forall>a b. p dvd (a * b) \<longrightarrow> p dvd a \<or> p dvd b)"
eberlm@63498
    46
eberlm@63633
    47
lemma not_prime_elem_zero [simp]: "\<not>prime_elem 0"
eberlm@63633
    48
  by (simp add: prime_elem_def)
eberlm@63498
    49
eberlm@63633
    50
lemma prime_elem_not_unit: "prime_elem p \<Longrightarrow> \<not>p dvd 1"
eberlm@63633
    51
  by (simp add: prime_elem_def)
eberlm@63498
    52
eberlm@63633
    53
lemma prime_elemI:
eberlm@63633
    54
    "p \<noteq> 0 \<Longrightarrow> \<not>p dvd 1 \<Longrightarrow> (\<And>a b. p dvd (a * b) \<Longrightarrow> p dvd a \<or> p dvd b) \<Longrightarrow> prime_elem p"
eberlm@63633
    55
  by (simp add: prime_elem_def)
eberlm@63498
    56
eberlm@63633
    57
lemma prime_elem_dvd_multD:
eberlm@63633
    58
    "prime_elem p \<Longrightarrow> p dvd (a * b) \<Longrightarrow> p dvd a \<or> p dvd b"
eberlm@63633
    59
  by (simp add: prime_elem_def)
eberlm@63498
    60
eberlm@63633
    61
lemma prime_elem_dvd_mult_iff:
eberlm@63633
    62
  "prime_elem p \<Longrightarrow> p dvd (a * b) \<longleftrightarrow> p dvd a \<or> p dvd b"
eberlm@63633
    63
  by (auto simp: prime_elem_def)
eberlm@63498
    64
eberlm@63633
    65
lemma not_prime_elem_one [simp]:
eberlm@63633
    66
  "\<not> prime_elem 1"
eberlm@63633
    67
  by (auto dest: prime_elem_not_unit)
eberlm@63498
    68
eberlm@63633
    69
lemma prime_elem_not_zeroI:
eberlm@63633
    70
  assumes "prime_elem p"
eberlm@63498
    71
  shows "p \<noteq> 0"
eberlm@63498
    72
  using assms by (auto intro: ccontr)
eberlm@63498
    73
wenzelm@65552
    74
lemma prime_elem_dvd_power:
eberlm@63633
    75
  "prime_elem p \<Longrightarrow> p dvd x ^ n \<Longrightarrow> p dvd x"
eberlm@63633
    76
  by (induction n) (auto dest: prime_elem_dvd_multD intro: dvd_trans[of _ 1])
eberlm@63534
    77
eberlm@63633
    78
lemma prime_elem_dvd_power_iff:
eberlm@63633
    79
  "prime_elem p \<Longrightarrow> n > 0 \<Longrightarrow> p dvd x ^ n \<longleftrightarrow> p dvd x"
eberlm@63633
    80
  by (auto dest: prime_elem_dvd_power intro: dvd_trans)
eberlm@63534
    81
eberlm@63633
    82
lemma prime_elem_imp_nonzero [simp]:
eberlm@63633
    83
  "ASSUMPTION (prime_elem x) \<Longrightarrow> x \<noteq> 0"
eberlm@63633
    84
  unfolding ASSUMPTION_def by (rule prime_elem_not_zeroI)
eberlm@63498
    85
eberlm@63633
    86
lemma prime_elem_imp_not_one [simp]:
eberlm@63633
    87
  "ASSUMPTION (prime_elem x) \<Longrightarrow> x \<noteq> 1"
eberlm@63498
    88
  unfolding ASSUMPTION_def by auto
eberlm@63498
    89
eberlm@63498
    90
end
eberlm@63498
    91
haftmann@62499
    92
context algebraic_semidom
haftmann@60804
    93
begin
haftmann@60804
    94
eberlm@63633
    95
lemma prime_elem_imp_irreducible:
eberlm@63633
    96
  assumes "prime_elem p"
eberlm@63498
    97
  shows   "irreducible p"
eberlm@63498
    98
proof (rule irreducibleI)
eberlm@63498
    99
  fix a b
eberlm@63498
   100
  assume p_eq: "p = a * b"
eberlm@63498
   101
  with assms have nz: "a \<noteq> 0" "b \<noteq> 0" by auto
eberlm@63498
   102
  from p_eq have "p dvd a * b" by simp
eberlm@63633
   103
  with \<open>prime_elem p\<close> have "p dvd a \<or> p dvd b" by (rule prime_elem_dvd_multD)
eberlm@63498
   104
  with \<open>p = a * b\<close> have "a * b dvd 1 * b \<or> a * b dvd a * 1" by auto
eberlm@63498
   105
  thus "a dvd 1 \<or> b dvd 1"
eberlm@63498
   106
    by (simp only: dvd_times_left_cancel_iff[OF nz(1)] dvd_times_right_cancel_iff[OF nz(2)])
eberlm@63633
   107
qed (insert assms, simp_all add: prime_elem_def)
eberlm@63498
   108
haftmann@63924
   109
lemma (in algebraic_semidom) unit_imp_no_irreducible_divisors:
haftmann@63924
   110
  assumes "is_unit x" "irreducible p"
haftmann@63924
   111
  shows   "\<not>p dvd x"
haftmann@63924
   112
proof (rule notI)
haftmann@63924
   113
  assume "p dvd x"
haftmann@63924
   114
  with \<open>is_unit x\<close> have "is_unit p"
haftmann@63924
   115
    by (auto intro: dvd_trans)
haftmann@63924
   116
  with \<open>irreducible p\<close> show False
haftmann@63924
   117
    by (simp add: irreducible_not_unit)
haftmann@63924
   118
qed
wenzelm@65552
   119
haftmann@63924
   120
lemma unit_imp_no_prime_divisors:
haftmann@63924
   121
  assumes "is_unit x" "prime_elem p"
haftmann@63924
   122
  shows   "\<not>p dvd x"
haftmann@63924
   123
  using unit_imp_no_irreducible_divisors[OF assms(1) prime_elem_imp_irreducible[OF assms(2)]] .
haftmann@63924
   124
eberlm@63633
   125
lemma prime_elem_mono:
eberlm@63633
   126
  assumes "prime_elem p" "\<not>q dvd 1" "q dvd p"
eberlm@63633
   127
  shows   "prime_elem q"
eberlm@63498
   128
proof -
eberlm@63498
   129
  from \<open>q dvd p\<close> obtain r where r: "p = q * r" by (elim dvdE)
eberlm@63498
   130
  hence "p dvd q * r" by simp
eberlm@63633
   131
  with \<open>prime_elem p\<close> have "p dvd q \<or> p dvd r" by (rule prime_elem_dvd_multD)
eberlm@63498
   132
  hence "p dvd q"
eberlm@63498
   133
  proof
eberlm@63498
   134
    assume "p dvd r"
eberlm@63498
   135
    then obtain s where s: "r = p * s" by (elim dvdE)
eberlm@63498
   136
    from r have "p * 1 = p * (q * s)" by (subst (asm) s) (simp add: mult_ac)
eberlm@63633
   137
    with \<open>prime_elem p\<close> have "q dvd 1"
eberlm@63498
   138
      by (subst (asm) mult_cancel_left) auto
eberlm@63498
   139
    with \<open>\<not>q dvd 1\<close> show ?thesis by contradiction
eberlm@63498
   140
  qed
eberlm@63498
   141
eberlm@63498
   142
  show ?thesis
eberlm@63633
   143
  proof (rule prime_elemI)
eberlm@63498
   144
    fix a b assume "q dvd (a * b)"
eberlm@63498
   145
    with \<open>p dvd q\<close> have "p dvd (a * b)" by (rule dvd_trans)
eberlm@63633
   146
    with \<open>prime_elem p\<close> have "p dvd a \<or> p dvd b" by (rule prime_elem_dvd_multD)
eberlm@63498
   147
    with \<open>q dvd p\<close> show "q dvd a \<or> q dvd b" by (blast intro: dvd_trans)
eberlm@63498
   148
  qed (insert assms, auto)
haftmann@62499
   149
qed
haftmann@62499
   150
eberlm@63498
   151
lemma irreducibleD':
eberlm@63498
   152
  assumes "irreducible a" "b dvd a"
eberlm@63498
   153
  shows   "a dvd b \<or> is_unit b"
eberlm@63498
   154
proof -
eberlm@63498
   155
  from assms obtain c where c: "a = b * c" by (elim dvdE)
eberlm@63498
   156
  from irreducibleD[OF assms(1) this] have "is_unit b \<or> is_unit c" .
eberlm@63498
   157
  thus ?thesis by (auto simp: c mult_unit_dvd_iff)
eberlm@63498
   158
qed
haftmann@60804
   159
eberlm@63498
   160
lemma irreducibleI':
eberlm@63498
   161
  assumes "a \<noteq> 0" "\<not>is_unit a" "\<And>b. b dvd a \<Longrightarrow> a dvd b \<or> is_unit b"
eberlm@63498
   162
  shows   "irreducible a"
eberlm@63498
   163
proof (rule irreducibleI)
eberlm@63498
   164
  fix b c assume a_eq: "a = b * c"
eberlm@63498
   165
  hence "a dvd b \<or> is_unit b" by (intro assms) simp_all
eberlm@63498
   166
  thus "is_unit b \<or> is_unit c"
eberlm@63498
   167
  proof
eberlm@63498
   168
    assume "a dvd b"
eberlm@63498
   169
    hence "b * c dvd b * 1" by (simp add: a_eq)
eberlm@63498
   170
    moreover from \<open>a \<noteq> 0\<close> a_eq have "b \<noteq> 0" by auto
eberlm@63498
   171
    ultimately show ?thesis by (subst (asm) dvd_times_left_cancel_iff) auto
eberlm@63498
   172
  qed blast
eberlm@63498
   173
qed (simp_all add: assms(1,2))
haftmann@60804
   174
eberlm@63498
   175
lemma irreducible_altdef:
eberlm@63498
   176
  "irreducible x \<longleftrightarrow> x \<noteq> 0 \<and> \<not>is_unit x \<and> (\<forall>b. b dvd x \<longrightarrow> x dvd b \<or> is_unit b)"
eberlm@63498
   177
  using irreducibleI'[of x] irreducibleD'[of x] irreducible_not_unit[of x] by auto
haftmann@60804
   178
eberlm@63633
   179
lemma prime_elem_multD:
eberlm@63633
   180
  assumes "prime_elem (a * b)"
haftmann@60804
   181
  shows "is_unit a \<or> is_unit b"
haftmann@60804
   182
proof -
eberlm@63633
   183
  from assms have "a \<noteq> 0" "b \<noteq> 0" by (auto dest!: prime_elem_not_zeroI)
eberlm@63633
   184
  moreover from assms prime_elem_dvd_multD [of "a * b"] have "a * b dvd a \<or> a * b dvd b"
haftmann@60804
   185
    by auto
haftmann@60804
   186
  ultimately show ?thesis
haftmann@60804
   187
    using dvd_times_left_cancel_iff [of a b 1]
haftmann@60804
   188
      dvd_times_right_cancel_iff [of b a 1]
haftmann@60804
   189
    by auto
haftmann@60804
   190
qed
haftmann@60804
   191
eberlm@63633
   192
lemma prime_elemD2:
eberlm@63633
   193
  assumes "prime_elem p" and "a dvd p" and "\<not> is_unit a"
haftmann@60804
   194
  shows "p dvd a"
haftmann@60804
   195
proof -
haftmann@60804
   196
  from \<open>a dvd p\<close> obtain b where "p = a * b" ..
eberlm@63633
   197
  with \<open>prime_elem p\<close> prime_elem_multD \<open>\<not> is_unit a\<close> have "is_unit b" by auto
haftmann@60804
   198
  with \<open>p = a * b\<close> show ?thesis
haftmann@60804
   199
    by (auto simp add: mult_unit_dvd_iff)
haftmann@60804
   200
qed
haftmann@60804
   201
nipkow@63830
   202
lemma prime_elem_dvd_prod_msetE:
eberlm@63633
   203
  assumes "prime_elem p"
nipkow@63830
   204
  assumes dvd: "p dvd prod_mset A"
eberlm@63633
   205
  obtains a where "a \<in># A" and "p dvd a"
eberlm@63633
   206
proof -
eberlm@63633
   207
  from dvd have "\<exists>a. a \<in># A \<and> p dvd a"
eberlm@63633
   208
  proof (induct A)
eberlm@63633
   209
    case empty then show ?case
eberlm@63633
   210
    using \<open>prime_elem p\<close> by (simp add: prime_elem_not_unit)
eberlm@63633
   211
  next
Mathias@63793
   212
    case (add a A)
nipkow@63830
   213
    then have "p dvd a * prod_mset A" by simp
nipkow@63830
   214
    with \<open>prime_elem p\<close> consider (A) "p dvd prod_mset A" | (B) "p dvd a"
eberlm@63633
   215
      by (blast dest: prime_elem_dvd_multD)
eberlm@63633
   216
    then show ?case proof cases
eberlm@63633
   217
      case B then show ?thesis by auto
eberlm@63633
   218
    next
eberlm@63633
   219
      case A
eberlm@63633
   220
      with add.hyps obtain b where "b \<in># A" "p dvd b"
eberlm@63633
   221
        by auto
eberlm@63633
   222
      then show ?thesis by auto
eberlm@63633
   223
    qed
eberlm@63633
   224
  qed
eberlm@63633
   225
  with that show thesis by blast
eberlm@66276
   226
eberlm@63633
   227
qed
eberlm@63633
   228
eberlm@63534
   229
context
eberlm@63534
   230
begin
eberlm@63534
   231
eberlm@63633
   232
private lemma prime_elem_powerD:
eberlm@63633
   233
  assumes "prime_elem (p ^ n)"
eberlm@63633
   234
  shows   "prime_elem p \<and> n = 1"
eberlm@63534
   235
proof (cases n)
eberlm@63534
   236
  case (Suc m)
eberlm@63534
   237
  note assms
eberlm@63534
   238
  also from Suc have "p ^ n = p * p^m" by simp
eberlm@63633
   239
  finally have "is_unit p \<or> is_unit (p^m)" by (rule prime_elem_multD)
eberlm@63633
   240
  moreover from assms have "\<not>is_unit p" by (simp add: prime_elem_def is_unit_power_iff)
eberlm@63534
   241
  ultimately have "is_unit (p ^ m)" by simp
eberlm@63534
   242
  with \<open>\<not>is_unit p\<close> have "m = 0" by (simp add: is_unit_power_iff)
eberlm@63534
   243
  with Suc assms show ?thesis by simp
eberlm@63534
   244
qed (insert assms, simp_all)
eberlm@63534
   245
eberlm@63633
   246
lemma prime_elem_power_iff:
eberlm@63633
   247
  "prime_elem (p ^ n) \<longleftrightarrow> prime_elem p \<and> n = 1"
eberlm@63633
   248
  by (auto dest: prime_elem_powerD)
eberlm@63534
   249
eberlm@63534
   250
end
eberlm@63534
   251
eberlm@63498
   252
lemma irreducible_mult_unit_left:
eberlm@63498
   253
  "is_unit a \<Longrightarrow> irreducible (a * p) \<longleftrightarrow> irreducible p"
eberlm@63498
   254
  by (auto simp: irreducible_altdef mult.commute[of a] is_unit_mult_iff
eberlm@63498
   255
        mult_unit_dvd_iff dvd_mult_unit_iff)
eberlm@63498
   256
eberlm@63633
   257
lemma prime_elem_mult_unit_left:
eberlm@63633
   258
  "is_unit a \<Longrightarrow> prime_elem (a * p) \<longleftrightarrow> prime_elem p"
eberlm@63633
   259
  by (auto simp: prime_elem_def mult.commute[of a] is_unit_mult_iff mult_unit_dvd_iff)
eberlm@63498
   260
eberlm@63633
   261
lemma prime_elem_dvd_cases:
eberlm@63633
   262
  assumes pk: "p*k dvd m*n" and p: "prime_elem p"
eberlm@63537
   263
  shows "(\<exists>x. k dvd x*n \<and> m = p*x) \<or> (\<exists>y. k dvd m*y \<and> n = p*y)"
eberlm@63537
   264
proof -
eberlm@63537
   265
  have "p dvd m*n" using dvd_mult_left pk by blast
eberlm@63537
   266
  then consider "p dvd m" | "p dvd n"
eberlm@63633
   267
    using p prime_elem_dvd_mult_iff by blast
eberlm@63537
   268
  then show ?thesis
eberlm@63537
   269
  proof cases
wenzelm@65552
   270
    case 1 then obtain a where "m = p * a" by (metis dvd_mult_div_cancel)
eberlm@63537
   271
      then have "\<exists>x. k dvd x * n \<and> m = p * x"
eberlm@63537
   272
        using p pk by (auto simp: mult.assoc)
eberlm@63537
   273
    then show ?thesis ..
eberlm@63537
   274
  next
wenzelm@65552
   275
    case 2 then obtain b where "n = p * b" by (metis dvd_mult_div_cancel)
wenzelm@65552
   276
    with p pk have "\<exists>y. k dvd m*y \<and> n = p*y"
eberlm@63537
   277
      by (metis dvd_mult_right dvd_times_left_cancel_iff mult.left_commute mult_zero_left)
eberlm@63537
   278
    then show ?thesis ..
eberlm@63537
   279
  qed
eberlm@63537
   280
qed
eberlm@63537
   281
eberlm@63633
   282
lemma prime_elem_power_dvd_prod:
eberlm@63633
   283
  assumes pc: "p^c dvd m*n" and p: "prime_elem p"
eberlm@63537
   284
  shows "\<exists>a b. a+b = c \<and> p^a dvd m \<and> p^b dvd n"
eberlm@63537
   285
using pc
eberlm@63537
   286
proof (induct c arbitrary: m n)
eberlm@63537
   287
  case 0 show ?case by simp
eberlm@63537
   288
next
eberlm@63537
   289
  case (Suc c)
eberlm@63537
   290
  consider x where "p^c dvd x*n" "m = p*x" | y where "p^c dvd m*y" "n = p*y"
eberlm@63633
   291
    using prime_elem_dvd_cases [of _ "p^c", OF _ p] Suc.prems by force
eberlm@63537
   292
  then show ?case
eberlm@63537
   293
  proof cases
wenzelm@65552
   294
    case (1 x)
eberlm@63537
   295
    with Suc.hyps[of x n] obtain a b where "a + b = c \<and> p ^ a dvd x \<and> p ^ b dvd n" by blast
eberlm@63537
   296
    with 1 have "Suc a + b = Suc c \<and> p ^ Suc a dvd m \<and> p ^ b dvd n"
eberlm@63537
   297
      by (auto intro: mult_dvd_mono)
eberlm@63537
   298
    thus ?thesis by blast
eberlm@63537
   299
  next
wenzelm@65552
   300
    case (2 y)
eberlm@63537
   301
    with Suc.hyps[of m y] obtain a b where "a + b = c \<and> p ^ a dvd m \<and> p ^ b dvd y" by blast
eberlm@63537
   302
    with 2 have "a + Suc b = Suc c \<and> p ^ a dvd m \<and> p ^ Suc b dvd n"
eberlm@63537
   303
      by (auto intro: mult_dvd_mono)
eberlm@63537
   304
    with Suc.hyps [of m y] show "\<exists>a b. a + b = Suc c \<and> p ^ a dvd m \<and> p ^ b dvd n"
eberlm@63537
   305
      by force
eberlm@63537
   306
  qed
eberlm@63537
   307
qed
eberlm@63537
   308
eberlm@63633
   309
lemma prime_elem_power_dvd_cases:
haftmann@63924
   310
  assumes "p ^ c dvd m * n" and "a + b = Suc c" and "prime_elem p"
haftmann@63924
   311
  shows "p ^ a dvd m \<or> p ^ b dvd n"
haftmann@63924
   312
proof -
haftmann@63924
   313
  from assms obtain r s
haftmann@63924
   314
    where "r + s = c \<and> p ^ r dvd m \<and> p ^ s dvd n"
haftmann@63924
   315
    by (blast dest: prime_elem_power_dvd_prod)
haftmann@63924
   316
  moreover with assms have
haftmann@63924
   317
    "a \<le> r \<or> b \<le> s" by arith
haftmann@63924
   318
  ultimately show ?thesis by (auto intro: power_le_dvd)
haftmann@63924
   319
qed
eberlm@63534
   320
eberlm@63633
   321
lemma prime_elem_not_unit' [simp]:
eberlm@63633
   322
  "ASSUMPTION (prime_elem x) \<Longrightarrow> \<not>is_unit x"
eberlm@63633
   323
  unfolding ASSUMPTION_def by (rule prime_elem_not_unit)
eberlm@63498
   324
eberlm@63633
   325
lemma prime_elem_dvd_power_iff:
eberlm@63633
   326
  assumes "prime_elem p"
haftmann@62499
   327
  shows "p dvd a ^ n \<longleftrightarrow> p dvd a \<and> n > 0"
eberlm@63633
   328
  using assms by (induct n) (auto dest: prime_elem_not_unit prime_elem_dvd_multD)
haftmann@62499
   329
haftmann@62499
   330
lemma prime_power_dvd_multD:
eberlm@63633
   331
  assumes "prime_elem p"
haftmann@62499
   332
  assumes "p ^ n dvd a * b" and "n > 0" and "\<not> p dvd a"
haftmann@62499
   333
  shows "p ^ n dvd b"
wenzelm@65552
   334
  using \<open>p ^ n dvd a * b\<close> and \<open>n > 0\<close>
eberlm@63633
   335
proof (induct n arbitrary: b)
haftmann@62499
   336
  case 0 then show ?case by simp
haftmann@62499
   337
next
haftmann@62499
   338
  case (Suc n) show ?case
haftmann@62499
   339
  proof (cases "n = 0")
eberlm@63633
   340
    case True with Suc \<open>prime_elem p\<close> \<open>\<not> p dvd a\<close> show ?thesis
eberlm@63633
   341
      by (simp add: prime_elem_dvd_mult_iff)
haftmann@62499
   342
  next
haftmann@62499
   343
    case False then have "n > 0" by simp
eberlm@63633
   344
    from \<open>prime_elem p\<close> have "p \<noteq> 0" by auto
haftmann@62499
   345
    from Suc.prems have *: "p * p ^ n dvd a * b"
haftmann@62499
   346
      by simp
haftmann@62499
   347
    then have "p dvd a * b"
haftmann@62499
   348
      by (rule dvd_mult_left)
eberlm@63633
   349
    with Suc \<open>prime_elem p\<close> \<open>\<not> p dvd a\<close> have "p dvd b"
eberlm@63633
   350
      by (simp add: prime_elem_dvd_mult_iff)
wenzelm@63040
   351
    moreover define c where "c = b div p"
haftmann@62499
   352
    ultimately have b: "b = p * c" by simp
haftmann@62499
   353
    with * have "p * p ^ n dvd p * (a * c)"
haftmann@62499
   354
      by (simp add: ac_simps)
haftmann@62499
   355
    with \<open>p \<noteq> 0\<close> have "p ^ n dvd a * c"
haftmann@62499
   356
      by simp
haftmann@62499
   357
    with Suc.hyps \<open>n > 0\<close> have "p ^ n dvd c"
haftmann@62499
   358
      by blast
haftmann@62499
   359
    with \<open>p \<noteq> 0\<close> show ?thesis
haftmann@62499
   360
      by (simp add: b)
haftmann@62499
   361
  qed
haftmann@62499
   362
qed
haftmann@62499
   363
eberlm@63633
   364
end
eberlm@63633
   365
haftmann@63924
   366
haftmann@63924
   367
subsection \<open>Generalized primes: normalized prime elements\<close>
haftmann@63924
   368
eberlm@63633
   369
context normalization_semidom
eberlm@63633
   370
begin
eberlm@63633
   371
haftmann@63924
   372
lemma irreducible_normalized_divisors:
haftmann@63924
   373
  assumes "irreducible x" "y dvd x" "normalize y = y"
haftmann@63924
   374
  shows   "y = 1 \<or> y = normalize x"
haftmann@63924
   375
proof -
haftmann@63924
   376
  from assms have "is_unit y \<or> x dvd y" by (auto simp: irreducible_altdef)
haftmann@63924
   377
  thus ?thesis
haftmann@63924
   378
  proof (elim disjE)
haftmann@63924
   379
    assume "is_unit y"
haftmann@63924
   380
    hence "normalize y = 1" by (simp add: is_unit_normalize)
haftmann@63924
   381
    with assms show ?thesis by simp
haftmann@63924
   382
  next
haftmann@63924
   383
    assume "x dvd y"
haftmann@63924
   384
    with \<open>y dvd x\<close> have "normalize y = normalize x" by (rule associatedI)
haftmann@63924
   385
    with assms show ?thesis by simp
haftmann@63924
   386
  qed
haftmann@63924
   387
qed
haftmann@63924
   388
eberlm@63633
   389
lemma irreducible_normalize_iff [simp]: "irreducible (normalize x) = irreducible x"
eberlm@63633
   390
  using irreducible_mult_unit_left[of "1 div unit_factor x" x]
eberlm@63633
   391
  by (cases "x = 0") (simp_all add: unit_div_commute)
eberlm@63633
   392
eberlm@63633
   393
lemma prime_elem_normalize_iff [simp]: "prime_elem (normalize x) = prime_elem x"
eberlm@63633
   394
  using prime_elem_mult_unit_left[of "1 div unit_factor x" x]
eberlm@63633
   395
  by (cases "x = 0") (simp_all add: unit_div_commute)
eberlm@63633
   396
eberlm@63633
   397
lemma prime_elem_associated:
eberlm@63633
   398
  assumes "prime_elem p" and "prime_elem q" and "q dvd p"
eberlm@63633
   399
  shows "normalize q = normalize p"
eberlm@63633
   400
using \<open>q dvd p\<close> proof (rule associatedI)
eberlm@63633
   401
  from \<open>prime_elem q\<close> have "\<not> is_unit q"
eberlm@63633
   402
    by (auto simp add: prime_elem_not_unit)
eberlm@63633
   403
  with \<open>prime_elem p\<close> \<open>q dvd p\<close> show "p dvd q"
eberlm@63633
   404
    by (blast intro: prime_elemD2)
eberlm@63633
   405
qed
eberlm@63633
   406
eberlm@63633
   407
definition prime :: "'a \<Rightarrow> bool" where
eberlm@63633
   408
  "prime p \<longleftrightarrow> prime_elem p \<and> normalize p = p"
eberlm@63633
   409
eberlm@63633
   410
lemma not_prime_0 [simp]: "\<not>prime 0" by (simp add: prime_def)
eberlm@63633
   411
eberlm@63633
   412
lemma not_prime_unit: "is_unit x \<Longrightarrow> \<not>prime x"
eberlm@63633
   413
  using prime_elem_not_unit[of x] by (auto simp add: prime_def)
eberlm@63633
   414
eberlm@63633
   415
lemma not_prime_1 [simp]: "\<not>prime 1" by (simp add: not_prime_unit)
eberlm@63633
   416
eberlm@63633
   417
lemma primeI: "prime_elem x \<Longrightarrow> normalize x = x \<Longrightarrow> prime x"
eberlm@63633
   418
  by (simp add: prime_def)
eberlm@63633
   419
eberlm@63633
   420
lemma prime_imp_prime_elem [dest]: "prime p \<Longrightarrow> prime_elem p"
eberlm@63633
   421
  by (simp add: prime_def)
eberlm@63633
   422
eberlm@63633
   423
lemma normalize_prime: "prime p \<Longrightarrow> normalize p = p"
eberlm@63633
   424
  by (simp add: prime_def)
eberlm@63633
   425
eberlm@63633
   426
lemma prime_normalize_iff [simp]: "prime (normalize p) \<longleftrightarrow> prime_elem p"
eberlm@63633
   427
  by (auto simp add: prime_def)
eberlm@63633
   428
eberlm@63633
   429
lemma prime_power_iff:
eberlm@63633
   430
  "prime (p ^ n) \<longleftrightarrow> prime p \<and> n = 1"
eberlm@63633
   431
  by (auto simp: prime_def prime_elem_power_iff)
eberlm@63633
   432
eberlm@63633
   433
lemma prime_imp_nonzero [simp]:
eberlm@63633
   434
  "ASSUMPTION (prime x) \<Longrightarrow> x \<noteq> 0"
eberlm@63633
   435
  unfolding ASSUMPTION_def prime_def by auto
eberlm@63633
   436
eberlm@63633
   437
lemma prime_imp_not_one [simp]:
eberlm@63633
   438
  "ASSUMPTION (prime x) \<Longrightarrow> x \<noteq> 1"
eberlm@63633
   439
  unfolding ASSUMPTION_def by auto
eberlm@63633
   440
eberlm@63633
   441
lemma prime_not_unit' [simp]:
eberlm@63633
   442
  "ASSUMPTION (prime x) \<Longrightarrow> \<not>is_unit x"
eberlm@63633
   443
  unfolding ASSUMPTION_def prime_def by auto
eberlm@63633
   444
eberlm@63633
   445
lemma prime_normalize' [simp]: "ASSUMPTION (prime x) \<Longrightarrow> normalize x = x"
eberlm@63633
   446
  unfolding ASSUMPTION_def prime_def by simp
eberlm@63633
   447
eberlm@63633
   448
lemma unit_factor_prime: "prime x \<Longrightarrow> unit_factor x = 1"
eberlm@63633
   449
  using unit_factor_normalize[of x] unfolding prime_def by auto
eberlm@63633
   450
eberlm@63633
   451
lemma unit_factor_prime' [simp]: "ASSUMPTION (prime x) \<Longrightarrow> unit_factor x = 1"
eberlm@63633
   452
  unfolding ASSUMPTION_def by (rule unit_factor_prime)
eberlm@63633
   453
eberlm@63633
   454
lemma prime_imp_prime_elem' [simp]: "ASSUMPTION (prime x) \<Longrightarrow> prime_elem x"
eberlm@63633
   455
  by (simp add: prime_def ASSUMPTION_def)
eberlm@63633
   456
eberlm@63633
   457
lemma prime_dvd_multD: "prime p \<Longrightarrow> p dvd a * b \<Longrightarrow> p dvd a \<or> p dvd b"
eberlm@63633
   458
  by (intro prime_elem_dvd_multD) simp_all
eberlm@63633
   459
haftmann@64631
   460
lemma prime_dvd_mult_iff: "prime p \<Longrightarrow> p dvd a * b \<longleftrightarrow> p dvd a \<or> p dvd b"
eberlm@63633
   461
  by (auto dest: prime_dvd_multD)
eberlm@63633
   462
wenzelm@65552
   463
lemma prime_dvd_power:
eberlm@63633
   464
  "prime p \<Longrightarrow> p dvd x ^ n \<Longrightarrow> p dvd x"
eberlm@63633
   465
  by (auto dest!: prime_elem_dvd_power simp: prime_def)
eberlm@63633
   466
eberlm@63633
   467
lemma prime_dvd_power_iff:
eberlm@63633
   468
  "prime p \<Longrightarrow> n > 0 \<Longrightarrow> p dvd x ^ n \<longleftrightarrow> p dvd x"
eberlm@63633
   469
  by (subst prime_elem_dvd_power_iff) simp_all
eberlm@63633
   470
nipkow@63830
   471
lemma prime_dvd_prod_mset_iff: "prime p \<Longrightarrow> p dvd prod_mset A \<longleftrightarrow> (\<exists>x. x \<in># A \<and> p dvd x)"
eberlm@63633
   472
  by (induction A) (simp_all add: prime_elem_dvd_mult_iff prime_imp_prime_elem, blast+)
eberlm@63633
   473
eberlm@66276
   474
lemma prime_dvd_prod_iff: "finite A \<Longrightarrow> prime p \<Longrightarrow> p dvd prod f A \<longleftrightarrow> (\<exists>x\<in>A. p dvd f x)"
eberlm@66276
   475
  by (auto simp: prime_dvd_prod_mset_iff prod_unfold_prod_mset)
eberlm@66276
   476
eberlm@63633
   477
lemma primes_dvd_imp_eq:
eberlm@63633
   478
  assumes "prime p" "prime q" "p dvd q"
eberlm@63633
   479
  shows   "p = q"
eberlm@63633
   480
proof -
eberlm@63633
   481
  from assms have "irreducible q" by (simp add: prime_elem_imp_irreducible prime_def)
eberlm@63633
   482
  from irreducibleD'[OF this \<open>p dvd q\<close>] assms have "q dvd p" by simp
eberlm@63633
   483
  with \<open>p dvd q\<close> have "normalize p = normalize q" by (rule associatedI)
eberlm@63633
   484
  with assms show "p = q" by simp
eberlm@63633
   485
qed
eberlm@63633
   486
nipkow@63830
   487
lemma prime_dvd_prod_mset_primes_iff:
eberlm@63633
   488
  assumes "prime p" "\<And>q. q \<in># A \<Longrightarrow> prime q"
nipkow@63830
   489
  shows   "p dvd prod_mset A \<longleftrightarrow> p \<in># A"
eberlm@63633
   490
proof -
nipkow@63830
   491
  from assms(1) have "p dvd prod_mset A \<longleftrightarrow> (\<exists>x. x \<in># A \<and> p dvd x)" by (rule prime_dvd_prod_mset_iff)
eberlm@63633
   492
  also from assms have "\<dots> \<longleftrightarrow> p \<in># A" by (auto dest: primes_dvd_imp_eq)
eberlm@63633
   493
  finally show ?thesis .
eberlm@63633
   494
qed
eberlm@63633
   495
nipkow@63830
   496
lemma prod_mset_primes_dvd_imp_subset:
nipkow@63830
   497
  assumes "prod_mset A dvd prod_mset B" "\<And>p. p \<in># A \<Longrightarrow> prime p" "\<And>p. p \<in># B \<Longrightarrow> prime p"
eberlm@63633
   498
  shows   "A \<subseteq># B"
eberlm@63633
   499
using assms
eberlm@63633
   500
proof (induction A arbitrary: B)
eberlm@63633
   501
  case empty
eberlm@63633
   502
  thus ?case by simp
eberlm@63633
   503
next
Mathias@63793
   504
  case (add p A B)
eberlm@63633
   505
  hence p: "prime p" by simp
eberlm@63633
   506
  define B' where "B' = B - {#p#}"
nipkow@63830
   507
  from add.prems have "p dvd prod_mset B" by (simp add: dvd_mult_left)
eberlm@63633
   508
  with add.prems have "p \<in># B"
nipkow@63830
   509
    by (subst (asm) (2) prime_dvd_prod_mset_primes_iff) simp_all
eberlm@63633
   510
  hence B: "B = B' + {#p#}" by (simp add: B'_def)
eberlm@63633
   511
  from add.prems p have "A \<subseteq># B'" by (intro add.IH) (simp_all add: B)
eberlm@63633
   512
  thus ?case by (simp add: B)
eberlm@63633
   513
qed
eberlm@63633
   514
nipkow@63830
   515
lemma normalize_prod_mset_primes:
nipkow@63830
   516
  "(\<And>p. p \<in># A \<Longrightarrow> prime p) \<Longrightarrow> normalize (prod_mset A) = prod_mset A"
eberlm@63633
   517
proof (induction A)
Mathias@63793
   518
  case (add p A)
eberlm@63633
   519
  hence "prime p" by simp
eberlm@63633
   520
  hence "normalize p = p" by simp
eberlm@63633
   521
  with add show ?case by (simp add: normalize_mult)
eberlm@63633
   522
qed simp_all
eberlm@63633
   523
nipkow@63830
   524
lemma prod_mset_dvd_prod_mset_primes_iff:
eberlm@63633
   525
  assumes "\<And>x. x \<in># A \<Longrightarrow> prime x" "\<And>x. x \<in># B \<Longrightarrow> prime x"
nipkow@63830
   526
  shows   "prod_mset A dvd prod_mset B \<longleftrightarrow> A \<subseteq># B"
nipkow@63830
   527
  using assms by (auto intro: prod_mset_subset_imp_dvd prod_mset_primes_dvd_imp_subset)
eberlm@63633
   528
nipkow@63830
   529
lemma is_unit_prod_mset_primes_iff:
eberlm@63633
   530
  assumes "\<And>x. x \<in># A \<Longrightarrow> prime x"
nipkow@63830
   531
  shows   "is_unit (prod_mset A) \<longleftrightarrow> A = {#}"
haftmann@63924
   532
  by (auto simp add: is_unit_prod_mset_iff)
haftmann@63924
   533
    (meson all_not_in_conv assms not_prime_unit set_mset_eq_empty_iff)
eberlm@63498
   534
nipkow@63830
   535
lemma prod_mset_primes_irreducible_imp_prime:
nipkow@63830
   536
  assumes irred: "irreducible (prod_mset A)"
eberlm@63633
   537
  assumes A: "\<And>x. x \<in># A \<Longrightarrow> prime x"
eberlm@63633
   538
  assumes B: "\<And>x. x \<in># B \<Longrightarrow> prime x"
eberlm@63633
   539
  assumes C: "\<And>x. x \<in># C \<Longrightarrow> prime x"
nipkow@63830
   540
  assumes dvd: "prod_mset A dvd prod_mset B * prod_mset C"
nipkow@63830
   541
  shows   "prod_mset A dvd prod_mset B \<or> prod_mset A dvd prod_mset C"
eberlm@63498
   542
proof -
nipkow@63830
   543
  from dvd have "prod_mset A dvd prod_mset (B + C)"
eberlm@63498
   544
    by simp
eberlm@63498
   545
  with A B C have subset: "A \<subseteq># B + C"
nipkow@63830
   546
    by (subst (asm) prod_mset_dvd_prod_mset_primes_iff) auto
Mathias@63919
   547
  define A1 and A2 where "A1 = A \<inter># B" and "A2 = A - A1"
eberlm@63498
   548
  have "A = A1 + A2" unfolding A1_def A2_def
eberlm@63498
   549
    by (rule sym, intro subset_mset.add_diff_inverse) simp_all
eberlm@63498
   550
  from subset have "A1 \<subseteq># B" "A2 \<subseteq># C"
eberlm@63498
   551
    by (auto simp: A1_def A2_def Multiset.subset_eq_diff_conv Multiset.union_commute)
nipkow@63830
   552
  from \<open>A = A1 + A2\<close> have "prod_mset A = prod_mset A1 * prod_mset A2" by simp
nipkow@63830
   553
  from irred and this have "is_unit (prod_mset A1) \<or> is_unit (prod_mset A2)"
eberlm@63498
   554
    by (rule irreducibleD)
eberlm@63498
   555
  with A have "A1 = {#} \<or> A2 = {#}" unfolding A1_def A2_def
nipkow@63830
   556
    by (subst (asm) (1 2) is_unit_prod_mset_primes_iff) (auto dest: Multiset.in_diffD)
eberlm@63498
   557
  with dvd \<open>A = A1 + A2\<close> \<open>A1 \<subseteq># B\<close> \<open>A2 \<subseteq># C\<close> show ?thesis
nipkow@63830
   558
    by (auto intro: prod_mset_subset_imp_dvd)
eberlm@63498
   559
qed
eberlm@63498
   560
nipkow@63830
   561
lemma prod_mset_primes_finite_divisor_powers:
eberlm@63633
   562
  assumes A: "\<And>x. x \<in># A \<Longrightarrow> prime x"
eberlm@63633
   563
  assumes B: "\<And>x. x \<in># B \<Longrightarrow> prime x"
eberlm@63498
   564
  assumes "A \<noteq> {#}"
nipkow@63830
   565
  shows   "finite {n. prod_mset A ^ n dvd prod_mset B}"
eberlm@63498
   566
proof -
eberlm@63498
   567
  from \<open>A \<noteq> {#}\<close> obtain x where x: "x \<in># A" by blast
eberlm@63498
   568
  define m where "m = count B x"
nipkow@63830
   569
  have "{n. prod_mset A ^ n dvd prod_mset B} \<subseteq> {..m}"
eberlm@63498
   570
  proof safe
nipkow@63830
   571
    fix n assume dvd: "prod_mset A ^ n dvd prod_mset B"
nipkow@63830
   572
    from x have "x ^ n dvd prod_mset A ^ n" by (intro dvd_power_same dvd_prod_mset)
eberlm@63498
   573
    also note dvd
nipkow@63830
   574
    also have "x ^ n = prod_mset (replicate_mset n x)" by simp
eberlm@63498
   575
    finally have "replicate_mset n x \<subseteq># B"
nipkow@63830
   576
      by (rule prod_mset_primes_dvd_imp_subset) (insert A B x, simp_all split: if_splits)
eberlm@63498
   577
    thus "n \<le> m" by (simp add: count_le_replicate_mset_subset_eq m_def)
haftmann@60804
   578
  qed
eberlm@63498
   579
  moreover have "finite {..m}" by simp
eberlm@63498
   580
  ultimately show ?thesis by (rule finite_subset)
eberlm@63498
   581
qed
eberlm@63498
   582
haftmann@63924
   583
end
eberlm@63498
   584
haftmann@63924
   585
haftmann@63924
   586
subsection \<open>In a semiring with GCD, each irreducible element is a prime elements\<close>
eberlm@63498
   587
eberlm@63498
   588
context semiring_gcd
eberlm@63498
   589
begin
eberlm@63498
   590
eberlm@63633
   591
lemma irreducible_imp_prime_elem_gcd:
eberlm@63498
   592
  assumes "irreducible x"
eberlm@63633
   593
  shows   "prime_elem x"
eberlm@63633
   594
proof (rule prime_elemI)
eberlm@63498
   595
  fix a b assume "x dvd a * b"
eberlm@63498
   596
  from dvd_productE[OF this] obtain y z where yz: "x = y * z" "y dvd a" "z dvd b" .
eberlm@63498
   597
  from \<open>irreducible x\<close> and \<open>x = y * z\<close> have "is_unit y \<or> is_unit z" by (rule irreducibleD)
eberlm@63498
   598
  with yz show "x dvd a \<or> x dvd b"
eberlm@63498
   599
    by (auto simp: mult_unit_dvd_iff mult_unit_dvd_iff')
eberlm@63498
   600
qed (insert assms, auto simp: irreducible_not_unit)
eberlm@63498
   601
eberlm@63633
   602
lemma prime_elem_imp_coprime:
eberlm@63633
   603
  assumes "prime_elem p" "\<not>p dvd n"
eberlm@63534
   604
  shows   "coprime p n"
eberlm@63534
   605
proof (rule coprimeI)
eberlm@63534
   606
  fix d assume "d dvd p" "d dvd n"
eberlm@63534
   607
  show "is_unit d"
eberlm@63534
   608
  proof (rule ccontr)
eberlm@63534
   609
    assume "\<not>is_unit d"
eberlm@63633
   610
    from \<open>prime_elem p\<close> and \<open>d dvd p\<close> and this have "p dvd d"
eberlm@63633
   611
      by (rule prime_elemD2)
eberlm@63534
   612
    from this and \<open>d dvd n\<close> have "p dvd n" by (rule dvd_trans)
eberlm@63534
   613
    with \<open>\<not>p dvd n\<close> show False by contradiction
eberlm@63534
   614
  qed
eberlm@63534
   615
qed
eberlm@63534
   616
eberlm@63633
   617
lemma prime_imp_coprime:
eberlm@63633
   618
  assumes "prime p" "\<not>p dvd n"
eberlm@63534
   619
  shows   "coprime p n"
eberlm@63633
   620
  using assms by (simp add: prime_elem_imp_coprime)
eberlm@63534
   621
wenzelm@65552
   622
lemma prime_elem_imp_power_coprime:
eberlm@63633
   623
  "prime_elem p \<Longrightarrow> \<not>p dvd a \<Longrightarrow> coprime a (p ^ m)"
eberlm@63633
   624
  by (auto intro!: coprime_exp dest: prime_elem_imp_coprime simp: gcd.commute)
eberlm@63534
   625
wenzelm@65552
   626
lemma prime_imp_power_coprime:
eberlm@63633
   627
  "prime p \<Longrightarrow> \<not>p dvd a \<Longrightarrow> coprime a (p ^ m)"
eberlm@63633
   628
  by (simp add: prime_elem_imp_power_coprime)
eberlm@63534
   629
eberlm@63633
   630
lemma prime_elem_divprod_pow:
eberlm@63633
   631
  assumes p: "prime_elem p" and ab: "coprime a b" and pab: "p^n dvd a * b"
eberlm@63534
   632
  shows   "p^n dvd a \<or> p^n dvd b"
eberlm@63534
   633
  using assms
eberlm@63534
   634
proof -
eberlm@63534
   635
  from ab p have "\<not>p dvd a \<or> \<not>p dvd b"
eberlm@63633
   636
    by (auto simp: coprime prime_elem_def)
wenzelm@65552
   637
  with p have "coprime (p^n) a \<or> coprime (p^n) b"
eberlm@63633
   638
    by (auto intro: prime_elem_imp_coprime coprime_exp_left)
eberlm@63534
   639
  with pab show ?thesis by (auto intro: coprime_dvd_mult simp: mult_ac)
eberlm@63534
   640
qed
eberlm@63534
   641
wenzelm@65552
   642
lemma primes_coprime:
eberlm@63633
   643
  "prime p \<Longrightarrow> prime q \<Longrightarrow> p \<noteq> q \<Longrightarrow> coprime p q"
eberlm@63633
   644
  using prime_imp_coprime primes_dvd_imp_eq by blast
eberlm@63534
   645
eberlm@63498
   646
end
eberlm@63498
   647
eberlm@63498
   648
haftmann@63924
   649
subsection \<open>Factorial semirings: algebraic structures with unique prime factorizations\<close>
haftmann@63924
   650
eberlm@63498
   651
class factorial_semiring = normalization_semidom +
eberlm@63498
   652
  assumes prime_factorization_exists:
haftmann@63924
   653
    "x \<noteq> 0 \<Longrightarrow> \<exists>A. (\<forall>x. x \<in># A \<longrightarrow> prime_elem x) \<and> prod_mset A = normalize x"
haftmann@63924
   654
haftmann@63924
   655
text \<open>Alternative characterization\<close>
wenzelm@65552
   656
haftmann@63924
   657
lemma (in normalization_semidom) factorial_semiring_altI_aux:
haftmann@63924
   658
  assumes finite_divisors: "\<And>x. x \<noteq> 0 \<Longrightarrow> finite {y. y dvd x \<and> normalize y = y}"
haftmann@63924
   659
  assumes irreducible_imp_prime_elem: "\<And>x. irreducible x \<Longrightarrow> prime_elem x"
haftmann@63924
   660
  assumes "x \<noteq> 0"
haftmann@63924
   661
  shows   "\<exists>A. (\<forall>x. x \<in># A \<longrightarrow> prime_elem x) \<and> prod_mset A = normalize x"
haftmann@63924
   662
using \<open>x \<noteq> 0\<close>
haftmann@63924
   663
proof (induction "card {b. b dvd x \<and> normalize b = b}" arbitrary: x rule: less_induct)
haftmann@63924
   664
  case (less a)
haftmann@63924
   665
  let ?fctrs = "\<lambda>a. {b. b dvd a \<and> normalize b = b}"
haftmann@63924
   666
  show ?case
haftmann@63924
   667
  proof (cases "is_unit a")
haftmann@63924
   668
    case True
haftmann@63924
   669
    thus ?thesis by (intro exI[of _ "{#}"]) (auto simp: is_unit_normalize)
haftmann@63924
   670
  next
haftmann@63924
   671
    case False
haftmann@63924
   672
    show ?thesis
haftmann@63924
   673
    proof (cases "\<exists>b. b dvd a \<and> \<not>is_unit b \<and> \<not>a dvd b")
haftmann@63924
   674
      case False
haftmann@63924
   675
      with \<open>\<not>is_unit a\<close> less.prems have "irreducible a" by (auto simp: irreducible_altdef)
haftmann@63924
   676
      hence "prime_elem a" by (rule irreducible_imp_prime_elem)
haftmann@63924
   677
      thus ?thesis by (intro exI[of _ "{#normalize a#}"]) auto
haftmann@63924
   678
    next
haftmann@63924
   679
      case True
haftmann@63924
   680
      then guess b by (elim exE conjE) note b = this
haftmann@63924
   681
haftmann@63924
   682
      from b have "?fctrs b \<subseteq> ?fctrs a" by (auto intro: dvd_trans)
haftmann@63924
   683
      moreover from b have "normalize a \<notin> ?fctrs b" "normalize a \<in> ?fctrs a" by simp_all
haftmann@63924
   684
      hence "?fctrs b \<noteq> ?fctrs a" by blast
haftmann@63924
   685
      ultimately have "?fctrs b \<subset> ?fctrs a" by (subst subset_not_subset_eq) blast
haftmann@63924
   686
      with finite_divisors[OF \<open>a \<noteq> 0\<close>] have "card (?fctrs b) < card (?fctrs a)"
haftmann@63924
   687
        by (rule psubset_card_mono)
haftmann@63924
   688
      moreover from \<open>a \<noteq> 0\<close> b have "b \<noteq> 0" by auto
haftmann@63924
   689
      ultimately have "\<exists>A. (\<forall>x. x \<in># A \<longrightarrow> prime_elem x) \<and> prod_mset A = normalize b"
haftmann@63924
   690
        by (intro less) auto
haftmann@63924
   691
      then guess A .. note A = this
haftmann@63924
   692
haftmann@63924
   693
      define c where "c = a div b"
haftmann@63924
   694
      from b have c: "a = b * c" by (simp add: c_def)
haftmann@63924
   695
      from less.prems c have "c \<noteq> 0" by auto
haftmann@63924
   696
      from b c have "?fctrs c \<subseteq> ?fctrs a" by (auto intro: dvd_trans)
haftmann@63924
   697
      moreover have "normalize a \<notin> ?fctrs c"
haftmann@63924
   698
      proof safe
haftmann@63924
   699
        assume "normalize a dvd c"
haftmann@63924
   700
        hence "b * c dvd 1 * c" by (simp add: c)
haftmann@63924
   701
        hence "b dvd 1" by (subst (asm) dvd_times_right_cancel_iff) fact+
haftmann@63924
   702
        with b show False by simp
haftmann@63924
   703
      qed
haftmann@63924
   704
      with \<open>normalize a \<in> ?fctrs a\<close> have "?fctrs a \<noteq> ?fctrs c" by blast
haftmann@63924
   705
      ultimately have "?fctrs c \<subset> ?fctrs a" by (subst subset_not_subset_eq) blast
haftmann@63924
   706
      with finite_divisors[OF \<open>a \<noteq> 0\<close>] have "card (?fctrs c) < card (?fctrs a)"
haftmann@63924
   707
        by (rule psubset_card_mono)
haftmann@63924
   708
      with \<open>c \<noteq> 0\<close> have "\<exists>A. (\<forall>x. x \<in># A \<longrightarrow> prime_elem x) \<and> prod_mset A = normalize c"
haftmann@63924
   709
        by (intro less) auto
haftmann@63924
   710
      then guess B .. note B = this
haftmann@63924
   711
haftmann@63924
   712
      from A B show ?thesis by (intro exI[of _ "A + B"]) (auto simp: c normalize_mult)
haftmann@63924
   713
    qed
haftmann@63924
   714
  qed
wenzelm@65552
   715
qed
haftmann@63924
   716
haftmann@63924
   717
lemma factorial_semiring_altI:
haftmann@63924
   718
  assumes finite_divisors: "\<And>x::'a. x \<noteq> 0 \<Longrightarrow> finite {y. y dvd x \<and> normalize y = y}"
haftmann@63924
   719
  assumes irreducible_imp_prime: "\<And>x::'a. irreducible x \<Longrightarrow> prime_elem x"
haftmann@63924
   720
  shows   "OFCLASS('a :: normalization_semidom, factorial_semiring_class)"
haftmann@63924
   721
  by intro_classes (rule factorial_semiring_altI_aux[OF assms])
wenzelm@65552
   722
haftmann@63924
   723
text \<open>Properties\<close>
haftmann@63924
   724
haftmann@63924
   725
context factorial_semiring
eberlm@63498
   726
begin
eberlm@63498
   727
eberlm@63498
   728
lemma prime_factorization_exists':
eberlm@63498
   729
  assumes "x \<noteq> 0"
nipkow@63830
   730
  obtains A where "\<And>x. x \<in># A \<Longrightarrow> prime x" "prod_mset A = normalize x"
eberlm@63498
   731
proof -
eberlm@63498
   732
  from prime_factorization_exists[OF assms] obtain A
nipkow@63830
   733
    where A: "\<And>x. x \<in># A \<Longrightarrow> prime_elem x" "prod_mset A = normalize x" by blast
eberlm@63498
   734
  define A' where "A' = image_mset normalize A"
nipkow@63830
   735
  have "prod_mset A' = normalize (prod_mset A)"
nipkow@63830
   736
    by (simp add: A'_def normalize_prod_mset)
eberlm@63498
   737
  also note A(2)
nipkow@63830
   738
  finally have "prod_mset A' = normalize x" by simp
eberlm@63633
   739
  moreover from A(1) have "\<forall>x. x \<in># A' \<longrightarrow> prime x" by (auto simp: prime_def A'_def)
eberlm@63498
   740
  ultimately show ?thesis by (intro that[of A']) blast
eberlm@63498
   741
qed
eberlm@63498
   742
eberlm@63633
   743
lemma irreducible_imp_prime_elem:
eberlm@63498
   744
  assumes "irreducible x"
eberlm@63633
   745
  shows   "prime_elem x"
eberlm@63633
   746
proof (rule prime_elemI)
eberlm@63498
   747
  fix a b assume dvd: "x dvd a * b"
eberlm@63498
   748
  from assms have "x \<noteq> 0" by auto
eberlm@63498
   749
  show "x dvd a \<or> x dvd b"
eberlm@63498
   750
  proof (cases "a = 0 \<or> b = 0")
eberlm@63498
   751
    case False
eberlm@63498
   752
    hence "a \<noteq> 0" "b \<noteq> 0" by blast+
eberlm@63498
   753
    note nz = \<open>x \<noteq> 0\<close> this
eberlm@63498
   754
    from nz[THEN prime_factorization_exists'] guess A B C . note ABC = this
nipkow@63830
   755
    from assms ABC have "irreducible (prod_mset A)" by simp
nipkow@63830
   756
    from dvd prod_mset_primes_irreducible_imp_prime[of A B C, OF this ABC(1,3,5)] ABC(2,4,6)
eberlm@63498
   757
      show ?thesis by (simp add: normalize_mult [symmetric])
eberlm@63498
   758
  qed auto
eberlm@63498
   759
qed (insert assms, simp_all add: irreducible_def)
eberlm@63498
   760
eberlm@63498
   761
lemma finite_divisor_powers:
eberlm@63498
   762
  assumes "y \<noteq> 0" "\<not>is_unit x"
eberlm@63498
   763
  shows   "finite {n. x ^ n dvd y}"
eberlm@63498
   764
proof (cases "x = 0")
eberlm@63498
   765
  case True
eberlm@63498
   766
  with assms have "{n. x ^ n dvd y} = {0}" by (auto simp: power_0_left)
eberlm@63498
   767
  thus ?thesis by simp
eberlm@63498
   768
next
eberlm@63498
   769
  case False
eberlm@63498
   770
  note nz = this \<open>y \<noteq> 0\<close>
eberlm@63498
   771
  from nz[THEN prime_factorization_exists'] guess A B . note AB = this
eberlm@63498
   772
  from AB assms have "A \<noteq> {#}" by (auto simp: normalize_1_iff)
nipkow@63830
   773
  from AB(2,4) prod_mset_primes_finite_divisor_powers [of A B, OF AB(1,3) this]
eberlm@63498
   774
    show ?thesis by (simp add: normalize_power [symmetric])
eberlm@63498
   775
qed
eberlm@63498
   776
eberlm@63498
   777
lemma finite_prime_divisors:
eberlm@63498
   778
  assumes "x \<noteq> 0"
eberlm@63633
   779
  shows   "finite {p. prime p \<and> p dvd x}"
eberlm@63498
   780
proof -
eberlm@63498
   781
  from prime_factorization_exists'[OF assms] guess A . note A = this
eberlm@63633
   782
  have "{p. prime p \<and> p dvd x} \<subseteq> set_mset A"
eberlm@63498
   783
  proof safe
eberlm@63633
   784
    fix p assume p: "prime p" and dvd: "p dvd x"
eberlm@63498
   785
    from dvd have "p dvd normalize x" by simp
nipkow@63830
   786
    also from A have "normalize x = prod_mset A" by simp
nipkow@63830
   787
    finally show "p \<in># A" using p A by (subst (asm) prime_dvd_prod_mset_primes_iff)
eberlm@63498
   788
  qed
eberlm@63498
   789
  moreover have "finite (set_mset A)" by simp
eberlm@63498
   790
  ultimately show ?thesis by (rule finite_subset)
haftmann@60804
   791
qed
haftmann@60804
   792
eberlm@63633
   793
lemma prime_elem_iff_irreducible: "prime_elem x \<longleftrightarrow> irreducible x"
eberlm@63633
   794
  by (blast intro: irreducible_imp_prime_elem prime_elem_imp_irreducible)
haftmann@62499
   795
eberlm@63498
   796
lemma prime_divisor_exists:
eberlm@63498
   797
  assumes "a \<noteq> 0" "\<not>is_unit a"
eberlm@63633
   798
  shows   "\<exists>b. b dvd a \<and> prime b"
eberlm@63498
   799
proof -
eberlm@63498
   800
  from prime_factorization_exists'[OF assms(1)] guess A . note A = this
eberlm@63498
   801
  moreover from A and assms have "A \<noteq> {#}" by auto
eberlm@63498
   802
  then obtain x where "x \<in># A" by blast
nipkow@63830
   803
  with A(1) have *: "x dvd prod_mset A" "prime x" by (auto simp: dvd_prod_mset)
wenzelm@63539
   804
  with A have "x dvd a" by simp
wenzelm@63539
   805
  with * show ?thesis by blast
eberlm@63498
   806
qed
haftmann@60804
   807
eberlm@63498
   808
lemma prime_divisors_induct [case_names zero unit factor]:
eberlm@63633
   809
  assumes "P 0" "\<And>x. is_unit x \<Longrightarrow> P x" "\<And>p x. prime p \<Longrightarrow> P x \<Longrightarrow> P (p * x)"
eberlm@63498
   810
  shows   "P x"
eberlm@63498
   811
proof (cases "x = 0")
eberlm@63498
   812
  case False
eberlm@63498
   813
  from prime_factorization_exists'[OF this] guess A . note A = this
nipkow@63830
   814
  from A(1) have "P (unit_factor x * prod_mset A)"
eberlm@63498
   815
  proof (induction A)
Mathias@63793
   816
    case (add p A)
eberlm@63633
   817
    from add.prems have "prime p" by simp
nipkow@63830
   818
    moreover from add.prems have "P (unit_factor x * prod_mset A)" by (intro add.IH) simp_all
nipkow@63830
   819
    ultimately have "P (p * (unit_factor x * prod_mset A))" by (rule assms(3))
eberlm@63498
   820
    thus ?case by (simp add: mult_ac)
eberlm@63498
   821
  qed (simp_all add: assms False)
eberlm@63498
   822
  with A show ?thesis by simp
eberlm@63498
   823
qed (simp_all add: assms(1))
eberlm@63498
   824
eberlm@63498
   825
lemma no_prime_divisors_imp_unit:
eberlm@63633
   826
  assumes "a \<noteq> 0" "\<And>b. b dvd a \<Longrightarrow> normalize b = b \<Longrightarrow> \<not> prime_elem b"
eberlm@63498
   827
  shows "is_unit a"
eberlm@63498
   828
proof (rule ccontr)
eberlm@63498
   829
  assume "\<not>is_unit a"
eberlm@63498
   830
  from prime_divisor_exists[OF assms(1) this] guess b by (elim exE conjE)
eberlm@63633
   831
  with assms(2)[of b] show False by (simp add: prime_def)
haftmann@60804
   832
qed
haftmann@62499
   833
eberlm@63498
   834
lemma prime_divisorE:
eberlm@63498
   835
  assumes "a \<noteq> 0" and "\<not> is_unit a"
eberlm@63633
   836
  obtains p where "prime p" and "p dvd a"
eberlm@63633
   837
  using assms no_prime_divisors_imp_unit unfolding prime_def by blast
eberlm@63498
   838
eberlm@63498
   839
definition multiplicity :: "'a \<Rightarrow> 'a \<Rightarrow> nat" where
eberlm@63498
   840
  "multiplicity p x = (if finite {n. p ^ n dvd x} then Max {n. p ^ n dvd x} else 0)"
eberlm@63498
   841
eberlm@63498
   842
lemma multiplicity_dvd: "p ^ multiplicity p x dvd x"
eberlm@63498
   843
proof (cases "finite {n. p ^ n dvd x}")
eberlm@63498
   844
  case True
eberlm@63498
   845
  hence "multiplicity p x = Max {n. p ^ n dvd x}"
eberlm@63498
   846
    by (simp add: multiplicity_def)
eberlm@63498
   847
  also have "\<dots> \<in> {n. p ^ n dvd x}"
eberlm@63498
   848
    by (rule Max_in) (auto intro!: True exI[of _ "0::nat"])
eberlm@63498
   849
  finally show ?thesis by simp
eberlm@63498
   850
qed (simp add: multiplicity_def)
eberlm@63498
   851
eberlm@63498
   852
lemma multiplicity_dvd': "n \<le> multiplicity p x \<Longrightarrow> p ^ n dvd x"
eberlm@63498
   853
  by (rule dvd_trans[OF le_imp_power_dvd multiplicity_dvd])
eberlm@63498
   854
eberlm@63498
   855
context
eberlm@63498
   856
  fixes x p :: 'a
eberlm@63498
   857
  assumes xp: "x \<noteq> 0" "\<not>is_unit p"
eberlm@63498
   858
begin
eberlm@63498
   859
eberlm@63498
   860
lemma multiplicity_eq_Max: "multiplicity p x = Max {n. p ^ n dvd x}"
eberlm@63498
   861
  using finite_divisor_powers[OF xp] by (simp add: multiplicity_def)
eberlm@63498
   862
eberlm@63498
   863
lemma multiplicity_geI:
eberlm@63498
   864
  assumes "p ^ n dvd x"
eberlm@63498
   865
  shows   "multiplicity p x \<ge> n"
eberlm@63498
   866
proof -
eberlm@63498
   867
  from assms have "n \<le> Max {n. p ^ n dvd x}"
eberlm@63498
   868
    by (intro Max_ge finite_divisor_powers xp) simp_all
eberlm@63498
   869
  thus ?thesis by (subst multiplicity_eq_Max)
eberlm@63498
   870
qed
eberlm@63498
   871
eberlm@63498
   872
lemma multiplicity_lessI:
eberlm@63498
   873
  assumes "\<not>p ^ n dvd x"
eberlm@63498
   874
  shows   "multiplicity p x < n"
eberlm@63498
   875
proof (rule ccontr)
eberlm@63498
   876
  assume "\<not>(n > multiplicity p x)"
eberlm@63498
   877
  hence "p ^ n dvd x" by (intro multiplicity_dvd') simp
eberlm@63498
   878
  with assms show False by contradiction
haftmann@62499
   879
qed
haftmann@62499
   880
eberlm@63498
   881
lemma power_dvd_iff_le_multiplicity:
eberlm@63498
   882
  "p ^ n dvd x \<longleftrightarrow> n \<le> multiplicity p x"
eberlm@63498
   883
  using multiplicity_geI[of n] multiplicity_lessI[of n] by (cases "p ^ n dvd x") auto
eberlm@63498
   884
eberlm@63498
   885
lemma multiplicity_eq_zero_iff:
eberlm@63498
   886
  shows   "multiplicity p x = 0 \<longleftrightarrow> \<not>p dvd x"
eberlm@63498
   887
  using power_dvd_iff_le_multiplicity[of 1] by auto
eberlm@63498
   888
eberlm@63498
   889
lemma multiplicity_gt_zero_iff:
eberlm@63498
   890
  shows   "multiplicity p x > 0 \<longleftrightarrow> p dvd x"
eberlm@63498
   891
  using power_dvd_iff_le_multiplicity[of 1] by auto
eberlm@63498
   892
eberlm@63498
   893
lemma multiplicity_decompose:
eberlm@63498
   894
  "\<not>p dvd (x div p ^ multiplicity p x)"
eberlm@63498
   895
proof
eberlm@63498
   896
  assume *: "p dvd x div p ^ multiplicity p x"
eberlm@63498
   897
  have "x = x div p ^ multiplicity p x * (p ^ multiplicity p x)"
eberlm@63498
   898
    using multiplicity_dvd[of p x] by simp
eberlm@63498
   899
  also from * have "x div p ^ multiplicity p x = (x div p ^ multiplicity p x div p) * p" by simp
eberlm@63498
   900
  also have "x div p ^ multiplicity p x div p * p * p ^ multiplicity p x =
eberlm@63498
   901
               x div p ^ multiplicity p x div p * p ^ Suc (multiplicity p x)"
eberlm@63498
   902
    by (simp add: mult_assoc)
eberlm@63498
   903
  also have "p ^ Suc (multiplicity p x) dvd \<dots>" by (rule dvd_triv_right)
eberlm@63498
   904
  finally show False by (subst (asm) power_dvd_iff_le_multiplicity) simp
eberlm@63498
   905
qed
eberlm@63498
   906
eberlm@63498
   907
lemma multiplicity_decompose':
eberlm@63498
   908
  obtains y where "x = p ^ multiplicity p x * y" "\<not>p dvd y"
eberlm@63498
   909
  using that[of "x div p ^ multiplicity p x"]
eberlm@63498
   910
  by (simp add: multiplicity_decompose multiplicity_dvd)
eberlm@63498
   911
eberlm@63498
   912
end
eberlm@63498
   913
eberlm@63498
   914
lemma multiplicity_zero [simp]: "multiplicity p 0 = 0"
eberlm@63498
   915
  by (simp add: multiplicity_def)
eberlm@63498
   916
eberlm@63633
   917
lemma prime_elem_multiplicity_eq_zero_iff:
eberlm@63633
   918
  "prime_elem p \<Longrightarrow> x \<noteq> 0 \<Longrightarrow> multiplicity p x = 0 \<longleftrightarrow> \<not>p dvd x"
eberlm@63534
   919
  by (rule multiplicity_eq_zero_iff) simp_all
eberlm@63534
   920
eberlm@63534
   921
lemma prime_multiplicity_other:
eberlm@63633
   922
  assumes "prime p" "prime q" "p \<noteq> q"
eberlm@63534
   923
  shows   "multiplicity p q = 0"
wenzelm@65552
   924
  using assms by (subst prime_elem_multiplicity_eq_zero_iff) (auto dest: primes_dvd_imp_eq)
eberlm@63534
   925
eberlm@63534
   926
lemma prime_multiplicity_gt_zero_iff:
eberlm@63633
   927
  "prime_elem p \<Longrightarrow> x \<noteq> 0 \<Longrightarrow> multiplicity p x > 0 \<longleftrightarrow> p dvd x"
eberlm@63534
   928
  by (rule multiplicity_gt_zero_iff) simp_all
eberlm@63534
   929
eberlm@63498
   930
lemma multiplicity_unit_left: "is_unit p \<Longrightarrow> multiplicity p x = 0"
eberlm@63498
   931
  by (simp add: multiplicity_def is_unit_power_iff unit_imp_dvd)
haftmann@62499
   932
eberlm@63498
   933
lemma multiplicity_unit_right:
eberlm@63498
   934
  assumes "is_unit x"
eberlm@63498
   935
  shows   "multiplicity p x = 0"
eberlm@63498
   936
proof (cases "is_unit p \<or> x = 0")
eberlm@63498
   937
  case False
eberlm@63498
   938
  with multiplicity_lessI[of x p 1] this assms
eberlm@63498
   939
    show ?thesis by (auto dest: dvd_unit_imp_unit)
eberlm@63498
   940
qed (auto simp: multiplicity_unit_left)
eberlm@63498
   941
eberlm@63498
   942
lemma multiplicity_one [simp]: "multiplicity p 1 = 0"
eberlm@63498
   943
  by (rule multiplicity_unit_right) simp_all
eberlm@63498
   944
eberlm@63498
   945
lemma multiplicity_eqI:
eberlm@63498
   946
  assumes "p ^ n dvd x" "\<not>p ^ Suc n dvd x"
eberlm@63498
   947
  shows   "multiplicity p x = n"
eberlm@63498
   948
proof -
eberlm@63498
   949
  consider "x = 0" | "is_unit p" | "x \<noteq> 0" "\<not>is_unit p" by blast
eberlm@63498
   950
  thus ?thesis
eberlm@63498
   951
  proof cases
eberlm@63498
   952
    assume xp: "x \<noteq> 0" "\<not>is_unit p"
eberlm@63498
   953
    from xp assms(1) have "multiplicity p x \<ge> n" by (intro multiplicity_geI)
eberlm@63498
   954
    moreover from assms(2) xp have "multiplicity p x < Suc n" by (intro multiplicity_lessI)
eberlm@63498
   955
    ultimately show ?thesis by simp
eberlm@63498
   956
  next
eberlm@63498
   957
    assume "is_unit p"
eberlm@63498
   958
    hence "is_unit (p ^ Suc n)" by (simp add: is_unit_power_iff del: power_Suc)
eberlm@63498
   959
    hence "p ^ Suc n dvd x" by (rule unit_imp_dvd)
eberlm@63498
   960
    with \<open>\<not>p ^ Suc n dvd x\<close> show ?thesis by contradiction
eberlm@63498
   961
  qed (insert assms, simp_all)
eberlm@63498
   962
qed
eberlm@63498
   963
eberlm@63498
   964
eberlm@63498
   965
context
eberlm@63498
   966
  fixes x p :: 'a
eberlm@63498
   967
  assumes xp: "x \<noteq> 0" "\<not>is_unit p"
eberlm@63498
   968
begin
eberlm@63498
   969
eberlm@63498
   970
lemma multiplicity_times_same:
eberlm@63498
   971
  assumes "p \<noteq> 0"
eberlm@63498
   972
  shows   "multiplicity p (p * x) = Suc (multiplicity p x)"
eberlm@63498
   973
proof (rule multiplicity_eqI)
eberlm@63498
   974
  show "p ^ Suc (multiplicity p x) dvd p * x"
eberlm@63498
   975
    by (auto intro!: mult_dvd_mono multiplicity_dvd)
eberlm@63498
   976
  from xp assms show "\<not> p ^ Suc (Suc (multiplicity p x)) dvd p * x"
eberlm@63498
   977
    using power_dvd_iff_le_multiplicity[OF xp, of "Suc (multiplicity p x)"] by simp
haftmann@62499
   978
qed
haftmann@62499
   979
haftmann@62499
   980
end
haftmann@62499
   981
eberlm@63498
   982
lemma multiplicity_same_power': "multiplicity p (p ^ n) = (if p = 0 \<or> is_unit p then 0 else n)"
eberlm@63498
   983
proof -
eberlm@63498
   984
  consider "p = 0" | "is_unit p" |"p \<noteq> 0" "\<not>is_unit p" by blast
eberlm@63498
   985
  thus ?thesis
eberlm@63498
   986
  proof cases
eberlm@63498
   987
    assume "p \<noteq> 0" "\<not>is_unit p"
eberlm@63498
   988
    thus ?thesis by (induction n) (simp_all add: multiplicity_times_same)
eberlm@63498
   989
  qed (simp_all add: power_0_left multiplicity_unit_left)
eberlm@63498
   990
qed
haftmann@62499
   991
eberlm@63498
   992
lemma multiplicity_same_power:
eberlm@63498
   993
  "p \<noteq> 0 \<Longrightarrow> \<not>is_unit p \<Longrightarrow> multiplicity p (p ^ n) = n"
eberlm@63498
   994
  by (simp add: multiplicity_same_power')
eberlm@63498
   995
eberlm@63633
   996
lemma multiplicity_prime_elem_times_other:
eberlm@63633
   997
  assumes "prime_elem p" "\<not>p dvd q"
eberlm@63498
   998
  shows   "multiplicity p (q * x) = multiplicity p x"
eberlm@63498
   999
proof (cases "x = 0")
eberlm@63498
  1000
  case False
eberlm@63498
  1001
  show ?thesis
eberlm@63498
  1002
  proof (rule multiplicity_eqI)
eberlm@63498
  1003
    have "1 * p ^ multiplicity p x dvd q * x"
eberlm@63498
  1004
      by (intro mult_dvd_mono multiplicity_dvd) simp_all
eberlm@63498
  1005
    thus "p ^ multiplicity p x dvd q * x" by simp
haftmann@62499
  1006
  next
eberlm@63498
  1007
    define n where "n = multiplicity p x"
eberlm@63498
  1008
    from assms have "\<not>is_unit p" by simp
eberlm@63498
  1009
    from multiplicity_decompose'[OF False this] guess y . note y = this [folded n_def]
eberlm@63498
  1010
    from y have "p ^ Suc n dvd q * x \<longleftrightarrow> p ^ n * p dvd p ^ n * (q * y)" by (simp add: mult_ac)
eberlm@63498
  1011
    also from assms have "\<dots> \<longleftrightarrow> p dvd q * y" by simp
eberlm@63633
  1012
    also have "\<dots> \<longleftrightarrow> p dvd q \<or> p dvd y" by (rule prime_elem_dvd_mult_iff) fact+
eberlm@63498
  1013
    also from assms y have "\<dots> \<longleftrightarrow> False" by simp
eberlm@63498
  1014
    finally show "\<not>(p ^ Suc n dvd q * x)" by blast
haftmann@62499
  1015
  qed
eberlm@63498
  1016
qed simp_all
eberlm@63498
  1017
haftmann@63924
  1018
lemma multiplicity_self:
haftmann@63924
  1019
  assumes "p \<noteq> 0" "\<not>is_unit p"
haftmann@63924
  1020
  shows   "multiplicity p p = 1"
haftmann@63924
  1021
proof -
haftmann@63924
  1022
  from assms have "multiplicity p p = Max {n. p ^ n dvd p}"
haftmann@63924
  1023
    by (simp add: multiplicity_eq_Max)
haftmann@63924
  1024
  also from assms have "p ^ n dvd p \<longleftrightarrow> n \<le> 1" for n
haftmann@63924
  1025
    using dvd_power_iff[of p n 1] by auto
haftmann@63924
  1026
  hence "{n. p ^ n dvd p} = {..1}" by auto
haftmann@63924
  1027
  also have "\<dots> = {0,1}" by auto
haftmann@63924
  1028
  finally show ?thesis by simp
haftmann@63924
  1029
qed
haftmann@63924
  1030
haftmann@63924
  1031
lemma multiplicity_times_unit_left:
haftmann@63924
  1032
  assumes "is_unit c"
haftmann@63924
  1033
  shows   "multiplicity (c * p) x = multiplicity p x"
haftmann@63924
  1034
proof -
haftmann@63924
  1035
  from assms have "{n. (c * p) ^ n dvd x} = {n. p ^ n dvd x}"
haftmann@63924
  1036
    by (subst mult.commute) (simp add: mult_unit_dvd_iff power_mult_distrib is_unit_power_iff)
haftmann@63924
  1037
  thus ?thesis by (simp add: multiplicity_def)
haftmann@63924
  1038
qed
haftmann@63924
  1039
haftmann@63924
  1040
lemma multiplicity_times_unit_right:
haftmann@63924
  1041
  assumes "is_unit c"
haftmann@63924
  1042
  shows   "multiplicity p (c * x) = multiplicity p x"
haftmann@63924
  1043
proof -
haftmann@63924
  1044
  from assms have "{n. p ^ n dvd c * x} = {n. p ^ n dvd x}"
haftmann@63924
  1045
    by (subst mult.commute) (simp add: dvd_mult_unit_iff)
haftmann@63924
  1046
  thus ?thesis by (simp add: multiplicity_def)
haftmann@63924
  1047
qed
haftmann@63924
  1048
haftmann@63924
  1049
lemma multiplicity_normalize_left [simp]:
haftmann@63924
  1050
  "multiplicity (normalize p) x = multiplicity p x"
haftmann@63924
  1051
proof (cases "p = 0")
haftmann@63924
  1052
  case [simp]: False
haftmann@63924
  1053
  have "normalize p = (1 div unit_factor p) * p"
haftmann@63924
  1054
    by (simp add: unit_div_commute is_unit_unit_factor)
haftmann@63924
  1055
  also have "multiplicity \<dots> x = multiplicity p x"
haftmann@63924
  1056
    by (rule multiplicity_times_unit_left) (simp add: is_unit_unit_factor)
haftmann@63924
  1057
  finally show ?thesis .
haftmann@63924
  1058
qed simp_all
haftmann@63924
  1059
haftmann@63924
  1060
lemma multiplicity_normalize_right [simp]:
haftmann@63924
  1061
  "multiplicity p (normalize x) = multiplicity p x"
haftmann@63924
  1062
proof (cases "x = 0")
haftmann@63924
  1063
  case [simp]: False
haftmann@63924
  1064
  have "normalize x = (1 div unit_factor x) * x"
haftmann@63924
  1065
    by (simp add: unit_div_commute is_unit_unit_factor)
haftmann@63924
  1066
  also have "multiplicity p \<dots> = multiplicity p x"
haftmann@63924
  1067
    by (rule multiplicity_times_unit_right) (simp add: is_unit_unit_factor)
haftmann@63924
  1068
  finally show ?thesis .
wenzelm@65552
  1069
qed simp_all
haftmann@63924
  1070
haftmann@63924
  1071
lemma multiplicity_prime [simp]: "prime_elem p \<Longrightarrow> multiplicity p p = 1"
haftmann@63924
  1072
  by (rule multiplicity_self) auto
haftmann@63924
  1073
haftmann@63924
  1074
lemma multiplicity_prime_power [simp]: "prime_elem p \<Longrightarrow> multiplicity p (p ^ n) = n"
haftmann@63924
  1075
  by (subst multiplicity_same_power') auto
haftmann@63924
  1076
eberlm@63498
  1077
lift_definition prime_factorization :: "'a \<Rightarrow> 'a multiset" is
eberlm@63633
  1078
  "\<lambda>x p. if prime p then multiplicity p x else 0"
eberlm@63498
  1079
  unfolding multiset_def
eberlm@63498
  1080
proof clarify
eberlm@63498
  1081
  fix x :: 'a
eberlm@63633
  1082
  show "finite {p. 0 < (if prime p then multiplicity p x else 0)}" (is "finite ?A")
eberlm@63498
  1083
  proof (cases "x = 0")
eberlm@63498
  1084
    case False
eberlm@63633
  1085
    from False have "?A \<subseteq> {p. prime p \<and> p dvd x}"
eberlm@63498
  1086
      by (auto simp: multiplicity_gt_zero_iff)
eberlm@63633
  1087
    moreover from False have "finite {p. prime p \<and> p dvd x}"
eberlm@63498
  1088
      by (rule finite_prime_divisors)
eberlm@63498
  1089
    ultimately show ?thesis by (rule finite_subset)
eberlm@63498
  1090
  qed simp_all
eberlm@63498
  1091
qed
eberlm@63498
  1092
haftmann@63905
  1093
abbreviation prime_factors :: "'a \<Rightarrow> 'a set" where
haftmann@63905
  1094
  "prime_factors a \<equiv> set_mset (prime_factorization a)"
haftmann@63905
  1095
eberlm@63498
  1096
lemma count_prime_factorization_nonprime:
eberlm@63633
  1097
  "\<not>prime p \<Longrightarrow> count (prime_factorization x) p = 0"
eberlm@63498
  1098
  by transfer simp
eberlm@63498
  1099
eberlm@63498
  1100
lemma count_prime_factorization_prime:
eberlm@63633
  1101
  "prime p \<Longrightarrow> count (prime_factorization x) p = multiplicity p x"
eberlm@63498
  1102
  by transfer simp
eberlm@63498
  1103
eberlm@63498
  1104
lemma count_prime_factorization:
eberlm@63633
  1105
  "count (prime_factorization x) p = (if prime p then multiplicity p x else 0)"
eberlm@63498
  1106
  by transfer simp
eberlm@63498
  1107
haftmann@63924
  1108
lemma dvd_imp_multiplicity_le:
haftmann@63924
  1109
  assumes "a dvd b" "b \<noteq> 0"
haftmann@63924
  1110
  shows   "multiplicity p a \<le> multiplicity p b"
haftmann@63924
  1111
proof (cases "is_unit p")
haftmann@63924
  1112
  case False
haftmann@63924
  1113
  with assms show ?thesis
haftmann@63924
  1114
    by (intro multiplicity_geI ) (auto intro: dvd_trans[OF multiplicity_dvd' assms(1)])
haftmann@63924
  1115
qed (insert assms, auto simp: multiplicity_unit_left)
eberlm@63498
  1116
eberlm@66276
  1117
lemma prime_power_inj:
eberlm@66276
  1118
  assumes "prime a" "a ^ m = a ^ n"
eberlm@66276
  1119
  shows   "m = n"
eberlm@66276
  1120
proof -
eberlm@66276
  1121
  have "multiplicity a (a ^ m) = multiplicity a (a ^ n)" by (simp only: assms)
eberlm@66276
  1122
  thus ?thesis using assms by (subst (asm) (1 2) multiplicity_prime_power) simp_all
eberlm@66276
  1123
qed
eberlm@66276
  1124
eberlm@66276
  1125
lemma prime_power_inj':
eberlm@66276
  1126
  assumes "prime p" "prime q"
eberlm@66276
  1127
  assumes "p ^ m = q ^ n" "m > 0" "n > 0"
eberlm@66276
  1128
  shows   "p = q" "m = n"
eberlm@66276
  1129
proof -
eberlm@66276
  1130
  from assms have "p ^ 1 dvd p ^ m" by (intro le_imp_power_dvd) simp
eberlm@66276
  1131
  also have "p ^ m = q ^ n" by fact
eberlm@66276
  1132
  finally have "p dvd q ^ n" by simp
eberlm@66276
  1133
  with assms have "p dvd q" using prime_dvd_power[of p q] by simp
eberlm@66276
  1134
  with assms show "p = q" by (simp add: primes_dvd_imp_eq)
eberlm@66276
  1135
  with assms show "m = n" by (simp add: prime_power_inj)
eberlm@66276
  1136
qed
eberlm@66276
  1137
eberlm@66276
  1138
lemma prime_power_eq_one_iff [simp]: "prime p \<Longrightarrow> p ^ n = 1 \<longleftrightarrow> n = 0"
eberlm@66276
  1139
  using prime_power_inj[of p n 0] by auto
eberlm@66276
  1140
eberlm@66276
  1141
lemma one_eq_prime_power_iff [simp]: "prime p \<Longrightarrow> 1 = p ^ n \<longleftrightarrow> n = 0"
eberlm@66276
  1142
  using prime_power_inj[of p 0 n] by auto
eberlm@66276
  1143
eberlm@66276
  1144
lemma prime_power_inj'':
eberlm@66276
  1145
  assumes "prime p" "prime q"
eberlm@66276
  1146
  shows   "p ^ m = q ^ n \<longleftrightarrow> (m = 0 \<and> n = 0) \<or> (p = q \<and> m = n)"
eberlm@66276
  1147
  using assms 
eberlm@66276
  1148
  by (cases "m = 0"; cases "n = 0")
eberlm@66276
  1149
     (auto dest: prime_power_inj'[OF assms])
eberlm@66276
  1150
eberlm@63498
  1151
lemma prime_factorization_0 [simp]: "prime_factorization 0 = {#}"
eberlm@63498
  1152
  by (simp add: multiset_eq_iff count_prime_factorization)
eberlm@63498
  1153
eberlm@63498
  1154
lemma prime_factorization_empty_iff:
eberlm@63498
  1155
  "prime_factorization x = {#} \<longleftrightarrow> x = 0 \<or> is_unit x"
eberlm@63498
  1156
proof
eberlm@63498
  1157
  assume *: "prime_factorization x = {#}"
eberlm@63498
  1158
  {
eberlm@63498
  1159
    assume x: "x \<noteq> 0" "\<not>is_unit x"
eberlm@63498
  1160
    {
eberlm@63633
  1161
      fix p assume p: "prime p"
eberlm@63498
  1162
      have "count (prime_factorization x) p = 0" by (simp add: *)
eberlm@63498
  1163
      also from p have "count (prime_factorization x) p = multiplicity p x"
eberlm@63498
  1164
        by (rule count_prime_factorization_prime)
eberlm@63498
  1165
      also from x p have "\<dots> = 0 \<longleftrightarrow> \<not>p dvd x" by (simp add: multiplicity_eq_zero_iff)
eberlm@63498
  1166
      finally have "\<not>p dvd x" .
eberlm@63498
  1167
    }
eberlm@63498
  1168
    with prime_divisor_exists[OF x] have False by blast
eberlm@63498
  1169
  }
eberlm@63498
  1170
  thus "x = 0 \<or> is_unit x" by blast
eberlm@63498
  1171
next
eberlm@63498
  1172
  assume "x = 0 \<or> is_unit x"
eberlm@63498
  1173
  thus "prime_factorization x = {#}"
eberlm@63498
  1174
  proof
eberlm@63498
  1175
    assume x: "is_unit x"
eberlm@63498
  1176
    {
eberlm@63633
  1177
      fix p assume p: "prime p"
eberlm@63498
  1178
      from p x have "multiplicity p x = 0"
eberlm@63498
  1179
        by (subst multiplicity_eq_zero_iff)
eberlm@63498
  1180
           (auto simp: multiplicity_eq_zero_iff dest: unit_imp_no_prime_divisors)
eberlm@63498
  1181
    }
eberlm@63498
  1182
    thus ?thesis by (simp add: multiset_eq_iff count_prime_factorization)
eberlm@63498
  1183
  qed simp_all
eberlm@63498
  1184
qed
eberlm@63498
  1185
eberlm@63498
  1186
lemma prime_factorization_unit:
eberlm@63498
  1187
  assumes "is_unit x"
eberlm@63498
  1188
  shows   "prime_factorization x = {#}"
eberlm@63498
  1189
proof (rule multiset_eqI)
eberlm@63498
  1190
  fix p :: 'a
eberlm@63498
  1191
  show "count (prime_factorization x) p = count {#} p"
eberlm@63633
  1192
  proof (cases "prime p")
eberlm@63498
  1193
    case True
eberlm@63498
  1194
    with assms have "multiplicity p x = 0"
eberlm@63498
  1195
      by (subst multiplicity_eq_zero_iff)
eberlm@63498
  1196
         (auto simp: multiplicity_eq_zero_iff dest: unit_imp_no_prime_divisors)
eberlm@63498
  1197
    with True show ?thesis by (simp add: count_prime_factorization_prime)
eberlm@63498
  1198
  qed (simp_all add: count_prime_factorization_nonprime)
eberlm@63498
  1199
qed
eberlm@63498
  1200
eberlm@63498
  1201
lemma prime_factorization_1 [simp]: "prime_factorization 1 = {#}"
eberlm@63498
  1202
  by (simp add: prime_factorization_unit)
eberlm@63498
  1203
eberlm@63498
  1204
lemma prime_factorization_times_prime:
eberlm@63633
  1205
  assumes "x \<noteq> 0" "prime p"
eberlm@63498
  1206
  shows   "prime_factorization (p * x) = {#p#} + prime_factorization x"
eberlm@63498
  1207
proof (rule multiset_eqI)
eberlm@63498
  1208
  fix q :: 'a
eberlm@63633
  1209
  consider "\<not>prime q" | "p = q" | "prime q" "p \<noteq> q" by blast
eberlm@63498
  1210
  thus "count (prime_factorization (p * x)) q = count ({#p#} + prime_factorization x) q"
eberlm@63498
  1211
  proof cases
eberlm@63633
  1212
    assume q: "prime q" "p \<noteq> q"
eberlm@63498
  1213
    with assms primes_dvd_imp_eq[of q p] have "\<not>q dvd p" by auto
eberlm@63498
  1214
    with q assms show ?thesis
eberlm@63633
  1215
      by (simp add: multiplicity_prime_elem_times_other count_prime_factorization)
eberlm@63498
  1216
  qed (insert assms, auto simp: count_prime_factorization multiplicity_times_same)
eberlm@63498
  1217
qed
eberlm@63498
  1218
nipkow@63830
  1219
lemma prod_mset_prime_factorization:
eberlm@63498
  1220
  assumes "x \<noteq> 0"
nipkow@63830
  1221
  shows   "prod_mset (prime_factorization x) = normalize x"
eberlm@63498
  1222
  using assms
eberlm@63498
  1223
  by (induction x rule: prime_divisors_induct)
eberlm@63498
  1224
     (simp_all add: prime_factorization_unit prime_factorization_times_prime
eberlm@63498
  1225
                    is_unit_normalize normalize_mult)
eberlm@63498
  1226
haftmann@63905
  1227
lemma in_prime_factors_iff:
haftmann@63905
  1228
  "p \<in> prime_factors x \<longleftrightarrow> x \<noteq> 0 \<and> p dvd x \<and> prime p"
eberlm@63498
  1229
proof -
haftmann@63905
  1230
  have "p \<in> prime_factors x \<longleftrightarrow> count (prime_factorization x) p > 0" by simp
eberlm@63633
  1231
  also have "\<dots> \<longleftrightarrow> x \<noteq> 0 \<and> p dvd x \<and> prime p"
eberlm@63498
  1232
   by (subst count_prime_factorization, cases "x = 0")
eberlm@63498
  1233
      (auto simp: multiplicity_eq_zero_iff multiplicity_gt_zero_iff)
eberlm@63498
  1234
  finally show ?thesis .
eberlm@63498
  1235
qed
eberlm@63498
  1236
haftmann@63905
  1237
lemma in_prime_factors_imp_prime [intro]:
haftmann@63905
  1238
  "p \<in> prime_factors x \<Longrightarrow> prime p"
haftmann@63905
  1239
  by (simp add: in_prime_factors_iff)
eberlm@63498
  1240
haftmann@63905
  1241
lemma in_prime_factors_imp_dvd [dest]:
haftmann@63905
  1242
  "p \<in> prime_factors x \<Longrightarrow> p dvd x"
haftmann@63905
  1243
  by (simp add: in_prime_factors_iff)
eberlm@63498
  1244
haftmann@63924
  1245
lemma prime_factorsI:
haftmann@63924
  1246
  "x \<noteq> 0 \<Longrightarrow> prime p \<Longrightarrow> p dvd x \<Longrightarrow> p \<in> prime_factors x"
haftmann@63924
  1247
  by (auto simp: in_prime_factors_iff)
haftmann@63924
  1248
haftmann@63924
  1249
lemma prime_factors_dvd:
haftmann@63924
  1250
  "x \<noteq> 0 \<Longrightarrow> prime_factors x = {p. prime p \<and> p dvd x}"
haftmann@63924
  1251
  by (auto intro: prime_factorsI)
haftmann@63924
  1252
haftmann@63924
  1253
lemma prime_factors_multiplicity:
haftmann@63924
  1254
  "prime_factors n = {p. prime p \<and> multiplicity p n > 0}"
haftmann@63924
  1255
  by (cases "n = 0") (auto simp add: prime_factors_dvd prime_multiplicity_gt_zero_iff)
eberlm@63498
  1256
eberlm@63498
  1257
lemma prime_factorization_prime:
eberlm@63633
  1258
  assumes "prime p"
eberlm@63498
  1259
  shows   "prime_factorization p = {#p#}"
eberlm@63498
  1260
proof (rule multiset_eqI)
eberlm@63498
  1261
  fix q :: 'a
eberlm@63633
  1262
  consider "\<not>prime q" | "q = p" | "prime q" "q \<noteq> p" by blast
eberlm@63498
  1263
  thus "count (prime_factorization p) q = count {#p#} q"
eberlm@63498
  1264
    by cases (insert assms, auto dest: primes_dvd_imp_eq
eberlm@63498
  1265
                simp: count_prime_factorization multiplicity_self multiplicity_eq_zero_iff)
eberlm@63498
  1266
qed
eberlm@63498
  1267
nipkow@63830
  1268
lemma prime_factorization_prod_mset_primes:
eberlm@63633
  1269
  assumes "\<And>p. p \<in># A \<Longrightarrow> prime p"
nipkow@63830
  1270
  shows   "prime_factorization (prod_mset A) = A"
eberlm@63498
  1271
  using assms
eberlm@63498
  1272
proof (induction A)
Mathias@63793
  1273
  case (add p A)
eberlm@63498
  1274
  from add.prems[of 0] have "0 \<notin># A" by auto
nipkow@63830
  1275
  hence "prod_mset A \<noteq> 0" by auto
eberlm@63498
  1276
  with add show ?case
eberlm@63498
  1277
    by (simp_all add: mult_ac prime_factorization_times_prime Multiset.union_commute)
eberlm@63498
  1278
qed simp_all
eberlm@63498
  1279
eberlm@63498
  1280
lemma prime_factorization_cong:
eberlm@63498
  1281
  "normalize x = normalize y \<Longrightarrow> prime_factorization x = prime_factorization y"
eberlm@63498
  1282
  by (simp add: multiset_eq_iff count_prime_factorization
eberlm@63498
  1283
                multiplicity_normalize_right [of _ x, symmetric]
eberlm@63498
  1284
                multiplicity_normalize_right [of _ y, symmetric]
eberlm@63498
  1285
           del:  multiplicity_normalize_right)
eberlm@63498
  1286
eberlm@63498
  1287
lemma prime_factorization_unique:
eberlm@63498
  1288
  assumes "x \<noteq> 0" "y \<noteq> 0"
eberlm@63498
  1289
  shows   "prime_factorization x = prime_factorization y \<longleftrightarrow> normalize x = normalize y"
eberlm@63498
  1290
proof
eberlm@63498
  1291
  assume "prime_factorization x = prime_factorization y"
nipkow@63830
  1292
  hence "prod_mset (prime_factorization x) = prod_mset (prime_factorization y)" by simp
nipkow@63830
  1293
  with assms show "normalize x = normalize y" by (simp add: prod_mset_prime_factorization)
eberlm@63498
  1294
qed (rule prime_factorization_cong)
eberlm@63498
  1295
eberlm@63498
  1296
lemma prime_factorization_mult:
eberlm@63498
  1297
  assumes "x \<noteq> 0" "y \<noteq> 0"
eberlm@63498
  1298
  shows   "prime_factorization (x * y) = prime_factorization x + prime_factorization y"
eberlm@63498
  1299
proof -
nipkow@63830
  1300
  have "prime_factorization (prod_mset (prime_factorization (x * y))) =
nipkow@63830
  1301
          prime_factorization (prod_mset (prime_factorization x + prime_factorization y))"
nipkow@63830
  1302
    by (simp add: prod_mset_prime_factorization assms normalize_mult)
nipkow@63830
  1303
  also have "prime_factorization (prod_mset (prime_factorization (x * y))) =
eberlm@63498
  1304
               prime_factorization (x * y)"
haftmann@63905
  1305
    by (rule prime_factorization_prod_mset_primes) (simp_all add: in_prime_factors_imp_prime)
nipkow@63830
  1306
  also have "prime_factorization (prod_mset (prime_factorization x + prime_factorization y)) =
eberlm@63498
  1307
               prime_factorization x + prime_factorization y"
haftmann@63905
  1308
    by (rule prime_factorization_prod_mset_primes) (auto simp: in_prime_factors_imp_prime)
eberlm@63498
  1309
  finally show ?thesis .
haftmann@62499
  1310
qed
haftmann@62499
  1311
eberlm@66276
  1312
lemma prime_factorization_prod:
eberlm@66276
  1313
  assumes "finite A" "\<And>x. x \<in> A \<Longrightarrow> f x \<noteq> 0"
eberlm@66276
  1314
  shows   "prime_factorization (prod f A) = (\<Sum>n\<in>A. prime_factorization (f n))"
eberlm@66276
  1315
  using assms by (induction A rule: finite_induct)
eberlm@66276
  1316
                 (auto simp: Sup_multiset_empty prime_factorization_mult)
eberlm@66276
  1317
haftmann@63924
  1318
lemma prime_elem_multiplicity_mult_distrib:
haftmann@63924
  1319
  assumes "prime_elem p" "x \<noteq> 0" "y \<noteq> 0"
haftmann@63924
  1320
  shows   "multiplicity p (x * y) = multiplicity p x + multiplicity p y"
haftmann@63924
  1321
proof -
haftmann@63924
  1322
  have "multiplicity p (x * y) = count (prime_factorization (x * y)) (normalize p)"
haftmann@63924
  1323
    by (subst count_prime_factorization_prime) (simp_all add: assms)
wenzelm@65552
  1324
  also from assms
haftmann@63924
  1325
    have "prime_factorization (x * y) = prime_factorization x + prime_factorization y"
haftmann@63924
  1326
      by (intro prime_factorization_mult)
wenzelm@65552
  1327
  also have "count \<dots> (normalize p) =
haftmann@63924
  1328
    count (prime_factorization x) (normalize p) + count (prime_factorization y) (normalize p)"
haftmann@63924
  1329
    by simp
wenzelm@65552
  1330
  also have "\<dots> = multiplicity p x + multiplicity p y"
haftmann@63924
  1331
    by (subst (1 2) count_prime_factorization_prime) (simp_all add: assms)
haftmann@63924
  1332
  finally show ?thesis .
haftmann@63924
  1333
qed
haftmann@63924
  1334
haftmann@63924
  1335
lemma prime_elem_multiplicity_prod_mset_distrib:
haftmann@63924
  1336
  assumes "prime_elem p" "0 \<notin># A"
haftmann@63924
  1337
  shows   "multiplicity p (prod_mset A) = sum_mset (image_mset (multiplicity p) A)"
haftmann@63924
  1338
  using assms by (induction A) (auto simp: prime_elem_multiplicity_mult_distrib)
haftmann@63924
  1339
haftmann@63924
  1340
lemma prime_elem_multiplicity_power_distrib:
haftmann@63924
  1341
  assumes "prime_elem p" "x \<noteq> 0"
haftmann@63924
  1342
  shows   "multiplicity p (x ^ n) = n * multiplicity p x"
haftmann@63924
  1343
  using assms prime_elem_multiplicity_prod_mset_distrib [of p "replicate_mset n x"]
haftmann@63924
  1344
  by simp
haftmann@63924
  1345
nipkow@64272
  1346
lemma prime_elem_multiplicity_prod_distrib:
haftmann@63924
  1347
  assumes "prime_elem p" "0 \<notin> f ` A" "finite A"
nipkow@64272
  1348
  shows   "multiplicity p (prod f A) = (\<Sum>x\<in>A. multiplicity p (f x))"
haftmann@63924
  1349
proof -
nipkow@64272
  1350
  have "multiplicity p (prod f A) = (\<Sum>x\<in>#mset_set A. multiplicity p (f x))"
nipkow@64272
  1351
    using assms by (subst prod_unfold_prod_mset)
wenzelm@65552
  1352
                   (simp_all add: prime_elem_multiplicity_prod_mset_distrib sum_unfold_sum_mset
haftmann@63924
  1353
                      multiset.map_comp o_def)
haftmann@63924
  1354
  also from \<open>finite A\<close> have "\<dots> = (\<Sum>x\<in>A. multiplicity p (f x))"
haftmann@63924
  1355
    by (induction A rule: finite_induct) simp_all
haftmann@63924
  1356
  finally show ?thesis .
haftmann@63924
  1357
qed
haftmann@63924
  1358
haftmann@63924
  1359
lemma multiplicity_distinct_prime_power:
haftmann@63924
  1360
  "prime p \<Longrightarrow> prime q \<Longrightarrow> p \<noteq> q \<Longrightarrow> multiplicity p (q ^ n) = 0"
haftmann@63924
  1361
  by (subst prime_elem_multiplicity_power_distrib) (auto simp: prime_multiplicity_other)
haftmann@63924
  1362
eberlm@63498
  1363
lemma prime_factorization_prime_power:
eberlm@63633
  1364
  "prime p \<Longrightarrow> prime_factorization (p ^ n) = replicate_mset n p"
eberlm@63498
  1365
  by (induction n)
eberlm@63498
  1366
     (simp_all add: prime_factorization_mult prime_factorization_prime Multiset.union_commute)
eberlm@63498
  1367
nipkow@63830
  1368
lemma prime_decomposition: "unit_factor x * prod_mset (prime_factorization x) = x"
nipkow@63830
  1369
  by (cases "x = 0") (simp_all add: prod_mset_prime_factorization)
eberlm@63498
  1370
eberlm@63498
  1371
lemma prime_factorization_subset_iff_dvd:
eberlm@63498
  1372
  assumes [simp]: "x \<noteq> 0" "y \<noteq> 0"
eberlm@63498
  1373
  shows   "prime_factorization x \<subseteq># prime_factorization y \<longleftrightarrow> x dvd y"
eberlm@63498
  1374
proof -
nipkow@63830
  1375
  have "x dvd y \<longleftrightarrow> prod_mset (prime_factorization x) dvd prod_mset (prime_factorization y)"
nipkow@63830
  1376
    by (simp add: prod_mset_prime_factorization)
eberlm@63498
  1377
  also have "\<dots> \<longleftrightarrow> prime_factorization x \<subseteq># prime_factorization y"
haftmann@63905
  1378
    by (auto intro!: prod_mset_primes_dvd_imp_subset prod_mset_subset_imp_dvd)
eberlm@63498
  1379
  finally show ?thesis ..
eberlm@63498
  1380
qed
eberlm@63498
  1381
wenzelm@65552
  1382
lemma prime_factorization_subset_imp_dvd:
eberlm@63534
  1383
  "x \<noteq> 0 \<Longrightarrow> (prime_factorization x \<subseteq># prime_factorization y) \<Longrightarrow> x dvd y"
eberlm@63534
  1384
  by (cases "y = 0") (simp_all add: prime_factorization_subset_iff_dvd)
eberlm@63534
  1385
eberlm@63498
  1386
lemma prime_factorization_divide:
eberlm@63498
  1387
  assumes "b dvd a"
eberlm@63498
  1388
  shows   "prime_factorization (a div b) = prime_factorization a - prime_factorization b"
eberlm@63498
  1389
proof (cases "a = 0")
eberlm@63498
  1390
  case [simp]: False
eberlm@63498
  1391
  from assms have [simp]: "b \<noteq> 0" by auto
eberlm@63498
  1392
  have "prime_factorization ((a div b) * b) = prime_factorization (a div b) + prime_factorization b"
eberlm@63498
  1393
    by (intro prime_factorization_mult) (insert assms, auto elim!: dvdE)
eberlm@63498
  1394
  with assms show ?thesis by simp
eberlm@63498
  1395
qed simp_all
eberlm@63498
  1396
haftmann@63905
  1397
lemma zero_not_in_prime_factors [simp]: "0 \<notin> prime_factors x"
haftmann@63905
  1398
  by (auto dest: in_prime_factors_imp_prime)
eberlm@63498
  1399
haftmann@63904
  1400
lemma prime_prime_factors:
haftmann@63905
  1401
  "prime p \<Longrightarrow> prime_factors p = {p}"
haftmann@63905
  1402
  by (drule prime_factorization_prime) simp
eberlm@63534
  1403
nipkow@64272
  1404
lemma prod_prime_factors:
eberlm@63534
  1405
  assumes "x \<noteq> 0"
eberlm@63534
  1406
  shows   "(\<Prod>p \<in> prime_factors x. p ^ multiplicity p x) = normalize x"
eberlm@63534
  1407
proof -
nipkow@63830
  1408
  have "normalize x = prod_mset (prime_factorization x)"
nipkow@63830
  1409
    by (simp add: prod_mset_prime_factorization assms)
eberlm@63534
  1410
  also have "\<dots> = (\<Prod>p \<in> prime_factors x. p ^ count (prime_factorization x) p)"
haftmann@63905
  1411
    by (subst prod_mset_multiplicity) simp_all
eberlm@63534
  1412
  also have "\<dots> = (\<Prod>p \<in> prime_factors x. p ^ multiplicity p x)"
wenzelm@65552
  1413
    by (intro prod.cong)
haftmann@63905
  1414
      (simp_all add: assms count_prime_factorization_prime in_prime_factors_imp_prime)
eberlm@63534
  1415
  finally show ?thesis ..
eberlm@63534
  1416
qed
eberlm@63534
  1417
eberlm@63534
  1418
lemma prime_factorization_unique'':
eberlm@63534
  1419
  assumes S_eq: "S = {p. 0 < f p}"
eberlm@63534
  1420
    and "finite S"
eberlm@63633
  1421
    and S: "\<forall>p\<in>S. prime p" "normalize n = (\<Prod>p\<in>S. p ^ f p)"
eberlm@63633
  1422
  shows "S = prime_factors n \<and> (\<forall>p. prime p \<longrightarrow> f p = multiplicity p n)"
eberlm@63534
  1423
proof
eberlm@63534
  1424
  define A where "A = Abs_multiset f"
eberlm@63534
  1425
  from \<open>finite S\<close> S(1) have "(\<Prod>p\<in>S. p ^ f p) \<noteq> 0" by auto
eberlm@63534
  1426
  with S(2) have nz: "n \<noteq> 0" by auto
haftmann@66938
  1427
  from S_eq \<open>finite S\<close> have count_A: "count A = f"
eberlm@63534
  1428
    unfolding A_def by (subst multiset.Abs_multiset_inverse) (simp_all add: multiset_def)
eberlm@63534
  1429
  from S_eq count_A have set_mset_A: "set_mset A = S"
eberlm@63534
  1430
    by (simp only: set_mset_def)
eberlm@63534
  1431
  from S(2) have "normalize n = (\<Prod>p\<in>S. p ^ f p)" .
nipkow@63830
  1432
  also have "\<dots> = prod_mset A" by (simp add: prod_mset_multiplicity S_eq set_mset_A count_A)
wenzelm@65552
  1433
  also from nz have "normalize n = prod_mset (prime_factorization n)"
nipkow@63830
  1434
    by (simp add: prod_mset_prime_factorization)
wenzelm@65552
  1435
  finally have "prime_factorization (prod_mset A) =
nipkow@63830
  1436
                  prime_factorization (prod_mset (prime_factorization n))" by simp
nipkow@63830
  1437
  also from S(1) have "prime_factorization (prod_mset A) = A"
nipkow@63830
  1438
    by (intro prime_factorization_prod_mset_primes) (auto simp: set_mset_A)
nipkow@63830
  1439
  also have "prime_factorization (prod_mset (prime_factorization n)) = prime_factorization n"
haftmann@63905
  1440
    by (intro prime_factorization_prod_mset_primes) auto
haftmann@63905
  1441
  finally show "S = prime_factors n" by (simp add: set_mset_A [symmetric])
wenzelm@65552
  1442
eberlm@63633
  1443
  show "(\<forall>p. prime p \<longrightarrow> f p = multiplicity p n)"
eberlm@63534
  1444
  proof safe
eberlm@63633
  1445
    fix p :: 'a assume p: "prime p"
eberlm@63534
  1446
    have "multiplicity p n = multiplicity p (normalize n)" by simp
wenzelm@65552
  1447
    also have "normalize n = prod_mset A"
nipkow@63830
  1448
      by (simp add: prod_mset_multiplicity S_eq set_mset_A count_A S)
wenzelm@65552
  1449
    also from p set_mset_A S(1)
nipkow@63830
  1450
    have "multiplicity p \<dots> = sum_mset (image_mset (multiplicity p) A)"
nipkow@63830
  1451
      by (intro prime_elem_multiplicity_prod_mset_distrib) auto
eberlm@63534
  1452
    also from S(1) p
eberlm@63534
  1453
    have "image_mset (multiplicity p) A = image_mset (\<lambda>q. if p = q then 1 else 0) A"
eberlm@63534
  1454
      by (intro image_mset_cong) (auto simp: set_mset_A multiplicity_self prime_multiplicity_other)
haftmann@66938
  1455
    also have "sum_mset \<dots> = f p"
haftmann@66938
  1456
      by (simp add: semiring_1_class.sum_mset_delta' count_A)
eberlm@63534
  1457
    finally show "f p = multiplicity p n" ..
eberlm@63534
  1458
  qed
eberlm@63534
  1459
qed
eberlm@63534
  1460
wenzelm@65552
  1461
lemma prime_factors_product:
eberlm@63534
  1462
  "x \<noteq> 0 \<Longrightarrow> y \<noteq> 0 \<Longrightarrow> prime_factors (x * y) = prime_factors x \<union> prime_factors y"
haftmann@63905
  1463
  by (simp add: prime_factorization_mult)
eberlm@63534
  1464
eberlm@63534
  1465
lemma dvd_prime_factors [intro]:
eberlm@63534
  1466
  "y \<noteq> 0 \<Longrightarrow> x dvd y \<Longrightarrow> prime_factors x \<subseteq> prime_factors y"
eberlm@63534
  1467
  by (intro set_mset_mono, subst prime_factorization_subset_iff_dvd) auto
eberlm@63534
  1468
eberlm@63534
  1469
(* RENAMED multiplicity_dvd *)
eberlm@63534
  1470
lemma multiplicity_le_imp_dvd:
eberlm@63633
  1471
  assumes "x \<noteq> 0" "\<And>p. prime p \<Longrightarrow> multiplicity p x \<le> multiplicity p y"
eberlm@63534
  1472
  shows   "x dvd y"
eberlm@63534
  1473
proof (cases "y = 0")
eberlm@63534
  1474
  case False
eberlm@63534
  1475
  from assms this have "prime_factorization x \<subseteq># prime_factorization y"
eberlm@63534
  1476
    by (intro mset_subset_eqI) (auto simp: count_prime_factorization)
eberlm@63534
  1477
  with assms False show ?thesis by (subst (asm) prime_factorization_subset_iff_dvd)
eberlm@63534
  1478
qed auto
eberlm@63534
  1479
eberlm@63534
  1480
lemma dvd_multiplicity_eq:
eberlm@63534
  1481
  "x \<noteq> 0 \<Longrightarrow> y \<noteq> 0 \<Longrightarrow> x dvd y \<longleftrightarrow> (\<forall>p. multiplicity p x \<le> multiplicity p y)"
eberlm@63534
  1482
  by (auto intro: dvd_imp_multiplicity_le multiplicity_le_imp_dvd)
eberlm@63534
  1483
eberlm@63534
  1484
lemma multiplicity_eq_imp_eq:
eberlm@63534
  1485
  assumes "x \<noteq> 0" "y \<noteq> 0"
eberlm@63633
  1486
  assumes "\<And>p. prime p \<Longrightarrow> multiplicity p x = multiplicity p y"
eberlm@63534
  1487
  shows   "normalize x = normalize y"
eberlm@63534
  1488
  using assms by (intro associatedI multiplicity_le_imp_dvd) simp_all
eberlm@63534
  1489
eberlm@63534
  1490
lemma prime_factorization_unique':
eberlm@63633
  1491
  assumes "\<forall>p \<in># M. prime p" "\<forall>p \<in># N. prime p" "(\<Prod>i \<in># M. i) = (\<Prod>i \<in># N. i)"
eberlm@63534
  1492
  shows   "M = N"
eberlm@63534
  1493
proof -
eberlm@63534
  1494
  have "prime_factorization (\<Prod>i \<in># M. i) = prime_factorization (\<Prod>i \<in># N. i)"
eberlm@63534
  1495
    by (simp only: assms)
eberlm@63534
  1496
  also from assms have "prime_factorization (\<Prod>i \<in># M. i) = M"
nipkow@63830
  1497
    by (subst prime_factorization_prod_mset_primes) simp_all
eberlm@63534
  1498
  also from assms have "prime_factorization (\<Prod>i \<in># N. i) = N"
nipkow@63830
  1499
    by (subst prime_factorization_prod_mset_primes) simp_all
eberlm@63534
  1500
  finally show ?thesis .
eberlm@63534
  1501
qed
eberlm@63534
  1502
eberlm@63537
  1503
lemma multiplicity_cong:
eberlm@63537
  1504
  "(\<And>r. p ^ r dvd a \<longleftrightarrow> p ^ r dvd b) \<Longrightarrow> multiplicity p a = multiplicity p b"
eberlm@63537
  1505
  by (simp add: multiplicity_def)
eberlm@63537
  1506
wenzelm@65552
  1507
lemma not_dvd_imp_multiplicity_0:
eberlm@63537
  1508
  assumes "\<not>p dvd x"
eberlm@63537
  1509
  shows   "multiplicity p x = 0"
eberlm@63537
  1510
proof -
eberlm@63537
  1511
  from assms have "multiplicity p x < 1"
eberlm@63537
  1512
    by (intro multiplicity_lessI) auto
eberlm@63537
  1513
  thus ?thesis by simp
eberlm@63537
  1514
qed
eberlm@63537
  1515
eberlm@66276
  1516
lemma inj_on_Prod_primes:
eberlm@66276
  1517
  assumes "\<And>P p. P \<in> A \<Longrightarrow> p \<in> P \<Longrightarrow> prime p"
eberlm@66276
  1518
  assumes "\<And>P. P \<in> A \<Longrightarrow> finite P"
eberlm@66276
  1519
  shows   "inj_on Prod A"
eberlm@66276
  1520
proof (rule inj_onI)
eberlm@66276
  1521
  fix P Q assume PQ: "P \<in> A" "Q \<in> A" "\<Prod>P = \<Prod>Q"
eberlm@66276
  1522
  with prime_factorization_unique'[of "mset_set P" "mset_set Q"] assms[of P] assms[of Q]
eberlm@66276
  1523
    have "mset_set P = mset_set Q" by (auto simp: prod_unfold_prod_mset)
eberlm@66276
  1524
    with assms[of P] assms[of Q] PQ show "P = Q" by simp
eberlm@66276
  1525
qed
eberlm@66276
  1526
eberlm@66276
  1527
eberlm@63534
  1528
haftmann@63924
  1529
subsection \<open>GCD and LCM computation with unique factorizations\<close>
haftmann@63924
  1530
eberlm@63498
  1531
definition "gcd_factorial a b = (if a = 0 then normalize b
eberlm@63498
  1532
     else if b = 0 then normalize a
Mathias@63919
  1533
     else prod_mset (prime_factorization a \<inter># prime_factorization b))"
eberlm@63498
  1534
eberlm@63498
  1535
definition "lcm_factorial a b = (if a = 0 \<or> b = 0 then 0
Mathias@63919
  1536
     else prod_mset (prime_factorization a \<union># prime_factorization b))"
eberlm@63498
  1537
eberlm@63498
  1538
definition "Gcd_factorial A =
nipkow@63830
  1539
  (if A \<subseteq> {0} then 0 else prod_mset (Inf (prime_factorization ` (A - {0}))))"
eberlm@63498
  1540
eberlm@63498
  1541
definition "Lcm_factorial A =
eberlm@63498
  1542
  (if A = {} then 1
eberlm@63498
  1543
   else if 0 \<notin> A \<and> subset_mset.bdd_above (prime_factorization ` (A - {0})) then
nipkow@63830
  1544
     prod_mset (Sup (prime_factorization ` A))
eberlm@63498
  1545
   else
eberlm@63498
  1546
     0)"
eberlm@63498
  1547
eberlm@63498
  1548
lemma prime_factorization_gcd_factorial:
eberlm@63498
  1549
  assumes [simp]: "a \<noteq> 0" "b \<noteq> 0"
Mathias@63919
  1550
  shows   "prime_factorization (gcd_factorial a b) = prime_factorization a \<inter># prime_factorization b"
eberlm@63498
  1551
proof -
eberlm@63498
  1552
  have "prime_factorization (gcd_factorial a b) =
Mathias@63919
  1553
          prime_factorization (prod_mset (prime_factorization a \<inter># prime_factorization b))"
eberlm@63498
  1554
    by (simp add: gcd_factorial_def)
Mathias@63919
  1555
  also have "\<dots> = prime_factorization a \<inter># prime_factorization b"
haftmann@63905
  1556
    by (subst prime_factorization_prod_mset_primes) auto
eberlm@63498
  1557
  finally show ?thesis .
eberlm@63498
  1558
qed
eberlm@63498
  1559
eberlm@63498
  1560
lemma prime_factorization_lcm_factorial:
eberlm@63498
  1561
  assumes [simp]: "a \<noteq> 0" "b \<noteq> 0"
Mathias@63919
  1562
  shows   "prime_factorization (lcm_factorial a b) = prime_factorization a \<union># prime_factorization b"
eberlm@63498
  1563
proof -
eberlm@63498
  1564
  have "prime_factorization (lcm_factorial a b) =
Mathias@63919
  1565
          prime_factorization (prod_mset (prime_factorization a \<union># prime_factorization b))"
eberlm@63498
  1566
    by (simp add: lcm_factorial_def)
Mathias@63919
  1567
  also have "\<dots> = prime_factorization a \<union># prime_factorization b"
haftmann@63905
  1568
    by (subst prime_factorization_prod_mset_primes) auto
eberlm@63498
  1569
  finally show ?thesis .
eberlm@63498
  1570
qed
eberlm@63498
  1571
eberlm@63498
  1572
lemma prime_factorization_Gcd_factorial:
eberlm@63498
  1573
  assumes "\<not>A \<subseteq> {0}"
eberlm@63498
  1574
  shows   "prime_factorization (Gcd_factorial A) = Inf (prime_factorization ` (A - {0}))"
eberlm@63498
  1575
proof -
eberlm@63498
  1576
  from assms obtain x where x: "x \<in> A - {0}" by auto
eberlm@63498
  1577
  hence "Inf (prime_factorization ` (A - {0})) \<subseteq># prime_factorization x"
eberlm@63498
  1578
    by (intro subset_mset.cInf_lower) simp_all
haftmann@63905
  1579
  hence "\<forall>y. y \<in># Inf (prime_factorization ` (A - {0})) \<longrightarrow> y \<in> prime_factors x"
eberlm@63498
  1580
    by (auto dest: mset_subset_eqD)
haftmann@63905
  1581
  with in_prime_factors_imp_prime[of _ x]
eberlm@63633
  1582
    have "\<forall>p. p \<in># Inf (prime_factorization ` (A - {0})) \<longrightarrow> prime p" by blast
eberlm@63498
  1583
  with assms show ?thesis
nipkow@63830
  1584
    by (simp add: Gcd_factorial_def prime_factorization_prod_mset_primes)
eberlm@63498
  1585
qed
eberlm@63498
  1586
eberlm@63498
  1587
lemma prime_factorization_Lcm_factorial:
eberlm@63498
  1588
  assumes "0 \<notin> A" "subset_mset.bdd_above (prime_factorization ` A)"
eberlm@63498
  1589
  shows   "prime_factorization (Lcm_factorial A) = Sup (prime_factorization ` A)"
eberlm@63498
  1590
proof (cases "A = {}")
eberlm@63498
  1591
  case True
eberlm@63498
  1592
  hence "prime_factorization ` A = {}" by auto
eberlm@63498
  1593
  also have "Sup \<dots> = {#}" by (simp add: Sup_multiset_empty)
eberlm@63498
  1594
  finally show ?thesis by (simp add: Lcm_factorial_def)
eberlm@63498
  1595
next
eberlm@63498
  1596
  case False
eberlm@63633
  1597
  have "\<forall>y. y \<in># Sup (prime_factorization ` A) \<longrightarrow> prime y"
haftmann@63905
  1598
    by (auto simp: in_Sup_multiset_iff assms)
eberlm@63498
  1599
  with assms False show ?thesis
nipkow@63830
  1600
    by (simp add: Lcm_factorial_def prime_factorization_prod_mset_primes)
eberlm@63498
  1601
qed
eberlm@63498
  1602
eberlm@63498
  1603
lemma gcd_factorial_commute: "gcd_factorial a b = gcd_factorial b a"
eberlm@63498
  1604
  by (simp add: gcd_factorial_def multiset_inter_commute)
eberlm@63498
  1605
eberlm@63498
  1606
lemma gcd_factorial_dvd1: "gcd_factorial a b dvd a"
eberlm@63498
  1607
proof (cases "a = 0 \<or> b = 0")
eberlm@63498
  1608
  case False
eberlm@63498
  1609
  hence "gcd_factorial a b \<noteq> 0" by (auto simp: gcd_factorial_def)
eberlm@63498
  1610
  with False show ?thesis
eberlm@63498
  1611
    by (subst prime_factorization_subset_iff_dvd [symmetric])
eberlm@63498
  1612
       (auto simp: prime_factorization_gcd_factorial)
eberlm@63498
  1613
qed (auto simp: gcd_factorial_def)
eberlm@63498
  1614
eberlm@63498
  1615
lemma gcd_factorial_dvd2: "gcd_factorial a b dvd b"
eberlm@63498
  1616
  by (subst gcd_factorial_commute) (rule gcd_factorial_dvd1)
eberlm@63498
  1617
eberlm@63498
  1618
lemma normalize_gcd_factorial: "normalize (gcd_factorial a b) = gcd_factorial a b"
eberlm@63498
  1619
proof -
Mathias@63919
  1620
  have "normalize (prod_mset (prime_factorization a \<inter># prime_factorization b)) =
Mathias@63919
  1621
          prod_mset (prime_factorization a \<inter># prime_factorization b)"
haftmann@63905
  1622
    by (intro normalize_prod_mset_primes) auto
eberlm@63498
  1623
  thus ?thesis by (simp add: gcd_factorial_def)
eberlm@63498
  1624
qed
eberlm@63498
  1625
eberlm@63498
  1626
lemma gcd_factorial_greatest: "c dvd gcd_factorial a b" if "c dvd a" "c dvd b" for a b c
eberlm@63498
  1627
proof (cases "a = 0 \<or> b = 0")
eberlm@63498
  1628
  case False
eberlm@63498
  1629
  with that have [simp]: "c \<noteq> 0" by auto
eberlm@63498
  1630
  let ?p = "prime_factorization"
eberlm@63498
  1631
  from that False have "?p c \<subseteq># ?p a" "?p c \<subseteq># ?p b"
eberlm@63498
  1632
    by (simp_all add: prime_factorization_subset_iff_dvd)
eberlm@63498
  1633
  hence "prime_factorization c \<subseteq>#
Mathias@63919
  1634
           prime_factorization (prod_mset (prime_factorization a \<inter># prime_factorization b))"
haftmann@63905
  1635
    using False by (subst prime_factorization_prod_mset_primes) auto
eberlm@63498
  1636
  with False show ?thesis
eberlm@63498
  1637
    by (auto simp: gcd_factorial_def prime_factorization_subset_iff_dvd [symmetric])
eberlm@63498
  1638
qed (auto simp: gcd_factorial_def that)
eberlm@63498
  1639
eberlm@63498
  1640
lemma lcm_factorial_gcd_factorial:
eberlm@63498
  1641
  "lcm_factorial a b = normalize (a * b) div gcd_factorial a b" for a b
eberlm@63498
  1642
proof (cases "a = 0 \<or> b = 0")
eberlm@63498
  1643
  case False
eberlm@63498
  1644
  let ?p = "prime_factorization"
nipkow@63830
  1645
  from False have "prod_mset (?p (a * b)) div gcd_factorial a b =
Mathias@63919
  1646
                     prod_mset (?p a + ?p b - ?p a \<inter># ?p b)"
nipkow@63830
  1647
    by (subst prod_mset_diff) (auto simp: lcm_factorial_def gcd_factorial_def
eberlm@63498
  1648
                                prime_factorization_mult subset_mset.le_infI1)
nipkow@63830
  1649
  also from False have "prod_mset (?p (a * b)) = normalize (a * b)"
nipkow@63830
  1650
    by (intro prod_mset_prime_factorization) simp_all
Mathias@63919
  1651
  also from False have "prod_mset (?p a + ?p b - ?p a \<inter># ?p b) = lcm_factorial a b"
eberlm@63498
  1652
    by (simp add: union_diff_inter_eq_sup lcm_factorial_def)
eberlm@63498
  1653
  finally show ?thesis ..
eberlm@63498
  1654
qed (auto simp: lcm_factorial_def)
eberlm@63498
  1655
eberlm@63498
  1656
lemma normalize_Gcd_factorial:
eberlm@63498
  1657
  "normalize (Gcd_factorial A) = Gcd_factorial A"
eberlm@63498
  1658
proof (cases "A \<subseteq> {0}")
eberlm@63498
  1659
  case False
eberlm@63498
  1660
  then obtain x where "x \<in> A" "x \<noteq> 0" by blast
eberlm@63498
  1661
  hence "Inf (prime_factorization ` (A - {0})) \<subseteq># prime_factorization x"
eberlm@63498
  1662
    by (intro subset_mset.cInf_lower) auto
eberlm@63633
  1663
  hence "prime p" if "p \<in># Inf (prime_factorization ` (A - {0}))" for p
haftmann@63905
  1664
    using that by (auto dest: mset_subset_eqD)
eberlm@63498
  1665
  with False show ?thesis
nipkow@63830
  1666
    by (auto simp add: Gcd_factorial_def normalize_prod_mset_primes)
eberlm@63498
  1667
qed (simp_all add: Gcd_factorial_def)
eberlm@63498
  1668
eberlm@63498
  1669
lemma Gcd_factorial_eq_0_iff:
eberlm@63498
  1670
  "Gcd_factorial A = 0 \<longleftrightarrow> A \<subseteq> {0}"
eberlm@63498
  1671
  by (auto simp: Gcd_factorial_def in_Inf_multiset_iff split: if_splits)
eberlm@63498
  1672
eberlm@63498
  1673
lemma Gcd_factorial_dvd:
eberlm@63498
  1674
  assumes "x \<in> A"
eberlm@63498
  1675
  shows   "Gcd_factorial A dvd x"
eberlm@63498
  1676
proof (cases "x = 0")
eberlm@63498
  1677
  case False
eberlm@63498
  1678
  with assms have "prime_factorization (Gcd_factorial A) = Inf (prime_factorization ` (A - {0}))"
eberlm@63498
  1679
    by (intro prime_factorization_Gcd_factorial) auto
eberlm@63498
  1680
  also from False assms have "\<dots> \<subseteq># prime_factorization x"
eberlm@63498
  1681
    by (intro subset_mset.cInf_lower) auto
eberlm@63498
  1682
  finally show ?thesis
eberlm@63498
  1683
    by (subst (asm) prime_factorization_subset_iff_dvd)
eberlm@63498
  1684
       (insert assms False, auto simp: Gcd_factorial_eq_0_iff)
eberlm@63498
  1685
qed simp_all
eberlm@63498
  1686
eberlm@63498
  1687
lemma Gcd_factorial_greatest:
eberlm@63498
  1688
  assumes "\<And>y. y \<in> A \<Longrightarrow> x dvd y"
eberlm@63498
  1689
  shows   "x dvd Gcd_factorial A"
eberlm@63498
  1690
proof (cases "A \<subseteq> {0}")
eberlm@63498
  1691
  case False
eberlm@63498
  1692
  from False obtain y where "y \<in> A" "y \<noteq> 0" by auto
eberlm@63498
  1693
  with assms[of y] have nz: "x \<noteq> 0" by auto
eberlm@63498
  1694
  from nz assms have "prime_factorization x \<subseteq># prime_factorization y" if "y \<in> A - {0}" for y
eberlm@63498
  1695
    using that by (subst prime_factorization_subset_iff_dvd) auto
eberlm@63498
  1696
  with False have "prime_factorization x \<subseteq># Inf (prime_factorization ` (A - {0}))"
eberlm@63498
  1697
    by (intro subset_mset.cInf_greatest) auto
eberlm@63498
  1698
  also from False have "\<dots> = prime_factorization (Gcd_factorial A)"
eberlm@63498
  1699
    by (rule prime_factorization_Gcd_factorial [symmetric])
eberlm@63498
  1700
  finally show ?thesis
eberlm@63498
  1701
    by (subst (asm) prime_factorization_subset_iff_dvd)
eberlm@63498
  1702
       (insert nz False, auto simp: Gcd_factorial_eq_0_iff)
eberlm@63498
  1703
qed (simp_all add: Gcd_factorial_def)
eberlm@63498
  1704
eberlm@63498
  1705
lemma normalize_Lcm_factorial:
eberlm@63498
  1706
  "normalize (Lcm_factorial A) = Lcm_factorial A"
eberlm@63498
  1707
proof (cases "subset_mset.bdd_above (prime_factorization ` A)")
eberlm@63498
  1708
  case True
nipkow@63830
  1709
  hence "normalize (prod_mset (Sup (prime_factorization ` A))) =
nipkow@63830
  1710
           prod_mset (Sup (prime_factorization ` A))"
nipkow@63830
  1711
    by (intro normalize_prod_mset_primes)
haftmann@63905
  1712
       (auto simp: in_Sup_multiset_iff)
eberlm@63498
  1713
  with True show ?thesis by (simp add: Lcm_factorial_def)
eberlm@63498
  1714
qed (auto simp: Lcm_factorial_def)
eberlm@63498
  1715
eberlm@63498
  1716
lemma Lcm_factorial_eq_0_iff:
eberlm@63498
  1717
  "Lcm_factorial A = 0 \<longleftrightarrow> 0 \<in> A \<or> \<not>subset_mset.bdd_above (prime_factorization ` A)"
eberlm@63498
  1718
  by (auto simp: Lcm_factorial_def in_Sup_multiset_iff)
eberlm@63498
  1719
eberlm@63498
  1720
lemma dvd_Lcm_factorial:
eberlm@63498
  1721
  assumes "x \<in> A"
eberlm@63498
  1722
  shows   "x dvd Lcm_factorial A"
eberlm@63498
  1723
proof (cases "0 \<notin> A \<and> subset_mset.bdd_above (prime_factorization ` A)")
eberlm@63498
  1724
  case True
eberlm@63498
  1725
  with assms have [simp]: "0 \<notin> A" "x \<noteq> 0" "A \<noteq> {}" by auto
eberlm@63498
  1726
  from assms True have "prime_factorization x \<subseteq># Sup (prime_factorization ` A)"
eberlm@63498
  1727
    by (intro subset_mset.cSup_upper) auto
eberlm@63498
  1728
  also have "\<dots> = prime_factorization (Lcm_factorial A)"
eberlm@63498
  1729
    by (rule prime_factorization_Lcm_factorial [symmetric]) (insert True, simp_all)
eberlm@63498
  1730
  finally show ?thesis
eberlm@63498
  1731
    by (subst (asm) prime_factorization_subset_iff_dvd)
eberlm@63498
  1732
       (insert True, auto simp: Lcm_factorial_eq_0_iff)
eberlm@63498
  1733
qed (insert assms, auto simp: Lcm_factorial_def)
eberlm@63498
  1734
eberlm@63498
  1735
lemma Lcm_factorial_least:
eberlm@63498
  1736
  assumes "\<And>y. y \<in> A \<Longrightarrow> y dvd x"
eberlm@63498
  1737
  shows   "Lcm_factorial A dvd x"
eberlm@63498
  1738
proof -
eberlm@63498
  1739
  consider "A = {}" | "0 \<in> A" | "x = 0" | "A \<noteq> {}" "0 \<notin> A" "x \<noteq> 0" by blast
eberlm@63498
  1740
  thus ?thesis
eberlm@63498
  1741
  proof cases
eberlm@63498
  1742
    assume *: "A \<noteq> {}" "0 \<notin> A" "x \<noteq> 0"
eberlm@63498
  1743
    hence nz: "x \<noteq> 0" if "x \<in> A" for x using that by auto
eberlm@63498
  1744
    from * have bdd: "subset_mset.bdd_above (prime_factorization ` A)"
eberlm@63498
  1745
      by (intro subset_mset.bdd_aboveI[of _ "prime_factorization x"])
eberlm@63498
  1746
         (auto simp: prime_factorization_subset_iff_dvd nz dest: assms)
eberlm@63498
  1747
    have "prime_factorization (Lcm_factorial A) = Sup (prime_factorization ` A)"
eberlm@63498
  1748
      by (rule prime_factorization_Lcm_factorial) fact+
eberlm@63498
  1749
    also from * have "\<dots> \<subseteq># prime_factorization x"
eberlm@63498
  1750
      by (intro subset_mset.cSup_least)
eberlm@63498
  1751
         (auto simp: prime_factorization_subset_iff_dvd nz dest: assms)
eberlm@63498
  1752
    finally show ?thesis
eberlm@63498
  1753
      by (subst (asm) prime_factorization_subset_iff_dvd)
eberlm@63498
  1754
         (insert * bdd, auto simp: Lcm_factorial_eq_0_iff)
eberlm@63498
  1755
  qed (auto simp: Lcm_factorial_def dest: assms)
eberlm@63498
  1756
qed
eberlm@63498
  1757
eberlm@63498
  1758
lemmas gcd_lcm_factorial =
eberlm@63498
  1759
  gcd_factorial_dvd1 gcd_factorial_dvd2 gcd_factorial_greatest
eberlm@63498
  1760
  normalize_gcd_factorial lcm_factorial_gcd_factorial
eberlm@63498
  1761
  normalize_Gcd_factorial Gcd_factorial_dvd Gcd_factorial_greatest
eberlm@63498
  1762
  normalize_Lcm_factorial dvd_Lcm_factorial Lcm_factorial_least
eberlm@63498
  1763
haftmann@60804
  1764
end
haftmann@60804
  1765
eberlm@63498
  1766
class factorial_semiring_gcd = factorial_semiring + gcd + Gcd +
eberlm@63498
  1767
  assumes gcd_eq_gcd_factorial: "gcd a b = gcd_factorial a b"
eberlm@63498
  1768
  and     lcm_eq_lcm_factorial: "lcm a b = lcm_factorial a b"
eberlm@63498
  1769
  and     Gcd_eq_Gcd_factorial: "Gcd A = Gcd_factorial A"
eberlm@63498
  1770
  and     Lcm_eq_Lcm_factorial: "Lcm A = Lcm_factorial A"
haftmann@60804
  1771
begin
haftmann@60804
  1772
eberlm@63498
  1773
lemma prime_factorization_gcd:
eberlm@63498
  1774
  assumes [simp]: "a \<noteq> 0" "b \<noteq> 0"
Mathias@63919
  1775
  shows   "prime_factorization (gcd a b) = prime_factorization a \<inter># prime_factorization b"
eberlm@63498
  1776
  by (simp add: gcd_eq_gcd_factorial prime_factorization_gcd_factorial)
haftmann@60804
  1777
eberlm@63498
  1778
lemma prime_factorization_lcm:
eberlm@63498
  1779
  assumes [simp]: "a \<noteq> 0" "b \<noteq> 0"
Mathias@63919
  1780
  shows   "prime_factorization (lcm a b) = prime_factorization a \<union># prime_factorization b"
eberlm@63498
  1781
  by (simp add: lcm_eq_lcm_factorial prime_factorization_lcm_factorial)
haftmann@60804
  1782
eberlm@63498
  1783
lemma prime_factorization_Gcd:
eberlm@63498
  1784
  assumes "Gcd A \<noteq> 0"
eberlm@63498
  1785
  shows   "prime_factorization (Gcd A) = Inf (prime_factorization ` (A - {0}))"
eberlm@63498
  1786
  using assms
eberlm@63498
  1787
  by (simp add: prime_factorization_Gcd_factorial Gcd_eq_Gcd_factorial Gcd_factorial_eq_0_iff)
eberlm@63498
  1788
eberlm@63498
  1789
lemma prime_factorization_Lcm:
eberlm@63498
  1790
  assumes "Lcm A \<noteq> 0"
eberlm@63498
  1791
  shows   "prime_factorization (Lcm A) = Sup (prime_factorization ` A)"
eberlm@63498
  1792
  using assms
eberlm@63498
  1793
  by (simp add: prime_factorization_Lcm_factorial Lcm_eq_Lcm_factorial Lcm_factorial_eq_0_iff)
eberlm@63498
  1794
eberlm@66276
  1795
lemma prime_factors_gcd [simp]: 
eberlm@66276
  1796
  "a \<noteq> 0 \<Longrightarrow> b \<noteq> 0 \<Longrightarrow> prime_factors (gcd a b) = 
eberlm@66276
  1797
     prime_factors a \<inter> prime_factors b"
eberlm@66276
  1798
  by (subst prime_factorization_gcd) auto
eberlm@66276
  1799
eberlm@66276
  1800
lemma prime_factors_lcm [simp]: 
eberlm@66276
  1801
  "a \<noteq> 0 \<Longrightarrow> b \<noteq> 0 \<Longrightarrow> prime_factors (lcm a b) = 
eberlm@66276
  1802
     prime_factors a \<union> prime_factors b"
eberlm@66276
  1803
  by (subst prime_factorization_lcm) auto
eberlm@66276
  1804
eberlm@63498
  1805
subclass semiring_gcd
eberlm@63498
  1806
  by (standard, unfold gcd_eq_gcd_factorial lcm_eq_lcm_factorial)
eberlm@63498
  1807
     (rule gcd_lcm_factorial; assumption)+
eberlm@63498
  1808
eberlm@63498
  1809
subclass semiring_Gcd
eberlm@63498
  1810
  by (standard, unfold Gcd_eq_Gcd_factorial Lcm_eq_Lcm_factorial)
eberlm@63498
  1811
     (rule gcd_lcm_factorial; assumption)+
haftmann@60804
  1812
eberlm@63534
  1813
lemma
eberlm@63534
  1814
  assumes "x \<noteq> 0" "y \<noteq> 0"
wenzelm@65552
  1815
  shows gcd_eq_factorial':
wenzelm@65552
  1816
          "gcd x y = (\<Prod>p \<in> prime_factors x \<inter> prime_factors y.
eberlm@63534
  1817
                          p ^ min (multiplicity p x) (multiplicity p y))" (is "_ = ?rhs1")
eberlm@63534
  1818
    and lcm_eq_factorial':
wenzelm@65552
  1819
          "lcm x y = (\<Prod>p \<in> prime_factors x \<union> prime_factors y.
eberlm@63534
  1820
                          p ^ max (multiplicity p x) (multiplicity p y))" (is "_ = ?rhs2")
eberlm@63534
  1821
proof -
eberlm@63534
  1822
  have "gcd x y = gcd_factorial x y" by (rule gcd_eq_gcd_factorial)
eberlm@63534
  1823
  also have "\<dots> = ?rhs1"
haftmann@63905
  1824
    by (auto simp: gcd_factorial_def assms prod_mset_multiplicity
nipkow@64272
  1825
          count_prime_factorization_prime dest: in_prime_factors_imp_prime intro!: prod.cong)
eberlm@63534
  1826
  finally show "gcd x y = ?rhs1" .
eberlm@63534
  1827
  have "lcm x y = lcm_factorial x y" by (rule lcm_eq_lcm_factorial)
eberlm@63534
  1828
  also have "\<dots> = ?rhs2"
haftmann@63905
  1829
    by (auto simp: lcm_factorial_def assms prod_mset_multiplicity
nipkow@64272
  1830
          count_prime_factorization_prime dest: in_prime_factors_imp_prime intro!: prod.cong)
eberlm@63534
  1831
  finally show "lcm x y = ?rhs2" .
eberlm@63534
  1832
qed
eberlm@63534
  1833
eberlm@63534
  1834
lemma
eberlm@63633
  1835
  assumes "x \<noteq> 0" "y \<noteq> 0" "prime p"
eberlm@63534
  1836
  shows   multiplicity_gcd: "multiplicity p (gcd x y) = min (multiplicity p x) (multiplicity p y)"
eberlm@63534
  1837
    and   multiplicity_lcm: "multiplicity p (lcm x y) = max (multiplicity p x) (multiplicity p y)"
eberlm@63534
  1838
proof -
eberlm@63534
  1839
  have "gcd x y = gcd_factorial x y" by (rule gcd_eq_gcd_factorial)
eberlm@63534
  1840
  also from assms have "multiplicity p \<dots> = min (multiplicity p x) (multiplicity p y)"
eberlm@63534
  1841
    by (simp add: count_prime_factorization_prime [symmetric] prime_factorization_gcd_factorial)
eberlm@63534
  1842
  finally show "multiplicity p (gcd x y) = min (multiplicity p x) (multiplicity p y)" .
eberlm@63534
  1843
  have "lcm x y = lcm_factorial x y" by (rule lcm_eq_lcm_factorial)
eberlm@63534
  1844
  also from assms have "multiplicity p \<dots> = max (multiplicity p x) (multiplicity p y)"
eberlm@63534
  1845
    by (simp add: count_prime_factorization_prime [symmetric] prime_factorization_lcm_factorial)
eberlm@63534
  1846
  finally show "multiplicity p (lcm x y) = max (multiplicity p x) (multiplicity p y)" .
eberlm@63534
  1847
qed
eberlm@63534
  1848
eberlm@63534
  1849
lemma gcd_lcm_distrib:
eberlm@63534
  1850
  "gcd x (lcm y z) = lcm (gcd x y) (gcd x z)"
eberlm@63534
  1851
proof (cases "x = 0 \<or> y = 0 \<or> z = 0")
eberlm@63534
  1852
  case True
eberlm@63534
  1853
  thus ?thesis
eberlm@63534
  1854
    by (auto simp: lcm_proj1_if_dvd lcm_proj2_if_dvd)
eberlm@63534
  1855
next
eberlm@63534
  1856
  case False
eberlm@63534
  1857
  hence "normalize (gcd x (lcm y z)) = normalize (lcm (gcd x y) (gcd x z))"
eberlm@63534
  1858
    by (intro associatedI prime_factorization_subset_imp_dvd)
wenzelm@65552
  1859
       (auto simp: lcm_eq_0_iff prime_factorization_gcd prime_factorization_lcm
eberlm@63534
  1860
          subset_mset.inf_sup_distrib1)
eberlm@63534
  1861
  thus ?thesis by simp
eberlm@63534
  1862
qed
eberlm@63534
  1863
eberlm@63534
  1864
lemma lcm_gcd_distrib:
eberlm@63534
  1865
  "lcm x (gcd y z) = gcd (lcm x y) (lcm x z)"
eberlm@63534
  1866
proof (cases "x = 0 \<or> y = 0 \<or> z = 0")
eberlm@63534
  1867
  case True
eberlm@63534
  1868
  thus ?thesis
eberlm@63534
  1869
    by (auto simp: lcm_proj1_if_dvd lcm_proj2_if_dvd)
eberlm@63534
  1870
next
eberlm@63534
  1871
  case False
eberlm@63534
  1872
  hence "normalize (lcm x (gcd y z)) = normalize (gcd (lcm x y) (lcm x z))"
eberlm@63534
  1873
    by (intro associatedI prime_factorization_subset_imp_dvd)
wenzelm@65552
  1874
       (auto simp: lcm_eq_0_iff prime_factorization_gcd prime_factorization_lcm
eberlm@63534
  1875
          subset_mset.sup_inf_distrib1)
eberlm@63534
  1876
  thus ?thesis by simp
eberlm@63534
  1877
qed
eberlm@63534
  1878
haftmann@60804
  1879
end
haftmann@60804
  1880
eberlm@63498
  1881
class factorial_ring_gcd = factorial_semiring_gcd + idom
haftmann@60804
  1882
begin
haftmann@60804
  1883
eberlm@63498
  1884
subclass ring_gcd ..
haftmann@60804
  1885
eberlm@63498
  1886
subclass idom_divide ..
haftmann@60804
  1887
haftmann@60804
  1888
end
haftmann@60804
  1889
haftmann@60804
  1890
end