src/HOL/Number_Theory/Primes.thy
author huffman
Thu Sep 12 15:08:07 2013 -0700 (2013-09-12)
changeset 53598 2bd8d459ebae
parent 47108 2a1953f0d20d
child 54228 229282d53781
permissions -rw-r--r--
remove unneeded assumption from prime_dvd_power lemmas;
add iff forms of prime_dvd_power lemmas (thanks to Jason Dagit)
haftmann@32479
     1
(*  Authors:    Christophe Tabacznyj, Lawrence C. Paulson, Amine Chaieb,
nipkow@31798
     2
                Thomas M. Rasmussen, Jeremy Avigad, Tobias Nipkow
huffman@31706
     3
huffman@31706
     4
haftmann@32479
     5
This file deals with properties of primes. Definitions and lemmas are
haftmann@32479
     6
proved uniformly for the natural numbers and integers.
huffman@31706
     7
huffman@31706
     8
This file combines and revises a number of prior developments.
huffman@31706
     9
huffman@31706
    10
The original theories "GCD" and "Primes" were by Christophe Tabacznyj
huffman@31706
    11
and Lawrence C. Paulson, based on \cite{davenport92}. They introduced
huffman@31706
    12
gcd, lcm, and prime for the natural numbers.
huffman@31706
    13
huffman@31706
    14
The original theory "IntPrimes" was by Thomas M. Rasmussen, and
huffman@31706
    15
extended gcd, lcm, primes to the integers. Amine Chaieb provided
huffman@31706
    16
another extension of the notions to the integers, and added a number
huffman@31706
    17
of results to "Primes" and "GCD". IntPrimes also defined and developed
huffman@31706
    18
the congruence relations on the integers. The notion was extended to
webertj@33718
    19
the natural numbers by Chaieb.
huffman@31706
    20
avigad@32036
    21
Jeremy Avigad combined all of these, made everything uniform for the
avigad@32036
    22
natural numbers and the integers, and added a number of new theorems.
avigad@32036
    23
nipkow@31798
    24
Tobias Nipkow cleaned up a lot.
wenzelm@21256
    25
*)
wenzelm@21256
    26
huffman@31706
    27
haftmann@32479
    28
header {* Primes *}
wenzelm@21256
    29
haftmann@32479
    30
theory Primes
haftmann@37294
    31
imports "~~/src/HOL/GCD"
huffman@31706
    32
begin
huffman@31706
    33
huffman@31706
    34
class prime = one +
wenzelm@44872
    35
  fixes prime :: "'a \<Rightarrow> bool"
huffman@31706
    36
huffman@31706
    37
instantiation nat :: prime
huffman@31706
    38
begin
wenzelm@21256
    39
wenzelm@44872
    40
definition prime_nat :: "nat \<Rightarrow> bool"
wenzelm@44872
    41
  where "prime_nat p = (1 < p \<and> (\<forall>m. m dvd p --> m = 1 \<or> m = p))"
huffman@31706
    42
wenzelm@44872
    43
instance ..
haftmann@23687
    44
huffman@31706
    45
end
huffman@31706
    46
huffman@31706
    47
instantiation int :: prime
huffman@31706
    48
begin
huffman@31706
    49
wenzelm@44872
    50
definition prime_int :: "int \<Rightarrow> bool"
wenzelm@44872
    51
  where "prime_int p = prime (nat p)"
huffman@31706
    52
wenzelm@44872
    53
instance ..
huffman@31706
    54
huffman@31706
    55
end
huffman@31706
    56
huffman@31706
    57
huffman@31706
    58
subsection {* Set up Transfer *}
huffman@31706
    59
haftmann@32479
    60
lemma transfer_nat_int_prime:
huffman@31706
    61
  "(x::int) >= 0 \<Longrightarrow> prime (nat x) = prime x"
wenzelm@44872
    62
  unfolding gcd_int_def lcm_int_def prime_int_def by auto
haftmann@23687
    63
haftmann@35644
    64
declare transfer_morphism_nat_int[transfer add return:
haftmann@32479
    65
    transfer_nat_int_prime]
huffman@31706
    66
wenzelm@44872
    67
lemma transfer_int_nat_prime: "prime (int x) = prime x"
wenzelm@44872
    68
  unfolding gcd_int_def lcm_int_def prime_int_def by auto
huffman@31706
    69
haftmann@35644
    70
declare transfer_morphism_int_nat[transfer add return:
haftmann@32479
    71
    transfer_int_nat_prime]
nipkow@31798
    72
wenzelm@21256
    73
haftmann@32479
    74
subsection {* Primes *}
huffman@31706
    75
nipkow@31952
    76
lemma prime_odd_nat: "prime (p::nat) \<Longrightarrow> p > 2 \<Longrightarrow> odd p"
huffman@31706
    77
  unfolding prime_nat_def
paulson@40461
    78
  by (metis gcd_lcm_complete_lattice_nat.bot_least nat_even_iff_2_dvd nat_neq_iff odd_1_nat)
huffman@31706
    79
nipkow@31952
    80
lemma prime_odd_int: "prime (p::int) \<Longrightarrow> p > 2 \<Longrightarrow> odd p"
huffman@31706
    81
  unfolding prime_int_def
nipkow@31952
    82
  apply (frule prime_odd_nat)
huffman@31706
    83
  apply (auto simp add: even_nat_def)
wenzelm@44872
    84
  done
huffman@31706
    85
nipkow@31992
    86
(* FIXME Is there a better way to handle these, rather than making them elim rules? *)
chaieb@22027
    87
nipkow@31952
    88
lemma prime_ge_0_nat [elim]: "prime (p::nat) \<Longrightarrow> p >= 0"
wenzelm@44872
    89
  unfolding prime_nat_def by auto
chaieb@22027
    90
nipkow@31952
    91
lemma prime_gt_0_nat [elim]: "prime (p::nat) \<Longrightarrow> p > 0"
wenzelm@44872
    92
  unfolding prime_nat_def by auto
wenzelm@22367
    93
nipkow@31952
    94
lemma prime_ge_1_nat [elim]: "prime (p::nat) \<Longrightarrow> p >= 1"
wenzelm@44872
    95
  unfolding prime_nat_def by auto
chaieb@22027
    96
nipkow@31952
    97
lemma prime_gt_1_nat [elim]: "prime (p::nat) \<Longrightarrow> p > 1"
wenzelm@44872
    98
  unfolding prime_nat_def by auto
wenzelm@22367
    99
nipkow@31952
   100
lemma prime_ge_Suc_0_nat [elim]: "prime (p::nat) \<Longrightarrow> p >= Suc 0"
wenzelm@44872
   101
  unfolding prime_nat_def by auto
wenzelm@22367
   102
nipkow@31952
   103
lemma prime_gt_Suc_0_nat [elim]: "prime (p::nat) \<Longrightarrow> p > Suc 0"
wenzelm@44872
   104
  unfolding prime_nat_def by auto
huffman@31706
   105
nipkow@31952
   106
lemma prime_ge_2_nat [elim]: "prime (p::nat) \<Longrightarrow> p >= 2"
wenzelm@44872
   107
  unfolding prime_nat_def by auto
huffman@31706
   108
nipkow@31952
   109
lemma prime_ge_0_int [elim]: "prime (p::int) \<Longrightarrow> p >= 0"
wenzelm@44872
   110
  unfolding prime_int_def prime_nat_def by auto
wenzelm@22367
   111
nipkow@31952
   112
lemma prime_gt_0_int [elim]: "prime (p::int) \<Longrightarrow> p > 0"
wenzelm@44872
   113
  unfolding prime_int_def prime_nat_def by auto
huffman@31706
   114
nipkow@31952
   115
lemma prime_ge_1_int [elim]: "prime (p::int) \<Longrightarrow> p >= 1"
wenzelm@44872
   116
  unfolding prime_int_def prime_nat_def by auto
chaieb@22027
   117
nipkow@31952
   118
lemma prime_gt_1_int [elim]: "prime (p::int) \<Longrightarrow> p > 1"
wenzelm@44872
   119
  unfolding prime_int_def prime_nat_def by auto
huffman@31706
   120
nipkow@31952
   121
lemma prime_ge_2_int [elim]: "prime (p::int) \<Longrightarrow> p >= 2"
wenzelm@44872
   122
  unfolding prime_int_def prime_nat_def by auto
wenzelm@22367
   123
huffman@31706
   124
huffman@31706
   125
lemma prime_int_altdef: "prime (p::int) = (1 < p \<and> (\<forall>m \<ge> 0. m dvd p \<longrightarrow>
huffman@31706
   126
    m = 1 \<or> m = p))"
huffman@31706
   127
  using prime_nat_def [transferred]
wenzelm@44872
   128
  apply (cases "p >= 0")
wenzelm@44872
   129
  apply blast
wenzelm@44872
   130
  apply (auto simp add: prime_ge_0_int)
wenzelm@44872
   131
  done
huffman@31706
   132
nipkow@31952
   133
lemma prime_imp_coprime_nat: "prime (p::nat) \<Longrightarrow> \<not> p dvd n \<Longrightarrow> coprime p n"
huffman@31706
   134
  apply (unfold prime_nat_def)
nipkow@31952
   135
  apply (metis gcd_dvd1_nat gcd_dvd2_nat)
huffman@31706
   136
  done
huffman@31706
   137
nipkow@31952
   138
lemma prime_imp_coprime_int: "prime (p::int) \<Longrightarrow> \<not> p dvd n \<Longrightarrow> coprime p n"
huffman@31706
   139
  apply (unfold prime_int_altdef)
nipkow@31952
   140
  apply (metis gcd_dvd1_int gcd_dvd2_int gcd_ge_0_int)
chaieb@27568
   141
  done
chaieb@27568
   142
nipkow@31952
   143
lemma prime_dvd_mult_nat: "prime (p::nat) \<Longrightarrow> p dvd m * n \<Longrightarrow> p dvd m \<or> p dvd n"
nipkow@31952
   144
  by (blast intro: coprime_dvd_mult_nat prime_imp_coprime_nat)
huffman@31706
   145
nipkow@31952
   146
lemma prime_dvd_mult_int: "prime (p::int) \<Longrightarrow> p dvd m * n \<Longrightarrow> p dvd m \<or> p dvd n"
nipkow@31952
   147
  by (blast intro: coprime_dvd_mult_int prime_imp_coprime_int)
huffman@31706
   148
nipkow@31952
   149
lemma prime_dvd_mult_eq_nat [simp]: "prime (p::nat) \<Longrightarrow>
huffman@31706
   150
    p dvd m * n = (p dvd m \<or> p dvd n)"
nipkow@31952
   151
  by (rule iffI, rule prime_dvd_mult_nat, auto)
chaieb@27568
   152
nipkow@31952
   153
lemma prime_dvd_mult_eq_int [simp]: "prime (p::int) \<Longrightarrow>
huffman@31706
   154
    p dvd m * n = (p dvd m \<or> p dvd n)"
nipkow@31952
   155
  by (rule iffI, rule prime_dvd_mult_int, auto)
chaieb@27568
   156
nipkow@31952
   157
lemma not_prime_eq_prod_nat: "(n::nat) > 1 \<Longrightarrow> ~ prime n \<Longrightarrow>
huffman@31706
   158
    EX m k. n = m * k & 1 < m & m < n & 1 < k & k < n"
huffman@31706
   159
  unfolding prime_nat_def dvd_def apply auto
wenzelm@44872
   160
  by (metis mult_commute linorder_neq_iff linorder_not_le mult_1
wenzelm@44872
   161
      n_less_n_mult_m one_le_mult_iff less_imp_le_nat)
chaieb@27568
   162
nipkow@31952
   163
lemma not_prime_eq_prod_int: "(n::int) > 1 \<Longrightarrow> ~ prime n \<Longrightarrow>
huffman@31706
   164
    EX m k. n = m * k & 1 < m & m < n & 1 < k & k < n"
huffman@31706
   165
  unfolding prime_int_altdef dvd_def
huffman@31706
   166
  apply auto
wenzelm@44872
   167
  by (metis div_mult_self1_is_id div_mult_self2_is_id
wenzelm@44872
   168
      int_div_less_self int_one_le_iff_zero_less zero_less_mult_pos less_le)
chaieb@27568
   169
huffman@53598
   170
lemma prime_dvd_power_nat: "prime (p::nat) \<Longrightarrow> p dvd x^n \<Longrightarrow> p dvd x"
huffman@53598
   171
  by (induct n) auto
chaieb@27568
   172
huffman@53598
   173
lemma prime_dvd_power_int: "prime (p::int) \<Longrightarrow> p dvd x^n \<Longrightarrow> p dvd x"
huffman@53598
   174
  apply (induct n)
nipkow@31952
   175
  apply (frule prime_ge_0_int)
huffman@31706
   176
  apply auto
wenzelm@44872
   177
  done
huffman@31706
   178
huffman@53598
   179
lemma prime_dvd_power_nat_iff: "prime (p::nat) \<Longrightarrow> n > 0 \<Longrightarrow>
huffman@53598
   180
    p dvd x^n \<longleftrightarrow> p dvd x"
huffman@53598
   181
  by (cases n) (auto elim: prime_dvd_power_nat)
huffman@53598
   182
huffman@53598
   183
lemma prime_dvd_power_int_iff: "prime (p::int) \<Longrightarrow> n > 0 \<Longrightarrow>
huffman@53598
   184
    p dvd x^n \<longleftrightarrow> p dvd x"
huffman@53598
   185
  by (cases n) (auto elim: prime_dvd_power_int)
huffman@53598
   186
huffman@53598
   187
wenzelm@44872
   188
subsubsection {* Make prime naively executable *}
nipkow@32007
   189
nipkow@32007
   190
lemma zero_not_prime_nat [simp]: "~prime (0::nat)"
nipkow@32007
   191
  by (simp add: prime_nat_def)
nipkow@32007
   192
nipkow@32007
   193
lemma zero_not_prime_int [simp]: "~prime (0::int)"
nipkow@32007
   194
  by (simp add: prime_int_def)
nipkow@32007
   195
nipkow@32007
   196
lemma one_not_prime_nat [simp]: "~prime (1::nat)"
nipkow@32007
   197
  by (simp add: prime_nat_def)
nipkow@32007
   198
nipkow@32007
   199
lemma Suc_0_not_prime_nat [simp]: "~prime (Suc 0)"
nipkow@32007
   200
  by (simp add: prime_nat_def One_nat_def)
nipkow@32007
   201
nipkow@32007
   202
lemma one_not_prime_int [simp]: "~prime (1::int)"
nipkow@32007
   203
  by (simp add: prime_int_def)
nipkow@32007
   204
haftmann@37607
   205
lemma prime_nat_code [code]:
wenzelm@44872
   206
    "prime (p::nat) \<longleftrightarrow> p > 1 \<and> (\<forall>n \<in> {1<..<p}. ~ n dvd p)"
wenzelm@44872
   207
  apply (simp add: Ball_def)
wenzelm@44872
   208
  apply (metis less_not_refl prime_nat_def dvd_triv_right not_prime_eq_prod_nat)
wenzelm@44872
   209
  done
nipkow@32007
   210
nipkow@32007
   211
lemma prime_nat_simp:
wenzelm@44872
   212
    "prime (p::nat) \<longleftrightarrow> p > 1 \<and> (\<forall>n \<in> set [2..<p]. \<not> n dvd p)"
wenzelm@44872
   213
  by (auto simp add: prime_nat_code)
nipkow@32007
   214
huffman@47108
   215
lemmas prime_nat_simp_numeral [simp] = prime_nat_simp [of "numeral m"] for m
nipkow@32007
   216
haftmann@37607
   217
lemma prime_int_code [code]:
haftmann@37607
   218
  "prime (p::int) \<longleftrightarrow> p > 1 \<and> (\<forall>n \<in> {1<..<p}. ~ n dvd p)" (is "?L = ?R")
nipkow@32007
   219
proof
wenzelm@44872
   220
  assume "?L"
wenzelm@44872
   221
  then show "?R"
huffman@44821
   222
    by (clarsimp simp: prime_gt_1_int) (metis int_one_le_iff_zero_less prime_int_altdef less_le)
nipkow@32007
   223
next
wenzelm@44872
   224
  assume "?R"
wenzelm@44872
   225
  then show "?L" by (clarsimp simp: Ball_def) (metis dvdI not_prime_eq_prod_int)
nipkow@32007
   226
qed
nipkow@32007
   227
wenzelm@44872
   228
lemma prime_int_simp: "prime (p::int) \<longleftrightarrow> p > 1 \<and> (\<forall>n \<in> set [2..p - 1]. ~ n dvd p)"
wenzelm@44872
   229
  by (auto simp add: prime_int_code)
nipkow@32007
   230
huffman@47108
   231
lemmas prime_int_simp_numeral [simp] = prime_int_simp [of "numeral m"] for m
nipkow@32007
   232
nipkow@32007
   233
lemma two_is_prime_nat [simp]: "prime (2::nat)"
wenzelm@44872
   234
  by simp
nipkow@32007
   235
nipkow@32007
   236
lemma two_is_prime_int [simp]: "prime (2::int)"
wenzelm@44872
   237
  by simp
nipkow@32007
   238
nipkow@32111
   239
text{* A bit of regression testing: *}
nipkow@32111
   240
wenzelm@44872
   241
lemma "prime(97::nat)" by simp
wenzelm@44872
   242
lemma "prime(97::int)" by simp
wenzelm@44872
   243
lemma "prime(997::nat)" by eval
wenzelm@44872
   244
lemma "prime(997::int)" by eval
nipkow@32111
   245
nipkow@32007
   246
nipkow@32007
   247
lemma prime_imp_power_coprime_nat: "prime (p::nat) \<Longrightarrow> ~ p dvd a \<Longrightarrow> coprime a (p^m)"
nipkow@31952
   248
  apply (rule coprime_exp_nat)
nipkow@31952
   249
  apply (subst gcd_commute_nat)
nipkow@31952
   250
  apply (erule (1) prime_imp_coprime_nat)
wenzelm@44872
   251
  done
chaieb@27568
   252
nipkow@32007
   253
lemma prime_imp_power_coprime_int: "prime (p::int) \<Longrightarrow> ~ p dvd a \<Longrightarrow> coprime a (p^m)"
nipkow@31952
   254
  apply (rule coprime_exp_int)
nipkow@31952
   255
  apply (subst gcd_commute_int)
nipkow@31952
   256
  apply (erule (1) prime_imp_coprime_int)
wenzelm@44872
   257
  done
chaieb@27568
   258
nipkow@31952
   259
lemma primes_coprime_nat: "prime (p::nat) \<Longrightarrow> prime q \<Longrightarrow> p \<noteq> q \<Longrightarrow> coprime p q"
nipkow@31952
   260
  apply (rule prime_imp_coprime_nat, assumption)
wenzelm@44872
   261
  apply (unfold prime_nat_def)
wenzelm@44872
   262
  apply auto
wenzelm@44872
   263
  done
chaieb@27568
   264
nipkow@31952
   265
lemma primes_coprime_int: "prime (p::int) \<Longrightarrow> prime q \<Longrightarrow> p \<noteq> q \<Longrightarrow> coprime p q"
nipkow@31952
   266
  apply (rule prime_imp_coprime_int, assumption)
paulson@40461
   267
  apply (unfold prime_int_altdef)
huffman@44821
   268
  apply (metis int_one_le_iff_zero_less less_le)
wenzelm@44872
   269
  done
chaieb@27568
   270
wenzelm@44872
   271
lemma primes_imp_powers_coprime_nat:
wenzelm@44872
   272
    "prime (p::nat) \<Longrightarrow> prime q \<Longrightarrow> p ~= q \<Longrightarrow> coprime (p^m) (q^n)"
nipkow@31952
   273
  by (rule coprime_exp2_nat, rule primes_coprime_nat)
chaieb@27568
   274
wenzelm@44872
   275
lemma primes_imp_powers_coprime_int:
wenzelm@44872
   276
    "prime (p::int) \<Longrightarrow> prime q \<Longrightarrow> p ~= q \<Longrightarrow> coprime (p^m) (q^n)"
nipkow@31952
   277
  by (rule coprime_exp2_int, rule primes_coprime_int)
chaieb@27568
   278
nipkow@31952
   279
lemma prime_factor_nat: "n \<noteq> (1::nat) \<Longrightarrow> \<exists> p. prime p \<and> p dvd n"
huffman@31706
   280
  apply (induct n rule: nat_less_induct)
huffman@31706
   281
  apply (case_tac "n = 0")
wenzelm@44872
   282
  using two_is_prime_nat
wenzelm@44872
   283
  apply blast
wenzelm@44872
   284
  apply (metis One_nat_def dvd.order_trans dvd_refl less_Suc0 linorder_neqE_nat
wenzelm@44872
   285
    nat_dvd_not_less neq0_conv prime_nat_def)
wenzelm@44872
   286
  done
chaieb@23244
   287
huffman@31706
   288
(* An Isar version:
huffman@31706
   289
nipkow@31952
   290
lemma prime_factor_b_nat:
huffman@31706
   291
  fixes n :: nat
huffman@31706
   292
  assumes "n \<noteq> 1"
huffman@31706
   293
  shows "\<exists>p. prime p \<and> p dvd n"
nipkow@23983
   294
huffman@31706
   295
using `n ~= 1`
nipkow@31952
   296
proof (induct n rule: less_induct_nat)
huffman@31706
   297
  fix n :: nat
huffman@31706
   298
  assume "n ~= 1" and
huffman@31706
   299
    ih: "\<forall>m<n. m \<noteq> 1 \<longrightarrow> (\<exists>p. prime p \<and> p dvd m)"
wenzelm@44872
   300
  then show "\<exists>p. prime p \<and> p dvd n"
huffman@31706
   301
  proof -
huffman@31706
   302
  {
huffman@31706
   303
    assume "n = 0"
nipkow@31952
   304
    moreover note two_is_prime_nat
huffman@31706
   305
    ultimately have ?thesis
nipkow@31952
   306
      by (auto simp del: two_is_prime_nat)
huffman@31706
   307
  }
huffman@31706
   308
  moreover
huffman@31706
   309
  {
huffman@31706
   310
    assume "prime n"
wenzelm@44872
   311
    then have ?thesis by auto
huffman@31706
   312
  }
huffman@31706
   313
  moreover
huffman@31706
   314
  {
huffman@31706
   315
    assume "n ~= 0" and "~ prime n"
huffman@31706
   316
    with `n ~= 1` have "n > 1" by auto
nipkow@31952
   317
    with `~ prime n` and not_prime_eq_prod_nat obtain m k where
huffman@31706
   318
      "n = m * k" and "1 < m" and "m < n" by blast
huffman@31706
   319
    with ih obtain p where "prime p" and "p dvd m" by blast
huffman@31706
   320
    with `n = m * k` have ?thesis by auto
huffman@31706
   321
  }
huffman@31706
   322
  ultimately show ?thesis by blast
huffman@31706
   323
  qed
nipkow@23983
   324
qed
nipkow@23983
   325
huffman@31706
   326
*)
huffman@31706
   327
huffman@31706
   328
text {* One property of coprimality is easier to prove via prime factors. *}
huffman@31706
   329
nipkow@31952
   330
lemma prime_divprod_pow_nat:
huffman@31706
   331
  assumes p: "prime (p::nat)" and ab: "coprime a b" and pab: "p^n dvd a * b"
huffman@31706
   332
  shows "p^n dvd a \<or> p^n dvd b"
huffman@31706
   333
proof-
wenzelm@44872
   334
  { assume "n = 0 \<or> a = 1 \<or> b = 1" with pab have ?thesis
huffman@31706
   335
      apply (cases "n=0", simp_all)
wenzelm@44872
   336
      apply (cases "a=1", simp_all)
wenzelm@44872
   337
      done }
huffman@31706
   338
  moreover
wenzelm@44872
   339
  { assume n: "n \<noteq> 0" and a: "a\<noteq>1" and b: "b\<noteq>1"
wenzelm@44872
   340
    then obtain m where m: "n = Suc m" by (cases n) auto
wenzelm@44872
   341
    from n have "p dvd p^n" apply (intro dvd_power) apply auto done
huffman@31706
   342
    also note pab
huffman@31706
   343
    finally have pab': "p dvd a * b".
nipkow@31952
   344
    from prime_dvd_mult_nat[OF p pab']
huffman@31706
   345
    have "p dvd a \<or> p dvd b" .
huffman@31706
   346
    moreover
nipkow@33946
   347
    { assume pa: "p dvd a"
nipkow@31952
   348
      from coprime_common_divisor_nat [OF ab, OF pa] p have "\<not> p dvd b" by auto
huffman@31706
   349
      with p have "coprime b p"
nipkow@31952
   350
        by (subst gcd_commute_nat, intro prime_imp_coprime_nat)
wenzelm@44872
   351
      then have pnb: "coprime (p^n) b"
nipkow@31952
   352
        by (subst gcd_commute_nat, rule coprime_exp_nat)
nipkow@33946
   353
      from coprime_dvd_mult_nat[OF pnb pab] have ?thesis by blast }
huffman@31706
   354
    moreover
nipkow@33946
   355
    { assume pb: "p dvd b"
huffman@31706
   356
      have pnba: "p^n dvd b*a" using pab by (simp add: mult_commute)
nipkow@31952
   357
      from coprime_common_divisor_nat [OF ab, of p] pb p have "\<not> p dvd a"
huffman@31706
   358
        by auto
huffman@31706
   359
      with p have "coprime a p"
nipkow@31952
   360
        by (subst gcd_commute_nat, intro prime_imp_coprime_nat)
wenzelm@44872
   361
      then have pna: "coprime (p^n) a"
nipkow@31952
   362
        by (subst gcd_commute_nat, rule coprime_exp_nat)
nipkow@33946
   363
      from coprime_dvd_mult_nat[OF pna pnba] have ?thesis by blast }
wenzelm@44872
   364
    ultimately have ?thesis by blast }
huffman@31706
   365
  ultimately show ?thesis by blast
nipkow@23983
   366
qed
nipkow@23983
   367
wenzelm@44872
   368
avigad@32036
   369
subsection {* Infinitely many primes *}
avigad@32036
   370
avigad@32036
   371
lemma next_prime_bound: "\<exists>(p::nat). prime p \<and> n < p \<and> p <= fact n + 1"
avigad@32036
   372
proof-
avigad@32036
   373
  have f1: "fact n + 1 \<noteq> 1" using fact_ge_one_nat [of n] by arith 
avigad@32036
   374
  from prime_factor_nat [OF f1]
wenzelm@44872
   375
  obtain p where "prime p" and "p dvd fact n + 1" by auto
wenzelm@44872
   376
  then have "p \<le> fact n + 1" apply (intro dvd_imp_le) apply auto done
wenzelm@44872
   377
  { assume "p \<le> n"
avigad@32036
   378
    from `prime p` have "p \<ge> 1" 
avigad@32036
   379
      by (cases p, simp_all)
avigad@32036
   380
    with `p <= n` have "p dvd fact n" 
avigad@32036
   381
      by (intro dvd_fact_nat)
avigad@32036
   382
    with `p dvd fact n + 1` have "p dvd fact n + 1 - fact n"
avigad@32036
   383
      by (rule dvd_diff_nat)
wenzelm@44872
   384
    then have "p dvd 1" by simp
wenzelm@44872
   385
    then have "p <= 1" by auto
avigad@32036
   386
    moreover from `prime p` have "p > 1" by auto
avigad@32036
   387
    ultimately have False by auto}
wenzelm@44872
   388
  then have "n < p" by presburger
avigad@32036
   389
  with `prime p` and `p <= fact n + 1` show ?thesis by auto
avigad@32036
   390
qed
avigad@32036
   391
avigad@32036
   392
lemma bigger_prime: "\<exists>p. prime p \<and> p > (n::nat)" 
wenzelm@44872
   393
  using next_prime_bound by auto
avigad@32036
   394
avigad@32036
   395
lemma primes_infinite: "\<not> (finite {(p::nat). prime p})"
avigad@32036
   396
proof
avigad@32036
   397
  assume "finite {(p::nat). prime p}"
avigad@32036
   398
  with Max_ge have "(EX b. (ALL x : {(p::nat). prime p}. x <= b))"
avigad@32036
   399
    by auto
avigad@32036
   400
  then obtain b where "ALL (x::nat). prime x \<longrightarrow> x <= b"
avigad@32036
   401
    by auto
wenzelm@44872
   402
  with bigger_prime [of b] show False
wenzelm@44872
   403
    by auto
avigad@32036
   404
qed
avigad@32036
   405
wenzelm@21256
   406
end