1-- Prime natural numbers2-- Part of Cumulus, the Grace17 mathematics library.3-- Copyright (c) 2026 Grant Passmore.4-- Released under the Apache License 2.0. See LICENSE in the repository root.5-- Authors: Grant Passmore67module Grace.Cumulus.NumberTheory.Prime8import Divisibility9import Set.Ordinal1011-- For von Neumann naturals, `1 in p` is literally the strict inequality 1 < p.12-- A prime has no proper natural divisor other than one.13-- The soft-typed binder is part of the predicate's meaning: Prime(p) entails14-- Nat(p), including when Prime is used inside Separation over omega.15predicate Prime (p : Nat) :=161∈ p ∧17 (∀ d, Nat(d) → d ∈ p → d ∣ p → d =1)1819-- The primes form a set because they are separated from omega.20def primes : Set:= { p ∈ ℕ | Prime(p) }2122theorem Prime.natural (p : Set)23requires hp : Prime(p)24showsNat(p)25proof26simp [Prime]2728lemma Prime.greater_than_one_membership (p : Set)29requires hp : Prime(p)30shows1∈ p31proof32simp [Prime]3334-- Representation bridge used internally by factorial arguments: a prime is35-- positive as a von Neumann natural. The public comparison theorem remains36-- `Prime.greater_than_one` below.37lemma Prime.positive_membership (p : Set)38requires hp : Prime(p)39shows0∈ p40proof41have hpNat := Prime.natural with hp42have hpOrdinal :=Ordinal.natural with hpNat43have transitive :=Ordinal.transitive with hpOrdinal44have oneSubset := transitive at145have oneMember := Prime.greater_than_one_membership with hp46specialize oneSubset with oneMember47have zeroMember := oneSubset at048have zeroInOne := membership.self_successor with Naturals.constructor.zero49exact zeroMember with zeroInOne5051theorem Prime.greater_than_one (p : Set)52requires hp : Prime(p)53shows1 < p54proof55have member := Prime.greater_than_one_membership with hp56exact Less.of_membership with member5758theorem Prime.divisor (p d : Set)59requires hp : Prime(p)60requires hd : Nat(d)61requires hproper : d ∈ p62requires hdivides : d ∣ p63shows d =164proof65rcases hp with⟨hpAbove, hpDivisors⟩66exact hpDivisors with d, hd, hproper, hdivides6768theorem primes.contains (p : Set)69requires hp : Prime(p)70shows p ∈ primes71proof72apply primes.membership.backward73constructor74 . have natural := Prime.natural with hp75simp [nat.omega]76 . exact hp7778-- Foundation induction on the von Neumann membership order is strong79-- induction on naturals. A hypothetical number with no prime divisor would80-- itself be prime: every proper divisor above one already has a smaller prime81-- divisor, contradicting the hypothesis.82lemma natural.exists_prime_divisor_membership (m : Set)83showsNat(m) →1∈ m →∃ p, Prime(p) ∧ p ∣ m84proof85induction m using membership86intros current ih hCurrentNat hAboveOne87by_contra hNoPrimeDivisor88apply hNoPrimeDivisor89witness current90constructor91constructor92 . exact hAboveOne93 . intros d hdNat hdProper hdDivides94havecases:= natural.zero_one_or_above_one with hdNat95casescaseswith hdZero | hdRest96 . exfalso97rw hdZero at hdDivides98have currentZero := zero.divides_only_zero with hCurrentNat, hdDivides99rw currentZero at hAboveOne100simp101 . cases hdRest with hdOne | hdAboveOne102 . exact hdOne103 . have smaller := ih at d with hdProper, hdNat, hdAboveOne104obtain⟨p, hpPrime, hpDividesD⟩:= smaller105exfalso106apply hNoPrimeDivisor107witness p108constructor109 . exact hpPrime110 . have hpNat := Prime.natural with hpPrime111exact divides.transitive with hdNat, hCurrentNat, hpDividesD, hdDivides112 . exact divides.reflexive with hCurrentNat113114-- Public Nat-order form. The membership-induction lemma above is the115-- representation bridge; elementary number theory uses this interface.116theorem natural.exists_prime_divisor (m : Nat)117requires hAboveOne : 1 < m118shows∃ p, Prime(p) ∧ p ∣ m119proof120apply natural.exists_prime_divisor_membership121 . assumption122 . apply membership.of_Less at1, m123 . exact hAboveOne124125lemma prime_divisor.factorial_successor_not_bounded (p n : Set)126requires hnNat : Nat(n)127requires hp : Prime(p)128requires hDivides : p ∣ succ(n!)129requires hpBound : p ∈ succ(n)130shows false131proof132have hpPositive := Prime.positive_membership with hp133have dividesFactorial := factorial.divisible_through with hnNat, hpPositive, hpBound134have common := divides.consecutive_one with dividesFactorial, hDivides135have aboveOne := Prime.greater_than_one_membership with hp136rw common at aboveOne137have oneMember := membership.one.forward with aboveOne138arith139140lemma prime_divisor.factorial_successor_above_membership (p n : Set)141requires hnNat : Nat(n)142requires hp : Prime(p)143requires hDivides : p ∣ succ(n!)144shows n ∈ p145proof146have hpNat := Prime.natural with hp147have hnOrdinal :=Ordinal.natural with hnNat148have hpOrdinal :=Ordinal.natural with hpNat149have comparison :=Ordinal.trichotomy with hnOrdinal, p, hpOrdinal150cases comparison with hnInP | hRest151 . exact hnInP152 . exfalso153apply prime_divisor.factorial_successor_not_bounded154 . exact hnNat155 . exact hp156 . exact hDivides157 . cases hRest with hnEqP | hpInN158 . apply membership.successor_backward{n := n} at p159right160simp161 . apply membership.successor_backward{n := n} at p162left163exact hpInN164165lemma factorial.successor_above_one_membership (n : Set)166requires hnNat : Nat(n)167shows1∈ succ(n!)168proof169have factorialNatural := factorial.closed with hnNat170have factorialCases := natural.zero_one_or_above_one with factorialNatural171cases factorialCases with hZero | hRest172 . exfalso173have nonzero := factorial.nonzero with hnNat174apply nonzero175exact hZero176 . cases hRest with hOne | hAbove177 . apply membership.successor_backward{n := n!} at1178right179simp180 . apply membership.successor_backward{n := n!} at1181left182exact hAbove183184theorem factorial.successor_greater_than_one (n : Nat)185shows1 < succ(n!)186proof187apply Less.of_membership188 . apply factorial.successor_above_one_membership at n189190-- Public Nat-level form of the factorial argument. Its implementation bridge191-- to von Neumann membership is confined to this proof.192theorem prime_divisor.factorial_successor_greater (p n : Nat)193requires hp : Prime(p)194requires hDivides : p ∣ succ(n!)195shows n < p196proof197apply Less.of_membership198 . apply prime_divisor.factorial_successor_above_membership at p, n199 . exact hp200 . exact hDivides201202-- The Euclid number has a prime divisor. This is the natural arithmetic203-- witness lemma; its proof alone crosses to the von Neumann representation to204-- use the foundational prime-divisor existence result.205lemma factorial_successor.exists_prime_divisor (n : Nat)206shows∃ p, Prime(p) ∧ p ∣ succ(n!)207proof208have aboveOne := factorial.successor_greater_than_one at n209exact natural.exists_prime_divisor with aboveOne210211-- Euclid's theorem as elementary number theory: the visible statement and212-- proof mention only naturals, primes, divisibility, factorial, and order.213theorem Euclid.larger_prime (n : Nat)214shows∃ p, Prime(p) ∧ n < p215proof216obtain⟨p, hpPrime, hpDivides⟩:= factorial_successor.exists_prime_divisor at n withassumption217witness p218constructor219 . exact hpPrime220 . apply prime_divisor.factorial_successor_greater at p, n221 . exact hpPrime222 . exact hpDivides223224theorem primes.unbounded (n : Nat)225shows∃ p, Prime(p) ∧ n < p226proof227apply Euclid.larger_prime228