Skip to content

Commit ff6bfd7

Browse files
authored
Add primop backed versions of natural-number operations (#1818)
1 parent c57f62b commit ff6bfd7

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1649,6 +1649,9 @@ Other minor changes
16491649
pattern <′-base = ≤′-refl
16501650
pattern <′-step {n} m<′n = ≤′-step {n} m<′n
16511651
1652+
_⊔′_ : ℕ → ℕ → ℕ
1653+
_⊓′_ : ℕ → ℕ → ℕ
1654+
∣_-_∣′ : ℕ → ℕ → ℕ
16521655
_! : ℕ → ℕ
16531656
```
16541657

@@ -1703,6 +1706,12 @@ Other minor changes
17031706
m<n⇒m<n*o : .{{_ : NonZero o}} → m < n → m < n * o
17041707
m<n⇒m<o*n : .{{_ : NonZero o}} → m < n → m < o * n
17051708
∸-monoˡ-< : m < o → n ≤ m → m ∸ n < o ∸ n
1709+
1710+
m≤n⇒∣m-n∣≡n∸m : m ≤ n → ∣ m - n ∣ ≡ n ∸ m
1711+
1712+
⊔≡⊔′ : m ⊔ n ≡ m ⊔′ n
1713+
⊓≡⊓′ : m ⊓ n ≡ m ⊓′ n
1714+
∣-∣≡∣-∣′ : ∣ m - n ∣ ≡ ∣ m - n ∣′
17061715
```
17071716

17081717
* Re-exported additional functions from `Data.Nat`:

src/Data/Nat/Base.agda

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,29 @@ zero ⊔ n = n
149149
suc m ⊔ zero = suc m
150150
suc m ⊔ suc n = suc (m ⊔ n)
151151

152+
-- Max defined in terms of primitive operations.
153+
-- This is much faster than `_⊔_` but harder to reason about. For proofs
154+
-- involving this function, convert it to `_⊔_` with `Data.Nat.Properties.⊔≡⊔‵`.
155+
_⊔′_ :
156+
m ⊔′ n with m <ᵇ n
157+
... | false = m
158+
... | true = n
159+
152160
-- Min.
153161

154162
_⊓_ :
155163
zero ⊓ n = zero
156164
suc m ⊓ zero = zero
157165
suc m ⊓ suc n = suc (m ⊓ n)
158166

167+
-- Min defined in terms of primitive operations.
168+
-- This is much faster than `_⊓_` but harder to reason about. For proofs
169+
-- involving this function, convert it to `_⊓_` wtih `Data.Nat.properties.⊓≡⊓′`.
170+
_⊓′_ :
171+
m ⊓′ n with m <ᵇ n
172+
... | false = n
173+
... | true = m
174+
159175
-- Division by 2, rounded downwards.
160176

161177
⌊_/2⌋ :
@@ -181,6 +197,15 @@ x ^ suc n = x * x ^ n
181197
∣ x - zero ∣ = x
182198
∣ suc x - suc y ∣ = ∣ x - y ∣
183199

200+
-- Distance in terms of primitive operations.
201+
-- This is much faster than `∣_-_∣` but harder to reason about. For proofs
202+
-- involving this function, convert it to `∣_-_∣` with
203+
-- `Data.Nat.Properties.∣-∣≡∣-∣′`.
204+
∣_-_∣′ :
205+
∣ x - y ∣′ with x <ᵇ y
206+
... | false = x ∸ y
207+
... | true = y ∸ x
208+
184209
-- Division
185210
-- Note properties of these are in `Nat.DivMod` not `Nat.Properties`
186211

src/Data/Nat/Properties.agda

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,6 +1098,19 @@ m≥n⇒m⊓n≡n {suc m} {suc n} (s≤s m≤n) = cong suc (m≥n⇒m⊓n≡n m
10981098
; x≥y⇒x⊔y≈x = m≥n⇒m⊔n≡m
10991099
}
11001100

1101+
------------------------------------------------------------------------
1102+
-- Equality to their counterparts defined in terms of primitive operations
1103+
1104+
⊔≡⊔′ : m n m ⊔ n ≡ m ⊔′ n
1105+
⊔≡⊔′ m n with m <ᵇ n in eq
1106+
... | false = m≥n⇒m⊔n≡m (≮⇒≥ (λ m<n subst T eq (<⇒<ᵇ m<n)))
1107+
... | true = m≤n⇒m⊔n≡n (<⇒≤ (<ᵇ⇒< m n (subst T (sym eq) _)))
1108+
1109+
⊓≡⊓′ : m n m ⊓ n ≡ m ⊓′ n
1110+
⊓≡⊓′ m n with m <ᵇ n in eq
1111+
... | false = m≥n⇒m⊓n≡n (≮⇒≥ (λ m<n subst T eq (<⇒<ᵇ m<n)))
1112+
... | true = m≤n⇒m⊓n≡m (<⇒≤ (<ᵇ⇒< m n (subst T (sym eq) _)))
1113+
11011114
------------------------------------------------------------------------
11021115
-- Derived properties of _⊓_ and _⊔_
11031116

@@ -1695,6 +1708,11 @@ m≤n⇒∣n-m∣≡n∸m {_} {zero} z≤n = refl
16951708
m≤n⇒∣n-m∣≡n∸m {_} {suc m} z≤n = refl
16961709
m≤n⇒∣n-m∣≡n∸m {_} {_} (s≤s m≤n) = m≤n⇒∣n-m∣≡n∸m m≤n
16971710

1711+
m≤n⇒∣m-n∣≡n∸m : {m n} m ≤ n ∣ m - n ∣ ≡ n ∸ m
1712+
m≤n⇒∣m-n∣≡n∸m {_} {zero} z≤n = refl
1713+
m≤n⇒∣m-n∣≡n∸m {_} {suc n} z≤n = refl
1714+
m≤n⇒∣m-n∣≡n∸m {_} {_} (s≤s m≤n) = m≤n⇒∣m-n∣≡n∸m m≤n
1715+
16981716
∣m-n∣≡m∸n⇒n≤m : {m n} ∣ m - n ∣ ≡ m ∸ n n ≤ m
16991717
∣m-n∣≡m∸n⇒n≤m {zero} {zero} eq = z≤n
17001718
∣m-n∣≡m∸n⇒n≤m {suc m} {zero} eq = z≤n
@@ -1796,6 +1814,11 @@ m≤∣m-n∣+n m n = subst (m ≤_) (+-comm n _) (m≤n+∣m-n∣ m n)
17961814
where open ≤-Reasoning
17971815
∣-∣-triangle (suc x) (suc y) (suc z) = ∣-∣-triangle x y z
17981816

1817+
∣-∣≡∣-∣′ : m n ∣ m - n ∣ ≡ ∣ m - n ∣′
1818+
∣-∣≡∣-∣′ m n with m <ᵇ n in eq
1819+
... | false = m≤n⇒∣n-m∣≡n∸m {n} {m} (≮⇒≥ (λ m<n subst T eq (<⇒<ᵇ m<n)))
1820+
... | true = m≤n⇒∣m-n∣≡n∸m {m} {n} (<⇒≤ (<ᵇ⇒< m n (subst T (sym eq) _)))
1821+
17991822
------------------------------------------------------------------------
18001823
-- Metric structures
18011824

0 commit comments

Comments
 (0)