diff --git a/CHANGELOG.md b/CHANGELOG.md index b025b64de8..7db05f126f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -156,6 +156,13 @@ New modules * `Effect.Foldable`: implementation of haskell-like `Foldable` +* Integer arithmetic modulo `n`, based on `Data.Nat.Bounded.*`: + ```agda + Data.Integer.Modulo.Base + Data.Integer.Modulo.Literals + Data.Integer.Modulo.Properties + ``` + Additions to existing modules ----------------------------- diff --git a/src/Data/Integer/Modulo/Base.agda b/src/Data/Integer/Modulo/Base.agda new file mode 100644 index 0000000000..0cef16ca1c --- /dev/null +++ b/src/Data/Integer/Modulo/Base.agda @@ -0,0 +1,118 @@ +------------------------------------------------------------------------ +-- The Agda standard library +-- +-- Integers mod n, type and basic operations +------------------------------------------------------------------------ + +{-# OPTIONS --cubical-compatible --safe #-} + +open import Data.Nat.Base as ℕ + using (ℕ; zero; suc; NonZero; NonTrivial; nonTrivial⇒nonZero; _<_; _∸_) + +module Data.Integer.Modulo.Base n .{{_ : NonTrivial n}} where + +open import Algebra.Bundles.Raw + using (RawMagma; RawMonoid; RawNearSemiring; RawSemiring; RawRing) +open import Data.Integer.Base as ℤ using (ℤ; _◂_; signAbs) +open import Data.Nat.Bounded.Base as ℕ< hiding (fromℕ; _∼_; ≡-Modℕ) +import Data.Nat.Properties as ℕ +open import Data.Sign.Base as Sign using (Sign) +open import Data.Unit.Base using (⊤) +open import Function.Base using (id; _∘_; _$_; _on_) +open import Relation.Binary.Core using (Rel) +open import Relation.Binary.PropositionalEquality.Core using (_≡_) + +private + variable + m o : ℕ + i j : ℕ< n + + instance + _ = nonTrivial⇒nonZero n + + m∸n-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