Skip to content
Draft
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
39 changes: 39 additions & 0 deletions src/Data/Nat/Properties.agda
Original file line number Diff line number Diff line change
Expand Up @@ -1634,6 +1634,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} z≤n _ = +-identityʳ m
m∸n+o≡m∸[n∸o] {suc m} {suc n} {zero} (s≤s n≤m) z≤n = +-identityʳ (m ∸ n)
m∸n+o≡m∸[n∸o] {suc m} {suc n} {suc o} (s≤s n≤m) (s≤s o≤n) = begin-equality
suc m ∸ suc n + suc o ≡⟨ +-suc (m ∸ n) o ⟩
suc (m ∸ n + o) ≡⟨ cong suc (m∸n+o≡m∸[n∸o] n≤m o≤n) ⟩
suc (m ∸ (n ∸ o)) ≡⟨ ∸-suc (≤-trans (m∸n≤m n o) n≤m) ⟨
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 @@ -1722,11 +1731,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.

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 @@ -1844,6 +1872,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
Copy link
Collaborator

Choose a reason for hiding this comment

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

The structure of this proof recalls that of ∣m-n∣≡[m∸n]∨[n∸m] (L1863).
Is there a refactoring which allows you to prove each result from some other, more general lemma?

Separately, you might like to consider refactoring this proof to not use with, but (more) simply via [ branch1 , branch2 ]′ $ ≤-total m n for suitable subproofs branch1, branch2, should you ever need to reason about this operation. cf. 'with (sometimes) considered harmful' #2123

∣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