-
Notifications
You must be signed in to change notification settings - Fork 78
/
arith.ml
1904 lines (1596 loc) · 73.7 KB
/
arith.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
(* ========================================================================= *)
(* Natural number arithmetic. *)
(* *)
(* John Harrison, University of Cambridge Computer Laboratory *)
(* *)
(* (c) Copyright, University of Cambridge 1998 *)
(* (c) Copyright, John Harrison 1998-2007 *)
(* (c) Copyright, Marco Maggesi 2015 *)
(* (c) Copyright, Andrea Gabrielli, Marco Maggesi 2017-2018 *)
(* (c) Copyright, Mario Carneiro 2020 *)
(* ========================================================================= *)
needs "recursion.ml";;
(* ------------------------------------------------------------------------- *)
(* Note: all the following proofs are intuitionistic and intensional, except *)
(* for the least number principle num_WOP. *)
(* (And except the arith rewrites at the end; these could be done that way *)
(* but they use the conditional anyway.) In fact, one could very easily *)
(* write a "decider" returning P \/ ~P for quantifier-free P. *)
(* ------------------------------------------------------------------------- *)
parse_as_infix("<",(12,"right"));;
parse_as_infix("<=",(12,"right"));;
parse_as_infix(">",(12,"right"));;
parse_as_infix(">=",(12,"right"));;
parse_as_infix("+",(16,"right"));;
parse_as_infix("-",(18,"left"));;
parse_as_infix("*",(20,"right"));;
parse_as_infix("EXP",(24,"left"));;
parse_as_infix("DIV",(22,"left"));;
parse_as_infix("MOD",(22,"left"));;
(* ------------------------------------------------------------------------- *)
(* The predecessor function. *)
(* ------------------------------------------------------------------------- *)
let PRE = new_recursive_definition num_RECURSION
`(PRE 0 = 0) /\
(!n. PRE (SUC n) = n)`;;
(* ------------------------------------------------------------------------- *)
(* Addition. *)
(* ------------------------------------------------------------------------- *)
let ADD = new_recursive_definition num_RECURSION
`(!n. 0 + n = n) /\
(!m n. (SUC m) + n = SUC(m + n))`;;
let ADD_0 = prove
(`!m. m + 0 = m`,
INDUCT_TAC THEN ASM_REWRITE_TAC[ADD]);;
let ADD_SUC = prove
(`!m n. m + (SUC n) = SUC(m + n)`,
INDUCT_TAC THEN ASM_REWRITE_TAC[ADD]);;
let ADD_CLAUSES = prove
(`(!n. 0 + n = n) /\
(!m. m + 0 = m) /\
(!m n. (SUC m) + n = SUC(m + n)) /\
(!m n. m + (SUC n) = SUC(m + n))`,
REWRITE_TAC[ADD; ADD_0; ADD_SUC]);;
let ADD_SYM = prove
(`!m n. m + n = n + m`,
INDUCT_TAC THEN ASM_REWRITE_TAC[ADD_CLAUSES]);;
let ADD_ASSOC = prove
(`!m n p. m + (n + p) = (m + n) + p`,
INDUCT_TAC THEN ASM_REWRITE_TAC[ADD_CLAUSES]);;
let ADD_AC = prove
(`(m + n = n + m) /\
((m + n) + p = m + (n + p)) /\
(m + (n + p) = n + (m + p))`,
MESON_TAC[ADD_ASSOC; ADD_SYM]);;
let ADD_EQ_0 = prove
(`!m n. (m + n = 0) <=> (m = 0) /\ (n = 0)`,
REPEAT INDUCT_TAC THEN REWRITE_TAC[ADD_CLAUSES; NOT_SUC]);;
let EQ_ADD_LCANCEL = prove
(`!m n p. (m + n = m + p) <=> (n = p)`,
INDUCT_TAC THEN ASM_REWRITE_TAC[ADD_CLAUSES; SUC_INJ]);;
let EQ_ADD_RCANCEL = prove
(`!m n p. (m + p = n + p) <=> (m = n)`,
ONCE_REWRITE_TAC[ADD_SYM] THEN MATCH_ACCEPT_TAC EQ_ADD_LCANCEL);;
let EQ_ADD_LCANCEL_0 = prove
(`!m n. (m + n = m) <=> (n = 0)`,
INDUCT_TAC THEN ASM_REWRITE_TAC[ADD_CLAUSES; SUC_INJ]);;
let EQ_ADD_RCANCEL_0 = prove
(`!m n. (m + n = n) <=> (m = 0)`,
ONCE_REWRITE_TAC[ADD_SYM] THEN MATCH_ACCEPT_TAC EQ_ADD_LCANCEL_0);;
(* ------------------------------------------------------------------------- *)
(* Now define "bitwise" binary representation of numerals. *)
(* ------------------------------------------------------------------------- *)
let BIT0 = prove
(`!n. BIT0 n = n + n`,
INDUCT_TAC THEN ASM_REWRITE_TAC[BIT0_DEF; ADD_CLAUSES]);;
let BIT1 = prove
(`!n. BIT1 n = SUC(n + n)`,
REWRITE_TAC[BIT1_DEF; BIT0]);;
let BIT0_THM = prove
(`!n. NUMERAL (BIT0 n) = NUMERAL n + NUMERAL n`,
REWRITE_TAC[NUMERAL; BIT0]);;
let BIT1_THM = prove
(`!n. NUMERAL (BIT1 n) = SUC(NUMERAL n + NUMERAL n)`,
REWRITE_TAC[NUMERAL; BIT1]);;
(* ------------------------------------------------------------------------- *)
(* Following is handy before num_CONV arrives. *)
(* ------------------------------------------------------------------------- *)
let ONE = prove
(`1 = SUC 0`,
REWRITE_TAC[BIT1; REWRITE_RULE[NUMERAL] ADD_CLAUSES; NUMERAL]);;
let TWO = prove
(`2 = SUC 1`,
REWRITE_TAC[BIT0; BIT1; REWRITE_RULE[NUMERAL] ADD_CLAUSES; NUMERAL]);;
(* ------------------------------------------------------------------------- *)
(* One immediate consequence. *)
(* ------------------------------------------------------------------------- *)
let ADD1 = prove
(`!m. SUC m = m + 1`,
REWRITE_TAC[BIT1_THM; ADD_CLAUSES]);;
(* ------------------------------------------------------------------------- *)
(* Multiplication. *)
(* ------------------------------------------------------------------------- *)
let MULT = new_recursive_definition num_RECURSION
`(!n. 0 * n = 0) /\
(!m n. (SUC m) * n = (m * n) + n)`;;
let MULT_0 = prove
(`!m. m * 0 = 0`,
INDUCT_TAC THEN ASM_REWRITE_TAC[MULT; ADD_CLAUSES]);;
let MULT_SUC = prove
(`!m n. m * (SUC n) = m + (m * n)`,
INDUCT_TAC THEN ASM_REWRITE_TAC[MULT; ADD_CLAUSES; ADD_ASSOC]);;
let MULT_CLAUSES = prove
(`(!n. 0 * n = 0) /\
(!m. m * 0 = 0) /\
(!n. 1 * n = n) /\
(!m. m * 1 = m) /\
(!m n. (SUC m) * n = (m * n) + n) /\
(!m n. m * (SUC n) = m + (m * n))`,
REWRITE_TAC[BIT1_THM; MULT; MULT_0; MULT_SUC; ADD_CLAUSES]);;
let MULT_SYM = prove
(`!m n. m * n = n * m`,
INDUCT_TAC THEN ASM_REWRITE_TAC[MULT_CLAUSES; EQT_INTRO(SPEC_ALL ADD_SYM)]);;
let LEFT_ADD_DISTRIB = prove
(`!m n p. m * (n + p) = (m * n) + (m * p)`,
GEN_TAC THEN INDUCT_TAC THEN ASM_REWRITE_TAC[ADD; MULT_CLAUSES; ADD_ASSOC]);;
let RIGHT_ADD_DISTRIB = prove
(`!m n p. (m + n) * p = (m * p) + (n * p)`,
ONCE_REWRITE_TAC[MULT_SYM] THEN MATCH_ACCEPT_TAC LEFT_ADD_DISTRIB);;
let MULT_ASSOC = prove
(`!m n p. m * (n * p) = (m * n) * p`,
INDUCT_TAC THEN ASM_REWRITE_TAC[MULT_CLAUSES; RIGHT_ADD_DISTRIB]);;
let MULT_AC = prove
(`(m * n = n * m) /\
((m * n) * p = m * (n * p)) /\
(m * (n * p) = n * (m * p))`,
MESON_TAC[MULT_ASSOC; MULT_SYM]);;
let MULT_EQ_0 = prove
(`!m n. (m * n = 0) <=> (m = 0) \/ (n = 0)`,
REPEAT INDUCT_TAC THEN REWRITE_TAC[MULT_CLAUSES; ADD_CLAUSES; NOT_SUC]);;
let EQ_MULT_LCANCEL = prove
(`!m n p. (m * n = m * p) <=> (m = 0) \/ (n = p)`,
INDUCT_TAC THEN REWRITE_TAC[MULT_CLAUSES; NOT_SUC] THEN
REPEAT INDUCT_TAC THEN
ASM_REWRITE_TAC[MULT_CLAUSES; ADD_CLAUSES; GSYM NOT_SUC; NOT_SUC] THEN
ASM_REWRITE_TAC[SUC_INJ; GSYM ADD_ASSOC; EQ_ADD_LCANCEL]);;
let EQ_MULT_RCANCEL = prove
(`!m n p. (m * p = n * p) <=> (m = n) \/ (p = 0)`,
ONCE_REWRITE_TAC[MULT_SYM; DISJ_SYM] THEN MATCH_ACCEPT_TAC EQ_MULT_LCANCEL);;
let MULT_2 = prove
(`!n. 2 * n = n + n`,
GEN_TAC THEN REWRITE_TAC[BIT0_THM; MULT_CLAUSES; RIGHT_ADD_DISTRIB]);;
let MULT_EQ_1 = prove
(`!m n. (m * n = 1) <=> (m = 1) /\ (n = 1)`,
INDUCT_TAC THEN INDUCT_TAC THEN REWRITE_TAC
[MULT_CLAUSES; ADD_CLAUSES; BIT0_THM; BIT1_THM; GSYM NOT_SUC] THEN
REWRITE_TAC[SUC_INJ; ADD_EQ_0; MULT_EQ_0] THEN
CONV_TAC TAUT);;
(* ------------------------------------------------------------------------- *)
(* Exponentiation. *)
(* ------------------------------------------------------------------------- *)
let EXP = new_recursive_definition num_RECURSION
`(!m. m EXP 0 = 1) /\
(!m n. m EXP (SUC n) = m * (m EXP n))`;;
let EXP_EQ_0 = prove
(`!m n. (m EXP n = 0) <=> (m = 0) /\ ~(n = 0)`,
REPEAT INDUCT_TAC THEN ASM_REWRITE_TAC
[BIT1_THM; NOT_SUC; NOT_SUC; EXP; MULT_CLAUSES; ADD_CLAUSES; ADD_EQ_0]);;
let EXP_EQ_1 = prove
(`!x n. x EXP n = 1 <=> x = 1 \/ n = 0`,
GEN_TAC THEN INDUCT_TAC THEN ASM_REWRITE_TAC[EXP; MULT_EQ_1; NOT_SUC] THEN
CONV_TAC TAUT);;
let EXP_ZERO = prove
(`!n. 0 EXP n = if n = 0 then 1 else 0`,
GEN_TAC THEN COND_CASES_TAC THEN ASM_REWRITE_TAC[EXP_EQ_0; EXP_EQ_1]);;
let EXP_ADD = prove
(`!m n p. m EXP (n + p) = (m EXP n) * (m EXP p)`,
GEN_TAC THEN GEN_TAC THEN INDUCT_TAC THEN
ASM_REWRITE_TAC[EXP; ADD_CLAUSES; MULT_CLAUSES; MULT_AC]);;
let EXP_ONE = prove
(`!n. 1 EXP n = 1`,
INDUCT_TAC THEN ASM_REWRITE_TAC[EXP; MULT_CLAUSES]);;
let EXP_1 = prove
(`!n. n EXP 1 = n`,
REWRITE_TAC[ONE; EXP; MULT_CLAUSES; ADD_CLAUSES]);;
let EXP_2 = prove
(`!n. n EXP 2 = n * n`,
REWRITE_TAC[BIT0_THM; BIT1_THM; EXP; EXP_ADD; MULT_CLAUSES; ADD_CLAUSES]);;
let MULT_EXP = prove
(`!p m n. (m * n) EXP p = m EXP p * n EXP p`,
INDUCT_TAC THEN ASM_REWRITE_TAC[EXP; MULT_CLAUSES; MULT_AC]);;
let EXP_MULT = prove
(`!m n p. m EXP (n * p) = (m EXP n) EXP p`,
GEN_TAC THEN INDUCT_TAC THEN
ASM_REWRITE_TAC[EXP_ADD; EXP; MULT_CLAUSES] THENL
[CONV_TAC(ONCE_DEPTH_CONV SYM_CONV) THEN
INDUCT_TAC THEN ASM_REWRITE_TAC[EXP; MULT_CLAUSES];
REWRITE_TAC[MULT_EXP] THEN MATCH_ACCEPT_TAC MULT_SYM]);;
let EXP_EXP = prove
(`!x m n. (x EXP m) EXP n = x EXP (m * n)`,
REWRITE_TAC[EXP_MULT]);;
(* ------------------------------------------------------------------------- *)
(* Define the orderings recursively too. *)
(* ------------------------------------------------------------------------- *)
let LE = new_recursive_definition num_RECURSION
`(!m. (m <= 0) <=> (m = 0)) /\
(!m n. (m <= SUC n) <=> (m = SUC n) \/ (m <= n))`;;
let LT = new_recursive_definition num_RECURSION
`(!m. (m < 0) <=> F) /\
(!m n. (m < SUC n) <=> (m = n) \/ (m < n))`;;
let GE = new_definition
`m >= n <=> n <= m`;;
let GT = new_definition
`m > n <=> n < m`;;
(* ------------------------------------------------------------------------- *)
(* Maximum and minimum of natural numbers. *)
(* ------------------------------------------------------------------------- *)
let MAX = new_definition
`!m n. MAX m n = if m <= n then n else m`;;
let MIN = new_definition
`!m n. MIN m n = if m <= n then m else n`;;
(* ------------------------------------------------------------------------- *)
(* Step cases. *)
(* ------------------------------------------------------------------------- *)
let LE_SUC_LT = prove
(`!m n. (SUC m <= n) <=> (m < n)`,
GEN_TAC THEN INDUCT_TAC THEN ASM_REWRITE_TAC[LE; LT; NOT_SUC; SUC_INJ]);;
let LT_SUC_LE = prove
(`!m n. (m < SUC n) <=> (m <= n)`,
GEN_TAC THEN INDUCT_TAC THEN ONCE_REWRITE_TAC[LT; LE] THEN
ASM_REWRITE_TAC[] THEN REWRITE_TAC[LT]);;
let LE_SUC = prove
(`!m n. (SUC m <= SUC n) <=> (m <= n)`,
REWRITE_TAC[LE_SUC_LT; LT_SUC_LE]);;
let LT_SUC = prove
(`!m n. (SUC m < SUC n) <=> (m < n)`,
REWRITE_TAC[LT_SUC_LE; LE_SUC_LT]);;
(* ------------------------------------------------------------------------- *)
(* Base cases. *)
(* ------------------------------------------------------------------------- *)
let LE_0 = prove
(`!n. 0 <= n`,
INDUCT_TAC THEN ASM_REWRITE_TAC[LE]);;
let LT_0 = prove
(`!n. 0 < SUC n`,
REWRITE_TAC[LT_SUC_LE; LE_0]);;
(* ------------------------------------------------------------------------- *)
(* Reflexivity. *)
(* ------------------------------------------------------------------------- *)
let LE_REFL = prove
(`!n. n <= n`,
INDUCT_TAC THEN REWRITE_TAC[LE]);;
let LT_REFL = prove
(`!n. ~(n < n)`,
INDUCT_TAC THEN ASM_REWRITE_TAC[LT_SUC] THEN REWRITE_TAC[LT]);;
let LT_IMP_NE = prove
(`!m n:num. m < n ==> ~(m = n)`,
MESON_TAC[LT_REFL]);;
(* ------------------------------------------------------------------------- *)
(* Antisymmetry. *)
(* ------------------------------------------------------------------------- *)
let LE_ANTISYM = prove
(`!m n. (m <= n /\ n <= m) <=> (m = n)`,
REPEAT INDUCT_TAC THEN ASM_REWRITE_TAC[LE_SUC; SUC_INJ] THEN
REWRITE_TAC[LE; NOT_SUC; GSYM NOT_SUC]);;
let LT_ANTISYM = prove
(`!m n. ~(m < n /\ n < m)`,
REPEAT INDUCT_TAC THEN ASM_REWRITE_TAC[LT_SUC] THEN REWRITE_TAC[LT]);;
let LET_ANTISYM = prove
(`!m n. ~(m <= n /\ n < m)`,
REPEAT INDUCT_TAC THEN ASM_REWRITE_TAC[LE_SUC; LT_SUC] THEN
REWRITE_TAC[LE; LT; NOT_SUC]);;
let LTE_ANTISYM = prove
(`!m n. ~(m < n /\ n <= m)`,
ONCE_REWRITE_TAC[CONJ_SYM] THEN REWRITE_TAC[LET_ANTISYM]);;
(* ------------------------------------------------------------------------- *)
(* Transitivity. *)
(* ------------------------------------------------------------------------- *)
let LE_TRANS = prove
(`!m n p. m <= n /\ n <= p ==> m <= p`,
REPEAT INDUCT_TAC THEN
ASM_REWRITE_TAC[LE_SUC; LE_0] THEN REWRITE_TAC[LE; NOT_SUC]);;
let LT_TRANS = prove
(`!m n p. m < n /\ n < p ==> m < p`,
REPEAT INDUCT_TAC THEN
ASM_REWRITE_TAC[LT_SUC; LT_0] THEN REWRITE_TAC[LT; NOT_SUC]);;
let LET_TRANS = prove
(`!m n p. m <= n /\ n < p ==> m < p`,
REPEAT INDUCT_TAC THEN
ASM_REWRITE_TAC[LE_SUC; LT_SUC; LT_0] THEN REWRITE_TAC[LT; LE; NOT_SUC]);;
let LTE_TRANS = prove
(`!m n p. m < n /\ n <= p ==> m < p`,
REPEAT INDUCT_TAC THEN
ASM_REWRITE_TAC[LE_SUC; LT_SUC; LT_0] THEN REWRITE_TAC[LT; LE; NOT_SUC]);;
(* ------------------------------------------------------------------------- *)
(* Totality. *)
(* ------------------------------------------------------------------------- *)
let LE_CASES = prove
(`!m n. m <= n \/ n <= m`,
REPEAT INDUCT_TAC THEN ASM_REWRITE_TAC[LE_0; LE_SUC]);;
let LT_CASES = prove
(`!m n. (m < n) \/ (n < m) \/ (m = n)`,
REPEAT INDUCT_TAC THEN ASM_REWRITE_TAC[LT_SUC; SUC_INJ] THEN
REWRITE_TAC[LT; NOT_SUC; GSYM NOT_SUC] THEN
W(W (curry SPEC_TAC) o hd o frees o snd) THEN
INDUCT_TAC THEN REWRITE_TAC[LT_0]);;
let LET_CASES = prove
(`!m n. m <= n \/ n < m`,
REPEAT INDUCT_TAC THEN ASM_REWRITE_TAC[LE_SUC_LT; LT_SUC_LE; LE_0]);;
let LTE_CASES = prove
(`!m n. m < n \/ n <= m`,
ONCE_REWRITE_TAC[DISJ_SYM] THEN MATCH_ACCEPT_TAC LET_CASES);;
(* ------------------------------------------------------------------------- *)
(* Relationship between orderings. *)
(* ------------------------------------------------------------------------- *)
let LE_LT = prove
(`!m n. (m <= n) <=> (m < n) \/ (m = n)`,
REPEAT INDUCT_TAC THEN
ASM_REWRITE_TAC[LE_SUC; LT_SUC; SUC_INJ; LE_0; LT_0] THEN
REWRITE_TAC[LE; LT]);;
let LT_LE = prove
(`!m n. (m < n) <=> (m <= n) /\ ~(m = n)`,
REWRITE_TAC[LE_LT] THEN REPEAT GEN_TAC THEN EQ_TAC THENL
[DISCH_TAC THEN ASM_REWRITE_TAC[] THEN DISCH_THEN SUBST_ALL_TAC THEN
POP_ASSUM MP_TAC THEN REWRITE_TAC[LT_REFL];
DISCH_THEN(CONJUNCTS_THEN2 STRIP_ASSUME_TAC MP_TAC) THEN
ASM_REWRITE_TAC[]]);;
let NOT_LE = prove
(`!m n. ~(m <= n) <=> (n < m)`,
REPEAT INDUCT_TAC THEN ASM_REWRITE_TAC[LE_SUC; LT_SUC] THEN
REWRITE_TAC[LE; LT; NOT_SUC; GSYM NOT_SUC; LE_0] THEN
W(W (curry SPEC_TAC) o hd o frees o snd) THEN
INDUCT_TAC THEN REWRITE_TAC[LT_0]);;
let NOT_LT = prove
(`!m n. ~(m < n) <=> n <= m`,
REPEAT INDUCT_TAC THEN ASM_REWRITE_TAC[LE_SUC; LT_SUC] THEN
REWRITE_TAC[LE; LT; NOT_SUC; GSYM NOT_SUC; LE_0] THEN
W(W (curry SPEC_TAC) o hd o frees o snd) THEN
INDUCT_TAC THEN REWRITE_TAC[LT_0]);;
let LT_IMP_LE = prove
(`!m n. m < n ==> m <= n`,
REWRITE_TAC[LT_LE] THEN REPEAT STRIP_TAC THEN ASM_REWRITE_TAC[]);;
let EQ_IMP_LE = prove
(`!m n. (m = n) ==> m <= n`,
REPEAT STRIP_TAC THEN ASM_REWRITE_TAC[LE_REFL]);;
(* ------------------------------------------------------------------------- *)
(* Often useful to shuffle between different versions of "0 < n". *)
(* ------------------------------------------------------------------------- *)
let LT_NZ = prove
(`!n. 0 < n <=> ~(n = 0)`,
INDUCT_TAC THEN ASM_REWRITE_TAC[NOT_SUC; LT; EQ_SYM_EQ] THEN
CONV_TAC TAUT);;
let LE_1 = prove
(`(!n. ~(n = 0) ==> 0 < n) /\
(!n. ~(n = 0) ==> 1 <= n) /\
(!n. 0 < n ==> ~(n = 0)) /\
(!n. 0 < n ==> 1 <= n) /\
(!n. 1 <= n ==> 0 < n) /\
(!n. 1 <= n ==> ~(n = 0))`,
REWRITE_TAC[LT_NZ; GSYM NOT_LT; ONE; LT]);;
(* ------------------------------------------------------------------------- *)
(* Relate the orderings to arithmetic operations. *)
(* ------------------------------------------------------------------------- *)
let LE_EXISTS = prove
(`!m n. (m <= n) <=> (?d. n = m + d)`,
GEN_TAC THEN INDUCT_TAC THEN ASM_REWRITE_TAC[LE] THENL
[REWRITE_TAC[CONV_RULE(LAND_CONV SYM_CONV) (SPEC_ALL ADD_EQ_0)] THEN
REWRITE_TAC[RIGHT_EXISTS_AND_THM; EXISTS_REFL];
EQ_TAC THENL
[DISCH_THEN(DISJ_CASES_THEN2 SUBST1_TAC MP_TAC) THENL
[EXISTS_TAC `0` THEN REWRITE_TAC[ADD_CLAUSES];
DISCH_THEN(X_CHOOSE_THEN `d:num` SUBST1_TAC) THEN
EXISTS_TAC `SUC d` THEN REWRITE_TAC[ADD_CLAUSES]];
ONCE_REWRITE_TAC[LEFT_IMP_EXISTS_THM] THEN
INDUCT_TAC THEN REWRITE_TAC[ADD_CLAUSES; SUC_INJ] THEN
DISCH_THEN SUBST1_TAC THEN REWRITE_TAC[] THEN DISJ2_TAC THEN
REWRITE_TAC[EQ_ADD_LCANCEL; GSYM EXISTS_REFL]]]);;
let LT_EXISTS = prove
(`!m n. (m < n) <=> (?d. n = m + SUC d)`,
GEN_TAC THEN INDUCT_TAC THEN REWRITE_TAC[LT; ADD_CLAUSES; GSYM NOT_SUC] THEN
ASM_REWRITE_TAC[SUC_INJ] THEN EQ_TAC THENL
[DISCH_THEN(DISJ_CASES_THEN2 SUBST1_TAC MP_TAC) THENL
[EXISTS_TAC `0` THEN REWRITE_TAC[ADD_CLAUSES];
DISCH_THEN(X_CHOOSE_THEN `d:num` SUBST1_TAC) THEN
EXISTS_TAC `SUC d` THEN REWRITE_TAC[ADD_CLAUSES]];
ONCE_REWRITE_TAC[LEFT_IMP_EXISTS_THM] THEN
INDUCT_TAC THEN REWRITE_TAC[ADD_CLAUSES; SUC_INJ] THEN
DISCH_THEN SUBST1_TAC THEN REWRITE_TAC[] THEN DISJ2_TAC THEN
REWRITE_TAC[SUC_INJ; EQ_ADD_LCANCEL; GSYM EXISTS_REFL]]);;
(* ------------------------------------------------------------------------- *)
(* Interaction with addition. *)
(* ------------------------------------------------------------------------- *)
let LE_ADD = prove
(`!m n. m <= m + n`,
GEN_TAC THEN INDUCT_TAC THEN
ASM_REWRITE_TAC[LE; ADD_CLAUSES; LE_REFL]);;
let LE_ADDR = prove
(`!m n. n <= m + n`,
ONCE_REWRITE_TAC[ADD_SYM] THEN MATCH_ACCEPT_TAC LE_ADD);;
let LT_ADD = prove
(`!m n. (m < m + n) <=> (0 < n)`,
INDUCT_TAC THEN ASM_REWRITE_TAC[ADD_CLAUSES; LT_SUC]);;
let LT_ADDR = prove
(`!m n. (n < m + n) <=> (0 < m)`,
ONCE_REWRITE_TAC[ADD_SYM] THEN MATCH_ACCEPT_TAC LT_ADD);;
let LE_ADD_LCANCEL = prove
(`!m n p. (m + n) <= (m + p) <=> n <= p`,
REWRITE_TAC[LE_EXISTS; GSYM ADD_ASSOC; EQ_ADD_LCANCEL]);;
let LE_ADD_RCANCEL = prove
(`!m n p. (m + p) <= (n + p) <=> (m <= n)`,
ONCE_REWRITE_TAC[ADD_SYM] THEN MATCH_ACCEPT_TAC LE_ADD_LCANCEL);;
let LT_ADD_LCANCEL = prove
(`!m n p. (m + n) < (m + p) <=> n < p`,
REWRITE_TAC[LT_EXISTS; GSYM ADD_ASSOC; EQ_ADD_LCANCEL; SUC_INJ]);;
let LT_ADD_RCANCEL = prove
(`!m n p. (m + p) < (n + p) <=> (m < n)`,
ONCE_REWRITE_TAC[ADD_SYM] THEN MATCH_ACCEPT_TAC LT_ADD_LCANCEL);;
let LE_ADD2 = prove
(`!m n p q. m <= p /\ n <= q ==> m + n <= p + q`,
REPEAT GEN_TAC THEN REWRITE_TAC[LE_EXISTS] THEN
DISCH_THEN(CONJUNCTS_THEN2
(X_CHOOSE_TAC `a:num`) (X_CHOOSE_TAC `b:num`)) THEN
EXISTS_TAC `a + b` THEN ASM_REWRITE_TAC[ADD_AC]);;
let LET_ADD2 = prove
(`!m n p q. m <= p /\ n < q ==> m + n < p + q`,
REPEAT GEN_TAC THEN REWRITE_TAC[LE_EXISTS; LT_EXISTS] THEN
DISCH_THEN(CONJUNCTS_THEN2
(X_CHOOSE_TAC `a:num`) (X_CHOOSE_TAC `b:num`)) THEN
EXISTS_TAC `a + b` THEN ASM_REWRITE_TAC[SUC_INJ; ADD_CLAUSES; ADD_AC]);;
let LTE_ADD2 = prove
(`!m n p q. m < p /\ n <= q ==> m + n < p + q`,
ONCE_REWRITE_TAC[ADD_SYM; CONJ_SYM] THEN
MATCH_ACCEPT_TAC LET_ADD2);;
let LT_ADD2 = prove
(`!m n p q. m < p /\ n < q ==> m + n < p + q`,
REPEAT STRIP_TAC THEN MATCH_MP_TAC LTE_ADD2 THEN
ASM_REWRITE_TAC[] THEN MATCH_MP_TAC LT_IMP_LE THEN
ASM_REWRITE_TAC[]);;
(* ------------------------------------------------------------------------- *)
(* And multiplication. *)
(* ------------------------------------------------------------------------- *)
let LT_MULT = prove
(`!m n. (0 < m * n) <=> (0 < m) /\ (0 < n)`,
REPEAT INDUCT_TAC THEN REWRITE_TAC[MULT_CLAUSES; ADD_CLAUSES; LT_0]);;
let LE_MULT2 = prove
(`!m n p q. m <= n /\ p <= q ==> m * p <= n * q`,
REPEAT GEN_TAC THEN REWRITE_TAC[LE_EXISTS] THEN
DISCH_THEN(CONJUNCTS_THEN2
(X_CHOOSE_TAC `a:num`) (X_CHOOSE_TAC `b:num`)) THEN
EXISTS_TAC `a * p + m * b + a * b` THEN
ASM_REWRITE_TAC[LEFT_ADD_DISTRIB] THEN
REWRITE_TAC[LEFT_ADD_DISTRIB; RIGHT_ADD_DISTRIB; ADD_ASSOC]);;
let LT_LMULT = prove
(`!m n p. ~(m = 0) /\ n < p ==> m * n < m * p`,
REPEAT GEN_TAC THEN REWRITE_TAC[LT_LE] THEN STRIP_TAC THEN CONJ_TAC THENL
[MATCH_MP_TAC LE_MULT2 THEN ASM_REWRITE_TAC[LE_REFL];
ASM_REWRITE_TAC[EQ_MULT_LCANCEL]]);;
let LE_MULT_LCANCEL = prove
(`!m n p. (m * n) <= (m * p) <=> (m = 0) \/ n <= p`,
REPEAT INDUCT_TAC THEN
ASM_REWRITE_TAC[MULT_CLAUSES; ADD_CLAUSES; LE_REFL; LE_0; NOT_SUC] THEN
REWRITE_TAC[LE_SUC] THEN
REWRITE_TAC[LE; LE_ADD_LCANCEL; GSYM ADD_ASSOC] THEN
ASM_REWRITE_TAC[GSYM(el 4(CONJUNCTS MULT_CLAUSES)); NOT_SUC]);;
let LE_MULT_RCANCEL = prove
(`!m n p. (m * p) <= (n * p) <=> (m <= n) \/ (p = 0)`,
ONCE_REWRITE_TAC[MULT_SYM; DISJ_SYM] THEN
MATCH_ACCEPT_TAC LE_MULT_LCANCEL);;
let LT_MULT_LCANCEL = prove
(`!m n p. (m * n) < (m * p) <=> ~(m = 0) /\ n < p`,
REPEAT INDUCT_TAC THEN
ASM_REWRITE_TAC[MULT_CLAUSES; ADD_CLAUSES; LT_REFL; LT_0; NOT_SUC] THEN
REWRITE_TAC[LT_SUC] THEN
REWRITE_TAC[LT; LT_ADD_LCANCEL; GSYM ADD_ASSOC] THEN
ASM_REWRITE_TAC[GSYM(el 4(CONJUNCTS MULT_CLAUSES)); NOT_SUC]);;
let LT_MULT_RCANCEL = prove
(`!m n p. (m * p) < (n * p) <=> (m < n) /\ ~(p = 0)`,
ONCE_REWRITE_TAC[MULT_SYM; CONJ_SYM] THEN
MATCH_ACCEPT_TAC LT_MULT_LCANCEL);;
let LT_MULT2 = prove
(`!m n p q. m < n /\ p < q ==> m * p < n * q`,
REPEAT STRIP_TAC THEN MATCH_MP_TAC LET_TRANS THEN
EXISTS_TAC `n * p` THEN
ASM_SIMP_TAC[LE_MULT_RCANCEL; LT_IMP_LE; LT_MULT_LCANCEL] THEN
UNDISCH_TAC `m < n` THEN CONV_TAC CONTRAPOS_CONV THEN SIMP_TAC[LT]);;
let LE_SQUARE_REFL = prove
(`!n. n <= n * n`,
INDUCT_TAC THEN REWRITE_TAC[MULT_CLAUSES; LE_0; LE_ADDR]);;
let LT_POW2_REFL = prove
(`!n. n < 2 EXP n`,
INDUCT_TAC THEN REWRITE_TAC[EXP] THEN REWRITE_TAC[MULT_2; ADD1] THEN
REWRITE_TAC[ONE; LT] THEN MATCH_MP_TAC LTE_ADD2 THEN
ASM_REWRITE_TAC[LE_SUC_LT; TWO] THEN
MESON_TAC[EXP_EQ_0; LE_1; NOT_SUC]);;
(* ------------------------------------------------------------------------- *)
(* Useful "without loss of generality" lemmas. *)
(* ------------------------------------------------------------------------- *)
let WLOG_LE = prove
(`(!m n. P m n <=> P n m) /\ (!m n. m <= n ==> P m n) ==> !m n. P m n`,
MESON_TAC[LE_CASES]);;
let WLOG_LT = prove
(`(!m. P m m) /\ (!m n. P m n <=> P n m) /\ (!m n. m < n ==> P m n)
==> !m y. P m y`,
MESON_TAC[LT_CASES]);;
let WLOG_LE_3 = prove
(`!P. (!x y z. P x y z ==> P y x z /\ P x z y) /\
(!x y z. x <= y /\ y <= z ==> P x y z)
==> !x y z. P x y z`,
MESON_TAC[LE_CASES]);;
(* ------------------------------------------------------------------------- *)
(* Existence of least and greatest elements of (finite) set. *)
(* ------------------------------------------------------------------------- *)
let num_WF = prove
(`!P. (!n. (!m. m < n ==> P m) ==> P n) ==> !n. P n`,
GEN_TAC THEN MP_TAC(SPEC `\n. !m. m < n ==> P m` num_INDUCTION) THEN
REWRITE_TAC[LT; BETA_THM] THEN MESON_TAC[LT]);;
let num_WOP = prove
(`!P. (?n. P n) <=> (?n. P(n) /\ !m. m < n ==> ~P(m))`,
GEN_TAC THEN EQ_TAC THENL [ALL_TAC; MESON_TAC[]] THEN
CONV_TAC CONTRAPOS_CONV THEN REWRITE_TAC[NOT_EXISTS_THM] THEN
DISCH_TAC THEN MATCH_MP_TAC num_WF THEN ASM_MESON_TAC[]);;
let num_MAX = prove
(`!P. (?x. P x) /\ (?M. !x. P x ==> x <= M) <=>
?m. P m /\ (!x. P x ==> x <= m)`,
GEN_TAC THEN EQ_TAC THENL
[DISCH_THEN(CONJUNCTS_THEN2 (X_CHOOSE_TAC `a:num`) MP_TAC) THEN
DISCH_THEN(X_CHOOSE_THEN `m:num` MP_TAC o ONCE_REWRITE_RULE[num_WOP]) THEN
DISCH_THEN(fun th -> EXISTS_TAC `m:num` THEN MP_TAC th) THEN
REWRITE_TAC[TAUT `(a /\ b ==> c /\ a) <=> (a /\ b ==> c)`] THEN
SPEC_TAC(`m:num`,`m:num`) THEN INDUCT_TAC THENL
[REWRITE_TAC[LE; LT] THEN DISCH_THEN(IMP_RES_THEN SUBST_ALL_TAC) THEN
POP_ASSUM ACCEPT_TAC;
DISCH_THEN(CONJUNCTS_THEN2 ASSUME_TAC (MP_TAC o SPEC `m:num`)) THEN
REWRITE_TAC[LT] THEN CONV_TAC CONTRAPOS_CONV THEN
DISCH_TAC THEN REWRITE_TAC[] THEN X_GEN_TAC `p:num` THEN
FIRST_ASSUM(MP_TAC o SPEC `p:num`) THEN REWRITE_TAC[LE] THEN
ASM_CASES_TAC `p = SUC m` THEN ASM_REWRITE_TAC[]];
REPEAT STRIP_TAC THEN EXISTS_TAC `m:num` THEN ASM_REWRITE_TAC[]]);;
(* ------------------------------------------------------------------------- *)
(* Other variants of induction. *)
(* ------------------------------------------------------------------------- *)
let LE_INDUCT = prove
(`!P. (!m:num. P m m) /\
(!m n. m <= n /\ P m n ==> P m (SUC n))
==> (!m n. m <= n ==> P m n)`,
GEN_TAC THEN REWRITE_TAC[IMP_CONJ; MESON[LE_EXISTS]
`(!m n:num. m <= n ==> R m n) <=> (!m d. R m (m + d))`] THEN
REPEAT DISCH_TAC THEN GEN_TAC THEN INDUCT_TAC THEN
ASM_SIMP_TAC[ADD_CLAUSES]);;
let num_INDUCTION_DOWN = prove
(`!(P:num->bool) m.
(!n. m <= n ==> P n) /\
(!n. n < m /\ P(n + 1) ==> P n)
==> !n. P n`,
REWRITE_TAC[GSYM ADD1] THEN REPEAT GEN_TAC THEN STRIP_TAC THEN
ONCE_REWRITE_TAC[MESON[] `(!x. P x) <=> ~(?x. ~P x)`] THEN
W(MP_TAC o PART_MATCH (lhand o lhand) num_MAX o rand o snd) THEN
MATCH_MP_TAC(TAUT `q /\ ~r ==> (p /\ q <=> r) ==> ~p`) THEN
ONCE_REWRITE_TAC[TAUT `~p ==> q <=> ~q ==> p`] THEN
REWRITE_TAC[NOT_EXISTS_THM; TAUT `~(~p /\ q) <=> q ==> p`; NOT_LE] THEN
ASM_MESON_TAC[LTE_CASES; LT; LT_IMP_LE]);;
(* ------------------------------------------------------------------------- *)
(* Oddness and evenness (recursively rather than inductively!) *)
(* ------------------------------------------------------------------------- *)
let EVEN = new_recursive_definition num_RECURSION
`(EVEN 0 <=> T) /\
(!n. EVEN (SUC n) <=> ~(EVEN n))`;;
let ODD = new_recursive_definition num_RECURSION
`(ODD 0 <=> F) /\
(!n. ODD (SUC n) <=> ~(ODD n))`;;
let NOT_EVEN = prove
(`!n. ~(EVEN n) <=> ODD n`,
INDUCT_TAC THEN ASM_REWRITE_TAC[EVEN; ODD]);;
let NOT_ODD = prove
(`!n. ~(ODD n) <=> EVEN n`,
INDUCT_TAC THEN ASM_REWRITE_TAC[EVEN; ODD]);;
let EVEN_OR_ODD = prove
(`!n. EVEN n \/ ODD n`,
INDUCT_TAC THEN REWRITE_TAC[EVEN; ODD; NOT_EVEN; NOT_ODD] THEN
ONCE_REWRITE_TAC[DISJ_SYM] THEN ASM_REWRITE_TAC[]);;
let EVEN_AND_ODD = prove
(`!n. ~(EVEN n /\ ODD n)`,
REWRITE_TAC[GSYM NOT_EVEN; ITAUT `~(p /\ ~p)`]);;
let EVEN_ADD = prove
(`!m n. EVEN(m + n) <=> (EVEN m <=> EVEN n)`,
INDUCT_TAC THEN ASM_REWRITE_TAC[EVEN; ADD_CLAUSES] THEN
X_GEN_TAC `p:num` THEN
DISJ_CASES_THEN MP_TAC (SPEC `n:num` EVEN_OR_ODD) THEN
DISJ_CASES_THEN MP_TAC (SPEC `p:num` EVEN_OR_ODD) THEN
REWRITE_TAC[GSYM NOT_EVEN] THEN DISCH_TAC THEN
ASM_REWRITE_TAC[]);;
let EVEN_MULT = prove
(`!m n. EVEN(m * n) <=> EVEN(m) \/ EVEN(n)`,
INDUCT_TAC THEN ASM_REWRITE_TAC[MULT_CLAUSES; EVEN_ADD; EVEN] THEN
X_GEN_TAC `p:num` THEN
DISJ_CASES_THEN MP_TAC (SPEC `n:num` EVEN_OR_ODD) THEN
DISJ_CASES_THEN MP_TAC (SPEC `p:num` EVEN_OR_ODD) THEN
REWRITE_TAC[GSYM NOT_EVEN] THEN DISCH_TAC THEN
ASM_REWRITE_TAC[]);;
let EVEN_EXP = prove
(`!m n. EVEN(m EXP n) <=> EVEN(m) /\ ~(n = 0)`,
GEN_TAC THEN INDUCT_TAC THEN
ASM_REWRITE_TAC[EVEN; EXP; ONE; EVEN_MULT; NOT_SUC] THEN
CONV_TAC ITAUT);;
let ODD_ADD = prove
(`!m n. ODD(m + n) <=> ~(ODD m <=> ODD n)`,
REPEAT GEN_TAC THEN REWRITE_TAC[GSYM NOT_EVEN; EVEN_ADD] THEN
CONV_TAC ITAUT);;
let ODD_MULT = prove
(`!m n. ODD(m * n) <=> ODD(m) /\ ODD(n)`,
REPEAT GEN_TAC THEN REWRITE_TAC[GSYM NOT_EVEN; EVEN_MULT] THEN
CONV_TAC ITAUT);;
let ODD_EXP = prove
(`!m n. ODD(m EXP n) <=> ODD(m) \/ (n = 0)`,
GEN_TAC THEN INDUCT_TAC THEN
ASM_REWRITE_TAC[ODD; EXP; ONE; ODD_MULT; NOT_SUC] THEN
CONV_TAC ITAUT);;
let EVEN_DOUBLE = prove
(`!n. EVEN(2 * n)`,
GEN_TAC THEN REWRITE_TAC[EVEN_MULT] THEN DISJ1_TAC THEN
PURE_REWRITE_TAC[BIT0_THM; BIT1_THM] THEN REWRITE_TAC[EVEN; EVEN_ADD]);;
let ODD_DOUBLE = prove
(`!n. ODD(SUC(2 * n))`,
REWRITE_TAC[ODD] THEN REWRITE_TAC[NOT_ODD; EVEN_DOUBLE]);;
let EVEN_EXISTS_LEMMA = prove
(`!n. (EVEN n ==> ?m. n = 2 * m) /\
(~EVEN n ==> ?m. n = SUC(2 * m))`,
INDUCT_TAC THEN REWRITE_TAC[EVEN] THENL
[EXISTS_TAC `0` THEN REWRITE_TAC[MULT_CLAUSES];
POP_ASSUM STRIP_ASSUME_TAC THEN CONJ_TAC THEN
DISCH_THEN(ANTE_RES_THEN(X_CHOOSE_TAC `m:num`)) THENL
[EXISTS_TAC `SUC m` THEN ASM_REWRITE_TAC[] THEN
REWRITE_TAC[MULT_2] THEN REWRITE_TAC[ADD_CLAUSES];
EXISTS_TAC `m:num` THEN ASM_REWRITE_TAC[]]]);;
let EVEN_EXISTS = prove
(`!n. EVEN n <=> ?m. n = 2 * m`,
GEN_TAC THEN EQ_TAC THEN DISCH_TAC THENL
[MATCH_MP_TAC(CONJUNCT1(SPEC_ALL EVEN_EXISTS_LEMMA)) THEN ASM_REWRITE_TAC[];
POP_ASSUM(CHOOSE_THEN SUBST1_TAC) THEN REWRITE_TAC[EVEN_DOUBLE]]);;
let ODD_EXISTS = prove
(`!n. ODD n <=> ?m. n = SUC(2 * m)`,
GEN_TAC THEN EQ_TAC THEN DISCH_TAC THENL
[MATCH_MP_TAC(CONJUNCT2(SPEC_ALL EVEN_EXISTS_LEMMA)) THEN
ASM_REWRITE_TAC[NOT_EVEN];
POP_ASSUM(CHOOSE_THEN SUBST1_TAC) THEN REWRITE_TAC[ODD_DOUBLE]]);;
let EVEN_ODD_DECOMPOSITION = prove
(`!n. (?k m. ODD m /\ (n = 2 EXP k * m)) <=> ~(n = 0)`,
MATCH_MP_TAC num_WF THEN X_GEN_TAC `n:num` THEN DISCH_TAC THEN
DISJ_CASES_TAC(SPEC `n:num` EVEN_OR_ODD) THENL
[ALL_TAC; ASM_MESON_TAC[ODD; EXP; MULT_CLAUSES]] THEN
FIRST_X_ASSUM(MP_TAC o GEN_REWRITE_RULE I [EVEN_EXISTS]) THEN
DISCH_THEN(X_CHOOSE_THEN `m:num` SUBST_ALL_TAC) THEN
FIRST_X_ASSUM(MP_TAC o SPEC `m:num`) THEN
ASM_CASES_TAC `m = 0` THEN ASM_REWRITE_TAC[MULT_EQ_0] THENL
[REWRITE_TAC[MULT_CLAUSES; LT] THEN
CONV_TAC(ONCE_DEPTH_CONV SYM_CONV) THEN
REWRITE_TAC[EXP_EQ_0; MULT_EQ_0; TWO; NOT_SUC] THEN MESON_TAC[ODD];
ALL_TAC] THEN
ANTS_TAC THENL
[GEN_REWRITE_TAC LAND_CONV [GSYM(el 2 (CONJUNCTS MULT_CLAUSES))] THEN
ASM_REWRITE_TAC[LT_MULT_RCANCEL; TWO; LT];
ALL_TAC] THEN
REWRITE_TAC[TWO; NOT_SUC] THEN REWRITE_TAC[GSYM TWO] THEN
ONCE_REWRITE_TAC[SWAP_EXISTS_THM] THEN
MATCH_MP_TAC MONO_EXISTS THEN X_GEN_TAC `p:num` THEN
DISCH_THEN(X_CHOOSE_THEN `k:num` STRIP_ASSUME_TAC) THEN
EXISTS_TAC `SUC k` THEN ASM_REWRITE_TAC[EXP; MULT_ASSOC]);;
(* ------------------------------------------------------------------------- *)
(* Cutoff subtraction, also defined recursively. (Not the HOL88 defn.) *)
(* ------------------------------------------------------------------------- *)
let SUB = new_recursive_definition num_RECURSION
`(!m. m - 0 = m) /\
(!m n. m - (SUC n) = PRE(m - n))`;;
let SUB_0 = prove
(`!m. (0 - m = 0) /\ (m - 0 = m)`,
REWRITE_TAC[SUB] THEN INDUCT_TAC THEN ASM_REWRITE_TAC[SUB; PRE]);;
let SUB_PRESUC = prove
(`!m n. PRE(SUC m - n) = m - n`,
GEN_TAC THEN INDUCT_TAC THEN ASM_REWRITE_TAC[SUB; PRE]);;
let SUB_SUC = prove
(`!m n. SUC m - SUC n = m - n`,
REPEAT INDUCT_TAC THEN ASM_REWRITE_TAC[SUB; PRE; SUB_PRESUC]);;
let SUB_REFL = prove
(`!n. n - n = 0`,
INDUCT_TAC THEN ASM_REWRITE_TAC[SUB_SUC; SUB_0]);;
let ADD_SUB = prove
(`!m n. (m + n) - n = m`,
GEN_TAC THEN INDUCT_TAC THEN ASM_REWRITE_TAC[ADD_CLAUSES; SUB_SUC; SUB_0]);;
let ADD_SUB2 = prove
(`!m n. (m + n) - m = n`,
ONCE_REWRITE_TAC[ADD_SYM] THEN MATCH_ACCEPT_TAC ADD_SUB);;
let SUB_EQ_0 = prove
(`!m n. (m - n = 0) <=> m <= n`,
REPEAT INDUCT_TAC THEN ASM_REWRITE_TAC[SUB_SUC; LE_SUC; SUB_0] THEN
REWRITE_TAC[LE; LE_0]);;
let ADD_SUBR2 = prove
(`!m n. m - (m + n) = 0`,
REWRITE_TAC[SUB_EQ_0; LE_ADD]);;
let ADD_SUBR = prove
(`!m n. n - (m + n) = 0`,
ONCE_REWRITE_TAC[ADD_SYM] THEN MATCH_ACCEPT_TAC ADD_SUBR2);;
let SUB_ADD = prove
(`!m n. n <= m ==> ((m - n) + n = m)`,
REWRITE_TAC[LE_EXISTS] THEN REPEAT STRIP_TAC THEN
ASM_REWRITE_TAC[ONCE_REWRITE_RULE[ADD_SYM] ADD_SUB] THEN
MATCH_ACCEPT_TAC ADD_SYM);;
let SUB_ADD_LCANCEL = prove
(`!m n p. (m + n) - (m + p) = n - p`,
INDUCT_TAC THEN ASM_REWRITE_TAC[ADD_CLAUSES; SUB_0; SUB_SUC]);;
let SUB_ADD_RCANCEL = prove
(`!m n p. (m + p) - (n + p) = m - n`,
ONCE_REWRITE_TAC[ADD_SYM] THEN MATCH_ACCEPT_TAC SUB_ADD_LCANCEL);;
let LEFT_SUB_DISTRIB = prove
(`!m n p. m * (n - p) = m * n - m * p`,
REPEAT GEN_TAC THEN CONV_TAC SYM_CONV THEN
DISJ_CASES_TAC(SPECL [`n:num`; `p:num`] LE_CASES) THENL
[FIRST_ASSUM(fun th -> REWRITE_TAC[REWRITE_RULE[GSYM SUB_EQ_0] th]) THEN
ASM_REWRITE_TAC[MULT_CLAUSES; SUB_EQ_0; LE_MULT_LCANCEL];
POP_ASSUM(CHOOSE_THEN SUBST1_TAC o REWRITE_RULE[LE_EXISTS]) THEN
REWRITE_TAC[LEFT_ADD_DISTRIB] THEN
REWRITE_TAC[ONCE_REWRITE_RULE[ADD_SYM] ADD_SUB]]);;
let RIGHT_SUB_DISTRIB = prove
(`!m n p. (m - n) * p = m * p - n * p`,
ONCE_REWRITE_TAC[MULT_SYM] THEN MATCH_ACCEPT_TAC LEFT_SUB_DISTRIB);;
let SUC_SUB1 = prove
(`!n. SUC n - 1 = n`,
REWRITE_TAC[ONE; SUB_SUC; SUB_0]);;
let EVEN_SUB = prove
(`!m n. EVEN(m - n) <=> m <= n \/ (EVEN(m) <=> EVEN(n))`,
REPEAT GEN_TAC THEN ASM_CASES_TAC `m <= n:num` THENL
[ASM_MESON_TAC[SUB_EQ_0; EVEN]; ALL_TAC] THEN
DISJ_CASES_TAC(SPECL [`m:num`; `n:num`] LE_CASES) THEN ASM_SIMP_TAC[] THEN
FIRST_ASSUM(MP_TAC o AP_TERM `EVEN` o MATCH_MP SUB_ADD) THEN
ASM_MESON_TAC[EVEN_ADD]);;
let ODD_SUB = prove
(`!m n. ODD(m - n) <=> n < m /\ ~(ODD m <=> ODD n)`,
REWRITE_TAC[GSYM NOT_EVEN; EVEN_SUB; DE_MORGAN_THM; NOT_LE] THEN
CONV_TAC TAUT);;
(* ------------------------------------------------------------------------- *)
(* The factorial function. *)
(* ------------------------------------------------------------------------- *)
let FACT = new_recursive_definition num_RECURSION
`(FACT 0 = 1) /\
(!n. FACT (SUC n) = (SUC n) * FACT(n))`;;
let FACT_LT = prove
(`!n. 0 < FACT n`,
INDUCT_TAC THEN ASM_REWRITE_TAC[FACT; LT_MULT] THEN
REWRITE_TAC[ONE; LT_0]);;
let FACT_LE = prove
(`!n. 1 <= FACT n`,
REWRITE_TAC[ONE; LE_SUC_LT; FACT_LT]);;
let FACT_NZ = prove
(`!n. ~(FACT n = 0)`,
REWRITE_TAC[GSYM LT_NZ; FACT_LT]);;
let FACT_MONO = prove
(`!m n. m <= n ==> FACT m <= FACT n`,
REPEAT GEN_TAC THEN
DISCH_THEN(X_CHOOSE_THEN `d:num` SUBST1_TAC o REWRITE_RULE[LE_EXISTS]) THEN
SPEC_TAC(`d:num`,`d:num`) THEN
INDUCT_TAC THEN REWRITE_TAC[ADD_CLAUSES; LE_REFL] THEN
REWRITE_TAC[FACT] THEN
MATCH_MP_TAC LE_TRANS THEN EXISTS_TAC `FACT(m + d)` THEN
ASM_REWRITE_TAC[] THEN
GEN_REWRITE_TAC LAND_CONV [GSYM(el 2 (CONJUNCTS MULT_CLAUSES))] THEN
REWRITE_TAC[LE_MULT_RCANCEL] THEN
REWRITE_TAC[ONE; LE_SUC; LE_0]);;
(* ------------------------------------------------------------------------- *)
(* More complicated theorems about exponential. *)
(* ------------------------------------------------------------------------- *)
let EXP_LT_0 = prove
(`!n x. 0 < x EXP n <=> ~(x = 0) \/ (n = 0)`,
REWRITE_TAC[GSYM NOT_LE; LE; EXP_EQ_0; DE_MORGAN_THM]);;
let LT_EXP = prove
(`!x m n. x EXP m < x EXP n <=> 2 <= x /\ m < n \/
(x = 0) /\ ~(m = 0) /\ (n = 0)`,
REPEAT GEN_TAC THEN
ASM_CASES_TAC `x = 0` THEN ASM_REWRITE_TAC[] THENL
[REWRITE_TAC[GSYM NOT_LT; TWO; ONE; LT] THEN
SPEC_TAC (`n:num`,`n:num`) THEN INDUCT_TAC THEN
REWRITE_TAC[EXP; NOT_SUC; MULT_CLAUSES; LT] THEN
SPEC_TAC (`m:num`,`m:num`) THEN INDUCT_TAC THEN
REWRITE_TAC[EXP; MULT_CLAUSES; NOT_SUC; LT_REFL; LT] THEN
REWRITE_TAC[ONE; LT_0]; ALL_TAC] THEN
EQ_TAC THENL
[CONV_TAC CONTRAPOS_CONV THEN
REWRITE_TAC[NOT_LT; DE_MORGAN_THM; NOT_LE] THEN
REWRITE_TAC[TWO; ONE; LT] THEN
ASM_REWRITE_TAC[SYM ONE] THEN
STRIP_TAC THEN ASM_REWRITE_TAC[EXP_ONE; LE_REFL] THEN
FIRST_ASSUM(X_CHOOSE_THEN `d:num` SUBST1_TAC o
REWRITE_RULE[LE_EXISTS]) THEN
SPEC_TAC(`d:num`,`d:num`) THEN INDUCT_TAC THEN
REWRITE_TAC[ADD_CLAUSES; EXP; LE_REFL] THEN
MATCH_MP_TAC LE_TRANS THEN EXISTS_TAC `1 * x EXP (n + d)` THEN
CONJ_TAC THENL
[ASM_REWRITE_TAC[MULT_CLAUSES];
REWRITE_TAC[LE_MULT_RCANCEL] THEN
DISJ1_TAC THEN UNDISCH_TAC `~(x = 0)` THEN
CONV_TAC CONTRAPOS_CONV THEN REWRITE_TAC[NOT_LE] THEN
REWRITE_TAC[ONE; LT]];
STRIP_TAC THEN
FIRST_ASSUM(X_CHOOSE_THEN `d:num` SUBST1_TAC o
REWRITE_RULE[LT_EXISTS]) THEN
SPEC_TAC(`d:num`,`d:num`) THEN INDUCT_TAC THEN