This file is indexed.

/usr/share/acl2-6.5/bdd.lisp is in acl2-source 6.5-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
; ACL2 Version 6.5 -- A Computational Logic for Applicative Common Lisp
; Copyright (C) 2014, Regents of the University of Texas

; This version of ACL2 is a descendent of ACL2 Version 1.9, Copyright
; (C) 1997 Computational Logic, Inc.  See the documentation topic NOTE-2-0.

; This program is free software; you can redistribute it and/or modify
; it under the terms of the LICENSE file distributed with ACL2.

; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
; LICENSE for more details.

; Written by:  Matt Kaufmann               and J Strother Moore
; email:       Kaufmann@cs.utexas.edu      and Moore@cs.utexas.edu
; Department of Computer Science
; University of Texas at Austin
; Austin, TX 78712 U.S.A.

; Table of contents.
;
; 0.    PRELIMINARY MACROS
; I.    INTRODUCTION AND DATA TYPES
; II.   OP-ALIST
; III.  HASH OPERATIONS
; IV.   HASH OPERATIONS: QUOTEPS
; V.    BDD RULES AND ONE-WAY UNIFIER
; VI.   SOME INTERFACE UTILITIES
; VII.  MAIN ALGORITHM
; VIII. TOP-LEVEL (INTERFACE) ROUTINES
; IX.   COMPILING THIS FILE AND OTHER HELPFUL TIPS
;

; Mx-id-bound is currently 438619, perhaps too low.  We could perhaps fix this
; by changing how we deal with close to 16 args in op-hash-index1, and by
; changing 131 in if-hash-index.

(in-package "ACL2")

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;; 0. PRELIMINARY MACROS ;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defmacro mvf (x &rest rest)

; We often wish to pass back a multiple value such that the first value is a
; fixnum.  Efficiency is apparently improved in GCL when that fixnum is not
; "boxed," but instead is treated as a raw C integer.  Currently ACL2 provides
; mechanisms for this, but they require that an appropriate THE expression
; surround such a value when it is the first value passed back by MV.  (Note
; that there seems to be no way to keep GCL from boxing fixnums in other than
; the first argument position of MV.)

  `(mv (the-fixnum ,x) ,@rest))

(defmacro logandf (&rest args)
  (xxxjoin-fixnum 'logand args -1))

(defmacro logxorf (&rest args)
  (xxxjoin-fixnum 'logxor args 0))

(defmacro logiorf (&rest args)
  (xxxjoin-fixnum 'logior args 0))

(defmacro ashf (x y)
  (list 'the-fixnum
        (list 'ash (list 'the-fixnum x) (list 'the-fixnum y))))

(defmacro mx-id-bound ()

; This bound on mx-id must be such that our calls of +f and *f involving mx-id
; produce fixnums.  At this writing the most severe such test is in
; op-hash-index1; see the comment there.

  (1- (floor (fixnum-bound) 153)))

(defmacro 1+mx-id (x)

; DILEMMA:  Do we let this macro box (1+ x) or not, and if so, when?  Here are
; some thoughts on the issue.

; Use this macro to increment mx-id,in order to guarantee that mx-id remains a
; fixnum.  X is known to be a nonnegative fixnum; this macro checks that we
; keep it a fixnum by adding 1 to it.  It actually checks even more, namely,
; that

  `(the-fixnum
    (let ((x ,x))
      (declare (type (signed-byte 30) x))

; Should we really include the declaration above?  The main reason seems to be
; in order for the incrementing operation below to run fast, but in fact we
; have done several experiments and it seems that the current version of this
; code is optimal for performance.  That's a bit surprising, since each mx-id
; gets consed into a list anyhow (a cst), and hence is boxed in GCL (which is
; the only list we are talking about here).  So, there wouldn't appear to be
; any particular advantage in wrapping the-fixnum around this form.  At any
; rate, the performance issues here seem to be quite minor.

; A typical use of this macro is of the form

;  (let ((new-mx-id (1+mx-id mx-id)))
;    (declare (type (signed-byte 30) new-mx-id))
;    (let ((new-cst (make-leaf-cst
;                    new-mx-id
;                    term
;                    nil)))
;      (mvf new-mx-id
;           ...)))

; Note that make-leaf-cst will box new-mx-id -- after all, it is consing
; new-mx-id into a list.  The present approach delays this boxing until that
; time, so that we don't have to unbox new-mx-id in the mvf form above.  The
; unboxed new-mx-id may actually never benefit from being unboxed, and in fact
; we may want to rework our entire bdd code so that mx-ids are always boxed.

      (cond ((< x ,(mx-id-bound))
             (1+f x))
            (t (the-fixnum ; the-fixnum call may help with proclaiming
                (er-hard-val 0 'bdd
                    "Maximum id for bdds exceeded.  Current maximum id is ~x0."
                    x)))))))

(defmacro bdd-error (mx-id fmt-string fmt-alist bad-cst ttree)

; Perhaps it would be more "natural" to define this macro to return

; `(mvf ,mx-id ,(cons fmt-string ,fmt-alist) ,bad-cst <nil-call-stack> ,ttree)

; since then we can view the result as

; `(mvf ,mx-id ,<msg> ,bad-cst ,call-stack ,ttree)

; However, we would like to have a very fast test for whether the tuple
; returned designates an error.  The present approach allows us to test with
; stringp on the second value returned.  We take advantage of that in the
; definition of bdd-mv-let.

; Note that the order of the first two values should not be changed:  we
; declare mx-id to be a fixnum at some point, and we we want the second
; position to be tested by stringp to see if we have an "error" situation.

; Keep this in sync with bdd-mv-let.

  `(mvf ,mx-id ,fmt-string (cons ,fmt-alist ,bad-cst)

; The following nil is really an initial value of the bdd-call-stack that is
; ultimately to be placed in a bddnote.  At the time of this writing,
; bdd-mv-let is the only place where we update this stack.

        nil
        ,ttree))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;; I. INTRODUCTION AND DATA TYPES ;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; In this work we represent terms in what we call the cheap syntax.  Such a
; "term" is called a "csterm".  Bryant would call it a "node."

; We are interested in normalized IF expressions corresponding to ACL2 terms.
; If x is not itself an IF term, then (IF x y z) is represented by

; `(k ,x ,boolp ,y . ,z)

; where k is a natural number that is uniquely associated with <x,y,z> and
; boolp is t if the term is known to be Boolean.  The association between k and
; <x,y,z> is arranged via a "hash table" discussed below.  The objective is
; that two canonicalized IF expressions are equal (and therefore represent the
; same term) iff their unique identifiers (cars) are =.

; We also represent "leaf" ACL2 terms, which are generally IF-free, as csts of
; a slightly different sort; see below.  (Note that these may have IFs in them
; because certain function symbols are "blocked" -- see bdd-constructors.)

; The list of "leaf" csts arising from variables in the input term, which we
; typically call leaf-cst-list, is passed around unchanged by various of our
; functions.  We rely on the op-ht to find csts for other leaves, and to avoid
; re-consing up leaf csts.

; The shapes of csts are as follows.  Note that two csts are equal iff their
; cars are =.

; Non-leaf:
; (unique-id tst boolp tbr . fbr) ; informally, represents (if tst tbr fbr)

; Leaf:
; (unique-id term boolp)
; where term is of one of the following forms:
;   variable
;   quotep
;   application of function symbol other than IF to a list of csts

; WARNING:  The definition of leafp below relies on the fact that leaf csts are
; exactly those whose final cdr is nil.  Do not succomb to the temptation to
; add a new field as the final cdr without taking this into account.

; Note:  It is tempting to replace the "term" in the last case by an ACL2 term,
; rather than an application of a function symbol to a list of csts.  However,
; the list of csts has presumably already been consed up, so we save the
; re-consing, awaiting the final decoding to build the actual ACL2 term if
; necessary.

; Macros for accessing canonicalized IFs:

(defmacro unique-id (x) `(the-fixnum (car ,x)))

(defmacro tst (x) `(cadr ,x)) ;a cst, not a number; but beware since tst=trm
                              ;and trm is a sort of term

; Note:  We found that 95% of the time on one test was being spent inside
; cst-boolp, when we used to keep this information directly in leaf nodes only.

(defmacro cst-boolp (x) `(caddr ,x))

(defmacro tbr (x) `(cadddr ,x))
(defmacro fbr (x) `(cddddr ,x))

(defmacro leafp (x)
  `(null (cdddr ,x)))

(defmacro trm (x) `(cadr ,x))

(defun bdd-constructors (wrld)
  (declare (xargs :guard
                  (and (plist-worldp wrld)
                       (alistp (table-alist 'acl2-defaults-table wrld)))))
  (let ((pair (assoc-eq :bdd-constructors (table-alist
                                           'acl2-defaults-table wrld))))
    (if pair
        (cdr pair)
      '(cons))))

(defun make-leaf-cst (unique-id trm boolp)

; We write the definition this way, rather than simply as something like (list*
; unique-id trm boolp ), in order to avoid repeatedly consing up '(t . nil) and
; '(nil . nil).

  (if boolp
      (list* unique-id trm '(t))
    (list* unique-id trm '(nil))))

(defun evg-fn-symb (x)

; This function takes the view that every explicit value can be constructed
; from 0.  It returns nil on 0, but, in principle, returns an appropriate
; function symbol otherwise.  At this point we choose not to support this idea
; in full, but instead we view cons as the only constructor.  We leave the full
; code in place as a comment, in case we choose to support this idea in the
; future.

;   (cond ((consp x) 'cons)
;         ((symbolp x) 'intern-in-package-of-symbol)
;         ((integerp x)
;          (cond ((< x 0) 'unary--)
;                ((< 0 x) 'binary-+)
;                (t nil)))
;         ((rationalp x)
;          (if (equal (numerator x) 1)
;              'unary-/
;            'binary-*))
;         ((complex-rationalp x)
;          'complex)
;         ((stringp x) 'coerce)
;         ((characterp x) 'char-code)
;         (t (er hard 'fn-symb "Unexpected object, ~x0."
;                x)))

  (cond ((consp x) 'cons)
        (t nil)))

(defun bdd-constructor-trm-p (trm bdd-constructors)
  (and (consp trm)
       (if (fquotep trm)
           (member-eq (evg-fn-symb (cadr trm)) bdd-constructors)
         (member-eq (car trm) bdd-constructors))))

(defun evg-type (x)

; This function takes the view that every explicit value can be constructed
; from 0.  It returns nil on 0, but, in principle, returns an appropriate
; function symbol otherwise.  See also evg-fn-symb.

  (cond ((consp x) 'consp)
        ((symbolp x) 'symbol)
        ((integerp x) 'integer)
        ((rationalp x) 'rational)
        ((complex-rationalp x) 'complex-rational)
        ((stringp x) 'string)
        ((characterp x) 'character)
        (t (er hard 'fn-symb "Unexpected object, ~x0."
               x))))

(defun make-if-cst (unique-id tst tbr fbr bdd-constructors)

; The second value returned is always a new cst.  The first value is non-nil
; when there is an "error", in which case that value is of the form (fmt-string
; . alist).

  (let* ((boolp (and (cst-boolp tbr)
                     (cst-boolp fbr)))
         (new-cst (list* unique-id tst boolp tbr fbr)))
    (cond
     ((or (and (leafp tbr)
               (bdd-constructor-trm-p (trm tbr) bdd-constructors))
          (and (leafp fbr)
               (bdd-constructor-trm-p (trm fbr) bdd-constructors)))
      (mv (msg "Attempted to create IF node during BDD processing with ~@0, ~
                which would produce a non-BDD term (as defined in :DOC ~
                bdd-algorithm).  See :DOC show-bdd."
               (let ((true-fn (trm tbr))
                     (false-fn (trm fbr)))
                 (cond
                  ((and (leafp tbr)
                        (bdd-constructor-trm-p (trm tbr) bdd-constructors))
                   (cond
                    ((and (leafp fbr)
                          (bdd-constructor-trm-p (trm fbr) bdd-constructors))
                     (msg "true branch with ~#0~[function symbol ~x1~/explicit ~
                         value of type ~x2~] and false branch with ~
                         ~#3~[function symbol ~x4~/explicit value  of type ~
                         ~x5~]"
                          (if (eq (car true-fn) 'quote) 1 0)
                          (car true-fn)
                          (and (eq (car true-fn) 'quote)
                               (evg-type (cadr true-fn)))
                          (if (eq (car false-fn) 'quote) 1 0)
                          (car false-fn)
                          (and (eq (car false-fn) 'quote)
                               (evg-type (cadr false-fn)))))
                    (t (msg "true branch with ~#0~[function symbol ~x1~/explicit ~
                           value of type ~x2~]"
                            (if (eq (car true-fn) 'quote) 1 0)
                            (car true-fn)
                            (and (eq (car true-fn) 'quote)
                                 (evg-type (cadr true-fn)))))))
                  (t (msg "false branch with ~#0~[function symbol ~x1~/explicit ~
                           value of type ~x2~]"
                          (if (eq (car false-fn) 'quote) 1 0)
                          (car false-fn)
                          (and (eq (car false-fn) 'quote)
                               (evg-type (cadr false-fn))))))))
          new-cst))
     (t (mv nil new-cst)))))

; We will always represent nil and t as described above.  To make this work, we
; must set the initial mx-id to 2, so the next unique id generated is 3.

; It is nearly inconsequential which of t or nil has the smaller id, but we
; find it handy to give t the smaller id, as noted in a comment in the
; definition of combine-op-csts-comm.

(defconst *cst-t*   (make-leaf-cst 1 *t* t))
(defconst *cst-nil* (make-leaf-cst 2 *nil* t))

(defmacro cst= (cst1 cst2)
  `(= (unique-id ,cst1)
      (unique-id ,cst2)))

(defmacro cst-tp (cst)
  `(= (unique-id ,cst) 1))

(defmacro cst-nilp (cst)
  `(= (unique-id ,cst) 2))

(defmacro cst-varp (cst)
  `(< 2 (unique-id ,cst)))

(defun cst-nonnilp (cst)
  (and (leafp cst)
       (if (quotep (trm cst))
           (not (cst-nilp cst))
         (and (nvariablep (trm cst))

; Consider other types here besides cons, e.g., that of numbers.  We may want
; to pass in a list of functions that have been checked to have type-sets that
; are disjoint from *ts-nil* and variable-free.  We would use a member-eq test
; below against such a list.  This list of function symbols could be determined
; easily from the list of all function symbols in op-alist.

              (eq (ffn-symb (trm cst)) 'cons)))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;; II. OP-ALIST ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; The goal of this section is to define functions op-alist and op-alist-info.
; See those definitions below for more details.  Briefly, these functions
; respectively build and do lookup in a so-called op-alist, which is a list of
; entries that describe function symbols occurring in the term for which we
; want to build a bdd.

(defun bool-mask1 (formals vars rune)

; Formals is the list of formals of a function symbol, and vars is a list of
; variables contained in formals such that every call of that function returns
; either t or nil, assuming that each var in vars is of boolean type.  This
; function returns a list in one-one correspondence with formals (but see
; below), replacing a formal by t if it belongs to vars (thus indicating that
; this position's actual might be returned) and nil if not.  Rune is a
; type-prescription record, used simply for the final cdr of the list returned
; (after all the t's and nil's have been listed as indicated above).

  (cond
   ((endp formals) rune)
   ((member-eq (car formals) vars)
    (cons t (bool-mask1 (cdr formals) vars rune)))
   (t (cons nil (bool-mask1 (cdr formals) vars rune)))))

(defun boolean-term-var (x)

; X is a term.  If x is of the form (booleanp v) or something "clearly"
; equivalent to it, return v.  Otherwise return nil.

  (and (not (variablep x))
       (not (fquotep x))
       (cond
        ((and (eq (ffn-symb x) 'booleanp)
              (variablep (fargn x 1)))
         (fargn x 1))
        ((eq (ffn-symb x) 'if)

; Check for translated version of (or (equal v t) (equal v nil)) or
; (or (equal v nil) (equal v t)).

         (let ((test (fargn x 1))
               (tbr (fargn x 2))
               (fbr (fargn x 3)))
           (and (not (variablep test))
                (not (fquotep test))
                (eq (ffn-symb test) 'equal)
                (let ((v (fargn test 1)))
                  (and (variablep v)
                       (let ((b (fargn test 2)))
                         (and (or (equal b *t*) (equal b *nil*))
                              (let ((c (if (equal b *t*) *nil* *t*)))
                                (if (and (equal test tbr)
                                         (equal fbr (fcons-term* 'equal v c)))
                                    v
                                  nil)))))))))
        (t nil))))

(defun boolean-hyps-vars (hyps)

; If hyps consists of terms of the form (booleanp v), or perhaps the
; equivalent, then we return a list indices of such v.

  (if (endp hyps)
      nil
    (let ((rst (boolean-hyps-vars (cdr hyps))))
      (if (eq rst t)
          t
        (let ((v (boolean-term-var (car hyps))))
          (if v
              (cons v rst)
            t))))))

(defun first-boolean-type-prescription (type-prescription-list ens formals)

; This function finds the most recent enabled type-prescription rule from the
; given list whose :basic-ts is boolean and :hyps are all of the form (booleanp
; v) or a "clearly" equivalent form, where the :term is of the form (fn ... v
; ...).  It returns two values.  The first is the :rune of the rule, which is
; non-nil if and only if such a rule is found.  If the first value is non-nil,
; then the second value is a "mask" as described in the comment in bool-mask.

  (cond
   ((endp type-prescription-list)
    (mv nil nil))
   ((and (enabled-numep
          (access type-prescription (car type-prescription-list) :nume)
          ens)
         (ts-subsetp
          (access type-prescription (car type-prescription-list) :basic-ts)
          *ts-boolean*))
    (let* ((tp (car type-prescription-list))
           (hyps (access type-prescription tp :hyps))
           (vars (access type-prescription tp :vars)))
      (if hyps
          (let ((more-vars (boolean-hyps-vars hyps)))
            (if (or (eq more-vars t)
                    (not (subsetp-eq more-vars formals)))
                (first-boolean-type-prescription (cdr type-prescription-list)
                                                 ens
                                                 formals)
              (mv (access type-prescription tp :rune)
                  (union-eq vars more-vars))))
        (mv (access type-prescription tp :rune)
            vars))))
   (t (first-boolean-type-prescription
       (cdr type-prescription-list) ens formals))))

(defun recognizer-rune (fn recognizer-alist wrld ens)
  (cond
   ((endp recognizer-alist) nil)
   ((and (eq fn (access recognizer-tuple (car recognizer-alist) :fn))
         (enabled-runep (access recognizer-tuple (car recognizer-alist) :rune)
                        ens
                        wrld))
    (access recognizer-tuple (car recognizer-alist) :rune))
   (t (recognizer-rune fn (cdr recognizer-alist) wrld ens))))

(defun bool-mask (fn recognizer-alist wrld ens)

; Returns a "mask" that is a suitable argument to bool-flg.  Thus, this
; function returns either nil or else a mask of the form

; (list* b1 b2 ... bn rune)

; where rune is a type prescription rune and each bi is either t or nil.  The
; function bool-flg will check that a given call of fn is boolean, returning
; rune if it can confirm this fact.  A bi must be marked t if the conclusion
; that the call of fn is boolean requires a check that the formal corresponding
; to bi is boolean.

; We give special treatment not only to compound recognizers, but also to
; Boolean-valued primitives, since these will not generally have built-in
; type-prescription rules.

  (cond
   ((or (eq fn 'equal) (eq fn '<))
    (list* nil nil *fake-rune-for-type-set*))
   ((eq fn 'not)

; `Not' is so basic that we could probably skip this case, but we might as well
; handle it appropriately.

    (list* nil *fake-rune-for-type-set*))
   (t
    (let ((rune (recognizer-rune fn recognizer-alist wrld ens))
          (formals (formals fn wrld)))
      (if rune
          (bool-mask1 formals nil rune)
        (mv-let (rune vars)

; We only consider the most recent type prescription with Boolean base type.
; Some day we might consider somehow combining all such type prescription
; rules.

                (first-boolean-type-prescription
                 (getprop fn 'type-prescriptions nil 'current-acl2-world wrld)
                 ens
                 formals)
                (and rune
                     (bool-mask1 formals vars rune))))))))

(defun commutative-p1 (fn lemmas ens)

; Fn is assumed to have arity 2 in the current ACL2 world.

  (cond
   ((endp lemmas) nil)
   (t (if (and (member-eq (access rewrite-rule (car lemmas) :subclass)
                          '(backchain abbreviation))
               (equal (access rewrite-rule (car lemmas) :equiv) 'equal)
               (enabled-numep (access rewrite-rule (car lemmas) :nume) ens)
               (null (access rewrite-rule (car lemmas) :hyps))
               (let ((lhs (access rewrite-rule (car lemmas) :lhs))
                     (rhs (access rewrite-rule (car lemmas) :rhs)))
                 (and (or (eq (ffn-symb lhs) fn)
                          (er hard 'commutative-p1
                              "We had thought we had a rewrite rule with :lhs ~
                               being a call of ~x0, but the :lhs is ~x1."
                              fn lhs))
                      (nvariablep rhs)
                      (not (fquotep rhs))
                      (eq (ffn-symb rhs) fn)
                      (variablep (fargn lhs 1))
                      (variablep (fargn lhs 2))
                      (not (eq (fargn lhs 1) (fargn lhs 2)))
                      (equal (fargn lhs 1) (fargn rhs 2))
                      (equal (fargn lhs 2) (fargn rhs 1)))))
          (access rewrite-rule (car lemmas) :rune)
        (commutative-p1 fn (cdr lemmas) ens)))))

(defun find-equivalence-rune (fn rules)
  (cond
   ((endp rules)
    nil)
   ((eq (access congruence-rule (car rules) :equiv) fn)
    (let ((rune (access congruence-rule (car rules) :rune)))
         (if (eq (car rune) :equivalence)
             rune
           (find-equivalence-rune fn (cdr rules)))))
   (t (find-equivalence-rune fn (cdr rules)))))

(defun equivalence-rune1 (fn congruences)

; For example, if fn is 'iff then congruences may contain:

; (EQUAL ((126 IFF :EQUIVALENCE IFF-IS-AN-EQUIVALENCE))
;        ((126 IFF :EQUIVALENCE IFF-IS-AN-EQUIVALENCE)))

; But the two singleton lists above can contain other members too.  See the
; Essay on Equivalence, Refinements, and Congruence-based Rewriting.

; See add-equivalence-rule.

  (cond
   ((endp congruences)
    (er hard 'equivalence-rune
        "Failed to find an equivalence rune for function symbol ~x0."
        fn))
   (t (let ((x (car congruences)))
        (case-match x
                    (('equal rules1 rules2)
                     (let ((rune (find-equivalence-rune fn rules1)))
                       (if (and rune
                                (equal rune (find-equivalence-rune fn rules2)))
                           rune
                         (equivalence-rune1 fn (cdr congruences)))))
                    (& (equivalence-rune1 fn (cdr congruences))))))))

(defun equivalence-rune (fn wrld)
  (declare (xargs :guard (equivalence-relationp fn wrld)))
  (cond
   ((eq fn 'equal)
    (fn-rune-nume 'equal nil nil wrld))
   (t (equivalence-rune1 fn
                         (getprop fn 'congruences
                                  '(:error "See equivalence-rune.")
                                  'current-acl2-world wrld)))))

(defun commutative-p (fn ens wrld)

; Note that if the value is non-nil, it is a rune justifying the commutativity
; of the given function.

  (and (= (arity fn wrld) 2)
       (if (equivalence-relationp fn wrld)
           (equivalence-rune fn wrld)
           (commutative-p1 fn
                           (getprop fn 'lemmas nil 'current-acl2-world wrld)
                           ens))))

; To memoize the various merging operations we will hash on the opcodes.
; Increasing each by a factor of 3 was intended to make it spread out a little
; more, but (at least this has been true at one time) it doesn't have much of
; an effect.

(defun op-alist (fns acc i ens wrld)

; Build a list of entries (op opcode comm-p enabled-executable-counterpartp
; mask).  The next index to use is i.  Call this as in (op-alist (remove1-eq
; 'if (all-fnnames term)) nil 6 (ens state) (w state)).  Keep this in sync with
; op-alist-info.

; Note that if comm-p is non-nil, it is a rune justifying the commutativity of
; the given function.  Similarly, if enabled-executable-counterpartp is non-nil
; then it is an :executable-counterpart rune.

  (cond
   ((endp fns) acc)
   ((> i (mx-id-bound))
    (er hard 'bdd
        "We are very surprised to see that, apparently, your problem for bdd ~
         processing involves ~x0 function symbols!  We cannot handle this many ~
         function symbols."
        (/ i 3)))
   (t (op-alist (cdr fns)
                (cons (list* (car fns)
                             i
                             (commutative-p (car fns) ens wrld)
                             (and (not (getprop (car fns) 'constrainedp nil
                                                'current-acl2-world wrld))
                                  (enabled-xfnp (car fns) ens wrld)
                                  (fn-rune-nume (car fns) nil t wrld))
                             (bool-mask (car fns)
                                        (global-val 'recognizer-alist wrld)
                                        wrld
                                        ens))
                      acc)
                (+ 3 i)
                ens
                wrld))))

(defun op-alist-info (fn op-alist)

; Returns (mv opcode comm-p enabled-exec-p mask).  Keep this in sync with
; op-alist.

  (cond
   ((endp op-alist)
    (mv (er hard 'op-alist-info
            "Function not found:  ~x0"
            fn)
        nil nil nil))
   ((eq fn (caar op-alist))
    (let ((info (cdar op-alist)))
      (mv (car info)
          (cadr info)
          (caddr info)
          (cdddr info))))
   (t (op-alist-info fn (cdr op-alist)))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;; III. HASH OPERATIONS ;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defmacro if-op-code () 3)

(defmacro hash-size ()

; Do not quote the body of this definition.  We want it computed at
; definition/compile time!

; At one time we used a defconst here, but then we realized that we would
; (apparently, at least, in GCL) have to pay the price both of looking up the
; value of that variable and also unboxing it for fixnum operations.  Although
; a little experimentation did not yield meaningful evidence that there is
; really an impact on performance, we proceed with a macro.

; WARNING:  Do not increase this size too close to (fixnum-bound).  See the
; warning in op-hash-index-evg.

  (1- (expt 2 15)))

; We have two hash arrays, 'if-ht for assigning unique-ids to csts, and 'op-ht
; for memo-izing the merge operators.  In each case the array assigns "buckets"
; to indices.

; Buckets in an if-ht are lists of non-leaf csts.

; Buckets in an op-ht are lists of entries of the form (cst op . args), where
; args is a list of csts.  The length of the list is the arity of op.
; Exception:  op can be quote, in which case args is a list containing a single
; evg.

(defmacro if-hash-index (x y z)

; Note that (+ 131 2 1) does not exceed 153.  See the comment about mx-id-bound
; in op-hash-index1.  There is probably nothing sacred about the choices of
; these three numbers 131, 2, and 1, although it seems good that they are
; relatively prime.

  `(logandf (+f (*f 131 (unique-id ,x))
                (*f 2 (unique-id ,y))
                (unique-id ,z))
            (hash-size)))

(defun op-hash-index1 (args i acc)

; There should be no more than 16 args before we "turn around", so that the
; multiplier on unique-ids is no more than (1+ (+ 2 3 ... 17)) = 153.  (The
; `1+' is because in op-hash-index we add in the op-code as well.  Op-codes are
; also bounded by mx-id-bound -- see op-alist -- as are of course unique-ids.)
; See the comment in mx-id-bound.

; If we want to increase the (mx-id-bound), we believe that we could start the
; "turnaround" here earlier.  But we have not yet checked this claim carefully.

  (declare (type (signed-byte 30) i acc)
           (xargs :measure (acl2-count args)))
  (the-fixnum
   (cond
    ((endp args) (if (< acc 0) (- acc) acc))
    ((or (= (the-fixnum i) 18)
         (= (the-fixnum i) -1))
     (if (> acc 0)
         (op-hash-index1 args -17 acc)
       (op-hash-index1 args 2 acc)))
    (t (op-hash-index1 (cdr args)
                       (1+f i)
                       (+f acc
                           (*f i
                               (unique-id (car args)))))))))

(defmacro op-hash-index (op-code args)
  `(logandf (+f ,op-code
                (op-hash-index1 ,args 2 1))
            (hash-size)))

(defmacro op-hash-index-2 (op-code arg1 arg2)

; This special case of op-hash-index is used for commutative operators in
; chk-memo-2.

  `(logandf (+f ,op-code
                (*f 2 (unique-id ,arg1))
                (*f 3 (unique-id ,arg2)))
            (hash-size)))

(defmacro op-hash-index-if (arg1 arg2 arg3)
  `(logandf (+f (if-op-code)
                (*f 2 (unique-id ,arg1))
                (*f 3 (unique-id ,arg2))
                (*f 4 (unique-id ,arg3)))
            (hash-size)))

; Having found the bucket associated with the hash-index, here is how
; we search it.

(defun if-search-bucket (x y z lst)

; Here lst is a list of non-leaf csts.

  (cond ((null lst) nil)
        ((and (cst= x (tst (car lst)))
              (cst= y (tbr (car lst)))
              (cst= z (fbr (car lst))))
         (car lst))
        (t (if-search-bucket x y z (cdr lst)))))

(defun cst=-lst (x y)
  (cond
   ((endp x) t)
   (t (and (cst= (car x) (car y))
           (cst=-lst (cdr x) (cdr y))))))

(defmacro eq-op (x y)

; This test must change if we start allowing LAMBDAs as operators.

  `(eq ,x ,y))

(defun op-search-bucket (op args lst)

; Here op is a function symbol and lst is a tail of a bucket from an op-ht.
; Thus, lst is a list of elements of the form (cst op0 . args0), where args0 is
; a list of csts unless op0 is 'quote, which it is not if op0 is op.

  (cond ((null lst) nil)
        ((and (eq-op op
                     (cadr (car lst)))
              (cst=-lst args (cddr (car lst))))
         (car (car lst)))
        (t (op-search-bucket op args (cdr lst)))))

(defun op-search-bucket-2 (op arg1 arg2 lst)

; This is a version of op-search-bucket for binary functions.  This is merely
; an optimization we use for commutative operators, since we know that they are
; binary.  We could of course use this for all binary operators, but the point
; here is that for commutative operators we delay consing up the commuted
; argument list until it is necessary.  See combine-op-csts-comm.

  (cond ((null lst) nil)
        ((and (eq-op op
                     (cadr (car lst)))
              (let ((args (cddr (car lst))))
                (and (cst= arg1 (car args))
                     (cst= arg2 (cadr args)))))
         (car (car lst)))
        (t (op-search-bucket-2 op arg1 arg2 (cdr lst)))))

(defun op-search-bucket-if (arg1 arg2 arg3 lst)

; This is a version of op-search-bucket that does not require us to cons up the
; arguments into a list, used in chk-memo-if.  This is merely an optimization
; we use since IF is such a common operation.

  (cond ((null lst) nil)
        ((and (eq-op 'if
                     (cadr (car lst)))
              (let ((args (cddr (car lst))))
                (and (cst= arg1 (car args))
                     (cst= arg2 (cadr args))
                     (cst= arg3 (caddr args)))))
         (car (car lst)))
        (t (op-search-bucket-if arg1 arg2 arg3 (cdr lst)))))

(defun chk-memo (op-code op args op-ht)

; If <op,arg1,arg2,...> has an entry in op-ht, return 0 and the entry.
; Otherwise, return the hash index for <op-code,arg1,arg2,...> (simply to avoid
; its recomputation) and NIL.  Entries are of the form (result op . args).  We
; return the hash index as the first value so that we can avoid boxing up a
; fixnum for it in GCL.

  (declare (type (signed-byte 30) op-code))
  (the-mv
   2
   (signed-byte 30)
   (let ((n (op-hash-index op-code args)))
     (declare (type (signed-byte 30) n))
     (let ((ans (op-search-bucket op args (aref1 'op-ht op-ht n))))
       (cond (ans (mvf 0 ans))
             (t (mvf n nil)))))))

(defun chk-memo-2 (op-code op arg1 arg2 op-ht)

; This is merely an optimization of chk-memo for the case where the operator is
; binary, in particularly for commutative operators; see the comment in
; op-search-bucket-2.

  (declare (type (signed-byte 30) op-code))
  (the-mv
   2
   (signed-byte 30)
   (let ((n (op-hash-index-2 op-code arg1 arg2)))
     (declare (type (signed-byte 30) n))
     (let ((ans (op-search-bucket-2 op arg1 arg2 (aref1 'op-ht op-ht n))))
       (cond (ans (mvf 0 ans))
             (t (mvf n nil)))))))

(defun chk-memo-if (arg1 arg2 arg3 op-ht)

; This is merely an optimization of chk-memo for the case where the operator is
; if, which is likely very common.  Note that by treating this special case as
; we do, we avoid consing up the list of arguments in some cases; see
; combine-if-csts.

  (the-mv
   2
   (signed-byte 30)
   (let ((n (op-hash-index-if arg1 arg2 arg3)))
     (declare (type (signed-byte 30) n))
     (let ((ans (op-search-bucket-if arg1 arg2 arg3 (aref1 'op-ht op-ht n))))
       (cond (ans (mvf 0 ans))
             (t (mvf n nil)))))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;; IV. HASH OPERATIONS: QUOTEPS ;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defmacro half-hash-size ()
  (floor (hash-size) 2))

(defmacro fourth-hash-size ()
  (floor (hash-size) 4))

(defun op-hash-index-string (index acc string)
  (declare (type (signed-byte 30) index acc))
  (the-fixnum
   (cond
    ((= index 0) acc)
    (t (let ((index (1- (the-fixnum index))))
         (declare (type (signed-byte 30) index))
         (op-hash-index-string
          index
          (logandf (hash-size)
                   (+f acc (char-code (char string index))))
          string))))))

(defun op-hash-index-evg (evg)
  (the-fixnum
   (cond
    ((integerp evg)
     (logandf (hash-size) evg))
    ((rationalp evg)
     (logandf (hash-size)
              (+ (numerator evg)
                 (* 17 (denominator evg)))))
    ((acl2-numberp evg)
     (logandf (hash-size)
              (+f (op-hash-index-evg (realpart evg))
                  (op-hash-index-evg (imagpart evg)))))
    ((characterp evg)
     (+f (fourth-hash-size)
         (char-code evg)))
    ((symbolp evg)
     (logandf (hash-size)

; WARNING:  Here we assume that (* 19 (hash-size)) is a fixnum.  We know it is
; because the hash index is at most (hash-size), which is well under
; (fixnum-bound).

              (*f 19 (op-hash-index-evg (symbol-name evg)))))
    ((stringp evg)
     (the-fixnum
      (op-hash-index-string (the-fixnum! (length evg) 'bdd)
                            (half-hash-size) evg)))
    (t ;cons
     (logandf (hash-size)
              (+f (op-hash-index-evg (car evg))
                  (*f 2 (op-hash-index-evg (cdr evg)))))))))

(defun op-search-bucket-quote (evg bucket)
  (cond ((null bucket) nil)
        ((and (eq-op 'quote
                     (cadr (car bucket)))
              (equal evg (caddr (car bucket))))
         (car (car bucket)))
        (t (op-search-bucket-quote evg (cdr bucket)))))

(defun chk-memo-quotep (term op-ht)
  (the-mv
   2
   (signed-byte 30)
   (let ((n (op-hash-index-evg (cadr term))))
     (declare (type (signed-byte 30) n))
     (let ((ans (op-search-bucket-quote
                 (cadr term)
                 (aref1 'op-ht op-ht n))))

; One might think that the calls of the-fixnum just below are not necessary,
; but in fact they do appear to produce better compiled code in GCL.

       (cond (ans (mvf 0 ans))
             (t (mvf n nil)))))))

(defun bdd-quotep (term op-ht mx-id)
  (declare (type (signed-byte 30) mx-id))
  (the-mv
   3
   (signed-byte 30)
   (cond
    ((equal term *t*)
     (mvf mx-id *cst-t* op-ht))
    ((equal term *nil*)
     (mvf mx-id *cst-nil* op-ht))
    (t
     (mv-let (hash-index ans)
             (chk-memo-quotep term op-ht)
             (declare (type (signed-byte 30) hash-index))
             (cond
              (ans (mvf mx-id ans op-ht))
              (t (let ((new-mx-id (1+mx-id mx-id)))
                   (declare (type (signed-byte 30) new-mx-id))
                   (let ((new-cst (make-leaf-cst
                                   new-mx-id
                                   term
                                   nil)))
                     (mvf new-mx-id
                          new-cst
                          (aset1 'op-ht op-ht hash-index
                                 (cons

; The following is the same as (list new-cst 'quote (cadr term)), but saves a
; cons.

                                  (cons new-cst term)
                                  (aref1 'op-ht op-ht hash-index)))))))))))))

(defmacro bdd-quotep+ (term op-ht if-ht mx-id ttree)
  `(mv-let (mx-id cst op-ht)
           (bdd-quotep ,term ,op-ht ,mx-id)
           (declare (type (signed-byte 30) mx-id))
           (mvf mx-id cst op-ht ,if-ht ,ttree)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;; V. BDD RULES AND ONE-WAY UNIFIER ;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; We could just use the rewrite-rule data structures already existing in the
; ACL2 world.  However, we suspect that it is a good idea, in order to support
; computationally intensive bdd computations, to avoid having to look at the
; enabled structure or dig deep into a rewrite rule in order to find the
; various fields we need.  In fact, we want to have the lhs available as
; quickly as possible, since that field is used the most.

(defrec bdd-rule
  (lhs rhs . rune)
  t)

(defun rewrite-rule-to-bdd-rule (lemma)
  (make bdd-rule
        :lhs  (access rewrite-rule lemma :lhs)
        :rhs  (access rewrite-rule lemma :rhs)
        :rune (access rewrite-rule lemma :rune)))

(defun bdd-rules-alist1
  (recp lemmas ens all-fns nondef-rules def-rules new-fns)

; This function returns lists of definitional and non-definitional bdd-rules
; corresponding to the lemmas of a given function symbol.  The arguments are as
; follows.

; recp:    True when the top-level function symbol for the lemmas is recursive
; lemmas:  The rewrite-rule structures that we want to convert to bdd-rules
; ens:     The current enabled structure
; all-fns: List of all function symbols already encountered in bdd rules built
; nondef-rules:  Bdd-rules accumulated so far not from definition rules
; def-rules:     Bdd-rules accumulated so far from definition rules
; new-fns: List of function symbols to be added to all-fns (an accumulator)

; At this point, we do not support backchaining:  that is, we assume that each
; rule has :hyps field of NIL.  We also do not allow free variables in the
; :rhs, and we require :lhs to be a function symbol call.  We also require a
; null loop-stopper (:heuristic-info for subclass 'backchain), rather than
; attempting to control looping during the bdd computation.  Perhaps some of
; these restrictions can be removed after some thought and additional
; implementation work.

; We require that the :rhs only contain function symbols that are known in the
; op-alist.  In order to enforce this requirement, we simply pass back two
; values:  a list of new function symbols to consider (i.e., ones not in
; all-fns that occur in :rhs fields) and the list of bdd-rules.

; As noted in a comment in bdd-rules-alist, the lists of lemmas returned by
; this function need to be reversed, because they have the oldest rules at the
; front.  That could easily be changed, though the natural way to do this would
; presumably render this function non-tail recursive.  At this point the issue
; seems sufficiently minor that we are satisfied to leave things this way.

  (cond
   ((endp lemmas) (mv new-fns nondef-rules def-rules))
   (t (let ((subclass (access rewrite-rule (car lemmas) :subclass)))
        (cond
         ((and (eq (access rewrite-rule (car lemmas) :equiv) 'equal)
               (enabled-numep (access rewrite-rule (car lemmas) :nume) ens)
               (case subclass
                     (definition
                       (and (null recp)
                            (null (access rewrite-rule (car lemmas) :hyps))
                            (subsetp-eq
                             (all-vars (access rewrite-rule (car lemmas)
                                               :rhs))
                             (all-vars (access rewrite-rule (car lemmas)
                                               :lhs)))))
                     (abbreviation
                      (subsetp-eq
                       (all-vars (access rewrite-rule (car lemmas) :rhs))
                       (all-vars (access rewrite-rule (car lemmas) :lhs))))
                     (backchain
                      (and (null (access rewrite-rule (car lemmas)
                                         :hyps))
                           (null (access rewrite-rule (car lemmas)
                                         :heuristic-info))
                           (subsetp-eq
                            (all-vars (access rewrite-rule (car lemmas)
                                              :rhs))
                            (all-vars (access rewrite-rule (car lemmas)
                                              :lhs)))))
                     (otherwise nil)))
          (bdd-rules-alist1 recp (cdr lemmas) ens all-fns
            (if (eq subclass 'definition)
                nondef-rules
              (cons (rewrite-rule-to-bdd-rule (car lemmas)) nondef-rules))
            (if (eq subclass 'definition)
                (cons (rewrite-rule-to-bdd-rule (car lemmas)) def-rules)
              def-rules)
            (union-eq (set-difference-eq
                       (all-fnnames (access rewrite-rule (car lemmas) :rhs))
                       all-fns)
                      new-fns)))
         (t (bdd-rules-alist1 recp (cdr lemmas) ens all-fns
              nondef-rules def-rules new-fns)))))))

(defun extra-rules-for-bdds (fn wrld)

; We include certain trivial rewrite rules regardless of whether there are
; explicit rewrite rules that corrrespond to them.

  (cond
   ((eq fn 'equal)
    (list (make rewrite-rule
                :nume nil :hyps nil :equiv 'equal

; Rockwell Addition:  I have totally stripped out all vestiges of the
; aborted attempt to implement :OUTSIDE-IN rewrite rules.  I won't comment
; on subsequent differences of this sort.

                :subclass 'backchain
                :heuristic-info nil
                :backchain-limit-lst *initial-default-backchain-limit*
                :rune *fake-rune-for-anonymous-enabled-rule*
                :lhs (fcons-term* 'equal *nil* 'x)
                :var-info t
                :rhs (fcons-term* 'if 'x *nil* *t*))
          (make rewrite-rule
                :nume nil :hyps nil :equiv 'equal
                :subclass 'backchain
                :heuristic-info nil
                :backchain-limit-lst *initial-default-backchain-limit*
                :rune *fake-rune-for-anonymous-enabled-rule*
                :lhs (fcons-term* 'equal 'x *nil*)
                :var-info t
                :rhs (fcons-term* 'if 'x *nil* *t*))))
   ((equivalence-relationp fn wrld)

; We do not need to include reflexivity when fn is 'equal, because it is
; hardwired into the definition of combine-op-csts.

    (list (make rewrite-rule
                :nume nil :hyps nil :equiv 'equal
                :subclass 'abbreviation
                :heuristic-info nil
                :backchain-limit-lst *initial-default-backchain-limit*
                :rune (equivalence-rune fn wrld)
                :lhs (fcons-term* fn 'x 'x)
                :var-info t
                :rhs *t*)))
   ((eq fn 'mv-nth)
    (list (make rewrite-rule
                :nume nil :hyps nil :equiv 'equal
                :subclass 'backchain
                :heuristic-info nil
                :backchain-limit-lst *initial-default-backchain-limit*
                :rune (fn-rune-nume 'mv-nth nil nil wrld)
                :lhs (fcons-term* 'mv-nth 'n (fcons-term* 'cons 'x 'y))
                :var-info t

; (if (zp n) x (mv-nth (- n 1) y))

                :rhs (fcons-term* 'if
                                  (fcons-term* 'zp 'n)
                                  'x
                                  (fcons-term* 'mv-nth
                                               (fcons-term* 'binary-+
                                                            'n
                                                            (kwote -1))
                                               'y)))))

   (t nil)))

(defun bdd-rules-alist (fns all-fns bdd-rules-alist ens wrld)

; Call this with a list fns of function symbols that does not include 'if, and
; all-fns the result of adding 'if to that list.  The parameter bdd-rules-alist
; is the accumulator, initially nil.

; WARNING:  Be sure to modify this function to account for hypotheses if we
; implement conditional rewriting with BDDs.

; Invariant:  fns is a subset of all-fns.  This is important for not just
; termination, but in fact to guarantee that the same function (car fns) is
; never processed twice by bdd-rules-alist1.

; NOTE:  Do we store entries when there are no rules, or not?  Not.  Suppose
; there are p elements of fns with a non-nil set of rules and q elements of fns
; with a nil set of rules.  Then the average number of CDRs required for lookup
; (assuming each function symbol is looked up the same number of times) is
; roughly (p+q)/2 if we store entries for nil sets of rules; and if we don't,
; it's:  [1/(p+q)]*(p*p/2 + q*p), which equals [p/2(p+q)]*(p + 2q).
; Now we can see that we're better off the second way, not storing nil entries:

; p+q >= [p/(p+q)]*(p + 2q) ?
; (p+q)^2 >= p^2 + 2pq ?
; q^2 >= 0 !
; Yes, in fact the inequality is strict if q > 0.

  (cond
   ((endp fns) (mv all-fns bdd-rules-alist))
   (t (mv-let (new-fns nondef-rules def-rules)
              (bdd-rules-alist1
               (recursivep (car fns) wrld)
               (append (getprop
                        (car fns) 'lemmas nil 'current-acl2-world wrld)
                       (extra-rules-for-bdds (car fns) wrld))
               ens
               (cons (car fns) all-fns)
               nil
               nil
               nil)
              (cond ((or def-rules nondef-rules)
                     (bdd-rules-alist
                       (append new-fns (cdr fns))
                       (append new-fns all-fns)
                       (cons (cons (car fns)

; The calls of reverse below ensure that rules will be tried in the appropriate
; order, i.e., most recent ones first.  See the comment in bdd-rules-alist1.

                                   (cons (reverse nondef-rules)
                                         (reverse def-rules)))
                             bdd-rules-alist)
                       ens
                       wrld))

; Otherwise do not store an entry for (car fns) in bdd-rules-alist, as argued
; in the comment above.

                    (t (bdd-rules-alist (cdr fns) all-fns bdd-rules-alist ens
                                        wrld)))))))

; We now adapt ACL2's one-way-unifier for terms to the realms of csts.

(defmacro one-way-unify1-cst-2 (mx-id p1 p2 cst1 cst2 alist op-ht)
  `(mv-let (mx-id ans alist1 op-ht)
           (one-way-unify1-cst ,mx-id ,p1 ,cst1 ,alist ,op-ht)
           (declare (type (signed-byte 30) mx-id))
           (cond
            (ans
             (mv-let (mx-id ans alist2 op-ht)
                     (one-way-unify1-cst mx-id ,p2 ,cst2 alist1 op-ht)
                     (declare (type (signed-byte 30) mx-id))
                     (cond
                      (ans (mvf mx-id t alist2 op-ht))
                      (t (mvf mx-id nil ,alist op-ht)))))
            (t (mvf mx-id nil ,alist op-ht)))))

(defmacro one-way-unify1-cst-3 (mx-id p1 p2 p3 cst1 cst2 cst3 alist op-ht)
  `(mv-let (mx-id ans alist2 op-ht)
           (one-way-unify1-cst-2 ,mx-id ,p1 ,p2 ,cst1 ,cst2 ,alist ,op-ht)
           (declare (type (signed-byte 30) mx-id))
           (cond
            (ans
             (mv-let (mx-id ans alist3 op-ht)
                     (one-way-unify1-cst mx-id ,p3 ,cst3 alist2 op-ht)
                     (declare (type (signed-byte 30) mx-id))
                     (cond
                      (ans (mvf mx-id t alist3 op-ht))
                      (t (mvf mx-id nil ,alist op-ht)))))
            (t (mvf mx-id nil ,alist op-ht)))))

(mutual-recursion

; The following functions are adapted from one-way-unify1 and the like.

(defun one-way-unify1-cst (mx-id pat cst alist op-ht)
  (declare (type (signed-byte 30) mx-id))
  (the-mv
   4
   (signed-byte 30)
   (cond ((variablep pat)
          (let ((pair (assoc-eq pat alist)))
            (cond (pair (cond ((cst= (cdr pair) cst)
                               (mvf mx-id t alist op-ht))
                              (t (mvf mx-id nil alist op-ht))))
                  (t (mvf mx-id t (cons (cons pat cst) alist) op-ht)))))
         ((not (leafp cst))
          (cond
           ((fquotep pat)
            (mvf mx-id nil alist op-ht))
           ((eq (ffn-symb pat) 'if)
            (one-way-unify1-cst-3 mx-id
                                  (fargn pat 1) (fargn pat 2) (fargn pat 3)
                                  (tst cst) (tbr cst) (fbr cst)
                                  alist op-ht))
           (t
            (mvf mx-id nil alist op-ht))))
         (t (let ((term (trm cst)))
              (cond
               ((fquotep pat)
                (cond ((equal pat term) (mvf mx-id t alist op-ht))
                      (t (mvf mx-id nil alist op-ht))))
               ((variablep term) (mvf mx-id nil alist op-ht))
               ((fquotep term) ;term is not a term, but fquotep is ok here
                (cond ((acl2-numberp (cadr term))
                       (let ((ffn-symb (ffn-symb pat)))
                         (case ffn-symb
                               (binary-+
                                (cond ((quotep (fargn pat 1))
                                       (mv-let (mx-id cst op-ht)
                                               (bdd-quotep
                                                (kwote (- (cadr term)
                                                          (fix (cadr (fargn pat
                                                                            1)))))
                                                op-ht mx-id)
                                               (declare (type (signed-byte 30)
                                                              mx-id))
                                               (one-way-unify1-cst
                                                mx-id (fargn pat 2)
                                                cst alist op-ht)))
                                      ((quotep (fargn pat 2))
                                       (mv-let (mx-id cst op-ht)
                                               (bdd-quotep
                                                (kwote (- (cadr term)
                                                          (fix (cadr (fargn pat
                                                                            2)))))
                                                op-ht mx-id)
                                               (declare (type (signed-byte 30)
                                                              mx-id))
                                               (one-way-unify1-cst
                                                mx-id (fargn pat 1)
                                                cst alist op-ht)))
                                      (t (mvf mx-id nil alist op-ht))))
                               (binary-*
                                (cond ((and (quotep (fargn pat 1))
                                            (integerp (cadr (fargn pat 1)))
                                            (> (abs (cadr (fargn pat 1))) 1))
                                       (mv-let (mx-id cst op-ht)
                                               (bdd-quotep
                                                (kwote (/ (cadr term)
                                                          (cadr (fargn pat 1))))
                                                op-ht mx-id)
                                               (declare (type (signed-byte 30)
                                                              mx-id))
                                               (one-way-unify1-cst
                                                mx-id (fargn pat 2)
                                                cst alist op-ht)))
                                      ((and (quotep (fargn pat 2))
                                            (integerp (cadr (fargn pat 2)))
                                            (> (abs (cadr (fargn pat 2))) 1))
                                       (mv-let (mx-id cst op-ht)
                                               (bdd-quotep
                                                (kwote (/ (cadr term)
                                                          (cadr (fargn pat 2))))
                                                op-ht mx-id)
                                               (declare (type (signed-byte 30)
                                                              mx-id))
                                               (one-way-unify1-cst
                                                mx-id (fargn pat 1)
                                                cst alist op-ht)))
                                      (t (mvf mx-id nil alist op-ht))))
                               (unary-- (cond ((>= (+ (realpart (cadr term))
                                                      (imagpart (cadr term)))
                                                   0)
                                               (mvf mx-id nil alist op-ht))
                                              (t (mv-let (mx-id cst op-ht)
                                                         (bdd-quotep
                                                          (kwote (- (cadr term)))
                                                          op-ht mx-id)
                                                         (declare (type
                                                                   (signed-byte
                                                                    30)
                                                                   mx-id))
                                                         (one-way-unify1-cst
                                                          mx-id (fargn pat 1)
                                                          cst alist
                                                          op-ht)))))
                               (unary-/ (cond ((or (>= (* (cadr term)
                                                          (conjugate (cadr term)))
                                                       1)
                                                   (eql 0 (cadr term)))
                                               (mvf mx-id nil alist op-ht))
                                              (t (mv-let (mx-id cst op-ht)
                                                         (bdd-quotep
                                                          (kwote (/ (cadr term)))
                                                          op-ht mx-id)
                                                         (declare (type
                                                                   (signed-byte
                                                                    30)
                                                                   mx-id))
                                                         (one-way-unify1-cst
                                                          mx-id (fargn pat 1)
                                                          cst alist op-ht)))))
                               (otherwise (mvf mx-id nil alist op-ht)))))

; We try to avoid some complications by avoiding intern-in-package-of-symbol
; and coerce for now.  We are not aware of any reason why they should present
; undue difficulties, however.

                      ((consp (cadr term))
                       (cond ((eq (ffn-symb pat) 'cons)

; We have to be careful with alist below so we are a no change loser.

                              (mv-let
                               (mx-id cst1 op-ht)
                               (bdd-quotep
                                (kwote (car (cadr term)))
                                op-ht mx-id)
                               (declare (type (signed-byte 30) mx-id))
                               (mv-let
                                (mx-id ans alist1 op-ht)
                                (one-way-unify1-cst
                                 mx-id (fargn pat 1) cst1 alist op-ht)
                                (declare (type (signed-byte 30) mx-id))
                                (cond
                                 (ans
                                  (mv-let
                                   (mx-id cst2 op-ht)
                                   (bdd-quotep
                                    (kwote (cdr (cadr term)))
                                    op-ht mx-id)
                                   (declare (type (signed-byte 30) mx-id))
                                   (mv-let (mx-id ans alist2 op-ht)
                                           (one-way-unify1-cst
                                            mx-id (fargn pat 2) cst2 alist1
                                            op-ht)
                                           (declare (type (signed-byte 30)
                                                          mx-id))
                                           (cond (ans (mvf mx-id t alist2
                                                           op-ht))
                                                 (t (mvf mx-id nil alist
                                                         op-ht))))))
                                 (t (mvf mx-id nil alist op-ht))))))
                             (t (mvf mx-id nil alist op-ht))))
                      (t (mvf mx-id nil alist op-ht))))
               ((eq (ffn-symb pat) (ffn-symb term))

; Note:  We do not allow lambda expressions at this point.  If that changes,
; then we should consider that case too.

                (cond ((eq (ffn-symb pat) 'equal)
                       (one-way-unify1-cst-equal mx-id
                                                 (fargn pat 1) (fargn pat 2)
                                                 (fargn term 1) (fargn term 2)
                                                 alist op-ht))
                      (t (mv-let (mx-id ans alist1 op-ht)
                                 (one-way-unify1-cst-lst mx-id
                                                         (fargs pat)
                                                         (fargs term)
                                                         alist op-ht)
                                 (declare (type (signed-byte 30) mx-id))
                                 (cond (ans (mvf mx-id t alist1 op-ht))
                                       (t (mvf mx-id nil alist op-ht)))))))
               (t (mvf mx-id nil alist op-ht))))))))

(defun one-way-unify1-cst-lst (mx-id pl cstl alist op-ht)

; This function is NOT a No Change Loser.

  (declare (type (signed-byte 30) mx-id))
  (the-mv
   4
   (signed-byte 30)
   (cond ((null pl) (mvf mx-id t alist op-ht))
         (t (mv-let (mx-id ans alist op-ht)
                    (one-way-unify1-cst mx-id (car pl) (car cstl) alist op-ht)
                    (declare (type (signed-byte 30) mx-id))
                    (cond
                     (ans
                      (one-way-unify1-cst-lst mx-id (cdr pl) (cdr cstl) alist
                                              op-ht))
                     (t (mvf mx-id nil alist op-ht))))))))

(defun one-way-unify1-cst-equal (mx-id pat1 pat2 cst1 cst2 alist op-ht)
  (declare (type (signed-byte 30) mx-id))
  (the-mv
   4
   (signed-byte 30)
   (mv-let (mx-id ans alist op-ht)
           (one-way-unify1-cst-2 mx-id pat1 pat2 cst1 cst2 alist op-ht)
           (declare (type (signed-byte 30) mx-id))
           (cond
            (ans (mvf mx-id t alist op-ht))
            (t (one-way-unify1-cst-2 mx-id pat2 pat1 cst1 cst2 alist
                                     op-ht))))))
)

(defun some-one-way-unify-cst-lst (cst-lst rules op-ht mx-id ttree)
  (declare (type (signed-byte 30) mx-id))
  (the-mv
   6
   (signed-byte 30)
   (cond
    ((endp rules)
     (mvf mx-id nil nil nil op-ht ttree))
    (t (mv-let (mx-id ans alist op-ht)
               (one-way-unify1-cst-lst
                mx-id (fargs (access bdd-rule (car rules) :lhs))
                cst-lst nil op-ht)
               (declare (type (signed-byte 30) mx-id))
               (cond
                (ans (mvf mx-id t
                          (access bdd-rule (car rules) :rhs)
                          alist op-ht
                         (push-lemma (access bdd-rule (car rules) :rune)
                                     ttree)))
                (t (some-one-way-unify-cst-lst cst-lst (cdr rules)
                                               op-ht mx-id ttree))))))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;; VI. SOME INTERFACE UTILITIES
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; We will ignore declaration opportunities in this section, especially for
; declaring mx-id to be a fixnum, because efficiency is a minor issue here.

(defun leaf-cst-list (lst bool-vars acc mx-id)

; Here lst is a list of variables from the input term.  Returns a list of leaf
; csts for those variables, i.e., elements of the form (unique-id variable
; bool), where if bool is t then variable is known to be Boolean.

  (cond
   ((endp lst) (mv mx-id acc))
   (t (mv-let (mx-id acc)
              (cond ((assoc-eq (car lst) acc)
                     (mv mx-id acc))
                    (t (let ((new-mx-id (1+mx-id mx-id)))
                         (mv new-mx-id
                             (cons (make-leaf-cst
                                    new-mx-id
                                    (car lst)
                                    (member-eq (car lst) bool-vars))
                                   acc)))))
              (leaf-cst-list (cdr lst) bool-vars acc mx-id)))))

(mutual-recursion

(defun decode-cst (cst cst-array)

; This takes a cst and returns a term and an updated cst-array, whose nth entry
; is the decoding of the cst with unique id n.

  (let ((term (aref1 'cst-array cst-array (unique-id cst))))
    (cond
     (term (mv term cst-array))
     ((leafp cst)
      (cond
       ((or (variablep (trm cst))
            (fquotep (trm cst)))
        (mv (trm cst) cst-array))
       (t (mv-let (args cst-array)
                  (decode-cst-lst (fargs (trm cst)) cst-array)
                  (let ((x (cons-term (ffn-symb (trm cst))
                                      args)))
                    (mv x
                        (aset1 'cst-array
                               cst-array
                               (unique-id cst)
                               x)))))))
     (t (mv-let
         (tst cst-array)
         (decode-cst (tst cst) cst-array)
         (mv-let
          (tbr cst-array)
          (decode-cst (tbr cst) cst-array)
          (mv-let
           (fbr cst-array)
           (decode-cst (fbr cst) cst-array)
           (let ((x (mcons-term* 'if tst tbr fbr)))
             (mv x
                 (aset1 'cst-array
                        cst-array
                        (unique-id cst)
                        x))))))))))

(defun decode-cst-lst (cst-lst cst-array)
  (cond
   ((endp cst-lst)
    (mv nil cst-array))
   (t (mv-let (first cst-array)
              (decode-cst (car cst-lst) cst-array)
              (mv-let (rest cst-array)
                      (decode-cst-lst (cdr cst-lst) cst-array)
                      (mv (cons first rest)
                          cst-array))))))
)

(defun decode-cst-alist1 (alist cst-array)
  (cond
   ((endp alist) (mv nil cst-array))
   (t (mv-let (term cst-array)
              (decode-cst (cdar alist) cst-array)
              (mv-let (rest cst-array)
                      (decode-cst-alist1 (cdr alist) cst-array)
                      (mv (cons (list (caar alist)
                                      term)
                                rest)
                          cst-array))))))

(defun decode-cst-alist (cst-alist cst-array)
  (mv-let (alist cst-array)
          (decode-cst-alist1 cst-alist cst-array)
          (declare (ignore cst-array))
          alist))

(defun leaf-cst-list-array (mx-id)
  (let ((dim (1+ mx-id)))
    (compress1 'cst-array
               `((:header :dimensions (,dim)
                          :maximum-length ,(+ 10 dim)
                          :default nil)))))

(defconst *some-non-nil-value* "Some non-nil value")

(defun falsifying-assignment1 (cst acc cst-array)

; Returns a list of doublets (var bool) that provide an environment for
; falsifying the given cst.  Also returns a new cst-array; we have to do that
; so that we always pass in the "current" cst-array, in order to avoid slow
; array access.

  (cond
   ((cst-nilp cst)
    (mv acc cst-array))
   ((quotep (trm cst))
    (mv (er hard 'falsifying-assignment1
            "Tried to falsify ~x0!"
            (trm cst))
        cst-array))
   ((leafp cst)
    (mv-let (term cst-array)
            (decode-cst cst cst-array)
            (mv (cons (list term nil) acc)
                cst-array)))
   ((cst-nonnilp (tbr cst))
    (mv-let (term cst-array)
            (decode-cst (tst cst) cst-array)
            (falsifying-assignment1 (fbr cst)
                                    (cons (list term nil)
                                          acc)
                                    cst-array)))
   (t
    (mv-let (term cst-array)
            (decode-cst (tst cst) cst-array)
            (falsifying-assignment1 (tbr cst)
                                    (cons (list term (if (cst-boolp (tst cst))
                                                         t
                                                       *some-non-nil-value*))
                                          acc)
                                    cst-array)))))

(defun falsifying-assignment (cst mx-id)
  (mv-let (asst cst-array)
          (falsifying-assignment1 cst nil (leaf-cst-list-array mx-id))
          (declare (ignore cst-array))
          (reverse asst)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;; VII. MAIN ALGORITHM ;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defun make-if (mx-id n op args x y z op-ht if-ht bdd-constructors)

; This function returns either

; (mvf mx-id cst op-ht if-ht)

; or (culling this from an "erroneous" return of make-if-cst below)

; (mvf mx-id fmt-string fmt-alist bad-cst)

; Intuitively, this function makes a cst representing (IF x y z).  But
; we know that this is the answer to the merge op(args) and we
; know that n is the hash index of <op,arg1,...>.  We know
; <op,arg1,...> is not in the op-ht.  We first look in the if-ht to
; see if (IF x y z) is there.  If so, we return it.  If not, we build
; an appropriate one, assigning the next unique id, which is (1+
; mx-id), and add it to the if-ht.  In any case, before returning, we
; store the returned cst as the answer for op(arg1,...) in op-ht.  We
; thus have to return four results: the new mx-id, the cst, and the
; two hash arrays.

  (declare (type (signed-byte 30) n mx-id))
  (the-mv
   4
   (signed-byte 30)
   (cond ((cst= y z) (mvf mx-id
                          y

; The following aset1 was added after Moore's first presentation of this
; work.  Its absence was discovered during a code-walk with Jim
; Bitner.  The times improved slightly on most examples, except mul08
; where we lost a few more seconds.  The times shown in
; ~moore/text/pc-hacking.mss -- the most recent version of a talk on
; this work -- have been updated to show the performance of this
; version of the code.

                          (aset1 'op-ht op-ht n
                                 (cons (list* y op args)
                                       (aref1 'op-ht op-ht n)))
                          if-ht))
         (t (let ((m (if-hash-index x y z)))
              (declare (type (signed-byte 30) m))
              (let* ((bucket (aref1 'if-ht if-ht m))
                     (old-if (if-search-bucket x y z bucket)))
                (cond (old-if (mvf mx-id
                                   old-if
                                   (aset1 'op-ht op-ht n
                                          (cons (list* old-if op args)
                                                (aref1 'op-ht op-ht n)))
                                   if-ht))
                      ((and (cst-tp y)
                            (cst-nilp z)
                            (cst-boolp x))
                       (mvf mx-id
                            x
                            (aset1 'op-ht op-ht n
                                   (cons (list* x op args)
                                         (aref1 'op-ht op-ht n)))
                            if-ht))
                      (t (let ((mx-id (1+mx-id mx-id)))
                           (declare (type (signed-byte 30) mx-id))
                           (mv-let
                            (erp new-if)
                            (make-if-cst mx-id x y z bdd-constructors)
                            (cond
                             (erp (mvf mx-id
                                       (car erp) ;fmt-string
                                       (cdr erp) ;fmt-alist
                                       new-if ;bad cst
                                       ))
                             (t
                              (mvf mx-id
                                   new-if
                                   (aset1 'op-ht op-ht n
                                          (cons (list* new-if op args)
                                                (aref1 'op-ht op-ht n)))
                                   (aset1 'if-ht if-ht m
                                          (cons new-if bucket)))))))))))))))

(defun make-if-no-memo (mx-id x y z op-ht if-ht bdd-constructors)

; Same as make-if, except that we do not change op-ht, and we assume that y and
; z are already known to be distinct.

  (declare (type (signed-byte 30) mx-id))
  (the-mv
   4
   (signed-byte 30)
   (let ((m (if-hash-index x y z)))
     (declare (type (signed-byte 30) m))
     (let* ((bucket (aref1 'if-ht if-ht m))
            (old-if (if-search-bucket x y z bucket)))
       (cond (old-if (mvf mx-id old-if op-ht if-ht))
             ((and (cst-tp y)
                   (cst-nilp z)
                   (cst-boolp x))
              (mvf mx-id x op-ht if-ht))
             (t (let ((mx-id (1+mx-id mx-id)))
                  (declare (type (signed-byte 30) mx-id))
                  (mv-let
                   (erp new-if)
                   (make-if-cst mx-id x y z bdd-constructors)
                   (cond
                    (erp (mvf mx-id
                              (car erp) ;fmt-string
                              (cdr erp) ;fmt-alist
                              new-if ;bad cst
                              ))
                    (t
                     (mvf mx-id new-if op-ht
                          (aset1 'if-ht if-ht m
                                 (cons new-if bucket)))))))))))))

(defmacro split-var (cst)

; The variable to split on from cst.  If cst is a leaf, then we only split on
; it if it is a cst-varp (i.e., not the representation of T or NIL) and is
; known to be Boolean.

  `(if (leafp ,cst)
       (if (and (cst-varp ,cst)
                (cst-boolp ,cst))
           ,cst
         nil)
     (tst ,cst)))

(defun min-var (acc args)

; Args is a list of csts.  We return nil if there is no variable to split on.
; Otherwise, we return the leaf cst with the smallest unique-id.  Call this
; with acc = nil.

  (declare (xargs :measure (acl2-count args)))
  (if (endp args)
      acc
    (let ((var (split-var (car args))))
      (if (null var)
          (min-var acc (cdr args))
        (min-var (cond
                  ((null acc)
                   var)
                  ((< (unique-id var) (unique-id acc))
                   var)
                  (t acc))
                 (cdr args))))))

(defun combine-op-csts1 (var-id args)

; Args is a list of csts, and var-id is the unique-id of a term that is not
; necessarily Boolean-valued.  We return (mv true-branch-args
; false-branch-args), where under the assumption that var-id is the unique id
; of a term that is not (semantically) nil, args represents the same list of
; terms as true-branch-args; and under the assumption that var-id is the unique
; id of a term that (semantically) equals nil, args represents the same list of
; terms as false-branch-args.

  (declare (type (signed-byte 30) var-id))
  (if (endp args)
      (mv nil nil)
    (mv-let (x y)
            (combine-op-csts1 var-id (cdr args))
            (cond
             ((leafp (car args))
              (if (and (= (the-fixnum var-id) (unique-id (car args)))

; Even though we are splitting on var-id, we need to know that it is the unique
; id of a boolean variable in order to simplify as shown below.  Note that
; var-id need only be the unique-id of a Boolean cst when split-var returns it
; by virtue of its being a leaf; it could be non-Boolean if split-var
; encounters it as a test.

                       (cst-boolp (car args)))
                  (mv (cons *cst-t* x) (cons *cst-nil* y))
                (mv (cons (car args) x) (cons (car args) y))))
             (t
              (if (= (the-fixnum var-id) (unique-id (tst (car args))))
                  (mv (cons (tbr (car args)) x) (cons (fbr (car args)) y))
                (mv (cons (car args) x) (cons (car args) y))))))))

(defun bool-flg (args mask)

; Checks that for each "bit" set in mask, the corresponding arg in args is
; known to be Boolean.  In the case that mask is (typically) from a type
; prescription, this allows us to conclude, assuming that the given function
; symbol's base type is Boolean, then the application of that function to args
; is Boolean.

; If this function returns a non-nil value, then that value is a type
; prescription rune.

  (cond
   ((endp args)

; Then mask is a type prescription rune.

    mask)
   ((car mask)
    (and (cst-boolp (car args))
         (bool-flg (cdr args) (cdr mask))))
   (t (bool-flg (cdr args) (cdr mask)))))

(defun some-bdd-constructorp (args bdd-constructors)
  (cond
   ((endp args) nil)
   (t (or (and (leafp (car args))
               (bdd-constructor-trm-p (trm (car args)) bdd-constructors))
          (some-bdd-constructorp (cdr args) bdd-constructors)))))

(defun combine-op-csts-simple
  (hash-index op mask args op-ht if-ht mx-id bdd-constructors ttree)

; Make a new leaf-cst for (op . args).  Note:  this function returns an "error"
; in the sense described in the bdd nest if the call attempts to build a
; non-bdd-constructor node when some argument is a bdd-constructor.  Pass in
; bdd-constructors = nil if no such attempt is possible; otherwise, we know
; that op is not a member of bdd-constructors.

  (declare (type (signed-byte 30) hash-index mx-id))
  (the-mv
   5
   (signed-byte 30)
   (let ((new-mx-id (1+mx-id mx-id))
         (rune (and mask

; If mask is non-nil, we guarantee that op corresponds to a function whose type
; is Boolean modulo that mask (for its type prescription).

                    (bool-flg args mask))))
     (declare (type (signed-byte 30) new-mx-id))
     (let ((new-cst (make-leaf-cst
                     new-mx-id
                     (cons op args)
                     rune)))
       (cond
        ((and bdd-constructors

; We presumably know that (not (member-eq op bdd-constructors)).

              (some-bdd-constructorp args bdd-constructors))
         (bdd-error
          new-mx-id
          "Attempted to create ~x0 node during BDD processing with an argument ~
           that is a call of ~#1~[a bdd-constructor~/CONS~], which would ~
           produce a non-BDD term (as defined in :DOC bdd-algorithm).  See ~
           :DOC show-bdd."
          (list (cons #\0 op)
                (cons #\1 (if (equal bdd-constructors '(cons))
                              1
                            0)))
          new-cst
          ttree))
        (t
         (mvf new-mx-id
              new-cst
              (aset1 'op-ht op-ht hash-index
                     (cons (list* new-cst op args)
                           (aref1 'op-ht op-ht hash-index)))
              if-ht
              (if rune (push-lemma rune ttree) ttree))))))))

(defmacro bdd-mv-let (vars form body)

; The idea here is that we want to allow functions in the bdd nest to return
; multiple values of the sort returned by the macro bdd-error.
; Combine-if-csts+ gets special treatment.

; This macro should only be used when the first var has a fixnum value.  We go
; even further by requiring that the first var be mx-id.  Whenever we write

; (bdd-mv-let vars form body)

; we assume that body returns the same number of values as does form.

; Keep this in sync with bdd-error, as indicated in a comment below.  The code
; below is the only place, as of this writing, where we update the
; bdd-call-stack.

  (declare (xargs :guard
                  (and (true-listp vars)
                       (eq (car vars) 'mx-id)
                       (< 2 (length vars))
                       (consp form)
                       (true-listp form)
                       (member-eq
                        (car form)
                        '(combine-if-csts+
                          combine-op-csts combine-op-csts+
                          combine-op-csts-guts combine-op-csts-comm
                          bdd bdd-alist bdd-list)))))
  `(mv-let ,vars
           ,form
           (declare (type (signed-byte 30) mx-id))
           (if (stringp ,(cadr vars))
               ,(cond
                 ((eq (car form) 'bdd)

; Vars is of the form returned by bdd-error:
; (mv mx-id fmt-string (cons fmt-alist bad-cst) call-stack ttree).
; We want to push the current term onto the call-stack.

                 (list 'mvf
                       (car vars)
                       (cadr vars)
                       (caddr vars)
                       (list 'cons

; Keep this in sync with the definition of bdd.  Here, (cadr form) is really
; the first argument of bdd, which should be a term, and (caddr form) is the
; second argument, which should be an alist.  The cons we generate here is the
; new value of the call-stack.

                             (list 'cons (cadr form) (caddr form))
                             (cadddr vars))
                       (cadddr (cdr vars))))
                 (t

; Then vars represents an "error", and we want to return an error of the same
; sort.  This sort will be different for combine-if-csts+ than for the other
; allowable functions (from the guard expresssion above), but no matter.

                  (cons 'mvf vars)))
             ,body)))

(defmacro combine-if-csts+ (cst1 cst2 cst3 op-ht if-ht mx-id bdd-constructors)
  `(cond
    ((cst-nilp ,cst1)
     (mvf ,mx-id ,cst3 ,op-ht ,if-ht))
    ((cst-nonnilp ,cst1)
     (mvf ,mx-id ,cst2 ,op-ht ,if-ht))
    (t (combine-if-csts ,cst1 ,cst2 ,cst3 ,op-ht ,if-ht ,mx-id
                        ,bdd-constructors))))

(defun combine-if-csts1 (var-id args)

; This function is identical to combine-op-csts1, except that the op is
; assumed to be IF.

  (declare (type (signed-byte 30) var-id))
  (mv-let (x y)
          (combine-op-csts1 var-id (cdr args))
          (cond
           ((leafp (car args))
            (if (= (the-fixnum var-id) (unique-id (car args)))
                (mv (cons *cst-t* x)
                    (cons *cst-nil* y))
              (mv (cons (car args) x)
                  (cons (car args) y))))
           (t
            (if (= (the-fixnum var-id) (unique-id (tst (car args))))
                (mv (cons (tbr (car args)) x) (cons (fbr (car args)) y))
              (mv (cons (car args) x) (cons (car args) y)))))))

(defun combine-if-csts
  (test-cst true-cst false-cst op-ht if-ht mx-id bdd-constructors)

; Similarly to the bdd nest, this function returns either

; (mvf mx-id cst op-ht if-ht)

; or

; (mvf mx-id fmt-string (cons fmt-alist bad-cst) nil).

; We assume here that test-cst is not the cst of a quotep, and that the input
; csts are really all csts (not error strings).

  (declare (type (signed-byte 30) mx-id))
  (the-mv
   4
   (signed-byte 30)
   (cond
    ((cst= true-cst false-cst)
     (mvf mx-id true-cst op-ht if-ht))
    ((cst= test-cst false-cst)
     (combine-if-csts test-cst true-cst *cst-nil* op-ht if-ht mx-id
                      bdd-constructors))
    ((and (cst= test-cst true-cst)
          (cst-boolp true-cst))
     (combine-if-csts test-cst *cst-t* false-cst op-ht if-ht mx-id
                      bdd-constructors))
    ((and (cst-nilp false-cst)
          (if (cst-tp true-cst)
              (cst-boolp test-cst)
            (cst= test-cst true-cst)))
     (mvf mx-id test-cst op-ht if-ht))
    (t (let ((true-var (split-var true-cst))
             (false-var (split-var false-cst)))
         (cond
          ((and (leafp test-cst)
                (or (null true-var)
                    (< (unique-id test-cst) (unique-id true-var)))
                (or (null false-var)
                    (< (unique-id test-cst) (unique-id false-var))))

; Then the test is the appropriate variable to split on for building a bdd, so
; we proceed to build a bdd.  Some test data suggests that it is more efficient
; to avoid op-ht memoization in this case; it makes sense anyhow that if-ht
; memoization would suffice here.  After all, very little work would be done
; inbetween looking in the op-ht and looking in the if-ht.  So, we neither
; consult nor use the op-ht when the test-cst is already in the right position.

           (make-if-no-memo mx-id test-cst true-cst false-cst op-ht if-ht
                            bdd-constructors))
          (t
           (mv-let
            (hash-index ans)
            (chk-memo-if test-cst true-cst false-cst op-ht)
            (declare (type (signed-byte 30) hash-index))
            (cond
             (ans (mvf mx-id ans op-ht if-ht))
             (t (let* ((args (list test-cst true-cst false-cst))
                       (min-var (min-var nil args)))

; Note that min-var is non-nil; otherwise split-var returns nil for each arg,
; and the previous case would apply.

                  (mv-let
                   (args1 args2)
                   (combine-if-csts1 (unique-id min-var) args)
                   (bdd-mv-let
                    (mx-id cst1 op-ht if-ht)
                    (combine-if-csts+ (car args1) (cadr args1) (caddr args1)
                                      op-ht if-ht mx-id bdd-constructors)
                    (bdd-mv-let
                     (mx-id cst2 op-ht if-ht)
                     (combine-if-csts+ (car args2) (cadr args2) (caddr args2)
                                       op-ht if-ht mx-id bdd-constructors)
                     (make-if mx-id hash-index 'if args min-var cst1 cst2
                              op-ht if-ht bdd-constructors)))))))))))))))

(defun cst-list-to-evg-list (cst-lst)
  (cond
   ((endp cst-lst) nil)
   (t (cons (cadr (trm (car cst-lst)))
            (cst-list-to-evg-list (cdr cst-lst))))))

(defun cst-quote-listp (cst-lst)
  (cond
   ((endp cst-lst) t)
   ((and (leafp (car cst-lst))
         (quotep (trm (car cst-lst))))
    (cst-quote-listp (cdr cst-lst)))
   (t nil)))

(defrec bddspv

; Bddspv stands for "BDD special variables", in analogy to pspv.  We simply
; prefer not to pass around such long argument lists.  In addition, we expect
; the code to be easier to modify this way; for example, the addition of
; bdd-constructors as a field in the bddspv avoids the need to massive
; modification of function calls.

  (op-alist bdd-rules-alist . bdd-constructors)
  t)

(defun bdd-ev-fncall
  (mx-id hash-index op mask args op-ht if-ht bdd-constructors rune ttree state)
  (declare (type (signed-byte 30) hash-index mx-id))
  (the-mv
   5
   (signed-byte 30)
   (mv-let (erp val latches)
           (ev-fncall op (cst-list-to-evg-list args) state nil nil nil)
           (declare (ignore latches))
           (cond
            (erp

; How can this case happen?  Ev-fncall can only "return an error" if there is a
; guard violation (not possible in this context) or a call of a constrained
; function (introduced locally in an encapsulate or introduced by defchoose).
; Although we have guaranteed that op is not constrained (see the code for
; op-alist), still the body of op could contain calls of constrained functions.

             (combine-op-csts-simple
              hash-index op mask args op-ht if-ht mx-id
              (and (not (member-eq op bdd-constructors))

; See the comment in combine-op-csts-simple.  The idea is that if op is in
; bdd-constructors, then we may suppress a certain check.

                   bdd-constructors)
              ttree))
            (t (bdd-quotep+ (list 'quote val) op-ht if-ht mx-id
                            (push-lemma rune ttree)))))))

(defmacro combine-op-csts+ (mx-id comm-p enabled-exec-p op-code op mask args op-ht
                                  if-ht op-bdd-rules ttree bddspv)

; In combine-op-csts-guts we want to call either combine-op-csts or
; combine-op-csts-comm, depending on the comm-p argument.  It would be slightly
; more efficient if we simply had two versions of combine-op-csts-guts:  one
; that calls combine-op-csts and one that calls combine-op-csts-comm.  But the
; savings seems quite trivial, so we devise this macro to call the appropriate
; function.

  `(if ,comm-p
       (combine-op-csts-comm ,mx-id ,comm-p ,enabled-exec-p ,op-code ,op ,mask
                             (car ,args) (cadr ,args) ,args ,op-ht ,if-ht
                             ,op-bdd-rules ,ttree ,bddspv state)
     (combine-op-csts ,mx-id ,enabled-exec-p ,op-code ,op ,mask
                      ,args ,op-ht ,if-ht ,op-bdd-rules
                      ,ttree ,bddspv state)))

(defun make-if-for-op
  (mx-id hash-index op args test-cst true-cst false-cst
         op-ht if-ht bdd-constructors)
  (declare (type (signed-byte 30) hash-index mx-id))
  (the-mv
   4
   (signed-byte 30)
   (cond
    ((cst= true-cst false-cst)

; Keep this case in sync with make-if.

     (mvf mx-id true-cst
          (aset1 'op-ht op-ht hash-index
                 (cons (list* true-cst op args)
                       (aref1 'op-ht op-ht hash-index)))
          if-ht))
    ((let ((true-split-var (split-var true-cst))
           (false-split-var (split-var false-cst))
           (test-id (unique-id test-cst)))
       (declare (type (signed-byte 30) test-id))
       (and (or (null true-split-var)
                (< test-id (unique-id true-split-var)))
            (or (null false-split-var)
                (< test-id (unique-id false-split-var)))))
     (make-if
      mx-id hash-index op args test-cst true-cst false-cst
      op-ht if-ht bdd-constructors))
    (t
     (bdd-mv-let
      (mx-id cst op-ht if-ht)
      (combine-if-csts+
       test-cst true-cst false-cst op-ht if-ht mx-id bdd-constructors)
      (mvf mx-id
           cst
           (aset1 'op-ht op-ht hash-index
                  (cons (list* cst op args)
                        (aref1 'op-ht op-ht hash-index)))
           if-ht))))))

(mutual-recursion

; All functions in this nest return either

; (mvf mx-id cst op-ht if-ht ttree)

; or (as returned by bdd-error)

; (mvf mx-id fmt-string (fmt-alist . bad-cst) call-stack ttree)

(defun combine-op-csts (mx-id enabled-exec-p op-code op mask args op-ht
                              if-ht op-bdd-rules ttree bddspv state)

; Returns a cst for (op . args).  For special treatment of the case where the
; operator is commutative, in order to avoid some consing, use
; combine-op-csts-comm.

  (declare (type (signed-byte 30) op-code mx-id))
  (the-mv
   5
   (signed-byte 30)
   (mv-let
    (hash-index ans)
    (chk-memo op-code op args op-ht)
    (declare (type (signed-byte 30) hash-index))
    (cond
     (ans (mvf mx-id ans op-ht if-ht ttree))
     ((and enabled-exec-p
           (cst-quote-listp args))
      (bdd-ev-fncall mx-id hash-index op mask args op-ht if-ht
                     (access bddspv bddspv :bdd-constructors)
                     enabled-exec-p ttree state))
     ((and (eq op 'booleanp)
           (cst-boolp (car args)))
      (mvf mx-id *cst-t* op-ht if-ht
           (push-lemma (fn-rune-nume 'booleanp nil nil (w state)) ttree)))
     (t (combine-op-csts-guts
         mx-id nil hash-index enabled-exec-p op-code op mask args op-ht
         if-ht op-bdd-rules ttree bddspv state))))))

(defun combine-op-csts-comm (mx-id comm-p enabled-exec-p op-code op mask arg1
                                   arg2 args op-ht if-ht op-bdd-rules ttree
                                   bddspv state)

; Returns a cst for (op arg1 arg2) where op is commutative and comm-p is a rune
; justifying commutativity of op.

; When args is non-nil, it is (list arg1 arg2).  The idea is to avoid making a
; cons when possible.

  (declare (type (signed-byte 30) op-code mx-id))
  (the-mv
   5
   (signed-byte 30)
   (cond
    ((and (eq op 'equal)
          (cst= arg1 arg2))

; Alternatively, we could wait until after the chk-memo-2 test below.  But in
; that case, we should make the appropriate entry in the op-ht so that we don't
; waste our time the next time the same call of 'equal arises, looking for an
; entry in op-ht that has not been (and will never be) put there.  But we
; prefer to avoid the op-ht entirely in this trivial case, and also avoid the
; computations having to do with commutativity.

; Actually, a few experiments suggest that we should have left this branch
; where it was, jut before the next branch involving 'equal.  But that makes no
; sense!  Since the performance degradation seemed to be at most a couple of
; percent, we'll leave it this way for now.

     (mvf mx-id *cst-t* op-ht if-ht
          (push-lemma (fn-rune-nume 'equal nil nil (w state)) ttree)))
    (t
     (mv-let
      (arg1 arg2 args ttree)
      (cond ((and (quotep arg2)
                  (not (quotep arg1)))
             (mv arg2 arg1 nil (push-lemma comm-p ttree)))
            ((< (unique-id arg2)
                (unique-id arg1))
             (mv arg2 arg1 nil (push-lemma comm-p ttree)))
            (t (mv arg1 arg2 args ttree)))
      (mv-let
       (hash-index ans)
       (chk-memo-2 op-code op arg1 arg2 op-ht)
       (declare (type (signed-byte 30) hash-index))
       (cond
        (ans (mvf mx-id ans op-ht if-ht ttree))
        ((and (eq op 'equal)
              (cst-tp arg1)
              (cst-boolp arg2))

; Note:  We are tempted to worry about the term (equal 'nil 't), which would
; not get caught by this case and hence, we fret, could fall through to a call
; of bdd-ev-fncall (which may be rather slower than we wish).  However, since
; the unique id is 1 for T and 2 for NIL, and we have already commuted the args
; if necessary, then there is nothing to worry about.

         (mvf mx-id arg2 op-ht if-ht
              (push-lemma (fn-rune-nume 'equal nil nil (w state)) ttree)))
        ((and enabled-exec-p
              (quotep (trm arg1))
              (quotep (trm arg2)))
         (bdd-ev-fncall mx-id hash-index op mask (or args (list arg1 arg2)) op-ht
                        if-ht
                        (access bddspv bddspv :bdd-constructors)
                        enabled-exec-p ttree state))
        (t (combine-op-csts-guts
            mx-id comm-p hash-index enabled-exec-p op-code op mask

; It is tempting to avoid consing up the following list, just in case it will
; be torn apart again.  However, this list is the one that is ultimately
; memoized, so we need it anyhow.

            (or args (list arg1 arg2))
            op-ht if-ht op-bdd-rules ttree bddspv state)))))))))

(defun combine-op-csts-guts
  (mx-id comm-p hash-index enabled-exec-p op-code op mask args op-ht if-ht
         op-bdd-rules ttree bddspv state)

; Note that op-bdd-rules is a pair of the form (bdd-lemmas . bdd-defs).  These
; are all the bdd rules that rewrite calls of the function symbol op.

  (declare (type (signed-byte 30) op-code mx-id hash-index))
  (the-mv
   5
   (signed-byte 30)
   (mv-let
    (mx-id ans rhs alist op-ht ttree)
    (some-one-way-unify-cst-lst args (car op-bdd-rules)
                                op-ht mx-id ttree)
    (declare (type (signed-byte 30) mx-id))
    (cond
     (ans
      (bdd-mv-let
       (mx-id cst op-ht if-ht ttree)
       (bdd rhs alist op-ht if-ht mx-id ttree bddspv state)

; We could consider avoiding the following memoization for the application of
; lemmas.  The "be" benchmarks suggest mixed results.

       (mvf mx-id cst
            (aset1 'op-ht op-ht hash-index
                   (cons (list* cst op args)
                         (aref1 'op-ht op-ht hash-index)))
            if-ht
            ttree)))
     (t (let ((bdd-constructors (access bddspv bddspv :bdd-constructors)))
          (cond
           ((member-eq op bdd-constructors)

; Then build a leaf node.  We do not distribute IF through calls of
; bdd-constructors.

            (combine-op-csts-simple
             hash-index op mask args op-ht if-ht mx-id nil ttree))
           (t (mv-let
               (mx-id ans rhs alist op-ht ttree)
               (some-one-way-unify-cst-lst args (cdr op-bdd-rules)
                                           op-ht mx-id ttree)
               (declare (type (signed-byte 30) mx-id))
               (cond
                (ans
                 (bdd-mv-let
                  (mx-id cst op-ht if-ht ttree)
                  (bdd rhs alist op-ht if-ht mx-id ttree bddspv state)

; We could consider avoiding the following memoization for the application of
; definitions.  The "be" benchmarks suggest mixed results.

                  (mvf mx-id cst
                       (aset1 'op-ht op-ht hash-index
                              (cons (list* cst op args)
                                    (aref1 'op-ht op-ht hash-index)))
                       if-ht ttree)))
                (t
                 (let ((min-var (min-var nil args)))

; There is certainly a potential here for more case splitting than me might
; desire.  For, notice that min-var could be non-nil even though all of the
; args are leaves, because split-var (called by min-var) is happy to return a
; leaf that is known to be Boolean (and not t or nil).  However, our current
; model of how OBDDs will be used suggests that we rarely get to this part of
; the code anyhow, because operators not belonging to bdd-constructors will
; have been expanded away using rewrite rules or definitions.  So, we see no
; need at this point to take pains to avoid case splitting.  Instead, we prefer
; to err on the side of "completeness".

                   (cond
                    ((null min-var)
                     (combine-op-csts-simple
                      hash-index op mask args op-ht if-ht mx-id

; At this point we know that op is a not a member of bdd-constructors.  So we
; must pass in bdd-constructors here rather than nil.  See the comment in
; combine-op-csts-simple.

                      bdd-constructors ttree))
                    (t (mv-let
                        (args1 args2)
                        (combine-op-csts1 (unique-id min-var) args)

; Collect args1 for the true branch and args2 for the false branch.  For
; example, (foo x0 (if min-var x1 x2) (if min-var x3 x4)) yields
; (mv (list x0 x1 x3) (list x0 x2 x4)).  More reifically:

; (combine-op-csts1 3 '((4 x0 t)
;                       (9  (3 y t) t (5 x1 t) . (6 x2 t))
;                       (10 (3 y t) t (7 x3 t) . (8 x4 t))))

; is equal to

; (mv ((4 X0 T) (5 X1 T) (7 X3 T))
;     ((4 X0 T) (6 X2 T) (8 X4 T)))

                        (bdd-mv-let
                         (mx-id cst1 op-ht if-ht ttree)
                         (combine-op-csts+ mx-id comm-p enabled-exec-p
                                           op-code op mask args1 op-ht
                                           if-ht op-bdd-rules ttree bddspv)
                         (bdd-mv-let
                          (mx-id cst2 op-ht if-ht ttree)
                          (combine-op-csts+ mx-id comm-p enabled-exec-p
                                            op-code op mask args2 op-ht
                                            if-ht op-bdd-rules ttree bddspv)
                          (mv-let
                           (mx-id ans op-ht if-ht)
                           (make-if-for-op
                            mx-id hash-index op args min-var cst1 cst2
                            op-ht if-ht bdd-constructors)
                           (declare (type (signed-byte 30) mx-id))
                           (cond ((stringp ans)
                                  (bdd-error mx-id ans op-ht if-ht ttree))
                                 (t
                                  (mvf mx-id ans op-ht if-ht
                                       ttree)))))))))))))))))))))

(defun bdd (term alist op-ht if-ht mx-id ttree bddspv state)
  (declare (xargs :measure (acl2-count term)
                  :guard (pseudo-termp term))
           (type (signed-byte 30) mx-id))
  (the-mv
   5
   (signed-byte 30)
   (cond
    ((variablep term)
     (mvf mx-id
          (or (cdr (assoc-eq term alist))
              (er hard 'bdd
                  "Didn't find variable ~x0!"
                  term))
          op-ht if-ht ttree))
    ((fquotep term)
     (cond
      ((eq (cadr term) t)
       (mvf mx-id *cst-t* op-ht if-ht ttree))
      ((eq (cadr term) nil)
       (mvf mx-id *cst-nil* op-ht if-ht ttree))
      (t (bdd-quotep+ term op-ht if-ht mx-id ttree))))
    ((or (eq (ffn-symb term) 'if)
         (eq (ffn-symb term) 'if*))
     (bdd-mv-let
      (mx-id test-cst op-ht if-ht ttree)
      (bdd (fargn term 1) alist op-ht if-ht
           mx-id

; We will need to note the use of if* eventually, so for simplicity we do it
; now.

           (if (eq (ffn-symb term) 'if)
               ttree
             (push-lemma (fn-rune-nume 'if* nil nil (w state)) ttree))
           bddspv state)

; Note that we don't simply call combine-if-csts+, because we want to avoid
; applying bdd to one of the branches if the test already decides the issue.

      (cond
       ((cst-nilp test-cst)
        (bdd (fargn term 3) alist op-ht if-ht mx-id ttree bddspv state))
       ((cst-nonnilp test-cst)
        (bdd (fargn term 2) alist op-ht if-ht mx-id ttree bddspv state))
       ((eq (ffn-symb term) 'if*)
        (bdd-error
         mx-id
         "Unable to resolve test of IF* for term~|~%~p0~|~%under the ~
          bindings~|~%~x1~|~%-- use SHOW-BDD to see a backtrace."
         (list (cons #\0 (untranslate term nil (w state)))
               (cons #\1
                     (decode-cst-alist alist
                                       (leaf-cst-list-array mx-id))))

; We need a cst next, though we don't care about it.

         *cst-t*
         ttree))
       (t
        (bdd-mv-let
         (mx-id true-cst op-ht if-ht ttree)
         (bdd (fargn term 2) alist op-ht if-ht mx-id ttree bddspv state)
         (bdd-mv-let
          (mx-id false-cst op-ht if-ht ttree)
          (bdd (fargn term 3) alist op-ht if-ht mx-id ttree bddspv state)
          (mv-let
           (mx-id cst op-ht if-ht)
           (combine-if-csts test-cst true-cst false-cst op-ht if-ht mx-id
                            (access bddspv bddspv :bdd-constructors))
           (declare (type (signed-byte 30) mx-id))
           (cond
            ((stringp cst)
             (bdd-error mx-id cst op-ht if-ht ttree))
            (t
             (mvf mx-id cst op-ht if-ht ttree))))))))))
    ((flambdap (ffn-symb term))
     (bdd-mv-let
      (mx-id alist op-ht if-ht ttree)
      (bdd-alist (lambda-formals (ffn-symb term))
                 (fargs term)
                 alist op-ht if-ht
                 mx-id ttree bddspv state)
      (bdd (lambda-body (ffn-symb term))
           alist op-ht if-ht mx-id ttree bddspv state)))
    (t (mv-let
        (opcode comm-p enabled-exec-p mask)
        (op-alist-info (ffn-symb term)
                       (access bddspv bddspv :op-alist))
        (declare (type (signed-byte 30) opcode))
        (cond
         (comm-p
          (bdd-mv-let
           (mx-id arg1 op-ht if-ht ttree)
           (bdd (fargn term 1) alist op-ht if-ht mx-id ttree bddspv state)
           (bdd-mv-let
            (mx-id arg2 op-ht if-ht ttree)
            (bdd (fargn term 2) alist op-ht if-ht mx-id ttree bddspv state)
            (combine-op-csts-comm mx-id comm-p enabled-exec-p opcode
                                  (ffn-symb term) mask
                                  arg1 arg2 nil op-ht if-ht
                                  (cdr (assoc-eq (ffn-symb term)
                                                 (access bddspv bddspv
                                                         :bdd-rules-alist)))
                                  ttree bddspv state))))
         (t
          (bdd-mv-let (mx-id lst op-ht if-ht ttree)
                   (bdd-list (fargs term) alist op-ht if-ht mx-id ttree bddspv
                             state)
                   (combine-op-csts mx-id enabled-exec-p opcode
                                    (ffn-symb term) mask

; For a first cut I'll keep this simple.  Later, we may want to avoid consing
; up lst in the first place if we're only going to mess with it.

                                    lst op-ht if-ht
                                    (cdr (assoc-eq (ffn-symb term)
                                                   (access bddspv bddspv
                                                           :bdd-rules-alist)))
                                    ttree bddspv state)))))))))

(defun bdd-alist (formals actuals alist op-ht if-ht mx-id ttree bddspv state)
  (declare (type (signed-byte 30) mx-id))
  (the-mv
   5
   (signed-byte 30)
   (cond
    ((endp formals)
     (mvf mx-id nil op-ht if-ht ttree))
    (t (bdd-mv-let
        (mx-id bdd op-ht if-ht ttree)
        (bdd (car actuals) alist op-ht if-ht mx-id ttree bddspv state)
        (bdd-mv-let (mx-id rest-alist op-ht if-ht ttree)
                    (bdd-alist (cdr formals) (cdr actuals)
                               alist op-ht if-ht mx-id ttree bddspv state)
                    (mvf mx-id
                         (cons (cons (car formals) bdd)
                               rest-alist)
                         op-ht if-ht ttree)))))))

(defun bdd-list (lst alist op-ht if-ht mx-id ttree bddspv state)
  (declare (type (signed-byte 30) mx-id))
  (the-mv
   5
   (signed-byte 30)
   (cond
    ((endp lst)
     (mvf mx-id nil op-ht if-ht ttree))
    (t (bdd-mv-let
        (mx-id bdd op-ht if-ht ttree)
        (bdd (car lst) alist op-ht if-ht mx-id ttree bddspv state)
        (bdd-mv-let (mx-id rest op-ht if-ht ttree)
                    (bdd-list (cdr lst) alist op-ht if-ht mx-id ttree
                              bddspv state)
                    (mvf mx-id (cons bdd rest) op-ht if-ht ttree)))))))
)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;; VIII. TOP-LEVEL (INTERFACE) ROUTINES     ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; We will ignore declaration opportunities in this section, especially for
; declaring mx-id to be a fixnum, because efficiency is a minor issue at this
; level.

; See axioms.lisp for the definition of if*.

(defun if-ht-max-length (state)
  (if (f-boundp-global 'if-ht-max-length state)
      (f-get-global 'if-ht-max-length state)
    (+ 100000 (hash-size))))

(defun op-ht-max-length (state)
  (if (f-boundp-global 'op-ht-max-length state)
      (f-get-global 'op-ht-max-length state)
    (+ 100000 (hash-size))))

(defun leaf-cst-list-to-alist (leaf-cst-list)

; Leaf-cst-list is a list of leaf csts of the form (unique-id var bool-flg).
; We return a corresponding alist in which each variable is paired with its
; cst.

  (cond
   ((endp leaf-cst-list)
    nil)
   (t (cons (cons (trm (car leaf-cst-list))
                  (car leaf-cst-list))
            (leaf-cst-list-to-alist (cdr leaf-cst-list))))))

#+(and gcl (not acl2-loop-only))
(defvar *request-bigger-fixnum-table*
  (fboundp 'system::allocate-bigger-fixnum-range))

(defun bdd-top (term input-vars bool-vars bdd-constructors cl-id ens state)

; This function returns a bddnote, where if an "error" occurs then the cst is
; nil.  This bddnote has an empty :term field.

; Input-vars should be the list of all variables, with the highest priority
; variables (those which will have the lowest unique-ids) listed first.  At any
; rate, all variables in bool-vars are to be considered Boolean-valued.

; This function is allowed to assume that we are in a context where only
; propositional equivalence need be maintained.

  (let* ((fns (all-fnnames term))
         (wrld (w state)))
    (mv-let (fns bdd-rules-alist)
            (bdd-rules-alist (remove1-eq 'if fns)
                             (add-to-set-eq 'if fns)
                             nil
                             ens
                             wrld)
            (let ((op-alist (op-alist fns nil 6 ens wrld))
                  (if-ht (compress1 'if-ht
                                    `((:header :dimensions
                                               (,(1+ (hash-size)))
                                               :maximum-length
                                               ,(if-ht-max-length state)
                                               :default nil))))
                  (op-ht (compress1 'op-ht
                                    `((:header :dimensions
                                               (,(1+ (hash-size)))
                                               :maximum-length
                                               ,(op-ht-max-length state)
                                               :default nil))))
                  (all-vars (let ((term-vars (reverse (all-vars term))))

; So, term-vars has the variables in print order of first occurrence, a very
; unsatisfying but very simple heuristic.

                              (cond ((not (symbol-listp input-vars))
                                     (er hard 'bdd-top
                                         "The second argument of BDD-TOP must ~
                                          be a list of variables, but ~x0 is ~
                                          not."
                                         input-vars))
                                    ((subsetp-eq term-vars input-vars)
                                     input-vars)
                                    (t (er hard 'bdd-top
                                           "The following variables are free ~
                                            in the input term, ~x0, but not do ~
                                            not occur in the specified input ~
                                            variables, ~x1:  ~x2."
                                           term input-vars
                                           (set-difference-eq term-vars
                                                              input-vars)))))))
              #+(and (not acl2-loop-only) akcl)
              (cond ((and (fboundp 'si::sgc-on)
                          (funcall 'si::sgc-on))
                     (fms "NOTE: Turning off SGC.  If you wish to turn SGC ~
                           back on again, execute (SI::SGC-ON T) in raw Lisp.~|"
                          nil (standard-co *the-live-state*)
                          *the-live-state* nil)
                     (funcall 'si::sgc-on nil)))
              #+(and gcl (not acl2-loop-only))
              (cond (*request-bigger-fixnum-table*
                     (allocate-fixnum-range 0 (hash-size))
                     (setq *request-bigger-fixnum-table* nil)))
              (mv-let (mx-id leaf-cst-list)
                      (leaf-cst-list all-vars
                                     bool-vars
                                     nil
                                     (max (unique-id *cst-nil*)
                                          (unique-id *cst-t*)))
                      (mv-let (mx-id cst op-ht if-ht ttree)
                              (bdd term (leaf-cst-list-to-alist leaf-cst-list)
                                   op-ht if-ht mx-id nil
                                   (make bddspv
                                         :op-alist op-alist
                                         :bdd-rules-alist bdd-rules-alist
                                         :bdd-constructors bdd-constructors)
                                   state)
                              (cond
                               ((stringp cst)

; Then we actually have
; (mv mx-id fmt-string (cons fmt-alist bad-cst) call-stack ttree).

                                (make bddnote
                                      :cl-id cl-id
                                      :goal-term term
                                      :mx-id mx-id
                                      :err-string cst
                                      :fmt-alist (car op-ht)
                                      :cst (cdr op-ht)
                                      :term nil
                                      :bdd-call-stack if-ht
                                      :ttree ttree))
                               (t
                                (make bddnote
                                      :cl-id cl-id
                                      :goal-term term
                                      :mx-id mx-id
                                      :err-string nil
                                      :fmt-alist nil
                                      :cst cst
                                      :term nil
                                      :bdd-call-stack nil
                                      :ttree ttree)))))))))

(defun get-bool-vars (vars type-alist ttree acc)
  (cond
   ((endp vars) (mv acc ttree))
   (t (let ((entry

; We use the low-level function assoc-eq here so that it is clear we are not
; depending on the ACL2 world.

             (assoc-eq (car vars) type-alist)))
        (cond
         ((and entry
               (ts-subsetp (cadr entry) *ts-boolean*))
          (get-bool-vars (cdr vars)
                         type-alist
                         (cons-tag-trees (cddr entry) ttree)
                         (cons (car vars) acc)))
         (t (get-bool-vars (cdr vars) type-alist ttree acc)))))))

(defun bdd-clause1 (hint-alist type-alist cl position ttree0 cl-id ens wrld
                               state)

; Returns (mv hitp x y), where:

; if hitp is 'error then x is a msg and y is nil or a bddnote;
; if hitp is 'miss then x is nil and y is a bddnote;
; else hitp is 'hit, in which case x is a list of clauses and y is a ttree.

  (let* ((term (case position
                     (:conc (mcons-term* 'if (car (last cl)) *t* *nil*))
                     (:all (mcons-term* 'if (disjoin cl) *t* *nil*))
                     (otherwise
                      (let ((lit (nth position cl)))
                        (case-match
                         lit
                         (('not x)
                          (mcons-term* 'if x *t* *nil*))
                         (& (mcons-term* 'not lit)))))))
         (all-vars (all-vars term))
         (vars-hint (cdr (assoc-eq :vars hint-alist)))
         (prove-hint (if (assoc-eq :prove hint-alist)
                         (cdr (assoc-eq :prove hint-alist))
                       t))
         (bdd-constructors-hint
          (if (assoc-eq :bdd-constructors hint-alist)
              (cdr (assoc-eq :bdd-constructors hint-alist))
            (bdd-constructors wrld))))
    (mv-let
     (bool-vars ttree1)
     (get-bool-vars all-vars type-alist ttree0 nil)
     (cond
      ((not (subsetp-eq (if (eq vars-hint t) all-vars vars-hint)
                        bool-vars))
       (let ((bad-vars
              (set-difference-eq (if (eq vars-hint t) all-vars vars-hint)
                                 bool-vars)))
         (mv 'error
             (msg "The following variable~#0~[ is~/s are~] not known to be ~
                   Boolean by trivial (type set) reasoning:  ~&0.  Perhaps you ~
                   need to add hypotheses to that effect.  Alternatively, you ~
                   may want to prove :type-prescription rules (see :DOC ~
                   type-prescription) or :forward-chaining (see :DOC ~
                   forward-chaining) rules to help with the situation, or ~
                   perhaps to start with the hint ~x1."
                  bad-vars
                  (list :cases
                        (if (consp (cdr bad-vars))
                            (list (cons 'and
                                        (pairlis$
                                         (make-list (length bad-vars)
                                                    :initial-element 'booleanp)
                                         (pairlis$ bad-vars nil))))
                          `((booleanp ,(car bad-vars))))))
             nil)))
      (t
       (let* ((real-vars-hint (if (eq vars-hint t) nil vars-hint))
              (bddnote (bdd-top term
                                (append real-vars-hint
                                        (set-difference-eq
                                         (reverse all-vars)
                                         real-vars-hint))
                                bool-vars
                                bdd-constructors-hint
                                cl-id
                                ens
                                state))
              (cst (access bddnote bddnote :cst))
              (err-string (access bddnote bddnote :err-string))
              (ttree (access bddnote bddnote :ttree)))
         (cond
          (err-string

; An error occurred; we aborted the bdd computation.

           (if prove-hint
               (mv 'error
                   (cons (access bddnote bddnote :err-string)
                         (access bddnote bddnote :fmt-alist))
                   bddnote)
             (mv 'miss nil bddnote)))
          ((cst-tp cst)
           (mv 'hit
               nil
               (add-to-tag-tree
                'bddnote
                bddnote
                (cons-tag-trees ttree ttree1))))
          (prove-hint
           (mv 'error
               (list "The :BDD hint for the current goal has ~
                      successfully simplified this goal, but has ~
                      failed to prove it.  Consider using (SHOW-BDD) ~
                      to suggest a counterexample; see :DOC show-bdd.")
               bddnote))
          (t (mv-let
              (new-term cst-array)
              (decode-cst
               cst
               (leaf-cst-list-array
                (access bddnote bddnote :mx-id)))
              (declare (ignore cst-array))
              (let* ((bddnote (change bddnote bddnote
                                      :term new-term))
                     (ttree (add-to-tag-tree
                             'bddnote
                             bddnote
                             (cons-tag-trees ttree ttree1))))
                (cond
                 ((eq position :conc)
                  (mv 'hit
                      (list (add-literal new-term
                                         (butlast cl 1)
                                         t))
                      ttree))
                 ((eq position :all)
                  (mv 'hit
                      (list (add-literal new-term nil nil))
                      ttree))
                 (t ; hypothesis
                  (mv 'hit
                      (list (subst-for-nth-arg (dumb-negate-lit new-term)
                                               position
                                               cl))
                      ttree)))))))))))))

(defmacro expand-and-or-simple+
  (term bool fns-to-be-ignored-by-rewrite wrld ttree)

; Unlike expand-and-or-simple, the list of terms (second value) returned by
; this macro is always ``correct,'' and the hitp value is always non-nil.

  `(mv-let (hitp lst ttree1)
           (expand-and-or-simple
            ,term ,bool ,fns-to-be-ignored-by-rewrite ,wrld ,ttree)
           (cond (hitp (mv hitp lst ttree1))
                 (t (mv t (list ,term) ,ttree)))))

(defun expand-and-or-simple
  (term bool fns-to-be-ignored-by-rewrite wrld ttree)

; See the comment in expand-clause.  This is a simple version of expand-and-or
; that does not expand abbreviations or, in fact, use lemmas at all (just the
; definitions of NOT, IF, and IMPLIES).  We expand the top-level fn symbol of
; term provided the expansion produces a conjunction -- when bool is nil -- or
; a disjunction -- when bool is t.  We return three values:  a hitp flag
; denoting success, the resulting list of terms (to be conjoined or disjoined
; to produce a term equivalent to term), and a new ttree.  If the hitp flag is
; nil then we make no guarantees about the ``resulting list of terms,'' which
; in fact (for efficiency) is typically nil.

; Note that this function only guarantees propositional (iff) equivalence of
; term with the resulting conjunction or disjunction.

  (cond ((variablep term)
         (mv nil nil ttree))
        ((fquotep term)
         (cond
          ((equal term *nil*)
           (if bool (mv t nil ttree) (mv nil nil ttree)))
          ((equal term *t*)
           (if bool (mv nil nil ttree) (mv t nil ttree)))
          (t
           (if bool (mv t (list *t*) ttree) (mv t nil ttree)))))
        ((member-equal (ffn-symb term) fns-to-be-ignored-by-rewrite)
         (mv nil nil ttree))
        ((flambda-applicationp term)

; We don't use (and-orp (lambda-body (ffn-symb term)) bool) here because that
; approach ignores nested lambdas.

         (mv-let (hitp lst ttree0)
                 (expand-and-or-simple
                  (lambda-body (ffn-symb term))
                  bool fns-to-be-ignored-by-rewrite wrld ttree)
                 (cond
                  (hitp (mv hitp
                            (subcor-var-lst
                             (lambda-formals (ffn-symb term))
                             (fargs term)
                             lst)
                            ttree0))
                  (t (mv nil nil ttree)))))
        ((eq (ffn-symb term) 'not)
         (mv-let (hitp lst ttree0)
                 (expand-and-or-simple (fargn term 1) (not bool)
                                       fns-to-be-ignored-by-rewrite wrld ttree)
                 (cond (hitp
                        (mv hitp
                            (dumb-negate-lit-lst lst)
                            (push-lemma (fn-rune-nume 'not nil nil wrld)
                                        ttree0)))
                       (t (mv nil nil ttree)))))
        ((and (eq (ffn-symb term) 'implies)
              bool)
         (expand-and-or-simple
          (subcor-var (formals 'implies wrld)
                      (fargs term)
                      (body 'implies t wrld))
          bool fns-to-be-ignored-by-rewrite wrld
          (push-lemma (fn-rune-nume 'implies nil nil wrld)
                      ttree)))
        ((eq (ffn-symb term) 'if)
         (let ((t1 (fargn term 1))
               (t2 (fargn term 2))
               (t3 (fargn term 3)))
           (cond
            ((or (equal t1 *nil*)
                 (equal t2 t3))
             (expand-and-or-simple+ t3 bool fns-to-be-ignored-by-rewrite
                                    wrld ttree))
            ((quotep t1)
             (expand-and-or-simple+ t2 bool fns-to-be-ignored-by-rewrite
                                    wrld ttree))
            ((and (quotep t2) (quotep t3))
             (cond
              ((equal t2 *nil*)

; We already know t2 is not t3, so we have t3 = *t* up to iff-equivalence, and
; hence we are looking at (not t1) up to iff-equivalence.

               (mv-let (hitp lst ttree)
                       (expand-and-or-simple t1 (not bool)
                                             fns-to-be-ignored-by-rewrite
                                             wrld ttree)
                       (mv t
                           (if hitp
                               (dumb-negate-lit-lst lst)
                             (list (dumb-negate-lit t1)))
                           ttree)))
              ((equal t3 *nil*)
               (expand-and-or-simple+ t1 bool
                                      fns-to-be-ignored-by-rewrite
                                      wrld ttree))
              (t
               (expand-and-or-simple *t* bool
                                     fns-to-be-ignored-by-rewrite
                                     wrld ttree))))
            ((and (quotep t3)
                  (eq (not bool) (equal t3 *nil*)))

; We combine the cases (or (not t1) t2) [bool = t] and (and t1 t2) [bool =
; nil].

             (mv-let (hitp lst1 ttree)
                     (expand-and-or-simple+ t1 nil
                                            fns-to-be-ignored-by-rewrite
                                            wrld ttree)
                     (declare (ignore hitp))
                     (mv-let (hitp lst2 ttree)
                             (expand-and-or-simple+
                              t2 bool
                              fns-to-be-ignored-by-rewrite wrld ttree)
                             (declare (ignore hitp))
                             (if bool
                                 (mv t
                                     (union-equal (dumb-negate-lit-lst lst1)
                                                  lst2)
                                     ttree)
                               (mv t (union-equal lst1 lst2) ttree)))))
            ((and (quotep t2)
                  (eq (not bool) (equal t2 *nil*)))

; We combine the cases (or t1 t3) [bool = t] and (and (not t1) t3)
; [bool = nil].

             (mv-let (hitp lst1 ttree)
                     (expand-and-or-simple+ t1 t
                                            fns-to-be-ignored-by-rewrite
                                            wrld ttree)
                     (declare (ignore hitp))
                     (mv-let (hitp lst2 ttree)
                             (expand-and-or-simple+
                              t3 bool
                              fns-to-be-ignored-by-rewrite wrld ttree)
                             (declare (ignore hitp))
                             (if bool
                                 (mv t (union-equal lst1 lst2) ttree)
                               (mv t
                                   (union-equal (dumb-negate-lit-lst lst1)
                                                lst2)
                                   ttree)))))
            (t (mv nil nil ttree)))))
        (t (mv nil nil ttree))))

(defun expand-clause
  (cl fns-to-be-ignored-by-rewrite wrld ttree acc)

; A classic form for a bdd problem is something like the following.

; (let ((x (list x0 x1 ...))
;   (implies (boolean-listp x)
;            ...)

; How do we let the bdd package know that x0, x1, ... are Boolean?  It needs to
; know that x really is (list x0 x1 ...), and then it needs to forward-chain
; from (boolean-listp (list x0 x1 ...)) to the conjunction of (booleanp xi).
; However, the clause handed to bdd-clause may be a one-element clause with the
; literal displayed above, so here we "flatten" this literal into a clause that
; is more amenable to forward-chaining.

  (cond
   ((endp cl) (mv acc ttree))
   (t (mv-let (hitp lst ttree)
              (expand-and-or-simple+
               (car cl) t fns-to-be-ignored-by-rewrite wrld ttree)
              (declare (ignore hitp))
              (expand-clause (cdr cl) fns-to-be-ignored-by-rewrite wrld
                             ttree (union-equal lst acc))))))

(defun bdd-clause (bdd-hint cl-id top-clause pspv wrld state)

; This function is analogous to simplify-clause (except that bdd-hint is passed
; in here), and shares much code with simplify-clause1.  It is separated out
; from apply-top-hints-clause for readability.  We return 4 values, as required
; by apply-top-hints-clause.

; Unlike simplify-clause1, we do not call ok-to-force, but instead we do not
; force during forward-chaining.  We may want to revisit that decision, but
; for now, we prefer to minimize forcing during bdd processing.

  (let ((rcnst (access prove-spec-var pspv :rewrite-constant))
        (literal-hint (or (cdr (assoc-eq :literal bdd-hint))
                          :all)))
    (cond
     ((and (integerp literal-hint)

; Note that literal-hint is a 0-based index; see translate-bdd-hint1.  We know
; that literal-hint is non-negative, translate-bdd-hint1 never returns a
; negative literal-hint.

           (not (< literal-hint (1- (length top-clause)))))
      (mv 'error
          (msg "There ~#0~[are no hypotheses~/is only one hypothesis~/are only ~
                ~n0 hypotheses~] in this goal, but your :BDD hint suggested ~
                that there would be at least ~x1 ~
                ~#1~[~/hypothesis~/hypotheses]."
               (1- (length top-clause))
               (1+ literal-hint))
          nil
          pspv))
     (t
      (mv-let (hitp current-clause current-clause-pts
                    remove-trivial-equivalences-ttree)
              (remove-trivial-equivalences top-clause
                                           (enumerate-elements top-clause 0)
                                           t
                                           (access rewrite-constant rcnst
                                                   :current-enabled-structure)
                                           wrld state nil nil nil)
              (declare (ignore hitp))
              (let ((position (cond ((integerp literal-hint)
                                     (position literal-hint
                                               current-clause-pts))
                                    (t literal-hint))))
                (cond
                 ((or (null position)
                      (and (eq literal-hint :conc)
                           (not (member (1- (length top-clause))
                                        current-clause-pts))))
                  (mv 'error
                      (msg "The attempt to use a :BDD hint for the goal named ~
                            \"~@0\" has failed.  The problem is that the hint ~
                            specified that BDD processing was to be used on ~
                            the ~#1~[~n2 hypothesis~/conclusion~], which has ~
                            somehow disappeared.  One possibility is that this ~
                            literal is an equivalence that has disappeared ~
                            after being used for substitution into the rest of ~
                            the goal.  Another possibility is that this ~
                            literal has merged with another.  We suspect, ~
                            therefore, that you would benefit by reconsidering ~
                            this :BDD hint, possibly attaching it to a ~
                            subsequent goal instead."
                           (tilde-@-clause-id-phrase cl-id)
                           (if (null position) 0 1)
                           (if (null position) (1+ literal-hint) 0))
                      nil
                      pspv))
                 (t
                  (let ((ens (access rewrite-constant rcnst
                                     :current-enabled-structure)))
                    (mv-let (expanded-clause ttree)
                            (expand-clause
                             current-clause
                             (access rewrite-constant
                                     rcnst
                                     :fns-to-be-ignored-by-rewrite)
                             wrld remove-trivial-equivalences-ttree nil)
                            (mv-let
                             (contradictionp type-alist fc-pair-lst)
                             (forward-chain-top 'bdd-clause
                                                expanded-clause
                                                nil
                                                nil ; Let's not force
                                                t ; do-not-reconsiderp
                                                wrld
                                                ens
                                                (match-free-override wrld)
                                                state)
                             (cond
                              (contradictionp

; Note: When forward-chain returns with contradictionp = t, then the
; "fc-pair-lst" is really a ttree.  We must add the remove-trivial-
; equivalences ttree to the ttree returned by forward-chain and we must
; remember our earlier case splits.

                               (mv 'hit
                                   nil
                                   (cons-tag-trees ttree fc-pair-lst)
                                   pspv))
                              (t (mv-let (changedp clauses ttree)

; Ttree is either nil or a bddnote if changedp is 'miss or 'error.  See
; waterfall-step.

                                         (bdd-clause1 bdd-hint type-alist
                                                      current-clause position
                                                      ttree
                                                      cl-id ens wrld state)
                                         (mv changedp clauses ttree
                                             pspv)))))))))))))))

; See show-bdd and successive definitions for code used to inspect the
; results of using OBDDs.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;; IX.   COMPILING THIS FILE AND OTHER HELPFUL TIPS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; In order to check for slow code, you can execute the following from ACL2 built
; on GCL, inside raw Lisp.
;
; (compile-file "bdd.lisp" :c-file t)
;
; Then, search the file bdd.c for make_fixnum and number_ for slow stuff.  Note
; that you'll find a lot of these, but you only need to worry about them in the
; workhorse functions, and you don't need to worry about CMPmake_fixnum when it
; is used for an error or for a new mx-id.
;
; When you find one of these, search upward for `local entry' to see which
; function or macro you are in.  Don't worry, for example, about commutative-p,
; which is a database kind of function rather than a workhorse function.
;
; You'll see things like the following (from local entry to BDD).  The idea here
; is that we are boxing a fixnum and pushing it on a stack, but why?  LnkLI253
; appears to be a function call, which is found near the end of the file to
; correspond to leaf-cst-list-array.  If we're still not clear on what's going
; on, we can look up 273 as well.  When we do this, we find that we are probably
; in the part of the BDD code shown at the end, which is not a problem.
;
;         V1570 = CMPmake_fixnum(V1549);
;         V1571= (*(LnkLI253))(/* INLINE-ARGS */V1569,V1570);
;         V1572= (*(LnkLI273))((V1525),/* INLINE-ARGS */V1571);
;
; ....
;
; static object  LnkTLI273(va_alist)va_dcl{va_list ap;va_start(ap);
;  return(object )call_proc(VV[273],&LnkLI273,2,ap);} /* DECODE-CST-ALIST */
;
; static object  LnkTLI253(va_alist)va_dcl{va_list ap;va_start(ap);
;  return(object )call_proc(VV[253],&LnkLI253,2,ap);} /* LEAF-CST-LIST-ARRAY */
;
; ; Source code from (defun bdd ...) [an earlier version]:
;
;         (bdd-error
;          mx-id
;          "Unable to resolve test of IF* for term~|~%~p0~|~%under the ~
;           bindings~|~%~x1~|~%-- use SHOW-BDD to see a backtrace."
;          (list (cons #\0 (untranslate term nil))
;                (cons #\1
;                      (decode-cst-alist alist
;                                        (leaf-cst-list-array
;                                         (strip-cdrs alist)
;                                         mx-id))))
;
; ; We need a cst next, though we don't care about it.
;
;          *cst-t*
;          ttree)