------------------------------------------------------------------------ -- The Agda standard library -- -- Primality ------------------------------------------------------------------------ {-# OPTIONS --cubical-compatible --safe #-} module Data.Nat.Primality where open import Data.List.Base using ([]; _∷_) open import Data.List.Relation.Unary.All as All using (All; []; _∷_) open import Data.Nat.Base open import Data.Nat.Divisibility open import Data.Nat.GCD using (module GCD; module Bézout) open import Data.Nat.ListAction using (product) open import Data.Nat.ListAction.Properties using (product≢0) open import Data.Nat.Properties open import Data.Product.Base using (∃-syntax; _×_; map₂; _,_) open import Data.Sum.Base using (_⊎_; inj₁; inj₂; [_,_]′) open import Function.Base using (flip; _∘_; _∘′_) open import Function.Bundles using (_⇔_; mk⇔) open import Relation.Nullary.Decidable as Dec using (yes; no; from-yes; from-no; ¬?; _×-dec_; _⊎-dec_; _→-dec_; decidable-stable) open import Relation.Nullary.Negation.Core using (¬_; contradiction; contradiction₂) open import Relation.Unary using (Pred; Decidable) open import Relation.Binary.Core using (Rel) open import Relation.Binary.PropositionalEquality.Core using (_≡_; _≢_; refl; cong) private variable d m n o p : ℕ recompute-nonTrivial : .{{NonTrivial n}} → NonTrivial n recompute-nonTrivial {n} {{nontrivial}} = Dec.recompute (nonTrivial? n) nontrivial ------------------------------------------------------------------------ -- Definitions ------------------------------------------------------------------------ -- The positive/existential relation `BoundedNonTrivialDivisor` is -- the basis for the whole development, as it captures the possible -- non-trivial divisors of a given number; its complement, `Rough`, -- therefore sets *lower* bounds on any possible such divisors. -- The predicate `Composite` is then defined as the 'diagonal' instance -- of `BoundedNonTrivialDivisor`, while `Prime` is essentially defined as -- the complement of `Composite`. Finally, `Irreducible` is the positive -- analogue of `Prime`. ------------------------------------------------------------------------ -- Roughness -- A number is m-rough if all its non-trivial divisors are bounded below -- by m. infix 10 _Rough_ _Rough_ : ℕ → Pred ℕ _ m Rough n = ¬ (n HasNonTrivialDivisorLessThan m) ------------------------------------------------------------------------ -- Compositeness -- A number is composite if it has a proper non-trivial divisor. Composite : Pred ℕ _ Composite n = n HasNonTrivialDivisorLessThan n -- A shorter pattern synonym for the record constructor producing a -- witness for `Composite`. pattern composite {d} d 1 is m-rough, then m ≤ n rough⇒≤ : .{{NonTrivial n}} → m Rough n → m ≤ n rough⇒≤ rough = ≮⇒≥ n≮m where n≮m = λ m>n → rough (hasNonTrivialDivisor m>n ∣-refl) -- If a number n is m-rough, and m ∤ n, then n is (suc m)-rough ∤⇒rough-suc : m ∤ n → m Rough n → (suc m) Rough n ∤⇒rough-suc m∤n r (hasNonTrivialDivisor d<1+m d∣n) with m<1+n⇒m1⇒m*n>1 q d _ = m*n≢0⇒m≢0 q -- Basic (counter-)examples of Composite ¬composite[0] : ¬ Composite 0 ¬composite[0] = 0-rough ¬composite[1] : ¬ Composite 1 ¬composite[1] = 1-rough composite[4] : Composite 4 composite[4] = composite-≢ 2 (λ()) (divides-refl 2) composite[6] : Composite 6 composite[6] = composite-≢ 3 (λ()) (divides-refl 2) composite⇒nonZero : Composite n → NonZero n composite⇒nonZero {suc _} _ = _ composite⇒nonTrivial : Composite n → NonTrivial n composite⇒nonTrivial {1} composite[1] = contradiction composite[1] ¬composite[1] composite⇒nonTrivial {2+ _} _ = _ composite? : Decidable Composite composite? n = Dec.map CompositeUpTo⇔Composite (compositeUpTo? n) where -- For technical reasons, in order to be able to prove decidability -- via the `all?` and `any?` combinators for *bounded* predicates on -- `ℕ`, we further define the bounded counterparts to predicates -- `P...` as `P...UpTo` and show the equivalence of the two. -- Equivalent bounded predicate definition CompositeUpTo : Pred ℕ _ CompositeUpTo n = ∃[ d ] d < n × NonTrivial d × d ∣ n -- Proof of equivalence comp-upto⇒comp : CompositeUpTo n → Composite n comp-upto⇒comp (_ , d 1 divides n, then p must be prime rough∧∣⇒prime : .{{NonTrivial p}} → p Rough n → p ∣ n → Prime p rough∧∣⇒prime r p∣n = prime (rough∧∣⇒rough r p∣n) -- If a number n is m-rough, and m * m > n, then n must be prime. rough∧square>⇒prime : .{{NonTrivial n}} → m Rough n → m * m > n → Prime n rough∧square>⇒prime rough m*m>n = prime ¬composite where ¬composite : ¬ Composite _ ¬composite (composite dn (*-mono-≤ (rough⇒≤ (rough∧∣⇒rough rough (quotient-∣ d∣n))) (rough⇒≤ (rough∧∣⇒rough rough d∣n))))) where instance _ = n>1⇒nonTrivial (quotient>1 d∣n d-nonZero⁻¹ _ {{productOfPrimes≢0 pas}} ------------------------------------------------------------------------ -- Basic (counter-)examples of Irreducible ¬irreducible[0] : ¬ Irreducible 0 ¬irreducible[0] irr[0] = contradiction₂ 2≡1⊎2≡0 (λ ()) (λ ()) where 2≡1⊎2≡0 = irr[0] {2} (divides-refl 0) irreducible[1] : Irreducible 1 irreducible[1] m∣1 = inj₁ (∣1⇒≡1 m∣1) irreducible[2] : Irreducible 2 irreducible[2] {zero} 0∣2 with () ← 0∣⇒≡0 0∣2 irreducible[2] {suc _} d∣2 with ∣⇒≤ d∣2 ... | z