src/HOL/Computational_Algebra/Primes.thy
author wenzelm
Fri Apr 07 21:17:18 2017 +0200 (2017-04-07)
changeset 65435 378175f44328
parent 65417 fc41a5650fb1
child 65583 8d53b3bebab4
permissions -rw-r--r--
tuned headers;
wenzelm@65435
     1
(*  Title:      HOL/Computational_Algebra/Primes.thy
wenzelm@65435
     2
    Author:     Christophe Tabacznyj
wenzelm@65435
     3
    Author:     Lawrence C. Paulson
wenzelm@65435
     4
    Author:     Amine Chaieb
wenzelm@65435
     5
    Author:     Thomas M. Rasmussen
wenzelm@65435
     6
    Author:     Jeremy Avigad
wenzelm@65435
     7
    Author:     Tobias Nipkow
wenzelm@65435
     8
    Author:     Manuel Eberl
huffman@31706
     9
wenzelm@65435
    10
This theory deals with properties of primes. Definitions and lemmas are
haftmann@32479
    11
proved uniformly for the natural numbers and integers.
huffman@31706
    12
huffman@31706
    13
This file combines and revises a number of prior developments.
huffman@31706
    14
huffman@31706
    15
The original theories "GCD" and "Primes" were by Christophe Tabacznyj
wenzelm@58623
    16
and Lawrence C. Paulson, based on @{cite davenport92}. They introduced
huffman@31706
    17
gcd, lcm, and prime for the natural numbers.
huffman@31706
    18
huffman@31706
    19
The original theory "IntPrimes" was by Thomas M. Rasmussen, and
huffman@31706
    20
extended gcd, lcm, primes to the integers. Amine Chaieb provided
huffman@31706
    21
another extension of the notions to the integers, and added a number
huffman@31706
    22
of results to "Primes" and "GCD". IntPrimes also defined and developed
huffman@31706
    23
the congruence relations on the integers. The notion was extended to
webertj@33718
    24
the natural numbers by Chaieb.
huffman@31706
    25
avigad@32036
    26
Jeremy Avigad combined all of these, made everything uniform for the
avigad@32036
    27
natural numbers and the integers, and added a number of new theorems.
avigad@32036
    28
nipkow@31798
    29
Tobias Nipkow cleaned up a lot.
eberlm@63534
    30
eberlm@63534
    31
Florian Haftmann and Manuel Eberl put primality and prime factorisation
eberlm@63534
    32
onto an algebraic foundation and thus generalised these concepts to 
eberlm@63534
    33
other rings, such as polynomials. (see also the Factorial_Ring theory).
eberlm@63534
    34
eberlm@63534
    35
There were also previous formalisations of unique factorisation by 
eberlm@63534
    36
Thomas Marthedal Rasmussen, Jeremy Avigad, and David Gray.
wenzelm@21256
    37
*)
wenzelm@21256
    38
wenzelm@60526
    39
section \<open>Primes\<close>
wenzelm@21256
    40
haftmann@32479
    41
theory Primes
eberlm@63534
    42
imports "~~/src/HOL/Binomial" Euclidean_Algorithm
huffman@31706
    43
begin
huffman@31706
    44
eberlm@63534
    45
(* As a simp or intro rule,
eberlm@63534
    46
eberlm@63534
    47
     prime p \<Longrightarrow> p > 0
eberlm@63534
    48
eberlm@63534
    49
   wreaks havoc here. When the premise includes \<forall>x \<in># M. prime x, it
eberlm@63534
    50
   leads to the backchaining
eberlm@63534
    51
eberlm@63534
    52
     x > 0
eberlm@63534
    53
     prime x
eberlm@63534
    54
     x \<in># M   which is, unfortunately,
eberlm@63534
    55
     count M x > 0
eberlm@63534
    56
*)
eberlm@63534
    57
lp15@55242
    58
declare [[coercion int]]
lp15@55242
    59
declare [[coercion_enabled]]
huffman@31706
    60
eberlm@63633
    61
lemma prime_elem_nat_iff:
eberlm@63633
    62
  "prime_elem (n :: nat) \<longleftrightarrow> (1 < n \<and> (\<forall>m. m dvd n \<longrightarrow> m = 1 \<or> m = n))"
eberlm@63534
    63
proof safe
eberlm@63633
    64
  assume *: "prime_elem n"
eberlm@63534
    65
  from * have "n \<noteq> 0" "n \<noteq> 1" by (intro notI, simp del: One_nat_def)+
eberlm@63534
    66
  thus "n > 1" by (cases n) simp_all
eberlm@63534
    67
  fix m assume m: "m dvd n" "m \<noteq> n"
eberlm@63534
    68
  from * \<open>m dvd n\<close> have "n dvd m \<or> is_unit m"
eberlm@63633
    69
    by (intro irreducibleD' prime_elem_imp_irreducible)
eberlm@63534
    70
  with m show "m = 1" by (auto dest: dvd_antisym)
eberlm@63534
    71
next
eberlm@63534
    72
  assume "n > 1" "\<forall>m. m dvd n \<longrightarrow> m = 1 \<or> m = n"
eberlm@63633
    73
  thus "prime_elem n"
eberlm@63633
    74
    by (auto simp: prime_elem_iff_irreducible irreducible_altdef)
eberlm@63534
    75
qed
eberlm@63534
    76
eberlm@63633
    77
lemma prime_nat_iff_prime_elem: "prime (n :: nat) \<longleftrightarrow> prime_elem n"
eberlm@63633
    78
  by (simp add: prime_def)
huffman@31706
    79
eberlm@63633
    80
lemma prime_nat_iff:
eberlm@63633
    81
  "prime (n :: nat) \<longleftrightarrow> (1 < n \<and> (\<forall>m. m dvd n \<longrightarrow> m = 1 \<or> m = n))"
eberlm@63633
    82
  by (simp add: prime_nat_iff_prime_elem prime_elem_nat_iff)
huffman@31706
    83
eberlm@63633
    84
lemma prime_elem_int_nat_transfer: "prime_elem (n::int) \<longleftrightarrow> prime_elem (nat (abs n))"
eberlm@63534
    85
proof
eberlm@63633
    86
  assume "prime_elem n"
eberlm@63633
    87
  thus "prime_elem (nat (abs n))" by (auto simp: prime_elem_def nat_dvd_iff)
eberlm@63534
    88
next
eberlm@63633
    89
  assume "prime_elem (nat (abs n))"
eberlm@63633
    90
  thus "prime_elem n"
eberlm@63633
    91
    by (auto simp: dvd_int_unfold_dvd_nat prime_elem_def abs_mult nat_mult_distrib)
eberlm@63534
    92
qed
eberlm@63534
    93
eberlm@63633
    94
lemma prime_elem_nat_int_transfer [simp]: "prime_elem (int n) \<longleftrightarrow> prime_elem n"
eberlm@63633
    95
  by (auto simp: prime_elem_int_nat_transfer)
eberlm@63534
    96
eberlm@63633
    97
lemma prime_nat_int_transfer [simp]: "prime (int n) \<longleftrightarrow> prime n"
eberlm@63633
    98
  by (auto simp: prime_elem_int_nat_transfer prime_def)
eberlm@63534
    99
eberlm@63633
   100
lemma prime_int_nat_transfer: "prime (n::int) \<longleftrightarrow> n \<ge> 0 \<and> prime (nat n)"
eberlm@63633
   101
  by (auto simp: prime_elem_int_nat_transfer prime_def)
huffman@31706
   102
eberlm@63633
   103
lemma prime_int_iff:
eberlm@63633
   104
  "prime (n::int) \<longleftrightarrow> (1 < n \<and> (\<forall>m. m \<ge> 0 \<and> m dvd n \<longrightarrow> m = 1 \<or> m = n))"
eberlm@63534
   105
proof (intro iffI conjI allI impI; (elim conjE)?)
eberlm@63633
   106
  assume *: "prime n"
eberlm@63633
   107
  hence irred: "irreducible n" by (simp add: prime_elem_imp_irreducible)
eberlm@63534
   108
  from * have "n \<ge> 0" "n \<noteq> 0" "n \<noteq> 1" 
eberlm@63633
   109
    by (auto simp: prime_def zabs_def not_less split: if_splits)
eberlm@63534
   110
  thus "n > 1" by presburger
eberlm@63534
   111
  fix m assume "m dvd n" \<open>m \<ge> 0\<close>
eberlm@63534
   112
  with irred have "m dvd 1 \<or> n dvd m" by (auto simp: irreducible_altdef)
eberlm@63534
   113
  with \<open>m dvd n\<close> \<open>m \<ge> 0\<close> \<open>n > 1\<close> show "m = 1 \<or> m = n"
eberlm@63534
   114
    using associated_iff_dvd[of m n] by auto
eberlm@63534
   115
next
eberlm@63534
   116
  assume n: "1 < n" "\<forall>m. m \<ge> 0 \<and> m dvd n \<longrightarrow> m = 1 \<or> m = n"
eberlm@63534
   117
  hence "nat n > 1" by simp
eberlm@63534
   118
  moreover have "\<forall>m. m dvd nat n \<longrightarrow> m = 1 \<or> m = nat n"
eberlm@63534
   119
  proof (intro allI impI)
eberlm@63534
   120
    fix m assume "m dvd nat n"
eberlm@63534
   121
    with \<open>n > 1\<close> have "int m dvd n" by (auto simp: int_dvd_iff)
eberlm@63534
   122
    with n(2) have "int m = 1 \<or> int m = n" by auto
eberlm@63534
   123
    thus "m = 1 \<or> m = nat n" by auto
eberlm@63534
   124
  qed
eberlm@63633
   125
  ultimately show "prime n" 
eberlm@63633
   126
    unfolding prime_int_nat_transfer prime_nat_iff by auto
eberlm@63534
   127
qed
chaieb@22027
   128
eberlm@63534
   129
eberlm@63534
   130
lemma prime_nat_not_dvd:
eberlm@63534
   131
  assumes "prime p" "p > n" "n \<noteq> (1::nat)"
eberlm@63534
   132
  shows   "\<not>n dvd p"
eberlm@63534
   133
proof
eberlm@63534
   134
  assume "n dvd p"
eberlm@63633
   135
  from assms(1) have "irreducible p" by (simp add: prime_elem_imp_irreducible)
eberlm@63534
   136
  from irreducibleD'[OF this \<open>n dvd p\<close>] \<open>n dvd p\<close> \<open>p > n\<close> assms show False
eberlm@63534
   137
    by (cases "n = 0") (auto dest!: dvd_imp_le)
eberlm@63534
   138
qed
chaieb@22027
   139
eberlm@63534
   140
lemma prime_int_not_dvd:
eberlm@63534
   141
  assumes "prime p" "p > n" "n > (1::int)"
eberlm@63534
   142
  shows   "\<not>n dvd p"
eberlm@63534
   143
proof
eberlm@63534
   144
  assume "n dvd p"
eberlm@63633
   145
  from assms(1) have "irreducible p" by (simp add: prime_elem_imp_irreducible)
eberlm@63534
   146
  from irreducibleD'[OF this \<open>n dvd p\<close>] \<open>n dvd p\<close> \<open>p > n\<close> assms show False
eberlm@63534
   147
    by (auto dest!: zdvd_imp_le)
eberlm@63534
   148
qed
eberlm@63534
   149
eberlm@63534
   150
lemma prime_odd_nat: "prime p \<Longrightarrow> p > (2::nat) \<Longrightarrow> odd p"
eberlm@63534
   151
  by (intro prime_nat_not_dvd) auto
eberlm@63534
   152
eberlm@63534
   153
lemma prime_odd_int: "prime p \<Longrightarrow> p > (2::int) \<Longrightarrow> odd p"
eberlm@63534
   154
  by (intro prime_int_not_dvd) auto
wenzelm@22367
   155
eberlm@63534
   156
lemma prime_ge_0_int: "prime p \<Longrightarrow> p \<ge> (0::int)"
eberlm@63633
   157
  unfolding prime_int_iff by auto
eberlm@63534
   158
eberlm@63534
   159
lemma prime_gt_0_nat: "prime p \<Longrightarrow> p > (0::nat)"
eberlm@63633
   160
  unfolding prime_nat_iff by auto
eberlm@63534
   161
eberlm@63534
   162
lemma prime_gt_0_int: "prime p \<Longrightarrow> p > (0::int)"
eberlm@63633
   163
  unfolding prime_int_iff by auto
eberlm@63534
   164
eberlm@63534
   165
lemma prime_ge_1_nat: "prime p \<Longrightarrow> p \<ge> (1::nat)"
eberlm@63633
   166
  unfolding prime_nat_iff by auto
eberlm@63534
   167
eberlm@63534
   168
lemma prime_ge_Suc_0_nat: "prime p \<Longrightarrow> p \<ge> Suc 0"
eberlm@63633
   169
  unfolding prime_nat_iff by auto
eberlm@63534
   170
eberlm@63534
   171
lemma prime_ge_1_int: "prime p \<Longrightarrow> p \<ge> (1::int)"
eberlm@63633
   172
  unfolding prime_int_iff by auto
eberlm@63534
   173
eberlm@63534
   174
lemma prime_gt_1_nat: "prime p \<Longrightarrow> p > (1::nat)"
eberlm@63633
   175
  unfolding prime_nat_iff by auto
huffman@31706
   176
lp15@61762
   177
lemma prime_gt_Suc_0_nat: "prime p \<Longrightarrow> p > Suc 0"
eberlm@63633
   178
  unfolding prime_nat_iff by auto
huffman@31706
   179
eberlm@63534
   180
lemma prime_gt_1_int: "prime p \<Longrightarrow> p > (1::int)"
eberlm@63633
   181
  unfolding prime_int_iff by auto
huffman@31706
   182
eberlm@63534
   183
lemma prime_ge_2_nat: "prime p \<Longrightarrow> p \<ge> (2::nat)"
eberlm@63633
   184
  unfolding prime_nat_iff by auto
eberlm@63534
   185
eberlm@63534
   186
lemma prime_ge_2_int: "prime p \<Longrightarrow> p \<ge> (2::int)"
eberlm@63633
   187
  unfolding prime_int_iff by auto
huffman@31706
   188
lp15@59669
   189
lemma prime_int_altdef:
lp15@55242
   190
  "prime p = (1 < p \<and> (\<forall>m::int. m \<ge> 0 \<longrightarrow> m dvd p \<longrightarrow>
lp15@55242
   191
    m = 1 \<or> m = p))"
eberlm@63633
   192
  unfolding prime_int_iff by blast
chaieb@27568
   193
haftmann@62481
   194
lemma not_prime_eq_prod_nat:
eberlm@63534
   195
  assumes "m > 1" "\<not>prime (m::nat)"
eberlm@63534
   196
  shows   "\<exists>n k. n = m * k \<and> 1 < m \<and> m < n \<and> 1 < k \<and> k < n"
eberlm@63534
   197
  using assms irreducible_altdef[of m]
eberlm@63633
   198
  by (auto simp: prime_elem_iff_irreducible prime_def irreducible_altdef)
huffman@53598
   199
lp15@64773
   200
    
lp15@64773
   201
subsection\<open>Largest exponent of a prime factor\<close>
lp15@64773
   202
text\<open>Possibly duplicates other material, but avoid the complexities of multisets.\<close>
lp15@64773
   203
  
lp15@64773
   204
lemma prime_power_cancel_less:
lp15@64773
   205
  assumes "prime p" and eq: "m * (p ^ k) = m' * (p ^ k')" and less: "k < k'" and "\<not> p dvd m"
lp15@64773
   206
  shows False
lp15@64773
   207
proof -
lp15@64773
   208
  obtain l where l: "k' = k + l" and "l > 0"
lp15@64773
   209
    using less less_imp_add_positive by auto
lp15@64773
   210
  have "m = m * (p ^ k) div (p ^ k)"
lp15@64773
   211
    using \<open>prime p\<close> by simp
lp15@64773
   212
  also have "\<dots> = m' * (p ^ k') div (p ^ k)"
lp15@64773
   213
    using eq by simp
lp15@64773
   214
  also have "\<dots> = m' * (p ^ l) * (p ^ k) div (p ^ k)"
lp15@64773
   215
    by (simp add: l mult.commute mult.left_commute power_add)
lp15@64773
   216
  also have "... = m' * (p ^ l)"
lp15@64773
   217
    using \<open>prime p\<close> by simp
lp15@64773
   218
  finally have "p dvd m"
lp15@64773
   219
    using \<open>l > 0\<close> by simp
lp15@64773
   220
  with assms show False
lp15@64773
   221
    by simp
lp15@64773
   222
qed
lp15@64773
   223
lp15@64773
   224
lemma prime_power_cancel:
lp15@64773
   225
  assumes "prime p" and eq: "m * (p ^ k) = m' * (p ^ k')" and "\<not> p dvd m" "\<not> p dvd m'"
lp15@64773
   226
  shows "k = k'"
lp15@64773
   227
  using prime_power_cancel_less [OF \<open>prime p\<close>] assms
lp15@64773
   228
  by (metis linorder_neqE_nat)
lp15@64773
   229
lp15@64773
   230
lemma prime_power_cancel2:
lp15@64773
   231
  assumes "prime p" "m * (p ^ k) = m' * (p ^ k')" "\<not> p dvd m" "\<not> p dvd m'"
lp15@64773
   232
  obtains "m = m'" "k = k'"
lp15@64773
   233
  using prime_power_cancel [OF assms] assms by auto
lp15@64773
   234
lp15@64773
   235
lemma prime_power_canonical:
lp15@64773
   236
  fixes m::nat
lp15@64773
   237
  assumes "prime p" "m > 0"
lp15@64773
   238
  shows "\<exists>k n. \<not> p dvd n \<and> m = n * p^k"
lp15@64773
   239
using \<open>m > 0\<close>
lp15@64773
   240
proof (induction m rule: less_induct)
lp15@64773
   241
  case (less m)
lp15@64773
   242
  show ?case
lp15@64773
   243
  proof (cases "p dvd m")
lp15@64773
   244
    case True
lp15@64773
   245
    then obtain m' where m': "m = p * m'"
lp15@64773
   246
      using dvdE by blast
lp15@64773
   247
    with \<open>prime p\<close> have "0 < m'" "m' < m"
lp15@64773
   248
      using less.prems prime_nat_iff by auto
lp15@64773
   249
    with m' less show ?thesis
lp15@64773
   250
      by (metis power_Suc mult.left_commute)
lp15@64773
   251
  next
lp15@64773
   252
    case False
lp15@64773
   253
    then show ?thesis
lp15@64773
   254
      by (metis mult.right_neutral power_0)
lp15@64773
   255
  qed
lp15@64773
   256
qed
lp15@64773
   257
huffman@53598
   258
wenzelm@60526
   259
subsubsection \<open>Make prime naively executable\<close>
nipkow@32007
   260
eberlm@63534
   261
lemma Suc_0_not_prime_nat [simp]: "~prime (Suc 0)"
eberlm@63633
   262
  unfolding One_nat_def [symmetric] by (rule not_prime_1)
nipkow@32007
   263
eberlm@63633
   264
lemma prime_nat_iff':
haftmann@65025
   265
  "prime (p :: nat) \<longleftrightarrow> p > 1 \<and> (\<forall>n \<in> {2..<p}. ~ n dvd p)"
eberlm@63534
   266
proof safe
haftmann@65025
   267
  assume "p > 1" and *: "\<forall>n\<in>{2..<p}. \<not>n dvd p"
eberlm@63633
   268
  show "prime p" unfolding prime_nat_iff
eberlm@63534
   269
  proof (intro conjI allI impI)
eberlm@63534
   270
    fix m assume "m dvd p"
eberlm@63534
   271
    with \<open>p > 1\<close> have "m \<noteq> 0" by (intro notI) auto
eberlm@63534
   272
    hence "m \<ge> 1" by simp
haftmann@65025
   273
    moreover from \<open>m dvd p\<close> and * have "m \<notin> {2..<p}" by blast
eberlm@63534
   274
    with \<open>m dvd p\<close> and \<open>p > 1\<close> have "m \<le> 1 \<or> m = p" by (auto dest: dvd_imp_le)
eberlm@63534
   275
    ultimately show "m = 1 \<or> m = p" by simp
eberlm@63534
   276
  qed fact+
eberlm@63633
   277
qed (auto simp: prime_nat_iff)
nipkow@32007
   278
eberlm@63633
   279
lemma prime_int_iff':
haftmann@65025
   280
  "prime (p :: int) \<longleftrightarrow> p > 1 \<and> (\<forall>n \<in> {2..<p}. ~ n dvd p)" (is "?lhs = ?rhs")
eberlm@63534
   281
proof
eberlm@63534
   282
  assume "?lhs"
haftmann@65025
   283
  thus "?rhs"
haftmann@65025
   284
      by (auto simp: prime_int_nat_transfer dvd_int_unfold_dvd_nat prime_nat_iff')
eberlm@63534
   285
next
eberlm@63534
   286
  assume "?rhs"
haftmann@65025
   287
  thus "?lhs"
haftmann@65025
   288
    by (auto simp: prime_int_nat_transfer zdvd_int prime_nat_iff')
eberlm@63534
   289
qed
nipkow@32007
   290
haftmann@64590
   291
lemma prime_int_numeral_eq [simp]:
haftmann@64590
   292
  "prime (numeral m :: int) \<longleftrightarrow> prime (numeral m :: nat)"
haftmann@64590
   293
  by (simp add: prime_int_nat_transfer)
nipkow@32007
   294
eberlm@63635
   295
lemma two_is_prime_nat [simp]: "prime (2::nat)"
haftmann@65025
   296
  by (simp add: prime_nat_iff')
nipkow@32007
   297
haftmann@64590
   298
lemma prime_nat_numeral_eq [simp]:
haftmann@64590
   299
  "prime (numeral m :: nat) \<longleftrightarrow>
haftmann@64590
   300
    (1::nat) < numeral m \<and>
haftmann@65025
   301
    (\<forall>n::nat \<in> set [2..<numeral m]. \<not> n dvd numeral m)"
haftmann@65025
   302
  by (simp only: prime_nat_iff' set_upt)  \<comment> \<open>TODO Sieve Of Erathosthenes might speed this up\<close>
eberlm@63534
   303
eberlm@63534
   304
wenzelm@60526
   305
text\<open>A bit of regression testing:\<close>
nipkow@32111
   306
haftmann@58954
   307
lemma "prime(97::nat)" by simp
eberlm@63534
   308
lemma "prime(97::int)" by simp
huffman@31706
   309
eberlm@63534
   310
lemma prime_factor_nat: 
eberlm@63534
   311
  "n \<noteq> (1::nat) \<Longrightarrow> \<exists>p. prime p \<and> p dvd n"
eberlm@63534
   312
  using prime_divisor_exists[of n]
eberlm@63534
   313
  by (cases "n = 0") (auto intro: exI[of _ "2::nat"])
nipkow@23983
   314
wenzelm@44872
   315
wenzelm@60526
   316
subsection \<open>Infinitely many primes\<close>
avigad@32036
   317
eberlm@63534
   318
lemma next_prime_bound: "\<exists>p::nat. prime p \<and> n < p \<and> p \<le> fact n + 1"
avigad@32036
   319
proof-
lp15@59730
   320
  have f1: "fact n + 1 \<noteq> (1::nat)" using fact_ge_1 [of n, where 'a=nat] by arith
avigad@32036
   321
  from prime_factor_nat [OF f1]
eberlm@63534
   322
  obtain p :: nat where "prime p" and "p dvd fact n + 1" by auto
wenzelm@44872
   323
  then have "p \<le> fact n + 1" apply (intro dvd_imp_le) apply auto done
wenzelm@44872
   324
  { assume "p \<le> n"
wenzelm@60526
   325
    from \<open>prime p\<close> have "p \<ge> 1"
avigad@32036
   326
      by (cases p, simp_all)
wenzelm@60526
   327
    with \<open>p <= n\<close> have "p dvd fact n"
lp15@59730
   328
      by (intro dvd_fact)
wenzelm@60526
   329
    with \<open>p dvd fact n + 1\<close> have "p dvd fact n + 1 - fact n"
avigad@32036
   330
      by (rule dvd_diff_nat)
wenzelm@44872
   331
    then have "p dvd 1" by simp
wenzelm@44872
   332
    then have "p <= 1" by auto
lp15@61762
   333
    moreover from \<open>prime p\<close> have "p > 1"
eberlm@63633
   334
      using prime_nat_iff by blast
avigad@32036
   335
    ultimately have False by auto}
wenzelm@44872
   336
  then have "n < p" by presburger
wenzelm@60526
   337
  with \<open>prime p\<close> and \<open>p <= fact n + 1\<close> show ?thesis by auto
avigad@32036
   338
qed
avigad@32036
   339
lp15@59669
   340
lemma bigger_prime: "\<exists>p. prime p \<and> p > (n::nat)"
wenzelm@44872
   341
  using next_prime_bound by auto
avigad@32036
   342
avigad@32036
   343
lemma primes_infinite: "\<not> (finite {(p::nat). prime p})"
avigad@32036
   344
proof
avigad@32036
   345
  assume "finite {(p::nat). prime p}"
avigad@32036
   346
  with Max_ge have "(EX b. (ALL x : {(p::nat). prime p}. x <= b))"
avigad@32036
   347
    by auto
avigad@32036
   348
  then obtain b where "ALL (x::nat). prime x \<longrightarrow> x <= b"
avigad@32036
   349
    by auto
wenzelm@44872
   350
  with bigger_prime [of b] show False
wenzelm@44872
   351
    by auto
avigad@32036
   352
qed
avigad@32036
   353
wenzelm@60526
   354
subsection\<open>Powers of Primes\<close>
lp15@55215
   355
wenzelm@60526
   356
text\<open>Versions for type nat only\<close>
lp15@55215
   357
lp15@59669
   358
lemma prime_product:
lp15@55215
   359
  fixes p::nat
lp15@55215
   360
  assumes "prime (p * q)"
lp15@55215
   361
  shows "p = 1 \<or> q = 1"
lp15@55215
   362
proof -
lp15@59669
   363
  from assms have
lp15@55215
   364
    "1 < p * q" and P: "\<And>m. m dvd p * q \<Longrightarrow> m = 1 \<or> m = p * q"
eberlm@63633
   365
    unfolding prime_nat_iff by auto
wenzelm@60526
   366
  from \<open>1 < p * q\<close> have "p \<noteq> 0" by (cases p) auto
lp15@55215
   367
  then have Q: "p = p * q \<longleftrightarrow> q = 1" by auto
lp15@55215
   368
  have "p dvd p * q" by simp
lp15@55215
   369
  then have "p = 1 \<or> p = p * q" by (rule P)
lp15@55215
   370
  then show ?thesis by (simp add: Q)
lp15@55215
   371
qed
lp15@55215
   372
eberlm@63534
   373
(* TODO: Generalise? *)
eberlm@63534
   374
lemma prime_power_mult_nat:
lp15@55215
   375
  fixes p::nat
lp15@55215
   376
  assumes p: "prime p" and xy: "x * y = p ^ k"
lp15@55215
   377
  shows "\<exists>i j. x = p ^i \<and> y = p^ j"
lp15@55215
   378
using xy
lp15@55215
   379
proof(induct k arbitrary: x y)
lp15@55215
   380
  case 0 thus ?case apply simp by (rule exI[where x="0"], simp)
lp15@55215
   381
next
lp15@55215
   382
  case (Suc k x y)
lp15@55215
   383
  from Suc.prems have pxy: "p dvd x*y" by auto
eberlm@63633
   384
  from prime_dvd_multD [OF p pxy] have pxyc: "p dvd x \<or> p dvd y" .
lp15@59669
   385
  from p have p0: "p \<noteq> 0" by - (rule ccontr, simp)
lp15@55215
   386
  {assume px: "p dvd x"
lp15@55215
   387
    then obtain d where d: "x = p*d" unfolding dvd_def by blast
lp15@55215
   388
    from Suc.prems d  have "p*d*y = p^Suc k" by simp
lp15@55215
   389
    hence th: "d*y = p^k" using p0 by simp
lp15@55215
   390
    from Suc.hyps[OF th] obtain i j where ij: "d = p^i" "y = p^j" by blast
lp15@59669
   391
    with d have "x = p^Suc i" by simp
lp15@55215
   392
    with ij(2) have ?case by blast}
lp15@59669
   393
  moreover
lp15@55215
   394
  {assume px: "p dvd y"
lp15@55215
   395
    then obtain d where d: "y = p*d" unfolding dvd_def by blast
haftmann@57512
   396
    from Suc.prems d  have "p*d*x = p^Suc k" by (simp add: mult.commute)
lp15@55215
   397
    hence th: "d*x = p^k" using p0 by simp
lp15@55215
   398
    from Suc.hyps[OF th] obtain i j where ij: "d = p^i" "x = p^j" by blast
lp15@59669
   399
    with d have "y = p^Suc i" by simp
lp15@55215
   400
    with ij(2) have ?case by blast}
lp15@55215
   401
  ultimately show ?case  using pxyc by blast
lp15@55215
   402
qed
lp15@55215
   403
eberlm@63534
   404
lemma prime_power_exp_nat:
lp15@55215
   405
  fixes p::nat
lp15@59669
   406
  assumes p: "prime p" and n: "n \<noteq> 0"
lp15@55215
   407
    and xn: "x^n = p^k" shows "\<exists>i. x = p^i"
lp15@55215
   408
  using n xn
lp15@55215
   409
proof(induct n arbitrary: k)
lp15@55215
   410
  case 0 thus ?case by simp
lp15@55215
   411
next
lp15@55215
   412
  case (Suc n k) hence th: "x*x^n = p^k" by simp
lp15@55215
   413
  {assume "n = 0" with Suc have ?case by simp (rule exI[where x="k"], simp)}
lp15@55215
   414
  moreover
lp15@55215
   415
  {assume n: "n \<noteq> 0"
eberlm@63534
   416
    from prime_power_mult_nat[OF p th]
lp15@55215
   417
    obtain i j where ij: "x = p^i" "x^n = p^j"by blast
lp15@55215
   418
    from Suc.hyps[OF n ij(2)] have ?case .}
lp15@55215
   419
  ultimately show ?case by blast
lp15@55215
   420
qed
lp15@55215
   421
eberlm@63534
   422
lemma divides_primepow_nat:
lp15@55215
   423
  fixes p::nat
lp15@59669
   424
  assumes p: "prime p"
lp15@55215
   425
  shows "d dvd p^k \<longleftrightarrow> (\<exists> i. i \<le> k \<and> d = p ^i)"
lp15@55215
   426
proof
lp15@59669
   427
  assume H: "d dvd p^k" then obtain e where e: "d*e = p^k"
haftmann@57512
   428
    unfolding dvd_def  apply (auto simp add: mult.commute) by blast
eberlm@63534
   429
  from prime_power_mult_nat[OF p e] obtain i j where ij: "d = p^i" "e=p^j" by blast
lp15@55215
   430
  from e ij have "p^(i + j) = p^k" by (simp add: power_add)
lp15@59669
   431
  hence "i + j = k" using p prime_gt_1_nat power_inject_exp[of p "i+j" k] by simp
lp15@55215
   432
  hence "i \<le> k" by arith
lp15@55215
   433
  with ij(1) show "\<exists>i\<le>k. d = p ^ i" by blast
lp15@55215
   434
next
lp15@55215
   435
  {fix i assume H: "i \<le> k" "d = p^i"
lp15@55215
   436
    then obtain j where j: "k = i + j"
lp15@55215
   437
      by (metis le_add_diff_inverse)
lp15@55215
   438
    hence "p^k = p^j*d" using H(2) by (simp add: power_add)
lp15@55215
   439
    hence "d dvd p^k" unfolding dvd_def by auto}
lp15@55215
   440
  thus "\<exists>i\<le>k. d = p ^ i \<Longrightarrow> d dvd p ^ k" by blast
lp15@55215
   441
qed
lp15@55215
   442
eberlm@63534
   443
wenzelm@60526
   444
subsection \<open>Chinese Remainder Theorem Variants\<close>
lp15@55238
   445
lp15@55238
   446
lemma bezout_gcd_nat:
lp15@55238
   447
  fixes a::nat shows "\<exists>x y. a * x - b * y = gcd a b \<or> b * x - a * y = gcd a b"
lp15@55238
   448
  using bezout_nat[of a b]
eberlm@62429
   449
by (metis bezout_nat diff_add_inverse gcd_add_mult gcd.commute
lp15@59669
   450
  gcd_nat.right_neutral mult_0)
lp15@55238
   451
lp15@55238
   452
lemma gcd_bezout_sum_nat:
lp15@59669
   453
  fixes a::nat
lp15@59669
   454
  assumes "a * x + b * y = d"
lp15@55238
   455
  shows "gcd a b dvd d"
lp15@55238
   456
proof-
lp15@55238
   457
  let ?g = "gcd a b"
lp15@59669
   458
    have dv: "?g dvd a*x" "?g dvd b * y"
lp15@55238
   459
      by simp_all
lp15@55238
   460
    from dvd_add[OF dv] assms
lp15@55238
   461
    show ?thesis by auto
lp15@55238
   462
qed
lp15@55238
   463
lp15@55238
   464
wenzelm@60526
   465
text \<open>A binary form of the Chinese Remainder Theorem.\<close>
lp15@55238
   466
eberlm@63534
   467
(* TODO: Generalise? *)
lp15@59669
   468
lemma chinese_remainder:
lp15@55238
   469
  fixes a::nat  assumes ab: "coprime a b" and a: "a \<noteq> 0" and b: "b \<noteq> 0"
lp15@55238
   470
  shows "\<exists>x q1 q2. x = u + q1 * a \<and> x = v + q2 * b"
lp15@55238
   471
proof-
lp15@55238
   472
  from bezout_add_strong_nat[OF a, of b] bezout_add_strong_nat[OF b, of a]
lp15@59669
   473
  obtain d1 x1 y1 d2 x2 y2 where dxy1: "d1 dvd a" "d1 dvd b" "a * x1 = b * y1 + d1"
lp15@55238
   474
    and dxy2: "d2 dvd b" "d2 dvd a" "b * x2 = a * y2 + d2" by blast
lp15@55238
   475
  then have d12: "d1 = 1" "d2 =1"
lp15@55238
   476
    by (metis ab coprime_nat)+
lp15@55238
   477
  let ?x = "v * a * x1 + u * b * x2"
lp15@55238
   478
  let ?q1 = "v * x1 + u * y2"
lp15@55238
   479
  let ?q2 = "v * y1 + u * x2"
lp15@59669
   480
  from dxy2(3)[simplified d12] dxy1(3)[simplified d12]
lp15@55238
   481
  have "?x = u + ?q1 * a" "?x = v + ?q2 * b"
lp15@55337
   482
    by algebra+
lp15@55238
   483
  thus ?thesis by blast
lp15@55238
   484
qed
lp15@55238
   485
wenzelm@60526
   486
text \<open>Primality\<close>
lp15@55238
   487
lp15@55238
   488
lemma coprime_bezout_strong:
lp15@55238
   489
  fixes a::nat assumes "coprime a b"  "b \<noteq> 1"
lp15@55238
   490
  shows "\<exists>x y. a * x = b * y + 1"
lp15@55238
   491
by (metis assms bezout_nat gcd_nat.left_neutral)
lp15@55238
   492
lp15@59669
   493
lemma bezout_prime:
lp15@55238
   494
  assumes p: "prime p" and pa: "\<not> p dvd a"
lp15@55238
   495
  shows "\<exists>x y. a*x = Suc (p*y)"
haftmann@62349
   496
proof -
lp15@55238
   497
  have ap: "coprime a p"
eberlm@63633
   498
    by (metis gcd.commute p pa prime_imp_coprime)
haftmann@62349
   499
  moreover from p have "p \<noteq> 1" by auto
haftmann@62349
   500
  ultimately have "\<exists>x y. a * x = p * y + 1"
haftmann@62349
   501
    by (rule coprime_bezout_strong)
haftmann@62349
   502
  then show ?thesis by simp    
lp15@55238
   503
qed
eberlm@63534
   504
(* END TODO *)
lp15@55238
   505
eberlm@63534
   506
eberlm@63534
   507
eberlm@63534
   508
subsection \<open>Multiplicity and primality for natural numbers and integers\<close>
eberlm@63534
   509
eberlm@63534
   510
lemma prime_factors_gt_0_nat:
eberlm@63534
   511
  "p \<in> prime_factors x \<Longrightarrow> p > (0::nat)"
haftmann@63905
   512
  by (simp add: in_prime_factors_imp_prime prime_gt_0_nat)
eberlm@63534
   513
eberlm@63534
   514
lemma prime_factors_gt_0_int:
eberlm@63534
   515
  "p \<in> prime_factors x \<Longrightarrow> p > (0::int)"
haftmann@63905
   516
  by (simp add: in_prime_factors_imp_prime prime_gt_0_int)
eberlm@63534
   517
haftmann@63905
   518
lemma prime_factors_ge_0_int [elim]: (* FIXME !? *)
eberlm@63534
   519
  fixes n :: int
eberlm@63534
   520
  shows "p \<in> prime_factors n \<Longrightarrow> p \<ge> 0"
haftmann@63905
   521
  by (drule prime_factors_gt_0_int) simp
haftmann@63905
   522
  
nipkow@63830
   523
lemma prod_mset_prime_factorization_int:
eberlm@63534
   524
  fixes n :: int
eberlm@63534
   525
  assumes "n > 0"
nipkow@63830
   526
  shows   "prod_mset (prime_factorization n) = n"
nipkow@63830
   527
  using assms by (simp add: prod_mset_prime_factorization)
eberlm@63534
   528
eberlm@63534
   529
lemma prime_factorization_exists_nat:
eberlm@63534
   530
  "n > 0 \<Longrightarrow> (\<exists>M. (\<forall>p::nat \<in> set_mset M. prime p) \<and> n = (\<Prod>i \<in># M. i))"
eberlm@63633
   531
  using prime_factorization_exists[of n] by (auto simp: prime_def)
eberlm@63534
   532
nipkow@63830
   533
lemma prod_mset_prime_factorization_nat [simp]: 
nipkow@63830
   534
  "(n::nat) > 0 \<Longrightarrow> prod_mset (prime_factorization n) = n"
nipkow@63830
   535
  by (subst prod_mset_prime_factorization) simp_all
eberlm@63534
   536
eberlm@63534
   537
lemma prime_factorization_nat:
eberlm@63534
   538
    "n > (0::nat) \<Longrightarrow> n = (\<Prod>p \<in> prime_factors n. p ^ multiplicity p n)"
nipkow@64272
   539
  by (simp add: prod_prime_factors)
eberlm@63534
   540
eberlm@63534
   541
lemma prime_factorization_int:
eberlm@63534
   542
    "n > (0::int) \<Longrightarrow> n = (\<Prod>p \<in> prime_factors n. p ^ multiplicity p n)"
nipkow@64272
   543
  by (simp add: prod_prime_factors)
eberlm@63534
   544
eberlm@63534
   545
lemma prime_factorization_unique_nat:
eberlm@63534
   546
  fixes f :: "nat \<Rightarrow> _"
eberlm@63534
   547
  assumes S_eq: "S = {p. 0 < f p}"
eberlm@63534
   548
    and "finite S"
eberlm@63534
   549
    and S: "\<forall>p\<in>S. prime p" "n = (\<Prod>p\<in>S. p ^ f p)"
eberlm@63633
   550
  shows "S = prime_factors n \<and> (\<forall>p. prime p \<longrightarrow> f p = multiplicity p n)"
eberlm@63534
   551
  using assms by (intro prime_factorization_unique'') auto
eberlm@63534
   552
eberlm@63534
   553
lemma prime_factorization_unique_int:
eberlm@63534
   554
  fixes f :: "int \<Rightarrow> _"
eberlm@63534
   555
  assumes S_eq: "S = {p. 0 < f p}"
eberlm@63534
   556
    and "finite S"
eberlm@63534
   557
    and S: "\<forall>p\<in>S. prime p" "abs n = (\<Prod>p\<in>S. p ^ f p)"
eberlm@63633
   558
  shows "S = prime_factors n \<and> (\<forall>p. prime p \<longrightarrow> f p = multiplicity p n)"
eberlm@63534
   559
  using assms by (intro prime_factorization_unique'') auto
eberlm@63534
   560
eberlm@63534
   561
lemma prime_factors_characterization_nat:
eberlm@63534
   562
  "S = {p. 0 < f (p::nat)} \<Longrightarrow>
eberlm@63534
   563
    finite S \<Longrightarrow> \<forall>p\<in>S. prime p \<Longrightarrow> n = (\<Prod>p\<in>S. p ^ f p) \<Longrightarrow> prime_factors n = S"
eberlm@63534
   564
  by (rule prime_factorization_unique_nat [THEN conjunct1, symmetric])
eberlm@63534
   565
eberlm@63534
   566
lemma prime_factors_characterization'_nat:
eberlm@63534
   567
  "finite {p. 0 < f (p::nat)} \<Longrightarrow>
eberlm@63534
   568
    (\<forall>p. 0 < f p \<longrightarrow> prime p) \<Longrightarrow>
eberlm@63534
   569
      prime_factors (\<Prod>p | 0 < f p. p ^ f p) = {p. 0 < f p}"
eberlm@63534
   570
  by (rule prime_factors_characterization_nat) auto
eberlm@63534
   571
eberlm@63534
   572
lemma prime_factors_characterization_int:
eberlm@63534
   573
  "S = {p. 0 < f (p::int)} \<Longrightarrow> finite S \<Longrightarrow>
eberlm@63534
   574
    \<forall>p\<in>S. prime p \<Longrightarrow> abs n = (\<Prod>p\<in>S. p ^ f p) \<Longrightarrow> prime_factors n = S"
eberlm@63534
   575
  by (rule prime_factorization_unique_int [THEN conjunct1, symmetric])
eberlm@63534
   576
eberlm@63534
   577
(* TODO Move *)
nipkow@64272
   578
lemma abs_prod: "abs (prod f A :: 'a :: linordered_idom) = prod (\<lambda>x. abs (f x)) A"
eberlm@63534
   579
  by (cases "finite A", induction A rule: finite_induct) (simp_all add: abs_mult)
eberlm@63534
   580
eberlm@63534
   581
lemma primes_characterization'_int [rule_format]:
eberlm@63534
   582
  "finite {p. p \<ge> 0 \<and> 0 < f (p::int)} \<Longrightarrow> \<forall>p. 0 < f p \<longrightarrow> prime p \<Longrightarrow>
eberlm@63534
   583
      prime_factors (\<Prod>p | p \<ge> 0 \<and> 0 < f p. p ^ f p) = {p. p \<ge> 0 \<and> 0 < f p}"
nipkow@64272
   584
  by (rule prime_factors_characterization_int) (auto simp: abs_prod prime_ge_0_int)
eberlm@63534
   585
eberlm@63534
   586
lemma multiplicity_characterization_nat:
eberlm@63633
   587
  "S = {p. 0 < f (p::nat)} \<Longrightarrow> finite S \<Longrightarrow> \<forall>p\<in>S. prime p \<Longrightarrow> prime p \<Longrightarrow>
eberlm@63534
   588
    n = (\<Prod>p\<in>S. p ^ f p) \<Longrightarrow> multiplicity p n = f p"
eberlm@63534
   589
  by (frule prime_factorization_unique_nat [of S f n, THEN conjunct2, rule_format, symmetric]) auto
eberlm@63534
   590
eberlm@63534
   591
lemma multiplicity_characterization'_nat: "finite {p. 0 < f (p::nat)} \<longrightarrow>
eberlm@63633
   592
    (\<forall>p. 0 < f p \<longrightarrow> prime p) \<longrightarrow> prime p \<longrightarrow>
eberlm@63534
   593
      multiplicity p (\<Prod>p | 0 < f p. p ^ f p) = f p"
eberlm@63534
   594
  by (intro impI, rule multiplicity_characterization_nat) auto
eberlm@63534
   595
eberlm@63534
   596
lemma multiplicity_characterization_int: "S = {p. 0 < f (p::int)} \<Longrightarrow>
eberlm@63633
   597
    finite S \<Longrightarrow> \<forall>p\<in>S. prime p \<Longrightarrow> prime p \<Longrightarrow> n = (\<Prod>p\<in>S. p ^ f p) \<Longrightarrow> multiplicity p n = f p"
eberlm@63534
   598
  by (frule prime_factorization_unique_int [of S f n, THEN conjunct2, rule_format, symmetric]) 
nipkow@64272
   599
     (auto simp: abs_prod power_abs prime_ge_0_int intro!: prod.cong)
eberlm@63534
   600
eberlm@63534
   601
lemma multiplicity_characterization'_int [rule_format]:
eberlm@63534
   602
  "finite {p. p \<ge> 0 \<and> 0 < f (p::int)} \<Longrightarrow>
eberlm@63633
   603
    (\<forall>p. 0 < f p \<longrightarrow> prime p) \<Longrightarrow> prime p \<Longrightarrow>
eberlm@63534
   604
      multiplicity p (\<Prod>p | p \<ge> 0 \<and> 0 < f p. p ^ f p) = f p"
eberlm@63534
   605
  by (rule multiplicity_characterization_int) (auto simp: prime_ge_0_int)
eberlm@63534
   606
eberlm@63534
   607
lemma multiplicity_one_nat [simp]: "multiplicity p (Suc 0) = 0"
eberlm@63534
   608
  unfolding One_nat_def [symmetric] by (rule multiplicity_one)
eberlm@63534
   609
eberlm@63534
   610
lemma multiplicity_eq_nat:
eberlm@63534
   611
  fixes x and y::nat
eberlm@63633
   612
  assumes "x > 0" "y > 0" "\<And>p. prime p \<Longrightarrow> multiplicity p x = multiplicity p y"
eberlm@63534
   613
  shows "x = y"
eberlm@63534
   614
  using multiplicity_eq_imp_eq[of x y] assms by simp
eberlm@63534
   615
eberlm@63534
   616
lemma multiplicity_eq_int:
eberlm@63534
   617
  fixes x y :: int
eberlm@63633
   618
  assumes "x > 0" "y > 0" "\<And>p. prime p \<Longrightarrow> multiplicity p x = multiplicity p y"
eberlm@63534
   619
  shows "x = y"
eberlm@63534
   620
  using multiplicity_eq_imp_eq[of x y] assms by simp
eberlm@63534
   621
eberlm@63534
   622
lemma multiplicity_prod_prime_powers:
eberlm@63633
   623
  assumes "finite S" "\<And>x. x \<in> S \<Longrightarrow> prime x" "prime p"
eberlm@63534
   624
  shows   "multiplicity p (\<Prod>p \<in> S. p ^ f p) = (if p \<in> S then f p else 0)"
eberlm@63534
   625
proof -
eberlm@63534
   626
  define g where "g = (\<lambda>x. if x \<in> S then f x else 0)"
eberlm@63534
   627
  define A where "A = Abs_multiset g"
eberlm@63534
   628
  have "{x. g x > 0} \<subseteq> S" by (auto simp: g_def)
eberlm@63534
   629
  from finite_subset[OF this assms(1)] have [simp]: "g :  multiset"
eberlm@63534
   630
    by (simp add: multiset_def)
eberlm@63534
   631
  from assms have count_A: "count A x = g x" for x unfolding A_def
eberlm@63534
   632
    by simp
eberlm@63534
   633
  have set_mset_A: "set_mset A = {x\<in>S. f x > 0}"
eberlm@63534
   634
    unfolding set_mset_def count_A by (auto simp: g_def)
eberlm@63534
   635
  with assms have prime: "prime x" if "x \<in># A" for x using that by auto
eberlm@63534
   636
  from set_mset_A assms have "(\<Prod>p \<in> S. p ^ f p) = (\<Prod>p \<in> S. p ^ g p) "
nipkow@64272
   637
    by (intro prod.cong) (auto simp: g_def)
eberlm@63534
   638
  also from set_mset_A assms have "\<dots> = (\<Prod>p \<in> set_mset A. p ^ g p)"
nipkow@64272
   639
    by (intro prod.mono_neutral_right) (auto simp: g_def set_mset_A)
nipkow@63830
   640
  also have "\<dots> = prod_mset A"
nipkow@64272
   641
    by (auto simp: prod_mset_multiplicity count_A set_mset_A intro!: prod.cong)
nipkow@63830
   642
  also from assms have "multiplicity p \<dots> = sum_mset (image_mset (multiplicity p) A)"
nipkow@63830
   643
    by (subst prime_elem_multiplicity_prod_mset_distrib) (auto dest: prime)
eberlm@63534
   644
  also from assms have "image_mset (multiplicity p) A = image_mset (\<lambda>x. if x = p then 1 else 0) A"
eberlm@63534
   645
    by (intro image_mset_cong) (auto simp: prime_multiplicity_other dest: prime)
nipkow@63830
   646
  also have "sum_mset \<dots> = (if p \<in> S then f p else 0)" by (simp add: sum_mset_delta count_A g_def)
eberlm@63534
   647
  finally show ?thesis .
eberlm@63534
   648
qed
eberlm@63534
   649
haftmann@63904
   650
lemma prime_factorization_prod_mset:
haftmann@63904
   651
  assumes "0 \<notin># A"
haftmann@63904
   652
  shows "prime_factorization (prod_mset A) = \<Union>#(image_mset prime_factorization A)"
haftmann@63904
   653
  using assms by (induct A) (auto simp add: prime_factorization_mult)
haftmann@63904
   654
nipkow@64272
   655
lemma prime_factors_prod:
haftmann@63904
   656
  assumes "finite A" and "0 \<notin> f ` A"
nipkow@64272
   657
  shows "prime_factors (prod f A) = UNION A (prime_factors \<circ> f)"
nipkow@64272
   658
  using assms by (simp add: prod_unfold_prod_mset prime_factorization_prod_mset)
haftmann@63904
   659
haftmann@63904
   660
lemma prime_factors_fact:
haftmann@63904
   661
  "prime_factors (fact n) = {p \<in> {2..n}. prime p}" (is "?M = ?N")
haftmann@63904
   662
proof (rule set_eqI)
haftmann@63904
   663
  fix p
haftmann@63904
   664
  { fix m :: nat
haftmann@63904
   665
    assume "p \<in> prime_factors m"
haftmann@63904
   666
    then have "prime p" and "p dvd m" by auto
haftmann@63904
   667
    moreover assume "m > 0" 
haftmann@63904
   668
    ultimately have "2 \<le> p" and "p \<le> m"
haftmann@63904
   669
      by (auto intro: prime_ge_2_nat dest: dvd_imp_le)
haftmann@63904
   670
    moreover assume "m \<le> n"
haftmann@63904
   671
    ultimately have "2 \<le> p" and "p \<le> n"
haftmann@63904
   672
      by (auto intro: order_trans)
haftmann@63904
   673
  } note * = this
haftmann@63904
   674
  show "p \<in> ?M \<longleftrightarrow> p \<in> ?N"
nipkow@64272
   675
    by (auto simp add: fact_prod prime_factors_prod Suc_le_eq dest!: prime_prime_factors intro: *)
haftmann@63904
   676
qed
haftmann@63904
   677
eberlm@63766
   678
lemma prime_dvd_fact_iff:
eberlm@63766
   679
  assumes "prime p"
haftmann@63904
   680
  shows "p dvd fact n \<longleftrightarrow> p \<le> n"
haftmann@63904
   681
  using assms
haftmann@63904
   682
  by (auto simp add: prime_factorization_subset_iff_dvd [symmetric]
haftmann@63905
   683
    prime_factorization_prime prime_factors_fact prime_ge_2_nat)
eberlm@63766
   684
eberlm@63534
   685
(* TODO Legacy names *)
eberlm@63633
   686
lemmas prime_imp_coprime_nat = prime_imp_coprime[where ?'a = nat]
eberlm@63633
   687
lemmas prime_imp_coprime_int = prime_imp_coprime[where ?'a = int]
eberlm@63633
   688
lemmas prime_dvd_mult_nat = prime_dvd_mult_iff[where ?'a = nat]
eberlm@63633
   689
lemmas prime_dvd_mult_int = prime_dvd_mult_iff[where ?'a = int]
eberlm@63633
   690
lemmas prime_dvd_mult_eq_nat = prime_dvd_mult_iff[where ?'a = nat]
eberlm@63633
   691
lemmas prime_dvd_mult_eq_int = prime_dvd_mult_iff[where ?'a = int]
eberlm@63633
   692
lemmas prime_dvd_power_nat = prime_dvd_power[where ?'a = nat]
eberlm@63633
   693
lemmas prime_dvd_power_int = prime_dvd_power[where ?'a = int]
eberlm@63633
   694
lemmas prime_dvd_power_nat_iff = prime_dvd_power_iff[where ?'a = nat]
eberlm@63633
   695
lemmas prime_dvd_power_int_iff = prime_dvd_power_iff[where ?'a = int]
eberlm@63633
   696
lemmas prime_imp_power_coprime_nat = prime_imp_power_coprime[where ?'a = nat]
eberlm@63633
   697
lemmas prime_imp_power_coprime_int = prime_imp_power_coprime[where ?'a = int]
eberlm@63534
   698
lemmas primes_coprime_nat = primes_coprime[where ?'a = nat]
eberlm@63534
   699
lemmas primes_coprime_int = primes_coprime[where ?'a = nat]
eberlm@63633
   700
lemmas prime_divprod_pow_nat = prime_elem_divprod_pow[where ?'a = nat]
eberlm@63633
   701
lemmas prime_exp = prime_elem_power_iff[where ?'a = nat]
eberlm@63534
   702
haftmann@65025
   703
text \<open>Code generation\<close>
haftmann@65025
   704
  
haftmann@65025
   705
context
haftmann@65025
   706
begin
haftmann@65025
   707
haftmann@65025
   708
qualified definition prime_nat :: "nat \<Rightarrow> bool"
haftmann@65025
   709
  where [simp, code_abbrev]: "prime_nat = prime"
haftmann@65025
   710
haftmann@65025
   711
lemma prime_nat_naive [code]:
haftmann@65025
   712
  "prime_nat p \<longleftrightarrow> p > 1 \<and> (\<forall>n \<in>{1<..<p}. \<not> n dvd p)"
haftmann@65025
   713
  by (auto simp add: prime_nat_iff')
haftmann@65025
   714
haftmann@65025
   715
qualified definition prime_int :: "int \<Rightarrow> bool"
haftmann@65025
   716
  where [simp, code_abbrev]: "prime_int = prime"
haftmann@65025
   717
haftmann@65025
   718
lemma prime_int_naive [code]:
haftmann@65025
   719
  "prime_int p \<longleftrightarrow> p > 1 \<and> (\<forall>n \<in>{1<..<p}. \<not> n dvd p)"
haftmann@65025
   720
  by (auto simp add: prime_int_iff')
haftmann@65025
   721
haftmann@65025
   722
lemma "prime(997::nat)" by eval
haftmann@65025
   723
haftmann@65025
   724
lemma "prime(997::int)" by eval
haftmann@65025
   725
  
eberlm@63635
   726
end
haftmann@65025
   727
haftmann@65025
   728
end