Library Coq.ZArith.Zpower
Power functions over Z
Nota : this file is mostly deprecated. The definition of
Z.pow
and its usual properties are now provided by module
BinInt.Z.
Powers of 2 are also available there (see
Z.shiftl and
Z.shiftr).
Only remain here:
- Zpower_nat : a power function with a nat exponent
- old-style powers of two, such as two_p
- Zdiv_rest : a division + modulo when the divisor is a power of 2
Zpower_nat z n is the n-th power of
z when
n is an unary
integer (type
nat) and
z a signed integer (type
Z)
Zpower_nat_is_exp says Zpower_nat is a morphism for
plus : nat->nat->nat and Z.mul : Z->Z->Z
Conversions between powers of unary and binary integers
The function (Z.pow_pos z) is a morphism
for Pos.add : positive->positive->positive and Z.mul : Z->Z->Z
Powers of 2
For the powers of two, that will be widely used, a more direct
calculus is possible.
shift n m computes
2^n * m, i.e.
m shifted by
n positions
Equivalence with notions defined in BinInt
Properties of these old versions of powers of two
Division by a power of two.
To
x:Z and
p:positive,
q,
r are associated such that
x = 2^p.q + r and
0 <= r < 2^p
Invariant:
d*q + r = d´*q + r /\ d´ = 2*d /\ 0<=r<d /\ 0<=r´<d´
Definition Zdiv_rest_aux (
qrd:
Z * Z * Z) :=
let ´
(q,r,d) :=
qrd in
(match q with
|
Z0 =>
(0
, r)
|
Zpos xH =>
(0
, d + r)
|
Zpos (
xI n) =>
(Zpos n, d + r)
|
Zpos (
xO n) =>
(Zpos n, r)
|
Zneg xH =>
(-1
, d + r)
|
Zneg (
xI n) =>
(Zneg n - 1
, d + r)
|
Zneg (
xO n) =>
(Zneg n, r)
end, 2
* d).
Definition Zdiv_rest (
x:
Z) (
p:
positive) :=
let (
qr,
d) :=
Pos.iter p Zdiv_rest_aux (x, 0
, 1
) in qr.
Lemma Zdiv_rest_correct1 (
x:
Z) (
p:
positive) :
let (
_,
d) :=
Pos.iter p Zdiv_rest_aux (x, 0
, 1
) in
d = two_power_pos p.
Lemma Zdiv_rest_correct2 (
x:
Z) (
p:
positive) :
let ´
(q,r,d) :=
Pos.iter p Zdiv_rest_aux (x, 0
, 1
) in
x = q * d + r /\ 0
<= r < d.
Old-style rich specification by proof of existence
Direct correctness of Zdiv_rest
Equivalence with Z.shiftr