Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions src/Data/Integer/IntConstruction.agda
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
------------------------------------------------------------------------
-- The Agda standard library
--
-- Construction of integers as a pair of naturals
------------------------------------------------------------------------

{-# 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 : ℕ

infix 4 _≃_ _≤_ _≥_ _<_ _>_

_≃_ : 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

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 ∣′

72 changes: 72 additions & 0 deletions src/Data/Integer/IntConstruction/IntProperties.agda
Original file line number Diff line number Diff line change
@@ -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
Loading