(** * A direct way to compute Z.modulo *)
Definition Zmod' a b :=
match a with
| Z0 => 0
| Zpos a' =>
match b with
| Z0 => a
| Zpos _ => Zmod_POS a' b
| Zneg b' =>
let r := Zmod_POS a' (Zpos b') in
match r  with Z0 =>  0 |  _  =>   b + r end
end
| Zneg a' =>
match b with
| Z0 => a
| Zpos _ =>
let r := Zmod_POS a' b in
match r with Z0 =>  0 | _  =>  b - r end
| Zneg b' => - (Zmod_POS a' (Zpos b'))
end
end.

(** 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. *)
Theorem Zdiv_eucl_extended :
forall b:Z,
b <> 0 ->
forall a:Z,
{qr : Z * Z | let (q, r) := qr in a = b * q + r /\ 0 <= r < Z.abs b}.