2015-02-25 19:39:17 +00:00
|
|
|
|
/-
|
2015-03-13 03:43:19 +00:00
|
|
|
|
Copyright (c) 2014 Jeremy Avigad. All rights reserved.
|
2015-02-25 19:39:17 +00:00
|
|
|
|
Released under Apache 2.0 license as described in the file LICENSE.
|
|
|
|
|
Author: Jeremy Avigad
|
|
|
|
|
|
2015-05-30 12:09:34 +00:00
|
|
|
|
Definitions and properties of div and mod, following the SSReflect library.
|
2015-03-13 03:43:19 +00:00
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
Following SSReflect and the SMTlib standard, we define a % b so that 0 ≤ a % b < |b| when b ≠ 0.
|
2015-02-25 19:39:17 +00:00
|
|
|
|
-/
|
|
|
|
|
import data.int.order data.nat.div
|
2015-12-28 18:30:23 +00:00
|
|
|
|
open [coercion] [reducible] nat
|
|
|
|
|
open [declaration] [class] nat (succ)
|
2015-02-25 19:39:17 +00:00
|
|
|
|
open eq.ops
|
|
|
|
|
|
|
|
|
|
namespace int
|
|
|
|
|
|
|
|
|
|
/- definitions -/
|
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
protected definition div (a b : ℤ) : ℤ :=
|
2015-02-25 19:39:17 +00:00
|
|
|
|
sign b *
|
|
|
|
|
(match a with
|
2015-10-29 19:36:26 +00:00
|
|
|
|
| of_nat m := of_nat (m / (nat_abs b))
|
|
|
|
|
| -[1+m] := -[1+ ((m:nat) / (nat_abs b))]
|
2015-02-25 19:39:17 +00:00
|
|
|
|
end)
|
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
definition int_has_div [reducible] [instance] [priority int.prio] : has_div int :=
|
|
|
|
|
has_div.mk int.div
|
2015-10-07 23:44:47 +00:00
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
lemma of_nat_div_eq (m : nat) (b : ℤ) : (of_nat m) / b = sign b * of_nat (m / (nat_abs b)) :=
|
2015-10-07 23:44:47 +00:00
|
|
|
|
rfl
|
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
lemma neg_succ_div_eq (m: nat) (b : ℤ) : -[1+m] / b = sign b * -[1+ (m / (nat_abs b))] :=
|
2015-10-09 21:17:09 +00:00
|
|
|
|
rfl
|
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
lemma div_def (a b : ℤ) : a / b =
|
2015-10-09 21:17:09 +00:00
|
|
|
|
sign b *
|
|
|
|
|
(match a with
|
2015-10-29 19:36:26 +00:00
|
|
|
|
| of_nat m := of_nat (m / (nat_abs b))
|
|
|
|
|
| -[1+m] := -[1+ ((m:nat) / (nat_abs b))]
|
2015-10-09 21:17:09 +00:00
|
|
|
|
end) :=
|
2015-10-07 23:44:47 +00:00
|
|
|
|
rfl
|
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
protected definition mod (a b : ℤ) : ℤ := a - a / b * b
|
2015-10-07 23:44:47 +00:00
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
definition int_has_mod [reducible] [instance] [priority int.prio] : has_mod int :=
|
|
|
|
|
has_mod.mk int.mod
|
2015-10-07 23:44:47 +00:00
|
|
|
|
|
2015-10-09 01:35:37 +00:00
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
lemma mod_def (a b : ℤ) : a % b = a - a / b * b :=
|
2015-10-09 01:35:37 +00:00
|
|
|
|
rfl
|
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
notation [priority int.prio] a ≡ b `[mod `:0 c:0 `]` := a % c = b % c
|
2015-03-13 03:43:19 +00:00
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
/- / -/
|
2015-02-25 19:39:17 +00:00
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
theorem of_nat_div (m n : nat) : of_nat (m / n) = (of_nat m) / (of_nat n) :=
|
2015-02-25 19:39:17 +00:00
|
|
|
|
nat.cases_on n
|
2015-10-13 20:16:19 +00:00
|
|
|
|
(begin rewrite [of_nat_div_eq, of_nat_zero, sign_zero, zero_mul, nat.div_zero] end)
|
|
|
|
|
(take (n : nat), by rewrite [of_nat_div_eq, sign_of_succ, one_mul])
|
2015-02-25 19:39:17 +00:00
|
|
|
|
|
|
|
|
|
theorem neg_succ_of_nat_div (m : nat) {b : ℤ} (H : b > 0) :
|
2015-10-29 19:36:26 +00:00
|
|
|
|
-[1+m] / b = -(m / b + 1) :=
|
2015-02-25 19:39:17 +00:00
|
|
|
|
calc
|
2015-10-29 19:36:26 +00:00
|
|
|
|
-[1+m] / b = sign b * _ : rfl
|
|
|
|
|
... = -[1+(m / (nat_abs b))] : by rewrite [sign_of_pos H, one_mul]
|
|
|
|
|
... = -(m / b + 1) : by rewrite [of_nat_div_eq, sign_of_pos H, one_mul]
|
2015-02-25 19:39:17 +00:00
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
protected theorem div_neg (a b : ℤ) : a / -b = -(a / b) :=
|
2015-10-08 18:01:57 +00:00
|
|
|
|
begin
|
|
|
|
|
induction a,
|
2015-10-29 19:36:26 +00:00
|
|
|
|
rewrite [*of_nat_div_eq, sign_neg, neg_mul_eq_neg_mul, nat_abs_neg],
|
2015-10-09 21:17:09 +00:00
|
|
|
|
rewrite [*neg_succ_div_eq, sign_neg, neg_mul_eq_neg_mul, nat_abs_neg],
|
2015-10-08 18:01:57 +00:00
|
|
|
|
end
|
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
theorem div_of_neg_of_pos {a b : ℤ} (Ha : a < 0) (Hb : b > 0) : a / b = -((-a - 1) / b + 1) :=
|
2015-10-08 18:01:57 +00:00
|
|
|
|
obtain (m : nat) (H1 : a = -[1+m]), from exists_eq_neg_succ_of_nat Ha,
|
2015-02-25 19:39:17 +00:00
|
|
|
|
calc
|
2015-10-29 19:36:26 +00:00
|
|
|
|
a / b = -(m / b + 1) : by rewrite [H1, neg_succ_of_nat_div _ Hb]
|
|
|
|
|
... = -((-a -1) / b + 1) : by rewrite [H1, neg_succ_of_nat_eq', neg_sub, sub_neg_eq_add,
|
2015-03-13 03:43:19 +00:00
|
|
|
|
add.comm 1, add_sub_cancel]
|
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
protected theorem div_nonneg {a b : ℤ} (Ha : a ≥ 0) (Hb : b ≥ 0) : a / b ≥ 0 :=
|
2015-03-13 03:43:19 +00:00
|
|
|
|
obtain (m : ℕ) (Hm : a = m), from exists_eq_of_nat Ha,
|
|
|
|
|
obtain (n : ℕ) (Hn : b = n), from exists_eq_of_nat Hb,
|
|
|
|
|
calc
|
2015-10-29 19:36:26 +00:00
|
|
|
|
a / b = m / n : by rewrite [Hm, Hn]
|
2015-10-08 18:01:57 +00:00
|
|
|
|
... ≥ 0 : by rewrite -of_nat_div; apply trivial
|
2015-03-13 03:43:19 +00:00
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
protected theorem div_nonpos {a b : ℤ} (Ha : a ≥ 0) (Hb : b ≤ 0) : a / b ≤ 0 :=
|
2015-03-13 03:43:19 +00:00
|
|
|
|
calc
|
2015-10-29 19:36:26 +00:00
|
|
|
|
a / b = -(a / -b) : by rewrite [int.div_neg, neg_neg]
|
|
|
|
|
... ≤ 0 : neg_nonpos_of_nonneg (int.div_nonneg Ha (neg_nonneg_of_nonpos Hb))
|
2015-03-13 03:43:19 +00:00
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
theorem div_neg' {a b : ℤ} (Ha : a < 0) (Hb : b > 0) : a / b < 0 :=
|
2015-07-21 16:10:56 +00:00
|
|
|
|
have -a - 1 ≥ 0, from le_sub_one_of_lt (neg_pos_of_neg Ha),
|
2015-10-29 19:36:26 +00:00
|
|
|
|
have (-a - 1) / b + 1 > 0, from lt_add_one_of_le (int.div_nonneg this (le_of_lt Hb)),
|
2015-03-13 03:43:19 +00:00
|
|
|
|
calc
|
2015-10-29 19:36:26 +00:00
|
|
|
|
a / b = -((-a - 1) / b + 1) : div_of_neg_of_pos Ha Hb
|
|
|
|
|
... < 0 : neg_neg_of_pos this
|
2015-02-25 19:39:17 +00:00
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
protected theorem zero_div (b : ℤ) : 0 / b = 0 :=
|
2015-10-13 20:16:19 +00:00
|
|
|
|
by rewrite [of_nat_div_eq, nat.zero_div, of_nat_zero, mul_zero]
|
2015-02-25 19:39:17 +00:00
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
protected theorem div_zero (a : ℤ) : a / 0 = 0 :=
|
|
|
|
|
by rewrite [div_def, sign_zero, zero_mul]
|
2015-02-25 19:39:17 +00:00
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
protected theorem div_one (a : ℤ) : a / 1 = a :=
|
2015-10-09 21:17:09 +00:00
|
|
|
|
assert (1 : int) > 0, from dec_trivial,
|
2015-03-13 03:43:19 +00:00
|
|
|
|
int.cases_on a
|
2015-10-13 00:18:10 +00:00
|
|
|
|
(take m : nat, by rewrite [-of_nat_one, -of_nat_div, nat.div_one])
|
|
|
|
|
(take m : nat, by rewrite [!neg_succ_of_nat_div this, -of_nat_one, -of_nat_div, nat.div_one])
|
2015-03-13 03:43:19 +00:00
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
theorem eq_div_mul_add_mod (a b : ℤ) : a = a / b * b + a % b :=
|
2015-02-25 19:39:17 +00:00
|
|
|
|
!add.comm ▸ eq_add_of_sub_eq rfl
|
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
theorem div_eq_zero_of_lt {a b : ℤ} : 0 ≤ a → a < b → a / b = 0 :=
|
2015-03-13 03:43:19 +00:00
|
|
|
|
int.cases_on a
|
2015-10-08 18:01:57 +00:00
|
|
|
|
(take (m : nat), assume H,
|
2015-03-13 03:43:19 +00:00
|
|
|
|
int.cases_on b
|
2015-10-08 18:01:57 +00:00
|
|
|
|
(take (n : nat),
|
2015-03-13 03:43:19 +00:00
|
|
|
|
assume H : m < n,
|
2015-10-29 19:36:26 +00:00
|
|
|
|
show m / n = 0,
|
2015-10-09 21:17:09 +00:00
|
|
|
|
by rewrite [-of_nat_div, nat.div_eq_zero_of_lt (lt_of_of_nat_lt_of_nat H)])
|
2015-10-08 18:01:57 +00:00
|
|
|
|
(take (n : nat),
|
2015-06-29 05:16:58 +00:00
|
|
|
|
assume H : m < -[1+n],
|
|
|
|
|
have H1 : ¬(m < -[1+n]), from dec_trivial,
|
2015-03-13 03:43:19 +00:00
|
|
|
|
absurd H H1))
|
2015-10-08 18:01:57 +00:00
|
|
|
|
(take (m : nat),
|
2015-06-29 05:16:58 +00:00
|
|
|
|
assume H : 0 ≤ -[1+m],
|
2015-07-21 16:10:56 +00:00
|
|
|
|
have ¬ (0 ≤ -[1+m]), from dec_trivial,
|
|
|
|
|
absurd H this)
|
2015-03-13 03:43:19 +00:00
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
theorem div_eq_zero_of_lt_abs {a b : ℤ} (H1 : 0 ≤ a) (H2 : a < abs b) : a / b = 0 :=
|
2015-03-13 03:43:19 +00:00
|
|
|
|
lt.by_cases
|
2015-07-21 16:10:56 +00:00
|
|
|
|
(suppose b < 0,
|
|
|
|
|
assert a < -b, from abs_of_neg this ▸ H2,
|
2015-03-13 03:43:19 +00:00
|
|
|
|
calc
|
2015-10-29 19:36:26 +00:00
|
|
|
|
a / b = - (a / -b) : by rewrite [int.div_neg, neg_neg]
|
|
|
|
|
... = 0 : by rewrite [div_eq_zero_of_lt H1 this, neg_zero])
|
2015-10-23 14:06:20 +00:00
|
|
|
|
(suppose b = 0, this⁻¹ ▸ !int.div_zero)
|
2015-07-21 16:10:56 +00:00
|
|
|
|
(suppose b > 0,
|
|
|
|
|
have a < b, from abs_of_pos this ▸ H2,
|
|
|
|
|
div_eq_zero_of_lt H1 this)
|
2015-03-13 03:43:19 +00:00
|
|
|
|
|
2015-10-09 21:17:09 +00:00
|
|
|
|
private theorem add_mul_div_self_aux1 {a : ℤ} {k : ℕ} (n : ℕ) (H1 : a ≥ 0) (H2 : k > 0) :
|
2015-10-29 19:36:26 +00:00
|
|
|
|
(a + n * k) / k = a / k + n :=
|
2015-10-08 18:01:57 +00:00
|
|
|
|
obtain (m : nat) (Hm : a = of_nat m), from exists_eq_of_nat H1,
|
2015-10-09 21:17:09 +00:00
|
|
|
|
begin
|
|
|
|
|
subst Hm,
|
|
|
|
|
rewrite [-of_nat_mul, -of_nat_add, -*of_nat_div, -of_nat_add, !nat.add_mul_div_self H2]
|
|
|
|
|
end
|
2015-02-25 19:39:17 +00:00
|
|
|
|
|
2015-10-09 21:17:09 +00:00
|
|
|
|
private theorem add_mul_div_self_aux2 {a : ℤ} {k : ℕ} (n : ℕ) (H1 : a < 0) (H2 : k > 0) :
|
2015-10-29 19:36:26 +00:00
|
|
|
|
(a + n * k) / k = a / k + n :=
|
2015-06-29 05:16:58 +00:00
|
|
|
|
obtain m (Hm : a = -[1+m]), from exists_eq_neg_succ_of_nat H1,
|
2015-10-09 21:17:09 +00:00
|
|
|
|
or.elim (nat.lt_or_ge m (n * k))
|
|
|
|
|
(assume m_lt_nk : m < n * k,
|
|
|
|
|
assert H3 : m + 1 ≤ n * k, from nat.succ_le_of_lt m_lt_nk,
|
2015-10-29 19:36:26 +00:00
|
|
|
|
assert H4 : m / k + 1 ≤ n,
|
2015-05-30 12:09:34 +00:00
|
|
|
|
from nat.succ_le_of_lt (nat.div_lt_of_lt_mul m_lt_nk),
|
2015-10-29 19:36:26 +00:00
|
|
|
|
have (-[1+m] + n * k) / k = -[1+m] / k + n, from calc
|
|
|
|
|
(-[1+m] + n * k) / k
|
|
|
|
|
= of_nat ((k * n - (m + 1)) / k) :
|
2015-12-06 07:27:46 +00:00
|
|
|
|
by rewrite [add.comm, neg_succ_of_nat_eq, of_nat_div, mul.comm k n,
|
2015-10-09 21:17:09 +00:00
|
|
|
|
of_nat_sub H3]
|
2015-10-29 19:36:26 +00:00
|
|
|
|
... = of_nat (n - m / k - 1) :
|
2015-10-14 19:27:09 +00:00
|
|
|
|
nat.mul_sub_div_of_lt (!nat.mul_comm ▸ m_lt_nk)
|
2015-10-29 19:36:26 +00:00
|
|
|
|
... = -[1+m] / k + n :
|
2015-10-22 20:36:45 +00:00
|
|
|
|
by rewrite [nat.sub_sub, of_nat_sub H4, int.add_comm, sub_eq_add_neg,
|
2015-10-09 21:17:09 +00:00
|
|
|
|
!neg_succ_of_nat_div (of_nat_lt_of_nat_of_lt H2),
|
|
|
|
|
of_nat_add, of_nat_div],
|
|
|
|
|
Hm⁻¹ ▸ this)
|
|
|
|
|
(assume nk_le_m : n * k ≤ m,
|
2015-10-29 19:36:26 +00:00
|
|
|
|
have -[1+m] / k + n = (-[1+m] + n * k) / k, from calc
|
|
|
|
|
-[1+m] / k + n
|
|
|
|
|
= -(of_nat ((m - n * k + n * k) / k) + 1) + n :
|
2015-10-09 21:17:09 +00:00
|
|
|
|
by rewrite [neg_succ_of_nat_div m (of_nat_lt_of_nat_of_lt H2),
|
|
|
|
|
nat.sub_add_cancel nk_le_m, of_nat_div]
|
2015-10-29 19:36:26 +00:00
|
|
|
|
... = -(of_nat ((m - n * k) / k + n) + 1) + n : nat.add_mul_div_self H2
|
|
|
|
|
... = -(of_nat (m - n * k) / k + 1) :
|
2015-05-25 10:00:41 +00:00
|
|
|
|
by rewrite [of_nat_add, *neg_add, add.right_comm, neg_add_cancel_right,
|
2015-06-27 08:47:36 +00:00
|
|
|
|
of_nat_div]
|
2015-10-29 19:36:26 +00:00
|
|
|
|
... = -[1+(m - n * k)] / k :
|
2015-05-25 11:52:20 +00:00
|
|
|
|
neg_succ_of_nat_div _ (of_nat_lt_of_nat_of_lt H2)
|
2015-10-29 19:36:26 +00:00
|
|
|
|
... = -(of_nat(m - n * k) + 1) / k : rfl
|
|
|
|
|
... = -(of_nat m - of_nat(n * k) + 1) / k : of_nat_sub nk_le_m
|
|
|
|
|
... = (-(of_nat m + 1) + n * k) / k :
|
2015-02-25 19:39:17 +00:00
|
|
|
|
by rewrite [sub_eq_add_neg, -*add.assoc, *neg_add, neg_neg, add.right_comm]
|
2015-10-29 19:36:26 +00:00
|
|
|
|
... = (-[1+m] + n * k) / k : rfl,
|
2015-10-09 21:17:09 +00:00
|
|
|
|
Hm⁻¹ ▸ this⁻¹)
|
2015-02-25 19:39:17 +00:00
|
|
|
|
|
2015-03-13 03:43:19 +00:00
|
|
|
|
private theorem add_mul_div_self_aux3 (a : ℤ) {b c : ℤ} (H1 : b ≥ 0) (H2 : c > 0) :
|
2015-10-29 19:36:26 +00:00
|
|
|
|
(a + b * c) / c = a / c + b :=
|
2015-10-09 21:17:09 +00:00
|
|
|
|
obtain (n : nat) (Hn : b = of_nat n), from exists_eq_of_nat H1,
|
|
|
|
|
obtain (k : nat) (Hk : c = of_nat k), from exists_eq_of_nat (le_of_lt H2),
|
2015-02-25 19:39:17 +00:00
|
|
|
|
have knz : k ≠ 0, from assume kz, !lt.irrefl (kz ▸ Hk ▸ H2),
|
|
|
|
|
have kgt0 : (#nat k > 0), from nat.pos_of_ne_zero knz,
|
2015-10-29 19:36:26 +00:00
|
|
|
|
have H3 : (a + n * k) / k = a / k + n, from
|
2015-02-25 19:39:17 +00:00
|
|
|
|
or.elim (lt_or_ge a 0)
|
2015-03-13 03:43:19 +00:00
|
|
|
|
(assume Ha : a < 0, add_mul_div_self_aux2 _ Ha kgt0)
|
|
|
|
|
(assume Ha : a ≥ 0, add_mul_div_self_aux1 _ Ha kgt0),
|
2015-02-25 19:39:17 +00:00
|
|
|
|
Hn⁻¹ ▸ Hk⁻¹ ▸ H3
|
|
|
|
|
|
2015-03-13 03:43:19 +00:00
|
|
|
|
private theorem add_mul_div_self_aux4 (a b : ℤ) {c : ℤ} (H : c > 0) :
|
2015-10-29 19:36:26 +00:00
|
|
|
|
(a + b * c) / c = a / c + b :=
|
2015-02-25 19:39:17 +00:00
|
|
|
|
or.elim (le.total 0 b)
|
2015-03-13 03:43:19 +00:00
|
|
|
|
(assume H1 : 0 ≤ b, add_mul_div_self_aux3 _ H1 H)
|
2015-02-25 19:39:17 +00:00
|
|
|
|
(assume H1 : 0 ≥ b,
|
|
|
|
|
eq.symm (calc
|
2015-10-29 19:36:26 +00:00
|
|
|
|
a / c + b = (a + b * c + -b * c) / c + b :
|
2015-02-25 19:39:17 +00:00
|
|
|
|
by rewrite [-neg_mul_eq_neg_mul, add_neg_cancel_right]
|
2015-10-29 19:36:26 +00:00
|
|
|
|
... = (a + b * c) / c + - b + b :
|
2015-03-13 03:43:19 +00:00
|
|
|
|
add_mul_div_self_aux3 _ (neg_nonneg_of_nonpos H1) H
|
2015-10-29 19:36:26 +00:00
|
|
|
|
... = (a + b * c) / c : neg_add_cancel_right))
|
2015-02-25 19:39:17 +00:00
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
protected theorem add_mul_div_self (a b : ℤ) {c : ℤ} (H : c ≠ 0) :
|
|
|
|
|
(a + b * c) / c = a / c + b :=
|
2015-02-25 19:39:17 +00:00
|
|
|
|
lt.by_cases
|
2015-03-13 03:43:19 +00:00
|
|
|
|
(assume H1 : 0 < c, !add_mul_div_self_aux4 H1)
|
2015-02-25 19:39:17 +00:00
|
|
|
|
(assume H1 : 0 = c, absurd H1⁻¹ H)
|
|
|
|
|
(assume H1 : 0 > c,
|
|
|
|
|
have H2 : -c > 0, from neg_pos_of_neg H1,
|
|
|
|
|
calc
|
2015-10-29 19:36:26 +00:00
|
|
|
|
(a + b * c) / c = - ((a + -b * -c) / -c) : by rewrite [int.div_neg, neg_mul_neg, neg_neg]
|
|
|
|
|
... = -(a / -c + -b) : !add_mul_div_self_aux4 H2
|
|
|
|
|
... = a / c + b : by rewrite [int.div_neg, neg_add, *neg_neg])
|
2015-02-25 19:39:17 +00:00
|
|
|
|
|
2015-10-23 14:06:20 +00:00
|
|
|
|
protected theorem add_mul_div_self_left (a : ℤ) {b : ℤ} (c : ℤ) (H : b ≠ 0) :
|
2015-10-29 19:36:26 +00:00
|
|
|
|
(a + b * c) / b = a / b + c :=
|
2015-10-23 14:06:20 +00:00
|
|
|
|
!mul.comm ▸ !int.add_mul_div_self H
|
2015-03-13 03:43:19 +00:00
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
protected theorem mul_div_cancel (a : ℤ) {b : ℤ} (H : b ≠ 0) : a * b / b = a :=
|
2015-03-13 03:43:19 +00:00
|
|
|
|
calc
|
2015-10-29 19:36:26 +00:00
|
|
|
|
a * b / b = (0 + a * b) / b : zero_add
|
|
|
|
|
... = 0 / b + a : !int.add_mul_div_self H
|
|
|
|
|
... = a : by rewrite [int.zero_div, zero_add]
|
2015-03-13 03:43:19 +00:00
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
protected theorem mul_div_cancel_left {a : ℤ} (b : ℤ) (H : a ≠ 0) : a * b / a = b :=
|
2015-10-23 14:06:20 +00:00
|
|
|
|
!mul.comm ▸ int.mul_div_cancel b H
|
2015-03-13 03:43:19 +00:00
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
protected theorem div_self {a : ℤ} (H : a ≠ 0) : a / a = 1 :=
|
2015-10-23 14:06:20 +00:00
|
|
|
|
!mul_one ▸ !int.mul_div_cancel_left H
|
2015-05-30 12:09:34 +00:00
|
|
|
|
|
|
|
|
|
/- mod -/
|
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
theorem of_nat_mod (m n : nat) : m % n = of_nat (m % n) :=
|
|
|
|
|
have H : m = of_nat (m % n) + m / n * n, from calc
|
|
|
|
|
m = of_nat (m / n * n + m % n) : nat.eq_div_mul_add_mod
|
|
|
|
|
... = of_nat (m / n) * n + of_nat (m % n) : rfl
|
|
|
|
|
... = m / n * n + of_nat (m % n) : of_nat_div
|
|
|
|
|
... = of_nat (m % n) + m / n * n : add.comm,
|
2015-05-30 12:09:34 +00:00
|
|
|
|
calc
|
2015-10-29 19:36:26 +00:00
|
|
|
|
m % n = m - m / n * n : rfl
|
|
|
|
|
... = of_nat (m % n) : sub_eq_of_eq_add H
|
2015-05-30 12:09:34 +00:00
|
|
|
|
|
|
|
|
|
theorem neg_succ_of_nat_mod (m : ℕ) {b : ℤ} (bpos : b > 0) :
|
2015-10-29 19:36:26 +00:00
|
|
|
|
-[1+m] % b = b - 1 - m % b :=
|
2015-05-30 12:09:34 +00:00
|
|
|
|
calc
|
2015-10-29 19:36:26 +00:00
|
|
|
|
-[1+m] % b = -(m + 1) - -[1+m] / b * b : rfl
|
|
|
|
|
... = -(m + 1) - -(m / b + 1) * b : neg_succ_of_nat_div _ bpos
|
|
|
|
|
... = -m + -1 + (b + m / b * b) :
|
2015-10-22 20:36:45 +00:00
|
|
|
|
by rewrite [neg_add, -neg_mul_eq_neg_mul, sub_neg_eq_add, right_distrib,
|
2015-05-30 12:09:34 +00:00
|
|
|
|
one_mul, (add.comm b)]
|
2015-10-29 19:36:26 +00:00
|
|
|
|
... = b + -1 + (-m + m / b * b) :
|
2015-05-30 12:09:34 +00:00
|
|
|
|
by rewrite [-*add.assoc, add.comm (-m), add.right_comm (-1), (add.comm b)]
|
2015-10-29 19:36:26 +00:00
|
|
|
|
... = b - 1 - m % b :
|
|
|
|
|
by rewrite [(mod_def), *sub_eq_add_neg, neg_add, neg_neg]
|
|
|
|
|
-- it seems the parser has difficulty here, because "mod" is a token?
|
2015-05-30 12:09:34 +00:00
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
theorem mod_neg (a b : ℤ) : a % -b = a % b :=
|
2015-05-30 12:09:34 +00:00
|
|
|
|
calc
|
2015-10-29 19:36:26 +00:00
|
|
|
|
a % -b = a - (a / -b) * -b : rfl
|
|
|
|
|
... = a - -(a / b) * -b : int.div_neg
|
|
|
|
|
... = a - a / b * b : neg_mul_neg
|
|
|
|
|
... = a % b : rfl
|
2015-05-30 12:09:34 +00:00
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
theorem mod_abs (a b : ℤ) : a % (abs b) = a % b :=
|
2015-05-30 12:09:34 +00:00
|
|
|
|
abs.by_cases rfl !mod_neg
|
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
theorem zero_mod (b : ℤ) : 0 % b = 0 :=
|
|
|
|
|
by rewrite [(mod_def), int.zero_div, zero_mul, sub_zero]
|
2015-10-09 21:17:09 +00:00
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
theorem mod_zero (a : ℤ) : a % 0 = a :=
|
|
|
|
|
by rewrite [(mod_def), mul_zero, sub_zero]
|
2015-05-30 12:09:34 +00:00
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
theorem mod_one (a : ℤ) : a % 1 = 0 :=
|
2015-05-30 12:09:34 +00:00
|
|
|
|
calc
|
2015-10-29 19:36:26 +00:00
|
|
|
|
a % 1 = a - a / 1 * 1 : rfl
|
2015-10-23 14:06:20 +00:00
|
|
|
|
... = 0 : by rewrite [mul_one, int.div_one, sub_self]
|
2015-05-30 12:09:34 +00:00
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
private lemma of_nat_mod_abs (m : ℕ) (b : ℤ) : m % (abs b) = of_nat (m % (nat_abs b)) :=
|
2015-05-30 12:09:34 +00:00
|
|
|
|
calc
|
2015-10-29 19:36:26 +00:00
|
|
|
|
m % (abs b) = m % (nat_abs b) : of_nat_nat_abs
|
|
|
|
|
... = of_nat (m % (nat_abs b)) : of_nat_mod
|
2015-05-30 12:09:34 +00:00
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
private lemma of_nat_mod_abs_lt (m : ℕ) {b : ℤ} (H : b ≠ 0) : m % (abs b) < (abs b) :=
|
2015-05-30 12:09:34 +00:00
|
|
|
|
have H1 : abs b > 0, from abs_pos_of_ne_zero H,
|
|
|
|
|
have H2 : (#nat nat_abs b > 0), from lt_of_of_nat_lt_of_nat (!of_nat_nat_abs⁻¹ ▸ H1),
|
|
|
|
|
calc
|
2015-10-29 19:36:26 +00:00
|
|
|
|
m % (abs b) = of_nat (m % (nat_abs b)) : of_nat_mod_abs m b
|
2015-05-30 12:09:34 +00:00
|
|
|
|
... < nat_abs b : of_nat_lt_of_nat_of_lt (!nat.mod_lt H2)
|
|
|
|
|
... = abs b : of_nat_nat_abs _
|
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
theorem mod_eq_of_lt {a b : ℤ} (H1 : 0 ≤ a) (H2 : a < b) : a % b = a :=
|
2015-10-09 21:17:09 +00:00
|
|
|
|
obtain (m : nat) (Hm : a = of_nat m), from exists_eq_of_nat H1,
|
|
|
|
|
obtain (n : nat) (Hn : b = of_nat n), from exists_eq_of_nat (le_of_lt (lt_of_le_of_lt H1 H2)),
|
2015-05-30 12:09:34 +00:00
|
|
|
|
begin
|
|
|
|
|
revert H2,
|
2015-09-12 14:00:34 +00:00
|
|
|
|
rewrite [Hm, Hn, of_nat_mod, of_nat_lt_of_nat_iff, of_nat_eq_of_nat_iff],
|
2015-05-30 12:09:34 +00:00
|
|
|
|
apply nat.mod_eq_of_lt
|
|
|
|
|
end
|
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
theorem mod_nonneg (a : ℤ) {b : ℤ} (H : b ≠ 0) : a % b ≥ 0 :=
|
2015-05-30 12:09:34 +00:00
|
|
|
|
have H1 : abs b > 0, from abs_pos_of_ne_zero H,
|
2015-10-29 19:36:26 +00:00
|
|
|
|
have H2 : a % (abs b) ≥ 0, from
|
2015-05-30 12:09:34 +00:00
|
|
|
|
int.cases_on a
|
2015-10-29 19:36:26 +00:00
|
|
|
|
(take m : nat, (of_nat_mod_abs m b)⁻¹ ▸ of_nat_nonneg (nat.mod m (nat_abs b)))
|
2015-10-09 21:17:09 +00:00
|
|
|
|
(take m : nat,
|
2015-10-29 19:36:26 +00:00
|
|
|
|
have H3 : 1 + m % (abs b) ≤ (abs b),
|
2015-05-30 12:09:34 +00:00
|
|
|
|
from (!add.comm ▸ add_one_le_of_lt (of_nat_mod_abs_lt m H)),
|
|
|
|
|
calc
|
2015-10-29 19:36:26 +00:00
|
|
|
|
-[1+m] % (abs b) = abs b - 1 - m % (abs b) : neg_succ_of_nat_mod _ H1
|
|
|
|
|
... = abs b - (1 + m % (abs b)) : by rewrite [*sub_eq_add_neg, neg_add, add.assoc]
|
|
|
|
|
... ≥ 0 : iff.mpr !sub_nonneg_iff_le H3),
|
2015-05-30 12:09:34 +00:00
|
|
|
|
!mod_abs ▸ H2
|
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
theorem mod_lt (a : ℤ) {b : ℤ} (H : b ≠ 0) : a % b < (abs b) :=
|
2015-05-30 12:09:34 +00:00
|
|
|
|
have H1 : abs b > 0, from abs_pos_of_ne_zero H,
|
2015-10-29 19:36:26 +00:00
|
|
|
|
have H2 : a % (abs b) < abs b, from
|
2015-05-30 12:09:34 +00:00
|
|
|
|
int.cases_on a
|
|
|
|
|
(take m, of_nat_mod_abs_lt m H)
|
2015-10-09 21:17:09 +00:00
|
|
|
|
(take m : nat,
|
2015-05-30 12:09:34 +00:00
|
|
|
|
have H3 : abs b ≠ 0, from assume H', H (eq_zero_of_abs_eq_zero H'),
|
2015-10-29 19:36:26 +00:00
|
|
|
|
have H4 : 1 + m % (abs b) > 0,
|
2015-10-09 21:17:09 +00:00
|
|
|
|
from add_pos_of_pos_of_nonneg dec_trivial (mod_nonneg _ H3),
|
2015-05-30 12:09:34 +00:00
|
|
|
|
calc
|
2015-10-29 19:36:26 +00:00
|
|
|
|
-[1+m] % (abs b) = abs b - 1 - m % (abs b) : neg_succ_of_nat_mod _ H1
|
|
|
|
|
... = abs b - (1 + m % (abs b)) : by rewrite [*sub_eq_add_neg, neg_add, add.assoc]
|
2015-05-30 12:09:34 +00:00
|
|
|
|
... < abs b : sub_lt_self _ H4),
|
|
|
|
|
!mod_abs ▸ H2
|
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
theorem add_mul_mod_self {a b c : ℤ} : (a + b * c) % c = a % c :=
|
2015-05-29 07:31:04 +00:00
|
|
|
|
decidable.by_cases
|
2015-10-13 20:16:19 +00:00
|
|
|
|
(assume cz : c = 0, by rewrite [cz, mul_zero, add_zero])
|
2015-10-29 19:36:26 +00:00
|
|
|
|
(assume cnz, by rewrite [(mod_def), !int.add_mul_div_self cnz, right_distrib,
|
2015-05-29 07:31:04 +00:00
|
|
|
|
sub_add_eq_sub_sub_swap, add_sub_cancel])
|
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
theorem add_mul_mod_self_left (a b c : ℤ) : (a + b * c) % b = a % b :=
|
2015-05-29 07:31:04 +00:00
|
|
|
|
!mul.comm ▸ !add_mul_mod_self
|
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
theorem add_mod_self {a b : ℤ} : (a + b) % b = a % b :=
|
2015-05-30 12:09:34 +00:00
|
|
|
|
by rewrite -(int.mul_one b) at {1}; apply add_mul_mod_self_left
|
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
theorem add_mod_self_left {a b : ℤ} : (a + b) % a = b % a :=
|
2015-05-30 12:09:34 +00:00
|
|
|
|
!add.comm ▸ !add_mod_self
|
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
theorem mod_add_mod (m n k : ℤ) : (m % n + k) % n = (m + k) % n :=
|
|
|
|
|
by rewrite [eq_div_mul_add_mod m n at {2}, add.assoc, add.comm (m / n * n), add_mul_mod_self]
|
2015-06-15 12:50:46 +00:00
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
theorem add_mod_mod (m n k : ℤ) : (m + n % k) % k = (m + n) % k :=
|
2015-06-15 12:50:46 +00:00
|
|
|
|
by rewrite [add.comm, mod_add_mod, add.comm]
|
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
theorem add_mod_eq_add_mod_right {m n k : ℤ} (i : ℤ) (H : m % n = k % n) :
|
|
|
|
|
(m + i) % n = (k + i) % n :=
|
2015-06-15 12:50:46 +00:00
|
|
|
|
by rewrite [-mod_add_mod, -mod_add_mod k, H]
|
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
theorem add_mod_eq_add_mod_left {m n k : ℤ} (i : ℤ) (H : m % n = k % n) :
|
|
|
|
|
(i + m) % n = (i + k) % n :=
|
2015-06-15 12:50:46 +00:00
|
|
|
|
by rewrite [add.comm, add_mod_eq_add_mod_right _ H, add.comm]
|
|
|
|
|
|
|
|
|
|
theorem mod_eq_mod_of_add_mod_eq_add_mod_right {m n k i : ℤ}
|
2015-10-29 19:36:26 +00:00
|
|
|
|
(H : (m + i) % n = (k + i) % n) :
|
|
|
|
|
m % n = k % n :=
|
|
|
|
|
assert H1 : (m + i + (-i)) % n = (k + i + (-i)) % n, from add_mod_eq_add_mod_right _ H,
|
2015-06-15 12:50:46 +00:00
|
|
|
|
by rewrite [*add_neg_cancel_right at H1]; apply H1
|
|
|
|
|
|
|
|
|
|
theorem mod_eq_mod_of_add_mod_eq_add_mod_left {m n k i : ℤ} :
|
2015-10-29 19:36:26 +00:00
|
|
|
|
(i + m) % n = (i + k) % n → m % n = k % n :=
|
2015-06-15 12:50:46 +00:00
|
|
|
|
by rewrite [add.comm i m, add.comm i k]; apply mod_eq_mod_of_add_mod_eq_add_mod_right
|
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
theorem mul_mod_left (a b : ℤ) : (a * b) % b = 0 :=
|
2015-05-29 07:31:04 +00:00
|
|
|
|
by rewrite [-zero_add (a * b), add_mul_mod_self, zero_mod]
|
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
theorem mul_mod_right (a b : ℤ) : (a * b) % a = 0 :=
|
2015-05-29 07:31:04 +00:00
|
|
|
|
!mul.comm ▸ !mul_mod_left
|
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
theorem mod_self {a : ℤ} : a % a = 0 :=
|
2015-03-13 03:43:19 +00:00
|
|
|
|
decidable.by_cases
|
|
|
|
|
(assume H : a = 0, H⁻¹ ▸ !mod_zero)
|
|
|
|
|
(assume H : a ≠ 0,
|
|
|
|
|
calc
|
2015-10-29 19:36:26 +00:00
|
|
|
|
a % a = a - a / a * a : rfl
|
2015-10-23 14:06:20 +00:00
|
|
|
|
... = 0 : by rewrite [!int.div_self H, one_mul, sub_self])
|
2015-03-13 03:43:19 +00:00
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
theorem mod_lt_of_pos (a : ℤ) {b : ℤ} (H : b > 0) : a % b < b :=
|
2015-03-13 03:43:19 +00:00
|
|
|
|
!abs_of_pos H ▸ !mod_lt (ne.symm (ne_of_lt H))
|
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
/- properties of / and % -/
|
2015-05-30 12:09:34 +00:00
|
|
|
|
|
2015-03-13 03:43:19 +00:00
|
|
|
|
theorem mul_div_mul_of_pos_aux {a : ℤ} (b : ℤ) {c : ℤ}
|
2015-10-29 19:36:26 +00:00
|
|
|
|
(H1 : a > 0) (H2 : c > 0) : a * b / (a * c) = b / c :=
|
2015-03-13 03:43:19 +00:00
|
|
|
|
have H3 : a * c ≠ 0, from ne.symm (ne_of_lt (mul_pos H1 H2)),
|
2015-10-29 19:36:26 +00:00
|
|
|
|
have H4 : a * (b % c) < a * c, from mul_lt_mul_of_pos_left (!mod_lt_of_pos H2) H1,
|
|
|
|
|
have H5 : a * (b % c) ≥ 0, from mul_nonneg (le_of_lt H1) (!mod_nonneg (ne.symm (ne_of_lt H2))),
|
2015-03-13 03:43:19 +00:00
|
|
|
|
calc
|
2015-10-29 19:36:26 +00:00
|
|
|
|
a * b / (a * c) = a * (b / c * c + b % c) / (a * c) : eq_div_mul_add_mod
|
2015-03-13 03:43:19 +00:00
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
... = (a * (b % c) + a * c * (b / c)) / (a * c) :
|
2015-10-23 14:24:30 +00:00
|
|
|
|
by rewrite [!add.comm, int.left_distrib, mul.comm _ c, -!mul.assoc]
|
2015-10-29 19:36:26 +00:00
|
|
|
|
... = a * (b % c) / (a * c) + b / c : !int.add_mul_div_self_left H3
|
|
|
|
|
... = 0 + b / c : {!div_eq_zero_of_lt H5 H4}
|
|
|
|
|
... = b / c : zero_add
|
2015-03-13 03:43:19 +00:00
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
theorem mul_div_mul_of_pos {a : ℤ} (b c : ℤ) (H : a > 0) : a * b / (a * c) = b / c :=
|
2015-03-13 03:43:19 +00:00
|
|
|
|
lt.by_cases
|
|
|
|
|
(assume H1 : c < 0,
|
|
|
|
|
have H2 : -c > 0, from neg_pos_of_neg H1,
|
|
|
|
|
calc
|
2015-10-29 19:36:26 +00:00
|
|
|
|
a * b / (a * c) = - (a * b / (a * -c)) :
|
2015-10-23 14:06:20 +00:00
|
|
|
|
by rewrite [-neg_mul_eq_mul_neg, int.div_neg, neg_neg]
|
2015-10-29 19:36:26 +00:00
|
|
|
|
... = - (b / -c) : mul_div_mul_of_pos_aux _ H H2
|
|
|
|
|
... = b / c : by rewrite [int.div_neg, neg_neg])
|
2015-03-13 03:43:19 +00:00
|
|
|
|
(assume H1 : c = 0,
|
|
|
|
|
calc
|
2015-10-29 19:36:26 +00:00
|
|
|
|
a * b / (a * c) = 0 : by rewrite [H1, mul_zero, int.div_zero]
|
|
|
|
|
... = b / c : by rewrite [H1, int.div_zero])
|
2015-03-13 03:43:19 +00:00
|
|
|
|
(assume H1 : c > 0,
|
|
|
|
|
mul_div_mul_of_pos_aux _ H H1)
|
|
|
|
|
|
2015-05-29 07:31:04 +00:00
|
|
|
|
theorem mul_div_mul_of_pos_left (a : ℤ) {b : ℤ} (c : ℤ) (H : b > 0) :
|
2015-10-29 19:36:26 +00:00
|
|
|
|
a * b / (c * b) = a / c :=
|
2015-03-13 03:43:19 +00:00
|
|
|
|
!mul.comm ▸ !mul.comm ▸ !mul_div_mul_of_pos H
|
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
theorem mul_mod_mul_of_pos {a : ℤ} (b c : ℤ) (H : a > 0) : a * b % (a * c) = a * (b % c) :=
|
|
|
|
|
by rewrite [(mod_def), mod_def, !mul_div_mul_of_pos H, mul_sub_left_distrib, mul.left_comm]
|
2015-03-13 03:43:19 +00:00
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
theorem lt_div_add_one_mul_self (a : ℤ) {b : ℤ} (H : b > 0) : a < (a / b + 1) * b :=
|
|
|
|
|
have H : a - a / b * b < b, from !mod_lt_of_pos H,
|
2015-03-13 03:43:19 +00:00
|
|
|
|
calc
|
2015-10-29 19:36:26 +00:00
|
|
|
|
a < a / b * b + b : iff.mpr !lt_add_iff_sub_lt_left H
|
|
|
|
|
... = (a / b + 1) * b : by rewrite [right_distrib, one_mul]
|
2015-03-13 03:43:19 +00:00
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
theorem div_le_of_nonneg_of_nonneg {a b : ℤ} (Ha : a ≥ 0) (Hb : b ≥ 0) : a / b ≤ a :=
|
2015-03-13 03:43:19 +00:00
|
|
|
|
obtain (m : ℕ) (Hm : a = m), from exists_eq_of_nat Ha,
|
|
|
|
|
obtain (n : ℕ) (Hn : b = n), from exists_eq_of_nat Hb,
|
|
|
|
|
calc
|
2015-10-29 19:36:26 +00:00
|
|
|
|
a / b = of_nat (m / n) : by rewrite [Hm, Hn, of_nat_div]
|
|
|
|
|
... ≤ m : of_nat_le_of_nat_of_le !nat.div_le_self
|
|
|
|
|
... = a : Hm
|
2015-03-13 03:43:19 +00:00
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
theorem abs_div_le_abs (a b : ℤ) : abs (a / b) ≤ abs a :=
|
|
|
|
|
have H : ∀a b, b > 0 → abs (a / b) ≤ abs a, from
|
2015-03-13 03:43:19 +00:00
|
|
|
|
take a b,
|
|
|
|
|
assume H1 : b > 0,
|
|
|
|
|
or.elim (le_or_gt 0 a)
|
|
|
|
|
(assume H2 : 0 ≤ a,
|
|
|
|
|
have H3 : 0 ≤ b, from le_of_lt H1,
|
|
|
|
|
calc
|
2015-10-29 19:36:26 +00:00
|
|
|
|
abs (a / b) = a / b : abs_of_nonneg (int.div_nonneg H2 H3)
|
|
|
|
|
... ≤ a : div_le_of_nonneg_of_nonneg H2 H3
|
|
|
|
|
... = abs a : abs_of_nonneg H2)
|
2015-03-13 03:43:19 +00:00
|
|
|
|
(assume H2 : a < 0,
|
|
|
|
|
have H3 : -a - 1 ≥ 0, from le_sub_one_of_lt (neg_pos_of_neg H2),
|
2015-10-29 19:36:26 +00:00
|
|
|
|
have H4 : (-a - 1) / b + 1 ≥ 0,
|
2015-10-23 14:06:20 +00:00
|
|
|
|
from add_nonneg (int.div_nonneg H3 (le_of_lt H1)) (of_nat_le_of_nat_of_le !nat.zero_le),
|
2015-10-29 19:36:26 +00:00
|
|
|
|
have H5 : (-a - 1) / b ≤ -a - 1, from div_le_of_nonneg_of_nonneg H3 (le_of_lt H1),
|
2015-03-13 03:43:19 +00:00
|
|
|
|
calc
|
2015-10-29 19:36:26 +00:00
|
|
|
|
abs (a / b) = abs ((-a - 1) / b + 1) : by rewrite [div_of_neg_of_pos H2 H1, abs_neg]
|
|
|
|
|
... = (-a - 1) / b + 1 : abs_of_nonneg H4
|
|
|
|
|
... ≤ -a - 1 + 1 : add_le_add_right H5 _
|
|
|
|
|
... = abs a : by rewrite [sub_add_cancel, abs_of_neg H2]),
|
2015-03-13 03:43:19 +00:00
|
|
|
|
lt.by_cases
|
|
|
|
|
(assume H1 : b < 0,
|
|
|
|
|
calc
|
2015-10-29 19:36:26 +00:00
|
|
|
|
abs (a / b) = abs (a / -b) : by rewrite [int.div_neg, abs_neg]
|
|
|
|
|
... ≤ abs a : H _ _ (neg_pos_of_neg H1))
|
2015-03-13 03:43:19 +00:00
|
|
|
|
(assume H1 : b = 0,
|
|
|
|
|
calc
|
2015-10-29 19:36:26 +00:00
|
|
|
|
abs (a / b) = 0 : by rewrite [H1, int.div_zero, abs_zero]
|
2015-03-13 03:43:19 +00:00
|
|
|
|
... ≤ abs a : abs_nonneg)
|
|
|
|
|
(assume H1 : b > 0, H _ _ H1)
|
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
theorem div_mul_cancel_of_mod_eq_zero {a b : ℤ} (H : a % b = 0) : a / b * b = a :=
|
2015-05-29 07:31:04 +00:00
|
|
|
|
by rewrite [eq_div_mul_add_mod a b at {2}, H, add_zero]
|
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
theorem mul_div_cancel_of_mod_eq_zero {a b : ℤ} (H : a % b = 0) : b * (a / b) = a :=
|
2015-05-29 07:31:04 +00:00
|
|
|
|
!mul.comm ▸ div_mul_cancel_of_mod_eq_zero H
|
|
|
|
|
|
2015-05-30 12:09:34 +00:00
|
|
|
|
/- dvd -/
|
2015-05-29 07:31:04 +00:00
|
|
|
|
|
2015-06-01 01:39:59 +00:00
|
|
|
|
theorem dvd_of_of_nat_dvd_of_nat {m n : ℕ} : of_nat m ∣ of_nat n → (#nat m ∣ n) :=
|
|
|
|
|
nat.by_cases_zero_pos n
|
2015-10-09 21:17:09 +00:00
|
|
|
|
(assume H, dvd_zero m)
|
|
|
|
|
(take n' : ℕ,
|
2015-06-01 01:39:59 +00:00
|
|
|
|
assume H1 : (#nat n' > 0),
|
|
|
|
|
have H2 : of_nat n' > 0, from of_nat_pos H1,
|
|
|
|
|
assume H3 : of_nat m ∣ of_nat n',
|
|
|
|
|
dvd.elim H3
|
|
|
|
|
(take c,
|
|
|
|
|
assume H4 : of_nat n' = of_nat m * c,
|
|
|
|
|
have H5 : c > 0, from pos_of_mul_pos_left (H4 ▸ H2) !of_nat_nonneg,
|
|
|
|
|
obtain k (H6 : c = of_nat k), from exists_eq_of_nat (le_of_lt H5),
|
2015-09-12 14:00:34 +00:00
|
|
|
|
have H7 : n' = (#nat m * k), from (of_nat.inj (H6 ▸ H4)),
|
2015-10-09 21:17:09 +00:00
|
|
|
|
dvd.intro H7⁻¹))
|
2015-06-01 01:39:59 +00:00
|
|
|
|
|
|
|
|
|
theorem of_nat_dvd_of_nat_of_dvd {m n : ℕ} (H : #nat m ∣ n) : of_nat m ∣ of_nat n :=
|
2015-10-09 21:17:09 +00:00
|
|
|
|
dvd.elim H
|
2015-06-01 01:39:59 +00:00
|
|
|
|
(take k, assume H1 : #nat n = m * k,
|
2015-09-12 14:00:34 +00:00
|
|
|
|
dvd.intro (H1⁻¹ ▸ rfl))
|
2015-06-01 01:39:59 +00:00
|
|
|
|
|
2015-10-09 21:17:09 +00:00
|
|
|
|
theorem of_nat_dvd_of_nat_iff (m n : ℕ) : of_nat m ∣ of_nat n ↔ m ∣ n :=
|
2015-06-01 01:39:59 +00:00
|
|
|
|
iff.intro dvd_of_of_nat_dvd_of_nat of_nat_dvd_of_nat_of_dvd
|
|
|
|
|
|
|
|
|
|
theorem dvd.antisymm {a b : ℤ} (H1 : a ≥ 0) (H2 : b ≥ 0) : a ∣ b → b ∣ a → a = b :=
|
|
|
|
|
begin
|
|
|
|
|
rewrite [-abs_of_nonneg H1, -abs_of_nonneg H2, -*of_nat_nat_abs],
|
2015-09-12 14:00:34 +00:00
|
|
|
|
rewrite [*of_nat_dvd_of_nat_iff, *of_nat_eq_of_nat_iff],
|
2015-06-01 01:39:59 +00:00
|
|
|
|
apply nat.dvd.antisymm
|
|
|
|
|
end
|
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
theorem dvd_of_mod_eq_zero {a b : ℤ} (H : b % a = 0) : a ∣ b :=
|
2015-05-29 07:31:04 +00:00
|
|
|
|
dvd.intro (!mul.comm ▸ div_mul_cancel_of_mod_eq_zero H)
|
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
theorem mod_eq_zero_of_dvd {a b : ℤ} (H : a ∣ b) : b % a = 0 :=
|
2015-05-29 07:31:04 +00:00
|
|
|
|
dvd.elim H (take z, assume H1 : b = a * z, H1⁻¹ ▸ !mul_mod_right)
|
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
theorem dvd_iff_mod_eq_zero (a b : ℤ) : a ∣ b ↔ b % a = 0 :=
|
2015-05-29 07:31:04 +00:00
|
|
|
|
iff.intro mod_eq_zero_of_dvd dvd_of_mod_eq_zero
|
|
|
|
|
|
|
|
|
|
definition dvd.decidable_rel [instance] : decidable_rel dvd :=
|
|
|
|
|
take a n, decidable_of_decidable_of_iff _ (iff.symm !dvd_iff_mod_eq_zero)
|
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
protected theorem div_mul_cancel {a b : ℤ} (H : b ∣ a) : a / b * b = a :=
|
2015-05-29 07:31:04 +00:00
|
|
|
|
div_mul_cancel_of_mod_eq_zero (mod_eq_zero_of_dvd H)
|
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
protected theorem mul_div_cancel' {a b : ℤ} (H : a ∣ b) : a * (b / a) = b :=
|
2015-10-23 14:06:20 +00:00
|
|
|
|
!mul.comm ▸ !int.div_mul_cancel H
|
2015-05-29 07:31:04 +00:00
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
protected theorem mul_div_assoc (a : ℤ) {b c : ℤ} (H : c ∣ b) : (a * b) / c = a * (b / c) :=
|
2015-05-29 07:31:04 +00:00
|
|
|
|
decidable.by_cases
|
2015-10-23 14:06:20 +00:00
|
|
|
|
(assume cz : c = 0, by rewrite [cz, *int.div_zero, mul_zero])
|
2015-05-29 07:31:04 +00:00
|
|
|
|
(assume cnz : c ≠ 0,
|
|
|
|
|
obtain d (H' : b = d * c), from exists_eq_mul_left_of_dvd H,
|
2015-10-23 14:06:20 +00:00
|
|
|
|
by rewrite [H', -mul.assoc, *(!int.mul_div_cancel cnz)])
|
2015-05-29 07:31:04 +00:00
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
theorem div_dvd_div {a b c : ℤ} (H1 : a ∣ b) (H2 : b ∣ c) : b / a ∣ c / a :=
|
|
|
|
|
have H3 : b = b / a * a, from (int.div_mul_cancel H1)⁻¹,
|
|
|
|
|
have H4 : c = c / a * a, from (int.div_mul_cancel (dvd.trans H1 H2))⁻¹,
|
2015-06-01 01:39:59 +00:00
|
|
|
|
decidable.by_cases
|
|
|
|
|
(assume H5 : a = 0,
|
2015-10-29 19:36:26 +00:00
|
|
|
|
have H6: c / a = 0, from (congr_arg _ H5 ⬝ !int.div_zero),
|
2015-06-01 01:39:59 +00:00
|
|
|
|
H6⁻¹ ▸ !dvd_zero)
|
|
|
|
|
(assume H5 : a ≠ 0,
|
|
|
|
|
dvd_of_mul_dvd_mul_right H5 (H3 ▸ H4 ▸ H2))
|
|
|
|
|
|
2015-10-23 14:06:20 +00:00
|
|
|
|
protected theorem div_eq_iff_eq_mul_right {a b : ℤ} (c : ℤ) (H : b ≠ 0) (H' : b ∣ a) :
|
2015-10-29 19:36:26 +00:00
|
|
|
|
a / b = c ↔ a = b * c :=
|
2015-05-29 07:31:04 +00:00
|
|
|
|
iff.intro
|
2015-10-23 14:06:20 +00:00
|
|
|
|
(assume H1, by rewrite [-H1, int.mul_div_cancel' H'])
|
|
|
|
|
(assume H1, by rewrite [H1, !int.mul_div_cancel_left H])
|
2015-05-29 07:31:04 +00:00
|
|
|
|
|
2015-10-23 14:06:20 +00:00
|
|
|
|
protected theorem div_eq_iff_eq_mul_left {a b : ℤ} (c : ℤ) (H : b ≠ 0) (H' : b ∣ a) :
|
2015-10-29 19:36:26 +00:00
|
|
|
|
a / b = c ↔ a = c * b :=
|
2015-10-23 14:06:20 +00:00
|
|
|
|
!mul.comm ▸ !int.div_eq_iff_eq_mul_right H H'
|
2015-05-29 07:31:04 +00:00
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
protected theorem eq_mul_of_div_eq_right {a b c : ℤ} (H1 : b ∣ a) (H2 : a / b = c) :
|
2015-05-29 07:31:04 +00:00
|
|
|
|
a = b * c :=
|
|
|
|
|
calc
|
2015-10-29 19:36:26 +00:00
|
|
|
|
a = b * (a / b) : int.mul_div_cancel' H1
|
|
|
|
|
... = b * c : H2
|
2015-05-29 07:31:04 +00:00
|
|
|
|
|
2015-10-23 14:06:20 +00:00
|
|
|
|
protected theorem div_eq_of_eq_mul_right {a b c : ℤ} (H1 : b ≠ 0) (H2 : a = b * c) :
|
2015-10-29 19:36:26 +00:00
|
|
|
|
a / b = c :=
|
2015-05-29 07:31:04 +00:00
|
|
|
|
calc
|
2015-10-29 19:36:26 +00:00
|
|
|
|
a / b = b * c / b : H2
|
|
|
|
|
... = c : !int.mul_div_cancel_left H1
|
2015-05-29 07:31:04 +00:00
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
protected theorem eq_mul_of_div_eq_left {a b c : ℤ} (H1 : b ∣ a) (H2 : a / b = c) :
|
2015-05-29 07:31:04 +00:00
|
|
|
|
a = c * b :=
|
2015-10-23 14:06:20 +00:00
|
|
|
|
!mul.comm ▸ !int.eq_mul_of_div_eq_right H1 H2
|
2015-05-29 07:31:04 +00:00
|
|
|
|
|
2015-10-23 14:06:20 +00:00
|
|
|
|
protected theorem div_eq_of_eq_mul_left {a b c : ℤ} (H1 : b ≠ 0) (H2 : a = c * b) :
|
2015-10-29 19:36:26 +00:00
|
|
|
|
a / b = c :=
|
2015-10-23 14:06:20 +00:00
|
|
|
|
int.div_eq_of_eq_mul_right H1 (!mul.comm ▸ H2)
|
2015-05-29 07:31:04 +00:00
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
theorem neg_div_of_dvd {a b : ℤ} (H : b ∣ a) : -a / b = -(a / b) :=
|
2015-06-08 12:43:51 +00:00
|
|
|
|
decidable.by_cases
|
2015-10-23 14:06:20 +00:00
|
|
|
|
(assume H1 : b = 0, by rewrite [H1, *int.div_zero, neg_zero])
|
2015-06-08 12:43:51 +00:00
|
|
|
|
(assume H1 : b ≠ 0,
|
|
|
|
|
dvd.elim H
|
|
|
|
|
(take c, assume H' : a = b * c,
|
2015-10-23 14:06:20 +00:00
|
|
|
|
by rewrite [H', neg_mul_eq_mul_neg, *!int.mul_div_cancel_left H1]))
|
2015-06-08 12:43:51 +00:00
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
protected theorem sign_eq_div_abs (a : ℤ) : sign a = a / (abs a) :=
|
2015-08-14 12:49:17 +00:00
|
|
|
|
decidable.by_cases
|
|
|
|
|
(suppose a = 0, by subst a)
|
|
|
|
|
(suppose a ≠ 0,
|
|
|
|
|
have abs a ≠ 0, from assume H, this (eq_zero_of_abs_eq_zero H),
|
|
|
|
|
have abs a ∣ a, from abs_dvd_of_dvd !dvd.refl,
|
2015-10-23 14:06:20 +00:00
|
|
|
|
eq.symm (iff.mpr (!int.div_eq_iff_eq_mul_left `abs a ≠ 0` this) !eq_sign_mul_abs))
|
2015-08-14 12:49:17 +00:00
|
|
|
|
|
2015-08-17 03:23:03 +00:00
|
|
|
|
theorem le_of_dvd {a b : ℤ} (bpos : b > 0) (H : a ∣ b) : a ≤ b :=
|
|
|
|
|
or.elim !le_or_gt
|
|
|
|
|
(suppose a ≤ 0, le.trans this (le_of_lt bpos))
|
|
|
|
|
(suppose a > 0,
|
|
|
|
|
obtain c (Hc : b = a * c), from exists_eq_mul_right_of_dvd H,
|
|
|
|
|
have a * c > 0, by rewrite -Hc; exact bpos,
|
2015-10-09 21:17:09 +00:00
|
|
|
|
have c > 0, from pos_of_mul_pos_left this (le_of_lt `a > 0`),
|
2015-08-17 03:23:03 +00:00
|
|
|
|
show a ≤ b, from calc
|
|
|
|
|
a = a * 1 : mul_one
|
|
|
|
|
... ≤ a * c : mul_le_mul_of_nonneg_left (add_one_le_of_lt `c > 0`) (le_of_lt `a > 0`)
|
|
|
|
|
... = b : Hc)
|
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
/- / and ordering -/
|
2015-05-29 07:31:04 +00:00
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
protected theorem div_mul_le (a : ℤ) {b : ℤ} (H : b ≠ 0) : a / b * b ≤ a :=
|
2015-05-30 12:09:34 +00:00
|
|
|
|
calc
|
2015-10-29 19:36:26 +00:00
|
|
|
|
a = a / b * b + a % b : eq_div_mul_add_mod
|
|
|
|
|
... ≥ a / b * b : le_add_of_nonneg_right (!mod_nonneg H)
|
2015-05-29 07:31:04 +00:00
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
protected theorem div_le_of_le_mul {a b c : ℤ} (H : c > 0) (H' : a ≤ b * c) : a / c ≤ b :=
|
2015-05-30 12:09:34 +00:00
|
|
|
|
le_of_mul_le_mul_right (calc
|
2015-10-29 19:36:26 +00:00
|
|
|
|
a / c * c = a / c * c + 0 : add_zero
|
|
|
|
|
... ≤ a / c * c + a % c : add_le_add_left (!mod_nonneg (ne_of_gt H))
|
|
|
|
|
... = a : eq_div_mul_add_mod
|
|
|
|
|
... ≤ b * c : H') H
|
2015-05-30 12:09:34 +00:00
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
protected theorem div_le_self (a : ℤ) {b : ℤ} (H1 : a ≥ 0) (H2 : b ≥ 0) : a / b ≤ a :=
|
2015-05-30 12:09:34 +00:00
|
|
|
|
or.elim (lt_or_eq_of_le H2)
|
|
|
|
|
(assume H3 : b > 0,
|
|
|
|
|
have H4 : b ≥ 1, from add_one_le_of_lt H3,
|
|
|
|
|
have H5 : a ≤ a * b, from calc
|
|
|
|
|
a = a * 1 : mul_one
|
|
|
|
|
... ≤ a * b : !mul_le_mul_of_nonneg_left H4 H1,
|
2015-10-23 14:06:20 +00:00
|
|
|
|
int.div_le_of_le_mul H3 H5)
|
2015-05-30 12:09:34 +00:00
|
|
|
|
(assume H3 : 0 = b,
|
2015-10-23 14:06:20 +00:00
|
|
|
|
by rewrite [-H3, int.div_zero]; apply H1)
|
2015-05-30 12:09:34 +00:00
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
protected theorem mul_le_of_le_div {a b c : ℤ} (H1 : c > 0) (H2 : a ≤ b / c) : a * c ≤ b :=
|
2015-05-30 12:09:34 +00:00
|
|
|
|
calc
|
2015-10-29 19:36:26 +00:00
|
|
|
|
a * c ≤ b / c * c : !mul_le_mul_of_nonneg_right H2 (le_of_lt H1)
|
2015-10-23 14:06:20 +00:00
|
|
|
|
... ≤ b : !int.div_mul_le (ne_of_gt H1)
|
2015-05-30 12:09:34 +00:00
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
protected theorem le_div_of_mul_le {a b c : ℤ} (H1 : c > 0) (H2 : a * c ≤ b) : a ≤ b / c :=
|
|
|
|
|
have H3 : a * c < (b / c + 1) * c, from
|
2015-05-30 12:09:34 +00:00
|
|
|
|
calc
|
2015-10-29 19:36:26 +00:00
|
|
|
|
a * c ≤ b : H2
|
|
|
|
|
... = b / c * c + b % c : eq_div_mul_add_mod
|
|
|
|
|
... < b / c * c + c : add_lt_add_left (!mod_lt_of_pos H1)
|
|
|
|
|
... = (b / c + 1) * c : by rewrite [right_distrib, one_mul],
|
2015-05-30 12:09:34 +00:00
|
|
|
|
le_of_lt_add_one (lt_of_mul_lt_mul_right H3 (le_of_lt H1))
|
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
protected theorem le_div_iff_mul_le {a b c : ℤ} (H : c > 0) : a ≤ b / c ↔ a * c ≤ b :=
|
2015-10-23 14:06:20 +00:00
|
|
|
|
iff.intro (!int.mul_le_of_le_div H) (!int.le_div_of_mul_le H)
|
2015-05-30 12:09:34 +00:00
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
protected theorem div_le_div {a b c : ℤ} (H : c > 0) (H' : a ≤ b) : a / c ≤ b / c :=
|
2015-10-23 14:06:20 +00:00
|
|
|
|
int.le_div_of_mul_le H (le.trans (!int.div_mul_le (ne_of_gt H)) H')
|
2015-05-30 12:09:34 +00:00
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
protected theorem div_lt_of_lt_mul {a b c : ℤ} (H : c > 0) (H' : a < b * c) : a / c < b :=
|
2015-05-30 12:09:34 +00:00
|
|
|
|
lt_of_mul_lt_mul_right
|
|
|
|
|
(calc
|
2015-10-29 19:36:26 +00:00
|
|
|
|
a / c * c = a / c * c + 0 : add_zero
|
|
|
|
|
... ≤ a / c * c + a % c : add_le_add_left (!mod_nonneg (ne_of_gt H))
|
|
|
|
|
... = a : eq_div_mul_add_mod
|
|
|
|
|
... < b * c : H')
|
2015-05-30 12:09:34 +00:00
|
|
|
|
(le_of_lt H)
|
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
protected theorem lt_mul_of_div_lt {a b c : ℤ} (H1 : c > 0) (H2 : a / c < b) : a < b * c :=
|
|
|
|
|
assert H3 : (a / c + 1) * c ≤ b * c,
|
2015-05-30 12:09:34 +00:00
|
|
|
|
from !mul_le_mul_of_nonneg_right (add_one_le_of_lt H2) (le_of_lt H1),
|
2015-10-29 19:36:26 +00:00
|
|
|
|
have H4 : a / c * c + c ≤ b * c, by rewrite [right_distrib at H3, one_mul at H3]; apply H3,
|
2015-05-30 12:09:34 +00:00
|
|
|
|
calc
|
2015-10-29 19:36:26 +00:00
|
|
|
|
a = a / c * c + a % c : eq_div_mul_add_mod
|
|
|
|
|
... < a / c * c + c : add_lt_add_left (!mod_lt_of_pos H1)
|
|
|
|
|
... ≤ b * c : H4
|
2015-05-29 07:31:04 +00:00
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
protected theorem div_lt_iff_lt_mul {a b c : ℤ} (H : c > 0) : a / c < b ↔ a < b * c :=
|
2015-10-23 14:06:20 +00:00
|
|
|
|
iff.intro (!int.lt_mul_of_div_lt H) (!int.div_lt_of_lt_mul H)
|
2015-05-29 07:31:04 +00:00
|
|
|
|
|
2015-10-23 14:06:20 +00:00
|
|
|
|
protected theorem div_le_iff_le_mul_of_div {a b : ℤ} (c : ℤ) (H : b > 0) (H' : b ∣ a) :
|
2015-10-29 19:36:26 +00:00
|
|
|
|
a / b ≤ c ↔ a ≤ c * b :=
|
2015-10-23 14:06:20 +00:00
|
|
|
|
by rewrite [propext (!le_iff_mul_le_mul_right H), !int.div_mul_cancel H']
|
2015-05-29 07:31:04 +00:00
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
protected theorem le_mul_of_div_le_of_div {a b c : ℤ} (H1 : b > 0) (H2 : b ∣ a) (H3 : a / b ≤ c) :
|
2015-05-30 12:09:34 +00:00
|
|
|
|
a ≤ c * b :=
|
2015-10-23 14:06:20 +00:00
|
|
|
|
iff.mp (!int.div_le_iff_le_mul_of_div H1 H2) H3
|
2015-03-13 03:43:19 +00:00
|
|
|
|
|
2015-10-29 19:36:26 +00:00
|
|
|
|
theorem div_pos_of_pos_of_dvd {a b : ℤ} (H1 : a > 0) (H2 : b ≥ 0) (H3 : b ∣ a) : a / b > 0 :=
|
2015-06-08 12:43:51 +00:00
|
|
|
|
have H4 : b ≠ 0, from
|
|
|
|
|
(assume H5 : b = 0,
|
|
|
|
|
have H6 : a = 0, from eq_zero_of_zero_dvd (H5 ▸ H3),
|
|
|
|
|
ne_of_gt H1 H6),
|
2015-10-29 19:36:26 +00:00
|
|
|
|
have H6 : (a / b) * b > 0, by rewrite (int.div_mul_cancel H3); apply H1,
|
2015-06-08 12:43:51 +00:00
|
|
|
|
pos_of_mul_pos_right H6 H2
|
|
|
|
|
|
|
|
|
|
theorem div_eq_div_of_dvd_of_dvd {a b c d : ℤ} (H1 : b ∣ a) (H2 : d ∣ c) (H3 : b ≠ 0)
|
|
|
|
|
(H4 : d ≠ 0) (H5 : a * d = b * c) :
|
2015-10-29 19:36:26 +00:00
|
|
|
|
a / b = c / d :=
|
2015-06-08 12:43:51 +00:00
|
|
|
|
begin
|
2015-10-23 14:06:20 +00:00
|
|
|
|
apply int.div_eq_of_eq_mul_right H3,
|
|
|
|
|
rewrite [-!int.mul_div_assoc H2],
|
2015-06-08 12:43:51 +00:00
|
|
|
|
apply eq.symm,
|
2015-10-23 14:06:20 +00:00
|
|
|
|
apply int.div_eq_of_eq_mul_left H4,
|
2015-06-08 12:43:51 +00:00
|
|
|
|
apply eq.symm H5
|
|
|
|
|
end
|
|
|
|
|
|
2015-02-25 19:39:17 +00:00
|
|
|
|
end int
|