2014-08-02 01:40:24 +00:00
|
|
|
|
--- Copyright (c) 2014 Floris van Doorn. All rights reserved.
|
|
|
|
|
--- Released under Apache 2.0 license as described in the file LICENSE.
|
|
|
|
|
--- Author: Floris van Doorn
|
|
|
|
|
|
2014-08-22 23:36:47 +00:00
|
|
|
|
-- data.nat.sub
|
|
|
|
|
-- ============
|
|
|
|
|
--
|
|
|
|
|
-- Subtraction on the natural numbers, as well as min, max, and distance.
|
|
|
|
|
|
2014-11-22 08:15:51 +00:00
|
|
|
|
import .order
|
2014-08-20 02:32:44 +00:00
|
|
|
|
import tools.fake_simplifier
|
|
|
|
|
|
2014-11-22 08:15:51 +00:00
|
|
|
|
open eq.ops
|
2014-09-03 23:00:38 +00:00
|
|
|
|
open fake_simplifier
|
2014-08-20 02:32:44 +00:00
|
|
|
|
|
2014-08-02 01:40:24 +00:00
|
|
|
|
namespace nat
|
|
|
|
|
|
|
|
|
|
-- subtraction
|
|
|
|
|
-- -----------
|
|
|
|
|
|
2014-11-22 08:15:51 +00:00
|
|
|
|
theorem sub_zero_right (n : ℕ) : n - 0 = n :=
|
|
|
|
|
rfl
|
2014-08-02 01:40:24 +00:00
|
|
|
|
|
2014-11-22 08:15:51 +00:00
|
|
|
|
theorem sub_succ_right (n m : ℕ) : n - succ m = pred (n - m) :=
|
|
|
|
|
rfl
|
2014-08-02 01:40:24 +00:00
|
|
|
|
|
2014-09-19 22:40:39 +00:00
|
|
|
|
irreducible sub
|
2014-08-02 01:40:24 +00:00
|
|
|
|
|
2014-10-05 19:39:13 +00:00
|
|
|
|
theorem sub_zero_left (n : ℕ) : 0 - n = 0 :=
|
|
|
|
|
induction_on n !sub_zero_right
|
2014-08-02 02:29:50 +00:00
|
|
|
|
(take k : nat,
|
|
|
|
|
assume IH : 0 - k = 0,
|
|
|
|
|
calc
|
2014-10-05 19:39:13 +00:00
|
|
|
|
0 - succ k = pred (0 - k) : !sub_succ_right
|
2014-08-27 01:47:36 +00:00
|
|
|
|
... = pred 0 : {IH}
|
2014-10-02 01:39:47 +00:00
|
|
|
|
... = 0 : pred.zero)
|
2014-08-27 01:47:36 +00:00
|
|
|
|
|
2014-10-05 19:39:13 +00:00
|
|
|
|
theorem sub_succ_succ (n m : ℕ) : succ n - succ m = n - m :=
|
2014-11-30 23:07:09 +00:00
|
|
|
|
succ_sub_succ_eq_sub n m
|
2014-08-02 01:40:24 +00:00
|
|
|
|
|
2014-10-05 19:39:13 +00:00
|
|
|
|
theorem sub_self (n : ℕ) : n - n = 0 :=
|
|
|
|
|
induction_on n !sub_zero_right (take k IH, !sub_succ_succ ⬝ IH)
|
2014-08-02 01:40:24 +00:00
|
|
|
|
|
2014-10-05 19:39:13 +00:00
|
|
|
|
theorem sub_add_add_right (n k m : ℕ) : (n + k) - (m + k) = n - m :=
|
2014-08-02 02:29:50 +00:00
|
|
|
|
induction_on k
|
|
|
|
|
(calc
|
2014-10-02 01:39:47 +00:00
|
|
|
|
(n + 0) - (m + 0) = n - (m + 0) : {!add.zero_right}
|
|
|
|
|
... = n - m : {!add.zero_right})
|
2014-08-02 02:29:50 +00:00
|
|
|
|
(take l : nat,
|
|
|
|
|
assume IH : (n + l) - (m + l) = n - m,
|
|
|
|
|
calc
|
2014-10-02 01:39:47 +00:00
|
|
|
|
(n + succ l) - (m + succ l) = succ (n + l) - (m + succ l) : {!add.succ_right}
|
|
|
|
|
... = succ (n + l) - succ (m + l) : {!add.succ_right}
|
2014-10-05 19:39:13 +00:00
|
|
|
|
... = (n + l) - (m + l) : !sub_succ_succ
|
2014-08-27 01:47:36 +00:00
|
|
|
|
... = n - m : IH)
|
2014-08-02 01:40:24 +00:00
|
|
|
|
|
2014-10-05 19:39:13 +00:00
|
|
|
|
theorem sub_add_add_left (k n m : ℕ) : (k + n) - (k + m) = n - m :=
|
|
|
|
|
!add.comm ▸ !add.comm ▸ !sub_add_add_right
|
2014-08-02 01:40:24 +00:00
|
|
|
|
|
2014-10-05 19:39:13 +00:00
|
|
|
|
theorem sub_add_left (n m : ℕ) : n + m - m = n :=
|
2014-08-02 02:29:50 +00:00
|
|
|
|
induction_on m
|
2014-10-05 19:39:13 +00:00
|
|
|
|
(!add.zero_right⁻¹ ▸ !sub_zero_right)
|
2014-08-02 02:29:50 +00:00
|
|
|
|
(take k : ℕ,
|
|
|
|
|
assume IH : n + k - k = n,
|
|
|
|
|
calc
|
2014-10-02 01:39:47 +00:00
|
|
|
|
n + succ k - succ k = succ (n + k) - succ k : {!add.succ_right}
|
2014-10-05 19:39:13 +00:00
|
|
|
|
... = n + k - k : !sub_succ_succ
|
2014-08-27 01:47:36 +00:00
|
|
|
|
... = n : IH)
|
2014-08-02 01:40:24 +00:00
|
|
|
|
|
|
|
|
|
-- TODO: add_sub_inv'
|
2014-10-05 19:39:13 +00:00
|
|
|
|
theorem sub_add_left2 (n m : ℕ) : n + m - n = m :=
|
|
|
|
|
!add.comm ▸ !sub_add_left
|
2014-08-02 02:29:50 +00:00
|
|
|
|
|
2014-10-05 19:39:13 +00:00
|
|
|
|
theorem sub_sub (n m k : ℕ) : n - m - k = n - (m + k) :=
|
2014-08-02 02:29:50 +00:00
|
|
|
|
induction_on k
|
|
|
|
|
(calc
|
2014-10-05 19:39:13 +00:00
|
|
|
|
n - m - 0 = n - m : !sub_zero_right
|
2014-10-02 01:39:47 +00:00
|
|
|
|
... = n - (m + 0) : {!add.zero_right⁻¹})
|
2014-08-02 02:29:50 +00:00
|
|
|
|
(take l : nat,
|
|
|
|
|
assume IH : n - m - l = n - (m + l),
|
|
|
|
|
calc
|
2014-10-05 19:39:13 +00:00
|
|
|
|
n - m - succ l = pred (n - m - l) : !sub_succ_right
|
2014-08-27 01:47:36 +00:00
|
|
|
|
... = pred (n - (m + l)) : {IH}
|
2014-10-05 19:39:13 +00:00
|
|
|
|
... = n - succ (m + l) : !sub_succ_right⁻¹
|
2014-10-02 01:39:47 +00:00
|
|
|
|
... = n - (m + succ l) : {!add.succ_right⁻¹})
|
2014-08-02 02:29:50 +00:00
|
|
|
|
|
2014-10-05 19:39:13 +00:00
|
|
|
|
theorem succ_sub_sub (n m k : ℕ) : succ n - m - succ k = n - m - k :=
|
2014-08-02 02:29:50 +00:00
|
|
|
|
calc
|
2014-10-05 19:39:13 +00:00
|
|
|
|
succ n - m - succ k = succ n - (m + succ k) : !sub_sub
|
2014-10-02 01:39:47 +00:00
|
|
|
|
... = succ n - succ (m + k) : {!add.succ_right}
|
2014-10-05 19:39:13 +00:00
|
|
|
|
... = n - (m + k) : !sub_succ_succ
|
|
|
|
|
... = n - m - k : !sub_sub⁻¹
|
2014-08-02 02:29:50 +00:00
|
|
|
|
|
2014-10-05 19:39:13 +00:00
|
|
|
|
theorem sub_add_right_eq_zero (n m : ℕ) : n - (n + m) = 0 :=
|
2014-08-02 02:29:50 +00:00
|
|
|
|
calc
|
2014-10-05 19:39:13 +00:00
|
|
|
|
n - (n + m) = n - n - m : !sub_sub⁻¹
|
|
|
|
|
... = 0 - m : {!sub_self}
|
|
|
|
|
... = 0 : !sub_zero_left
|
2014-08-02 02:29:50 +00:00
|
|
|
|
|
2014-10-05 19:39:13 +00:00
|
|
|
|
theorem sub_comm (m n k : ℕ) : m - n - k = m - k - n :=
|
2014-08-02 02:29:50 +00:00
|
|
|
|
calc
|
2014-10-05 19:39:13 +00:00
|
|
|
|
m - n - k = m - (n + k) : !sub_sub
|
2014-10-02 01:39:47 +00:00
|
|
|
|
... = m - (k + n) : {!add.comm}
|
2014-10-05 19:39:13 +00:00
|
|
|
|
... = m - k - n : !sub_sub⁻¹
|
2014-08-02 02:29:50 +00:00
|
|
|
|
|
2014-10-05 19:39:13 +00:00
|
|
|
|
theorem sub_one (n : ℕ) : n - 1 = pred n :=
|
2014-08-02 02:29:50 +00:00
|
|
|
|
calc
|
2014-10-05 19:39:13 +00:00
|
|
|
|
n - 1 = pred (n - 0) : !sub_succ_right
|
|
|
|
|
... = pred n : {!sub_zero_right}
|
2014-08-02 02:29:50 +00:00
|
|
|
|
|
2014-10-05 19:39:13 +00:00
|
|
|
|
theorem succ_sub_one (n : ℕ) : succ n - 1 = n :=
|
|
|
|
|
!sub_succ_succ ⬝ !sub_zero_right
|
2014-08-02 01:40:24 +00:00
|
|
|
|
|
|
|
|
|
-- add_rewrite sub_add_left
|
|
|
|
|
|
|
|
|
|
-- ### interaction with multiplication
|
|
|
|
|
|
2014-10-05 19:39:13 +00:00
|
|
|
|
theorem mul_pred_left (n m : ℕ) : pred n * m = n * m - m :=
|
2014-08-02 02:29:50 +00:00
|
|
|
|
induction_on n
|
|
|
|
|
(calc
|
2014-10-02 01:39:47 +00:00
|
|
|
|
pred 0 * m = 0 * m : {pred.zero}
|
|
|
|
|
... = 0 : !mul.zero_left
|
2014-10-05 19:39:13 +00:00
|
|
|
|
... = 0 - m : !sub_zero_left⁻¹
|
2014-10-02 01:39:47 +00:00
|
|
|
|
... = 0 * m - m : {!mul.zero_left⁻¹})
|
2014-08-02 02:29:50 +00:00
|
|
|
|
(take k : nat,
|
|
|
|
|
assume IH : pred k * m = k * m - m,
|
|
|
|
|
calc
|
2014-10-02 01:39:47 +00:00
|
|
|
|
pred (succ k) * m = k * m : {!pred.succ}
|
2014-10-05 19:39:13 +00:00
|
|
|
|
... = k * m + m - m : !sub_add_left⁻¹
|
2014-10-02 01:39:47 +00:00
|
|
|
|
... = succ k * m - m : {!mul.succ_left⁻¹})
|
2014-08-02 02:29:50 +00:00
|
|
|
|
|
2014-10-05 19:39:13 +00:00
|
|
|
|
theorem mul_pred_right (n m : ℕ) : n * pred m = n * m - n :=
|
2014-10-02 01:39:47 +00:00
|
|
|
|
calc n * pred m = pred m * n : !mul.comm
|
2014-10-05 19:39:13 +00:00
|
|
|
|
... = m * n - n : !mul_pred_left
|
2014-10-02 01:39:47 +00:00
|
|
|
|
... = n * m - n : {!mul.comm}
|
2014-08-02 02:29:50 +00:00
|
|
|
|
|
2014-10-05 19:39:13 +00:00
|
|
|
|
theorem mul_sub_distr_right (n m k : ℕ) : (n - m) * k = n * k - m * k :=
|
2014-08-02 02:29:50 +00:00
|
|
|
|
induction_on m
|
|
|
|
|
(calc
|
2014-10-05 19:39:13 +00:00
|
|
|
|
(n - 0) * k = n * k : {!sub_zero_right}
|
|
|
|
|
... = n * k - 0 : !sub_zero_right⁻¹
|
2014-10-02 01:39:47 +00:00
|
|
|
|
... = n * k - 0 * k : {!mul.zero_left⁻¹})
|
2014-08-02 02:29:50 +00:00
|
|
|
|
(take l : nat,
|
|
|
|
|
assume IH : (n - l) * k = n * k - l * k,
|
|
|
|
|
calc
|
2014-10-05 19:39:13 +00:00
|
|
|
|
(n - succ l) * k = pred (n - l) * k : {!sub_succ_right}
|
|
|
|
|
... = (n - l) * k - k : !mul_pred_left
|
2014-08-27 01:47:36 +00:00
|
|
|
|
... = n * k - l * k - k : {IH}
|
2014-10-05 19:39:13 +00:00
|
|
|
|
... = n * k - (l * k + k) : !sub_sub
|
2014-10-02 01:39:47 +00:00
|
|
|
|
... = n * k - (succ l * k) : {!mul.succ_left⁻¹})
|
2014-08-02 02:29:50 +00:00
|
|
|
|
|
2014-10-05 19:39:13 +00:00
|
|
|
|
theorem mul_sub_distr_left (n m k : ℕ) : n * (m - k) = n * m - n * k :=
|
2014-08-02 02:29:50 +00:00
|
|
|
|
calc
|
2014-10-02 01:39:47 +00:00
|
|
|
|
n * (m - k) = (m - k) * n : !mul.comm
|
2014-10-05 19:39:13 +00:00
|
|
|
|
... = m * n - k * n : !mul_sub_distr_right
|
2014-10-02 01:39:47 +00:00
|
|
|
|
... = n * m - k * n : {!mul.comm}
|
|
|
|
|
... = n * m - n * k : {!mul.comm}
|
2014-08-02 01:40:24 +00:00
|
|
|
|
|
|
|
|
|
-- ### interaction with inequalities
|
|
|
|
|
|
2014-08-02 02:29:50 +00:00
|
|
|
|
theorem succ_sub {m n : ℕ} : m ≥ n → succ m - n = succ (m - n) :=
|
|
|
|
|
sub_induction n m
|
|
|
|
|
(take k,
|
|
|
|
|
assume H : 0 ≤ k,
|
|
|
|
|
calc
|
2014-10-05 19:39:13 +00:00
|
|
|
|
succ k - 0 = succ k : !sub_zero_right
|
|
|
|
|
... = succ (k - 0) : {!sub_zero_right⁻¹})
|
2014-08-02 02:29:50 +00:00
|
|
|
|
(take k,
|
|
|
|
|
assume H : succ k ≤ 0,
|
2014-10-05 18:36:39 +00:00
|
|
|
|
absurd H !not_succ_zero_le)
|
2014-08-02 02:29:50 +00:00
|
|
|
|
(take k l,
|
|
|
|
|
assume IH : k ≤ l → succ l - k = succ (l - k),
|
|
|
|
|
take H : succ k ≤ succ l,
|
|
|
|
|
calc
|
2014-10-05 19:39:13 +00:00
|
|
|
|
succ (succ l) - succ k = succ l - k : !sub_succ_succ
|
2014-08-27 01:47:36 +00:00
|
|
|
|
... = succ (l - k) : IH (succ_le_cancel H)
|
2014-10-05 19:39:13 +00:00
|
|
|
|
... = succ (succ l - succ k) : {!sub_succ_succ⁻¹})
|
2014-08-02 01:40:24 +00:00
|
|
|
|
|
2014-08-02 02:29:50 +00:00
|
|
|
|
theorem le_imp_sub_eq_zero {n m : ℕ} (H : n ≤ m) : n - m = 0 :=
|
2014-10-05 19:39:13 +00:00
|
|
|
|
obtain (k : ℕ) (Hk : n + k = m), from le_elim H, Hk ▸ !sub_add_right_eq_zero
|
2014-08-02 01:40:24 +00:00
|
|
|
|
|
2014-08-02 02:29:50 +00:00
|
|
|
|
theorem add_sub_le {n m : ℕ} : n ≤ m → n + (m - n) = m :=
|
|
|
|
|
sub_induction n m
|
|
|
|
|
(take k,
|
|
|
|
|
assume H : 0 ≤ k,
|
|
|
|
|
calc
|
2014-10-02 01:39:47 +00:00
|
|
|
|
0 + (k - 0) = k - 0 : !add.zero_left
|
2014-10-05 19:39:13 +00:00
|
|
|
|
... = k : !sub_zero_right)
|
2014-10-05 18:36:39 +00:00
|
|
|
|
(take k, assume H : succ k ≤ 0, absurd H !not_succ_zero_le)
|
2014-08-02 02:29:50 +00:00
|
|
|
|
(take k l,
|
|
|
|
|
assume IH : k ≤ l → k + (l - k) = l,
|
|
|
|
|
take H : succ k ≤ succ l,
|
|
|
|
|
calc
|
2014-10-05 19:39:13 +00:00
|
|
|
|
succ k + (succ l - succ k) = succ k + (l - k) : {!sub_succ_succ}
|
2014-10-02 01:39:47 +00:00
|
|
|
|
... = succ (k + (l - k)) : !add.succ_left
|
2014-08-27 01:47:36 +00:00
|
|
|
|
... = succ l : {IH (succ_le_cancel H)})
|
2014-08-02 02:29:50 +00:00
|
|
|
|
|
|
|
|
|
theorem add_sub_ge_left {n m : ℕ} : n ≥ m → n - m + m = n :=
|
2014-10-05 18:36:39 +00:00
|
|
|
|
!add.comm ▸ !add_sub_le
|
2014-08-02 02:29:50 +00:00
|
|
|
|
|
|
|
|
|
theorem add_sub_ge {n m : ℕ} (H : n ≥ m) : n + (m - n) = n :=
|
|
|
|
|
calc
|
|
|
|
|
n + (m - n) = n + 0 : {le_imp_sub_eq_zero H}
|
2014-10-02 01:39:47 +00:00
|
|
|
|
... = n : !add.zero_right
|
2014-08-02 01:40:24 +00:00
|
|
|
|
|
2014-08-02 02:29:50 +00:00
|
|
|
|
theorem add_sub_le_left {n m : ℕ} : n ≤ m → n - m + m = m :=
|
2014-10-02 01:39:47 +00:00
|
|
|
|
!add.comm ▸ add_sub_ge
|
2014-08-02 01:40:24 +00:00
|
|
|
|
|
2014-10-05 19:39:13 +00:00
|
|
|
|
theorem le_add_sub_left (n m : ℕ) : n ≤ n + (m - n) :=
|
2014-10-05 18:36:39 +00:00
|
|
|
|
or.elim !le_total
|
2014-08-02 02:29:50 +00:00
|
|
|
|
(assume H : n ≤ m, (add_sub_le H)⁻¹ ▸ H)
|
2014-10-05 18:36:39 +00:00
|
|
|
|
(assume H : m ≤ n, (add_sub_ge H)⁻¹ ▸ !le_refl)
|
2014-08-02 01:40:24 +00:00
|
|
|
|
|
2014-10-05 19:39:13 +00:00
|
|
|
|
theorem le_add_sub_right (n m : ℕ) : m ≤ n + (m - n) :=
|
2014-10-05 18:36:39 +00:00
|
|
|
|
or.elim !le_total
|
|
|
|
|
(assume H : n ≤ m, (add_sub_le H)⁻¹ ▸ !le_refl)
|
2014-08-27 01:47:36 +00:00
|
|
|
|
(assume H : m ≤ n, (add_sub_ge H)⁻¹ ▸ H)
|
2014-08-02 01:40:24 +00:00
|
|
|
|
|
|
|
|
|
theorem sub_split {P : ℕ → Prop} {n m : ℕ} (H1 : n ≤ m → P 0) (H2 : ∀k, m + k = n -> P k)
|
2014-08-02 02:29:50 +00:00
|
|
|
|
: P (n - m) :=
|
2014-10-05 18:36:39 +00:00
|
|
|
|
or.elim !le_total
|
2014-08-27 01:47:36 +00:00
|
|
|
|
(assume H3 : n ≤ m, (le_imp_sub_eq_zero H3)⁻¹ ▸ (H1 H3))
|
2014-08-02 02:29:50 +00:00
|
|
|
|
(assume H3 : m ≤ n, H2 (n - m) (add_sub_le H3))
|
|
|
|
|
|
2014-08-27 01:47:36 +00:00
|
|
|
|
theorem le_elim_sub {n m : ℕ} (H : n ≤ m) : ∃k, m - k = n :=
|
2014-08-02 02:29:50 +00:00
|
|
|
|
obtain (k : ℕ) (Hk : n + k = m), from le_elim H,
|
2014-12-16 03:05:03 +00:00
|
|
|
|
exists.intro k
|
2014-08-02 02:29:50 +00:00
|
|
|
|
(calc
|
2014-08-27 01:47:36 +00:00
|
|
|
|
m - k = n + k - k : {Hk⁻¹}
|
2014-10-05 19:39:13 +00:00
|
|
|
|
... = n : !sub_add_left)
|
2014-08-02 02:29:50 +00:00
|
|
|
|
|
|
|
|
|
theorem add_sub_assoc {m k : ℕ} (H : k ≤ m) (n : ℕ) : n + m - k = n + (m - k) :=
|
|
|
|
|
have l1 : k ≤ m → n + m - k = n + (m - k), from
|
|
|
|
|
sub_induction k m
|
|
|
|
|
(take m : ℕ,
|
|
|
|
|
assume H : 0 ≤ m,
|
|
|
|
|
calc
|
2014-10-05 19:39:13 +00:00
|
|
|
|
n + m - 0 = n + m : !sub_zero_right
|
|
|
|
|
... = n + (m - 0) : {!sub_zero_right⁻¹})
|
2014-10-05 18:36:39 +00:00
|
|
|
|
(take k : ℕ, assume H : succ k ≤ 0, absurd H !not_succ_zero_le)
|
2014-08-02 02:29:50 +00:00
|
|
|
|
(take k m,
|
|
|
|
|
assume IH : k ≤ m → n + m - k = n + (m - k),
|
|
|
|
|
take H : succ k ≤ succ m,
|
|
|
|
|
calc
|
2014-10-02 01:39:47 +00:00
|
|
|
|
n + succ m - succ k = succ (n + m) - succ k : {!add.succ_right}
|
2014-10-05 19:39:13 +00:00
|
|
|
|
... = n + m - k : !sub_succ_succ
|
2014-08-27 01:47:36 +00:00
|
|
|
|
... = n + (m - k) : IH (succ_le_cancel H)
|
2014-10-05 19:39:13 +00:00
|
|
|
|
... = n + (succ m - succ k) : {!sub_succ_succ⁻¹}),
|
2014-08-02 02:29:50 +00:00
|
|
|
|
l1 H
|
|
|
|
|
|
|
|
|
|
theorem sub_eq_zero_imp_le {n m : ℕ} : n - m = 0 → n ≤ m :=
|
|
|
|
|
sub_split
|
|
|
|
|
(assume H1 : n ≤ m, assume H2 : 0 = 0, H1)
|
|
|
|
|
(take k : ℕ,
|
|
|
|
|
assume H1 : m + k = n,
|
|
|
|
|
assume H2 : k = 0,
|
2014-10-02 01:39:47 +00:00
|
|
|
|
have H3 : n = m, from !add.zero_right ▸ H2 ▸ H1⁻¹,
|
2014-10-05 18:36:39 +00:00
|
|
|
|
H3 ▸ !le_refl)
|
2014-08-02 01:40:24 +00:00
|
|
|
|
|
|
|
|
|
theorem sub_sub_split {P : ℕ → ℕ → Prop} {n m : ℕ} (H1 : ∀k, n = m + k -> P k 0)
|
2014-08-02 02:29:50 +00:00
|
|
|
|
(H2 : ∀k, m = n + k → P 0 k) : P (n - m) (m - n) :=
|
2014-10-05 18:36:39 +00:00
|
|
|
|
or.elim !le_total
|
2014-08-02 02:29:50 +00:00
|
|
|
|
(assume H3 : n ≤ m,
|
|
|
|
|
le_imp_sub_eq_zero H3⁻¹ ▸ (H2 (m - n) (add_sub_le H3⁻¹)))
|
|
|
|
|
(assume H3 : m ≤ n,
|
|
|
|
|
le_imp_sub_eq_zero H3⁻¹ ▸ (H1 (n - m) (add_sub_le H3⁻¹)))
|
|
|
|
|
|
|
|
|
|
theorem sub_intro {n m k : ℕ} (H : n + m = k) : k - n = m :=
|
|
|
|
|
have H2 : k - n + n = m + n, from
|
|
|
|
|
calc
|
2014-08-27 01:47:36 +00:00
|
|
|
|
k - n + n = k : add_sub_ge_left (le_intro H)
|
|
|
|
|
... = n + m : H⁻¹
|
2014-10-02 01:39:47 +00:00
|
|
|
|
... = m + n : !add.comm,
|
|
|
|
|
add.cancel_right H2
|
2014-08-02 02:29:50 +00:00
|
|
|
|
|
|
|
|
|
theorem sub_lt {x y : ℕ} (xpos : x > 0) (ypos : y > 0) : x - y < x :=
|
2014-11-22 17:36:33 +00:00
|
|
|
|
sub.lt xpos ypos
|
2014-08-02 02:29:50 +00:00
|
|
|
|
|
|
|
|
|
theorem sub_le_right {n m : ℕ} (H : n ≤ m) (k : nat) : n - k ≤ m - k :=
|
|
|
|
|
obtain (l : ℕ) (Hl : n + l = m), from le_elim H,
|
2014-10-05 18:36:39 +00:00
|
|
|
|
or.elim !le_total
|
|
|
|
|
(assume H2 : n ≤ k, (le_imp_sub_eq_zero H2)⁻¹ ▸ !zero_le)
|
2014-08-02 02:29:50 +00:00
|
|
|
|
(assume H2 : k ≤ n,
|
|
|
|
|
have H3 : n - k + l = m - k, from
|
|
|
|
|
calc
|
2014-11-22 08:15:51 +00:00
|
|
|
|
n - k + l = l + (n - k) : add.comm
|
2014-08-27 01:47:36 +00:00
|
|
|
|
... = l + n - k : (add_sub_assoc H2 l)⁻¹
|
2014-10-02 01:39:47 +00:00
|
|
|
|
... = n + l - k : {!add.comm}
|
2014-08-27 01:47:36 +00:00
|
|
|
|
... = m - k : {Hl},
|
2014-08-02 02:29:50 +00:00
|
|
|
|
le_intro H3)
|
|
|
|
|
|
|
|
|
|
theorem sub_le_left {n m : ℕ} (H : n ≤ m) (k : nat) : k - m ≤ k - n :=
|
|
|
|
|
obtain (l : ℕ) (Hl : n + l = m), from le_elim H,
|
|
|
|
|
sub_split
|
2014-10-05 18:36:39 +00:00
|
|
|
|
(assume H2 : k ≤ m, !zero_le)
|
2014-08-02 02:29:50 +00:00
|
|
|
|
(take m' : ℕ,
|
|
|
|
|
assume Hm : m + m' = k,
|
|
|
|
|
have H3 : n ≤ k, from le_trans H (le_intro Hm),
|
|
|
|
|
have H4 : m' + l + n = k - n + n, from
|
|
|
|
|
calc
|
2014-11-22 08:15:51 +00:00
|
|
|
|
m' + l + n = n + (m' + l) : add.comm
|
|
|
|
|
... = n + (l + m') : add.comm
|
|
|
|
|
... = n + l + m' : add.assoc
|
|
|
|
|
... = m + m' : {Hl}
|
|
|
|
|
... = k : Hm
|
|
|
|
|
... = k - n + n : (add_sub_ge_left H3)⁻¹,
|
2014-10-02 01:39:47 +00:00
|
|
|
|
le_intro (add.cancel_right H4))
|
2014-08-02 01:40:24 +00:00
|
|
|
|
|
2014-12-17 18:32:38 +00:00
|
|
|
|
theorem sub_pos_of_gt {m n : ℕ} (H : n > m) : n - m > 0 :=
|
|
|
|
|
have H1 : n = n - m + m, from (add_sub_ge_left (lt_imp_le H))⁻¹,
|
|
|
|
|
have H2 : 0 + m < n - m + m, from (add.zero_left m)⁻¹ ▸ H1 ▸ H,
|
|
|
|
|
!add_lt_cancel_right H2
|
|
|
|
|
|
2014-08-02 01:40:24 +00:00
|
|
|
|
-- theorem sub_lt_cancel_right {n m k : ℕ) (H : n - k < m - k) : n < m
|
|
|
|
|
-- :=
|
|
|
|
|
-- _
|
|
|
|
|
|
|
|
|
|
-- theorem sub_lt_cancel_left {n m k : ℕ) (H : n - m < n - k) : k < m
|
|
|
|
|
-- :=
|
|
|
|
|
-- _
|
|
|
|
|
|
2014-10-05 19:39:13 +00:00
|
|
|
|
theorem sub_triangle_inequality (n m k : ℕ) : n - k ≤ (n - m) + (m - k) :=
|
2014-08-02 02:29:50 +00:00
|
|
|
|
sub_split
|
2014-10-02 01:39:47 +00:00
|
|
|
|
(assume H : n ≤ m, !add.zero_left⁻¹ ▸ sub_le_right H k)
|
2014-08-02 02:29:50 +00:00
|
|
|
|
(take mn : ℕ,
|
|
|
|
|
assume Hmn : m + mn = n,
|
|
|
|
|
sub_split
|
|
|
|
|
(assume H : m ≤ k,
|
2014-08-27 01:47:36 +00:00
|
|
|
|
have H2 : n - k ≤ n - m, from sub_le_left H n,
|
|
|
|
|
have H3 : n - k ≤ mn, from sub_intro Hmn ▸ H2,
|
2014-10-02 01:39:47 +00:00
|
|
|
|
show n - k ≤ mn + 0, from !add.zero_right⁻¹ ▸ H3)
|
2014-08-02 02:29:50 +00:00
|
|
|
|
(take km : ℕ,
|
2014-08-27 01:47:36 +00:00
|
|
|
|
assume Hkm : k + km = m,
|
|
|
|
|
have H : k + (mn + km) = n, from
|
|
|
|
|
calc
|
2014-11-22 08:15:51 +00:00
|
|
|
|
k + (mn + km) = k + (km + mn): add.comm
|
|
|
|
|
... = k + km + mn : add.assoc
|
|
|
|
|
... = m + mn : Hkm
|
|
|
|
|
... = n : Hmn,
|
2014-08-27 01:47:36 +00:00
|
|
|
|
have H2 : n - k = mn + km, from sub_intro H,
|
2014-10-05 18:36:39 +00:00
|
|
|
|
H2 ▸ !le_refl))
|
2014-08-02 01:40:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-- add_rewrite sub_self mul_sub_distr_left mul_sub_distr_right
|
|
|
|
|
|
|
|
|
|
|
2014-11-22 08:15:51 +00:00
|
|
|
|
-- absolute difference
|
2014-08-02 01:40:24 +00:00
|
|
|
|
-- --------------------------------------------
|
|
|
|
|
|
|
|
|
|
-- ### absolute difference
|
|
|
|
|
|
|
|
|
|
-- This section is still incomplete
|
|
|
|
|
|
|
|
|
|
definition dist (n m : ℕ) := (n - m) + (m - n)
|
|
|
|
|
|
2014-10-05 19:39:13 +00:00
|
|
|
|
theorem dist_comm (n m : ℕ) : dist n m = dist m n :=
|
2014-10-02 01:39:47 +00:00
|
|
|
|
!add.comm
|
2014-08-02 01:40:24 +00:00
|
|
|
|
|
2014-10-05 19:39:13 +00:00
|
|
|
|
theorem dist_self (n : ℕ) : dist n n = 0 :=
|
2014-08-02 02:29:50 +00:00
|
|
|
|
calc
|
2014-11-22 08:15:51 +00:00
|
|
|
|
(n - n) + (n - n) = 0 + (n - n) : sub_self
|
|
|
|
|
... = 0 + 0 : sub_self
|
|
|
|
|
... = 0 : rfl
|
2014-08-02 01:40:24 +00:00
|
|
|
|
|
2014-08-02 02:29:50 +00:00
|
|
|
|
theorem dist_eq_zero {n m : ℕ} (H : dist n m = 0) : n = m :=
|
2014-10-02 01:39:47 +00:00
|
|
|
|
have H2 : n - m = 0, from add.eq_zero_left H,
|
2014-08-02 02:29:50 +00:00
|
|
|
|
have H3 : n ≤ m, from sub_eq_zero_imp_le H2,
|
2014-10-02 01:39:47 +00:00
|
|
|
|
have H4 : m - n = 0, from add.eq_zero_right H,
|
2014-08-02 02:29:50 +00:00
|
|
|
|
have H5 : m ≤ n, from sub_eq_zero_imp_le H4,
|
|
|
|
|
le_antisym H3 H5
|
2014-08-02 01:40:24 +00:00
|
|
|
|
|
2014-08-02 02:29:50 +00:00
|
|
|
|
theorem dist_le {n m : ℕ} (H : n ≤ m) : dist n m = m - n :=
|
|
|
|
|
calc
|
|
|
|
|
dist n m = 0 + (m - n) : {le_imp_sub_eq_zero H}
|
2014-10-02 01:39:47 +00:00
|
|
|
|
... = m - n : !add.zero_left
|
2014-08-02 01:40:24 +00:00
|
|
|
|
|
2014-08-02 02:29:50 +00:00
|
|
|
|
theorem dist_ge {n m : ℕ} (H : n ≥ m) : dist n m = n - m :=
|
2014-10-05 19:39:13 +00:00
|
|
|
|
!dist_comm ▸ dist_le H
|
2014-08-02 01:40:24 +00:00
|
|
|
|
|
2014-10-05 19:39:13 +00:00
|
|
|
|
theorem dist_zero_right (n : ℕ) : dist n 0 = n :=
|
|
|
|
|
dist_ge !zero_le ⬝ !sub_zero_right
|
2014-08-02 01:40:24 +00:00
|
|
|
|
|
2014-10-05 19:39:13 +00:00
|
|
|
|
theorem dist_zero_left (n : ℕ) : dist 0 n = n :=
|
|
|
|
|
dist_le !zero_le ⬝ !sub_zero_right
|
2014-08-02 02:29:50 +00:00
|
|
|
|
|
|
|
|
|
theorem dist_intro {n m k : ℕ} (H : n + m = k) : dist k n = m :=
|
|
|
|
|
calc
|
|
|
|
|
dist k n = k - n : dist_ge (le_intro H)
|
2014-08-27 01:47:36 +00:00
|
|
|
|
... = m : sub_intro H
|
2014-08-02 02:29:50 +00:00
|
|
|
|
|
2014-10-05 19:39:13 +00:00
|
|
|
|
theorem dist_add_right (n k m : ℕ) : dist (n + k) (m + k) = dist n m :=
|
2014-08-02 02:29:50 +00:00
|
|
|
|
calc
|
2014-08-27 01:47:36 +00:00
|
|
|
|
dist (n + k) (m + k) = ((n+k) - (m+k)) + ((m+k)-(n+k)) : rfl
|
2014-10-05 19:39:13 +00:00
|
|
|
|
... = (n - m) + ((m + k) - (n + k)) : {!sub_add_add_right}
|
|
|
|
|
... = (n - m) + (m - n) : {!sub_add_add_right}
|
2014-08-02 01:40:24 +00:00
|
|
|
|
|
2014-10-05 19:39:13 +00:00
|
|
|
|
theorem dist_add_left (k n m : ℕ) : dist (k + n) (k + m) = dist n m :=
|
|
|
|
|
!add.comm ▸ !add.comm ▸ !dist_add_right
|
2014-08-02 01:40:24 +00:00
|
|
|
|
|
|
|
|
|
-- add_rewrite dist_self dist_add_right dist_add_left dist_zero_left dist_zero_right
|
|
|
|
|
|
2014-08-02 02:29:50 +00:00
|
|
|
|
theorem dist_ge_add_right {n m : ℕ} (H : n ≥ m) : dist n m + m = n :=
|
|
|
|
|
calc
|
|
|
|
|
dist n m + m = n - m + m : {dist_ge H}
|
2014-08-27 01:47:36 +00:00
|
|
|
|
... = n : add_sub_ge_left H
|
2014-08-02 01:40:24 +00:00
|
|
|
|
|
2014-08-02 02:29:50 +00:00
|
|
|
|
theorem dist_eq_intro {n m k l : ℕ} (H : n + m = k + l) : dist n k = dist l m :=
|
|
|
|
|
calc
|
2014-10-05 19:39:13 +00:00
|
|
|
|
dist n k = dist (n + m) (k + m) : !dist_add_right⁻¹
|
2014-08-27 01:47:36 +00:00
|
|
|
|
... = dist (k + l) (k + m) : {H}
|
2014-10-05 19:39:13 +00:00
|
|
|
|
... = dist l m : !dist_add_left
|
2014-08-02 01:40:24 +00:00
|
|
|
|
|
2014-08-02 02:29:50 +00:00
|
|
|
|
theorem dist_sub_move_add {n m : ℕ} (H : n ≥ m) (k : ℕ) : dist (n - m) k = dist n (k + m) :=
|
|
|
|
|
have H2 : n - m + (k + m) = k + n, from
|
|
|
|
|
calc
|
2014-11-22 08:15:51 +00:00
|
|
|
|
n - m + (k + m) = n - m + (m + k) : add.comm
|
|
|
|
|
... = n - m + m + k : add.assoc
|
|
|
|
|
... = n + k : {add_sub_ge_left H}
|
|
|
|
|
... = k + n : add.comm,
|
2014-08-02 02:29:50 +00:00
|
|
|
|
dist_eq_intro H2
|
2014-08-02 01:40:24 +00:00
|
|
|
|
|
2014-08-02 02:29:50 +00:00
|
|
|
|
theorem dist_sub_move_add' {k m : ℕ} (H : k ≥ m) (n : ℕ) : dist n (k - m) = dist (n + m) k :=
|
2014-10-05 19:39:13 +00:00
|
|
|
|
(dist_sub_move_add H n ▸ !dist_comm) ▸ !dist_comm
|
2014-08-02 01:40:24 +00:00
|
|
|
|
|
|
|
|
|
--triangle inequality formulated with dist
|
2014-10-05 19:39:13 +00:00
|
|
|
|
theorem triangle_inequality (n m k : ℕ) : dist n k ≤ dist n m + dist m k :=
|
2014-08-02 02:29:50 +00:00
|
|
|
|
have H : (n - m) + (m - k) + ((k - m) + (m - n)) = (n - m) + (m - n) + ((m - k) + (k - m)),
|
|
|
|
|
by simp,
|
2014-10-05 19:39:13 +00:00
|
|
|
|
H ▸ add_le !sub_triangle_inequality !sub_triangle_inequality
|
2014-08-02 02:29:50 +00:00
|
|
|
|
|
2014-10-05 19:39:13 +00:00
|
|
|
|
theorem dist_add_le_add_dist (n m k l : ℕ) : dist (n + m) (k + l) ≤ dist n k + dist m l :=
|
2014-08-02 02:29:50 +00:00
|
|
|
|
have H : dist (n + m) (k + m) + dist (k + m) (k + l) = dist n k + dist m l, from
|
2014-10-05 19:39:13 +00:00
|
|
|
|
!dist_add_left ▸ !dist_add_right ▸ rfl,
|
|
|
|
|
H ▸ !triangle_inequality
|
2014-08-02 01:40:24 +00:00
|
|
|
|
|
|
|
|
|
--interaction with multiplication
|
|
|
|
|
|
2014-10-05 19:39:13 +00:00
|
|
|
|
theorem dist_mul_left (k n m : ℕ) : dist (k * n) (k * m) = k * dist n m :=
|
2014-08-27 01:47:36 +00:00
|
|
|
|
have H : ∀n m, dist n m = n - m + (m - n), from take n m, rfl,
|
2014-08-02 02:29:50 +00:00
|
|
|
|
by simp
|
2014-08-02 01:40:24 +00:00
|
|
|
|
|
2014-10-05 19:39:13 +00:00
|
|
|
|
theorem dist_mul_right (n k m : ℕ) : dist (n * k) (m * k) = dist n m * k :=
|
2014-08-27 01:47:36 +00:00
|
|
|
|
have H : ∀n m, dist n m = n - m + (m - n), from take n m, rfl,
|
2014-08-02 02:29:50 +00:00
|
|
|
|
by simp
|
2014-08-02 01:40:24 +00:00
|
|
|
|
|
|
|
|
|
-- add_rewrite dist_mul_right dist_mul_left dist_comm
|
|
|
|
|
|
2014-08-22 23:36:47 +00:00
|
|
|
|
--needed to prove of_nat a * of_nat b = of_nat (a * b) in int
|
2014-10-05 19:39:13 +00:00
|
|
|
|
theorem dist_mul_dist (n m k l : ℕ) : dist n m * dist k l = dist (n * k + m * l) (n * l + m * k) :=
|
2014-08-02 02:29:50 +00:00
|
|
|
|
have aux : ∀k l, k ≥ l → dist n m * dist k l = dist (n * k + m * l) (n * l + m * k), from
|
|
|
|
|
take k l : ℕ,
|
|
|
|
|
assume H : k ≥ l,
|
|
|
|
|
have H2 : m * k ≥ m * l, from mul_le_left H m,
|
2014-10-05 18:36:39 +00:00
|
|
|
|
have H3 : n * l + m * k ≥ m * l, from le_trans H2 !le_add_left,
|
2014-08-02 02:29:50 +00:00
|
|
|
|
calc
|
2014-08-27 01:47:36 +00:00
|
|
|
|
dist n m * dist k l = dist n m * (k - l) : {dist_ge H}
|
2014-10-05 19:39:13 +00:00
|
|
|
|
... = dist (n * (k - l)) (m * (k - l)) : !dist_mul_right⁻¹
|
2014-08-27 01:47:36 +00:00
|
|
|
|
... = dist (n * k - n * l) (m * k - m * l) : by simp
|
|
|
|
|
... = dist (n * k) (m * k - m * l + n * l) : dist_sub_move_add (mul_le_left H n) _
|
2014-10-02 01:39:47 +00:00
|
|
|
|
... = dist (n * k) (n * l + (m * k - m * l)) : {!add.comm}
|
2014-08-27 01:47:36 +00:00
|
|
|
|
... = dist (n * k) (n * l + m * k - m * l) : {(add_sub_assoc H2 (n * l))⁻¹}
|
|
|
|
|
... = dist (n * k + m * l) (n * l + m * k) : dist_sub_move_add' H3 _,
|
2014-10-05 18:36:39 +00:00
|
|
|
|
or.elim !le_total
|
2014-10-05 19:39:13 +00:00
|
|
|
|
(assume H : k ≤ l, !dist_comm ▸ !dist_comm ▸ aux l k H)
|
2014-08-02 02:29:50 +00:00
|
|
|
|
(assume H : l ≤ k, aux k l H)
|
2014-08-02 01:40:24 +00:00
|
|
|
|
|
2014-08-20 02:32:44 +00:00
|
|
|
|
end nat
|