Built with Alectryon, running Coq+SerAPI v8.10.0+0.7.0. Coq sources are in this panel; goals and messages will appear in the other. Bubbles () indicate interactive fragments: hover for details, tap to reveal contents. Use Ctrl+↑ Ctrl+↓ to navigate, Ctrl+🖱️ to focus.
(************************************************************************) (* * The Coq Proof Assistant / The Coq Development Team *) (* v * INRIA, CNRS and contributors - Copyright 1999-2018 *) (* <O___,, * (see CREDITS file for the list of authors) *) (* \VV/ **************************************************************) (* // * This file is distributed under the terms of the *) (* * GNU Lesser General Public License Version 2.1 *) (* * (see LICENSE file for the text of the license) *) (************************************************************************) Require Import Nnat ZArith_base Lia ZArithRing Zdiv Morphisms. Local Open Scope Z_scope.
This file provides results about the Round-Toward-Zero Euclidean
division Z.quotrem, whose projections are Z.quot (noted ÷)
and Z.rem.
This division and Z.div agree only on positive numbers.
Otherwise, Z.div performs Round-Toward-Bottom (a.k.a Floor).
This Z.quot is compatible with the division of usual
programming languages such as Ocaml. In addition, it has nicer
properties with respect to opposite and other usual operations.
The definition of this division is now in file BinIntDef,
while most of the results about here are now in the main module
BinInt.Z, thanks to the generic "Numbers" layer. Remain here:
- some compatibility notation for old names.
- some extra results with less preconditions (in particular exploiting the arbitrary value of division by 0).
Notation Ndiv_Zquot := N2Z.inj_quot (only parsing). Notation Nmod_Zrem := N2Z.inj_rem (only parsing). Notation Z_quot_rem_eq := Z.quot_rem' (only parsing). Notation Zrem_lt := Z.rem_bound_abs (only parsing). Notation Z_quot_mult := Z.quot_mul (only parsing).
Particular values taken for a÷0 and (Z.rem a 0).
We avise to not rely on these arbitrary values.
a:Za ÷ 0 = 0now destruct a. Qed.a:Za ÷ 0 = 0a:ZZ.rem a 0 = anow destruct a. Qed.a:ZZ.rem a 0 = a
The following results are expressed without the b<>0 condition
whenever possible.
a:ZZ.rem 0 a = 0now destruct a. Qed.a:ZZ.rem 0 a = 0a:Z0 ÷ a = 0now destruct a. Qed. Hint Resolve Zrem_0_l Zrem_0_r Zquot_0_l Zquot_0_r Z.quot_1_r Z.rem_1_r : zarith. Ltac zero_or_not a := destruct (Z.eq_decidable a 0) as [->|?]; [rewrite ?Zquot_0_l, ?Zrem_0_l, ?Zquot_0_r, ?Zrem_0_r; auto with zarith|].a:Z0 ÷ a = 0a:ZZ.rem a a = 0a:ZZ.rem a a = 0now apply Z.rem_same. Qed.a:ZH:a <> 0Z.rem a a = 0a, b:ZZ.rem (a * b) b = 0a, b:ZZ.rem (a * b) b = 0now apply Z.rem_mul. Qed.a, b:ZH:b <> 0Z.rem (a * b) b = 0
(* The precise equalities that are invalid with "historic" Zdiv. *)a, b:Z- a ÷ b = - (a ÷ b)a, b:Z- a ÷ b = - (a ÷ b)now apply Z.quot_opp_l. Qed.a, b:ZH:b <> 0- a ÷ b = - (a ÷ b)a, b:Za ÷ - b = - (a ÷ b)a, b:Za ÷ - b = - (a ÷ b)now apply Z.quot_opp_r. Qed.a, b:ZH:b <> 0a ÷ - b = - (a ÷ b)a, b:ZZ.rem (- a) b = - Z.rem a ba, b:ZZ.rem (- a) b = - Z.rem a bnow apply Z.rem_opp_l. Qed.a, b:ZH:b <> 0Z.rem (- a) b = - Z.rem a ba, b:ZZ.rem a (- b) = Z.rem a ba, b:ZZ.rem a (- b) = Z.rem a bnow apply Z.rem_opp_r. Qed.a, b:ZH:b <> 0Z.rem a (- b) = Z.rem a ba, b:Z- a ÷ - b = a ÷ ba, b:Z- a ÷ - b = a ÷ bnow apply Z.quot_opp_opp. Qed.a, b:ZH:b <> 0- a ÷ - b = a ÷ ba, b:ZZ.rem (- a) (- b) = - Z.rem a ba, b:ZZ.rem (- a) (- b) = - Z.rem a bnow apply Z.rem_opp_opp. Qed.a, b:ZH:b <> 0Z.rem (- a) (- b) = - Z.rem a b
The sign of the remainder is the one of a. Due to the possible
nullity of a, a general result is to be stated in the following form:
a, b:Z0 <= Z.sgn (Z.rem a b) * Z.sgn aa, b:Z0 <= Z.sgn (Z.rem a b) * Z.sgn aa:Z0 <= Z.sgn a * Z.sgn aa, b:ZH:b <> 00 <= Z.sgn (Z.rem a b) * Z.sgn aapply Z.square_nonneg.a:Z0 <= Z.sgn a * Z.sgn aa, b:ZH:b <> 00 <= Z.sgn (Z.rem a b) * Z.sgn aa, b:ZH:b <> 0H0:Z.rem a b <> 00 <= Z.sgn (Z.rem a b) * Z.sgn aapply Z.square_nonneg. Qed.a, b:ZH:b <> 0H0:Z.rem a b <> 00 <= Z.sgn a * Z.sgn a
This can also be said in a simpler way:
a, b:Z0 <= Z.rem a b * aa, b:Z0 <= Z.rem a b * aa:Z0 <= a * aa, b:ZH:b <> 00 <= Z.rem a b * aapply Z.square_nonneg.a:Z0 <= a * anow apply Z.rem_sign_mul. Qed.a, b:ZH:b <> 00 <= Z.rem a b * a
Reformulation of Z.rem_bound_abs in 2 then 4 particular cases.
a, b:Z0 <= a -> b <> 0 -> 0 <= Z.rem a b < Z.abs bintros; generalize (Z.rem_nonneg a b) (Z.rem_bound_abs a b); lia. Qed.a, b:Z0 <= a -> b <> 0 -> 0 <= Z.rem a b < Z.abs ba, b:Za <= 0 -> b <> 0 -> - Z.abs b < Z.rem a b <= 0intros; generalize (Z.rem_nonpos a b) (Z.rem_bound_abs a b); lia. Qed.a, b:Za <= 0 -> b <> 0 -> - Z.abs b < Z.rem a b <= 0a, b:Z0 <= a -> 0 < b -> 0 <= Z.rem a b < bintros; generalize (Zrem_lt_pos a b); lia. Qed.a, b:Z0 <= a -> 0 < b -> 0 <= Z.rem a b < ba, b:Z0 <= a -> b < 0 -> 0 <= Z.rem a b < - bintros; generalize (Zrem_lt_pos a b); lia. Qed.a, b:Z0 <= a -> b < 0 -> 0 <= Z.rem a b < - ba, b:Za <= 0 -> 0 < b -> - b < Z.rem a b <= 0intros; generalize (Zrem_lt_neg a b); lia. Qed.a, b:Za <= 0 -> 0 < b -> - b < Z.rem a b <= 0a, b:Za <= 0 -> b < 0 -> b < Z.rem a b <= 0intros; generalize (Zrem_lt_neg a b); lia. Qed.a, b:Za <= 0 -> b < 0 -> b < Z.rem a b <= 0
Definition Remainder a b r := (0 <= a /\ 0 <= r < Z.abs b) \/ (a <= 0 /\ -Z.abs b < r <= 0). Definition Remainder_alt a b r := Z.abs r < Z.abs b /\ 0 <= r * a.forall a b r : Z, Remainder a b r <-> Remainder_alt a b rforall a b r : Z, Remainder a b r <-> Remainder_alt a b ra, b, r:ZH:0 <= aH0:0 <= rH2:r < Z.abs bZ.abs r < Z.abs ba, b, r:ZH:a <= 0H0:- Z.abs b < rH2:r <= 0Z.abs r < Z.abs ba, b, r:ZH:a <= 0H0:- Z.abs b < rH2:r <= 00 <= r * aa, b, r:ZH0:Z.abs r < Z.abs bH1:0 <= r * a0 <= a /\ 0 <= r < Z.abs b \/ a <= 0 /\ - Z.abs b < r <= 0lia.a, b, r:ZH:0 <= aH0:0 <= rH2:r < Z.abs bZ.abs r < Z.abs blia.a, b, r:ZH:a <= 0H0:- Z.abs b < rH2:r <= 0Z.abs r < Z.abs ba, b, r:ZH:a <= 0H0:- Z.abs b < rH2:r <= 00 <= r * aapply Z.mul_nonneg_nonneg; lia.a, b, r:ZH:a <= 0H0:- Z.abs b < rH2:r <= 00 <= - r * - aa, b, r:ZH0:Z.abs r < Z.abs bH1:0 <= r * a0 <= a /\ 0 <= r < Z.abs b \/ a <= 0 /\ - Z.abs b < r <= 0a, b, r:ZH0:Z.abs r < Z.abs bH1:0 <= r * a0 <= Z.sgn r * Z.sgn aa, b, r:ZH0:Z.abs r < Z.abs bH1:0 <= r * aH:0 <= Z.sgn r * Z.sgn a0 <= a /\ 0 <= r < Z.abs b \/ a <= 0 /\ - Z.abs b < r <= 0rewrite <-Z.sgn_mul, Z.sgn_nonneg; auto.a, b, r:ZH0:Z.abs r < Z.abs bH1:0 <= r * a0 <= Z.sgn r * Z.sgn adestruct r; simpl Z.sgn in *; lia. Qed.a, b, r:ZH0:Z.abs r < Z.abs bH1:0 <= r * aH:0 <= Z.sgn r * Z.sgn a0 <= a /\ 0 <= r < Z.abs b \/ a <= 0 /\ - Z.abs b < r <= 0a, b, q, r:ZRemainder a b r -> a = b * q + r -> q = a ÷ b /\ r = Z.rem a ba, b, q, r:ZRemainder a b r -> a = b * q + r -> q = a ÷ b /\ r = Z.rem a ba, b, q, r:ZH:0 <= aH0:0 <= r < Z.abs bH1:a = b * q + rq = a ÷ b /\ r = Z.rem a ba, b, q, r:ZH:a <= 0H0:- Z.abs b < r <= 0H1:a = b * q + rq = a ÷ b /\ r = Z.rem a ba, b, q, r:ZH:0 <= aH0:0 <= r < Z.abs bH1:a = b * q + r0 <= Z.rem a b < Z.abs ba, b, q, r:ZH:0 <= aH0:0 <= r < Z.abs bH1:a = b * q + rb * q + r = b * (a ÷ b) + Z.rem a ba, b, q, r:ZH:a <= 0H0:- Z.abs b < r <= 0H1:a = b * q + rq = a ÷ b /\ r = Z.rem a ba, b, q, r:ZH:0 <= aH0:0 <= r < Z.abs bH1:a = b * q + rb <> 0a, b, q, r:ZH:0 <= aH0:0 <= r < Z.abs bH1:a = b * q + rb * q + r = b * (a ÷ b) + Z.rem a ba, b, q, r:ZH:a <= 0H0:- Z.abs b < r <= 0H1:a = b * q + rq = a ÷ b /\ r = Z.rem a ba, b, q, r:ZH:0 <= aH0:0 <= r < Z.abs bH1:a = b * q + rb * q + r = b * (a ÷ b) + Z.rem a ba, b, q, r:ZH:a <= 0H0:- Z.abs b < r <= 0H1:a = b * q + rq = a ÷ b /\ r = Z.rem a ba, b, q, r:ZH:a <= 0H0:- Z.abs b < r <= 0H1:a = b * q + rq = a ÷ b /\ r = Z.rem a ba, b, q, r:ZH:a <= 0H0:- Z.abs b < r <= 0H1:a = b * q + rq = - - a ÷ b /\ r = Z.rem (- - a) ba, b, q, r:ZH:a <= 0H0:- Z.abs b < r <= 0H1:a = b * q + rq = - (- a ÷ b) /\ r = - Z.rem (- a) ba, b, q, r:ZH:a <= 0H0:- Z.abs b < r <= 0H1:a = b * q + r(0 <= - r < Z.abs b -> 0 <= Z.rem (- a) b < Z.abs b -> b * - q + - r = b * (- a ÷ b) + Z.rem (- a) b -> - q = - a ÷ b /\ - r = Z.rem (- a) b) -> q = - (- a ÷ b) /\ r = - Z.rem (- a) ba, b, q, r:ZH:a <= 0H0:- Z.abs b < r <= 0H1:a = b * q + r(0 <= - a -> b <> 0 -> 0 <= Z.rem (- a) b < Z.abs b) -> (0 <= - r < Z.abs b -> 0 <= Z.rem (- a) b < Z.abs b -> b * - q + - r = b * (- a ÷ b) + Z.rem (- a) b -> - q = - a ÷ b /\ - r = Z.rem (- a) b) -> q = - (- a ÷ b) /\ r = - Z.rem (- a) blia. Qed.a, b, q, r:ZH:a <= 0H0:- Z.abs b < r <= 0H1:a = b * q + r(0 <= - a -> b <> 0 -> 0 <= Z.rem (- a) b < Z.abs b) -> (0 <= - r < Z.abs b -> 0 <= Z.rem (- a) b < Z.abs b -> - a = - a -> - q = - a ÷ b /\ - r = Z.rem (- a) b) -> q = - (- a ÷ b) /\ r = - Z.rem (- a) ba, b, q, r:ZRemainder a b r -> a = b * q + r -> q = a ÷ bintros; destruct (Zquot_mod_unique_full a b q r); auto. Qed.a, b, q, r:ZRemainder a b r -> a = b * q + r -> q = a ÷ ba, b, q, r:ZRemainder a b r -> a = b * q + r -> r = Z.rem a bintros; destruct (Zquot_mod_unique_full a b q r); auto. Qed.a, b, q, r:ZRemainder a b r -> a = b * q + r -> r = Z.rem a b
(* Division of positive numbers is positive. *)a, b:Z0 <= a -> 0 <= b -> 0 <= a ÷ ba, b:Z0 <= a -> 0 <= b -> 0 <= a ÷ ba, b:ZH:0 <= aH0:0 <= b0 <= a ÷ bapply Z.quot_pos; auto with zarith. Qed.a, b:ZH:0 <= aH0:0 <= bH1:b <> 00 <= a ÷ b
As soon as the divisor is greater or equal than 2,
the division is strictly decreasing.
a, b:Z0 < a -> 2 <= b -> a ÷ b < aa, b:Z0 < a -> 2 <= b -> a ÷ b < aapply Z.quot_lt; auto with zarith. Qed.a, b:ZH:0 < aH0:2 <= ba ÷ b < a
≤ is compatible with a positive division.
a, b, c:Z0 <= c -> a <= b -> a ÷ c <= b ÷ ca, b, c:Z0 <= c -> a <= b -> a ÷ c <= b ÷ ca, b, c:ZH:0 <= cH0:a <= ba ÷ c <= b ÷ capply Z.quot_le_mono; auto with zarith. Qed.a, b, c:ZH:0 <= cH0:a <= bH1:c <> 0a ÷ c <= b ÷ c
With our choice of division, rounding of (a÷b) is always done toward 0:
a, b:Z0 <= a -> 0 <= b * (a ÷ b) <= aa, b:Z0 <= a -> 0 <= b * (a ÷ b) <= aa, b:ZH:0 <= a0 <= b * (a ÷ b) <= aapply Z.mul_quot_le; auto with zarith. Qed.a, b:ZH:0 <= aH0:b <> 00 <= b * (a ÷ b) <= aa, b:Za <= 0 -> a <= b * (a ÷ b) <= 0a, b:Za <= 0 -> a <= b * (a ÷ b) <= 0a, b:ZH:a <= 0a <= b * (a ÷ b) <= 0apply Z.mul_quot_ge; auto with zarith. Qed.a, b:ZH:a <= 0H0:b <> 0a <= b * (a ÷ b) <= 0
The previous inequalities between b*(a÷b) and a are exact
iff the modulo is zero.
a, b:Za = b * (a ÷ b) <-> Z.rem a b = 0a, b:Za = b * (a ÷ b) <-> Z.rem a b = 0a, b:Za = b * (a ÷ b) <-> Z.rem a b = 0a:Za = 0 * 0 <-> a = 0a, b:ZH:b <> 0a = b * (a ÷ b) <-> Z.rem a b = 0apply Z.quot_exact; auto. Qed.a, b:ZH:b <> 0a = b * (a ÷ b) <-> Z.rem a b = 0
A modulo cannot grow beyond its starting point.
a, b:Z0 <= a -> 0 <= b -> Z.rem a b <= aa, b:Z0 <= a -> 0 <= b -> Z.rem a b <= aa, b:ZH:0 <= aH0:0 <= bZ.rem a b <= aapply Z.rem_le; auto with zarith. Qed.a, b:ZH:0 <= aH0:0 <= bH1:b <> 0Z.rem a b <= a
Some additional inequalities about Zdiv.
forall a b q : Z, 0 < b -> a <= q * b -> a ÷ b <= qintros a b q; rewrite Z.mul_comm; apply Z.quot_le_upper_bound. Qed.forall a b q : Z, 0 < b -> a <= q * b -> a ÷ b <= qforall a b q : Z, 0 <= a -> 0 < b -> a < q * b -> a ÷ b < qintros a b q; rewrite Z.mul_comm; apply Z.quot_lt_upper_bound. Qed.forall a b q : Z, 0 <= a -> 0 < b -> a < q * b -> a ÷ b < qforall a b q : Z, 0 < b -> q * b <= a -> q <= a ÷ bintros a b q; rewrite Z.mul_comm; apply Z.quot_le_lower_bound. Qed.forall a b q : Z, 0 < b -> q * b <= a -> q <= a ÷ bforall a b : Z, 0 <= Z.sgn (a ÷ b) * Z.sgn a * Z.sgn bdestruct a as [ |a|a]; destruct b as [ |b|b]; simpl; auto with zarith; unfold Z.quot; simpl; destruct N.pos_div_eucl; simpl; destruct n; simpl; auto with zarith. Qed.forall a b : Z, 0 <= Z.sgn (a ÷ b) * Z.sgn a * Z.sgn b
First, a result that used to be always valid with Zdiv,
but must be restricted here.
For instance, now (9+(-5)*2) rem 2 = -1 <> 1 = 9 rem 2
forall a b c : Z, 0 <= (a + b * c) * a -> Z.rem (a + b * c) c = Z.rem a cforall a b c : Z, 0 <= (a + b * c) * a -> Z.rem (a + b * c) c = Z.rem a ca, b, c:ZH:0 <= (a + b * c) * aZ.rem (a + b * c) c = Z.rem a capply Z.rem_add; auto with zarith. Qed.a, b, c:ZH:0 <= (a + b * c) * aH0:c <> 0Z.rem (a + b * c) c = Z.rem a cforall a b c : Z, 0 <= (a + b * c) * a -> c <> 0 -> (a + b * c) ÷ c = a ÷ c + bforall a b c : Z, 0 <= (a + b * c) * a -> c <> 0 -> (a + b * c) ÷ c = a ÷ c + bapply Z.quot_add; auto with zarith. Qed.a, b, c:ZH:0 <= (a + b * c) * aH0:c <> 0(a + b * c) ÷ c = a ÷ c + bforall a b c : Z, 0 <= (a * b + c) * c -> b <> 0 -> b <> 0 -> (a * b + c) ÷ b = a + c ÷ bforall a b c : Z, 0 <= (a * b + c) * c -> b <> 0 -> b <> 0 -> (a * b + c) ÷ b = a + c ÷ bapply Z.quot_add_l; auto with zarith. Qed.a, b, c:ZH:0 <= (a * b + c) * cH0, H1:b <> 0(a * b + c) ÷ b = a + c ÷ b
Cancellations.
forall a b c : Z, c <> 0 -> a * c ÷ (b * c) = a ÷ bforall a b c : Z, c <> 0 -> a * c ÷ (b * c) = a ÷ ba, b, c:ZH:c <> 0a * c ÷ (b * c) = a ÷ bapply Z.quot_mul_cancel_r; auto. Qed.a, b, c:ZH:c <> 0H0:b <> 0a * c ÷ (b * c) = a ÷ bforall a b c : Z, c <> 0 -> c * a ÷ (c * b) = a ÷ bforall a b c : Z, c <> 0 -> c * a ÷ (c * b) = a ÷ ba, b, c:ZH:c <> 0c * a ÷ (c * b) = a ÷ ba, b, c:ZH:c <> 0c * a ÷ (b * c) = a ÷ ba, b, c:ZH:c <> 0H0:b <> 0c * a ÷ (b * c) = a ÷ bapply Z.quot_mul_cancel_l; auto. Qed.a, b, c:ZH:c <> 0H0:b <> 0c * a ÷ (c * b) = a ÷ bforall a b c : Z, Z.rem (c * a) (c * b) = c * Z.rem a bforall a b c : Z, Z.rem (c * a) (c * b) = c * Z.rem a ba, b, c:ZZ.rem (c * a) (c * b) = c * Z.rem a ba, b, c:ZH:c <> 0Z.rem (c * a) (c * b) = c * Z.rem a ba, b, c:ZH:c <> 0Z.rem (c * a) (b * c) = c * Z.rem a ba, b, c:ZH:c <> 0H0:b <> 0Z.rem (c * a) (b * c) = c * Z.rem a bapply Z.mul_rem_distr_l; auto. Qed.a, b, c:ZH:c <> 0H0:b <> 0Z.rem (c * a) (c * b) = c * Z.rem a bforall a b c : Z, Z.rem (a * c) (b * c) = Z.rem a b * cforall a b c : Z, Z.rem (a * c) (b * c) = Z.rem a b * ca, b, c:ZZ.rem (a * c) (b * c) = Z.rem a b * ca, b, c:ZH:b <> 0Z.rem (a * c) (b * c) = Z.rem a b * ca, b, c:ZH:b <> 0Z.rem (a * c) (c * b) = Z.rem a b * ca, b, c:ZH:b <> 0H0:c <> 0Z.rem (a * c) (c * b) = Z.rem a b * capply Z.mul_rem_distr_r; auto. Qed.a, b, c:ZH:b <> 0H0:c <> 0Z.rem (a * c) (b * c) = Z.rem a b * c
Operations modulo.
forall a n : Z, Z.rem (Z.rem a n) n = Z.rem a nforall a n : Z, Z.rem (Z.rem a n) n = Z.rem a na, n:ZZ.rem (Z.rem a n) n = Z.rem a napply Z.rem_rem; auto. Qed.a, n:ZH:n <> 0Z.rem (Z.rem a n) n = Z.rem a nforall a b n : Z, Z.rem (a * b) n = Z.rem (Z.rem a n * Z.rem b n) nforall a b n : Z, Z.rem (a * b) n = Z.rem (Z.rem a n * Z.rem b n) na, b, n:ZZ.rem (a * b) n = Z.rem (Z.rem a n * Z.rem b n) napply Z.mul_rem; auto. Qed.a, b, n:ZH:n <> 0Z.rem (a * b) n = Z.rem (Z.rem a n * Z.rem b n) n
addition and modulo
Generally speaking, unlike with Zdiv, 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.
forall a b n : Z, 0 <= a * b -> Z.rem (a + b) n = Z.rem (Z.rem a n + Z.rem b n) nforall a b n : Z, 0 <= a * b -> Z.rem (a + b) n = Z.rem (Z.rem a n + Z.rem b n) na, b, n:ZH:0 <= a * bZ.rem (a + b) n = Z.rem (Z.rem a n + Z.rem b n) napply Z.add_rem; auto. Qed.a, b, n:ZH:0 <= a * bH0:n <> 0Z.rem (a + b) n = Z.rem (Z.rem a n + Z.rem b n) nforall a b n : Z, 0 <= a * b -> Z.rem (Z.rem a n + b) n = Z.rem (a + b) nforall a b n : Z, 0 <= a * b -> Z.rem (Z.rem a n + b) n = Z.rem (a + b) na, b, n:ZH:0 <= a * bZ.rem (Z.rem a n + b) n = Z.rem (a + b) napply Z.add_rem_idemp_l; auto. Qed.a, b, n:ZH:0 <= a * bH0:n <> 0Z.rem (Z.rem a n + b) n = Z.rem (a + b) nforall a b n : Z, 0 <= a * b -> Z.rem (b + Z.rem a n) n = Z.rem (b + a) nforall a b n : Z, 0 <= a * b -> Z.rem (b + Z.rem a n) n = Z.rem (b + a) na, b, n:ZH:0 <= a * bZ.rem (b + Z.rem a n) n = Z.rem (b + a) na, b, n:ZH:0 <= a * bH0:n <> 0Z.rem (b + Z.rem a n) n = Z.rem (b + a) nrewrite Z.mul_comm; auto. Qed.a, b, n:ZH:0 <= a * bH0:n <> 00 <= b * aforall a b n : Z, Z.rem (Z.rem a n * b) n = Z.rem (a * b) nforall a b n : Z, Z.rem (Z.rem a n * b) n = Z.rem (a * b) na, b, n:ZZ.rem (Z.rem a n * b) n = Z.rem (a * b) napply Z.mul_rem_idemp_l; auto. Qed.a, b, n:ZH:n <> 0Z.rem (Z.rem a n * b) n = Z.rem (a * b) nforall a b n : Z, Z.rem (b * Z.rem a n) n = Z.rem (b * a) nforall a b n : Z, Z.rem (b * Z.rem a n) n = Z.rem (b * a) na, b, n:ZZ.rem (b * Z.rem a n) n = Z.rem (b * a) napply Z.mul_rem_idemp_r; auto. Qed.a, b, n:ZH:n <> 0Z.rem (b * Z.rem a n) n = Z.rem (b * a) n
Unlike with Zdiv, the following result is true without restrictions.
forall a b c : Z, a ÷ b ÷ c = a ÷ (b * c)forall a b c : Z, a ÷ b ÷ c = a ÷ (b * c)a, b, c:Za ÷ b ÷ c = a ÷ (b * c)a, b, c:ZH:b <> 0a ÷ b ÷ c = a ÷ (b * c)a, b, c:ZH:b <> 0a ÷ b ÷ c = a ÷ (c * b)a, b, c:ZH:b <> 0H0:c <> 0a ÷ b ÷ c = a ÷ (c * b)apply Z.quot_quot; auto. Qed.a, b, c:ZH:b <> 0H0:c <> 0a ÷ b ÷ c = a ÷ (b * c)
A last inequality:
forall a b c : Z, 0 <= a -> 0 <= b -> 0 <= c -> c * (a ÷ b) <= c * a ÷ bforall a b c : Z, 0 <= a -> 0 <= b -> 0 <= c -> c * (a ÷ b) <= c * a ÷ ba, b, c:ZH:0 <= aH0:0 <= bH1:0 <= cc * (a ÷ b) <= c * a ÷ bapply Z.quot_mul_le; auto with zarith. Qed.a, b, c:ZH:0 <= aH0:0 <= bH1:0 <= cH2:b <> 0c * (a ÷ b) <= c * a ÷ b
Z.rem is related to divisibility (see more in Znumtheory)
forall a b : Z, Z.rem a b = 0 <-> (exists c : Z, a = b * c)forall a b : Z, Z.rem a b = 0 <-> (exists c : Z, a = b * c)a, b:ZZ.rem a b = 0 <-> (exists c : Z, a = b * c)a:Za = 0 <-> (exists c : Z, a = 0 * c)a, b:ZH:b <> 0Z.rem a b = 0 <-> (exists c : Z, a = b * c)a, b:ZH:b <> 0Z.rem a b = 0 <-> (exists c : Z, a = b * c)split; intros (c,Hc); exists c; subst; auto with zarith. Qed.a, b:ZH:b <> 0(b | a) <-> (exists c : Z, a = b * c)
Particular case : dividing by 2 is related with parity
forall a : Z, Remainder a 2 (if Z.odd a then Z.sgn a else 0)forall a : Z, Remainder a 2 (if Z.odd a then Z.sgn a else 0)Remainder 0 2 (if Z.odd 0 then Z.sgn 0 else 0)p:positiveRemainder (Z.pos p) 2 (if Z.odd (Z.pos p) then Z.sgn (Z.pos p) else 0)p:positiveRemainder (Z.neg p) 2 (if Z.odd (Z.neg p) then Z.sgn (Z.neg p) else 0)Remainder 0 2 0p:positiveRemainder (Z.pos p) 2 (if Z.odd (Z.pos p) then Z.sgn (Z.pos p) else 0)p:positiveRemainder (Z.neg p) 2 (if Z.odd (Z.neg p) then Z.sgn (Z.neg p) else 0)0 <= 0 /\ 0 <= 0 < Z.abs 2p:positiveRemainder (Z.pos p) 2 (if Z.odd (Z.pos p) then Z.sgn (Z.pos p) else 0)p:positiveRemainder (Z.neg p) 2 (if Z.odd (Z.neg p) then Z.sgn (Z.neg p) else 0)0 <= 0 /\ 0 <= 0 < 2p:positiveRemainder (Z.pos p) 2 (if Z.odd (Z.pos p) then Z.sgn (Z.pos p) else 0)p:positiveRemainder (Z.neg p) 2 (if Z.odd (Z.neg p) then Z.sgn (Z.neg p) else 0)p:positiveRemainder (Z.pos p) 2 (if Z.odd (Z.pos p) then Z.sgn (Z.pos p) else 0)p:positiveRemainder (Z.neg p) 2 (if Z.odd (Z.neg p) then Z.sgn (Z.neg p) else 0)p:positive0 <= Z.pos p /\ 0 <= (if Z.odd (Z.pos p) then Z.sgn (Z.pos p) else 0) < Z.abs 2p:positiveRemainder (Z.neg p) 2 (if Z.odd (Z.neg p) then Z.sgn (Z.neg p) else 0)p:positiveRemainder (Z.neg p) 2 (if Z.odd (Z.neg p) then Z.sgn (Z.neg p) else 0)destruct p; simpl; split; now auto with zarith. Qed.p:positiveZ.neg p <= 0 /\ - Z.abs 2 < (if Z.odd (Z.neg p) then Z.sgn (Z.neg p) else 0) <= 0forall a : Z, Z.rem a 2 = (if Z.odd a then Z.sgn a else 0)forall a : Z, Z.rem a 2 = (if Z.odd a then Z.sgn a else 0)a:ZZ.rem a 2 = (if Z.odd a then Z.sgn a else 0)a:Z(if Z.odd a then Z.sgn a else 0) = Z.rem a 2a:ZRemainder a 2 (if Z.odd a then Z.sgn a else 0)a:Za = 2 * Z.quot2 a + (if Z.odd a then Z.sgn a else 0)apply Zquot2_odd_eqn. Qed.a:Za = 2 * Z.quot2 a + (if Z.odd a then Z.sgn a else 0)forall a : Z, Z.rem a 2 = (if Z.even a then 0 else Z.sgn a)forall a : Z, Z.rem a 2 = (if Z.even a then 0 else Z.sgn a)a:ZZ.rem a 2 = (if Z.even a then 0 else Z.sgn a)now destruct Z.even. Qed.a:Z(if negb (Z.even a) then Z.sgn a else 0) = (if Z.even a then 0 else Z.sgn a)forall a : Z, Z.even a = (Z.rem a 2 =? 0)forall a : Z, Z.even a = (Z.rem a 2 =? 0)a:ZZ.even a = (Z.rem a 2 =? 0)destruct a as [ |p|p]; trivial; now destruct p. Qed.a:ZZ.even a = ((if Z.even a then 0 else Z.sgn a) =? 0)forall a : Z, Z.odd a = negb (Z.rem a 2 =? 0)forall a : Z, Z.odd a = negb (Z.rem a 2 =? 0)a:ZZ.odd a = negb (Z.rem a 2 =? 0)destruct a as [ |p|p]; trivial; now destruct p. Qed.a:ZZ.odd a = negb ((if Z.odd a then Z.sgn a else 0) =? 0)
They agree at least on positive numbers:
forall a b : Z, 0 <= a -> 0 < b -> a ÷ b = a / b /\ Z.rem a b = a mod bforall a b : Z, 0 <= a -> 0 < b -> a ÷ b = a / b /\ Z.rem a b = a mod ba, b:ZH:0 <= aH0:0 < ba ÷ b = a / b /\ Z.rem a b = a mod ba, b:ZH:0 <= aH0:0 < b0 <= Z.rem a b < Z.abs ba, b:ZH:0 <= aH0:0 < b0 <= a mod b < Z.abs ba, b:ZH:0 <= aH0:0 < bb * (a ÷ b) + Z.rem a b = b * (a / b) + a mod ba, b:ZH:0 <= aH0:0 < b0 <= a mod b < Z.abs ba, b:ZH:0 <= aH0:0 < bb * (a ÷ b) + Z.rem a b = b * (a / b) + a mod ba, b:ZH:0 <= aH0:0 < bb * (a ÷ b) + Z.rem a b = b * (a / b) + a mod bsymmetry; apply Z.quot_rem; auto with *. Qed.a, b:ZH:0 <= aH0:0 < bb * (a ÷ b) + Z.rem a b = aforall a b : Z, 0 <= a -> 0 <= b -> a ÷ b = a / bforall a b : Z, 0 <= a -> 0 <= b -> a ÷ b = a / ba, b:ZHa:0 <= aHb:0 <= ba ÷ b = a / ba, b:ZHa:0 <= aHb:0 < ba ÷ b = a / ba, b:ZHa:0 <= aHb:0 = ba ÷ b = a / bgeneralize (Zquotrem_Zdiv_eucl_pos a b Ha Hb); intuition.a, b:ZHa:0 <= aHb:0 < ba ÷ b = a / bsubst; now rewrite Zquot_0_r, Zdiv_0_r. Qed.a, b:ZHa:0 <= aHb:0 = ba ÷ b = a / bforall a b : Z, 0 <= a -> 0 < b -> Z.rem a b = a mod bintros a b Ha Hb; generalize (Zquotrem_Zdiv_eucl_pos a b Ha Hb); intuition. Qed.forall a b : Z, 0 <= a -> 0 < b -> Z.rem a b = a mod b
Modulos are null at the same places
forall a b : Z, b <> 0 -> Z.rem a b = 0 <-> a mod b = 0forall a b : Z, b <> 0 -> Z.rem a b = 0 <-> a mod b = 0rewrite Zrem_divides, Zmod_divides; intuition. Qed.a, b:ZH:b <> 0Z.rem a b = 0 <-> a mod b = 0