1
(************************************************************************)
2
(* v * The Coq Proof Assistant / The Coq Development Team *)
3
(* <O___,, * CNRS-Ecole Polytechnique-INRIA Futurs-Universite Paris Sud *)
4
(* \VV/ **************************************************************)
5
(* // * This file is distributed under the terms of the *)
6
(* * GNU Lesser General Public License Version 2.1 *)
7
(************************************************************************)
8
(*i $Id: Zmax.v 10291 2007-11-06 02:18:53Z letouzey $ i*)
10
Require Import Arith_base.
11
Require Import BinInt.
12
Require Import Zcompare.
13
Require Import Zorder.
15
Open Local Scope Z_scope.
17
(******************************************)
18
(** Maximum of two binary integer numbers *)
20
Definition Zmax m n :=
26
(** * Characterization of maximum on binary integer numbers *)
28
Lemma Zmax_case : forall (n m:Z) (P:Z -> Type), P n -> P m -> P (Zmax n m).
30
intros n m P H1 H2; unfold Zmax in |- *; case (n ?= m); auto with arith.
33
Lemma Zmax_case_strong : forall (n m:Z) (P:Z -> Type),
34
(m<=n -> P n) -> (n<=m -> P m) -> P (Zmax n m).
36
intros n m P H1 H2; unfold Zmax, Zle, Zge in *.
37
rewrite <- (Zcompare_antisym n m) in H1.
38
destruct (n ?= m); (apply H1|| apply H2); discriminate.
41
Lemma Zmax_spec : forall x y:Z,
42
x >= y /\ Zmax x y = x \/
43
x < y /\ Zmax x y = y.
45
intros; unfold Zmax, Zlt, Zge.
46
destruct (Zcompare x y); [ left | right | left ]; split; auto; discriminate.
49
Lemma Zmax_left : forall n m:Z, n>=m -> Zmax n m = n.
51
intros n m; unfold Zmax, Zge; destruct (n ?= m); auto.
52
intro H; elim H; auto.
55
Lemma Zmax_right : forall n m:Z, n<=m -> Zmax n m = m.
57
intros n m; unfold Zmax, Zle.
58
generalize (Zcompare_Eq_eq n m).
59
destruct (n ?= m); auto.
60
intros _ H; elim H; auto.
63
(** * Least upper bound properties of max *)
65
Lemma Zle_max_l : forall n m:Z, n <= Zmax n m.
67
intros; apply Zmax_case_strong; auto with zarith.
70
Notation Zmax1 := Zle_max_l (only parsing).
72
Lemma Zle_max_r : forall n m:Z, m <= Zmax n m.
74
intros; apply Zmax_case_strong; auto with zarith.
77
Notation Zmax2 := Zle_max_r (only parsing).
79
Lemma Zmax_lub : forall n m p:Z, n <= p -> m <= p -> Zmax n m <= p.
81
intros; apply Zmax_case; assumption.
84
(** * Semi-lattice properties of max *)
86
Lemma Zmax_idempotent : forall n:Z, Zmax n n = n.
88
intros; apply Zmax_case; auto.
91
Lemma Zmax_comm : forall n m:Z, Zmax n m = Zmax m n.
93
intros; do 2 apply Zmax_case_strong; intros;
94
apply Zle_antisym; auto with zarith.
97
Lemma Zmax_assoc : forall n m p:Z, Zmax n (Zmax m p) = Zmax (Zmax n m) p.
99
intros n m p; repeat apply Zmax_case_strong; intros;
100
reflexivity || (try apply Zle_antisym); eauto with zarith.
103
(** * Additional properties of max *)
105
Lemma Zmax_irreducible_inf : forall n m:Z, Zmax n m = n \/ Zmax n m = m.
107
intros; apply Zmax_case; auto.
110
Lemma Zmax_le_prime_inf : forall n m p:Z, p <= Zmax n m -> p <= n \/ p <= m.
112
intros n m p; apply Zmax_case; auto.
115
(** * Operations preserving max *)
117
Lemma Zsucc_max_distr :
118
forall n m:Z, Zsucc (Zmax n m) = Zmax (Zsucc n) (Zsucc m).
120
intros n m; unfold Zmax in |- *; rewrite (Zcompare_succ_compat n m);
121
elim_compare n m; intros E; rewrite E; auto with arith.
124
Lemma Zplus_max_distr_r : forall n m p:Z, Zmax (n + p) (m + p) = Zmax n m + p.
126
intros x y n; unfold Zmax in |- *.
127
rewrite (Zplus_comm x n); rewrite (Zplus_comm y n);
128
rewrite (Zcompare_plus_compat x y n).
129
case (x ?= y); apply Zplus_comm.
132
(** * Maximum and Zpos *)
134
Lemma Zpos_max : forall p q, Zpos (Pmax p q) = Zmax (Zpos p) (Zpos q).
136
intros; unfold Zmax, Pmax; simpl; generalize (Pcompare_Eq_eq p q).
137
destruct Pcompare; auto.
138
intro H; rewrite H; auto.
141
Lemma Zpos_max_1 : forall p, Zmax 1 (Zpos p) = Zpos p.
143
intros; unfold Zmax; simpl; destruct p; simpl; auto.
146
(** * Characterization of Pminus in term of Zminus and Zmax *)
148
Lemma Zpos_minus : forall p q, Zpos (Pminus p q) = Zmax 1 (Zpos p - Zpos q).
151
case_eq (Pcompare p q Eq).
152
intros H; rewrite (Pcompare_Eq_eq _ _ H).
155
unfold Pminus; rewrite Pminus_mask_diag; auto.
156
intros; rewrite Pminus_Lt; auto.
157
destruct (Zmax_spec 1 (Zpos p - Zpos q)) as [(H1,H2)|(H1,H2)]; auto.
158
elimtype False; clear H2.
159
assert (H1':=Zlt_trans 0 1 _ Zlt_0_1 H1).
160
generalize (Zlt_0_minus_lt _ _ H1').
162
rewrite (ZC2 _ _ H); intro; discriminate.
163
intros; simpl; rewrite H.
164
symmetry; apply Zpos_max_1.