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