(************************************************************************)
(*  v      *   The Coq Proof Assistant  /  The Coq Development Team     *)
(* <O___,, * CNRS-Ecole Polytechnique-INRIA Futurs-Universite Paris Sud *)
(*   \VV/  **************************************************************)
(*    //   *      This file is distributed under the terms of the       *)
(*         *       GNU Lesser General Public License Version 2.1        *)
(************************************************************************)
(*i $Id$ i*)
(** * Normalisation functions for rational numbers. *)
Require Export Znumtheory.
Require Export Zgcd_old.
Require Export QArith_base.
(** First, a function that (tries to) build a positive back from a Z. *)
Definition Z2P (z : Z) :=
match z with
| Z0 => 1%positive
| Zpos p => p
| Zneg p => p
end.
(** A simple cancelation by powers of two *)
Fixpoint Pfactor_twos (p p':positive) {struct p} : (positive*positive) :=
match p, p' with
| xO p, xO p' => Pfactor_twos p p'
| _, _ => (p,p')
end.
Definition Qfactor_twos (q:Q) :=
let (p,q) := q in
match p with
| Z0 => 0
| Zpos p => let (p,q) := Pfactor_twos p q in (Zpos p)#q
| Zneg p => let (p,q) := Pfactor_twos p q in (Zneg p)#q
end.

Lemma Pfactor_twos_correct : forall p p',
(p*(snd (Pfactor_twos p p')))%positive =
(p'*(fst (Pfactor_twos p p')))%positive.