aboutsummaryrefslogtreecommitdiffhomepage
path: root/theories/Zarith/zarith_aux.v
blob: 5b247589fc51e9599950bc00d00d210d621c0a76 (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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
(*i $Id$ i*)

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

Require Arith.
Require Export fast_integer.

Meta Definition ElimCompare com1 com2:=
  Elim (Dcompare (Zcompare com1 com2)); [
         Idtac 
       | Intro hidden_auxiliary; Elim hidden_auxiliary; 
         Clear hidden_auxiliary ] .

(*s Order relations *)
Definition Zlt := [x,y:Z](Zcompare x y) = INFERIEUR.
Definition Zgt := [x,y:Z](Zcompare x y) = SUPERIEUR.
Definition Zle := [x,y:Z]~(Zcompare x y) = SUPERIEUR.
Definition Zge := [x,y:Z]~(Zcompare x y) = INFERIEUR.

(*s Absolu function *)
Definition absolu [x:Z] : nat :=
  Cases x of
     ZERO   => O
  | (POS p) => (convert p)
  | (NEG p) => (convert p)
  end.

Definition Zabs [z:Z] : Z :=
  Cases z of 
     ZERO   => ZERO
  | (POS p) => (POS p)
  | (NEG p) => (POS p)
  end.

(*s Properties of absolu function *)

Lemma Zabs_eq : (x:Z) (Zle ZERO x) -> (Zabs x)=x.
Destruct x; Auto with arith.
Compute; Intros; Absurd SUPERIEUR=SUPERIEUR; Trivial with arith.
Save.

(*s From nat to Z *)
Definition inject_nat := 
  [x:nat]Cases x of
           O => ZERO
         | (S y) => (POS (anti_convert y))
         end.

(*s Successor and Predecessor functions on Z *)
Definition Zs := [x:Z](Zplus x (POS xH)).
Definition Zpred := [x:Z](Zplus x (NEG xH)).

(* Properties of the order relation *)
Theorem Zgt_Sn_n : (n:Z)(Zgt (Zs n) n).

Intros n; Unfold Zgt Zs; Pattern 2 n; Rewrite <- (Zero_right n); 
Rewrite Zcompare_Zplus_compatible;Auto with arith.
Save.

(*s Properties of the order *)
Theorem Zle_gt_trans : (n,m,p:Z)(Zle m n)->(Zgt m p)->(Zgt n p).

Unfold Zle Zgt; Intros n m p H1 H2; (ElimCompare 'm 'n); [
  Intro E; Elim (Zcompare_EGAL m n); Intros H3 H4;Rewrite <- (H3 E); Assumption
| Intro H3; Apply Zcompare_trans_SUPERIEUR with y:=m;[
    Elim (Zcompare_ANTISYM n m); Intros H4 H5;Apply H5; Assumption
  | Assumption ]
| Intro H3; Absurd (Zcompare m n)=SUPERIEUR;Assumption ].
Save.

Theorem Zgt_le_trans : (n,m,p:Z)(Zgt n m)->(Zle p m)->(Zgt n p).

Unfold Zle Zgt ;Intros n m p H1 H2; (ElimCompare 'p 'm); [
  Intros E;Elim (Zcompare_EGAL p m);Intros H3 H4; Rewrite (H3 E); Assumption
| Intro H3; Apply Zcompare_trans_SUPERIEUR with y:=m; [
    Assumption
  | Elim (Zcompare_ANTISYM m p); Auto with arith ]
| Intro H3; Absurd (Zcompare p m)=SUPERIEUR;Assumption ].
Save.

Theorem Zle_S_gt : (n,m:Z) (Zle (Zs n) m) -> (Zgt m n).

Intros n m H;Apply Zle_gt_trans with m:=(Zs n);[ Assumption | Apply Zgt_Sn_n ].
Save.

Theorem Zcompare_n_S : (n,m:Z)(Zcompare (Zs n) (Zs m)) = (Zcompare n m).
Intros n m;Unfold Zs ;Do 2 Rewrite -> [t:Z](Zplus_sym t (POS xH));
Rewrite -> Zcompare_Zplus_compatible;Auto with arith.
Save.
 
Theorem Zgt_n_S : (n,m:Z)(Zgt m n) -> (Zgt (Zs m) (Zs n)).

Unfold Zgt; Intros n m H; Rewrite Zcompare_n_S; Auto with arith.
Save.

Lemma Zle_not_gt     : (n,m:Z)(Zle n m) -> ~(Zgt n m).

Unfold Zle Zgt; Auto with arith.
Save.

Lemma Zgt_antirefl   : (n:Z)~(Zgt n n).

Unfold Zgt ;Intros n; Elim (Zcompare_EGAL n n); Intros H1 H2;
Rewrite H2; [ Discriminate | Trivial with arith ].
Save.

Lemma Zgt_not_sym    : (n,m:Z)(Zgt n m) -> ~(Zgt m n).

Unfold Zgt ;Intros n m H; Elim (Zcompare_ANTISYM n m); Intros H1 H2;
Rewrite -> H1; [ Discriminate | Assumption ].
Save.

Lemma Zgt_not_le     : (n,m:Z)(Zgt n m) -> ~(Zle n m).

Unfold Zgt Zle not; Auto with arith.
Save.

Lemma Zgt_trans      : (n,m,p:Z)(Zgt n m)->(Zgt m p)->(Zgt n p).

Unfold Zgt; Exact Zcompare_trans_SUPERIEUR.
Save.

Lemma Zle_gt_S       : (n,p:Z)(Zle n p)->(Zgt (Zs p) n).

Unfold Zle Zgt ;Intros n p H; (ElimCompare 'n 'p); [
  Intros H1;Elim (Zcompare_EGAL n p);Intros H2 H3; Rewrite <- H2; [
    Exact (Zgt_Sn_n n)
  | Assumption ]
 
| Intros H1;Apply Zcompare_trans_SUPERIEUR with y:=p; [
    Exact (Zgt_Sn_n p)
  | Elim (Zcompare_ANTISYM p n); Auto with arith ]
| Intros H1;Absurd (Zcompare n p)=SUPERIEUR;Assumption ].
Save.

Lemma Zgt_pred       : (n,p:Z)(Zgt p (Zs n))->(Zgt (Zpred p) n).

Unfold Zgt Zs Zpred ;Intros n p H; 
Rewrite <- [x,y:Z](Zcompare_Zplus_compatible x y (POS xH));
Rewrite (Zplus_sym p); Rewrite Zplus_assoc; Rewrite [x:Z](Zplus_sym x n);
Simpl; Assumption.
Save.

Lemma Zsimpl_gt_plus_l : (n,m,p:Z)(Zgt (Zplus p n) (Zplus p m))->(Zgt n m).

Unfold Zgt; Intros n m p H; Rewrite <- (Zcompare_Zplus_compatible n m p); 
Assumption.
Save.

Lemma Zgt_reg_l      : (n,m,p:Z)(Zgt n m)->(Zgt (Zplus p n) (Zplus p m)).

Unfold Zgt; Intros n m p H; Rewrite (Zcompare_Zplus_compatible n m p); 
Assumption.
Save.
Theorem Zcompare_et_un: 
  (x,y:Z) (Zcompare x y)=SUPERIEUR <-> 
    ~(Zcompare x (Zplus y (POS xH)))=INFERIEUR.

Intros x y; Split; [
  Intro H; (ElimCompare 'x '(Zplus y (POS xH)));[
    Intro H1; Rewrite H1; Discriminate
  | Intros H1; Elim SUPERIEUR_POS with 1:=H; Intros h H2; 
    Absurd (gt (convert h) O) /\ (lt (convert h) (S O)); [
      Unfold not ;Intros H3;Elim H3;Intros H4 H5; Absurd (gt (convert h) O); [
        Unfold gt ;Apply le_not_lt; Apply le_S_n; Exact H5
      | Assumption]
    | Split; [
        Elim (ZL4 h); Intros i H3;Rewrite H3; Apply gt_Sn_O
      | Change (lt (convert h) (convert xH)); 
        Apply compare_convert_INFERIEUR;
        Change (Zcompare (POS h) (POS xH))=INFERIEUR;
        Rewrite <- H2; Rewrite <- [m,n:Z](Zcompare_Zplus_compatible m n y);
        Rewrite (Zplus_sym x);Rewrite Zplus_assoc; Rewrite Zplus_inverse_r;
        Simpl; Exact H1 ]]
  | Intros H1;Rewrite -> H1;Discriminate ]
| Intros H; (ElimCompare 'x '(Zplus y (POS xH))); [
    Intros H1;Elim (Zcompare_EGAL x (Zplus y (POS xH))); Intros H2 H3;
    Rewrite  (H2 H1); Exact (Zgt_Sn_n y)
  | Intros H1;Absurd (Zcompare x (Zplus y (POS xH)))=INFERIEUR;Assumption
  | Intros H1; Apply Zcompare_trans_SUPERIEUR with y:=(Zs y); 
      [ Exact H1 | Exact (Zgt_Sn_n y) ]]].
Save.

Lemma Zgt_S_n        : (n,p:Z)(Zgt (Zs p) (Zs n))->(Zgt p n).

Unfold Zs Zgt;Intros n p;Do 2 Rewrite -> [m:Z](Zplus_sym m (POS xH));
Rewrite -> (Zcompare_Zplus_compatible p n (POS xH));Trivial with arith.
Save.

Lemma Zle_S_n     : (n,m:Z) (Zle (Zs m) (Zs n)) -> (Zle m n).

Unfold Zle not ;Intros m n H1 H2;Apply H1;
Unfold Zs ;Do 2 Rewrite <- (Zplus_sym (POS xH));
Rewrite -> (Zcompare_Zplus_compatible n m (POS xH));Assumption.
Save.

Lemma Zgt_le_S       : (n,p:Z)(Zgt p n)->(Zle (Zs n) p).

Unfold Zgt Zle; Intros n p H; Elim (Zcompare_et_un p n); Intros H1 H2;
Unfold not ;Intros H3; Unfold not in H1; Apply H1; [
  Assumption
| Elim (Zcompare_ANTISYM (Zplus n (POS xH)) p);Intros H4 H5;Apply H4;Exact H3].
Save.

Lemma Zgt_S_le       : (n,p:Z)(Zgt (Zs p) n)->(Zle n p).

Intros n p H;Apply Zle_S_n; Apply Zgt_le_S; Assumption.
Save.

Theorem Zgt_S        : (n,m:Z)(Zgt (Zs n) m)->((Zgt n m)\/(<Z>m=n)).

Intros n m H; Unfold Zgt; (ElimCompare 'n 'm); [
  Elim (Zcompare_EGAL n m); Intros H1 H2 H3;Rewrite -> H1;Auto with arith
| Intros H1;Absurd (Zcompare m n)=SUPERIEUR; 
    [ Exact (Zgt_S_le m n H) | Elim (Zcompare_ANTISYM m n); Auto with arith ]
| Auto with arith ].
Save.

Theorem Zgt_trans_S  : (n,m,p:Z)(Zgt (Zs n) m)->(Zgt m p)->(Zgt n p).

Intros n m p H1 H2;Apply Zle_gt_trans with m:=m;
  [ Apply Zgt_S_le; Assumption | Assumption ].
Save.

Theorem Zeq_S : (n,m:Z) n=m -> (Zs n)=(Zs m).
Intros n m H; Rewrite H; Auto with arith.
Save.

Theorem Zpred_Sn : (m:Z) m=(Zpred (Zs m)).
Intros m; Unfold Zpred Zs; Rewrite <- Zplus_assoc; Simpl; 
Rewrite Zplus_sym; Auto with arith.
Save.

Theorem Zeq_add_S : (n,m:Z) (Zs n)=(Zs m) -> n=m.

Intros n m H.
Change (Zplus (Zplus (NEG xH) (POS xH)) n)=
       (Zplus (Zplus (NEG xH) (POS xH)) m);
Do 2 Rewrite <- Zplus_assoc; Do 2 Rewrite (Zplus_sym (POS xH));
Unfold Zs in H;Rewrite H; Trivial with arith.
Save.
 
Theorem Znot_eq_S : (n,m:Z) ~(n=m) -> ~((Zs n)=(Zs m)).

Unfold not ;Intros n m H1 H2;Apply H1;Apply Zeq_add_S; Assumption.
Save.
 
Lemma Zsimpl_plus_l : (n,m,p:Z)(Zplus n m)=(Zplus n p)->m=p.
Intros n m p H; Cut (Zplus (Zopp n) (Zplus n m))=(Zplus (Zopp n) (Zplus n p));[
  Do 2 Rewrite -> Zplus_assoc; Rewrite -> (Zplus_sym (Zopp n) n);
  Rewrite -> Zplus_inverse_r;Simpl; Trivial with arith
| Rewrite -> H; Trivial with arith ].
Save.

Theorem Zn_Sn : (n:Z) ~(n=(Zs n)).
Intros n;Cut ~ZERO=(POS xH);[
  Unfold not ;Intros H1 H2;Apply H1;Apply (Zsimpl_plus_l n);Rewrite Zero_right;
  Exact H2
| Discriminate ].
Save.

Lemma Zplus_n_O : (n:Z) n=(Zplus n ZERO).
Intro; Rewrite Zero_right; Trivial with arith.
Save.

Lemma Zplus_unit_left : (n,m:Z) (Zplus n ZERO)=m -> n=m.
Intro; Rewrite Zero_right; Trivial with arith.
Save.

Lemma Zplus_unit_right : (n,m:Z) n=(Zplus m ZERO) -> n=m.
Intros n m; Rewrite (Zero_right m); Trivial with arith.
Save.

Lemma Zplus_n_Sm : (n,m:Z) (Zs (Zplus n m))=(Zplus n (Zs m)).

Intros n m; Unfold Zs; Rewrite Zplus_assoc; Trivial with arith.
Save.

Lemma Zmult_n_O : (n:Z) ZERO=(Zmult n ZERO).

Intro;Rewrite Zmult_sym;Simpl; Trivial with arith.
Save.

Lemma Zmult_n_Sm : (n,m:Z) (Zplus (Zmult n m) n)=(Zmult n (Zs m)).

Intros n m;Unfold Zs; Rewrite Zmult_plus_distr_r;
Rewrite (Zmult_sym n (POS xH));Rewrite Zmult_one; Trivial with arith.
Save.

Theorem Zle_n : (n:Z) (Zle n n).
Intros n;Elim (Zcompare_EGAL n n);Unfold Zle ;Intros H1 H2;Rewrite H2;
  [ Discriminate | Trivial with arith ].
Save. 

Theorem Zle_refl : (n,m:Z) n=m -> (Zle n m).
Intros; Rewrite H; Apply Zle_n.
Save.

Theorem Zle_trans : (n,m,p:Z)(Zle n m)->(Zle m p)->(Zle n p).

Intros n m p;Unfold 1 3 Zle; Unfold not; Intros H1 H2 H3;Apply H1;
Exact (Zgt_le_trans n p m H3 H2).
Save.

Theorem Zle_n_Sn : (n:Z)(Zle n (Zs n)).

Intros n; Apply Zgt_S_le;Apply Zgt_trans with m:=(Zs n) ;Apply Zgt_Sn_n.
Save.

Lemma Zle_n_S : (n,m:Z) (Zle m n) -> (Zle (Zs m) (Zs n)).

Unfold Zle not ;Intros m n H1 H2; Apply H1; 
Rewrite <- (Zcompare_Zplus_compatible n m (POS xH));
Do 2 Rewrite (Zplus_sym (POS xH)); Exact H2.
Save.

Hints Resolve Zle_n Zle_n_Sn Zle_trans Zle_n_S : zarith.
Hints Immediate Zle_refl : zarith.

Lemma Zs_pred : (n:Z) n=(Zs (Zpred n)).

Intros n; Unfold Zs Zpred ;Rewrite <- Zplus_assoc; Simpl; Rewrite Zero_right;
Trivial with arith.
Save. 

Hints Immediate Zs_pred : zarith.
 
Theorem Zle_pred_n : (n:Z)(Zle (Zpred n) n).

Intros n;Pattern 2 n ;Rewrite Zs_pred; Apply Zle_n_Sn.
Save.
 
Theorem Zle_trans_S : (n,m:Z)(Zle (Zs n) m)->(Zle n m).

Intros n m H;Apply Zle_trans with m:=(Zs n); [ Apply Zle_n_Sn | Assumption ].
Save.

Theorem Zle_Sn_n : (n:Z)~(Zle (Zs n) n).

Intros n; Apply Zgt_not_le; Apply Zgt_Sn_n.
Save.

Theorem Zle_antisym : (n,m:Z)(Zle n m)->(Zle m n)->(n=m).

Unfold Zle ;Intros n m H1 H2; (ElimCompare 'n 'm); [
  Elim (Zcompare_EGAL n m);Auto with arith
| Intros H3;Absurd (Zcompare m n)=SUPERIEUR; [
    Assumption
  | Elim (Zcompare_ANTISYM m n);Auto with arith ]
| Intros H3;Absurd (Zcompare n m)=SUPERIEUR;Assumption ].
Save.

Theorem Zgt_lt : (m,n:Z) (Zgt m n) -> (Zlt n m).
Unfold Zgt Zlt ;Intros m n H; Elim (Zcompare_ANTISYM m n); Auto with arith.
Save.

Theorem Zlt_gt : (m,n:Z) (Zlt m n) -> (Zgt n m).
Unfold Zgt Zlt ;Intros m n H; Elim (Zcompare_ANTISYM n m); Auto with arith.
Save.

Theorem Zge_le : (m,n:Z) (Zge m n) -> (Zle n m).
Intros m n; Change ~(Zlt m n)-> ~(Zgt n m);
Unfold not; Intros H1 H2; Apply H1; Apply Zgt_lt; Assumption.
Save.

Theorem Zle_ge : (m,n:Z) (Zle m n) -> (Zge n m).
Intros m n; Change ~(Zgt m n)-> ~(Zlt n m);
Unfold not; Intros H1 H2; Apply H1; Apply Zlt_gt; Assumption.
Save.

Theorem Zlt_n_Sn : (n:Z)(Zlt n (Zs n)).
Intro n; Apply Zgt_lt; Apply Zgt_Sn_n.
Save.
Theorem Zlt_S : (n,m:Z)(Zlt n m)->(Zlt n (Zs m)).
Intros n m H;Apply Zgt_lt; Apply Zgt_trans with m:=m; [
  Apply Zgt_Sn_n
| Apply Zlt_gt; Assumption ].
Save.

Theorem Zlt_n_S : (n,m:Z)(Zlt n m)->(Zlt (Zs n) (Zs m)).
Intros n m H;Apply Zgt_lt;Apply Zgt_n_S;Apply Zlt_gt; Assumption.
Save.

Theorem Zlt_S_n : (n,m:Z)(Zlt (Zs n) (Zs m))->(Zlt n m).

Intros n m H;Apply Zgt_lt;Apply Zgt_S_n;Apply Zlt_gt; Assumption.
Save.

Theorem Zlt_n_n : (n:Z)~(Zlt n n).

Intros n;Elim (Zcompare_EGAL n n); Unfold Zlt ;Intros H1 H2;
Rewrite H2; [ Discriminate | Trivial with arith ].
Save.

Lemma Zlt_pred : (n,p:Z)(Zlt (Zs n) p)->(Zlt n (Zpred p)).

Intros n p H;Apply Zlt_S_n; Rewrite <- Zs_pred; Assumption.
Save.

Lemma Zlt_pred_n_n : (n:Z)(Zlt (Zpred n) n).

Intros n; Apply Zlt_S_n; Rewrite <- Zs_pred; Apply Zlt_n_Sn.
Save.
 
Theorem Zlt_le_S : (n,p:Z)(Zlt n p)->(Zle (Zs n) p).
Intros n p H; Apply Zgt_le_S; Apply Zlt_gt; Assumption.
Save.

Theorem Zlt_n_Sm_le : (n,m:Z)(Zlt n (Zs m))->(Zle n m).
Intros n m H; Apply Zgt_S_le; Apply Zlt_gt; Assumption.
Save.

Theorem Zle_lt_n_Sm : (n,m:Z)(Zle n m)->(Zlt n (Zs m)).
Intros n m H; Apply Zgt_lt; Apply Zle_gt_S; Assumption.
Save.

Theorem Zlt_le_weak : (n,m:Z)(Zlt n m)->(Zle n m).
Unfold Zlt Zle ;Intros n m H;Rewrite H;Discriminate.
Save.

Theorem Zlt_trans : (n,m,p:Z)(Zlt n m)->(Zlt m p)->(Zlt n p).
Intros n m p H1 H2; Apply Zgt_lt; Apply Zgt_trans with m:= m; 
Apply Zlt_gt; Assumption.
Save.
Theorem Zlt_le_trans : (n,m,p:Z)(Zlt n m)->(Zle m p)->(Zlt n p).
Intros n m p H1 H2;Apply Zgt_lt;Apply Zle_gt_trans with m:=m;
  [ Assumption | Apply Zlt_gt;Assumption ].
Save.

Theorem Zle_lt_trans : (n,m,p:Z)(Zle n m)->(Zlt m p)->(Zlt n p).

Intros n m p H1 H2;Apply Zgt_lt;Apply Zgt_le_trans with m:=m; 
  [ Apply Zlt_gt;Assumption | Assumption ].
Save.
 
Theorem Zle_lt_or_eq : (n,m:Z)(Zle n m)->((Zlt n m) \/ n=m).

Unfold Zle Zlt ;Intros n m H; (ElimCompare 'n 'm); [
  Elim (Zcompare_EGAL n m);Auto with arith
| Auto with arith
| Intros H';Absurd (Zcompare n m)=SUPERIEUR;Assumption ].
Save.

Theorem Zle_or_lt : (n,m:Z)((Zle n m)\/(Zlt m n)).

Unfold Zle Zlt ;Intros n m; (ElimCompare 'n 'm); [
  Intros E;Rewrite -> E;Left;Discriminate
| Intros E;Rewrite -> E;Left;Discriminate
| Elim (Zcompare_ANTISYM n m); Auto with arith ].
Save.

Theorem Zle_not_lt : (n,m:Z)(Zle n m) -> ~(Zlt m n).

Unfold Zle Zlt; Unfold not ;Intros n m H1 H2;Apply H1; 
Elim (Zcompare_ANTISYM n m);Auto with arith.
Save.

Theorem Zlt_not_le : (n,m:Z)(Zlt n m) -> ~(Zle m n).
Unfold Zlt Zle not ;Intros n m H1 H2; Apply H2; Elim (Zcompare_ANTISYM m n);
Auto with arith.
Save.

Theorem Zlt_not_sym : (n,m:Z)(Zlt n m) -> ~(Zlt m n).
Intros n m H;Apply Zle_not_lt; Apply Zlt_le_weak; Assumption.
Save.

Theorem Zle_le_S : (x,y:Z)(Zle x y)->(Zle x (Zs y)).
Intros.
Apply Zle_trans with y; Trivial with zarith.
Save.

Hints Resolve Zle_le_S : zarith.

Definition Zmin := [n,m:Z]
 <Z>Cases (Zcompare n m) of
      EGAL      => n
    | INFERIEUR => n
    | SUPERIEUR => m
    end.

Lemma Zmin_SS : (n,m:Z)((Zs (Zmin n m))=(Zmin (Zs n) (Zs m))).

Intros n m;Unfold Zmin; Rewrite (Zcompare_n_S n m);
(ElimCompare 'n 'm);Intros E;Rewrite E;Auto with arith.
Save.

Lemma Zle_min_l : (n,m:Z)(Zle (Zmin n m) n).

Intros n m;Unfold Zmin ; (ElimCompare 'n 'm);Intros E;Rewrite -> E;
  [ Apply Zle_n | Apply Zle_n | Apply Zlt_le_weak; Apply Zgt_lt;Exact E ].
Save.

Lemma Zle_min_r : (n,m:Z)(Zle (Zmin n m) m).

Intros n m;Unfold Zmin ; (ElimCompare 'n 'm);Intros E;Rewrite -> E;[
  Unfold Zle ;Rewrite -> E;Discriminate
| Unfold Zle ;Rewrite -> E;Discriminate
| Apply Zle_n ].
Save.

Lemma Zmin_case : (n,m:Z)(P:Z->Set)(P n)->(P m)->(P (Zmin n m)).
Intros n m P H1 H2; Unfold Zmin; Case (Zcompare n m);Auto with arith.
Save.

Lemma Zmin_or : (n,m:Z)(Zmin n m)=n \/ (Zmin n m)=m.
Unfold Zmin; Intros; Elim (Zcompare n m); Auto.
Save.

Lemma Zmin_n_n : (n:Z) (Zmin n n)=n.
Unfold Zmin; Intros; Elim (Zcompare n n); Auto.
Save.

Lemma Zplus_assoc_l : (n,m,p:Z)((Zplus n (Zplus m p))=(Zplus (Zplus n m) p)).

Exact Zplus_assoc.
Save.

Lemma Zplus_assoc_r : (n,m,p:Z)(Zplus (Zplus n m) p) =(Zplus n (Zplus m p)).

Intros; Symmetry; Apply Zplus_assoc.
Save.

Lemma Zplus_permute : (n,m,p:Z) (Zplus n (Zplus m p))=(Zplus m (Zplus n p)).

Intros n m p;
Rewrite Zplus_sym;Rewrite <- Zplus_assoc; Rewrite (Zplus_sym p n); Trivial with arith.
Save.

Lemma Zsimpl_le_plus_l : (p,n,m:Z)(Zle (Zplus p n) (Zplus p m))->(Zle n m).

Intros p n m; Unfold Zle not ;Intros H1 H2;Apply H1; 
Rewrite (Zcompare_Zplus_compatible n m p); Assumption.
Save.
 
Lemma Zle_reg_l : (n,m,p:Z)(Zle n m)->(Zle (Zplus p n) (Zplus p m)).

Intros n m p; Unfold Zle not ;Intros H1 H2;Apply H1; 
Rewrite <- (Zcompare_Zplus_compatible n m p); Assumption.
Save.

Lemma Zle_reg_r : (a,b,c:Z) (Zle a b)->(Zle (Zplus a c) (Zplus b c)).

Intros a b c;Do 2 Rewrite [n:Z](Zplus_sym n c); Exact (Zle_reg_l a b c).
Save.

Lemma Zle_plus_plus : 
 (n,m,p,q:Z) (Zle n m)->(Zle p q)->(Zle (Zplus n p) (Zplus m q)).

Intros n m p q; Intros H1 H2;Apply Zle_trans with m:=(Zplus n q); [
  Apply Zle_reg_l;Assumption | Apply Zle_reg_r;Assumption ].
Save.

Lemma Zplus_Snm_nSm : (n,m:Z)(Zplus (Zs n) m)=(Zplus n (Zs m)).

Unfold Zs ;Intros n m; Rewrite <- Zplus_assoc; Rewrite (Zplus_sym (POS xH));
Trivial with arith.
Save.

Lemma Zsimpl_lt_plus_l : (n,m,p:Z)(Zlt (Zplus p n) (Zplus p m))->(Zlt n m).

Unfold Zlt ;Intros n m p; Rewrite Zcompare_Zplus_compatible;Trivial with arith.
Save.
 

Lemma Zlt_reg_l : (n,m,p:Z)(Zlt n m)->(Zlt (Zplus p n) (Zplus p m)).

Unfold Zlt ;Intros n m p; Rewrite Zcompare_Zplus_compatible;Trivial with arith.
Save.

Definition Zminus := [m,n:Z](Zplus m (Zopp n)).

Lemma Zminus_plus_simpl : 
  (n,m,p:Z)((Zminus n m)=(Zminus (Zplus p n) (Zplus p m))).

Intros n m p;Unfold Zminus; Rewrite Zopp_Zplus; Rewrite Zplus_assoc;
Rewrite (Zplus_sym p); Rewrite <- (Zplus_assoc n p); Rewrite Zplus_inverse_r;
Rewrite Zero_right; Trivial with arith.
Save.

Lemma Zminus_n_O : (n:Z)(n=(Zminus n ZERO)).

Intro; Unfold Zminus; Simpl;Rewrite Zero_right; Trivial with arith.
Save.

Lemma Zminus_n_n : (n:Z)(ZERO=(Zminus n n)).
Intro; Unfold Zminus; Rewrite Zplus_inverse_r; Trivial with arith.
Save.

Lemma Zplus_minus : (n,m,p:Z)(n=(Zplus m p))->(p=(Zminus n m)).

Intros n m p H;Unfold Zminus;Apply (Zsimpl_plus_l m); 
Rewrite (Zplus_sym m (Zplus n (Zopp m))); Rewrite <- Zplus_assoc;
Rewrite Zplus_inverse_l; Rewrite Zero_right; Rewrite H; Trivial with arith.
Save.
 
Lemma Zminus_plus : (n,m:Z)(Zminus (Zplus n m) n)=m.
Intros n m;Unfold Zminus ;Rewrite -> (Zplus_sym n m);Rewrite <- Zplus_assoc;
Rewrite -> Zplus_inverse_r; Apply Zero_right.
Save.

Lemma Zle_plus_minus : (n,m:Z) (Zplus n (Zminus m n))=m.

Unfold Zminus; Intros n m; Rewrite Zplus_permute; Rewrite Zplus_inverse_r;
Apply Zero_right.
Save.

Lemma Zminus_Sn_m : (n,m:Z)((Zs (Zminus n m))=(Zminus (Zs n) m)).

Intros n m;Unfold Zminus Zs; Rewrite (Zplus_sym n (Zopp m));
Rewrite <- Zplus_assoc;Apply Zplus_sym.
Save.

Lemma Zlt_minus : (n,m:Z)(Zlt ZERO m)->(Zlt (Zminus n m) n).

Intros n m H; Apply Zsimpl_lt_plus_l with p:=m; Rewrite Zle_plus_minus;
Pattern 1 n ;Rewrite <- (Zero_right n); Rewrite (Zplus_sym m n);
Apply Zlt_reg_l; Assumption.
Save.

Lemma Zlt_O_minus_lt : (n,m:Z)(Zlt ZERO (Zminus n m))->(Zlt m n).

Intros n m H; Apply Zsimpl_lt_plus_l with p:=(Zopp m); Rewrite Zplus_inverse_l;
Rewrite Zplus_sym;Exact H.
Save.

Lemma Zmult_plus_distr_l : 
  (n,m,p:Z)((Zmult (Zplus n m) p)=(Zplus (Zmult n p) (Zmult m p))).

Intros n m p;Rewrite Zmult_sym;Rewrite Zmult_plus_distr_r; 
Do 2 Rewrite -> (Zmult_sym p); Trivial with arith.
Save.

Lemma Zmult_minus_distr :
  (n,m,p:Z)((Zmult (Zminus n m) p)=(Zminus (Zmult n p) (Zmult m p))).
Intros n m p;Unfold Zminus; Rewrite Zmult_plus_distr_l; Rewrite Zopp_Zmult;
Trivial with arith.
Save.
 
Lemma Zmult_assoc_r : (n,m,p:Z)((Zmult (Zmult n m) p) = (Zmult n (Zmult m p))).
Intros n m p; Rewrite Zmult_assoc; Trivial with arith.
Save.

Lemma Zmult_assoc_l : (n,m,p:Z)(Zmult n (Zmult m p)) = (Zmult (Zmult n m) p).
Intros n m p; Rewrite Zmult_assoc; Trivial with arith.
Save.

Theorem Zmult_permute : (n,m,p:Z)(Zmult n (Zmult m p)) = (Zmult m (Zmult n p)).
Intros; Rewrite -> (Zmult_assoc m n p); Rewrite -> (Zmult_sym m n).
Apply Zmult_assoc.
Save.

Lemma Zmult_1_n : (n:Z)(Zmult (POS xH) n)=n.
Exact Zmult_one.
Save.

Lemma Zmult_n_1 : (n:Z)(Zmult n (POS xH))=n.
Intro; Rewrite Zmult_sym; Apply Zmult_one.
Save.

Lemma Zmult_Sm_n : (n,m:Z) (Zplus (Zmult n m) m)=(Zmult (Zs n) m).
Intros n m; Unfold Zs; Rewrite Zmult_plus_distr_l; Rewrite Zmult_1_n;
Trivial with arith.
Save.

(* 
  Just for compatibility with previous versions 
  Use [Zmult_plus_distr_r] and [Zmult_plus_distr_l] rather than
  their synomymous 
*)
Definition Zmult_Zplus_distr := Zmult_plus_distr_r.
Definition Zmult_plus_distr := Zmult_plus_distr_l.