Skip to content
Merged
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,11 @@ Additions to existing modules
∸-suc : .(m ≤ n) → suc n ∸ m ≡ suc (n ∸ m)
^-distribʳ-* : ∀ m n o → (n * o) ^ m ≡ n ^ m * o ^ m
2*suc[n]≡2+n+n : ∀ n → 2 * (suc n) ≡ 2 + (n + n)
m∸n+o≡m∸[n∸o] : ∀ {m n o} → .(n ≤ m) → .(o ≤ n) → (m ∸ n) + o ≡ m ∸ (n ∸ o)
m∸n≤m⊔n : ∀ m n → m ∸ n ≤ m ⊔ n
m⊔n∸[m∸n]≡n : ∀ m n → m ⊔ n ∸ (m ∸ n) ≡ n
m⊔n≡m∸n+n : ∀ m n → m ⊔ n ≡ m ∸ n + n
∣m-n∣≡m⊔n∸m⊓n : ∀ m n → ∣ m - n ∣ ≡ m ⊔ n ∸ m ⊓ n
```

* In `Data.Product.Properties`:
Expand Down
39 changes: 39 additions & 0 deletions src/Data/Nat/Properties.agda
Original file line number Diff line number Diff line change
Expand Up @@ -1640,6 +1640,15 @@ m≤n⇒n∸m≤n (s≤s m≤n) = m≤n⇒m≤1+n (m≤n⇒n∸m≤n m≤n)
suc ((m + n) ∸ o) ≡⟨ cong suc (+-∸-assoc m o≤n) ⟩
suc (m + (n ∸ o)) ∎

m∸n+o≡m∸[n∸o] : ∀ {m n o} → .(n ≤ m) → .(o ≤ n) → (m ∸ n) + o ≡ m ∸ (n ∸ o)
m∸n+o≡m∸[n∸o] {m} {zero} {zero} _ _ = +-identityʳ m
m∸n+o≡m∸[n∸o] {suc m} {suc n} {zero} _ _ = +-identityʳ (m ∸ n)
m∸n+o≡m∸[n∸o] {suc m} {suc n} {suc o} sn≤sm so≤sn = begin-equality
suc m ∸ suc n + suc o ≡⟨ +-suc (m ∸ n) o ⟩
suc (m ∸ n + o) ≡⟨ cong suc (m∸n+o≡m∸[n∸o] (s≤s⁻¹ sn≤sm) (s≤s⁻¹ so≤sn)) ⟩
suc (m ∸ (n ∸ o)) ≡⟨ ∸-suc (≤-trans (m∸n≤m n o) (s≤s⁻¹ sn≤sm)) ⟨
suc m ∸ (n ∸ o) ∎

m≤n+o⇒m∸n≤o : ∀ m n {o} → m ≤ n + o → m ∸ n ≤ o
m≤n+o⇒m∸n≤o m zero le = le
m≤n+o⇒m∸n≤o zero (suc n) _ = z≤n
Expand Down Expand Up @@ -1728,11 +1737,30 @@ even≢odd (suc m) (suc n) eq = even≢odd m n (suc-injective (begin-equality
------------------------------------------------------------------------
-- Properties of _∸_ and _⊓_ and _⊔_

m∸n≤m⊔n : ∀ m n → m ∸ n ≤ m ⊔ n
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This property doesn't feel natural to me, and the definition is short enough I don't really think it needs a name

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks kind of similar to the m⊔n≤m+n that is already here.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm okay - lets one do proofs at a higher level with fewer steps.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you provide an example of an "interesting" proof where this lemma is a useful step?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if that counts as interesting, but the only place where I used it is down below in the m⊔n∸[m∸n]≡n.

Just to be clear I'm fine with removing that. In this PR I've just included stuff that I've tried to lookup in the stdlib first and written down once I couldn't find it.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having pondered this for a while, I'm leaning towards @Taneb 's position more than @JacquesCarette 's.

Copy link
Collaborator

@jamesmckinna jamesmckinna Feb 24, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Namely, ... TL;DR: Fairbairn threshhold, I think. It's used only once.

m∸n≤m⊔n m n = ≤-trans (m∸n≤m m n) (m≤m⊔n m n)

m⊓n+n∸m≡n : ∀ m n → (m ⊓ n) + (n ∸ m) ≡ n
m⊓n+n∸m≡n zero n = refl
m⊓n+n∸m≡n (suc m) zero = refl
m⊓n+n∸m≡n (suc m) (suc n) = cong suc $ m⊓n+n∸m≡n m n

m⊔n∸[m∸n]≡n : ∀ m n → m ⊔ n ∸ (m ∸ n) ≡ n
m⊔n∸[m∸n]≡n zero n = cong (n ∸_) (0∸n≡0 n)
m⊔n∸[m∸n]≡n (suc m) zero = n∸n≡0 m
m⊔n∸[m∸n]≡n (suc m) (suc n) = begin-equality
suc (m ⊔ n) ∸ (m ∸ n) ≡⟨ ∸-suc (m∸n≤m⊔n m n) ⟩
suc ((m ⊔ n) ∸ (m ∸ n)) ≡⟨ cong suc (m⊔n∸[m∸n]≡n m n) ⟩
suc n ∎

m⊔n≡m∸n+n : ∀ m n → m ⊔ n ≡ m ∸ n + n
m⊔n≡m∸n+n zero n = sym (cong (_+ n) (0∸n≡0 n))
m⊔n≡m∸n+n (suc m) zero = sym (cong suc (+-identityʳ m))
m⊔n≡m∸n+n (suc m) (suc n) = begin-equality
suc (m ⊔ n) ≡⟨ cong suc (m⊔n≡m∸n+n m n) ⟩
suc (m ∸ n + n) ≡⟨ +-suc (m ∸ n) n ⟨
m ∸ n + suc n ∎

[m∸n]⊓[n∸m]≡0 : ∀ m n → (m ∸ n) ⊓ (n ∸ m) ≡ 0
[m∸n]⊓[n∸m]≡0 zero zero = refl
[m∸n]⊓[n∸m]≡0 zero (suc n) = refl
Expand Down Expand Up @@ -1850,6 +1878,17 @@ m∸n≤∣m-n∣ m n with ≤-total m n
∣m-n∣≤m⊔n (suc m) zero = ≤-refl
∣m-n∣≤m⊔n (suc m) (suc n) = m≤n⇒m≤1+n (∣m-n∣≤m⊔n m n)

∣m-n∣≡m⊔n∸m⊓n : ∀ m n → ∣ m - n ∣ ≡ m ⊔ n ∸ m ⊓ n
∣m-n∣≡m⊔n∸m⊓n m n with ≤-total m n
... | inj₁ m≤n = begin-equality
∣ m - n ∣ ≡⟨ m≤n⇒∣m-n∣≡n∸m m≤n ⟩
n ∸ m ≡⟨ cong₂ _∸_ (m≤n⇒m⊔n≡n m≤n) (m≤n⇒m⊓n≡m m≤n) ⟨
m ⊔ n ∸ m ⊓ n ∎
... | inj₂ n≤m = begin-equality
∣ m - n ∣ ≡⟨ m≤n⇒∣n-m∣≡n∸m n≤m ⟩
m ∸ n ≡⟨ cong₂ _∸_ (m≥n⇒m⊔n≡m n≤m) (m≥n⇒m⊓n≡n n≤m) ⟨
m ⊔ n ∸ m ⊓ n ∎

∣-∣-identityˡ : LeftIdentity 0 ∣_-_∣
∣-∣-identityˡ x = refl

Expand Down