module Data.Nat.Properties where
Natural numbers - properties🔗
This module contains proofs of arithmetic identities for the natural numbers. Since they’re mostly simple inductive arguments written in equational reasoning style, they are very lightly commented:
Addition🔗
: (x y z : Nat) → x + (y + z) ≡ (x + y) + z
+-associative = refl
+-associative zero y z (suc x) y z =
+-associative (x + (y + z)) ≡⟨ ap suc (+-associative x y z) ⟩
suc ((x + y) + z) ∎
suc
: (x : Nat) → x + 0 ≡ x
+-zeror = refl
+-zeror zero (suc x) =
+-zeror (x + 0) ≡⟨ ap suc (+-zeror x) ⟩
suc
suc x ∎
: (x y : Nat) → x + suc y ≡ suc (x + y)
+-sucr = refl
+-sucr zero y (suc x) y = ap suc (+-sucr x y)
+-sucr
: (x y : Nat) → x + y ≡ y + x
+-commutative = sym (+-zeror y)
+-commutative zero y (suc x) y =
+-commutative (x + y) ≡⟨ ap suc (+-commutative x y) ⟩
suc (y + x) ≡⟨ sym (+-sucr y x) ⟩
suc
y + suc x ∎
: ∀ k x y → k + x ≡ k + y → x ≡ y
+-inj = p
+-inj zero x y p (suc k) x y p = +-inj k x y (suc-inj p) +-inj
Multiplication🔗
: (x y z : Nat) → (x + y) * z ≡ x * z + y * z
*-distrib-+r = refl
*-distrib-+r zero y z (suc x) y z =
*-distrib-+r (x + y) * z ≡⟨ ap₂ _+_ refl (*-distrib-+r x y z) ⟩
z + (x * z + y * z) ≡⟨ +-associative z (x * z) (y * z) ⟩
z +
z + x * z + y * z ∎
: (m n : Nat) → m * suc n ≡ m + m * n
*-sucr = refl
*-sucr zero n (suc m) n =
*-sucr
suc m * suc n ≡⟨⟩_+_ refl (*-sucr m n) ⟩
suc n + m * suc n ≡⟨ ap₂ (m + m * n) ≡⟨⟩
suc n + (n + (m + m * n)) ≡⟨ ap suc (+-associative n m (m * n)) ⟩
suc (n + m + m * n) ≡⟨ ap (λ x → suc (x + m * n)) (+-commutative n m) ⟩
suc (m + n + m * n) ≡˘⟨ ap suc (+-associative m n (m * n)) ⟩
suc (m + (n + m * n)) ≡⟨⟩
suc
suc m + suc m * n ∎
: (x : Nat) → 1 * x ≡ x
*-onel = +-zeror x
*-onel x
: (x : Nat) → x * 1 ≡ x
*-oner = refl
*-oner zero (suc x) =
*-oner (x * 1) ≡⟨ ap suc (*-oner x) ⟩
suc
suc x ∎
: (x : Nat) → x * 0 ≡ 0
*-zeror = refl
*-zeror zero (suc x) = *-zeror x
*-zeror
: (x y : Nat) → x * y ≡ y * x
*-commutative = sym (*-zeror y)
*-commutative zero y (suc x) y =
*-commutative _+_ refl (*-commutative x y) ⟩
y + x * y ≡⟨ ap₂ (*-sucr y x) ⟩
y + y * x ≡⟨ sym
y * suc x ∎
: (x y z : Nat) → z * (x + y) ≡ z * x + z * y
*-distrib-+l =
*-distrib-+l x y z (x + y) ≡⟨ *-commutative z (x + y) ⟩
z * (x + y) * z ≡⟨ *-distrib-+r x y z ⟩
_+_ (*-commutative x z) (*-commutative y z) ⟩
x * z + y * z ≡⟨ ap₂
z * x + z * y ∎
: (x y z : Nat) → (x * y) * z ≡ x * (y * z)
*-associative = refl
*-associative zero y z (suc x) y z =
*-associative (y + x * y) * z ≡⟨ *-distrib-+r y (x * y) z ⟩
(x * y) * z ≡⟨ ap₂ _+_ refl (*-associative x y z) ⟩
y * z + (y * z) ∎
y * z + x *
: ∀ k x y → x * suc k ≡ y * suc k → x ≡ y
*-suc-inj = refl
*-suc-inj k zero zero p (suc y) p = absurd (zero≠suc p)
*-suc-inj k zero (suc x) zero p = absurd (suc≠zero p)
*-suc-inj k (suc x) (suc y) p = ap suc (*-suc-inj k x y (+-inj _ _ _ p))
*-suc-inj k
: ∀ k x y → suc k * x ≡ suc k * y → x ≡ y
*-suc-inj' = *-suc-inj k x y (*-commutative x (suc k) ·· p ·· *-commutative (suc k) y)
*-suc-inj' k x y p
: ∀ k x y .⦃ _ : Positive k ⦄ → x * k ≡ y * k → x ≡ y
*-injr (suc k) x y p = *-suc-inj k x y p
*-injr
: ∀ k x y .⦃ _ : Positive k ⦄ → k * x ≡ k * y → x ≡ y
*-injl (suc k) x y p = *-suc-inj' k x y p
*-injl
: ∀ x n → x * n ≡ 1 → x ≡ 1
*-is-onel = p
*-is-onel zero n p (suc zero) zero p = refl
*-is-onel (suc (suc x)) zero p = absurd (zero≠suc (sym (*-zeror x) ∙ p))
*-is-onel (suc x) (suc zero) p = ap suc (sym (*-oner x)) ∙ p
*-is-onel (suc x) (suc (suc n)) p = absurd (zero≠suc (sym (suc-inj p)))
*-is-onel
: ∀ x n → x * n ≡ 1 → n ≡ 1
*-is-oner = *-is-onel n x (*-commutative n x ∙ p)
*-is-oner x n p
: ∀ x y → x * y ≡ 0 → (x ≡ 0) ⊎ (y ≡ 0)
*-is-zero = inl refl
*-is-zero zero y p (suc x) zero p = inr refl
*-is-zero (suc x) (suc y) p = absurd (suc≠zero p)
*-is-zero
: ∀ x y ⦃ _ : Positive y ⦄ → x * y ≡ 0 → x ≡ 0
*-is-zerol (suc y) p with *-is-zero x (suc y) p
*-is-zerol x ... | inl p = p
... | inr q = absurd (suc≠zero q)
: ∀ x y ⦃ _ : Positive x ⦄ → x * y ≡ 0 → y ≡ 0
*-is-zeror (suc x) y p with *-is-zero (suc x) y p
*-is-zeror ... | inl p = absurd (suc≠zero p)
... | inr q = q
Exponentiation🔗
: (x : Nat) → x ^ 1 ≡ x
^-oner = *-oner x
^-oner x
: (x : Nat) → 1 ^ x ≡ 1
^-onel = refl
^-onel zero (suc x) =
^-onel (1 ^ x) + 0 ≡⟨ +-zeror (1 ^ x) ⟩
(1 ^ x) ≡⟨ ^-onel x ⟩
1 ∎
: (x y z : Nat) → x ^ (y + z) ≡ (x ^ y) * (x ^ z)
^-+-hom-*r = sym (+-zeror (x ^ z))
^-+-hom-*r x zero z (suc y) z =
^-+-hom-*r x (y + z) ≡⟨ ap (x *_) (^-+-hom-*r x y z) ⟩
x * x ^ (x ^ y * x ^ z) ≡⟨ sym (*-associative x (x ^ y) (x ^ z)) ⟩
x *
x * x ^ y * x ^ z ∎
: (x y z : Nat) → (x * y) ^ z ≡ x ^ z * y ^ z
^-distrib-*r = refl
^-distrib-*r x y zero (suc z) =
^-distrib-*r x y (x * y) ^ z ≡⟨ ap (λ a → x * y * a) (^-distrib-*r x y z) ⟩
x * y * (x ^ z * y ^ z) ≡⟨ sym (*-associative (x * y) (x ^ z) (y ^ z)) ⟩
x * y * (_* y ^ z) (*-associative x y (x ^ z)) ⟩
x * y * x ^ z * y ^ z ≡⟨ ap (y * x ^ z) * y ^ z ≡⟨ ap (λ a → x * a * y ^ z) (*-commutative y (x ^ z)) ⟩
x * (x ^ z * y) * y ^ z ≡⟨ ap (_* y ^ z) (sym (*-associative x (x ^ z) y)) ⟩
x * (x * x ^ z) y (y ^ z) ⟩
x * x ^ z * y * y ^ z ≡⟨ *-associative (y * y ^ z) ∎
x * x ^ z *
: (x y z : Nat) → (x ^ y) ^ z ≡ x ^ (y * z)
^-*-adjunct = ^-onel z
^-*-adjunct x zero z (suc y) zero = ^-*-adjunct x y zero
^-*-adjunct x (suc y) (suc z) =
^-*-adjunct x (x * x ^ y) ^ z ≡⟨ ap (λ a → x * x ^ y * a) (^-distrib-*r x (x ^ y) z) ⟩
x * x ^ y * (x ^ z * (x ^ y) ^ z) ≡⟨ ap (λ a → x * x ^ y * (x ^ z * a)) (^-*-adjunct x y z) ⟩
x * x ^ y * (x ^ z * x ^ (y * z)) ≡⟨ ap (λ a → x * x ^ y * a) (sym (^-+-hom-*r x z (y * z))) ⟩
x * x ^ y * (x ^ (z + (y * z))) ≡⟨ *-associative x (x ^ y) (x ^ (z + y * z)) ⟩
x * x ^ y * (x ^ y * (x ^ (z + (y * z)))) ≡⟨ ap (x *_) (sym (^-+-hom-*r x y (z + y * z))) ⟩
x * (y + (z + y * z)) ≡⟨ ap (λ a → x * x ^ a) (+-associative y z (y * z)) ⟩
x * x ^ (y + z + y * z) ≡⟨ ap (λ a → x * x ^ (a + y * z)) (+-commutative y z) ⟩
x * x ^ (z + y + y * z) ≡˘⟨ ap (λ a → x * x ^ a) (+-associative z y (y * z)) ⟩
x * x ^ (z + (y + y * z)) ≡⟨ ap (λ a → x * x ^ (z + a)) (sym (*-sucr y z)) ⟩
x * x ^ (z + y * suc z) ∎ x * x ^
Compatibility🔗
The order relation on the natural numbers is also compatible with the arithmetic operators:
: (x y : Nat) → x ≤ (x + y)
+-≤l = 0≤x
+-≤l zero y (suc x) y = s≤s (+-≤l x y)
+-≤l
: (x y : Nat) → y ≤ (x + y)
+-≤r = 0≤x
+-≤r x zero (suc y) = subst (λ p → suc y ≤ p) (sym (+-sucr x y)) (s≤s (+-≤r x y))
+-≤r x
: (x y : Nat) → x - y ≤ x
monus-≤ = x≤x
monus-≤ x zero (suc y) = 0≤x
monus-≤ zero (suc x) (suc y) = ≤-sucr (monus-≤ x y)
monus-≤
: (x y z : Nat) → x ≤ y → (z + x) ≤ (z + y)
+-preserves-≤l .0 y zero 0≤x = 0≤x
+-preserves-≤l .0 y (suc z) 0≤x =
+-preserves-≤l (+-preserves-≤l zero y z 0≤x)
s≤s .(suc _) .(suc _) zero (s≤s p) = s≤s p
+-preserves-≤l .(suc _) .(suc _) (suc z) (s≤s p) =
+-preserves-≤l (+-preserves-≤l (suc _) (suc _) z (s≤s p))
s≤s
: (x y z : Nat) → x ≤ y → (x + z) ≤ (y + z)
+-preserves-≤r = subst (λ a → a ≤ (y + z)) (+-commutative z x)
+-preserves-≤r x y z prf (subst (λ a → (z + x) ≤ a) (+-commutative z y) (+-preserves-≤l x y z prf))
: (x y x' y' : Nat) → x ≤ y → x' ≤ y' → (x + x') ≤ (y + y')
+-preserves-≤ = ≤-trans
+-preserves-≤ x y x' y' prf prf' (+-preserves-≤r x y x' prf) (+-preserves-≤l x' y' y prf')
: (x y z : Nat) → x < y → (z + x) < (z + y)
+-preserves-<l (suc y) z (s≤s p) = ≤-trans (s≤s (+-preserves-≤l x y z p)) (≤-refl' (sym (+-sucr z y)))
+-preserves-<l x
: (x y z : Nat) → x < y → (x + z) < (y + z)
+-preserves-<r = substâ‚‚ _<_ (+-commutative z x) (+-commutative z y) (+-preserves-<l x y z p)
+-preserves-<r x y z p
: ∀ x y x' y' → x < y → x' < y' → (x + x') < (y + y')
+-preserves-< = <-trans _ _ _ (+-preserves-<r x y x' p) (+-preserves-<l x' y' y q)
+-preserves-< x y x' y' p q
: (x y z : Nat) → x ≤ y → (z * x) ≤ (z * y)
*-preserves-≤l = 0≤x
*-preserves-≤l x y zero prf (suc z) prf = +-preserves-≤ x y (z * x) (z * y) prf
*-preserves-≤l x y (*-preserves-≤l x y z prf)
: (x y z : Nat) → x ≤ y → (x * z) ≤ (y * z)
*-preserves-≤r = subst (λ a → a ≤ (y * z)) (*-commutative z x)
*-preserves-≤r x y z prf (subst (λ a → (z * x) ≤ a) (*-commutative z y) (*-preserves-≤l x y z prf))
: (x y x' y' : Nat) → x ≤ y → x' ≤ y' → (x * x') ≤ (y * y')
*-preserves-≤ = ≤-trans
*-preserves-≤ x y x' y' prf prf' (*-preserves-≤r x y x' prf) (*-preserves-≤l x' y' y prf')
: (x y z : Nat) → (z + x) ≤ (z + y) → x ≤ y
+-reflects-≤l = prf
+-reflects-≤l x y zero prf (suc z) (s≤s prf) = +-reflects-≤l x y z prf
+-reflects-≤l x y
: (x y z : Nat) → (x + z) ≤ (y + z) → x ≤ y
+-reflects-≤r =
+-reflects-≤r x y z prf
+-reflects-≤l x y z(subst (_≤ (z + y)) (+-commutative x z)
(subst ((x + z) ≤_) (+-commutative y z) prf))
: ∀ {x z} y → x + y ≡ z → x ≤ z
difference→≤ {x} {z} zero p = subst (x ≤_) (sym (+-zeror x) ∙ p) ≤-refl
difference→≤ {zero} {z} (suc y) p = 0≤x
difference→≤ {suc x} {zero} (suc y) p = absurd (suc≠zero p)
difference→≤ {suc x} {suc z} (suc y) p = s≤s (difference→≤ (suc y) (suc-inj p))
difference→≤
: ∀ {x} → x ≠0 → 0 < x
nonzero→positive {zero} p = absurd (p refl)
nonzero→positive {suc x} p = s≤s 0≤x
nonzero→positive
: ∀ x {y z} .⦃ _ : Positive x ⦄ → (y * x) ≤ (z * x) → y ≤ z
*-cancel-≤r (suc x) {zero} {z} p = 0≤x
*-cancel-≤r (suc x) {suc y} {suc z} (s≤s p) = s≤s
*-cancel-≤r (*-cancel-≤r (suc x) {y} {z} (+-reflects-≤l (y * suc x) (z * suc x) x p))
Monus🔗
: ∀ a → 0 - a ≡ 0
monus-zero = refl
monus-zero zero (suc a) = refl
monus-zero
: ∀ k m n → (k + m) - (k + n) ≡ m - n
monus-cancell = λ _ _ → refl
monus-cancell zero (suc k) = monus-cancell k
monus-cancell
: ∀ m n k → (m - n) * k ≡ m * k - n * k
monus-distribr = refl
monus-distribr m zero k (suc n) k = sym (monus-zero (k + n * k))
monus-distribr zero (suc m) (suc n) k =
monus-distribr (monus-cancell k (m * k) (n * k))
monus-distribr m n k ∙ sym
: ∀ m n k → (m + k) - (n + k) ≡ m - n
monus-cancelr = (λ i → +-commutative m k i - +-commutative n k i) ∙ monus-cancell k m n
monus-cancelr m n k
: ∀ m n k → m - (n + k) ≡ m - n - k
monus-addl = refl
monus-addl zero zero k (suc n) k = sym (monus-zero k)
monus-addl zero (suc m) zero k = refl
monus-addl (suc m) (suc n) k = monus-addl m n k
monus-addl
: ∀ m n k → m - n - k ≡ m - k - n
monus-commute =
monus-commute m n k
m - n - k ≡˘⟨ monus-addl m n k ⟩(n + k) ≡⟨ ap (m -_) (+-commutative n k) ⟩
m - (k + n) ≡⟨ monus-addl m k n ⟩
m -
m - k - n ∎
: ∀ x y z → x + y ≡ z → y ≡ z - x
monus-swapl = sym (monus-cancell x y 0) ∙ ap (x + y -_) (+-zeror x) ∙ ap (_- x) p
monus-swapl x y z p
: ∀ x y z → x + y ≡ z → x ≡ z - y
monus-swapr = sym (monus-cancelr x 0 y) ∙ ap (_- y) p monus-swapr x y z p
Maximum🔗
: (x y z : Nat) → max x (max y z) ≡ max (max x y) z
max-assoc = refl
max-assoc zero zero zero (suc z) = refl
max-assoc zero zero (suc y) zero = refl
max-assoc zero (suc y) (suc z) = refl
max-assoc zero (suc x) zero zero = refl
max-assoc (suc x) zero (suc z) = refl
max-assoc (suc x) (suc y) zero = refl
max-assoc (suc x) (suc y) (suc z) = ap suc (max-assoc x y z)
max-assoc
: (x y : Nat) → x ≤ max x y
max-≤l = 0≤x
max-≤l zero zero (suc y) = 0≤x
max-≤l zero (suc x) zero = ≤-refl
max-≤l (suc x) (suc y) = s≤s (max-≤l x y)
max-≤l
: (x y : Nat) → y ≤ max x y
max-≤r = 0≤x
max-≤r zero zero (suc y) = ≤-refl
max-≤r zero (suc x) zero = 0≤x
max-≤r (suc x) (suc y) = s≤s (max-≤r x y)
max-≤r
: (x y z : Nat) → x ≤ z → y ≤ z → max x y ≤ z
max-univ = 0≤x
max-univ zero zero z 0≤x 0≤x (suc y) (suc z) 0≤x (s≤s q) = s≤s q
max-univ zero (suc x) zero (suc z) (s≤s p) 0≤x = s≤s p
max-univ (suc x) (suc y) (suc z) (s≤s p) (s≤s q) = s≤s (max-univ x y z p q)
max-univ
: (x : Nat) → max 0 x ≡ x
max-zerol = refl
max-zerol zero (suc x) = refl
max-zerol
: (x : Nat) → max x 0 ≡ x
max-zeror = refl
max-zeror zero (suc x) = refl max-zeror
Minimum🔗
: (x y z : Nat) → min x (min y z) ≡ min (min x y) z
min-assoc = refl
min-assoc zero zero zero (suc z) = refl
min-assoc zero zero (suc y) zero = refl
min-assoc zero (suc y) (suc z) = refl
min-assoc zero (suc x) zero zero = refl
min-assoc (suc x) zero (suc z) = refl
min-assoc (suc x) (suc y) zero = refl
min-assoc (suc x) (suc y) (suc z) = ap suc (min-assoc x y z)
min-assoc
: (x y : Nat) → min x y ≤ x
min-≤l = 0≤x
min-≤l zero zero (suc y) = 0≤x
min-≤l zero (suc x) zero = 0≤x
min-≤l (suc x) (suc y) = s≤s (min-≤l x y)
min-≤l
: (x y : Nat) → min x y ≤ y
min-≤r = 0≤x
min-≤r zero zero (suc y) = 0≤x
min-≤r zero (suc x) zero = 0≤x
min-≤r (suc x) (suc y) = s≤s (min-≤r x y)
min-≤r
: (x y z : Nat) → z ≤ x → z ≤ y → z ≤ min x y
min-univ = 0≤x
min-univ x y zero 0≤x 0≤x (suc x) (suc y) (suc z) (s≤s p) (s≤s q) = s≤s (min-univ x y z p q)
min-univ
: (x : Nat) → min 0 x ≡ 0
min-zerol = refl
min-zerol zero (suc x) = refl
min-zerol
: (x : Nat) → min x 0 ≡ 0
min-zeror = refl
min-zeror zero (suc x) = refl min-zeror
The factorial function🔗
: ∀ n → Positive (factorial n)
factorial-positive = s≤s 0≤x
factorial-positive zero (suc n) = *-preserves-≤ 1 (suc n) 1 (factorial n) (s≤s 0≤x) (factorial-positive n)
factorial-positive
: ∀ n → n ≤ factorial n
≤-factorial = 0≤x
≤-factorial zero (suc n) = subst (_≤ factorial (suc n)) (*-oner (suc n)) (*-preserves-≤ (suc n) (suc n) 1 (factorial n) ≤-refl (factorial-positive n)) ≤-factorial