diff --git a/CHANGELOG.md b/CHANGELOG.md index b025b64de8..d9437ef84e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -98,7 +98,16 @@ New modules * Properties of `IdempotentCommutativeMonoid`s refactored out from `Algebra.Solver.IdempotentCommutativeMonoid`: ```agda Algebra.Properties.IdempotentCommutativeMonoid - ``` + ``` + +* Isomorphism between `Fin` and an 'obvious' definition `ℕ<` of + 'bounded natural number' type, in: + ```agda + Data.Nat.Bounded + Data.Nat.Bounded.Base + Data.Nat.Bounded.Literals + Data.Nat.Bounded.Properties + ``` * Consequences of module monomorphisms ```agda diff --git a/src/Data/Nat/Bounded.agda b/src/Data/Nat/Bounded.agda new file mode 100644 index 0000000000..0af9e09467 --- /dev/null +++ b/src/Data/Nat/Bounded.agda @@ -0,0 +1,29 @@ +------------------------------------------------------------------------ +-- The Agda standard library +-- +-- Bounded natural numbers +------------------------------------------------------------------------ + +{-# OPTIONS --cubical-compatible --safe #-} + +module Data.Nat.Bounded where + +------------------------------------------------------------------------ +-- Publicly re-export the contents of the base module + +open import Data.Nat.Bounded.Base public + +------------------------------------------------------------------------ +-- Publicly re-export properties + +open import Data.Nat.Bounded.Properties public + using + -- queries + ( + _≟_ + -- equalities + ; ⟦⟧≡⟦⟧⇒≡ ; ≡⇒⟦⟧≡⟦⟧ + ; ≡-mod⇒fromℕ≡fromℕ ; fromℕ≡fromℕ⇒≡-mod + -- inversions + ; _/∼≡fromℕ ; _/∼≡fromℕ⁻¹; /∼≡-injective + ) diff --git a/src/Data/Nat/Bounded/Base.agda b/src/Data/Nat/Bounded/Base.agda new file mode 100644 index 0000000000..a230b09b06 --- /dev/null +++ b/src/Data/Nat/Bounded/Base.agda @@ -0,0 +1,96 @@ +------------------------------------------------------------------------ +-- The Agda standard library +-- +-- Bounded natural numbers, and their equivalence to `Fin` +------------------------------------------------------------------------ + +{-# OPTIONS --cubical-compatible --safe #-} + +module Data.Nat.Bounded.Base where + +open import Data.Nat.Base as ℕ +import Data.Nat.DivMod as ℕ +import Data.Nat.Properties as ℕ +open import Data.Fin.Base as Fin using (Fin) +import Data.Fin.Properties as Fin +open import Relation.Binary.Core using (Rel) +open import Relation.Binary.Definitions +open import Relation.Nullary.Decidable.Core using (recompute) +open import Relation.Nullary.Negation.Core using (¬_) + +private + variable + m n o : ℕ + +------------------------------------------------------------------------ +-- Definition + +record ℕ< (n : ℕ) : Set where + constructor ⟦_⟧<_ + field + ⟦_⟧ : ℕ + .bounded : ⟦_⟧ < n + +open ℕ< public using (⟦_⟧) + +-- Constructors: 'n-1 mod n', '0 mod n', '1 mod n' + +⟦-1⟧< ⟦0⟧< ⟦1⟧< : .{{ NonZero n }} → ℕ< n +⟦-1⟧< {n = suc m} = ⟦ m ⟧< ℕ.n<1+n m +⟦0⟧< = ⟦ 0 ⟧< >-nonZero⁻¹ _ +⟦1⟧< {n = 1} = ⟦0⟧< +⟦1⟧< {n = 2+ _} = ⟦ 1 ⟧< nonTrivial⇒n>1 _ + +-- Projection from ℕ + +fromℕ : .{{ NonZero n }} → ℕ → ℕ< n +fromℕ {n} m = ⟦ m % n ⟧< ℕ.m%n-nonZero (ℕ.m-nonZero (ℕ.m-nonZero (ℕ.m