-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumerics.v
More file actions
3960 lines (3535 loc) · 100 KB
/
numerics.v
File metadata and controls
3960 lines (3535 loc) · 100 KB
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
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Set Implicit Arguments.
Unset Strict Implicit.
Require Import NArith QArith Qreals Reals Fourier.
Require Import OUVerT.dyadic.
Import List.
Import ListNotations.
Require Import Lra Lia Reals.
Require Import mathcomp.ssreflect.ssreflect.
From mathcomp Require Import all_ssreflect.
From mathcomp Require Import all_algebra.
Import GRing.Theory Num.Def Num.Theory.
(** This file defines conversions between Ssreflect/MathComp and
Coq Standard Library implementations of various numeric types,
such as:
- int <-> Z
- rat <-> Q
- rat -> R
*)
Delimit Scope Numeric_scope with Num.
Local Open Scope Numeric_scope.
Lemma Nat_le_exists_diff: forall m n : nat, Peano.le m n -> exists p, addn m p = n.
Proof.
intros.
exists (subn n m).
rewrite subnKC; auto.
assert(reflect (m <= n)%coq_nat (m <= n)%N).
apply leP; auto.
inversion H0. auto. exfalso. auto.
Qed.
Module Numerics.
Class Numeric (T:Type) :=
mkNumeric {
plus: T -> T -> T where "n + m" := (plus n m) : Numeric_scope;
neg : T->T where "- n" := (neg n) : Num;
mult: T -> T -> T where "n * m" := (mult n m) : Num;
pow_nat: T -> nat -> T;
to_R : T -> R;
of_nat: nat -> T;
plus_id: T;
mult_id: T;
lt: T->T->Prop where "n < m" := (lt n m) : Num;
ltb: T->T->bool;
eqb: T->T->bool;
}.
Infix "+" := plus : Numeric_scope.
Notation "- n" := (neg n) : Numeric_scope.
Infix "*" := mult : Numeric_scope.
Infix "<" := lt : Numeric_scope.
Notation "1" := Numerics.mult_id : Numeric_scope.
Notation "0" := Numerics.plus_id : Numeric_scope.
Class Numeric_Props (T:Type) `{numeric_t : Numeric T} :=
mkNumericProps {
plus_id_lt_mult_id: plus_id < mult_id;
mult_plus_id_l: forall t : T, plus_id * t = plus_id;
of_nat_plus_id: of_nat O = plus_id;
of_nat_succ_l: forall n : nat, of_nat (S n) = mult_id + of_nat n;
plus_comm : forall r1 r2, r1 + r2 = r2 + r1;
plus_assoc : forall r1 r2 r3, r1 + (r2 + r3) = r1 + r2 + r3;
plus_neg_r : forall r, r + - r = plus_id;
plus_id_l : forall r, plus_id + r = r;
mult_comm : forall r1 r2, r1 * r2 = r2 * r1;
mult_assoc : forall r1 r2 r3, r1 * (r2 * r3) = r1 * r2 * r3;
mult_id_l : forall r, mult_id * r = r;
mult_plus_distr_l : forall r1 r2 r3, r1 * (r2 + r3) = r1 * r2 + r1 * r3;
lt_asym : forall r1 r2, r1 < r2 -> ~ r2 < r1;
lt_trans : forall r1 r2 r3, r1 < r2 -> r2 < r3 -> r1 < r3;
plus_lt_compat_l : forall r r1 r2, r1 < r2 -> r + r1 < r + r2;
mult_lt_compat_l : forall r r1 r2, plus_id < r -> (r1 < r2 <-> r * r1 < r * r2);
pow_natO: forall t, pow_nat t O = mult_id;
pow_nat_rec: forall t n, pow_nat t (S n) = t * pow_nat t n;
to_R_plus: forall t1 t2 : T, Rplus (to_R t1) (to_R t2) = to_R (t1 + t2);
to_R_mult: forall t1 t2 : T, Rmult (to_R t1) (to_R t2) = to_R (t1 * t2);
to_R_lt: forall t1 t2 : T, t1 < t2 <-> Rlt (to_R t1) (to_R t2);
to_R_neg: forall t : T, Ropp (to_R t) = to_R (- t);
to_R_inj: forall n m : T, to_R n = to_R m -> n = m;
total_order_T : forall r1 r2, {r1 < r2} + {r1 = r2} + {r2 < r1};
eqb_true_iff: forall n m, eqb n m <-> n = m;
ltb_true_iff: forall n m, ltb n m <-> n < m;
}.
Ltac to_R_distr :=
repeat (
try (rewrite <- to_R_mult);
try (rewrite <- to_R_plus);
try (rewrite <- to_R_neg)
).
Section use_Numeric.
Context {Nt:Type} `{H : Numeric Nt} .
Definition le (n m : Nt) : Prop := n < m \/ n = m.
Infix "<=" := le : Numeric_scope.
Definition leb (x y : Nt) : bool :=
orb (ltb x y) (eqb x y).
Lemma le_lt_or_eq: forall n m : Nt, n < m \/ n = m <-> n <= m .
Proof.
split; auto.
Qed.
Definition minus (n m : Nt) := n + - m.
Context `{NtProps : Numeric_Props (numeric_t := H) Nt}.
Lemma eqb_false_iff: forall n m, eqb n m = false <-> n <> m.
Proof.
intros n m.
split; intros.
{
unfold not. intros.
rewrite <- eqb_true_iff in H1.
rewrite H0 in H1. inversion H1.
}
destruct (eqb n m) eqn:e; auto.
exfalso.
by apply eqb_true_iff in e.
Qed.
Lemma ltb_false_iff: forall n m, ltb n m = false <-> ~ n < m.
Proof.
intros n m.
split; intros.
{
unfold not.
intros.
apply ltb_true_iff in H1.
by rewrite H0 in H1.
}
destruct (ltb n m) eqn:e; auto.
by apply ltb_true_iff in e.
Qed.
Lemma le_lt_dec: forall x y : Nt, {x <= y} + {y < x}.
Proof.
intros.
destruct (total_order_T x y); auto.
destruct s.
{ left. left. auto. }
left. right. auto.
Qed.
Lemma plus_mult_distr_r: forall r1 r2 r3, (r2 + r3) * r1 = r2 * r1 + r3 * r1.
Proof.
intros.
rewrite mult_comm.
rewrite mult_plus_distr_l.
rewrite mult_comm.
rewrite -> mult_comm with r1 r3.
auto.
Qed.
Program Definition numeric_ring := @mk_rt Nt plus_id mult_id plus mult minus neg eq plus_id_l
plus_comm plus_assoc mult_id_l mult_comm mult_assoc _ _ plus_neg_r.
Next Obligation.
apply plus_mult_distr_r.
Qed.
Add Ring NT_RING : numeric_ring.
Lemma lt_irrefl: forall n : Nt, ~ n < n.
Proof.
unfold not.
intros.
assert (~ n < n).
{ apply lt_asym in H0. auto. }
apply H1. auto.
Qed.
Lemma plus_assoc_reverse : forall r1 r2 r3, r1 + r2 + r3 = r1 + (r2 + r3).
Proof. intros. rewrite plus_assoc. auto. Qed.
Lemma mult_plus_id_r: forall t : Nt, t * plus_id = plus_id.
Proof. intros. rewrite mult_comm. apply mult_plus_id_l. Qed.
Lemma lt_not_eq: forall r1 r2 : Nt, r1 < r2 -> r1 <> r2.
Proof.
unfold not.
intros.
rewrite H1 in H0.
apply lt_irrefl in H0.
auto.
Qed.
Lemma eq_dec: forall t1 t2 : Nt, {t1 = t2} + {t1 <> t2}.
Proof.
intros.
destruct total_order_T with t1 t2.
{
destruct s; auto.
apply lt_not_eq in l. auto.
}
right.
apply lt_not_eq in l. auto.
Qed.
Lemma le_not_lt: forall n m : Nt, (n <= m)-> ~ (m < n).
Proof.
intros.
unfold not.
intros.
unfold le in H0.
destruct H0.
{ apply lt_asym in H1. apply H1. auto. }
rewrite H0 in H1.
apply lt_irrefl in H1.
inversion H1.
Qed.
Lemma plus_neg_l: forall t1, (-t1) + t1 = plus_id.
Proof.
intros.
rewrite plus_comm.
apply plus_neg_r.
Qed.
Lemma plus_id_r: forall t : Nt, t + plus_id = t.
Proof.
intros.
rewrite plus_comm.
apply plus_id_l.
Qed.
Lemma nat1_mult_id: of_nat (S O) = mult_id.
Proof.
rewrite of_nat_succ_l.
rewrite of_nat_plus_id.
rewrite plus_id_r.
auto.
Qed.
Lemma double_neg: forall t : Nt, - - t = t.
Proof.
intros.
rewrite <- plus_id_l.
rewrite <- plus_neg_l with (- t).
rewrite <- plus_assoc.
rewrite plus_neg_l.
rewrite plus_id_r.
auto.
Qed.
Lemma neg_plus_id: -plus_id = plus_id.
Proof.
assert(-plus_id = -plus_id + plus_id).
{ rewrite plus_id_r. auto. }
rewrite H0.
rewrite plus_neg_l.
auto.
Qed.
Lemma plus_lt_compat_r : forall r r1 r2, r1 < r2 -> r1 + r < r2 + r.
Proof.
intros.
rewrite plus_comm.
rewrite -> plus_comm with r2 r.
apply plus_lt_compat_l.
auto.
Qed.
Lemma mult_lt_compat_r : forall r r1 r2, plus_id < r -> (r1 < r2 <-> r1 * r < r2 * r).
Proof.
intros.
rewrite mult_comm.
rewrite -> mult_comm with r2 r.
apply mult_lt_compat_l; auto.
Qed.
Lemma plus_elim_l: forall t1 t2 t3: Nt, t1 + t2 = t1 + t3 -> t2 = t3.
Proof.
intros.
rewrite <- plus_id_l.
rewrite <- plus_neg_l with t1.
rewrite <- plus_assoc.
rewrite <- H0.
rewrite plus_assoc.
rewrite plus_neg_l.
rewrite plus_id_l.
auto.
Qed.
Lemma plus_elim_r: forall t1 t2 t3: Nt, t2 + t1 = t3 + t1 -> t2 = t3.
Proof.
intros.
rewrite plus_comm in H0.
rewrite -> plus_comm with t3 t1 in H0.
apply plus_elim_l in H0.
auto.
Qed.
Lemma neg_pos_neg: forall t1 : Nt, plus_id < t1 <-> - t1 < plus_id.
Proof.
intros.
split; intros.
{
apply plus_lt_compat_l with (-t1) plus_id t1 in H0.
rewrite plus_neg_l in H0.
rewrite plus_id_r in H0.
auto.
}
apply plus_lt_compat_l with t1 (-t1) plus_id in H0.
rewrite plus_neg_r in H0.
rewrite plus_id_r in H0.
auto.
Qed.
Lemma plus_neg_distr: forall n m : Nt, - (n + m) = -n + -m.
Proof.
intros.
apply plus_elim_r with m.
rewrite <- plus_assoc.
rewrite plus_neg_l.
rewrite plus_id_r.
apply plus_elim_r with n.
rewrite plus_neg_l.
rewrite <- plus_assoc.
rewrite -> plus_comm with n m.
apply plus_neg_l.
Qed.
Lemma mult_id_r: forall n : Nt, n * 1 = n.
Proof.
intros.
rewrite mult_comm.
apply mult_id_l.
Qed.
Lemma neg_mult_distr_l: forall n m : Nt, - (n * m) = -n * m.
Proof.
intros.
apply plus_elim_r with (n * m).
rewrite plus_neg_l.
rewrite <- plus_mult_distr_r.
rewrite plus_neg_l.
rewrite mult_plus_id_l.
auto.
Qed.
Lemma neg_mult_distr_r: forall n m : Nt, - (n * m) = n * -m.
Proof.
intros.
rewrite -> mult_comm with n (-m).
rewrite <- neg_mult_distr_l.
rewrite mult_comm.
auto.
Qed.
Lemma mult_elim_pos: forall t1 t2 t3 : Nt, plus_id < t1 -> t1 * t2 = t1 * t3 -> t2 = t3.
Proof.
intros.
destruct total_order_T with t2 t3.
{
destruct s; auto.
apply mult_lt_compat_l with t1 t2 t3 in l; auto.
rewrite H1 in l.
apply lt_irrefl in l.
inversion l.
}
apply mult_lt_compat_l with t1 t3 t2 in l; auto.
rewrite H1 in l.
apply lt_irrefl in l.
inversion l.
Qed.
Lemma mult_elim_l: forall t1 t2 t3 : Nt, plus_id <> t1 -> t1 * t2 = t1 * t3 -> t2 = t3.
Proof.
intros.
destruct total_order_T with t1 plus_id.
{
destruct s; auto.
{
apply mult_elim_pos with (-t1).
{ apply neg_pos_neg. rewrite double_neg. auto. }
repeat rewrite <- neg_mult_distr_l.
rewrite H1.
auto.
}
exfalso.
apply H0.
auto.
}
apply mult_elim_pos with t1; auto.
Qed.
Lemma lt_le_dec: forall t1 t2 : Nt, {t1 < t2} + {t2 <= t1}.
Proof.
intros.
unfold le.
destruct total_order_T with t1 t2; auto.
destruct s; auto.
Qed.
Definition abs (x : Nt) : Nt :=
if leb plus_id x then x else -x.
Definition min (x y : Nt) : Nt :=
if leb x y then x else y.
Lemma le_lt_weak: forall (n m : Nt), n < m -> n <= m.
Proof.
intros.
unfold le.
left.
apply H0.
Qed.
Hint Resolve le_lt_weak.
Lemma lt_not_le: forall n m : Nt, (n < m) -> ~ (m <= n).
Proof.
unfold not.
intros.
apply le_not_lt in H0; auto.
Qed.
Lemma le_refl: forall t, t <= t.
Proof.
intros.
unfold le.
auto.
Qed.
Hint Immediate le_refl.
Hint Resolve ltb_true_iff.
Hint Resolve ltb_false_iff.
Lemma leb_true_iff: forall x y : Nt, leb x y <-> x <= y.
Proof.
intros.
unfold leb.
split; intros.
{
apply orb_prop in H0.
destruct H0.
left. by apply ltb_true_iff.
right. by apply eqb_true_iff.
}
destruct H0.
{
apply ltb_true_iff in H0.
by rewrite H0.
}
apply eqb_true_iff in H0.
rewrite H0.
destruct (ltb x y); auto.
Qed.
Hint Resolve leb_true_iff.
Lemma leb_false_iff: forall x y : Nt, leb x y = false <-> ~ x <= y.
Proof.
intros.
unfold not.
split; intros.
{
apply leb_true_iff in H1. by rewrite H1 in H0.
}
destruct (leb x y) eqn:e; auto.
apply leb_true_iff in e. apply H0 in e. inversion e.
Qed.
Hint Resolve leb_false_iff.
Lemma not_lt_le: forall n m : Nt, ~ (n < m) -> m <= n.
Proof.
intros.
destruct le_lt_dec with n m; auto.
unfold le in l.
destruct l; auto.
{ exfalso; auto. }
rewrite H1. auto.
Qed.
Lemma not_le_lt: forall n m : Nt, ~ (n <= m) -> m < n.
Proof.
intros.
destruct lt_le_dec with n m.
{ apply le_lt_weak in l. exfalso; auto. }
unfold le in l.
destruct l; auto.
rewrite H1 in H0.
exfalso.
auto.
Qed.
Lemma leb_refl: forall n : Nt, leb n n = true.
Proof.
intros. auto.
apply leb_true_iff.
apply le_refl.
Qed.
Hint Resolve leb_refl.
Lemma ltb_irrefl: forall n : Nt, ltb n n = false.
Proof.
intros.
apply ltb_false_iff.
apply lt_irrefl.
Qed.
Lemma eqb_refl: forall n : Nt, eqb n n.
Proof. intros. rewrite eqb_true_iff. auto. Qed.
Lemma eqb_symm: forall n m: Nt, eqb n m = eqb m n.
Proof.
intros.
destruct (eqb n m) eqn:e.
{ apply eqb_true_iff in e. rewrite e. rewrite eqb_refl. auto. }
apply eqb_false_iff in e.
assert (m <> n). unfold not. intros. apply e. auto.
apply eqb_false_iff in H0.
auto.
Qed.
Hint Resolve ltb_irrefl.
Hint Resolve plus_le_compat_l.
Hint Resolve plus_le_compat_r.
Hint Resolve plus_lt_compat_l.
Hint Resolve plus_le_compat_r.
Lemma plus_lt_compat: forall t1 t2 t3 t4, t1 < t2 -> t3 < t4 -> (t1 + t3) < (t2 + t4).
Proof.
intros.
apply plus_lt_compat_l with t3 t1 t2 in H0.
apply plus_lt_compat_l with t2 t3 t4 in H1.
apply lt_trans with (t2 + t3); auto.
rewrite plus_comm.
rewrite -> plus_comm with t2 t3.
auto.
Qed.
Lemma plus_lt_le_compat: forall t1 t2 t3 t4, t1 < t2 -> t3 <= t4 -> (t1 + t3 ) < (t2 + t4).
Proof.
intros.
destruct H1.
{ apply plus_lt_compat; auto. }
rewrite H1.
rewrite plus_comm.
rewrite -> plus_comm with t2 t4.
auto.
Qed.
Lemma plus_le_lt_compat: forall t1 t2 t3 t4, t1 <= t2 -> t3 < t4 -> (t1 + t3 ) < (t2 + t4).
Proof.
intros.
rewrite plus_comm.
rewrite -> plus_comm with t2 t4.
apply plus_lt_le_compat; auto.
Qed.
Lemma plus_le_compat: forall t1 t2 t3 t4, t1 <= t2 -> t3 <= t4 -> (t1 + t3) <= (t2 + t4).
Proof.
intros.
unfold le in *.
destruct H1; destruct H0.
{ left. apply plus_lt_compat; auto. }
{ left. apply plus_le_lt_compat; auto. unfold le. right. auto. }
{ left. apply plus_lt_le_compat; auto. unfold le. right. auto. }
right.
rewrite H0.
rewrite H1.
auto.
Qed.
Lemma lt_le_trans: forall x y z : Nt, x < y -> y <= z -> x < z.
Proof.
intros.
rewrite <- plus_id_r.
rewrite <- plus_id_r with x.
rewrite <- plus_neg_r with y.
repeat rewrite plus_assoc.
rewrite -> plus_comm with z y.
apply plus_lt_le_compat.
2 : { apply le_refl. }
apply plus_lt_le_compat; auto.
Qed.
Lemma le_not_eq_lt: forall x y : Nt, x <= y -> x <> y -> x < y.
Proof.
intros.
unfold le in H0.
destruct H0; intuition.
Qed.
Lemma le_trans: forall x y z : Nt, x <= y -> y <= z -> x <= z.
Proof.
intros.
destruct eq_dec with x y.
{ rewrite e. apply H1. }
apply le_lt_weak.
apply lt_le_trans with y; auto.
apply le_not_eq_lt; auto.
Qed.
Hint Resolve mult_plus_id_l.
Hint Resolve mult_plus_id_r.
Hint Resolve mult_id_l.
Hint Resolve mult_id_r.
Hint Resolve plus_id_l.
Hint Resolve plus_id_r.
Lemma mult_le_compat_l: forall x y z : Nt, plus_id <= x -> y <= z -> x * y <= x * z.
Proof.
intros.
unfold le in *.
destruct H0.
2: {right. rewrite <- H0. repeat rewrite mult_plus_id_l. auto. }
destruct H1.
{ left. apply mult_lt_compat_l; auto. }
right.
rewrite H1.
auto.
Qed.
Lemma mult_le_compat_l_reverse: forall x y z : Nt, plus_id < x -> x * y <= x* z -> y <= z.
Proof.
intros.
unfold le in *.
destruct H1.
{
left.
rewrite mult_lt_compat_l; auto.
apply H1.
auto.
}
right.
assert (0 <> x). apply lt_not_eq. auto.
apply mult_elim_l with x; auto.
Qed.
Lemma mult_le_compat_r: forall x y z : Nt, plus_id <= x -> y <= z -> y * x <= z * x.
Proof.
intros.
rewrite mult_comm.
rewrite -> mult_comm with z x.
apply mult_le_compat_l; auto.
Qed.
Lemma mult_le_compat_r_reverse: forall x y z : Nt, plus_id < x -> y * x <= z * x -> y <= z.
Proof.
intros.
rewrite mult_comm in H1.
rewrite -> mult_comm with z x in H1.
apply mult_le_compat_l_reverse with x; auto.
Qed.
Lemma le_both_eq: forall x y : Nt, x <= y -> y <= x -> x = y.
Proof.
intros.
destruct H0; auto.
apply lt_not_le in H0.
exfalso. auto.
Qed.
Lemma neg_neg_pos: forall t1 : Nt, t1 < 0 <-> 0 < - t1.
Proof.
intros.
split; intros.
{ apply neg_pos_neg. rewrite double_neg. auto. }
apply neg_pos_neg in H0. rewrite double_neg in H0. auto.
Qed.
Hint Resolve plus_mult_distr_r.
Lemma n_plus_n_eq_2n: forall n : Nt, n + n = (1 + 1) * n.
Proof.
intros. auto 20.
rewrite plus_mult_distr_r.
rewrite mult_id_l.
auto.
Qed.
Lemma lt_neg: forall n m : Nt, n < m <-> - m < - n.
Proof.
intros.
split; intros.
{
apply plus_lt_compat_l with (-n) n m in H0.
rewrite plus_neg_l in H0.
apply plus_lt_compat_r with (-m) 0 (-n + m) in H0.
rewrite <- plus_assoc in H0.
rewrite plus_neg_r in H0.
rewrite plus_id_r in H0.
rewrite plus_id_l in H0.
auto.
}
apply plus_lt_compat_l with n (-m) (-n) in H0.
rewrite plus_neg_r in H0.
apply plus_lt_compat_r with m (n + -m) 0 in H0.
rewrite <- plus_assoc in H0.
rewrite plus_neg_l in H0.
rewrite plus_id_r in H0.
rewrite plus_id_l in H0.
auto.
Qed.
Lemma le_lt_trans: forall x y z : Nt, x <= y -> y < z -> x < z.
Proof.
intros.
unfold le in H0.
destruct H0.
apply lt_trans with y; auto.
rewrite H0. auto.
Qed.
Lemma neg_eq: forall n m : Nt, -n = -m -> n = m.
Proof.
intros.
rewrite -> plus_elim_r with (-n) n m; auto.
rewrite plus_neg_r.
rewrite -> plus_elim_r with (-m) 0 (m + - n); auto.
rewrite -> plus_comm with m (- n).
rewrite <- plus_assoc.
rewrite plus_neg_r.
rewrite plus_id_l.
rewrite plus_id_r.
rewrite H0.
auto.
Qed.
Lemma mult_le_compat: forall r1 r2 r3 r4,plus_id <= r1 -> plus_id <= r3 -> r1 <= r2 -> r3 <= r4 ->
(r1 * r3) <= (r2 * r4).
Proof.
intros.
destruct total_order_T with r2 0;
try (destruct s); try (
unfold le in H0,H1;
destruct H0; destruct H1; try (
apply mult_le_compat_l with r2 r3 r4 in H3;
try (unfold le; auto; fail);
apply mult_le_compat_l with r3 r1 r2 in H2;
try (unfold le; auto; fail);
apply le_trans with (r3 * r2);
rewrite mult_comm; auto; fail); fail
).
unfold le in H0.
destruct H0.
{
exfalso.
apply lt_irrefl with 0.
apply lt_le_trans with r1; auto.
apply le_trans with r2; auto.
}
rewrite <- H0 in H2.
exfalso.
apply lt_irrefl with 0.
apply le_lt_trans with r2; auto.
Qed.
Lemma mult_lt_0_compat: forall r1 r2 : Nt, 0 < r1 -> 0 < r2 -> 0 < r1 * r2.
Proof.
intros.
apply mult_lt_compat_l with r1 0 r2 in H1; auto.
rewrite mult_plus_id_r in H1.
auto.
Qed.
Lemma plus_le_compat_l : forall r r1 r2, r1 <= r2 -> r + r1 <= r + r2.
Proof.
unfold le.
intros.
destruct H0.
{ left. apply plus_lt_compat_l. auto. }
rewrite H0.
auto.
Qed.
Lemma plus_le_compat_r : forall r r1 r2, r1 <= r2 -> r1 + r <= r2 + r.
Proof.
intros.
rewrite plus_comm.
rewrite -> plus_comm with r2 r.
apply plus_le_compat_l.
auto.
Qed.
Lemma plus_le_compat_l_reverse : forall r r1 r2, r + r1 <= r + r2 -> r1 <= r2 .
Proof.
intros.
apply plus_le_compat_l with (-r) (r + r1) (r + r2) in H0.
repeat rewrite plus_assoc in H0.
rewrite plus_neg_l in H0.
repeat rewrite plus_id_l in H0.
auto.
Qed.
Lemma plus_le_compat_r_reverse : forall r r1 r2, r1 + r <= r2 + r -> r1 <= r2 .
Proof.
intros.
rewrite plus_comm in H0.
rewrite -> plus_comm with r2 r in H0.
apply plus_le_compat_l_reverse in H0.
auto.
Qed.
Lemma mult_simpl_l: forall n m p : Nt, n = m -> p * n = p * m.
Proof. intros. rewrite H0. auto. Qed.
Lemma mult_simpl_r: forall n m p : Nt, n = m -> n * p = m * p.
Proof. intros. rewrite H0. auto. Qed.
Lemma plus_simpl_l: forall n m p : Nt, n = m -> p + n = p + m.
Proof. intros. rewrite H0. auto. Qed.
Lemma plus_simpl_r: forall n m p : Nt, n = m -> n + p = m + p.
Proof. intros. rewrite H0. auto. Qed.
Lemma abs_mult_pos_l: forall n m : Nt, 0 <= n -> abs (n * m) = n * abs m.
Proof.
intros.
unfold abs.
destruct (leb 0 m) eqn:e.
{
apply leb_true_iff in e.
apply mult_le_compat_l with n 0 m in e; auto.
rewrite mult_plus_id_r in e.
apply leb_true_iff in e.
rewrite e.
auto.
}
apply leb_false_iff in e.
apply not_le_lt in e.
destruct H0.
2: {
rewrite <- H0.
repeat rewrite mult_plus_id_l.
rewrite leb_refl.
auto.
}
apply mult_lt_compat_l with n m 0 in e; auto.
rewrite <- mult_plus_id_l with 0 in e.
repeat rewrite mult_plus_id_r in e.
apply lt_not_le in e.
apply leb_false_iff in e.
rewrite e.
rewrite neg_mult_distr_r.
auto.
Qed.
Lemma abs_mult_pos_r: forall n m : Nt, 0 <= n -> abs (m * n) = abs m * n.
Proof. intros. rewrite mult_comm. rewrite abs_mult_pos_l; auto. apply mult_comm. Qed.
Lemma le_abs: forall n : Nt, n <= abs n.
Proof.
intros.
unfold abs.
destruct (leb 0 n) eqn:e.
apply le_refl.
apply plus_le_compat_l_reverse with n.
rewrite plus_neg_r.
rewrite <- plus_id_l.
apply leb_false_iff in e.
apply not_le_lt in e.
apply le_lt_weak in e.
apply plus_le_compat; auto.
Qed.
Lemma le_neg: forall n m : Nt, - n <= -m <-> m <= n.
Proof.
unfold le.
intros.
split; intros;
destruct H0;
try (apply lt_neg in H0; auto; fail);
try (apply neg_eq in H0);
try (rewrite H0);
auto.
Qed.
Lemma abs_neg: forall n : Nt, abs (-n) = abs n.
Proof.
intros.
unfold abs.
destruct (leb 0 (- n) ) eqn:e.
{
apply leb_true_iff in e.
rewrite <- neg_plus_id in e.
rewrite -> le_neg in e.
unfold le in e.
destruct e.
{
apply lt_not_le in H0.
apply leb_false_iff in H0.
rewrite H0.
auto.
}
rewrite H0.
destruct (leb 0 0); auto.
apply neg_plus_id.
}
apply leb_false_iff in e.
apply not_le_lt in e.
rewrite <- neg_plus_id in e.
apply lt_neg in e.
apply le_lt_weak in e.
apply leb_true_iff in e.
rewrite e.
apply double_neg.
Qed.
Lemma abs_posb: forall n : Nt, leb 0 n -> abs n = n.
Proof. intros. unfold abs. rewrite H0. auto. Qed.
Lemma abs_negb: forall n : Nt, leb 0 n = false -> abs n = - n.
Proof. intros. unfold abs. rewrite H0. auto. Qed.
Lemma abs_plus_le: forall n m : Nt, abs (n + m) <= abs n + abs m.
Proof.
intros.
destruct (leb 0 (n + m)) eqn:e_nm.
{
rewrite abs_posb; auto.
apply plus_le_compat; apply le_abs.
}
rewrite abs_negb; auto.
rewrite plus_neg_distr.
apply plus_le_compat; rewrite <- abs_neg; apply le_abs.
Qed.
Lemma pow_nat_add: forall (n m : nat) (x : Nt), pow_nat x (n + m) = pow_nat x n * pow_nat x m.
Proof.
intros.
induction m.
rewrite pow_natO. rewrite mult_id_r. rewrite addn0. auto.
rewrite addnS.
repeat rewrite pow_nat_rec.
rewrite IHm.
rewrite mult_assoc.
rewrite -> mult_comm with x (pow_nat x n).
rewrite mult_assoc.