forked from jrh13/hol-light
-
Notifications
You must be signed in to change notification settings - Fork 0
/
realax.ml
1971 lines (1734 loc) · 87.2 KB
/
realax.ml
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
(* ========================================================================= *)
(* Theory of real numbers. *)
(* *)
(* John Harrison, University of Cambridge Computer Laboratory *)
(* *)
(* (c) Copyright, University of Cambridge 1998 *)
(* (c) Copyright, John Harrison 1998-2007 *)
(* ========================================================================= *)
needs "lists.ml";;
(* ------------------------------------------------------------------------- *)
(* The main infix overloaded operations *)
(* ------------------------------------------------------------------------- *)
parse_as_infix("++",(16,"right"));;
parse_as_infix("**",(20,"right"));;
parse_as_infix("<<=",(12,"right"));;
parse_as_infix("===",(10,"right"));;
parse_as_infix ("treal_mul",(20,"right"));;
parse_as_infix ("treal_add",(16,"right"));;
parse_as_infix ("treal_le",(12,"right"));;
parse_as_infix ("treal_eq",(10,"right"));;
make_overloadable "+" `:A->A->A`;;
make_overloadable "-" `:A->A->A`;;
make_overloadable "*" `:A->A->A`;;
make_overloadable "/" `:A->A->A`;;
make_overloadable "<" `:A->A->bool`;;
make_overloadable "<=" `:A->A->bool`;;
make_overloadable ">" `:A->A->bool`;;
make_overloadable ">=" `:A->A->bool`;;
make_overloadable "--" `:A->A`;;
make_overloadable "pow" `:A->num->A`;;
make_overloadable "inv" `:A->A`;;
make_overloadable "abs" `:A->A`;;
make_overloadable "max" `:A->A->A`;;
make_overloadable "min" `:A->A->A`;;
make_overloadable "&" `:num->A`;;
do_list overload_interface
["+",`(+):num->num->num`; "-",`(-):num->num->num`;
"*",`(*):num->num->num`; "<",`(<):num->num->bool`;
"<=",`(<=):num->num->bool`; ">",`(>):num->num->bool`;
">=",`(>=):num->num->bool`];;
let prioritize_num() = prioritize_overload(mk_type("num",[]));;
(* ------------------------------------------------------------------------- *)
(* Absolute distance function on the naturals. *)
(* ------------------------------------------------------------------------- *)
let dist = new_definition
`dist(m,n) = (m - n) + (n - m)`;;
(* ------------------------------------------------------------------------- *)
(* Some easy theorems. *)
(* ------------------------------------------------------------------------- *)
let DIST_REFL = prove
(`!n. dist(n,n) = 0`,
REWRITE_TAC[dist; SUB_REFL; ADD_CLAUSES]);;
let DIST_LZERO = prove
(`!n. dist(0,n) = n`,
REWRITE_TAC[dist; SUB_0; ADD_CLAUSES]);;
let DIST_RZERO = prove
(`!n. dist(n,0) = n`,
REWRITE_TAC[dist; SUB_0; ADD_CLAUSES]);;
let DIST_SYM = prove
(`!m n. dist(m,n) = dist(n,m)`,
REWRITE_TAC[dist] THEN MATCH_ACCEPT_TAC ADD_SYM);;
let DIST_LADD = prove
(`!m p n. dist(m + n,m + p) = dist(n,p)`,
REWRITE_TAC[dist; SUB_ADD_LCANCEL]);;
let DIST_RADD = prove
(`!m p n. dist(m + p,n + p) = dist(m,n)`,
REWRITE_TAC[dist; SUB_ADD_RCANCEL]);;
let DIST_LADD_0 = prove
(`!m n. dist(m + n,m) = n`,
REWRITE_TAC[dist; ADD_SUB2; ADD_SUBR2; ADD_CLAUSES]);;
let DIST_RADD_0 = prove
(`!m n. dist(m,m + n) = n`,
ONCE_REWRITE_TAC[DIST_SYM] THEN MATCH_ACCEPT_TAC DIST_LADD_0);;
let DIST_LMUL = prove
(`!m n p. m * dist(n,p) = dist(m * n,m * p)`,
REWRITE_TAC[dist; LEFT_ADD_DISTRIB; LEFT_SUB_DISTRIB]);;
let DIST_RMUL = prove
(`!m n p. dist(m,n) * p = dist(m * p,n * p)`,
REWRITE_TAC[dist; RIGHT_ADD_DISTRIB; RIGHT_SUB_DISTRIB]);;
let DIST_EQ_0 = prove
(`!m n. (dist(m,n) = 0) <=> (m = n)`,
REWRITE_TAC[dist; ADD_EQ_0; SUB_EQ_0; LE_ANTISYM]);;
(* ------------------------------------------------------------------------- *)
(* Simplifying theorem about the distance operation. *)
(* ------------------------------------------------------------------------- *)
let DIST_ELIM_THM = prove
(`P(dist(x,y)) <=> !d. ((x = y + d) ==> P(d)) /\ ((y = x + d) ==> P(d))`,
DISJ_CASES_TAC(SPECL [`x:num`; `y:num`] LE_CASES) THEN
POP_ASSUM(X_CHOOSE_THEN `e:num` SUBST1_TAC o REWRITE_RULE[LE_EXISTS]) THEN
REWRITE_TAC[dist; ADD_SUB; ADD_SUB2; ADD_SUBR; ADD_SUBR2] THEN
REWRITE_TAC[ADD_CLAUSES; EQ_ADD_LCANCEL] THEN
GEN_REWRITE_TAC (RAND_CONV o ONCE_DEPTH_CONV) [EQ_SYM_EQ] THEN
REWRITE_TAC[GSYM ADD_ASSOC; EQ_ADD_LCANCEL_0; ADD_EQ_0] THEN
ASM_CASES_TAC `e = 0` THEN ASM_REWRITE_TAC[] THEN
EQ_TAC THEN REPEAT STRIP_TAC THEN ASM_REWRITE_TAC[] THEN
FIRST_ASSUM MATCH_MP_TAC THEN ASM_REWRITE_TAC[]);;
(* ------------------------------------------------------------------------- *)
(* Now some more theorems. *)
(* ------------------------------------------------------------------------- *)
let DIST_LE_CASES,DIST_ADDBOUND,DIST_TRIANGLE,DIST_ADD2,DIST_ADD2_REV =
let DIST_ELIM_TAC =
let conv =
HIGHER_REWRITE_CONV[SUB_ELIM_THM; COND_ELIM_THM; DIST_ELIM_THM] false in
CONV_TAC conv THEN TRY GEN_TAC THEN CONJ_TAC THEN
DISCH_THEN(fun th -> SUBST_ALL_TAC th THEN
(let l,r = dest_eq (concl th) in
if is_var l && not (vfree_in l r) then ALL_TAC
else ASSUME_TAC th)) in
let DIST_ELIM_TAC' =
REPEAT STRIP_TAC THEN REPEAT DIST_ELIM_TAC THEN
REWRITE_TAC[GSYM NOT_LT; LT_EXISTS] THEN
DISCH_THEN(CHOOSE_THEN SUBST_ALL_TAC) THEN POP_ASSUM MP_TAC THEN
CONV_TAC(LAND_CONV NUM_CANCEL_CONV) THEN
REWRITE_TAC[ADD_CLAUSES; NOT_SUC] in
let DIST_LE_CASES = prove
(`!m n p. dist(m,n) <= p <=> (m <= n + p) /\ (n <= m + p)`,
REPEAT GEN_TAC THEN REPEAT DIST_ELIM_TAC THEN
REWRITE_TAC[GSYM ADD_ASSOC; LE_ADD; LE_ADD_LCANCEL])
and DIST_ADDBOUND = prove
(`!m n. dist(m,n) <= m + n`,
REPEAT GEN_TAC THEN DIST_ELIM_TAC THENL
[ONCE_REWRITE_TAC[ADD_SYM]; ALL_TAC] THEN
REWRITE_TAC[ADD_ASSOC; LE_ADDR])
and [DIST_TRIANGLE; DIST_ADD2; DIST_ADD2_REV] = (CONJUNCTS o prove)
(`(!m n p. dist(m,p) <= dist(m,n) + dist(n,p)) /\
(!m n p q. dist(m + n,p + q) <= dist(m,p) + dist(n,q)) /\
(!m n p q. dist(m,p) <= dist(m + n,p + q) + dist(n,q))`,
DIST_ELIM_TAC') in
DIST_LE_CASES,DIST_ADDBOUND,DIST_TRIANGLE,DIST_ADD2,DIST_ADD2_REV;;
let DIST_TRIANGLE_LE = prove
(`!m n p q. dist(m,n) + dist(n,p) <= q ==> dist(m,p) <= q`,
REPEAT STRIP_TAC THEN MATCH_MP_TAC LE_TRANS THEN
EXISTS_TAC `dist(m,n) + dist(n,p)` THEN ASM_REWRITE_TAC[DIST_TRIANGLE]);;
let DIST_TRIANGLES_LE = prove
(`!m n p q r s.
dist(m,n) <= r /\ dist(p,q) <= s ==> dist(m,p) <= dist(n,q) + r + s`,
REPEAT STRIP_TAC THEN MATCH_MP_TAC DIST_TRIANGLE_LE THEN
EXISTS_TAC `n:num` THEN GEN_REWRITE_TAC RAND_CONV [ADD_SYM] THEN
REWRITE_TAC[GSYM ADD_ASSOC] THEN MATCH_MP_TAC LE_ADD2 THEN
ASM_REWRITE_TAC[] THEN MATCH_MP_TAC DIST_TRIANGLE_LE THEN
EXISTS_TAC `q:num` THEN GEN_REWRITE_TAC RAND_CONV [ADD_SYM] THEN
REWRITE_TAC[LE_ADD_LCANCEL] THEN ONCE_REWRITE_TAC[DIST_SYM] THEN
ASM_REWRITE_TAC[]);;
(* ------------------------------------------------------------------------- *)
(* Useful lemmas about bounds. *)
(* ------------------------------------------------------------------------- *)
let BOUNDS_LINEAR = prove
(`!A B C. (!n. A * n <= B * n + C) <=> A <= B`,
REPEAT GEN_TAC THEN EQ_TAC THENL
[CONV_TAC CONTRAPOS_CONV THEN REWRITE_TAC[NOT_LE] THEN
DISCH_THEN(CHOOSE_THEN SUBST1_TAC o REWRITE_RULE[LT_EXISTS]) THEN
REWRITE_TAC[RIGHT_ADD_DISTRIB; LE_ADD_LCANCEL] THEN
DISCH_THEN(MP_TAC o SPEC `SUC C`) THEN
REWRITE_TAC[NOT_LE; MULT_CLAUSES; ADD_CLAUSES; LT_SUC_LE] THEN
REWRITE_TAC[ADD_ASSOC; LE_ADDR];
DISCH_THEN(CHOOSE_THEN SUBST1_TAC o REWRITE_RULE[LE_EXISTS]) THEN
REWRITE_TAC[RIGHT_ADD_DISTRIB; GSYM ADD_ASSOC; LE_ADD]]);;
let BOUNDS_LINEAR_0 = prove
(`!A B. (!n. A * n <= B) <=> (A = 0)`,
REPEAT GEN_TAC THEN MP_TAC(SPECL [`A:num`; `0`; `B:num`] BOUNDS_LINEAR) THEN
REWRITE_TAC[MULT_CLAUSES; ADD_CLAUSES; LE]);;
let BOUNDS_DIVIDED = prove
(`!P. (?B. !n. P(n) <= B) <=>
(?A B. !n. n * P(n) <= A * n + B)`,
GEN_TAC THEN EQ_TAC THEN STRIP_TAC THENL
[MAP_EVERY EXISTS_TAC [`B:num`; `0`] THEN
GEN_TAC THEN REWRITE_TAC[ADD_CLAUSES] THEN
GEN_REWRITE_TAC RAND_CONV [MULT_SYM] THEN
ASM_REWRITE_TAC[LE_MULT_LCANCEL];
EXISTS_TAC `P(0) + A + B` THEN GEN_TAC THEN
MP_TAC(SPECL [`n:num`; `(P:num->num) n`; `P(0) + A + B`]
LE_MULT_LCANCEL) THEN
ASM_CASES_TAC `n = 0` THEN ASM_REWRITE_TAC[LE_ADD] THEN
DISCH_THEN(SUBST1_TAC o SYM) THEN
MATCH_MP_TAC LE_TRANS THEN EXISTS_TAC `A * n + B` THEN
ASM_REWRITE_TAC[] THEN REWRITE_TAC[LEFT_ADD_DISTRIB] THEN
GEN_REWRITE_TAC RAND_CONV [ADD_SYM] THEN
GEN_REWRITE_TAC (RAND_CONV o ONCE_DEPTH_CONV) [MULT_SYM] THEN
REWRITE_TAC[GSYM ADD_ASSOC; LE_ADD_LCANCEL] THEN
MATCH_MP_TAC LE_TRANS THEN EXISTS_TAC `B * n` THEN
REWRITE_TAC[LE_ADD] THEN UNDISCH_TAC `~(n = 0)` THEN
SPEC_TAC(`n:num`,`n:num`) THEN INDUCT_TAC THEN
ASM_REWRITE_TAC[MULT_CLAUSES; LE_ADD]]);;
let BOUNDS_NOTZERO = prove
(`!P A B. (P 0 0 = 0) /\ (!m n. P m n <= A * (m + n) + B) ==>
(?B. !m n. P m n <= B * (m + n))`,
REPEAT STRIP_TAC THEN EXISTS_TAC `A + B` THEN
REPEAT GEN_TAC THEN ASM_CASES_TAC `m + n = 0` THENL
[RULE_ASSUM_TAC(REWRITE_RULE[ADD_EQ_0]) THEN ASM_REWRITE_TAC[LE_0];
MATCH_MP_TAC LE_TRANS THEN EXISTS_TAC `A * (m + n) + B` THEN
ASM_REWRITE_TAC[] THEN REWRITE_TAC[RIGHT_ADD_DISTRIB; LE_ADD_LCANCEL] THEN
UNDISCH_TAC `~(m + n = 0)` THEN SPEC_TAC(`m + n`,`p:num`) THEN
INDUCT_TAC THEN REWRITE_TAC[MULT_CLAUSES; LE_ADD]]);;
let BOUNDS_IGNORE = prove
(`!P Q. (?B. !i. P(i) <= Q(i) + B) <=>
(?B N. !i. N <= i ==> P(i) <= Q(i) + B)`,
REPEAT GEN_TAC THEN EQ_TAC THEN STRIP_TAC THENL
[EXISTS_TAC `B:num` THEN ASM_REWRITE_TAC[];
POP_ASSUM MP_TAC THEN SPEC_TAC(`B:num`,`B:num`) THEN
SPEC_TAC(`N:num`,`N:num`) THEN INDUCT_TAC THENL
[REWRITE_TAC[LE_0] THEN GEN_TAC THEN DISCH_TAC THEN
EXISTS_TAC `B:num` THEN ASM_REWRITE_TAC[];
GEN_TAC THEN DISCH_TAC THEN FIRST_ASSUM MATCH_MP_TAC THEN
EXISTS_TAC `B + P(N:num)` THEN X_GEN_TAC `i:num` THEN
DISCH_TAC THEN ASM_CASES_TAC `SUC N <= i` THENL
[MATCH_MP_TAC LE_TRANS THEN EXISTS_TAC `Q(i:num) + B` THEN
REWRITE_TAC[LE_ADD; ADD_ASSOC] THEN FIRST_ASSUM MATCH_MP_TAC THEN
ASM_REWRITE_TAC[];
UNDISCH_TAC `~(SUC N <= i)` THEN REWRITE_TAC[NOT_LE; LT] THEN
ASM_REWRITE_TAC[GSYM NOT_LE] THEN DISCH_THEN SUBST1_TAC THEN
REWRITE_TAC[ADD_ASSOC] THEN ONCE_REWRITE_TAC[ADD_SYM] THEN
REWRITE_TAC[LE_ADD]]]]);;
(* ------------------------------------------------------------------------- *)
(* Define type of nearly additive functions. *)
(* ------------------------------------------------------------------------- *)
let is_nadd = new_definition
`is_nadd x <=> (?B. !m n. dist(m * x(n),n * x(m)) <= B * (m + n))`;;
let is_nadd_0 = prove
(`is_nadd (\n. 0)`,
REWRITE_TAC[is_nadd; MULT_CLAUSES; DIST_REFL; LE_0]);;
let nadd_abs,nadd_rep =
new_basic_type_definition "nadd" ("mk_nadd","dest_nadd") is_nadd_0;;
override_interface ("fn",`dest_nadd`);;
override_interface ("afn",`mk_nadd`);;
(* ------------------------------------------------------------------------- *)
(* Properties of nearly-additive functions. *)
(* ------------------------------------------------------------------------- *)
let NADD_CAUCHY = prove
(`!x. ?B. !m n. dist(m * fn x n,n * fn x m) <= B * (m + n)`,
REWRITE_TAC[GSYM is_nadd; nadd_rep; nadd_abs; ETA_AX]);;
let NADD_BOUND = prove
(`!x. ?A B. !n. fn x n <= A * n + B`,
GEN_TAC THEN X_CHOOSE_TAC `B:num` (SPEC `x:nadd` NADD_CAUCHY) THEN
MAP_EVERY EXISTS_TAC [`B + fn x 1`; `B:num`] THEN GEN_TAC THEN
POP_ASSUM(MP_TAC o SPECL [`n:num`; `1`]) THEN
REWRITE_TAC[DIST_LE_CASES; MULT_CLAUSES] THEN
DISCH_THEN(MP_TAC o CONJUNCT2) THEN
REWRITE_TAC[LEFT_ADD_DISTRIB; RIGHT_ADD_DISTRIB; MULT_CLAUSES] THEN
REWRITE_TAC[ADD_AC; MULT_AC]);;
let NADD_MULTIPLICATIVE = prove
(`!x. ?B. !m n. dist(fn x (m * n),m * fn x n) <= B * m + B`,
GEN_TAC THEN X_CHOOSE_TAC `B:num` (SPEC `x:nadd` NADD_CAUCHY) THEN
EXISTS_TAC `B + fn x 0` THEN REPEAT GEN_TAC THEN
ASM_CASES_TAC `n = 0` THENL
[MATCH_MP_TAC (LE_IMP DIST_ADDBOUND) THEN
ASM_REWRITE_TAC[MULT_CLAUSES; RIGHT_ADD_DISTRIB; MULT_AC] THEN
REWRITE_TAC[LE_EXISTS] THEN CONV_TAC(ONCE_DEPTH_CONV NUM_CANCEL_CONV) THEN
REWRITE_TAC[GSYM EXISTS_REFL]; UNDISCH_TAC `~(n = 0)`] THEN
REWRITE_TAC[TAUT `(~a ==> b) <=> a \/ b`; GSYM LE_MULT_LCANCEL;
DIST_LMUL] THEN
REWRITE_TAC[MULT_ASSOC] THEN GEN_REWRITE_TAC
(LAND_CONV o RAND_CONV o RAND_CONV o LAND_CONV) [MULT_SYM] THEN
POP_ASSUM(MATCH_MP_TAC o LE_IMP) THEN
REWRITE_TAC[LE_EXISTS; RIGHT_ADD_DISTRIB; LEFT_ADD_DISTRIB; MULT_AC] THEN
CONV_TAC(ONCE_DEPTH_CONV NUM_CANCEL_CONV) THEN
REWRITE_TAC[GSYM EXISTS_REFL]);;
let NADD_ADDITIVE = prove
(`!x. ?B. !m n. dist(fn x (m + n),fn x m + fn x n) <= B`,
GEN_TAC THEN X_CHOOSE_TAC `B:num` (SPEC `x:nadd` NADD_CAUCHY) THEN
EXISTS_TAC `3 * B + fn x 0` THEN REPEAT GEN_TAC THEN
ASM_CASES_TAC `m + n = 0` THENL
[RULE_ASSUM_TAC(REWRITE_RULE[ADD_EQ_0]) THEN ONCE_REWRITE_TAC[DIST_SYM] THEN
ASM_REWRITE_TAC[ADD_CLAUSES; DIST_LADD_0; LE_ADDR];
MATCH_MP_TAC LE_TRANS THEN EXISTS_TAC `3 * B` THEN
REWRITE_TAC[LE_ADD] THEN UNDISCH_TAC `~(m + n = 0)`] THEN
REWRITE_TAC[TAUT `(~a ==> b) <=> a \/ b`; GSYM LE_MULT_LCANCEL] THEN
REWRITE_TAC[DIST_LMUL; LEFT_ADD_DISTRIB] THEN
GEN_REWRITE_TAC (LAND_CONV o RAND_CONV o LAND_CONV) [RIGHT_ADD_DISTRIB] THEN
MATCH_MP_TAC(LE_IMP DIST_ADD2) THEN
SUBGOAL_THEN `(m + n) * 3 * B = B * (m + m + n) + B * (n + m + n)`
SUBST1_TAC THENL
[REWRITE_TAC[SYM(REWRITE_CONV [ARITH] `1 + 1 + 1`)] THEN
REWRITE_TAC[RIGHT_ADD_DISTRIB; LEFT_ADD_DISTRIB; MULT_CLAUSES] THEN
REWRITE_TAC[MULT_AC] THEN CONV_TAC NUM_CANCEL_CONV THEN REFL_TAC;
MATCH_MP_TAC LE_ADD2 THEN ASM_REWRITE_TAC[]]);;
let NADD_SUC = prove
(`!x. ?B. !n. dist(fn x (SUC n),fn x n) <= B`,
GEN_TAC THEN X_CHOOSE_TAC `B:num` (SPEC `x:nadd` NADD_ADDITIVE) THEN
EXISTS_TAC `B + fn x 1` THEN GEN_TAC THEN
MATCH_MP_TAC(LE_IMP DIST_TRIANGLE) THEN
EXISTS_TAC `fn x n + fn x 1` THEN
ASM_REWRITE_TAC[ADD1] THEN MATCH_MP_TAC LE_ADD2 THEN
ASM_REWRITE_TAC[DIST_LADD_0; LE_REFL]);;
let NADD_DIST_LEMMA = prove
(`!x. ?B. !m n. dist(fn x (m + n),fn x m) <= B * n`,
GEN_TAC THEN X_CHOOSE_TAC `B:num` (SPEC `x:nadd` NADD_SUC) THEN
EXISTS_TAC `B:num` THEN GEN_TAC THEN
INDUCT_TAC THEN REWRITE_TAC[ADD_CLAUSES; DIST_REFL; LE_0] THEN
MATCH_MP_TAC(LE_IMP DIST_TRIANGLE) THEN
EXISTS_TAC `fn x (m + n)` THEN
REWRITE_TAC[ADD1; LEFT_ADD_DISTRIB] THEN
GEN_REWRITE_TAC RAND_CONV [ADD_SYM] THEN
MATCH_MP_TAC LE_ADD2 THEN ASM_REWRITE_TAC[GSYM ADD1; MULT_CLAUSES]);;
let NADD_DIST = prove
(`!x. ?B. !m n. dist(fn x m,fn x n) <= B * dist(m,n)`,
GEN_TAC THEN X_CHOOSE_TAC `B:num` (SPEC `x:nadd` NADD_DIST_LEMMA) THEN
EXISTS_TAC `B:num` THEN REPEAT GEN_TAC THEN
DISJ_CASES_THEN MP_TAC (SPECL [`m:num`; `n:num`] LE_CASES) THEN
DISCH_THEN(CHOOSE_THEN SUBST1_TAC o ONCE_REWRITE_RULE[LE_EXISTS]) THENL
[ONCE_REWRITE_TAC[DIST_SYM]; ALL_TAC] THEN
ASM_REWRITE_TAC[DIST_LADD_0]);;
let NADD_ALTMUL = prove
(`!x y. ?A B. !n. dist(n * fn x (fn y n),fn x n * fn y n) <= A * n + B`,
REPEAT GEN_TAC THEN X_CHOOSE_TAC `B:num` (SPEC `x:nadd` NADD_CAUCHY) THEN
MP_TAC(SPEC `y:nadd` NADD_BOUND) THEN
DISCH_THEN(X_CHOOSE_THEN `M:num` (X_CHOOSE_TAC `L:num`)) THEN
MAP_EVERY EXISTS_TAC [`B * (1 + M)`; `B * L`] THEN GEN_TAC THEN
GEN_REWRITE_TAC (LAND_CONV o RAND_CONV o RAND_CONV) [MULT_SYM] THEN
MATCH_MP_TAC LE_TRANS THEN EXISTS_TAC `B * (n + fn y n)` THEN
ASM_REWRITE_TAC[] THEN REWRITE_TAC[LEFT_ADD_DISTRIB; RIGHT_ADD_DISTRIB] THEN
REWRITE_TAC[MULT_CLAUSES; GSYM ADD_ASSOC; LE_ADD_LCANCEL] THEN
ASM_REWRITE_TAC[GSYM LEFT_ADD_DISTRIB; GSYM MULT_ASSOC; LE_MULT_LCANCEL]);;
(* ------------------------------------------------------------------------- *)
(* Definition of the equivalence relation and proof that it *is* one. *)
(* ------------------------------------------------------------------------- *)
override_interface ("===",`(nadd_eq):nadd->nadd->bool`);;
let nadd_eq = new_definition
`x === y <=> ?B. !n. dist(fn x n,fn y n) <= B`;;
let NADD_EQ_REFL = prove
(`!x. x === x`,
GEN_TAC THEN REWRITE_TAC[nadd_eq; DIST_REFL; LE_0]);;
let NADD_EQ_SYM = prove
(`!x y. x === y <=> y === x`,
REPEAT GEN_TAC THEN REWRITE_TAC[nadd_eq] THEN
GEN_REWRITE_TAC (RAND_CONV o ONCE_DEPTH_CONV) [DIST_SYM] THEN REFL_TAC);;
let NADD_EQ_TRANS = prove
(`!x y z. x === y /\ y === z ==> x === z`,
REPEAT GEN_TAC THEN REWRITE_TAC[nadd_eq] THEN
DISCH_THEN(CONJUNCTS_THEN2
(X_CHOOSE_TAC `B1:num`) (X_CHOOSE_TAC `B2:num`)) THEN
EXISTS_TAC `B1 + B2` THEN X_GEN_TAC `n:num` THEN
MATCH_MP_TAC (LE_IMP DIST_TRIANGLE) THEN EXISTS_TAC `fn y n` THEN
MATCH_MP_TAC LE_ADD2 THEN ASM_REWRITE_TAC[]);;
(* ------------------------------------------------------------------------- *)
(* Injection of the natural numbers. *)
(* ------------------------------------------------------------------------- *)
override_interface ("&",`nadd_of_num:num->nadd`);;
let nadd_of_num = new_definition
`&k = afn(\n. k * n)`;;
let NADD_OF_NUM = prove
(`!k. fn(&k) = \n. k * n`,
REWRITE_TAC[nadd_of_num; GSYM nadd_rep; is_nadd] THEN
REWRITE_TAC[DIST_REFL; LE_0; MULT_AC]);;
let NADD_OF_NUM_WELLDEF = prove
(`!m n. (m = n) ==> &m === &n`,
REPEAT GEN_TAC THEN DISCH_THEN SUBST1_TAC THEN
MATCH_ACCEPT_TAC NADD_EQ_REFL);;
let NADD_OF_NUM_EQ = prove
(`!m n. (&m === &n) <=> (m = n)`,
REPEAT GEN_TAC THEN EQ_TAC THEN REWRITE_TAC[NADD_OF_NUM_WELLDEF] THEN
REWRITE_TAC[nadd_eq; NADD_OF_NUM] THEN
REWRITE_TAC[GSYM DIST_RMUL; BOUNDS_LINEAR_0; DIST_EQ_0]);;
(* ------------------------------------------------------------------------- *)
(* Definition of (reflexive) ordering and the only special property needed. *)
(* ------------------------------------------------------------------------- *)
override_interface ("<<=",`nadd_le:nadd->nadd->bool`);;
let nadd_le = new_definition
`x <<= y <=> ?B. !n. fn x n <= fn y n + B`;;
let NADD_LE_WELLDEF_LEMMA = prove
(`!x x' y y'. x === x' /\ y === y' /\ x <<= y ==> x' <<= y'`,
REPEAT GEN_TAC THEN REWRITE_TAC[nadd_eq; nadd_le] THEN
REWRITE_TAC[DIST_LE_CASES; FORALL_AND_THM] THEN
DISCH_THEN(CONJUNCTS_THEN2 (X_CHOOSE_TAC `B1:num`) MP_TAC) THEN
DISCH_THEN(CONJUNCTS_THEN2 (X_CHOOSE_TAC `B2:num`) MP_TAC) THEN
DISCH_THEN(X_CHOOSE_TAC `B:num`) THEN
EXISTS_TAC `(B2 + B) + B1` THEN X_GEN_TAC `n:num` THEN
FIRST_ASSUM(MATCH_MP_TAC o LE_IMP o CONJUNCT2) THEN
REWRITE_TAC[ADD_ASSOC; LE_ADD_RCANCEL] THEN
FIRST_ASSUM(MATCH_MP_TAC o LE_IMP) THEN ASM_REWRITE_TAC[LE_ADD_RCANCEL]);;
let NADD_LE_WELLDEF = prove
(`!x x' y y'. x === x' /\ y === y' ==> (x <<= y <=> x' <<= y')`,
REPEAT STRIP_TAC THEN EQ_TAC THEN DISCH_TAC THEN
MATCH_MP_TAC NADD_LE_WELLDEF_LEMMA THEN ASM_REWRITE_TAC[] THENL
[MAP_EVERY EXISTS_TAC [`x:nadd`; `y:nadd`];
MAP_EVERY EXISTS_TAC [`x':nadd`; `y':nadd`] THEN
ONCE_REWRITE_TAC[NADD_EQ_SYM]] THEN
ASM_REWRITE_TAC[]);;
let NADD_LE_REFL = prove
(`!x. x <<= x`,
REWRITE_TAC[nadd_le; LE_ADD]);;
let NADD_LE_TRANS = prove
(`!x y z. x <<= y /\ y <<= z ==> x <<= z`,
REPEAT GEN_TAC THEN REWRITE_TAC[nadd_le] THEN
DISCH_THEN(CONJUNCTS_THEN2 (X_CHOOSE_TAC `B1:num`) MP_TAC) THEN
DISCH_THEN(X_CHOOSE_TAC `B2:num`) THEN
EXISTS_TAC `B2 + B1` THEN GEN_TAC THEN
FIRST_ASSUM(MATCH_MP_TAC o LE_IMP) THEN
ASM_REWRITE_TAC[ADD_ASSOC; LE_ADD_RCANCEL]);;
let NADD_LE_ANTISYM = prove
(`!x y. x <<= y /\ y <<= x <=> (x === y)`,
REPEAT GEN_TAC THEN REWRITE_TAC[nadd_le; nadd_eq; DIST_LE_CASES] THEN
EQ_TAC THENL
[DISCH_THEN(CONJUNCTS_THEN2 (X_CHOOSE_TAC `B1:num`)
(X_CHOOSE_TAC `B2:num`)) THEN
EXISTS_TAC `B1 + B2` THEN GEN_TAC THEN CONJ_TAC THEN
FIRST_ASSUM(MATCH_MP_TAC o LE_IMP) THEN
ASM_REWRITE_TAC[ADD_ASSOC; LE_ADD_RCANCEL; LE_ADD; LE_ADDR];
DISCH_THEN(X_CHOOSE_TAC `B:num`) THEN
CONJ_TAC THEN EXISTS_TAC `B:num` THEN ASM_REWRITE_TAC[]]);;
let NADD_LE_TOTAL_LEMMA = prove
(`!x y. ~(x <<= y) ==> !B. ?n. ~(n = 0) /\ fn y n + B < fn x n`,
REPEAT GEN_TAC THEN REWRITE_TAC[nadd_le; NOT_FORALL_THM; NOT_EXISTS_THM] THEN
REWRITE_TAC[NOT_LE] THEN DISCH_TAC THEN GEN_TAC THEN
POP_ASSUM(X_CHOOSE_TAC `n:num` o SPEC `B + fn x 0`) THEN
EXISTS_TAC `n:num` THEN POP_ASSUM MP_TAC THEN
ASM_CASES_TAC `n = 0` THEN ASM_REWRITE_TAC[NOT_LT; ADD_ASSOC; LE_ADDR] THEN
CONV_TAC CONTRAPOS_CONV THEN REWRITE_TAC[NOT_LT] THEN
DISCH_THEN(MATCH_MP_TAC o LE_IMP) THEN REWRITE_TAC[ADD_ASSOC; LE_ADD]);;
let NADD_LE_TOTAL = prove
(`!x y. x <<= y \/ y <<= x`,
REPEAT GEN_TAC THEN GEN_REWRITE_TAC I [TAUT `a <=> ~ ~ a`] THEN
X_CHOOSE_TAC `B1:num` (SPEC `x:nadd` NADD_CAUCHY) THEN
X_CHOOSE_TAC `B2:num` (SPEC `y:nadd` NADD_CAUCHY) THEN
PURE_ONCE_REWRITE_TAC[DE_MORGAN_THM] THEN
DISCH_THEN(MP_TAC o end_itlist CONJ o
map (MATCH_MP NADD_LE_TOTAL_LEMMA) o CONJUNCTS) THEN
REWRITE_TAC[AND_FORALL_THM] THEN DISCH_THEN(MP_TAC o SPEC `B1 + B2`) THEN
REWRITE_TAC[RIGHT_AND_EXISTS_THM] THEN REWRITE_TAC[LEFT_AND_EXISTS_THM] THEN
DISCH_THEN(X_CHOOSE_THEN `m:num` (X_CHOOSE_THEN `n:num` MP_TAC)) THEN
DISCH_THEN(MP_TAC o MATCH_MP
(ITAUT `(~a /\ b) /\ (~c /\ d) ==> ~(c \/ ~b) /\ ~(a \/ ~d)`)) THEN
REWRITE_TAC[NOT_LT; GSYM LE_MULT_LCANCEL] THEN REWRITE_TAC[NOT_LE] THEN
DISCH_THEN(MP_TAC o MATCH_MP LT_ADD2) THEN REWRITE_TAC[NOT_LT] THEN
REWRITE_TAC[LEFT_ADD_DISTRIB] THEN
ONCE_REWRITE_TAC[AC ADD_AC
`(a + b + c) + (d + e + f) = (d + b + e) + (a + c + f)`] THEN
MATCH_MP_TAC LE_ADD2 THEN REWRITE_TAC[GSYM RIGHT_ADD_DISTRIB] THEN
CONJ_TAC THEN GEN_REWRITE_TAC (RAND_CONV o RAND_CONV) [MULT_SYM] THEN
RULE_ASSUM_TAC(REWRITE_RULE[DIST_LE_CASES]) THEN ASM_REWRITE_TAC[]);;
let NADD_ARCH = prove
(`!x. ?n. x <<= &n`,
REWRITE_TAC[nadd_le; NADD_OF_NUM; NADD_BOUND]);;
let NADD_OF_NUM_LE = prove
(`!m n. (&m <<= &n) <=> m <= n`,
REPEAT GEN_TAC THEN REWRITE_TAC[nadd_le; NADD_OF_NUM] THEN
REWRITE_TAC[BOUNDS_LINEAR]);;
(* ------------------------------------------------------------------------- *)
(* Addition. *)
(* ------------------------------------------------------------------------- *)
override_interface ("++",`nadd_add:nadd->nadd->nadd`);;
let nadd_add = new_definition
`x ++ y = afn(\n. fn x n + fn y n)`;;
let NADD_ADD = prove
(`!x y. fn(x ++ y) = \n. fn x n + fn y n`,
REPEAT GEN_TAC THEN
REWRITE_TAC[nadd_add; GSYM nadd_rep; is_nadd] THEN
X_CHOOSE_TAC `B1:num` (SPEC `x:nadd` NADD_CAUCHY) THEN
X_CHOOSE_TAC `B2:num` (SPEC `y:nadd` NADD_CAUCHY) THEN
EXISTS_TAC `B1 + B2` THEN MAP_EVERY X_GEN_TAC [`m:num`; `n:num`] THEN
GEN_REWRITE_TAC (LAND_CONV o ONCE_DEPTH_CONV) [LEFT_ADD_DISTRIB] THEN
MATCH_MP_TAC (LE_IMP DIST_ADD2) THEN REWRITE_TAC[RIGHT_ADD_DISTRIB] THEN
MATCH_MP_TAC LE_ADD2 THEN ASM_REWRITE_TAC[]);;
let NADD_ADD_WELLDEF = prove
(`!x x' y y'. x === x' /\ y === y' ==> (x ++ y === x' ++ y')`,
REPEAT GEN_TAC THEN REWRITE_TAC[nadd_eq; NADD_ADD] THEN
DISCH_THEN(CONJUNCTS_THEN2
(X_CHOOSE_TAC `B1:num`) (X_CHOOSE_TAC `B2:num`)) THEN
EXISTS_TAC `B1 + B2` THEN X_GEN_TAC `n:num` THEN
MATCH_MP_TAC (LE_IMP DIST_ADD2) THEN
MATCH_MP_TAC LE_ADD2 THEN ASM_REWRITE_TAC[]);;
(* ------------------------------------------------------------------------- *)
(* Basic properties of addition. *)
(* ------------------------------------------------------------------------- *)
let NADD_ADD_SYM = prove
(`!x y. (x ++ y) === (y ++ x)`,
REPEAT GEN_TAC THEN REWRITE_TAC[nadd_add] THEN
GEN_REWRITE_TAC (RAND_CONV o ONCE_DEPTH_CONV) [ADD_SYM] THEN
REWRITE_TAC[NADD_EQ_REFL]);;
let NADD_ADD_ASSOC = prove
(`!x y z. (x ++ (y ++ z)) === ((x ++ y) ++ z)`,
REPEAT GEN_TAC THEN ONCE_REWRITE_TAC[nadd_add] THEN
REWRITE_TAC[NADD_ADD; ADD_ASSOC; NADD_EQ_REFL]);;
let NADD_ADD_LID = prove
(`!x. (&0 ++ x) === x`,
GEN_TAC THEN REWRITE_TAC[nadd_eq; NADD_ADD; NADD_OF_NUM] THEN
REWRITE_TAC[MULT_CLAUSES; ADD_CLAUSES; DIST_REFL; LE_0]);;
let NADD_ADD_LCANCEL = prove
(`!x y z. (x ++ y) === (x ++ z) ==> y === z`,
REPEAT GEN_TAC THEN REWRITE_TAC[nadd_eq; NADD_ADD; DIST_LADD]);;
let NADD_LE_ADD = prove
(`!x y. x <<= (x ++ y)`,
REPEAT GEN_TAC THEN REWRITE_TAC[nadd_le; NADD_ADD] THEN
EXISTS_TAC `0` THEN REWRITE_TAC[ADD_CLAUSES; LE_ADD]);;
let NADD_LE_EXISTS = prove
(`!x y. x <<= y ==> ?d. y === x ++ d`,
REPEAT GEN_TAC THEN REWRITE_TAC[nadd_le] THEN
DISCH_THEN(X_CHOOSE_THEN `B:num` MP_TAC) THEN
REWRITE_TAC[LE_EXISTS; SKOLEM_THM] THEN
DISCH_THEN(X_CHOOSE_THEN `d:num->num` (ASSUME_TAC o GSYM)) THEN
EXISTS_TAC `afn d` THEN REWRITE_TAC[nadd_eq; NADD_ADD] THEN
EXISTS_TAC `B:num` THEN X_GEN_TAC `n:num` THEN
SUBGOAL_THEN `fn(afn d) = d` SUBST1_TAC THENL
[REWRITE_TAC[GSYM nadd_rep; is_nadd] THEN
X_CHOOSE_TAC `B1:num` (SPEC `x:nadd` NADD_CAUCHY) THEN
X_CHOOSE_TAC `B2:num` (SPEC `y:nadd` NADD_CAUCHY) THEN
EXISTS_TAC `B1 + (B2 + B)` THEN REPEAT GEN_TAC THEN
MATCH_MP_TAC(LE_IMP DIST_ADD2_REV) THEN
MAP_EVERY EXISTS_TAC [`m * fn x n`; `n * fn x m`] THEN
ONCE_REWRITE_TAC[RIGHT_ADD_DISTRIB] THEN
GEN_REWRITE_TAC RAND_CONV [ADD_SYM] THEN
MATCH_MP_TAC LE_ADD2 THEN ASM_REWRITE_TAC[] THEN
ONCE_REWRITE_TAC[ADD_SYM] THEN
ASM_REWRITE_TAC[GSYM LEFT_ADD_DISTRIB] THEN
GEN_REWRITE_TAC (LAND_CONV o ONCE_DEPTH_CONV) [LEFT_ADD_DISTRIB] THEN
MATCH_MP_TAC(LE_IMP DIST_ADD2) THEN REWRITE_TAC[RIGHT_ADD_DISTRIB] THEN
GEN_REWRITE_TAC RAND_CONV [ADD_SYM] THEN MATCH_MP_TAC LE_ADD2 THEN
ONCE_REWRITE_TAC[ADD_SYM] THEN ASM_REWRITE_TAC[] THEN
GEN_REWRITE_TAC (LAND_CONV o ONCE_DEPTH_CONV) [MULT_SYM] THEN
REWRITE_TAC[GSYM DIST_LMUL; DIST_ADDBOUND; LE_MULT_LCANCEL];
ASM_REWRITE_TAC[DIST_RADD_0; LE_REFL]]);;
let NADD_OF_NUM_ADD = prove
(`!m n. &m ++ &n === &(m + n)`,
REWRITE_TAC[nadd_eq; NADD_OF_NUM; NADD_ADD] THEN
REWRITE_TAC[RIGHT_ADD_DISTRIB; DIST_REFL; LE_0]);;
(* ------------------------------------------------------------------------- *)
(* Multiplication. *)
(* ------------------------------------------------------------------------- *)
override_interface ("**",`nadd_mul:nadd->nadd->nadd`);;
let nadd_mul = new_definition
`x ** y = afn(\n. fn x (fn y n))`;;
let NADD_MUL = prove
(`!x y. fn(x ** y) = \n. fn x (fn y n)`,
REPEAT GEN_TAC THEN
REWRITE_TAC[nadd_mul; GSYM nadd_rep; is_nadd] THEN
X_CHOOSE_TAC `B:num` (SPEC `y:nadd` NADD_CAUCHY) THEN
X_CHOOSE_TAC `C:num` (SPEC `x:nadd` NADD_DIST) THEN
X_CHOOSE_TAC `D:num` (SPEC `x:nadd` NADD_MULTIPLICATIVE) THEN
MATCH_MP_TAC BOUNDS_NOTZERO THEN
REWRITE_TAC[MULT_CLAUSES; DIST_REFL] THEN
MAP_EVERY EXISTS_TAC [`D + C * B`; `D + D`] THEN
REPEAT GEN_TAC THEN MATCH_MP_TAC LE_TRANS THEN
EXISTS_TAC `(D * m + D) + (D * n + D) + C * B * (m + n)` THEN CONJ_TAC THENL
[MATCH_MP_TAC (LE_IMP DIST_TRIANGLE) THEN
EXISTS_TAC `fn x (m * fn y n)` THEN
MATCH_MP_TAC LE_ADD2 THEN
ONCE_REWRITE_TAC[DIST_SYM] THEN ASM_REWRITE_TAC[] THEN
MATCH_MP_TAC (LE_IMP DIST_TRIANGLE) THEN
EXISTS_TAC `fn x (n * fn y m)` THEN
MATCH_MP_TAC LE_ADD2 THEN
ONCE_REWRITE_TAC[DIST_SYM] THEN ASM_REWRITE_TAC[] THEN
MATCH_MP_TAC LE_TRANS THEN
EXISTS_TAC `C * dist(m * fn y n,n * fn y m)` THEN
ASM_REWRITE_TAC[LE_MULT_LCANCEL];
MATCH_MP_TAC EQ_IMP_LE THEN
REWRITE_TAC[LEFT_ADD_DISTRIB; RIGHT_ADD_DISTRIB; MULT_ASSOC; ADD_AC]]);;
(* ------------------------------------------------------------------------- *)
(* Properties of multiplication. *)
(* ------------------------------------------------------------------------- *)
let NADD_MUL_SYM = prove
(`!x y. (x ** y) === (y ** x)`,
REPEAT GEN_TAC THEN REWRITE_TAC[nadd_eq; NADD_MUL] THEN
X_CHOOSE_THEN `A1:num` MP_TAC (SPECL [`x:nadd`; `y:nadd`] NADD_ALTMUL) THEN
DISCH_THEN(X_CHOOSE_TAC `B1:num`) THEN
X_CHOOSE_THEN `A2:num` MP_TAC (SPECL [`y:nadd`; `x:nadd`] NADD_ALTMUL) THEN
DISCH_THEN(X_CHOOSE_TAC `B2:num`) THEN REWRITE_TAC[BOUNDS_DIVIDED] THEN
REWRITE_TAC[DIST_LMUL] THEN MAP_EVERY EXISTS_TAC [`A1 + A2`; `B1 + B2`] THEN
GEN_TAC THEN REWRITE_TAC[RIGHT_ADD_DISTRIB] THEN
ONCE_REWRITE_TAC[AC ADD_AC `(a + b) + (c + d) = (a + c) + (b + d)`] THEN
MATCH_MP_TAC (LE_IMP DIST_TRIANGLE) THEN
EXISTS_TAC `fn x n * fn y n` THEN
MATCH_MP_TAC LE_ADD2 THEN ASM_REWRITE_TAC[] THEN
ONCE_REWRITE_TAC [DIST_SYM] THEN
GEN_REWRITE_TAC (LAND_CONV o funpow 2 RAND_CONV) [MULT_SYM] THEN
ASM_REWRITE_TAC[]);;
let NADD_MUL_ASSOC = prove
(`!x y z. (x ** (y ** z)) === ((x ** y) ** z)`,
REPEAT GEN_TAC THEN ONCE_REWRITE_TAC[nadd_mul] THEN
REWRITE_TAC[NADD_MUL; NADD_EQ_REFL]);;
let NADD_MUL_LID = prove
(`!x. (&1 ** x) === x`,
REWRITE_TAC[NADD_OF_NUM; nadd_mul; MULT_CLAUSES] THEN
REWRITE_TAC[nadd_abs; NADD_EQ_REFL; ETA_AX]);;
let NADD_LDISTRIB = prove
(`!x y z. x ** (y ++ z) === (x ** y) ++ (x ** z)`,
REPEAT GEN_TAC THEN REWRITE_TAC[nadd_eq] THEN
REWRITE_TAC[NADD_ADD; NADD_MUL] THEN
X_CHOOSE_TAC `B:num` (SPEC `x:nadd` NADD_ADDITIVE) THEN
EXISTS_TAC `B:num` THEN ASM_REWRITE_TAC[]);;
let NADD_MUL_WELLDEF_LEMMA = prove
(`!x y y'. y === y' ==> (x ** y) === (x ** y')`,
REPEAT GEN_TAC THEN REWRITE_TAC[nadd_eq; NADD_MUL] THEN
DISCH_THEN(X_CHOOSE_TAC `B1:num`) THEN
X_CHOOSE_TAC `B2:num` (SPEC `x:nadd` NADD_DIST) THEN
EXISTS_TAC `B2 * B1` THEN X_GEN_TAC `n:num` THEN
MATCH_MP_TAC LE_TRANS THEN
EXISTS_TAC `B2 * dist(fn y n,fn y' n)` THEN
ASM_REWRITE_TAC[LE_MULT_LCANCEL]);;
let NADD_MUL_WELLDEF = prove
(`!x x' y y'. x === x' /\ y === y'
==> (x ** y) === (x' ** y')`,
REPEAT GEN_TAC THEN STRIP_TAC THEN MATCH_MP_TAC NADD_EQ_TRANS THEN
EXISTS_TAC `x' ** y` THEN CONJ_TAC THENL
[MATCH_MP_TAC NADD_EQ_TRANS THEN EXISTS_TAC `y ** x'` THEN
REWRITE_TAC[NADD_MUL_SYM] THEN MATCH_MP_TAC NADD_EQ_TRANS THEN
EXISTS_TAC `y ** x` THEN REWRITE_TAC[NADD_MUL_SYM]; ALL_TAC] THEN
MATCH_MP_TAC NADD_MUL_WELLDEF_LEMMA THEN ASM_REWRITE_TAC[]);;
let NADD_OF_NUM_MUL = prove
(`!m n. &m ** &n === &(m * n)`,
REWRITE_TAC[nadd_eq; NADD_OF_NUM; NADD_MUL] THEN
REWRITE_TAC[MULT_ASSOC; DIST_REFL; LE_0]);;
(* ------------------------------------------------------------------------- *)
(* A few handy lemmas. *)
(* ------------------------------------------------------------------------- *)
let NADD_LE_0 = prove
(`!x. &0 <<= x`,
GEN_TAC THEN
REWRITE_TAC[nadd_le; NADD_OF_NUM; MULT_CLAUSES; LE_0]);;
let NADD_EQ_IMP_LE = prove
(`!x y. x === y ==> x <<= y`,
REPEAT GEN_TAC THEN
REWRITE_TAC[nadd_eq; nadd_le; DIST_LE_CASES] THEN
DISCH_THEN(X_CHOOSE_TAC `B:num`) THEN EXISTS_TAC `B:num` THEN
ASM_REWRITE_TAC[]);;
let NADD_LE_LMUL = prove
(`!x y z. y <<= z ==> (x ** y) <<= (x ** z)`,
REPEAT GEN_TAC THEN
DISCH_THEN(X_CHOOSE_TAC `d:nadd` o MATCH_MP NADD_LE_EXISTS) THEN
MATCH_MP_TAC NADD_LE_TRANS THEN
EXISTS_TAC `x ** y ++ x ** d` THEN REWRITE_TAC[NADD_LE_ADD] THEN
MATCH_MP_TAC NADD_EQ_IMP_LE THEN
MATCH_MP_TAC NADD_EQ_TRANS THEN
EXISTS_TAC `x ** (y ++ d)` THEN
ONCE_REWRITE_TAC[NADD_EQ_SYM] THEN
REWRITE_TAC[NADD_LDISTRIB] THEN
MATCH_MP_TAC NADD_MUL_WELLDEF THEN
ASM_REWRITE_TAC[NADD_EQ_REFL]);;
let NADD_LE_RMUL = prove
(`!x y z. x <<= y ==> (x ** z) <<= (y ** z)`,
MESON_TAC[NADD_LE_LMUL; NADD_LE_WELLDEF; NADD_MUL_SYM]);;
let NADD_LE_RADD = prove
(`!x y z. x ++ z <<= y ++ z <=> x <<= y`,
REPEAT GEN_TAC THEN REWRITE_TAC[nadd_le; NADD_ADD] THEN
GEN_REWRITE_TAC (LAND_CONV o funpow 2 BINDER_CONV o RAND_CONV)
[ADD_SYM] THEN
REWRITE_TAC[ADD_ASSOC; LE_ADD_RCANCEL] THEN
GEN_REWRITE_TAC (LAND_CONV o funpow 2 BINDER_CONV o RAND_CONV)
[ADD_SYM] THEN REFL_TAC);;
let NADD_LE_LADD = prove
(`!x y z. x ++ y <<= x ++ z <=> y <<= z`,
MESON_TAC[NADD_LE_RADD; NADD_ADD_SYM; NADD_LE_WELLDEF]);;
let NADD_RDISTRIB = prove
(`!x y z. (x ++ y) ** z === x ** z ++ y ** z`,
MESON_TAC[NADD_LDISTRIB; NADD_MUL_SYM; NADD_ADD_WELLDEF;
NADD_EQ_TRANS; NADD_EQ_REFL; NADD_EQ_SYM]);;
(* ------------------------------------------------------------------------- *)
(* The Archimedean property in a more useful form. *)
(* ------------------------------------------------------------------------- *)
let NADD_ARCH_MULT = prove
(`!x k. ~(x === &0) ==> ?N. &k <<= &N ** x`,
REPEAT GEN_TAC THEN REWRITE_TAC[nadd_eq; nadd_le; NOT_EXISTS_THM] THEN
X_CHOOSE_TAC `B:num` (SPEC `x:nadd` NADD_CAUCHY) THEN
DISCH_THEN(MP_TAC o SPEC `B + k`) THEN
REWRITE_TAC[NOT_FORALL_THM; NADD_OF_NUM] THEN
REWRITE_TAC[MULT_CLAUSES; DIST_RZERO; NOT_LE] THEN
DISCH_THEN(X_CHOOSE_TAC `N:num`) THEN
MAP_EVERY EXISTS_TAC [`N:num`; `B * N`] THEN X_GEN_TAC `i:num` THEN
REWRITE_TAC[NADD_MUL; NADD_OF_NUM] THEN
MATCH_MP_TAC(GEN_ALL(fst(EQ_IMP_RULE(SPEC_ALL LE_ADD_RCANCEL)))) THEN
EXISTS_TAC `B * i` THEN
REWRITE_TAC[GSYM ADD_ASSOC; GSYM LEFT_ADD_DISTRIB] THEN
MATCH_MP_TAC LE_TRANS THEN EXISTS_TAC `i * fn x N` THEN
RULE_ASSUM_TAC(REWRITE_RULE[DIST_LE_CASES]) THEN ASM_REWRITE_TAC[] THEN
REWRITE_TAC[GSYM RIGHT_ADD_DISTRIB] THEN
GEN_REWRITE_TAC RAND_CONV [MULT_SYM] THEN
REWRITE_TAC[LE_MULT_RCANCEL] THEN DISJ1_TAC THEN
MATCH_MP_TAC LT_IMP_LE THEN ONCE_REWRITE_TAC[ADD_SYM] THEN
FIRST_ASSUM ACCEPT_TAC);;
let NADD_ARCH_ZERO = prove
(`!x k. (!n. &n ** x <<= k) ==> (x === &0)`,
REPEAT GEN_TAC THEN CONV_TAC CONTRAPOS_CONV THEN DISCH_TAC THEN
REWRITE_TAC[NOT_FORALL_THM] THEN
X_CHOOSE_TAC `p:num` (SPEC `k:nadd` NADD_ARCH) THEN
FIRST_ASSUM(MP_TAC o MATCH_MP NADD_ARCH_MULT) THEN
DISCH_THEN(X_CHOOSE_TAC `N:num` o SPEC `p:num`) THEN
EXISTS_TAC `N + 1` THEN DISCH_TAC THEN UNDISCH_TAC `~(x === &0)` THEN
REWRITE_TAC[GSYM NADD_LE_ANTISYM; NADD_LE_0] THEN
MATCH_MP_TAC(GEN_ALL(fst(EQ_IMP_RULE(SPEC_ALL NADD_LE_RADD)))) THEN
EXISTS_TAC `&N ** x` THEN MATCH_MP_TAC NADD_LE_TRANS THEN
EXISTS_TAC `k:nadd` THEN CONJ_TAC THENL
[SUBGOAL_THEN `&(N + 1) ** x === x ++ &N ** x` MP_TAC THENL
[ONCE_REWRITE_TAC[ADD_SYM] THEN
MATCH_MP_TAC NADD_EQ_TRANS THEN
EXISTS_TAC `&1 ** x ++ &N ** x` THEN CONJ_TAC THENL
[MATCH_MP_TAC NADD_EQ_TRANS THEN
EXISTS_TAC `(&1 ++ &N) ** x` THEN CONJ_TAC THENL
[MESON_TAC[NADD_OF_NUM_ADD; NADD_MUL_WELLDEF; NADD_EQ_REFL;
NADD_EQ_SYM];
MESON_TAC[NADD_RDISTRIB; NADD_MUL_SYM; NADD_EQ_SYM; NADD_EQ_TRANS]];
MESON_TAC[NADD_ADD_WELLDEF; NADD_EQ_REFL; NADD_MUL_LID]];
ASM_MESON_TAC[NADD_LE_WELLDEF; NADD_EQ_REFL]];
ASM_MESON_TAC[NADD_LE_TRANS; NADD_LE_WELLDEF; NADD_EQ_REFL;
NADD_ADD_LID]]);;
let NADD_ARCH_LEMMA = prove
(`!x y z. (!n. &n ** x <<= &n ** y ++ z) ==> x <<= y`,
REPEAT STRIP_TAC THEN
DISJ_CASES_TAC(SPECL [`x:nadd`; `y:nadd`] NADD_LE_TOTAL) THEN
ASM_REWRITE_TAC[] THEN
FIRST_ASSUM(X_CHOOSE_TAC `d:nadd` o MATCH_MP NADD_LE_EXISTS) THEN
MATCH_MP_TAC NADD_EQ_IMP_LE THEN
MATCH_MP_TAC NADD_EQ_TRANS THEN EXISTS_TAC `y ++ d` THEN
ASM_REWRITE_TAC[] THEN
MATCH_MP_TAC NADD_EQ_TRANS THEN EXISTS_TAC `y ++ &0` THEN CONJ_TAC THENL
[MATCH_MP_TAC NADD_ADD_WELLDEF THEN REWRITE_TAC[NADD_EQ_REFL] THEN
MATCH_MP_TAC NADD_ARCH_ZERO THEN EXISTS_TAC `z:nadd` THEN
ASM_MESON_TAC[NADD_MUL_WELLDEF; NADD_LE_WELLDEF; NADD_LDISTRIB;
NADD_LE_LADD; NADD_EQ_REFL];
ASM_MESON_TAC[NADD_ADD_LID; NADD_ADD_WELLDEF; NADD_EQ_TRANS;
NADD_ADD_SYM]]);;
(* ------------------------------------------------------------------------- *)
(* Completeness. *)
(* ------------------------------------------------------------------------- *)
let NADD_COMPLETE = prove
(`!P. (?x. P x) /\ (?M. !x. P x ==> x <<= M) ==>
?M. (!x. P x ==> x <<= M) /\
!M'. (!x. P x ==> x <<= M') ==> M <<= M'`,
GEN_TAC THEN DISCH_THEN
(CONJUNCTS_THEN2 (X_CHOOSE_TAC `a:nadd`) (X_CHOOSE_TAC `m:nadd`)) THEN
SUBGOAL_THEN
`!n. ?r. (?x. P x /\ &r <<= &n ** x) /\
!r'. (?x. P x /\ &r' <<= &n ** x) ==> r' <= r` MP_TAC THENL
[GEN_TAC THEN REWRITE_TAC[GSYM num_MAX] THEN CONJ_TAC THENL
[MAP_EVERY EXISTS_TAC [`0`; `a:nadd`] THEN ASM_REWRITE_TAC[NADD_LE_0];
X_CHOOSE_TAC `N:num` (SPEC `m:nadd` NADD_ARCH) THEN
EXISTS_TAC `n * N` THEN X_GEN_TAC `p:num` THEN
DISCH_THEN(X_CHOOSE_THEN `w:nadd` STRIP_ASSUME_TAC) THEN
ONCE_REWRITE_TAC[GSYM NADD_OF_NUM_LE] THEN
MATCH_MP_TAC NADD_LE_TRANS THEN EXISTS_TAC `&n ** w` THEN
ASM_REWRITE_TAC[] THEN MATCH_MP_TAC NADD_LE_TRANS THEN
EXISTS_TAC `&n ** &N` THEN CONJ_TAC THENL
[MATCH_MP_TAC NADD_LE_LMUL THEN MATCH_MP_TAC NADD_LE_TRANS THEN
EXISTS_TAC `m:nadd` THEN ASM_REWRITE_TAC[] THEN
FIRST_ASSUM MATCH_MP_TAC THEN ASM_REWRITE_TAC[];
MATCH_MP_TAC NADD_EQ_IMP_LE THEN
MATCH_ACCEPT_TAC NADD_OF_NUM_MUL]];
ONCE_REWRITE_TAC[SKOLEM_THM] THEN
DISCH_THEN(X_CHOOSE_THEN `r:num->num`
(fun th -> let th1,th2 = CONJ_PAIR(SPEC `n:num` th) in
MAP_EVERY (MP_TAC o GEN `n:num`) [th1; th2])) THEN
DISCH_THEN(MP_TAC o GEN `n:num` o SPECL [`n:num`; `SUC(r(n:num))`]) THEN
REWRITE_TAC[LE_SUC_LT; LT_REFL; NOT_EXISTS_THM] THEN
DISCH_THEN(ASSUME_TAC o GENL [`n:num`; `x:nadd`] o MATCH_MP
(ITAUT `(a \/ b) /\ ~(c /\ b) ==> c ==> a`) o CONJ
(SPECL [`&n ** x`; `&(SUC(r(n:num)))`] NADD_LE_TOTAL) o SPEC_ALL) THEN
DISCH_TAC] THEN
SUBGOAL_THEN `!n i. i * r(n) <= n * r(i) + n` ASSUME_TAC THENL
[REPEAT GEN_TAC THEN
FIRST_ASSUM(X_CHOOSE_THEN `x:nadd` STRIP_ASSUME_TAC o SPEC `n:num`) THEN
ONCE_REWRITE_TAC[GSYM NADD_OF_NUM_LE] THEN
MATCH_MP_TAC NADD_LE_TRANS THEN
EXISTS_TAC `&i ** &n ** x` THEN CONJ_TAC THENL
[MATCH_MP_TAC NADD_LE_TRANS THEN
EXISTS_TAC `&i ** &(r(n:num))` THEN CONJ_TAC THENL
[MATCH_MP_TAC NADD_EQ_IMP_LE THEN
ONCE_REWRITE_TAC[NADD_EQ_SYM] THEN MATCH_ACCEPT_TAC NADD_OF_NUM_MUL;
MATCH_MP_TAC NADD_LE_LMUL THEN ASM_REWRITE_TAC[]];
MATCH_MP_TAC NADD_LE_TRANS THEN
EXISTS_TAC `&n ** &(SUC(r(i:num)))` THEN CONJ_TAC THENL
[MATCH_MP_TAC NADD_LE_TRANS THEN EXISTS_TAC `&n ** &i ** x` THEN
CONJ_TAC THENL
[MATCH_MP_TAC NADD_EQ_IMP_LE THEN
MATCH_MP_TAC NADD_EQ_TRANS THEN
EXISTS_TAC `(&i ** &n) ** x` THEN
REWRITE_TAC[NADD_MUL_ASSOC] THEN
MATCH_MP_TAC NADD_EQ_TRANS THEN
EXISTS_TAC `(&n ** &i) ** x` THEN
REWRITE_TAC[ONCE_REWRITE_RULE[NADD_EQ_SYM] NADD_MUL_ASSOC] THEN
MATCH_MP_TAC NADD_MUL_WELLDEF THEN
REWRITE_TAC[NADD_MUL_SYM; NADD_EQ_REFL];
MATCH_MP_TAC NADD_LE_LMUL THEN
FIRST_ASSUM MATCH_MP_TAC THEN ASM_REWRITE_TAC[]];
ONCE_REWRITE_TAC[ADD_SYM] THEN REWRITE_TAC[GSYM MULT_SUC] THEN
MATCH_MP_TAC NADD_EQ_IMP_LE THEN
REWRITE_TAC[NADD_OF_NUM_MUL]]]; ALL_TAC] THEN
EXISTS_TAC `afn r` THEN SUBGOAL_THEN `fn(afn r) = r` ASSUME_TAC THENL
[REWRITE_TAC[GSYM nadd_rep] THEN REWRITE_TAC[is_nadd; DIST_LE_CASES] THEN
EXISTS_TAC `1` THEN REWRITE_TAC[MULT_CLAUSES] THEN
REWRITE_TAC[FORALL_AND_THM] THEN
GEN_REWRITE_TAC RAND_CONV [SWAP_FORALL_THM] THEN
GEN_REWRITE_TAC (LAND_CONV o funpow 2 BINDER_CONV o
funpow 2 RAND_CONV) [ADD_SYM] THEN
REWRITE_TAC[] THEN MAP_EVERY X_GEN_TAC [`i:num`; `n:num`] THEN
MATCH_MP_TAC LE_TRANS THEN EXISTS_TAC `n * r(i:num) + n` THEN
ASM_REWRITE_TAC[ADD_ASSOC; LE_ADD]; ALL_TAC] THEN
CONJ_TAC THENL
[X_GEN_TAC `x:nadd` THEN DISCH_TAC THEN
MATCH_MP_TAC NADD_ARCH_LEMMA THEN
EXISTS_TAC `&2` THEN X_GEN_TAC `n:num` THEN
MATCH_MP_TAC NADD_LE_TRANS THEN
EXISTS_TAC `&(SUC(r(n:num)))` THEN CONJ_TAC THENL
[FIRST_ASSUM MATCH_MP_TAC THEN ASM_REWRITE_TAC[];
ASM_REWRITE_TAC[nadd_le; NADD_ADD; NADD_MUL; NADD_OF_NUM] THEN
ONCE_REWRITE_TAC[ADD_SYM] THEN
REWRITE_TAC[ADD1; RIGHT_ADD_DISTRIB] THEN
REWRITE_TAC[MULT_2; MULT_CLAUSES; ADD_ASSOC; LE_ADD_RCANCEL] THEN
REWRITE_TAC[GSYM ADD_ASSOC] THEN ONCE_REWRITE_TAC[ADD_SYM] THEN
ONCE_REWRITE_TAC[BOUNDS_IGNORE] THEN
MAP_EVERY EXISTS_TAC [`0`; `n:num`] THEN
X_GEN_TAC `i:num` THEN DISCH_TAC THEN
GEN_REWRITE_TAC LAND_CONV [MULT_SYM] THEN
MATCH_MP_TAC LE_TRANS THEN EXISTS_TAC `n * r(i:num) + n` THEN
ASM_REWRITE_TAC[LE_ADD_LCANCEL; ADD_CLAUSES]];
X_GEN_TAC `z:nadd` THEN DISCH_TAC THEN
MATCH_MP_TAC NADD_ARCH_LEMMA THEN EXISTS_TAC `&1` THEN
X_GEN_TAC `n:num` THEN MATCH_MP_TAC NADD_LE_TRANS THEN
EXISTS_TAC `&(r(n:num)) ++ &1` THEN CONJ_TAC THENL
[ASM_REWRITE_TAC[nadd_le; NADD_ADD; NADD_MUL; NADD_OF_NUM] THEN
EXISTS_TAC `0` THEN REWRITE_TAC[ADD_CLAUSES; MULT_CLAUSES] THEN
GEN_TAC THEN GEN_REWRITE_TAC (RAND_CONV o LAND_CONV) [MULT_SYM] THEN
ASM_REWRITE_TAC[];
REWRITE_TAC[NADD_LE_RADD] THEN
FIRST_ASSUM(X_CHOOSE_THEN `x:nadd` MP_TAC o SPEC `n:num`) THEN
DISCH_THEN STRIP_ASSUME_TAC THEN
MATCH_MP_TAC NADD_LE_TRANS THEN EXISTS_TAC `&n ** x` THEN
ASM_REWRITE_TAC[] THEN MATCH_MP_TAC NADD_LE_LMUL THEN
FIRST_ASSUM MATCH_MP_TAC THEN ASM_REWRITE_TAC[]]]);;
(* ------------------------------------------------------------------------- *)
(* A bit more on nearly-multiplicative functions. *)
(* ------------------------------------------------------------------------- *)
let NADD_UBOUND = prove
(`!x. ?B N. !n. N <= n ==> fn x n <= B * n`,
GEN_TAC THEN X_CHOOSE_THEN `A1:num`
(X_CHOOSE_TAC `A2:num`) (SPEC `x:nadd` NADD_BOUND) THEN
EXISTS_TAC `A1 + A2` THEN EXISTS_TAC `1` THEN
REPEAT STRIP_TAC THEN MATCH_MP_TAC LE_TRANS THEN
EXISTS_TAC `A1 * n + A2` THEN ASM_REWRITE_TAC[] THEN
REWRITE_TAC[RIGHT_ADD_DISTRIB; LE_ADD_LCANCEL] THEN
GEN_REWRITE_TAC LAND_CONV [GSYM(el 3 (CONJUNCTS MULT_CLAUSES))] THEN
ASM_REWRITE_TAC[LE_MULT_LCANCEL]);;
let NADD_NONZERO = prove
(`!x. ~(x === &0) ==> ?N. !n. N <= n ==> ~(fn x n = 0)`,
GEN_TAC THEN DISCH_THEN(MP_TAC o MATCH_MP NADD_ARCH_MULT) THEN
DISCH_THEN(MP_TAC o SPEC `1`) THEN
REWRITE_TAC[nadd_le; NADD_MUL; NADD_OF_NUM; MULT_CLAUSES] THEN
DISCH_THEN(X_CHOOSE_THEN `A1:num` (X_CHOOSE_TAC `A2:num`)) THEN
EXISTS_TAC `A2 + 1` THEN X_GEN_TAC `n:num` THEN REPEAT DISCH_TAC THEN
FIRST_ASSUM(UNDISCH_TAC o check is_forall o concl) THEN
REWRITE_TAC[NOT_FORALL_THM; NOT_LE; GSYM LE_SUC_LT; ADD1] THEN
EXISTS_TAC `n:num` THEN ASM_REWRITE_TAC[MULT_CLAUSES; ADD_CLAUSES]);;
let NADD_LBOUND = prove
(`!x. ~(x === &0) ==> ?A N. !n. N <= n ==> n <= A * fn x n`,
GEN_TAC THEN DISCH_TAC THEN
FIRST_ASSUM(X_CHOOSE_TAC `N:num` o MATCH_MP NADD_NONZERO) THEN
FIRST_ASSUM(MP_TAC o MATCH_MP NADD_ARCH_MULT) THEN
DISCH_THEN(MP_TAC o SPEC `1`) THEN
REWRITE_TAC[nadd_le; NADD_MUL; NADD_OF_NUM; MULT_CLAUSES] THEN
DISCH_THEN(X_CHOOSE_THEN `A1:num` (X_CHOOSE_TAC `A2:num`)) THEN
EXISTS_TAC `A1 + A2` THEN EXISTS_TAC `N:num` THEN GEN_TAC THEN
DISCH_THEN(ANTE_RES_THEN ASSUME_TAC) THEN
MATCH_MP_TAC LE_TRANS THEN EXISTS_TAC `A1 * fn x n + A2` THEN
ASM_REWRITE_TAC[RIGHT_ADD_DISTRIB; LE_ADD_LCANCEL] THEN
GEN_REWRITE_TAC LAND_CONV [GSYM(el 3 (CONJUNCTS MULT_CLAUSES))] THEN
REWRITE_TAC[LE_MULT_LCANCEL] THEN DISJ2_TAC THEN
REWRITE_TAC[GSYM(REWRITE_CONV[ARITH_SUC] `SUC 0`)] THEN
ASM_REWRITE_TAC[GSYM NOT_LT; LT]);;
(* ------------------------------------------------------------------------- *)
(* Auxiliary function for the multiplicative inverse. *)
(* ------------------------------------------------------------------------- *)
let nadd_rinv = new_definition
`nadd_rinv(x) = \n. (n * n) DIV (fn x n)`;;
let NADD_MUL_LINV_LEMMA0 = prove
(`!x. ~(x === &0) ==> ?A B. !n. nadd_rinv x n <= A * n + B`,
GEN_TAC THEN DISCH_TAC THEN ONCE_REWRITE_TAC[BOUNDS_IGNORE] THEN
FIRST_ASSUM(MP_TAC o MATCH_MP NADD_LBOUND) THEN
DISCH_THEN(X_CHOOSE_THEN `A:num` (X_CHOOSE_TAC `N:num`)) THEN
MAP_EVERY EXISTS_TAC [`A:num`; `0`; `SUC N`] THEN
GEN_TAC THEN DISCH_TAC THEN REWRITE_TAC[ADD_CLAUSES] THEN
MP_TAC(SPECL [`nadd_rinv x n`; `A * n`; `n:num`] LE_MULT_RCANCEL) THEN
UNDISCH_TAC `SUC N <= n` THEN ASM_CASES_TAC `n = 0` THEN
ASM_REWRITE_TAC[LE; NOT_SUC] THEN DISCH_TAC THEN
DISCH_THEN(SUBST1_TAC o SYM) THEN
MATCH_MP_TAC LE_TRANS THEN EXISTS_TAC `nadd_rinv x n * A * fn x n` THEN
ASM_REWRITE_TAC[LE_MULT_LCANCEL] THEN CONJ_TAC THENL
[DISJ2_TAC THEN FIRST_ASSUM MATCH_MP_TAC THEN MATCH_MP_TAC LE_TRANS THEN
EXISTS_TAC `SUC N` THEN ASM_REWRITE_TAC[LE; LE_REFL];
GEN_REWRITE_TAC LAND_CONV [MULT_SYM] THEN
REWRITE_TAC[GSYM MULT_ASSOC; LE_MULT_LCANCEL] THEN
DISJ2_TAC THEN ASM_CASES_TAC `fn x n = 0` THEN
ASM_REWRITE_TAC[MULT_CLAUSES; LE_0; nadd_rinv] THEN
FIRST_ASSUM(MP_TAC o MATCH_MP DIVISION) THEN
DISCH_THEN(fun t -> GEN_REWRITE_TAC RAND_CONV [CONJUNCT1(SPEC_ALL t)]) THEN
GEN_REWRITE_TAC LAND_CONV [MULT_SYM] THEN REWRITE_TAC[LE_ADD]]);;
let NADD_MUL_LINV_LEMMA1 = prove