File: type-set.lisp

package info (click to toggle)
acl2 8.6%2Bdfsg-3
  • links: PTS
  • area: main
  • in suites: sid
  • size: 1,138,276 kB
  • sloc: lisp: 17,818,294; java: 125,359; python: 28,122; javascript: 23,458; cpp: 18,851; ansic: 11,569; perl: 7,678; xml: 5,591; sh: 3,978; makefile: 3,840; ruby: 2,633; yacc: 1,126; ml: 763; awk: 295; csh: 233; lex: 197; php: 178; tcl: 49; asm: 23; haskell: 17
file content (2871 lines) | stat: -rw-r--r-- 117,631 bytes parent folder | download | duplicates (5)
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
(in-package "PACO")

; ---------------------------------------------------------------------------
; Section:  Computing the Types of Terms

(defun def-basic-type-sets1 (lst i)
  (cond ((endp lst) nil)
        (t (cons (list 'defconst (car lst) (list 'the-type-set (expt 2 i)))
                 (def-basic-type-sets1 (cdr lst) (+ i 1))))))

(defmacro def-basic-type-sets (&rest lst)
  (let ((n (length lst)))
    `(progn
       (defconst *actual-primitive-types* ',lst)
       (defconst *min-type-set* (- (expt 2 ,n)))
       (defconst *max-type-set* (- (expt 2 ,n) 1))
       (defmacro the-type-set (x)
         `(the (integer ,*min-type-set* ,*max-type-set*) ,x))
       ,@(def-basic-type-sets1 lst 0))))

(defun list-of-the-type-set (x)
  (cond ((consp x)
         (cons (list 'the-type-set (car x))
               (list-of-the-type-set (cdr x))))
        (t nil)))

(defmacro ts= (a b)
  (list '= (list 'the-type-set a) (list 'the-type-set b)))

(defmacro ts-complement (x)
  (list 'the-type-set (list 'lognot (list 'the-type-set x))))

(defmacro ts-complementp (x)
  (list 'minusp x))

(defun ts-union-fn (x)
  (list 'the-type-set
        (cond ((null x) '*ts-empty*)
              ((null (cdr x)) (car x))
              (t (xxxjoin 'logior
                          (list-of-the-type-set x))))))

(defmacro ts-union (&rest x)
  (declare (xargs :guard (true-listp x)))
  (ts-union-fn x))

(defmacro ts-intersection (&rest x)
  (list 'the-type-set
        (cons 'logand (list-of-the-type-set x))))

(defmacro ts-intersectp (&rest x)
  (list 'not (list 'ts= (cons 'ts-intersection x) '*ts-empty*)))

(defmacro ts-subsetp (ts1 ts2)
  (list 'let (list (list 'ts1 ts1)
                   (list 'ts2 ts2))
        '(ts= (ts-intersection ts1 ts2) ts1)))

(defun ts-builder-macro1 (x case-lst seen)
  (cond ((endp case-lst) nil)
        ((or (eq (caar case-lst) t)
             (eq (caar case-lst) 'otherwise))
         (sublis (list (cons 'x x)
                       (cons 'seen seen)
                       (cons 'ts2 (cadr (car case-lst))))
                 '((cond ((ts-intersectp x (ts-complement (ts-union . seen)))
                          ts2)
                         (t *ts-empty*)))))
        (t (cons (sublis (list (cons 'x x)
                               (cons 'ts1 (caar case-lst))
                               (cons 'ts2 (cadr (car case-lst))))
                         '(cond ((ts-intersectp x ts1) ts2)
                                (t *ts-empty*)))
                 (ts-builder-macro1 x (cdr case-lst) (cons (caar case-lst)
                                                           seen))))))

(defun ts-builder-macro (x case-lst)
  (cons 'ts-union
        (ts-builder-macro1 x case-lst nil)))

(defmacro ts-builder (&rest args)
  (ts-builder-macro (car args) (cdr args)))

(def-basic-type-sets
  *ts-zero*
  *ts-one*
  *ts-integer>1*
  *ts-positive-ratio*
  *ts-negative-integer*
  *ts-negative-ratio*
  *ts-complex-rational*
  *ts-nil*
  *ts-t*
  *ts-non-t-non-nil-symbol*
  *ts-proper-cons*
  *ts-improper-cons*
  *ts-string*
  *ts-character*)

(defconst *ts-positive-integer* (ts-union *ts-one*
                                          *ts-integer>1*))

(defconst *ts-non-negative-integer* (ts-union *ts-zero*
                                              *ts-positive-integer*))

(defconst *ts-non-positive-integer* (ts-union *ts-zero*
                                              *ts-negative-integer*))

(defconst *ts-integer* (ts-union *ts-positive-integer*
                                 *ts-zero*
                                 *ts-negative-integer*))

(defconst *ts-rational* (ts-union *ts-integer*
                                  *ts-positive-ratio*
                                  *ts-negative-ratio*))

(defconst *ts-acl2-number* (ts-union *ts-rational*
                                     *ts-complex-rational*))

(defconst *ts-rational-acl2-number* (ts-union *ts-rational*
                                              *ts-complex-rational*))

(defconst *ts-negative-rational* (ts-union *ts-negative-integer*
                                           *ts-negative-ratio*))

(defconst *ts-positive-rational* (ts-union *ts-positive-integer*
                                           *ts-positive-ratio*))

(defconst *ts-non-positive-rational* (ts-union *ts-zero*
                                               *ts-negative-rational*))

(defconst *ts-non-negative-rational* (ts-union *ts-zero*
                                               *ts-positive-rational*))

(defconst *ts-ratio* (ts-union *ts-positive-ratio*
                               *ts-negative-ratio*))

(defconst *ts-cons* (ts-union *ts-proper-cons*
                              *ts-improper-cons*))

(defconst *ts-boolean* (ts-union *ts-nil* *ts-t*))

(defconst *ts-true-list* (ts-union *ts-nil* *ts-proper-cons*))

(defconst *ts-non-nil* (ts-complement *ts-nil*))

(defconst *ts-symbol* (ts-union *ts-nil*
                                *ts-t*
                                *ts-non-t-non-nil-symbol*))

(defconst *ts-true-list-or-string* (ts-union *ts-true-list* *ts-string*))

(defconst *ts-empty* 0)

(defconst *ts-unknown* -1)

(defun type-set-binary-+-alist-entry (ts1 ts2)
  (ts-builder ts1
              (*ts-zero* ts2)
              (*ts-one*
               (ts-builder ts2
                           (*ts-zero* ts1)
                           (*ts-one* *ts-integer>1*)
                           (*ts-integer>1* *ts-integer>1*)
                           (*ts-negative-integer* *ts-non-positive-integer*)
                           (*ts-positive-ratio* *ts-positive-ratio*)
                           (*ts-negative-ratio* *ts-ratio*)
                           (*ts-complex-rational* *ts-complex-rational*)
                           ))
              (*ts-integer>1*
               (ts-builder ts2
                           (*ts-zero* ts1)
                           (*ts-one* *ts-integer>1*)
                           (*ts-integer>1* *ts-integer>1*)
                           (*ts-negative-integer* *ts-integer*)
                           (*ts-positive-ratio* *ts-positive-ratio*)
                           (*ts-negative-ratio* *ts-ratio*)
                           (*ts-complex-rational* *ts-complex-rational*)
                           ))
              (*ts-negative-integer*
               (ts-builder ts2
                           (*ts-zero* ts1)
                           (*ts-one* *ts-non-positive-integer*)
                           (*ts-integer>1* *ts-integer*)
                           (*ts-negative-integer* *ts-negative-integer*)
                           (*ts-positive-ratio* *ts-ratio*)
                           (*ts-negative-ratio* *ts-negative-ratio*)
                           (*ts-complex-rational* *ts-complex-rational*)
                           ))
              (*ts-positive-ratio*
               (ts-builder ts2
                           (*ts-zero* ts1)
                           (*ts-one* *ts-positive-ratio*)
                           (*ts-integer>1* *ts-positive-ratio*)
                           (*ts-negative-integer* *ts-ratio*)
                           (*ts-positive-ratio* *ts-positive-rational*)
                           (*ts-negative-ratio* *ts-rational*)
                           (*ts-complex-rational* *ts-complex-rational*)
                           ))
              (*ts-negative-ratio*
               (ts-builder ts2
                           (*ts-zero* ts1)
                           (*ts-one* *ts-ratio*)
                           (*ts-integer>1* *ts-ratio*)
                           (*ts-negative-integer* *ts-negative-ratio*)
                           (*ts-positive-ratio* *ts-rational*)
                           (*ts-negative-ratio* *ts-negative-rational*)
                           (*ts-complex-rational* *ts-complex-rational*)
                           ))
              (*ts-complex-rational*
               (ts-builder ts2
                           (*ts-zero* ts1)
                           (*ts-one* *ts-complex-rational*)
                           (*ts-integer>1* *ts-complex-rational*)
                           (*ts-negative-integer* *ts-complex-rational*)
                           (*ts-positive-ratio* *ts-complex-rational*)
                           (*ts-negative-ratio* *ts-complex-rational*)
                           (*ts-complex-rational* *ts-rational-acl2-number*)
                           ))
              ))

(defun type-set-binary-*-alist-entry (ts1 ts2)
  (ts-builder ts1
              (*ts-zero* *ts-zero*)
              (*ts-one* ts2)
              (*ts-integer>1*
               (ts-builder ts2
                           (*ts-zero* *ts-zero*)
                           (*ts-one* ts1)
                           (*ts-integer>1* *ts-integer>1*)
                           (*ts-negative-integer* *ts-negative-integer*)
                           (*ts-positive-ratio* *ts-positive-rational*)
                           (*ts-negative-ratio* *ts-negative-rational*)
                           (*ts-complex-rational* *ts-complex-rational*)
                           ))
              (*ts-negative-integer*
               (ts-builder ts2
                           (*ts-zero* *ts-zero*)
                           (*ts-one* ts1)
                           (*ts-integer>1* *ts-negative-integer*)
                           (*ts-negative-integer* *ts-positive-integer*)
                           (*ts-positive-ratio* *ts-negative-rational*)
                           (*ts-negative-ratio* *ts-positive-rational*)
                           (*ts-complex-rational* *ts-complex-rational*)
                           ))
              (*ts-positive-ratio*
               (ts-builder ts2
                           (*ts-zero* *ts-zero*)
                           (*ts-one* ts1)
                           (*ts-integer>1* *ts-positive-rational*)
                           (*ts-negative-integer* *ts-negative-rational*)
                           (*ts-positive-ratio* *ts-positive-rational*)
                           (*ts-negative-ratio* *ts-negative-rational*)
                           (*ts-complex-rational* *ts-complex-rational*)
                           ))
              (*ts-negative-ratio*
               (ts-builder ts2
                           (*ts-zero* *ts-zero*)
                           (*ts-one* ts1)
                           (*ts-integer>1* *ts-negative-rational*)
                           (*ts-negative-integer* *ts-positive-rational*)
                           (*ts-positive-ratio* *ts-negative-rational*)
                           (*ts-negative-ratio* *ts-positive-rational*)
                           (*ts-complex-rational* *ts-complex-rational*)
                           ))
              (*ts-complex-rational*
               (ts-builder ts2
                           (*ts-zero* *ts-zero*)
                           (*ts-one* ts1)
                           (*ts-integer>1* *ts-complex-rational*)
                           (*ts-negative-integer* *ts-complex-rational*)
                           (*ts-positive-ratio* *ts-complex-rational*)
                           (*ts-negative-ratio* *ts-complex-rational*)
                           (*ts-complex-rational*
                            (ts-intersection *ts-rational-acl2-number*
                                              (ts-complement *ts-zero*)))
                           ))
              ))

(defun type-set-<-alist-entry (ts1 ts2)
  (ts-builder ts1
              (*ts-zero*
               (ts-builder ts2
                           (*ts-zero* *ts-nil*)
                           (*ts-one* *ts-t*)
                           (*ts-integer>1* *ts-t*)
                           (*ts-negative-integer* *ts-nil*)
                           (*ts-positive-ratio* *ts-t*)
                           (*ts-negative-ratio* *ts-nil*)
                           ))
              (*ts-one*
               (ts-builder ts2
                           (*ts-zero* *ts-nil*)
                           (*ts-one* *ts-nil*)
                           (*ts-integer>1* *ts-t*)
                           (*ts-negative-integer* *ts-nil*)
                           (*ts-positive-ratio* *ts-boolean*)
                           (*ts-negative-ratio* *ts-nil*)
                           ))
              (*ts-integer>1*
               (ts-builder ts2
                           (*ts-zero* *ts-nil*)
                           (*ts-one* *ts-nil*)
                           (*ts-integer>1* *ts-boolean*)
                           (*ts-negative-integer* *ts-nil*)
                           (*ts-positive-ratio* *ts-boolean*)
                           (*ts-negative-ratio* *ts-nil*)
                           ))
              (*ts-negative-integer*
               (ts-builder ts2
                           (*ts-zero* *ts-t*)
                           (*ts-one* *ts-t*)
                           (*ts-integer>1* *ts-t*)
                           (*ts-negative-integer* *ts-boolean*)
                           (*ts-positive-ratio* *ts-t*)
                           (*ts-negative-ratio* *ts-boolean*)
                           ))
              (*ts-positive-ratio*
               (ts-builder ts2
                           (*ts-zero* *ts-nil*)
                           (*ts-one* *ts-boolean*)
                           (*ts-integer>1* *ts-boolean*)
                           (*ts-negative-integer* *ts-nil*)
                           (*ts-positive-ratio* *ts-boolean*)
                           (*ts-negative-ratio* *ts-nil*)
                           ))
              (*ts-negative-ratio*
               (ts-builder ts2
                           (*ts-zero* *ts-t*)
                           (*ts-one* *ts-t*)
                           (*ts-integer>1* *ts-t*)
                           (*ts-negative-integer* *ts-boolean*)
                           (*ts-positive-ratio* *ts-t*)
                           (*ts-negative-ratio* *ts-boolean*)
                           ))

              ))

(defun numeric-type-set (ts)

; This coerces ts into the type *ts-acl2-number*.  That is, if ts contains
; nonnumeric bits then those bits are shut off and *ts-zero* is turned on.
; Another way to look at it is that if term has type ts then (fix term) has
; type (numeric-type-set ts).

  (let ((numeric-subtype
         (ts-intersection ts *ts-acl2-number*)))
    (if (ts= numeric-subtype ts)
        ts
      (ts-union numeric-subtype *ts-zero*))))

(defun rational-type-set (ts)

; This function is like numeric-type-set, but coerces ts to the
; rationals.

  (let ((rational-subtype
         (ts-intersection ts *ts-rational*)))
    (if (ts= rational-subtype ts)
        ts
      (ts-union rational-subtype *ts-zero*))))

; We start with probably the most complicated primitive type set
; function, that for binary-+.

(defun type-set-binary-+ (term ts1 ts2)

; Because 1- (i.e., SUB1) is so common and often is applied to
; strictly positive integers, it is useful to know that, in such
; cases, the result is a non-negative integer.  We therefore test for
; (+ x -1) and its commuted version (+ -1 x).  To be predictable, we
; also look for (+ x +1), and its commuted version, when x is strictly
; negative.  We specially arrange for the answer type-set to be empty
; if either of the input type-sets is empty.  This occurs when we are
; guessing type sets.  The idea is that some other branch ought to
; give us a nonempty type-set before this one can meaningfully
; contribute to the answer.  Before we added the special processing of
; +1 and -1 we did not have to check for the empty case because the
; array referenced by aref2 has the property that if either type-set
; is empty the result is empty.

  (let ((arg1 (fargn term 1))
        (arg2 (fargn term 2)))
    (cond ((or (ts= ts1 *ts-empty*)
               (ts= ts2 *ts-empty*))
           *ts-empty*)
          ((and (equal arg2 ''-1)
                (ts-subsetp ts1 *ts-positive-integer*))
           *ts-non-negative-integer*)
          ((and (equal arg1 ''-1)
                (ts-subsetp ts2 *ts-positive-integer*))
           *ts-non-negative-integer*)
          (t (type-set-binary-+-alist-entry
              (numeric-type-set ts1)
              (numeric-type-set ts2))))))

(defun type-set-binary-* (ts1 ts2)
  (cond ((or (ts= ts1 *ts-empty*)
             (ts= ts2 *ts-empty*))
         *ts-empty*)
        (t (type-set-binary-*-alist-entry
            (numeric-type-set ts1)
            (numeric-type-set ts2)))))

(defun type-set-not (ts)
  (cond
   ((ts= ts *ts-nil*)
    *ts-t*)
   ((ts-subsetp *ts-nil* ts)
    *ts-boolean*)
   (t *ts-nil*)))

(defun type-set-< (arg1 arg2 ts1 ts2)

  (declare (xargs :measure (+ (acl2-count arg1)
                              (acl2-count arg2))))

; This function is not cut from the standard mold because instead of
; taking term it takes the two args.  This allows us easily to
; implement certain transformations on inequalities: When x is an
; integer,

; (<  x 1) is (not (< 0 x)) and
; (< -1 x) is (not (< x 0)).

; Warning: It is important to assume-true-false that type-set-< make
; these transformations.  See the comments about type-set-< in
; assume-true-false.

  (let* ((nts1 (numeric-type-set ts1))
         (nts2 (numeric-type-set ts2)))
    (cond ((and (equal arg1 *-1*)

; Actually we don't have to add 0 back in, as done by numeric-type-set, before
; making the following test.  But let's keep things simple.

                (ts-subsetp nts1 *ts-integer*))
           (type-set-not
            (type-set-< arg2 *0* ts2 *ts-zero*)))
          ((or (ts-intersectp ts1 *ts-complex-rational*)
               (ts-intersectp ts2 *ts-complex-rational*))
           *ts-boolean*)
          (t (type-set-<-alist-entry nts1 nts2)))))

(defun type-set-unary-- (ts)
  (let ((ts1 (numeric-type-set ts)))
    (cond
     ((ts= ts1 *ts-acl2-number*)
      *ts-acl2-number*)
     (t
      (ts-builder ts1
                  (*ts-zero* *ts-zero*)
                  (*ts-one* *ts-negative-integer*)
                  (*ts-integer>1* *ts-negative-integer*)
                  (*ts-positive-ratio* *ts-negative-ratio*)
                  (*ts-negative-integer* *ts-positive-integer*)
                  (*ts-negative-ratio* *ts-positive-ratio*)
                  (*ts-complex-rational* *ts-complex-rational*))))))

(defun type-set-unary-/ (ts)
  (let* ((ts1 (numeric-type-set ts)))
    (ts-builder ts1
                (*ts-zero* *ts-zero*)
                (*ts-one* *ts-one*)
                (*ts-integer>1* *ts-positive-ratio*)
                (*ts-positive-ratio* *ts-positive-rational*)
                (*ts-negative-rational* *ts-negative-rational*)
                (*ts-complex-rational* *ts-complex-rational*))))

(defun type-set-numerator (ts)
  (let* ((ts1 (rational-type-set ts)))
    (ts-builder ts1
                (*ts-zero* *ts-zero*)
                (*ts-one* *ts-one*)
                (*ts-integer>1* *ts-integer>1*)
                (*ts-positive-ratio* *ts-positive-integer*)
                (*ts-negative-rational* *ts-negative-integer*))))

(defun type-set-denominator (ts)
  (let* ((ts1 (rational-type-set ts)))
    (ts-builder ts1
                (*ts-integer* *ts-one*)
                (*ts-ratio* *ts-integer>1*))))

(defun type-set-realpart (ts)
  (cond ((ts-intersectp ts *ts-complex-rational*)
         *ts-rational*)
        (t (numeric-type-set ts))))

(defun type-set-imagpart (ts)
  (cond ((ts-subsetp ts *ts-complex-rational*)
         (ts-union *ts-positive-rational*
                   *ts-negative-rational*))
        ((ts-intersectp ts *ts-complex-rational*)
         *ts-rational*)
        (t *ts-zero*)))

(defun type-set-complex (ts1 ts2)
  (let ((ts1 (rational-type-set ts1))
        (ts2 (rational-type-set ts2)))
    (cond ((ts= ts2 *ts-zero*)
           ts1)
          ((ts= (ts-intersection ts2 *ts-zero*)
                *ts-empty*)
           *ts-complex-rational*)
          ((ts= ts1 *ts-rational*)
           *ts-acl2-number*)
          (t (ts-union ts1 *ts-complex-rational*)))))

; Essay on the Recognizer-Alist and Recognizer-Tuples

; The recognizer-alist is stored as a global variable in the world w
; and accessed via

; (global-val 'recognizer-alist w).

; The recognizer alist contains records of the following form:

(defrec recognizer-tuple
  (nume (fn . true-ts) . (false-ts . strongp)))

; The initial value of the recognizer alist is shown after we discuss the
; meaning of these records.

; In a recognizer-tuple, fn is the name of some Boolean-valued
; function of one argument.  True-ts and and false-ts are type sets.
; If such a record is on the recognizer-alist then it is the case that
; (fn x) implies that the type set of x is a subset of true-ts and
; (not (fn x)) implies that the type set of x is a subset of false-ts.
; Furthermore, if strongp is t, then true-ts is the complement of
; false-ts; i.e., (fn x) recognizes exactly the subset identified by
; true-ts.

; For example, if we prove that

; (BOOLEANP X) -> (OR (EQUAL X T) (EQUAL X NIL))

; then we can add the following tuple

; (make recognizer-tuple
;       :nume 123
;       :fn BOOLEANP
;       :true-ts *ts-boolean*
;       :false-ts *ts-unknown*
;       :strongp nil)

; to the list.  Observe that the false-ts for this pair does not tell us
; much.  But if we proved the above AND

; (NOT (BOOLEANP X)) -> (NOT (OR (EQUAL X T) (EQUAL X NIL)))

; we could add the tuple:

; (make recognizer-tuple
;       :nume 123
;       :fn BOOLEANP
;       :true-ts *ts-boolean*
;       :false-ts (ts-complement *ts-boolean*)
;       :strongp t)

; And we would know as much about BOOLEANP as we know about integerp.

; Consider the function PRIMEP.  It implies its argument is a positive
; integer.  Its negation tells us nothing about the type of its argument.

; (make recognizer-tuple
;       :nume 123
;       :fn PRIMEP
;       :true-ts *ts-positive-integer*
;       :false-ts *ts-unknown*
;       :strongp nil)

; Suppose now x is a term whose type set we know.  What is the type
; set of (PRIMEP x)?  If the type set for x includes the positive
; integer bit, the type set for (PRIMEP x) may include *ts-t* so we
; will throw that in.  If the type set for x includes any of
; *ts-unknown*'s bits (of course it does) we will throw in *ts-nil*.
; The interesting thing about this is that if the type set of x does
; not include the positive integers, we'll know (PRIME x) is nil.

; If we assume (PRIME x) true, we will restrict the type of x to the
; positive integers.  If we assume (PRIME x) false, we won't restrict
; x at all.

; Consider the function RATTREEP that recognizes cons-trees of
; rational numbers.  We can prove that (RATTREEP x) implies the type
; set of x is in *ts-cons* union *ts-rational*.  We can prove that
; (NOT (RATTREEP x)) implies that the type set of x is not
; *ts-rational*.  That means the false-ts for RATTREEP is the
; complement of the rationals.  If we were asked to get the type set
; of (RATTREEP x) where x is rational, we'd throw in a *ts-t* because
; the type of x intersects the true-ts and we'd not throw in anything
; else (because the type of x does not interesect the false ts).  If
; we were asked to assume (RATTREEP x) then on the true branch x's
; type would be interesected with the conses and the rationals.  On
; the false branch, the rationals would be deleted.

(defun most-recent-enabled-recog-tuple (fn alist ens)

; This function finds the first recognizer-tuple on alist whose :fn is
; fn.

  (cond ((endp alist) nil)
        ((and (eq fn (access recognizer-tuple (car alist) :fn))
              (enabled-numep (access recognizer-tuple (car alist) :nume)
                             ens))
         (car alist))
        (t (most-recent-enabled-recog-tuple fn (cdr alist) ens))))

(defun type-set-recognizer (recog-tuple arg-ts)

; Recog-tuple is a recognizer-tuple.  Then we know that (fn x) implies
; that the type set of x, arg-ts, is a subset of true-ts.
; Furthermore, we know that ~(fn x) implies that arg-ts is a subset of
; false-ts.  In addition, we know that fn is a Boolean valued fn.

; This function is supposed to determine the type set of (fn x) where
; arg-ts is the type set of x.  Observe that if arg-ts intersects with
; true-ts then (fn x) might be true, so we should throw in *ts-t*.
; Conversely, if arg-ts does not intersect with true-ts then (fn x)
; cannot possibly be true.  Exactly analogous statements can be made
; about false-ts.

; We return the type set of (fn x).

  (ts-builder
   arg-ts
   ((access recognizer-tuple recog-tuple :true-ts) *ts-t*)
   ((access recognizer-tuple recog-tuple :false-ts) *ts-nil*)))

(defun type-set-car (ts)
  (cond ((ts-intersectp ts *ts-cons*) *ts-unknown*)
        (t *ts-nil*)))

(defun type-set-cdr (ts)
  (ts-builder ts
              (*ts-proper-cons* *ts-true-list*)
              (*ts-improper-cons* (ts-complement *ts-true-list*))
              (otherwise *ts-nil*)))

(defun type-set-coerce (term ts1 ts2)

  (cond ((equal (fargn term 2) ''list)

; If the first argument is of type *ts-string* then the result could
; be either nil or a proper cons.  But if the first argument isn't
; possibly a string, the result is NIL.

         (cond ((ts-intersectp *ts-string* ts1)
                *ts-true-list*)
               (t *ts-nil*)))
        ((quotep (fargn term 2))
         *ts-string*)
        ((not (ts-intersectp *ts-non-t-non-nil-symbol* ts2))
; Observe that the first argument doesn't matter here.
         *ts-string*)
        (t (ts-union *ts-true-list* *ts-string*))))

(defun type-set-intern-in-package-of-symbol (ts1 ts2)
  (cond ((not (ts-intersectp ts1 *ts-string*))
         *ts-nil*)
        ((not (ts-intersectp ts2 *ts-symbol*))
         *ts-nil*)
        (t *ts-symbol*)))

(defun type-set-length (ts)
  (ts-builder ts
              (*ts-string* *ts-non-negative-integer*)
              (*ts-cons* *ts-positive-integer*)
              (otherwise *ts-zero*)))

(defun type-set-cons (ts2)

; Ts2 is the type set of the second argument of the cons.

  (ts-builder ts2
              (*ts-true-list* *ts-proper-cons*)
              (otherwise *ts-improper-cons*)))

(defconst *singleton-type-sets*
  (list *ts-t* *ts-nil* *ts-zero*))

(defun type-set-equal (ts1 ts2)
  (cond ((member ts1 *singleton-type-sets*)
         (cond ((ts= ts1 ts2) *ts-t*)
               ((ts-intersectp ts1 ts2)
                *ts-boolean*)
               (t *ts-nil*)))
        ((ts-intersectp ts1 ts2)
         *ts-boolean*)
        (t *ts-nil*)))

(defun type-set-quote (evg)
  (cond ((atom evg)
         (cond ((rationalp evg)
                (cond ((integerp evg)
                       (cond ((int= evg 0) *ts-zero*)
                             ((int= evg 1) *ts-one*)
                             ((> evg 0) *ts-integer>1*)
                             (t *ts-negative-integer*)))
                      ((> evg 0) *ts-positive-ratio*)
                      (t *ts-negative-ratio*)))
               ((complex-rationalp evg)
                *ts-complex-rational*)
               ((symbolp evg)
                (cond ((eq evg t) *ts-t*)
                      ((eq evg nil) *ts-nil*)
                      (t *ts-non-t-non-nil-symbol*)))
               ((stringp evg) *ts-string*)
               (t *ts-character*)))
        ((true-listp evg)
         *ts-proper-cons*)
        (t *ts-improper-cons*)))

(defun type-set-char-code (ts)

; (char-code x) is always a non-negative integer.  If x is not a
; characterp, then its code is 0.  If x is a character, its code
; might be 0 or positive.

  (cond ((not (ts-intersectp ts *ts-character*))
         *ts-zero*)
        (t *ts-non-negative-integer*)))

; Type Prescriptions

; A type-prescription is a structure, below, that describes how to
; compute the type of a term.  They are stored on the property list of
; the top function symbol of the term, under the property
; 'type-prescriptions.  Unlike Nqthm's "type-prescription-lst" ANY
; enabled type-prescription in 'type-prescriptions may contribute to
; the type-set of the associated function symbol.

(defrec type-prescription
  (nume (basic-ts . term) . (hyps . vars)))

; Term is a term, hyps is a list of terms, basic-ts is a type-set, and vars is
; a list of variables that occur in term.  Let term' be some instance of term
; under the substitution sigma.  Then, provided the sigma instance of hyps is
; true, the type-set of term' is the union of basic-ts with the type-sets of
; the sigma images of the vars.

; For example, for APP we might have the type-prescription:

; (make type-prescription
;       :nume 123
;       :term (app x y)
;       :hyps ((true-listp x))
;       :basic-ts *ts-cons*
;       :vars '(y))

; The above example corresponds to what we'd get from the lemma:
; (implies (true-listp x)
;          (or (consp (app x y))
;              (equal (app x y) y)))

; When type-set uses :TYPE-PRESCRIPTION rules it will intersect all
; the known type-sets for term.

(defconst *expandable-boot-strap-non-rec-fns*
  '(not
    implies eq atom eql = /= null endp zerop
    synp
    plusp minusp listp prog2$ force case-split))

; Warning: All functions listed above must be defun'd non-recursively
; in axioms.lisp!  In addition, all of the guards involved in the
; guarded bodies of the functions must be satisfied.  In particular,
; we assume that the bodies of these functions could pass
; verify-guards, and in fact that boot-strapping sets their
; 'unnormalized-body properties (see distribute-first-if).
; One "benefit" of being on this list is that type-set expands the
; function inline.  However, for best results, any function used in
; the guard of a function on this list ought to be well understood by
; type-set.  For example, eql is on the list.  The guard of eql
; involves eqlablep.  For best results, eqlablep ought to be well
; understood by type-set, which, in that particular case can be
; arranged by making it a compound recognizer.

; There has been some thought about whether we should put IFF on this
; list.  We have decided not, because type-set knows a lot about it by
; virtue of its being an equivalence relation.  But this position has
; never been seriously scrutinized.

; In a break with nqthm, we have decided to let type-set expand some
; function applications to get better type-sets for them.  The
; functions in question are those listed above.

; In an even more pervasive break, we have decided to make type-set
; keep track of the dependencies between literals of the goal clause
; and the type-sets computed.  The ttree argument to type-set below is
; a running accumulator that is returned as the second value of
; type-set.  Among the tags in the ttree are 'pt tags.  The value of
; the tag is a "parent tree" indicating the set of literals of the
; current-clause upon which the type deduction depends.  See the Essay
; on Parent Trees.  The type-alist in general contains entries of the
; form (term ts . ttree), where ttree is the tag tree encoding all of
; the 'PTs upon which depend the assertion that term has type-set ts.

(defun mv-atf (not-flg mbt mbf tta fta)

; Every exit of assume-true-false is via this function.  See assume-
; true-false for details.

  (if not-flg
      (mv mbf mbt fta tta)
      (mv mbt mbf tta fta)))

(defun non-cons-cdr (term)
  (cond ((variablep term) term)
        ((fquotep term) term)
        ((eq (ffn-symb term) 'cons)
         (non-cons-cdr (fargn term 2)))
        (t term)))

(defun extend-type-alist (term ts type-alist)

; This function extends type-alist, essentially by adding the entry
; (term . ts).  However, this function preserves two important
; invariants on type-alists: no term is bound to *ts-unknown* and no
; constant is ever bound.

  (cond
   ((ts= ts *ts-unknown*) type-alist)
   ((variablep term)
    (cons (cons term ts) type-alist))
   ((fquotep term) type-alist)
   (t (cons (cons term ts) type-alist))))

(defun zip-variable-type-alist (var-lst ts-lst)
  (cond ((endp var-lst) nil)
        (t (extend-type-alist (car var-lst) (car ts-lst)
                              (zip-variable-type-alist (cdr var-lst)
                                                       (cdr ts-lst))))))

(defun assoc-equal-equality (lhs rhs type-alist)
  (cond ((endp type-alist) nil)
        ((and (nvariablep (caar type-alist))
              (not (fquotep (caar type-alist)))
              (eq (ffn-symb (caar type-alist)) 'EQUAL)
              (or (and (equal lhs (fargn (caar type-alist) 1))
                       (equal rhs (fargn (caar type-alist) 2)))
                  (and (equal rhs (fargn (caar type-alist) 1))
                       (equal lhs (fargn (caar type-alist) 2)))))
         (car type-alist))
        (t (assoc-equal-equality lhs rhs (cdr type-alist)))))

(defun look-in-type-alist (term type-alist)

; Look in the type-alist for term (and its commutation if term is an EQUAL)
; and return the type-set to which it is bound or nil.

  (cond ((variablep term) (cdr (assoc-equal term type-alist)))
        ((quotep term) nil)
        ((eq (ffn-symb term) 'EQUAL)
         (cdr (assoc-equal-equality (fargn term 1)
                                    (fargn term 2)
                                    type-alist)))
        (t (cdr (assoc-equal term type-alist)))))

(defun push-ancestor (lit ancestors)

; This function is used to push a new entry onto ancestors.  Lit is a
; term to be assumed true.

; Note:  It is important that the literal, lit, be in the car of the
; frame constructed below.

  (let* ((alit lit)
         (alit-atm (mv-let (not-flg atm)
                           (strip-not alit)
                           (declare (ignore not-flg))
                           atm)))
    (mv-let (fn-cnt-alit-atm p-fn-cnt-alit-atm)
            (fn-count alit-atm)
            (cons (list alit              ; the literal being assumed true
                                          ; (negation of hyp!)
                        alit-atm          ; the atom of that literal
                        fn-cnt-alit-atm   ; the fn-count of that atom
                        p-fn-cnt-alit-atm ; the pseudo-fn-count of that atom
                        )
                  ancestors))))

(defun ancestors-check1 (lit-atm lit fn-cnt p-fn-cnt ancestors)

; Roughly speaking, ancestors is a list of all the things we can
; assume by virtue of our trying to prove their negations.  That is,
; when we backchain from B to A by applying (implies A B), we try to
; prove A and so we put (NOT A) on ancestors and can legitimately
; assume it (i.e., (NOT A)) true.  Roughly speaking, if lit is a
; member-equal of ancestors, we return (mv t t) and if the complement
; of lit is a member-equal we return (mv t nil).  If neither case
; obtains, we return (mv nil nil).

; We implement the complement check as follows.  lit-atm is the atom
; of the literal lit.  Consider a literal of ancestors, alit, and its
; atom, alit-atm.  If lit-atm is alit-atm and lit is not equal to alit,
; then lit and alit are complementary.  The following table supports
; this observation.  It shows all the combinations by considering that
; lit is either a positive or negative p, and alit is a p of either
; sign or some other literal of either sign.  The entries labeled =
; mark those when lit is alit.  The entries labeled comp mark those
; when lit and alit are complementary.

; lit \  alit:    p (not p) q (not q)

; p               =   comp  x    x
; (not p)         comp =    x    x

  (cond
   ((endp ancestors)
    (mv nil nil))
   (t
    (let ((alit              (car (car ancestors)))
          (alit-atm          (cadr (car ancestors)))
          (fn-cnt-alit-atm   (caddr (car ancestors)))
          (p-fn-cnt-alit-atm (cadddr (car ancestors))))
      (cond
       ((equal alit lit)
        (mv t t))
       ((equal lit-atm alit-atm) (mv t nil))
       ((and (or (> fn-cnt fn-cnt-alit-atm)
                 (and (eql fn-cnt fn-cnt-alit-atm)
                      (>= p-fn-cnt p-fn-cnt-alit-atm)))
             (worse-than-or-equal lit-atm alit-atm))
        (mv t nil))
       (t (ancestors-check1 lit-atm lit fn-cnt p-fn-cnt
                            (cdr ancestors))))))))

(defun ancestors-check (lit ancestors)

; We return two values.  The first is whether we should abort trying
; to establish lit.  The second is whether lit is (assumed) true in
; ancestors.

; We abort iff either lit is assumed true or else it is worse than or
; equal to some other literal we're trying to establish.  (Actually,
; we compare the atoms of the two literals in the worse-than check.)

  (mv-let (not-flg lit-atm)
          (strip-not lit)
          (declare (ignore not-flg))
          (mv-let (fn-cnt p-fn-cnt)
                  (fn-count lit-atm)
                  (ancestors-check1 lit-atm lit fn-cnt p-fn-cnt
                                    ancestors))))

(defun search-type-alist (term typ type-alist unify-subst)

; We search type-alist for an instance of term bound to a type-set
; that is a subset of typ.

; For example, if typ is *ts-rational* then we seek an instance of
; term that is known to be a subset of the rationals.  Most commonly,
; typ is *ts-non-nil*.  In that case, we seek an instance of term
; that is non-nil.  Thus, this function can be thought of as trying to
; "make term true."  To use this function to "make term false," use
; the ts-complement of the desired type.  I.e., if you wish to find a
; false instance of term use *ts-nil*.

; By "instance" here we always mean an instance under an extension of
; unify-subst.  The extension is returned when we are successful.

; We return two values.  The first indicates whether we succeeded.
; The second is the final unify-subst.  If we did not succeed, the
; second value is our input unify-subst.  I.e., we are a No-Change
; Loser.

; The No-Change Policy:  Many multi-valued functions here return a
; flag that indicates whether they "won" or "lost" and, in the case
; that they won, return "new values" for certain of their arguments.
; Here for example, we return a new value for unify-subst.  In early
; coding we adopted the policy that when they "lost" the additional
; values were irrelevant and were often nil.  This policy prevented
; the use of such forms as:
; (mv-let (wonp unify-subst)
;         (search-type-alist ... unify-subst ...)
;         (cond (wonp ...)
;               (t otherwise...)))
; because at otherwise... unify-subst was no longer what it had been before
; the search-type-alist.  Instead we had to think of a new name for
; it in the mv-let and use the appropriate one below.

; We then adopted what we now call the "No-Change Policy".  If a
; function returns a won/lost flag and some altered arguments, the
; No-Change Policy is that it returns its input arguments in case it
; loses.  We will note explicitly when a function is a No-Change
; Loser.

  (cond ((endp type-alist)
         (mv nil unify-subst))
        ((ts-subsetp (cdr (car type-alist)) typ)
         (mv-let (ans unify-subst)
           (one-way-unify1 term (car (car type-alist)) unify-subst)
           (cond (ans (mv t unify-subst))
                 (t (search-type-alist term
                                       typ
                                       (cdr type-alist)
                                       unify-subst)))))
        (t (search-type-alist term
                              typ
                              (cdr type-alist)
                              unify-subst))))

(defun term-and-typ-to-lookup (hyp wrld ens)
  (mv-let
    (not-flg term)
    (strip-not hyp)
    (let ((recog-tuple (and (nvariablep term)
                            (not (fquotep term))
                            (not (flambda-applicationp term))
			    (<reduce-id>
			     (most-recent-enabled-recog-tuple
			      (ffn-symb term)
			      (global-val 'recognizer-alist wrld)
			      ens)))))
      (cond ((and recog-tuple
                  (access recognizer-tuple recog-tuple :strongp))
             (mv (fargn term 1)
                 (if not-flg
                     (access recognizer-tuple recog-tuple :false-ts)
                   (access recognizer-tuple recog-tuple :true-ts))))
            (t (mv term
                   (if not-flg *ts-nil* *ts-non-nil*)))))))

(defun lookup-hyp (hyp type-alist wrld unify-subst ens)

; See if hyp is true by type-alist considerations -- possibly
; extending the unify-subst.  If successful we return t and a new
; unify-subst.  No-Change Loser.

  (mv-let (term typ)
          (term-and-typ-to-lookup hyp wrld ens)
          (search-type-alist term typ type-alist unify-subst)))

(mutual-recursion

(defun sublis-var-and-mark-free (alist form)

; This function is rather odd: it is equivalent to (sublis-var alist'
; form) where alist' is derived from alist by adding a pair (var .
; ???-var) for each variable var in form that is not assigned a value
; by alist.  Thus it creates an instance of form.  However, the free
; vars of form are assigned essentially arbitrary variable values and
; while no two free vars are identified by this process, there is no
; guarantee that the variables introduced in their stead are "new."
; For example, ???-var may come into the instantiated term via alist.
; The only reason for this function is to highlight the free vars in a
; term upon which we will split, in the half-hearted hope that the
; user will spot it.

  (cond ((variablep form)
         (let ((a (assoc-eq form alist)))
           (cond (a (cdr a))
                 (t (packn (list "???-" form))))))
        ((fquotep form)
         form)
        (t (cons-term (ffn-symb form)
                      (sublis-var-and-mark-free-lst alist (fargs form))))))

(defun sublis-var-and-mark-free-lst (alist l)
  (if (endp l)
      nil
    (cons (sublis-var-and-mark-free alist (car l))
          (sublis-var-and-mark-free-lst alist (cdr l)))))

)

(defun assume-true-false-<
  (not-flg arg1 arg2 ts1 ts2 type-alist)

; This function returns an extended type-alist by assuming (< ts1 ts2) true if
; not-flg is nil, but assuming (< ts1 ts2) false if not-flg is not nil.  It
; assumes that type-set (and hence type-set-<) was not able to decide the truth
; or falsity of (< ts1 ts2).  We could put this code in-line in
; assume-true-false, but the `true-type-alist' and `false-type-alist' are dealt
; with symmetrically, so it's convenient to share code via this function.

; Here are the cases we handle.  In this sketch we are glib about the
; possibility that arg1 or arg2 is nonnumeric or complex, but our code handles
; the more general situation.

; When we assume (< arg1 arg2) true,
; * if arg1 is positive then arg2 is positive
; * if arg1 is in the nonnegatives then arg2 is strictly positive
; * if arg2 is in the nonpositives then arg1 is strictly negative
; When we say "arg1 is in the nonnegatives" we mean to include the
; case where arg1 is strictly positive.  Note also that if arg1 may be
; negative, then arg2 could be anything (given that we've made the
; normalization for integers above).  Thus, the above two cases are as
; strong as we can be.

; When we assume (< arg1 arg2) false we find it easier to think about
; assuming (<= arg2 arg1) true:
; * if arg1 is negative, then arg2 is negative
; * if arg1 is nonpositive, then arg2 is nonpositive
; * if arg2 is nonnegative, then arg1 is nonnegative
; Note that if arg1 may be positive then arg2 could be anything, so
; there are no other cases we can express.

  (cond
   ((and
     (not not-flg)
     (ts-subsetp ts1
                 (ts-union *ts-non-negative-rational*
                           (ts-complement *ts-acl2-number*)))
     (ts-intersectp
      ts2
      (ts-complement (ts-union *ts-positive-rational* *ts-complex-rational*))))

; The test says: We are dealing with (< arg1 arg2) where arg1 is non-negative
; or a non-number.  We are thus allowed to deduce that arg2 is strictly
; positive or complex.  That is, we may delete the non-positive reals
; and non-numbers from its existing type-set.  If that doesn't change
; anything, we don't want to do it, so we have the third conjunct above that
; says arg2 contains some non-positive reals or some non-numbers.

; A worry is that the intersection below is empty.  Can that happen?  If it
; did, then we would have that arg1 is a non-negative real or a non-number,
; and arg2 is a non-positive real or a non-number.  Supposedly type-set-<
; would have then reported that (< arg1 arg2) must be false and mbf would be t.
; So the empty intersection cannot arise.

    (extend-type-alist
     arg2
     (ts-intersection ts2
                      (ts-union *ts-positive-rational* *ts-complex-rational*))
     type-alist))

; The remaining cases are analogous to that above.

   ((and (not not-flg)
         (ts-subsetp ts2
                     (ts-union *ts-non-positive-rational*
                               (ts-complement *ts-acl2-number*)))
         (ts-intersectp
          ts1
          (ts-complement
           (ts-union *ts-negative-rational* *ts-complex-rational*))))
    (extend-type-alist
     arg1
     (ts-intersection ts1
                      (ts-union *ts-negative-rational*
                                *ts-complex-rational*))
     type-alist))
   ((and not-flg
         (ts-subsetp ts1
                     *ts-negative-rational*)
         (ts-intersectp ts2
                        (ts-complement (ts-union *ts-complex-rational*
                                                 *ts-negative-rational*))))
; We are dealing with (not (< arg1 arg2)) which is (<= arg2 arg1) and we here
; know that arg1 is negative.  Thus, arg2 must be negative or complex.  See the
; case below for more details.

    (extend-type-alist
     arg2
     (ts-intersection ts2
                      (ts-union *ts-complex-rational*
                                *ts-negative-rational*))
     type-alist))
   ((and not-flg
         (ts-subsetp ts1
                     (ts-union *ts-non-positive-rational*
                               (ts-complement *ts-acl2-number*)))
         (ts-intersectp ts2
                        *ts-positive-rational*))

; Here we are dealing with (not (< arg1 arg2)) which is (<= arg2 arg1).  We
; know arg1 is <= 0.  We will thus deduce that arg2 is <= 0, and hence not a
; positive real, if we don't already know it.  But the worry again arises
; that the intersection of arg2's known type and the complement of the
; positive-reals is empty.  Suppose it were.  Then arg2 is a strictly
; positive real.  But if arg1 is a non-positive real or a non-number
; and arg2 is a positive real, then type-set-< knows that (< arg1 arg2) is
; true.  Thus, this worry is again baseless.

    (extend-type-alist
     arg2
     (ts-intersection
      ts2
      (ts-complement *ts-positive-rational*))
     type-alist))
   ((and not-flg
         (ts-subsetp ts2
                     *ts-positive-rational*)
         (ts-intersectp ts1
                        (ts-complement
                         (ts-union *ts-complex-rational*
                                   *ts-positive-rational*))))
    (extend-type-alist
     arg1
     (ts-intersection
      ts1
      (ts-union *ts-complex-rational*
                *ts-positive-rational*))
     type-alist))
   ((and not-flg
         (ts-subsetp
          ts2
          (ts-complement
           (ts-union *ts-complex-rational*
                     *ts-negative-rational*)))
         (ts-intersectp ts1
                        *ts-negative-rational*))
    (extend-type-alist
     arg1
     (ts-intersection
      ts1
      (ts-complement *ts-negative-rational*))
     type-alist))
   (t type-alist)))

(defun mv-atf-2 (not-flg true-type-alist false-type-alist
                         new-term xnot-flg x)

; This function is a variation of mv-atf in which mbt and mbf
; are known to be nil.  The scenario is that there is
; an implicit term that we want to assume true or false, and we have
; generated two other terms x and new-term to assume true or false
; instead, each with its own parity (xnot-flg and not-flg,
; respectively).  We want to avoid putting redundant information on
; the type-alist, which would happen if we are not careful in the case
; that x and new-term are the same term modulo their respective
; parities.

; We assume that new-term is not a call of NOT.

  (let ((tta0 (extend-type-alist
               new-term
               *ts-t*
               true-type-alist))
        (fta0 (extend-type-alist
               new-term
               *ts-nil*
               false-type-alist))
        (same-parity (eq not-flg xnot-flg)))
    (cond
     ((equal new-term ; new-term is not a call of NOT, so we negate x
             (cond (same-parity x)
                   (t (dumb-negate-lit x))))
      (mv-atf not-flg nil nil tta0 fta0))
     (t
      (let ((tta1 (extend-type-alist
                   x
                   (if same-parity *ts-t* *ts-nil*)
                   tta0))
            (fta1 (extend-type-alist
                   x
                   (if same-parity *ts-nil* *ts-t*)
                   fta0)))
        (mv-atf not-flg nil nil tta1 fta1))))))

; I spent a while trying to come up with a measure of terms under
; which lambda expansion reduced the size.  The measure was intended
; to handle the cases in the type-set clique in which the recursion
; was on subcor-var expressions.  However, some of those calls are
; used to expand *expandable-boot-strap-non-rec-fns*.  But for those
; recursions to terminate we must know that every member of that list
; has a non-recursive body in w.  That is a fairly complicated
; invariant of w and I don't want to involve it in the termination
; argument.

; A second termination problem has to do with type-prescription
; lemmas.  Should there be a lemma like (implies (integerp (fn x))
; (integerp (fn x))) in w then we would loop forever -- or at least
; termination would depend on properties of ancestors-check that I
; don't want to analyze.

; The bottom line: I think it is best to add a numeric argument, nnn, to
; handle these two recursions.

(include-book "std/basic/two-nats-measure" :dir :system)

(defun lex4 (i j k l)
  (acl2::nat-list-measure (list i j k l)))

(defthm type-set-admission-lemma1
  (<= (acl2-count (car x))
      (acl2-count x))
  :rule-classes :linear)

(defthm type-set-admission-lemma2
  (<= (acl2-count (cdr x))
      (acl2-count x))
  :rule-classes :linear)

(defthm type-set-admission-lemma3
  (implies (consp x)
           (< (acl2-count (car x))
              (acl2-count x)))
  :rule-classes :linear)

(defthm type-set-admission-lemma4
  (implies (consp x)
           (< (acl2-count (cdr x))
              (acl2-count x)))
  :rule-classes :linear)

(defthm type-set-admission-lemma5
  (implies (car x)
           (< (acl2-count (cdr x)) (acl2-count x)))
  :rule-classes :linear)

(defthm acl2-count-non-cons-cdr
  (<= (acl2-count (non-cons-cdr x)) (acl2-count x))
  :hints (("Goal" :induct (non-cons-cdr x)))
  :rule-classes :linear)

(mutual-recursion

(defun type-set (x type-alist ancestors ens w nnn)

  (declare (xargs :measure (lex4 nnn (acl2-count x) 5 0)
                  :hints
                  (("Goal" :in-theory (disable getprop
                                               ancestors-check push-ancestor
                                               sublis-var free-varsp
                                               enabled-numep
                                               look-in-type-alist lookup-hyp
                                               one-way-unify acl2-count
                                               member-eq acl2::member-equal)))))

; X is a term and type-alist is a type alist mapping terms to their
; type-sets and thus encoding the current assumptions.  We return the
; type-set of term under those assumptions.

; Note:  If ancestors is t it means:  don't backchain.  Act as though
; the literal we're backchaining on is worse than everything in sight.
; This is called the ``t-ancestors hack'' and is commented upon below.

  (let ((ts0 (look-in-type-alist x type-alist)))
    (cond
     (ts0 ts0)
     ((variablep x) *ts-unknown*)
     ((fquotep x) (type-set-quote (cadr x)))
     (t
      (let ((fn (ffn-symb x)))
        (cond
         ((or (flambdap fn)
              (member-eq fn *expandable-boot-strap-non-rec-fns*))

; PSIM test: If the lambda expression uses its formals repeatedly in
; the body, this treatment could hammer memoization.

          (if (zp nnn)
              *ts-unknown*
            (type-set (subcor-var (formals fn w)
                                  (fargs x)
                                  (body fn t w))
                      type-alist ancestors ens w (- nnn 1))))
         ((eq fn 'not)
          (type-set-not
           (type-set (fargn x 1) type-alist ancestors ens w nnn)))
         (t
          (let* ((recog-tuple
                  (most-recent-enabled-recog-tuple
                   fn
                   (global-val 'recognizer-alist w)
                   ens)))
            (cond
             (recog-tuple
              (<type-set-id>
               (let ((ts (type-set-recognizer
                          recog-tuple
                          (type-set (fargn x 1)
                                    type-alist ancestors ens w nnn))))
                 (cond
                  ((or (ts= ts *ts-t*)
                       (ts= ts *ts-nil*))
                   ts)
                  (t
                   (ts-intersection
                    ts
                    (type-set-with-rules
                     (getprop fn 'type-prescriptions nil w)
                     x type-alist ancestors ens w *ts-unknown*
                     nnn)))))))
             ((eq fn 'if)
              (mv-let
               (must-be-true must-be-false true-type-alist false-type-alist)
               (assume-true-false (fargn x 1) type-alist ancestors ens w nnn)
               (cond (must-be-true
                      (type-set (fargn x 2)
                                true-type-alist ancestors ens w nnn))
                     (must-be-false
                      (type-set (fargn x 3)
                                false-type-alist ancestors ens w nnn))
                     (t (ts-union (type-set (fargn x 2)
                                            true-type-alist
                                            ancestors ens
                                            w nnn)
                                  (type-set (fargn x 3)
                                            false-type-alist
                                            ancestors ens
                                            w nnn))))))
             (t
              (type-set-with-rules
               (getprop fn 'type-prescriptions nil w)
               x type-alist ancestors ens w *ts-unknown* nnn)))))))))))

(defun type-set-relieve-hyps (hyps alist type-alist ancestors ens w nnn)

  (declare (xargs :measure (lex4 nnn 0 0 (acl2-count hyps))))

; Hyps is a list of terms, implicitly conjoined.  Alist is a
; substitution mapping variables in hyps to terms governed by
; type-alist.  Consider the result, hyps', of substituting alist into
; each hyp in hyps.  We wish to know whether, by type-set reasoning
; alone, we can get that hyps' are all true in the context of
; type-alist.  We do the substitution one hyp at a time, so we don't
; pay the price of consing up instances beyond the first hyp that
; fails.  We extend alist as necessary to choose free variables in
; hyps.  But we do not return the final extension of the alist because
; type-prescription lemmas never contain free variables in the
; conclusion.  While we are at it, we record in an extension of
; type-alist the type computed for each hyp', so that if subsequent
; rules need that information, they can get it quickly.  We return
; (mv wonp type-alist').

  (cond
   ((endp hyps) (mv t type-alist))
   (t
    (let* ((hyp (car hyps)))
      (mv-let
       (lookup-hyp-ans alist)
       (lookup-hyp hyp type-alist w alist ens)
       (cond
        (lookup-hyp-ans
         (type-set-relieve-hyps (cdr hyps) alist type-alist ancestors
                                ens w nnn))
        ((free-varsp hyp alist)
         (cond ((and (equalityp hyp)
                     (variablep (fargn hyp 1))
                     (not (assoc-eq (fargn hyp 1) alist))
                     (not (free-varsp (fargn hyp 2) alist)))
                (type-set-relieve-hyps
                 (cdr hyps)
                 (cons (cons (fargn hyp 1) (fargn hyp 2)) alist)
                 type-alist ancestors ens w nnn))
               (t (mv nil type-alist))))
        (t
         (mv-let
          (not-flg atm)
          (strip-not hyp)
          (let ((atm1 (sublis-var alist atm)))
            (mv-let
             (on-ancestorsp assumed-true)

; Note: Here is one of two places where we implement the t-ancestors
; hack.

             (if (eq ancestors t)
                 (mv t nil)
               (ancestors-check (if not-flg
                                    (fcons-term* 'not atm1)
                                  atm1)
                                ancestors))
             (cond
              (on-ancestorsp
               (cond
                (assumed-true
                 (type-set-relieve-hyps (cdr hyps)
                                        alist type-alist ancestors ens w nnn))
                (t (mv nil type-alist))))
              ((zp nnn) (mv nil type-alist))
              (t
               (let* ((ts1 (type-set atm1 type-alist

; Here is the other place we enforce the t-ancestors hack.

                                     (if (eq ancestors t)
                                         t
                                       (push-ancestor
                                        (if not-flg
                                            atm1
                                          (fcons-term* 'not atm1))
                                        ancestors)) ens
                                     w (- nnn 1)))
                      (type-alist
                       (cond ((assoc-equal atm1 type-alist)
                              type-alist)
                             (t (extend-type-alist atm1 ts1 type-alist))))
                      (ts (if not-flg
                              (cond ((ts= ts1 *ts-nil*) *ts-t*)
                                    ((ts-intersectp ts1 *ts-nil*)
                                     *ts-boolean*)
                                    (t *ts-nil*))
                            ts1)))
                 (cond
                  ((ts= ts *ts-nil*) (mv nil type-alist))
                  ((ts-intersectp *ts-nil* ts)
                   (mv nil type-alist))
                  (t
                   (type-set-relieve-hyps (cdr hyps)
                                          alist type-alist
                                          ancestors ens w nnn))))))))))))))))

(defun extend-type-alist-with-bindings (alist type-alist ancestors ens w nnn)

  (declare (xargs :measure (lex4 nnn 0 0 (acl2-count alist))))

; Alist is an alist that pairs variables in some rule with terms.  We compute
; the type-set of each term in the range of alist and extend type-alist with
; new entries that pair each term to its type-set.

  (cond ((endp alist) type-alist)
        ((zp nnn) type-alist)
        (t (extend-type-alist-with-bindings
            (cdr alist)
            (cond ((assoc-equal (cdr (car alist)) type-alist)
                   type-alist)
                  (t
                   (extend-type-alist
                    (cdr (car alist))
                    (type-set (cdr (car alist))
                              type-alist ancestors ens w
                              (- nnn 1))
                    type-alist)))
            ancestors ens w nnn))))

(defun type-set-with-rule (tp term type-alist ancestors ens w nnn)
  (declare (xargs :measure (lex4 nnn (acl2-count term) 3 0)))

; We apply the type-prescription, tp, to term, if possible, and return
; a type-set and an extended type-alist.  If the rule is inapplicable,
; the type-set is *ts-unknown* and the ``extended'' type-alist is the
; original type-alist.

; This is a No Change Loser with respect to type-alist.

  (if (or (zp nnn)
          (not (enabled-numep (access type-prescription tp :nume) ens)))
      (mv *ts-unknown* type-alist)
    (mv-let
     (unify-ans unify-subst)
     (one-way-unify (access type-prescription tp :term)
                    term)
     (cond
      (unify-ans
       (<type-set-with-rule-id>
        (let* ((hyps (access type-prescription tp :hyps))
               (type-alist1
                (cond
                 ((null hyps) type-alist)
                 (t (extend-type-alist-with-bindings unify-subst
                                                     type-alist
                                                     ancestors ens w
                                                     (- nnn 1))))))
          (mv-let
           (relieve-hyps-ans type-alist1)
           (type-set-relieve-hyps hyps
                                  unify-subst
                                  type-alist1
                                  ancestors
                                  ens
                                  w
                                  (- nnn 1))
           (cond
            (relieve-hyps-ans
             (mv (type-set-with-rule1 unify-subst
                                      (access type-prescription tp :vars)
                                      type-alist1 ancestors ens w
                                      (access type-prescription tp :basic-ts)
                                      (- nnn 1))
                 type-alist1))
            (t (mv *ts-unknown* type-alist)))))))
      (t (mv *ts-unknown* type-alist))))))

(defun type-set-with-rule1 (alist vars type-alist ancestors ens w basic-ts nnn)

  (declare (xargs :measure (lex4 nnn 0 0 (acl2-count alist))))

; Alist is an alist that maps variables to terms.  The terms are in
; the context described by type-alist.  Vars is a list of variables.
; We map over the pairs in alist unioning into basic-ts the type-sets
; of those terms whose corresponding vars are in vars.  We ultimately
; return the final basic-ts.

  (cond
   ((endp alist) basic-ts)
   ((zp nnn) basic-ts)
   (t (type-set-with-rule1 (cdr alist) vars type-alist ancestors ens w
                           (if (member-eq (caar alist) vars)
                               (ts-union
                                (type-set (cdar alist) type-alist
                                          ancestors ens w (- nnn 1))
                                basic-ts)
                             basic-ts) nnn))))

(defun type-set-with-rules (tp-lst term type-alist ancestors ens w ts nnn)

  (declare (xargs :measure (lex4 nnn
                                 (acl2-count term)
                                 4
                                 (acl2-count tp-lst))))

; We try to apply each type-prescription in tp-lst, intersecting
; together all the type sets we get.

  (cond
   ((endp tp-lst)
    (ts-intersection (type-set-primitive term type-alist ancestors ens w nnn)
                     ts))
   ((ts-subsetp ts (access type-prescription (car tp-lst) :basic-ts))

; Our goal is to make the final type-set, ts, as small as possible by
; intersecting it with the type-sets returned to the various rules.
; If ts is already smaller than the :basic-ts of a rule, there is no
; point in trying that rule.

    (type-set-with-rules (cdr tp-lst) term type-alist ancestors ens w ts nnn))
   (t
    (mv-let
     (ts1 type-alist)
     (type-set-with-rule (car tp-lst) term type-alist ancestors ens w nnn)
     (type-set-with-rules (cdr tp-lst)
                          term type-alist ancestors ens w
                          (ts-intersection ts1 ts) nnn)))))

(defun type-set-primitive (term type-alist ancestors ens w nnn)

  (declare (xargs :measure (lex4 nnn (acl2-count term) 3 0)))

; This function should handle
; every non-recognizer function handled in *primitive-formals-and-guards*,
; ev-fncall, and cons-term1, though like cons-term1, we also handle NOT.
; Exception:  Since code-char is so simple type-theoretically, we handle its
; type set computation with rule code-char-type in axioms.lisp.  It is
; perfectly acceptable to handle function symbols here that are not handled by
; the functions above.  For example, we compute a type-set for length in a
; special manner below, but cons-term1 and the others do not know about
; length.

  (case (ffn-symb term)
    (cons
     (type-set-cons
      (type-set (fargn term 2) type-alist ancestors ens w nnn)))
    (equal
     (cond
      ((equal (fargn term 1) (fargn term 2))
       *ts-t*)
      (t (type-set-equal
          (type-set (fargn term 1) type-alist ancestors ens w nnn)
          (type-set (fargn term 2) type-alist ancestors ens w nnn)))))
    (unary--
     (type-set-unary--
      (type-set (fargn term 1) type-alist ancestors ens w nnn)))
    (unary-/
     (type-set-unary-/
      (type-set (fargn term 1)
                type-alist
                ancestors ens
                w nnn)))
    (denominator
     (type-set-denominator
      (type-set (fargn term 1) type-alist ancestors ens w nnn)))
    (numerator
     (type-set-numerator
      (type-set (fargn term 1) type-alist ancestors ens w nnn)))
    (car
     (type-set-car
      (type-set (fargn term 1) type-alist ancestors ens w nnn)))
    (cdr
     (type-set-cdr
      (type-set (fargn term 1) type-alist ancestors ens w nnn)))
    (symbol-name
     *ts-string*)
    (symbol-package-name
     *ts-string*)
    (intern-in-package-of-symbol
     (type-set-intern-in-package-of-symbol
      (type-set (fargn term 1) type-alist ancestors ens w nnn)
      (type-set (fargn term 2) type-alist ancestors ens w nnn)))
    (coerce
     (type-set-coerce
      term
      (type-set (fargn term 1) type-alist ancestors ens w nnn)
      (type-set (fargn term 2) type-alist ancestors ens w nnn)))
    (length
     (type-set-length
      (type-set (fargn term 1) type-alist ancestors ens w nnn)))
    (binary-+
     (type-set-binary-+
      term
      (type-set (fargn term 1) type-alist ancestors ens w nnn)
      (type-set (fargn term 2) type-alist ancestors ens w nnn)))
    (binary-*
     (type-set-binary-*
      (type-set (fargn term 1) type-alist ancestors ens w nnn)
      (type-set (fargn term 2) type-alist ancestors ens w nnn)))
    (<
     (type-set-< (fargn term 1)
                 (fargn term 2)
                 (type-set (fargn term 1) type-alist ancestors ens w nnn)
                 (type-set (fargn term 2) type-alist ancestors ens w nnn)))
    (not
     (type-set-not
      (type-set (fargn term 1) type-alist ancestors ens w nnn)))
    (realpart
     (type-set-realpart
      (type-set (fargn term 1) type-alist ancestors ens w nnn)))
    (imagpart
     (type-set-imagpart
      (type-set (fargn term 1) type-alist ancestors ens w nnn)))
    (complex
     (type-set-complex
      (type-set (fargn term 1) type-alist ancestors ens w nnn)
      (type-set (fargn term 2) type-alist ancestors ens w nnn)))
    (char-code
     (type-set-char-code
      (type-set (fargn term 1) type-alist ancestors ens w nnn)))
    (otherwise *ts-unknown*)))

(defun assume-true-false (x type-alist ancestors ens w nnn)

  (declare (xargs :measure (lex4 nnn (acl2-count x) 12 0)))

; We assume x both true and false, extending type-alist as appropriate.

; We return four values.
; must-be-true     - t iff x is definitely true under type-alist and w.
; must-be-false    - t iff x is definitely false under type-alist and w.
; true-type-alist  - an extension of type-alist encoding the assumption
;                    that x is true; valid only if not must-be-false.
; false-type-alist - and extension of type-alist encoding the assumption
;                    that x is false; valid only if not must-be-true.

  (mv-let
   (xnot-flg x)
   (strip-not x)
   (cond
    ((variablep x)
     (assume-true-false1 xnot-flg x type-alist ancestors ens w nnn))
    ((fquotep x)
     (if (equal x *nil*)
         (mv-atf xnot-flg nil t nil type-alist)
         (mv-atf xnot-flg t nil type-alist nil)))
    ((flambda-applicationp x)
     (assume-true-false1 xnot-flg x type-alist ancestors ens w nnn))
    (t
     (let ((recog-tuple
            (most-recent-enabled-recog-tuple
             (ffn-symb x)
             (global-val 'recognizer-alist w)
             ens)))
       (cond
        (recog-tuple
         (<assume-true-false-id>
          (let* ((ts (type-set (fargn x 1) type-alist ancestors ens w nnn))
                 (t-int (ts-intersection ts (access recognizer-tuple
                                                    recog-tuple :true-ts)))
                 (f-int (ts-intersection ts (access recognizer-tuple
                                                    recog-tuple :false-ts))))
            (cond
             ((ts= t-int *ts-empty*)
              (mv-atf xnot-flg nil t nil type-alist))
             ((ts= f-int *ts-empty*)
              (mv-atf xnot-flg t nil type-alist nil))
             (t

; At this point we know that we can't determine whether (recog arg) is
; true or false.  We therefore will be returning two type-alists which
; restrict arg's type according to the two intersections computed
; above.

              (mv-atf xnot-flg nil nil
                      (extend-with-proper/improper-cons-ts-tuple
                       (fargn x 1) t-int type-alist ancestors
                       (if (access recognizer-tuple recog-tuple :strongp)
                           type-alist
                         (extend-type-alist x *ts-t* type-alist))
                       ens w nnn)
                      (extend-with-proper/improper-cons-ts-tuple
                       (fargn x 1) f-int type-alist ancestors
                       (if (access recognizer-tuple recog-tuple :strongp)
                           type-alist
                         (extend-type-alist x *ts-nil* type-alist))
                       ens w nnn)))))))
        ((member-eq (ffn-symb x) *expandable-boot-strap-non-rec-fns*)
         (if (zp nnn)
             (assume-true-false1 xnot-flg x type-alist ancestors ens w nnn)
           (mv-let
            (mbt mbf tta fta)
            (assume-true-false
             (subcor-var (formals (ffn-symb x) w)
                         (fargs x)
                         (body (ffn-symb x) t w))
             type-alist ancestors ens w (- nnn 1))
            (mv-atf xnot-flg mbt mbf tta fta))))
        ((eq (ffn-symb x) 'equal)
         (let ((arg1 (fargn x 1))
               (arg2 (fargn x 2)))
           (cond
            ((equal arg1 arg2)
             (mv-atf xnot-flg t nil type-alist nil))
            ((and (quotep arg1) (quotep arg2))
             (mv-atf xnot-flg nil t nil type-alist))
            (t
             (let ((ts (look-in-type-alist x type-alist)))
               (cond
                ((and ts (ts= ts *ts-t*))
                 (mv-atf xnot-flg t nil type-alist nil))
                ((and ts (ts= ts *ts-nil*))
                 (mv-atf xnot-flg nil t nil type-alist))
                (t (let* ((ts1 (type-set arg1 type-alist ancestors ens w nnn))
                          (ts2 (type-set arg2 type-alist ancestors ens w nnn))
                          (int (ts-intersection ts1 ts2)))
                     (cond
                      ((ts= int *ts-empty*)
                       (mv-atf xnot-flg nil t nil type-alist))
                      ((and (ts= ts1 ts2)
                            (member ts1 *singleton-type-sets*))
                       (mv-atf xnot-flg t nil type-alist nil))
                      (t
                       (let* ((true-type-alist1
                               (extend-type-alist x *ts-t* type-alist))
                              (true-type-alist2
                               (cond
                                ((ts= ts1 int) true-type-alist1)
                                (t (extend-with-proper/improper-cons-ts-tuple
                                    arg1 int type-alist ancestors
                                    true-type-alist1 ens w nnn))))
                              (true-type-alist3
                               (cond
                                ((ts= ts2 int) true-type-alist2)
                                (t (extend-with-proper/improper-cons-ts-tuple
                                    arg2 int type-alist ancestors
                                    true-type-alist2 ens w nnn))))
                              (false-type-alist1
                               (extend-type-alist x *ts-nil* type-alist))
                              (false-type-alist2
                               (cond
                                ((member ts2 *singleton-type-sets*)
                                 (extend-with-proper/improper-cons-ts-tuple
                                  arg1
                                  (ts-intersection ts1 (ts-complement ts2))
                                  type-alist ancestors
                                  false-type-alist1 ens w nnn))
                                (t false-type-alist1)))
                              (false-type-alist3
                               (cond
                                ((member ts1 *singleton-type-sets*)
                                 (extend-with-proper/improper-cons-ts-tuple
                                  arg2
                                  (ts-intersection ts2 (ts-complement ts1))
                                  type-alist ancestors
                                  false-type-alist2 ens w nnn))
                                (t false-type-alist2))))
                         (mv-atf xnot-flg nil nil
                                 true-type-alist3 false-type-alist3))))))))))))
        ((eq (ffn-symb x) '<)
         (let ((ts0 (type-set x type-alist ancestors ens w nnn)))
           (cond
            ((ts= ts0 *ts-nil*)
             (mv-atf xnot-flg nil t nil type-alist))
            ((not (ts-intersectp ts0 *ts-nil*))
             (mv-atf xnot-flg t nil type-alist nil))
            (t
             (let* ((ts1 (type-set (fargn x 1) type-alist ancestors ens w nnn))
                    (ts2 (type-set (fargn x 2) type-alist ancestors ens w nnn)))

; In the mv-let below we effectively implement the facts that, when x
; is of type *ts-integer* (< x 1) is ~(< 0 x), and (< -1 x) is ~(< x
; 0).  By normalizing such inequalities around 0 we can more easily
; recognize the ones covered by our builtin types.

; WARNING: A bug once lurked here, so beware.  The term we are
; assuming is represented by xnot-flg and x.  We are about to
; re-represent it in terms of not-flg, arg1 and arg2.  Do not
; accidentally use not-flg with x or xnot-flg with (< arg1 arg2)!  In
; the old code, we had only one name for these two flgs.

               (mv-let
                (not-flg arg1 arg2 ts1 ts2)
                (cond
                 ((and (equal (fargn x 2) *1*)
                       (ts-subsetp ts1
                                   (ts-union (ts-complement
                                              *ts-acl2-number*)
                                             *ts-integer*)))
                  (mv (not xnot-flg) *0* (fargn x 1) *ts-zero* ts1))
                 ((and (equal (fargn x 1) *-1*)
                       (ts-subsetp ts2
                                   (ts-union (ts-complement
                                              *ts-acl2-number*)
                                             *ts-integer*)))
                  (mv (not xnot-flg) (fargn x 2) *0* ts2 *ts-zero*))
                 (t (mv xnot-flg (fargn x 1) (fargn x 2) ts1 ts2)))

; Foreshadow 1:  Note that if neither of the newly bound arg1 nor arg2
; is *0* then not-flg is xnot-flg and arg1 and arg2 are the corresponding
; arguments of x.  That is because on the first two of the three branches
; of the cond above, one of the two args is set to *0*.  We use this curious
; fact below.

; In the mv-let below we effectively implement the fact that, when x is of type
; *ts-integer* (< 0 (+ 1 x)) is ~(< x 0).  The symmetric equivalence of (< (+
; -1 x) 0) to ~(< 0 x) is also handled.

; We will assume that the binary-+ has been commuted so that the constant arg,
; if any, is the first.

; Note that the output of this transformation is not subject to the first
; transformation, above, so we do not have to consider repeating that
; transformation.  However, it is conceivable that the output of this
; transformation is subject to its symmetric counterpart.  In particular, if we
; composed this transformation with itself we might reduce (< 0 (+ 1 (+ -1 x)))
; to (< 0 x).  We prefer instead to take the position that some arithmetic
; simplifier will reduce the +-expressions.

                (mv-let
                 (not-flg arg1 arg2 ts1 ts2)
                 (cond ((and (equal arg1 *0*)
                             (ts-subsetp ts2

; It is sound to use (ts-intersection ts2 *ts-acl2-number*) in place of ts2
; above, but since below we see that arg2 is a call of binary-+, we know that
; ts2 is already contained in *ts-acl2-number*.

                                         *ts-integer*)
                             (nvariablep arg2)
                             (not (fquotep arg2))
                             (eq (ffn-symb arg2) 'binary-+)
                             (equal (fargn arg2 1) *1*))

; So the term is of the form (< 0 (+ 1 x)) and we know x is some integer (or a
; non-number).  We transform it to ~(< x 0).  But we must determine the
; type-set of x.  It cannot be done merely by inverting the type-set of (+ 1
; x): the latter might be *ts-integer* and x could either be
; *ts-non-positive-integer* or *ts-integer*, or even a non-number.  Some cases
; we could invert:  if (+ 1 x) is non-positive, then we know x must be strictly
; negative.  But rather than invert, we just call type-set on x.

                        (mv (not not-flg)
                            (fargn arg2 2)
                            *0*
                            (type-set (fargn arg2 2)
                                      type-alist
                                      ancestors ens w nnn)
                            *ts-zero*))
                       ((and (equal arg2 *0*)
                             (ts-subsetp ts1 *ts-integer*)
                             (nvariablep arg1)
                             (not (fquotep arg1))
                             (eq (ffn-symb arg1) 'binary-+)
                             (equal (fargn arg1 1) *-1*))
                        (mv (not not-flg)
                            *0*
                            (fargn arg1 2)
                            *ts-zero*
                            (type-set (fargn arg1 2)
                                      type-alist
                                      ancestors ens w nnn)))
                       (t (mv not-flg arg1 arg2 ts1 ts2)))

; Foreshadow 2:  Observe that if, at this point, neither the newly bound arg1
; nor the newly bound arg2 is *0*, then the newly bound not-flg, arg1 and arg2
; are all equal to their old values (outside this mv-let).  That is because the
; cond above, which determines the new values of not-flg, arg1 and arg2 here,
; has the property that on the two branches that change the not-flg, one of
; the two args is set to *0*.  If neither arg is *0* then we could have only
; come out on the last clause of the cond above and not-flg etc are thus
; unchanged.  We use this curious property below.

; The transformations just carried out have the possibly odd effect of
; assuming (< 0 x) false when asked to assume (< x 1) true, for integral x.
; This effectively sets x's type-set to the non-positives.  One might
; then ask, what happens if we later decide to get the type-set of (<
; x 1).  We would hope that, having assumed it, we would realize it
; was true!  Indeed we do, but only because type-set-< makes the same
; normalization of (< x 1).  This raises the question: Could we reduce
; the size of our code by doing the normalization in only one place,
; either here in assume-true-false or there in type-set-<?  The real
; reason we do it in both places has nothing to do with the subtleties
; discussed here; there is no guarantee that both of these functions
; will be called.  If (< x 1) appears in a test, we will call
; assume-true-false on it and we have to normalize it to produce the
; desired tta and fta.  If (< x 1) appears as the entire resultant
; term, we'll just call type-set on it and we have to normalize it to
; decide it.

; Another question raised is: "What about the second transformation done
; above?"  We assume ~(< x 0) when asked to assume (< 0 (+ 1 x)), with the
; effect that x is given (roughly) the type-set non-negative integer.  Note
; that type-set-< does not make this second transformation.  Will we recognize
; (< 0 (+ 1 x)) as true later?  Yes.  If x is non-negative, then type-set
; determines that (+ 1 x) is positive and hence (< 0 (+ 1 x)) is true.

                 (cond
                  ((equal arg1 *0*)
                   (cond
                    ((ts-subsetp ts2 *ts-positive-rational*)
                     (mv-atf not-flg t nil type-alist nil))
                    ((ts-subsetp ts2
                                 (ts-union (ts-complement *ts-acl2-number*)
                                           *ts-non-positive-rational*))
                     (mv-atf not-flg nil t nil type-alist))
                    (t
                     (let* ((true-type-alist
                             (extend-type-alist
                              arg2
                              (ts-intersection
                               ts2
                               (ts-union *ts-positive-rational*
                                         *ts-complex-rational*))
                              type-alist))
                            (false-type-alist
                             (extend-type-alist
                              arg2
                              (ts-intersection
                               ts2 (ts-complement *ts-positive-rational*))
                              type-alist)))

; We formerly put the inequality explicitly on the type-alist only in
; the case that (ts-intersectp ts2 *ts-complex-rational*).  We leave
; in place the comment regarding that case, below.  However, we now
; put the inequality on the type-alist in all cases, in order to
; assist in relieving hypotheses involving free variables.  Robert
; Krug sent the following relevant example.

                       #|
 (defstub foo (x) t)

 (defaxiom test1
   (implies (and (<= 0 x)
                 (rationalp x))
            (foo y)))

 (thm
  (implies (and (rationalp x)
                (<= 0 x))
           (foo y)))
|#

; The thm fails, because the hypothesis of test1 is not relieved.  The
; following trace excerpt shows why.

                       #|
  1> (SEARCH-TYPE-ALIST (< X '0)    ; Attempt to find that (< X 0)
                        64          ; is false.  Note:
                                    ; (decode-type-set 64) = '*TS-NIL*
                        ((X 7))     ; Type-alist:  X is a non-negative
                                    ; rational.  Note:
                                    ; (decode-type-set 7) =
                                    ; *TS-NON-NEGATIVE-RATIONAL*
                        ((Y . Y))   ; unify-subst
                        NIL)>       ; ttree
  <1 (SEARCH-TYPE-ALIST NIL ((Y . Y)) NIL)>   ; failed to relieve hyp
|#

; As seen below, assume-true-false had failed to put the inequality
; explicitly on the type-alist.

                       #|
  1> (ASSUME-TRUE-FALSE (< X '0) ; condition assumed true or false
                        NIL      ; a tag tree
                        NIL      ; force-flg
                        NIL      ; never mind this one...
                        ((X 31)) ; type-alist: X is rational
                        NIL      ; ancestors
                        |some-enabled-structure|
                        |current-acl2-world|)>
  <1 (ASSUME-TRUE-FALSE NIL             ; must-be-true
                        NIL             ; must-be-false
                        ((X 24) (X 31)) ; true-type-alist:
                                        ; X is negative rational
                        ((X 7) (X 31))  ; false-type-alist:
                                        ; X is non-negative rational
                        NIL)>           ; tag tree
|#

; But wait, there's more!  Robert subsequently sent an example showing
; that it is not enough to put the current inequality with 0, e.g.,
; (fcons-term* '< *0* arg2), on the type-alist.  The original equality
; may need to be there as well.  Here is his example, which ACL2 can
; now prove (see mv-atf-2).

                       #|
 (defstub foo (x) t)

 (defaxiom test
   (implies (and (<= 1 x)
                 (integerp x))
            (foo y)))

 (thm
   (implies (and (integerp x)
                 (<= 1 x))
            (foo y)))
|#

; Start old comment regarding the case that (ts-intersectp ts2
; *ts-complex-rational*).

; Long comment on why we extend the true-type-alist to accommodate complex
; numbers.

; For an example that illustrates why we need to put (fcons-term* '< *0* arg2)
; on the true-type-alist explicitly in this case, try the following.

                       #|
 (encapsulate
  (((foo *) => *))
  (local (defun foo (x) (<= 0 x)))
  (defthm foo-type (implies (<= 0 x) (equal (foo x) t))
    :rule-classes :type-prescription))

 (thm (implies (<= 0 x) (equal (foo x) t)))
|#

; If we simply use true-type-alist here, we'll lose the information that (< 0
; arg2).  That is, we desire that the true-type-alist is sufficient for
; deducing what we are assuming true; but if arg2 can be a complex number, we
; will not be able to make that determination.  So, we put this inequality on
; the type-alist, explicitly.  We do so in the order shown for two reasons,
; probably neither of them particularly important (but at least, we document
; what they are).  For one, we want type-set to find the explicit inequality
; first, in case it ever tries to decide it.  Although we do not expect
; type-set to have any trouble even if we bury the inequality after an entry
; for arg2, this coding seems more robust.  More importantly, however, we want
; to call extend-type-alist, which is a bit complicated, on as short a
; type-alist as possible.

; End old comment regarding the case that (ts-intersectp ts2
; *ts-complex-rational*).

                       (mv-atf-2 not-flg true-type-alist false-type-alist
                                 (fcons-term* '< *0* arg2)
                                 xnot-flg x)))))
                  ((equal arg2 *0*)
                   (cond
                    ((ts-subsetp ts1
                                 *ts-negative-rational*)
                     (mv-atf not-flg t nil type-alist nil))
                    ((ts-subsetp ts1
                                 (ts-union (ts-complement *ts-acl2-number*)
                                           *ts-non-negative-rational*))
                     (mv-atf not-flg nil t nil type-alist))
                    (t
                     (let* ((true-type-alist
                             (extend-type-alist
                              arg1
                              (ts-intersection
                               ts1
                               (ts-union *ts-negative-rational*
                                         *ts-complex-rational*))
                              type-alist))
                            (false-type-alist
                             (extend-type-alist
                              arg1
                              (ts-intersection
                               ts1 (ts-complement
                                    *ts-negative-rational*))
                              type-alist)))
                       (mv-atf-2 not-flg true-type-alist false-type-alist
                                 (fcons-term* '< arg1 *0*)
                                 xnot-flg x)))))
                  (t (mv-let
                      (mbt mbf tta fta)
                      (assume-true-false1
                       xnot-flg ; = not-flg
                       x ; = (fcons-term* '< arg1 arg2)

; Once upon a time we had (fcons-term* '< arg1 arg2), above, instead of x.
; But we claim that not-flg is xnot-flg and that arg1 and arg2 are the
; corresponding arguments of x so that x is equal to (fcons-term* '< arg1 arg2).
; The proof is as follows.  We are in the t clause of a cond.  The preceding
; tests establish that neither arg1 nor arg2 is *0* here.  Hence, by
; Foreshadow 2 above we conclude that not-flg, arg1 and arg2 are
; unchanged from their values at Foreshadow 1.  But at Foreshadow 1 we
; see that if neither arg is *0* not-flg is xnot-flg and arg1 and arg2 are
; the corresponding components of x.  Q.E.D.

                       type-alist ancestors ens w nnn)

; Inefficiency: It is somewhat troubling that we are holding ts1 and
; ts2 in our hands while invoking assume-true-false1 on (< arg1 arg2),
; knowing full-well that it will recompute ts1 and ts2.  Sigh.  It
; would be nice to avoid this duplication of effort.

; PSIM test:  The comment above will not apply to Paco because of memoization.

; We could now return (mv mbt mbf tta fta) as the answer.  But, in the
; case that mbt and mbf are both nil we want to tighten up the returned
; type-alists a little if we can.  Suppose we are dealing with (< a1 a2) and a1
; is known to be positive.  Then a2 is (even more) positive.  We can add that
; to the tta, if it changes the type-set of a2.

                      (cond
                       ((or mbt mbf)

; Just return the already computed answers if we've settled the
; question.

                        (mv mbt mbf tta fta))
                       (t (let ((tta
                                 (assume-true-false-<
                                  not-flg
                                  arg1 arg2 ts1 ts2 tta))
                                (fta
                                 (assume-true-false-<
                                  (not not-flg)
                                  arg1 arg2 ts1 ts2 fta)))
                            (mv nil nil tta fta))))))))))))))
        ((or (eq (ffn-symb x) 'car)
             (eq (ffn-symb x) 'cdr))

; In this comment we assume (ffn-symb x) is car but everything we say is true
; for the cdr case as well.  Suppose xnot-flg is nil.  Then after the
; assume-true-false1 below, tta is the result of assuming (car arg) non-nil.
; But if (car arg) is non-nil, then arg is non-nil too.  That is, (implies (car
; arg) arg) is a theorem: Pf.  Consider the contrapositive, (implies (not arg)
; (not (car arg))). Q.E.D.  So we assume arg onto tta as well as (car arg).
; Fta, on the other hand, is the result of assuming (car arg) nil.  That tells
; us nothing about arg, e.g., arg could be nil, a cons (whose car is nil) or
; anything violating car's guard.  Summarizing this case: if xnot-flg is nil,
; then we assume both (car arg) and arg non-nil onto tta and assume only (car
; arg) nil onto fta.

; Now on the other hand, suppose xnot-flg is t.  Then tta contains
; the assumption that (car arg) is nil and fta contains the
; assumption that (car arg) is non-nil.  We can add to fta the
; assumption that arg is non-nil.  Observe that the two cases are
; symmetric if we simply swap the role of tta and fta before we start
; and after we are done.  The first swap is done by the let below.
; The second is done by mv-atf.

         (mv-let (mbt mbf tta fta)
                 (assume-true-false1
                  xnot-flg x type-alist ancestors ens w nnn)
                 (cond ((or mbt mbf)
                        (mv mbt mbf tta fta))
                       (t (let ((tta (if xnot-flg fta tta))
                                (fta (if xnot-flg tta fta)))
                            (mv-let (mbt1 mbf tta1 fta1)
                                    (assume-true-false
                                     (fargn x 1) tta ancestors ens w nnn)
                                    (declare (ignore mbt1 fta1))
                                    (mv-atf xnot-flg mbt mbf tta1 fta)))))))
        (t (assume-true-false1 xnot-flg x type-alist
                               ancestors ens w nnn))))))))

(defun assume-true-false1 (not-flg x type-alist ancestors ens w nnn)

  (declare (xargs :measure (lex4 nnn (acl2-count x) 11 0)))

; Roughly speaking, this is the simple assume-true-false, which just
; computes the type-set of x and announces that x must be t, must be
; f, or else announces neither and creates two new type-alists with x
; bound to its type minus *ts-t* and to *ts-nil*.  It returns the
; standard 4 results of assume-true-false.

  (let ((ts (type-set x type-alist ancestors ens w nnn)))
    (cond ((ts= ts *ts-nil*)
           (mv-atf not-flg nil t nil type-alist))
          ((not (ts-intersectp ts *ts-nil*))
           (mv-atf not-flg t nil type-alist nil))
          (t
           (mv-atf not-flg nil nil
                   (extend-with-proper/improper-cons-ts-tuple
                    x
                    (ts-intersection ts *ts-non-nil*)
                    type-alist ancestors type-alist ens w nnn)
                   (extend-type-alist x *ts-nil* type-alist))))))

(defun proper/improper-cons-ts-tuple (term ts type-alist ancestors ens w nnn)

  (declare (xargs :measure (lex4 nnn (acl2-count term) 9 0)))

; We return a tuple of the form (mv term' ts') that asserts the
; assumption that term has type set ts.  Most often, term' and ts' are
; just term and ts, i.e., the function is the identity.  However, if
; term is of the form (cons a x) we do certain auxiliary processing
; related to the existence of *ts-true-list* and its subtypes.

; We make various implicit assumptions about term and ts, all
; summarized by the restriction that this function can only be called
; by assume-true-false after checking the current type-set of term and
; failing to decide the question.

; We start with two examples.  Suppose term is (cons a1 x) and ts is
; *ts-true-list*.  Then the "various implicit assumptions" are
; violated because assume-true-false would never ask us to do this:
; the type-set of term is, at worst, the union of *ts-proper-cons* and
; *ts-improper-cons*, and certainly doesn't include *ts-nil*.  But
; assume-true-false always asks us to assume a type-set that is a
; subset of the current type-set.

; So suppose we are asked to assume that (cons a1 x) is of type
; *ts-proper-cons*.  Then we can deduce that x is of type
; *ts-true-list*.  Indeed, these two are equivalent because if we
; store the latter we can compute the former with type-set.  But
; because x is a subterm of (cons a1 x) we prefer to store the
; assumption about x because it will find greater use.  However, we
; may already know something about the type of x.  For example, x may
; be known to be non-nil.  So we are obliged to intersect the old type
; of x with the newly derived type if we want to keep maximizing what
; we know.  Because of the "implicit assumptions" this intersection
; will never produce the empty type set: if it is impossible for x to
; have the required type, then assume-true-false better not ask us to
; make the assumption.  For example, if x is known not to be a
; true-list, then assume-true-false would never ask us to assume that
; (cons a1 x) is proper.

; The example above is based on the theorem
;    (proper-consp (cons a1 x))   <-> (true-listp x).
; We similarly build in the theorem
;    (improper-consp (cons a1 x)) <-> (not (true-listp x)).

  (cond
   ((and (nvariablep term)
         (not (fquotep term))
         (eq (ffn-symb term) 'cons)
         (or (ts= ts *ts-proper-cons*)
             (ts= ts *ts-improper-cons*)))
    (let* ((x (non-cons-cdr term)))

; Can x be an explicit value?  If it can, then we'd be in trouble because
; we return a type-alist binding that sets the value of x.  But in fact
; x cannot be an explicit value.  If it were, then assume-true-false
; would have decided whether (cons a x) was proper or improper.

      (let ((tsx (type-set x type-alist ancestors ens w nnn)))
        (cond
         ((ts= ts *ts-proper-cons*)
          (mv x (ts-intersection tsx *ts-true-list*)))
         (t
          (mv x (ts-intersection tsx (ts-complement *ts-true-list*))))))))
   (t (mv term ts))))

(defun extend-with-proper/improper-cons-ts-tuple
  (term ts type-alist ancestors type-alist-to-be-extended ens w nnn)

  (declare (xargs :measure (lex4 nnn (acl2-count term) 10 0)))

; Programming Note:

; Our convention is to call this function to extend the type-alist
; unless we know that the supplied ts is neither *ts-proper-cons* nor
; *ts-improper-cons*.  That is, we use this function to construct
; type-alist entries in only some of the cases.  See also
; extend-type-alist-simple and extend-type-alist.

  (mv-let (term ts)
          (proper/improper-cons-ts-tuple term ts type-alist
                                         ancestors ens w nnn)
          (extend-type-alist term ts type-alist-to-be-extended)))

)

(defun type-set-lst (x type-alist ancestors ens w nnn)

; This function computes the type-set of each element of x and returns
; the corresponding list of type-sets.

  (cond
   ((endp x) nil)
   (t (cons (type-set (car x) type-alist ancestors ens w nnn)
            (type-set-lst (cdr x) type-alist ancestors ens w nnn)))))

; This is a completely arbitrary limitation on type-set.  The nnn argument
; limits
; (1) The number of times we can expand a lambda application or a
;     non-rec function in type-set;
; (2) The depth of backchaining in relieving the hyps of type-prescription
;     rules;

; However, (2) is a little misleading because we decrement nnn twice
; in a certain backchaining step.  After the unification of the term
; with a rule's pattern, we create a type-alist pairing the variables
; in the rule with the types of the instantiations of those variables.
; We decrement nnn once when we call the function,
; extend-type-alist-with-bindings, since the alist over which we are
; recursing can be arbitrarily long -- it is a function of the rule.
; Then, when we compute the type-set of each instantiation, we
; decrement nnn again, as though the instantiation could be
; arbitrarily big.  In fact, each instantiation is a subterm of the
; original term and hence strictly smaller.  But proving that they are
; smaller would require proving a theorem about one-way-unify.  Rather
; than do that, we just tick nnn.  So with a small nnn we might get
; *ts-unknown* contributions to the type-sets of the instantiations on
; big terms, even though in principle we need not.  In addition, we
; decrement nnn when we get the type of a hypothesis and when we get
; the types of the variables that the type-prescription rule lists.

(defconst *type-set-nnn* 30)

; ---------------------------------------------------------------------------
; Section:  Elementary Uses of Type-Sets

; Next we develop the idea of type-alist-clause, which converts a
; clause to a type-alist.  The Paco version of this is much simpler
; than ACL2's.  ACL2 is concerned with ordering equivalence relations
; so that it is complete on chains of equalities, tracks the
; dependencies of type-alist entries on literals so we don't have to
; reprocess the same clause subsets repeatedly, and takes care to
; process the resulting type-alist in a way that reduces the
; dependence on the order of the literals in the clause.

(defun type-alist-clause (cl type-alist ens wrld)

; We construct an extension of type-alist in which every literal of cl
; is assumed false.  We return two values.  The first is t or nil and
; indicates whether we found a contradiction, i.e., that some literal
; of cl is true.  The second is the resulting type-alist (or nil if we
; got a contradiction).

  (cond ((endp cl) (mv nil type-alist))
        (t (mv-let
            (mbt mbf tta fta)
            (assume-true-false (car cl)
                               type-alist
                               nil
                               ens
                               wrld
                               *type-set-nnn*)
            (declare (ignore tta))
            (cond
             (mbt (mv t nil))
             (mbf (type-alist-clause (cdr cl) type-alist ens wrld))
             (t (type-alist-clause (cdr cl) fta ens wrld)))))))

(defun known-whether-nil (x type-alist ens wrld)

; This function determines whether we know, from type-set reasoning,
; whether x is nil or not.  It returns two values.  The first is the
; answer to the question "Do we know whether x is nil or not?"  If the
; answer to that question is yes, the second value is the answer to
; the question "Is x nil?"  If the answer to the first question is no,
; the second value is nil.

  (cond ((quotep x)
         (mv t (equal x *nil*)))
        (t (let ((ts (type-set x type-alist nil ens wrld *type-set-nnn*)))
             (cond ((ts= ts *ts-nil*)
                    (mv t t))
                   ((ts-intersectp ts *ts-nil*)
                    (mv nil nil))
                   (t (mv t nil)))))))

(defun ts-booleanp (term type-alist ens wrld)
  (ts-subsetp (type-set term type-alist nil ens wrld *type-set-nnn*)
              *ts-boolean*))

(defun weak-cons-occur (x y)

; Both x and y are terms.  In addition, x is known to be non-quoted
; and not a CONS expression.  Consider the binary tree obtained by
; viewing the term y as a CONS tree.  We return t iff x is a tip of
; that tree.

  (cond ((variablep y) (eq x y))
        ((fquotep y) nil)
        ((eq (ffn-symb y) 'cons)
         (or (weak-cons-occur x (fargn y 1))
             (weak-cons-occur x (fargn y 2))))
        (t (equal x y))))

(defun equal-x-cons-x-yp (lhs rhs)

; We answer the question ``Is (EQUAL lhs rhs) definitely nil?''  If
; our result is t, then the equality is definitely nil, without
; further qualification.  If we say we don't know, i.e., nil, nothing
; is claimed.

; However, we know some things about lhs and rhs that allow us to
; make this function answer ``I don't know'' more quickly and more
; often than it might otherwise.  We assume tht lhs and rhs are not
; identical terms and we know they are not both quoted constants
; (though either may be) and we know that their type sets have a
; non-empty intersection.

; We make our positive decision based on structural reasoning.  For
; example, (EQUAL x (CONS x &)), is NIL because x occurs properly
; within the CONS tree.  This observation does not depend on type-sets or
; anything else.

; However, we don't want to do too much work exploring the two terms.
; For example, if they are both large explicit values we don't want to
; look for them in eachother.  We know that we will eventually apply
; the CONS-EQUAL axiom, which will rewrite the equality of two conses
; (constants or otherwise) to the conjoined equalities of their
; components.  Thus, if both lhs and rhs are CONS expressions (i.e., a
; consityp) or quoted list constants, we just return nil and let the
; :REWRITE rules take care of it.

; One more minor optimization: if one of our args is a consityp and
; the other is a quoted constant then the constant must be a consp or
; else the type sets wouldn't intersect.

  (cond ((variablep lhs)
         (cond ((consityp rhs)
                (or (weak-cons-occur lhs (fargn rhs 1))
                    (weak-cons-occur lhs (fargn rhs 2))))
               (t nil)))
        ((fquotep lhs) nil)
        ((eq (ffn-symb lhs) 'cons)
         (cond ((variablep rhs)
                (or (weak-cons-occur rhs (fargn lhs 1))
                    (weak-cons-occur rhs (fargn lhs 2))))
               ((fquotep rhs) nil)
               ((eq (ffn-symb rhs) 'cons) nil)
               (t (or (weak-cons-occur rhs (fargn lhs 1))
                      (weak-cons-occur rhs (fargn lhs 2))))))
        ((consityp rhs)
         (or (weak-cons-occur lhs (fargn rhs 1))
             (weak-cons-occur lhs (fargn rhs 2))))
        (t nil)))

(defun not-ident (term1 term2 type-alist ens wrld)

; We return t iff (equal term1 term2) is false.

  (cond ((and (quotep term1)
              (quotep term2))
         (not (equal term1 term2)))
        (t (let ((ts1 (type-set term1 type-alist nil ens wrld *type-set-nnn*))
                 (ts2 (type-set term2 type-alist nil ens wrld *type-set-nnn*)))
             (cond
              ((not (ts-intersectp ts1 ts2))
               t)
              ((equal-x-cons-x-yp term1 term2)
               t)
              (t nil))))))

; ---------------------------------------------------------------------------
; Section:  IF-Normalization

(defun first-if (args i)

; This function searches the top level of the list args for an
; top-level IF expression.  If it does not find one, it returns
; 2 nils.  Otherwise, it returns the position of the first one
; it finds and the IF expression found.

  (cond ((endp args) (mv nil nil))
        ((and (nvariablep (car args))
              (not (quotep (car args)))
              (eq (ffn-symb (car args)) 'if))
         (mv i (car args)))
        (t (first-if (cdr args) (1+ i)))))

; We are headed toward the function normalize, which produces a term
; in IF-normal form.  The ACL2 version of this function is problematic
; to admit because it is both mutually recursive and reflexive.  The
; reflexivity comes about because after normalizing alpha in (if alpha
; beta gamma) and getting (if (if a1 a2 a3) beta gamma), we normalize
; (if a1 (if a2 beta gamma) (if a3 beta gamma)).  The reason we have
; to normalize alpha to see the IF is that it might be buried in a
; function call, e.g., (if (foo (if a1 a2 a3)) beta gamma).  After
; spending a while trying to admit the function I punted and recode it
; so that it is not reflexive.  But now it makes several passes.  The
; basic algorithm is: distribute all IFs and then reduce by exploiting
; tests.  To distribute IFs, I distribute IFs in all args
; independently and then create an IF-distributed function
; application.

; We now define the function reduce, which takes a term in which the
; IFs have been completely distributed and simplifies it by using the
; assumptions of the tests in the branches.  Thus, for example, (IF A
; B A) is reduced to (IF A B NIL).

(defun reduce-with-type-set (term iff-flg type-alist ens wrld)
  (let ((ts (type-set term type-alist nil ens wrld *type-set-nnn*)))
    (cond ((ts-intersectp ts *ts-nil*)
           (cond ((ts= ts *ts-nil*) *nil*)
                 (t term)))
          (iff-flg *t*)
          ((ts= ts *ts-t*) *t*)
          ((ts= ts *ts-zero*) *0*)
          (t term))))

(defun cons-term-if (t1 t2 t3 iff-flg type-alist ens wrld)
  (cond ((equal t1 *t*) t2)
        ((equal t1 *nil*) t3)
        ((equal t2 t3) t2)
        ((and (equal t1 t2)
              (equal t3 *nil*))
         t1)
        ((and (equal t2 *t*)
              (equal t3 *nil*))
         (cond
          (iff-flg t1)
          ((ts-booleanp t1 type-alist ens wrld) t1)
          (t (fcons-term* 'if t1 t2 t3))))
        (t (fcons-term* 'if t1 t2 t3))))

(defun cons-term-equal (t1 t2)
  (cond ((equal t1 t2) *t*)
        ((and (quotep t1) (quotep t2)) *nil*)
        (t (fcons-term* 'equal t1 t2))))

(defun reduce (term iff-flg type-alist ens wrld recursivep)
  (declare (xargs :hints (("Goal" :in-theory (disable assume-true-false)))))
  (cond
   ((variablep term)
    (reduce-with-type-set term iff-flg type-alist ens wrld))
   ((fquotep term)
    (if iff-flg
        (if (equal term *nil*) *nil* *t*)
      term))
   ((eq (ffn-symb term) 'IF)
    (if recursivep
        (let ((t1 (fargn term 1)))
          (mv-let
           (mbt mbf tta fta)
           (assume-true-false t1 type-alist nil ens wrld *type-set-nnn*)
           (cond
            (mbt (reduce (fargn term 2) iff-flg type-alist ens wrld t))
            (mbf (reduce (fargn term 3) iff-flg type-alist ens wrld t))
            (t (let ((t2 (reduce (fargn term 2) iff-flg tta ens wrld t))
                     (t3 (reduce (fargn term 3) iff-flg fta ens wrld t)))
                 (cons-term-if t1 t2 t3
                               iff-flg type-alist ens wrld))))))
      term))
   ((or iff-flg
        (eq (ffn-symb term) 'NOT)
        (<reduce-id>
         (most-recent-enabled-recog-tuple
          (ffn-symb term)
          (global-val 'recognizer-alist wrld)
          ens))
        (eq (ffn-symb term) 'EQUAL)
        (eq (ffn-symb term) '<))
    (mv-let (mbt mbf tta fta)
            (assume-true-false term type-alist nil ens wrld *type-set-nnn*)
            (declare (ignore tta fta))
            (cond
             (mbt *t*)
             (mbf *nil*)

; If assume-true-false doesn't categorize this term, we assume that
; reduce-with-type-set wouldn't either.  Is it possible that
; type-set knows more about some terms of the above shape than
; assume-true-false?

             (t term))))
   (t (reduce-with-type-set term iff-flg type-alist ens wrld))))

; Now we define distribute-ifs, which distributes the IFs in a term.

(defthm acl2-count-subst-for-nth
  (implies (and (integerp i)
                (<= 0 i)
                (< i (len args))
                (< (acl2-count new-arg) (acl2-count (nth i args))))
           (< (acl2-count (subst-for-nth new-arg i args))
              (acl2-count args)))
  :rule-classes :linear)

(defthm bounds-on-mv-nth-0-first-if-gen
  (implies (and (integerp z)
                (<= 0 z)
                (car (first-if args z)))
           (and (integerp (car (first-if args z)))
                (<= 0 (car (first-if args z)))
                (< (car (first-if args z)) (+ (len args) z))
                (<= z (car (first-if args z)))))
  :rule-classes nil)

(defthm mv-nth-0-first-if-type-prescription
  (implies (and (integerp z)
                (<= 0 z))
           (or (null (car (first-if args z)))
               (and (integerp (car (first-if args z)))
                    (<= 0 (car (first-if args z))))))
  :rule-classes :type-prescription
  :hints (("Goal" :use (:instance bounds-on-mv-nth-0-first-if-gen))))

(defthm bounds-on-mv-nth-0-first-if-linear1
  (implies (car (first-if args 0))
           (< (car (first-if args 0)) (len args)))
  :rule-classes :linear
  :hints (("Goal" :use (:instance bounds-on-mv-nth-0-first-if-gen (z 0)))))

(defthm bounds-on-mv-nth-0-first-if-linear2
  (implies (and (integerp z)
                (<= 0 z)
                (car (first-if args z)))
           (<= z (car (first-if args z))))
  :hints (("Goal" :use bounds-on-mv-nth-0-first-if-gen))
  :rule-classes :linear)

(include-book "arithmetic/top-with-meta" :dir :system)

(defthm mv-nth-1-first-if-gen
  (implies (and (integerp z)
                (<= 0 z)
                (car (first-if args z)))
           (equal (MV-NTH 1 (FIRST-IF ARGS z))
                  (nth (- (car (first-if args z)) z)
                       args))))

(defthm consp-nth-mv-nth-0-first-if-gen
  (implies (and (integerp z)
                (<= 0 z)
                (car (first-if args z)))
           (consp (nth (- (car (first-if args z)) z)
                       args)))
  :rule-classes nil)

(defthm consp-nth-mv-nth-0-first-if
  (implies (car (first-if args 0))
           (consp (nth (car (first-if args 0))
                       args)))
  :hints (("Goal" :use (:instance consp-nth-mv-nth-0-first-if-gen (z 0)))))

(defthm acl2-count-caddr
  (implies (consp x)
           (< (acl2-count (caddr x)) (acl2-count x))))

(defun dcons-term (fn args type-alist ens wrld)

; Distributed cons-term: Distribute the IFs in the args as you build
; (fn . args).

  (declare (xargs :measure (acl2-count args)
                  :hints (("Goal" :in-theory (disable assume-true-false)))))

  (cond
   ((and (eq fn 'IF)
         (not (and (nvariablep (car args))
                   (not (fquotep (car args)))
                   (eq (ffn-symb (car args)) 'IF))))

; We could reduce the following expression.  The test, (car args), has
; not been assumed true/false for the two branches -- they were
; distributed independently.  But if we dive here, then we dive
; repeatedly.  I think it is best to do a single reducion from the
; top.

    (cons-term-if (car args) (cadr args) (caddr args)
                  nil type-alist ens wrld))
   (t
    (mv-let
     (n if-expr)
     (first-if args 0)
     (cond
      ((null n)

; There is no IF at the top-level of term, and since all the args are
; distributed, we know there are no IFs at all.  We are thus at the
; bottom of the IF tree and type-alist has on it everything we know.

       (cond ((equal fn 'EQUAL)
              (cons-term-equal (car args) (cadr args)))
             ((equal fn 'IF)
              (cons-term-if (car args) (cadr args) (caddr args)
                            nil type-alist ens wrld))
             (t (cons-term fn args))))

; And here is the code after which this function was named.  We have
; found an if-expr in the args of term at location n.  Since that if
; is in normal form, its test is not an if.  We split on that test and
; distribute the if and continue developing the two branches.  Since
; the type-alist may change as we go but the IF expressions were all
; distributed in advance, their tests may be known in the current
; type-alist.

      (t (let ((t1 (fargn if-expr 1)))
           (mv-let
            (mbt mbf tta fta)
            (assume-true-false t1 type-alist nil ens wrld *type-set-nnn*)
            (cond
             (mbt
              (dcons-term
               fn
               (subst-for-nth (fargn if-expr 2) n args)
               type-alist ens wrld))
             (mbf
              (dcons-term
               fn
               (subst-for-nth (fargn if-expr 3) n args)
               type-alist ens wrld))
             (t
              (let ((t2
                     (dcons-term
                      fn
                      (subst-for-nth (fargn if-expr 2) n args)
                      tta ens wrld))
                    (t3
                     (dcons-term
                      fn
                      (subst-for-nth (fargn if-expr 3) n args)
                      fta ens wrld)))
                (cons-term-if t1 t2 t3 nil type-alist ens wrld))))))))))))

(mutual-recursion

(defun distribute-ifs (term iff-flg type-alist ens wrld)

  (declare (xargs :measure (acl2-count term)
                  :hints (("Goal"
                           :do-not-induct t
                           :in-theory
                           (disable assume-true-false
                                    type-set)))))

; This function distributes IFs in term, simplifying with type-set
; reasoning as it goes.  We return a term equivalent to term
; (propositionally equivalent, if the iff-flg is t) under the
; assumptions in type-alist.

  (cond
   ((variablep term)
    (reduce-with-type-set term iff-flg type-alist ens wrld))
   ((fquotep term)
    (cond ((and iff-flg (not (equal term *nil*))) *t*)
          (t term)))
   ((flambda-applicationp term)
    (let* ((dargs (distribute-ifs-lst (fargs term) nil type-alist ens wrld))
           (dbody (distribute-ifs
                   (lambda-body (ffn-symb term))
                   iff-flg
                   (zip-variable-type-alist
                    (lambda-formals (ffn-symb term))
                    (type-set-lst dargs
                                  type-alist
                                  nil
                                  ens
                                  wrld
                                  *type-set-nnn*))
                   ens wrld)))

; We distribute IFs in the args and body of the lambda, but we do not
; expand the lambda.

      (fcons-term (list 'lambda (lambda-formals (ffn-symb term)) dbody)
                  dargs)))
   (t
    (dcons-term (ffn-symb term)
                (distribute-ifs-lst (fargs term)
                                    (if (eq (ffn-symb term) 'IF)
                                        (if iff-flg
                                            t
                                          'IF)
                                      nil)
                                    type-alist ens wrld)
                type-alist ens wrld))))

(defun distribute-ifs-lst (args iff-flg type-alist ens wrld)

  (declare (xargs :measure (acl2-count args)))

; The handling of the iff-flg is unusual here.  Normally the flag is
; either nil or t.  Here it can also take the value IF.  When it is
; IF, the first element of args is distributed with iff-flg = t and
; all others are distributed with iff-flg nil.

  (cond ((endp args) nil)
        (t (cons (distribute-ifs (car args)
                                 (if (eq iff-flg 'IF) t iff-flg)
                                 type-alist ens wrld)
                 (distribute-ifs-lst (cdr args)
                                     (if (eq iff-flg 'IF) nil iff-flg)
                                     type-alist ens wrld)))))

)

(defun normalize (term iff-flg type-alist ens wrld)
  (reduce (distribute-ifs term iff-flg type-alist ens wrld)
          iff-flg type-alist ens wrld t))

; For an interesting test of normalization see normalize-test.lisp.
; For that test, at least, Paco peformed about 10 times better than
; ACL2!  But theorem proving performance measuring is notoriously
; unpredictive of actual behavior because of the variability of
; performance across different combinatoric cases.