Library Stdlib.Numbers.Integer.Abstract.ZDivTrunc
Euclidean Division for integers (Trunc convention)
Module Type ZQuotProp
(Import A : ZAxiomsSig')
(Import B : ZMulOrderProp A)
(Import C : ZSgnAbsProp A B).
We benefit from what already exists for NZ
Module Import Private_Div.
Module Quot2Div <: NZDiv A.
Definition div := quot.
Definition modulo := A.rem.
Definition div_wd := quot_wd.
Definition mod_wd := rem_wd.
Definition div_mod := quot_rem.
Definition mod_bound_pos := rem_bound_pos.
End Quot2Div.
Module NZQuot := Nop <+ NZDivProp A Quot2Div B.
End Private_Div.
Ltac pos_or_neg a :=
let LT := fresh "LT" in
let LE := fresh "LE" in
destruct (le_gt_cases 0 a) as [LE|LT]; [|rewrite <- opp_pos_neg in LT].
Another formulation of the main equation
A few sign rules (simple ones)
Lemma rem_opp_opp : forall a b, b ~= 0 -> (-a) rem (-b) == - (a rem b).
Lemma quot_opp_l : forall a b, b ~= 0 -> (-a)÷b == -(a÷b).
Lemma quot_opp_r : forall a b, b ~= 0 -> a÷(-b) == -(a÷b).
Lemma quot_opp_opp : forall a b, b ~= 0 -> (-a)÷(-b) == a÷b.
Uniqueness theorems
Theorem quot_rem_unique : forall b q1 q2 r1 r2 : t,
(0<=r1<b \/ b<r1<=0) -> (0<=r2<b \/ b<r2<=0) ->
b*q1+r1 == b*q2+r2 -> q1 == q2 /\ r1 == r2.
Theorem quot_unique:
forall a b q r, 0<=a -> 0<=r<b -> a == b*q + r -> q == a÷b.
Theorem rem_unique:
forall a b q r, 0<=a -> 0<=r<b -> a == b*q + r -> r == a rem b.
A division by itself returns 1
A division of a small number by a bigger one yields zero.
Same situation, in term of remulo:
Lemma quot_0_l: forall a, a~=0 -> 0÷a == 0.
Lemma rem_0_l: forall a, a~=0 -> 0 rem a == 0.
Lemma quot_1_r: forall a, a÷1 == a.
Lemma rem_1_r: forall a, a rem 1 == 0.
Lemma quot_1_l: forall a, 1<a -> 1÷a == 0.
Lemma rem_1_l: forall a, 1<a -> 1 rem a == 1.
Lemma quot_mul : forall a b, b~=0 -> (a*b)÷b == a.
Lemma rem_mul : forall a b, b~=0 -> (a*b) rem b == 0.
Theorem quot_unique_exact a b q: b~=0 -> a == b*q -> q == a÷b.
The sign of a rem b is the one of a (when it's not null)
Lemma rem_nonneg : forall a b, b~=0 -> 0 <= a -> 0 <= a rem b.
Lemma rem_nonpos : forall a b, b~=0 -> a <= 0 -> a rem b <= 0.
Lemma rem_sign_mul : forall a b, b~=0 -> 0 <= (a rem b) * a.
Lemma rem_sign_nz : forall a b, b~=0 -> a rem b ~= 0 ->
sgn (a rem b) == sgn a.
Lemma rem_sign : forall a b, a~=0 -> b~=0 -> sgn (a rem b) ~= -sgn a.
Operations and absolute value
Lemma rem_abs_l : forall a b, b ~= 0 -> (abs a) rem b == abs (a rem b).
Lemma rem_abs_r : forall a b, b ~= 0 -> a rem (abs b) == a rem b.
Lemma rem_abs : forall a b, b ~= 0 -> (abs a) rem (abs b) == abs (a rem b).
Lemma quot_abs_l : forall a b, b ~= 0 -> (abs a)÷b == (sgn a)*(a÷b).
Lemma quot_abs_r : forall a b, b ~= 0 -> a÷(abs b) == (sgn b)*(a÷b).
Lemma quot_abs : forall a b, b ~= 0 -> (abs a)÷(abs b) == abs (a÷b).
We have a general bound for absolute values
Theorem rem_le: forall a b, 0<=a -> 0<b -> a rem b <= a.
Theorem quot_pos : forall a b, 0<=a -> 0<b -> 0<= a÷b.
Lemma quot_str_pos : forall a b, 0<b<=a -> 0 < a÷b.
Lemma quot_small_iff : forall a b, b~=0 -> (a÷b==0 <-> abs a < abs b).
Lemma rem_small_iff : forall a b, b~=0 -> (a rem b == a <-> abs a < abs b).
As soon as the divisor is strictly greater than 1,
the division is strictly decreasing.
le is compatible with a positive division.
With this choice of division,
rounding of quot is always done toward zero:
Lemma mul_quot_le : forall a b, 0<=a -> b~=0 -> 0 <= b*(a÷b) <= a.
Lemma mul_quot_ge : forall a b, a<=0 -> b~=0 -> a <= b*(a÷b) <= 0.
For positive numbers, considering S (a÷b) leads to an upper bound for a
Similar results with negative numbers
Lemma mul_pred_quot_lt: forall a b, a<=0 -> 0<b -> b*(P (a÷b)) < a.
Lemma mul_pred_quot_gt: forall a b, 0<=a -> b<0 -> a < b*(P (a÷b)).
Lemma mul_succ_quot_lt: forall a b, a<=0 -> b<0 -> b*(S (a÷b)) < a.
Inequality mul_quot_le is exact iff the modulo is zero.
Some additional inequalities about quot.
Theorem quot_lt_upper_bound:
forall a b q, 0<=a -> 0<b -> a < b*q -> a÷b < q.
Theorem quot_le_upper_bound:
forall a b q, 0<b -> a <= b*q -> a÷b <= q.
Theorem quot_le_lower_bound:
forall a b q, 0<b -> b*q <= a -> q <= a÷b.
A division respects opposite monotonicity for the divisor
Relations between usual operations and rem and quot
Lemma rem_add : forall a b c, c~=0 -> 0 <= (a+b*c)*a ->
(a + b * c) rem c == a rem c.
Lemma quot_add : forall a b c, c~=0 -> 0 <= (a+b*c)*a ->
(a + b * c) ÷ c == a ÷ c + b.
Lemma quot_add_l: forall a b c, b~=0 -> 0 <= (a*b+c)*c ->
(a * b + c) ÷ b == a + c ÷ b.
Cancellations.
Lemma quot_mul_cancel_r : forall a b c, b~=0 -> c~=0 ->
(a*c)÷(b*c) == a÷b.
Lemma quot_mul_cancel_l : forall a b c, b~=0 -> c~=0 ->
(c*a)÷(c*b) == a÷b.
Lemma mul_rem_distr_r: forall a b c, b~=0 -> c~=0 ->
(a*c) rem (b*c) == (a rem b) * c.
Lemma mul_rem_distr_l: forall a b c, b~=0 -> c~=0 ->
(c*a) rem (c*b) == c * (a rem b).
Operations modulo.
Theorem rem_rem: forall a n, n~=0 ->
(a rem n) rem n == a rem n.
Lemma mul_rem_idemp_l : forall a b n, n~=0 ->
((a rem n)*b) rem n == (a*b) rem n.
Lemma mul_rem_idemp_r : forall a b n, n~=0 ->
(a*(b rem n)) rem n == (a*b) rem n.
Theorem mul_rem: forall a b n, n~=0 ->
(a * b) rem n == ((a rem n) * (b rem n)) rem n.
addition and modulo
Generally speaking, unlike with other conventions, we don't have
(a+b) rem n = (a rem n + b rem n) rem n
for any a and b.
For instance, take (8 + (-10)) rem 3 = -2 whereas
(8 rem 3 + (-10 rem 3)) rem 3 = 1.
Lemma add_rem_idemp_l : forall a b n, n~=0 -> 0 <= a*b ->
((a rem n)+b) rem n == (a+b) rem n.
Lemma add_rem_idemp_r : forall a b n, n~=0 -> 0 <= a*b ->
(a+(b rem n)) rem n == (a+b) rem n.
Theorem add_rem: forall a b n, n~=0 -> 0 <= a*b ->
(a+b) rem n == (a rem n + b rem n) rem n.
Conversely, the following results need less restrictions here.
Lemma quot_quot : forall a b c, b~=0 -> c~=0 ->
(a÷b)÷c == a÷(b*c).
Lemma mod_mul_r : forall a b c, b~=0 -> c~=0 ->
a rem (b*c) == a rem b + b*((a÷b) rem c).
Lemma rem_quot: forall a b, b~=0 ->
a rem b ÷ b == 0.
A last inequality: