Skip to content

Commit eba9c37

Browse files
committed
chore(Data/Nat/Prime): split Defs; rearrange Basic (#18233)
This PR reduces the theory included with `Data/Nat/Prime/Defs.lean`, in particular allowing us to define `CharP` without needing theory on ordered monoids. Most of the contents got moved to `Basic.lean`, which would increase the downstream imports for quite a few file. So in turn I split up `Basic.lean` based on the imports and logical structure: * Results related to the factorial go to `Prime/Factorial.lean` * Results related to integers go to `Prime/Int.lean` (which should probably be merged with `Data/Int/NatPrime.lean) * The proof that there are infinitely many primes goes to `Prime/Infinite.lean` * Some results on powers of prime that needed a higher amount of ordered monoid theory go to `Prime/Pow.lean` This is not a universal import win, since some files that imported `Defs.lean` now need to import `Basic.lean` instead, but I think that's worth it for keeping `Defs` files succinct.
1 parent 2a1cffe commit eba9c37

File tree

22 files changed

+206
-115
lines changed

22 files changed

+206
-115
lines changed

Archive/Imo/Imo2019Q4.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Authors: Floris van Doorn
55
-/
66
import Mathlib.Data.Nat.Factorial.BigOperators
77
import Mathlib.Data.Nat.Multiplicity
8-
import Mathlib.Data.Nat.Prime.Basic
8+
import Mathlib.Data.Nat.Prime.Int
99
import Mathlib.Tactic.IntervalCases
1010
import Mathlib.Tactic.GCongr
1111

Mathlib.lean

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2544,6 +2544,10 @@ import Mathlib.Data.Nat.PartENat
25442544
import Mathlib.Data.Nat.Periodic
25452545
import Mathlib.Data.Nat.Prime.Basic
25462546
import Mathlib.Data.Nat.Prime.Defs
2547+
import Mathlib.Data.Nat.Prime.Factorial
2548+
import Mathlib.Data.Nat.Prime.Infinite
2549+
import Mathlib.Data.Nat.Prime.Int
2550+
import Mathlib.Data.Nat.Prime.Pow
25472551
import Mathlib.Data.Nat.PrimeFin
25482552
import Mathlib.Data.Nat.Set
25492553
import Mathlib.Data.Nat.Size

Mathlib/Algebra/Order/Floor/Prime.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Copyright (c) 2022 Yuyang Zhao. All rights reserved.
33
Released under Apache 2.0 license as described in the file LICENSE.
44
Authors: Yuyang Zhao
55
-/
6-
import Mathlib.Data.Nat.Prime.Basic
6+
import Mathlib.Data.Nat.Prime.Infinite
77
import Mathlib.Topology.Algebra.Order.Floor
88

99
/-!

Mathlib/Data/Int/NatPrime.lean

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Copyright (c) 2020 Bryan Gin-ge Chen. All rights reserved.
33
Released under Apache 2.0 license as described in the file LICENSE.
44
Authors: Kevin Lacker, Bryan Gin-ge Chen
55
-/
6+
import Mathlib.Algebra.Group.Int
67
import Mathlib.Data.Nat.Prime.Basic
78

89
/-!

Mathlib/Data/Nat/Choose/Dvd.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Released under Apache 2.0 license as described in the file LICENSE.
44
Authors: Chris Hughes, Patrick Stevens
55
-/
66
import Mathlib.Data.Nat.Choose.Basic
7-
import Mathlib.Data.Nat.Prime.Basic
7+
import Mathlib.Data.Nat.Prime.Factorial
88

99
/-!
1010
# Divisibility properties of binomial coefficients

Mathlib/Data/Nat/Factorization/PrimePow.lean

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Authors: Bhavik Mehta
55
-/
66
import Mathlib.Algebra.IsPrimePow
77
import Mathlib.Data.Nat.Factorization.Basic
8+
import Mathlib.Data.Nat.Prime.Pow
89

910
/-!
1011
# Prime powers and factorizations

Mathlib/Data/Nat/Factors.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Authors: Leonardo de Moura, Jeremy Avigad, Mario Carneiro
55
-/
66
import Mathlib.Algebra.BigOperators.Ring.List
77
import Mathlib.Data.Nat.GCD.Basic
8-
import Mathlib.Data.Nat.Prime.Defs
8+
import Mathlib.Data.Nat.Prime.Basic
99
import Mathlib.Data.List.Prime
1010
import Mathlib.Data.List.Sort
1111
import Mathlib.Data.List.Perm.Subperm

Mathlib/Data/Nat/Prime/Basic.lean

Lines changed: 27 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,47 @@ Released under Apache 2.0 license as described in the file LICENSE.
44
Authors: Leonardo de Moura, Jeremy Avigad, Mario Carneiro
55
-/
66
import Mathlib.Algebra.Associated.Basic
7-
import Mathlib.Algebra.Order.Monoid.Unbundled.Pow
8-
import Mathlib.Algebra.Ring.Int
9-
import Mathlib.Data.Nat.Factorial.Basic
7+
import Mathlib.Algebra.Ring.Parity
108
import Mathlib.Data.Nat.Prime.Defs
11-
import Mathlib.Order.Bounds.Basic
129

1310
/-!
14-
## Notable Theorems
11+
# Prime numbers
1512
16-
- `Nat.exists_infinite_primes`: Euclid's theorem that there exist infinitely many prime numbers.
17-
This also appears as `Nat.not_bddAbove_setOf_prime` and `Nat.infinite_setOf_prime` (the latter
18-
in `Data.Nat.PrimeFin`).
13+
This file develops the theory of prime numbers: natural numbers `p ≥ 2` whose only divisors are
14+
`p` and `1`.
1915
2016
-/
2117

22-
2318
open Bool Subtype
2419

2520
open Nat
2621

2722
namespace Nat
2823
variable {n : ℕ}
2924

25+
theorem prime_mul_iff {a b : ℕ} : Nat.Prime (a * b) ↔ a.Prime ∧ b = 1 ∨ b.Prime ∧ a = 1 := by
26+
simp only [irreducible_mul_iff, ← irreducible_iff_nat_prime, Nat.isUnit_iff]
27+
28+
theorem not_prime_mul {a b : ℕ} (a1 : a ≠ 1) (b1 : b ≠ 1) : ¬Prime (a * b) := by
29+
simp [prime_mul_iff, _root_.not_or, *]
30+
31+
theorem not_prime_mul' {a b n : ℕ} (h : a * b = n) (h₁ : a ≠ 1) (h₂ : b ≠ 1) : ¬Prime n :=
32+
h ▸ not_prime_mul h₁ h₂
33+
34+
theorem Prime.dvd_iff_eq {p a : ℕ} (hp : p.Prime) (a1 : a ≠ 1) : a ∣ p ↔ p = a := by
35+
refine ⟨?_, by rintro rfl; rfl⟩
36+
rintro ⟨j, rfl⟩
37+
rcases prime_mul_iff.mp hp with (⟨_, rfl⟩ | ⟨_, rfl⟩)
38+
· exact mul_one _
39+
· exact (a1 rfl).elim
40+
41+
theorem Prime.eq_two_or_odd {p : ℕ} (hp : Prime p) : p = 2 ∨ p % 2 = 1 :=
42+
p.mod_two_eq_zero_or_one.imp_left fun h =>
43+
((hp.eq_one_or_self_of_dvd 2 (dvd_of_mod_eq_zero h)).resolve_left (by decide)).symm
44+
45+
theorem Prime.eq_two_or_odd' {p : ℕ} (hp : Prime p) : p = 2 ∨ Odd p :=
46+
Or.imp_right (fun h => ⟨p / 2, (div_add_mod p 2).symm.trans (congr_arg _ h)⟩) hp.eq_two_or_odd
47+
3048
section
3149

3250
theorem Prime.five_le_of_ne_two_of_ne_three {p : ℕ} (hp : p.Prime) (h_two : p ≠ 2)
@@ -78,26 +96,6 @@ theorem dvd_of_forall_prime_mul_dvd {a b : ℕ}
7896
obtain ⟨p, hp⟩ := exists_prime_and_dvd ha
7997
exact _root_.trans (dvd_mul_left a p) (hdvd p hp.1 hp.2)
8098

81-
/-- Euclid's theorem on the **infinitude of primes**.
82-
Here given in the form: for every `n`, there exists a prime number `p ≥ n`. -/
83-
theorem exists_infinite_primes (n : ℕ) : ∃ p, n ≤ p ∧ Prime p :=
84-
let p := minFac (n ! + 1)
85-
have f1 : n ! + 11 := ne_of_gt <| succ_lt_succ <| factorial_pos _
86-
have pp : Prime p := minFac_prime f1
87-
have np : n ≤ p :=
88-
le_of_not_ge fun h =>
89-
have h₁ : p ∣ n ! := dvd_factorial (minFac_pos _) h
90-
have h₂ : p ∣ 1 := (Nat.dvd_add_iff_right h₁).2 (minFac_dvd _)
91-
pp.not_dvd_one h₂
92-
⟨p, np, pp⟩
93-
94-
/-- A version of `Nat.exists_infinite_primes` using the `BddAbove` predicate. -/
95-
theorem not_bddAbove_setOf_prime : ¬BddAbove { p | Prime p } := by
96-
rw [not_bddAbove_iff]
97-
intro n
98-
obtain ⟨p, hi, hp⟩ := exists_infinite_primes n.succ
99-
exact ⟨p, hp, hi⟩
100-
10199
theorem Prime.even_iff {p : ℕ} (hp : Prime p) : Even p ↔ p = 2 := by
102100
rw [even_iff_two_dvd, prime_dvd_prime_iff_eq prime_two hp, eq_comm]
103101

@@ -159,18 +157,6 @@ theorem Prime.pow_eq_iff {p a k : ℕ} (hp : p.Prime) : a ^ k = p ↔ a = p ∧
159157
rw [← h] at hp
160158
rw [← h, hp.eq_one_of_pow, eq_self_iff_true, _root_.and_true, pow_one]
161159

162-
theorem pow_minFac {n k : ℕ} (hk : k ≠ 0) : (n ^ k).minFac = n.minFac := by
163-
rcases eq_or_ne n 1 with (rfl | hn)
164-
· simp
165-
have hnk : n ^ k ≠ 1 := fun hk' => hn ((pow_eq_one_iff hk).1 hk')
166-
apply (minFac_le_of_dvd (minFac_prime hn).two_le ((minFac_dvd n).pow hk)).antisymm
167-
apply
168-
minFac_le_of_dvd (minFac_prime hnk).two_le
169-
((minFac_prime hnk).dvd_of_dvd_pow (minFac_dvd _))
170-
171-
theorem Prime.pow_minFac {p k : ℕ} (hp : p.Prime) (hk : k ≠ 0) : (p ^ k).minFac = p := by
172-
rw [Nat.pow_minFac hk, hp.minFac_eq]
173-
174160
theorem Prime.mul_eq_prime_sq_iff {x y p : ℕ} (hp : p.Prime) (hx : x ≠ 1) (hy : y ≠ 1) :
175161
x * y = p ^ 2 ↔ x = p ∧ y = p := by
176162
refine ⟨fun h => ?_, fun ⟨h₁, h₂⟩ => h₁.symm ▸ h₂.symm ▸ (sq _).symm⟩
@@ -197,14 +183,6 @@ theorem Prime.mul_eq_prime_sq_iff {x y p : ℕ} (hp : p.Prime) (hx : x ≠ 1) (h
197183
rw [sq, Nat.mul_right_eq_self_iff (Nat.mul_pos hp.pos hp.pos : 0 < a * a)] at h
198184
exact h
199185

200-
theorem Prime.dvd_factorial : ∀ {n p : ℕ} (_ : Prime p), p ∣ n ! ↔ p ≤ n
201-
| 0, _, hp => iff_of_false hp.not_dvd_one (not_le_of_lt hp.pos)
202-
| n + 1, p, hp => by
203-
rw [factorial_succ, hp.dvd_mul, Prime.dvd_factorial hp]
204-
exact
205-
fun h => h.elim (le_of_dvd (succ_pos _)) le_succ_of_le, fun h =>
206-
(_root_.lt_or_eq_of_le h).elim (Or.inr ∘ le_of_lt_succ) fun h => Or.inl <| by rw [h]⟩
207-
208186
theorem Prime.coprime_pow_of_not_dvd {p m a : ℕ} (pp : Prime p) (h : ¬p ∣ a) : Coprime a (p ^ m) :=
209187
(pp.coprime_iff_not_dvd.2 h).symm.pow_right _
210188

@@ -269,33 +247,4 @@ theorem succ_dvd_or_succ_dvd_of_succ_sum_dvd_mul {p : ℕ} (p_prime : Prime p) {
269247
exact hpd5.elim (fun h : p ∣ m / p ^ k => Or.inl <| mul_dvd_of_dvd_div hpm h)
270248
fun h : p ∣ n / p ^ l => Or.inr <| mul_dvd_of_dvd_div hpn h
271249

272-
theorem prime_iff_prime_int {p : ℕ} : p.Prime ↔ _root_.Prime (p : ℤ) :=
273-
fun hp =>
274-
⟨Int.natCast_ne_zero_iff_pos.2 hp.pos, mt Int.isUnit_iff_natAbs_eq.1 hp.ne_one, fun a b h => by
275-
rw [← Int.dvd_natAbs, Int.natCast_dvd_natCast, Int.natAbs_mul, hp.dvd_mul] at h
276-
rwa [← Int.dvd_natAbs, Int.natCast_dvd_natCast, ← Int.dvd_natAbs, Int.natCast_dvd_natCast]⟩,
277-
fun hp =>
278-
Nat.prime_iff.2
279-
⟨Int.natCast_ne_zero.1 hp.1,
280-
(mt Nat.isUnit_iff.1) fun h => by simp [h, not_prime_one] at hp, fun a b => by
281-
simpa only [Int.natCast_dvd_natCast, (Int.ofNat_mul _ _).symm] using hp.2.2 a b⟩⟩
282-
283-
/-- Two prime powers with positive exponents are equal only when the primes and the
284-
exponents are equal. -/
285-
lemma Prime.pow_inj {p q m n : ℕ} (hp : p.Prime) (hq : q.Prime)
286-
(h : p ^ (m + 1) = q ^ (n + 1)) : p = q ∧ m = n := by
287-
have H := dvd_antisymm (Prime.dvd_of_dvd_pow hp <| h ▸ dvd_pow_self p (succ_ne_zero m))
288-
(Prime.dvd_of_dvd_pow hq <| h.symm ▸ dvd_pow_self q (succ_ne_zero n))
289-
exact ⟨H, succ_inj'.mp <| Nat.pow_right_injective hq.two_le (H ▸ h)⟩
290-
291250
end Nat
292-
293-
namespace Int
294-
295-
theorem prime_two : Prime (2 : ℤ) :=
296-
Nat.prime_iff_prime_int.mp Nat.prime_two
297-
298-
theorem prime_three : Prime (3 : ℤ) :=
299-
Nat.prime_iff_prime_int.mp Nat.prime_three
300-
301-
end Int

Mathlib/Data/Nat/Prime/Defs.lean

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ Released under Apache 2.0 license as described in the file LICENSE.
44
Authors: Leonardo de Moura, Jeremy Avigad, Mario Carneiro
55
-/
66
import Batteries.Data.Nat.Gcd
7-
import Mathlib.Algebra.Prime.Lemmas
8-
import Mathlib.Algebra.Ring.Parity
7+
import Mathlib.Algebra.Prime.Defs
8+
import Mathlib.Algebra.Ring.Nat
9+
import Mathlib.Order.Lattice
910

1011
/-!
1112
# Prime numbers
@@ -161,29 +162,6 @@ theorem prime_dvd_prime_iff_eq {p q : ℕ} (pp : p.Prime) (qp : q.Prime) : p ∣
161162
theorem Prime.not_dvd_one {p : ℕ} (pp : Prime p) : ¬p ∣ 1 :=
162163
Irreducible.not_dvd_one pp
163164

164-
theorem prime_mul_iff {a b : ℕ} : Nat.Prime (a * b) ↔ a.Prime ∧ b = 1 ∨ b.Prime ∧ a = 1 := by
165-
simp only [irreducible_mul_iff, ← irreducible_iff_nat_prime, Nat.isUnit_iff]
166-
167-
theorem not_prime_mul {a b : ℕ} (a1 : a ≠ 1) (b1 : b ≠ 1) : ¬Prime (a * b) := by
168-
simp [prime_mul_iff, _root_.not_or, *]
169-
170-
theorem not_prime_mul' {a b n : ℕ} (h : a * b = n) (h₁ : a ≠ 1) (h₂ : b ≠ 1) : ¬Prime n :=
171-
h ▸ not_prime_mul h₁ h₂
172-
173-
theorem Prime.dvd_iff_eq {p a : ℕ} (hp : p.Prime) (a1 : a ≠ 1) : a ∣ p ↔ p = a := by
174-
refine ⟨?_, by rintro rfl; rfl⟩
175-
rintro ⟨j, rfl⟩
176-
rcases prime_mul_iff.mp hp with (⟨_, rfl⟩ | ⟨_, rfl⟩)
177-
· exact mul_one _
178-
· exact (a1 rfl).elim
179-
180-
theorem Prime.eq_two_or_odd {p : ℕ} (hp : Prime p) : p = 2 ∨ p % 2 = 1 :=
181-
p.mod_two_eq_zero_or_one.imp_left fun h =>
182-
((hp.eq_one_or_self_of_dvd 2 (dvd_of_mod_eq_zero h)).resolve_left (by decide)).symm
183-
184-
theorem Prime.eq_two_or_odd' {p : ℕ} (hp : Prime p) : p = 2 ∨ Odd p :=
185-
Or.imp_right (fun h => ⟨p / 2, (div_add_mod p 2).symm.trans (congr_arg _ h)⟩) hp.eq_two_or_odd
186-
187165
section MinFac
188166

189167
theorem minFac_lemma (n k : ℕ) (h : ¬n < k * k) : sqrt n - k < sqrt n + 2 - k :=
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/-
2+
Copyright (c) 2015 Microsoft Corporation. All rights reserved.
3+
Released under Apache 2.0 license as described in the file LICENSE.
4+
Authors: Leonardo de Moura, Jeremy Avigad, Mario Carneiro
5+
-/
6+
import Mathlib.Data.Nat.Factorial.Basic
7+
import Mathlib.Data.Nat.Prime.Defs
8+
9+
/-!
10+
# Prime natural numbers and the factorial operator
11+
12+
-/
13+
14+
open Bool Subtype
15+
16+
open Nat
17+
18+
namespace Nat
19+
20+
theorem Prime.dvd_factorial : ∀ {n p : ℕ} (_ : Prime p), p ∣ n ! ↔ p ≤ n
21+
| 0, _, hp => iff_of_false hp.not_dvd_one (not_le_of_lt hp.pos)
22+
| n + 1, p, hp => by
23+
rw [factorial_succ, hp.dvd_mul, Prime.dvd_factorial hp]
24+
exact
25+
fun h => h.elim (le_of_dvd (succ_pos _)) le_succ_of_le, fun h =>
26+
(_root_.lt_or_eq_of_le h).elim (Or.inr ∘ le_of_lt_succ) fun h => Or.inl <| by rw [h]⟩
27+
28+
end Nat

0 commit comments

Comments
 (0)