Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
6fe6a4a
floor upper bound, ceil lower bound
aortega0703 Feb 19, 2026
c900d0b
Add bounds for floor and ceil
aortega0703 Feb 21, 2026
9c6018a
add introduction of absolute value via inequalities
aortega0703 Feb 24, 2026
1a25ede
Add rounding error for floor, ceil, and round
aortega0703 Feb 25, 2026
90d1ab8
Merge branch 'master' into rounding-properties
aortega0703 Feb 25, 2026
b164b41
Apply suggestions from code review
aortega0703 Feb 25, 2026
585f6f7
Remove unnecessary lambda notation
aortega0703 Feb 25, 2026
3a2ee47
Switch syntax of sym on equalities
aortega0703 Feb 26, 2026
9d2818c
Apply suggestions from code review
aortega0703 Feb 26, 2026
8476db7
Simplify proofs using group properties
aortega0703 Feb 27, 2026
baf6c2c
change use of subst in favour of equational reasoning
aortega0703 Feb 27, 2026
64b0b08
remove subst and rewrite from proof
aortega0703 Feb 28, 2026
5fa0001
prove ceil <= floor + 1
aortega0703 Feb 28, 2026
2b072f9
Replace lambda notation and sym use
aortega0703 Feb 28, 2026
dc9708f
clean ceil <= floor + 1 proof
aortega0703 Mar 2, 2026
60cae8c
floor and ceil of integers
aortega0703 Mar 2, 2026
c62939a
Merge branch 'master' of github.com:agda/agda-stdlib into rounding-pr…
aortega0703 Mar 2, 2026
c78eaba
Merge branch 'master' of github.com:agda/agda-stdlib into rounding-pr…
aortega0703 Mar 12, 2026
bfacc6e
Remove unnecessary lemma
aortega0703 Mar 18, 2026
50a0707
Merge branch 'master' of github.com:agda/agda-stdlib into rounding-pr…
aortega0703 Mar 18, 2026
cabc7f4
prettier proof of ⌈q⌉≤⌊q⌋+1
aortega0703 Aug 2, 2026
a928d66
floor and ceil are optimal
aortega0703 Aug 2, 2026
623db70
relate floor and ceil and reduce their error
aortega0703 Aug 3, 2026
3e15b03
*-cancel-/ for Integers
aortega0703 Aug 9, 2026
2d7c32a
congruence for floor, ceil, round in Unnormalized Rational
aortega0703 Aug 9, 2026
9f3c683
round(-q) = round(q) for Unnormalized Rational
aortega0703 Aug 9, 2026
b798e8b
tweak proof of error of floor and round
aortega0703 Aug 9, 2026
5fe7de9
round is optimal
aortega0703 Aug 10, 2026
75b5fb9
remove unnecessary rewrites, change n to i for integer
aortega0703 Aug 10, 2026
d8193fd
extract lemma from q<⌊q⌋+1
aortega0703 Aug 10, 2026
acc75b4
Update CHANGELOG.md
aortega0703 Aug 10, 2026
0a7aec6
Merge branch 'master' into rounding-properties
aortega0703 Aug 10, 2026
a7117d5
Merge branch 'master' into rounding-properties
aortega0703 Aug 10, 2026
74b1dee
Merge branch 'rounding-properties' of github.com:aortega0703/agda-std…
aortega0703 Aug 10, 2026
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
103 changes: 102 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,14 @@ Additions to existing modules
viaList : (List A → List B) → (DiffList A → DiffList B)
```

* In `Data.Bool.Properties`:
```agda
T-to-≡ : ∀ {x} → T x → x ≡ true
≡-to-T : ∀ {x} → x ≡ true → T x
T-not-to-≡ : ∀ {x} → T (not x) → x ≡ false
≡-to-T-not : ∀ {x} → x ≡ false → T (not x)
```

* In `Data.DifferenceList.Properties`:
```agda
fromList-++ : ∀ xs ys → fromList (xs List.++ ys) ≗ fromList xs ++ fromList ys
Expand All @@ -404,9 +412,55 @@ Additions to existing modules
viaList⁺ : (f : List A → List B) → xs ∼ dxs → f xs ∼ viaList f dxs
```

* In `Data.Integer.Base`:
```
_<ᵇ_ : ℤ → ℤ → Bool
-≤-⁻¹ : ∀ {m} {n} → -[1+ m ] ≤ -[1+ n ] → n ℕ.≤ m
+≤+⁻¹ : ∀ {m} {n} → + m ≤ + n → m ℕ.≤ n
-<-⁻¹ : ∀ {m} {n} → -[1+ m ] < -[1+ n ] → n ℕ.< m
+<+⁻¹ : ∀ {m} {n} → + m < + n → m ℕ.< n
```

* In `Data.Integer.DivMod`:
```agda
i/ℕ1≡i : ∀ i → i /ℕ 1 ≡ i
i/1≡i : ∀ i → i / + 1 ≡ i
/ℕ-congʳ : ∀ i {m} {n} .{{_ : ℕ.NonZero m}} → .{{_ : ℕ.NonZero n}} →
m ≡ n → i /ℕ m ≡ i /ℕ n
nonNeg[i]⇒i/ℕd : ∀ i d .{{_ : ℕ.NonZero d}} .{{_ : NonNegative i}} →
i /ℕ d ≡ + (∣ i ∣ ℕ./ d)
neg[i]∧∣i∣%d≡0⇒i/ℕd : ∀ i {d} .{{_ : ℕ.NonZero d}} .{{_ : Negative i}} →
∣ i ∣ ℕ.% d ≡ 0 → i /ℕ d ≡ - (+ (∣ i ∣ ℕ./ d))
neg[i]∧∣i∣%d≢0⇒i/ℕd : ∀ i d .{{_ : ℕ.NonZero d}} .{{_ : Negative i}}
.{{_ : ℕ.NonZero (∣ i ∣ ℕ.% d)}} → i /ℕ d ≡ -[1+ ∣ i ∣ ℕ./ d ]
*-cancelˡ-/ℕ : ∀ m i n .{{_ : ℕ.NonZero n}} .{{_ : ℕ.NonZero (m ℕ.* n)}} →
(+ m * i) /ℕ (m ℕ.* n) ≡ i /ℕ n
*-cancelʳ-/ℕ : ∀ i m n .{{_ : ℕ.NonZero n}} .{{_ : ℕ.NonZero (n ℕ.* m)}} →
(i * + m) /ℕ (n ℕ.* m) ≡ i /ℕ n
*-cancelˡ-/ : ∀ i j k .{{_ : NonZero k}} .{{_ : NonZero (i * k)}} →
.{{_ : NonNegative i}} → (i * j) / (i * k) ≡ j / k
*-cancelʳ-/ : ∀ i j k .{{_ : NonZero k}} .{{_ : NonZero (k * j)}} →
.{{_ : NonNegative j}} → (i * j) / (k * j) ≡ i / k
```

* In `Data.Integer.GCD`:
```agda
gcd[i,i]≡∣i∣ : ∀ i → gcd i i ≡ + ∣i∣
gcd[i,i]≡∣i∣ : ∀ i → gcd i i ≡ + ∣i∣
```

* In `Data.Integer.Properties`:
```
<ᵇ⇒< : T (i <ᵇ j) → i < j
<⇒<ᵇ : i < j → T (i <ᵇ j)
nonZero⁻¹ : ∀ i → .{{NonZero i}} → i ≢ 0ℤ
nonNeg∧nonZero⇒Pos : ∀ i → .{{NonNegative i}} → .{{NonZero i}} → Positive i
∣i-j∣≡0⇒i≡j : ∀ {i} {j} → ∣ i - j ∣ ≡ 0 → i ≡ j
i*j≢0⇒i≢0 : ∀ i {j} .{{_ : NonZero (i * j)}} → NonZero i
i*j≢0⇒j≢0 : ∀ i {j} .{{_ : NonZero (i * j)}} → NonZero j
i≥0∧j≥0⇒i*j≥0 : ∀ i j → .{{NonNegative i}} → .{{NonNegative j}} →
NonNegative (i * j)
i>0∧j<0⇒i*j<0 : ∀ i j → .{{Positive i}} → .{{Negative j}} →
Negative (i * j)
```

* In `Data.List.Membership.Propositional.Properties`:
Expand Down Expand Up @@ -437,6 +491,8 @@ Additions to existing modules
* In `Data.Nat.DivMod`:
```agda
m<suc[m/n]*n : ∀ m n → m < suc (m / n) * n
m*n%o≡m*n%[m*o] : ∀ m n o .{{_ : NonZero o}} .{{_ : NonZero (m * o)}} →
m * (n % o) ≡ (m * n) % (m * o)
```

* In `Data.Nat.GCD`:
Expand Down Expand Up @@ -468,6 +524,7 @@ Additions to existing modules
m≢0∧n≢0⇒m+n≢0 : ∀ m .{{_ : NonZero m}} n .{{_ : NonZero n}} → NonZero (m + n)
m+n≢0⇒m≢0∨n≢0 : ∀ m n .{{_ : NonZero (m + n)} → NonZero m ⊎ NonZero n
*-almostCancelʳ-≡ : AlmostRightCancellative 0 _*_
m*n≡0⇒n≡0 : ∀ m n .{{_ : NonZero m}} → m * n ≡ 0 → n ≡ 0
```

* In `Data.Product`:
Expand All @@ -491,6 +548,42 @@ Additions to existing modules
i / n + j / n ≡ (i ℤ.+ j) / n
```

* In `Data.Rational.Unnormalised.Properties`:
```agda
≤ᵇ-reflects-≤ : ∀ p q → Reflects (p ≤ q) (p ≤ᵇ q)
≰ᵇ⇒≰ : T (not (p ≤ᵇ q)) → p ≰ q
≰⇒≰ᵇ : p ≰ q → T (not (p ≤ᵇ q))
<ᵇ-reflects-< : ∀ p q → Reflects (p < q) (p <ᵇ q)
≮ᵇ⇒≮ : T (not (p <ᵇ q)) → p ≮ q
≮⇒≮ᵇ : p ≮ q → T (not (p <ᵇ q))
neg-distrib-minus : ∀ p q → - (p - q) ≡ q - p
/-cancelʳ-< : ∀ {i} {j} d .{{_ : ℕ.NonZero d}} → i / d < j / d → i ℤ.< j
/-cancelʳ-≤ : ∀ {i} {j} d .{{_ : ℕ.NonZero d}} → i / d ≤ j / d → i ℤ.≤ j
∣p-q∣≤∣p-r∣+∣r-q∣ : ∀ p q r → ∣ p - q ∣ ≤ ∣ p - r ∣ + ∣ r - q ∣
∣p-q∣≡∣q-p∣ : ∀ p q → ∣ p - q ∣ ≡ ∣ q - p ∣
-q≤p≤q⇒|p|≤q : - q ≤ p → p ≤ q → ∣ p ∣ ≤ q
-q<p<q⇒∣p∣<q : ∀ {p q} → - q < p → p < q → ∣ p ∣ < q
floor-cong : ∀ {p} {q} → p ≃ q → ⌊ p ⌋ ≡ ⌊ q ⌋
ceiling-cong : ∀ {p} {q} → p ≃ q → ⌈ p ⌉ ≡ ⌈ q ⌉
round-cong : ∀ {p} {q} → p ≃ q → round p ≡ round q
⌊i/1⌋≡i : ∀ i → ⌊ i / 1 ⌋ ≡ i
⌈i/1⌉≡i : ∀ i → ⌈ i / 1 ⌉ ≡ i
⌊-q⌋≡-⌈q⌉ : ∀ q → ⌊ - q ⌋ ≡ ℤ.- ⌈ q ⌉
⌈-q⌉≡-⌊q⌋ : ∀ q → ⌈ - q ⌉ ≡ ℤ.- ⌊ q ⌋
round[-q]≡-round[q] : ∀ q → round (- q) ≡ ℤ.- (round q)
⌊q⌋≤q : ⌊ q ⌋ / 1 ≤ q
q<⌊q⌋+1 : q < ⌊ q ⌋ / 1 + 1ℚᵘ
q≤⌈q⌉ : q ≤ ⌈ q ⌉ / 1
⌈q⌉-1<q : ⌈ q ⌉ / 1 - 1ℚᵘ < q
⌈q⌉≤⌊q⌋+1 : ∀ q → ⌈ q ⌉ ℤ.≤ ⌊ q ⌋ ℤ.+ 1ℤ
∣q-⌊q⌋∣<1 : ∀ q → ∣ q - ⌊ q ⌋ / 1 ∣ < 1ℚᵘ
∣q-⌈q⌉∣<1 : ∀ q → ∣ q - ⌈ q ⌉ / 1 ∣ < 1ℚᵘ
∣q-round[q]∣≤½ : ∀ q → ∣ q - (round q) / 1 ∣ ≤ ½
i≤q⇒i≤⌊q⌋ : ∀ i q → i / 1 ≤ q → i ℤ.≤ ⌊ q ⌋
q≤i⇒⌈q⌉≤i : ∀ i q → q ≤ i / 1 → ⌈ q ⌉ ℤ.≤ i
∣q-round[q]∣≤∣q-i∣ : ∀ q i → ∣ q - (round q) / 1 ∣ ≤ ∣ q - i / 1 ∣
```

* In `Data.Sum.Relation.Binary.Pointwise`:
```agda
elim : R =[ f ]⇒ T → S =[ g ]⇒ T →
Expand Down Expand Up @@ -592,6 +685,14 @@ Additions to existing modules
surjective : Surjective _≈₁_ _≈₂_ to
```

* In `Relation.Nullary.Reflects`
```agda
reflects-true : ∀ {b} → Reflects A b → b ≡ true → A
reflects-false : ∀ {b} → Reflects A b → b ≡ false → ¬ A
reflects-proof : ∀ {b} → Reflects A b → A → b ≡ true
reflects-refute : ∀ {b} → Reflects A b → ¬ A → b ≡ false
```

* In `Function.Structures.IsRightInverse`:
```agda
injective : Injective _≈₁_ _≈₂_ to
Expand Down
12 changes: 12 additions & 0 deletions src/Data/Bool/Properties.agda
Original file line number Diff line number Diff line change
Expand Up @@ -853,10 +853,22 @@ T-≡ : ∀ {x} → T x ⇔ x ≡ true
T-≡ {false} = mk⇔ (λ ()) (λ ())
T-≡ {true} = mk⇔ (const refl) (const _)

T-to-≡ : ∀ {x} → T x → x ≡ true
T-to-≡ = Equivalence.to T-≡

≡-to-T : ∀ {x} → x ≡ true → T x
≡-to-T = Equivalence.from T-≡

T-not-≡ : ∀ {x} → T (not x) ⇔ x ≡ false
T-not-≡ {false} = mk⇔ (const refl) (const _)
T-not-≡ {true} = mk⇔ (λ ()) (λ ())

T-not-to-≡ : ∀ {x} → T (not x) → x ≡ false
T-not-to-≡ = Equivalence.to T-not-≡

≡-to-T-not : ∀ {x} → x ≡ false → T (not x)
≡-to-T-not = Equivalence.from T-not-≡

T-∧ : ∀ {x y} → T (x ∧ y) ⇔ (T x × T y)
T-∧ {true} {true} = mk⇔ (const (_ , _)) (const _)
T-∧ {true} {false} = mk⇔ (λ ()) proj₂
Expand Down
13 changes: 13 additions & 0 deletions src/Data/Integer/Base.agda
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,19 @@ data _<_ : ℤ → ℤ → Set where
-<+ : ∀ {m n} → -[1+ m ] < + n
+<+ : ∀ {m n} → (m<n : m ℕ.< n) → + m < + n

-- Destructors of _≤_, _<_
-≤-⁻¹ : ∀ {m} {n} → -[1+ m ] ≤ -[1+ n ] → n ℕ.≤ m
-≤-⁻¹ (-≤- n≤m) = n≤m

+≤+⁻¹ : ∀ {m} {n} → + m ≤ + n → m ℕ.≤ n
+≤+⁻¹ (+≤+ m≤n) = m≤n

-<-⁻¹ : ∀ {m} {n} → -[1+ m ] < -[1+ n ] → n ℕ.< m
-<-⁻¹ (-<- n<m) = n<m

+<+⁻¹ : ∀ {m} {n} → + m < + n → m ℕ.< n
+<+⁻¹ (+<+ m<n) = m<n

_≥_ : Rel ℤ 0ℓ
x ≥ y = y ≤ x

Expand Down
127 changes: 121 additions & 6 deletions src/Data/Integer/DivMod.agda
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@

module Data.Integer.DivMod where

open import Data.Integer.Base using (+_; -[1+_]; +[1+_]; NonZero; _%_; ∣_∣;
_%ℕ_; _/ℕ_; _+_; _*_; -_; _-_; pred; -1ℤ; 0ℤ; _⊖_; _≤_; _<_; +≤+; suc;
+<+)
open import Data.Integer.Base
open import Data.Integer.Properties
open import Data.Nat.Base as ℕ using (ℕ; z≤n; s≤s; z<s; s<s)
import Data.Nat.Properties as ℕ using (m∸n≤m)
import Data.Nat.DivMod as ℕ using (m≡m%n+[m/n]*n; m%n≤n; m%n<n)
import Data.Nat.DivMod as ℕ using (m≡m%n+[m/n]*n; m%n≤n; m%n<n; n/1≡n; n%1≡0;
m*n/m*o≡n/o; m*n%o≡m*n%[m*o])
import Data.Nat.Properties as ℕ using (m∸n≤m; m*n≢0; m*n≢0⇒m≢0; m*n≢0⇒n≢0;
m*n≡0⇒n≡0; *-comm)
open import Function.Base using (_∘′_)
open import Relation.Binary.PropositionalEquality.Core
using (_≡_; cong; sym; subst)
using (_≡_; _≢_; refl; cong; sym; subst; trans; respʳ)
open import Relation.Nullary.Negation using (contradiction)
open ≤-Reasoning

------------------------------------------------------------------------
Expand Down Expand Up @@ -129,6 +130,120 @@ a≡a%n+[a/n]*n n d@(-[1+ _ ]) = begin-equality
+ r + - q * d ≡⟨ cong (_+_ (+ r) ∘′ (_* d)) (sym (-1*i≡-i q)) ⟩
+ r + n / d * d ∎

i/ℕ1≡i : ∀ i → i /ℕ 1 ≡ i
i/ℕ1≡i (+ n) = cong +_ (ℕ.n/1≡n n)
i/ℕ1≡i -[1+ n ] with ℕ.suc n ℕ.% 1 | ℕ.n%1≡0 (ℕ.suc n)
... | ℕ.zero | suc[n]%1≡0 = cong (λ x → - (+ x)) (ℕ.n/1≡n (ℕ.suc n))

i/1≡i : ∀ i → i / + 1 ≡ i
i/1≡i i = trans (div-pos-is-/ℕ i 1) (i/ℕ1≡i i)

/ℕ-congʳ : ∀ i {m} {n} .{{_ : ℕ.NonZero m}} → .{{_ : ℕ.NonZero n}} →
m ≡ n → i /ℕ m ≡ i /ℕ n
/ℕ-congʳ i {m} {n} refl = refl

nonNeg[i]⇒i/ℕd : ∀ i d .{{_ : ℕ.NonZero d}} .{{_ : NonNegative i}} →
i /ℕ d ≡ + (∣ i ∣ ℕ./ d)
nonNeg[i]⇒i/ℕd (+ i) d = refl

neg[i]∧∣i∣%d≡0⇒i/ℕd : ∀ i {d} .{{_ : ℕ.NonZero d}} .{{_ : Negative i}} →
∣ i ∣ ℕ.% d ≡ 0 → i /ℕ d ≡ - (+ (∣ i ∣ ℕ./ d))
neg[i]∧∣i∣%d≡0⇒i/ℕd -[1+ n ] {d} _ with ℕ.zero ← ℕ.suc n ℕ.% d = refl

neg[i]∧∣i∣%d≢0⇒i/ℕd : ∀ i d .{{_ : ℕ.NonZero d}} .{{_ : Negative i}}
.{{_ : ℕ.NonZero (∣ i ∣ ℕ.% d)}} → i /ℕ d ≡ -[1+ ∣ i ∣ ℕ./ d ]
neg[i]∧∣i∣%d≢0⇒i/ℕd -[1+ n ] d {{_}} {{_}} {{mod}} with ℕ.suc n ℕ.% d
... | ℕ.zero = contradiction refl (ℕ.≢-nonZero⁻¹ ℕ.zero)
... | ℕ.suc _ = refl

*-cancelˡ-/ℕ : ∀ m i n .{{_ : ℕ.NonZero n}} .{{_ : ℕ.NonZero (m ℕ.* n)}} →
(+ m * i) /ℕ (m ℕ.* n) ≡ i /ℕ n
*-cancelˡ-/ℕ m i@(+ _) n = begin-equality
(+ m * i) /ℕ (m ℕ.* n)
≡⟨ nonNeg[i]⇒i/ℕd (+ m * i) (m ℕ.* n) ⟩
+ (∣ + m * i ∣ ℕ./ (m ℕ.* n))
≡⟨ cong (+_ ∘′ (ℕ._/ (m ℕ.* n))) (∣i*j∣≡∣i∣*∣j∣ (+ m) i) ⟩
+ ((m ℕ.* ∣ i ∣) ℕ./ (m ℕ.* n))
≡⟨ cong +_ (ℕ.m*n/m*o≡n/o m ∣ i ∣ n) ⟩
+ (∣ i ∣ ℕ./ n)
≡⟨ nonNeg[i]⇒i/ℕd i n ⟨
i /ℕ n ∎
where
instance
_ : ℕ.NonZero m
_ = ℕ.m*n≢0⇒m≢0 m
_ : NonNegative (+ m * i)
_ = i≥0∧j≥0⇒i*j≥0 (+ m) i
*-cancelˡ-/ℕ m i@(-[1+ _ ]) n = helper
where
m*[∣i∣%n]≡∣m*i∣%[m*n] : m ℕ.* (∣ i ∣ ℕ.% n) ≡ ∣ + m * i ∣ ℕ.% (m ℕ.* n)
m*[∣i∣%n]≡∣m*i∣%[m*n] = trans (ℕ.m*n%o≡m*n%[m*o] m ∣ i ∣ n)
(cong (ℕ._% (m ℕ.* n)) (sym (∣i*j∣≡∣i∣*∣j∣ (+ m) i)))
instance
_ : ℕ.NonZero m
_ = ℕ.m*n≢0⇒m≢0 m
_ : Positive (+ m)
_ = nonNeg∧nonZero⇒Pos (+ m)
_ : Negative (+ m * i)
_ = i>0∧j<0⇒i*j<0 (+ m) i
helper : (+ m * i) /ℕ (m ℕ.* n) ≡ i /ℕ n
helper with ∣ + m * i ∣ ℕ.% (m ℕ.* n) in ∣m*i∣%[m*n]
... | ℕ.zero = begin-equality
(+ m * i) /ℕ (m ℕ.* n)
≡⟨ neg[i]∧∣i∣%d≡0⇒i/ℕd (+ m * i) ∣m*i∣%[m*n] ⟩
- (+ (∣ + m * i ∣ ℕ./ (m ℕ.* n)))
≡⟨ cong (-_ ∘′ +_ ∘′ (ℕ._/ _)) (∣i*j∣≡∣i∣*∣j∣ (+ m) i) ⟩
- (+ ((m ℕ.* ∣ i ∣) ℕ./ (m ℕ.* n)))
≡⟨ cong (-_ ∘′ +_) (ℕ.m*n/m*o≡n/o m ∣ i ∣ n) ⟩
- (+ (∣ i ∣ ℕ./ n)) ≡⟨ neg[i]∧∣i∣%d≡0⇒i/ℕd i ∣i∣%m≡0 ⟨
i /ℕ n ∎
where
m*[∣i∣%n]≡0 : m ℕ.* (∣ i ∣ ℕ.% n) ≡ 0
m*[∣i∣%n]≡0 = trans m*[∣i∣%n]≡∣m*i∣%[m*n] ∣m*i∣%[m*n]
∣i∣%m≡0 : ∣ i ∣ ℕ.% n ≡ 0
∣i∣%m≡0 = ℕ.m*n≡0⇒n≡0 m _ m*[∣i∣%n]≡0
... | ℕ.suc _ = begin-equality
(+ m * i) /ℕ (m ℕ.* n)
≡⟨ neg[i]∧∣i∣%d≢0⇒i/ℕd (+ m * i) (m ℕ.* n) ⟩
-[1+ ∣ + m * i ∣ ℕ./ (m ℕ.* n) ]
≡⟨ cong (-[1+_] ∘′ (ℕ._/ (m ℕ.* n))) (∣i*j∣≡∣i∣*∣j∣ (+ m) i) ⟩
-[1+ (m ℕ.* ∣ i ∣) ℕ./ (m ℕ.* n) ]
≡⟨ cong -[1+_] (ℕ.m*n/m*o≡n/o m ∣ i ∣ n) ⟩
-[1+ ∣ i ∣ ℕ./ n ] ≡⟨ neg[i]∧∣i∣%d≢0⇒i/ℕd i n ⟨
i /ℕ n ∎
where instance
∣m*i∣%[m*n]≢0 : ℕ.NonZero (∣ + m * i ∣ ℕ.% (m ℕ.* n))
∣m*i∣%[m*n]≢0 rewrite ∣m*i∣%[m*n] = _
m*[∣i∣%n]≢0 : ℕ.NonZero (m ℕ.* (∣ i ∣ ℕ.% n))
m*[∣i∣%n]≢0 rewrite m*[∣i∣%n]≡∣m*i∣%[m*n] | ∣m*i∣%[m*n] = _
∣i∣%n≢0 : ℕ.NonZero (∣ i ∣ ℕ.% n)
∣i∣%n≢0 = ℕ.m*n≢0⇒n≢0 m

*-cancelʳ-/ℕ : ∀ i m n .{{_ : ℕ.NonZero n}} .{{_ : ℕ.NonZero (n ℕ.* m)}} →
(i * + m) /ℕ (n ℕ.* m) ≡ i /ℕ n
*-cancelʳ-/ℕ i m n rewrite *-comm i (+ m) | ℕ.*-comm n m = *-cancelˡ-/ℕ m i n

*-cancelˡ-/ : ∀ i j k .{{_ : NonZero k}} .{{_ : NonZero (i * k)}} →
.{{_ : NonNegative i}} → (i * j) / (i * k) ≡ j / k
*-cancelˡ-/ (+ i) j k = begin-equality
(sign (+ i * k) ◃ 1) * ((+ i * j) /ℕ ∣ + i * k ∣)
≡⟨ cong (λ x → (x ◃ 1) * ((+ i * j) /ℕ _)) (sign-* (+ i) k)⟩
(sign k ◃ 1) * ((+ i * j) /ℕ ∣ + i * k ∣)
≡⟨ cong ((sign k ◃ 1) *_) (/ℕ-congʳ (+ i * j) (∣i*j∣≡∣i∣*∣j∣ (+ i) k)) ⟩
(sign k ◃ 1) * ((+ i * j) /ℕ (∣ + i ∣ ℕ.* ∣ k ∣))
≡⟨ cong ((sign k ◃ 1) *_) (*-cancelˡ-/ℕ i j ∣ k ∣) ⟩
j / k ∎
where
instance
_ : NonZero (+ i)
_ = i*j≢0⇒i≢0 (+ i)
_ : ℕ.NonZero (∣ + i ∣ ℕ.* ∣ k ∣)
_ = ℕ.m*n≢0 ∣ + i ∣ ∣ k ∣

*-cancelʳ-/ : ∀ i j k .{{_ : NonZero k}} .{{_ : NonZero (k * j)}} →
.{{_ : NonNegative j}} → (i * j) / (k * j) ≡ i / k
*-cancelʳ-/ i j k rewrite *-comm i j | *-comm k j = *-cancelˡ-/ j i k

------------------------------------------------------------------------
-- DEPRECATED NAMES
------------------------------------------------------------------------
Expand Down
Loading
Loading