Skip to content

Commit 4092f7c

Browse files
committed
Relate nats and INT
1 parent c876509 commit 4092f7c

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

src/Data/Integer/IntConstruction.agda

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ record ℤ : Set where
2121
minuend :
2222
subtrahend :
2323

24+
infix 4 _≃_ _≤_ _≥_ _<_ _>_
25+
2426
_≃_ : Rel ℤ _
2527
(a ⊖ b) ≃ (c ⊖ d) = a ℕ.+ d ≡ c ℕ.+ b
2628

@@ -42,15 +44,26 @@ _>_ = flip _<_
4244
1ℤ :
4345
1ℤ = 10
4446

47+
infixl 6 _+_
4548
_+_ :
4649
(a ⊖ b) + (c ⊖ d) = (a ℕ.+ c) ⊖ (b ℕ.+ d)
4750

51+
infixl 7 _*_
4852
_*_ :
4953
(a ⊖ b) * (c ⊖ d) = (a ℕ.* c ℕ.+ b ℕ.* d) ⊖ (a ℕ.* d ℕ.+ b ℕ.* c)
5054

55+
infix 8 -_
5156
-_ :
5257
- (a ⊖ b) = b ⊖ a
5358

59+
infix 8 ⁻_ ⁺_
60+
61+
⁺_ :
62+
⁺ n = n ⊖ 0
63+
64+
⁻_ :
65+
⁻ n = 0 ⊖ n
66+
5467
∣_∣ :
5568
∣ a ⊖ b ∣ = ℕ.∣ a - b ∣′
5669

src/Data/Integer/IntConstruction/Properties.agda

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import Algebra.Properties.CommutativeSemigroup as CommSemigroupProps
1212
open import Data.Integer.IntConstruction
1313
open import Data.Nat.Base as ℕ using (ℕ)
1414
import Data.Nat.Properties as ℕ
15+
open import Data.Product.Base
1516
open import Function.Base
1617
open import Relation.Binary
1718
open import Relation.Binary.PropositionalEquality
@@ -62,6 +63,9 @@ _≃?_ : Decidable _≃_
6263
≤-refl : Reflexive _≤_
6364
≤-refl = ℕ.≤-refl
6465

66+
≤-reflexive : _≃_ ⇒ _≤_
67+
≤-reflexive = ℕ.≤-reflexive
68+
6569
≤-trans : Transitive _≤_
6670
≤-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)
6771

@@ -253,3 +257,61 @@ _<?_ : Decidable _<_
253257

254258
*-comm : Commutative _*_
255259
*-comm (a ⊖ b) (c ⊖ d) = cong₂ ℕ._+_ (cong₂ ℕ._+_ (ℕ.*-comm a c) (ℕ.*-comm b d)) (trans (ℕ.+-comm (c ℕ.* b) (d ℕ.* a)) (cong₂ ℕ._+_ (ℕ.*-comm d a) (ℕ.*-comm c b)))
260+
261+
------------------------------------------------------------------------
262+
-- Properties of ⁺_
263+
264+
⁺-cong : {m n} m ≡ n ⁺ m ≃ ⁺ n
265+
⁺-cong refl = refl
266+
267+
⁺-injective : {m n} ⁺ m ≃ ⁺ n m ≡ n
268+
⁺-injective {m} {n} m+0≡n+0 = begin
269+
m ≡⟨ ℕ.+-identityʳ m ⟨
270+
m ℕ.+ 0 ≡⟨ m+0≡n+0 ⟩
271+
n ℕ.+ 0 ≡⟨ ℕ.+-identityʳ n ⟩
272+
n ∎
273+
where open ≡-Reasoning
274+
275+
⁺-mono-≤ : Monotonic₁ ℕ._≤_ _≤_ ⁺_
276+
⁺-mono-≤ = ℕ.+-monoˡ-≤ 0
277+
278+
⁺-mono-< : Monotonic₁ ℕ._<_ _<_ ⁺_
279+
⁺-mono-< = ℕ.+-monoˡ-< 0
280+
281+
⁺-+-homo : m n ⁺ (m ℕ.+ n) ≃ ⁺ m + ⁺ n
282+
⁺-+-homo m n = refl
283+
284+
⁺-0-homo :0 ≡ 0ℤ
285+
⁺-0-homo = refl
286+
287+
⁺-*-homo : m n ⁺ (m ℕ.* n) ≃ ⁺ m * ⁺ n
288+
⁺-*-homo m n = begin
289+
m ℕ.* n ℕ.+ (m ℕ.* 0 ℕ.+ 0) ≡⟨ cong (m ℕ.* n ℕ.+_) (ℕ.+-identityʳ (m ℕ.* 0)) ⟩
290+
m ℕ.* n ℕ.+ m ℕ.* 0 ≡⟨ cong (m ℕ.* n ℕ.+_) (ℕ.*-zeroʳ m) ⟩
291+
m ℕ.* n ℕ.+ 0 ≡⟨ ℕ.+-identityʳ (m ℕ.* n ℕ.+ 0) ⟨
292+
m ℕ.* n ℕ.+ 0 ℕ.+ 0
293+
where open ≡-Reasoning
294+
295+
------------------------------------------------------------------------
296+
-- Properties of ∣_∣
297+
298+
∣∣-cong : {i j} i ≃ j ∣ i ∣ ≡ ∣ j ∣
299+
∣∣-cong {a ⊖ b} {c ⊖ d} a+d≡c+b = begin
300+
ℕ.∣ a - b ∣′ ≡⟨ ℕ.∣-∣≡∣-∣′ a b ⟨
301+
ℕ.∣ a - b ∣ ≡⟨ ℕ.∣m+o-n+o∣≡∣m-n∣ a b d ⟨
302+
ℕ.∣ a ℕ.+ d - b ℕ.+ d ∣ ≡⟨ cong ℕ.∣_- b ℕ.+ d ∣ a+d≡c+b ⟩
303+
ℕ.∣ c ℕ.+ b - b ℕ.+ d ∣ ≡⟨ cong ℕ.∣_- b ℕ.+ d ∣ (ℕ.+-comm c b) ⟩
304+
ℕ.∣ b ℕ.+ c - b ℕ.+ d ∣ ≡⟨ ℕ.∣m+n-m+o∣≡∣n-o∣ b c d ⟩
305+
ℕ.∣ c - d ∣ ≡⟨ ℕ.∣-∣≡∣-∣′ c d ⟩
306+
ℕ.∣ c - d ∣′ ∎
307+
where open ≡-Reasoning
308+
309+
∣⁺_∣ : n ∣ ⁺ n ∣ ≡ n
310+
∣⁺ n ∣ = refl
311+
312+
∣⁻_∣ : n ∣ ⁻ n ∣ ≡ n
313+
∣⁻ ℕ.zero ∣ = refl
314+
∣⁻ ℕ.suc n ∣ = refl
315+
316+
∣∣-surjective : n ∃[ i ] {j} j ≃ i ∣ j ∣ ≡ n
317+
∣∣-surjective n = ⁺ n , λ {j} j≃⁺n trans (∣∣-cong {i = j} j≃⁺n) ∣⁺ n ∣

src/Data/Nat/Properties.agda

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1834,6 +1834,10 @@ m≤n⇒∣m-n∣≡n∸m {n = _} (s≤s m≤n) = m≤n⇒∣m-n∣≡n∸m
18341834
∣m+n-m+o∣≡∣n-o∣ zero n o = refl
18351835
∣m+n-m+o∣≡∣n-o∣ (suc m) n o = ∣m+n-m+o∣≡∣n-o∣ m n o
18361836

1837+
∣m+o-n+o∣≡∣m-n∣ : m n o ∣ m + o - n + o ∣ ≡ ∣ m - n ∣
1838+
∣m+o-n+o∣≡∣m-n∣ m n zero = cong₂ ∣_-_∣ (+-identityʳ m) (+-identityʳ n)
1839+
∣m+o-n+o∣≡∣m-n∣ m n (suc o) = trans (cong₂ ∣_-_∣ (+-suc m o) (+-suc n o)) (∣m+o-n+o∣≡∣m-n∣ m n o)
1840+
18371841
m∸n≤∣m-n∣ : m n m ∸ n ≤ ∣ m - n ∣
18381842
m∸n≤∣m-n∣ m n with ≤-total m n
18391843
... | inj₁ m≤n = subst (_≤ ∣ m - n ∣) (sym (m≤n⇒m∸n≡0 m≤n)) z≤n

0 commit comments

Comments
 (0)