Require Import ZArith.
Require Import NArith.
Require Import NAux.
Require Export Replace2.
Require Import P.

Require Import Reals.

(* Definition of the opposite for nat *)
Definition Natopp := (fun x:nat => 0%nat).

(* Definition of the opposite for N *)
Definition Nopp := (fun x:N => 0%N).

(* Auxillary functions for Z *)

Definition is_Z0 := (Zeq_bool 0).
Definition is_Z1 := (Zeq_bool 1).
Definition is_Zpos := (Zle_bool 0).
Definition is_Zdiv :=
  fun x y => if Zeq_bool x Z0 then false else Zeq_bool Z0 (Zmod y x).
Definition Zgcd :=
 fun x y => (if (is_Zdiv x y) then x else if (is_Zdiv y x) then y else 1%Z).

(* Check if a nat is a number *)
Ltac is_NatCst p :=
  match p with
  | O => constr:(true)
  | S ?p' => is_NatCst p'
  | _ => constr:(false)
end.

(* Convert a Z into a nat if it is a number *)
Ltac NatCst t :=
  match is_NatCst t with
  | false => constr:(false)
  | _ => let res := eval compute in (Z_of_nat t) in constr:(res)
end.


(* Check if a number is a positive *)
Ltac is_PCst p :=
  match p with
  | xH => constr:(true)
  | xO ?p' => is_PCst p'
  | xI ?p' => is_PCst p'
  | _ => constr:(false)
end.

(* Check if a N is a number *)
Ltac is_NCst p :=
  match p with
  | N0 => constr:(true)
  | Npos ?p' => is_PCst p'
  | _ => constr:(false)
end.

(* Convert a Z into a nat if it is a number *)
Ltac NCst t :=
  match is_NCst t with
  | false => constr:(false)
  | _ => let res := eval compute in (Z_of_N t) in constr:(res)
end.

(* If a number is an integer return itself otherwise false *)
Ltac ZCst t :=
  match t with
  | Z0 => constr:(t)
  | Zpos ?p => match is_PCst p with
               | false => constr:(false)
               | _ => constr:(t)
               end
  | Zneg ?p => match is_PCst p with
               | false => constr:(false)
               | _ => constr:(t)
               end
  | _ => constr:(false)
  end.

(* Check if a number is an integer *)
Ltac is_ZCst t := match t with
                | Z0 => constr:(true)
                | Zpos ?p => is_PCst p
                | Zneg ?p => is_PCst p
                | _ => constr:(false) end.


(* Turn a positive into a real *)
Fixpoint P2R (z: positive) {struct z}: R :=
  match z with
     xH => 1%R
  | (xO xH) => 2%R
  | (xI xH) => 3%R
  | (xO z1) => (2*(P2R z1))%R
  | (xI z1) => (1+2*(P2R z1))%R
 end.

(* Turn an integer into a real *)
Definition Z2R (z: Z): R :=
  match z with
     Z0 => 0%R
  | (Zpos z1) => (P2R z1)%R
  | (Zneg z1) => (-(P2R z1))%R
 end.

(* Turn a R when possible into a Z *)
Ltac RCst t :=
  match t with
   | R0 => constr:(Z0)
   | R1 => constr:(Zpos xH)
   | Rplus ?e1 ?e2 =>
       match (RCst e1) with
        false => constr:(false)
      | ?e3 => match (RCst e2) with
                 false => constr:(false)
              |  ?e4 =>  eval vm_compute in (Zplus e3  e4)
              end
      end
   | Rminus ?e1 ?e2 =>
       match (RCst e1) with
        false => constr:(false)
      | ?e3 => match (RCst e2) with
                 false => constr:(false)
              |  ?e4 => eval vm_compute in (Zminus e3  e4)
              end
      end
   | Rmult ?e1 ?e2 =>
       match (RCst e1) with
        false => constr:(false)
      | ?e3 => match (RCst e2) with
                 false => constr:(false)
              |  ?e4 => eval vm_compute in (Zmult e3  e4)
              end
      end
   | Ropp ?e1 =>
       match (RCst e1) with
        false => constr:(false)
      | ?e3 => eval vm_compute in (Z.opp e3)
      end
   | IZR ?e1 =>
       match (ZCst e1) with
        false => constr:(false)
      | ?e3 => e3
      end

   | _ => constr:(false)
 end.


(* Remove the Z.abs_nat of a number, unfortunately stops at
   the first Z.abs_nat x where x is not a number *)

Ltac clean_zabs term :=
  match term with
   context id [(Z.abs_nat ?X)] =>
     match is_ZCst X with
       true =>
         let x := eval vm_compute in (Z.abs_nat X) in
         let y := context id [x] in
           clean_zabs y
     | false => term
     end
    | _ => term
  end.

(* Remove the Z.abs_N of a number, unfortunately stops at
   the first Z.abs_nat x where x is not a number *)

Ltac clean_zabs_N term :=
  match term with
   context id [(Z.abs_N ?X)] =>
     match is_ZCst X with
       true =>
         let x := eval vm_compute in (Z.abs_N X) in
         let y := context id [x] in
           clean_zabs_N y
     | false => term
     end
    | _ => term
  end.

(* Equality test for Ltac *)

Ltac eqterm t1 t2 :=
  match constr:((t1,t2)) with (?X, ?X) => true | _ => false end.

(* For replace *)

Theorem trans_equal_r : forall (A: Set) (x y z:A), y = z -> x = y -> x = z.
(* Goal: forall (A : Set) (x y z : A) (_ : @eq A y z) (_ : @eq A x y), @eq A x z *)
intros; apply trans_equal with y; auto.
Qed.

(* Theorems for nat *)

Open Scope nat_scope.

Theorem plus_eq_compat_l: forall a b c, b = c -> a + b = a + c.
(* Goal: forall (a b c : nat) (_ : @eq nat b c), @eq nat (Init.Nat.add a b) (Init.Nat.add a c) *)
intros; apply f_equal2 with (f := plus); auto.
Qed.

Theorem plus_neg_compat_l: forall a b c, b <> c -> a + b <> a + c.
(* Goal: forall (a b c : R) (_ : not (@eq R b c)), not (@eq R (Rplus a b) (Rplus a c)) *)
intros a b c H H1; case H.
(* Goal: @eq nat b c *)
apply plus_reg_l with a; auto.
Qed.

Theorem plus_ge_compat_l: forall n m p : nat, n >= m -> p + n >= p + m.
(* Goal: forall (n m p : nat) (_ : ge n m), ge (Init.Nat.add p n) (Init.Nat.add p m) *)
intros n m p H; unfold ge; apply plus_le_compat_l; auto.
Qed.

Theorem plus_neg_reg_l: forall a b c,  a + b <> a + c -> b <> c.
(* Goal: forall (a b c : R) (_ : not (@eq R (Rplus a b) (Rplus a c))), not (@eq R b c) *)
intros a b c H H1; case H; subst; auto.
Qed.

Theorem plus_ge_reg_l: forall n m p : nat, p + n >= p + m -> n >= m.
(* Goal: forall (n m p : nat) (_ : ge (Init.Nat.add p n) (Init.Nat.add p m)), ge n m *)
intros n m p H; unfold ge; apply plus_le_reg_l with p; auto.
Qed.

(* For replace *)
Theorem eq_lt_trans_l : forall x y z, (x = z) -> (x < y) -> (z < y).
(* Goal: forall (x y z : R) (_ : @eq R y z) (_ : Rge x y), Rge x z *)
intros x y z H; rewrite H; auto.
Qed.
Theorem eq_lt_trans_r : forall x y z, (y = z) -> (x < y) -> (x < z).
(* Goal: forall (x y z : R) (_ : @eq R y z) (_ : Rge x y), Rge x z *)
intros x y z H; rewrite H; auto.
Qed.
Theorem eq_gt_trans_l : forall x y z, (x = z) -> (x > y) -> (z > y).
(* Goal: forall (x y z : R) (_ : @eq R y z) (_ : Rge x y), Rge x z *)
intros x y z H; rewrite H; auto.
Qed.
Theorem eq_gt_trans_r : forall x y z, (y = z) -> (x > y) -> (x > z).
(* Goal: forall (x y z : R) (_ : @eq R y z) (_ : Rge x y), Rge x z *)
intros x y z H; rewrite H; auto.
Qed.
Theorem eq_le_trans_l : forall x y z, (x = z) -> (x <= y) -> (z <= y).
(* Goal: forall (x y z : R) (_ : @eq R y z) (_ : Rge x y), Rge x z *)
intros x y z H; rewrite H; auto.
Qed.
Theorem eq_le_trans_r : forall x y z, (y = z) -> (x <= y) -> (x <= z).
(* Goal: forall (x y z : R) (_ : @eq R y z) (_ : Rge x y), Rge x z *)
intros x y z H; rewrite H; auto.
Qed.
Theorem eq_ge_trans_l : forall x y z, (x = z) -> (x >= y) -> (z >= y).
(* Goal: forall (x y z : R) (_ : @eq R y z) (_ : Rge x y), Rge x z *)
intros x y z H; rewrite H; auto.
Qed.
Theorem eq_ge_trans_r : forall x y z, (y = z) -> (x >= y) -> (x >= z).
(* Goal: forall (x y z : R) (_ : @eq R y z) (_ : Rge x y), Rge x z *)
intros x y z H; rewrite H; auto.
Qed.

Theorem ge_trans: forall x y z, (x >= z) -> (z >= y) -> (x >= y).
(* Goal: forall (x y z : nat) (_ : ge x z) (_ : ge z y), ge x y *)
intros x y z H1 H2; red; apply le_trans with z; auto.
Qed.

Close Scope nat_scope.

(* Theorems for N *)

Open Scope N_scope.

Theorem Nplus_eq_compat_l: forall a b c, b = c -> a + b = a + c.
(* Goal: forall (a b c : N) (_ : @eq N b c), @eq N (N.add a b) (N.add a c) *)
intros; apply f_equal2 with (f:= Nplus); auto.
Qed.

Theorem Nplus_neg_compat_l: forall a b c, b <> c -> a + b <> a + c.
(* Goal: forall (a b c : N) (_ : not (@eq N b c)), not (@eq N (N.add a b) (N.add a c)) *)
intros a b c H1 H2; case H1.
(* Goal: @eq N b c *)
apply Nplus_reg_l with a; auto.
Qed.

Theorem Nplus_lt_compat_l: forall n m p, n < m -> p + n < p + m.
(* Goal: forall (n m p : N) (_ : N.ge n m), N.ge (N.add p n) (N.add p m) *)
intros; to_nat; auto with arith.
Qed.

Theorem Nplus_gt_compat_l: forall n m p, n > m -> p + n > p + m.
(* Goal: forall (n m p : N) (_ : N.ge n m), N.ge (N.add p n) (N.add p m) *)
intros; to_nat; auto with arith.
Qed.

Theorem Nplus_le_compat_l: forall n m p, n <= m -> p + n <= p + m.
(* Goal: forall (n m p : N) (_ : N.ge n m), N.ge (N.add p n) (N.add p m) *)
intros; to_nat; auto with arith.
Qed.

Theorem Nplus_ge_compat_l: forall n m p, n >= m -> p + n >= p + m.
(* Goal: forall (n m p : N) (_ : N.ge n m), N.ge (N.add p n) (N.add p m) *)
intros; to_nat; auto with arith.
Qed.

Theorem Nplus_neg_reg_l: forall a b c,  a + b <> a + c -> b <> c.
(* Goal: forall (a b c : N) (_ : not (@eq N (N.add a b) (N.add a c))), not (@eq N b c) *)
intros a b c H H1; case H; apply f_equal2 with (f:= Nplus); auto.
Qed.

Theorem Nplus_lt_reg_l: forall n m p, p + n < p + m -> n < m.
(* Goal: forall (n m p : N) (_ : N.lt (N.add p n) (N.add p m)), N.lt n m *)
intros; to_nat; apply plus_lt_reg_l with nn1; auto with arith.
Qed.

Theorem Nplus_gt_reg_l: forall n m p, p + n > p + m -> n > m.
(* Goal: forall (n m p : N) (_ : N.gt (N.add p n) (N.add p m)), N.gt n m *)
intros; to_nat; apply plus_gt_reg_l with nn1; auto with arith.
Qed.

Theorem Nplus_le_reg_l: forall n m p, p + n <= p + m -> n <= m.
(* Goal: forall (n m p : N) (_ : N.le (N.add p n) (N.add p m)), N.le n m *)
intros; to_nat; apply plus_le_reg_l with nn1; auto with arith.
Qed.

Theorem Nplus_ge_reg_l: forall n m p, p + n >= p + m -> n >= m.
(* Goal: forall (n m p : N) (_ : N.ge (N.add p n) (N.add p m)), N.ge n m *)
intros; to_nat; apply plus_ge_reg_l with nn1; auto with arith.
Qed.

(* For replace *)
Theorem Neq_lt_trans_l : forall x y z, (x = z) -> (x < y) -> (z < y).
(* Goal: forall (x y z : N) (_ : @eq N y z) (_ : N.lt x y), N.lt x z *)
intros; subst; auto.
Qed.

Theorem Neq_lt_trans_r : forall x y z, (y = z) -> (x < y) -> (x < z).
(* Goal: forall (x y z : N) (_ : @eq N y z) (_ : N.lt x y), N.lt x z *)
intros; subst; auto.
Qed.

Theorem Neq_gt_trans_l : forall x y z, (x = z) -> (x > y) -> (z > y).
(* Goal: forall (x y z : R) (_ : @eq R y z) (_ : Rge x y), Rge x z *)
intros x y z H; rewrite H; auto.
Qed.
Theorem Neq_gt_trans_r : forall x y z, (y = z) -> (x > y) -> (x > z).
(* Goal: forall (x y z : R) (_ : @eq R y z) (_ : Rge x y), Rge x z *)
intros x y z H; rewrite H; auto.
Qed.
Theorem Neq_le_trans_l : forall x y z, (x = z) -> (x <= y) -> (z <= y).
(* Goal: forall (x y z : R) (_ : @eq R y z) (_ : Rge x y), Rge x z *)
intros x y z H; rewrite H; auto.
Qed.
Theorem Neq_le_trans_r : forall x y z, (y = z) -> (x <= y) -> (x <= z).
(* Goal: forall (x y z : R) (_ : @eq R y z) (_ : Rge x y), Rge x z *)
intros x y z H; rewrite H; auto.
Qed.
Theorem Neq_ge_trans_l : forall x y z, (x = z) -> (x >= y) -> (z >= y).
(* Goal: forall (x y z : R) (_ : @eq R y z) (_ : Rge x y), Rge x z *)
intros x y z H; rewrite H; auto.
Qed.
Theorem Neq_ge_trans_r : forall x y z, (y = z) -> (x >= y) -> (x >= z).
(* Goal: forall (x y z : R) (_ : @eq R y z) (_ : Rge x y), Rge x z *)
intros x y z H; rewrite H; auto.
Qed.

Theorem Nge_trans: forall x y z, (x >= z) -> (z >= y) -> (x >= y).
(* Goal: forall (x y z : N) (_ : N.ge x z) (_ : N.ge z y), N.ge x y *)
intros; to_nat; red; apply le_trans with nn1; auto with arith.
Qed.

Close Scope N_scope.

(* Theorems for Z *)

Open Scope Z_scope.

Theorem Zplus_eq_compat_l: forall a b c:Z, (b = c -> a + b = a + c)%Z.
(* Goal: forall (a b c : Z) (_ : @eq Z b c), @eq Z (Z.add a b) (Z.add a c) *)
intros; apply f_equal2 with (f := Zplus); auto.
Qed.

Theorem Zplus_neg_compat_l: forall a b c: Z, (b <> c -> a + b <> a + c)%Z.
(* Goal: forall (a b c : R) (_ : not (@eq R b c)), not (@eq R (Rplus a b) (Rplus a c)) *)
intros a b c H H1; case H.
(* Goal: @eq Z b c *)
apply Zplus_reg_l with a; auto.
Qed.

Theorem Zplus_ge_compat_l: forall n m p : Z, (n >= m -> p + n >= p + m)%Z.
(* Goal: forall (n m p : Z) (_ : Z.ge n m), Z.ge (Z.add p n) (Z.add p m) *)
intros n m p H; apply Z.le_ge; apply Zplus_le_compat_l; auto; apply Z.ge_le; auto.
Qed.

Theorem Zplus_neg_reg_l: forall a b c: Z,  (a + b <> a + c -> b <> c)%Z.
(* Goal: forall (a b c : R) (_ : not (@eq R (Rplus a b) (Rplus a c))), not (@eq R b c) *)
intros a b c H H1; case H; subst; auto.
Qed.

Theorem Zplus_ge_reg_l: forall n m p : Z, (p + n >= p + m -> n >= m)%Z.
(* Goal: forall (n m p : Z) (_ : Z.ge (Z.add p n) (Z.add p m)), Z.ge n m *)
intros n m p H; apply Z.le_ge; apply Zplus_le_reg_l with p; auto; apply Z.ge_le; auto.
Qed.

(* Theorems to simplify the goal 0 ? x * y and x * y ? 0 where ? is < > <= >= *)

Theorem Zle_sign_pos_pos: forall x y: Z, (0 <= x -> 0 <= y  -> 0 <= x * y)%Z.
(* Goal: forall (n m : Z) (_ : Z.gt m n), Z.gt (Z.opp n) (Z.opp m) *)
auto with zarith.
Qed.

Theorem Zle_sign_neg_neg: forall x y: Z, (x <= 0 -> y <= 0  -> 0 <= x * y)%Z.
(* Goal: forall (x y : Z) (_ : Z.le x Z0) (_ : Z.le y Z0), Z.le Z0 (Z.mul x y) *)
intros x y H1 H2; replace (x * y)%Z with (-x * -y)%Z; auto with zarith; ring.
Qed.

Theorem Zopp_le: forall n m, (m <= n -> -n <= -m)%Z.
(* Goal: forall (n m : Z) (_ : Z.gt m n), Z.gt (Z.opp n) (Z.opp m) *)
auto with zarith.
Qed.

Theorem Zle_pos_neg: forall x, (0 <= -x -> x <= 0)%Z.
(* Goal: forall (n m : Z) (_ : Z.gt m n), Z.gt (Z.opp n) (Z.opp m) *)
auto with zarith.
Qed.

Theorem Zle_sign_pos_neg: forall x y: Z, (0 <= x -> y <= 0  -> x * y <= 0)%Z.
(* Goal: forall (x y : Z) (_ : Z.le Z0 x) (_ : Z.le y Z0), Z.le (Z.mul x y) Z0 *)
intros x y H1 H2; apply Zle_pos_neg; replace (- (x * y))%Z with (x * (- y))%Z; auto with zarith; ring.
Qed.

Theorem Zle_sign_neg_pos: forall x y: Z, (x <= 0 -> 0 <= y  -> x * y <= 0)%Z.
(* Goal: forall (x y : Z) (_ : Z.le x Z0) (_ : Z.le Z0 y), Z.le (Z.mul x y) Z0 *)
intros x y H1 H2; apply Zle_pos_neg; replace (- (x * y))%Z with (-x * y)%Z; auto with zarith; ring.
Qed.


Theorem Zlt_sign_pos_pos: forall x y: Z, (0 < x -> 0 < y  -> 0 < x * y)%Z.
(* Goal: forall (n m : Z) (_ : Z.gt m n), Z.gt (Z.opp n) (Z.opp m) *)
intros; apply Zmult_lt_O_compat; auto with zarith.
Qed.

Theorem Zlt_sign_neg_neg: forall x y: Z, (x < 0 -> y < 0  -> 0 < x * y)%Z.
(* Goal: forall (x y : Z) (_ : Z.lt x Z0) (_ : Z.lt y Z0), Z.lt Z0 (Z.mul x y) *)
intros x y H1 H2; replace (x * y)%Z with (-x * -y)%Z; auto with zarith; try ring.
(* Goal: forall (n m : Z) (_ : Z.gt m n), Z.gt (Z.opp n) (Z.opp m) *)
apply Zmult_lt_O_compat; auto with zarith.
Qed.

Theorem Zlt_pos_neg: forall x, (0 < -x -> x < 0)%Z.
(* Goal: forall (n m : Z) (_ : Z.gt m n), Z.gt (Z.opp n) (Z.opp m) *)
auto with zarith.
Qed.

Theorem Zlt_sign_pos_neg: forall x y: Z, (0 < x -> y < 0  -> x * y < 0)%Z.
(* Goal: forall (x y : Z) (_ : Z.lt Z0 x) (_ : Z.lt y Z0), Z.lt (Z.mul x y) Z0 *)
intros x y H1 H2; apply Zlt_pos_neg; replace (- (x * y))%Z with (x * (- y))%Z; auto with zarith; try ring.
(* Goal: forall (n m : Z) (_ : Z.gt m n), Z.gt (Z.opp n) (Z.opp m) *)
apply Zmult_lt_O_compat; auto with zarith.
Qed.

Theorem Zlt_sign_neg_pos: forall x y: Z, (x < 0 -> 0 < y  -> x * y < 0)%Z.
(* Goal: forall (x y : Z) (_ : Z.lt x Z0) (_ : Z.lt Z0 y), Z.lt (Z.mul x y) Z0 *)
intros x y H1 H2; apply Zlt_pos_neg; replace (- (x * y))%Z with (-x * y)%Z; auto with zarith; try ring.
(* Goal: forall (n m : Z) (_ : Z.gt m n), Z.gt (Z.opp n) (Z.opp m) *)
apply Zmult_lt_O_compat; auto with zarith.
Qed.

Theorem Zge_sign_neg_neg: forall x y: Z, (0 >= x -> 0 >= y  -> x * y >= 0)%Z.
(* Goal: forall (n m : Z) (_ : Z.gt m n), Z.gt (Z.opp n) (Z.opp m) *)
intros; apply Z.le_ge; apply Zle_sign_neg_neg; auto with zarith.
Qed.

Theorem Zge_sign_pos_pos: forall x y: Z, (x >= 0 -> y >= 0  -> x * y >= 0)%Z.
(* Goal: forall (n m : Z) (_ : Z.gt m n), Z.gt (Z.opp n) (Z.opp m) *)
intros; apply Z.le_ge; apply Zle_sign_pos_pos; auto with zarith.
Qed.

Theorem Zge_neg_pos: forall x, (0 >= -x -> x >= 0)%Z.
(* Goal: forall (n m : Z) (_ : Z.gt m n), Z.gt (Z.opp n) (Z.opp m) *)
auto with zarith.
Qed.

Theorem Zge_sign_neg_pos: forall x y: Z, (0 >= x -> y >= 0  -> 0>= x * y)%Z.
(* Goal: forall (n m : Z) (_ : Z.gt m n), Z.gt (Z.opp n) (Z.opp m) *)
intros; apply Z.le_ge; apply Zle_sign_neg_pos; auto with zarith.
Qed.

Theorem Zge_sign_pos_neg: forall x y: Z, (x >= 0 -> 0 >= y  -> 0 >= x * y)%Z.
(* Goal: forall (n m : Z) (_ : Z.gt m n), Z.gt (Z.opp n) (Z.opp m) *)
intros; apply Z.le_ge; apply Zle_sign_pos_neg; auto with zarith.
Qed.


Theorem Zgt_sign_neg_neg: forall x y: Z, (0 > x -> 0 > y  -> x * y > 0)%Z.
(* Goal: forall (n m : Z) (_ : Z.gt m n), Z.gt (Z.opp n) (Z.opp m) *)
intros; apply Z.lt_gt; apply Zlt_sign_neg_neg; auto with zarith.
Qed.

Theorem Zgt_sign_pos_pos: forall x y: Z, (x > 0 -> y > 0  -> x * y > 0)%Z.
(* Goal: forall (n m : Z) (_ : Z.gt m n), Z.gt (Z.opp n) (Z.opp m) *)
intros; apply Z.lt_gt; apply Zlt_sign_pos_pos; auto with zarith.
Qed.

Theorem Zgt_neg_pos: forall x, (0 > -x -> x > 0)%Z.
(* Goal: forall (n m : Z) (_ : Z.gt m n), Z.gt (Z.opp n) (Z.opp m) *)
auto with zarith.
Qed.

Theorem Zgt_sign_neg_pos: forall x y: Z, (0 > x -> y > 0  -> 0> x * y)%Z.
(* Goal: forall (n m : Z) (_ : Z.gt m n), Z.gt (Z.opp n) (Z.opp m) *)
intros; apply Z.lt_gt; apply Zlt_sign_neg_pos; auto with zarith.
Qed.

Theorem Zgt_sign_pos_neg: forall x y: Z, (x > 0 -> 0 > y  -> 0 > x * y)%Z.
(* Goal: forall (n m : Z) (_ : Z.gt m n), Z.gt (Z.opp n) (Z.opp m) *)
intros; apply Z.lt_gt; apply Zlt_sign_pos_neg; auto with zarith.
Qed.

(* Theorems to simplify the hyp 0 ? x * y and x * y ? 0 where ? is < > <= >= *)

Theorem Zle_sign_pos_pos_rev: forall x y: Z, (0 < x -> 0 <= x * y -> 0 <= y)%Z.
(* Goal: forall (n m : Z) (_ : Z.gt m n), Z.gt (Z.opp n) (Z.opp m) *)
intros x y H1 H2; case (Zle_or_lt 0 y); auto with zarith.
(* Goal: forall (n m : Z) (_ : Z.gt m n), Z.gt (Z.opp n) (Z.opp m) *)
intros H3; absurd (0 <= x * y)%Z; auto with zarith.
(* Goal: not (Z.le Z0 (Z.mul x y)) *)
apply Zlt_not_le;apply Zlt_sign_pos_neg; auto.
Qed.

Theorem Zle_sign_neg_neg_rev: forall x y: Z, (x < 0 -> 0 <= x * y ->  y <= 0)%Z.
(* Goal: forall (n m : Z) (_ : Z.gt m n), Z.gt (Z.opp n) (Z.opp m) *)
intros x y H1 H2; case (Zle_or_lt y  0); auto with zarith.
(* Goal: forall (n m : Z) (_ : Z.gt m n), Z.gt (Z.opp n) (Z.opp m) *)
intros H3; absurd (0 <= x * y)%Z; auto with zarith.
(* Goal: not (Z.le Z0 (Z.mul x y)) *)
apply Zlt_not_le;apply Zlt_sign_neg_pos; auto.
Qed.

Theorem Zle_sign_pos_neg_rev: forall x y: Z, (0 < x -> x * y <= 0 -> y <= 0)%Z.
(* Goal: forall (n m : Z) (_ : Z.gt m n), Z.gt (Z.opp n) (Z.opp m) *)
intros x y H1 H2; case (Zle_or_lt y 0); auto with zarith.
(* Goal: forall (n m : Z) (_ : Z.gt m n), Z.gt (Z.opp n) (Z.opp m) *)
intros H3; absurd (x * y <= 0)%Z; auto with zarith.
(* Goal: not (Z.le (Z.mul x y) Z0) *)
apply Zlt_not_le;apply Zlt_sign_pos_pos; auto.
Qed.

Theorem Zle_sign_neg_pos_rev: forall x y: Z, (x < 0 -> x * y <= 0 ->  0 <= y)%Z.
(* Goal: forall (n m : Z) (_ : Z.gt m n), Z.gt (Z.opp n) (Z.opp m) *)
intros x y H1 H2; case (Zle_or_lt 0 y); auto with zarith.
(* Goal: forall (n m : Z) (_ : Z.gt m n), Z.gt (Z.opp n) (Z.opp m) *)
intros H3; absurd (x * y <= 0)%Z; auto with zarith.
(* Goal: not (Z.le (Z.mul x y) Z0) *)
apply Zlt_not_le;apply Zlt_sign_neg_neg; auto.
Qed.

Theorem Zge_sign_pos_pos_rev: forall x y: Z, (x > 0 -> x * y >= 0 -> y >= 0)%Z.
(* Goal: forall (n m : Z) (_ : Z.gt m n), Z.gt (Z.opp n) (Z.opp m) *)
intros x y H1 H2; apply Z.le_ge; apply Zle_sign_pos_pos_rev with x; auto with zarith.
Qed.

Theorem Zge_sign_neg_neg_rev: forall x y: Z, (0 > x -> x * y  >= 0->  0 >= y)%Z.
(* Goal: forall (n m : Z) (_ : Z.gt m n), Z.gt (Z.opp n) (Z.opp m) *)
intros x y H1 H2; apply Z.le_ge; apply Zle_sign_neg_neg_rev with x; auto with zarith.
Qed.

Theorem Zge_sign_pos_neg_rev: forall x y: Z, (x > 0 -> 0 >= x * y -> 0 >= y)%Z.
(* Goal: forall (n m : Z) (_ : Z.gt m n), Z.gt (Z.opp n) (Z.opp m) *)
intros x y H1 H2; apply Z.le_ge; apply Zle_sign_pos_neg_rev with x; auto with zarith.
Qed.

Theorem Zge_sign_neg_pos_rev: forall x y: Z, (0 > x -> 0 >= x * y ->  y >= 0)%Z.
(* Goal: forall (n m : Z) (_ : Z.gt m n), Z.gt (Z.opp n) (Z.opp m) *)
intros x y H1 H2; apply Z.le_ge; apply Zle_sign_neg_pos_rev with x; auto with zarith.
Qed.

Theorem Zlt_sign_pos_pos_rev: forall x y: Z, (0 < x -> 0 < x * y -> 0 < y)%Z.
(* Goal: forall (n m : Z) (_ : Z.gt m n), Z.gt (Z.opp n) (Z.opp m) *)
intros x y H1 H2; case (Zle_or_lt y 0); auto with zarith.
(* Goal: forall (n m : Z) (_ : Z.gt m n), Z.gt (Z.opp n) (Z.opp m) *)
intros H3; absurd (0 < x * y)%Z; auto with zarith.
(* Goal: forall (n m : Z) (_ : Z.gt m n), Z.gt (Z.opp n) (Z.opp m) *)
apply Zle_not_lt;apply Zle_sign_pos_neg; auto with zarith.
Qed.

Theorem Zlt_sign_neg_neg_rev: forall x y: Z, (x < 0 -> 0 < x * y ->  y < 0)%Z.
(* Goal: forall (n m : Z) (_ : Z.gt m n), Z.gt (Z.opp n) (Z.opp m) *)
intros x y H1 H2; case (Zle_or_lt 0 y); auto with zarith.
(* Goal: forall (n m : Z) (_ : Z.gt m n), Z.gt (Z.opp n) (Z.opp m) *)
intros H3; absurd (0 < x * y)%Z; auto with zarith.
(* Goal: forall (n m : Z) (_ : Z.gt m n), Z.gt (Z.opp n) (Z.opp m) *)
apply Zle_not_lt;apply Zle_sign_neg_pos; auto with zarith.
Qed.

Theorem Zlt_sign_pos_neg_rev: forall x y: Z, (0 < x -> x * y < 0 -> y < 0)%Z.
(* Goal: forall (n m : Z) (_ : Z.gt m n), Z.gt (Z.opp n) (Z.opp m) *)
intros x y H1 H2; case (Zle_or_lt 0 y); auto with zarith.
(* Goal: forall (n m : Z) (_ : Z.gt m n), Z.gt (Z.opp n) (Z.opp m) *)
intros H3; absurd (x * y < 0)%Z; auto with zarith.
(* Goal: forall (n m : Z) (_ : Z.gt m n), Z.gt (Z.opp n) (Z.opp m) *)
apply Zle_not_lt;apply Zle_sign_pos_pos; auto with zarith.
Qed.

Theorem Zlt_sign_neg_pos_rev: forall x y: Z, (x < 0 -> x * y < 0 ->  0 < y)%Z.
(* Goal: forall (n m : Z) (_ : Z.gt m n), Z.gt (Z.opp n) (Z.opp m) *)
intros x y H1 H2; case (Zle_or_lt y 0); auto with zarith.
(* Goal: forall (n m : Z) (_ : Z.gt m n), Z.gt (Z.opp n) (Z.opp m) *)
intros H3; absurd (x * y < 0)%Z; auto with zarith.
(* Goal: forall (n m : Z) (_ : Z.gt m n), Z.gt (Z.opp n) (Z.opp m) *)
apply Zle_not_lt;apply Zle_sign_neg_neg; auto with zarith.
Qed.

Theorem Zgt_sign_pos_pos_rev: forall x y: Z, (x > 0 -> x * y > 0 -> y > 0)%Z.
(* Goal: forall (n m : Z) (_ : Z.gt m n), Z.gt (Z.opp n) (Z.opp m) *)
intros x y H1 H2; apply Z.lt_gt; apply Zlt_sign_pos_pos_rev with x; auto with zarith.
Qed.

Theorem Zgt_sign_neg_neg_rev: forall x y: Z, (0 > x -> x * y  > 0->  0 > y)%Z.
(* Goal: forall (n m : Z) (_ : Z.gt m n), Z.gt (Z.opp n) (Z.opp m) *)
intros x y H1 H2; apply Z.lt_gt; apply Zlt_sign_neg_neg_rev with x; auto with zarith.
Qed.

Theorem Zgt_sign_pos_neg_rev: forall x y: Z, (x > 0 -> 0 > x * y -> 0 > y)%Z.
(* Goal: forall (n m : Z) (_ : Z.gt m n), Z.gt (Z.opp n) (Z.opp m) *)
intros x y H1 H2; apply Z.lt_gt; apply Zlt_sign_pos_neg_rev with x; auto with zarith.
Qed.

Theorem Zgt_sign_neg_pos_rev: forall x y: Z, (0 > x -> 0 > x * y ->  y > 0)%Z.
(* Goal: forall (n m : Z) (_ : Z.gt m n), Z.gt (Z.opp n) (Z.opp m) *)
intros x y H1 H2; apply Z.lt_gt; apply Zlt_sign_neg_pos_rev with x; auto with zarith.
Qed.

(* Theorem to simplify x * y ? x * z where ? is < > <= >= *)

Theorem Zmult_le_neg_compat_l:
  forall n m p : Z, (m <= n)%Z -> (p <= 0)%Z -> (p * n <= p * m)%Z.
(* Goal: forall (n m p : Z) (_ : Z.gt m n) (_ : Z.gt Z0 p), Z.gt (Z.mul p n) (Z.mul p m) *)
intros n m p H1 H2; replace (p * n)%Z with (-(-p * n))%Z; auto with zarith; try ring.
(* Goal: Z.gt (Z.opp (Z.mul (Z.opp p) n)) (Z.mul p m) *)
replace (p * m)%Z with (-(-p * m))%Z; auto with zarith; try ring.
(* Goal: forall (n m : Z) (_ : Z.gt m n), Z.gt (Z.opp n) (Z.opp m) *)
apply Zopp_le; apply Zmult_le_compat_l; auto with zarith.
Qed.

Theorem Zopp_lt: forall n m, (m < n -> -n < -m)%Z.
(* Goal: forall (n m : Z) (_ : Z.gt m n), Z.gt (Z.opp n) (Z.opp m) *)
auto with zarith.
Qed.

Theorem Zmult_lt_neg_compat_l:
  forall n m p : Z, (m < n)%Z -> (p < 0)%Z -> (p * n < p * m)%Z.
(* Goal: forall (n m p : Z) (_ : Z.gt m n) (_ : Z.gt Z0 p), Z.gt (Z.mul p n) (Z.mul p m) *)
intros n m p H1 H2; replace (p * n)%Z with (-(-p * n))%Z; auto with zarith; try ring.
(* Goal: Z.gt (Z.opp (Z.mul (Z.opp p) n)) (Z.mul p m) *)
replace (p * m)%Z with (-(-p * m))%Z; auto with zarith; try ring.
(* Goal: forall (n m : Z) (_ : Z.gt m n), Z.gt (Z.opp n) (Z.opp m) *)
apply Zopp_lt; apply Zmult_lt_compat_l; auto with zarith.
Qed.

Theorem Zopp_ge: forall n m, (m >= n -> -n >= -m)%Z.
(* Goal: forall (n m : Z) (_ : Z.gt m n), Z.gt (Z.opp n) (Z.opp m) *)
auto with zarith.
Qed.

Theorem Zmult_ge_neg_compat_l:
  forall n m p : Z, (m >= n)%Z -> (0 >= p)%Z -> (p * n >= p * m)%Z.
(* Goal: forall (n m p : Z) (_ : Z.gt m n) (_ : Z.gt Z0 p), Z.gt (Z.mul p n) (Z.mul p m) *)
intros n m p H1 H2; replace (p * n)%Z with (-(-p * n))%Z; auto with zarith; try ring.
(* Goal: Z.gt (Z.opp (Z.mul (Z.opp p) n)) (Z.mul p m) *)
replace (p * m)%Z with (-(-p * m))%Z; auto with zarith; try ring.
(* Goal: forall (n m : Z) (_ : Z.gt m n), Z.gt (Z.opp n) (Z.opp m) *)
apply Zopp_ge; apply Zmult_ge_compat_l; auto with zarith.
Qed.

Theorem Zopp_gt: forall n m, (m > n -> -n > -m)%Z.
(* Goal: forall (n m : Z) (_ : Z.gt m n), Z.gt (Z.opp n) (Z.opp m) *)
auto with zarith.
Qed.

Theorem Zmult_gt_neg_compat_l:
  forall n m p : Z, (m > n)%Z -> (0 > p)%Z -> (p * n > p * m)%Z.
(* Goal: forall (n m p : Z) (_ : Z.gt m n) (_ : Z.gt Z0 p), Z.gt (Z.mul p n) (Z.mul p m) *)
intros n m p H1 H2; replace (p * n)%Z with (-(-p * n))%Z; auto with zarith; try ring.
(* Goal: Z.gt (Z.opp (Z.mul (Z.opp p) n)) (Z.mul p m) *)
replace (p * m)%Z with (-(-p * m))%Z; auto with zarith; try ring.
(* Goal: forall (n m : Z) (_ : Z.gt m n), Z.gt (Z.opp n) (Z.opp m) *)
apply Zopp_gt; apply Zmult_gt_compat_l; auto with zarith.
Qed.


(* Theorem to simplify a hyp x * y ? x * z where ? is < > <= >= *)


Theorem Zmult_le_compat_l_rev:
  forall n m p : Z, (0 < p)%Z -> (p * n <= p * m)%Z -> (n <= m)%Z.
(* Goal: forall (n m p : Z) (_ : Z.lt p Z0) (_ : Z.lt (Z.mul p n) (Z.mul p m)), Z.lt m n *)
intros n m p H H1; case (Zle_or_lt n m); auto; intros H2.
(* Goal: forall (n m : Z) (_ : Z.gt m n), Z.gt (Z.opp n) (Z.opp m) *)
absurd (p * n <= p * m)%Z; auto with zarith.
(* Goal: not (Z.le (Z.mul p n) (Z.mul p m)) *)
apply Zlt_not_le; apply Zmult_lt_compat_l; auto.
Qed.

Theorem Zmult_le_neg_compat_l_rev:
  forall n m p : Z, (p < 0)%Z -> (p * n <= p * m)%Z -> (m <= n)%Z.
(* Goal: forall (n m p : Z) (_ : Z.lt Z0 p) (_ : Z.lt (Z.mul p n) (Z.mul p m)), Z.lt n m *)
intros n m p H H1; case (Zle_or_lt m n); auto; intros H2.
(* Goal: forall (n m : Z) (_ : Z.gt m n), Z.gt (Z.opp n) (Z.opp m) *)
absurd (p * n <= p * m)%Z; auto with zarith.
(* Goal: not (Z.le (Z.mul p n) (Z.mul p m)) *)
apply Zlt_not_le; apply Zmult_lt_neg_compat_l; auto.
Qed.

Theorem Zmult_lt_compat_l_rev:
  forall n m p : Z, (0 < p)%Z -> (p * n < p * m)%Z -> (n < m)%Z.
(* Goal: forall (n m p : Z) (_ : Z.lt Z0 p) (_ : Z.lt (Z.mul p n) (Z.mul p m)), Z.lt n m *)
intros n m p H H1; case (Zle_or_lt m n); auto; intros H2.
(* Goal: forall (n m : Z) (_ : Z.gt m n), Z.gt (Z.opp n) (Z.opp m) *)
absurd (p * n < p * m)%Z; auto with zarith.
(* Goal: forall (n m : Z) (_ : Z.gt m n), Z.gt (Z.opp n) (Z.opp m) *)
apply Zle_not_lt; apply Zmult_le_compat_l; auto with zarith.
Qed.

Theorem Zmult_lt_neg_compat_l_rev:
  forall n m p : Z, (p < 0)%Z -> (p * n < p * m)%Z -> (m < n)%Z.
(* Goal: forall (n m p : Z) (_ : Z.lt p Z0) (_ : Z.lt (Z.mul p n) (Z.mul p m)), Z.lt m n *)
intros n m p H H1; case (Zle_or_lt n m); auto; intros H2.
(* Goal: forall (n m : Z) (_ : Z.gt m n), Z.gt (Z.opp n) (Z.opp m) *)
absurd (p * n < p * m)%Z; auto with zarith.
(* Goal: forall (n m : Z) (_ : Z.gt m n), Z.gt (Z.opp n) (Z.opp m) *)
apply Zle_not_lt; apply Zmult_le_neg_compat_l; auto with zarith.
Qed.

Theorem Zmult_ge_compat_l_rev:
  forall n m p : Z, (p > 0)%Z -> (p * n >= p * m)%Z -> (n >= m)%Z.
intros n m p H H1;
(* Goal: forall (n m : Z) (_ : Z.gt m n), Z.gt (Z.opp n) (Z.opp m) *)
 apply Z.le_ge; apply Zmult_le_compat_l_rev with p; auto with zarith.
Qed.

Theorem Zmult_ge_neg_compat_l_rev:
  forall n m p : Z, (0 > p)%Z -> (p * n >= p * m)%Z -> (m >= n)%Z.
intros n m p H H1;
(* Goal: forall (n m : Z) (_ : Z.gt m n), Z.gt (Z.opp n) (Z.opp m) *)
 apply Z.le_ge; apply Zmult_le_neg_compat_l_rev with p; auto with zarith.
Qed.

Theorem Zmult_gt_compat_l_rev:
  forall n m p : Z, (p > 0)%Z -> (p * n > p * m)%Z -> (n > m)%Z.
intros n m p H H1;
(* Goal: forall (n m : Z) (_ : Z.gt m n), Z.gt (Z.opp n) (Z.opp m) *)
 apply Z.lt_gt; apply Zmult_lt_compat_l_rev with p; auto with zarith.
Qed.

Theorem Zmult_gt_neg_compat_l_rev:
  forall n m p : Z, (0 > p)%Z -> (p * n > p * m)%Z -> (m > n)%Z.
intros n m p H H1;
(* Goal: forall (n m : Z) (_ : Z.gt m n), Z.gt (Z.opp n) (Z.opp m) *)
 apply Z.lt_gt; apply Zmult_lt_neg_compat_l_rev with p; auto with zarith.
Qed.

(* For replace *)

Theorem eq_Zlt_trans_l : forall x y z, (x = z) -> (x < y) -> (z < y).
(* Goal: forall (x y z : R) (_ : @eq R y z) (_ : Rge x y), Rge x z *)
intros x y z H; rewrite H; auto.
Qed.
Theorem eq_Zlt_trans_r : forall x y z, (y = z) -> (x < y) -> (x < z).
(* Goal: forall (x y z : R) (_ : @eq R y z) (_ : Rge x y), Rge x z *)
intros x y z H; rewrite H; auto.
Qed.
Theorem eq_Zgt_trans_l : forall x y z, (x = z) -> (x > y) -> (z > y).
(* Goal: forall (x y z : R) (_ : @eq R y z) (_ : Rge x y), Rge x z *)
intros x y z H; rewrite H; auto.
Qed.
Theorem eq_Zgt_trans_r : forall x y z, (y = z) -> (x > y) -> (x > z).
(* Goal: forall (x y z : R) (_ : @eq R y z) (_ : Rge x y), Rge x z *)
intros x y z H; rewrite H; auto.
Qed.
Theorem eq_Zle_trans_l : forall x y z, (x = z) -> (x <= y) -> (z <= y).
(* Goal: forall (x y z : R) (_ : @eq R y z) (_ : Rge x y), Rge x z *)
intros x y z H; rewrite H; auto.
Qed.
Theorem eq_Zle_trans_r : forall x y z, (y = z) -> (x <= y) -> (x <= z).
(* Goal: forall (x y z : R) (_ : @eq R y z) (_ : Rge x y), Rge x z *)
intros x y z H; rewrite H; auto.
Qed.
Theorem eq_Zge_trans_l : forall x y z, (x = z) -> (x >= y) -> (z >= y).
(* Goal: forall (x y z : R) (_ : @eq R y z) (_ : Rge x y), Rge x z *)
intros x y z H; rewrite H; auto.
Qed.
Theorem eq_Zge_trans_r : forall x y z, (y = z) -> (x >= y) -> (x >= z).
(* Goal: forall (x y z : R) (_ : @eq R y z) (_ : Rge x y), Rge x z *)
intros x y z H; rewrite H; auto.
Qed.

Theorem Zge_trans: forall x y z, (x >= z) -> (z >= y) -> (x >= y).
(* Goal: forall (x y z : Z) (_ : Z.ge x z) (_ : Z.ge z y), Z.ge x y *)
intros x y z H1 H2; red; apply Zge_trans with z; auto.
Qed.

Close Scope Z_scope.

(* Theorems for R *)

Open Scope R_scope.

Theorem Rplus_eq_compat_l: forall a b c:R, (b = c -> a + b = a + c)%R.
(* Goal: forall (a b c : R) (_ : @eq R b c), @eq R (Rplus a b) (Rplus a c) *)
intros; apply f_equal2 with (f := Rplus); auto.
Qed.

Theorem Rplus_neg_compat_l: forall a b c: R, (b <> c -> a + b <> a + c)%R.
(* Goal: forall (a b c : R) (_ : not (@eq R b c)), not (@eq R (Rplus a b) (Rplus a c)) *)
intros a b c H H1; case H.
(* Goal: @eq R b c *)
apply Rplus_eq_reg_l with a; auto.
Qed.

Theorem Rplus_ge_compat_l: forall n m p : R, (n >= m -> p + n >= p + m)%R.
(* Goal: forall (n m p : R) (_ : Rge n m), Rge (Rplus p n) (Rplus p m) *)
intros n m p H; apply Rle_ge; apply Rplus_le_compat_l; auto; apply Rge_le; auto.
Qed.

Theorem Rplus_neg_reg_l: forall a b c: R,  (a + b <> a + c -> b <> c)%R.
(* Goal: forall (a b c : R) (_ : not (@eq R (Rplus a b) (Rplus a c))), not (@eq R b c) *)
intros a b c H H1; case H; subst; auto.
Qed.

Theorem Rplus_ge_reg_l: forall n m p : R, (p + n >= p + m -> n >= m)%R.
(* Goal: forall (n m p : R) (_ : Rge (Rplus p n) (Rplus p m)), Rge n m *)
intros n m p H; apply Rle_ge; apply Rplus_le_reg_l with p; auto; apply Rge_le; auto.
Qed.

(* Theorems to simplify the goal 0 ? x * y and x * y ? 0 where ? is < > <= >= *)

Theorem Rle_sign_pos_pos: forall x y, (0 <= x -> 0 <= y  -> 0 <= x * y)%R.
(* Goal: forall (x y : R) (_ : Rle (IZR Z0) x) (_ : Rle (IZR Z0) y), Rle (IZR Z0) (Rmult x y) *)
intros x y H; apply Rmult_le_pos; auto with real.
Qed.

Theorem Rle_sign_neg_neg: forall x y, (x <= 0 -> y <= 0  -> 0 <= x * y)%R.
(* Goal: forall (x y : R) (_ : Rlt x (IZR Z0)) (_ : Rlt y (IZR Z0)), Rlt (IZR Z0) (Rmult x y) *)
intros x y H1 H2; replace (x * y)%R with (-x * -y)%R; auto with real; try ring.
(* Goal: Rle (IZR Z0) (Rmult (Ropp x) y) *)
apply Rmult_le_pos; auto with real.
Qed.

Theorem Rle_pos_neg: forall x, (0 <= -x -> x <= 0)%R.
(* Goal: forall (n m : R) (_ : Rgt m n), Rgt (Ropp n) (Ropp m) *)
intros x H; rewrite <- (Ropp_involutive 0);  rewrite <- (Ropp_involutive x); auto with real.
(* Goal: Rle (Ropp (Ropp x)) (Ropp (Ropp (IZR Z0))) *)
apply Ropp_le_contravar; auto with real.
(* Goal: Rlt (Ropp x) (Ropp (IZR Z0)) *)
rewrite Ropp_0; auto with real.
Qed.

Theorem Rle_sign_pos_neg: forall x y: R, (0 <= x -> y <= 0  -> x * y <= 0)%R.
(* Goal: forall (x y : R) (_ : Rle (IZR Z0) x) (_ : Rle y (IZR Z0)), Rle (Rmult x y) (IZR Z0) *)
intros x y H1 H2; apply Rle_pos_neg; replace (- (x * y))%R with (x * (- y))%R; auto with real; try ring.
(* Goal: Rle (IZR Z0) (Rmult (Ropp x) y) *)
apply Rmult_le_pos; auto with real.
Qed.

Theorem Rle_sign_neg_pos: forall x y, (x <= 0 -> 0 <= y  -> x * y <= 0)%R.
(* Goal: forall (x y : R) (_ : Rle x (IZR Z0)) (_ : Rle (IZR Z0) y), Rle (Rmult x y) (IZR Z0) *)
intros x y H1 H2; apply Rle_pos_neg; replace (- (x * y))%R with (-x * y)%R; auto with real; try ring.
(* Goal: Rle (IZR Z0) (Rmult (Ropp x) y) *)
apply Rmult_le_pos; auto with real.
Qed.

Theorem Rlt_sign_pos_pos: forall x y, (0 < x -> 0 < y  -> 0 < x * y)%R.
(* Goal: forall (x y : R) (_ : Rlt (IZR Z0) x) (_ : Rlt (IZR Z0) y), Rlt (IZR Z0) (Rmult x y) *)
intros; apply Rmult_lt_0_compat; auto with real.
Qed.

Theorem Rlt_sign_neg_neg: forall x y, (x < 0 -> y < 0  -> 0 < x * y)%R.
(* Goal: forall (x y : R) (_ : Rlt x (IZR Z0)) (_ : Rlt y (IZR Z0)), Rlt (IZR Z0) (Rmult x y) *)
intros x y H1 H2; replace (x * y)%R with (-x * -y)%R; auto with real; try ring.
(* Goal: Rlt (IZR Z0) (Rmult (Ropp x) y) *)
apply Rmult_lt_0_compat; auto with real.
Qed.

Theorem Rlt_pos_neg: forall x, (0 < -x -> x < 0)%R.
(* Goal: forall (n m : R) (_ : Rgt m n), Rgt (Ropp n) (Ropp m) *)
intros x H; rewrite <- (Ropp_involutive 0);  rewrite <- (Ropp_involutive x); auto with real.
(* Goal: Rlt (Ropp (Ropp x)) (Ropp (Ropp (IZR Z0))) *)
apply Ropp_lt_contravar; auto with real.
(* Goal: Rlt (Ropp x) (Ropp (IZR Z0)) *)
rewrite Ropp_0; auto with real.
Qed.

Theorem Rlt_sign_pos_neg: forall x y, (0 < x -> y < 0  -> x * y < 0)%R.
(* Goal: forall (x y : R) (_ : Rlt (IZR Z0) x) (_ : Rlt y (IZR Z0)), Rlt (Rmult x y) (IZR Z0) *)
intros x y H1 H2; apply Rlt_pos_neg; replace (- (x * y))%R with (x * (- y))%R; auto with real; try ring.
(* Goal: Rlt (IZR Z0) (Rmult x (Ropp y)) *)
apply Rmult_lt_0_compat; auto.
(* Goal: Rlt (IZR Z0) (Ropp y) *)
replace 0%R with (-0)%R; auto with real.
Qed.

Theorem Rlt_sign_neg_pos: forall x y, (x < 0 -> 0 < y  -> x * y < 0)%R.
(* Goal: forall (x y : R) (_ : Rlt x (IZR Z0)) (_ : Rlt (IZR Z0) y), Rlt (Rmult x y) (IZR Z0) *)
intros x y H1 H2; apply Rlt_pos_neg; replace (- (x * y))%R with (-x * y)%R; auto with real; try ring.
(* Goal: Rlt (IZR Z0) (Rmult (Ropp x) y) *)
apply Rmult_lt_0_compat; auto with real.
Qed.



Theorem Rge_sign_neg_neg: forall x y, (0 >= x -> 0 >= y  -> x * y >= 0)%R.
(* Goal: forall (x y : R) (_ : Rge (IZR Z0) x) (_ : Rge (IZR Z0) y), Rge (Rmult x y) (IZR Z0) *)
intros; apply Rle_ge; apply Rle_sign_neg_neg; auto with real.
Qed.

Theorem Rge_sign_pos_pos: forall x y, (x >= 0 -> y >= 0  -> x * y >= 0)%R.
(* Goal: forall (x y : R) (_ : Rge x (IZR Z0)) (_ : Rge y (IZR Z0)), Rge (Rmult x y) (IZR Z0) *)
intros; apply Rle_ge; apply Rle_sign_pos_pos; auto with real.
Qed.

Theorem Rge_neg_pos: forall x, (0 >= -x -> x >= 0)%R.
(* Goal: forall (n m : R) (_ : Rgt m n), Rgt (Ropp n) (Ropp m) *)
intros x H; rewrite <- (Ropp_involutive 0);  rewrite <- (Ropp_involutive x); auto with real.
(* Goal: Rle (Ropp (Ropp x)) (Ropp (Ropp (IZR Z0))) *)
apply Rle_ge;apply Ropp_le_contravar; auto with real.
(* Goal: Rlt (Ropp x) (Ropp (IZR Z0)) *)
rewrite Ropp_0; auto with real.
Qed.

Theorem Rge_sign_neg_pos: forall x y: R, (0 >= x -> y >= 0  -> 0>= x * y)%R.
(* Goal: forall (x y : R) (_ : Rge (IZR Z0) x) (_ : Rge y (IZR Z0)), Rge (IZR Z0) (Rmult x y) *)
intros; apply Rle_ge; apply Rle_sign_neg_pos; auto with real.
Qed.

Theorem Rge_sign_pos_neg: forall x y, (x >= 0 -> 0 >= y  -> 0 >= x * y)%R.
(* Goal: forall (x y : R) (_ : Rge x (IZR Z0)) (_ : Rge (IZR Z0) y), Rge (IZR Z0) (Rmult x y) *)
intros; apply Rle_ge; apply Rle_sign_pos_neg; auto with real.
Qed.


Theorem Rgt_sign_neg_neg: forall x y, (0 > x -> 0 > y  -> x * y > 0)%R.
(* Goal: forall (n m : R) (_ : Rgt m n), Rgt (Ropp n) (Ropp m) *)
intros; red;  apply Rlt_sign_neg_neg; auto with real.
Qed.

Theorem Rgt_sign_pos_pos: forall x y, (x > 0 -> y > 0  -> x * y > 0)%R.
(* Goal: forall (x y : R) (_ : Rgt x (IZR Z0)) (_ : Rgt y (IZR Z0)), Rgt (Rmult x y) (IZR Z0) *)
intros; red; apply Rlt_sign_pos_pos; auto with real.
Qed.

Theorem Rgt_neg_pos: forall x, (0 > -x -> x > 0)%R.
(* Goal: forall (n m : R) (_ : Rgt m n), Rgt (Ropp n) (Ropp m) *)
intros x H; rewrite <- (Ropp_involutive 0);  rewrite <- (Ropp_involutive x); auto with real.
(* Goal: Rlt (Ropp (Ropp x)) (Ropp (Ropp (IZR Z0))) *)
red;apply Ropp_lt_contravar; auto with real.
(* Goal: Rlt (Ropp x) (Ropp (IZR Z0)) *)
rewrite Ropp_0; auto with real.
Qed.

Theorem Rgt_sign_neg_pos: forall x y, (0 > x -> y > 0  -> 0> x * y)%R.
(* Goal: forall (x y : R) (_ : Rgt (IZR Z0) x) (_ : Rgt y (IZR Z0)), Rgt (IZR Z0) (Rmult x y) *)
intros; red; apply Rlt_sign_neg_pos; auto with real.
Qed.

Theorem Rgt_sign_pos_neg: forall x y, (x > 0 -> 0 > y  -> 0 > x * y)%R.
(* Goal: forall (x y : R) (_ : Rgt x (IZR Z0)) (_ : Rgt (IZR Z0) y), Rgt (IZR Z0) (Rmult x y) *)
intros; red; apply Rlt_sign_pos_neg; auto with real.
Qed.

(* Theorems to simplify the hyp 0 ? x * y and x * y ? 0 where ? is < > <= >= *)

Theorem Rle_sign_pos_pos_rev: forall x y: R, (0 < x -> 0 <= x * y -> 0 <= y)%R.
(* Goal: forall (x y : R) (_ : Rlt (IZR Z0) x) (_ : Rlt (Rmult x y) (IZR Z0)), Rlt y (IZR Z0) *)
intros x y H1 H2; case (Rle_or_lt 0 y); auto with real.
(* Goal: forall _ : Rlt (IZR Z0) y, Rle y (IZR Z0) *)
intros H3; absurd (0 <= x * y)%R; auto with real.
(* Goal: not (Rle (IZR Z0) (Rmult x y)) *)
apply Rlt_not_le;apply Rlt_sign_pos_neg; auto.
Qed.

Theorem Rle_sign_neg_neg_rev: forall x y: R, (x < 0 -> 0 <= x * y ->  y <= 0)%R.
(* Goal: forall (n m : R) (_ : Rgt m n), Rgt (Ropp n) (Ropp m) *)
intros x y H1 H2; case (Rle_or_lt y  0); auto with real.
(* Goal: forall _ : Rlt (IZR Z0) y, Rle y (IZR Z0) *)
intros H3; absurd (0 <= x * y)%R; auto with real.
(* Goal: not (Rle (IZR Z0) (Rmult x y)) *)
apply Rlt_not_le;apply Rlt_sign_neg_pos; auto.
Qed.

Theorem Rle_sign_pos_neg_rev: forall x y: R, (0 < x -> x * y <= 0 -> y <= 0)%R.
(* Goal: forall (x y : R) (_ : Rlt x (IZR Z0)) (_ : Rlt (Rmult x y) (IZR Z0)), Rlt (IZR Z0) y *)
intros x y H1 H2; case (Rle_or_lt y 0); auto with real.
(* Goal: forall _ : Rlt y (IZR Z0), Rle (IZR Z0) y *)
intros H3; absurd (x * y <= 0)%R; auto with real.
(* Goal: not (Rle (Rmult x y) (IZR Z0)) *)
apply Rlt_not_le;apply Rlt_sign_pos_pos; auto.
Qed.

Theorem Rle_sign_neg_pos_rev: forall x y: R, (x < 0 -> x * y <= 0 ->  0 <= y)%R.
(* Goal: forall (x y : R) (_ : Rlt (IZR Z0) x) (_ : Rlt (Rmult x y) (IZR Z0)), Rlt y (IZR Z0) *)
intros x y H1 H2; case (Rle_or_lt 0 y); auto with real.
(* Goal: forall _ : Rlt y (IZR Z0), Rle (IZR Z0) y *)
intros H3; absurd (x * y <= 0)%R; auto with real.
(* Goal: not (Rle (Rmult x y) (IZR Z0)) *)
apply Rlt_not_le;apply Rlt_sign_neg_neg; auto.
Qed.

Theorem Rge_sign_pos_pos_rev: forall x y: R, (x > 0 -> x * y >= 0 -> y >= 0)%R.
(* Goal: forall (x y : R) (_ : Rgt x (IZR Z0)) (_ : Rge (Rmult x y) (IZR Z0)), Rge y (IZR Z0) *)
intros x y H1 H2; apply Rle_ge; apply Rle_sign_pos_pos_rev with x; auto with real.
Qed.

Theorem Rge_sign_neg_neg_rev: forall x y: R, (0 > x -> x * y  >= 0->  0 >= y)%R.
(* Goal: forall (x y : R) (_ : Rgt (IZR Z0) x) (_ : Rge (Rmult x y) (IZR Z0)), Rge (IZR Z0) y *)
intros x y H1 H2; apply Rle_ge; apply Rle_sign_neg_neg_rev with x; auto with real.
Qed.

Theorem Rge_sign_pos_neg_rev: forall x y: R, (x > 0 -> 0 >= x * y -> 0 >= y)%R.
(* Goal: forall (x y : R) (_ : Rgt x (IZR Z0)) (_ : Rge (IZR Z0) (Rmult x y)), Rge (IZR Z0) y *)
intros x y H1 H2; apply Rle_ge; apply Rle_sign_pos_neg_rev with x; auto with real.
Qed.

Theorem Rge_sign_neg_pos_rev: forall x y: R, (0 > x -> 0 >= x * y ->  y >= 0)%R.
(* Goal: forall (x y : R) (_ : Rgt (IZR Z0) x) (_ : Rge (IZR Z0) (Rmult x y)), Rge y (IZR Z0) *)
intros x y H1 H2; apply Rle_ge; apply Rle_sign_neg_pos_rev with x; auto with real.
Qed.

Theorem Rlt_sign_pos_pos_rev: forall x y: R, (0 < x -> 0 < x * y -> 0 < y)%R.
(* Goal: forall (x y : R) (_ : Rlt x (IZR Z0)) (_ : Rlt (Rmult x y) (IZR Z0)), Rlt (IZR Z0) y *)
intros x y H1 H2; case (Rle_or_lt y 0); auto with real.
(* Goal: forall _ : Rle (IZR Z0) y, Rlt y (IZR Z0) *)
intros H3; absurd (0 < x * y)%R; auto with real.
(* Goal: not (Rlt (IZR Z0) (Rmult x y)) *)
apply Rle_not_lt;apply Rle_sign_pos_neg; auto with real.
Qed.

Theorem Rlt_sign_neg_neg_rev: forall x y: R, (x < 0 -> 0 < x * y ->  y < 0)%R.
(* Goal: forall (x y : R) (_ : Rlt (IZR Z0) x) (_ : Rlt (Rmult x y) (IZR Z0)), Rlt y (IZR Z0) *)
intros x y H1 H2; case (Rle_or_lt 0 y); auto with real.
(* Goal: forall _ : Rle (IZR Z0) y, Rlt y (IZR Z0) *)
intros H3; absurd (0 < x * y)%R; auto with real.
(* Goal: not (Rlt (IZR Z0) (Rmult x y)) *)
apply Rle_not_lt;apply Rle_sign_neg_pos; auto with real.
Qed.

Theorem Rlt_sign_pos_neg_rev: forall x y: R, (0 < x -> x * y < 0 -> y < 0)%R.
(* Goal: forall (x y : R) (_ : Rlt (IZR Z0) x) (_ : Rlt (Rmult x y) (IZR Z0)), Rlt y (IZR Z0) *)
intros x y H1 H2; case (Rle_or_lt 0 y); auto with real.
(* Goal: forall _ : Rle y (IZR Z0), Rlt (IZR Z0) y *)
intros H3; absurd (x * y < 0)%R; auto with real.
(* Goal: not (Rlt (Rmult x y) (IZR Z0)) *)
apply Rle_not_lt;apply Rle_sign_pos_pos; auto with real.
Qed.

Theorem Rlt_sign_neg_pos_rev: forall x y: R, (x < 0 -> x * y < 0 ->  0 < y)%R.
(* Goal: forall (x y : R) (_ : Rlt x (IZR Z0)) (_ : Rlt (Rmult x y) (IZR Z0)), Rlt (IZR Z0) y *)
intros x y H1 H2; case (Rle_or_lt y 0); auto with real.
(* Goal: forall _ : Rle y (IZR Z0), Rlt (IZR Z0) y *)
intros H3; absurd (x * y < 0)%R; auto with real.
(* Goal: not (Rlt (Rmult x y) (IZR Z0)) *)
apply Rle_not_lt;apply Rle_sign_neg_neg; auto with real.
Qed.

Theorem Rgt_sign_pos_pos_rev: forall x y: R, (x > 0 -> x * y > 0 -> y > 0)%R.
(* Goal: forall (x y : R) (_ : Rgt x (IZR Z0)) (_ : Rgt (Rmult x y) (IZR Z0)), Rgt y (IZR Z0) *)
intros x y H1 H2; red; apply Rlt_sign_pos_pos_rev with x; auto with real.
Qed.

Theorem Rgt_sign_neg_neg_rev: forall x y: R, (0 > x -> x * y  > 0->  0 > y)%R.
(* Goal: forall (x y : R) (_ : Rgt (IZR Z0) x) (_ : Rgt (Rmult x y) (IZR Z0)), Rgt (IZR Z0) y *)
intros x y H1 H2; red; apply Rlt_sign_neg_neg_rev with x; auto with real.
Qed.

Theorem Rgt_sign_pos_neg_rev: forall x y: R, (x > 0 -> 0 > x * y -> 0 > y)%R.
(* Goal: forall (x y : R) (_ : Rgt x (IZR Z0)) (_ : Rgt (IZR Z0) (Rmult x y)), Rgt (IZR Z0) y *)
intros x y H1 H2; red; apply Rlt_sign_pos_neg_rev with x; auto with real.
Qed.

Theorem Rgt_sign_neg_pos_rev: forall x y: R, (0 > x -> 0 > x * y ->  y > 0)%R.
(* Goal: forall (x y : R) (_ : Rgt (IZR Z0) x) (_ : Rgt (IZR Z0) (Rmult x y)), Rgt y (IZR Z0) *)
intros x y H1 H2; red; apply Rlt_sign_neg_pos_rev with x; auto with real.
Qed.

(* Theorem to simplify x * y ? x * z where ? is < > <= >= *)

Theorem Rmult_le_compat_l:
  forall n m p : R, (m <= n)%R -> (0 <= p)%R -> (p * m <= p * n)%R.
(* Goal: forall (n m : R) (_ : Rgt m n), Rgt (Ropp n) (Ropp m) *)
auto with real.
Qed.

Theorem Rmult_le_neg_compat_l:
  forall n m p : R, (m <= n)%R -> (p <= 0)%R -> (p * n <= p * m)%R.
(* Goal: forall (n m p : R) (_ : Rgt m n) (_ : Rgt (IZR Z0) p), Rgt (Rmult p n) (Rmult p m) *)
intros n m p H1 H2; replace (p * n)%R with (-(-p * n))%R; auto with real; try ring.
(* Goal: Rgt (Ropp (Rmult (Ropp p) n)) (Rmult p m) *)
replace (p * m)%R with (-(-p * m))%R; auto with real; try ring.
Qed.

Theorem Ropp_lt: forall n m, (m < n -> -n < -m)%R.
(* Goal: forall (n m : R) (_ : Rgt m n), Rgt (Ropp n) (Ropp m) *)
auto with real.
Qed.

Theorem Rmult_lt_neg_compat_l:
  forall n m p : R, (m < n)%R -> (p < 0)%R -> (p * n < p * m)%R.
(* Goal: forall (n m p : R) (_ : Rgt m n) (_ : Rgt (IZR Z0) p), Rgt (Rmult p n) (Rmult p m) *)
intros n m p H1 H2; replace (p * n)%R with (-(-p * n))%R; auto with real; try ring.
(* Goal: Rgt (Ropp (Rmult (Ropp p) n)) (Rmult p m) *)
replace (p * m)%R with (-(-p * m))%R; auto with real; try ring.
Qed.

Theorem Ropp_ge: forall n m, (m >= n -> -n >= -m)%R.
(* Goal: forall (n m : R) (_ : Rgt m n), Rgt (Ropp n) (Ropp m) *)
auto with real.
Qed.

Theorem Rmult_ge_compat_l:
  forall n m p : R, (m >= n)%R -> (p >= 0)%R -> (p * m >= p * n)%R.
(* Goal: forall (n m : R) (_ : Rgt m n), Rgt (Ropp n) (Ropp m) *)
intros n m p H H1; apply Rle_ge; auto with real.
Qed.

Theorem Rmult_ge_neg_compat_l:
  forall n m p : R, (m >= n)%R -> (0 >= p)%R -> (p * n >= p * m)%R.
(* Goal: forall (n m p : R) (_ : Rgt m n) (_ : Rgt (IZR Z0) p), Rgt (Rmult p n) (Rmult p m) *)
intros n m p H1 H2; replace (p * n)%R with (-(-p * n))%R; auto with real; try ring.
(* Goal: Rgt (Ropp (Rmult (Ropp p) n)) (Rmult p m) *)
replace (p * m)%R with (-(-p * m))%R; auto with real;try ring.
Qed.

Theorem Ropp_gt: forall n m, (m > n -> -n > -m)%R.
(* Goal: forall (n m : R) (_ : Rgt m n), Rgt (Ropp n) (Ropp m) *)
auto with real.
Qed.

Theorem Rmult_gt_compat_l:
  forall n m p : R, (n > m)%R -> (p > 0)%R -> (p * n > p * m)%R.
(* Goal: forall (n m : R) (_ : Rgt m n), Rgt (Ropp n) (Ropp m) *)
unfold Rgt; auto with real.
Qed.


Theorem Rmult_gt_neg_compat_l:
  forall n m p : R, (m > n)%R -> (0 > p)%R -> (p * n > p * m)%R.
(* Goal: forall (n m p : R) (_ : Rgt m n) (_ : Rgt (IZR Z0) p), Rgt (Rmult p n) (Rmult p m) *)
intros n m p H1 H2; replace (p * n)%R with (-(-p * n))%R; auto with real; try ring.
(* Goal: Rgt (Ropp (Rmult (Ropp p) n)) (Rmult p m) *)
replace (p * m)%R with (-(-p * m))%R; auto with real; try ring.
Qed.

(* Theorem to simplify a hyp x * y ? x * z where ? is < > <= >= *)


Theorem Rmult_le_compat_l_rev:
  forall n m p : R, (0 < p)%R -> (p * n <= p * m)%R -> (n <= m)%R.
(* Goal: forall (n m p : R) (_ : Rlt p (IZR Z0)) (_ : Rlt (Rmult p n) (Rmult p m)), Rlt m n *)
intros n m p H H1; case (Rle_or_lt n m); auto; intros H2.
(* Goal: forall (n m : R) (_ : Rgt m n), Rgt (Ropp n) (Ropp m) *)
absurd (p * n <= p * m)%R; auto with real.
(* Goal: not (Rle (Rmult p n) (Rmult p m)) *)
apply Rlt_not_le; apply Rmult_lt_compat_l; auto.
Qed.

Theorem Rmult_le_neg_compat_l_rev:
  forall n m p : R, (p < 0)%R -> (p * n <= p * m)%R -> (m <= n)%R.
(* Goal: forall (n m p : R) (_ : Rlt (IZR Z0) p) (_ : Rlt (Rmult p n) (Rmult p m)), Rlt n m *)
intros n m p H H1; case (Rle_or_lt m n); auto; intros H2.
(* Goal: forall (n m : R) (_ : Rgt m n), Rgt (Ropp n) (Ropp m) *)
absurd (p * n <= p * m)%R; auto with real.
(* Goal: not (Rle (Rmult p n) (Rmult p m)) *)
apply Rlt_not_le; apply Rmult_lt_neg_compat_l; auto.
Qed.

Theorem Rmult_lt_compat_l_rev:
  forall n m p : R, (0 < p)%R -> (p * n < p * m)%R -> (n < m)%R.
(* Goal: forall (n m p : R) (_ : Rlt (IZR Z0) p) (_ : Rlt (Rmult p n) (Rmult p m)), Rlt n m *)
intros n m p H H1; case (Rle_or_lt m n); auto; intros H2.
(* Goal: forall (n m : R) (_ : Rgt m n), Rgt (Ropp n) (Ropp m) *)
absurd (p * n < p * m)%R; auto with real.
(* Goal: forall (n m : R) (_ : Rgt m n), Rgt (Ropp n) (Ropp m) *)
apply Rle_not_lt; apply Rmult_le_compat_l; auto with real.
Qed.

Theorem Rmult_lt_neg_compat_l_rev:
  forall n m p : R, (p < 0)%R -> (p * n < p * m)%R -> (m < n)%R.
(* Goal: forall (n m p : R) (_ : Rlt p (IZR Z0)) (_ : Rlt (Rmult p n) (Rmult p m)), Rlt m n *)
intros n m p H H1; case (Rle_or_lt n m); auto; intros H2.
(* Goal: forall (n m : R) (_ : Rgt m n), Rgt (Ropp n) (Ropp m) *)
absurd (p * n < p * m)%R; auto with real.
(* Goal: forall (n m : R) (_ : Rgt m n), Rgt (Ropp n) (Ropp m) *)
apply Rle_not_lt; apply Rmult_le_neg_compat_l; auto with real.
Qed.

Theorem Rmult_ge_compat_l_rev:
  forall n m p : R, (p > 0)%R -> (p * n >= p * m)%R -> (n >= m)%R.
intros n m p H H1;
(* Goal: forall (n m : R) (_ : Rgt m n), Rgt (Ropp n) (Ropp m) *)
 apply Rle_ge; apply Rmult_le_compat_l_rev with p; auto with real.
Qed.

Theorem Rmult_ge_neg_compat_l_rev:
  forall n m p : R, (0 > p)%R -> (p * n >= p * m)%R -> (m >= n)%R.
intros n m p H H1;
(* Goal: forall (n m : R) (_ : Rgt m n), Rgt (Ropp n) (Ropp m) *)
 apply Rle_ge; apply Rmult_le_neg_compat_l_rev with p; auto with real.
Qed.

Theorem Rmult_gt_compat_l_rev:
  forall n m p : R, (p > 0)%R -> (p * n > p * m)%R -> (n > m)%R.
intros n m p H H1;
(* Goal: forall (n m : R) (_ : Rgt m n), Rgt (Ropp n) (Ropp m) *)
 red; apply Rmult_lt_compat_l_rev with p; auto with real.
Qed.

Theorem Rmult_gt_neg_compat_l_rev:
  forall n m p : R, (0 > p)%R -> (p * n > p * m)%R -> (m > n)%R.
intros n m p H H1;
(* Goal: forall (n m : R) (_ : Rgt m n), Rgt (Ropp n) (Ropp m) *)
 red; apply Rmult_lt_neg_compat_l_rev with p; auto with real.
Qed.

(* For replace *)

Theorem eq_Rlt_trans_l : forall x y z, (x = z) -> (x < y) -> (z < y).
(* Goal: forall (x y z : R) (_ : @eq R y z) (_ : Rge x y), Rge x z *)
intros x y z H; rewrite H; auto.
Qed.
Theorem eq_Rlt_trans_r : forall x y z, (y = z) -> (x < y) -> (x < z).
(* Goal: forall (x y z : R) (_ : @eq R y z) (_ : Rge x y), Rge x z *)
intros x y z H; rewrite H; auto.
Qed.
Theorem eq_Rgt_trans_l : forall x y z, (x = z) -> (x > y) -> (z > y).
(* Goal: forall (x y z : R) (_ : @eq R y z) (_ : Rge x y), Rge x z *)
intros x y z H; rewrite H; auto.
Qed.
Theorem eq_Rgt_trans_r : forall x y z, (y = z) -> (x > y) -> (x > z).
(* Goal: forall (x y z : R) (_ : @eq R y z) (_ : Rge x y), Rge x z *)
intros x y z H; rewrite H; auto.
Qed.
Theorem eq_Rle_trans_l : forall x y z, (x = z) -> (x <= y) -> (z <= y).
(* Goal: forall (x y z : R) (_ : @eq R y z) (_ : Rge x y), Rge x z *)
intros x y z H; rewrite H; auto.
Qed.
Theorem eq_Rle_trans_r : forall x y z, (y = z) -> (x <= y) -> (x <= z).
(* Goal: forall (x y z : R) (_ : @eq R y z) (_ : Rge x y), Rge x z *)
intros x y z H; rewrite H; auto.
Qed.
Theorem eq_Rge_trans_l : forall x y z, (x = z) -> (x >= y) -> (z >= y).
(* Goal: forall (x y z : R) (_ : @eq R y z) (_ : Rge x y), Rge x z *)
intros x y z H; rewrite H; auto.
Qed.
Theorem eq_Rge_trans_r : forall x y z, (y = z) -> (x >= y) -> (x >= z).
(* Goal: forall (x y z : R) (_ : @eq R y z) (_ : Rge x y), Rge x z *)
intros x y z H; rewrite H; auto.
Qed.

Theorem Rge_trans: forall x y z, (x >= z) -> (z >= y) -> (x >= y).
(* Goal: forall (x y z : R) (_ : Rge x z) (_ : Rge z y), Rge x y *)
intros x y z H1 H2; red; apply Rge_trans with z; auto.
Qed.

(* For RGroundTac *)


Theorem Z2R_correct: forall p, (Z2R p) = (IZR p).
(* Goal: forall p : Z, @eq R (Z2R p) (IZR p) *)
intros p; case p; auto.
(* Goal: forall p : positive, @eq R (Z2R (Zneg p)) (IZR (Zneg p)) *)
intros p1; elim p1; auto.
(* Goal: forall (n m : Z) (_ : Z.gt m n), Z.gt (Z.opp n) (Z.opp m) *)
intros p2 Rec; pattern (Zpos (xI p2)) at 2; replace (Zpos (xI p2)) with (2 * (Zpos p2) +1)%Z; auto with zarith.
(* Goal: @eq R (Z2R (Zneg (xI p2))) (IZR (Z.add (Z.mul (Zpos (xO xH)) (Zneg p2)) (Zneg xH))) *)
(* Goal: forall (p : positive) (_ : @eq R (Z2R (Zneg p)) (IZR (Zneg p))), @eq R (Z2R (Zneg (xO p))) (IZR (Zneg (xO p))) *)
rewrite plus_IZR; rewrite mult_IZR; rewrite <- Rec.
(* Goal: @eq R (Z2R (Zneg (xO p2))) (Rmult (IZR (Zpos (xO xH))) (Z2R (Zneg p2))) *)
simpl Z2R; simpl IZR; case p2; intros; simpl (P2R 1);ring.
(* Goal: forall (n m : Z) (_ : Z.gt m n), Z.gt (Z.opp n) (Z.opp m) *)
intros p2 Rec; pattern (Zpos (xO p2)) at 2; replace (Zpos (xO p2)) with (2 * (Zpos p2))%Z; auto with zarith.
(* Goal: @eq R (Z2R (Zneg (xO p2))) (IZR (Z.mul (Zpos (xO xH)) (Zneg p2))) *)
rewrite mult_IZR; rewrite <- Rec.
(* Goal: @eq R (Z2R (Zneg (xO p2))) (Rmult (IZR (Zpos (xO xH))) (Z2R (Zneg p2))) *)
simpl Z2R; simpl IZR; case p2; intros; simpl (P2R 1); ring.
(* Goal: forall p : positive, @eq R (Z2R (Zneg p)) (IZR (Zneg p)) *)
intros p1; elim p1; auto.
(* Goal: forall (n m : Z) (_ : Z.gt m n), Z.gt (Z.opp n) (Z.opp m) *)
intros p2 Rec; pattern (Zneg (xI p2)) at 2; replace (Zneg (xI p2)) with ((2 * (Zneg p2) +  -1))%Z; auto with zarith.
(* Goal: @eq R (Z2R (Zneg (xI p2))) (IZR (Z.add (Z.mul (Zpos (xO xH)) (Zneg p2)) (Zneg xH))) *)
(* Goal: forall (p : positive) (_ : @eq R (Z2R (Zneg p)) (IZR (Zneg p))), @eq R (Z2R (Zneg (xO p))) (IZR (Zneg (xO p))) *)
rewrite plus_IZR; rewrite mult_IZR; rewrite <- Rec.
(* Goal: @eq R (Z2R (Zneg (xO p2))) (Rmult (IZR (Zpos (xO xH))) (Z2R (Zneg p2))) *)
simpl Z2R; simpl IZR; case p2; intros; simpl (P2R 1); ring.
(* Goal: forall (n m : Z) (_ : Z.gt m n), Z.gt (Z.opp n) (Z.opp m) *)
intros p2 Rec; pattern (Zneg (xO p2)) at 2; replace (Zneg (xO p2)) with (2 * (Zneg p2))%Z; auto with zarith.
(* Goal: @eq R (Z2R (Zneg (xO p2))) (IZR (Z.mul (Zpos (xO xH)) (Zneg p2))) *)
rewrite mult_IZR; rewrite <- Rec.
(* Goal: @eq R (Z2R (Zneg (xO p2))) (Rmult (IZR (Zpos (xO xH))) (Z2R (Zneg p2))) *)
simpl Z2R; simpl IZR; case p2; intros; simpl (P2R 1); ring.
Qed.

Theorem Z2R_le: forall p q, (p <= q)%Z -> (Z2R p <= Z2R q)%R.
(* Goal: forall (p q : Z) (_ : Z.le p q), Rle (Z2R p) (Z2R q) *)
intros p q; repeat rewrite Z2R_correct; intros; apply IZR_le; auto.
Qed.

Theorem Z2R_lt: forall p q, (p < q)%Z -> (Z2R p < Z2R q)%R.
(* Goal: forall (p q : Z) (_ : Z.lt p q), Rlt (Z2R p) (Z2R q) *)
intros p q; repeat rewrite Z2R_correct; intros; apply IZR_lt; auto.
Qed.

Theorem Z2R_ge: forall p q, (p >= q)%Z -> (Z2R p >= Z2R q)%R.
(* Goal: forall (p q : Z) (_ : Z.ge p q), Rge (Z2R p) (Z2R q) *)
intros p q; repeat rewrite Z2R_correct; intros; apply IZR_ge; auto.
Qed.

Theorem Z2R_gt: forall p q, (p > q)%Z -> (Z2R p > Z2R q)%R.
intros p q; repeat rewrite Z2R_correct; intros; red; apply IZR_lt;
apply Z.gt_lt; auto.
Qed.

Close Scope R_scope.
