Skip to content

Commit 6557a4a

Browse files
authored
[ fix #1302 ] Redefine _⊖_ to use _≤ᵇ_ and _∸_ (#1303)
1 parent a0b06ba commit 6557a4a

File tree

8 files changed

+314
-94
lines changed

8 files changed

+314
-94
lines changed

CHANGELOG.md

Lines changed: 62 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,35 @@ Bug-fixes
2121
* The binary relation `_≉_` exposed by records in `Relation.Binary.Bundles` now has
2222
the correct infix precedence.
2323

24+
* Added version to library name
25+
2426
Non-backwards compatible changes
2527
--------------------------------
2628

2729
* The internal build utilities package `lib.cabal` has been renamed
2830
`agda-stdlib-utils.cabal` to avoid potential conflict or confusion.
2931
Please note that the package is not intended for external use.
30-
* The module `Algebra.Construct.Zero` and `Algebra.Module.Construct.Zero` are now level-polymorphic, each taking two implicit level parameters.
32+
33+
* The module `Algebra.Construct.Zero` and `Algebra.Module.Construct.Zero`
34+
are now level-polymorphic, each taking two implicit level parameters.
35+
36+
* Previously the definition of `_⊖_` in `Data.Integer.Base` was defined
37+
inductively as:
38+
```agda
39+
_⊖_ : ℕ → ℕ → ℤ
40+
m ⊖ ℕ.zero = + m
41+
ℕ.zero ⊖ ℕ.suc n = -[1+ n ]
42+
ℕ.suc m ⊖ ℕ.suc n = m ⊖ n
43+
```
44+
which meant that the unary arguments had to be evaluated. To make it
45+
much faster it's definition has been changed to use operations on ``
46+
that are backed by builtin operations:
47+
```agda
48+
_⊖_ : ℕ → ℕ → ℤ
49+
m ⊖ n with m ℕ.<ᵇ n
50+
... | true = - + (n ℕ.∸ m)
51+
... | false = + (m ℕ.∸ n)
52+
```
3153

3254
Deprecated modules
3355
------------------
@@ -115,6 +137,13 @@ Other minor additions
115137
CancellativeCommutativeSemiring c ℓ : Set (suc (c ⊔ ℓ))
116138
```
117139

140+
* Added new definitions to `Algebra.Definitions`:
141+
```agda
142+
AlmostLeftCancellative e _•_ = ∀ {x} y z → ¬ x ≈ e → (x • y) ≈ (x • z) → y ≈ z
143+
AlmostRightCancellative e _•_ = ∀ {x} y z → ¬ x ≈ e → (y • x) ≈ (z • x) → y ≈ z
144+
AlmostCancellative e _•_ = AlmostLeftCancellative e _•_ × AlmostRightCancellative e _•_
145+
```
146+
118147
* Added new records to `Algebra.Morphism.Structures`:
119148
```agda
120149
IsNearSemiringHomomorphism (⟦_⟧ : A → B) : Set (a ⊔ ℓ₁ ⊔ ℓ₂)
@@ -128,27 +157,38 @@ Other minor additions
128157
IsLatticeIsomorphism (⟦_⟧ : A → B) : Set (a ⊔ b ⊔ ℓ₁ ⊔ ℓ₂)
129158
```
130159

131-
* Added new proofs to `Relation.Binary.Construct.Closure.Transitive`:
160+
* Added new definitions to `Algebra.Structures`:
132161
```agda
133-
reflexive : Reflexive _∼_ → Reflexive _∼⁺_
134-
symmetric : Symmetric _∼_ → Symmetric _∼⁺_
135-
transitive : Transitive _∼⁺_
136-
wellFounded : WellFounded _∼_ → WellFounded _∼⁺_
162+
IsCommutativeMagma (• : Op₂ A) : Set (a ⊔ ℓ)
163+
IsCancellativeCommutativeSemiring (+ * : Op₂ A) (0# 1# : A) : Set (a ⊔ ℓ)
164+
```
137165

138-
* Added new definitions to `Algebra.Definitions`:
166+
* Added new proofs in `Data.Integer.Properties`:
139167
```agda
140-
AlmostLeftCancellative e _•_ = ∀ {x} y z → ¬ x ≈ e → (x • y) ≈ (x • z) → y ≈ z
141-
AlmostRightCancellative e _•_ = ∀ {x} y z → ¬ x ≈ e → (y • x) ≈ (z • x) → y ≈ z
142-
AlmostCancellative e _•_ = AlmostLeftCancellative e _•_ × AlmostRightCancellative e _•_
168+
[1+m]⊖[1+n]≡m⊖n : suc m ⊖ suc n ≡ m ⊖ n
169+
⊖-≤ : m ≤ n → m ⊖ n ≡ - + (n ∸ m)
170+
-m+n≡n⊖m : - (+ m) + + n ≡ n ⊖ m
171+
m-n≡m⊖n : + m + (- + n) ≡ m ⊖ n
143172
```
144173

145-
* Added new record to `Algebra.Structures`:
174+
* Added new definition in `Data.Nat.Base`:
146175
```agda
147-
IsCommutativeMagma (• : Op₂ A) : Set (a ⊔ ℓ)
148-
IsCancellativeCommutativeSemiring (+ * : Op₂ A) (0# 1# : A) : Set (a ⊔ ℓ)
176+
_≤ᵇ_ : (m n : ℕ) → Bool
149177
```
150178

151-
* Add version to library name
179+
* Added new proofs in `Data.Nat.Properties`:
180+
```agda
181+
≤ᵇ⇒≤ : T (m ≤ᵇ n) → m ≤ n
182+
≤⇒≤ᵇ : m ≤ n → T (m ≤ᵇ n)
183+
184+
<ᵇ-reflects-< : Reflects (m < n) (m <ᵇ n)
185+
≤ᵇ-reflects-≤ : Reflects (m ≤ n) (m ≤ᵇ n)
186+
```
187+
188+
* Added new proof in `Relation.Nullary.Reflects`:
189+
```agda
190+
fromEquivalence : (T b → P) → (P → T b) → Reflects P b
191+
```
152192

153193
* Add new properties to `Data.Vec.Properties`:
154194
```agda
@@ -160,6 +200,14 @@ Other minor additions
160200
zipWith-replicate : zipWith {n = n} _⊕_ (replicate x) (replicate y) ≡ replicate (x ⊕ y)
161201
```
162202

203+
* Added new proofs to `Relation.Binary.Construct.Closure.Transitive`:
204+
```agda
205+
reflexive : Reflexive _∼_ → Reflexive _∼⁺_
206+
symmetric : Symmetric _∼_ → Symmetric _∼⁺_
207+
transitive : Transitive _∼⁺_
208+
wellFounded : WellFounded _∼_ → WellFounded _∼⁺_
209+
```
210+
163211
* Add new properties to `Data.Integer.Properties`:
164212
```agda
165213
+-*-commutativeSemiring : CommutativeSemiring 0ℓ 0ℓ

src/Data/Integer/Base.agda

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414

1515
module Data.Integer.Base where
1616

17+
open import Data.Bool.Base using (true; false)
1718
open import Data.Empty using (⊥)
18-
open import Data.Unit using (⊤)
19+
open import Data.Unit.Base using (⊤)
1920
open import Data.Nat.Base as ℕ
2021
using (ℕ; z≤n; s≤s) renaming (_+_ to _ℕ+_; _*_ to _ℕ*_)
2122
open import Data.Sign as Sign using (Sign) renaming (_*_ to _S*_)
@@ -188,11 +189,12 @@ signAbs +[1+ n ] = Sign.+ ◂ ℕ.suc n
188189
- +[1+ n ] = -[1+ n ]
189190

190191
-- Subtraction of natural numbers.
191-
192+
-- We define it using _<ᵇ_ and _∸_ rather than inductively so that it
193+
-- is backed by builtin operations. This makes it much faster.
192194
_⊖_ :
193-
m ⊖ ℕ.zero = + m
194-
ℕ.zero ⊖ ℕ.suc n = -[1+ n ]
195-
ℕ.suc m ⊖ ℕ.suc n = m ⊖ n
195+
m ⊖ n with m ℕ.<ᵇ n
196+
... | true = - + (n ℕ.∸ m)
197+
... | false = + (m ℕ.∸ n)
196198

197199
-- Addition.
198200

src/Data/Integer/DivMod.agda

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
module Data.Integer.DivMod where
1010

11+
open import Data.Bool.Base using (true; false)
1112
open import Data.Fin.Base as Fin using (Fin)
1213
import Data.Fin.Properties as FProp
1314
open import Data.Integer.Base as ℤ
@@ -97,9 +98,11 @@ a≡a%ℕn+[a/ℕn]*n -[1+ n ] sd@(ℕ.suc d) with (ℕ.suc n) NDM.divMod (ℕ.s
9798

9899
open ≡-Reasoning
99100

100-
fin-inv : d (k : Fin d) + (ℕ.suc d) - + ℕ.suc (Fin.toℕ k) ≡ + (d ℕ.∸ Fin.toℕ k)
101-
fin-inv (ℕ.suc n) Fin.zero = refl
102-
fin-inv (ℕ.suc n) (Fin.suc k) = ⊖-≥ {n} {Fin.toℕ k} (NProp.<⇒≤ (FProp.toℕ<n k))
101+
fin-inv : d (k : Fin d) +[1+ d ] - +[1+ Fin.toℕ k ] ≡ + (d ℕ.∸ Fin.toℕ k)
102+
fin-inv d k = begin
103+
+[1+ d ] - +[1+ Fin.toℕ k ] ≡⟨ m-n≡m⊖n (ℕ.suc d) (ℕ.suc (Fin.toℕ k)) ⟩
104+
ℕ.suc d ⊖ ℕ.suc (Fin.toℕ k) ≡⟨ ⊖-≥ (ℕ.s≤s (FProp.toℕ≤n k)) ⟩
105+
+ (d ℕ.∸ Fin.toℕ k) ∎ where open ≡-Reasoning
103106

104107
[n/ℕd]*d≤n : n d {d≢0} (n divℕ d) {d≢0} ℤ.* ℤ.+ d ℤ.≤ n
105108
[n/ℕd]*d≤n n (ℕ.suc d) = let q = n divℕ ℕ.suc d; r = n modℕ ℕ.suc d in begin

0 commit comments

Comments
 (0)