(* Copyright 2001, 2009 Luc Rutten  This file is part of ModRed.  ModRed 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 3 of the License, or (at your option) any later version.  ModRed 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 Lesser General Public License for more details.  You should have received a copy of the GNU Lesser General Public License along with ModRed.  If not, see <http://www.gnu.org/licenses/>. *)
(* Preparations for fast modular reduction lemmas. *)
(* Coq 8.2 *)
Open Scope Z_scope.
Definition in_Z_ (m x : Z) := 0 <= x < m.
Definition Z_ (m : Z) := sig (in_Z_ m).
Coercion Z_from_Z_ (m : Z) (x : Z_ m) := proj1_sig (P := in_Z_ m) x.
Notation _Z := (Z_from_Z_ _).
Definition in_Zpls (x : Z) := 0 < x.
Definition Zpls := sig in_Zpls.
Coercion Zpls_ (x : Zpls) := proj1_sig (P := in_Zpls) x.
intros n i. case i. unfold in_Z_. simpl. tauto. Qed.
intros n i. case i. unfold in_Z_. simpl. tauto. Qed.
intros x m H. elim (Z_mod_lt x m); omega. Qed.
intros x m H. elim (Z_mod_lt x m); omega. Qed.
intros x m H H0. cut (m * (x / m) + x mod m < m). intro H1. cut (m * (x / m) < m).
intro H2. elim (Zle_or_lt 0 (x / m)); intro H3. elim (Zle_lt_or_eq 0 (x / m)). intro H4.
cut (m * 1 <= m * (x / m)). intro H5. absurd (m * (x / m) < m). omega. assumption.
apply Zmult_le_compat_l. omega. omega. omega. assumption. absurd (x / m < 0). apply Zle_not_lt.
apply Z.ge_le. apply Z_div_ge0; omega. assumption. cut (0 <= x mod m). omega. elim (Z_mod_lt x m). omega.
omega. rewrite <- Z_div_mod_eq. assumption. omega. Qed.
intros a m H H0. apply Z.ge_le. apply Z_div_ge0. omega. omega. Qed.
intros. apply Z.ge_le. apply Z_div_ge. omega. omega. Qed.
intros x m H. replace (x + m) with (x + 1 * m). apply Z_mod_plus. omega. ring. Qed.
intros x m H. replace (x - m) with (x + -1 * m). apply Z_mod_plus. omega. ring. Qed.
intros. rewrite Zplus_comm. apply Z_mod_plus. omega. Qed.
intros. unfold Zminus. rewrite <- Zopp_mult_distr_l_reverse. apply Z_mod_plus. omega. Qed.
intros. rewrite Zmult_comm. rewrite <- Z_div_mod_eq. trivial. omega. Qed.
intros a b m H. replace (a + b) with (a / m * m + a mod m + b). rewrite Zplus_comm.
rewrite Zplus_comm with (m := a mod m). rewrite Zplus_assoc. rewrite Z_mod_plus.
rewrite Zplus_comm. trivial. omega. rewrite Zdivmod_split. trivial. assumption. Qed.
intros a b m H. replace (a * b) with ((a / m * m + a mod m) * b).
rewrite Zmult_plus_distr_l. rewrite Zmod_plus_distr_l.
replace (a / m * m * b) with (0 + a / m * b * m). rewrite Z_mod_plus. trivial. omega.
ring. assumption. rewrite Zdivmod_split. trivial. assumption. Qed.
unfold Zminus. intros. apply Zmod_plus_distr_l. assumption. Qed.
intros a b m H. unfold Zminus. replace (- b) with (b * -1). do 2 rewrite Zplus_comm with (n := a).
rewrite Zmod_plus_distr_l. rewrite Zmod_mult_distr_l. rewrite <- Zmod_plus_distr_l.
apply f_equal2 with (f := Zmod). ring. trivial. assumption. assumption. assumption. ring. Qed.
intros x m H H0. rewrite <- Zdivmod_split with (x := x) (m := m).
replace ((x / m * m + x mod m) mod m) with (x mod m).
rewrite Zdiv_small_0; auto. rewrite Zdivmod_split. trivial. omega. omega. Qed.
intros a b H. cut (a / b * b + a mod b = a). omega. apply Zdivmod_split. assumption. Qed.
intros. rewrite Zdiv_a_b_b_Zmod. ring. assumption. Qed.
intros x y z H H0. rewrite <- Zminus_Zdiv_Zmult with (a := x). rewrite Zmult_assoc.
apply Zmod_minus_km. assumption. fold (0 * 0). apply Zmult_lt_0_compat; omega. Qed.
intros. rewrite Zplus_comm. rewrite Z_div_plus. rewrite Zdiv_small_0. ring.
assumption. assumption. omega. Qed.
intros. replace (a * m - b) with ((a - 1) * m + (m - b)). apply Zdiv_mult_plus. auto. omega.
omega. ring. Qed.
intros. rewrite Zplus_comm. rewrite Z_div_plus. ring. omega. Qed.
intro a. replace a with (a * 1). rewrite Z_div_mult. ring. omega. ring. Qed.
intro z. cut (0 <= z mod 1). cut (z mod 1 < 1). omega. apply Zmod_lt_z_m. omega. apply Zmod_le_0_z. omega. Qed.
Set Implicit Arguments.
Unset Strict Implicit.
Definition If (T : Set) (b : bool) (t e : T) :=
(* If T b t e  =  if b then t else e *)
match b with
| true => t
| false => e
end.
Set Strict Implicit.
Unset Implicit Arguments.
simple destruct x. simpl. omega. intros. simpl. symmetry.
apply Zpos_eq_Z_of_nat_o_nat_of_P. intros p H. absurd (0 <= Zneg p). unfold Z.le. tauto.
assumption. Qed.
simple induction i. replace (Zpower_nat 2 0) with 1. omega. trivial. intros n H.
rewrite <- two_power_nat_correct. rewrite two_power_nat_S. rewrite two_power_nat_correct.
omega. Qed.
induction i. simpl. trivial. rewrite <- two_power_nat_correct. rewrite two_power_nat_S.
rewrite two_power_nat_correct. rewrite IHi. replace (Z_of_nat (S i)) with (1 + Z_of_nat i).
rewrite Zpower_exp. replace (2 ^ 1) with 2. trivial. trivial. omega. omega. rewrite inj_S.
omega. Qed.
intros i H. rewrite Zpower_nat_Zpower. rewrite Z_to_inject_nat. trivial. assumption. Qed.
intros x H. rewrite Zpower_Zpower_nat. apply Zlt_0_power_nat. assumption. Qed.
intros i j Hi Hij. replace j with (i + (j - i)). rewrite Zpower_exp.
apply Z.le_lt_trans with (2 ^ i * 2 ^ 0). simpl. omega. apply Zmult_lt_compat_l. apply lt_0_Zpow.
assumption. replace (j - i) with (j - i - 1 + 1).  rewrite Zpower_exp. replace (2 ^ 1) with 2.
replace (2 ^ 0) with 1. cut (0 < 2 ^ (j - i - 1)). omega. apply lt_0_Zpow. omega. trivial. trivial.
omega. omega.  ring. omega. omega. ring. Qed.
intros i j Hi Hij. elim (Zle_lt_or_eq i j). intro. apply Zlt_le_weak. apply Zlt_pow_lt.
assumption. assumption. intro H. rewrite H. omega. assumption. Qed.
intros i j Hi Hj Hij. elim (Zle_lt_or_eq i j). trivial. intro H. rewrite H in Hij.
absurd (2 ^ j < 2 ^ j). omega. assumption. elim (Zle_or_lt i j). trivial. intro H. cut (2 ^ j < 2 ^ i).
intro H0. absurd (2 ^ j < 2 ^ i); omega. apply Zlt_pow_lt; omega. Qed.
Definition Zlog_sup (M : Z) :=
match M with
| Zpos p => log_sup p
| _ => 0
end.
intros i H. replace (2 ^ i) with (Zpos (shift_nat (Z.abs_nat i) 1)). simpl. rewrite log_sup_shift_nat.
apply Z_to_inject_nat. assumption. rewrite shift_nat_correct. rewrite Zpower_nat_Zpower.
rewrite Z_to_inject_nat. ring. assumption. Qed.

Lemma two_p_pow : forall z : Z, 0 <= z -> two_p z = 2 ^ z.