File: r-and-i-answer-key-input.lsp

package info (click to toggle)
acl2 8.6%2Bdfsg-2
  • links: PTS
  • area: main
  • in suites: trixie
  • size: 1,111,420 kB
  • sloc: lisp: 17,818,294; java: 125,359; python: 28,122; javascript: 23,458; cpp: 18,851; ansic: 11,569; perl: 7,678; xml: 5,591; sh: 3,976; makefile: 3,833; ruby: 2,633; yacc: 1,126; ml: 763; awk: 295; csh: 233; lex: 197; php: 178; tcl: 49; asm: 23; haskell: 17
file content (4501 lines) | stat: -rw-r--r-- 125,118 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
; Copyright (C) 2022, ForrestHunt, Inc.
; Written by J Strother Moore
; License: A 3-clause BSD license.  See the LICENSE file distributed with ACL2.
; Release approved by DARPA with "DISTRIBUTION STATEMENT A. Approved
; for public release. Distribution is unlimited."

; Recursion and Induction:  Answer Key
; J Strother Moore

; Updated October 2022 by Matt Kaufmann for latest R&I and to produce output
; that can be checked during ACL2 regression testing.

; NOTE: Readers of "Recursion and Induction" would be best served by looking
; not at the present file, but rather, at file:
; r-and-i-answer-key-log.txt

(in-package "ACL2")

; This file provides input for generating output file
; r-and-i-answer-key-log.out when certifying the book,
; r-and-i-answer-key-book.lisp.  In particular, that output file contains the
; comments below that are delimited by #{""" ... """}.  The certification
; process checks that r-and-i-answer-key-log.out agrees with the "golden"
; output file, r-and-i-answer-key-log.txt.

:----- #{"""

; This file was generated by ACL2 from the input file
; r-and-i-answer-key-input.lsp.  The output generated up to
; this point should be ignored by anyone who reads it.

; When the problem requires that you show a term or answer a
; question informally, the answer is written as a comment.

; We answer some ``fill in the blank'' questions by showing
; a theorem.  For example, if the question were to ask you
; to show the constant that is equal to (expt 2 31), we
; might show the theorem (equal (expt 2 31) 2147483648).

; We answer definition problems by just showing the
; appropriate defun.  Sometimes we have to provide the right
; measure.  For example,

; (defun f (x)
;  (declare (xargs :measure (ms x)))
;  <body>)

; means

;   The definition (defun f (x) <body>) is admissible under
;   the ACL2 definitional principle if you use the measure
;   (ms x) as the sense of the size of the argument.

; When the problem requires you to do a proof in the logic,
; we generally answer by showing an ACL2 command of the
; form

; (theorem <Problem-i>
;          <formula>
;          :hints (("Goal" :induct <term>
;                          :enable (<name1>
;                                   ...
;                                   <namek>)))
;          :rule-classes <classes>)

; (Notice that the double-quote character (") is escaped
; with a backslash (\), which is similarly escaped, in
; the output file.)

; You can read this as follows.

;   <Problem-i> is the challenge to prove the theorem
;   <formula>.

;   Proof: Induct as suggested by <term>, i.e., to unwind
;   the recursive function in the term <term>.  Then,
;   simplify each case.  The simplification may freely use
;   propositional calculus, axioms, definitions, and lemmas
;   about arithmetic.  However, the proof will require you
;   to use certain lemmas proved earlier in this file,
;   namely those whose names, <name1>, ..., <namek>, are
;   listed.  Q.E.D.

;   If no :induct <term> is given, induction is not necessary
;   to the proof; simplification alone sufficies.

;   If no names are listed, then propositional calculus,
;   axioms, definitions, and arithmetic suffice.

;   If no hints at all are given, it is as though no :induct
;   <term> is given and no lemmas are named, i.e., the proof
;   is by straightforward simplification.

;   You can ignore the :rule-classes entry <classes>; it
;   just tells the mechanical theorem prover how to use this
;   theorem in the future.

;   If you want to see the actual proof output produced by
;   ACL2 when carrying out this high-level description of
;   the proof, inspect the file r-and-i-answer-key-log.txt.

;   In the script below, the first time we use an :induct
;   hint we include as a comment the output you'll find in
;   r-and-i-answer-key-log.txt so you can see how the output
;   elaborates the proof sketch provided by the input.
;   Similarly, we provide output the first time we use an
;   :in-theory hint to list the names of relevant lemmas.

; There is no need for you to understand how we define the
; theorem command to make this happen.  So skip the
; following defmacro and go to the solution of Problem 1.

; WARNING: If you start using ACL2 we do not recommend that
; you use the theorem macro.  Instead, use defthm directly.
; Theorem has the same interface as defthm but (a) no
; induction is done automatically -- you have to give the
; induction hint explicitly and no proof has more than one
; induction, and (b) after the new theorem is proved and
; installed as a rule, the new rule is disabled.  The latter
; feature then forces us to enable each rule proved with
; theorem whenever we want to use it.  Together features (a)
; and (b) ensure that a theorem command pretty much has all
; the information you need to reconstruct the proof
; yourself.  It is far better to get used to the idea that
; ACL2 often chooses the right induction(s) and uses rules
; freely after you've proved them.  So use defthm instead of
; reproducing this macro in your scripts.
"""}

(defmacro theorem
  (name form
        &key (rule-classes ':rewrite rule-classes-suppliedp)
        hints)
  (let ((old-hints hints)
        (hints
         (cond ((null hints)
                '(("Goal" :do-not-induct t
                          :do-not '(generalize))))
               ((and (true-listp hints)
                     (equal (length hints) 1)
                     (equal (car (car hints)) "Goal"))
                `(("Goal"
                   :do-not-induct t
                   :do-not '(generalize)
                   ,@(cdr (car hints)))))
               (t nil))))

; Note that if hints is nil, something bad has happened and
; we detect it and react below.
                      
    (cond
     ((null hints)
      `(er soft 'theorem
           "The :hints value ~x0 is not allowed ~
            by this macro."
           ',old-hints))
     ((and rule-classes-suppliedp
           (equal rule-classes nil))
      `(defthm ,name ,form
         :rule-classes nil
         :hints ,hints))
     (t `(progn
           (defthm ,name ,form
             ,@(if rule-classes-suppliedp
                   `(:rule-classes ,rule-classes)
                 nil)
             :hints ,hints)
           (in-theory (disable ,name))
           (value-triple ',name))))))

(set-gag-mode nil)
(set-state-ok t)

:next-problem #{"""
; ------------------------------------------------------------
; Problem 1.
; 1. Monday                      symbol
; 2. greek letter pi             ill-formed (not ASCII)
; 3. HelloWorld!                 symbol 
; 4. --1                         symbol (!)
; 5. -1                          integer
; 6. *PI*                        symbol
; 7. 31415x10**-4                symbol
; 8. (A . B . C)                 ill-formed (illegal dot)
; 9. Hello World!                ill-formed (two symbols)
; 10. if                         symbol
; 11. invokevirtual              symbol
; 12. ((1) . (2))                pair
; 13. <=                         symbol
; 14. ((A . 1) (B . 2) (C . 3))  pair
; 15. Hello_World!               symbol
; 16. +                          symbol
; 17. lo-part                    symbol
; 18. 31415926535897932384626433832795028841971693993751058209749445923
;                                integer 
; 19. (1 . (2 . 3))              pair
; 20. (1 . 2 3)                  ill-formed (illegal dot)
; 21. "Hello World!"             string
; 22. ((1) (2) . 3)              pair
; 23. ()                         symbol (nil)
"""}

:next-problem #{"""
; ------------------------------------------------------------
; Problem 2.

; Rather than just show the answer, I'll define a function to do it
; and then show the result.

; I freely use full ACL2 here since this function isn't part of the
; answer!

; The theorem below equates our computation of the equivalence
; classes with an object that associates each value with the labels
; of all expressions that represent it.  Here is that list.

;            constant          labels
;         '(((1 2 3)           (1 11))
;           ((NIL NIL NIL)     (2))
;           (((NIL NIL))       (3))
;           ((1 (2 . 3) 4)     (4 6))
;           ((NIL NIL)         (5 10 12))
;           ((HELLOWORLD !)    (7))
;           ((1 (2 3) 4)       (8))
;           (((A . T) (B) (C)) (9 16))
;           ((A B C)           (13 14))
;           ((HELLO WORLD !)   (15))))
"""}

(defun add-label-to-bucket (label const buckets)
  (if (endp buckets)
      (list (list const (list label)))
    (if (equal const (car (car buckets)))
        (cons (list const (append (cadr (car buckets)) (list label)))
              (cdr buckets))
      (cons (car buckets)
            (add-label-to-bucket label const (cdr buckets))))))

(defun make-equiv-classes (alist buckets)
  (if (endp alist)
      buckets
    (make-equiv-classes (cdr alist)
                        (add-label-to-bucket (caar alist)
                                             (cadr (car alist))
                                             buckets))))

; Implementation note (can be ignored by the reader):
; Because everything in a certified book has to be a theorem, I
; state the answer as the equivalence of the question and the answer:

(theorem problem-2
  (equal (make-equiv-classes 

; Implementation note (can be ignored by the reader; this is already
; explained above):
; Here is an alist representing the question, a bunch of labeled
; constants.

          '((1 (1 . (2 3)))
            (2 (nil . (nil nil)))
            (3 ((nil nil) . nil))
            (4 (1 (2 . 3) 4))
            (5 (nil nil))
            (6 (1 (2 . 3) . (4 . ())))
            (7 (HelloWorld !))
            (8 (1 (2 3 . ()) 4))
            (9 ((A . t) (B . nil)(C . nil)))
            (10 (()()))
            (11 (1 2 3))
            (12 (() () . nil))
            (13 (A B C))
            (14 (a . (b . (c))))
            (15 (HELLO WORLD !))
            (16 ((a . t) (b) . ((c)))))
          nil)

; Here is the answer, showing the labels of equivalent constants.

;           constant          labels
         '(((1 2 3)           (1 11))
           ((NIL NIL NIL)     (2))
           (((NIL NIL))       (3))
           ((1 (2 . 3) 4)     (4 6))
           ((NIL NIL)         (5 10 12))
           ((HELLOWORLD !)    (7))
           ((1 (2 3) 4)       (8))
           (((A . T) (B) (C)) (9 16))
           ((A B C)           (13 14))
           ((HELLO WORLD !)   (15))))
  :rule-classes nil)

:next-problem #{"""
; ------------------------------------------------------------
; Problem 3.

; 1. (car (cdr x))                yes
; 2. (cons (car x y) z)           no - car takes 1 arg
; 3. (cons 1 2)                   no - constants must be quoted
; 4. (cons '1 '2)                 yes
; 5. (cons one two)               yes - one and two are variable symbols
; 6. (cons 'one 'two)             yes - 'one and 'two are constants
; 7. (equal '1 (car (cons '2 '3)))yes
; 8. (if t 1 2)                   no - constants must be quoted
; 9. (if 't '1 '2)                yes
; 10. (car (cons (cdr hi-part) (car lo-part)))    yes
; 11. car(cons x y)               no - wrong syntax
; 12. car(cons(x,y))              no - wrong syntax
; 13. (cons 1 (2 3 4))            no - constants must be quoted
"""}

:next-problem #{"""
; ------------------------------------------------------------
; Problem 4.
"""}

(theorem problem-4
  (and (equal '((1 . 2) . (3 . 4))            ; 1.
              (cons (cons '1 '2)
                    (cons '3 '4)))
       (equal '(1 2 3)                        ; 2.
              (cons '1
                    (cons '2
                          (cons '3 'nil))))
       (equal '((1 . t) (2 . nil) (3 . t))    ; 3.
              (cons (cons '1 't)
                    (cons (cons '2 'nil)
                          (cons (cons '3 't)
                                'nil))))
       (equal '((A . 1) (B . 2))              ; 4.
              (cons (cons 'a '1)
                    (cons (cons 'b '2)
                          'nil))))
  :rule-classes nil)

:next-problem #{"""
; ------------------------------------------------------------
; Problem 5.
"""}

(theorem problem-5
  (and (equal (cons (cons '1 '2) (cons (cons '3 '4) 'nil))  ; 1. 
              '((1 . 2) (3 . 4)))
       (equal (cons '1 (cons '2 '3))                        ; 2.
              '(1 2 . 3))
       (equal (cons 'nil (cons (cons 'nil 'nil) 'nil))      ; 3.
              '(nil (nil)))
       (equal (if 'nil '1 '2)                               ; 4.
              '2)
       (equal (if '1 '2 '3)                                 ; 5.
              '2)
       (equal (equal 'nil (cons 'nil 'nil))                 ; 6.
              'nil)
       (equal (equal 'Hello 'HELLO)                         ; 7.
              't)
       (equal (equal (cons '1 '2) (cons '1 'two))           ; 8.
              'nil))
  :rule-classes nil)

:next-problem #{"""
; ------------------------------------------------------------
; Problem 6.

; Answer: (car (cons (car a) (cons (cdr x) (cons '"Hello" z))))
"""}

:next-problem #{"""
; ------------------------------------------------------------
; Problem 7.

; 1. (cons '1 (cons '2 (cons '3 'nil)))
; 2. (equal '"Hello" hello)
; 3. (and (or a1 (or a2 a3))
;         (and (or b1 (or b2 b3))
;              (or c1 (or c2 c3))))
; 4. (equal x (cons 'or (cons 'a1 (cons 'a2 (cons 'a3 'nil)))))
; 5. (cons cons
;          (cons 'cons
;                (cons 'cons
;                      (cons (cons 'quote (cons 'cons 'nil))
;                            'nil))))
"""}

:next-problem #{"""
; ------------------------------------------------------------
; Problem 8.
"""}

(defun app (x y)
  (if (consp x)
      (cons (car x) (app (cdr x) y))
    y))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 9.
"""}

(defun rev (x)
  (if (consp x)
      (app (rev (cdr x)) (cons (car x) nil))
    nil))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 10.
"""}

(defun mapnil (x)
  (if (consp x)
      (cons nil (mapnil (cdr x)))
    nil))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 11.
"""}

(defun swap-tree (x)
  (if (consp x)
      (cons (swap-tree (cdr x))
            (swap-tree (car x)))
    x))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 12.
"""}

(defun mem (e x)
  (if (consp x)
      (if (equal e (car x))
          t
        (mem e (cdr x)))
    nil))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 13.
"""}

(defun sub (x y)
  (if (consp x)
      (if (mem (car x) y)
          (sub (cdr x) y)
        nil)
    t))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 14.
"""}

(defun int (x y)
  (if (consp x)
      (if (mem (car x) y)
          (cons (car x) (int (cdr x) y))
        (int (cdr x) y))
    nil))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 15.
"""}

(defun tip (e x)
  (if (consp x)
      (or (tip e (car x))
          (tip e (cdr x)))
    (equal e x)))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 16.
"""}

(defun flatten (x)
  (if (consp x)
      (app (flatten (car x))
           (flatten (cdr x)))
    (cons x nil)))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 17.
"""}

(defun evenlen (x)
  (if (consp x)
      (if (consp (cdr x))
          (evenlen (cdr (cdr x)))
        nil)
    t))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 18.

; Theorem. (and p q) /= nil  <-> p /= nil & q /= nil.

; Proof. We consider three cases.  We use lhs and rhs to refer to the
; left and right sides of the equivalence operator.  Because of the
; way we break the cases, it is straightforward to determine the value
; of the rhs and we write it in parentheses at the top of each case.
; We show the lhs has the same truthvalue.

; Case 1.  p /= nil & q /= nil  (rhs is true)
; (and p q) = q /= nil.  Thus lhs is true.

; Case 2.  p /= nil & q  = nil  (rhs is false)
; (and p q) = q = nil.  Thus lhs is false.

; Case 3.  p  = nil             (rhs is false)
; (and p q) = nil.  Thus lhs is false.

; Q.E.D.
"""}

:next-problem #{"""
; ------------------------------------------------------------
; Problem 19.

; Theorem. (or p q) /= nil <-> p /= nil v q /= nil.

; Proof. We consider three cases and use the same format as the proof
; in Problem 18.

; Case 1. p /= nil  (rhs is true)
; (or p q) = p /= nil.  Thus lhs is true.

; Case 2. p  = nil and q /= nil  (rhs is true)
; (or p q) = q /= nil.  Thus lhs is true.

; Case 3. p  = nil & q  = nil  (rhs is false)
; (or p q) = nil.  Thus lhs is false.

; Q.E.D.
"""}

:next-problem #{"""
; ------------------------------------------------------------
; Problem 20.

; Theorem. (implies p q) /= nil <-> (p /= nil -> q /= nil).

; Proof.  Same format as 18.

; Case 1. p /= nil and q /= nil (rhs is true)
; (implies p q) = t.  Thus lhs is true.

; Case 2. p /= nil and q  = nil (rhs is false)
; (implies p q) = nil.  Thus lhs is false.

; Case 3. p  = nil              (rhs is true)
; (implies p q) = t.  Thus lhs is true.

; Q.E.D.
"""}

:next-problem #{"""
; ------------------------------------------------------------
; Problem 21.

; Theorem. (iff p q) /=  <-> (p /= nil <-> q /= nil)

; Proof. We consider four cases.

; Case 1.  p /= nil and q /= nil (rhs is true)
; (iff p q)
;  = (and (implies p q) (implies q p))
;  = (and t t)
;  = t.
; Thus lhs is true.

; Case 2.  p /= nil and q  = nil (rhs is false)
; (iff p q)
;  = (and (implies p q) (implies q p)) 
;  = (and nil t)
;  = nil.
; Thus lhs is false.

; Case 3.  p  = nil and q /= nil (rhs is false)
; (iff p q)
;  = (and (implies p q) (implies q p)) 
;  = (and t nil)
;  = nil.
; Thus lhs is false.

; Case 4.  p  = nil and q  = nil (rhs is true)
; (iff p q)
;  = (and (implies p q) (implies q p)) 
;  = (and t t)
;  = t.
; Thus lhs is true.

; Q.E.D.
"""}

:next-problem #{"""
; ------------------------------------------------------------
; Problem 22.

; Theorem.  (equal x y) /= nil  <->  (x = y)

; Proof.

; Case 1.  x=y (rhs is true)
; (equal x y) = t, by Axiom 5.  Thus lhs is true.

; Case 2.  x /= y (rhs is false)
; (equal x y) /= t, by Axiom 5.  Thus, by Axiom 4,
; (equal x y) = nil.  Thus, lhs is false.

; Q.E.D.
"""}

:next-problem #{"""
; ------------------------------------------------------------
; Problem 23.

; Theorem.  (implies (and p (implies q r)) s) <->
;           ((p & -q) -> s) & ((p & q & r) -> s)

; Proof.  The proof will also establish the intermediate formula
; requested.  

; Since the lhs above is a term treated as a formula, the lhs is

; (implies (and p (implies q r)) s) /= nil

; <->

; (and p (implies q r)) /= nil -> (s /= nil)

; <->

; ((p /= nil) & (implies q r) /= nil) -> (s /= nil)

; <->

; (p /= nil) & (q /= nil -> r /= nil) -> (s /= nil)

; <->

; (p & (q -> r)) -> s

; We won't use this last formula, since it uses terms as formulas.
; Instead, we'll go back one step:

; (p /= nil) & (q /= nil -> r /= nil) -> (s /= nil)

; <->

; (q  = nil) -> ((p /= nil) & (q /= nil -> r /= nil) -> (s /= nil))
; &
; (q /= nil) -> ((p /= nil) & (q /= nil -> r /= nil) -> (s /= nil))

; The previous step is just the tautology a <-> ((-b -> a) & (b -> a)).
; Continuing, using (a -> (b -> c)) <-> ((a & b) -> c)

; <->

; (q  = nil) & (p /= nil) & (q /= nil -> r /= nil) -> (s /= nil)
; &
; (q /= nil) * (p /= nil) & (q /= nil -> r /= nil) -> (s /= nil)

; <->

; (p /= nil) & (q  = nil) & t -> (s /= nil)
; &
; (p /= nil) & (q /= nil) & (r /= nil) -> (s /= nil)

; <->

; (p & -q) -> s
; &
; (p & q & r) -> s

; Q.E.D.

; This proof was tedious only because we tried to show some of the
; propositional calculus steps.
"""}

:next-problem #{"""
; ------------------------------------------------------------
; Problem 24.

; Theorem.  (equal (car (if a b c)) (if a (car b) (car c)))

; Proof.  We will reduce the lhs, (car (if a b c)), to the rhs, (if a
; (car b) (car c)), under two cases, thus establishing lhs=rhs and
; thus (equal lhs rhs).

; Case 1.  a /= nil.

; lhs = (car (if a b c)) = (car b)
; rhs = (if a (car b) (car c)) = (car b).

; Case 2.  a  = nil.

; lhs = (car (if a b c)) = (car c)
; rhs = (if a (car b) (car c)) = (car c).

; Q.E.D.
"""}

:next-problem #{"""
; ------------------------------------------------------------
; Problem 25.

; Theorem.  (equal (if (if a b c) x y)
;                  (if a (if b x y) (if c x y)))

; Proof.  Cases on a.  We take even bigger steps than in Problem 24.

; Case 1. a /= nil.
; lhs = (if b x y) = rhs.

; Case 2. a  = nil.
; lhs = (if c x y) = rhs.

; Q.E.D.
"""}

:next-problem #{"""
; ------------------------------------------------------------
; Problem 26.

; Theorem.  (equal (treecopy (cons a b))
;                  (cons (treecopy a) (treecopy b)))

; Proof.  
; (treecopy (cons a b))
; =                                          {by def treecopy}
; (if (consp (cons a b))
;     (cons (treecopy (car (cons a b)))
;           (treecopy (cdr (cons a b))))
;     (cons a b))
; =                                          {by Axiom 7}
; (if t
;     (cons (treecopy (car (cons a b)))
;           (treecopy (cdr (cons a b))))
;     (cons a b))
; =                                          {by Axiom 1 and 2}
; (cons (treecopy (car (cons a b)))
;       (treecopy (cdr (cons a b))))
; =                                          {by Axiom 9 and 10}
; (cons (treecopy a)
;       (treecopy b))
; Q.E.D.
"""}

:next-problem #{"""
; ------------------------------------------------------------
; Problem 27.

; Proof that 'June = 'July (under the scenario described).

; While f is defined as initially shown, prove the theorem
; Lemma 1:  (f x) = 1.
; After the redefinition, prove the theorem
; Lemma 2:  (f x) = 2.
; By transitivity of equality and Lemmas 1 and 2, 1 = 2.  By
; propositional calculus, we know that if p is a theorem, then 
; -p -> q.  Hence,

; (1 /= 2) -> 'June = 'July.

; By Axiom 1, we know (1 /= 2).  Thus

; 'June = 'July

; Q.E.D.

; Of course, the crux of this proof is establishing any contradiction.
"""}

:next-problem #{"""
; ------------------------------------------------------------
; Problem 28.

; Proof that 1=2, given (defun f (x) (cons x y)).

; Lemma 1: (cdr (f x)) = y.  Proof: by def f and Axiom 10.  Q.E.D.

; Thus, 
; (cdr (f 0)) = 1             {instantiation of Lemma 1}
; (cdr (f 0)) = 2             {instantiation of Lemma 1}
; 1=2                         {trans of equality}
; Q.E.D.
"""}

:next-problem #{"""
; ------------------------------------------------------------
; Problem 29.

; Proof that t=nil, given (defun f (x) (not (f x))).

; First observe that (f x) = (if (f x) nil t).  Thus,
; since (f x) = nil or (f x) /= nil, we know that (f x)
; is either t or nil.  Consider the two cases.

; Case 1.  (f x) = t
; (f x) = (not (f x)) = (not t) = nil.

; Case 2.  (f x) = nil
; (f x) = (not (f x)) = (not nil) = t.

; Q.E.D.
"""}

:next-problem #{"""
; ------------------------------------------------------------
; Problem 30.

; Restriction 1 prevents the redefinition of a function, as 
; tried in Problem 27.  Restriction 3 prevents the use of
; global variables, as tried in Problem 28.  Restriction 4
; prevents the silly recursion, as tried in Problem 29.
; In particular, x is not a car/cdr nest and so the definition
; of f in Problem 29 is not acceptable.
"""}

:next-problem #{"""
; ------------------------------------------------------------
; Problem 31.

; Yes, because (consp x) governs a recursive call on a
; car/cdr nest around x, namely the nest (cdr (cdr x)).
"""}

:next-problem #{"""
; ------------------------------------------------------------
; Problem 32.

; No.  The only governing test is (consp x) but the only car/cdr nest
; in recursion is around y.
"""}

:next-problem #{"""
; ------------------------------------------------------------
; Problem 33.

; No.  The governing test, (consp x), is for the first formal but the
; first actual in the recursive call is not a car/cdr nest around the
; first formal.  This is how the answer to Problem 32 should read!
"""}

:next-problem #{"""
; ------------------------------------------------------------
; Problem 34.

; Yes, because (consp x) governs the recursive call (f (cdr (cdr x))),
; and (cdr (cdr x)) is a car/cdr nest around x.
"""}

:next-problem #{"""
; ------------------------------------------------------------
; Problem 35.

; No, because (consp x) does not govern the recursive call.  There is
; no (consp x) anywhere in the definition.  (not (endp x)) governs the
; recursive call and that is not syntactically identical to (consp x).
; However, it is logically equivalent and we will eventually improve
; the definitional principle so that definitions like this are
; allowed.
"""}

:next-problem #{"""
; ------------------------------------------------------------
; Problem 36.

; Yes.  (Consp x) governs a recursion on a car/cdr nest around x in
; the same argument position.  It doesn't matter what happens in other
; argument positions.
"""}

:next-problem #{"""
; ------------------------------------------------------------
; Problem 37.

; Yes, for the same reasons given in Problem 36.
"""}

:next-problem #{"""
; ------------------------------------------------------------
; Problem 38.

; No.  If the first defun is allowed, then we have an axiom about g
; and then we cannot ``redefine'' g with the second defun.  The
; symmetric argument blocks defining them in the other order.
"""}

:next-problem #{"""
; ------------------------------------------------------------
; Problem 39.

; No.  All we can do is expand the definition of (f x) and
; consider cases.  We can construct a ``proof'' like this:

; Case 1:  (not (consp x))
; (f x) = t {by def f} 

; Case 2:  (consp x) & (not (consp (cdr x)))
; (f x) = (f (cdr x)) = t {by def f}

; Case 3:  (consp x) & (consp (cdr x)) & (not (consp (cdr (cdr x))))
; (f x) = (f (cdr x)) = (f (cdr (cdr x))) = t {by def f}

; ...

; But we cannot reach the terminal case of the recursion unless we
; know how long x is.  And infinite proofs are not allowed!
"""}

:next-problem #{"""
; ------------------------------------------------------------
; Problem 40.
"""}

(theorem problem-40
         (equal (app (app a b) c)
                (app a (app b c)))

         :hints (("Goal"
                  :induct (app a b))))

:----- #{"""
; Here is the proof output you'll find for this event in
; r-and-i-answer-key-log.txt.  Notice that the output prints the
; induction scheme suggested by the hint.

[Note:  A hint was supplied for the goal above.  Thanks!]

Name the formula above *1.

We have been told to use induction.  One induction scheme is suggested
by the induction hint.  

We will induct according to a scheme suggested by (APP A B).  This
suggestion was produced using the :induction rule APP.  If we let (:P A B C)
denote *1 above then the induction scheme we'll use is
(AND (IMPLIES (NOT (CONSP A)) (:P A B C))
     (IMPLIES (AND (CONSP A) (:P (CDR A) B C))
              (:P A B C))).
This induction is justified by the same argument used to admit APP.
When applied to the goal at hand the above induction scheme produces
two nontautological subgoals.

Subgoal *1/2
(IMPLIES (NOT (CONSP A))
         (EQUAL (APP (APP A B) C)
                (APP A (APP B C)))).

But simplification reduces this to T, using the :definition APP and
primitive type reasoning.

Subgoal *1/1
(IMPLIES (AND (CONSP A)
              (EQUAL (APP (APP (CDR A) B) C)
                     (APP (CDR A) (APP B C))))
         (EQUAL (APP (APP A B) C)
                (APP A (APP B C)))).

But simplification reduces this to T, using the :definition APP, primitive
type reasoning and the :rewrite rules CAR-CONS and CDR-CONS.

That completes the proof of *1.

Q.E.D.

Summary
Form:  ( DEFTHM PROBLEM-40 ...)
Rules: ((:DEFINITION APP)
        (:FAKE-RUNE-FOR-TYPE-SET NIL)
        (:INDUCTION APP)
        (:REWRITE CAR-CONS)
        (:REWRITE CDR-CONS))
"""}

:next-problem #{"""
; ------------------------------------------------------------
; Problem 41.
"""}

(theorem problem-41-counterexample
  (not (equal (app '(1 2 3 . 4) nil)
              '(1 2 3 . 4)))
  :rule-classes nil)

(defun proper (x)
  (if (consp x)
      (proper (cdr x))
    (equal x nil)))

(theorem problem-41
         (implies (proper x)
                  (equal (app x nil) x))

         :hints (("Goal"
                  :induct (app x nil))))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 42.
"""}

(theorem problem-42
         (equal (mapnil (app a b))
                (app (mapnil a) (mapnil b)))

         :hints (("Goal"
                  :induct (app a b))))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 43.
"""}

(theorem problem-43
         (equal (rev (mapnil x))
                (mapnil (rev x)))

         :hints (("Goal"
                  :induct (rev x)
                  :in-theory (enable problem-42))))

:----- #{"""
; Here is the output generated.  You can find similar output
; for every theorem in r-and-i-answer-key-log.txt.  Note that
; the output makes it clear which subgoal requires Problem-42.
; In addition, the output lists all the rules used.  All of
; the rules below, except Problem-42, are part of the basic
; propositional calculus, axioms, definitions, and
; arithmetic we allow ourselves to use freely in Recursion
; and Induction.

[Note:  A hint was supplied for the goal above.  Thanks!]

Name the formula above *1.

We have been told to use induction.  One induction scheme is suggested
by the induction hint.  

We will induct according to a scheme suggested by (REV X).  This suggestion
was produced using the :induction rule REV.  If we let (:P X) denote
*1 above then the induction scheme we'll use is
(AND (IMPLIES (NOT (CONSP X)) (:P X))
     (IMPLIES (AND (CONSP X) (:P (CDR X)))
              (:P X))).
This induction is justified by the same argument used to admit REV.
When applied to the goal at hand the above induction scheme produces
two nontautological subgoals.

Subgoal *1/2
(IMPLIES (NOT (CONSP X))
         (EQUAL (REV (MAPNIL X))
                (MAPNIL (REV X)))).

But simplification reduces this to T, using the :definitions MAPNIL
and REV and the :executable-counterparts of EQUAL, MAPNIL and REV.

Subgoal *1/1
(IMPLIES (AND (CONSP X)
              (EQUAL (REV (MAPNIL (CDR X)))
                     (MAPNIL (REV (CDR X)))))
         (EQUAL (REV (MAPNIL X))
                (MAPNIL (REV X)))).

But simplification reduces this to T, using the :definitions MAPNIL
and REV, the :executable-counterparts of CONS and MAPNIL, primitive
type reasoning, the :rewrite rules CAR-CONS, CDR-CONS and PROBLEM-42
and the :type-prescription rule MAPNIL.

That completes the proof of *1.

Q.E.D.

Summary
Form:  ( DEFTHM PROBLEM-43 ...)
Rules: ((:DEFINITION MAPNIL)
        (:DEFINITION REV)
        (:EXECUTABLE-COUNTERPART CONS)
        (:EXECUTABLE-COUNTERPART EQUAL)
        (:EXECUTABLE-COUNTERPART MAPNIL)
        (:EXECUTABLE-COUNTERPART REV)
        (:FAKE-RUNE-FOR-TYPE-SET NIL)
        (:INDUCTION REV)
        (:REWRITE CAR-CONS)
        (:REWRITE CDR-CONS)
        (:REWRITE PROBLEM-42)
        (:TYPE-PRESCRIPTION MAPNIL))
"""}

:next-problem #{"""
; ------------------------------------------------------------
; Problem 44.
"""}

(theorem problem-44-counterexample
  (not (equal (rev (rev '(1 2 3 . 4)))
              '(1 2 3 . 4)))
  :rule-classes nil)

(theorem problem-44c
         (equal (proper (app a b))
                (proper b))

         :hints (("Goal"
                  :induct (app a b))))

(theorem problem-44b
         (proper (rev x))

         :hints (("Goal"
                  :induct (rev x)
                  :in-theory (enable problem-44c))))

(theorem problem-44a
         (equal (rev (app a b))
                (app (rev b) (rev a)))

         :hints (("Goal"
                  :induct (app a b)
                  :in-theory (enable problem-40
                                     problem-41
                                     problem-44b))))

(theorem problem-44
         (implies (proper x)
                  (equal (rev (rev x)) x))

         :hints (("Goal"
                  :induct (rev x)
                  :in-theory (enable problem-44a))))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 45.
"""}

(theorem problem-45
         (equal (swap-tree (swap-tree x)) x)

         :hints (("Goal"
                  :induct (swap-tree x))))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 46.
"""}

(theorem problem-46
         (equal (mem e (app a b))
                (or (mem e a)
                    (mem e b)))

         :hints (("Goal"
                  :induct (mem e a))))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 47.
"""}

(theorem problem-47a
         (implies (not (mem e b))
                  (not (mem e (int a b))))

         :hints (("Goal"
                  :induct (int a b))))

(theorem problem-47
         (equal (mem e (int a b))
                (and (mem e a)
                     (mem e b)))

         :hints (("Goal"
                  :induct (mem e a)
                  :in-theory (enable problem-47a))))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 48.
"""}

(theorem problem-48a
         (implies (sub x (cdr y))
                  (sub x y))

         :hints (("Goal"
                  :induct (sub x y))))

(theorem problem-48
         (sub x x)

         :hints (("Goal"
                  :induct (sub x x)
                  :in-theory (enable problem-48a))))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 49.
"""}

(theorem problem-49a
         (implies (and (mem e a)
                       (sub a b))
                  (mem e b))

         :hints (("Goal"
                  :induct (sub a b))))

(theorem problem-49
         (implies (and (sub a b)
                       (sub b c))
                  (sub a c))

         :hints (("Goal"
                  :induct (sub a c)
                  :in-theory (enable problem-48a
                                     problem-49a))))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 50.
"""}

(theorem problem-50a
         (equal (sub (app a b) c)
                (and (sub a c)
                     (sub b c)))

         :hints (("Goal"
                  :induct (sub a c)
                  :in-theory (enable problem-48a
                                     problem-48
                                     problem-49a))))

(theorem problem-50
         (sub (app a a) a)

         :hints (("Goal"
                  :in-theory (enable problem-48
                                     problem-50a)))
         :rule-classes nil)

:next-problem #{"""
; ------------------------------------------------------------
; Problem 51.
"""}

(defun mapnil1 (x a)
  (if (consp x)
      (mapnil1 (cdr x) (cons nil a))
    a))

(defun nils (x)
  (if (consp x)
      (and (equal (car x) nil)
           (nils (cdr x)))
    (equal x nil)))

(theorem problem-51a
         (equal (mapnil1 a (cons nil b))
                (cons nil (mapnil1 a b)))

         :hints (("Goal"
                  :induct (mapnil1 a b))))

(theorem problem-51b
         (implies (nils x)
                  (equal (mapnil1 x nil) x))

         :hints (("Goal"
                  :induct (nils x)
                  :in-theory (enable problem-51a))))

(theorem problem-51
         (implies (and (nils a)
                       (nils b))
                  (equal (mapnil1 a b)
                         (mapnil1 b a)))

         :hints (("Goal"
                  :induct (mapnil1 a b)
                  :in-theory (enable problem-51a
                                     problem-51b))))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 52.
"""}

(defun rm (x y)
  (if (consp y)
      (if (equal x (car y))
	  (cdr y)
	(cons (car y) (rm x (cdr y))))
    y))

(defun perm (x y)
  (if (consp x)
      (and (mem (car x) y)
           (perm (cdr x) (rm (car x) y)))
      (not (consp y))))



:next-problem #{"""
; ------------------------------------------------------------
; Problem 53.
"""}

(theorem problem-53
         (perm x x)

         :hints (("Goal"
                  :induct (perm x x))))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 54.
"""}

(theorem problem-54a
         (implies (mem a x)
                  (equal (perm x (cons a y))
                         (perm (rm a x) y)))

         :hints (("Goal"
                  :induct (perm x y))))

(theorem problem-54
         (implies (perm x y)
                  (perm y x))

         :hints (("Goal"
                  :induct (perm x y)
                  :in-theory (enable problem-54a))))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 55.
"""}

(theorem problem-55a
         (implies (mem a (rm b x))
                  (mem a x))

         :hints (("Goal"
                  :induct (mem a x))))

(theorem problem-55b
         (implies (and (perm x y)
                       (mem a x))
                  (mem a y))

         :hints (("Goal"
                  :induct (perm x y)
                  :in-theory (enable problem-55a))))

(theorem problem-55c
         (equal (rm a (rm b x))
                (rm b (rm a x)))

         :hints (("Goal"
                  :induct (rm a x))))

(theorem problem-55d
         (implies (and (not (equal e d))
                       (mem e x))
                  (mem e (rm d x)))

         :hints (("Goal"
                  :induct (rm d x)
                  :in-theory (enable problem-48
                                     problem-49a))))

(theorem problem-55e
         (implies (perm x y)
                  (perm (rm a x)
                        (rm a y)))

         :hints (("Goal"
                  :induct (perm x y)
                  :in-theory (enable problem-54a
                                     problem-54
                                     problem-55c
                                     problem-55d))))

(theorem problem-55
         (implies (and (perm x y)
                       (perm y z))
                  (perm x z))

         :hints (("Goal"
                  :induct (list (perm x y) (perm x z))
                  :in-theory (enable problem-54
                                     problem-55b
                                     problem-55e))))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 56.

; Before Problem 56, the document introduces a total order.  As
; explained in the document, <<= is intended as a more suggestive name
; than the ACL2 built-in relation lexorder.
"""}

(defun <<= (x y) (lexorder x y))

:----- #{"""
; ACL2 has already proved that lexorder is a non-strict total order.
; So this lemma about <<= is trival and unnecessary.
"""}

(theorem <<=-non-strict-total-order
  (and (<<= x x)
       (implies (and (<<= x y)
                     (<<= y x))  
                (equal x y))
       (implies (and (<<= x y)
                     (<<= y z))
                (<<= x z))
       (or (<<= x y)
           (<<= y x)))
  :rule-classes nil)

:----- #{"""
; Here is the solution to Problem 56.
"""}

(defun ordered (x)
  (if (consp x)
      (if (consp (cdr x))
          (and (<<= (car x) (cadr x))
               (ordered (cdr x)))
        t)
    t))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 57.
"""}

(defun insert (e x)
  (if (consp x)
      (if (<<= e (car x))
          (cons e x)
        (cons (car x)
              (insert e (cdr x))))
    (cons e nil)))

(defun isort (x)
  (if (consp x)
      (insert (car x)
              (isort (cdr x)))
    nil))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 58.
"""}

(theorem problem-58a
         (implies (ordered x)
                  (ordered (insert e x)))

         :hints (("Goal"
                  :induct (insert e x))))

(theorem problem-58
         (ordered (isort x))

         :hints (("Goal"
                  :induct (isort x)
                  :in-theory (enable problem-58a))))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 59.

; I will show two proofs of Problem 59.
"""}

(theorem problem-59a
         (implies (perm a b)
                  (perm (insert e a) (cons e b)))

         :hints (("Goal"
                  :induct (perm a b)
                  :in-theory (enable problem-48
                                     problem-49a
                                     problem-54a
                                     problem-54))))

(theorem problem-59
         (perm (isort x) x)

         :hints (("Goal"
                  :induct (isort x)
                  :in-theory (enable problem-54
                                     problem-59a))))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 60.
"""}

(theorem problem-60a
         (equal (insert e (insert d x))
                (insert d (insert e x)))

         :hints (("Goal"
                  :induct (insert e x))))
  
(theorem problem-60b
         (equal (isort (app x (list e)))
                (insert e (isort x)))

         :hints (("Goal"
                  :induct (isort x)
                  :in-theory (enable problem-60a))))

(theorem problem-60c
         (equal (isort (rev x))
                (isort x))

         :hints (("Goal"
                  :induct (isort x)
                  :in-theory (enable problem-60b))))

(theorem problem-60d
         (equal (isort (insert e x))
                (insert e (isort x)))

         :hints (("Goal"
                  :induct (insert e x)
                  :in-theory (enable problem-60a))))

(theorem problem-60e
         (equal (isort (isort x))
                (isort x))

         :hints (("Goal"
                  :induct (isort x)
                  :in-theory (enable problem-60d))))

(theorem problem-60
         (equal (isort (rev (isort l)))
                (isort l))

         :hints (("Goal"
                  
                  :in-theory (enable problem-60c
                                     problem-60e))))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 61.
"""}

(defun rev1 (x a)
  (if (consp x)
      (rev1 (cdr x) (cons (car x) a))
    a))

(theorem problem-61a
         (equal (rev1 x a) (app (rev x) a))

         :hints (("Goal"
                  :induct (rev1 x a)
                  :in-theory (enable problem-40))))

(theorem problem-61
         (equal (rev1 x nil) (rev x))

         :hints (("Goal"
                  :in-theory (enable problem-41
                                     problem-44b
                                     problem-61a))))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 62.

; This proof relies on the neat fact that we proved above
; (mapnil1 a (cons nil b)) = (cons nil (mapnil1 a b)).
"""}

(theorem problem-62-direct
         (equal (mapnil1 a nil)
                (mapnil a))

         :hints (("Goal"
                  :induct (mapnil a)
                  :in-theory (enable problem-51a))))

:----- #{"""
; Generally, a better way to prove theorems like this is to
; generalize the accumulator.  Here is that proof.
  
; Note that the following lemma is the analogue of the neat fact
; mentioned above.
"""}

(theorem problem-62a
         (implies (nils a)
                  (equal (app a (cons nil b))
                         (cons nil (app a b))))

         :hints (("Goal"
                  :induct (app a b))))

(theorem problem-62b
         (nils (mapnil a))

         :hints (("Goal"
                  :induct (mapnil a))))

(theorem problem-62c
         (equal (mapnil1 a b)
                (app (mapnil a) b))

         :hints (("Goal"
                  :induct (mapnil1 a b)
                  :in-theory (enable problem-62a
                                     problem-62b))))

(theorem problem-62d
         (proper (mapnil a))

         :hints (("Goal"
                  :induct (mapnil a))))

(theorem problem-62
         (equal (mapnil1 a nil)
                (mapnil a))

         :hints (("Goal"
                  :in-theory (enable problem-41
                                     problem-62c
                                     problem-62d))))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 63.

; Theorem. x /= (cons x y)

; This fact is built into ACL2 so its proof is not enlightening.  But
; here a primitive proof and it is enlightening because it requires
; induction.

; Proof.  Induction on x.  

; Base Case:  
; (implies (not (consp x))
;          (not (equal x (cons x y))))
; Trivial, since the contrapositive is true by Axiom 7 and equality.

; Induction Step:
; (implies (and (consp x)
;               (not (equal (car x) (cons (car x) (cdr x)))))
;          (not (equal x (cons x y))))

; Note the unusual choice of induction hypothesis.  We replaced x
; by (car x) and y by (cdr x).
         
; So, given (consp x) and Axiom 11, the induction hypothesis becomes

;                (not (equal (car x) x))

; Now suppose the conclusion is false.  That is, suppose
; x = (cons x y).  Then take the car of both sides and we get
; (car x) = x.  Which is contradicted by the induction hypothesis.

; Q.E.D.
"""}

:next-problem #{"""
; ------------------------------------------------------------
; Problem 64.
"""}

(defun mcflatten (x a)
  (if (consp x)
      (mcflatten (car x)
                 (mcflatten (cdr x) a))
    (cons x a)))

(theorem problem-64a
         (equal (mcflatten x a)
                (app (flatten x) a))

         :hints (("Goal"
                  :induct (mcflatten x a)
                  :in-theory (enable problem-40))))

(theorem problem-64b
         (proper (flatten x))

         :hints (("Goal"
                  :induct (flatten x)
                  :in-theory (enable problem-44c))))

(theorem problem-64
         (equal (mcflatten x nil)
                (flatten x))

         :hints (("Goal"
                  :in-theory (enable problem-41
                                     problem-64a
                                     problem-64b))))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 65.
"""}

(defun nat (x)
  (if (consp x)
      (and (equal (car x) nil)
           (nat (cdr x)))
    (equal x nil)))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 66.
"""}

(defun fix-nat (x)
  (if (consp x)
      (cons nil (fix-nat (cdr x)))
    nil))

:----- #{"""
; It is useful to know
"""}

(theorem problem-66a
         (nat (fix-nat x))

         :hints (("Goal"
                  :induct (fix-nat x))))

(theorem problem-66b
         (implies (nat x)
                  (equal (fix-nat x) x))

         :hints (("Goal"
                  :induct (nat x))))

(defun plus (x y)
  (if (consp x)
      (cons nil (plus (cdr x) y))
    (fix-nat y)))

(theorem problem-66c
         (nat (plus x y))

         :hints (("Goal"
                  :induct (plus x y)
                  :in-theory (enable problem-66a))))

(theorem problem-66d
         (equal (plus (fix-nat i) j)
                (plus i j))

         :hints (("Goal"
                  :induct (plus i j))))

(theorem problem-66e
         (equal (plus i (fix-nat j))
                (plus i j))

         :hints (("Goal"
                  :induct (plus i j)
                  :in-theory (enable problem-66a
                                     problem-66b))))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 67.
"""}

(defun times (x y)
  (if (consp x)
      (plus y (times (cdr x) y))
    nil))

(theorem problem-67a
         (nat (times x y))

         :hints (("Goal"
                  :induct (times x y)
                  :in-theory (enable problem-66c))))

(theorem problem-67b
         (equal (times (fix-nat i) j)
                (times i j))

         :hints (("Goal"
                  :induct (times i j))))

(theorem problem-67c
         (equal (times i (fix-nat j))
                (times i j))

         :hints (("Goal"
                  :induct (times i j)
                  :in-theory (enable problem-66d))))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 68.
"""}

(defun power (x y)
  (if (consp y)
      (times x (power x (cdr y)))
    '(nil)))

(theorem problem-68a
         (nat (power x y))

         :hints (("Goal"
                  :induct (power x y)
                  :in-theory (enable problem-67a))))

(theorem problem-68b
         (equal (power (fix-nat i) j)
                (power i j))

         :hints (("Goal"
                  :induct (power i j)
                  :in-theory (enable problem-67b))))

(theorem problem-68c
         (equal (power i (fix-nat j))
                (power i j))

         :hints (("Goal"
                  :induct (power i j))))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 69.
"""}

(defun lesseqp (x y)
  (if (consp x)
      (if (consp y)
          (lesseqp (cdr x) (cdr y))
        nil)
    t))

(theorem problem-69a
         (equal (lesseqp (fix-nat i) j)
                (lesseqp i j))

         :hints (("Goal"
                  :induct (list (fix-nat i) (lesseqp i j)))))

:----- #{"""
; Note: I included (fix-nat i) in the :induct hint above because it
; forces the theorem prover to expand (fix-nat i) in the conclusion of
; the induction step.
"""}

(theorem problem-69b
         (equal (lesseqp i (fix-nat j))
                (lesseqp i j))

         :hints (("Goal"
                  :induct (lesseqp i j))))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 70.
"""}

(defun evennat (x)
  (if (consp x)
      (if (consp (cdr x))
          (evennat (cdr (cdr x)))
        nil)
    t))

(theorem problem-70a
         (equal (evennat (fix-nat i))
                (evennat i))

         :hints (("Goal"
                  :induct (evennat i))))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 71.

; We start with a general 0-identity for plus.
"""}

(theorem problem-71a
         (implies (not (consp z))
                  (equal (plus i z) (fix-nat i)))

         :hints (("Goal"
                  :induct (plus i z))))

(theorem problem-71
         (implies (nat i)
                  (equal (plus i nil) i))

         :hints (("Goal"
                  :in-theory (enable problem-66b
                                     problem-71a))))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 72.
"""}

(theorem problem-72
         (equal (plus (plus i j) k)
                (plus i (plus j k)))

         :hints (("Goal"
                  :induct (plus i j)
                  :in-theory (enable problem-66b
                                     problem-66c
                                     problem-66d))))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 73.
"""}

(theorem problem-73a
         (equal (plus i (cons x j))
                (cons nil (plus i j)))

         :hints (("Goal"
                  :induct (plus i j))))

(theorem problem-73
         (equal (plus i j)
                (plus j i))

         :hints (("Goal"
                  :induct (plus i j)
                  :in-theory (enable problem-71a
                                     problem-73a))))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 74.

; Note that the following lemma is another sense of commutativity
; for plus.
"""}

(theorem problem-74a
         (equal (plus i (plus j k))
                (plus j (plus i k)))

         :hints (("Goal"
                  :induct (plus j k)
                  :in-theory (enable problem-66b
                                     problem-66c
                                     problem-66e
                                     problem-73a))))

(theorem problem-74b
         (equal (times (plus i j) k)
                (plus (times i k) (times j k)))

         :hints (("Goal"
                  :induct (times i k)
                  :in-theory (enable problem-66b
                                     problem-67a
                                     problem-67b
                                     problem-73
                                     problem-74a))))

(theorem problem-74
         (equal (times (times i j) k)
                (times i (times j k)))

         :hints (("Goal"
                  :induct (times i j)
                  :in-theory (enable problem-74b))))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 75.
"""}

(theorem problem-75a
         (implies (not (consp z))
                  (equal (times i z) nil))

         :hints (("Goal"
                  :induct (times i z)
                  :in-theory (enable problem-71a
                                     problem-73))))

(theorem problem-75b
         (equal (times i (cons x j))
                (plus i (times i j)))

         :hints (("Goal"
                  :induct (times i j)
                  :in-theory (enable problem-71a
                                     problem-73
                                     problem-74a))))

(theorem problem-75
         (equal (times i j)
                (times j i))

         :hints (("Goal"
                  :induct (times i j)
                  :in-theory (enable problem-75a
                                     problem-75b))))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 76.
"""}

(theorem problem-76a
         (equal (times i (plus j k))
                (plus (times i j)
                      (times i k)))

         :hints (("Goal"
                  :induct (times i k)
                  :in-theory (enable problem-72
                                     problem-73
                                     problem-74a))))

(theorem problem-76b
         (equal (times i (times j k))
                (times j (times i k)))

         :hints (("Goal"
                  :induct (times j k)
                  :in-theory (enable problem-75
                                     problem-76a))))

(theorem problem-76
         (equal (power b (plus i j))
                (times (power b i)
                       (power b j)))

         :hints (("Goal"
                  :induct (power b i)
                  :in-theory (enable problem-66b
                                     problem-68a
                                     problem-68c
                                     problem-73
                                     problem-75
                                     problem-76b))))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 77.
"""}

(theorem problem-77a
         (equal (power (times a b) i)
                (times (power a i)
                       (power b i)))

         :hints (("Goal"
                  :induct (power b i)
                  :in-theory (enable problem-74
                                     problem-75
                                     problem-76b))))

(theorem problem-77b
         (equal (power '(nil) i)
                '(nil))

         :hints (("Goal"
                  :induct (power '(nil) i))))

(theorem problem-77
         (equal (power (power b i) j)
                (power b (times i j)))

         :hints (("Goal"
                  :induct (times i j)
                  :in-theory (enable problem-76
                                     problem-77a
                                     problem-77b))))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 78.
"""}

(theorem problem-78
         (lesseqp i i)

         :hints (("Goal"
                  :induct (lesseqp i i))))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 79.
"""}

(theorem problem-79
         (implies (and (lesseqp i j)
                       (lesseqp j k))
                  (lesseqp i k))

         :hints (("Goal"
                  :induct (list (lesseqp i j) (lesseqp i k)))))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 80.
"""}

(theorem problem-80
         (equal (lesseqp (plus i j) (plus i k))
                (lesseqp j k))

         :hints (("Goal"
                  :induct (plus i k)
                  :in-theory (enable problem-69a
                                     problem-69b))))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 81.
"""}

(theorem problem-81
         (implies (and (evennat i)
                       (evennat j))
                  (evennat (plus i j)))

         :hints (("Goal"
                  :induct (evennat i)
                  :in-theory (enable problem-70a
                                     problem-73
                                     problem-73a))))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 82.
"""}

(defun cc (x)
  (if (consp x)
      (+ 1
         (+ (cc (car x))
            (cc (cdr x))))
    0))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 83.

; Theorem. (natp (cc x))

; Proof.  Induction on x.
; Base Case:  (not (consp x))
; (cc x) = 0.
; (natp 0) = t.

; Induction Step:
;  (implies (and (consp x)
;                (natp (cc (car x)))
;                (natp (cc (cdr x))))
;           (natp (cc x)))

; The conclusion becomes
; (natp (+ 1 (+ (cc (car x)) (cc (cdr x)))))
; and that follows by standard arithmetic from the induction
; hypotheses.

; Q.E.D.
"""}

:next-problem #{"""
; ------------------------------------------------------------
; Problem 84.

; (and (implies (consp x)
;               (< (cc (car x)) (cc x)))
;      (implies (consp x)
;               (< (cc (cdr x)) (cc x))))
"""}

:next-problem #{"""
; ------------------------------------------------------------
; Problem 85.
"""}

(theorem problem-85
  (and (implies (consp x)
                (< (cc (car x)) (cc x)))
       (implies (consp x)
                (< (cc (cdr x)) (cc x))))
  :rule-classes :linear)

:next-problem #{"""
; ------------------------------------------------------------
; Problem 86.

; Del was defined when we introduced perm.
"""}

:next-problem #{"""
; ------------------------------------------------------------
; Problem 87.
"""}

(theorem problem-87a
         (implies (mem e x)
                  (< (cc (rm e x)) (cc x)))

         :hints (("Goal"
                  :induct (cc x)))
         :rule-classes :linear)

(defun f23 (e x)
  (declare (xargs :measure (cc x)))
  (if (mem e x)
      (f23 e (rm e x))
    23))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 88.

; No.  We need to inductively assume that (f23 e (rm e x))
; is 23 to prove that (f e x) is 23.
"""}

:next-problem #{"""
; ------------------------------------------------------------
; Problem 89.

; Ack terminates because the second argument always either gets
; smaller or stays fixed.  But when the second argument stays fixed,
; the first argument gets smaller.
"""}

:next-problem #{"""
; ------------------------------------------------------------
; Problem 90.

; Rather than just show the answers, I write a function that computes
; the answers.  Again, I feel free to use any feature of ACL2, since
; this question did not require you to write these functions.

; By loading the file (certified book) below, we can write
; ordinals in a nice algebraic notation.  Thus, I can create my
; examples by writing expressions like:

;  (o+ (o* (o^ (omega) 3) 5)
;      (o* (o^ (omega) 1) 25)
;      7)

; instead of ((3 . 5) (1 . 25) . 7).
"""}

(include-book "ordinals/ordinals" :dir :system)

:----- #{"""
; So here is the function that checks a pair of ordinals and announces
; whether a<b, a=b, or a>b.
"""}

(defun check-row (row)
  (let ((a (car row))
        (b (cadr row)))
    (if (and (o-p a)
             (o-p b))
        (if (o< a b) 'a<b
            (if (equal a b) 'a=b 'b<a))
      'not-o-p!)))

:----- #{"""
; And now I check all the rows in a table.
; Here are the rows of that table with suitable column headers.

;                 a                      b                           answer
;           (1    23                     100                         A<B)
;           (2    1000000                ((1 . 1) . 0)               A<B)
;           (3    ((2 . 1) . 0)          ((1 . 2) . 0)               B<A)
;           (4    ((3 . 5) (1 . 25) . 7) ((3 . 5) (2 . 1) . 3)       A<B)
;           (5    ((((2 . 1) . 0) . 5) . 3)
;                                        ((((1 . 1) . 0) . 5) (1 . 25) . 7)
                                                                     B<A)))

"""}

(defun check-all-rows (tab i)
  (if (endp tab)
      nil
    (cons (list i (car (car tab)) (cadr (car tab)) (check-row (car tab)))
          (check-all-rows (cdr tab) (+ 1 i)))))

(theorem problem-90
         (equal
          (check-all-rows
           (list (list 23  100)
                 (list 1000000 (omega))
                 (list (o^ (omega) 2)
                       (o* (omega) 2))
                 (list (o+ (o* (o^ (omega) 3) 5)
                           (o* (o^ (omega) 1) 25)
                           7)
                       (o+ (o* (o^ (omega) 3) 5)
                           (o* (o^ (omega) 2) 1)
                           3))
                 (list (o+ (o* (o^ (omega) (o^ (omega) 2)) 5)
                           3)
                       (o+ (o* (o^ (omega) (omega)) 5)
                           (o* (o^ (omega) 1) 25)
                           7)))
           1)
;                 a                      b                           answer

          '((1    23                     100                         A<B)
            (2    1000000                ((1 . 1) . 0)               A<B)
            (3    ((2 . 1) . 0)          ((1 . 2) . 0)               B<A)
            (4    ((3 . 5) (1 . 25) . 7) ((3 . 5) (2 . 1) . 3)       A<B)
            (5    ((((2 . 1) . 0) . 5) . 3)
                                         ((((1 . 1) . 0) . 5) (1 . 25) . 7)
                                                                     B<A)))

         :hints (("Goal"
                  :in-theory (enable (omega)
                                     (o^)
                                     (o*)
                                     (o+))))
         :rule-classes nil)

:next-problem #{"""
; ------------------------------------------------------------
; Problem 91.

; Note: If you don't load the ordinals book, you can skip enabling of
; all the functions below!  They're normally enabled.  The book disables
; them so that you can use the algebraic notation and lemmas will fire
; to simplify.  Since I used raw conses to construct my ordinals, I'm
; having to fight the book here.  But by using raw conses, you can
; construct a simple proof without the ordinals book.
"""}

(defun m2 (i j)
  (cons (cons 1 (+ 1 i)) j))

(in-theory (enable o< o+ o* omega (omega)
                   o-first-coeff
                   o-first-expt
                   o-rst
                   o-finp
                   o-p))

(theorem problem-91
  (implies (and (natp i1)
                (natp j1)
                (natp i2)
                (natp j2))
           (and (o-p (m2 i1 j1))
                (iff (o< (m2 i1 j1)
                         (m2 i2 j2))
                     (if (equal i1 i2)
                         (< j1 j2)
                       (< i1 i2)))))
  :rule-classes nil)

:next-problem #{"""
; ------------------------------------------------------------
; Problem 92.

; The longest decreasing chain from 10 has eleven ordinals in it:

; 0 o< 1 o< 2 o< 3 o< 4 o< 5 o< 6 o< 7 o< 8 o< 9 o< 10

; There is no longest decreasing chain from ((1 . 1) . 0).  But any
; step down from that will be a finite natural, e.g., n.  And from n
; down to 0 there can be at most n+1 steps.  So any decreasing chain
; from ((1 . 1) . 0) will have at most n+2 ordinals in it, but n can
; be an arbitrary natural.
"""}

:next-problem #{"""
; ------------------------------------------------------------
; Problem 93.

; The following infinite chain is decreasing.

; x0 ((2 . 1) . 0)
; x1 ((1 . 1) (2 . 1) . 0)
; x2 ((1 . 1) (1 . 1) (2 . 1) . 0) 
; x3 ((1 . 1) (1 . 1) (1 . 1) (2 . 1) . 0) 
;    ...

; That is, ... x3 o< x2 o< x1 o< x0.  But all these objects (except
; x0) fail to be ordinals because the powers of omega do not decrease.
"""}

:next-problem #{"""
; ------------------------------------------------------------
; Problem 94.

; For a proof of the well-foundedness of our ordinals, see the ACL2
; online documentation topic proof-of-well-foundedness:

; http://www.cs.utexas.edu/users/moore/acl2/manuals/latest/index.html?topic=ACL2____PROOF-OF-WELL-FOUNDEDNESS

; Alternatively, go to the ACL2 home page

; http://www.cs.utexas.edu/users/moore/acl2

; and click on
; > The User's Manual
;  > Index of all documented topics
;   > P
;    > PROOF-OF-WELL-FOUNDEDNESS
"""}

:next-problem #{"""
; ------------------------------------------------------------
; Problem 95.

; The measure is cc.  The measure conjecture is
"""}

(theorem problem-95
         (and (o-p (cc x))
              (implies (not (atom x))
                       (o< (cc (first x)) (cc x)))
              (implies (not (atom x))
                       (o< (cc (rest x)) (cc x))))

         :hints (("Goal"
                  :in-theory (enable problem-85)))
         :rule-classes nil)

:----- #{"""
; To admit this definition of tree-copy with the measure cc we would
; write the following and it would generate and prove the conjecture
; above.  

; (defun tree-copy (x)
;   (declare (xargs :measure (cc x)))
;   (if (atom x)
;       x
;     (cons (tree-copy (first x))
;           (tree-copy (rest x)))))

; However we cannot, since tree-copy has already been defined.  For
; the other problems of this sort, we actually define the required
; function and you can look at the ACL2 output to see the measure
; conjectures and their proofs.
"""}

:next-problem #{"""
; ------------------------------------------------------------
; Problem 96.
"""}

(defun ack (x y)
  (declare (xargs :measure (m2 (nfix y) (nfix x))))
  (if (zp x)
      1
    (if (zp y)
        (if (equal x 1) 2 (+ x 2))
      (ack (ack (- x 1) y) (- y 1)))))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 97.

; The proof of the existence of non-primitive recursive functions
; has not been carried out in ACL2 but was carried out in the
; closely related Nqthm.  See

; http://www.cs.utexas.edu/users/boyer/ftp/nqthm/nqthm-1992/examples/basic/pr.events
"""}

:next-problem #{"""
; ------------------------------------------------------------
; Problem 98.
"""}

(defun f1 (i j)
  (declare (xargs :measure (if (and (natp i)
                                    (natp j)
                                    (< i j))
                               (- j i)
                             0)))
  (if (and (natp i)
           (natp j)
           (< i j))
      (f1 (+ 1 i) j)
    1))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 99.
"""}

(defun f2 (x)
  (declare (xargs :measure
                  (if (equal x nil)
                      0
                    (+ 1 (cc x)))))
  (if (equal x nil)
      2
    (and (f2 (car x))
         (f2 (cdr x)))))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 100.
"""}

(defun f3 (x y)
  (declare (xargs :measure (+ (cc x)
                              (cc y))))
  (if (and (endp x)
           (endp y))
      3
    (f3 (cdr x) (cdr y))))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 101.

; I'd like to thank Anand Padmanaban for helping me formulate and
; solve this problem.

; The following mysterious incantation makes ACL2 add a new axiom,
; called problem-101a, that constrains m, p, dn, and up as stated in
; the problem.  For example, dn represents ``down'', and we know
; nothing about (up x).
"""}

(encapsulate ((p (x) t)
              (dn (x) t)  ; dn = ``down''
              (up (x) t)
              (m (x) t))
             (local (defun p (x) (not (zp x))))
             (local (defun dn (x) (- x 1)))
             (local (defun up (x) x))
             (local (defun m (x) (acl2-count x)))
             (theorem problem-101a
               (and (o-p (m x))
                    (implies (p x)
                             (o< (m (dn x)) (m x))))))

(theorem problem-101b
         (implies (p x)
                  (not (equal (m x) 0)))

         :hints (("Goal"
                  :use
                  ((:instance problem-101a (x x))
                   (:instance problem-101a (x (dn x)))))))

(theorem problem-101c
         (or (consp (m x))
             (and (integerp (m x))
                  (<= 0 (m x))))
         :rule-classes :type-prescription

         :hints (("Goal"
                  :use
                  ((:instance problem-101a (x x))
                   (:instance problem-101a (x (dn x)))))))

(theorem problem-101d
         (implies (p x)
                  (not (equal (m (dn x)) (m x))))

         :hints (("Goal"
                  :use
                  ((:instance problem-101a (x x))
                   (:instance problem-101a (x (dn x)))))))

(defun f4 (x y q)
  (declare (xargs :measure (if q
                               (if (equal (m x) 0)
                                   1
                                 (make-ord (m x) 1 1))
                             (if (equal (m y) 0)
                                 2
                               (make-ord (m y) 1 2)))

                  :hints (("Goal"
                           :in-theory (enable problem-101a
                                              problem-101b)))))
  (if (p x)
      (if q
          (f4 y (dn x) (not q))
        (f4 y (up x) (not q)))
    4))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 102.
"""}

(theorem problem-102
         (equal (f1 i j) 1)

         :hints (("Goal"
                  :induct (f1 i j))))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 103.
"""}

(theorem problem-103
         (equal (f2 x) 2)

         :hints (("Goal"
                  :induct (f2 x))))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 104.
"""}

(theorem problem-104
         (equal (f3 x y) 3)

         :hints (("Goal"
                  :induct (f3 x y))))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 105.
"""}

(theorem problem-105
         (equal (f4 x y q) 4)

         :hints (("Goal"
                  :induct (f4 x y q))))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 106.
"""}

(defun flatten! (x)
  (declare (xargs :measure (m2 (cc x) (cc (car x)))))
  (if (atom x)
      (cons x nil)
    (if (atom (car x))
        (cons (car x) (flatten! (cdr x)))
      (flatten! (cons (caar x) (cons (cdar x) (cdr x)))))))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 107.
"""}

(theorem problem-107
         (equal (flatten! x) (flatten x))

         :hints (("Goal"
                  :induct (flatten! x)
                  :in-theory (enable problem-40))))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 108.
"""}

(defun gopher (x)
  (declare (xargs :measure (cc (car x))))
  (if (or (atom x)
          (atom (car x)))
      x
    (gopher (cons (car (car x))
                  (cons (cdr (car x))
                        (cdr x))))))

(theorem problem-108a
         (<= (cc (gopher x)) (cc x))

         :hints (("Goal"
                  :induct (gopher x)))
         :rule-classes :linear)

(theorem problem-108b
         (equal (consp (gopher x))
                (consp x))

         :hints (("Goal"
                  :induct (gopher x))))

(theorem problem-108c
         (implies (consp x)
                  (< (cc (cdr x)) (cc x)))

         :hints (("Goal"
                  :in-theory (enable problem-85)))
         :rule-classes :linear)

(theorem problem-108d
         (implies (consp x)
                  (< (cc (cdr (gopher x))) (cc x)))

         :hints (("Goal"
                  :in-theory (enable problem-108a
                                     problem-108b
                                     problem-108c))))

(defun samefringe (x y)
  (declare (xargs :measure (cc x)

                  :hints (("Goal"
                           :in-theory (enable problem-108d)))))
  (if (or (atom x)
          (atom y))
      (equal x y)
    (and (equal (car (gopher x))
                (car (gopher y)))
         (samefringe (cdr (gopher x))
                     (cdr (gopher y))))))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 109.
"""}

(theorem problem-109a
         (equal (car (gopher x))
                (if (consp x)
                    (car (flatten x))
                  nil))

         :hints (("Goal"
                  :induct (gopher x)
                  :in-theory (enable problem-40))))

(theorem problem-109b
         (equal (flatten (cdr (gopher x)))
                (if (consp x)
                    (cdr (flatten x))
                  (cons nil nil)))

         :hints (("Goal"
                  :induct (gopher x)
                  :in-theory (enable problem-40))))

(theorem problem-109c
         (equal (equal (cons e nil) (app x y))
                (if (consp x)
                    (and (equal (car x) e)
                         (atom (cdr x))
                         (equal y nil))
                  (equal y (cons e nil))))

         :hints (("Goal"
                  :induct (app x y))))

(theorem problem-109d
         (equal (equal (flatten x) (cons a nil))
                (and (atom x)
                     (equal x a)))

         :hints (("Goal"
                  :induct (flatten x)
                  :in-theory (enable problem-40
                                     problem-109c))))

(theorem problem-109
         (equal (samefringe x y)
                (equal (flatten x)
                       (flatten y)))

         :hints (("Goal"
                  :induct (samefringe x y)
                  :in-theory (enable problem-109a
                                     problem-109b
                                     problem-109d))))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 110.

; McCarthy's idea was that (gopher x) returns a pair (ans . state)
; where ans is the left-most atom in the tree x and state is the state
; of the gopher co-routine.  Thus, if gopher is invoked on state it
; returns the next left-most atom, etc.
"""}

:next-problem #{"""
; ------------------------------------------------------------
; Problem 111.
"""}

(defun rel (fn x y)
  (if (equal fn '<<=)
      (<<= x y)
    (if (equal fn '>>=)
        (<<= y x)
      (if (equal fn '<<)
          (and (<<= x y) (not (equal x y)))
        (and (<<= y x) (not (equal x y)))))))
  
(defun filter (fn x e)
  (cond
   ((endp x) nil)
   ((rel fn (car x) e)
    (cons (car x) (filter fn (cdr x) e)))
   (t (filter fn (cdr x) e))))

(theorem problem-111a
         (<= (cc (filter fn x e)) (cc x))

         :hints (("Goal"
                  :induct (cc x)))
         :rule-classes :linear)

(defun qsort (x)
  (declare (xargs :measure (cc x)))
  (if (endp x)
      nil
    (if (endp (cdr x))
        x
      (app (qsort (filter '<< (cdr x) (car x)))
           (cons (car x)
                 (qsort (filter '>>= (cdr x) (car x))))))))

(theorem problem-111b
         (and (booleanp (perm x y))
              (perm x x)
              (implies (perm x y) (perm y x))
              (implies (and (perm x y) (perm y z))
                       (perm x z)))

         :hints (("Goal"
                  :in-theory (enable problem-53
                                     problem-54
                                     problem-55)))
         :rule-classes (:equivalence))

(theorem problem-111c
         (implies (not (rel fn d e))
                  (equal (filter fn (rm d y) e)
                         (filter fn y e)))

         :hints (("Goal"
                  :induct (filter fn y e))))

(theorem problem-111d
         (equal (mem d (filter fn x e))
                (and (mem d x)
                     (rel fn d e)))

         :hints (("Goal"
                  :induct (len x)
                  :in-theory (enable problem-48
                                     problem-49a))))

(theorem problem-111e
         (implies (not (mem d x))
                  (equal (rm d x) x))

         :hints (("Goal"
                  :induct (rm d x))))

(theorem problem-111f
         (equal (filter fn (rm d x) e)
                (rm d (filter fn x e)))

         :hints (("Goal"
                  :induct (filter fn x e)
                  :in-theory (enable problem-111c
                                     problem-111d
                                     problem-111e))))

(theorem problem-111g
         (implies (perm x y)
                  (perm (filter fn x e)
                        (filter fn y e)))

         :hints (("Goal"
                  :induct (perm x y)
                  :in-theory (enable problem-48
                                     problem-49a
                                     problem-54a
                                     problem-54
                                     problem-55b
                                     problem-55
                                     problem-111b
                                     problem-111d
                                     problem-111e
                                     problem-111f)))
         :rule-classes :congruence)

(theorem problem-111h
         (perm (app a (cons e b))
               (cons e (app a b)))

         :hints (("Goal"
                  :induct (app a b)
                  :in-theory (enable problem-46
                                     problem-54a
                                     problem-54
                                     problem-55
                                     problem-111b))))

(theorem problem-111i
         (perm (app (filter '<< x e)
                    (filter '>>= x e))
               x)

         :hints (("Goal"
                  :induct (filter '>>= x e)
                  :in-theory (enable problem-54a
                                     problem-54
                                     problem-55
                                     problem-111b
                                     problem-111h))))

(theorem problem-111j
         (implies (mem e x)
                  (perm (app (rm e x) y)
                        (rm e (app x y))))

         :hints (("Goal"
                  :induct (app x y)
                  :in-theory (enable problem-53
                                     problem-54
                                     problem-54a
                                     problem-55
                                     problem-111b))))

(theorem problem-111k
         (implies (perm a b)
                  (perm (app a c) (app b c)))

         :hints (("Goal"
                  :induct (perm a b)
                  :in-theory (enable problem-46
                                     problem-48
                                     problem-49a
                                     problem-54a
                                     problem-54
                                     problem-55b
                                     problem-55
                                     problem-111b
                                     problem-111j)))
         :rule-classes :congruence)

(theorem problem-111l
         (implies (perm a b)
                  (perm (app c a) (app c b)))

         :hints (("Goal"
                  :induct (app c b)
                  :in-theory (enable problem-54a
                                     problem-54
                                     problem-55
                                     problem-111b)))
         :rule-classes :congruence)

(theorem problem-111-a
         (perm (qsort x) x)

         :hints (("Goal"
                  :induct (qsort x)
                  :in-theory (enable problem-54
                                     problem-111b
                                     problem-111h
                                     problem-111i
                                     problem-111k
                                     problem-111l))))

(defun all-rel (fn x e)
  (cond ((endp x) t)
        ((rel fn (car x) e)
         (all-rel fn (cdr x) e))
        (t nil)))

(theorem problem-111m
  (equal (car (app a b))
         (if (consp a)
             (car a)
           (car b))))

(theorem problem-111n
         (implies (and (lexorder e (car a))
                       (ordered a))
                  (all-rel '>>= a e))

         :hints (("Goal"
                  :induct (all-rel '>>= a e))))

(theorem problem-111o
         (implies (and (lexorder d (car a))
                       (consp a)
                       (ordered (app a (cons e b))))
                  (lexorder d e))

         :hints (("Goal"
                  :induct (app a (cons e b))
                  :in-theory (enable problem-111m))))

(theorem problem-111p
         (iff (ordered (app a (cons e b)))
              (and (ordered a)
                   (ordered b)
                   (all-rel '<<= a e)
                   (all-rel '>>= b e)))

         :hints (("Goal"
                  :induct (all-rel '<<= a e)
                  :in-theory (enable problem-111m
                                     problem-111n
                                     problem-111o))))

(theorem problem-111q
         (all-rel '<<= (filter '<< x e) e)

         :hints (("Goal"
                  :induct (filter '<< x e))))

(theorem problem-111r
         (all-rel '>>= (filter '>>= x e) e)

         :hints (("Goal"
                  :induct (filter '>>= x e))))

(theorem problem-111s
         (implies (all-rel fn x e)
                  (all-rel fn (rm d x) e))

         :hints (("Goal"
                  :induct (rm d x))))

(theorem problem-111t
         (implies (and (all-rel fn (rm d x) e)
                       (rel fn d e))
                  (all-rel fn x e))

         :hints (("Goal"
                  :induct (all-rel fn x e))))

(theorem problem-111u
         (implies (and (all-rel fn b e) (mem d b))
                  (rel fn d e))

         :hints (("Goal"
                  :induct (mem d b))))

(theorem problem-111v
         (implies (and (rel fn d e) (mem d b))
                  (equal (all-rel fn (rm d b) e)
                         (all-rel fn b e)))

         :hints (("Goal"
                  :induct (all-rel fn b e))))

:----- #{"""
; Note that I disable rel below!
"""}

(theorem problem-111w
         (implies (perm a b)
                  (equal (all-rel fn a e)
                         (all-rel fn b e)))

         :hints (("Goal"
                  :induct (perm a b)
                  :in-theory (e/d (problem-55b
                                   problem-111u
                                   problem-111v)
                                  (rel))))
         :rule-classes :congruence)

(theorem problem-111-b
         (ordered (qsort x))

         :hints (("Goal"
                  :induct (qsort x)
                  :in-theory (enable problem-111p
                                     problem-111q
                                     problem-111r
                                     problem-111w
                                     problem-111-a))))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 112.
"""}

(defun no-dups (x)
  (if (endp x)
      t
    (if (mem (car x) (cdr x))
        nil
      (no-dups (cdr x)))))

(defun list-of-natsp (lst)
  (cond ((endp lst) t)
        (t (and (natp (car lst))
                (list-of-natsp (cdr lst))))))

(defun maximal (lst)
  (cond ((endp lst) -1)
        (t (max (car lst)
                (maximal (cdr lst))))))

(theorem problem-112a
         (implies (list-of-natsp lst)
                  (iff (mem (maximal lst) lst)
                       (consp lst)))

         :hints (("Goal"
                  :induct (mem (maximal lst) lst))))

(theorem problem-112b
         (implies (list-of-natsp lst)
                  (list-of-natsp (del e lst)))

         :hints (("Goal"
                  :induct (del e lst))))

(theorem problem-112c
         (implies (no-dups lst)
                  (no-dups (rm e lst)))

         :hints (("Goal"
                  :induct (rm e lst)
                  :in-theory (enable problem-55a
                                     problem-111e))))

(theorem problem-112d
         (implies (list-of-natsp lst)
                  (and (integerp (maximal lst))
                       (<= -1 (maximal lst))))

         :hints (("Goal"
                  :induct (list-of-natsp lst)))
         :rule-classes :forward-chaining)

(theorem problem-112e
         (implies (and (list-of-natsp lst)
                       (no-dups lst)
                       (consp lst))
                  (< (maximal (rm (maximal lst) lst))
                     (maximal lst)))

         :hints (("Goal"
                  :induct (rm (maximal lst) lst)
                  :in-theory (enable problem-112a
                                     problem-112d)))
         :rule-classes :linear)



(theorem problem-112f
         (equal (len (rm e lst))
                (if (mem e lst)
                    (- (len lst) 1)
                  (len lst)))

         :hints (("Goal"
                  :induct (rm e lst)
                  :in-theory (enable problem-111e))))

(defun induction-hint (lst n)
  (declare (xargs :measure (acl2-count n)))
  (cond ((zp n) (list lst n))
        ((endp lst) (list lst n))
        (t (induction-hint (rm (maximal lst) lst) (- n 1)))))

(theorem problem-112g
         (implies (list-of-natsp x)
                  (list-of-natsp (rm e x)))

         :hints (("Goal"
                  :induct (rm e x)
                  :in-theory (enable problem-111e))))

(theorem problem-112h
         (implies (and (consp x)
                       (list-of-natsp x))
                  (<= 0 (maximal x)))

         :hints (("Goal"
                  :induct (list-of-natsp x)))
         :rule-classes :linear)

(theorem problem-112i
         (equal (< 0 (len x))
                (consp x))

         :hints (("Goal"
                  :induct (len x))))

(theorem problem-112j
         (implies (and (natp n)
                       (list-of-natsp lst)
                       (no-dups lst)
                       (< (maximal lst) n))
                  (<= (len lst) n))

         :hints (("Goal"
                  :induct (induction-hint lst n)
                  :in-theory (enable problem-112a
                                     problem-112c
                                     problem-112d
                                     problem-112e
                                     problem-112f
                                     problem-112g
                                     problem-112h
                                     problem-112i))))

(theorem problem-112
         (implies (and (list-of-natsp lst)
                       (no-dups lst))
                  (<= (len lst) (+ 1 (maximal lst))))

         :hints (("Goal"
                  
                  :in-theory (enable problem-112d
                                     problem-112j))))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 113.
"""}

(defun addressp (ptr m)
  (and (natp ptr)
       (< ptr (len m))))

(defun inc (x)
  (if (integerp x) (+ 1 x) 'infinite))

(defun len-diff (mem seen)
; How many elements of mem are not in seen?  This basically decreases.
  (if (consp mem)
      (if (mem (car mem) seen)
          (len-diff (cdr mem) seen)
        (+ 1 (len-diff (cdr mem) seen)))
    0))

(theorem problem-113a
         (implies (and (natp n)
                       (< n (len m)))
                  (mem (nth n m) m))

         :hints (("Goal"
                  :induct (nth n m)
                  :in-theory (enable problem-112i))))

(theorem problem-113b
         (<= (len-diff mem (cons ptr seen))
             (len-diff mem seen))

         :hints (("Goal"
                  :induct (len-diff mem (cons ptr seen))
                  :in-theory (enable problem-48
                                     problem-49a)))
         :rule-classes :linear)

(theorem problem-113c
         (implies (and (mem ptr mem)
                       (not (mem ptr seen)))
                  (< (len-diff mem (cons ptr seen))
                     (len-diff mem seen)))

         :hints (("Goal"
                  :induct (len-diff mem seen)
                  :in-theory (enable problem-48
                                     problem-49a
                                     problem-113b)))
         :rule-classes :linear)

(defun deref-cnt (ptr mem seen)
  (declare (xargs :measure (m2 (if (mem ptr mem) 1 2)
                               (len-diff mem seen))

                  :hints (("Goal"
                           :in-theory (enable problem-113a
                                              problem-113c)))))
  (if (addressp ptr mem)
      (if (mem ptr seen)
          'infinite
        (inc (deref-cnt (nth ptr mem) mem (cons ptr seen))))
    0))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 114.
"""}

(defun path (ptr mem seen)
  (declare (xargs :measure (m2 (if (mem ptr mem) 1 2)
                               (len-diff mem seen))

                  :hints (("Goal"
                           :in-theory (enable problem-113a
                                              problem-113c)))))
  (if (addressp ptr mem)
      (if (mem ptr seen)
          'infinite
        (path (nth ptr mem) mem (cons ptr seen)))
    (cons ptr seen)))

(theorem problem-114a
         (equal (deref-cnt ptr mem seen)
                (if (equal (path ptr mem seen) 'infinite)
                    'infinite
                  (- (len (path ptr mem seen)) (+ 1 (len seen)))))

         :hints (("Goal"
                  :induct (path ptr mem seen)
                  :in-theory (enable problem-48
                                     problem-49a))))

(defun addressesp (x mem)
  (if (endp x)
      t
    (and (addressp (car x) mem)
         (addressesp (cdr x) mem))))

(theorem problem-114b
         (implies (and (addressesp x mem)
                       (not (natp e)))
                  (not (mem e x)))

         :hints (("Goal"
                  :induct (mem e x))))

(theorem problem-114c
         (implies (and (addressesp x mem)
                       (>= e (len mem)))
                  (not (mem e x)))

         :hints (("Goal"
                  :induct (mem e x))))

(theorem problem-114d
         (implies (and (no-dups seen)
                       (addressesp seen mem))
                  (no-dups (path ptr mem seen)))

         :hints (("Goal"
                  :induct (path ptr mem seen)
                  :in-theory (enable problem-48
                                     problem-49a
                                     problem-114b
                                     problem-114c))))

(theorem problem-114e
         (implies (sub a b) (sub a (cons e b)))

         :hints (("Goal"
                  
                  :in-theory (enable problem-48a
                                     problem-48
                                     problem-49))))

(theorem problem-114f
         (sub a (app a b))

         :hints (("Goal"
                  :induct (sub a (app a b))
                  :in-theory (enable problem-48a
                                     problem-114e))))

(theorem problem-114g
         (sub b (app a b))

         :hints (("Goal"
                  :induct (app a b)
                  :in-theory (enable problem-48
                                     problem-114e))))

(theorem problem-114h
         (sub (path ptr mem seen)
              (cons ptr (app seen mem)))

         :hints (("Goal"
                  :induct (path ptr mem seen)
                  :in-theory (enable problem-46
                                     problem-48
                                     problem-49
                                     problem-49a
                                     problem-113a
                                     problem-114e
                                     problem-114f))))

(theorem problem-114i
         (implies (and (not (mem e a))
                       (sub a b))
                  (sub a (rm e b)))

         :hints (("Goal"
                  :induct (sub a b)
                  :in-theory (enable problem-48a
                                     problem-55a
                                     problem-55d))))

(theorem problem-114j
         (implies (and (no-dups x)
                       (sub x y))
                  (<= (len x) (len y)))

         :hints (("Goal"
                  :induct (perm x y)
                  :in-theory (enable problem-48a
                                     problem-48
                                     problem-49a
                                     problem-112f
                                     problem-114i)))
         :rule-classes nil)


(theorem problem-114k
         (equal (len (app a b))
                (+ (len a) (len b)))

         :hints (("Goal"
                  :induct (app a b))))

(theorem problem-114l
         (implies (and (addressesp seen mem)
                       (no-dups seen))
                  (<= (len (path ptr mem seen))
                      (+ 1 (len seen) (len mem))))

         :hints (("Goal"
                  :in-theory (enable problem-114d
                                     problem-114h
                                     problem-114k)
                  :use (:instance problem-114j
                                  (x (path ptr mem seen))
                                  (y (cons ptr (app seen mem))))))
         :rule-classes :linear)

(theorem problem-114
         (<= (deref-cnt ptr mem nil) (+ 1 (len mem)))

         :hints (("Goal"
                  :in-theory (enable problem-114a
                                     problem-114l))))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 115.
"""}

(defun fib (i)
  (if (zp i)
      0
    (if (equal i 1)
        1
      (+ (fib (- i 1))(fib (- i 2))))))

(defun ffib (i j k)
  (if (zp i)
      j
    (if (equal i 1)
        k
      (ffib (- i 1) k (+ j k)))))

(theorem problem-115a
         (equal (ffib 2 i j) (+ i j))

         :hints (("Goal"
                  :expand (ffib 2 i j))))

(theorem problem-115b
         (implies (and (natp n)
                       (< 0 n)
                       (natp i)
                       (natp j))
                  (equal (ffib n i j) 
                         (+ (* (fib (- n 1)) i)
                            (* (fib n) j))))

         :hints (("Goal"
                  :induct (ffib n i j)
                  :in-theory (enable problem-115a))))

(theorem problem-115
         (equal (ffib n 0 1) (fib n))

         :hints (("Goal"
                  :in-theory (enable problem-115b)
                  :cases ((zp n)))))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 116.
"""}

(defun rot (x)
  (if (endp x)
      nil
    (app (cdr x) (list (car x)))))

(defun rotn (n x)
  (if (zp n) x (rotn (- n 1) (rot x))))

(theorem problem-116-counterexample
  (not (equal (rotn (len '(1 2 3 . 4)) '(1 2 3 . 4))
              '(1 2 3 . 4)))
  :rule-classes nil)

(defun rot-hint (x y)
  (if (consp x)
      (rot-hint (cdr x)
                (app y (cons (car x) nil)))
    y))

(theorem problem-116a
         (implies (and (proper x)
                       (proper y))
                  (equal (rotn (len x) (app x y))
                         (app y x)))

         :hints (("Goal"
                  :induct (rot-hint x y)
                  :in-theory (enable problem-40
                                     problem-41
                                     problem-44c)))
         :rule-classes nil)

(theorem problem-116
         (implies (proper x)
                  (equal (rotn (len x) x) x))

         :hints (("Goal"
                  
                  :in-theory (enable problem-41)
                  :use (:instance problem-116a (x x) (y nil)))))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 117.
"""}

(defun n (v)
  (cond ((endp v) 0)
        ((car v) (+ 1 (* 2 (n (cdr v)))))
        (t (* 2 (n (cdr v))))))

(defun band (p q) (if p (if q t nil) nil))
(defun bnot (p)   (if p nil t))
(defun bor  (p q) (if p t (if q t nil)))
(defun bxor (p q) (if p (if q nil t) (if q t nil)))
(defun bmaj (p q c)
  (bor (band p q)
       (bor (band p c)
            (band q c))))

(defun vadd1 (x y c)
  (if (endp x)
      (cons c nil)
    (cons (bxor (car x) (bxor (car y) c))
          (vadd1 (cdr x) (cdr y)
                 (bmaj (car x) (car y) c)))))

(theorem problem-117a
         (equal (equal (len x) 0)
                (not (consp x)))

         :hints (("Goal"
                  :induct (len x))))

(theorem problem-117b
         (implies (equal (len x) (len y))
                  (equal (n (vadd1 x y c))
                         (+ (n x) (n y) (if c 1 0))))

         :hints (("Goal"
                  :induct (vadd1 x y c)
                  :in-theory (enable problem-117a))))

(defun vadd (x y)
  (vadd1 x y nil))

(theorem problem-117
         (implies (equal (len x) (len y))
                  (equal (n (vadd x y))
                         (+ (n x) (n y))))

         :hints (("Goal"
                  :in-theory (enable problem-117b))))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 118.

; This problem supposes we have two mutually recursive equations.
; To get mutually recursive definitions admitted into ACL2, we
; use this form:
"""}

(mutual-recursion
 (defun fx (x)
   (if (consp x)
       (cons (fx (car x))
             (gx (cdr x)))
     x))

 (defun gx (x)
   (if (consp x)
       (cons (gx (car x))
             (fx (cdr x)))
     x))
 )

(defun tree-copy (x)
  (if (consp x)
      (cons (tree-copy (car x))
            (tree-copy (cdr x)))
    x))

(theorem problem-118
         (and (equal (fx x) x)
              (equal (gx x) x))

         :hints (("Goal"
                  :induct (tree-copy x))))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 119.

; This problem calls for us to develop a methodology for introducing
; mutually recursive functions and to use it to introduce fx and gx
; above.  But to solve the previous problem we introduced the names fx
; and gx and cannot re-introduce them.  So we will demonstrate our
; methodology to introduce a renamed pair of functions:

; (mutual-recursion
;  (defun fz (x)
;    (if (consp x)
;        (cons (fz (car x))
;              (gz (cdr x)))
;      x))
; 
;  (defun gz (x)
;    (if (consp x)
;        (cons (gz (car x))
;              (fz (cdr x)))
;      x))
;  )

; Step 1 of the methodology is to introduce a singly recursive
; function that has a flag argument that determines which of the
; mutually recursive functions it is supposed to compute.
; When fn is 'fz it computes fz; otherwise it computes gz.
"""}

(defun fzgz (fn x)
  (if (equal fn 'fz)
      (if (consp x)
          (cons (fzgz 'fz (car x))
                (fzgz 'gz (cdr x)))
        x)
      (if (consp x)
          (cons (fzgz 'gz (car x))
                (fzgz 'fz (cdr x)))
        x)))

:----- #{"""
; Step 2 is to define the individual functions using the singly
; recursive function.
"""}

(defun fz (x) (fzgz 'fz x))
(defun gz (x) (fzgz 'gz x))

:----- #{"""
; Step 3 is to prove that fz and gz each satisfy the desired
; equations.
"""}

(theorem problem-119-a
  (equal (fz x)
         (if (consp x)
             (cons (fz (car x))
                   (gz (cdr x)))
           x))
  :rule-classes nil)

(theorem problem-119-b
  (equal (gz x)
         (if (consp x)
             (cons (gz (car x))
                   (fz (cdr x)))
           x))
  :rule-classes nil)

:----- #{"""
; Note that we could now behave as though those equations had
; been added as definitional axioms rather than theorems and
; we could simply ignore the earlier definitions of fz and gz.
"""}

:next-problem #{"""
; ------------------------------------------------------------
; Problem 120.
"""}

(defun expr-fn (fn x)
  (if (equal fn 'expr)
      (if (atom x)
          (symbolp x)
        (and (symbolp (car x))
             (expr-fn 'list (cdr x))))
    (if (atom x)
        (equal x nil)
      (and (expr-fn 'expr (car x))
           (expr-fn 'list (cdr x))))))

(defun expr (x) (expr-fn 'expr x))

(defun expr-list (x) (expr-fn 'list x))

(theorem problem-120-a
  (equal (expr x)
         (if (atom x)
             (symbolp x)
           (and (symbolp (car x))
                (expr-list (cdr x)))))
  :rule-classes nil)

(theorem problem-120-b
  (equal (expr-list x)
         (if (atom x)
             (equal x nil)
           (and (expr (car x))
                (expr-list (cdr x)))))
  :rule-classes nil)


:next-problem #{"""
; ------------------------------------------------------------
; Problem 121.
"""}

(defun substitution (s)
  (if (endp s)
      (equal s nil)
    (and (true-listp (car s))
         (equal (len (car s)) 2)
         (symbolp (car (car s)))
         (expr (car (cdr (car s))))
         (substitution (cdr s)))))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 122.
"""}

(defun lookup (var alist)
  (if (endp alist)
      var
    (if (equal var (car (car alist)))
        (car (cdr (car alist)))
      (lookup var (cdr alist)))))

(defun slash-fn (fn x s)
  (if (equal fn 'expr)
      (if (atom x)
          (lookup x s)
        (cons (car x) (slash-fn 'list (cdr x) s)))
    (if (atom x)
        nil
      (cons (slash-fn 'expr (car x) s)
            (slash-fn 'list (cdr x) s)))))

(defun slash (x s) (slash-fn 'expr x s))
(defun slash-list (lst s) (slash-fn 'list lst s))


(theorem problem-122-a
  (equal (slash x s)
         (if (atom x)
             (lookup x s)
           (cons (car x) (slash-list (cdr x) s))))
  :rule-classes nil)

(theorem problem-122-b
  (equal (slash-list lst s)
         (if (atom lst)
             nil
           (cons (slash (car lst) s)
                 (slash-list (cdr lst) s))))
  :rule-classes nil)

:next-problem #{"""
; ------------------------------------------------------------
; Problem 123.
"""}

(theorem problem-123a
         (implies (and (symbolp x)
                       (substitution s))
                  (expr-fn 'expr (lookup x s)))

         :hints (("Goal"
                  :induct (lookup x s))))

(theorem problem-123b
         (implies (and (expr-fn fn x)
                       (substitution s))
                  (expr-fn fn (slash-fn fn x s)))

         :hints (("Goal"
                  :induct (slash-fn fn x s)
                  :in-theory (enable problem-123a))))

(theorem problem-123
         (implies (and (expr x)
                       (substitution s))
                  (expr (slash x s)))

         :hints (("Goal"
                  :in-theory (enable problem-123b))))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 124.

; The definition of f5 cannot be admitted because of the recursive
; call (f5 (f5 (- x 1))).  We can prove that a measure of the argument
; decreases in the inner recursive call.  (The measure would be cc.)
; But we cannot prove that ANY measure decreases in the outer
; recursive call, because the actual is (f5 (- x 1)) and we do not
; have ANY axiom about f5 yet.
"""}

:next-problem #{"""
; ------------------------------------------------------------
; Problem 125.

; The nested recursion in mcflatten is admissible because it occurs in
; an argument position that is not being measured.  We admit mcflatten
; by measuring the first argument, x.  We do not care what term is in
; the second position, a.

; The nested recursion in ack is more problematic but still
; acceptable.  Recall why ack terminates: either the second argument,
; y, decreases or else the second argument stays fixed and the first,
; x, decreases.  Note that when y decreases, we don't care what x
; does.  So if x is occupied by a nested recursive call, it is ok.
"""}

:next-problem #{"""
; ------------------------------------------------------------
; Problem 126.
"""}

(defun f5 (x)
  (declare (xargs :measure (nfix x)))
  (if (zp x)
      0
    (if (< (nfix (f5 (- x 1))) (nfix x))
        (+ 1 (f5 (f5 (- x 1))))
      'undef)))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 127.
"""}

(theorem problem-127
         (implies (natp x) (<= (f5 x) x))

         :hints (("Goal"
                  :induct (f5 x)))
         :rule-classes :linear)

:next-problem #{"""
; ------------------------------------------------------------
; Problem 128.
"""}

(theorem problem-128
         (equal (f5 x)
                (if (zp x)
                    0
                  (+ 1 (f5 (f5 (- x 1))))))

         :hints (("Goal"
                  :in-theory (enable problem-127)))
         :rule-classes nil)

:next-problem #{"""
; ------------------------------------------------------------
; Problem 129.
"""}

(defun flatten-m (x)
  (if (consp x)
      (if (consp (car x))
          (cons (car (car x))
                (cons (cdr (car x))
                      (flatten-m (cdr x))))
        (cons (car x) (flatten-m (cdr x))))
    nil))

(theorem problem-129a
         (implies (and (natp ptr)
                       (< ptr (len g))
                       (consp (nth ptr g)))
                  (mem (car (nth ptr g)) (flatten-m g)))

         :hints (("Goal"
                  :induct (nth ptr g)
                  :in-theory (enable problem-48
                                     problem-49a
                                     problem-112i
                                     problem-114e))))

(theorem problem-129b
         (implies (and (natp ptr)
                       (< ptr (len g))
                       (consp (nth ptr g)))
                  (mem (cdr (nth ptr g)) (flatten-m g)))

         :hints (("Goal"
                  :induct (nth ptr g)
                  :in-theory (enable problem-48
                                     problem-49a
                                     problem-112i
                                     problem-114e))))

(theorem problem-129c
         (implies (and (natp ptr)
                       (< ptr (len g))
                       (not (consp (nth ptr g))))
                  (mem (nth ptr g) (flatten-m g)))

         :hints (("Goal"
                  :induct (nth ptr g)
                  :in-theory (enable problem-48
                                     problem-49a
                                     problem-112i
                                     problem-114e))))

(theorem problem-129d
         (implies (sub seen1 seen2)
                  (<= (len-diff x seen2)
                      (len-diff x seen1)))

         :hints (("Goal"
                  :induct (len-diff x seen2)
                  :in-theory (enable problem-48
                                     problem-49a)))
         :rule-classes :linear)

(theorem problem-129e
         (implies (and (not (mem ptr seen1))
                       (mem ptr seen2)
                       (sub seen1 seen2)
                       (mem ptr x))
                  (< (len-diff x seen2)
                     (len-diff x seen1)))

         :hints (("Goal"
                  :induct (len-diff x seen1)
                  :in-theory (enable problem-48
                                     problem-49
                                     problem-49a
                                     problem-129d)))
         :rule-classes :linear)

(defun reachables (ptr m seen)
  (declare (xargs :measure (m2 (if (mem ptr (flatten-m m)) 1 2)
                               (len-diff (flatten-m m) seen))

                  :hints (("Goal"
                           :in-theory (enable problem-48
                                              problem-114e
                                              problem-129a
                                              problem-129b
                                              problem-129c
                                              problem-129e)))))
  (if (addressp ptr m)
      (if (mem ptr seen)
          seen
        (if (consp (nth ptr m))
            (if (sub (cons ptr seen)
                     (reachables (cdr (nth ptr m))
                                 m
                                 (cons ptr seen)))
                (reachables (car (nth ptr m))
                            m
                            (reachables (cdr (nth ptr m))
                                        m
                                        (cons ptr seen)))
              'undef)
          (reachables (nth ptr m) m (cons ptr seen))))
    seen))

:----- #{"""
; This is the key fact about reachables: it doesn't unmark any nodes!
"""}

(theorem problem-129f
         (sub seen (reachables ptr m seen))

         :hints (("Goal"
                  :induct (reachables ptr m seen)
                  :in-theory (enable problem-48
                                     problem-49
                                     problem-49a)))
         :rule-classes nil)

(theorem problem-129g
         (sub (cons ptr seen)
              (reachables (cdr (nth ptr m))
                          m
                          (cons ptr seen)))

         :hints (("Goal"
                  :use (:instance problem-129f
                                  (ptr (cdr (nth ptr m)))
                                  (m m)
                                  (seen (cons ptr seen))))))

(theorem problem-129
         (equal (reachables ptr m seen)
                (if (addressp ptr m)
                    (if (mem ptr seen)
                        seen
                      (if (consp (nth ptr m))
                          (reachables (car (nth ptr m))
                                      m
                                      (reachables (cdr (nth ptr m))
                                                  m
                                                  (cons ptr seen)))
                        (reachables (nth ptr m) m (cons ptr seen))))
                  seen))

         :hints (("Goal"
                  :in-theory (enable problem-129g)))
         :rule-classes nil)

:next-problem #{"""
; ------------------------------------------------------------
; Problem 130.
"""}

(defun rmb (x)
  (declare (xargs :measure (cc x)))
  (if (consp x)
      (if (consp (cdr x))
          (if (< (cc (cdr (rmb (cdr x))))
                 (cc x))
              (if (< (cc (cons (car x)
                               (rmb (cdr (rmb (cdr x))))))
                     (cc x))
                  (cons (car (rmb (cdr x)))
                        (rmb (cons (car x)
                                   (rmb (cdr (rmb (cdr x)))))))
                'undef)
            'undef)
        (cons (car x) nil))
    nil))

(theorem problem-130a
  (<= (cc (cdr x)) (cc x))
  :rule-classes :linear)

(theorem problem-130b
         (<= (cc (rmb x)) (cc x))

         :hints (("Goal"
                  :induct (rmb x)))
         :rule-classes :linear)

(theorem problem-130c
         (implies (consp x)
                  (consp (rmb x)))

         :hints (("Goal"
                  :induct (rmb x)
                  :in-theory (enable problem-108c
                                     problem-130a
                                     problem-130b))))

(theorem problem-130
         (equal (rmb x)
                (if (consp x)
                    (if (consp (cdr x))
                        (cons (car (rmb (cdr x)))
                              (rmb (cons (car x)
                                         (rmb (cdr (rmb (cdr x)))))))
                      (cons (car x) nil))
                  nil))

         :hints (("Goal"
                  
                  :in-theory (enable problem-108c
                                     problem-130b
                                     problem-130c)))
         :rule-classes ((:definition :controller-alist ((rmb t)))))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 131.

; Basically, just use the facts we already know about app and rev,
; plus induction hypotheses that the rmb calls are rev calls,
; to prove the theorem.  The proof requires some
; knowledge of proper lists, since our lemmas about app and rev
; involve that concept.  In particular, we need:
"""}

(theorem problem-131a
  (equal (proper (cdr x))
         (or (atom x)
             (proper x))))

:----- #{"""
; Informally, that's enough to do the proof if you could just
; do an induction to unwind rmb.  But we haven't proved the
; measure conjectures about the rmb recursion.  Rather than
; try that, we take a nice short cut.  Since we are going to
; be proving that rmb is rev, we will prove the measure conjectures
; about the rmb recursion -- but we'll substitute rev calls for
; each rmb call.  That is what the hint below does.  We need
; a few lemmas.
"""}

(theorem problem-131b
         (<= (cc (app a b))
             (+ (cc a)
                (cc b)))

         :hints (("Goal"
                  :induct (cc a)))
         :rule-classes :linear)

(theorem problem-131c
         (<= (cc (rev x))
             (cc x))

         :hints (("Goal"
                  :induct (cc x)
                  :in-theory (enable problem-131b)))
         :rule-classes :linear)
           
(theorem problem-131d
         (equal (consp (rev x))
                (consp x))

         :hints (("Goal"
                  :induct (rev x))))

(defun rmb-hint (x)
  (declare (xargs :measure (cc x)

                  :hints (("Goal"
                           :in-theory (enable problem-108c
                                              problem-130a
                                              problem-131c
                                              problem-131d)))))
  (if (consp x)
      (if (consp (cdr x))
          (list (rmb-hint (cdr x))
                (rmb-hint (cdr (rev (cdr x))))
                (rmb-hint (cons (car x)
                                (rev (cdr (rev (cdr x)))))))
        x)
    x))

(theorem problem-131
         (equal (rmb x) (rev x))
         :hints (("Goal"
                  :induct (rmb-hint x)
                  :in-theory (enable problem-44b
                                     problem-44
                                     problem-130
                                     problem-131a
                                     problem-131d))))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 132.
"""}

(defun if-exprp (x)
  (and (consp x)
       (equal (car x) 'if)))

(defun a1 (x) (cadr x))
(defun a2 (x) (caddr x))
(defun a3 (x) (cadddr x))

:----- #{"""
; In ACL2, (ASSOC-EQUAL x alist) finds the first pair in alist whose
; car is equal to x.  We use it below.
"""}

(defun assignment (var alist)
  (if (quotep var)
      (car (cdr var)) ; var = (QUOTE val)
      (cdr (assoc-equal var alist))))

:----- #{"""
; We can't call our evaluator eval because that is a pre-defined
; Common Lisp function.  We use peval instead; it stands for
; ``propositional evaluator.''
"""}

(defun peval (x alist)
  (declare (xargs :measure (cc x)))
  (cond ((if-exprp x)
         (if (peval (a1 x) alist)
             (peval (a2 x) alist)
           (peval (a3 x) alist)))
        (t (assignment x alist))))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 133.

; Here is the definition of IF-normal form:
"""}

(defun normp (x)
  (declare (xargs :measure (cc x)))
  (if (if-exprp x)
      (and (not (if-exprp (a1 x)))
           (normp (a2 x))
           (normp (a3 x)))
    t))

:----- #{"""
; To admit norm, we need some measures.
"""}

(defun if-depth (x)
  (declare (xargs :measure (cc x)))
  (if (if-exprp x)
      (+ 1 (if-depth (a1 x)))
    0))

(defun if-complexity (x)
  (declare (xargs :measure (cc x)))
  (if (if-exprp x)
      (* (if-complexity (a1 x))
         (+ (if-complexity (a2 x))
            (if-complexity (a3 x))))
    1))

(theorem problem-133a
         (<= 1 (if-complexity x))
         :hints (("Goal" :induct (if-complexity x)))
         :rule-classes :linear)

(theorem problem-133b
         (implies (and (rationalp x)
                       (rationalp y)
                       (<= 1 x)
                       (< 0 y))
                  (<= y (* x y)))
         :rule-classes :linear)

(theorem problem-133c
         (implies (if-exprp x)
                  (< (if-complexity (a2 x))
                     (if-complexity x)))
         :hints (("Goal"
                  :induct (if-complexity x)
                  :in-theory (enable problem-133b)))
         :rule-classes :linear)

(theorem problem-133d
         (implies (if-exprp x)
                  (< (if-complexity
                      (a3 x))
                     (if-complexity x)))
         :hints (("Goal"
                  :induct (if-complexity x)
                  :in-theory (enable problem-133b)))
         :rule-classes :linear)

(defun norm (x)
  (declare (xargs :measure (m2 (if-complexity x) (if-depth x))
                  :hints (("Goal" :in-theory (enable problem-133a
                                                     problem-133c
                                                     problem-133d)))))
  (cond ((not (if-exprp x)) x)
        ((if-exprp (a1 x))
         (norm (list 'if (a1 (a1 x))
                     (list 'if
                           (a2 (a1 x))
                           (a2 x)
                           (a3 x))
                     (list 'if
                           (a3 (a1 x))
                           (a2 x)
                           (a3 x)))))
        (t (list 'if (a1 x)
                 (norm (a2 x))
                 (norm (a3 x))))))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 134.
"""}

(defun assignedp (var alist)
   (or (quotep var)
       (assoc-equal var alist)))

(defun assume-true (var alist)
   (cons (cons var t)
         alist))

(defun assume-false (var alist)
  (cons (cons var nil)
        alist))

(defun tautp (x alist)
  (declare (xargs :measure (cc x)))
  (if (if-exprp x)
      (if (assignedp (a1 x) alist)
          (if (assignment (a1 x) alist)
              (tautp (a2 x) alist)
            (tautp (a3 x) alist))
        (and (tautp (a2 x)
                    (assume-true (a1 x) alist))
             (tautp (a3 x)
                    (assume-false (a1 x) alist))))
    (assignment x alist)))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 135.
"""}

(defun tautology-checker (x)
   (tautp (norm x) nil))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 136.
"""}

(theorem problem-136a
         (implies (alistp a)
                  (equal (assoc-equal x (app a b))
                         (if (assoc-equal x a)
                             (assoc-equal x a)
                           (assoc-equal x b))))
         :hints (("Goal" :induct (app a b))))

(theorem problem-136b
         (implies (iff (cdr (assoc-equal var alist)) val)
                  (iff (peval x (cons (cons var val) alist))
                       (peval x alist)))
         :hints (("Goal" :induct (peval x alist))))

(theorem problem-136c
         (implies (and (alistp a)
                       (normp x)
                       (tautp x a))
                  (peval x (app a b)))
         :hints (("Goal"
                  :induct (tautp x a)
                  :in-theory (enable problem-136a
                                     problem-136b))))
  
(theorem problem-136d
         (normp (norm x))
         :hints (("Goal" :induct (norm x))))

(theorem problem-136e
         (equal (peval (norm x) a)
                (peval x a))
         :hints (("Goal" :induct (norm x))))

(theorem problem-136
         (implies (tautology-checker x)
                  (peval x a))
         :hints (("Goal"
                  :in-theory (enable problem-136d
                                     problem-136e)
                  :use (:instance problem-136c
                                  (a nil)
                                  (b a)
                                  (x (norm x))))))

:next-problem #{"""
; ------------------------------------------------------------
; Problem 137.

; Below we use two ACL2 language features not previously used in this
; material, mv and mv-let.  (mv a b) returns a vector (list) of length
; 2 containing a and b.  (mv-let (x y) <foo> <bar>) evaluates <foo>,
; obtaining a list of length 2, binds x and y to the to elements, and
; then evaluates <bar>.  Thus,
; (mv a b)
; is the same as
; (cons a (cons b nil))
; and
; (mv-let (x y) <foo> <bar>)
; is the same as
; ((lambda (vector)
;   ((lambda (x y)
;     <bar>)
;    (nth 0 vector)
;    (nth 1 vector)))
;  <foo>)
; However, if mv and mv-let are used instead of the expressions
; above, the syntax checker in ACL2 insures that <foo> really does
; produce a vector of length 2.  In addition, mv and mv-let are more
; efficient than the expressions above, because they do not actually
; create the vector.
"""}

(defun falsify (x alist)
  (declare (xargs :measure (cc x)))
  (if (if-exprp x)
      (if (assignedp (a1 x) alist)
          (if (assignment (a1 x) alist)
              (falsify (a2 x) alist)
            (falsify (a3 x) alist))
        (mv-let (ans new-alist)
                (falsify (a2 x)
                         (assume-true (a1 x) alist))
                (if ans
                    (mv ans new-alist)
                  (falsify (a3 x)
                           (assume-false (a1 x) alist)))))
    (if (assignedp x alist)
        (if (assignment x alist)
            (mv nil nil)
          (mv t alist))
      (mv t (assume-false x alist)))))

(defun falsifying-alist (x)
  (mv-let (ans alist)
          (falsify (norm x) nil)
          (declare (ignore ans))
          alist))

:----- #{"""
; For reasons having to do with the syntactic checking noted above,
; ACL2 accesses the components of the vector with mv-nth rather than
; nth.  Logically, mv-nth is nth.
"""}

(theorem problem-137a
         (implies (and (normp x)
                       (not (tautp x a)))
                  (mv-nth 0 (falsify x a)))
         :hints (("Goal" :induct (falsify x a))))

(theorem problem-137b
         (implies (assoc-equal var a)
                  (equal (cdr (assoc-equal var (mv-nth 1 (falsify x a))))
                         (if (mv-nth 0 (falsify x a))
                             (cdr (assoc-equal var a))
                           nil)))
         :hints (("Goal" :induct (falsify x a))))

(theorem problem-137c
         (implies (and (mv-nth 0 (falsify x a))
                       (normp x))
                  (not (peval x (mv-nth 1 (falsify x a)))))
         :hints (("Goal"
                  :induct (falsify x a)
                  :in-theory (enable problem-137b))))

(theorem problem-137
         (implies (not (tautology-checker x))
                  (not (peval x (falsifying-alist x))))
         :hints (("Goal"
                  :in-theory (enable problem-136d
                                     problem-136e
                                     problem-137a)
                  :use (:instance problem-137c
                                  (a nil)
                                  (x (norm x))))))