Library Coq.ZArith.Zdiv
Euclidean Division
Initial Contribution by Claude Marché and Xavier Urbain
The definition of the division is now in BinIntDef, the initial
specifications and properties are in BinInt.
Main division theorems
NB: many things are stated twice for compatibility reasons
For stating the fully general result, let's give a short name
to the condition on the remainder.
Another equivalent formulation:
Now comes the fully general result about Euclidean division.
The same results as before, stated separately in terms of Z.div and Z.modulo
Existence theorem
Uniqueness theorems
Basic values of divisions and modulo.
Lemma Zmod_0_l:
forall a, 0
mod a = 0.
Lemma Zmod_0_r:
forall a,
a mod 0
= 0.
Lemma Zdiv_0_l:
forall a, 0
/a = 0.
Lemma Zdiv_0_r:
forall a,
a/0
= 0.
Ltac zero_or_not a :=
destruct (
Z.eq_dec a 0);
[
subst;
rewrite ?
Zmod_0_l, ?
Zdiv_0_l, ?
Zmod_0_r, ?
Zdiv_0_r;
auto with zarith|].
Lemma Zmod_1_r:
forall a,
a mod 1
= 0.
Lemma Zdiv_1_r:
forall a,
a/1
= a.
Hint Resolve Zmod_0_l Zmod_0_r Zdiv_0_l Zdiv_0_r Zdiv_1_r Zmod_1_r
:
zarith.
Lemma Zdiv_1_l:
forall a, 1
< a -> 1
/a = 0.
Lemma Zmod_1_l:
forall a, 1
< a -> 1
mod a = 1.
Lemma Z_div_same_full :
forall a:
Z,
a<>0 ->
a/a = 1.
Lemma Z_mod_same_full :
forall a,
a mod a = 0.
Lemma Z_mod_mult :
forall a b,
(a*b) mod b = 0.
Lemma Z_div_mult_full :
forall a b:
Z,
b <> 0 ->
(a*b)/b = a.
Order results about Z.modulo and Z.div
As soon as the divisor is greater or equal than 2,
the division is strictly decreasing.
A division of a small number by a bigger one yields zero.
Same situation, in term of modulo:
Z.ge is compatible with a positive division.
Same, with Z.le.
With our choice of division, rounding of (a/b) is always done toward bottom:
The previous inequalities are exact iff the modulo is zero.
A modulo cannot grow beyond its starting point.
Some additionnal inequalities about Z.div.
A division of respect opposite monotonicity for the divisor
Relations between usual operations and Z.modulo and Z.div
Z.opp and Z.div, Z.modulo.
Due to the choice of convention for our Euclidean division,
some of the relations about Z.opp and divisions are rather complex.
Lemma Zdiv_opp_opp :
forall a b:
Z,
(-a)/(-b) = a/b.
Lemma Zmod_opp_opp :
forall a b:
Z,
(-a) mod (-b) = - (a mod b).
Lemma Z_mod_zero_opp_full :
forall a b:
Z,
a mod b = 0 ->
(-a) mod b = 0.
Lemma Z_mod_nz_opp_full :
forall a b:
Z,
a mod b <> 0 ->
(-a) mod b = b - (a mod b).
Lemma Z_mod_zero_opp_r :
forall a b:
Z,
a mod b = 0 ->
a mod (-b) = 0.
Lemma Z_mod_nz_opp_r :
forall a b:
Z,
a mod b <> 0 ->
a mod (-b) = (a mod b) - b.
Lemma Z_div_zero_opp_full :
forall a b:
Z,
a mod b = 0 ->
(-a)/b = -(a/b).
Lemma Z_div_nz_opp_full :
forall a b:
Z,
a mod b <> 0 ->
(-a)/b = -(a/b)-1.
Lemma Z_div_zero_opp_r :
forall a b:
Z,
a mod b = 0 ->
a/(-b) = -(a/b).
Lemma Z_div_nz_opp_r :
forall a b:
Z,
a mod b <> 0 ->
a/(-b) = -(a/b)-1.
Cancellations.
Operations modulo.
Theorem Zmod_mod:
forall a n,
(a mod n) mod n = a mod n.
Theorem Zmult_mod:
forall a b n,
(a * b) mod n = ((a mod n) * (b mod n)) mod n.
Theorem Zplus_mod:
forall a b n,
(a + b) mod n = (a mod n + b mod n) mod n.
Theorem Zminus_mod:
forall a b n,
(a - b) mod n = (a mod n - b mod n) mod n.
Lemma Zplus_mod_idemp_l:
forall a b n,
(a mod n + b) mod n = (a + b) mod n.
Lemma Zplus_mod_idemp_r:
forall a b n,
(b + a mod n) mod n = (b + a) mod n.
Lemma Zminus_mod_idemp_l:
forall a b n,
(a mod n - b) mod n = (a - b) mod n.
Lemma Zminus_mod_idemp_r:
forall a b n,
(a - b mod n) mod n = (a - b) mod n.
Lemma Zmult_mod_idemp_l:
forall a b n,
(a mod n * b) mod n = (a * b) mod n.
Lemma Zmult_mod_idemp_r:
forall a b n,
(b * (a mod n)) mod n = (b * a) mod n.
For a specific number N, equality modulo N is hence a nice setoid
equivalence, compatible with +, - and *.
Unfortunately, the previous result isn't always true on negative numbers.
For instance: 3/(-2)/(-2) = 1 <> 0 = 3 / (-2*-2)
A last inequality:
Z.modulo is related to divisibility (see more in Znumtheory)
Particular case : dividing by 2 is related with parity
Compatibility
Weaker results kept only for compatibility
A direct way to compute Z.modulo
Another convention is possible for division by negative numbers:
quotient is always the biggest integer smaller than or equal to a/b
remainder is hence always positive or null.
Division and modulo in Z agree with same in nat: