summaryrefslogtreecommitdiff
path: root/theories7/ZArith/ZArith_dec.v
blob: 985f7601103a0041bab0c9d681e40dca6cd7667b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
(************************************************************************)
(*  v      *   The Coq Proof Assistant  /  The Coq Development Team     *)
(* <O___,, * CNRS-Ecole Polytechnique-INRIA Futurs-Universite Paris Sud *)
(*   \VV/  **************************************************************)
(*    //   *      This file is distributed under the terms of the       *)
(*         *       GNU Lesser General Public License Version 2.1        *)
(************************************************************************)

(*i $Id: ZArith_dec.v,v 1.1.2.1 2004/07/16 19:31:42 herbelin Exp $ i*)

Require Sumbool.

Require BinInt.
Require Zorder.
Require Zcompare.
Require Zsyntax.
V7only [Import Z_scope.].
Open Local Scope Z_scope.

Lemma Dcompare_inf : (r:relation) {r=EGAL} + {r=INFERIEUR} + {r=SUPERIEUR}.
Proof.
Induction r; Auto with arith. 
Defined.

Lemma Zcompare_rec :
  (P:Set)(x,y:Z)
    ((Zcompare x y)=EGAL -> P) ->
    ((Zcompare x y)=INFERIEUR -> P) ->
    ((Zcompare x y)=SUPERIEUR -> P) ->
    P.
Proof.
Intros P x y H1 H2 H3.
Elim (Dcompare_inf (Zcompare x y)).
Intro H. Elim H; Auto with arith. Auto with arith.
Defined.

Section decidability.

Variables x,y : Z.

(** Decidability of equality on binary integers *)

Definition Z_eq_dec : {x=y}+{~x=y}.
Proof.
Apply Zcompare_rec with x:=x y:=y.
Intro. Left. Elim (Zcompare_EGAL x y); Auto with arith.
Intro H3. Right. Elim (Zcompare_EGAL x y). Intros H1 H2. Unfold not. Intro H4.
  Rewrite (H2 H4) in H3. Discriminate H3.
Intro H3. Right. Elim (Zcompare_EGAL x y). Intros H1 H2. Unfold not. Intro H4.
  Rewrite (H2 H4) in H3. Discriminate H3.
Defined. 

(** Decidability of order on binary integers *)

Definition Z_lt_dec : {(Zlt x y)}+{~(Zlt x y)}.
Proof.
Unfold Zlt.
Apply Zcompare_rec with x:=x y:=y; Intro H.
Right. Rewrite H. Discriminate.
Left; Assumption.
Right. Rewrite H. Discriminate.
Defined.

Definition Z_le_dec : {(Zle x y)}+{~(Zle x y)}.
Proof.
Unfold Zle.
Apply Zcompare_rec with x:=x y:=y; Intro H.
Left. Rewrite H. Discriminate.
Left. Rewrite H. Discriminate.
Right. Tauto.
Defined.

Definition Z_gt_dec : {(Zgt x y)}+{~(Zgt x y)}.
Proof.
Unfold Zgt.
Apply Zcompare_rec with x:=x y:=y; Intro H.
Right. Rewrite H. Discriminate.
Right. Rewrite H. Discriminate.
Left; Assumption.
Defined.

Definition Z_ge_dec : {(Zge x y)}+{~(Zge x y)}.
Proof.
Unfold Zge.
Apply Zcompare_rec with x:=x y:=y; Intro H.
Left. Rewrite H. Discriminate.
Right. Tauto.
Left. Rewrite H. Discriminate.
Defined.

Definition Z_lt_ge_dec : {`x < y`}+{`x >= y`}.
Proof.
Exact Z_lt_dec.
Defined.

V7only [ (* From Zextensions *) ].
Lemma Z_lt_le_dec: {`x < y`}+{`y <= x`}.
Proof.
Intros.
Elim Z_lt_ge_dec.
Intros; Left; Assumption.
Intros; Right; Apply Zge_le; Assumption.
Qed.

Definition Z_le_gt_dec : {`x <= y`}+{`x > y`}.
Proof.
Elim Z_le_dec; Auto with arith.
Intro. Right. Apply not_Zle; Auto with arith.
Defined.

Definition Z_gt_le_dec : {`x > y`}+{`x <= y`}.
Proof.
Exact Z_gt_dec.
Defined.

Definition Z_ge_lt_dec : {`x >= y`}+{`x < y`}.
Proof.
Elim Z_ge_dec; Auto with arith.
Intro. Right. Apply not_Zge; Auto with arith.
Defined.

Definition Z_le_lt_eq_dec : `x <= y` -> {`x < y`}+{x=y}.
Proof.
Intro H.
Apply Zcompare_rec with x:=x y:=y.
Intro. Right. Elim (Zcompare_EGAL x y); Auto with arith.
Intro. Left. Elim (Zcompare_EGAL x y); Auto with arith.
Intro H1. Absurd `x > y`; Auto with arith.
Defined.

End decidability.

(** Cotransitivity of order on binary integers *)

Lemma Zlt_cotrans:(n,m:Z)`n<m`->(p:Z){`n<p`}+{`p<m`}.
Proof.
 Intros x y H z.
 Case (Z_lt_ge_dec x z).
 Intro.
 Left.
 Assumption.
 Intro.
 Right.
 Apply Zle_lt_trans with m:=x.
 Apply Zge_le.
 Assumption.
 Assumption.
Defined.

Lemma Zlt_cotrans_pos:(x,y:Z)`0<x+y`->{`0<x`}+{`0<y`}.
Proof.
 Intros x y H.
 Case (Zlt_cotrans `0` `x+y` H x).
 Intro.
 Left.
 Assumption.
 Intro.
 Right.
 Apply Zsimpl_lt_plus_l with p:=`x`.
 Rewrite Zero_right.
 Assumption.
Defined.

Lemma Zlt_cotrans_neg:(x,y:Z)`x+y<0`->{`x<0`}+{`y<0`}.
Proof.
 Intros x y H;
 Case (Zlt_cotrans `x+y` `0` H x);
 Intro Hxy;
 [ Right;
   Apply Zsimpl_lt_plus_l with p:=`x`;
   Rewrite Zero_right
 | Left
 ];
 Assumption.
Defined.

Lemma not_Zeq_inf:(x,y:Z)`x<>y`->{`x<y`}+{`y<x`}.
Proof.
 Intros x y H.
 Case Z_lt_ge_dec with x y.
 Intro.
 Left.
 Assumption.
 Intro H0.
 Generalize (Zge_le ? ? H0).
 Intro.
 Case (Z_le_lt_eq_dec ? ? H1).
 Intro.
 Right.
 Assumption.
 Intro.
 Apply False_rec.
 Apply H.
 Symmetry.
 Assumption.
Defined.

Lemma Z_dec:(x,y:Z){`x<y`}+{`x>y`}+{`x=y`}.
Proof.
 Intros x y.
 Case (Z_lt_ge_dec x y).
 Intro H.
 Left.
 Left.
 Assumption.
 Intro H.
 Generalize (Zge_le ? ? H).
 Intro H0.
 Case (Z_le_lt_eq_dec y x H0).
 Intro H1.
 Left.
 Right.
 Apply Zlt_gt.
 Assumption.
 Intro.
 Right.
 Symmetry.
 Assumption.
Defined.


Lemma Z_dec':(x,y:Z){`x<y`}+{`y<x`}+{`x=y`}.
Proof.
 Intros x y.
 Case (Z_eq_dec x y);
 Intro H;
 [ Right;
   Assumption
 | Left;
   Apply (not_Zeq_inf ?? H)
 ].
Defined.



Definition Z_zerop : (x:Z){(`x = 0`)}+{(`x <> 0`)}.
Proof.
Exact [x:Z](Z_eq_dec x ZERO).
Defined.

Definition Z_notzerop := [x:Z](sumbool_not ? ? (Z_zerop x)).

Definition Z_noteq_dec := [x,y:Z](sumbool_not ? ? (Z_eq_dec x y)).