aboutsummaryrefslogtreecommitdiffhomepage
path: root/theories/ZArith/Znat.v
blob: 8f4a69b1ee09d9365f7daaf91765742366112ea7 (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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
(* -*- coding: utf-8 -*- *)
(************************************************************************)
(*  v      *   The Coq Proof Assistant  /  The Coq Development Team     *)
(* <O___,, *   INRIA - CNRS - LIX - LRI - PPS - Copyright 1999-2010     *)
(*   \VV/  **************************************************************)
(*    //   *      This file is distributed under the terms of the       *)
(*         *       GNU Lesser General Public License Version 2.1        *)
(************************************************************************)

(** Binary Integers (Pierre Crégut, CNET, Lannion, France) *)

Require Export Arith_base.
Require Import BinPos BinInt BinNat Zcompare Zorder.
Require Import Decidable Peano_dec Min Max Compare_dec.

Local Open Scope Z_scope.

Definition neq (x y:nat) := x <> y.

(************************************************)
(** Properties of the injection from nat into Z *)

Lemma Zpos_P_of_succ_nat : forall n:nat,
 Zpos (P_of_succ_nat n) = Zsucc (Z_of_nat n).
Proof.
  intros [|n]. trivial. apply Zpos_succ_morphism.
Qed.

(** Injection and successor *)

Theorem inj_0 : Z_of_nat 0 = 0.
Proof.
  reflexivity.
Qed.

Theorem inj_S : forall n:nat, Z_of_nat (S n) = Zsucc (Z_of_nat n).
Proof.
  exact Zpos_P_of_succ_nat.
Qed.

(** Injection and comparison *)

Theorem inj_compare : forall n m:nat,
 (Z_of_nat n ?= Z_of_nat m) = nat_compare n m.
Proof.
 induction n; destruct m; trivial.
 rewrite 2 inj_S, Zcompare_succ_compat. now simpl.
Qed.

(** Injection and equality. *)

Theorem inj_eq : forall n m:nat, n = m -> Z_of_nat n = Z_of_nat m.
Proof.
  intros; subst; trivial.
Qed.

Theorem inj_eq_rev : forall n m:nat, Z_of_nat n = Z_of_nat m -> n = m.
Proof.
  intros n m H. apply nat_compare_eq.
  now rewrite <- inj_compare, H, Zcompare_refl.
Qed.

Theorem inj_neq : forall n m:nat, neq n m -> Zne (Z_of_nat n) (Z_of_nat m).
Proof.
  unfold neq, Zne. intros n m H. contradict H. now apply inj_eq_rev.
Qed.

Theorem inj_eq_iff : forall n m:nat, n=m <-> Z_of_nat n = Z_of_nat m.
Proof.
 split; [apply inj_eq | apply inj_eq_rev].
Qed.

(** Injection and order *)

(** Both ways ... *)

Theorem inj_le_iff : forall n m:nat, (n<=m)%nat <-> Z_of_nat n <= Z_of_nat m.
Proof.
 intros. unfold Zle. rewrite inj_compare. apply nat_compare_le.
Qed.

Theorem inj_lt_iff : forall n m:nat, (n<m)%nat <-> Z_of_nat n < Z_of_nat m.
Proof.
 intros. unfold Zlt. rewrite inj_compare. apply nat_compare_lt.
Qed.

Theorem inj_ge_iff : forall n m:nat, (n>=m)%nat <-> Z_of_nat n >= Z_of_nat m.
Proof.
 intros. unfold Zge. rewrite inj_compare. apply nat_compare_ge.
Qed.

Theorem inj_gt_iff : forall n m:nat, (n>m)%nat <-> Z_of_nat n > Z_of_nat m.
Proof.
 intros. unfold Zgt. rewrite inj_compare. apply nat_compare_gt.
Qed.

(** One way ... *)

Theorem inj_le : forall n m:nat, (n <= m)%nat -> Z_of_nat n <= Z_of_nat m.
Proof. apply inj_le_iff. Qed.

Theorem inj_lt : forall n m:nat, (n < m)%nat -> Z_of_nat n < Z_of_nat m.
Proof. apply inj_lt_iff. Qed.

Theorem inj_ge : forall n m:nat, (n >= m)%nat -> Z_of_nat n >= Z_of_nat m.
Proof. apply inj_ge_iff. Qed.

Theorem inj_gt : forall n m:nat, (n > m)%nat -> Z_of_nat n > Z_of_nat m.
Proof. apply inj_gt_iff. Qed.

(** The other way ... *)

Theorem inj_le_rev : forall n m:nat, Z_of_nat n <= Z_of_nat m -> (n <= m)%nat.
Proof. apply inj_le_iff. Qed.

Theorem inj_lt_rev : forall n m:nat, Z_of_nat n < Z_of_nat m -> (n < m)%nat.
Proof. apply inj_lt_iff. Qed.

Theorem inj_ge_rev : forall n m:nat, Z_of_nat n >= Z_of_nat m -> (n >= m)%nat.
Proof. apply inj_ge_iff. Qed.

Theorem inj_gt_rev : forall n m:nat, Z_of_nat n > Z_of_nat m -> (n > m)%nat.
Proof. apply inj_gt_iff. Qed.

(** Injection and usual operations *)

Theorem inj_plus : forall n m:nat, Z_of_nat (n + m) = Z_of_nat n + Z_of_nat m.
Proof.
 induction n as [|n IH]; intros [|m]; simpl; trivial.
 rewrite <- plus_n_O; trivial.
 change (Z_of_nat (S (n + S m)) = Z_of_nat (S n) + Z_of_nat (S m)).
 now rewrite inj_S, IH, 2 inj_S, Zplus_succ_l.
Qed.

Theorem inj_mult : forall n m:nat, Z_of_nat (n * m) = Z_of_nat n * Z_of_nat m.
Proof.
 induction n as [|n IH]; intros m; trivial.
 rewrite inj_S, Zmult_succ_l, <- IH, <- inj_plus.
 simpl. now rewrite plus_comm.
Qed.

Theorem inj_minus1 :
  forall n m:nat, (m <= n)%nat -> Z_of_nat (n - m) = Z_of_nat n - Z_of_nat m.
Proof.
 intros n m H.
 apply (Zplus_reg_l (Z_of_nat m)); unfold Zminus.
 rewrite Zplus_permute, Zplus_opp_r, <- inj_plus, Zplus_0_r.
 f_equal. symmetry. now apply le_plus_minus.
Qed.

Theorem inj_minus2 : forall n m:nat, (m > n)%nat -> Z_of_nat (n - m) = 0.
Proof.
 intros. rewrite not_le_minus_0; auto with arith.
Qed.

Theorem inj_minus : forall n m:nat,
 Z_of_nat (minus n m) = Zmax 0 (Z_of_nat n - Z_of_nat m).
Proof.
 intros n m. unfold Zmax.
 destruct (le_lt_dec m n) as [H|H].
 (* m <= n *)
 rewrite inj_minus1; trivial.
 apply inj_le, Zle_minus_le_0 in H. revert H. unfold Zle.
 case Zcompare_spec; intuition.
 (* n < m *)
 rewrite inj_minus2; trivial.
 apply inj_lt, Zlt_gt in H.
 apply (Zplus_gt_compat_r _ _ (- Z_of_nat m)) in H.
 rewrite Zplus_opp_r in H.
 unfold Zminus. rewrite H; auto.
Qed.

Theorem inj_min : forall n m:nat,
  Z_of_nat (min n m) = Zmin (Z_of_nat n) (Z_of_nat m).
Proof.
 intros n m. unfold Zmin. rewrite inj_compare.
 case nat_compare_spec; intros; f_equal; subst; auto with arith.
Qed.

Theorem inj_max : forall n m:nat,
  Z_of_nat (max n m) = Zmax (Z_of_nat n) (Z_of_nat m).
Proof.
 intros n m. unfold Zmax. rewrite inj_compare.
 case nat_compare_spec; intros; f_equal; subst; auto with arith.
Qed.

(** Composition of injections **)

Lemma Z_of_nat_of_P : forall p, Z_of_nat (nat_of_P p) = Zpos p.
Proof.
 intros p. destruct (nat_of_P_is_S p) as (n,H). rewrite H.
 simpl. f_equal. rewrite <- (nat_of_P_of_succ_nat n) in H.
 symmetry. now apply nat_of_P_inj.
Qed.

(** For compatibility *)
Definition Zpos_eq_Z_of_nat_o_nat_of_P p := eq_sym (Z_of_nat_of_P p).

(******************************************************************)
(** Properties of the injection from N into Z *)

Lemma Z_of_nat_of_N : forall n, Z_of_nat (nat_of_N n) = Z_of_N n.
Proof.
  intros [|n]. trivial.
  simpl. apply Z_of_nat_of_P.
Qed.

Lemma Z_of_N_of_nat : forall n, Z_of_N (N_of_nat n) = Z_of_nat n.
Proof.
 now destruct n.
Qed.

Lemma Z_of_N_eq : forall n m, n = m -> Z_of_N n = Z_of_N m.
Proof.
 intros; f_equal; assumption.
Qed.

Lemma Z_of_N_eq_rev : forall n m, Z_of_N n = Z_of_N m -> n = m.
Proof.
 intros [|n] [|m]; simpl; congruence.
Qed.

Lemma Z_of_N_eq_iff : forall n m, n = m <-> Z_of_N n = Z_of_N m.
Proof.
 split; [apply Z_of_N_eq | apply Z_of_N_eq_rev].
Qed.

Lemma Z_of_N_compare : forall n m,
 Zcompare (Z_of_N n) (Z_of_N m) = Ncompare n m.
Proof.
 intros [|n] [|m]; trivial.
Qed.

Lemma Z_of_N_le_iff : forall n m, (n<=m)%N <-> Z_of_N n <= Z_of_N m.
Proof.
 intros. unfold Zle. now rewrite Z_of_N_compare.
Qed.

Lemma Z_of_N_le : forall n m, (n<=m)%N -> Z_of_N n <= Z_of_N m.
Proof.
 apply Z_of_N_le_iff.
Qed.

Lemma Z_of_N_le_rev : forall n m, Z_of_N n <= Z_of_N m -> (n<=m)%N.
Proof.
 apply Z_of_N_le_iff.
Qed.

Lemma Z_of_N_lt_iff : forall n m, (n<m)%N <-> Z_of_N n < Z_of_N m.
Proof.
 intros. unfold Zlt. now rewrite Z_of_N_compare.
Qed.

Lemma Z_of_N_lt : forall n m, (n<m)%N -> Z_of_N n < Z_of_N m.
Proof.
 apply Z_of_N_lt_iff.
Qed.

Lemma Z_of_N_lt_rev : forall n m, Z_of_N n < Z_of_N m -> (n<m)%N.
Proof.
 apply Z_of_N_lt_iff.
Qed.

Lemma Z_of_N_ge_iff : forall n m, (n>=m)%N <-> Z_of_N n >= Z_of_N m.
Proof.
 intros. unfold Zge. now rewrite Z_of_N_compare.
Qed.

Lemma Z_of_N_ge : forall n m, (n>=m)%N -> Z_of_N n >= Z_of_N m.
Proof.
 apply Z_of_N_ge_iff.
Qed.

Lemma Z_of_N_ge_rev : forall n m, Z_of_N n >= Z_of_N m -> (n>=m)%N.
Proof.
 apply Z_of_N_ge_iff.
Qed.

Lemma Z_of_N_gt_iff : forall n m, (n>m)%N <-> Z_of_N n > Z_of_N m.
Proof.
 intros. unfold Zgt. now rewrite Z_of_N_compare.
Qed.

Lemma Z_of_N_gt : forall n m, (n>m)%N -> Z_of_N n > Z_of_N m.
Proof.
 apply Z_of_N_gt_iff.
Qed.

Lemma Z_of_N_gt_rev : forall n m, Z_of_N n > Z_of_N m -> (n>m)%N.
Proof.
 apply Z_of_N_gt_iff.
Qed.

Lemma Z_of_N_pos : forall p:positive, Z_of_N (Npos p) = Zpos p.
Proof.
 reflexivity.
Qed.

Lemma Z_of_N_abs : forall z:Z, Z_of_N (Zabs_N z) = Zabs z.
Proof.
 now destruct z.
Qed.

Lemma Z_of_N_le_0 : forall n, 0 <= Z_of_N n.
Proof.
 now destruct n.
Qed.

Lemma Z_of_N_plus : forall n m, Z_of_N (n+m) = Z_of_N n + Z_of_N m.
Proof.
 now destruct n, m.
Qed.

Lemma Z_of_N_mult : forall n m, Z_of_N (n*m) = Z_of_N n * Z_of_N m.
Proof.
 now destruct n, m.
Qed.

Lemma Z_of_N_minus : forall n m, Z_of_N (n-m) = Zmax 0 (Z_of_N n - Z_of_N m).
Proof.
 intros [|n] [|m]; simpl; trivial.
 case Pcompare_spec; intros H.
 subst. now rewrite Pminus_mask_diag.
 rewrite Pminus_mask_Lt; trivial.
 unfold Pminus.
 destruct (Pminus_mask_Gt n m (ZC2 _ _ H)) as (q & -> & _); trivial.
Qed.

Lemma Z_of_N_succ : forall n, Z_of_N (Nsucc n) = Zsucc (Z_of_N n).
Proof.
 intros [|n]; simpl; trivial. now rewrite Zpos_succ_morphism.
Qed.

Lemma Z_of_N_min : forall n m, Z_of_N (Nmin n m) = Zmin (Z_of_N n) (Z_of_N m).
Proof.
 intros. unfold Zmin, Nmin. rewrite Z_of_N_compare. now case Ncompare.
Qed.

Lemma Z_of_N_max : forall n m, Z_of_N (Nmax n m) = Zmax (Z_of_N n) (Z_of_N m).
Proof.
 intros. unfold Zmax, Nmax. rewrite Z_of_N_compare.
 case Ncompare_spec; intros; subst; trivial.
Qed.

(** Results about the [Zabs_N] function, converting from Z to N *)

Lemma Zabs_of_N : forall n, Zabs_N (Z_of_N n) = n.
Proof.
 now destruct n.
Qed.

Lemma Zabs_N_succ_abs : forall n,
 Zabs_N (Zsucc (Zabs n)) = Nsucc (Zabs_N n).
Proof.
 intros [ |n|n]; simpl; trivial; now rewrite Pplus_one_succ_r.
Qed.

Lemma Zabs_N_succ : forall n, 0<=n ->
 Zabs_N (Zsucc n) = Nsucc (Zabs_N n).
Proof.
 intros n Hn. rewrite <- Zabs_N_succ_abs. repeat f_equal.
 symmetry; now apply Zabs_eq.
Qed.

Lemma Zabs_N_plus_abs : forall n m,
 Zabs_N (Zabs n + Zabs m) = (Zabs_N n + Zabs_N m)%N.
Proof.
 intros [ |n|n] [ |m|m]; simpl; trivial.
Qed.

Lemma Zabs_N_plus : forall n m, 0<=n -> 0<=m ->
 Zabs_N (n + m) = (Zabs_N n + Zabs_N m)%N.
Proof.
 intros n m Hn Hm.
 rewrite <- Zabs_N_plus_abs; repeat f_equal;
  symmetry; now apply Zabs_eq.
Qed.

Lemma Zabs_N_mult_abs : forall n m,
 Zabs_N (Zabs n * Zabs m) = (Zabs_N n * Zabs_N m)%N.
Proof.
 intros [ |n|n] [ |m|m]; simpl; trivial.
Qed.

Lemma Zabs_N_mult : forall n m, 0<=n -> 0<=m ->
 Zabs_N (n * m) = (Zabs_N n * Zabs_N m)%N.
Proof.
 intros n m Hn Hm.
 rewrite <- Zabs_N_mult_abs; repeat f_equal;
  symmetry; now apply Zabs_eq.
Qed.