File: system.tex

package info (click to toggle)
hol88 2.02.19940316dfsg-5
  • links: PTS
  • area: main
  • in suites: bookworm
  • size: 65,816 kB
  • sloc: ml: 199,939; ansic: 9,666; sh: 6,913; makefile: 6,032; lisp: 2,747; yacc: 894; sed: 201; cpp: 87; awk: 5
file content (5951 lines) | stat: -rw-r--r-- 256,210 bytes parent folder | download | duplicates (11)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
\chapter{The HOL Logic in ML}\label{HOLsyschapter}

In this chapter, the concrete representation of the \HOL\ logic is described.
This involves describing the \ML\ functions that comprise the interface to the
logic (up to and including Section~\ref{avra_terms}); the quotation, printing
and parsing of logical terms (Section~\ref{quotation}); the representation of
theorems (Section~\ref{avra_theorems}); the representation of theories
(Section~\ref{theoryfns}); the basic \HOL\ theories that are built into the
\HOL\ system (Sections~\ref{HOL-theory} and \ref{rules}); the methods for
extending theories (throughout Section~\ref{HOL-ancestry} and in
Section~\ref{types-package}); and the \ML\ system functions concerning the
logic (Section~\ref{HOLflags}).  It is assumed that the reader is familiar
with \ML.  If  not, the introduction to \ML\ in {\sl Getting  Started with
HOL\/} in \TUTORIAL\ should be read first. 

The \HOL\ system provides \ML\ types \ml{type} and \ml{term} to represent
types and terms of the \HOL\ logic, as defined in Sections~\ref{types} and
\ref{terms}, respectively.\footnote{Care must be taken to avoid confusion
between ML types and types of the HOL logic: the ML type {\tt type} represents
types of the HOL logic in ML.} It also provides primitive \ML\ functions for
creating and manipulating values of these types.  The key idea of the \HOL\
system, due to Robin Milner\index{Milner, R.}, and discussed in this chapter,
is that theorems are represented as an abstract \ML\ type whose only
pre-defined values are axioms, and whose only operations are rules of
inference. This means that the only way to construct theorems in \HOL\ is
to apply rules of inference to axioms or existing theorems; hence the
consistency of the logic is preserved.

The purpose of the meta-language \ML\ is to provide a programming environment
in which to build theorem proving tools to assist in the construction of
proofs.  When the \HOL\ system is built, a range of useful theorems are
pre-proved and a set of tools pre-defined. The basic system thus offers a rich
initial environment; users can further enrich it by implementing their own
application specific tools and building their own application specific
theories.


\section{Lexical matters}
\label{HOL-lex}

\index{identifiers, in HOL logic@identifiers, in \HOL\ logic|(}
The name of a \HOL\  variable\index{variables, in HOL logic@variables, in \HOL\ logic!names of|(} 
can be any  \ML\ string, but the quotation
mechanism will parse  only names that are  
identifiers (see Section~\ref{ident} below).  
The use of non-identifiers as  variable names  is discouraged except
in special  circumstances  (for example, when  writing  
derived   rules  that  generate
variables with names that are guaranteed to be different from existing names).
The names of type 
variables\index{type variables, in HOL logic@type variables, in \HOL\ logic!names of} in the \HOL\ logic are strings
of \ml{*}s optionally followed by a number or an identifier (see
Section~\ref{tyvars} for examples).
The name of a type constant or a term constant of the \HOL\ logic can
be any identifier.  \ML\ identifiers are described in Part~\ref{MLpart}; the 
lexical structure of \HOL\ identifiers is identical, however the
description is repeated here for convenience. 
\index{identifiers, in HOL logic@identifiers, in \HOL\ logic|)}

\subsection{Identifiers}
\label{ident}

The structure of \HOL\ identifiers is partly programmable. The default
is that an identifier can be of two forms:
\begin{myenumerate}
\item A sequence of alphanumerics starting with a letter, 
where the default structure of an alphanumeric is that it is a letter, 
a digit, a prime (\ml{'}) or an underbar (\ty{\_}).
\item A special symbol chosen from the following list

{\small \begin{verbatim}
   **     ++     <--    <->    -->    ---    ><     >>

   >=     <==    <=>    ===    ==>    \/     //     /\

   !?     !!     !\     ?!     ??     ?\     :=     <>

   <-     <<     <=     --     ->     =>     ==
\end{verbatim}}

\end{myenumerate}

\noindent A letter is a member of the list:


\begin{hol}\begin{verbatim}
   a b c d e f g h i j k l m n o p q r s t u v w x y z

   A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
\end{verbatim}\end{hol}

\noindent \HOL\ is case-sensitive: upper and lower case letters are 
considered to be different. 

The function

\begin{boxed}\index{new_letter@\ml{new\_letter}|pin}
\begin{verbatim}
   new_letter : string -> void
\end{verbatim}\end{boxed}

\noindent makes a new character behave like a letter. For example:

\begin{hol}\begin{verbatim}
   new_letter `+`;;
\end{verbatim}\end{hol}

\noindent will make \ml{+int} and \ml{foo+bar} become allowable identifiers.
Failure occurs if the argument string is longer than one character.

The function

\begin{boxed}\index{is_letter@\ml{is\_letter}|pin}
\begin{verbatim}
   is_letter : string -> bool
\end{verbatim}\end{boxed}

\noindent tests if a unit string is a letter; it fails if the string has more
than one character.

Alphanumerics are, by default, letters or digits.  A digit is one of
{\small\verb%0%}, {\small\verb%1%}, {\small\verb%2%}, {\small\verb%3%},
{\small\verb%4%}, {\small\verb%5%}, {\small\verb%6%}, {\small\verb%7%},
{\small\verb%8%}, or {\small\verb%9%}. 
A {\it number\/} is a string of one or more digits.
The function

\begin{boxed}\index{new_alphanum@\ml{new\_alphanum}|pin}
\begin{verbatim}
   new_alphanum : string -> void
\end{verbatim}\end{boxed}
   
\noindent makes a new character behave like an alphanumeric. For example:

\begin{hol}\begin{verbatim}
   new_alphanum `+`;;
\end{verbatim}\end{hol}

\noindent will make \ml{foo+bar} and \ml{foo+} become allowable names (but not
\ml{+bar}). Failure occurs if the argument string is longer than one
character.

The function

\begin{boxed}\index{is_alphanum@\ml{is\_alphanum}|pin}
\begin{verbatim}
   is_alphanum : string -> bool
\end{verbatim}\end{boxed}

\noindent tests if a unit string is an alphanumeric; it fails if the string
has more than one character.

It is a consequence of the way lexical analysis is implemented that any
initial subsequence of a special symbol is also a special symbol (e.g. since
\ml{==>} is a special symbol, so are \ml{==} and \ml{=}). The function

\begin{boxed}\index{new_special_symbol@\ml{new\_special\_symbol}|pin}
\begin{verbatim}
   new_special_symbol : string -> bool
\end{verbatim}\end{boxed}
  
\noindent makes the argument string, and all its substrings, special symbols;
it fails if 
\begin{myenumerate}
\item the argument string is a single character, or 
\item it starts with a letter or an alphanumeric. 
\end{myenumerate}

\noindent For example:

\begin{hol}\begin{verbatim}
   new_special_symbol `===>>>`;;
\end{verbatim}\end{hol}

\noindent makes \ml{===>>>}, \ml{===>>}, \ml{===>} and \ml{===} 
into new special symbols 
(\ml{==} is already a special symbol because \ml{==>} is).

The function

\begin{boxed}\index{special_symbols@\ml{special\_symbols}|pin}
\begin{verbatim}
   special_symbols : void -> string list
\end{verbatim}\end{boxed}

\noindent  gives the list of currently declared special symbols\index{variables, in HOL logic@variables, in \HOL\ logic!names of|)} .


\subsubsection{Separators}

The separators used by the \HOL\ lexical analyser are (with ascii codes in
brackets):

\bigskip

space (32), carriage return (13), line feed (10), tab ({\verb%^%}I, 9), 
form feed ({\verb%^%}L, 12)



\subsection{Type variable names}
\label{tyvars}

The names of type variables in the \HOL\ logic are strings
of \ml{*}s optionally followed by a number or by an identifier; for example:

\begin{hol}\begin{verbatim}
   *   **   ***   *'   **'   **25   ***thing
\end{verbatim}\end{hol}


\section{Types}\index{types, in HOL logic@types, in \HOL\ logic}

The allowed types\index{type constraint!in HOL logic@in \HOL\ logic} depend on which type constants\index{type constants, in HOL logic@type constants, in \HOL\ logic} have been declared in the
current theory. See Section~\ref{theoryfns} for details of how such
declarations are made.


There are two primitive constructor\index{types, in HOL logic@types, in \HOL\ logic!constructors for}
\index{type constructors!in HOL logic@in \HOL\ logic} functions for values of type
\ml{type}:


\begin{boxed}
\index{function types, in HOL logic@function types, in \HOL\ logic!constructors for}\index{mk_vartype@\ml{mk\_vartype}|pin}
\index{mk_type@\ml{mk\_type}|pin}
\begin{verbatim}
   mk_vartype : string -> type
   mk_type    : (string # type list) -> type
\end{verbatim}\end{boxed}

The function \ml{mk\_vartype}
 constructs a type variable\index{type variables, in HOL logic@type variables, in \HOL\ logic!constructor for} with a given name;
it fails if the name is not an allowable type variable name (\ie\ not
a string of \ml{*}s followed by a number or identifier).  

The function \ml{mk\_type} constructs a compound type\index{compound types, in HOL logic@compound types, in \HOL\ logic!constructors for}
 from a string
representing the name of the type operator and a list of types representing the
arguments to   the  operator.     Function   types  $\sigma_1\fun\sigma_2$  
of the logic are
represented in \ML\ as though they were compound types
$(\sigma_1,\sigma_2)$\ml{fun} (in  Section~\ref{types}, however,
function types were not regarded as compound types).

The evaluation of 
\ml{mk\_type(`}$name$\ml{`,\ [}$\sigma_1$\ml{;}$\cdots$\ml{;}$\sigma_n$\ml{])}
fails if
\begin{myenumerate}
\item $name$ is not an identifier;
\item $name$ is not a type operator of the current theory;
\item $name$ is a type operator of the current theory, 
but its arity is not $n$.
\end{myenumerate}

For example, \ml{mk\_type(`bool`,[])}\index{truth values, in HOL logic@truth values, in \HOL\ logic}\index{bool, the type in HOL logic@\ml{bool}, the type in \HOL\ logic} evaluates to
an \ML\ value of type term representing the type \ty{bool} and
{\small\verb%mk_type(`fun`, [mk_type(`ind`,[]); mk_type(`bool`,[])])%}
evaluates to a value representing $\ty{ind}\fun\ty{bool}$.
(These types are introduced in Section~\ref{boolthy}).

There are two primitive destructor\index{types, in HOL logic@types, in
\HOL\ logic!destructors for}\index{type destructors, in HOL logic@type destructors, in \HOL\ logic}
 functions for values of type
\ml{type}:


\begin{boxed}
\index{function types, in HOL logic@function types, in \HOL\ logic!destructors for}
\index{dest_vartype@\ml{dest\_vartype}|pin}
\index{dest_type@\ml{dest\_type}|pin}
\begin{verbatim}
   dest_vartype : type -> string
   dest_type    : type -> (string # type list)
\end{verbatim}\end{boxed}

\noindent The function \ml{dest\_vartype}\index{type variables, in HOL logic@type variables, in \HOL\ logic!destructors for}\index{compound types, in HOL logic@compound types, in \HOL\ logic!destructors for}
 extracts the name of a type variable.
The function \ml{dest\_type}  destructs a  compound type  into the  name of the
type operator  and  a  list  of  the  argument  types;  \ml{dest\_vartype}  and
\ml{dest\_type} are  thus the  inverses of  \ml{mk\_vartype} and \ml{mk\_type},
respectively.  The destructors fail on arguments of the wrong form.


Types are printed\index{ type constraint, in HOL logic@\ml{:} (type constraint, in \HOL\ logic)}
\index{printing, in HOL logic@printing, in \HOL\ logic!of types} in the form \ml{":}$\ \cdots\ $\ml{"}
using the quotation syntax described in Section~\ref{quotation}.
For example, the \ML\ value of type \ml{type} representing
$\ty{ind}\fun(\ty{ind}\fun\ty{bool})$ would be printed
as \ml{":ind -> ind -> bool"}.

\section{Terms}
\label{avra_terms}

The four primitive kinds of  terms of the logic
are  described in  Section~\ref{terms}.  The
\ML\ functions for manipulating these are described in this section.  There are
also various derived terms that are described in Section~\ref{derived-terms}.

The allowed terms depend on which constants have been declared
in the current theory. See Section~\ref{theoryfns} for details
of how such declarations are made. 

There are four primitive constructor\index{variables, in HOL logic@variables, in \HOL\ logic!constructor for}\index{terms, in HOL logic@terms, in \HOL\ logic!constructors for}\index{term constructors, in HOL logic@term constructors, in \HOL\ logic}
functions for values of type
\ml{term}:

\begin{boxed}
\index{mk_var@\ml{mk\_var}|pin}
\begin{verbatim}
   mk_var : (string # type) -> term
\end{verbatim}\end{boxed}

\noindent\ml{mk\_var(}$x$\ml{,}$\sigma$\ml{)} evaluates to a variable with name
$x$ and type $\sigma$; it always succeeds.

\begin{boxed}
\index{mk_const@\ml{mk\_const}|pin}
\begin{verbatim}
   mk_const : (string # type) -> term
\end{verbatim}\end{boxed}


\noindent\ml{mk\_const(}$c$\ml{,}$\sigma$\ml{)} evaluates to a 
term representing
the constant\index{constants, in HOL logic@constants, in \HOL\ logic!constructor for} with name $c$ and type $\sigma$; it fails if:
\begin{myenumerate}
\item $c$ is not an allowable constant name;
\item $c$ is not the name of a constant in the current theory;
\item $\sigma$ is not an instance of the generic type of $c$
(the generic type of a constant is established when the constant is defined;
see Section~\ref{theoryfns}).
\end{myenumerate}

\begin{boxed}\index{mk_comb@\ml{mk\_comb}|pin}
\begin{verbatim}
   mk_comb : (term # term) -> term
\end{verbatim}\end{boxed}

\noindent\ml{mk\_comb(}$t_1$\ml{,}$t_2$\ml{)}\index{function application, in HOL logic@function application, in \HOL\ logic!constructor for} evaluates to a term 
representing the combination\index{combinations, in HOL logic@combinations, in \HOL\ logic!constructor for}
$t_1\ t_2$. It fails if:
\begin{myenumerate}
\item the type of $t_1$ does not have the form \ml{$\sigma'$->$\sigma$};
\item the type of $t_1$ has the form \ml{$\sigma'$->$\sigma$}, but the
type of $t_2$ is not equal to $\sigma'$.
\end{myenumerate}

\begin{boxed}
\index{mk_abs@\ml{mk\_abs}|pin}
\begin{verbatim}
   mk_abs : (term # term) -> term
\end{verbatim}\end{boxed}

\noindent\ml{mk\_abs(}$x$\ml{,}$t$\ml{)} evaluates to a term representing
the abstraction\index{function abstraction, in HOL logic@function abstraction, in \HOL\ logic!constructor for} $\lquant{x}t$; it fails if $x$ is not a variable.


There are four primitive destructor\index{term destructors, in HOL logic@term destructors, in \HOL\ logic}\index{variables, in HOL logic@variables, in \HOL\ logic!destructor for}\index{constants, in HOL logic@constants, in \HOL\ logic!destructor for}\index{combinations, in HOL logic@combinations, in \HOL\ logic!destructor for}\index{function abstraction, in HOL logic@function abstraction, in \HOL\ logic!destructor for}\index{function application, in HOL logic@function application, in \HOL\ logic!destructor for} functions on terms:

\begin{boxed}

\index{dest_var@\ml{dest\_var}|pin
}\index{dest_const@\ml{dest\_const}|pin}
\index{dest_comb@\ml{dest\_comb}|pin}
\index{dest_abs@\ml{dest\_abs}|pin}
\begin{verbatim}
   dest_var   : term -> (string # type)
   dest_const : term -> (string # type)
   dest_comb  : term -> (term # term)
   dest_abs   : term -> (term # term)
\end{verbatim}\end{boxed}

These are the inverses of \ml{mk\_var}, \ml{mk\_const},
\ml{mk\_comb} and \ml{mk\_abs}, respectively. They fail when applied
to terms of the wrong form. Other useful destructor functions are
\ml{rator}\index{rator@\ml{rator}},
\ml{rand}\index{rand@\ml{rand}},
\ml{bndvar}\index{bndvar@\ml{bndvar}},
\ml{body}\index{body@\ml{body}},
\ml{lhs}\index{lhs@\ml{lhs}} and
\ml{rhs}\index{rhs@\ml{rhs}}.
See \REFERENCE\ for details.

The function

\begin{boxed}\index{type_of@\ml{type\_of}|pin}
\begin{verbatim}
   type_of : term -> type
\end{verbatim}\end{boxed}

\noindent returns the type\index{types, in HOL logic@types, in \HOL\ logic!determination of} of a term. It could be defined (recursively)
in terms of the
destructors but is predefined for convenience.

Terms are printed in the form \ml{"}$\ \cdots\ $\ml{"}
using the quotation syntax\index{quotation, in HOL logic@quotation, in \HOL\ logic} described in Section~\ref{quotation}.
For example, the term representing 

\[ \uquant{x\ y}x<y \imp\equant{z}x+z = y \]

\noindent would be printed as:

\[ \ml{"!x y. x < y ==> ?z. x + z = y"} \]

Note that a colon\index{ type constraint, in HOL logic@\ml{:} (type constraint, in \HOL\ logic)}
 is used to distinguish type quotation from term quotation;
the former have the form \ml{":}$\ \cdots\ $\ml{"} and the latter have
 the form \ml{"}$\ \cdots\ $\ml{"}.


\section{Quotation}
\label{quotation}\label{gen-abs}\label{let}
\index{type checking, in HOL logic@type checking, in \HOL\ logic!of quotation syntax|(}

\index{quotation, in HOL logic@quotation, in \HOL\ logic|(}
\index{ type quotes, in ML@\ml{"":$\cdots$""} (type quotes, in \ML)|(}
\index{ term quotes, in ML@\ml{""$\cdots$""} (term quotes, in \ML)|(}
\HOL\ types and terms can be input\index{terms, in HOL logic@terms, in \HOL\ logic!input of} to the system in two ways:  by using
constructor functions, or by using {\it quotation\/}.  The 
former 
allows some terms to be built which cannot be  constructed using quotation.
For example, a term containing two variables with  the same  name but different
types, \eg\ the term  $x_{\ty{bool}}=(x_{\ty{num}}=1)$, 
 can be built only by using
constructors.

It would be tedious, however, to always have to input types and terms using the
constructor functions. The \HOL\ system, following \LCF\index{LCF@\LCF}, has a special 
quotation\index{quotation, in HOL logic@quotation, in \HOL\ logic!parser for}\index{parsing, of HOL logic@parsing, of \HOL\ logic!of quotation syntax} parser and
type checker that enables terms to be input using a fairly standard syntax.  The
\HOL\ printer also outputs types and terms using this syntax\index{printing, in HOL logic@printing, in \HOL\ logic!of quotation syntax}.

For example, the \ML\ expression {\small\verb%":bool->bool"%} denotes
exactly the same value (of \ML\ type {\small\verb%type%}) as

\begin{hol}\index{bool, the type in HOL logic@\ml{bool}, the type in \HOL\ logic}
\begin{verbatim}
   mk_type(`fun`,[mk_type(`bool`,[]);mk_type(`bool`,[])])
\end{verbatim}\end{hol}

\noindent and
{\small\verb%"\x.x+1"%}\index{function abstraction, in HOL logic@function abstraction, in \HOL\ logic} can be used instead of

\newpage %PBHACK

{\small\baselineskip\HOLSpacing\begin{verbatim}

   mk_abs
    (mk_var(`x`,mk_type(`num`,[])),
     mk_comb
     (mk_comb
      (mk_const
       (`+`,
        mk_type(`fun`,[mk_type(`num`,[]);
                       mk_type(`fun`,[mk_type(`num`,[]);
                                      mk_type(`num`,[])])])),
       mk_var(`x`, mk_type(`num`,[]))),
      mk_const(`1`, mk_type(`num`,[]))))
\end{verbatim}}

\index{type constraint!in HOL logic@in \HOL\ logic|(}It should be noted 
that there is no  explicit type  information in {\small\verb%"\x.x+1"%}.
The \HOL\ type checker knows that \ml{1} has type \ml{num} and  \ml{+} has type
\ml{num->(num->num)}.  From this information it can infer that both occurrences
of {\small\verb%x%} in {\small\verb%"\x.x+1"%} could have type
{\small\verb%num%}.  This  is  not  the  only  possible type  assignment;  for
example, the first occurrence of {\small\verb%x%} could have type \ml{bool} and
the second one have type {\small\verb%num%}.  In that  case there  would be two
{\it different\/} variables with name {\small\verb%x%}, namely
{\small\verb%x%}$_{\tt bool}$ and  {\small\verb%x%}$_{\tt num}$,  the second of
which is free.  In fact, as mentioned, 
the only  way to  construct a  term with this
second type assignment is  by using  constructors, since  the type checker uses
the heuristic that all variables in a term
 with the same name  have the  same type.   This is
illustrated in the following session.

\setcounter{sessioncount}{1}
\begin{session}\begin{verbatim}
#"x = (x = 1)";;
Badly typed application of:  "$= x"
   which has type:           ":num -> bool"
to the argument term:        "x = 1"
   which has type:           ":bool"

evaluation failed     mk_comb in quotation

#mk_eq
# (mk_var(`x`,mk_type(`bool`,[])), 
#  mk_eq
#   (mk_var(`x`,mk_type(`num`,[])),
#    mk_const(`1`,mk_type(`num`,[]))));;
"x = (x = 1)" : term
\end{verbatim}\end{session}

The quotation type checker was designed  and implemented  by Robin  Milner\index{Milner, R.}.  It
employs heuristics  like  the  one  above  to  infer  a  sensible  type for all
variables occurring in a term.  If there are not enough clues,  then the system
will complain with an error message.

To give the system a hint, types can be  explicitly indicated  by following any
subterm with a colon and then a type.  For example,
{\small\verb%"f(x:num):bool"%} will   type check   with   {\small\verb%f%}   and
{\small\verb%x%} getting types  {\small\verb%num->bool%} and {\small\verb%num%}
respectively.  If  there  are polymorphic  constants in  a term,  there must be
enough type  information  to uniquely  identify a  type instance  for each such
constant.  There is  also a  feature called  {\it sticky  types\/} that enables
variables to be given default types; this is described in
Section~\ref{stickytypes}.

The type checking\index{type checker, for HOL logic vs ML@type checker, for \HOL\ logic {\it vs} \ML} algorithm used for the \HOL\ logic differs from that used
for \ML. For example, the \ML\ expression {\small\verb%\x.x%}\index{terms, in HOL logic@terms, in \HOL\ logic!function abstraction}\index{function abstraction, in HOL logic@function abstraction, in \HOL\ logic!type checking of}\index{function abstraction, in HOL logic@function abstraction, in \HOL\ logic!syntax of}
 will get \ML\
type {\small\verb%*->*%}, but the \HOL\ term {\small\verb%"\x.x"%} will
fail to type check, as shown in the session below.

\setcounter{sessioncount}{1}
\begin{session}\begin{verbatim}
#"\x. x";;
Indeterminate types:  "x:?"

evaluation failed     types indeterminate in quotation
\end{verbatim}\end{session}

\noindent To get the term {\small\verb%\x.x%} 
to type check, the type of the variable \ml{x} must be  given explicitly, 
for example by writing {\small\verb%"\x:*.x"%}. 

\begin{session}\begin{verbatim}
#"\x:*. x";;
"\x. x" : term
\end{verbatim}\end{session}

\noindent This treatment of types within quotations is inherited from \LCF\index{LCF@\LCF}.
\index{type constraint!in HOL logic@in \HOL\ logic|)}
\index{ term quotes, in ML@\ml{""$\cdots$""} (term quotes, in \ML)|)}

\subsection{Type quotation}

\index{type variables, in HOL logic@type variables, in \HOL\ logic!constructor for}
\index{type constructors!in HOL logic@in \HOL\ logic}
\index{term constructors, in HOL logic@term constructors, in \HOL\ logic|(}
\index{terms, in HOL logic@terms, in \HOL\ logic!constructors for|(}
The table below shows \ML\ expressions for various kinds of type
quotations\index{quotation, in HOL logic@quotation, in \HOL\ logic!of types}.
The expressions in the same row are equivalent.

\bigskip


\begin{center}
\index{compound types, in HOL logic@compound types, in \HOL\ logic!constructors for}
\index{ type variables, in HOL logic@\ml{*,\,**,\,}$\ldots$ (type variables, in \HOL\ logic)}
\index{types, in HOL logic@types, in \HOL\ logic!constructors for}
\index{ function type operator, in HOL logic@\ml{->} (function type operator, in \HOL\ logic)}
\index{mk_vartype@\ml{mk\_vartype}}
\index{mk_type@\ml{mk\_type}}
\begin{tabular}{|l|l|l|} \hline
\multicolumn{3}{|c|}{ } \\
\multicolumn{3}{|c|}{\bf Types} \\
\multicolumn{3}{|c|}{ } \\
{\it Kind of type} & {\it \ML\ quotation} & 
{\it Constructor expression}  \\ \hline
 & & \\
Type variable &
{\small\verb%":*%}$\cdots${\small\verb%"%} & {\small\verb%mk_vartype(`*%}$\cdots${\small\verb%`)%}   \\ \hline
Type constant &
{\small\verb%":%}$op${\small\verb%"%} & {\small\verb%mk_type(`%}$op${\small\verb%`,[])%}   \\ \hline
Function type &
{\small\verb%":%}$\sigma_1${\small\verb%->%}$\sigma_2${\small\verb%"%} &
{\small\verb%mk_type(`fun`, [":%}$\sigma_1${\small\verb%";":%}$\sigma_2${\small\verb%"])%} \\ \hline
Compound type &
{\small\verb%":(%}$\sigma_1${\small\verb%,%} $\ldots$ {\small\verb%,%} $\sigma_n${\small\verb%)%}$op${\small\verb%"%} &
{\small\verb%mk_type(`%}$op${\small\verb%`, [":%}$\sigma_1${\small\verb%";%} $\ldots$ {\small\verb%;":%}$\sigma_n${\small\verb%"])%}
\\ \hline
\end{tabular}
\end{center}

\index{ type quotes, in ML@\ml{"":$\cdots$""} (type quotes, in \ML)|)}

\subsection{Term quotation}

\index{terms, in HOL logic@terms, in \HOL\ logic!syntax of|(}
Equivalent ways of inputting the four primitive kinds of term are shown in
the next table.

\bigskip

\begin{center}
\index{combinations, in HOL logic@combinations, in \HOL\ logic!quotation of}
\index{terms, in HOL logic@terms, in \HOL\ logic!primitive}
\index{terms, in HOL logic@terms, in \HOL\ logic!constructors for}
\index{quotation, in HOL logic@quotation, in \HOL\ logic!of primitive terms}
\index{function abstraction, in HOL logic@function abstraction, in \HOL\ logic!symbol for}
\index{function application, in HOL logic@function application, in \HOL\ logic!constructor for}
\index{function application, in HOL logic@function application, in \HOL\ logic!syntax of}
\index{variables, in HOL logic@variables, in \HOL\ logic!constructor for}
\index{variables, in HOL logic@variables, in \HOL\ logic!syntax of} 
\index{mk_var@\ml{mk\_var}}
\index{mk_const@\ml{mk\_const}}
\index{mk_comb@\ml{mk\_comb}}
\index{mk_abs@\ml{mk\_abs}}
\begin{tabular}{|l|l|l|} \hline
\multicolumn{3}{|c|}{ } \\
\multicolumn{3}{|c|}{\bf Primitive terms} \\
\multicolumn{3}{|c|}{ } \\
{\it Kind of term} & {\it \ML\ quotation} & 
{\it Constructor expression}  \\ \hline
 & & \\
Variable & {\small\verb%"%}$var${\small\verb%:%}$\sigma${\small\verb%"%} &
{\small\verb%mk_var(`%}$var${\small\verb%`,":%}$\sigma${\small\verb%")%} \\ \hline
Constant & {\small\verb%"%}$const${\small\verb%:%}$\sigma${\small\verb%"%} &
{\small\verb%mk_const(`%}$const${\small\verb%`,":%}$\sigma${\small\verb%")%} \\ \hline
Combination & {\small\verb%"%}$t_1\ t_2${\small\verb%"%} &
{\small\verb%mk_comb("%}$t_1${\small\verb%","%}$t_2${\small\verb%")%} \\ \hline
Abstraction & {\small\verb%"\%}$x${\small\verb%.%}$t${\small\verb%"%} &
{\small\verb%mk_abs("%}$x${\small\verb%","%}$t${\small\verb%")%} \\ \hline
\end{tabular}
\end{center}\index{type checking, in HOL logic@type checking, in \HOL\ logic!of quotation syntax|)}

\subsection{Special syntactic forms}
\label{derived-terms}

\index{type checking, in HOL logic@type checking, in \HOL\ logic!special forms in|(}
\index{quotation, in HOL logic@quotation, in \HOL\ logic!of non-primitive terms|(}
The \HOL\ quotation parser\index{quotation, in HOL logic@quotation, in \HOL\ logic!parser for} can translate
various standard logical
notations\index{parsing, of HOL logic@parsing, of \HOL\ logic!of standard notations} into primitive terms. For example, if {\small\verb%+%} has been
declared an infix\index{infixes, in HOL logic@infixes, in \HOL\ logic} (as explained in Section~\ref{theoryfns}), then
{\small\verb%"x+1"%} is translated to {\small\verb%"$+ x 1"%}. The escape
character {\small\verb%$%}\index{ escape, in HOL logic parser@{\small\verb+$+} (escape, in \HOL\ logic parser)}\index{declared constants, in HOL logic@declared constants, in \HOL\ logic}\index{infixes, in HOL logic@infixes, in \HOL\ logic}
 suppresses the infix behaviour of
{\small\verb%+%} and prevents the quotation parser getting confused.  In
general, {\small\verb%$%} can be used to suppress any special syntactic
behaviour a constant name\index{constants, in HOL logic@constants, in \HOL\ logic!supressing parsing behaviour of} might have. This is illustrated in the table
below, in which the terms in the column headed `{\it \ML\ quotation}' are
translated by the quotation parser to the corresponding terms in the column
headed `{\it Primitive term\/}'. Conversely, the terms in the latter
column are always printed in the form shown in the former one.
\label{cond}The \ML\ constructor expressions in the rightmost column
evaluate to the same values (of type {\small\verb%term%}) as the other
quotations in the same row.

\bigskip

\begin{center}
\index{choice operator, in HOL logic@choice operator, in \HOL\ logic!syntax of}
\index{ negation, in HOL logic@{\small\verb+~+} (negation, in \HOL\ logic)}
\index{ disjunction, in HOL logic@{\small\verb+\/+} (disjunction, in \HOL\ logic)}
\index{ conjunction, in HOL logic@{\small\verb+/\+} (conjunction, in \HOL\ logic)}
\index{ implication, in HOL logic@{\small\verb+==>+} (implication, in \HOL\ logic)}
\index{ equality, in HOL logic@\ml{=} (equality, in \HOL\ logic)}
\index{ universal quantifier, in HOL logic@{\small\verb+"!+} (universal quantifier, in \HOL\ logic)}
\index{ existential quantifier, in HOL logic@{\small\verb+?+} (existential quantifier, in \HOL\ logic)}
\index{ choice function, in HOL logic@{\small\verb+"@+} (choice function, in \HOL\ logic)}
\index{terms, in HOL logic@terms, in \HOL\ logic!non-primitive}
\index{terms, in HOL logic@terms, in \HOL\ logic!constructors for}
\index{conditional predicate, in HOL logic@conditional predicate, in \HOL\ logic}
\index{conditionals, in HOL logic@conditionals, in \HOL\ logic}
\index{conjunction, in HOL logic@conjunction, in \HOL\ logic!constructor for}
\index{disjunction, in HOL logic@disjunction, in \HOL\ logic!constructor for}
\index{equality, in HOL logic@equality, in \HOL\ logic!syntax of}
\index{negation, in HOL logic@negation, in \HOL\ logic!syntax of}
\index{negation, in HOL logic@negation, in \HOL\ logic!constructor for}
\index{existential quantifier, in HOL logic@existential quantifier, in \HOL\ logic!syntax of}
\index{universal quantifier, in HOL logic@universal quantifier, in \HOL\ logic!syntax of}
\index{implication, in HOL logic@implication, in \HOL\ logic!syntax of}
\index{mk_neg@\ml{mk\_neg}}
\index{mk_disj@\ml{mk\_disj}}
\index{mk_conj@\ml{mk\_conj}}
\index{mk_imp@\ml{mk\_imp}}
\index{mk_eq@\ml{mk\_eq}}
\index{mk_forall@\ml{mk\_forall}}
\index{mk_exists@\ml{mk\_exists}}
\index{mk_select@\ml{mk\_select}}
\index{mk_cond@\ml{mk\_cond}}
\index{mk_let@\ml{mk\_let}}
\index{conjunction, in HOL logic@conjunction, in \HOL\ logic!syntax of}
\begin{tabular}{|l|l|l|l|} \hline
\multicolumn{4}{|c|}{ } \\
\multicolumn{4}{|c|}{\bf Non-primitive terms} \\
\multicolumn{4}{|c|}{ } \\
{\it Kind of term} & {\it \ML\ quotation} & 
{\it Primitive term} &
{\it Constructor expression} \\ \hline
 & & & \\
Negation & {\small\verb%"~%}$t${\small\verb%"%} & {\small\verb%"$~ %}$t${\small\verb%"%} & {\small\verb%mk_neg("%}$t${\small\verb%")%} \\ \hline
Disjunction & {\small\verb%"%}$t_1${\small\verb%\/%}$t_2${\small\verb%"%} & {\small\verb%"$\/ %}$t_1\ t_2${\small\verb%"%} &
{\small\verb%mk_disj("%}$t_1${\small\verb%","%}$t_2${\small\verb%")%} \\ \hline
Conjunction & {\small\verb%"%}$t_1${\small\verb%/\%}$t_2${\small\verb%"%} & {\small\verb%"$/\ %}$t_1\ t_2${\small\verb%"%} &
{\small\verb%mk_conj("%}$t_1${\small\verb%","%}$t_2${\small\verb%")%} \\ \hline
Implication & {\small\verb%"%}$t_1${\small\verb%==>%}$t_2${\small\verb%"%} & {\small\verb%"$==> %}$t_1\ t_2${\small\verb%"%} &
{\small\verb%mk_imp("%}$t_1${\small\verb%","%}$t_2${\small\verb%")%} \\ \hline
Equality & {\small\verb%"%}$t_1${\small\verb%=%}$t_2${\small\verb%"%} & {\small\verb%"$= %}$t_1\ t_2${\small\verb%"%} &
{\small\verb%mk_eq("%}$t_1${\small\verb%","%}$t_2${\small\verb%")%} \\ \hline
$\forall$-quantification & {\small\verb%"!%}$x${\small\verb%.%}$t${\small\verb%"%} &
{\small\verb%"$!(\%}$x${\small\verb%.%}$t${\small\verb%)"%} & {\small\verb%mk_forall("%}$x${\small\verb%","%}$t${\small\verb%")%} \\ \hline
$\exists$-quantification & {\small\verb%"?%}$x${\small\verb%.%}$t${\small\verb%"%} &
{\small\verb%"$?(\%}$x${\small\verb%.%}$t${\small\verb%)"%} & {\small\verb%mk_exists("%}$x${\small\verb%","%}$t${\small\verb%")%} \\ \hline
$\hilbert$-term & {\small\verb%"@%}$x${\small\verb%.%}$t${\small\verb%"%} &
{\small\verb%"$@(\%}$x${\small\verb%.%}$t${\small\verb%)"%} & {\small\verb%mk_select("%}$x${\small\verb%","%}$t${\small\verb%")%} \\ \hline
Conditional\index{COND@\ml{COND}} & {\small\verb%"(%}$t${\small\verb%=>%}$t_1${\small\verb%|%}$t_2${\small\verb%)"%} &
{\small\verb%"COND %}$t\ t_1\ t_2${\small\verb%"%} & {\small\verb%mk_cond("%}$t${\small\verb%","%}$t_1${\small\verb%","%}$t_2${\small\verb%")%}
 \\ \hline
{\small\verb%let%}-expression & {\small\verb%"let %}$x${\small\verb%=%}$t_1${\small\verb% in %}$t_2${\small\verb%"%} &
{\small\verb%"LET(\%}$x${\small\verb%.%}$t_2${\small\verb%)%}$t_1${\small\verb%"%} &
{\small\verb%mk_let("\%}$x${\small\verb%.%}$t_1${\small\verb%","%}$t_2${\small\verb%")%} \\ \hline
\end{tabular}
\end{center}

\bigskip

There are constructors, destructors and indicators for all the
obvious constructs. (Indicators, \eg\ \ml{is\_neg}, return truth
values indicating whether or not a term belongs to the syntax
class in question.) In addition to the constructors listed in the table there
are constructors for pairs and lists, namely
\ml{mk\_pair}\index{mk_pair@\ml{mk\_pair}},
\ml{mk\_cons}\index{mk_cons@\ml{mk\_cons}} and
\ml{mk\_list}\index{mk_list@\ml{mk\_list}} (see \REFERENCE).
The constants {\small\verb%COND%}\index{COND@\ml{COND}} and {\small\verb%LET%} are explained in
Sections~\ref{conditionals} and \ref{let-exp}, respectively.  
The constants {\small\verb%\/%}\index{disjunction, in HOL logic@disjunction, in \HOL\ logic!syntax of}, {\small\verb%/\%},
{\small\verb%==>%} and {\small\verb%=%} are examples of {\it infixes\/}
and represent $\vee$, $\wedge$, $\imp$ and equality, respectively. If
$c$ is declared to be an infix, then the \HOL\ parser will translate
{\small\verb%"%}$t_1\ c\ t_2${\small\verb%"%} to 
{\small\verb%"$%}$c\ t_1\ t_2${\small\verb%"%}.  

The constants {\small\verb%!%}, {\small\verb%?%} and {\small\verb%@%} are examples
of \label{binder} {\it binders\/}\index{binders, in HOL logic@binders, in \HOL\ logic}
 and represent $\forall$, $\exists$ and $\hilbert$, respectively.  If $c$ 
is declared to be a binder, then the
\HOL\ parser will translate {\small\verb%"%}$c\ x${\small\verb%.%}$t${\small\verb%"%} to the combination
{\small\verb%"$%}$c${\small\verb%(\%}$x${\small\verb%.%}$t${\small\verb%)"%}
(\ie\ the application of the constant $c$ to the representation of
the abstraction $\lquant{x}t$)\index{ function abstraction binder, in HOL logic@{\small\verb+\+} (function abstraction binder, in \HOL\ logic)}.

In addition to the kinds of terms in the tables above, 
the parser also supports the following syntactic abbreviations.


\begin{center}

\index{variables, in HOL logic@variables, in \HOL\ logic!multiple bound}
\index{list_mk_comb@\ml{list\_mk\_comb}|pin} 
\index{list_mk_abs@\ml{list\_mk\_abs}|pin} 
\index{list_mk_forall@\ml{list\_mk\_forall}|pin} 
\index{list_mk_exists@\ml{list\_mk\_exists}|pin}
\index{combinations, in HOL logic@combinations, in \HOL\ logic!abbreviation for multiple} 
\index{existential quantifier, in HOL logic@existential quantifier, in \HOL\ logic!abbreviation for multiple}
\index{universal quantifier, in HOL logic@universal quantifier, in \HOL\ logic!abbreviation for multiple}
\begin{tabular}{|l|l|l|} \hline
\multicolumn{3}{|c|}{ } \\
\multicolumn{3}{|c|}{\bf Syntactic abbreviations} \\
\multicolumn{3}{|c|}{ } \\
{\it Abbreviated term} & {\it Meaning} & 
{\it Constructor expression} \\ \hline
 & &  \\
{\small\verb%"%}$t\ t_1 \cdots t_n${\small\verb%"%} &
{\small\verb%"(%}$\cdots${\small\verb%(%}$t\ t_1${\small\verb%)%}$\cdots t_n${\small\verb%)"%} &
{\small\verb%list_mk_comb("%}$t${\small\verb%",["%}$t_1${\small\verb%"; %}$\ldots${\small\verb% ;"%}$t_n${\small\verb%"])%} \\ \hline
{\small\verb%"\%}$x_1\cdots x_n${\small\verb%.%}$t${\small\verb%"%} &
{\small\verb%"\%}$x_1${\small\verb%. %}$\cdots${\small\verb% \%}$x_n${\small\verb%.%}$t${\small\verb%"%} &
{\small\verb%list_mk_abs(["%}$x_1${\small\verb%"; %}$\ldots${\small\verb% ;"%}$x_n${\small\verb%"],"%}$t${\small\verb%")%}
\\ \hline
{\small\verb%"!%}$x_1\cdots x_n${\small\verb%.%}$t${\small\verb%"%} &
{\small\verb%"!%}$x_1${\small\verb%. %}$\cdots${\small\verb% !%}$x_n${\small\verb%.%}$t${\small\verb%"%} &
{\small\verb%list_mk_forall(["%}$x_1${\small\verb%"; %}$\ldots${\small\verb% ;"%}$x_n${\small\verb%"],"%}$t${\small\verb%")%} 
\\ \hline
{\small\verb%"?%}$x_1\cdots x_n${\small\verb%.%}$t${\small\verb%"%} &
{\small\verb%"?%}$x_1${\small\verb%. %}$\cdots${\small\verb% ?%}$x_n${\small\verb%.%}$t${\small\verb%"%} &
{\small\verb%list_mk_exists(["%}$x_1${\small\verb%"; %}$\ldots${\small\verb% ;"%}$x_n${\small\verb%"],"%}$t${\small\verb%")%} \\ 
\hline
\end{tabular}
\end{center}

\noindent There are also constructors
\ml{list\_mk\_conj}\index{list_mk_conj@\ml{list\_mk\_conj}},
\ml{list\_mk\_disj}\index{list_mk_disj@\ml{list\_mk\_disj}},
\ml{list\_mk\_imp}\index{list_mk_imp@\ml{list\_mk\_imp}} and
\ml{list\_mk\_pair}\index{list_mk_pair@\ml{list\_mk\_pair}}
for conjunctions, disjunctions, implications and tuples respectively.
The corresponding destructor functions are called \ml{strip\_comb}, \etc,
except that there is no \ml{strip\_conj} or \ml{strip\_disj}. Instead there are
functions called \ml{conjuncts} and \ml{disjuncts}, but it should be noted
that these are not proper inverses of \ml{list\_mk\_conj} and
\ml{list\_mk\_disj}.
\index{term constructors, in HOL logic@term constructors, in \HOL\ logic|)}
\index{terms, in HOL logic@terms, in \HOL\ logic!constructors for|)}


\section{Syntax for restricted quantification}\label{res-quant}
\index{types, in HOL logic@types, in \HOL\ logic!dependent}
\index{dependent types in HOL logic@dependent types in \HOL\ logic}
\index{quantifiers!restricted}

Syntactic support for restricted quantification and abstraction is 
provided. This follows a suggestion discussed at the Second \HOL\ Users
Meeting and implements a method of simulating subtypes and dependent
types with predicates. 

Currently no derived rules are provided to support this notation, so
any inferences will need to work on the underlying semantic
representation.

The new syntax automatically translates as follows:

\begin{hol}
{\small\verb%   \%}$v${\small\verb%::%}$P${\small\verb%.%}$B${\small\verb%    <---->   RES_ABSTRACT %}$P${\small\verb% (\%}$v${\small\verb%.%}$B${\small\verb%)%}\\
{\small\verb%   !%}$v${\small\verb%::%}$P${\small\verb%.%}$B${\small\verb%    <---->   RES_FORALL   %}$P${\small\verb% (\%}$v${\small\verb%.%}$B${\small\verb%)%}\\
{\small\verb%   ?%}$v${\small\verb%::%}$P${\small\verb%.%}$B${\small\verb%    <---->   RES_EXISTS   %}$P${\small\verb% (\%}$v${\small\verb%.%}$B${\small\verb%)%}\\
{\small\verb%   @%}$v${\small\verb%::%}$P${\small\verb%.%}$B${\small\verb%    <---->   RES_SELECT   %}$P${\small\verb% (\%}$v${\small\verb%.%}$B${\small\verb%)%}
\end{hol}

Anything can be written between the binder and `\ml{::}' that can be
written between the binder and `\ml{.}`. See the examples below.

The flag \ml{print\_restrict} has default \ml{true}, but if set to \ml{false}
will disable the pretty printing. This is useful for seeing what the semantics
of particular restricted abstractions are.

The constants \ml{RES\_ABSTRACT}, \ml{RES\_FORALL}, \ml{RES\_EXISTS} and 
\ml{RES\_SELECT} are
defined in the theory \ml{bool} by:


\begin{hol}\index{RES_FORALL@\ml{RES\_FORALL}}
\index{RES_EXISTS@\ml{RES\_EXISTS}}
\index{RES_SELECT@\ml{RES\_SELECT}}
\index{RES_ABSTRACT@\ml{RES\_ABSTRACT}}\begin{verbatim}
   RES_ABSTRACT P B  =  \x:*. (P x => B x | ARB:**)

   RES_FORALL   P B  =  !x:*. P x ==> B x

   RES_EXISTS   P B  =  ?x:*. P x /\ B x

   RES_SELECT   P B  =  @x:*. P x /\ B x
\end{verbatim}\end{hol}

\noindent where the constant \ml{ARB}\index{ARB@\ml{ARB}},
which is also defined in the theory \ml{bool}, has the definition:

\begin{hol}\begin{verbatim}
   ARB  =  @x:*. T
\end{verbatim}\end{hol}

User-defined binders can also have restricted forms, which are set up
with the function:

\begin{boxed}\index{associate_restriction@\ml{associate\_restriction}|pin}
\begin{verbatim}
   associate_restriction : (string # string) -> *
\end{verbatim}\end{boxed}


\noindent If \m{c} is the name
of a binder and \ml{RES\_}\m{c} is the name of a suitable constant (which
must be explicitly defined), then executing:

\begin{hol}
{\small\verb%   associate_restriction(`%}$c${\small\verb%`, `RES_%}$c${\small\verb%`)%}
\end{hol}

\noindent will cause the parser and pretty-printer to support:

\begin{hol}
{\small\verb%   %}$c$ $v${\small\verb%::%}$P${\small\verb%. %}$B${\small\verb%    <---->   RES_%}$c$ $P${\small\verb% (\%}$v${\small\verb%. %}$B${\small\verb%)%}
\end{hol}

\noindent Note that associations between user defined binders and their
restrictions are not stored in theory files, so they have to be set up
for each \HOL\ session (e.g. with a {\small\verb%hol-init.ml%}
initialization file -- see Section~\ref{hol-init}).

Here is an example session:

\setcounter{sessioncount}{1}
\begin{session}\begin{verbatim}
#"!x y::P. x<y";;
"!x y :: P. x < y" : term

#set_flag(`print_restrict`, false);;
true : bool

#"!x y::P. x<y";;
"RES_FORALL P(\x. RES_FORALL P(\y. x < y))" : term

#"?(x,y) p::(\(m,n).m<n). p=(x,y)";;
"RES_EXISTS
 (\(m,n). m < n)
 (\(x,y). RES_EXISTS(\(m,n). m < n)(\p. p = x,y))"
: term

#"\x y z::P.[0;x;y;z]";;
"RES_ABSTRACT P(\x. RES_ABSTRACT P(\y. RES_ABSTRACT P(\z. [0;x;y;z])))"
: term
\end{verbatim}\end{session}

A conversion that rewrites away the constants \ml{RES\_ABSTRACT},
\ml{RES\_FORALL}, \ml{RES\_EXISTS} and \ml{RES\_SELECT} is:

\begin{hol}\begin{verbatim}
   let RESTRICT_CONV =
	(PURE_REWRITE_CONV [definition `bool` `RES_ABSTRACT`;
                            definition `bool` `RES_FORALL`;
                            definition `bool` `RES_EXISTS`;
                            definition `bool` `RES_SELECT`])
        THENC (DEPTH_CONV BETA_CONV)
\end{verbatim}\end{hol}

\noindent This is a bit unsatisfactory, as is shown by the artificial
example below:

\setcounter{sessioncount}{1}
\begin{session}\begin{verbatim}
#let t = "!x y::P.?f:num->num::Q. f(@n::R.T) = (x+y)";;
t = "!x y :: P. ?f :: Q. f(@n :: R. T) = x + y" : term

#RESTRICT_CONV t;;
|- (!x y :: P. ?f :: Q. f(@n :: R. T) = x + y) =
   (!x. P x ==> (!x'. P x' ==> (?x. Q x /\ (x(@x. R x /\ T) = x + x'))))
\end{verbatim}\end{session}

The variable $x$ in the definitions of the constants
\ml{RES\_ABSTRACT}, \ml{RES\_FORALL}, \ml{RES\_EXISTS} and
\ml{RES\_SELECT} gets confused with the variable in the supplied term.
This can be avoided by changing \ml{RESTRICT\_CONV} to perform
explicit alpha-conversion. For example, by implementing a conversion:

\begin{hol}
{\small\verb%   RES_FORALL %}$P${\small\verb% (\%}$v$\ml{.}$B$\ml{[}$v$\ml{])  ---->  !}$v$\ml{. }$P$ $v$\ml{ ==> }$B$\ml{[}$v$\ml{]}
\end{hol}

\noindent Dealing with the case when
$v$ is a variable structure is also desirable. For example:

\begin{session}\begin{verbatim}
#let t1 = "!(m,n)::P. m<n";;
t1 = "!(m,n) :: P. m < n" : term

#RESTRICT_CONV t1;;
|- (!(m,n) :: P. m < n) = (!x. P x ==> (\(m,n). m < n)x)

\end{verbatim}\end{session}

\noindent If anyone writes the desired conversions please let us know!

Here is an example of a user-defined restriction:

\begin{session}\begin{verbatim}
#new_binder_definition(`DURING`, "DURING(p:num#num->bool) = $!p");;
|- !p. $DURING p = $! p

#"DURING x::(m,n). p x";;
no restriction constant associated with DURING
skipping: x " ;; parse failed  

#new_definition
# (`RES_DURING`, "RES_DURING(m,n)p = !x. m<=x /\ x<=n ==> p x");;
|- !m n p. RES_DURING(m,n)p = (!x. m <= x /\ x <= n ==> p x)

#associate_restriction(`DURING`,`RES_DURING`);;
() : void

#"DURING x::(m,n). p x";;
"DURING x :: (m,n). p x" : term

#set_flag(`print_restrict`,false);;
true : bool

#"DURING x::(m,n). p x";;
"RES_DURING(m,n)(\x. p x)" : term
\end{verbatim}\end{session}




\subsection{Conditionals}
\label{conditionals}

A conditional\index{terms, in HOL logic@terms, in \HOL\ logic!conditional}\index{conditional predicate, in HOL logic@conditional predicate, in \HOL\ logic}\index{conditionals, in HOL logic@conditionals, in \HOL\ logic}
 term \ml{"$t_1\ $=>$\ t_2\ $|$\ t_3$"} means 
`if $t_1$ then $t_2$ else
$t_3$' and abbreviates the application 
\ml{"COND\ $t_1\ t_2\ t_3$"}, where \ml{COND}\index{COND@\ml{COND}}\index{conditionals, in HOL logic@conditionals, in \HOL\ logic}
is a predefined
constant of the theory \ml{bool} (see Section~\ref{boolthy}). 
An iterated conditional

\[
t_{11}\ \ml{=>}\ t_{12}\ \ml{|}\ 
t_{21}\ \ml{=>}\ t_{22}\ \ml{|}\ 
\ \ldots\ \ml{|}\ 
t_{n1}\ \ml{=>}\ t_{n2}\ \ml{|}\ t_{n3}
\]

\noindent translates\index{parsing, of HOL logic@parsing, of \HOL\ logic!of conditionals} to:

\[
t_{11}\ \ml{=>}\ t_{12}\ \ml{|}\ 
(t_{21}\ \ml{=>}\ t_{22}\ \ml{|}\ 
\ \ldots\ \ml{|}\ 
(t_{n1}\ \ml{=>}\ t_{n2}\ \ml{|}\ t_{n3})\ \ldots\ )
\]

\noindent which, in turn, abbreviates:

\[\ml{COND}\ t_{11}\ t_{12}\ \ml{(COND}\ t_{21}\ t_{22}\ \ldots\ 
\ml{(COND}\ t_{n1}\ t_{n2}\ t_{n3}\ml{)}\ \ldots\ \ml{)}\]

\subsection{Paired abstractions}
\label{HOL-varstruct}
\index{pairs, in HOL logic@pairs, in \HOL\ logic!in abstractions|(}
\index{UNCURRY@\ml{UNCURRY}|(}

\index{terms, in HOL logic@terms, in \HOL\ logic!pair|(}
\index{parsing, of HOL logic@parsing, of \HOL\ logic!of quotation syntax|(}
\index{function abstraction, in HOL logic@function abstraction, in \HOL\ logic!paired|(}
\index{function abstraction, in HOL logic@function abstraction, in \HOL\ logic!uncurrying, in paired|(}
The quotation parser\index{parsing, of HOL logic@parsing, of \HOL\ logic!of function abstractions}\index{function abstraction, in HOL logic@function abstraction, in \HOL\ logic!abbreviation for multiple}\index{terms, in HOL logic@terms, in \HOL\ logic!function abstraction}
 will convert
{\small\verb%"\(%}$x_1${\small\verb%,%}$x_2${\small\verb%).%}$t${\small\verb%"%}
to {\small\verb%"UNCURRY(\%}$x_1\ x_2${\small\verb%.%}$t${\small\verb%)"%},
where the constant {\small\verb%UNCURRY%} 
is defined by:

\begin{hol}\begin{verbatim}
   UNCURRY f (x,y)  =  f x y
\end{verbatim}\end{hol}

\noindent See Section~\ref{prod} for more details and an explanation
of pair terms \ml{($t_1$,$t_2$)}.
The transformation is done recursively so that, for example,


\begin{hol}\begin{alltt}
   "\verb%\%(\m{x\sb{1}},\m{x\sb{2}},\m{x\sb{3}}).\m{t}"
\end{alltt}\end{hol}

\noindent is converted to

\begin{hol}\begin{alltt}
   "UNCURRY \verb%\%\m{x\sb{1}}.UNCURRY(\verb%\%\m{x\sb{2}},\m{x\sb{3}}.\m{t}))"
\end{alltt}\end{hol}

\noindent More generally,
the quotation parser repeatedly applies the transformation:

\begin{hol}\begin{alltt}
   "\verb%\%(\m{v\sb{1}},\m{v\sb{2}}).\m{t}"\m{\quad \leadsto\quad }"UNCURRY(\verb%\%\m{v\sb{1}}.\verb%\%\m{v\sb{2}}.\m{t})"
\end{alltt}\end{hol}

\noindent until no more variable structures remain. For example:

\begin{flushleft}
\begin{tabular}{@{}ll}
{\small\verb%   "\(%}$x${\small\verb%,%}$y${\small\verb%).%}$t${\small\verb%"%}
&$\leadsto\ \ ${\small\verb%"UNCURRY(\%}$x\
y${\small\verb%.%}$t${\small\verb%)"%}\\
{\small\verb%   "\(%}$x_1${\small\verb%,%}$x_2${\small\verb%,%}$\ldots${\small\verb%,%}$x_n${\small\verb%).%}$t${\small\verb%"%}
&$\leadsto\ \ ${\small\verb%"UNCURRY(\%}$x_1${\small\verb%.\(%}$x_2${\small\verb%,%}$\ldots${\small\verb%,%}$x_n${\small\verb%).%}$t${\small\verb%)"%}\\
{\small\verb%   "\((%}$x_1${\small\verb%,%}$\ldots${\small\verb%,%}$x_n${\small\verb%),%}$y_1${\small\verb%,%}$\ldots${\small\verb%,%}$y_m${\small\verb%).%}t{\small\verb%"%}
&$\leadsto\ \ ${\small\verb%"UNCURRY(\(%}$x_1${\small\verb%,%}$\ldots${\small\verb%,%}$x_n${\small\verb%).\(%}$y_1${\small\verb%,%}$\ldots${\small\verb%,%}$y_m${\small\verb%).%}$t${\small\verb%)"%}\\
\end{tabular}
\end{flushleft}

\index{parsing, of HOL logic@parsing, of \HOL\ logic!of quotation syntax|)} 
The \HOL\ top-level printer\index{printing, in HOL logic@printing, in \HOL\ logic!of function abstractions}
 inverts these transformations if the 
flag\footnote{See Section~\ref{flags} for details of \ML\ flags.}
\ml{print\_uncurry} is \ml{true}. For example:

\setcounter{sessioncount}{1}
\begin{session}\begin{verbatim}
#"\((a,b),(x,y)). (a+b) < (x*y)";;
"\((a,b),x,y). (a + b) < (x * y)" : term

#set_flag(`print_uncurry`,false);;
true : bool

#"\((a,b),(x,y)). (a+b) < (x*y)";;
"UNCURRY(UNCURRY(\a b. UNCURRY(\x y. (a + b) < (x * y))))" : term
\end{verbatim}\end{session}

Note that a variable structure like \ml{"(x,y)"} in 
{\small\verb%"\(x,y).x+y"%} 
is not a subterm
of the abstraction\index{function abstraction, in HOL logic@function abstraction, in \HOL\ logic!subterms of} in which it occurs; it disappears on parsing\index{binders, in HOL logic@binders, in \HOL\ logic!parsing of}\index{parsing, of HOL logic@parsing, of \HOL\ logic!of binders}. This can 
lead to unexpected errors (accompanied by obscure error messages).  
For example:

\begin{session}\begin{verbatim}
#"\(x,y).x+y";;
"\(x,y). x + y" : term

#let p = "(x:num,y:num)";;
p = "x,y" : term

#"\^p.x+y";;
evaluation failed     mk_abs in quotation 
\end{verbatim}\end{session}

Furthermore, if the type checker complains, it may print out
diagnostic messages referring to the transformed term:

\begin{session}\begin{verbatim}
#"\(x,y). x+1";;
Indeterminate types:
    "UNCURRY:(num -> (?1 -> num)) -> (num # ?2 -> num)"

evaluation failed     types indeterminate in quotation 
\end{verbatim}\end{session}

If $b$ is a binder, then
\ml{"}$b$\ml{(}$x_1${\small\verb%,%}$x_2${\small\verb%).%}$t${\small\verb%"%}
is parsed as
{\small\verb%"$%}$b${\small\verb%(\(%}$x_1${\small\verb%,%}$x_2${\small\verb%).%}$t${\small\verb%)"%}, and hence transformed as above.
For example, {\small\verb%"!(x,y).x>y"%} parses to
{\small\verb%"$!(UNCURRY(\x.\y.$> x y))"%} (where {\small{\tt >}} is an infixed
constant of the theory {\small\verb%num%} meaning `is greater than')\index{function abstraction, in HOL logic@function abstraction, in \HOL\ logic!paired|)}. 
\index{function abstraction, in HOL logic@function abstraction, in \HOL\ logic!uncurrying, in paired|)}\index{pairs, in HOL logic@pairs, in \HOL\ logic!in abstractions|)}\index{terms, in HOL logic@terms, in \HOL\ logic!pair|)}\index{UNCURRY@\ml{UNCURRY}|)}


%A good exercise for the reader would be to understand why this use 
%of \ml{UNCURRY} supports the intuitive meaning suggested by the surface
%notation.

Applications of paired abstraction to tuples can be $\beta$-reduced using
\ml{PAIRED\_BETA\_CONV} (see Section~\ref{genbeta}).

\subsection{{\tt let}-terms}
\label{let-exp}


The quotation parser\index{parsing, of HOL logic@parsing, of \HOL\ logic!of let-terms@of \ml{let}-terms}
 accepts \ml{let}-terms\index{terms, in HOL logic@terms, in \HOL\ logic!let-@\ml{let}-}\index{let-terms, in HOL logic@\ml{let}-terms, in \HOL\ logic!as abbreviations} superficially similar to those in 
\ML. For example, the following terms are allowed:

\begin{hol}\begin{verbatim}
   "let x = 1 and y = 2 in x+y"

   "let f(x,y) = (x*x)+(y*y) and a = 20*20 and b = 50*49 in f(a,b)"
\end{verbatim}\end{hol}

As with paired abstractions, \ml{let}-terms are actually abbreviations for 
ordinary  terms which are specially supported by the parser and pretty printer.
The constant \ml{LET}\index{LET@\ml{LET}} is defined (in the theory \ml{bool}) by:

\begin{hol}\index{function abstraction, in HOL logic@function abstraction, in \HOL\ logic!relation to let-terms@relation to \ml{let}-terms}
\begin{verbatim}
   LET = (\f x. f x)
\end{verbatim}\end{hol}

\noindent and is used to encode \ml{let}-terms in the logic. The parser
repeatedly applies the transformations:

\bigskip

\noindent
{\small\begin{tabular}{@{}ll}
{\small\verb%   "let %}$f\ v_1\ \ldots\ v_n${\small\verb% = %}$t_1${\small\verb% in
%}$t_2${\small\verb%"%}
&$\leadsto\ \ ${\small\verb%"LET(\%}$f${\small\verb%.%}$t_2$
{\small\verb%)(\%}$v_1\ \ldots\ v_n${\small\verb%.%}$t_1${\small\verb%)"%}\\
{\small\verb%   "let (%}$v_1${\small\verb%,%}$\ldots${\small\verb%,%}$v_n${\small\verb%) =
%}$t_1${\small\verb% in %}$t_2${\small\verb%"%}
&$\leadsto\ \ ${\small\verb%"LET(\(%}$v_1${\small\verb%,%}$\ldots${\small\verb%,%}$v_n${\small\verb%).%}$t_2${\small\verb%)%}$t_1${\small\verb%"%}\\
{\small\verb%   "let %}$v_1${\small\verb%=%}$t_1${\small\verb% and %}$\ldots${\small\verb% and %}$v_n${\small\verb%=%}$t_n${\small\verb% in
%}$t${\small\verb%"%}
&$\leadsto\ \ ${\small\verb%"LET(%}$\ldots${\small\verb%(LET(LET(\%}$v_1\ldots v_n${\small\verb%.%}$t${\small\verb%)%}$t_1${\small\verb%)%}$t_2${\small\verb%)%}$\ldots${\small\verb%)%}$t_n${\small\verb%"%}\\
\end{tabular}}

\bigskip
 

\noindent The printer\index{printing, in HOL logic@printing, in \HOL\ logic!of let-terms@of \ml{let}-terms} inverts these transformations if the flag 
\ml{print\_let} is \ml{true}.  For example:

\setcounter{sessioncount}{1}
\begin{session}\begin{verbatim}
#"let x = 1 and y = 2 in x+y";;
"let x = 1 and y = 2 in x + y" : term

#set_flag(`print_let`,false);;
true : bool

#"let x = 1 and y = 2 in x+y";;
"LET(LET(\x y. x + y)1)2"

#"let (x,y) = (1,2) in x+y";;
"LET(UNCURRY(\x y. x + y))(1,2)" : term
\end{verbatim}\end{session}

Note that, as with uncurried terms, the underlying representation in \HOL\ can
manifest itself in error messages. For example:

\setcounter{sessioncount}{1}
\begin{session}\begin{verbatim}
#"let x = y in z";;
Indeterminate types:  "LET:(?1 -> ?2) -> (?3 -> ?4)"
 
evaluation failed     types indeterminate in quotation 
\end{verbatim}\end{session}

The reader is recommended to convince himself or herself that the
translations of \ml{let}-terms represent the intuitive meaning suggested by 
the surface syntax.\index{quotation, in HOL logic@quotation, in \HOL\ logic|)}\index{quotation, in HOL logic@quotation, in \HOL\ logic!of non-primitive terms|)}\index{terms, in HOL logic@terms, in \HOL\ logic!syntax of|)}\index{type checking, in HOL logic@type checking, in \HOL\ logic!special forms in|)}

\ml{let}-terms can be simplified with \ml{let\_CONV} -- see Section~\ref{let-terms}.

\section{Syntax for sets}\index{set theory notation}

The special purpose set-theoretic notations 
{\small\verb%"{%}$t_1,t_2,\ldots,t_n${\small\verb%}"%} and
{\small\verb%"{%}$t${\small\verb%|%}$p${\small\verb%}"%} are available.
The normal interpretation of the former is the finite set containing 
$t_1,t_2,\ldots, t_n$ and the normal interpretation of the latter
is the set of all $t$s such that $p$. These interpretations are predefined for
the library \ml{sets}, but the user can use the syntax for other purposes if
he or she wishes, using the functions:

\begin{boxed}
\index{define_finite_set_syntax@\ml{define\_finite\_set\_syntax}|pin}
\index{define_set_abstraction_syntax@\ml{define\_set\_abstraction\_syntax}|pin}
\begin{alltt}
   define_finite_set_syntax      : (string # string) -> void
   define_set_abstraction_syntax : string -> void
\end{alltt}\end{boxed}

\noindent Executing:

\begin{hol}\begin{alltt}
   define_finite_set_syntax(`\(c\sb{1}\)`,`\(c\sb{2}\)`)
\end{alltt}\end{hol}

\noindent causes {\small\verb%"{%}$t_1,\ldots,t_n${\small\verb%}"%}
to parse to:

\begin{hol}\begin{alltt}
   "\(c\sb{2}\) \(t\sb{1}\) (\(c\sb{2}\) \(t\sb{2}\) \(\cdots\) (\(c\sb{2}\) \(t\sb{n}\) \(c\sb{1}\)) \(\cdots\) ))"
\end{alltt}\end{hol}

\noindent with failure if either $c_1$ or $c_2$ is not the name of a constant.

In the library \ml{sets}, the empty set is \ml{EMPTY} and
the infixed function \ml{INSERT} adds an element to a set.
Executing:

\begin{hol}\begin{verbatim}
   define_finite_set_syntax(`EMPTY`,`INSERT`)
\end{verbatim}\end{hol}

\noindent will cause

\begin{hol}\begin{verbatim}
   "{1,2,3,4}"
\end{verbatim}\end{hol}

\noindent to parse to

\begin{hol}\begin{verbatim}
   "1 INSERT (2 INSERT (3 INSERT (4 INSERT EMPTY)))"
\end{verbatim}\end{hol}


Executing:

\begin{hol}\begin{alltt}
   define_set_abstraction_syntax `\(c\)`
\end{alltt}\end{hol}

\noindent causes  {\small\verb%"{%}$t${\small\verb%|%}$p${\small\verb%}"%}
to parse to:

\medskip

\noindent{\small
{\verb%   "%}}$c${\small{\verb%(\(%}$x_1${\verb%,%}$\ldots${\verb%,%}$x_n${\verb%).(%}$t${\verb%,%}$p${\verb%))"%}
}

\medskip

\noindent where $x_1$, $\ldots$ , $x_n$ are the free variables occurring in both $t$
and $p$.  If there are no such free variables then an error results.
The order in which the variables are listed in the variable structure
of the paired abstraction is an unspecified function of the structure
of $t$ (it is approximately left to right). Failure if $c$ is not the
name of a constant.

For example, if the library \ml{sets} (i.e. what used to be \ml{all\_sets})
is loaded, then

\begin{hol}\begin{verbatim}
   define_set_abstraction_syntax `GSPEC`
\end{verbatim}\end{hol}

\noindent will cause

\begin{hol}\begin{verbatim}
   "{x+y | (x < y) /\ (y < z)}"
\end{verbatim}\end{hol}

\noindent to parse to:

\begin{hol}\begin{verbatim}
   "GSPEC(\(x,y). ((x+y), (x < y) /\ (y < z)))"
\end{verbatim}\end{hol}

\noindent where \ml{GSPEC} is defined by:

\begin{hol}\begin{verbatim}
   |- !f. GSPEC f = SPEC(\x. ?y. x,T = f y)
\end{verbatim}\end{hol}

\noindent and \ml{SPEC} abstracts a predicate to a set (it is the abstraction
bijection used in the definition of the type operator \ml{set}).
Other examples are:

\begin{hol}\begin{verbatim}
   "{x+y+z | (x < y) /\ (y < z)}"
\end{verbatim}\end{hol}

\noindent will parse to:

\begin{hol}\begin{verbatim}
   "GSPEC(\(x,y,z). (x+(y+z), (x < y /\ y < z)))" 
\end{verbatim}\end{hol}

\noindent and

\begin{hol}\begin{verbatim}
   "{x+y+w | (x < y) /\ (y < z)}"
\end{verbatim}\end{hol}

\noindent will parse to:

\begin{hol}\begin{verbatim}
   "GSPEC(\(x,y). (x+(y+w), (x < y /\ y < z)))"
\end{verbatim}\end{hol}


Note that the precedence of comma is increased in the contexts 
``{\small\verb%{%}$\cdots${\small\verb%}%}'' and 
``{\small\verb%{%}$\cdots${\small\verb%|%}''.
Terms will be printed in set notation if the flag \ml{print\_set} is
\ml{true}.
Note that 

\medskip

\ml{"}$c${\small\verb%(\(%}$x_1$\ml{,}$\ldots$\ml{,}$x_n$\ml{).(}$t$\ml{,}$p$\ml{))"} 

\medskip

\noindent will only print as 
{\small\verb%"{%}$t${\small\verb%|%}$p${\small\verb%}"%} 
if the variables $x_1$, $\ldots$ , 
$x_n$ occur free in both $t$ and $p$ (and \ml{print\_set} is \ml{true}) .


\subsection{Antiquotation}

Within a quotation, expressions of the form
{\small\verb%^(%}$t${\small\verb%)%}\index{ antiquotation, in HOL logic@{\small\verb+^+} (antiquotation, in \HOL\ logic)} (where $t$ is an \ML\ expression of
type\index{type checking, in HOL logic@type checking, in \HOL\ logic!antiquotation in} {\small\verb%term%} or {\small\verb%type%}) are called {\it
antiquotations\/}\index{terms, in HOL logic@terms, in \HOL\ logic!antiquotation}\index{antiquotation, in HOL logic terms@antiquotation, in \HOL\ logic terms}.
An antiquotation {\small\verb%^(%}$t${\small\verb%)%}
evaluates to the \ML\ value of $t$. For example, {\small\verb%"x \/
^(mk_conj("y:bool","z:bool"))"%} evaluates to the same term as
{\small\verb%"x \/ (y /\ z)"%}. The most common use of antiquotation
is when the term $t$ is just an \ML\ variable $x$. In this case
{\small\verb%^(%}$x${\small\verb%)%} can be abbreviated by
{\small\verb%^%}$x$. 

The following session illustrates antiquotation.

\setcounter{sessioncount}{1}
\begin{session}\begin{verbatim}
#let y = "x+1";;
y = "x + 1" : term

#let z = "y = ^y";;
z = "y = x + 1" : term

#"!x:num.?y:num.^z";;
"!x. ?y. y = x + 1" : term
\end{verbatim}\end{session}


\subsection{User-programmable quotation typechecker}
\index{quotation, in HOL logic@quotation, in \HOL\ logic!typechecker for|(}

The typechecker for \HOL\ quotations contains a number of arbitrary
design decisions. Several people have suggested changes, e.g. that
full Hindley/Milner type inference be performed. Rather than try to
create a single new improved typechecker, a facility is now provided
that enables the user to write his or her own one and then to install
it in the system.

\index{preterms|(}
The \ML\ abstract type {\small\verb%preterm%} represents the parse trees of \HOL\ 
terms. A typechecker is a function of type {\small\verb%preterm->term%}. If the flag
\ml{preterm} is set to \ml{true} (the default is \ml{false}), then \HOL\ will use
whatever \ML\ function is currently bound to the name 
\ml{preterm\_handler} as
the quotation typechecker. The way this works is that when
\ml{preterm} is true the parser produces a preterm rather than a term,
and then wraps a call of \ml{preterm\_handler} 
around the quotation.\index{type checking, in HOL logic@type checking, 
in \HOL\ logic!user programmed} Other uses of preterms are possible, for example
as patterns for describing terms. 

The definition of the \ML\ type {\small\verb%preterm%} is:

\begin{hol}\index{preterm@\ml{preterm}!the type}\begin{alltt}
   rectype preterm = 
     preterm_var      of string                        \({\it Variables}\) 
   | preterm_const    of string                        \({\it Constants}\) 
   | preterm_comb     of preterm # preterm             \({\it Combinations}\) 
   | preterm_abs      of preterm # preterm             \({\it Abstractions}\) 
   | preterm_typed    of preterm # type                \({\it Explicit typing}\) 
   | preterm_antiquot of term                          \({\it Antiquotation}\) 
\end{alltt}\end{hol}

The function:

\begin{boxed}\index{preterm_to_term@\ml{preterm\_to\_term}|pin}
\begin{verbatim}
   preterm_to_term : preterm -> term
\end{verbatim}\end{boxed}

\noindent invokes the standard \HOL\ typechecker on a preterm and returns the
resulting typechecked term (or causes the standard error message).\index{preterms|)}



The following is a rather contrived example:

\setcounter{sessioncount}{1}
\begin{session}\index{preterm_handler@\ml{preterm\_handler}|pin}
\begin{verbatim}
#letref p_reg = preterm_var `x`;;
p_reg = preterm_var `x` : preterm

#let preterm_handler p = p_reg:=p; 
                         print_string `Typechecking ... `;
                         print_newline();
                         preterm_to_term p;;
preterm_handler = - : (preterm -> term)
\end{verbatim}\end{session}

\begin{session}
\begin{verbatim}
#set_flag(`preterm`,true);;
false : bool

#"x+y";;
Typechecking ...
"x + y" : term

#p_reg;;
preterm_comb((preterm_comb((preterm_const `+`), preterm_var `x`)),
             preterm_var `y`)
: preterm
\end{verbatim}\end{session}

\noindent Different top-level typechecking can be defined by using a
different definition of the function
\ml{preterm\_handler}. Note that quotations and their typechecking
is purely a `user interface' feature, so changing the typechecker does
not compromise the logical soundness of \HOL.\index{quotation, in HOL logic@quotation, in \HOL\ logic!typechecker for|)}



\section{Theorems}
\label{avra_theorems}
% I have added this section in a hurry 9 Nov 89.  Was missing!

In Chapter~\ref{logic}, the notion of deduction was introduced in terms
of {\it sequents\/}\index{sequents!in natural deduction}, where a sequent
is a pair whose second component is a formula being
asserted (a conclusion)\index{conclusions!of sequents}, 
and whose first component is a set of formulas (hypotheses)\index{hypotheses!of sequents}.
Based on this was the notion of a {\it deductive system\/}\index{natural deduction}\index{deductive systems}: a set of pairs,
whose second component is a sequent, and
whose first component is a sequent list\footnote{Note that these sequents
form a list, not a set; that is, are ordered.}.
The concept of a sequent {\it following from\/}\index{follows from, in natural deduction}
a set of sequents via a
deductive system was then defined: a sequent follows from a set of sequents if
the sequent
is the last element of some chain of sequents, each of whose
elements is either in the set, or itself follows from the set along with
earlier elements of the chain, via the deductive system.

A notation for `follows from' was then introduced.
That a
sequent $(\{t_1,\ldots,t_n\},\ t)$ follows from
a set of sequents $\Delta$, via a deductive system ${\cal D}$, is
denoted\index{turnstile notation|(} by: $t_1,\ldots,t_n\vdash_{{\cal D},\Delta} t$.
(It was noted that
where either ${\cal D}$ or $\Delta$ were clear by context, their mention
could be omitted; and where the set of hypotheses was empty,
its mention could be omitted.)

A sequent that follows from the empty set of sequents via
a deductive system is called a {\it theorem\/} of that deductive system.
That is, a theorem\index{theorems, in natural deduction} is the last element of a {\it proof\/}\index{proof!in natural deduction} (in the sense
of Chapter~\ref{logic}) from the empty
set of sequents. When a pair $(L,(\Gamma,t))$ belongs to a deductive system,
and the list $L$ is empty, 
then the sequent $(\Gamma,t)$ is called an {\it axiom\/}\index{axioms!in
natural deduction}. 
Any pair $(L,(\Gamma,t))$ belonging to 
a deductive system is called a {\it primitive inference\/}\index{inference, in natural deduction}\index{primitive inference, in natural deduction}
of the system, with hypotheses\footnote{Note that
`hypotheses' and `conclusion' are also used for the components
of sequents.} $L$ and 
conclusion $(\Gamma,t)$.

A formula\index{formulas as terms, in HOL logic@formulas as terms, in \HOL\ logic}
 in the abstract is represented concretely in \HOL\ by
a term whose \HOL\ type is {\small\verb%":bool"%}.
Therefore, a term\index{terms, in HOL logic@terms, in \HOL\ logic!as logical formulas} of type {\small\verb%":bool"%} is used to represent
a member of the set of hypotheses of a sequent;
and likewise to represent the
conclusion of a sequent.
Sets in this context
are represented by lists, so the set of hypotheses of a sequent\index{sequents!representation of, in HOL logic@representation of, in \HOL\ logic}
is represented by a list of {\small\verb%":bool"%}-typed terms.

A theorem in the abstract is represented concretely in the \HOL\
system by a value with the \ML\ abstract type
{\small\verb%thm%}\index{thm@\ml{thm}}. 
The type {\small\verb%thm%} has a primitive destructor function

\begin{boxed}
\index{dest_thm@\ml{dest\_thm}|pin}
\begin{verbatim}
   dest_thm : thm -> (term list # term)
\end{verbatim}\end{boxed}

\noindent which returns a pair consisting of the hypothesis\index{hypotheses!of theorems} list and
the conclusion\index{conclusions!of theorems}, respectively, of a theorem.
From this, two destructor\index{theorems, in HOL logic@theorems, in \HOL\ logic!destructors for} functions are derived


\begin{boxed}
\index{hyp@\ml{hyp}|pin}
\index{concl@\ml{concl}|pin}
\begin{verbatim}
   hyp   : thm -> term list
   concl : thm -> term
\end{verbatim}\end{boxed}

\noindent for extracting the hypothesis list and the conclusion, respectively,
of a theorem. The \ML\ type {\small\verb%thm%} does not have
a primitive constructor function.  In this way, the \ML\ type system protects
the \HOL\ logic
from the arbitrary and unrecorded construction 
of theorems, which would compromise
the consistency\index{consistency, of HOL logic@consistency, of \HOL\ logic} of the logic. (Functions which return theorems as values,
\eg\ functions representing primitive inferences,
are discussed first in Section~\ref{rules}, and further in 
Chapter\ref{derived-rules}.)

It was mentioned in Chapter~\ref{logic} that the deductive system of \HOL\
includes five axioms\footnote{This is
a simplification: 
%each inference rule corresponds to an infinite family of elements
%of the deductive system, and
the axioms are an extension 
of the basic logic. See Sections~\ref{boolthy} and \ref{ind}.}.
In that Chapter, the axioms were presented in abstract form.
The concrete representation of the axioms in \HOL\ is given
in Section~\ref{HOL-theory}.
% and of the inference rules, in 
%Section~\ref{rules}. 
To anticipate, the axiom \ml{BOOL\_CASES\_AX}
mentioned in Chapter~\ref{logic} is printed in \HOL\ as follows
(where \ml{"T"} and \ml{"F"} 
are the \HOL\ logic's constants representing truth and
falsity, respectively):

\begin{hol}
\index{F@\ml{F}!axiom for}
\begin{verbatim}
   |- !t. (t = T) \/ (t = F)
\end{verbatim}\end{hol}

\noindent Note the special print format\index{printing, in HOL logic@printing, in \HOL\ logic!of theorems},
 with the approximation
to the abstract $\vdash$ notation\index{theorem notation, in HOL logic@theorem notation, in \HOL\ logic|(}, \ml{|-}, used to indicate \ML\ type
{\small\verb%thm%} status;
the absence of \HOL\ quotation marks\index{ theorem marker, in HOL logic@\ml{"|-} (theorem marker, in \HOL\ logic)} 
 in the \ml{|-} context; and
the absence of type information following the printed term.
The session below illustrates
the use of the destructor functions:

\setcounter{sessioncount}{1}
\begin{session}\begin{verbatim}
#let th = BOOL_CASES_AX;;
th = |- !t. (t = T) \/ (t = F)

#hyp th;;
[] : term list

#concl th;;
"!t. (t = T) \/ (t = F)" : term

#type_of it;;
":bool" : type
\end{verbatim}\end{session}\index{turnstile notation|)}

\noindent In addition to the print conventions mentioned above,
the printing of theorems prints hypotheses\index{printing, in HOL logic@printing, in \HOL\ logic!of hypotheses of theorems}
 as periods (\ie\ full stops or
dots).  The function \ml{print\_all\_thm}\index{print_all_thm@\ml{print\_all\_thm}} prints theorems with
hypotheses shown in full. These points are illustrated with a
theorem inferred, for example purposes, from another axiom mentioned
in Chapter~\ref{logic}: \ml{SELECT\_AX}.

\setcounter{sessioncount}{1}
\begin{session}\begin{verbatim}
th = . |- P($@ P)

#print_all_thm th;;
P x |- P($@ P)() : void
\end{verbatim}\end{session}\index{theorem notation, in HOL logic@theorem notation, in \HOL\ logic|)}


\section{Theories}\index{theories, in HOL logic@theories, in \HOL\ logic!representation of|(}
\label{theoryfns}

It was mentioned in Chapter~\ref{logic} that a theory is a $4$-tuple

\[ {\cal T}\ =\ \langle{\sf Struc}_{\cal T},\ 
                {\sf Sig}_{\cal T},\ 
                {\sf Axioms}_{\cal T},\ 
                {\sf Theorems}_{\cal T}\rangle \]

\noindent where
\begin{myenumerate}
\item ${\sf Struc}_{\cal T}$ is
the type structure of ${\cal T}$;
\item ${\sf Sig}_{\cal T}$ is 
the signature of ${\cal T}$;
\item ${\sf Axioms}_{\cal T}$ is 
the set of axioms of ${\cal T}$;
\item ${\sf Theorems}_{\cal T}$ is the set of
theorems of ${\cal T}$.
\end{myenumerate}

Such a theory is represented  in the  \HOL\ system  as a  collection of files,
called theory files\index{theory files|(}. Each  file has  a name  of the  form $name$\ml{.th}, where
$name$ is a string supplied by the user.  Various additional pieces of
information are stored in the theory files, including the parsing status of the
constants (\ie\ whether they are infixes or binders), as well as several items
involving concepts not yet introduced: type abbreviations (see
Section~\ref{typeabbrev} for this); and which axioms are definitions, type
definitions or specifications (see Section~\ref{avra_definitional} for the last
three).

Theory files are structured hierarchically to represent sequences of
extensions of an initial theory (see Section~\ref{extensions}) called
\ml{HOL}\index{HOL@\ml{HOL}}. Each theory file making up a theory records some types, constants,
axioms and theorems, together with pointers to other theory files called its
{\it parents\/}\index{parents, of HOL theories@parents, of \HOL\ theories}.  The theory represented by such a theory file is obtained by
taking the union of all the types, constants, axioms and theorems in the file,
together with the types, constants, axioms and theorems in all the theory
files reachable by following pointers to parents. This collection of reachable
files is called the {\it ancestry\/}\index{ancestry, of HOL system theories@ancestry, of \HOL\ system theories}\index{theories, in HOL logic@theories, in \HOL\ logic!hierarchies of}
of the theory file. Axioms (including
definitions and specifications) and theorems are named in the \HOL\ system by
two strings: the name of the theory file in which they are stored, together with
a name supplied by the user\index{theory files|)}.

The data stored in a single theory file is called a {\it theory segment\/}\index{theory segments};
this is not really a logical concept, but rather a concept of the
representation of theories in the \HOL\ system. It is necessary to distinguish
theories from their constituent theory segments  because the naming of data
in theories is based on the names given to segments.  Specifically, axioms,
definitions, specifications and theorems are named\index{theories, in HOL logic@theories, in \HOL\ logic!naming of}
 by a pair of strings
$\langle thy,name \rangle$,
where $thy$ is the name of the theory segment current when the
item was declared and $name$ is a specific name supplied by the user (see the
functions \ml{new\_axiom}, \ml{new\_definition}, below). Different items
can have the same specific name if the associated segment is different.

A typical\index{HOL system@\HOL\ system!typical work in} piece of work with the \HOL\ system consists in a number of
sessions\index{sessions with HOL system@sessions, with \HOL\ system}.
In the first of these, a new theory, ${\cal T}$ say, is created by
extending existing theories with a number of definitions. The concrete result
of the session will be a theory file ${\cal T}$\ml{.th} whose contents is the
segment created during the session and whose ancestry represents the desired
logical theory.  In subsequent sessions this theory is extended by proving new
theorems that will be stored in the file ${\cal T}$\ml{.th}. The logical
meaning of these sessions is that a new extension to ${\cal T}$ is created
which replaces the old version.  Subsequent pieces of work can build on (\ie\
extend) the definitions and theorems of ${\cal T}$ by making it a parent of
new theories.

There are two modes of working with \HOL: {\it draft mode\/}\index{draft mode, in HOL system@draft mode, in \HOL\ system} and {\it proof
mode\/}\index{proof mode, in HOL system@proof mode, in \HOL\ system}.
In the former, the sets ${\sf Struc}_{\cal T}$, ${\sf Sig}_{\cal T}$
and ${\sf Axioms}_{\cal T}$ can be extended; in the latter only ${\sf
Theorems}_{\cal T}$ can be changed. In draft mode, inconsistencies can be
introduced by asserting inconsistent axioms, but in proof mode only
consistency-preserving actions (namely valid proof) can be evaluated. Draft mode is
analogous to `super user mode' in Unix, in that it gives access to dangerous
facilities. Everything that can be done in proof mode can be done in draft
mode, but not vice versa.

There is always a {\it current theory\/} which is the theory represented by
the current theory segment together with its ancestry. The name of the current
theory is returned by the \ML\ function:

\begin{boxed}\index{current_theory@\ml{current\_theory}|pin}
\begin{verbatim}
   current_theory : void -> string
\end{verbatim}\end{boxed}

Initially \HOL\ is in proof mode with current
theory called \ml{HOL}, which is described in Section~\ref{HOL-theory}\index{theories, in HOL logic@theories, in \HOL\ logic!representation of|)}.

\subsection{Primitive ML functions for creating theories}
\label{theoryprims}

The \ML\ functions for creating theories\index{theories, in HOL logic@theories, in \HOL\ logic!creation of|(} are listed below.

\begin{boxed}
\index{new_theory@\ml{new\_theory}|pin}
\begin{verbatim}
   new_theory : string -> void
\end{verbatim}\end{boxed}

\noindent \ml{new\_theory `$thy$`} can be done in both
proof\index{proof mode, in HOL system@proof mode, in \HOL\ system} and
draft\index{draft mode, in HOL system@draft mode, in \HOL\ system} modes. It
switches into draft mode for a new theory with name $thy$ and it fails if
there already exists a file $thy$\ml{.th} in the current search path. The
current theory becomes a new parent of $thy$.


\begin{boxed}
\index{new_parent@\ml{new\_parent}|pin}
\begin{verbatim}
   new_parent : string -> void
\end{verbatim}\end{boxed}


\noindent Executing \ml{new\_parent}\ $thy$ makes $thy$ into
a parent
of the current theory. Failure if:
\begin{myenumerate}
\item not in draft mode;
\item $thy$ is not a theory on the current search path;
\item there is a type in $thy$ with the same
name as a type in the current theory;
\item there is a constant in $thy$ with the same
name as a constant in the current theory.
\end{myenumerate}


\begin{boxed}
\index{new_type@\ml{new\_type}|pin}
\begin{verbatim}
   new_type : int -> string -> void
\end{verbatim}\end{boxed}


\noindent Executing \ml{new\_type}$\ n\ \ml{`\ty{op}`}$ makes \ty{op}
a new $n$-ary type operator\index{type operators, in HOL logic@type operators, in \HOL\ logic!declaration} in the current theory.
Failure if:
\begin{myenumerate}
\item not in draft mode;
\item there already exists a type operator named $\ty{op}$ in the current
theory.
\end{myenumerate}


\begin{boxed}
\index{new_constant@\ml{new\_constant}|pin}
\begin{verbatim}
   new_constant : (string # type) -> void
\end{verbatim}\end{boxed}

\noindent Executing {\small\verb%new_constant(`%}$c${\small\verb%`,%}$\sigma${\small\verb%)%} makes
$c_{\sigma'}$ a new constant\index{constants, in HOL logic@constants, in \HOL\ logic!declaration of} of the current theory,
for all $c_{\sigma'}$ where $\sigma'$ is an instance of $\sigma$.
The type $\sigma$ is
called the {\it generic type\/}\index{generic types, in HOL logic@generic types, in \HOL\ logic} of $c$. Failure if:
\begin{myenumerate}
\item not in draft mode;
\item there already exists a constant named $c$ in the current
theory.
\end{myenumerate}

\begin{boxed}
\index{new_infix@\ml{new\_infix}|pin}
\begin{verbatim}
   new_infix : (string # type) -> void
\end{verbatim}\end{boxed}


\noindent Executing \ml{new\_infix(`}$ix$\ml{`,}$\sigma$\ml{)}
declares $ix$ to be a new constant with generic type $\sigma$ and
infix status.
Failure if:
\begin{myenumerate}
\item not in draft mode;
\item there already exists a constant named $ix$ in the current theory;
\item $\sigma$ not of the form \ml{$\sigma_1$->$\sigma_2$->$\sigma_3$}.
\end{myenumerate}


\begin{boxed}
\index{new_binder@\ml{new\_binder}|pin}
\begin{verbatim}
   new_binder : (string # type) -> void
\end{verbatim}\end{boxed}


\noindent Executing \ml{new\_binder(`}$b$\ml{`,}$\sigma$\ml{)}\index{binders, in HOL logic@binders, in \HOL\ logic!declaration of}
declares $b$ to be a new constant with generic type $\sigma$ and 
binder status.
Failure if:
\begin{myenumerate}
\item not in draft mode;
\item there already exists a constant named $b$ in the current theory;
\item $\sigma$ not of the form \ml{($\sigma_1$->$\sigma_2$)->$\sigma_3$}.
\end{myenumerate}

\begin{boxed}
\index{new_axiom@\ml{new\_axiom}|pin}
\begin{verbatim}
   new_axiom : (string # term) -> thm
\end{verbatim}\end{boxed}


\noindent Executing \ml{new\_axiom(`}$name$\ml{`,}$t$\ml{)} declares the 
sequent
\ml{(\{\},$t$)} to be an axiom\index{axioms!declaration of, in HOL logic@declaration of, in \HOL\ logic} of the current theory with name $name$. 
Failure if:
\begin{myenumerate}
\item not in draft mode;
\item there is already an axiom, definition or specification 
named $name$ in the current theory segment.
\end{myenumerate}


Once a theorem has been proved, it can be saved with the function

\begin{boxed}
\index{save_thm@\ml{save\_thm}|pin}
\begin{verbatim}
   save_thm : (string # thm) -> thm
\end{verbatim}\end{boxed}

\noindent
Evaluating \ml{save\_thm(`}$name$\ml{`,}$th$\ml{)} will save the theorem\index{theorems, in HOL logic@theorems, in \HOL\ logic!saving of}\index{saving theorems}
$th$ with name $name$ in the current theory segment.

The following function is used to finish a session in  draft mode\index{draft mode, in HOL system@draft mode, in \HOL\ system}  and save the
current theory segment in a theory file on disk.

\begin{boxed}
\index{close_theory@\ml{close\_theory}|pin}
\begin{verbatim}
   close_theory : void -> void
\end{verbatim}\end{boxed}

\noindent {\bf Warning:} quitting \HOL\ without\index{exiting of HOL system@exiting of \HOL\ system} closing the session with
\ml{close\_theory} may result in the theory segment created during the session
being lost (\ie\ not saved in a theory file)!\index{theories, in HOL logic@theories, in \HOL\ logic!creation of|)}

\subsection{Functions for creating definitional extensions}\index{extension, of HOL logic@extension, of \HOL\ logic!definitional}\index{definitional extension, of HOL logic@definitional extension, of \HOL\ logic}\index{theories, in HOL logic@theories, in \HOL\ logic!extension of|(}
\label{avra_definitional}

There are three kinds of definitional extensions:
constant definitions, constant specifications and type definitions.

\subsubsection{Constant definitions}

In Section~\ref{defs} a constant definition\index{extension, of HOL logic@extension, of \HOL\ logic!by constant definition}\index{constant definition extension, of HOL logic@constant definition extension, of \HOL\ logic!ML function for@\ML\ function for|(}
over a signature $\Sigma_{\Omega}$ is defined to be
an equation, \ie\ a formula of the form $c_{\sigma}=t_{\sigma}$,
such that:
\begin{myenumerate}
\item $c$ is not the name of any constant in $\Sigma_{\Omega}$;
\item $t_{\sigma}$ is a closed term in ${\sf Terms}_{\Sigma_{\Omega}}$;
\item all the type variables occurring in $t_{\sigma}$ occur in $\sigma$.
\end{myenumerate}

In \HOL, definitions can be slightly more general than this, in that
an equation:

\[ c\ v_1\ \cdots\ v_n\ =\ t \]

\noindent is  allowed  to  be a  definition where  $v_1$, $\dots$, $v_n$ are
variable structures (\ie\ tuples of distinct variables).   Such  an equation is
logically equivalent to:

\[ c\ =\ \lambda v_1\ \cdots\ v_n.\  t \]

\noindent which is a definition in the sense of  Section~\ref{defs} if (i),
(ii) and (iii) hold.  

The following  \ML\ function  creates\index{defining mechanisms, for HOL logic@defining mechanisms, for \HOL\ logic} a  new definition in
the current theory.

\begin{boxed}
\index{new_definition@\ml{new\_definition}|pin}
\begin{verbatim}
   new_definition : (string # term) -> thm
\end{verbatim}\end{boxed}


\noindent Evaluating
 \ml{new\_definition(`}$name$\ml{`,\ "}$c\ v_1\ \cdots\ v_n\ =\ t$\ml{")},
where $c$ is not already a constant, declares the sequent
\ml{(\{\},$\lambda v_1\ \cdots\ v_n.\  t$)} to be a constant definition\index{definitions, adding to HOL logic@definitions, adding to \HOL\ logic} 
of the current theory. The name associated with the definition in
this theory is $name$. 
Failure if:
\begin{myenumerate}
\item not in draft mode;
\item there is already an axiom, definition or specification 
named $name$ in the current theory segment;
\item $c$ is already a constant in the current theory;
\item $t$ contains free variables that are not in any of
the variable structures $v_1$, $\dots$, $v_n$ (this is equivalent
to requiring $\lambda v_1\ \cdots\ v_n.\  t$ to be a closed term);
\item there is a type variable in  $v_1$, $\dots$, $v_n$ or $t$
that does not occur in the type of $c$.
\end{myenumerate}

The following two functions declare new constants that have infix\index{infixes, in HOL logic@infixes, in \HOL\ logic!declaration of}\index{infixes, in HOL logic@infixes, in \HOL\ logic} or binder
status. The failure conditions are the same as for \ml{new\_definition}, 
with the
additional conditions that the constants being declared must be types of
the appropriate form (\ml{$\sigma_1$->$\sigma_2$->$\sigma_3$} for infixes and
\ml{($\sigma_1$->$\sigma_2$)->$\sigma_3$} for binders).


\begin{boxed}
\index{new_infix_definition@\ml{new\_infix\_definition}|pin}
\index{new_binder_definition@\ml{new\_binder\_definition}|pin}
\begin{verbatim}
   new_infix_definition  : (string # term) -> thm
   new_binder_definition : (string # term) -> thm
\end{verbatim}\end{boxed}

\noindent Note that until an infix or binder declaration has been processed the
constant being defined will not have its special status.\index{parsing, of HOL logic@parsing, of \HOL\ logic!of constants, before definition} It is therefore
necessary to use
the constant in normal prefix position when making the definition\index{defining mechanisms, for HOL logic@defining mechanisms, for \HOL\ logic}. For example,

\begin{hol}
{\small\verb%   new_infix_definition(`%}$ix${\small\verb%_DEF`, "%}$m$ $ix$ $n${\small\verb% = ... ")%}
\end{hol}

\noindent will not work, as at the time when the quotation is parsed, \ml{ix}
does not have infix status and hence \ml{m ix n} will parse with \ml{m} as the
function. The definition must thus have the form:

\begin{hol}
{\small\verb%   new_infix_definition(`%}$ix${\small\verb%_DEF`, "%}$ix$ $m$ $n${\small\verb% = ... ")%}
\end{hol}

\noindent Only after this has been processed will $ix$ be an infix. It is a
common practice among \HOL\ users to write a {\small\verb%$%}\index{ escape, in HOL logic parser@{\small\verb+$+} (escape, in \HOL\ logic parser)}\index{declared constants, in HOL logic@declared constants, in \HOL\ logic}
before
the constant being defined as an infix or binder
to indicate that after the definition is made, it will have a special
syntactic status; \ie\ to write:

\begin{hol}
{\small\verb%   new_infix_definition(`%}$ix${\small\verb%_DEF`, "$%}$ix$ $m$ $n${\small\verb% = ... ")%}
\end{hol}

\noindent and similarly with \ml{new\_binder\_definition}. This use of
{\small\verb%$%} is not necessary; but after the definition has been
made {\small\verb%$%} must, of course, 
 be used if the syntactic status needs to be suppressed.
\index{constant definition extension, of HOL logic@constant definition extension, of \HOL\ logic!ML function for@\ML\ function for|)}

\subsubsection{Constant specifications}
\label{conspec}

\index{specification of constants, in HOL logic@specification of constants, in \HOL\ logic|(}
\index{extension, of HOL logic@extension, of \HOL\ logic!by constant specification}
In Section~\ref{specs} a constant specification\index{constant specification extension, of HOL logic@constant specification extension, of \HOL\ logic!ML function for@\ML\ function for} for a theory ${\cal T}$
is defined to be a pair:

\[ \langle(c_1,\ldots,c_n),\ \lquant{{x_1}_{\sigma_1}
\cdots {x_n}_{\sigma_n}}t_{\ty{bool}}\rangle \]

\noindent such that:

\begin{myenumerate}
\item $c_1$, $\dots$, $c_n$ are distinct names that
are not the names of any constants in ${\sf Sig}_{\cal T}$.
\item $\lquant{{x_1}_{\sigma_1}
\cdots {x_n}_{\sigma_n}}t_{\ty{bool}}\ \in\ {\sf Terms}_{\cal T}$.
\item $tyvars(\lquant{{x_1}_{\sigma_1}
\cdots {x_n}_{\sigma_n}}t_{\ty{bool}})\ \subseteq\ tyvars(\sigma_i)$ for
$1\leq i\leq n$.
\item $\equant{{x_1}_{\sigma_1}\ \cdots\ {x_n}_{\sigma_n}}t
\ \in\ {\sf Theorems}_{\cal T}$.
\end{myenumerate}

The following \ML\ function is used to make constant specifications in
the \HOL\ system.

\begin{boxed}
\index{new_specification@\ml{new\_specification}|pin}
\begin{verbatim}
   new_specification : string -> ((string#string)list) -> thm -> thm
\end{verbatim}\end{boxed}

Evaluating:

\medskip

\begin{tabular}{l}
   \ml{new\_specification}\\
\ \ml{`}$name$\ml{`}\\ 
\ \ml{[`}$flag_1$\ml{`,`}$c_1$\ml{`;\ }$\ldots$\ml{\ ;\ `}$flag_n$\ml{`,`}$c_n$\ml{`]}\\
\ \ml{|-\ ?}$x_1\ \cdots\ x_n$\ml{.}\  $t$\ml{[}$x_1$\ml{,}$\ \ldots\
$\ml{,}$x_n$\ml{]}\\
\end{tabular}

\medskip

\noindent simultaneously  introduces  new constants  named $c_1$, $\dots$,
$c_n$ satisfying the property:



\[ \ml{|- }t\ml{[}c_1\ml{,}\ \ldots\ \ml{,}c_n\ml{]} \]


\noindent If \ml{`}$flag_i$\ml{`} is \ml{`constant`} 
then $c_i$ is declared an ordinary constant, if it is
\ml{`infix`} then $c_i$ is declared an infix and if it is 
\ml{`binder`}\index{binders, in HOL logic@binders, in \HOL\ logic} then $\ml{c}_i$ is declared
a binder.  Any other value of flag causes an error. This theorem is stored,
with name $name$, as a definition in the current theory segment. A call to
\ml{new\_specification} fails if:

\begin{myenumerate}
\item not in draft mode;
\item there is already an axiom, definition or specification 
named $name$ in the current theory segment;
\item the theorem argument has a non-empty assumption list;
\item there are free variables in the theorem argument;
\item $c_1$, $\dots$, $c_n$ are not distinct variables;
\item some $c_i$ is already a constant in the current theory;
\item some $c_i$ is not an allowed name for a constant;
\item some $flag_i$ is not either \ml{constant}, \ml{infix}
or \ml{binder};
\item the type of $c_i$ is not suitable for a constant with the syntactic
status specified by $flag_i$;
\item the type of some $c_i$ does not contain all the type
variables which occur in the term
{\small\verb%\%}$x_1\ \cdots\ x_n$\ml{.}\  $t$\ml{[}$x_1$\ml{,}$\ \ldots\
$\ml{,}$x_n$\ml{]}.
\end{myenumerate}

% =====================================================================
% Following section deleted [TFM 90.12.01]		

% Specifications with assumptions\index{constant specifications, in HOL
% logic@constant specifications, in \HOL\ logic!with
% assumptions|(}\index{assumptions!in constant specifications} are sometimes
% convenient. Rather than generalize the primitive mechanism to support this,
% \HOL\ achieves the same effect by providing a way of encoding specifications
% with assumptions as specifications without them.  This encoding uses the 
% infix constant
% \ml{IS\_ASSUMPTION\_OF}\index{IS_ASSUMPTION_OF@\ml{IS\_ASSUMPTION\_OF}|(},
% which has type \ml{bool->bool->bool} and is 
% defined (in the theory \ml{bool})
% to be equal to the primitive constant \ml{==>}.  A theorem of the form
% $t_1$\ml{,}$t_2$\ml{,}$\ldots$\ml{,}$t_n$\ml{\ |-\ }$t$ is logically
% equivalent to the following theorem without assumptions:

% \[\ml{|-\ }  \begin{array}[t]{@{}l}
% 	  t_1\ \ml{IS\_ASSUMPTION\_OF}\\
%           t_2\ \ml{IS\_ASSUMPTION\_OF}\\
%           \qquad\qquad \vdots\\
%           t_n\ \ml{IS\_ASSUMPTION\_OF}\ t\\
% \end{array} \]

% If the flag \ml{undisch\_defs} is \ml{true} (the default), then a
% specification that encodes a theorem with assumptions using
% \ml{IS\_ASSUMPTION\_OF} will be returned by the \ML\ definition retrieving
% functions (\ie\ \ml{definition} and \ml{definitions}) as a theorem with
% assumptions. For example, specifying \ml{c} by:

% \begin{hol}\begin{verbatim}
%    |- ?x. (?x.t[x]) IS_ASSUMPTION_OF t[x]
% \end{verbatim}\end{hol}

% \noindent will result in \ml{c} being defined\index{defining mechanisms, for
% HOL logic@defining mechanisms, for \HOL\ logic} by:

% \begin{hol}\begin{verbatim}
%    ?x.t[x] |- t[c]
% \end{verbatim}\end{hol}

% \noindent(although, in fact, \ml{|- (?x.t[x]) IS\_ASSUMPTION\_OF t[c]}, is
% what is actually stored). Note that the constant \ml{==>} could not be used
% instead of \ml{IS\_ASSUMPTION\_OF}, because there would then be no way of
% preventing the specification:


% \begin{hol}\begin{verbatim}
%    |- ?x.t[x] ==> t[c]
% \end{verbatim}\end{hol}

% \noindent being interpreted as the specification:

% \begin{hol}\begin{verbatim}
%    ?x.t[x] |- t[c]
% \end{verbatim}\end{hol}

% \noindent These two specifications are logically equivalent, but there may be
% pragmatic reasons for wanting one form or the other.  \index{constant
% specifications, in HOL logic@constant specifications, in \HOL\ logic!with
% assumptions|)} \index{extension, of HOL logic@extension, of \HOL\ logic!by
% constant specification|)}\index{constant specification extension, of HOL
% logic@constant specification extension, of \HOL\ logic!ML function for@\ML\
% function for|)} \index{IS_ASSUMPTION_OF@\ml{IS\_ASSUMPTION\_OF}|)}

\subsubsection{Type definitions}\index{extension, of HOL logic@extension, of \HOL\ logic!by type definition|(}
\label{type-defs}\index{type definitions, in HOL logic@type definitions, in \HOL\ logic|(}

In Section~\ref{tydefs} it is explained that
defining\index{type definitions, in HOL logic@type definitions, in \HOL\ logic!introduction of}\index{type definition extension, in HOL logic@type definition extension, in \HOL\ logic!ML function for@\ML\ function for|(}
a new type $(\alpha_1,\ldots,\alpha_n)\ty{op}$ in a theory ${\cal T}$ consists
of introducing $\ty{op}$ as a new $n$-ary type operator and 

\[\turn \equant{f_{(\alpha_1,\ldots,\alpha_n)\ty{op}\fun\sigma}}\TyDef\ p\ f\]

\noindent as a new axiom, where $p$ is a predicate
characterizing\index{characteristic predicate, of type definitions} a
non-empty subset of an existing type $\sigma$.  Formally, a type definition
for a theory ${\cal T}$ is a $3$-tuple 

\[ \langle \sigma,\ (\alpha_1,\ldots,\alpha_n)\ty{op},
    \ p_{\sigma\fun\ty{bool}}\rangle \]

\noindent where:

\begin{myenumerate}
\item $\sigma\in{\sf Types}_{\cal T}$  and
$tyvars(\sigma)\in\{\alpha_1, \ldots , \alpha_n\}$.
\item \ty{op} is not the name of a type constant in ${\sf Struc}_{\cal T}$.
\item $p\in{\sf Terms}_{\cal T}$ is a closed term of
type $\sigma\fun\ty{bool}$  and
$tyvars(p)\subseteq\{\alpha_1, \ldots , \alpha_n\}$.
\item $\equant{x_{\sigma}}p\ x \ \subseteq\ {\sf Theorems}_{\cal T}$.
\end{myenumerate}

The following \ML\ function makes a type definition in the \HOL\ system.

\begin{boxed}
\index{new_type_definition@\ml{new\_type\_definition}|pin}
\begin{verbatim}
   new_type_definition : (string # term # thm) -> thm
\end{verbatim}\end{boxed}

\noindent If $t$ is a term of type
$\sigma$\ml{->bool} containing $n$ distinct type variables, then
evaluating:

{\def\op{{\normalsize\sl op}}
\begin{hol}\begin{alltt}
   new_type_definition(`{\op}`, \m{t}, |- ?\m{x}.\m{\:t \;x}) 
\end{alltt}\end{hol}}

\noindent results in \ty{op} being declared as a new $n$-ary type operator
characterized by the definitional\index{definitional axioms}\index{type operators, in HOL logic@type operators, in \HOL\ logic!definitional axioms for} axiom:

\begin{hol}\begin{alltt}
   |- ?rep. TYPE\_DEFINITION \m{t} rep
\end{alltt}\end{hol}

\noindent which is stored as a definition with the automatically 
generated name
\ty{op}\ml{\_TY\_DEF}.\index{TY_DEF@$\ldots$\ml{\_TY\_DEF}}\footnote{In 
previous versions of HOL, type definitions 
were stored as axioms rather than definitions.} The constant 
\ml{TYPE\_DEFINITION}\index{TYPE_DEFINITION@\ml{TYPE\_DEFINITION}} 
is defined in the theory \ml{bool} by:

\begin{hol}\begin{verbatim}
   |- TYPE_DEFINITION (P:*->bool) (rep:**->*) =
       (!x' x''. (rep x' = rep x'') ==> (x' = x'')) /\	
       (!x. P x = (?x'. x = rep x'))
\end{verbatim}\end{hol}

\noindent Executing \ml{new\_type\_definition(`\ty{op}`,\ }$t$\ml{,\ 
|- ?}$x$\ml{.}\ $t\ x$\ml{)} fails if:
\begin{myenumerate}
\item not in draft mode;
\item $\ty{op}$ is already the name of a type or type operator 
in the current theory;
\item there already exists a constant definition, constant specification,
type definition or axiom named  \ty{op}\ml{\_TY\_DEF} in the current theory; 
\item $t$ does not have a type of the form $\sigma$\ml{->bool}.
\end{myenumerate}
\index{extension, of HOL logic@extension, of \HOL\ logic!by type definition|)}
\index{theories, in HOL logic@theories, in \HOL\ logic!extension of|)}\index{type definition extension, in HOL logic@type definition extension, in \HOL\ logic!ML function for@\ML\ function for|)}\index{type definitions, in HOL logic@type definitions, in \HOL\ logic|)}

\subsubsection{Defining bijections}
\index{type definitions, in HOL logic@type definitions, in \HOL\ logic!defining bijections for|(}

The result of a type definition using \ml{new\_type\_definition} is a theorem
which asserts only the {\it existence\/} of a
bijection\index{bijection of types, in HOL logic@bijection of types, in \HOL\ logic} 
from the type it defines to the corresponding subset of an existing type.  To
introduce constants that in fact denote such a bijection and its inverse, the
following \ML\ function is provided:

\begin{boxed}
\index{define_new_type_bijections@\ml{define\_new\_type\_bijections}|pin}
\begin{verbatim}
   define_new_type_bijections : string -> string -> string -> thm -> thm
\end{verbatim}\end{boxed}

\noindent This function takes three string arguments and a theorem argument.
The theorem argument must be a definitional axiom of the form returned by
\ml{new\_type\_definition}.  The first string argument is the name under which
the constant definition (a constant specification, in fact) made by
{\small\verb!define_new_type_bijections!} will be stored in the current theory
segment, and the second and third string arguments are user-specified names for
the two constants that are to be defined. These constants are defined so as to
denote mutually inverse bijections between the defined type, whose definition
is given by the supplied theorem, and the representing type of this defined
type.

Evaluating:

\medskip
{\def\op{{\normalsize\sl op}}
\begin{hol}\begin{alltt}
   define\_new\_type\_bijections `\m{name}` `\m{abs}` `\m{rep}` 
           |- ?rep:newty->ty. TYPE\_DEFINITION \m{P} rep
\end{alltt}\end{hol}}

\medskip

\noindent automatically defines two new constants
\m{abs}{\small\verb!:ty->newty!} and \m{rep}{\small\verb!:ty->newty!} 
such that:

{\def\bk{\char'134}
\begin{hol}\begin{alltt}
   |- (!a. \m{abs}(\m{rep} a) = a) /\bk (!r. \m{P} r = (\m{rep}(\m{abs} r) = r))
\end{alltt}\end{hol}}

\noindent This theorem, which is the defining property for the constants
\m{abs} and \m{rep}, is stored under the name `\m{name}` in the current theory
segment.  It is also the value returned by \ml{define\_new\_type\_bijections}.
The theorem states that \m{abs} is the left inverse of \m{rep} and---for
values satisfying \m{P}---that \m{rep} is the left inverse of \m{abs}.

A call to 
\ml{define\_new\_type\_bijections \m{name} \m{abs} \m{rep} \m{th}}
fails if:

\begin{myenumerate}
\item not in draft mode;
\item either $abs$ or $rep$ is already the name of a constant in
the current theory;
\item there already exists a constant definition, constant specification,
type definition or axiom named  \m{name} in the current theory; 
\item $th$ is not a theorem of the form returned by
\ml{new\_type\_definition}.
\end{myenumerate}%
\index{type definitions, in HOL logic@type definitions, in \HOL\ logic!defining bijections for|)}

\subsubsection{Properties of type bijections}
\index{type definitions, in HOL logic@type definitions, in \HOL\ logic!properties of bijections for|(}

The following \ML\ functions are provided for proving that the bijections
introduced by \ml{define\_new\_type\_isomorphisms} are injective (one-to-one)
and surjective (onto):

\begin{boxed}
\index{prove_rep_fn_one_one@\ml{prove\_rep\_fn\_one\_one}|pin}
\index{prove_rep_fn_onto@\ml{prove\_rep\_fn\_onto}|pin}
\index{prove_abs_fn_one_one@\ml{prove\_abs\_fn\_one\_one}|pin}
\index{prove_abs_fn_onto@\ml{prove\_abs\_fn\_onto}|pin}
\begin{verbatim}
   prove_rep_fn_one_one : thm -> thm
   prove_rep_fn_onto    : thm -> thm
   prove_abs_fn_one_one : thm -> thm
   prove_abs_fn_onto    : thm -> thm
\end{verbatim}\end{boxed}

\noindent The theorem argument to each of these functions must be a theorem 
of the form returned by \ml{define\_new\_type\_bijections}:

{\def\bk{\char'134}
\begin{hol}\begin{alltt}
   |- (!a. \m{abs}(\m{rep} a) = a) /\bk (!r. \m{P} r = (\m{rep}(\m{abs} r) = r))
\end{alltt}\end{hol}}

\noindent If \m{th} is a theorem of this form, then evaluating
\ml{prove\_rep\_fn\_one\_one \m{th}} proves that the function \m{rep} is
one-to-one, and returns the theorem:

\begin{hol}\begin{alltt}
   |- !a a'. (\m{rep} a = \m{rep} a') = (a = a')
\end{alltt}\end{hol}

\noindent Likewise, \ml{prove\_rep\_fn\_onto \m{th}} proves that \m{rep} is
onto the set of values that satisfy \m{P}:

{\def\bk{\char'134}
\begin{hol}\begin{alltt}
   |- !r. \m{P} r = (?a. r = \m{rep} a)
\end{alltt}\end{hol}}

\noindent Evaluating \ml{prove\_abs\_fn\_one\_one \m{th}} proves that \m{abs}
is one-to-one for values that satisfy \m{P}, and returns the theorem:

{\def\bk{\char'134}
\begin{hol}\begin{alltt}
   |- !r r'. \m{P} r ==> \m{P} r' ==> ((\m{abs} r = \m{abs} r') = (r = r'))
\end{alltt}\end{hol}}

\noindent And evaluating \ml{prove\_abs\_fn\_onto \m{th}} proves that \m{abs}
is onto, returning the theorem:

{\def\bk{\char'134}
\begin{hol}\begin{alltt}
   |- !a. ?r. (a = \m{abs} r) /\bk \m{P} r
\end{alltt}\end{hol}}

\noindent All four functions will fail if applied to any theorem that does not
have the form of a theorem returned by \ml{define\_new\_type\_bijections}.
None of these functions saves anything on the current theory file. In fact, it
should usually be unnecessary to save the results proved by these functions,
since they can be generated quickly whenever required from the theorem returned
by \ml{define\_new\_type\_bijections}, which is itself saved.  Of course,
within any given session, one would bind results to \ML\ identifiers.

\index{type definitions, in HOL logic@type definitions, in \HOL\ logic!properties of bijections for|)}

\subsection{Primitive ML function for accessing theories}

\index{theories, in HOL logic@theories, in \HOL\ logic!functions for accessing|(}
\index{axioms!retrieval of, in HOL system@retrieval of, in \HOL\ system|(}
The arguments of \ML\ type {\small\verb%string%} to {\small\verb%new_axiom%},
{\small\verb%new_definition%}
\etc\ are the names of the corresponding axioms and definitions. These
names are used when accessing theories with the functions
{\small\verb%axiom%}, {\small\verb%definition%}, \etc, described below.
%The various functions for setting up theories are illustrated in the
%example session in Section~\ref{example}.



A theory with no descendants\index{theories, in HOL logic@theories, in \HOL\ logic!hierarchies of}
can be extended by adding new parents, types,
constants, axioms and definitions. Theories that are already the parents of
other theories cannot be extended in this way.\footnote{It 
would be difficult
to implement the necessary checks to ensure that added types,
constants \etc\ did not invalidate declarations in the descendant theories.}
When a new theory is being created or an existing one extended it is necessary
to be in draft mode.\index{draft mode, in HOL system@draft mode, in \HOL\ system}
In proof mode\index{proof mode, in HOL system@proof mode, in \HOL\ system} the
functions with prefix `{\small\verb%new_%}'\index{new_@\ml{new\_}$\ldots$}
listed above are not available.  

The functions for entering an already existing theory in either draft mode
or proof mode are, respectively:


\begin{boxed}\index{extend_theory@\ml{extend\_theory}|pin}
\index{load_theory@\ml{load\_theory}|pin}
\begin{verbatim}
   extend_theory : string -> void
   load_theory   : string -> void
\end{verbatim}\end{boxed}

The function \ml{close\_theory}\index{close_theory@\ml{close\_theory}}
 (see Section~\ref{theoryprims}) is used 
to finish a session and write all new declarations to the theory file.

There are various functions for loading the contents of theory files:

\begin{boxed}
\index{parents@\ml{parents}|pin}
\index{types@\ml{types}|pin}
\index{constants@\ml{constants}|pin}
\index{infixes@\ml{infixes}|pin}
\index{binders@\ml{binders}|pin}
\index{axioms@\ml{axioms}|pin}
\index{definitions@\ml{definitions}|pin}
\index{theorems@\ml{theorems}|pin}
\begin{verbatim}
   parents     : string -> string list
   types       : string -> (int # string) list
   constants   : string -> term list
   infixes     : string -> term list
   binders     : string -> term list
   axioms      : string -> (string # thm) list
   definitions : string -> (string # thm) list
   theorems    : string -> (string # thm) list
\end{verbatim}\end{boxed}

\noindent The first argument is the name of a theory (which must be in the
ancestry of the current theory segment); the result is a list of the
components of the theory. The name of the current theory can be abbreviated by
\ml{`-`}.\index{ abbreviation, of HOL theory part names@\ml{-}
(abbreviation, of \HOL\ theory part names)} For example, \ml{parents `-`} returns the parents of the current
theory.

In the case of \ml{types} a list of arity-name pairs is returned; in the
case of  \ml{axioms}, \ml{definitions} or \ml{theorems} a list 
of string-theorem
pairs is returned, where the string is the name of the theorem representing the
axiom, definition or theorem that was supplied by the user. Note that constant
specifications and type definitions are both retrieved using the function
\ml{definitions}.



Individual axioms, definitions and theorems can be read (from the current or
ancestor theories) using the following \ML\ functions:



\begin{boxed}
\index{axiom@\ml{axiom}|pin}
\index{definition@\ml{definition}|pin}
\index{theorem@\ml{theorem}|pin}
\begin{verbatim}
   axiom      : string -> string -> thm
   definition : string -> string -> thm
   theorem    : string -> string -> thm
\end{verbatim}\end{boxed}

\noindent The  first  argument  is  the  theory (\ml{`-`}  can be  used for the
current theory); the second argument is  the user  supplied name  of the axiom,
definition or theorem in the theory.

Theories can be printed using the function
{\small\verb%print_theory%}\index{printing, in HOL logic@printing, in \HOL\ logic!of theories}\index{print_theory@\ml{print\_theory}}, 
which takes a theory name and then
prints out the named theory in a readable format.\index{axioms!retrieval of, in HOL system@retrieval of, in \HOL\ system|)}\index{theories, in HOL logic@theories, in \HOL\ logic!functions for accessing|)}


\subsection{The theory {\tt HOL}}
\label{HOL-theory}

%The types and constants in the built-in theories are listed here.  The axioms,
%definitions and pre-proved theorems are not listed; they can be found in
%Section~\ref{preproved-theories}.  

\index{axioms!primitive, of HOL logic@primitive, of \HOL\ logic|(}
At start-up,  the  initial  theory  for  users  of  the \HOL\  system is called
\ml{HOL}\index{HOL@\ml{HOL}}, which is constructed when the \HOL\ system is made.   The ancestry of
this theory is quite complicated and reflects, to  some extent,  the history of
\HOL\ as  it  evolved  from  \LCF.\index{LCF@\LCF}    This ancestry, which
is described  in detail in
Section~\ref{HOL-ancestry},  contains two important  theories: \ml{bool}\index{bool, the HOL theory@\ml{bool}, the \HOL\ theory} and
\ml{ind}. These define the primitive logical basis of the \HOL\ logic.

\subsubsection{The theory {\tt bool}}
\label{boolthy}

The theory {\small\verb%bool%} introduces the type
 {\small\verb%bool%} and
contains four of the five axioms\index{axioms!in bool theory@in \ml{bool} theory}
for higher order logic (the fifth axiom is
in the theory {\small\verb%ind%}).  These axioms, together with the rules
of inference described later in Section~\ref{rules}, constitute the core of the
\HOL\ logic.  Because of the way the \HOL\ system evolved from
\LCF,\index{LCF@\LCF}\footnote{To simplify the porting of the LCF theorem-proving
tools to the HOL system, the HOL logic was made as like PP$\lambda$ (the logic
built-in to LCF) as possible.} the particular axiomatization\index{axioms!non-primitive, of HOL logic@non-primitive, of \HOL\ logic} of
higher order logic it uses differs from the classical
axiomatization due to Church\index{Church, A.} \cite{Church}.  The biggest difference is that
in Church's formulation type variables\index{type variables, in HOL logic@type variables, in \HOL\ logic!differences from classical} are in the meta-language, whereas
in the \HOL\ logic they are part of the object language.

There are three primitive constants\index{constants, in HOL logic@constants, in \HOL\ logic!primitive logical}\index{primitive constants, of HOL logic@primitive constants, of \HOL\ logic}
 in the theory {\small\verb%bool%}\index{bool, the type in HOL logic@\ml{bool}, the type in \HOL\ logic}:
{\small\verb%=%}\index{ equality, in HOL logic@\ml{=} (equality, in \HOL\ logic)}\index{equality, in HOL logic@equality, in \HOL\ logic}
 (equality, an infix), {\small\verb%==>%}\index{ implication, in HOL logic@{\small\verb+==>+} (implication, in \HOL\ logic)} (implication, an
infix) and {\small\verb%@%}\index{ choice function, in HOL logic@{\small\verb+"@+} (choice function, in \HOL\ logic)}\index{choice operator, in HOL logic@choice operator, in \HOL\ logic} (choice, a binder).  Equality\index{equality, in HOL logic@equality, in \HOL\ logic}\index{implication, in HOL logic@implication, in \HOL\ logic}
 and implication
are standard predicate calculus notions, but choice is more exotic:
if $t$ is a term having type $\sigma${\small\verb%->bool%}, 
then {\small\verb%@x.%}$t${\small\verb% x%} (or, equivalently,
{\small\verb%$@%}$t$) denotes {\it some\/} member of the set whose
characteristic\index{characteristic predicate, of type definitions}
 function is $t$. If the set is empty, then
{\small\verb%@x.%}$t${\small\verb% x%} denotes an arbitrary member of the
set denoted by $\sigma$. The constant {\small\verb%@%} is a higher order
version of Hilbert's\index{Hilbert, D.}\index{epsilon operator}
 $\hilbert$-operator; it is related to the constant
$\iota$ in Church's formulation of higher order logic. For more details,
see Church's\index{Church, A.} original paper \cite{Church}, Leisenring's\index{Leisenring, A.} book 
on Hilbert's $\hilbert$-symbol \cite{Leisenring}, or
Andrews' textbook on type theory \cite{Andrews}.


The logical constants\index{logical constants, in HOL logic@logical constants, in \HOL\ logic} {\small\verb%T%}\index{truth values, in HOL logic@truth values, in \HOL\ logic!constants for}\index{T@\ml{T}!defined in terms of primitives} (truth), {\small\verb%F%}\index{F@\ml{F}!defined in terms of primitives} (falsity),
{\small\verb%~%} (negation)\index{ negation, in HOL logic@{\small\verb+~+} (negation, in \HOL\ logic)}, {\small\verb%/\%} (conjunction)\index{ conjunction, in HOL logic@{\small\verb+/\+} (conjunction, in \HOL\ logic)}\index{conjunction, in HOL logic@conjunction, in \HOL\ logic!defined in terms of primitives},
{\small\verb%\/%} (disjunction)\index{ disjunction, in HOL logic@{\small\verb+\/+} (disjunction, in \HOL\ logic)}\index{disjunction, in HOL logic@disjunction, in \HOL\ logic!defined in terms of primitives}, {\small\verb%!%} (universal
quantification)\index{ universal quantifier, in HOL logic@{\small\verb+"!+} (universal quantifier, in \HOL\ logic)}\index{universal quantifier, in HOL logic@universal quantifier, in \HOL\ logic!defined in terms of primitives}, {\small\verb%?%} (existential quantification)\index{ existential quantifier, in HOL logic@{\small\verb+?+} (existential quantifier, in \HOL\ logic)}\index{existential quantifier, in HOL logic@existential quantifier, in \HOL\ logic!defined in terms of primitives}
and {\small\verb%?!%} (unique existence quantifier)\index{ exists unique, in HOL logic@{\small\verb+?"!+} (exists unique, in \HOL\ logic)}\index{exists unique, in HOL logic@exists unique, in \HOL\ logic!defined in terms of primitives}
 can all
be defined in terms of equality\index{equality, in HOL logic@equality, in \HOL\ logic}, implication and choice.  The definitions
listed below are fairly standard; each one is preceded by its \ML\ name.
(Later definitions sometimes use earlier ones.) 


\begin{hol}
\index{truth values, in HOL logic@truth values, in \HOL\ logic!definition of}
\index{T_DEF@\ml{T\_DEF}}
\index{T@\ml{T}!definitional axiom for}
\index{disjunction, in HOL logic@disjunction, in \HOL\ logic!definitional axiom for}
\index{conjunction, in HOL logic@conjunction, in \HOL\ logic!definitional axiom for}
\index{iff, in HOL logic@iff, in \HOL\ logic!definitional axiom for}
\index{negation, in HOL logic@negation, in \HOL\ logic!definitional axiom for}
\index{exists unique, in HOL logic@exists unique, in \HOL\ logic}
\index{F@\ml{F}!axiom for}
\index{F@\ml{F}!definitional axiom for}
\index{ exists unique, in HOL logic@{\small\verb+?"!+} (exists unique, in \HOL\ logic)}
\index{T_DEF@\ml{T\_DEF}}
\index{FORALL_DEF@\ml{FORALL\_DEF}}
\index{EXISTS_DEF@\ml{EXISTS\_DEF}}
\index{AND_DEF@\ml{AND\_DEF}}
\index{OR_DEF@\ml{OR\_DEF}}
\index{F_DEF@\ml{F\_DEF}}
\index{NOT_DEF@\ml{NOT\_DEF}}
\index{EXISTS_UNIQUE_DEF@\ml{EXISTS\_UNIQUE\_DEF}}
\index{conjunction, in HOL logic@conjunction, in \HOL\ logic!definitional axiom for}
\index{disjunction, in HOL logic@disjunction, in \HOL\ logic!definitional axiom for}
\index{equality, in HOL logic@equality, in \HOL\ logic!primitive axiom for}
\index{existential quantifier, in HOL logic@existential quantifier, in \HOL\ logic!definitional axiom for}
\index{universal quantifier, in HOL logic@universal quantifier, in \HOL\ logic!definitional axiom for}
\index{exists unique, in HOL logic@exists unique, in \HOL\ logic!definitional axiom for}
\begin{verbatim}
   T_DEF              |- T   = ((\x:bool. x)=(\x. x))

   FORALL_DEF         |- $!  = \P:*->bool. P=(\x. T)

   EXISTS_DEF         |- $?  = \P:*->bool. P($@ P)

   AND_DEF            |- $/\ = \t1 t2. !t. (t1 ==> t2 ==> t) ==> t

   OR_DEF             |- $\/ = \t1 t2. !t. (t1 ==> t) ==> (t2 ==> t) ==> t

   F_DEF              |- F   = !t. t

   NOT_DEF            |- $~  = \t. t ==> F

   EXISTS_UNIQUE_DEF  |- $?! = (\P. $? P /\ (!x y. P x /\ P y ==> (x = y)))
\end{verbatim}\end{hol}


There are four\index{universal quantifier, in HOL logic@universal quantifier, in \HOL\ logic!in four primitive axioms} axioms in the theory {\small\verb%bool%}\index{bool, the HOL theory@\ml{bool}, the \HOL\ theory}:

\begin{hol}
\index{BOOL_CASES_AX@\ml{BOOL\_CASES\_AX}}
\index{IMP_ANTISYM_AX@\ml{IMP\_ANTISYM\_AX}}
\index{ETA_AX@\ml{ETA\_AX}}
\index{SELECT_AX@\ml{SELECT\_AX}}
\index{implication, in HOL logic@implication, in \HOL\ logic!primitive axiom for}
\index{ choice function, in HOL logic@{\small\verb+"@+} (choice function, in \HOL\ logic)}
\index{choice axiom}
\index{choice operator, in HOL logic@choice operator, in \HOL\ logic!primitive axiom for}
\begin{verbatim}
   BOOL_CASES_AX   |- !t. (t = T) \/ (t = F)

   IMP_ANTISYM_AX  |- !t1 t2. (t1 ==> t2) ==> (t2 ==> t1) ==> (t1 = t2)

   ETA_AX          |- !t. (\x. t x) = t

   SELECT_AX       |- !P:*->bool x. P x ==> P($@ P)
\end{verbatim}\end{hol}

\noindent
The fifth and last axiom of the \HOL\ logic is the Axiom of
Infinity;\index{axiom of infinity} this
is in the theory {\small\verb%ind%} described in Section~\ref{ind}.  

The theory {\small\verb%bool%} also supplies the definitions of a number of
useful constants. The constant {\small\verb%LET%}\index{let-terms, in HOL logic@\ml{let}-terms, in \HOL\ logic!constant for} is used in representing terms
containing local variable bindings (\ie\
{\small\verb%let%}-terms\index{let-terms, in HOL logic@\ml{let}-terms, in \HOL\ logic!definitional axiom for}, as discussed in Section~\ref{let}), and the
constant {\small\verb%COND%} is used in representing conditionals.  Both
constants are defined in the theory \ml{bool}, and have the following
definitions:

\begin{hol}
\index{LET_DEF@\ml{LET\_DEF}}
\index{COND_DEF@\ml{COND\_DEF}}
\index{COND@\ml{COND}}
\index{LET@\ml{LET}}
\index{conditional predicate, in HOL logic@conditional predicate, in \HOL\ logic!definitional axiom for}
\index{conditionals, in HOL logic@conditionals, in \HOL\ logic}
\begin{verbatim}
   LET_DEF      |- LET       = \f x. f x

   COND_DEF     |- COND      = \t t1 t2.@x.((t=T)==>(x=t1))/\((t=F)==>(x=t2))
\end{verbatim}\end{hol}

\noindent The theory \ml{bool} also contains the definitions of the constants
{\small\verb!RES_FORALL!}, {\small\verb!RES_EXISTS!},
{\small\verb!RES_SELECT!}, {\small\verb!ARB!} and {\small\verb!RES_ABSTRACT!},
which are used to support restricted
quantification\index{quantifiers!restricted} in the \HOL\ logic (see
Section~\ref{res-quant}).  The definitions are:

\begin{hol}
\index{RES_FORALL@\ml{RES\_FORALL}}
\index{RES_EXISTS@\ml{RES\_EXISTS}}
\index{RES_SELECT@\ml{RES\_SELECT}}
\index{RES_ABSTRACT@\ml{RES\_ABSTRACT}}
\index{ARB@\ml{ARB}}
\begin{verbatim}
   RES_FORALL   |- !P B. RES_FORALL P B = (!x. P x ==> B x)

   RES_EXISTS   |- !P B. RES_EXISTS P B = (?x. P x /\ B x)

   RES_SELECT   |- !P B. RES_SELECT P B = (@x. P x /\ B x)

   ARB          |- ARB = (@x. T)

   RES_ABSTRACT |- !P B. RES_ABSTRACT P B = (\x. (P x => B x | ARB))
\end{verbatim}\end{hol}

\noindent The theory \ml{bool} also contains the definitions of
the constants {\small\verb%ONE_ONE%} and
{\small\verb%ONTO%}, which are used in stating the Axiom of Infinity\index{axioms!non-primitive, of HOL logic@non-primitive, of \HOL\ logic}\index{axiom of infinity} (see Section~\ref{ind})\index{specification of constants, in HOL logic@specification of constants, in \HOL\ logic|)}. The definitions are:

\begin{hol}
\index{ONE_ONE_DEF@\ml{ONE\_ONE\_DEF}}
\index{ONTO_DEF@\ml{ONTO\_DEF}}
\index{one-to-one predicate, in HOL logic@one-to-one predicate, in \HOL\ logic!definitional axiom for}
\index{onto predicate, in HOL logic@onto predicate, in \HOL\ logic!definitional axiom for}
\begin{verbatim}
   ONE_ONE_DEF |- ONE_ONE f = (!x1 x2. (f x1 = f x2) ==> (x1 = x2))

   ONTO_DEF    |- ONTO f    = (!y. ?x. y = f x)
\end{verbatim}\end{hol}

\noindent For further discussion of the 
theory \ml{bool} see Section~\ref{boolfull}. 

\subsubsection{The theory {\tt ind}}
\label{ind}

The theory {\small\verb%ind%}\index{ind, the theory@\ml{ind}, the theory} introduces the type {\small\verb%ind%}\index{ind, the type@\ml{ind}, the type} of 
{\it individuals\/}\index{individuals}
 and the {\it Axiom of Infinity\/}\index{axiom of infinity}. This axiom states
that the set denoted by {\small\verb%ind%} is infinite. The four axioms of
the theory {\small\verb%bool%}, the rules of inference in
Section~\ref{rules} and the Axiom of Infinity are, together, sufficient for
developing all of standard mathematics. Thus, in principle, the user of the
\HOL\ system should never need to make a non-definitional\index{axioms!dispensibility of adding}\index{definitional theories} theory. In
practice, it is often very tempting to take the risk of introducing new
axioms because deriving them from definitions can be tedious---proving that
`axioms' follow from definitions amounts to proving their consistency.


The Axiom of Infinity\index{axioms!in ind theory@in \ml{ind} theory} is


{\begin{hol}
\index{INFINITY_AX@\ml{INFINITY\_AX}}
\index{axiom of infinity}
\index{existential quantifier, in HOL logic@existential quantifier, in \HOL\ logic!in infinity axiom}
\begin{verbatim}
  INFINITY_AX  |- ?f:ind->ind. ONE_ONE f /\ ~(ONTO f)
\end{verbatim}\end{hol}}

\noindent
This asserts that there exists a one-to-one map from {\small\verb%ind%} to
itself that is not onto. This implies that the type {\small\verb%ind%}
denotes an infinite set.\index{axioms!primitive, of HOL logic@primitive, of \HOL\ logic|)}

\subsection{Primitive rules of inference of the HOL Logic}
\label{rules}

\index{inference rules, of HOL logic@inference rules, of \HOL\ logic!primitive|(}
The primitive rules of inference of the logic were described abstractly
in Section~\ref{HOLrules}. The descriptions relied on meta-variables
$t$, $t_1$, $t_2$, and so on.
%In Section~\ref{avra_theorems}, a primitive inference was defined as
%a pair $(L,(\Gamma,t))$ belonging to 
%a deductive system. 
In the \HOL\ logic, infinite families of primitive
inferences are grouped together and thought of as single primitive inference
schemes.\index{families of inferences, in HOL logic@families of inferences, in \HOL\ logic}  Each family contains all the concrete instances of one
particular inference `pattern'. These can be produced, in
abstract form, by instantiating the meta-variables in Section~\ref{HOLrules}
to concrete terms.

In \HOL, primitive inference schemes are represented
by \ML\ functions that return theorems as values.
That is, for particular \HOL\ terms, the \ML\ functions return
the instance of the theorem at those terms. The \ML\ functions
are part of the \ML\ abstract type
 \ml{thm}\index{thm@\ml{thm}}:
although \ml{thm} has no primitive constructors, it has (eight)
operations which return theorems as values: \ml{ASSUME}, \ml{REFL}, 
\ml{BETA\_CONV}, \ml{SUBST}, \ml{ABS}, \ml{INST\_TYPE},
\ml{DISCH} and \ml{MP}.\index{inference schemes, in HOL logic@inference schemes, in \HOL\ logic}

The \ML\ functions that implement the primitive inference
schemes in the \HOL\
system are  described  below.
The same notation\index{notation!for specification of rules}\index{inferences, in HOL logic@inferences, in \HOL\ logic!notation for}
 is used here as in Section~\ref{HOLrules}:
hypotheses above a horizontal line and conclusion\index{conclusions!of inference rules} beneath.
The  machine-readable  {\small ASCII}
notation is used for the logical constants.

\subsubsection{Assumption introduction}\index{assumption introduction, in HOL logic@assumption introduction, in \HOL\ logic!ML function for@\ML\ function for} 

\begin{boxed}
\index{ASSUME@\ml{ASSUME}|pin}
\begin{verbatim}
   ASSUME : term -> thm
\end{verbatim}\end{boxed}

\begin{center}
\begin{tabular}{c}
  \\ \hline
$t${\small\verb% |- %}$t$ \\
\end{tabular}
\end{center}

\noindent
{\small\verb%ASSUME %}$t${\small\verb%%} evaluates to $t${\small\verb%|- %}$t$.
Failure if $t$ is not of type \ml{bool}.

\bigskip

\subsubsection{Reflexivity}\index{reflexivity, in HOL logic@reflexivity, in \HOL\ logic!ML function for@\ML\ function for} 

\begin{boxed}\index{REFL@\ml{REFL}|pin}
\begin{verbatim}
   REFL : term -> thm
\end{verbatim}\end{boxed}

\begin{center}
\begin{tabular}{c}
  \\ \hline
{\small\verb% |- %}$t${\small\verb% = %}$t$ \\
\end{tabular}
\end{center}

\noindent {\small\verb%REFL %}$t${\small\verb%%} evaluates to {\small\verb%|-
%}$t${\small\verb% = %}$t$. A call to \ml{REFL} never fails.

\bigskip

\subsubsection{Beta-conversion}\index{beta-conversion, in HOL logic@beta-conversion, in \HOL\ logic!ML function for@\ML\ function for} 

\begin{boxed}\index{BETA_CONV@\ml{BETA\_CONV}|pin}
\begin{verbatim}
   BETA_CONV : term -> thm
\end{verbatim}\end{boxed}

\begin{center}
\begin{tabular}{c}
  \\ \hline
{\small\verb% |- (\%}$x${\small\verb%.%}$t_1${\small\verb%)%}$t_2${\small\verb% = %}$t_1[t_2/x]$
\end{tabular}
\end{center}

\begin{itemize}
\item where $t_1[t_2/x]$ denotes the result of substituting $t_2$ for $x$
in $t_1$, with suitable renaming of variables to prevent free variables
in $t_2$ becoming bound after substitution. The substitution
 $t_1[t_2/x]$ is always defined.
\end{itemize}


\noindent {\small\verb%BETA_CONV (\%}$x${\small\verb%.%}$t_1${\small\verb%)%}$t_2${\small\verb%%} evaluates to the
theorem {\small\verb%|-
(\%}$x${\small\verb%.%}$t_1${\small\verb%)%}$t_2${\small\verb% = %}$t_1[t_2/x]$.
Failure if the argument to \ml{BETA\_CONV} is not a $\beta$-redex (\ie\ is not
of the form {\small\verb%(\%}$x${\small\verb%.%}$t_1${\small\verb%)%}$t_2${\small\verb%%}).

\bigskip

\subsubsection{Substitution}\index{substitution rule, in HOL logic@substitution rule, in \HOL\ logic!ML function for@\ML\ function for|(}\index{SUBST@\ml{SUBST}|(} 

\begin{boxed}
\begin{verbatim}
   SUBST : (thm # term)list -> term -> thm -> thm
\end{verbatim}\end{boxed}

\begin{center}
\begin{tabular}{c}
$\Gamma_1${\small\verb% |- %} $t_1${\small\verb%=%}$t'_1$ {\small\verb%  %} $\cdots$ {\small\verb%  %}
$\Gamma_n${\small\verb% |- %} $t_n${\small\verb%=%}$t'_n$ {\small\verb%  %} 
$\Gamma${\small\verb% |- %} $t[t_1,\ldots,t_n]$ \\ \hline
$\Gamma_1 \cup \cdots 
\cup \Gamma_n \cup \Gamma${\small\verb% |- %} $t[t'_1,\ldots,t'_n]$ \\
\end{tabular}
\end{center}

\bigskip

\begin{itemize}
\item where $t[t_1,\ldots,t_n]$ denotes a term $t$ with some free 
occurrences of the terms $t_1$, $\dots$, $t_n$ singled out and
$t[t'_1,\ldots,t'_n]$ denotes the result of simultaneously replacing each
such occurrences of $t_i$ by $t'_i$ (for $1{\leq}i {\leq} n$), 
with suitable renaming of variables to prevent free variables
in $t_i'$ becoming bound after substitution.
\end{itemize}

\noindent
The first argument to {\small\verb%SUBST%} is a list
{\small\verb%[(|-%}$t_1${\small\verb%=%}$t'_1${\small\verb%, %}$x_1${\small\verb%);%}$\:\ldots\:${\small\verb%;(|-%}$t_n${\small\verb%=%}
$t'_n${\small\verb%, %}$x_n${\small\verb%)]%}.  The second argument is a
template term $t[x_1,\ldots,x_n]$ in which occurrences of the variable
$x_i$ (where $1 \leq i\leq n$) are used to mark the places where
substitutions with {\small\verb%|- %}$t_i${\small\verb%=%}$t'_i$ are to be
done. Thus

\bigskip

{\small\verb%SUBST [(|-%}$t_1${\small\verb%=%}$t'_1${\small\verb%, %}$x_1${\small\verb%);%}$\ldots${\small\verb%;(|-%}$t_n${\small\verb%=%}
$t'_n${\small\verb%, %}$x_n${\small\verb%)]  %}$t[x_1,\ldots,x_n]${\small\verb%  %}
$\Gamma${\small\verb% |- %}$t[t_1,\ldots,t_n]$

\bigskip 

\noindent returns $\Gamma${\small\verb% |- %}$t[t'_1,\ldots,t'_n]$.
Failure if:
\begin{myenumerate}
\item any of the arguments are of the wrong form;
\item the type of $x_i$ is not equal to the type of $t_i$ for some
$1\leq i\leq n$.
\end{myenumerate}\index{SUBST@\ml{SUBST}|)}\index{substitution rule, in HOL logic@substitution rule, in \HOL\ logic!ML function for@\ML\ function for|)}

\subsubsection{Abstraction}\index{abstraction rule, in HOL logic@abstraction rule, in \HOL\ logic!ML function for@\ML\ function for} 
\index{function abstraction, in HOL logic@function abstraction, in \HOL\ logic!inference rules for}

 
\begin{boxed}\index{ABS@\ml{ABS}|pin}
\begin{verbatim}
   ABS : term -> thm -> thm
\end{verbatim}\end{boxed}


\begin{center}
\begin{tabular}{c}
$\Gamma${\small\verb% |- %}$t_1${\small\verb% = %}$t_2$ \\ \hline
$\Gamma${\small\verb% |- (\%}$x${\small\verb%.%}$t_1${\small\verb%) = (\%}$x${\small\verb%.%}$t_2${\small\verb%)%} \\
\end{tabular}
\end{center}

\begin{itemize}
\item where $x$ is not free in $\Gamma$.
\end{itemize}

\noindent 
{\small\verb%ABS %}$x${\small\verb% %}$\Gamma${\small\verb% |- %}$t_1${\small\verb%=%}$t_2$ returns the theorem
$\Gamma${\small\verb% |- (\%}$x${\small\verb%.%}$t_1${\small\verb%) = (\%}$x${\small\verb%.%}$t_2${\small\verb%)%}.
Failure if $x$ is not a variable, or $x$
 occurs free in any assumption in $\Gamma$.


\bigskip

\subsubsection{Type instantiation}\index{type instantiation, in HOL logic@type instantiation, in \HOL\ logic!ML function for@\ML\ function for}\index{types, in HOL logic@types, in \HOL\ logic!instantiation of}

\begin{boxed}\index{INST_TYPE@\ml{INST\_TYPE}|pin}
\begin{verbatim}
   INST_TYPE : (type#type) list -> thm -> thm
\end{verbatim}\end{boxed}

\begin{center}
\begin{tabular}{c}
$\Gamma${\small\verb% |- %}$t$ \\ \hline
$\Gamma${\small\verb% |- %}$t[\sigma_1,\ \ldots\ ,\sigma_n/\alpha_1,\ \ldots\ ,\alpha_n]$
\end{tabular}
\end{center}

\bigskip

\begin{itemize}
\item $t[\sigma_1,\ \ldots\ ,\sigma_n/\alpha_1,\ \ldots\ ,\alpha_n]$ 
denotes the result of substituting (in parallel) the types $\sigma_1$,
$\ldots$\ , $\sigma_n$ for the type variables $\alpha_1$, $\ldots$\ ,
$\alpha_n$ in $t$, with the restriction that none of $\alpha_1$, $\ldots$\
, $\alpha_n$ occur in $\Gamma$.
\end{itemize}

\noindent
{\small\verb%INST_TYPE[(%}$\sigma_1${\small\verb%,%}$\alpha_1${\small\verb%);%}$\ldots${\small\verb%;(%}$\sigma_n${\small\verb%,%}$\alpha_n${\small\verb%)] %}$th$
returns the result of instantiating each occurrence of $\alpha_i$ in the
theorem $th$ to $\sigma_i$ (for $1 \leq i \leq n$). Failure if:
\begin{myenumerate}
\item arguments of the wrong form (\eg\ an $\alpha_i$ is not a type variable);
\item $\alpha_i$
(for $1\leq i\leq n$) occurs in any assumption in $\Gamma$.
\end{myenumerate}

\bigskip

\subsubsection{Discharging an assumption}\index{discharging assumptions, in HOL logic@discharging assumptions, in \HOL\ logic!ML function for@\ML\ function for} 

 
\begin{boxed}\index{DISCH@\ml{DISCH}|pin}
\begin{verbatim}
   DISCH : term -> thm -> thm
\end{verbatim}\end{boxed}

\begin{center}
\begin{tabular}{c}
$\Gamma${\small\verb% |- %} $t_2$ \\ \hline
$\Gamma{-}\{t_1\}${\small\verb% |- %} $t_1${\small\verb% ==> %}$t_2$
\end{tabular}
\end{center}

\begin{itemize}
\item  $\Gamma{-}\{t_1\}$ denotes the set obtained by removing $t_1$ 
from $\Gamma$ (note that $t_1$ need not occur in $\Gamma$; in this case
$\Gamma{-}\{t_1\} = \Gamma$).
\end{itemize}

\noindent
{\small\verb%DISCH %}$t_1${\small\verb% %}$\Gamma${\small\verb% |- %}$t_2$
evaluates to the theorem
$\Gamma{-}\{t_1\}${\small\verb% |- %}$t_1${\small\verb% ==> %}$t_2$.
\ml{DISCH} fails if the term given as its first argument is not of 
type \ml{bool}.



\bigskip

\subsubsection{Modus Ponens}\index{Modus Ponens, in HOL logic@Modus Ponens, in \HOL\ logic!ML function for@\ML\ function for} 

 
\begin{boxed}\index{MP@\ml{MP}|pin}
\begin{verbatim}
   MP : thm -> thm -> thm
\end{verbatim}\end{boxed}

\begin{center}
\begin{tabular}{c}
$\Gamma_1${\small\verb% |- %}$t_1${\small\verb% ==> %}$t_2$ {\small\verb%     %} $\Gamma_2${\small\verb% |- %}$t_1$ \\
\hline
$\Gamma_1 \cup \Gamma_2${\small\verb% |- %}$t_2$ \\
\end{tabular}
\end{center}

\noindent
{\small\verb%MP%} takes two theorems (in the order shown above) and returns
the result of applying Modus Ponens; it fails if the arguments are not of the
right form.
\index{inference rules, of HOL logic@inference rules, of \HOL\ logic!primitive|)}

\subsection{Type abbreviations}\label{typeabbrev}\index{types, in HOL logic@types, in \HOL\ logic!abbreviation of}\index{type abbreviations!in HOL logic@in \HOL\ logic}\index{abbreviation of types, in HOL logic@abbreviation of types, in \HOL\ logic|(}
It is possible to introduce an abbreviation for a monomorphic type using the
function:

\begin{boxed}\index{new_type_abbrev@\ml{new\_type\_abbrev}|pin}
\begin{verbatim}
   new_type_abbrev : (string # type) -> void 
\end{verbatim}\end{boxed}

\noindent Evaluating \ml{new\_type\_abbrev(`}$name$\ml{`,":}$\sigma$\ml{")}
enables $name$ to be used in quotations instead of $\sigma$. The evaluation
fails
if $\sigma$ is polymorphic. Type abbreviations
are recorded in theory files, so that
when a theory is loaded, any type abbreviations made are
activated. The list of currently active abbreviations in a theory
is given by the function:

\begin{boxed}\index{type_abbrevs@\ml{type\_abbrevs}|pin}
\begin{verbatim}
   type_abbrevs : string -> (string # type) list
\end{verbatim}\end{boxed}


Note that abbreviation can also be made using antiquotation\index{antiquotation, in HOL logic terms@antiquotation, in \HOL\ logic terms}, without the
restriction to monomorphic types. Such \ML\ abbreviations are not, of course,
stored in theory files and so do not persist beyond a single session. 
The following session illustrates various ways of
abbreviating types:

\setcounter{sessioncount}{1}
\begin{session}\begin{verbatim}
#new_theory `numpair`;;
() : void

#new_type_abbrev(`numpair`, ":num#num");;
() : void

#let t1 = "x:numpair";;
t1 = "x" : term

#type_of t1;;
":num # num" : type

#":numpair" = ":num#num";;
true : bool
\end{verbatim}\end{session}

\noindent The alternative to introducing a type abbreviation is
to give an \ML\ name to the type, and then to use this name via antiquotation.
Continuing the session:\index{abbreviation of types, in HOL logic@abbreviation of types, in \HOL\ logic|)}

\begin{session}\begin{verbatim}
#let ty = ":num#num";;
ty = ":num # num" : type

#let t2 = "x:^ty";;
t2 = "x" : term

#t1 = t2;;
true : bool
\end{verbatim}\end{session}

\pagebreak[2]

\noindent The type abbreviation is stored in the theory file and so
persists across sessions. This can be seen by the result of printing
the theory \ml{numpair}:

\begin{session}\begin{verbatim}
#print_theory`numpair`;;
The Theory numpair
Parents --  HOL     
Type Abbreviations --  numpair ":num # num"     
******************** numpair ********************

() : void
\end{verbatim}\end{session}

\noindent If the session is then ended: 

\begin{session}\begin{verbatim}
#close_theory();;
() : void

#quit();;
\end{verbatim}\end{session}

\noindent and a new session is started in which the theory \ml{numpair} is
loaded:

\setcounter{sessioncount}{1}
\begin{session}\begin{verbatim}
#load_theory`numpair`;;
Theory numpair loaded
() : void

#"x:numpair";;
"x" : term

#type_abbrevs `-`;;
[(`numpair`, ":num # num")] : (string # type) list
\end{verbatim}\end{session}

\noindent then the type abbreviation persists.

Type abbreviations tend to be little used in practice; the antiquotation
method is usually sufficient.

\section{The ancestry of the theory {\tt HOL}}
\label{HOL-ancestry}

\index{theories, in HOL logic@theories, in \HOL\ logic!hierarchies of}
The ancestry\index{ancestry, of HOL system theories@ancestry, of \HOL\ system theories}
of the theory \ml{HOL}\index{HOL@\ml{HOL}} is:

\begin{center}\index{BASIC-HOL@\ml{BASIC-HOL}}\index{bool, the HOL theory@\ml{bool}, the \HOL\ theory}\index{ind, the theory@\ml{ind}, the theory}
\begin{picture}(65,115)

\thicklines


% -----------------------------------------------------------
% Lines in theory hierarchy graph
% -----------------------------------------------------------

\put(40,5){\line(-4,1){20}}      % HOL --> tydefs
\put(40,5){\line(0,1){5}}	 % HOL --> sum
\put(40,5){\line(4,1){20}}	 % HOL --> one

\put(20,15){\line(0,1){5}}	 % tydefs --> ltree
\put(40,15){\line(-2,3){10}}	 % sum    --> combin

\put(20,25){\line(-2,1){10}}	 % ltree --> tree
\put(20,25){\line(2,1){10}}	 % ltree --> combin

\put(10,35){\line(0,1){5}}	 % tree --> list
\put(10,45){\line(0,1){5}}	 % list --> arithmetic
\put(10,55){\line(0,1){5}}	 % arithmetics --> prim_rec
\put(10,65){\line(0,1){5}}	 % prim_rec --> num

\put(10,75){\line(4,1){20}}      % num --> BASIC-HOL

\put(30,85){\line(0,1){5}}	 % BASIC-HOL --> ind
\put(30,95){\line(0,1){5}}	 % ind --> bool
\put(30,105){\line(0,1){5}}	 % bool --> PPLAMB
\put(30,35){\line(0,1){45}}	 % combin --> BASIC-HOL
\put(60,20){\line(-1,2){30}}	 % one --> BASIC-HOL
\put(60,15){\line(0,1){5}}	 % one --> BASIC-HOL



% -----------------------------------------------------------
% Theory names:
% -----------------------------------------------------------

\put(40,2.5){\makebox(0,0){\verb!HOL!}}

\put(20,12.5){\makebox(0,0){\verb!tydefs!}}
\put(40,12.5){\makebox(0,0){\verb!sum!}}
\put(60,12.5){\makebox(0,0){\verb!one!}}

\put(20,22.5){\makebox(0,0){\verb!ltree!}}

\put(30,32.5){\makebox(0,0){\verb!combin!}}
\put(10,32.5){\makebox(0,0){\verb!tree!}}

\put(10,42.5){\makebox(0,0){\verb!list!}}
\put(10,52.5){\makebox(0,0){\verb!arithmetic!}}
\put(10,62.5){\makebox(0,0){\verb!prim\_rec!}}
\put(10,72.5){\makebox(0,0){\verb!num!}}
\put(30,82.5){\makebox(0,0){\verb!BASIC-HOL!}}
\put(30,92.5){\makebox(0,0){\verb!ind!}}
\put(30,102.5){\makebox(0,0){\verb!bool!}}
\put(30,112.5){\makebox(0,0){\verb!PPLAMB!}}

\end{picture}
\end{center}

\noindent In the rest of this section, each of the theories in  the ancestry of
\ml{HOL} is  briefly described.   A  complete list  of all  the definitions and
theorems in each theory is  not given  here; the sections  that follow provide
only an overview of the contents of each theory.  For a complete list of
all the built-in axioms, definitions and theorems in \HOL, see \REFERENCE.

\subsection{The theory {\tt PPLAMB}}

The most primitive theory is called \ml{PPLAMB}\index{PPLAMB@\ml{PPLAMB}} and is a stripped down
version of the theory underlying \LCF.\index{LCF@\LCF}  The \HOL\ theory \ml{PPLAMB}
does not represent any logical principles but is an interface between the \HOL\
and \LCF\ systems (it contains the declaration of the primitive 
type operator \ml{->}).\index{ function type operator, in HOL logic@\ml{->} (function type operator, in \HOL\ logic)}
For a while the two systems developed in parallel, upgrades in \LCF\ from
Gerard Huet\index{Huet, G.} of {\small INRIA} or Larry Paulson\index{Paulson, L.} of Cambridge resulting in
upgrades in \HOL.

\subsection{The theory {\tt bool}}\label{boolfull}

The theory \ml{bool}\index{bool, the HOL theory@\ml{bool}, the \HOL\ theory|(} has already been partly described in 
Section~\ref{boolthy}.
It contains the definitions of several sorts of constants:

\begin{myenumerate}


\item logical constants:\index{primitive constants, of HOL logic@primitive constants, of \HOL\ logic}\index{truth values, in HOL logic@truth values, in \HOL\ logic}\index{logical constants, in HOL logic@logical constants, in \HOL\ logic}\index{ implication, in HOL logic@{\small\verb+==>+} (implication, in \HOL\ logic)}\index{conjunction, in HOL logic@conjunction, in \HOL\ logic}\index{disjunction, in HOL logic@disjunction, in \HOL\ logic}%
\index{constants, in HOL logic@constants, in \HOL\ logic!logical}\index{ universal quantifier, in HOL logic@{\small\verb+"!+} (universal quantifier, in \HOL\ logic)}%
\index{ existential quantifier, in HOL logic@{\small\verb+?+} (existential quantifier, in \HOL\ logic)}%
\index{ conjunction, in HOL logic@{\small\verb+/\+} (conjunction, in \HOL\ logic)}%
\index{ disjunction, in HOL logic@{\small\verb+\/+} (disjunction, in \HOL\ logic)}%
\index{F@\ml{F}}\index{T@\ml{T}}%
\index{existential quantifier, in HOL logic@existential quantifier, in \HOL\ logic}%
\index{universal quantifier, in HOL logic@universal quantifier, in \HOL\ logic}
\index{ negation, in HOL logic@{\small\verb+~+} (negation, in \HOL\ logic)}
\index{ exists unique, in HOL logic@{\small\verb+?"!+} (exists unique, in \HOL\ logic)}%
\index{ choice function, in HOL logic@{\small\verb+"@+} (choice function, in \HOL\ logic)}%
\index{choice operator, in HOL logic@choice operator, in \HOL\ logic}%
\index{equality, in HOL logic@equality, in \HOL\ logic}%
\index{iff, in HOL logic@iff, in \HOL\ logic}%
\index{implication, in HOL logic@implication, in \HOL\ logic}%
\index{one-to-one predicate, in HOL logic@one-to-one predicate, in \HOL\ logic!in bool theory@in \ml{bool} theory}%
\index{onto predicate, in HOL logic@onto predicate, in \HOL\ logic!in bool theory@in \ml{bool} theory} \ml{T}, \ml{F}, {\small\verb!~!}, 
{\small\verb%/\%}, {\small\verb%\/%}, \ml{==>},
\ml{=}, \ml{!}, \ml{?}, \ml{?!} and \ml{@}.

\item system constants\index{constants, in HOL logic@constants, in \HOL\ logic!system}, which are used in coding \HOL\ theories as
\LCF\index{LCF@\LCF}\ theories. These constants are \ml{HOL\_DEFINITION}\index{HOL_DEFINITION@\ml{HOL\_DEFINITION}} and
\ml{BINDERS}\index{BINDERS@\ml{BINDERS}}. They have no logical significance 
and are part of the internal workings of the \HOL\ system.


\item miscellaneous constants\index{constants, in HOL logic@constants, in \HOL\ logic!abbreviational}%
\index{COND@\ml{COND}}%
\index{FCOND@\ml{FCOND}}%
\index{LET@\ml{LET}}%
\index{CURRY@\ml{CURRY}}%
\index{UNCURRY@\ml{UNCURRY}}%
\index{RES_FORALL@\ml{RES\_FORALL}}%
\index{RES_EXISTS@\ml{RES\_EXISTS}}%
\index{RES_SELECT@\ml{RES\_SELECT}}%
\index{RES_ABSTRACT@\ml{RES\_ABSTRACT}}%
\index{ARB@\ml{ARB}}, which support various special syntactic forms:
\ml{COND} (for conditionals, see Section~\ref{conditionals});
\ml{LET} (for \ml{let}-terms\index{let-terms, in HOL logic@\ml{let}-terms, in \HOL\ logic!constant for}, see Section~\ref{let-exp});
\ml{CURRY} and \ml{UNCURRY} (for paired abstractions, 
see Section~\ref{HOL-varstruct}); and
\ml{RES\_FORALL},
\ml{RES\_EXISTS},
\ml{RES\_SELECT},
\ml{RES\_ABSTRACT}
and \ml{ARB} (for restricted quantification, 
see Section~\ref{res-quant}).

\item the constant \ml{TYPE\_DEFINITION}\index{TYPE_DEFINITION@\ml{TYPE\_DEFINITION}}, which is used by the type definition mechanism\index{constants, in HOL logic@constants, in \HOL\ logic!for type definitions} (see Section~\ref{type-defs}).

\item constants associated with pairs:\index{constants, in HOL logic@constants, in \HOL\ logic!for pairs}
\index{MK_PAIR@\ml{MK\_PAIR}}
\index{IS_PAIR@\ml{IS\_PAIR}}
 \ml{,} (\ie\ the comma symbol), 
\ml{MK\_PAIR}, \ml{IS\_PAIR}, \ml{REP\_prod}, \ml{FST} and \ml{SND}
(see Section~\ref{prod} below).
\end{myenumerate}

\subsubsection{Pairs and the type {\tt prod}}
\label{prod}

\index{representing types, in HOL logic@representing types, in \HOL\ logic!pair example of|(}
\index{pairs, in HOL logic@pairs, in \HOL\ logic|(}
\index{product types!in HOL logic@in \HOL\ logic|(}
The Cartesian  product  type  operator\index{type operators, in HOL logic@type operators, in \HOL\ logic!for pairs}  \ml{prod}\index{prod@\ml{prod}} is  defined  in  the  theory \ml{bool}, although logically it should really  be in  a separate  theory.  The
reason for its  definition early  on is that certain  syntactic forms (\eg\
paired abstractions) presuppose pairs.  

Values of type
{\small\verb%(%}$\sigma_1${\small\verb%,%}$\sigma_2${\small\verb%)prod%} are
ordered pairs  whose  first  component  has  type  $\sigma_1$  and whose second
component has type $\sigma_2$.  The \HOL\ parser\index{parsing, of HOL logic@parsing, of \HOL\ logic!of pairs}
converts
type expressions of the 
form \ml{":}$\sigma_1${\small\verb%#%}$\sigma_2$\ml{"}\index{ product type operator, in HOL logic@{\small\verb+#+} (product
type operator, in \HOL\ logic)} into
\ml{(}$\sigma_1$\ml{,}$\sigma_2$\ml{)prod}\index{ pair constructor, in HOL
logic@\ml{,} (pair constructor, in \HOL\ logic)}, and   the   printer
inverts  this transformation. Pairs\index{pairing constructor, in HOL
logic@pairing constructor, in \HOL\ logic} are constructed with an infixed
comma symbol 


\begin{hol}\begin{verbatim}
   $, : * -> ** -> *#**
\end{verbatim}\end{hol}

\noindent so, for example, if $t_1$ and $t_2$ have types $\sigma_1$ and 
$\sigma_2$
respectively, then \ml{"}$t_1$\ml{,}$t_2$\ml{"} is a term with type
$\sigma_1${\small\verb%#%}$\sigma_2$. It is usual, but not necessary, to write
pairs within brackets:
\ml{"(}$t_1$\ml{,}$t_2$\ml{)"}. The comma symbol associates\index{pairing constructor, in HOL logic@pairing constructor, in \HOL\ logic!associativity of} to the right, so
 that
\ml{"(}$t_1$\ml{,}$t_2$\ml{,}$\ldots$\ml{,}$t_n$\ml{)"}
means
\ml{"(}$t_1$\ml{,(}$t_2$\ml{,}$\ldots$\ml{,}$t_n$\ml{))"}.
The constants 


\begin{hol}
\index{FST, the constant in HOL logic@\ml{FST}, the constant in \HOL\ logic}
\index{SND, the constant in HOL logic@\ml{SND}, the constant in \HOL\ logic}
\begin{verbatim}
   FST : * # ** -> *
   SND : * # ** -> **
\end{verbatim}\end{hol}

\noindent select the first and second components of pairs. 

Cartesian products are defined by representing a pair
{\small\verb%(%}$t_1${\small\verb%,%}$t_2${\small\verb%)%} by the function

\begin{hol}\begin{alltt}
   \verb!\!a b. (a=\m{t\sb{1}}) /\verb!\! (b=\m{t\sb{2}})
\end{alltt}\end{hol}

\noindent The
representing type of $\sigma_1${\small\verb%#%}$\sigma_2$ is thus
$\sigma_1${\small\verb%->%}$\sigma_2${\small\verb%->bool%}.  To define
pairs this way, the constants \ml{MK\_PAIR} and
\ml{IS\_PAIR}\index{IS_PAIR@\ml{IS\_PAIR}} are first defined.


\begin{hol}
\index{MK_PAIR_DEF@\ml{MK\_PAIR\_DEF}}
\index{IS_PAIR_DEF@\ml{IS\_PAIR\_DEF}}
\begin{verbatim}
   MK_PAIR_DEF   |- !x y. MK_PAIR x y = (\a b. (a = x) /\ (b = y))
 
   IS_PAIR_DEF   |- !p. IS_PAIR p = (?x y. p = MK_PAIR x y)
\end{verbatim}\end{hol}

\noindent From these two definitions it is easy to prove that:

\begin{hol}\begin{verbatim}
   |- ?p:*->**->bool. IS_PAIR p
\end{verbatim}\end{hol}

\noindent since {\small\verb%|- IS_PAIR(MK_PAIR x y)%} follows  easily from the
definition of  \ml{IS\_PAIR}.    The  existence  theorem shown  above is called
{\small\verb%PAIR_EXISTS%}\index{PAIR_EXISTS@\ml{PAIR\_EXISTS}}.
 Given this theorem, the type operator
{\small\verb%prod%} is defined by evaluating:

\begin{hol}\begin{verbatim}
   new_type_definition(`prod`, "IS_PAIR:(*->**->bool)->bool", PAIR_EXISTS)
\end{verbatim}\end{hol}

\noindent which results in the definitional axiom\index{axioms!non-primitive, of HOL logic@non-primitive, of \HOL\ logic}\index{axioms!in bool theory@in \ml{bool} theory} \ml{prod\_TY\_DEF} shown
below being asserted in the theory \ml{bool}.

\begin{hol}\begin{verbatim}
   prod_TY_DEF  |- ?rep. TYPE_DEFINITION IS_PAIR rep
\end{verbatim}\end{hol}

Next, a new constant {\small\verb%REP_prod%} is defined, which
maps a pair to its representation as a function:

\begin{hol}
\index{REP_prod@\ml{REP\_prod}}
\begin{verbatim}
   REP_prod    |- REP_prod =
                  (@rep : * # ** -> (* -> (** -> bool)).
                    (!p' p''. (rep p' = rep p'') ==> (p' = p'')) /\
                    (!p. IS_PAIR p = (?p'. p = rep p')))
\end{verbatim}\end{hol}

The infix constructor `{\small\verb%,%}'
and the selectors
{\small\verb%FST:*#**->*%} and {\small\verb%SND:*#**->**%} are then
defined by the equations shown below.



\begin{hol}\index{COMMA_DEF@\ml{COMMA\_DEF}}
\index{FST_DEF@\ml{FST\_DEF}}
\index{SND_DEF@\ml{SND\_DEF}}
\index{pairing constructor, in HOL logic@pairing constructor, in \HOL\ logic!definition of}
\index{FST, the constant in HOL logic@\ml{FST}, the constant in \HOL\ logic!definition of}
\index{selectors, in HOL logic@selectors, in \HOL\ logic}
\begin{verbatim}
   COMMA_DEF  |- !x y. x,y = (@p. REP_prod p = MK_PAIR x y)
 
   FST_DEF    |- !p. FST p = (@x. ?y. MK_PAIR x y = REP_prod p)

   SND_DEF    |- !p. SND p = (@y. ?x. MK_PAIR x y = REP_prod p)
\end{verbatim}\end{hol}

The following standard theorems about pairs follow easily from these
definitions and the axiom \ml{prod\_TY\_DEF}.  Although these theorems could
be derived by formal proof, they are (for implementation reasons) asserted as
axioms in the theory \ml{bool} .


\begin{hol}
\index{PAIR@\ml{PAIR}}
\index{FST, the axiom in HOL logic@\ml{FST}, the axiom in \HOL\ logic}
\index{SND, the axiom in HOL logic@\ml{SND}, the axiom in \HOL\ logic}
\index{PAIR_EQ@\ml{PAIR\_EQ}}
\begin{verbatim}
   PAIR     |- !x. (FST x,SND x) = x

   FST      |- !x y. FST(x,y) = x

   SND      |- !x y. SND(x,y) = y

   PAIR_EQ  |- !x y a b. (x,y = a,b)  =  (x = a) /\ (y = b)
\end{verbatim}\end{hol}
\index{pairs, in HOL logic@pairs, in \HOL\ logic|)}
\index{product types!in HOL logic@in \HOL\ logic|)}
\index{representing types, in HOL logic@representing types, in \HOL\ logic!pair example of|)}
\index{bool, the HOL theory@\ml{bool}, the \HOL\ theory|)}

\subsection{The theory {\tt BASIC-HOL}}

The theory \ml{BASIC-HOL}\index{BASIC-HOL@\ml{BASIC-HOL}} roughly corresponds to the theory
\theory{INIT}\index{initial theory, of HOL logic@initial theory, of \HOL\ logic}\index{INIT@\ml{INIT}!as BASIC_HOL@as \ml{BASIC\_HOL}} described in Section~\ref{INIT}. It consists of the data
in the theories \ml{bool} and \ml{ind}, together with one pre-proved theorem
called \ml{ABS\_REP\_THM}\index{ABS_REP_THM@\ml{ABS\_REP\_THM}}, which establishes various standard properties
of isomorphisms\index{isomorphism of types, in HOL logic@isomorphism of types, in \HOL\ logic} and is used for deriving consequences of type definitions (see 
Section~\ref{type-defs}).

The descendents of \ml{BASIC-HOL} are obtained by definitional extension.
There are, however, a few impurities in these extensions, with the result that
not all of them could be built purely by using the definitional mechanisms of
\HOL. The theory \ml{num}, for example, contains an infinite family of
constants \ml{0}, \ml{1}, \ml{2} \etc, but there is currently no 
mechanism that allows users to define such infinite families.

\subsection{The theory {\tt num}}

The theory \ml{num}\index{num, the theory in HOL logic@\ml{num}, the theory in \HOL\ logic}
defines the type \ml{num} of natural numbers to be
isomorphic to a countable subset of the primitive type \ml{ind}.  In this
theory, the constants \ml{0}\index{ zero, in HOL logic@\ml{0} ( zero, in \HOL\ logic)}
 and \ml{SUC} (the successor function) are defined
and Peano's axioms\index{axioms!in num theory@in \ml{num} theory}\index{Peano's axioms}\index{axioms!non-primitive, of HOL logic@non-primitive, of \HOL\ logic} pre-proved in the form:


\begin{hol}
\index{NOT_SUC@\ml{NOT\_SUC}}
\index{INV_SUC@\ml{INV\_SUC}}
\index{INDUCTION@\ml{INDUCTION}}
\begin{verbatim}
   NOT_SUC    |- !n. ~(SUC n = 0)
   INV_SUC    |- !m n. (SUC m = SUC n) ==> (m = n)
   INDUCTION  |- !P. P 0 /\ (!n. P n ==> P(SUC n)) ==> (!n. P n)
\end{verbatim}\end{hol}

In higher order logic, Peano's axioms are sufficient for developing number
theory because addition and multiplication can be defined. In first order
logic these must be taken as primitive.  Note also that
{\small\verb%INDUCTION%}\index{induction rule!for numbers, in HOL logic@for numbers, in \HOL\ logic} could not be stated as a single axiom in
first order logic because predicates (\eg\ {\small\verb%P%}) cannot be
quantified.

Uses of the theorem \ml{INDUCTION} are supported by the built-in derived
inference rule \ml{INDUCT}\index{INDUCT@\ml{INDUCT}}
and the built-in
tactic \ml{INDUCT\_TAC}\index{INDUCT_TAC@\ml{INDUCT\_TAC}}
(see the documentation on these functions in \REFERENCE\ for details).

\subsubsection{Numerals}

Associated with the theory \ml{num} are the numerals of type
\ml{:num}\index{num, the type in HOL logic@\ml{num}, the type in \HOL\ logic}, 
an infinite family of constants: \ml{1}, \ml{2}, \ml{3}, etc. These can
be regarded logically as an infinite collection of defined
constants, introduced by the infinite series of  definitional axioms:

\begin{hol}\begin{alltt}
   |- 1 = SUC 0{\rm ,  } |- 2 = SUC 1{\rm ,  } |- 3 = SUC 2{\rm ,\normalsize   etc.}
\end{alltt}\end{hol}

\noindent This infinite list of theorems cannot, of course, actually be 
stored in the theory \ml{num}.  \HOL\ therefore provides the \ML\ function:

\begin{boxed}
\index{num_CONV@\ml{num\_CONV}|pin}
\begin{verbatim}
   num_CONV : term -> thm
\end{verbatim}\end{boxed}

\noindent which can be used to generate the defining equation for any 
constant number of type \ml{num}.  For example:

\setcounter{sessioncount}{1}
\begin{session}\begin{verbatim}
#let thm1 = num_CONV "1";;
thm1 = |- 1 = SUC 0

#let thm2 = num_CONV "2";;
thm2 = |- 2 = SUC 1

#let thm3 = num_CONV "3141592653";;
thm3 = |- 3141592653 = SUC 3141592652
\end{verbatim}\end{session}

\noindent The defining equation for any numeral of type \ml{num} can likewise
be obtained using \ml{num\_CONV}.

\subsection{The theory {\tt prim\_rec}}
\label{prim_rec}

\index{primitive recursive definitions, in HOL logic@primitive recursive definitions, in \HOL\ logic!automated|(}
\index{primitive recursion theorem!for numbers|(}
\index{prim_rec@\ml{prim\_rec}|(}
In classical logic, unlike domain theory logics such as \PPL\index{PPlambda (same as PPLAMBDA), of LCF system@\ml{PP}$\lambda$ (same as \ml{PPLAMBDA}), of \ml{LCF} system},
arbitrary recursive definitions\index{recursive definitions, in classical logics} are not allowed. For example, there is no
function $f$ (of type \ml{num->num}) such that

\begin{hol}
{\small\verb%   !%}$x${\small\verb%. %}$f$ $x${\small\verb%  =  (%}$f$ $x${\small\verb%) + 1%}
\end{hol}

\noindent Certain restricted forms of recursive\index{primitive recursive
functions}
definition do, however, uniquely
define functions. An important example are the {\it primitive recursive\/}
functions.\footnote{In higher order logic, primitive recursion
is much more powerful than in first order logic;
for example, Ackermann's function can be defined
by primitive recursion in higher order logic.} For 
any $x$ and $f$ the {\it primitive
recursion theorem\/} tells us that there is a unique function
{\small\verb%fn%} such that:

\begin{hol}
{\small\verb%   %}\ml{fn}{\small\verb% 0 = %}$x${\small\verb%) /\ (!%}$n${\small\verb%.%}\ml{fn}{\small\verb%(%}\ml{SUC} $n${\small\verb%) = %}$f${\small\verb% (%}\ml{fn} $n${\small\verb%)%} $n${\small\verb%)%}
\end{hol}

The primitive recursion
 theorem follows from Peano's\index{Peano's axioms}
 axioms. When the \HOL\
system is built, the following theorem is proved and stored in the theory
{\small\verb%prim_rec%}:


\begin{hol}\index{num_Axiom@\ml{num\_Axiom}}
\index{characterizing theorem!for numbers}
\begin{verbatim}
   num_Axiom   |- !x f. ?!fn. (fn 0 = x) /\ (!n. fn(SUC n) = f (fn n) n)
\end{verbatim}\end{hol}

\noindent The theorem states the validity of primitive recursive
definitions on the natural numbers: for any \ml{x} and \ml{f} there exists a
corresponding total function \ml{fn} which satisfies
the primitive recursive definition whose form is determined by \ml{x} and
\ml{f}.

\subsubsection{Primitive recursive definitions}\label{num-prim-rec}

\index{type definitions, in HOL logic@type definitions, in \HOL\ logic!primitive recursive|(}
\index{recursive definitions, in HOL logic@recursive definitions, in \HOL\ logic!automated, for numbers|(}
\index{primitive recursion theorem!automated use of, in HOL system@automated use of, in \HOL\ system|(}
The primitive\index{primitive recursive definitions, in HOL logic@primitive recursive definitions, in \HOL\ logic!justification of} recursion theorem 
can be used to justify any  definition of a
function on the  natural numbers  by primitive  recursion.   For example, a
primitive recursive definition in higher order
logic of the form

\begin{hol}\begin{alltt}
   fun 0       x\(\sb{1}\) \m{\dots} x\(\sb{i}\) = \m{f\sb{1}[}x\(\sb{1}\)\m{,\ldots,\,} x\(\sb{i}]\) 
   fun (SUC n) x\(\sb{1}\) \m{\dots} x\(\sb{i}\) = \m{f\sb{2}[}fun n \m{t\sb{1} \dots t\sb{i},} n\m{,} x\(\sb{1}\)\m{,\ldots,\,}x\(\sb{i}]\)
\end{alltt}\end{hol}

\noindent where all the free variables in the  terms $t_1$,  
\dots, $t_i$ are contained in $\{$\ml{n}, $\ml{x}_1$, \dots, $\ml{x}_i\}$, 
is logically equivalent to:

\begin{hol}\begin{alltt}
   fun 0       = \verb!\!x\(\sb{1}\) \m{\dots} x\(\sb{i}\).\m{f\sb{1}[}x\(\sb{1}\)\m{,\ldots,\,}x\(\sb{i}]\)
   fun (SUC n) = \verb!\!x\(\sb{1}\) \m{\dots} x\(\sb{i}\).\m{f\sb{2}[}fun n \m{t\sb{1} \dots t\sb{i},} n\m{,}x\(\sb{1}\)\m{,\ldots,\,}x\(\sb{i}]\)
               = (\verb!\!f n x\(\sb{1}\) \m{\dots} x\(\sb{i}\).\m{f\sb{2}[}f \m{t\sb{1} \dots t\sb{i},} n\m{,} x\(\sb{1}\)\m{,\ldots,\,}x\(\sb{i}]\)) (fun n) n
\end{alltt}\end{hol}

The existence  of  a  recursive  function  \ml{fun} which  satisfies these two
equations follows directly from the primitive recursion theorem
\ml{num\_Axiom} shown above.   Specializing the  quantified variables \verb!x!
and \verb!f!  in  a suitably  type-instantiated version  of \ml{num\_Axiom} so
that

\begin{hol}\begin{alltt}
   x\m{=}\verb!\!x\(\sb{1}\) \(\dots\) x\(\sb{i}\).\m{f\sb{1}[}x\(\sb{1}\)\(,\ldots,\,\)x\(\sb{i}]\)  {\rm and}  f\(=\)\verb!\!f n x\(\sb{1}\) \(\dots\) x\(\sb{i}\).\m{f\sb{2}[}f \m{t\sb{1} \dots t\sb{i},} n\(,\) x\(\sb{1}\)\(,\ldots,\,\)x\(\sb{i}]\))
\end{alltt}\end{hol}

\noindent yields (ignoring the uniqueness of \ml{fn}) 
the existence theorem shown below:

\begin{hol}\begin{alltt}
   |- ?fn. fn 0       = \verb!\!x\(\sb{1}\) \(\dots\) x\(\sb{i}\).\m{f\sb{1}[}x\(\sb{1}\)\(,\ldots,\,\)x\(\sb{i}]\) /\verb!\!
           fn (SUC n) = (\verb!\!f n x\(\sb{1}\) \(\dots\) x\(\sb{i}\).\m{f\sb{2}[}f \m{t\sb{1} \dots t\sb{i},} n\(,\) x\(\sb{1}\)\(,\ldots,\,\)x\(\sb{i}]\)) (fn n) n
\end{alltt}\end{hol}

\noindent This theorem allows a constant \ml{fun} to be introduced (via the
definitional mechanism of constant specifications---see Section~\ref{conspec})
to denote the recursive function that satisfies the two equations in the body
of the theorem. Introducing a constant \ml{fun} to name the function asserted
to exist by the theorem shown above, and simplifying using $\beta$-reduction,
yields the following theorem:

\begin{hol}\begin{alltt}
   |- fun 0       = \verb!\!x\(\sb{1}\) \(\dots\) x\(\sb{i}\).\m{f\sb{1}[}x\(\sb{1}\)\(,\ldots,\,\)x\(\sb{i}]\) /\verb!\!
      fun (SUC n) = \verb!\!x\(\sb{1}\) \(\dots\) x\(\sb{i}\).\m{f\sb{2}[}fun n \m{t\sb{1} \dots t\sb{i},} n\(,\) x\(\sb{1}\)\(,\ldots,\,\)x\(\sb{i}]\)
\end{alltt}\end{hol}

\noindent It follows immediately from this theorem that the constant \ml{fun}
satisfies the primitive recursive defining equations given by the theorem shown
below:

\begin{hol}\begin{alltt}
   |- fun 0 x\(\sb{1}\) \(\dots\) x\(\sb{i}\) = \m{f\sb{1}[}x\(\sb{1}\)\(,\ldots,\,\)x\(\sb{i}]\)
      fun (SUC n) x\(\sb{1}\) \(\dots\) x\(\sb{i}\) = \m{f\sb{2}[}fun n \m{t\sb{1} \dots t\sb{i},} n\(,\) x\(\sb{1}\)\(,\ldots,\,\)x\(\sb{i}]\)
\end{alltt}\end{hol}

To automate the use of the primitive recursion theorem in deriving recursive
definitions of this kind, the \HOL\ system provides two functions which,
together, first do automatic proofs of the existence of primitive recursive
functions and then make constant specifications to introduce constants that
denote such functions:

\begin{boxed}
\index{new_prim_rec_definition@\ml{new\_prim\_rec\_definition}|pin}
\index{new_infix_prim_rec_definition@\ml{new\_infix\_prim\_rec\_definition}|pin}
\begin{verbatim}
   new_prim_rec_definition       : (string # term) -> thm
   new_infix_prim_rec_definition : (string # term) -> thm
\end{verbatim}\end{boxed}

\noindent Evaluating

\begin{hol}\begin{alltt}
   new_prim_rec_definition
    (`fun_DEF`,
     "(fun 0 x\(\sb{1}\) \(\dots\) x\(\sb{i}\) = \m{f\sb{1}[}x\(\sb{1}\)\(,\ldots,\,\)x\(\sb{i}]\) /\verb!\!
      (fun (SUC n) x\(\sb{1}\) \(\dots\) x\(\sb{i}\) = \m{f\sb{2}[}fun n \m{t\sb{1} \dots t\sb{i},} n\(,\) x\(\sb{1}\)\(,\ldots,\,\)x\(\sb{i}]\))");;
\end{alltt}\end{hol}

\noindent automatically proves the theorem:

\begin{hol}\begin{alltt}
   |-  ?fun. !x\(\sb{1}\) \(\dots\) x\(\sb{i}\). fun 0 x\(\sb{1}\) \(\dots\) x\(\sb{i}\) = \m{f\sb{1}[}x\(\sb{1}\)\(,\ldots,\,\)x\(\sb{i}]\) /\verb!\!
             !x\(\sb{1}\) \(\dots\) x\(\sb{i}\). fun (SUC n) x\(\sb{1}\) \(\dots\) x\(\sb{i}\) = \m{f\sb{2}[}fun n \m{t\sb{1} \dots t\sb{i},} n\(,\) x\(\sb{1}\)\(,\ldots,\,\)x\(\sb{i}]\)
\end{alltt}\end{hol}

\noindent and then declares a new constant \ml{fun} with this property as its
specification. This constant specification is returned as a theorem by 
\ml{new\_prim\_rec\_definition} and is saved with name \ml{fun\_DEF}\index{DEF@$\ldots$\ml{\_DEF}}
in the current theory segment. Failure occurs if:

\begin{myenumerate}
\item \HOL\ cannot prove there is a function satisfying the specification
(\ie\ if the term supplied to \ml{new\_prim\_rec\_definition} 
 is not a well-formed primitive recursive definition);
\item any other condition for making a constant specification is violated
(see the failure conditions for \ml{new\_specification} in 
Section~\ref{conspec}).
\end{myenumerate}

The \ML\  function \ml{new\_prim\_rec\_definition}  is, in  fact, slightly more
general than  is  indicated  by  the  example  application  shown  above.    In
particular, a  curried\index{currying, in ML@currying, in \ML!in primitive recursive definitions}
primitive  recursive  function defined  using this \ML\
function can be defined  by primitive  recursion on  any one  of its arguments.
For example, a curried  addition function  \ml{plus:num->num->num} can be
defined by primitive recursion on its first argument:

\setcounter{sessioncount}{1}
\begin{session}\begin{verbatim}
#let PLUS = new_prim_rec_definition           
#           (`PLUS`,
#            "(plus 0 n = n) /\                     
#             (plus (SUC m) n = SUC(plus m n))");;
PLUS = |- (!n. plus 0 n = n) /\ (!m n. plus(SUC m)n = SUC(plus m n))
\end{verbatim}\end{session}

\noindent or by primitive recursion on its second argument:

\setcounter{sessioncount}{1}
\begin{session}\begin{verbatim}
#let PLUS = new_prim_rec_definition
#            (`PLUS`,
#             "(plus m 0 = m) /\
#              (plus m (SUC n) = SUC(plus m n))");;
PLUS = |- (!m. plus m 0 = m) /\ (!m n. plus m(SUC n) = SUC(plus m n))
\end{verbatim}\end{session}

The \ML\ function \ml{new\_prim\_rec\_definition} also allows the user to
partially specify the value of a function defined (possibly recursively) on the
natural numbers by giving its value for only one of \ml{0} or \ml{SUC n}.  For
example, a decrement function \ml{DEC}, whose value is specified for only
positive natural numbers, can be defined using \ml{new\_prim\_rec\_definition}
as follows

\setcounter{sessioncount}{1}
\begin{session}\begin{verbatim}
#let DEC = new_prim_rec_definition
#            (`DEC`, "DEC (SUC n) = n");;
DEC = |- !n. DEC(SUC n) = n
\end{verbatim}\end{session}

\noindent This definition specifies the value of the function \ml{DEC} only for
positive natural numbers. In particular, the value of \ml{DEC 0} is left
unspecified, and the only non-trivial property that can be proved to hold of
the constant \ml{DEC} is the property stated by the theorem returned by the 
call to \ml{new\_prim\_rec\_definition} shown in the session above.

The function  \ml{new\_infix\_prim\_rec\_definition}\index{new_infix_prim_rec_definition@\ml{new\_infix\_prim\_rec\_definition}}
 can  be used  to define an
infixed\index{infixes, in HOL logic@infixes, in \HOL\ logic!primitive recursive definitions}
 function by primitive recursion on the natural numbers.  It takes its
arguments in the same form as \ml{new\_prim\_rec\_definition} and has similar
failure conditions; the only difference is that the resulting function constant
has infix status.  Here, for example, is the recursive definition of the
constant \ml{+} used by the system:

\begin{hol}\begin{verbatim}
   new_infix_prim_rec_definition
    (`ADD`,
     "($+ 0 n = n) /\
      ($+ (SUC m) n = SUC($+ m n))")
\end{verbatim}\end{hol}

\noindent The  {\small\verb%$%}'s  are  there (as documentation)
  to  indicate  that  the constant
{\small\verb%+%}\index{ addition, in HOL logic@\ml{+} (addition, in \HOL\ logic)}
is  being  declared  to  be  an  infix.   Evaluating this \ML\
expression will  create  the  following  constant specification  in the current
theory:

\begin{hol}\index{ADD@\ml{ADD}}
\begin{verbatim}
   ADD   |- (!n. 0 + n = n) /\ (!m n. (SUC m) + n = SUC(m + n))
\end{verbatim}\end{hol}

For further details about recursive definitions, see
Section~\ref{prim-rec-defs}, or the \REFERENCE\ documentation
on
\ml{new\_prim\_rec\_definition} and \ml{new\_infix\_prim\_rec\_definition}.
\index{prim_rec@\ml{prim\_rec}|)}
\index{primitive recursion theorem!automated use of, in HOL system@automated use of, in \HOL\ system|)}
\index{primitive recursion theorem!for numbers|)}
\index{primitive recursive definitions, in HOL logic@primitive recursive definitions, in \HOL\ logic!automated|)}
\index{recursive definitions, in HOL logic@recursive definitions, in \HOL\ logic!automated, for numbers|)}
\index{type definitions, in HOL logic@type definitions, in \HOL\ logic!primitive recursive|)}

\subsubsection{The less-than relation}

The less-than relation `{\small{\tt\verb+<+}}'\index{ less than, in HOL logic@\ml{<} (less than, in \HOL\ logic)}
  is most  naturally defined by
primitive recursion. However, it  is  needed for  
the proof of the primitive recursion theorem,
so  it  must  be  defined  
before definition  by primitive recursion is available.  
The theory \ml{prim\_rec} therefore  contains the following
non-recursive definition\index{less than, in HOL logic@less than, in \HOL\ logic} of \ml{<}:

\begin{hol}
\index{LESS@\ml{LESS}}
\begin{verbatim}
   LESS  |- !m n. m < n = (?P. (!n'. P(SUC n') ==> P n') /\ P m /\ ~P n)
\end{verbatim}\end{hol}

\noindent
This definition says that {\small\verb%"m < n%"} if there exists a set (with
characteristic function {\small\verb%P%}) that is downward
closed\footnote{A set of numbers is {\it downward closed\/} if whenever it
contains the successor of a number, it also contains the number.} and
contains {\small\verb%m%} but not {\small\verb%n%}.

\subsection{The theory {\tt arithmetic}}


The built-in theory {\small\verb%arithmetic%}\index{number theory, in HOL logic@number theory, in \HOL\ logic}
\index{arithmetic@\ml{arithmetic}} contains primitive recursive
definitions of following standard arithmetic operators.


\begin{hol}
\index{ADD@\ml{ADD}}
\index{SUB@\ml{SUB}}
\index{MULT@\ml{MULT}}
\index{EXP@\ml{EXP}}
\index{ subtraction, in HOL logic@\ml{-} (subtraction, in \HOL\ logic)}
\index{ multiplication, in HOL logic@\ml{*} (multiplication, in \HOL\ logic)}
\begin{verbatim}
   ADD            |- (!n. 0 + n = n) /\ 
                     (!m n. (SUC m) + n = SUC(m + n))

   SUB            |- (!m. 0 - m = 0) /\
                     (!m n. (SUC m) - n = (m < n => 0 | SUC(m - n)))

   MULT           |- (!n. 0 * n = 0) /\ 
                     (!m n. (SUC m) * n = (m * n) + n)

   EXP            |- (!m. m EXP 0 = 1) /\ 
                     (!m n. m EXP (SUC n) = m * (m EXP n))
\end{verbatim}\end{hol}

\noindent It also contains the following non-recursive definitions.

\begin{hol}
\index{arithmetic, in HOL logic@arithmetic, in \HOL\ logic}
\index{ greater than, in HOL logic@\ml{>} (greater than, in \HOL\ logic)}
\index{ less or equal, in HOL logic@\ml{<=} (less or equal, in \HOL\ logic)}
\index{ greater or equal, in HOL logic@\ml{>=} (greater or equal, in \HOL\ logic)}
\index{MOD@\ml{MOD}}
\index{DIV@\ml{DIV}}
\begin{verbatim}
   GREATER        |- !m n. m > n = n < m

   LESS_OR_EQ     |- !m n. m <= n = m < n \/ (m = n)

   GREATER_OR_EQ  |- !m n. m >= n = m > n \/ (m = n)

   DIVISION       |- !n. 0 < n ==> (!k. (k = ((k DIV n) * n) + (k MOD n)) /\ 
                                        (k MOD n) < n)
\end{verbatim}\end{hol}

An \adhoc\ but useful collection of over a hundred elementary theorems of
arithmetic are pre-proved when \HOL\ is built and stored in the theory
{\small\verb%arithmetic%}.  Each theorem will be autoloaded when its name is
first mentioned during any \HOL\ session. For a complete list of available
theorems, see \REFERENCE.

\subsection{The theory {\tt list}}\label{avra_list}\index{list, the type operator in HOL logic@\ml{list}, the type operator in \HOL\ logic} 

\index{recursive definitions, in HOL logic@recursive definitions, in \HOL\ logic!automated for lists|(}
\index{types, in HOL logic@types, in \HOL\ logic!tools for construction of}
\index{lists, in HOL logic@lists, in \HOL\ logic|(}
\index{list theory, in HOL logic@\ml{list} theory, in \HOL\ logic|(}
\index{ lists, in HOL logic@\ml{[} $\cdots$ \ml{;} $\cdots$ \ml{]} (lists, in \HOL\ logic)|(}
The theory \ml{list} introduces the unary type operator \ml{list} by a type
definition.\footnote{For details of the definition,
see~\cite{HOL-paper,Melham-banff}.}  The standard list processing functions
are then defined on this type:

\begin{hol}
\index{NIL@\ml{NIL}}
\index{CONS@\ml{CONS}}
\index{HD, the constant in HOL logic@\ml{HD}, the constant in \HOL\ logic}
\index{TL, the constant in HOL logic@\ml{TL}, the constant in \HOL\ logic}
\index{NULL, the constant in HOL logic@\ml{NULL}, the constant in \HOL\ logic}
\begin{verbatim}
   NIL  : (*)list 
   CONS : * -> (*)list -> (*)list
   HD   : (*)list -> *
   TL   : (*)list -> (*)list
   NULL : (*)list -> bool
\end{verbatim}\end{hol}

The \HOL\ parser\index{parsing, of HOL logic@parsing, of \HOL\ logic!of list expressions} has been specially  modified to  parse the expression
 {\small\verb%[]%} into
{\small\verb%NIL%} and to parse the expression
{\small\verb%[%}$t_1${\small\verb%;%}$t_2${\small\verb%;%}$\ldots${\small\verb%;%}$t_n${\small\verb%]%}
into {\small\verb%CONS %}$t_1${\small\verb% (CONS %}$t_2 \cdots\
${\small\verb%(CONS %}$t_n${\small\verb%  NIL)%}$\  \cdots\ ${\small\verb%)%}.
The \HOL\ printer\index{printing, in HOL logic@printing, in \HOL\ logic!of list expressions}
 reverses these transformations.

The functions \ml{NIL} and \ml{CONS} are defined in terms of the representing
type of lists. From their definitions, the following fundamental theorems about
lists\index{list theorems, in HOL logic@list theorems, in \HOL\ logic|(}
are proved and stored in the theory \ml{list}.

\begin{hol}
\index{list_Axiom@\ml{list\_Axiom}}
\index{axioms!non-primitive, of HOL logic@non-primitive, of \HOL\ logic}
\index{axioms!in list theory@in \ml{list} theory}
\index{list_INDUCT@\ml{list\_INDUCT}}
\index{list_CASES@\ml{list\_CASES}}
\index{CONS_11@\ml{CONS\_{11}}}
\index{NOT_NIL_CONS@\ml{NOT\_NIL\_CONS}}
\index{NOT_CONS_NIL@\ml{NOT\_CONS\_NIL}}
\index{characterizing theorem!for lists}
\begin{verbatim}
   list_Axiom    |- !x f. ?!fn.(fn[] = x) /\ (!h t. fn(CONS h t) = f(fn t)h t)

   list_INDUCT   |- !P. P[] /\ (!t. P t ==> (!h. P(CONS h t))) ==> (!l. P l)

   list_CASES    |- !l. (l = []) \/ (?t h. l = CONS h t)

   CONS_11       |- !h t h' t'. (CONS h t = CONS h' t') = (h = h') /\ (t = t')

   NOT_NIL_CONS  |- !h t. ~([] = CONS h t)

   NOT_CONS_NIL  |- !h t. ~(CONS h t = [])
\end{verbatim}\end{hol}

A derived rule\index{induction rule!for lists, in HOL logic@for lists, in \HOL\ logic}
 of structural induction called \ml{LIST\_INDUCT}\index{LIST_INDUCT@\ml{LIST\_INDUCT}}\index{LIST_INDUCT_TAC@\ml{LIST\_INDUCT\_TAC}} is provided,
together with an associated structural induction tactic \ml{LIST\_INDUCT\_TAC}.
These automate the use of the theorem \ml{list\_INDUCT}. See the \REFERENCE\ 
documentation  on these two functions for details.

The theorem  \ml{list\_Axiom}  shown  above  is  analogous   to  the  primitive
recursion theorem\index{primitive recursion theorem!for lists} on the
natural numbers discussed above in
Section~\ref{num-prim-rec}.  It  states  the  validity  of  primitive recursive
definitions on lists, and can be used to justify any such definition.  The \ML\
functions

\begin{boxed}
\index{new_list_rec_definition@\ml{new\_list\_rec\_definition}|pin}
\index{new_infix_list_rec_definition@\ml{new\_infix\_list\_rec\_definition}}
\begin{verbatim}
   new_list_rec_definition       : (string # term) -> thm
   new_infix_list_rec_definition : (string # term) -> thm
\end{verbatim}\end{boxed}

\noindent use this theorem to do automatic\index{primitive recursion theorem!automated use of, in HOL system@automated use of, in \HOL\ system}
 proofs of the existence of primitive
recursive functions on lists and then make constant specifications to introduce
constants that  denote  such  functions.   They are
analogous to the corresponding functions
\ml{new\_prim\_rec\_definition} and      \ml{new\_infix\_prim\_rec\_definition}
discussed in Section~\ref{num-prim-rec}.  For example, the \HOL\ system defines
a length function, \ml{LENGTH}, on lists  by 
the  primitive recursive  definition on lists
shown below:

\begin{hol}\begin{verbatim}
   new_list_rec_definition
     (`LENGTH`,
     "(LENGTH NIL = 0) /\
      (!h:*. !t. LENGTH (CONS h t) = SUC (LENGTH t))")
\end{verbatim}\end{hol}

\noindent When this \ML\
expression is evaluated, \HOL\ uses \ml{list\_Axiom} to prove existence 
of a function that satisfies the given primitive recursive definition, 
introduces a constant to name this function using a constant specification, and
stores the resulting theorem:


\begin{hol}\begin{verbatim}
    LENGTH   |- (LENGTH[] = 0) /\ (!h t. LENGTH(CONS h t) = SUC(LENGTH t))
\end{verbatim}\end{hol}

\noindent in the current theory (in this case, the theory \ml{list}).  

Using  \ml{new\_list\_rec\_definition},   the  predicate   \ml{NULL}  and  the
selectors \ml{HD} and  \ml{TL} are  defined\index{list definitions, in HOL logic@list definitions, in \HOL\ logic}
 in  the theory  \ml{list} by the
specifications:


\begin{hol}
\index{NULL, the definition in HOL logic@\ml{NULL}, the definition in \HOL\ logic}
\index{HD, the definition in HOL logic@\ml{HD}, the definition in \HOL\ logic}
\index{TL, the definition in HOL logic@\ml{TL}, the definition in \HOL\ logic}
\begin{verbatim}
   NULL |- NULL[] /\ (!h t. ~NULL(CONS h t))
 
   HD   |- !(h:*) t. HD(CONS h t) = h

   TL   |- !(h:*) t. TL(CONS h t) = t
\end{verbatim}\end{hol}

\noindent The following primitive recursive definitions of functions on lists 
are also made in the theory \ml{list}:


\begin{hol}
\index{SUM, the theorem in HOL logic@\ml{SUM}, the theorem in \HOL\ logic}
\index{APPEND, the theorem in HOL logic@\ml{APPEND}, the theorem in \HOL\ logic}
\index{concatenation, of lists!in HOL logic@in \HOL\ logic}
\index{FLAT, the theorem in HOL logic@\ml{FLAT}, the theorem in \HOL\ logic}
\index{LENGTH, the theorem in HOL logic@\ml{LENGTH}, the theorem in \HOL\ logic}
\index{MAP, the theorem in HOL logic@\ml{MAP}, the theorem in \HOL\ logic}
\index{EL, the theorem in HOL logic@\ml{EL}, the theorem in \HOL\ logic}

\index{SUM, the constant in HOL logic@\ml{SUM}, the constant in \HOL\ logic}
\index{APPEND, the constant in HOL logic@\ml{APPEND}, the constant in \HOL\ logic}
\index{FLAT, the constant in HOL logic@\ml{FLAT}, the constant in \HOL\ logic}
\index{LENGTH, the constant in HOL logic@\ml{LENGTH}, the constant in \HOL\ logic}
\index{MAP, the constant in HOL logic@\ml{MAP}, the constant in \HOL\ logic}
\index{EL, the constant in HOL logic@\ml{EL}, the constant in \HOL\ logic}
\index{EVERY, the HOL constant@\ml{EVERY}, the \HOL\ constant}
\index{EVERY_DEF@\ml{EVERY\_DEF}}
\begin{verbatim}
   SUM        |- (SUM[] = 0) /\ (!h t. SUM(CONS h t) = h + (SUM t))

   APPEND     |- (!l. APPEND[]l = l) /\
                 (!l1 l2 h. APPEND(CONS h l1)l2 = CONS h(APPEND l1 l2))

   FLAT       |- (FLAT[] = []) /\ (!h t. FLAT(CONS h t) = APPEND h(FLAT t))

   LENGTH     |- (LENGTH[] = 0) /\ (!h t. LENGTH(CONS h t) = SUC(LENGTH t))

   MAP        |- (!f. MAP f[] = []) /\
                 (!f h t. MAP f(CONS h t) = CONS(f h)(MAP f t))

   EL         |- (!l. EL 0 l = HD l) /\ (!l n. EL(SUC n)l = EL n(TL l))

   EVERY_DEF  |- (!P. EVERY P[] = T) /\
                 (!P h t. EVERY P(CONS h t) = P h /\ EVERY P t)
\end{verbatim}\end{hol}

\noindent There is also a selection of pre-proved theorems about lists stored
in the theory \ml{list}. These are autoloaded when their names are first
mentioned during any \HOL\ session. For a complete list of available theorems,
see \REFERENCE.

\index{list theorems, in HOL logic@list theorems, in \HOL\ logic|)} \index{
lists, in HOL logic@\ml{[} $\cdots$ \ml{;} $\cdots$ \ml{]} (lists, in \HOL\
logic)|)} \index{list theory, in HOL logic@\ml{list} theory, in \HOL\ logic|)}
\index{lists, in HOL logic@lists, in \HOL\ logic|)} \index{recursive
definitions, in HOL logic@recursive definitions, in \HOL\ logic!automated for
lists|)}

\subsection{The theory {\tt combin}}

\index{function composition, in HOL logic@function composition, in \HOL\
logic|(}

The theory \ml{combin}\index{combin@\ml{combin}}\index{combinators, in HOL
logic@combinators, in \HOL\ logic} contains the definitions of function
composition (infixed \ml{o})\index{ function composition operator, in HOL
logic@\ml{o} (function composition operator, in \HOL\ logic)|(}
 and the combinators \ml{S}\index{S, constant in HOL logic@\ml{S}, constant in
\HOL\ logic}, \ml{K}\index{K, the constant in HOL logic@\ml{K}, the constant in
\HOL\ logic}
 and \ml{I}\index{I, constant in HOL logic@\ml{I}, constant in \HOL\ logic}.


\begin{hol} \index{K_DEF@\ml{K\_DEF}} \index{S_DEF@\ml{S\_DEF}}
\index{I_DEF@\ml{I\_DEF}} \begin{verbatim}
   o_DEF |- !f g. f o g = (\x. f(g x))

   K_DEF |- K = (\x y. x)

   S_DEF |- S = (\f g x. f x(g x))

   I_DEF |- I = S K K \end{verbatim}\end{hol}


\noindent The following elementary properties are pre-proved in the theory
\ml{combin}:

\begin{hol} \index{K_THM@\ml{K\_THM}} \index{S_THM@\ml{S\_THM}}
\index{I_THM@\ml{I\_THM}} \index{I_o_ID@\ml{I\_o\_ID}} \begin{verbatim}
   o_THM |- !f g x. (f o g)x = f(g x)

   o_ASSOC |- !f g h. f o (g o h) = (f o g) o h

   K_THM |- !x y. K x y = x

   S_THM |- !f g x. S f g x = f x(g x)

   I_THM |- !x. I x = x

   I_o_ID |- !f. (I o f = f) /\ (f o I = f) \end{verbatim}\end{hol}

Having the symbols \ml{o}, \ml{S}, \ml{K} and \ml{I} as built-in
constants\index{variables, in HOL logic@variables, in \HOL\ logic!with constant
names} is sometimes inconvenient because they are often wanted as mnemonic
names for variables (\eg\ \ml{S} to range over sets and \ml{o} to range over
outputs).  These names may therefore be changed in future releases of the
system, or the theory \ml{combin} may be made into a library.  But variables
(though not constants) with these names can be used in the current system if
\ml{o}, \ml{S}, \ml{K} and \ml{I} are first hidden (see Section~\ref{hidden}).
\index{ function composition operator, in HOL logic@\ml{o} (function
composition operator, in \HOL\ logic)|)} \index{function composition, in HOL
logic@function composition, in \HOL\ logic|)}

\subsection{The theories {\tt tree} and {\tt ltree}} \index{recursive
definitions, in HOL logic@recursive definitions, in \HOL\ logic!automated, for
trees|(}

\index{labelled tree theory, in HOL logic@labelled \ml{tree} theory, in \HOL\
logic|(} The theories \ml{tree}\index{tree, the HOL theory@\ml{tree}, the \HOL\
theory}
 and \ml{ltree}\index{ltree, the HOL theory@\ml{ltree}, the \HOL\ theory|(}
 contain the definitions of two structurally-isomorphic types of
finitely-branching ordered trees.  The types defined in these theories are used
by Tom Melham's\index{Melham, T.} type definition package (see
Section~\ref{types-package}) to construct representations for arbitrary
concrete recursive types.  They are not intended for general use, and the
theorems stored in these two built-in theories are therefore not loaded into
the system at start-up.  The following is a summary of the main theorems which
are available in the theories \ml{tree} and \ml{ltree}, and which may be of use
in certain specialized applications.  For full details of the logical basis for
these two theories, see~\cite{Melham-banff}.

\subsubsection{The theory {\tt tree}}

In the theory \ml{tree}, a type \ml{tree}\index{tree, the HOL type@\ml{tree},
the \HOL\ type} is defined to denote the set of all ordered trees whose nodes
can branch any (finite) number of times.  A constructor function

\index{node@\ml{node}} \begin{hol}\begin{verbatim}
   node : (tree)list -> tree \end{verbatim}\end{hol}

\noindent is then defined in the theory \ml{tree}.  This function can be used
to construct any tree-structured value of type \ml{tree}.  The expression
\ml{"node []"} denotes the tree consisting of a single leaf node with no
subtrees.  If $tl\ml{:(tree)list}$ is a non-empty list of trees, then the term
$\ml{"node }tl\ml{"}$ denotes the tree whose immediate subtrees are the trees
in the list $tl$.  Using \ml{node}, it is possible to construct a tree of any
shape.  For example, the tree

\begin{center}
{\setlength{\unitlength}{0.75mm}
\begin{picture}(60,25)
\thicklines
\put(10,10){\makebox(0,0){$\bullet$}}
\put(30,10){\makebox(0,0){$\bullet$}}
\put(50,10){\makebox(0,0){$\bullet$}}
\put(30,20){\makebox(0,0){$\bullet$}}
\put(44,0){\makebox(0,0){$\bullet$}}
\put(56,0){\makebox(0,0){$\bullet$}}
\put(30,20){\line(0,-1){10}}
\put(30,20){\line(2,-1){20}}
\put(30,20){\line(-2,-1){20}}
\put(50,10){\line(3,-5){6}}
\put(50,10){\line(-3,-5){6}}
\end{picture}}
\end{center}

\noindent is denoted by the term 
\ml{"node[node[]; node[]; node[node[]; node[]]"}.

The next two theorems follow from the formal definition of \ml{node} and
are stored in the theory \ml{tree}:


\begin{hol}
\index{node_11@\ml{node\_{11}}}
\index{tree_Induct@\ml{tree\_Induct}}
\index{induction rule!for trees, in HOL logic@for \ml{trees}, in \HOL\ logic}
\begin{verbatim}
   node_11      |- !tl1 tl2. (node tl1 = node tl2) = (tl1 = tl2)
   tree_Induct  |- !P. (!tl. EVERY P tl ==> P(node tl)) ==> (!t. P t)
\end{verbatim}\end{hol}

\noindent These  two  theorems are  analogous to  the Peano\index{Peano's axioms}  postulates for the
natural numbers, and are used to prove the  following abstract characterization
of the defined type \ml{tree}.


\begin{hol}
\index{tree_Axiom@\ml{tree\_Axiom}}
\index{characterizing theorem!for finitely-branching ordered trees}
\begin{verbatim}
   tree_Axiom  |- !f. ?! fn. !tl. fn(node tl) = f(MAP fn tl)tl
\end{verbatim}\end{hol}

\noindent This theorem states the validity of general `primitive recursive'
definitions of functions over finitely-branching ordered trees.

\subsubsection{The theory {\tt ltree}}

In the theory \ml{ltree}\index{ltree@$\ldots$\ml{ltree}}, a type of {\it
labelled\/} trees (called \ml{(*)ltree}) is defined. Labelled trees have the
same sort of structure as values of the defined type \ml{tree} discussed above.
The only difference is that a tree of type \ml{(*)ltree} has a value or `label'
of type \ml{*} associated with each of its nodes.  A constructor

\begin{hol}
\index{Node@\ml{Node}}
\begin{verbatim}
   Node : * -> (*)ltree -> (*)ltree
\end{verbatim}\end{hol}

\noindent is defined in the theory \ml{ltree}.  The function \ml{Node}
constructs labelled trees by mapping a label of type \ml{*} and a list of
labelled subtrees to a labelled tree of type \ml{(*)ltree}.
The following theorems
 about labelled trees are pre-proved and stored in the
theory \ml{ltree}.


\begin{hol}
\index{Node_11@\ml{Node\_{11}}}
\index{ltree_Induct@\ml{ltree\_Induct}}
\index{ltree_Axiom@\ml{ltree\_Axiom}}
\begin{verbatim}
   Node_11       |- !v1 v2 trl1 trl2.
                    (Node v1 trl1 = Node v2 trl2) = (v1 = v2) /\ (trl1 = trl2)
   ltree_Induct  |- !P. (!t. EVERY P t ==> (!h. P(Node h t))) ==> (!l. P l)
   ltree_Axiom   |- !f. ?! fn. !v tl. fn(Node v tl) = f(MAP fn tl)v tl
\end{verbatim}\end{hol}

\noindent These  theorems  are  analogous to  their counterparts  in the theory
\ml{tree} discussed  above.    The theorems  \ml{Node\_{11}} 
and \ml{ltree\_Induct} amount to a Peano-type characterization of labelled
trees, and the theorem \ml{ltree\_Axiom} is a primitive recursion theorem for
labelled trees.
\index{labelled tree theory, in HOL logic@labelled \ml{tree} theory, in \HOL\ logic|)}
\index{ltree, the HOL theory@\ml{ltree}, the \HOL\ theory|)}
\index{recursive definitions, in HOL logic@recursive definitions, in \HOL\ logic!automated, for trees|)}

\subsection{The theory {\tt tydefs}}

The theory \ml{tydefs}\index{tydefs@\ml{tydefs}}
 is a  technical theory used
to support the type definition package described in
Section~\ref{types-package}.  The main result proved in \ml{tydefs} is a
theorem called \ml{TY\_DEF\_THM}\index{TY_DEF_THM@\ml{TY\_DEF\_THM}},
 which is used by the type definition package
to derive automatically abstract characterizations for
arbitrary concrete recursive types.  The interested reader can find the
details of the derivation of \ml{TY\_DEF\_THM} in~\cite{Melham-banff}.

\subsection{The theory {\tt sum}}
\label{sum}


The theory \ml{sum}\index{sum@\ml{sum}}
 defines the binary  disjoint union\index{disjoint union theory, in HOL
logic@disjoint union theory, in \HOL\ logic|(}  type operator \ml{sum}.
A type  {\small\verb%(%}$\sigma_1${\small\verb%,%}$\sigma_2${\small\verb%)sum%}
denotes the  disjoint  union  of  types  $\sigma_1$  and $\sigma_2$.   The type
operator {\small\verb%sum%} can be defined just as {\small\verb%prod%} was, but
the details are omitted here.\footnote{The definition of disjoint  unions in 
the
HOL system is due to Tom Melham. The technical details of this definition can 
be found in~\cite{Melham-banff}.}  The \HOL\ parser\index{parsing, of HOL logic@parsing, of \HOL\ logic!of sum types}
converts
\ml{":}$\sigma_1${\small\verb%+%}$\sigma_2$\ml{"}\index{ disjoint union
type operator, in HOL logic@\ml{+} (disjoint union
type operator, in HOL logic)} into
\ml{(}$\sigma_1$\ml{,}$\sigma_2$\ml{)sum}, and the printer inverts this.

The standard operations on sums are:


\begin{hol}
\index{disjoint union theory, in HOL logic@disjoint union theory, in \HOL\ logic|)}
\index{INL, the constant in HOL logic@\ml{INL}, the constant in \HOL\ logic}
\index{INR, the constant in HOL logic@\ml{INR}, the constant in \HOL\ logic}
\index{ISL, the constant in HOL logic@\ml{ISL}, the constant in \HOL\ logic}
\index{ISR, the constant in HOL logic@\ml{ISR}, the constant in \HOL\ logic}
\index{OUTL, the constant in HOL logic@\ml{OUTL}, the constant in \HOL\ logic}
\index{OUTR, the constant in HOL logic@\ml{OUTR}, the constant in \HOL\ logic}
\begin{verbatim}
   INL  : *      -> * + ** 
   INR  : **     -> * + **
   ISL  : * + ** -> bool 
   ISR  : * + ** -> bool
   OUTL : * + ** -> * 
   OUTR : * + ** -> **
\end{verbatim}\end{hol}

\noindent These are all defined as constants in the theory \ml{sum}.  The
constants \ml{INL} and \ml{INR} inject into the left and right summands,
respectively. The constants \ml{ISL} and \ml{ISR} test for membership of the
left and right summands, respectively. The constants \ml{OUTL} and \ml{OUTR}
project from a sum to the left and right summands, respectively.

The following two theorems, which are minor variants of each other, are
pre-proved in the built-in theory \ml{sum}. Each one, on its own, provides a
complete and abstract characterization of the disjoint sum type.

\begin{hol}
\index{sum_Axiom@\ml{sum\_Axiom}}
\index{sum_axiom@\ml{sum\_axiom}}
\begin{verbatim}
   sum_axiom   |- !f g. ?! h. (h o INL = f) /\ (h o INR = g)

   sum_Axiom = |- !f g. ?! h. (!x. h(INL x) = f x) /\ (!x. h(INR x) = g x)
\end{verbatim}\end{hol}

\noindent Also provided as built-in, are the following theorems having to
do with the discriminator functions \ml{ISL} and \ml{ISR}:

\begin{hol}
\index{ISL, the theorem in HOL logic@\ml{ISL}, the theorem in \HOL\ logic}
\index{ISR, the theorem in HOL logic@\ml{ISR}, the theorem in \HOL\ logic}
\index{ISL_OR_ISR@\ml{ISL\_OR\_ISR}}
\begin{verbatim}
   ISL         |- (!x. ISL(INL x)) /\ (!y. ~ISL(INR y))

   ISR         |- (!x. ISR(INR x)) /\ (!y. ~ISR(INL y))

   ISL_OR_ISR  |- !x. ISL x \/ ISR x
\end{verbatim}\end{hol}

\noindent The \ml{sum} theory also provides the following built-in theorems:

\begin{hol}
\index{OUTL, the theorem in HOL logic@\ml{OUTL}, the theorem in \HOL\ logic}
\index{OUTR, the theorem in HOL logic@\ml{OUTR}, the theorem in \HOL\ logic}
\index{INL, the theorem in HOL logic@\ml{INL}, the theorem in \HOL\ logic}
\index{INR, the theorem in HOL logic@\ml{INR}, the theorem in \HOL\ logic}
\begin{verbatim}
   OUTL        |- !x. OUTL(INL x) = x

   OUTR        |- !x. OUTR(INR x) = x

   INL         |- !x. ISL x ==> (INL(OUTL x) = x)

   INR         |- !x. ISR x ==> (INR(OUTR x) = x)
\end{verbatim}\end{hol}

\noindent which describe the projection functions \ml{OUTL} and \ml{OUTR}.

\subsection{The theory {\tt one}}%
\index{one, the HOL theory@\ml{one}, the \HOL\ theory}%
\index{one, the HOL type@\ml{one}, the \HOL\ type}%

The theory \ml{one} defines  the type  \ml{":one"} which  contains one element.
The constant  \ml{one}  is specified  to denote  this element.   The pre-proved
theorems in the theory \ml{one} are:

\begin{hol}
\index{one_axiom@\ml{one\_axiom}}
\index{one, the HOL theorem@\ml{one}, the \HOL\ theorem}
\index{one_Axiom@\ml{one\_Axiom}}
\begin{verbatim}
   one_axiom   |- !(f:* -> one) (g:* -> one). f = g
   one         |- !(v:one). v = one
   one_Axiom   |- !(e:*). ?!(fn:one->*). fn one = e
\end{verbatim}\end{hol}

\noindent These three theorems are equivalent characterizations of the type 
with only one value.

\section{The type definition package}\label{types-package}\index{extension, of HOL logic@extension, of \HOL\ logic!by type definition|(}
\index{type definition package, in HOL system@type definition package, in \HOL\ system|(}

In the \HOL\  system, new types and type operators can be introduced\index{extension, of HOL logic@extension, of \HOL\ logic}
using the  consistency-preserving definitional  mechanism  of  
type  definitions\index{type definition extension, in HOL logic@type definition extension, in \HOL\ logic|(} (see Sections~\ref{tydefs} and~\ref{type-defs}).  The \ML\
rule for introducing a new type is:

\begin{hol}
\index{new_type_definition@\ml{new\_type\_definition}}
\begin{verbatim}
   new_type_definition : (string # term # thm) -> thm
\end{verbatim}\end{hol}

\noindent This rule allows
axioms of a restricted form to be added to the primitive basis of the logic.
These axioms are analogous to definitional axioms for new constants: they
define new types in terms of other type expressions already present in the
logic. Like the rule \ml{new\_definition} for making constant definitions, 
the rule  \ml{new\_type\_definition}
 for type definitions
ensures that adding a new syntactic entity (in this case, a type or 
type operator) is a conservative extension of the logic. 

The basic idea behind \ml{new\_type\_definition} is  that a  type definition is
made by  adding an  axiom to  the logic  which asserts  that the  set of values
denoted by a  new type  is isomorphic\index{isomorphism of types, in HOL logic@isomorphism of types, in \HOL\ logic}  to an  appropriate subset  of the values
denoted by  a type  expression already  present in  the logic.   A definitional
axiom\index{definitional axioms} of this form merely states  
that a  new type  is isomorphic  to a particular
subset of an existing type.  From such type definition  axioms, it  is usual to
prove theorems that characterize newly-defined types more abstractly.  The idea
is to prove a collection of theorems that state  the essential  properties of a
new type without reference to how it is defined.   These  theorems then
constitute a derived `abstract axiomatization' of the new type, and 
once  they have  been proved they
become the basis for all further reasoning about it.  

With this approach, introducing a new type (or type operator) in \HOL\
involves two distinct steps:

\setcounter{myenumi}{1}
\begin{list}{\arabic{myenumi}.}{\usecounter{myenumi}
\setlength{\leftmargin}{10mm}
\setlength{\rightmargin}{5mm}
\setlength{\labelwidth}{3mm}
\setlength{\labelsep}{2mm}
\setlength{\listparindent}{0mm}
\setlength{\itemsep}{8pt plus1pt minus2pt}
\setlength{\topsep}{3mm}
\setlength{\parsep}{0mm}}
\setlength{\abovedisplayshortskip}{8pt plus1pt minus1pt} 
\setlength{\belowdisplayshortskip}{8pt plus1pt minus1pt}

\item Finding an appropriate representation for the new type, and making a type \mbox{definition} using \ml{new\_type\_definition} based
 on this representation.

\item Using the axiomatic definition of the new type and the properties of its
representation to prove a set of theorems that abstractly characterizes it.

\end{list}

Defining a new type using this approach can be hard work.  But a set of tools
is provided in the system which---for a certain class of commonly-used {\it
concrete recursive types\/}\index{types, in HOL logic@types, in \HOL\
logic!tools for construction of}\index{concrete recursive types, in HOL
logic@concrete recursive types, in \HOL\ logic}---automatically carries out all
the formal proofs necessary to define these types and derive abstract
characterizations from their definitions.  This section provides a user-level
overview of these tools.  Details of the formal proofs carried out by these
tools are discussed in~\cite{Melham-banff}.

\subsection{Defining types}

\index{type definition package, in HOL system@type definition package, in \HOL\ system!use of, in recursive type definition|(}
\index{recursive types, in HOL logic@recursive types, in \HOL\ logic!tools for construction of|(}
The main \ML\ function in the \HOL\ type definition package is

\begin{boxed}
\index{define_type@\ml{define\_type}|pin}
\begin{verbatim}
   define_type : string -> string -> thm
\end{verbatim}\end{boxed}

\noindent This function can be used to define any concrete recursive type in
the \HOL\ system.  These are types whose values are generated by a set of {\it
constructors\/} (i.e.\ functions) which yield concrete representations for
these values.  Examples include types which denote finite sets of atomic values
(enumerated types), types which denote sets of structured values (record types)
or finite disjoint unions of structured values (variant records), and types
which denote sets of recursive data structures (recursive types).

The two inputs to \ml{define\_type} are both strings.  The first string
is a name  under which the results of making
the type definition will be stored in the current theory segment. The second is
a user-supplied informal\footnote{ In this context, {\it informal\/} means not
in the language of higher order logic.} specification of the concrete
recursive type to be defined.  This type specification is written in 
a notation (explained below) which resembles
a data type declaration in functional programming languages like Standard
\ML~\cite{sml}.  It simply states the names of the new type's
constructors and the logical types of their \mbox{arguments.}  The output is a
theorem which abstractly characterizes the properties of
the desired recursive type---i.e.\ a 
derived `abstract axiomatization' of the type.

\subsubsection{Input syntax}

The type specification given as input\index{type definition package, in HOL system@type definition package, in \HOL\ system!input to|(} to \ml{define\_type}
must be an \ML\ string\index{strings, in ML@strings, in \ML!as input to HOL type definition package@as input to \HOL\ type definition package} (of \ML\ type \ml{string}) of the form:

{\def\op{{\normalsize\sl op}}
\begin{hol}\begin{alltt}
   `{\op} = \(C\sb{1}\;ty{}\sb{1}\sp{1}\;\ldots\;ty{}\sb{1}\sp{k\sb{1}} \) | \(\cdots\) | \(C\sb{m}\;ty{}\sb{m}\sp{1}\;\ldots\;ty{}\sb{m}\sp{k\sb{m}}\)`
\end{alltt}\end{hol}}

\noindent where each $ty_i^{j}$ is either a type expression already defined
as a type in the current theory (this type expression must not
contain \ty{op}) or is the name \ty{op} itself.  A string of this form
describes an $n$-ary type operator \ty{op},
where $n$ is the number of distinct type variables in the types 
$ty_i^{j}$ on the right hand side of the equation.
If $n = 0$ then \ty{op} is a type constant;
otherwise \ty{op} is an $n$-ary type operator.  The concrete 
type described has $m$
distinct constructors \m{C_1, \dots, C_m} where $m \geq 1$.
Each constructor \m{C_i} takes $k_i$ arguments, where $k_i \geq 0$;
and the types of these arguments are given by the type
expressions $ty_i^j$ for $1 \leq j \leq k_i$.  If one or more of the type
expressions $ty_i^{j}$ is the type \ty{op} itself, then the \mbox{equation}
specifies a {\it recursive\/} type.  In any specification of a recursive type,
at least one constructor must be non-recursive---i.e.\ all its arguments must
have types which already exist in the current theory.

The input parser for \ml{define\_type} treats type expressions exactly as the
\HOL\ quotation parser does, with precedences among the various built-in type
operators in force.\index{type definition package, in HOL system@type definition package, in \HOL\ system!input to|)}

\subsubsection{The type specified}

The logical type described by an input string of the form
shown above is intended
to denote the set of all values which can be finitely 
generated using the constructors \m{C_1, \dots, C_m}, 
where each constructor is one-to-one and any two
different constructors yield different values.   Every value of this 
type will be denoted by some term of the form:

\[ C_i\;x_i^1\;\ldots\;x_i^{k_i} \]

\noindent where $x_i^j$ is a term of type $ty_i^j$ for $1 \leq j \leq
k_i$. In addition, any two terms:

\[ C_i\;x_i^1\;\ldots\;x_i^{k_i} \qquad {\rm and} \qquad
   C_j\;x_j^1\;\ldots\;x_j^{k_j} \]

\noindent denote equal values exactly when their constructors are the same
(i.e.\ $i=j$) and these constructors are applied to equal arguments 
(i.e.\ $x_i^n = x_j^n\;\,{\rm for}\;\,1\leq n\leq k_i$).  

\subsubsection{The output}\label{define-type-output}
\index{type definition package, in HOL system@type definition package, in \HOL\ system!output of|(}

\noindent For any type specification 
in the form of an equation of the kind discussed above, executing:

{\def\op{{\normalsize\sl op}}
\begin{hol}
\index{define_type@\ml{define\_type}}
\begin{alltt}
   define\_type `\m{name}` `{\op} = \(C\sb{1}\;ty{}\sb{1}\sp{1}\;\ldots\;ty{}\sb{1}\sp{k\sb{1}} \) | \(\cdots\) | \(C\sb{m}\;ty{}\sb{m}\sp{1}\;\ldots\;ty{}\sb{m}\sp{k\sb{m}}\)`
\end{alltt}\end{hol}}

\noindent will make a formal definition for a  
type (or  type operator) \ty{op}
in the current theory segment, make appropriate definitions for
constants  \m{C_1, C_2,\dots, C_m},
and automatically prove a theorem which
provides an abstract
characterization\index{characterizing theorem!for defined types}\index{automated derivation!of characterizing theorems for recursive types} 
of the newly-defined type \ty{op}.  This theorem, which is
stored in the current theory segment under the name $name$ and also returned by
\ml{define\_type}, has the form shown below:

{\def\op{{\normalsize\sl op}}
\begin{hol}\begin{alltt}
   |- !f\(\sb{1}\:\cdots\:\)f\(\sb{m}\). ?!fn:{\op}->*.
         !x\(\sb{1}\sp{1}\;\,\cdots\,\;\)x\(\sb{1}\sp{k\sb{1}}\). fn(\m{C}\(\sb{1}\,\) x\(\sb{1}\sp{1}\;\,\ldots\,\;\)x\(\sb{1}\sp{k\sb{1}}\)) = f\(\sb{1}\) (fn x\(\sb{1}\sp{1}\))\(\;\ldots\;\)(fn x\(\sb{1}\sp{k\sb{1}}\)) x\(\sb{1}\sp{1}\;\ldots\;\)x\(\sb{1}\sp{k\sb{1}}\)
                                      \(\vdots\)
         !x\(\sb{m}\sp{1}\;\cdots\;\)x\(\sb{1}\sp{k\sb{m}}\!\). fn(\m{C}\(\sb{m}\) x\(\sb{m}\sp{1}\;\ldots\;\)x\(\sb{m}\sp{k\sb{m}}\)) = f\(\sb{m}\) (fn x\(\sb{m}\sp{1}\))\(\;\ldots\;\)(fn x\(\sb{m}\sp{k\sb{m}}\)) x\(\sb{m}\sp{1}\;\ldots\;\)x\(\sb{m}\sp{k\sb{m}}\)
\end{alltt}\end{hol}}     


\noindent where the right hand sides of the equations include recursive
applications `$\ml{fn}\;\ml{x}_i^j$' only for variables  $\ml{x}_i^j$ of type
\ty{op}.  (See the examples given below.) A theorem of this form asserts the
unique existence of primitive recursive functions defined by cases on the
constructors \m{C_1, C_2,\dots,C_m}.  This is a slight
extension of the {\it initiality\/}\index{initiality} property by which
structures of this kind are characterized in the `initial algebra' approach to
specifying abstract data types~\cite{goguen}.  This property provides an
abstract characterization of the type \ty{op} which is both succinct and
complete, in the sense that it completely determines the structure of the
values of \ty{op} up to isomorphism.

The call to \ml{define\_type} shown above fails if:

\begin{myenumerate}
 
\item not in draft mode\index{draft mode, in HOL system@draft mode, in \HOL\ system};

\item \ty{op} is already the name of a type constant or type operator in the
current theory;

\item any one of $C_1,\dots,C_{m}$ is already the name
of a constant in the current theory.

\item either \ty{op} or any one $C_{1},\dots,C_{m}$ is not a
legal identifier.  Identifiers must start with a letter (as defined by
\ml{is\_letter}) and contain only alphanumeric characters (as defined by
\ml{is\_alphanum})

\item $\ml{ABS\_}\ty{op}$\index{ABS_@\ml{ABS\_}$\ldots$}
 or $\ml{REP\_}\ty{op}$\index{REP_@\ml{REP\_}$\ldots$}
 are already constants in the
current theory;

\item there is already an axiom, definition, constant specification or type
definition stored under either the name
$\ty{op}\ml{\_TY\_DEF}$\index{TY_DEF@$\ldots$\ml{\_TY\_DEF}} or the name
$\ty{op}\ml{\_ISO\_DEF}$ in the current
theory segment.

\item there is already a theorem stored under the name \ml{`\m{name}`} in the
current theory segment.

\item the input type specification does not conform to the syntax described
above.

\end{myenumerate}
\index{recursive types, in HOL logic@recursive types, in \HOL\ logic!tools for construction of|)}\index{type definition extension, in HOL logic@type definition extension, in \HOL\ logic|)}
\index{type definition package, in HOL system@type definition package, in \HOL\ system!output of|)}
\index{type definition package, in HOL system@type definition package, in \HOL\ system!use of, in recursive type definition|)}

\subsubsection{Examples}\label{define-type-example}

\index{primitive recursion theorem!a degenerate case|(}
The session that follows illustrates the use of \ml{define\_type} in defining
a variety of simple concrete types.  It is assumed that the session begins
with the user in draft mode.

The first definition is simple, the definition of a type \ml{three} with
exactly three distinct values: \ml{ONE}, \ml{TWO}, and \ml{THREE}.

\setcounter{sessioncount}{1}\label{types-session}
\begin{session}\begin{verbatim}
#let three_Axiom = define_type `three_Axiom` `three = ONE | TWO | THREE`;;
three_Axiom = 
|- !e0 e1 e2. ?! fn. (fn ONE = e0) /\ (fn TWO = e1) /\ (fn THREE = e2)
\end{verbatim}\end{session}

\noindent The theorem returned by \ml{define\_type} provides a complete 
and abstract characterization of
a defined logical type \ml{three} which denotes a set of
exactly three elements.  This characterization takes the form of a
degenerate `primitive recursion'
 theorem for the concrete type \ml{three}. 
Since \ml{three} is an enumerated type with no recursive constructors, 
the theorem returned by \ml{define\_type} simply
states that any function defined by cases on the three constants  \ml{ONE},
\ml{TWO}, and \ml{THREE} exists and is uniquely defined. 

It follows immediately from this theorem that the type constant \ml{three}
denotes a set containing exactly three values: the fact that the function
\ml{fn} always exists implies that the constants \ml{ONE}, \ml{TWO}, and
\ml{THREE} denote distinct values of type \ml{three}, and the fact that \ml{fn}
is uniquely determined by its values for \ml{ONE}, \ml{TWO}, and \ml{THREE}
implies that these constants denote the only values of type \ml{three}.
\index{primitive recursion theorem!a degenerate case|)}

The next call to \ml{define\_type} defines a `record type' \ml{rec}, values of
which are records with three boolean fields (essentially 3-tuples):

\begin{session}\begin{verbatim}
#let rec_Axiom = define_type `rec_Axiom` `rec = REC bool bool bool`;;
rec_Axiom = |- !f. ?! fn. !b0 b1 b2. fn(REC b0 b1 b2) = f b0 b1 b2
\end{verbatim}\end{session}\label{rec-def}

\noindent Here, the resulting theorem states that a function \ml{fn} on
record values of type \ml{rec} can be
uniquely defined in terms of a function \ml{f} of the three components of
the record.

A more interesting {\it recursive\/}
  example is the type of natural 
numbers\index{number theory, in HOL logic@number theory, in \HOL\ logic!type definition package version of}
\index{type definition package, in HOL system@type definition package, in \HOL\ system!use of, to build number theory}, which can be defined using \ml{define\_type} as follows:

\begin{session}\begin{verbatim}
#let nat_Axiom = define_type `nat_Axiom` `nat = Z | Suc nat`;;
nat_Axiom = |- !e f. ?! fn. (fn Z = e) /\ (!n. fn(Suc n) = f(fn n)n)
\end{verbatim}\end{session}

\noindent Here, the input string describes a type \ml{nat} with two
constructors: \ml{Z}, which stands for zero; and \ml{Suc}, which is the
successor function on natural numbers.  (The names 
\ml{Z}, and \ml{Suc} are used here because \ml{0} and \ml{SUC} are already
constants in the built-in \HOL\ theory \ml{num}.)
The output theorem is just the primitive recursion 
theorem\footnote{See Section~\ref{prim_rec} for a discussion of
the primitive recursion theorem.}
for the natural numbers; it states that any primitive recursive definition on
the natural numbers (\ie\ on values of type \ml{nat})
uniquely defines a total function.

A recursive type of labelled binary trees\index{binary tree theory, in HOL logic@binary tree theory, in \HOL\ logic|(}, where labels of type \ml{*}
appear only on leaf nodes, can likewise be defined using \ml{define\_type}.
The input states that a binary tree is either a leaf node (\ml{LEAF})
labelled by a value of type \ml{*} or an internal node \ml{NODE} with
two binary trees as subtrees:

\begin{session}\begin{verbatim}
#let btree_Axiom = 
#    define_type `btree_Axiom` `btree = LEAF * | NODE btree btree`;;
btree_Axiom = 
|- !f0 f1.
    ?! fn.
     (!x. fn(LEAF x) = f0 x) /\
     (!b1 b2. fn(NODE b1 b2) = f1(fn b1)(fn b2)b1 b2)
\end{verbatim}\end{session}\label{btree-def}

\noindent The result returned by the call to {\small \verb!define_type!} is,
in this case, an abstract
characterization for a defined type {\small\verb!(*)btree!},
in the form of a `primitive recursion theorem' for the required
type of labelled binary trees.\index{binary tree theory, in HOL logic@binary tree theory, in \HOL\ logic|)}

Any simple  concrete  recursive  type  can  be  defined  automatically  from  a
user-supplied equation using \ml{define\_type} in exactly the same way.

\subsection{Defining recursive functions}\label{prim-rec-defs}

\index{type definition package, in HOL system@type definition package, in \HOL\ system!use of, in recursive function definition|(}
\index{recursive definitions, in HOL logic@recursive definitions, in \HOL\ logic!automated, for recursive types|(}
\index{primitive recursion theorem!for binary trees|(}
An important property of the characterizing theorems for concrete types
shown in the examples given above is that they
provide a formal
means for defining recursive functions on those types.
When a concrete 
recursive type  \ty{op} is
 characterized by a theorem of the kind returned by
\ml{define\_type}\index{types, in HOL logic@types, in \HOL\ logic!tools for construction of} (see Section~\ref{define-type-output}) this theorem
can be used to 
prove the existence of any 
{\it primitive recursive\/} function on \ty{op} and to
define constants which denote such functions.  

This is illustrated
for a particular
example by the method of defining primitive recursive functions on
the natural numbers  discussed in Section~\ref{num-prim-rec}.  In that section,
an \ML\ function \ml{new\_prim\_rec\_definition}\index{new_prim_rec_definition@\ml{new\_prim\_rec\_definition}}
 was described which automates
the logical inferences necessary to derive particular primitive recursive
definitions on the built-in defined type \ml{num} of natural numbers.  The
basis of this function is the primitive recursion theorem

\begin{hol}
\index{num_Axiom@\ml{num\_Axiom}}
\begin{verbatim}
   num_Axiom   |- !x f. ?!fn. (fn 0 = x) /\ (!n. fn(SUC n) = f (fn n) n)
\end{verbatim}\end{hol}

\noindent which is pre-proved and stored in the built-in theory \ml{prim\_rec}\index{prim_rec@\ml{prim\_rec}}
(see Section~\ref{prim_rec}).  The \ML\ function
\ml{new\_prim\_rec\_definition} uses \ml{num\_Axiom} to automate the
justification of any user-supplied primitive recursive definition on the
natural numbers.

The type definition package\index{primitive recursion theorem!automated use of, in HOL system@automated use of, in \HOL\ system|(} provides a similar function for defining 
primitive recursive functions on
arbitrary concrete recursive
types.\footnote{In fact, {\tt new\_prim\_rec\_definition} is defined in ML
using the more general tools provided by the type definition package.}
The \ML\ function

\begin{boxed}\index{new_recursive_definition@\ml{new\_recursive\_definition}|pin}
\begin{verbatim}
   new_recursive_definition : bool -> thm -> string -> term -> thm
\end{verbatim}\end{boxed}

\noindent automates the
inferences necessary to justify any given primitive recursive definition on a
concrete recursive type of the kind definable by \ml{define\_type}.
 It takes four arguments.  The first is a boolean
flag which indicates if the function to be defined will be an infix\index{infixes, in HOL logic@infixes, in \HOL\ logic!in recursive type definitions} or not.  
The second is the primitive recursion theorem for the concrete type in question
(\ie\ a theorem obtained from {\small\verb!define_type!}).\index{define_type@\ml{define\_type}}  The third
argument is a name under which the resulting definition will be saved in the
current theory segment.
The fourth argument is a term giving the desired primitive recursive
definition.  The value returned
by  {\small\verb!new_recursive_definition!} is a theorem
which states the primitive recursive definition requested by the
user.  This theorem is derived by formal proof from an instance of the general
primitive recursion theorem\index{automated derivation!of recursive definitions}
 given as the second argument.

If the \ML\ variable \ty{op}\ml{\_Axiom} is bound to a theorem of the form
returned by \ml{define\_type},
then evaluating:

{\def\op{{\normalsize\sl op}}
\begin{hol}\begin{alltt}
   new_recursive_definition 
     `\m{flag}` \op\_Axiom `\m{name}` "{\normalsize\it primitive recursive definition on \op}"
\end{alltt}\end{hol}}

\noindent  automatically proves the existence of the primitive recursive
function supplied as the fourth argument, 
and then declares a new constant in the current theory
with this definition as its
specification. This constant specification is returned as a theorem
and is saved in the current theory segment under the name
$name$.
If $flag$ is \ml{true}, the constant is given infix status.
Failure occurs if:

\begin{myenumerate}
\item \HOL\ cannot prove there is a function 
satisfying the defining equations supplied by the user 
(\ie\ the term supplied to \ml{new\_recursive\_definition} 
 is not a well-formed primitive recursive definition on values 
of type \ty{op});
\item any other condition for making a constant specification is violated
(see the failure conditions for \ml{new\_specification} in 
Section~\ref{conspec}).
\end{myenumerate}

Curried\index{currying, in ML@currying, in \ML!in recursive definitions}
functions defined using \ml{new\_recursive\_definition} can be
recursive on any one of their arguments.  Furthermore, defining equations need
not be given for all the constructors of the concrete type in question.  See
the examples given in the next section, or the examples of functions 
defined on \ml{num} given in Section~\ref{num-prim-rec} for more details.

The \ML\ function 

\begin{boxed}
\index{prove_rec_fn_exists@\ml{prove\_rec\_fn\_exists}|pin}
\begin{verbatim}
   prove_rec_fn_exists : thm -> term -> thm
\end{verbatim}\end{boxed}

\noindent is a version of \ml{new\_recursive\_definition} which proves only
that the required function exists; it does not make a constant specification.
The first argument is a theorem of the form returned by \ml{define\_type},
and the second is a user-supplied primitive recursive function definition.
The theorem which is returned asserts the existence of the recursively-defined
function in question (if it is primitive recursive over the type characterized
by the theorem given as the first argument). 
\index{recursive definitions, in HOL logic@recursive definitions, in \HOL\ logic!automated, for recursive types|)}
\index{type definition package, in HOL system@type definition package, in \HOL\ system!use of, in recursive function definition|)}
\index{primitive recursion theorem!automated use of, in HOL system@automated use of, in \HOL\ system|)}

\subsubsection{More examples}

Continuing the example session started above in Section~\ref{define-type-example},
the following interactions with the system show how the \ML\ function
\ml{new\_recursive\_definition} can be used to define functions on concrete types,
which have themselves been defined using \ml{define\_type}.

Given the characterizing theorem
\ml{btree\_Axiom} for the type of labelled binary trees
defined in Section~\ref{types-session}, a recursive function \ml{Leaves}, which
computes the number of leaf nodes in a binary tree, 
can be defined recursively in \HOL\ as shown below:

\begin{session}\begin{verbatim}
#let Leaves = 
#    new_recursive_definition false btree_Axiom `Leaves` 
#      "(Leaves (LEAF (x:*)) = 1) /\
#       (Leaves (NODE t1 t2) = (Leaves t1) + (Leaves t2))";;
Leaves = 
|- (!x. Leaves(LEAF x) = 1) /\
   (!t1 t2. Leaves(NODE t1 t2) = (Leaves t1) + (Leaves t2))
\end{verbatim}\end{session}

\noindent The result of the call to {\small\verb!new_recursive_definition!} is
a theorem which states that the constant {\small\verb!Leaves!} satisfies the
primitive-recursive defining equations supplied by the user.  This theorem is
derived automatically from an instance of the general primitive recursion
theorem for binary trees ({\small\verb!btree_Axiom!}) and an appropriate
constant specification for the constant {\small\verb!Leaves!}. 

The function defined using \ml{new\_recursive\_definition}\index{type
definition package, in HOL system@type definition package, in \HOL\
system!use of, in case definition} need not, in fact, be recursive.
Here is the definition of a predicate \ml{IsLeaf}, which is true of
binary trees which are leaves, but is false of the internal nodes in a
binary tree:

\begin{session}\begin{verbatim}
#let IsLeaf = 
#    new_recursive_definition false btree_Axiom `IsLeaf` 
#      "(IsLeaf (NODE t1 t2) = F) /\ (IsLeaf (LEAF (x:*)) = T)";;
IsLeaf = |- (!t1 t2. IsLeaf(NODE t1 t2) = F) /\ (!x. IsLeaf(LEAF x) = T)
\end{verbatim}\end{session}

\noindent Note that two equations defining a (recursive or non-recursive)
function on binary trees by cases can be given in either order.  Here, the
\ml{NODE} case is given first, and the \ml{LEAF} case second.  The reverse
order was used in the above definition of \ml{Leaves}.

The \ML\ function  {\small\verb!new_recursive_definition!} also allows the user
to partially specify\index{type definition package, in HOL system@type definition package, in \HOL\ system!use of, in partial definition} the value of a function defined on a concrete type, by
allowing defining equations for some of the constructors to be omitted.  Here,
for example, is the definition of a function \ml{Label} which extracts the
label from a leaf node.  The value of \ml{Label} applied to an internal node
is left unspecified:

\begin{session}\begin{verbatim}
#let Label = 
#    new_recursive_definition false btree_Axiom `Label` 
#      "Label (LEAF (x:*)) = x";;
Label = |- !x. Label(LEAF x) = x
\end{verbatim}\end{session}

\index{type definition package, in HOL system@type definition package, in \HOL\ system!use of, in curried infix definition|(} 
Curried functions can also be defined, and the recursion can be on any
argument.  The next definition defines an infix (curried)
function \ml{<<} which expresses the idea that one tree is a proper
subtree of another.

\begin{session}\begin{verbatim}
#let Subtree = 
#    new_recursive_definition true btree_Axiom `Subtree` 
#      "(<< (t:(*)btree) (LEAF (x:*)) = F) /\
#       (<< t (NODE t1 t2) = ((t=t1) \/ (t=t2) \/ (<< t t1) \/ (<< t t2)))";;
Subtree = 
|- (!t x. t << (LEAF x) = F) /\
   (!t t1 t2.
     t << (NODE t1 t2) = (t = t1) \/ (t = t2) \/ t << t1 \/ t << t2)
\end{verbatim}\end{session}

\noindent Note that the first argument to the \ML\ function is \ml{true}
(to indicate that the function being defined is to have infix status) and that
the constant \ml{<<} is an infix after the definition has been made.
Furthermore, the function \ml{<<} is recursive on its second argument\index{type definition package, in HOL system@type definition package, in \HOL\ system!use of, in curried infix definition|)}.

Finally, the function {\small\verb!new_recursive_definition!} can also be used
to define functions by cases on enumerated types.  For example, a predicate
\ml{One}, which is true of only the value \ml{ONE} of the three-valued type
\ml{three} defined above in Section~\ref{types-session}, can be defined as
follows:

\begin{session}\begin{verbatim}
#let One = new_recursive_definition false three_Axiom `One` 
#            "(One ONE = T) /\ (One TWO = F) /\ (One THREE = F)";;
One = |- (One ONE = T) /\ (One TWO = F) /\ (One THREE = F)
\end{verbatim}\end{session}

The existence only of
any function definable using \ml{new\_recursive\_definition} can be proved
using \ml{prove\_rec\_fn\_exists}.  For example:

\begin{session}\begin{verbatim}
#close_theory();;
() : void

#let exists = prove_rec_fn_exists three_Axiom 
#             "(f ONE = T) /\ (f TWO = F) /\ (f THREE = F)";;
exists = |- ?f. (f ONE = T) /\ (f TWO = F) /\ (f THREE = F)
\end{verbatim}\end{session}

\noindent The resulting theorem simply states the existence of the 
required function.  Here, a constant is not defined, and the user need
not be in draft mode.
\index{primitive recursion theorem!for binary trees|)}

\subsection{Structural induction}

\index{type definition package, in HOL system@type definition package, in \HOL\ system!use of, to build induction tools|(} 
\index{induction rule!structural, derivation of|(}
For any concrete recursive type definable 
 using the \HOL\ type definition package there is a structural induction
theorem which states the validity of proof by induction  
on the structure of the type's values.  The \ML\ function

\begin{boxed}
\index{prove_induction_thm@\ml{prove\_induction\_thm}|pin}
\begin{verbatim}
   prove_induction_thm : thm -> thm 
\end{verbatim}\end{boxed}


\noindent can be used to derive a structural induction\index{automated derivation!of structural induction theorems}\index{induction rule!for concrete recursive types}
theorem for any concrete
recursive type defined using \ml{define\_type}.  If the \ML\ 
variable \ty{op}\ml{\_Axiom} is bound to a theorem of the form
returned by \ml{define\_type}, 
then executing 

\[ \ml{prove\_induction\_thm}\;\ty{op}\ml{\_Axiom} \]

\noindent will prove  and  return a  structural induction  theorem for  
the concrete type
\ty{op}.  The `induction' theorem is degenerate in the case of non-recursive
types (see the examples given below). Failure occurs, or an unpredictable 
output theorem is returned, if the input theorem does not have the form
of a theorem  returned by \ml{define\_type}.

\subsubsection{Examples}

A structural induction theorem on the type of binary trees defined in the session
beginning on Section~\ref{types-session} can be proved by:

\begin{session}\begin{verbatim}
#let btree_Induct = prove_induction_thm btree_Axiom;;
btree_Induct =
|- !P.
    (!x. P(LEAF x)) /\ (!b1 b2. P b1 /\ P b2 ==> P(NODE b1 b2)) ==>
    (!b. P b)
\end{verbatim}\end{session}


\noindent The output theorem states that a predicate \ml{P} is true of all
binary trees if it is true of all labelled leaf nodes, and whenever it is true
of two binary trees \ml{b1} and \ml{b2} it is also true of the binary tree
\ml{NODE b1 b2}, in which \ml{b1} and \ml{b2} occur as immediate left and right
subtrees.

For non-recursive types, the induction theorem returned by
\ml{prove\_induction\_thm} is degenerate: there are no `step' cases in the
induction.  For the two types \ml{three} and \ml{rec} defined in the preceding
interactions of this session, the induction theorems are:

\begin{session}\begin{verbatim}
#let three_Induct = prove_induction_thm three_Axiom;;
three_Induct = |- !P. P ONE /\ P TWO /\ P THREE ==> (!t. P t)

#let rec_Induct = prove_induction_thm rec_Axiom;;
rec_Induct = |- !P. (!b0 b1 b2. P(REC b0 b1 b2)) ==> (!r. P r)
\end{verbatim}\end{session}

\noindent Here, induction simply reduces to the consideration of
cases,\index{case analysis, in HOL logic@case analysis, in \HOL\ logic!as instance  of induction} one for each of the constructors for the concrete type involved.
\index{induction rule!structural, derivation of|)}

\subsection{Structural induction tactics}
\label{avrasi}
%I added one paragraph because this section is so out of sequence. Avra 9/11/89.

\index{theorem continuations!use of, in derivation of induction tactics|(}
This section has been included here for reference
because it relates chiefly to the
type definition package, but it involves concepts not defined until
later, in Chapter~\ref{tactics-and-tacticals}. Tactics, goals
and subgoals are
defined in Section~\ref{tactics}; and theorem continuations, in
Section~\ref{asm-manip}. \ml{MAP\_EVERY} is defined in Section~\ref{avra_manip1}.
\ml{ASSUME\_TAC} is defined in Section~\ref{avra_builtin}.
\ml{MP\_TAC}  and \ml{INDUCT\_TAC} can be found in \REFERENCE.

The \ML\ function

\begin{boxed}
\index{INDUCT_THEN@\ml{INDUCT\_THEN}|pin}
\index{induction tactics!derivation of|(}
\begin{verbatim}
   INDUCT_THEN : thm -> (thm -> tactic) -> tactic
\end{verbatim}\end{boxed}

\noindent can be used to generate
a structural induction tactic\index{automated derivation!of structural induction tactics}
for any concrete types definable using \ml{define\_type}.\index{define_type@\ml{define\_type}}  The first argument
is an induction theorem of the form returned by the function
\ml{prove\_induction\_thm}\index{prove_induction_thm@\ml{prove\_induction\_thm}} discussed in the previous section.  The second
argument is a theorem continuation\index{characterizing theorem!use of, in deriving induction} (see
Chapter~\ref{tactics-and-tacticals}) that determines what is to be done with
the induction hypotheses when the resulting tactic is applied to a goal.

If $th$ is an induction theorem for a concrete type \ty{op} with
$m$ constructors  \mbox{$C_1$, \dots, $C_m$}
(\ie\ a theorem of the kind returned by
\ml{prove\_induction\_thm}) and $F$ is a theorem continuation, then the
tactic $\ml{INDUCT\_THEN}\;th\;F$ will reduce a goal 
{\small\verb%(%}$\Gamma${\small\verb%,"!%}$x{:}
\ty{op}${\small\verb%.%}$t[x]${\small\verb%")%} to the collection of $m$
induction subgoals generated by:

\[ \begin{array}[t]{@{}l@{}l}
 \ml{MAP\_EVERY } F \ml{ [}th_1^1\ml{;}\;\ldots\ml{;}\;th_1^{k_1}\ml{]} & 
\ml{ (}\Gamma\ml{, "}t[ C_1\;x_1^1\;\ldots\;x_1^{k_1}]\ml{")},\\
\qquad\qquad \vdots & \mbox{} \\
 \ml{MAP\_EVERY } F \ml{ [}th_m^1\ml{;}\;\ldots\ml{;}\;th_m^{k_m}\ml{]} & 
\ml{ (}\Gamma\ml{, "}t[ C_m\;x_m^1\;\ldots\;x_m^{k_m}]\ml{")}
 \end{array}
\]\index{MAP_EVERY@\ml{MAP\_EVERY}}

\noindent where $th_i^j$ is a theorem of the form $\ml{|- }t[x_i^j]$ asserting
the truth of $t[x_i^j]$ for the $j$th recursive argument (for non-recursive 
arguments, there will be no $th_i^j$ in the list) of the $i$th constructor
$C_i$ (for $1 \leq i \leq m$).

The most common use of \ml{INDUCT\_THEN} is in conjunction with the theorem
continuation \ml{ASSUME\_TAC}.  For example, the built-in
induction tactic \ml{INDUCT\_TAC} for mathematical induction on 
the natural numbers is defined in \ML\ by:

\begin{hol}
\index{INDUCT_TAC@\ml{INDUCT\_TAC}}
\begin{verbatim}
   let INDUCT_TAC = INDUCT_THEN INDUCTION ASSUME_TAC 
\end{verbatim}\end{hol}

\noindent This built-in tactic reduces a goal
 {\small\verb%(%}$\Gamma${\small\verb%,"!%}$n${\small\verb%.%}$t[n]${\small\verb%")%} to a basis subgoal
{\small\verb%(%}$\Gamma${\small\verb%,"%}$t[${\small\verb%0%}$]${\small\verb%")%} 
and a step subgoal 
{\small\verb%(%}$\Gamma\cup\{${\small\verb%"%}$t[n]${\small\verb%"%}$\}${\small\verb%,"%}$t[${\small\verb%SUC %}$n]${\small\verb%")%}.
The extra assumption  {\small\verb%"%}$t[n]${\small\verb%"%} (\ie\ the
induction hypothesis)
is added to the assumptions $\Gamma$ by \ml{ASSUME\_TAC}.  

By contrast, the
induction tactic \ml{INDUCT\_MP\_TAC} (which is not built-in) defined
by:

\begin{hol}\begin{verbatim}
   let INDUCT_MP_TAC = INDUCT_THEN INDUCTION MP_TAC
\end{verbatim}\end{hol}

\noindent reduces a goal
 {\small\verb%(%}$\Gamma${\small\verb%,"!%}$n${\small\verb%.%}$t[n]${\small\verb%")%} to a basis subgoal
{\small\verb%(%}$\Gamma${\small\verb%,"%}$t[${\small\verb%0%}$]${\small\verb%")%} 
and an induction step subgoal 
{\small\verb%(%}$\Gamma${\small\verb%, "%}$t[n]${\small\verb% ==> %}$t[${\small\verb%SUC %}$n]${\small\verb%")%}.
Here, the theorem continuation \ml{MP\_TAC} makes the induction hypothesis
an antecedent of the step subgoal, rather than an assumption.

As this example illustrates, the theorem continuation $F$ in 
an induction tactic

\[ \ml{INDUCT\_THEN }th\;\;F \]

\noindent generated using an induction theorem $th$ can be thought of as a
function which determines what is to be done with the induction hypotheses
corresponding to the recursive arguments of constructors in the step
cases of a proof by structural induction.  When $F$ is \ml{ASSUME\_TAC}, 
the induction hypotheses become assumptions in the subgoals generated; and when
$F$ is \ml{MP\_TAC}, the induction hypotheses become the antecedents of
implicative subgoals.  Other theorem continuations (for which, see
Chapter~\ref{tactics-and-tacticals} and \REFERENCE) can also be used\index{theorem continuations!use of, in derivation of induction tactics|)}.
\index{type definition package, in HOL system@type definition package, in \HOL\ system!use of, to build induction tools|)}\index{induction tactics!derivation of|)} 

\subsection{Other tools}


The function

\begin{boxed}
\index{prove_constructors_one_one@\ml{prove\_constructors\_one\_one}|pin}
\begin{verbatim}
   prove_constructors_one_one : thm -> thm
\end{verbatim}\end{boxed}

\noindent proves that the constructors\index{constructors, of concrete types in HOL logic@constructors, of concrete types in \HOL\ logic!proving one-to-one}
 of a concrete type which take arguments
are one-to-one\index{automated derivation!of one-to-one theorems}.  The argument to \ml{prove\_constructors\_one\_one} is a
theorem of the form returned by \ml{define\_type}.

The function 

\begin{boxed}
\index{prove_constructors_distinct@\ml{prove\_constructors\_distinct}|pin}
\begin{verbatim}
   prove_constructors_distinct : thm -> thm
\end{verbatim}\end{boxed}

\noindent proves that the constructors\index{constructors, of concrete types in HOL logic@constructors, of concrete types in \HOL\ logic!proving distinct}
 of a concrete type yield distinct\index{automated derivation!of
distinctness theorems} values.  The argument to
\ml{prove\_constructors\_distinct} is again 
a theorem of the form returned by \ml{define\_type}.

The function

\begin{boxed}
\index{prove_cases_thm@\ml{prove\_cases\_thm}|pin}
\begin{verbatim}
   prove_cases_thm : thm -> thm
\end{verbatim}\end{boxed}

\noindent proves a cases\index{automated derivation!of case analysis theorems}
 theorem\index{case analysis, in HOL logic@case analysis, in \HOL\ logic!theorems for} for any concrete type.  Such a theorem states that every
value can be constructed using one of the type's constructors.  This property
follows more easily (and therefore is faster to prove) from induction than from
primitive recursion, so the function \ml{prove\_cases\_thm} takes as an
argument an induction theorem of the kind returned by
\ml{prove\_induction\_thm}.\index{prove_induction_thm@\ml{prove\_induction\_thm}}

These auxiliary tools work for any concrete type definable using
\ml{define\_type}.

\subsubsection{Examples}

The following interactions with the system show the proof that the constructor
\ml{LEAF} for the type \ml{(*)btree}  is one-one,
and also that the constructor \ml{REC} for the type \ml{rec} is one-to-one.

\begin{session}\begin{verbatim}
#let LEAF_one_one = prove_constructors_one_one btree_Axiom;;
LEAF_one_one = 
|- (!x x'. (LEAF x = LEAF x') = (x = x')) /\
   (!b1 b2 b1' b2'.
     (NODE b1 b2 = NODE b1' b2') = (b1 = b1') /\ (b2 = b2'))

#let REC_one_one = prove_constructors_one_one rec_Axiom;;
REC_one_one = 
|- !b0 b1 b2 b0' b1' b2'.
    (REC b0 b1 b2 = REC b0' b1' b2') =
    (b0 = b0') /\ (b1 = b1') /\ (b2 = b2')
\end{verbatim}\end{session}

The function \ml{prove\_constructors\_one\_one} fails when the concrete 
type involved has no constructors that take arguments.  For example:

\begin{session}\begin{verbatim}
#let th = prove_constructors_one_one three_Axiom;;
evaluation failed     prove_constructors_one_one: invalid input theorem
\end{verbatim}\end{session}

The function \ml{prove\_constructors\_distinct} returns the theorem stating
that the constructors of a concrete type yield pair-wise distinct values.  For
example:

\begin{session}\begin{verbatim}
#let NOT_LEAF_NODE = prove_constructors_distinct btree_Axiom;;
NOT_LEAF_NODE = |- !x b1 b2. ~(LEAF x = NODE b1 b2)

#let three_distinct = prove_constructors_distinct three_Axiom;;
three_distinct = |- ~(ONE = TWO) /\ ~(ONE = THREE) /\ ~(TWO = THREE)
\end{verbatim}\end{session}

Cases theorems\index{case analysis, in HOL logic@case analysis, in \HOL\ logic!as instance  of induction} are proved from structural induction theorems. For the binary
tree example considered in the present session, here is the cases theorem:

\begin{session}\begin{verbatim}
#let btree_cases = prove_cases_thm btree_Induct;;
btree_cases = |- !b. (?x. b = LEAF x) \/ (?b1 b2. b = NODE b1 b2)
\end{verbatim}\end{session}

\noindent Note that the structural induction theorem for binary trees, 
\ml{btree\_Induct}, is used.
\index{extension, of HOL logic@extension, of \HOL\ logic!by type definition|)}
\index{type definition package, in HOL system@type definition package, in \HOL\ system|)}

\section{Miscellaneous system features}
\label{avramisc}

This section describes some of the features
that exist for managing the interface\index{HOL system@\HOL\ system!adjustment of user interface of} to the
\HOL\ system.  (Other system functions can be found in
Chapter~\ref{sysfuns}.)

\begin{itemize}
\item Flags for controlling the parsing and printing of terms.
\item A mechanism called {\it sticky types\/} allows default types of
variables to be established.
\item A facility called {\it interface maps\/} permits
a limited amount of overloading to be achieved. Interface maps also support
 the use of conventional logical characters on bit-mapped screens.
\item Functions for changing the {\small ASCII}
versions to $\lambda$ and $\vdash$ inside quotations
(the defaults are {\small\verb%\%} and
{\small\verb%|-%} respectively).
\item A method for hiding constants from the quotation parser, so that 
variables
with the same names as constants can be input by quotation.
\item A method for arranging for definitions and theorems (and so on)
to be loaded automatically when their names are encountered by the \ML\ parser.
\item Functions to control whether entire theories are kept in memory
(`cached') or not.
\item A function for adjusting the maximum depth to which terms and 
theorems are printed by the pretty printer (the default is 500).
\item Functions for counting the number of primitive inferences done in
an evaluation, and timing it.
\end{itemize}

\subsection{Flags for the HOL logic}
\label{HOLflags}

\index{HOL system@\HOL\ system!adjustment of user interface of|(}
See also Chapter~\ref{sysfuns} for a description  of the  flag mechanism which
is used  to  control  the  state  of the  \HOL\ system.   The  subset of flags\index{HOL system@\HOL\ system!flags in}\index{flags, in ML@flags, in \ML}
that control aspects of \HOL\ relating to the logic is summarized in the table
below.


\begin{center}
\index{counting inferences, in HOL proofs@counting inferences, in \HOL\ proofs}
\index{inferences, in HOL logic@inferences, in \HOL\ logic!counting of}
\index{printing, in HOL logic@printing, in \HOL\ logic!of types}
\index{types, in HOL logic@types, in \HOL\ logic!printing of}
\index{types, in HOL logic@types, in \HOL\ logic!default}
\index{theory files!pretty printing of}
\index{pretty printing!flags for, in HOL system@flags for, in \HOL\ system}
\index{type checking, in HOL logic@type checking, in \HOL\ logic!verbose errors in}
\index{interface maps}
\index{printing, in HOL logic@printing, in \HOL\ logic!of quantification}
\index{let-terms, in HOL logic@\ml{let}-terms, in \HOL\ logic!pretty printing of}
\index{infixes, in HOL logic@infixes, in \HOL\ logic!pretty printing of}
\index{printing, in HOL logic@printing, in \HOL\ logic!pretty printing}
%\index{printing, in HOL logic@printing, in \HOL\ logic!undischarging 
%IS_ASSUMPTION_OFs in@undischarging \ml{IS\_ASSUMPTION\_OF}s in}
%\index{IS_ASSUMPTION_OF@\ml{IS\_ASSUMPTION\_OF}}
\index{timing@\ml{timing}}
\index{show_types@\ml{show\_types}}
\index{sticky@\ml{sticky}}
\index{theory_pp@\ml{theory\_pp}}
\index{type_error@\ml{type\_error}}
\index{interface_print@\ml{interface\_print}}
\index{print_cond@\ml{print\_cond}}
\index{print_quant@\ml{print\_quant}}
\index{print_let@\ml{print\_let}}
\index{print_list@\ml{print\_list}}
\index{print_uncurry@\ml{print\_uncurry}}
\index{print_infix@\ml{print\_infix}}
\index{function abstraction, in HOL logic@function abstraction, in \HOL\ logic!paired}
\index{function abstraction, in HOL logic@function abstraction, in \HOL\ logic!pretty printing, of paired}
\begin{tabular}{|l|l|l|} \hline
\multicolumn{3}{|c|}{ } \\
\multicolumn{3}{|c|}{\bf Settable system flags} \\
\multicolumn{3}{|c|}{ } \\
{\it Flag} & {\it Function} & 
{\it Default value} \\ \hline
 & &  \\
\ml{timing} &    Print number of theorems proved& \ml{false}\\ \hline

\ml{show\_types} &    Prints types in quotations   &    \ml{false}\\ \hline

\ml{sticky}     &         Activates sticky types   &        \ml{false}\\ \hline


\ml{theory\_pp} & Pretty printing of theory files &   \ml{false} \\ \hline

\ml{type\_error} & Verbose type checking errors in quotations& \ml{true} \\ \hline

\ml{interface\_print}  &   Causes inverse of interface map &     \ml{true}\\[-1mm]
 &                       to be used when printing & \\ \hline

\ml{print\_cond} & Pretty print \HOL\ conditionals  &      \ml{true}\\ \hline

\ml{print\_quant} &   Pretty print \HOL\ quantifications &     \ml{true}\\ \hline

\ml{print\_let} &   Pretty print \HOL\ \ml{let}-expressions   &\ml{true}\\ \hline

\ml{print\_list}   &       Pretty print \HOL\ lists &           \ml{true}\\ \hline

\ml{print\_uncurry} & Pretty print \HOL\ paired abstractions & \ml{true}\\ \hline

\ml{print\_infix} &    Pretty print \HOL\ infixes & \ml{true}\\ \hline
\end{tabular}
\end{center}


\subsection{Sticky types and default types}
\label{stickytypes}

\index{sticky types, in HOL logic@sticky types, in \HOL\ logic|(}
\index{types, in HOL logic@types, in \HOL\ logic!default|(}
\index{types, in HOL logic@types, in \HOL\ logic!sticky|(}
\index{type checking, in HOL logic@type checking, in \HOL\ logic!sticky types in|(}
\index{HOL system@\HOL\ system!sticky types in|(}
\index{default types, in HOL logic@default types, in \HOL\ logic|(}
The quotation type checker  normally requires that there be enough information
in a quotation to fully determine the types of all subterms of the term being 
type checked. This information comes from two sources:

\begin{myenumerate}
\item the generic\index{types, in HOL logic@types, in \HOL\ logic!generic}
 types of constants, and 
\item explicit type\index{types, in HOL logic@types, in \HOL\ logic!explicit printing of}
 information given after a colon.
\end{myenumerate}

\noindent If sticky types are  activated (by  setting the  flag \ml{sticky} to
\ml{true}) then the last type a variable had in  a session  is remembered, and
this is used as a default type during type checking.   The sticky  type is only
used, however, if the variable in question is completely  unconstrained by its
context in the quotation.  If the context partially  determines the  type of a
variable, then sticky types are not used.  For example, in the term \ml{"f 1"}
the variable \ml{f} is constrained to have a function type, but the range type
is not  determined.    In  the quotation  \ml{"(f,1)"} the  variable \ml{f} is
completely unconstrained.   In  the latter  case an  (appropriate) sticky type
would resolve the typing, but in the former  it would  not.   Sticky types can
never cause  the type checking  of a  quotation to  fail when  it would succeed
without them.

\setcounter{sessioncount}{1}
\begin{session}\begin{verbatim}
#set_flag(`sticky`,true);;
false : bool

#"f:num->num";;
"f" : term

#"f 1";;
Indeterminate types:  "f:num -> ?"

evaluation failed     types indeterminate in quotation

#"f";;
"f" : term
\end{verbatim}\end{session}

A name can be given a sticky type explicity with the function:

\begin{boxed}
\index{set_sticky_type@\ml{set\_sticky\_type}|pin}
\begin{verbatim}
   set_sticky_type : string # type -> void
\end{verbatim}\end{boxed}

\noindent  This sets sticky types whether or not sticky
types are active.

Note that the flag \ml{sticky} controls whether types  are automatically stuck
onto names; it does not control  whether such  sticky types  are actually used
during type checking:  sticky types are always used if they are defined.

Sticky types can be removed with the function:

\begin{boxed}
\index{remove_sticky_type@\ml{remove\_sticky\_type}|pin}
\begin{verbatim}
   remove_sticky_type : string -> type
\end{verbatim}\end{boxed}

\noindent The sticky type is returned (failure if the variable does not 
have a sticky type). A function to return the sticky type of a variable
can be defined by:

\begin{hol}\begin{verbatim}
   let sticky_type v =
    let ty = remove_sticky_type v
    in
    (set_sticky_type(v,ty);ty)
\end{verbatim}\end{hol}

\noindent The list of sticky types active in a session is computed by the 
function:

\begin{boxed}
\index{sticky_list@\ml{sticky\_list}|pin}
\begin{verbatim}
   sticky_list : void -> (string # type) list
\end{verbatim}\end{boxed}\index{default types, in HOL logic@default types, in \HOL\ logic|)}\index{HOL system@\HOL\ system!sticky types in|)}\index{type checking, in HOL logic@type checking, in \HOL\ logic!sticky types in|)}
\index{types, in HOL logic@types, in \HOL\ logic!sticky|)}
\index{types, in HOL logic@types, in \HOL\ logic!default|)}
\index{sticky types, in HOL logic@sticky types, in \HOL\ logic|)}

\subsection{Interface maps}

An interface map\index{interface maps|(}\index{HOL system@\HOL\ system!interface maps in|(} is a list of pairs of strings:

\begin{hol}\begin{alltt}
   [`\m{a\sb{1}}`,`\m{c\sb{1}}`; \m{\ldots} ;`\m{a\sb{n}}`,`\m{c\sb{n}}`]
\end{alltt}\end{hol}


If such a map is active, then whenever $a_i$ (for $1\leq i \leq n$) occurs as a
constant or  variable  in  a quoted  term, it  is translated  (by the quotation
parser) to  $c_i$,  which  must  be  an  existing constant  (`$a$' is for
`abbreviation' and `$c$' for `constant').

The function:

\begin{boxed}
\index{set_interface_map@\ml{set\_interface\_map}|pin}
\begin{verbatim}
   set_interface_map : string list -> string list
\end{verbatim}\end{boxed}

\noindent installs an interface map (the previous map is undone and returned). 
The effect of executing:

\begin{hol}\begin{alltt}
   set_interface_map [`\m{a\sb{1}}`,`\m{c\sb{1}}`; \m{\ldots} ;`\m{a\sb{n}}`,`\m{c\sb{n}}`];;
\end{alltt}\end{hol}

\noindent is to install 
\ml{[`\m{a_1}`,`\m{c_1}`;\m{\ \ldots\ } ;`\m{a_n}`,`\m{c_n}`]}
as the current interface map.  Any
binder\index{binders, in HOL logic@binders, in \HOL\ logic!under interface maps} or infix\index{infixes, in HOL logic@infixes, in \HOL\ logic!under interface maps} status (including precedences)\index{precedence, in HOL logic@precedence, in \HOL\ logic!under interface maps} of $c_i$ is propagated 
to $a_i$ in the order:


\[ {\it status\/}(c_1)\;\;\leadsto\;\; a_1\;\; {\rm then}\;\;
   {\it status\/}(c_2)\;\;\leadsto\;\; a_2\;\; {\rm then}\;\;\;
    \ldots\;\;\;{\rm then}\;\; {\it status\/}(c_n)\;\;\leadsto\;\;a_n \]



\noindent This order can be important. For example, if $ix$ is the 
name of an
infixed constant and $c$ the name of a non-infixed 
constant, then executing:

{\def\con#1{\mbox{\normalsize\sf #1}}
\begin{hol}\begin{alltt}
   set\_interface\_map[`\m{c}`,`\m{ix}`; `\m{ix}`,`\m{c}`];;
\end{alltt}\end{hol}}


\noindent will result in both $c$ and $ix$ being infixes; but 
executing:

{\def\con#1{\mbox{\normalsize\sf #1}}
\begin{hol}\begin{alltt}
   set\_interface\_map[`\m{ix}`,`\m{c}`; `\m{c}`,`\m{ix}`];;
\end{alltt}\end{hol}}


\noindent results in neither $c$ nor $ix$ being an infix:

The current interface map is given by:


\begin{boxed}
\index{interface_map@\ml{interface\_map}|pin}
\begin{verbatim}
   interface_map : void -> string list
\end{verbatim}\end{boxed}

\index{printing, in HOL logic@printing, in \HOL\ logic!under interface maps|(}
To avoid parsing and printing ambiguities, maps must be  single valued (\ie\
one  abbreviation  cannot  abbreviate  two  constants)  and one-to-one (\ie\
different abbreviations must map to  different constants).   Furthermore, if
$a_i$ is already a constant  then it  must be  given an  abbreviation by the
map.  For example:

\begin{hol}\begin{verbatim}
   set_interface_map[`+`,`APPEND`];;
\end{verbatim}\end{hol}

\noindent is not allowed because it would make it unclear how to print 
pre-existing terms involving \ml{+}. To avoid this problem, an abbreviation 
for the old \ml{+} must also be provided.  

The flag \ml{interface\_print}\index{interface_print@\ml{interface\_print}}
 determines whether the inverse of the current
interface map is applied when printing.
This should be useful for debugging as
it shows what is `really there'\index{printing, in HOL logic@printing, in \HOL\ logic!under interface maps|)}.

The following functions are also provided:


\begin{boxed}
\index{is_constant@\ml{is\_constant}|pin}
\index{is_infix@\ml{is\_infix}|pin}
\index{is_binder@\ml{is\_binder}|pin}
\index{constants, in HOL logic@constants, in \HOL\ logic!indicator function for}
\begin{verbatim}
   is_constant : string -> bool
   is_infix    : string -> bool
   is_binder   : string -> bool
\end{verbatim}\end{boxed}

\noindent these tell whether a string is the name of a constant, infix\index{infixes, in HOL logic@infixes, in \HOL\ logic!indicator functions for} or 
binder\index{binders, in HOL logic@binders, in \HOL\ logic!indicator functions for} respectively (note: being an infix or binder entails being a constant).

The function

\begin{boxed}
\index{allowed_constant@\ml{allowed\_constant}|pin}
\begin{verbatim}
   allowed_constant : string -> bool
\end{verbatim}\end{boxed}

\noindent tests whether a string has the correct lexical structure to be a 
constant.

The function:

\begin{boxed}
\index{draft_mode@\ml{draft\_mode}|pin}
\begin{verbatim}
   draft_mode : void -> bool
\end{verbatim}\end{boxed}

\noindent returns \ml{true} in draft mode\index{draft mode, in HOL system@draft mode, in \HOL\ system}\footnote{See Section~\ref{theoryfns}
for an explanation of `draft mode'.} and \ml{false} otherwise.

The following sessions illustrate the use of interface maps (the comments are
provided by the author, not by the system). They
also show the great potential of interface maps for
generating confusion! The first session illustrates checking that
interface maps are well formed.

\setcounter{sessioncount}{1}
\begin{session}\begin{verbatim}
#set_interface_map[`+`,`APPEND`];;
evaluation failed     + would become hidden 

#set_interface_map[`+`,`APPEND`;`+`,`++`];;
evaluation failed     interface map not single valued 

#set_interface_map[`+`,`APPEND`;`++`,`+`];;
[] : (string # string) list
\end{verbatim}\end{session}

\noindent The next session shows the propagation of infix status by the interface map declared above:

\begin{session}\begin{verbatim}
#"1 ++ 2";;                           % ++ is not an infix. %
Badly typed application of:  "1"
   which has type:           ":num"
to the argument term:        "++"
   which has type:           ":num -> (num -> num)"

evaluation failed     mk_comb in quotation 

#"++ 1 2";;
"++ 1 2" : term

#set_interface_map[`++`,`+`; `+`,`APPEND`];;
[(`+`,`APPEND`); (`++`,`+`)] : (string # string) list

#"1 ++ 2";;
"1 ++ 2" : term
\end{verbatim}\end{session}

\noindent Setting the flag \ml{interface\_print} to \ml{false}
causes the true form of terms to be shown.

\begin{session}\begin{verbatim}
#set_flag(`interface_print`, false);;
true : bool

#"1 ++ 2";;
"1 + 2" : term

#dest_const "++";;           % This illustrates potential for confusion. %
`+`,":num -> (num -> num)" : (string # type)
\end{verbatim}\end{session}

\noindent Continuing:
\vfill
\newpage
\begin{session}\begin{verbatim}
#"[1]+[2]";;                 % APPEND is not an infix. %
Badly typed application of:  "[1]"
   which has type:           ":(num)list"
to the argument term:        "APPEND"
   which has type:           ":(?1)list -> ((?1)list -> (?1)list)"

evaluation failed     mk_comb in quotation 

#"+[1][2]";;
"APPEND[1][2]" : term

#"APPEND [1] [2]";;
"APPEND[1][2]" : term
\end{verbatim}\end{session}

\noindent Here is an example illustrating the kind of thing to avoid.

\begin{session}\begin{verbatim}
#ADD1;;                     % Built in theorem. %
Theorem ADD1 autoloaded from theory `arithmetic`.
ADD1 = |- !m. SUC m = m + 1

|- !m. SUC m = m + 1

#set_flag(`interface_print`, true);;
false : bool

#ADD1;;
|- !m. SUC m = m ++ 1

#set_interface_map[`+`,`-`; `-`,`+`];;
[`++`,`+`; `+`,`APPEND`] : (string # string) list

#ADD1;;
|- !m. SUC m = m - 1
\end{verbatim}\end{session}

There are some subtleties and rough edges to the current implementation
of interface maps. For example, any special parsing or printing properties
of special constants (\ml{!}, \ml{?}, {\small\verb%/\%}, {\small\verb%\/%},
\etc) are transferred to the names that abbreviate them. For example, notice the 
disappearing spaces in the next session.

\setcounter{sessioncount}{1}
\begin{session}\begin{verbatim}
#set_interface_map[`Forall`,`!`; `Exists`,`?`];;
[] : (string # string) list

#"Forall x. Exists y. x < y";;
"Forallx. Existsy. x < y" : term
\end{verbatim}\end{session}

\noindent This happens because the printer does not print spaces after
\ml{!} and \ml{?} and this behaviour gets transferred to \ml{Forall} and
\ml{Exists} by the interface map.
It would be straightforward, but rather messy, to have the printer insert 
spaces after \ml{Forall} and \ml{Exists}. This will be implemented, and other 
changes or additions, if experience indicates it is sensible.
Interface maps are still rather
experimental, and it is expected that various details will need 
modification as experience is gained. A feature 
not currently implemented, but possibly useful, would
allow the relative precedences of user-defined infixes to be adjusted.
\index{HOL system@\HOL\ system!interface maps in|)}
\index{interface maps|)}

\subsection{Changing the lambda symbol}
\label{change-lambda}

\index{lambda symbol in HOL logic, resetting@lambda symbol, in \HOL\ logic, resetting|(} 
\index{customization of HOL system@customization of \HOL\ system|(}
\index{function abstraction, in HOL logic@function abstraction, in \HOL\ logic!changing symbol for|(}
\index{HOL system@\HOL\ system!changeable symbols of|(}
\index{symbols in HOL logic, changeable@symbols in \HOL\ logic, changeable|(}

The \ML\ function:

\begin{boxed}
\index{set_lambda@\ml{set\_lambda}|pin}
\begin{verbatim}
   set_lambda : string -> string
\end{verbatim}\end{boxed}

\noindent allows  an  alternative symbol  (or string)  to be  used to represent
lambda ($\lambda$)  inside  quotations.\index{function abstraction, in HOL logic@function abstraction, in \HOL\ logic!changing symbol for}
The  previous $\lambda$-representing
string is returned.  Note that after calling \ml{set\_lambda}:

\begin{itemize}

\item  The symbol  {\small\verb%\%}\index{ function abstraction binder, in HOL logic@{\small\verb+\+} (function abstraction binder, in \HOL\ logic)}
 is  still available for input.

\item All abstractions will be printed with the new string.


\item It is necessary to use:
{\verb%set_lambda `\\`%}
to restore the alternative lambda to {\small\verb%\%}\index{ escape, in ML strings@{\small\verb+\+} (escape, in \ML\ strings)} 
(since {\small\verb%\%} is the escape character inside \ML\ strings).
\end{itemize}

\noindent For example:

\setcounter{sessioncount}{1}
\begin{session}\begin{verbatim}
#set_lambda `Lambda`;;
`\` : string

#"Lambda x.x+1";;
"Lambda x. x + 1" : term

#"\x.x+1";;
"Lambda x. x + 1" : term
\end{verbatim}\end{session}
\index{lambda symbol in HOL logic, resetting@lambda symbol, in \HOL\ logic, resetting|)} 

\subsection{Changing the turnstile symbol}
\label{turnstile}

The turnstile symbol {\small\verb%|-%}, which is used as an {\small ASCII}
approximation to $\vdash$ when printing  theorems, can  be changed  with the 
\ML\ function:

\begin{boxed}
\index{set_turnstile@\ml{set\_turnstile}|pin}
\begin{verbatim}
   set_turnstile : string -> string
\end{verbatim}\end{boxed}

\noindent The previous representation of turnstile is returned. For example:

\setcounter{sessioncount}{1}
\begin{session}\begin{verbatim}
#REFL "x:num";;
|- x = x

#set_turnstile `Proved! `;;
`|- ` : string

#REFL "x:num";;
Proved! x = x

#set_turnstile `       |-  `;;
`Proved! ` : string

#REFL "x:num";;
       |-  x = x
\end{verbatim}\end{session}
\index{customization of HOL system@customization of \HOL\ system|)}
\index{function abstraction, in HOL logic@function abstraction, in \HOL\ logic!changing symbol for|)}
\index{HOL system@\HOL\ system!changeable symbols of|)}
\index{symbols in HOL logic, changeable@symbols in \HOL\ logic, changeable|)}

\subsection{Hiding constants}
\label{hidden}

\index{parsing, of HOL logic@parsing, of \HOL\ logic!hiding constant status in|(}
\index{HOL system@\HOL\ system!hiding constants in|(}
The following function can be used to hide\index{constants, in HOL logic@constants, in \HOL\ logic!hiding status of|(} the constant  status of  a name from
the quotation parser.

\begin{boxed}
\index{hide_constant@\ml{hide\_constant}|pin}
\begin{verbatim}
   hide_constant : string -> void
\end{verbatim}\end{boxed}


\noindent Evaluating \ml{hide\_constant\ `}$x$\ml{`}
makes the quotation parser treat $x$ as a variable (lexical
rules permitting), even if $x$ is the name of a constant in the current theory
(constants and variables can have the same name).  
This is useful if one wants to use variables\index{variables, in HOL logic@variables, in \HOL\ logic!with constant names}  with the same names
as previously declared (or built-in) constants (\eg\ \ml{o}, \ml{I}, \ml{S} 
\etc). 
The name $x$ is still a
constant for the constructors, theories, etc; \ml{hide\_constant} 
affects only  parsing.

Hiding a constant and then attempting to declare it as a new
constant will fail (as it must, if the system is to remain sound).\index{soundness!of HOL logic@of \HOL\ logic}\index{constants, in HOL logic@constants, in \HOL\ logic!hiding status of|)}

The function

\begin{boxed}
\index{unhide_constant@\ml{unhide\_constant}|pin}
\begin{verbatim}
   unhide_constant : string -> void
\end{verbatim}\end{boxed}

\noindent undoes the hiding; it fails if its argument is not a previously 
hidden constant.

The function:

\begin{boxed}
\index{is_hidden@\ml{is\_hidden}|pin}
\begin{verbatim}
   is_hidden : string -> bool
\end{verbatim}\end{boxed}

\noindent tests whether a string is the name of a hidden constant.
\index{HOL system@\HOL\ system!adjustment of user interface of|)}
\index{HOL system@\HOL\ system!hiding constants in|)}
\index{parsing, of HOL logic@parsing, of \HOL\ logic!hiding constant status in|)}

\subsection{Autoloading of axioms, definitions and theorems}\index{axioms!autoloading of, in HOL system@autoloading of, in \HOL\ system}

It is possible to mark the name of an axiom, definition or  theorem so that
if it occurs in an \ML\ expression  then the  named item  will be automatically
loaded\index{loading, of HOL theories@loading, of \HOL\ theories}\index{automatic loading, of HOL theory components@automatic loading, of \HOL\ theory components} from  the appropriate  theory before  the \ML\  expression is evaluated.
This provides a kind of crude `paging'\index{theories, in HOL logic@theories, in \HOL\ logic!paging of} of theories.   Many standard definitions
and theorems are set up to autoload
 if they are used.

The function to set up an autoloading action is:

\begin{boxed}
\index{autoload_theory@\ml{autoload\_theory}|pin}
\begin{verbatim}
   autoload_theory : (string # string # string) -> void
\end{verbatim}\end{boxed}

\noindent Evaluating
\ml{autoload\_theory(`}$kind$\ml{`,`}$thy$\ml{`,`}$name$\ml{`)}, where   $kind$
is \ml{axiom}, \ml{definition} or \ml{theorem}, has the  side effect of setting
the name  $name$  so  that  when  it  is  encountered by  the  \ML\ parser the
appropriate sort of object will be autoloaded from the theory $thy$.   Note that
constant specifications\index{constant specifications, in HOL logic@constant specifications, in \HOL\ logic!autoloading of}\index{type definitions, in HOL logic@type definitions, in \HOL\ logic!autoloading of} and type definitions are  regarded as  special cases of
definitions.

An autoloading action set up with \ml{autoload\_theory} will only be done once,
namely the first time the name is encountered in the session.  Autoload actions
can be removed from a name using the function:

\begin{boxed}
\index{undo_autoload@\ml{undo\_autoload}|pin}
\begin{verbatim}
   undo_autoload : string -> bool
\end{verbatim}\end{boxed}

The value \ml{true} is returned
if an autoload action had been set up, \ml{false} is returned otherwise.

\subsection{Cached theories}

When a  theory  is  first  accessed  using   the  functions  \ml{load\_theory},
\ml{axiom}, \ml{definition}, \ml{theorem} \etc, the  whole theory  is read from
disk into a data-structure in memory called a {\it theory cache\/}.\index{theories, in HOL logic@theories, in \HOL\ logic!caching of}\index{caching}\index{theory caching}\index{HOL system@\HOL\ system!caching theories in}  Subsequent
accesses then refer to this in-memory cache  and are  therefore faster.   Theories,
however, can be very large, and on machines with limited memory,
this can  lead to a lot of
garbage collection, or even running out of space.  To deal with
this problem, the \ML\ function:

\begin{boxed}
\index{delete_cache@\ml{delete\_cache}|pin}
\begin{verbatim}
   delete_cache : string -> void
\end{verbatim}\end{boxed}


\noindent can be used to delete the theory cache (but not the theory 
file on disk), thereby 
freeing up some space (at the cost of making the next theory access slower). 
For example, a function that fetches a single theorem without 
bringing in the whole theory is:

\begin{hol}\begin{verbatim}
   let fetch_thm_without_caching thy thm =
       let th = theorem thy thm
       in  (delete_cache thy; th);;
\end{verbatim}\end{hol}

Whether  a theory has been cached or not can be discovered with the function:

\begin{boxed}
\index{cached_theories@\ml{cached\_theories}|pin}
\begin{verbatim}
   cached_theories : void -> (string # bool) list
\end{verbatim}\end{boxed}

\noindent This returns a list of pairs 
\ml{(}$thy$\ml{,}$b$\ml{)} where $thy$ is the name of 
a theory that has been loaded and
$b$ is a truth value that is \ml{true} if $thy$'s cache has been deleted with
\ml{delete\_cache} and \ml{false} otherwise.

\subsection{Adjusting the pretty-print depth}

\index{ML@\ML!pretty printer for|(}
The following \ML\ function  can be used to adjust the maximum depth of 
printing\index{printing, in HOL logic@printing, in \HOL\ logic!structural depth adjustment in}.

\begin{boxed}
\index{max_print_depth@\ml{max\_print\_depth}|pin}
\begin{verbatim}
   max_print_depth : int -> int
\end{verbatim}\end{boxed}

\noindent The default print depth\index{default print depth, for HOL logic@default print depth, for \HOL\ logic|(} is $500$. Evaluating
\ml{max\_print\_depth}$\ n$ sets the maximum to $n$ and returns 
the previous value
of the maximum. Subterms nested more deeply than 
the maximum print depth are printed as
{\small\verb%&%}. For example:

\setcounter{sessioncount}{1}
\begin{session}\begin{verbatim}
#ADD_CLAUSES;;
Theorem ADD_CLAUSES autoloaded from theory `arithmetic`.
ADD_CLAUSES = 
|- (0 + m = m) /\
   (m + 0 = m) /\
   ((SUC m) + n = SUC(m + n)) /\
   (m + (SUC n) = SUC(m + n))

|- (0 + m = m) /\
   (m + 0 = m) /\
   ((SUC m) + n = SUC(m + n)) /\
   (m + (SUC n) = SUC(m + n))

#max_print_depth 7;;
500 : int

#ADD_CLAUSES;;
|- (& + & = m) /\ (& + & = m) /\ ((& + & = &(&) /\ (& + (& = &(&)

#max_print_depth 5;;
7 : int

#ADD_CLAUSES;;
|- (& /\ (& /\ (& /\ (&

#max_print_depth 3;;
5 : int

#ADD_CLAUSES;;
|- &
\end{verbatim}\end{session}
\index{default print depth, for HOL logic@default print depth, for \HOL\ logic|)}

\noindent For more details about pretty printing in \ML, see
Section~\ref{pretty-print}.
\index{ML@\ML!pretty printer for|)}

\subsection{Timing and counting theorems}

Whenever \HOL\  performs  a  primitive  inference  (or  accepts   an  axiom  or
definition) a counter\index{inferences, in HOL logic@inferences, in \HOL\ logic!counting of}\index{timing of HOL evaluations@timing of \HOL\ evaluations}\index{HOL system@\HOL\ system!timing and counting inferences in}  is incremented.  The value of this counter is returned by
the function:

\begin{boxed}
\index{thm_count@\ml{thm\_count}|pin}
\index{counting inferences, in HOL proofs@counting inferences, in \HOL\ proofs}
\begin{verbatim}
   thm_count : void -> int 
\end{verbatim}\end{boxed}

\noindent This counter can be reset with the function:

\begin{boxed}
\index{set_thm_count@\ml{set\_thm\_count}|pin}
\begin{verbatim}
   set_thm_count : int -> int
\end{verbatim}\end{boxed}
 
\noindent The previous value of the counter is returned.

The following function is used to switch \ML\ into a  mode in  which the number
of primitive inferences done during each top-level interaction is  shown.  
Run-time and garbage collection time are also shown.

\begin{boxed}
\index{timer@\ml{timer}|pin}
\begin{verbatim}
   timer : bool -> bool
\end{verbatim}\end{boxed}

\noindent Executing \ml{timer true} causes  the number  of primitive inferences
and timings to be  printed; \ml{timer  false} switches  the printing  off.  The
previous setting  is  
returned.    Executing  \ml{timer}$\ b$  is equivalent to
setting the flag \ml{timing} to the value $b$ (see Section~\ref{flags}).