-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* triangular numbers * add instance for nat * export naturals * add forgetter * update * export more, to make the use of Nat as a CSR more natural * update references, cosmetics * organize imports * Cleanup * cleanup, remove reexport of Nat, suc, zero * qualify import * description
- Loading branch information
1 parent
86b5233
commit 1c0396c
Showing
3 changed files
with
127 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{-# OPTIONS --safe #-} | ||
module Cubical.Algebra.CommSemiring.Instances.Nat where | ||
|
||
open import Cubical.Foundations.Prelude | ||
open import Cubical.Foundations.HLevels | ||
|
||
import Cubical.Data.Nat as Nat | ||
open import Cubical.Data.Nat using (ℕ; suc; zero) | ||
|
||
open import Cubical.Algebra.CommSemiring | ||
|
||
ℕasCSR : CommSemiring ℓ-zero | ||
ℕasCSR .fst = ℕ | ||
ℕasCSR .snd = str | ||
where | ||
open CommSemiringStr | ||
|
||
str : CommSemiringStr ℕ | ||
0r str = zero | ||
1r str = suc zero | ||
_+_ str = Nat._+_ | ||
_·_ str = Nat._·_ | ||
isCommSemiring str = | ||
makeIsCommSemiring | ||
Nat.isSetℕ | ||
Nat.+-assoc Nat.+-zero Nat.+-comm | ||
Nat.·-assoc Nat.·-identityʳ | ||
(λ x y z → sym (Nat.·-distribˡ x y z)) | ||
(λ _ → refl) | ||
Nat.·-comm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
{-# OPTIONS --safe #-} | ||
{- | ||
In this module, the commonly known identity between the sum of the first (n+1) natural | ||
numbers (also known as the n-th triangular number) and the product ½ · n · (n+1) is | ||
proven in the equivalent form: | ||
2 · (∑ (first (suc n))) ≡ n · (n + 1) | ||
-} | ||
module Cubical.Data.Nat.Triangular where | ||
|
||
open import Cubical.Foundations.Prelude | ||
open import Cubical.Foundations.Function | ||
|
||
open import Cubical.Data.FinData as Fin | ||
open import Cubical.Data.Nat using (ℕ; suc; zero) | ||
|
||
open import Cubical.Algebra.CommSemiring | ||
open import Cubical.Algebra.CommSemiring.Instances.Nat | ||
open import Cubical.Algebra.Semiring.BigOps | ||
|
||
open import Cubical.Tactics.NatSolver.Reflection | ||
|
||
open Sum (CommSemiring→Semiring ℕasCSR) | ||
open CommSemiringStr (snd ℕasCSR) | ||
|
||
-- the first n natural number, i.e. {0,1,...,n-1} | ||
first : (n : ℕ) → FinVec ℕ n | ||
first n i = toℕ i | ||
|
||
firstDecompose : (n : ℕ) → first (suc n) ∘ weakenFin ≡ first n | ||
firstDecompose n i l = | ||
Fin.elim | ||
(λ l → first (suc _) (weakenFin l) ≡ first _ l) | ||
refl | ||
(λ _ → weakenRespToℕ _) | ||
l i | ||
|
||
sumFormula : (n : ℕ) → 2 · (∑ (first (suc n))) ≡ n · (n + 1) | ||
sumFormula zero = refl | ||
sumFormula (suc n) = | ||
2 · ∑ (first (2 + n)) ≡⟨ step0 ⟩ | ||
2 · (∑ (first (2 + n) ∘ weakenFin) + first (2 + n) (fromℕ (suc n))) ≡⟨ step1 ⟩ | ||
2 · (∑ (first (2 + n) ∘ weakenFin) + (suc n)) ≡⟨ step2 ⟩ | ||
2 · (∑ (first (1 + n)) + (suc n)) ≡⟨ step3 ⟩ | ||
2 · ∑ (first (1 + n)) + 2 · (suc n) ≡⟨ step4 ⟩ | ||
n · (n + 1) + 2 · (suc n) ≡⟨ useSolver n ⟩ | ||
(suc n) · (suc (n + 1)) ∎ | ||
where | ||
step0 = cong (λ u → 2 · u) (∑Last (first (2 + n))) | ||
step1 = cong (λ u → 2 · (∑ (first (2 + n) ∘ weakenFin) + u)) (toFromId _) | ||
step2 = cong (λ u → 2 · ((∑ u) + (suc n))) (firstDecompose (suc n)) | ||
step3 = ·DistR+ 2 (∑ (first (1 + n))) (suc n) | ||
step4 = cong (λ u → u + 2 · (suc n)) (sumFormula n) | ||
|
||
useSolver : ∀ (n : ℕ) → n · (n + 1) + 2 · (suc n) ≡ (suc n) · (suc (n + 1)) | ||
useSolver = solve |