(* This program is free software; you can redistribute it and/or      *)
(* modify it under the terms of the GNU Lesser General Public License *)
(* as published by the Free Software Foundation; either version 2.1   *)
(* of the License, or (at your option) any later version.             *)
(*                                                                    *)
(* This program is distributed in the hope that it will be useful,    *)
(* but WITHOUT ANY WARRANTY; without even the implied warranty of     *)
(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the      *)
(* GNU General Public License for more details.                       *)
(*                                                                    *)
(* You should have received a copy of the GNU Lesser General Public   *)
(* License along with this program; if not, write to the Free         *)
(* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *)
(* 02110-1301 USA                                                     *)


(* Contribution to the Coq Library   V6.3 (July 1999)                    *)

(* ---                     Mult_compl.v                                 ----*)
(*                                                                          *)
(* Author: Pierre Casteran.                                                 *)
(*    LABRI, URA CNRS 1304,                                                 *)
(*    Departement d'Informatique, Universite Bordeaux I,                    *)
(*    33405 Talence CEDEX,                                                  *)
(*    e-mail:  casteran@labri.u-bordeaux.fr                                 *)


(*
   This file contains some lemmas on multiplication; it is an 
   extension of the module Mult.v *)

Require Import Arith.
Require Import Constants.
Require Import Le_lt_compl.

(* multiplication is monotonous *)

Lemma mult_le_l : forall a b c : nat, b <= c -> a * b <= a * c.
(******************************************)
Proof.
(* Goal: forall (a b c : nat) (_ : le b c), le (Init.Nat.mul a b) (Init.Nat.mul a c) *)
 simple induction a; simpl in |- *; [ auto with arith | intros ].
(* Goal: le (Init.Nat.add b (Init.Nat.mul n b)) (Init.Nat.add c (Init.Nat.mul n c)) *)
 apply plus_le_compat; auto with arith.
Qed.
Hint Resolve mult_le_l: arith.


(* multiplication is strictly monotonous *)

Lemma mult_lt_l : forall a b c : nat, 0 < a -> b < c -> a * b < a * c.
(******************************************)
Proof.
(* Goal: forall (a b c : nat) (_ : lt O a) (_ : lt b c), lt (Init.Nat.mul a b) (Init.Nat.mul a c) *)
 simple induction a.
(* Goal: forall (b c : nat) (_ : lt O O) (_ : lt b c), lt (Init.Nat.mul O b) (Init.Nat.mul O c) *)
(* Goal: forall (n : nat) (_ : forall (b c : nat) (_ : lt O n) (_ : lt b c), lt (Init.Nat.mul n b) (Init.Nat.mul n c)) (b c : nat) (_ : lt O (S n)) (_ : lt b c), lt (Init.Nat.mul (S n) b) (Init.Nat.mul (S n) c) *)
 intros; absurd (0 < 0); auto with arith.
(* Goal: forall (n : nat) (_ : forall (b c : nat) (_ : lt O n) (_ : lt b c), lt (Init.Nat.mul n b) (Init.Nat.mul n c)) (b c : nat) (_ : lt O (S n)) (_ : lt b c), lt (Init.Nat.mul (S n) b) (Init.Nat.mul (S n) c) *)
 intros; simpl in |- *.
(* Goal: lt (Init.Nat.add b (Init.Nat.mul n b)) (Init.Nat.add c (Init.Nat.mul n c)) *)
 apply lt_le_trans with (c + n * b).
 rewrite (plus_comm b (n * b)); rewrite (plus_comm c (n * b));
  apply plus_lt_compat_l; auto with arith.
(* Goal: le (Init.Nat.add c (Init.Nat.mul n b)) (Init.Nat.add c (Init.Nat.mul n c)) *)
 apply plus_le_compat_l; auto with arith.
Qed.

Hint Resolve mult_lt_l: arith.

(* same properties at right *)

Lemma mult_le_r : forall a b c : nat, b <= c -> b * a <= c * a.
(*****************************************)
Proof.
(* Goal: forall (a b c : nat) (_ : lt O a) (_ : lt b c), lt (Init.Nat.mul b a) (Init.Nat.mul c a) *)
 intros; rewrite (mult_comm b a); rewrite (mult_comm c a); auto with arith.
Qed.
Hint Resolve mult_le_r: arith.


Lemma mult_lt_r : forall a b c : nat, 0 < a -> b < c -> b * a < c * a.
(******************************************)
Proof.
(* Goal: forall (a b c : nat) (_ : lt O a) (_ : lt b c), lt (Init.Nat.mul b a) (Init.Nat.mul c a) *)
 intros; rewrite (mult_comm b a); rewrite (mult_comm c a); auto with arith.
Qed.
Hint Resolve mult_lt_r: arith.


Lemma mult_inj_l : forall a b c : nat, a * c < b * c -> a < b.
(********************************************)
Proof.
(* Goal: forall (p q : nat) (_ : lt O q), le p (Init.Nat.mul q p) *)
 intros.
(* Goal: lt a b *)
 case (le_or_lt b a); [ intro | auto with arith ].
(* Goal: lt a b *)
 absurd (b * c <= a * c); auto with arith.
Qed.


Lemma mult_p_lt_qp : forall p q : nat, 0 < p -> one < q -> p < q * p.  
(************************************)
Proof.
(* Goal: forall (p q : nat) (_ : lt O q), le p (Init.Nat.mul q p) *)
 intros.
(* Goal: lt p (Init.Nat.mul q p) *)
 rewrite mult_comm.
(* Goal: lt p (Nat.mul p q) *)
 pattern p at 1 in |- *; rewrite <- mult_1_r; auto with arith.
Qed.

Lemma mult_p_le_qp : forall p q : nat, 0 < q -> p <= q * p.
(************************************)
Proof.
(* Goal: forall (p q : nat) (_ : lt O q), le p (Init.Nat.mul q p) *)
 intros.
(* Goal: le p (Init.Nat.mul q p) *)
 pattern p at 1 in |- *; rewrite <- mult_1_r.
(* Goal: le (Nat.mul p (S O)) (Init.Nat.mul q p) *)
 rewrite <- (mult_comm p q); auto with arith.
Qed.

Hint Resolve mult_p_lt_qp mult_p_le_qp: arith.
 
Lemma mult_p_lt_pq : forall p q : nat, 0 < p -> one < q -> p < p * q.  
(***********************************)
Proof.
(* Goal: forall (p q : nat) (_ : lt O q), le p (Init.Nat.mul p q) *)
 intros; rewrite mult_comm; auto with arith.
Qed.


Lemma mult_p_le_pq : forall p q : nat, 0 < q -> p <= p * q.
(************************************)
Proof.
(* Goal: forall (p q : nat) (_ : lt O q), le p (Init.Nat.mul p q) *)
 intros; rewrite mult_comm; auto with arith.
Qed.

Hint Resolve mult_p_lt_pq mult_p_le_pq: arith.


(* exact quotient 
******************)


Lemma quotient_positive : forall q n n0 : nat, n = q * n0 -> 0 < n -> 0 < q.
(***************************************)
Proof.
(* Goal: forall (q n n0 : nat) (_ : @eq nat n (Init.Nat.mul q n0)) (_ : lt O n), lt O q *)
 intros q n n0 H H0.
(* Goal: lt O q *)
 case (lt_or_Zero q); [ auto with arith | intro H1 ].
(* Goal: lt O q *)
 absurd (0 < q * n0).
(* Goal: not (lt O (Init.Nat.mul q n0)) *)
(* Goal: lt O (Init.Nat.mul q n0) *)
 rewrite H1; simpl in |- *; auto with arith.
(* Goal: lt O (Init.Nat.mul q n0) *)
 rewrite <- H; auto with arith.
Qed.

Lemma quotient_gt_one : forall q n n0 : nat, n0 < n -> n = q * n0 -> one < q.
(*************************************************)
Proof.
 intros q n n0 H H0; case (one_cases q);
  [ auto with arith
  | simple induction 1; intro; absurd (n0 < n); auto with arith ].
(* Goal: not (lt n0 n) *)
(* Goal: not (lt n0 n) *)
 rewrite H0; rewrite H2; auto with arith.
(* Goal: not (lt n0 n) *)
 rewrite H0; rewrite H2; simpl in |- *; rewrite <- plus_n_O; auto with arith.
Qed.






