From 03dc09d0f6fddf296322be8ccfbf44f32f659259 Mon Sep 17 00:00:00 2001 From: Nathan van Doorn Date: Sun, 23 Nov 2025 18:01:52 +0100 Subject: [PATCH 1/4] Attempt at the INT construction --- src/Data/Integer/IntConstruction.agda | 50 ++++ .../Integer/IntConstruction/Properties.agda | 249 ++++++++++++++++++ 2 files changed, 299 insertions(+) create mode 100644 src/Data/Integer/IntConstruction.agda create mode 100644 src/Data/Integer/IntConstruction/Properties.agda diff --git a/src/Data/Integer/IntConstruction.agda b/src/Data/Integer/IntConstruction.agda new file mode 100644 index 0000000000..be7d10fcce --- /dev/null +++ b/src/Data/Integer/IntConstruction.agda @@ -0,0 +1,50 @@ +{-# OPTIONS --safe --cubical-compatible #-} + +module Data.Integer.IntConstruction where + +open import Data.Nat.Base as ℕ using (ℕ) +open import Function.Base using (flip) +open import Relation.Binary +open import Relation.Binary.PropositionalEquality + +infixl 6 _⊖_ + +record ℤ : Set where + constructor _⊖_ + field + minuend : ℕ + subtrahend : ℕ + +_≃_ : Rel ℤ _ +(a ⊖ b) ≃ (c ⊖ d) = a ℕ.+ d ≡ c ℕ.+ b + +_≤_ : Rel ℤ _ +(a ⊖ b) ≤ (c ⊖ d) = a ℕ.+ d ℕ.≤ c ℕ.+ b + +_≥_ : Rel ℤ _ +_≥_ = flip _≤_ + +_<_ : Rel ℤ _ +(a ⊖ b) < (c ⊖ d) = a ℕ.+ d ℕ.< c ℕ.+ b + +_>_ : Rel ℤ _ +_>_ = flip _<_ + +0ℤ : ℤ +0ℤ = 0 ⊖ 0 + +1ℤ : ℤ +1ℤ = 1 ⊖ 0 + +_+_ : ℤ → ℤ → ℤ +(a ⊖ b) + (c ⊖ d) = (a ℕ.+ c) ⊖ (b ℕ.+ d) + +_*_ : ℤ → ℤ → ℤ +(a ⊖ b) * (c ⊖ d) = (a ℕ.* c ℕ.+ b ℕ.* d) ⊖ (a ℕ.* d ℕ.+ b ℕ.* c) + +-_ : ℤ → ℤ +- (a ⊖ b) = b ⊖ a + +∣_∣ : ℤ → ℕ +∣ a ⊖ b ∣ = ℕ.∣ a - b ∣′ + diff --git a/src/Data/Integer/IntConstruction/Properties.agda b/src/Data/Integer/IntConstruction/Properties.agda new file mode 100644 index 0000000000..7848c7a1eb --- /dev/null +++ b/src/Data/Integer/IntConstruction/Properties.agda @@ -0,0 +1,249 @@ +{-# OPTIONS --safe --cubical-compatible #-} + +module Data.Integer.IntConstruction.Properties where + +import Algebra.Properties.CommutativeSemigroup as CommSemigroupProps +open import Data.Integer.IntConstruction +open import Data.Nat.Base as ℕ using (ℕ) +import Data.Nat.Properties as ℕ +open import Function.Base +open import Relation.Binary +open import Relation.Binary.PropositionalEquality +open import Relation.Nullary.Decidable + +open import Algebra.Definitions _≃_ + +private + trans-helper + : ∀ {r} (R : Rel ℕ r) + → ∀ a b c d e f + → R (a ℕ.+ d ℕ.+ (c ℕ.+ f)) (c ℕ.+ b ℕ.+ (e ℕ.+ d)) + → R ((a ℕ.+ f) ℕ.+ (d ℕ.+ c)) ((e ℕ.+ b) ℕ.+ (d ℕ.+ c)) + trans-helper R a b c d e f mono = subst₂ R preamble postamble mono + where + open ≡-Reasoning + open CommSemigroupProps ℕ.+-commutativeSemigroup + preamble : (a ℕ.+ d) ℕ.+ (c ℕ.+ f) ≡ (a ℕ.+ f) ℕ.+ (d ℕ.+ c) + preamble = begin + (a ℕ.+ d) ℕ.+ (c ℕ.+ f) ≡⟨ cong (a ℕ.+ d ℕ.+_) (ℕ.+-comm c f) ⟩ + (a ℕ.+ d) ℕ.+ (f ℕ.+ c) ≡⟨ medial a d f c ⟩ + (a ℕ.+ f) ℕ.+ (d ℕ.+ c) ∎ + postamble : (c ℕ.+ b) ℕ.+ (e ℕ.+ d) ≡ (e ℕ.+ b) ℕ.+ (d ℕ.+ c) + postamble = begin + (c ℕ.+ b) ℕ.+ (e ℕ.+ d) ≡⟨ ℕ.+-comm (c ℕ.+ b) (e ℕ.+ d) ⟩ + (e ℕ.+ d) ℕ.+ (c ℕ.+ b) ≡⟨ cong (e ℕ.+ d ℕ.+_) (ℕ.+-comm c b) ⟩ + (e ℕ.+ d) ℕ.+ (b ℕ.+ c) ≡⟨ medial e d b c ⟩ + (e ℕ.+ b) ℕ.+ (d ℕ.+ c) ∎ + +------------------------------------------------------------------------ +-- Properties of _≃_ + +≃-refl : Reflexive _≃_ +≃-refl = refl + +≃-sym : Symmetric _≃_ +≃-sym = sym + +≃-trans : Transitive _≃_ +≃-trans {a ⊖ b} {c ⊖ d} {e ⊖ f} i≃j j≃k = ℕ.+-cancelʳ-≡ (d ℕ.+ c) (a ℕ.+ f) (e ℕ.+ b) $ trans-helper _≡_ a b c d e f (cong₂ ℕ._+_ i≃j j≃k) + +_≃?_ : Decidable _≃_ +(a ⊖ b) ≃? (c ⊖ d) = a ℕ.+ d ℕ.≟ c ℕ.+ b + +------------------------------------------------------------------------ +-- Properties of _≤_ + +≤-refl : Reflexive _≤_ +≤-refl = ℕ.≤-refl + +≤-trans : Transitive _≤_ +≤-trans {a ⊖ b} {c ⊖ d} {e ⊖ f} i≤j j≤k = ℕ.+-cancelʳ-≤ (d ℕ.+ c) (a ℕ.+ f) (e ℕ.+ b) $ trans-helper ℕ._≤_ a b c d e f (ℕ.+-mono-≤ i≤j j≤k) + +≤-antisym : Antisymmetric _≃_ _≤_ +≤-antisym i≤j j≤i = ℕ.≤-antisym i≤j j≤i + +≤-total : Total _≤_ +≤-total (a ⊖ b) (c ⊖ d) = ℕ.≤-total (a ℕ.+ d) (c ℕ.+ b) + +_≤?_ : Decidable _≤_ +(a ⊖ b) ≤? (c ⊖ d) = a ℕ.+ d ℕ.≤? c ℕ.+ b + +------------------------------------------------------------------------ +-- Properties of _<_ + +<-irrefl : Irreflexive _≃_ _<_ +<-irrefl = ℕ.<-irrefl + +<-trans : Transitive _<_ +<-trans {a ⊖ b} {c ⊖ d} {e ⊖ f} ij = ℕ.+-cancelʳ-< (d ℕ.+ c) (e ℕ.+ b) (a ℕ.+ f) $ trans-helper ℕ._>_ a b c d e f (ℕ.+-mono-<-≤ i>j (ℕ.≤-reflexive (sym j≃k))) + +<-respʳ-≃ : _<_ Respectsʳ _≃_ +<-respʳ-≃ {a ⊖ b} {c ⊖ d} {e ⊖ f} j≃k i Date: Sun, 23 Nov 2025 18:15:28 +0100 Subject: [PATCH 2/4] module headers --- src/Data/Integer/IntConstruction.agda | 6 ++++++ src/Data/Integer/IntConstruction/Properties.agda | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/src/Data/Integer/IntConstruction.agda b/src/Data/Integer/IntConstruction.agda index be7d10fcce..4ba9eba3b3 100644 --- a/src/Data/Integer/IntConstruction.agda +++ b/src/Data/Integer/IntConstruction.agda @@ -1,3 +1,9 @@ +------------------------------------------------------------------------ +-- The Agda standard library +-- +-- Construction of integers as a pair of naturals +------------------------------------------------------------------------ + {-# OPTIONS --safe --cubical-compatible #-} module Data.Integer.IntConstruction where diff --git a/src/Data/Integer/IntConstruction/Properties.agda b/src/Data/Integer/IntConstruction/Properties.agda index 7848c7a1eb..c7bc946be9 100644 --- a/src/Data/Integer/IntConstruction/Properties.agda +++ b/src/Data/Integer/IntConstruction/Properties.agda @@ -1,3 +1,9 @@ +------------------------------------------------------------------------ +-- The Agda standard library +-- +-- Properties of the construction of integers as a pair of naturals +------------------------------------------------------------------------ + {-# OPTIONS --safe --cubical-compatible #-} module Data.Integer.IntConstruction.Properties where From c87650903f9fc3588ffc130dac024cdbdc69f09c Mon Sep 17 00:00:00 2001 From: Nathan van Doorn Date: Mon, 24 Nov 2025 09:50:57 +0100 Subject: [PATCH 3/4] Add relation between integer definitions --- .../IntConstruction/IntProperties.agda | 72 +++++++++++++++++++ src/Data/Integer/IntConstruction/Tmp.agda | 24 +++++++ 2 files changed, 96 insertions(+) create mode 100644 src/Data/Integer/IntConstruction/IntProperties.agda create mode 100644 src/Data/Integer/IntConstruction/Tmp.agda diff --git a/src/Data/Integer/IntConstruction/IntProperties.agda b/src/Data/Integer/IntConstruction/IntProperties.agda new file mode 100644 index 0000000000..2b6a16b09d --- /dev/null +++ b/src/Data/Integer/IntConstruction/IntProperties.agda @@ -0,0 +1,72 @@ +------------------------------------------------------------------------ +-- The Agda standard library +-- +-- To be merged into Data.Integer.Properties before merging! +------------------------------------------------------------------------ + +{-# OPTIONS --safe --cubical-compatible #-} + +module Data.Integer.IntConstruction.IntProperties where + +open import Data.Integer.Base +open import Data.Integer.IntConstruction as INT using (_≃_) +open import Data.Integer.IntConstruction.Tmp +open import Data.Integer.Properties +import Data.Nat.Base as ℕ +open import Data.Product.Base +open import Function.Base +open import Relation.Binary.PropositionalEquality + +fromINT-cong : ∀ {i j} → i ≃ j → fromINT i ≡ fromINT j +fromINT-cong {a INT.⊖ b} {c INT.⊖ d} a+d≡c+b = begin + a ⊖ b ≡⟨ m-n≡m⊖n a b ⟨ + + a - + b ≡⟨ cong (_- + b) (+-identityʳ (+ a)) ⟨ + (+ a + 0ℤ) - + b ≡⟨ cong (λ z → (+ a + z) - + b) (+-inverseʳ (+ d)) ⟨ + (+ a + (+ d - + d)) - + b ≡⟨ cong (_- + b) (+-assoc (+ a) (+ d) (- + d)) ⟨ + (+ (a ℕ.+ d) - + d) - + b ≡⟨ cong (λ z → (+ z - + d) - + b) a+d≡c+b ⟩ + (+ (c ℕ.+ b) - + d) - + b ≡⟨ cong (_- + b) (+-assoc (+ c) (+ b) (- + d)) ⟩ + (+ c + (+ b - + d)) - + b ≡⟨ cong (λ z → (+ c + z) - + b) (+-comm (+ b) (- + d)) ⟩ + (+ c + (- + d + + b)) - + b ≡⟨ cong (_- + b) (+-assoc (+ c) (- + d) (+ b)) ⟨ + ((+ c - + d) + + b) - + b ≡⟨ +-assoc (+ c - + d) (+ b) (- + b) ⟩ + (+ c - + d) + (+ b - + b) ≡⟨ cong₂ _+_ (m-n≡m⊖n c d) (+-inverseʳ (+ b)) ⟩ + c ⊖ d + 0ℤ ≡⟨ +-identityʳ (c ⊖ d) ⟩ + c ⊖ d ∎ + where open ≡-Reasoning + +fromINT-injective : ∀ {i j} → fromINT i ≡ fromINT j → i ≃ j +fromINT-injective {a INT.⊖ b} {c INT.⊖ d} a⊖b≡c⊖d = +-injective $ begin + + a + + d ≡⟨ cong (_+ + d) (+-identityʳ (+ a)) ⟨ + (+ a + 0ℤ) + + d ≡⟨ cong (λ z → (+ a + z) + + d) (+-inverseˡ (+ b)) ⟨ + (+ a + (- + b + + b)) + + d ≡⟨ cong (_+ + d) (+-assoc (+ a) (- + b) (+ b)) ⟨ + ((+ a - + b) + + b) + + d ≡⟨ cong (λ z → (z + + b) + + d) (m-n≡m⊖n a b) ⟩ + (a ⊖ b + + b) + + d ≡⟨ cong (λ z → (z + + b) + + d) a⊖b≡c⊖d ⟩ + (c ⊖ d + + b) + + d ≡⟨ cong (λ z → (z + + b) + + d) (m-n≡m⊖n c d) ⟨ + ((+ c - + d) + + b) + + d ≡⟨ cong (_+ + d) (+-assoc (+ c) (- + d) (+ b)) ⟩ + (+ c + (- + d + + b)) + + d ≡⟨ cong (λ z → (+ c + z) + + d) (+-comm (- + d) (+ b)) ⟩ + (+ c + (+ b - + d)) + + d ≡⟨ cong (_+ + d) (+-assoc (+ c) (+ b) (- + d)) ⟨ + ((+ c + + b) - + d) + + d ≡⟨ +-assoc (+ c + + b) (- + d) (+ d) ⟩ + (+ c + + b) + (- + d + + d) ≡⟨ cong (_+_ (+ c + + b)) (+-inverseˡ (+ d)) ⟩ + (+ c + + b) + 0ℤ ≡⟨ +-identityʳ (+ c + + b) ⟩ + + c + + b ∎ + where open ≡-Reasoning + +fromINT-surjective : ∀ j → ∃[ i ] ∀ {z} → z ≃ i → fromINT z ≡ j +fromINT-surjective j .proj₁ = toINT j +fromINT-surjective (+ n) .proj₂ {a INT.⊖ b} a+0≡n+b = begin + a ⊖ b ≡⟨ m-n≡m⊖n a b ⟨ + + a - + b ≡⟨ cong (_- + b) (+-identityʳ (+ a)) ⟨ + (+ a + 0ℤ) - + b ≡⟨ cong (λ z → + z - + b) a+0≡n+b ⟩ + (+ n + + b) - + b ≡⟨ +-assoc (+ n) (+ b) (- + b) ⟩ + + n + (+ b - + b) ≡⟨ cong (_+_ (+ n)) (+-inverseʳ (+ b)) ⟩ + + n + 0ℤ ≡⟨ +-identityʳ (+ n) ⟩ + + n ∎ + where open ≡-Reasoning +fromINT-surjective (-[1+ n ]) .proj₂ {a INT.⊖ b} a+sn≡b = begin + a ⊖ b ≡⟨ m-n≡m⊖n a b ⟨ + + a - + b ≡⟨ cong (λ z → + a - + z) a+sn≡b ⟨ + + a - (+ a + + ℕ.suc n) ≡⟨ cong (_+_ (+ a)) (neg-distrib-+ (+ a) (+ ℕ.suc n)) ⟩ + + a + (- + a - + ℕ.suc n) ≡⟨ +-assoc (+ a) (- + a) (- + ℕ.suc n) ⟨ + (+ a - + a) - + ℕ.suc n ≡⟨ cong (_- + ℕ.suc n) (+-inverseʳ (+ a)) ⟩ + 0ℤ - + ℕ.suc n ≡⟨ +-identityˡ (- + ℕ.suc n) ⟩ + -[1+ n ] ∎ + where open ≡-Reasoning diff --git a/src/Data/Integer/IntConstruction/Tmp.agda b/src/Data/Integer/IntConstruction/Tmp.agda new file mode 100644 index 0000000000..a585346618 --- /dev/null +++ b/src/Data/Integer/IntConstruction/Tmp.agda @@ -0,0 +1,24 @@ +------------------------------------------------------------------------ +-- The Agda standard library +-- +-- To be merged into Data.Integer.Base before merging! +------------------------------------------------------------------------ + +{-# OPTIONS --safe --cubical-compatible #-} + +module Data.Integer.IntConstruction.Tmp where + +open import Data.Integer.Base +import Data.Integer.IntConstruction as INT +import Data.Nat.Base as ℕ + +fromINT : INT.ℤ → ℤ +fromINT (m INT.⊖ n) = m ⊖ n + +toINT : ℤ → INT.ℤ +toINT (+ n) = n INT.⊖ 0 +toINT (-[1+ n ]) = 0 INT.⊖ ℕ.suc n + +-- here we have a choice: +-- * either we can show the definitions in Data.Int.Base match those on INT under the above isomorphism; +-- * or we could _redefine_ those operations _as_ the operations on INT under the above isormophism. From 4092f7c3d62f7963dd1e2730ebddec8e12364c1c Mon Sep 17 00:00:00 2001 From: Nathan van Doorn Date: Mon, 24 Nov 2025 10:48:04 +0100 Subject: [PATCH 4/4] Relate nats and INT --- src/Data/Integer/IntConstruction.agda | 13 ++++ .../Integer/IntConstruction/Properties.agda | 62 +++++++++++++++++++ src/Data/Nat/Properties.agda | 4 ++ 3 files changed, 79 insertions(+) diff --git a/src/Data/Integer/IntConstruction.agda b/src/Data/Integer/IntConstruction.agda index 4ba9eba3b3..b9357536fb 100644 --- a/src/Data/Integer/IntConstruction.agda +++ b/src/Data/Integer/IntConstruction.agda @@ -21,6 +21,8 @@ record ℤ : Set where minuend : ℕ subtrahend : ℕ +infix 4 _≃_ _≤_ _≥_ _<_ _>_ + _≃_ : Rel ℤ _ (a ⊖ b) ≃ (c ⊖ d) = a ℕ.+ d ≡ c ℕ.+ b @@ -42,15 +44,26 @@ _>_ = flip _<_ 1ℤ : ℤ 1ℤ = 1 ⊖ 0 +infixl 6 _+_ _+_ : ℤ → ℤ → ℤ (a ⊖ b) + (c ⊖ d) = (a ℕ.+ c) ⊖ (b ℕ.+ d) +infixl 7 _*_ _*_ : ℤ → ℤ → ℤ (a ⊖ b) * (c ⊖ d) = (a ℕ.* c ℕ.+ b ℕ.* d) ⊖ (a ℕ.* d ℕ.+ b ℕ.* c) +infix 8 -_ -_ : ℤ → ℤ - (a ⊖ b) = b ⊖ a +infix 8 ⁻_ ⁺_ + +⁺_ : ℕ → ℤ +⁺ n = n ⊖ 0 + +⁻_ : ℕ → ℤ +⁻ n = 0 ⊖ n + ∣_∣ : ℤ → ℕ ∣ a ⊖ b ∣ = ℕ.∣ a - b ∣′ diff --git a/src/Data/Integer/IntConstruction/Properties.agda b/src/Data/Integer/IntConstruction/Properties.agda index c7bc946be9..ecc4ceae77 100644 --- a/src/Data/Integer/IntConstruction/Properties.agda +++ b/src/Data/Integer/IntConstruction/Properties.agda @@ -12,6 +12,7 @@ import Algebra.Properties.CommutativeSemigroup as CommSemigroupProps open import Data.Integer.IntConstruction open import Data.Nat.Base as ℕ using (ℕ) import Data.Nat.Properties as ℕ +open import Data.Product.Base open import Function.Base open import Relation.Binary open import Relation.Binary.PropositionalEquality @@ -62,6 +63,9 @@ _≃?_ : Decidable _≃_ ≤-refl : Reflexive _≤_ ≤-refl = ℕ.≤-refl +≤-reflexive : _≃_ ⇒ _≤_ +≤-reflexive = ℕ.≤-reflexive + ≤-trans : Transitive _≤_ ≤-trans {a ⊖ b} {c ⊖ d} {e ⊖ f} i≤j j≤k = ℕ.+-cancelʳ-≤ (d ℕ.+ c) (a ℕ.+ f) (e ℕ.+ b) $ trans-helper ℕ._≤_ a b c d e f (ℕ.+-mono-≤ i≤j j≤k) @@ -253,3 +257,61 @@ _