(* 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/>. *)
(* Unsigned arithmetic where w denotes a processor's word size. *)
(* Defines plusw (addition), minusw (subtraction), multw (low-word multiplication), uhwm (high-word multiplication), multp2 (left shift, or multiplication by a power of two), ursh (right shift, or division by a power of two), and ltw (comparison and choice). *)
(* Coq 8.2 *)
Open Scope Z_scope.
(* Lemma is_in_Zpls_64 : in_Zpls 64.  unfold in_Zpls. omega. Qed.  Definition in_Zpls_64 := exist in_Zpls 64 is_in_Zpls_64.  Definition w_def : Zpls.  exact in_Zpls_64. Qed. *)
Local Parameter w_def : Zpls.
Definition w := proj1_sig w_def.
(* Defining w like this is not as elegant as using a Variable in {w:Z | `2 < w`} but it allows easier splitting of modules because w does not have to be used as a parameter. When w_def is replaced with a Transparent definition, it also enables performing "real" computations. *)
unfold w. case w_def. trivial. Qed.
apply Zlt_le_weak. exact lt_0_w. Qed.
apply lt_0_Zpow. exact le_0_w. Qed.
unfold in_Z_. split. omega. exact lt02w. Qed.
Definition zero_in_Z_ := exist (in_Z_ (2 ^ w)) 0 zero_is_in_Z_.
transitivity (2 ^ (1 + (w - 1))). rewrite Zplus_minus. trivial. rewrite Zpower_exp.
ring. omega. cut (0 < w). omega. exact lt_0_w. Qed.
rewrite Zpow_2_w_is_2Zpow_2_wm1. cut (0 < 2 ^ (w - 1)). omega. apply lt_0_Zpow. cut (0 < w). omega.
exact lt_0_w. Qed.

Lemma mod_in_Z__ : forall z : Z, in_Z_ (2 ^ w) (z mod 2 ^ w).