Library Coq.Numbers.Integer.Abstract.ZDivTrunc


Euclidean Division for integers (Trunc convention)



We use here the convention known as Trunc, or Round-Toward-Zero, where a/b is the integer with the largest absolute value to be between zero and the exact fraction. It can be summarized by:

a = bq+r /\ 0 <= |r| < |b| /\ Sign(r) = Sign(a)

This is the convention of Ocaml and many other systems (C, ASM, ...). This convention is named "T" in the following paper:

R. Boute, "The Euclidean definition of the functions div and mod", ACM Transactions on Programming Languages and Systems, Vol. 14, No.2, pp. 127-144, April 1992.

See files ZDivFloor and ZDivEucl for others conventions.

Require Import ZAxioms ZProperties NZDiv.

Module Type ZDivSpecific (Import Z:ZAxiomsSig')(Import DM : DivMod' Z).
 Axiom mod_bound : forall a b, 0<=a -> 0<b -> 0 <= a mod b < b.
 Axiom mod_opp_l : forall a b, b ~= 0 -> (-a) mod b == - (a mod b).
 Axiom mod_opp_r : forall a b, b ~= 0 -> a mod (-b) == a mod b.
End ZDivSpecific.

Module Type ZDiv (Z:ZAxiomsSig)
 := DivMod Z <+ NZDivCommon Z <+ ZDivSpecific Z.

Module Type ZDivSig := ZAxiomsExtSig <+ ZDiv.
Module Type ZDivSig' := ZAxiomsExtSig' <+ ZDiv <+ DivModNotation.

Module ZDivPropFunct (Import Z : ZDivSig')(Import ZP : ZPropSig Z).

We benefit from what already exists for NZ

 Module Import NZDivP := NZDivPropFunct Z ZP Z.

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

Lemma mod_eq :
 forall a b, b~=0 -> a mod b == a - b*(a/b).

A few sign rules (simple ones)

Lemma mod_opp_opp : forall a b, b ~= 0 -> (-a) mod (-b) == - (a mod b).

Lemma div_opp_l : forall a b, b ~= 0 -> (-a)/b == -(a/b).

Lemma div_opp_r : forall a b, b ~= 0 -> a/(-b) == -(a/b).

Lemma div_opp_opp : forall a b, b ~= 0 -> (-a)/(-b) == a/b.

The sign of a mod b is the one of a


Lemma mod_sign : forall a b, b~=0 -> 0 <= (a mod b) * a.

Uniqueness theorems

Theorem div_mod_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 div_unique:
 forall a b q r, 0<=a -> 0<=r<b -> a == b*q + r -> q == a/b.

Theorem mod_unique:
 forall a b q r, 0<=a -> 0<=r<b -> a == b*q + r -> r == a mod b.

A division by itself returns 1

Lemma div_same : forall a, a~=0 -> a/a == 1.

Lemma mod_same : forall a, a~=0 -> a mod a == 0.

A division of a small number by a bigger one yields zero.

Theorem div_small: forall a b, 0<=a<b -> a/b == 0.

Same situation, in term of modulo:

Theorem mod_small: forall a b, 0<=a<b -> a mod b == a.

Basic values of divisions and modulo.


Lemma div_0_l: forall a, a~=0 -> 0/a == 0.

Lemma mod_0_l: forall a, a~=0 -> 0 mod a == 0.

Lemma div_1_r: forall a, a/1 == a.

Lemma mod_1_r: forall a, a mod 1 == 0.

Lemma div_1_l: forall a, 1<a -> 1/a == 0.

Lemma mod_1_l: forall a, 1<a -> 1 mod a == 1.

Lemma div_mul : forall a b, b~=0 -> (a*b)/b == a.

Lemma mod_mul : forall a b, b~=0 -> (a*b) mod b == 0.

Order results about mod and div


A modulo cannot grow beyond its starting point.

Theorem mod_le: forall a b, 0<=a -> 0<b -> a mod b <= a.

Theorem div_pos : forall a b, 0<=a -> 0<b -> 0<= a/b.

Lemma div_str_pos : forall a b, 0<b<=a -> 0 < a/b.

Lemma div_small_iff : forall a b, b~=0 -> (a/b==0 <-> abs a < abs b).

Lemma mod_small_iff : forall a b, b~=0 -> (a mod b == a <-> abs a < abs b).

As soon as the divisor is strictly greater than 1, the division is strictly decreasing.

Lemma div_lt : forall a b, 0<a -> 1<b -> a/b < a.

le is compatible with a positive division.

Lemma div_le_mono : forall a b c, 0<c -> a<=b -> a/c <= b/c.

With this choice of division, rounding of div is always done toward zero:

Lemma mul_div_le : forall a b, 0<=a -> b~=0 -> 0 <= b*(a/b) <= a.

Lemma mul_div_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

Lemma mul_succ_div_gt: forall a b, 0<=a -> 0<b -> a < b*(S (a/b)).

Similar results with negative numbers

Lemma mul_pred_div_lt: forall a b, a<=0 -> 0<b -> b*(P (a/b)) < a.

Lemma mul_pred_div_gt: forall a b, 0<=a -> b<0 -> a < b*(P (a/b)).

Lemma mul_succ_div_lt: forall a b, a<=0 -> b<0 -> b*(S (a/b)) < a.

Inequality mul_div_le is exact iff the modulo is zero.

Lemma div_exact : forall a b, b~=0 -> (a == b*(a/b) <-> a mod b == 0).

Some additionnal inequalities about div.

Theorem div_lt_upper_bound:
  forall a b q, 0<=a -> 0<b -> a < b*q -> a/b < q.

Theorem div_le_upper_bound:
  forall a b q, 0<b -> a <= b*q -> a/b <= q.

Theorem div_le_lower_bound:
  forall a b q, 0<b -> b*q <= a -> q <= a/b.

A division respects opposite monotonicity for the divisor

Lemma div_le_compat_l: forall p q r, 0<=p -> 0<q<=r -> p/r <= p/q.

Relations between usual operations and mod and div


Unlike with other division conventions, some results here aren't always valid, and need to be restricted. For instance (a+b*c) mod c <> a mod c for a=9,b=-5,c=2

Lemma mod_add : forall a b c, c~=0 -> 0 <= (a+b*c)*a ->
 (a + b * c) mod c == a mod c.

Lemma div_add : forall a b c, c~=0 -> 0 <= (a+b*c)*a ->
 (a + b * c) / c == a / c + b.

Lemma div_add_l: forall a b c, b~=0 -> 0 <= (a*b+c)*c ->
 (a * b + c) / b == a + c / b.

Cancellations.

Lemma div_mul_cancel_r : forall a b c, b~=0 -> c~=0 ->
 (a*c)/(b*c) == a/b.

Lemma div_mul_cancel_l : forall a b c, b~=0 -> c~=0 ->
 (c*a)/(c*b) == a/b.

Lemma mul_mod_distr_r: forall a b c, b~=0 -> c~=0 ->
  (a*c) mod (b*c) == (a mod b) * c.

Lemma mul_mod_distr_l: forall a b c, b~=0 -> c~=0 ->
  (c*a) mod (c*b) == c * (a mod b).

Operations modulo.

Theorem mod_mod: forall a n, n~=0 ->
 (a mod n) mod n == a mod n.

Lemma mul_mod_idemp_l : forall a b n, n~=0 ->
 ((a mod n)*b) mod n == (a*b) mod n.

Lemma mul_mod_idemp_r : forall a b n, n~=0 ->
 (a*(b mod n)) mod n == (a*b) mod n.

Theorem mul_mod: forall a b n, n~=0 ->
 (a * b) mod n == ((a mod n) * (b mod n)) mod n.

addition and modulo

Generally speaking, unlike with other conventions, we don't have (a+b) mod n = (a mod n + b mod n) mod n for any a and b. For instance, take (8 + (-10)) mod 3 = -2 whereas (8 mod 3 + (-10 mod 3)) mod 3 = 1.

Lemma add_mod_idemp_l : forall a b n, n~=0 -> 0 <= a*b ->
 ((a mod n)+b) mod n == (a+b) mod n.

Lemma add_mod_idemp_r : forall a b n, n~=0 -> 0 <= a*b ->
 (a+(b mod n)) mod n == (a+b) mod n.

Theorem add_mod: forall a b n, n~=0 -> 0 <= a*b ->
 (a+b) mod n == (a mod n + b mod n) mod n.

Conversely, the following result needs less restrictions here.

Lemma div_div : forall a b c, b~=0 -> c~=0 ->
 (a/b)/c == a/(b*c).

A last inequality:

Theorem div_mul_le:
 forall a b c, 0<=a -> 0<b -> 0<=c -> c*(a/b) <= (c*a)/b.

mod is related to divisibility

Lemma mod_divides : forall a b, b~=0 ->
 (a mod b == 0 <-> exists c, a == b*c).

End ZDivPropFunct.