From 779720e34503ab203f1019849e1089dfec9cb977 Mon Sep 17 00:00:00 2001 From: Adam Kleber Date: Wed, 16 Jul 2025 00:50:59 -0400 Subject: [PATCH 1/7] added Cubical.Data.Nat.Primes --- Cubical/Data/Nat/Primes/Base.agda | 25 ++ Cubical/Data/Nat/Primes/Concrete.agda | 52 ++++ Cubical/Data/Nat/Primes/DecProps.agda | 104 ++++++++ Cubical/Data/Nat/Primes/Factors.agda | 109 ++++++++ Cubical/Data/Nat/Primes/Imports.agda | 20 ++ Cubical/Data/Nat/Primes/Infinitude.agda | 61 +++++ Cubical/Data/Nat/Primes/Lemmas.agda | 321 ++++++++++++++++++++++++ Cubical/Data/Nat/Primes/Split.agda | 245 ++++++++++++++++++ 8 files changed, 937 insertions(+) create mode 100644 Cubical/Data/Nat/Primes/Base.agda create mode 100644 Cubical/Data/Nat/Primes/Concrete.agda create mode 100644 Cubical/Data/Nat/Primes/DecProps.agda create mode 100644 Cubical/Data/Nat/Primes/Factors.agda create mode 100644 Cubical/Data/Nat/Primes/Imports.agda create mode 100644 Cubical/Data/Nat/Primes/Infinitude.agda create mode 100644 Cubical/Data/Nat/Primes/Lemmas.agda create mode 100644 Cubical/Data/Nat/Primes/Split.agda diff --git a/Cubical/Data/Nat/Primes/Base.agda b/Cubical/Data/Nat/Primes/Base.agda new file mode 100644 index 0000000000..078feb4f0b --- /dev/null +++ b/Cubical/Data/Nat/Primes/Base.agda @@ -0,0 +1,25 @@ +{-# OPTIONS --safe #-} + +module Cubical.Data.Nat.Primes.Base where + +open import Cubical.Data.Nat.Primes.Imports +open import Cubical.Data.Nat.Primes.Lemmas using (1<·1<=3<) + + +record isPrime (n : ℕ) : Type where + constructor prime + field + n-proper : 1 < n + primality : ∀ d → d ∣ n → (d ≡ 1) ⊎ (d ≡ n) + +record isComposite (n : ℕ) : Type where + constructor composite + field + p q : ℕ + p-prime : isPrime p + q-proper : 1 < q + factors : p · q ≡ n + least : ∀ z → 1 < z → z ∣ n → p ≤ z + + 3 Date: Wed, 16 Jul 2025 00:56:27 -0400 Subject: [PATCH 2/7] removed commented-out code --- Cubical/Data/Nat/Primes/Split.agda | 25 ------------------------- 1 file changed, 25 deletions(-) diff --git a/Cubical/Data/Nat/Primes/Split.agda b/Cubical/Data/Nat/Primes/Split.agda index 656a4c7f7f..91adb5722b 100644 --- a/Cubical/Data/Nat/Primes/Split.agda +++ b/Cubical/Data/Nat/Primes/Split.agda @@ -218,28 +218,3 @@ module _ (P : ℕ → Type ℓ) (Pdec : ∀ n → Dec (P n)) where ... | eq x=y = x=y ... | lt x Date: Wed, 16 Jul 2025 15:07:58 -0400 Subject: [PATCH 3/7] removed unused code, made changes according to suggestions --- Cubical/Data/Nat/Primes/Base.agda | 8 ++- Cubical/Data/Nat/Primes/Infinitude.agda | 2 +- Cubical/Data/Nat/Primes/Lemmas.agda | 82 +++++-------------------- Cubical/Data/Nat/Primes/Split.agda | 2 +- 4 files changed, 26 insertions(+), 68 deletions(-) diff --git a/Cubical/Data/Nat/Primes/Base.agda b/Cubical/Data/Nat/Primes/Base.agda index 078feb4f0b..ef9af8b4d4 100644 --- a/Cubical/Data/Nat/Primes/Base.agda +++ b/Cubical/Data/Nat/Primes/Base.agda @@ -2,17 +2,23 @@ module Cubical.Data.Nat.Primes.Base where -open import Cubical.Data.Nat.Primes.Imports +open import Cubical.Foundations.Prelude public +open import Cubical.Data.Nat +open import Cubical.Data.Nat.Order +open import Cubical.Data.Nat.Divisibility +open import Cubical.Data.Sum hiding (elim ; rec ; map) open import Cubical.Data.Nat.Primes.Lemmas using (1<·1<=3<) record isPrime (n : ℕ) : Type where + -- no-eta-equality constructor prime field n-proper : 1 < n primality : ∀ d → d ∣ n → (d ≡ 1) ⊎ (d ≡ n) record isComposite (n : ℕ) : Type where + -- no-eta-equality constructor composite field p q : ℕ diff --git a/Cubical/Data/Nat/Primes/Infinitude.agda b/Cubical/Data/Nat/Primes/Infinitude.agda index 81de7253d4..fec81c543e 100644 --- a/Cubical/Data/Nat/Primes/Infinitude.agda +++ b/Cubical/Data/Nat/Primes/Infinitude.agda @@ -1,4 +1,4 @@ -{-# OPTIONS --safe -W noUnsupportedIndexedMatch #-} +{-# OPTIONS --safe #-} module Cubical.Data.Nat.Primes.Infinitude where diff --git a/Cubical/Data/Nat/Primes/Lemmas.agda b/Cubical/Data/Nat/Primes/Lemmas.agda index cdcdfb9e6a..0fa3b659d8 100644 --- a/Cubical/Data/Nat/Primes/Lemmas.agda +++ b/Cubical/Data/Nat/Primes/Lemmas.agda @@ -12,15 +12,15 @@ private -- some successive implication syntax -step→ : {A : Type ℓ} (B : Type ℓ') → (A → B) → A → B -step→ _ f = f +step⇒ : {A : Type ℓ} (B : Type ℓ') → (A → B) → A → B +step⇒ _ f = f -step→⟨⟩ : (A : Type ℓ) → A → A -step→⟨⟩ A a = a +step⇒⟨⟩ : (A : Type ℓ) → A → A +step⇒⟨⟩ A a = a -syntax step→ B f x = x →⟨ f ⟩ B -syntax step→⟨⟩ A a = a →⟨⟩ A -infixl 2 step→ step→⟨⟩ +syntax step⇒ B f x = x ⇒⟨ f ⟩ B +syntax step⇒⟨⟩ A a = a ⇒⟨⟩ A +infixl -9 step⇒ step⇒⟨⟩ -- auxiliary inequality, multiplication, divisibility lemmas @@ -123,17 +123,12 @@ PropFac< (suc a) (suc (suc b)) zero _ 0 suc (suc (b + a · suc (suc b))) ≡ suc n + ⇒⟨ injSuc ⟩ suc (b + a · suc (suc b)) ≡ n + ⇒⟨ 0 ,_ ⟩ b + a · suc (suc b) < n + ⇒⟨ +a q · n ≡ a + b + ⇒⟨ subst (λ x → q · n ≡ a + x) (sym pn=b) ⟩ q · n ≡ a + p · n + ⇒⟨ (λ x → cong (_∸ (p · n)) x ∙ +∸ a (p · n)) ⟩ q · n ∸ p · n ≡ a + ⇒⟨ (∸-distribʳ q p n) ∙_ ⟩ (q ∸ p) · n ≡ a ) ∣₁ @@ -209,52 +201,12 @@ DecProd : {A : Type ℓ} {P : A → Type ℓ'} {Q : A → Type ℓ''} → (∀ a → Dec (P a)) → (∀ a → Dec (Q a)) → (∀ a → Dec (P a × Q a)) DecProd {P = P} {Q = Q} Pdec Qdec a = DecProd-aux P Q (Pdec a) (Qdec a) -DecToSum : {A : Type ℓ} → (Dec A) → (A ⊎ (¬ A)) -DecToSum (yes a) = inl a -DecToSum (no ¬a) = inr ¬a - -×-≡elim : {A : Type ℓ} {B : Type ℓ'} → {(a , b) (a' , b') : (A × B)} → - ((a , b) ≡ (a' , b')) → (a ≡ a') × (b ≡ b') -×-≡elim p = (λ i → p i .fst) , (λ i → p i .snd) - -DecProd-aux-inj : {A : Type ℓ} (P : A → Type ℓ') (Q : A → Type ℓ'') → ∀ {a} → - (DPa : Dec (P a)) → (DQa : Dec (Q a)) → (Pa : P a) → (Qa : Q a) → - (DecProd-aux P Q DPa DQa ≡ DecProd-aux P Q (yes Pa) (yes Qa)) → - (DPa ≡ yes Pa) × (DQa ≡ yes Qa) -DecProd-aux-inj P Q (yes p₁) (no ¬p) Pa Qa p = ⊥.rec (¬p Qa) -DecProd-aux-inj P Q (no ¬p) (yes p₁) Pa Qa p = ⊥.rec (¬p Pa) -DecProd-aux-inj P Q (no ¬p) (no ¬p₁) Pa Qa p = ⊥.rec (¬p Pa) -DecProd-aux-inj P Q (yes Pa') (yes Qa') Pa Qa y'=y = cong yes (pq .fst) , cong yes (pq .snd) where - inlPQ'=inlPQ : inl (Pa' , Qa') ≡ inl (Pa , Qa) - inlPQ'=inlPQ = cong DecToSum y'=y - Pa'Qa'=PaQa : (Pa' , Qa') ≡ (Pa , Qa) - Pa'Qa'=PaQa = lower (⊎Path.encode (inl (Pa' , Qa')) (inl (Pa , Qa)) inlPQ'=inlPQ) - pq : (Pa' ≡ Pa) × (Qa' ≡ Qa) - pq = ×-≡elim Pa'Qa'=PaQa - -DecProdComp : {A : Type ℓ} {P : A → Type ℓ'} {Q : A → Type ℓ''} → - (Pdec : ∀ a → Dec (P a)) → (Qdec : ∀ a → Dec (Q a)) → (a : A) → - (Pa : P a) → (Qa : Q a) → (DecProd Pdec Qdec) a ≡ yes (Pa , Qa) → - (Pdec a ≡ yes Pa) × (Qdec a ≡ yes Qa) -DecProdComp {P = P} {Q = Q} Pdec Qdec a = DecProd-aux-inj P Q (Pdec a) (Qdec a) - HasFactor : (n k : ℕ) → Type HasFactor n k = (1 < k) × (k ∣ n) DecHF : ∀ {n} k → Dec (HasFactor n k) DecHF = DecProd Dec-1 Date: Wed, 16 Jul 2025 17:44:42 -0400 Subject: [PATCH 4/7] added no-eta-equality to isPrime and isComposite --- Cubical/Data/Nat/Primes/Base.agda | 4 +- Cubical/Data/Nat/Primes/DecProps.agda | 178 ++++++++++++++++++++------ Cubical/Data/Nat/Primes/Factors.agda | 39 +++--- 3 files changed, 166 insertions(+), 55 deletions(-) diff --git a/Cubical/Data/Nat/Primes/Base.agda b/Cubical/Data/Nat/Primes/Base.agda index ef9af8b4d4..895e3324e9 100644 --- a/Cubical/Data/Nat/Primes/Base.agda +++ b/Cubical/Data/Nat/Primes/Base.agda @@ -11,14 +11,14 @@ open import Cubical.Data.Nat.Primes.Lemmas using (1<·1<=3<) record isPrime (n : ℕ) : Type where - -- no-eta-equality + no-eta-equality constructor prime field n-proper : 1 < n primality : ∀ d → d ∣ n → (d ≡ 1) ⊎ (d ≡ n) record isComposite (n : ℕ) : Type where - -- no-eta-equality + no-eta-equality constructor composite field p q : ℕ diff --git a/Cubical/Data/Nat/Primes/DecProps.agda b/Cubical/Data/Nat/Primes/DecProps.agda index 8277198d9c..2ff4938b62 100644 --- a/Cubical/Data/Nat/Primes/DecProps.agda +++ b/Cubical/Data/Nat/Primes/DecProps.agda @@ -8,6 +8,7 @@ open import Cubical.Data.Nat.Primes.Base open import Cubical.Data.Nat.Primes.Factors open import Cubical.Data.Empty as ⊥ hiding (elim) open import Cubical.Data.Sum.Properties +open import Cubical.Reflection.RecordEquiv open isPrime open isComposite @@ -23,50 +24,151 @@ private PropIso : ∀ {A : Type ℓ} {B : Type ℓ'} → isProp A → isProp B → (A → B) → (B → A) → Iso A B PropIso propA propB fun inv = iso fun inv (λ _ → propB _ _) (λ _ → propA _ _) + primeProp : ∀ n → isProp (isPrime n) -primeProp n (prime 1 Date: Wed, 16 Jul 2025 18:03:43 -0400 Subject: [PATCH 5/7] cleaned up awfulness --- Cubical/Data/Nat/Primes/DecProps.agda | 160 ++++++-------------------- 1 file changed, 35 insertions(+), 125 deletions(-) diff --git a/Cubical/Data/Nat/Primes/DecProps.agda b/Cubical/Data/Nat/Primes/DecProps.agda index 2ff4938b62..d957eb1ee9 100644 --- a/Cubical/Data/Nat/Primes/DecProps.agda +++ b/Cubical/Data/Nat/Primes/DecProps.agda @@ -33,132 +33,42 @@ primeProp n np np' i .primality = λ x=1 x=n → ¬n Date: Wed, 16 Jul 2025 18:07:35 -0400 Subject: [PATCH 6/7] aesthetic change --- Cubical/Data/Nat/Primes/Infinitude.agda | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Cubical/Data/Nat/Primes/Infinitude.agda b/Cubical/Data/Nat/Primes/Infinitude.agda index fec81c543e..9b44949737 100644 --- a/Cubical/Data/Nat/Primes/Infinitude.agda +++ b/Cubical/Data/Nat/Primes/Infinitude.agda @@ -47,10 +47,10 @@ nthPrime (suc n) = (next-prime .fst , next-prime .snd .fst .snd , snprimes) wher open Iso ℕ≅primeℕ : Iso ℕ (Σ ℕ isPrime) -fun ℕ≅primeℕ n = (pn .fst , pn .snd .fst) where pn = nthPrime n -inv ℕ≅primeℕ (p , _) = countBelow isPrime DecPrime p -leftInv ℕ≅primeℕ n = nthPrime n .snd .snd -rightInv ℕ≅primeℕ (p , p-prime) = +ℕ≅primeℕ .fun n = (pn .fst , pn .snd .fst) where pn = nthPrime n +ℕ≅primeℕ .inv (p , _) = countBelow isPrime DecPrime p +ℕ≅primeℕ .leftInv n = nthPrime n .snd .snd +ℕ≅primeℕ .rightInv (p , p-prime) = ΣPathP (answer , isProp→PathP (λ i → primeProp (answer i)) pn-prime p-prime) where pn = nthPrime (countBelow isPrime DecPrime p) pn-prime = pn .snd .fst From 0fca3b57425543c3fda53f40497f35b3681144f6 Mon Sep 17 00:00:00 2001 From: Adam Kleber Date: Wed, 16 Jul 2025 22:36:04 -0400 Subject: [PATCH 7/7] renamed Split to Count and moved it out of Primes --- .../Nat/{Primes/Split.agda => Count.agda} | 43 +++++++++++++++++-- Cubical/Data/Nat/Primes/Infinitude.agda | 2 +- Cubical/Data/Nat/Primes/Lemmas.agda | 31 ++++--------- 3 files changed, 48 insertions(+), 28 deletions(-) rename Cubical/Data/Nat/{Primes/Split.agda => Count.agda} (86%) diff --git a/Cubical/Data/Nat/Primes/Split.agda b/Cubical/Data/Nat/Count.agda similarity index 86% rename from Cubical/Data/Nat/Primes/Split.agda rename to Cubical/Data/Nat/Count.agda index acd08a484b..45644c9a39 100644 --- a/Cubical/Data/Nat/Primes/Split.agda +++ b/Cubical/Data/Nat/Count.agda @@ -1,20 +1,55 @@ {-# OPTIONS --safe #-} -module Cubical.Data.Nat.Primes.Split where +module Cubical.Data.Nat.Count where -open import Cubical.Data.Nat.Primes.Imports -open import Cubical.Data.Nat.Primes.Lemmas +open import Cubical.Foundations.Prelude +open import Cubical.Foundations.Function +open import Cubical.Data.Nat +open import Cubical.Data.Nat.Order +open import Cubical.Data.Sigma +open import Cubical.Data.Sum hiding (elim ; rec ; map) +open import Cubical.Data.List hiding (elim ; rec ; map) + +open import Cubical.Relation.Nullary open import Cubical.Data.Empty as ⊥ hiding (elim) private variable - ℓ ℓ' : Level + ℓ ℓ' ℓ'' : Level + + -- some definitions and lemmas decToN : {A : Type ℓ} → Dec A → ℕ decToN (yes _) = 1 decToN (no _) = 0 + data All {A : Type ℓ} (P : A → Type ℓ') : List A → Type (ℓ-max ℓ ℓ') where + [] : All P [] + _∷_ : ∀ {x xs} → P x → All P xs → All P (x ∷ xs) + + mapAll : {A : Type ℓ} {P Q : A → Type ℓ'} {xs : List A} → (∀ {a} → P a → Q a) → All P xs → All Q xs + mapAll f [] = [] + mapAll f (Px ∷ Pxs) = f Px ∷ mapAll f Pxs + + add-equations : ∀ {a} {b} {c} {d} → a ≡ b → c ≡ d → a + c ≡ b + d + add-equations {b = b} {c = c} a=b c=d = cong (_+ c) a=b ∙ cong (b +_) c=d + + <≠ : forall {m} {n} → m < n → ¬ m ≡ n + <≠ {m = m} m