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