File: terms-as-dag.lisp

package info (click to toggle)
acl2 8.3dfsg-2
  • links: PTS
  • area: main
  • in suites: bullseye
  • size: 309,408 kB
  • sloc: lisp: 3,311,842; javascript: 22,569; cpp: 9,029; ansic: 7,872; perl: 6,501; xml: 3,838; java: 3,738; makefile: 3,383; ruby: 2,633; sh: 2,489; ml: 763; python: 741; yacc: 721; awk: 260; csh: 186; php: 171; lex: 154; tcl: 49; asm: 23; haskell: 17
file content (3429 lines) | stat: -rw-r--r-- 106,575 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
;;; ============================================================================
;;; terms-as-dag.lisp
;;; Título: Storing first--order terms as dags
;;; ============================================================================

#| To certify this book:

(in-package "ACL2")

(certify-book "terms-as-dag")


|#

(in-package "ACL2")


(include-book "dag-unification-rules")


;;; ============================================================================
;;;
;;; 0) Introducción
;;;
;;; ============================================================================

;;; In this book ({\tt terms-as-dag.lisp}), we define a function to
;;; store first--order terms (respresented in standard list/prefix
;;; notation) as directed acyclic graphs (dags), with shared
;;; variables. This function is essential (it is the first step) in the
;;; design of a unification algorithm using a dag representation.

;;; The following function {\tt term-as-dag-l} (with its main auxiliary
;;; function {\tt term-as-dag-aux-l}) receives as input a first--order
;;; term and a graph and returns a graph that stores the input terms
;;; (following the conventions explained in the book {\tt
;;; dags.lisp}). Note that the result is a graph such that the term
;;; pointed by the index @0@ is the input term:



(defun term-as-dag-aux-l (flg term g h variables)
  (if flg
      (if (variable-p term)
	  (let* ((bound (assoc term variables))
		 (g (update-dagi-l h (if bound (cdr bound) (cons term t)) g))
		 (new-variables (if bound variables (acons term h variables))))
	    (mv g (1+ h) nil new-variables))
	(mv-let (g h1 hsl var1)
		(term-as-dag-aux-l nil (cdr term) g (1+ h) variables)
		(let* ((g (update-dagi-l h (cons (car term) hsl) g)))
		  (mv g h1 nil var1))))
    (if (endp term)
	(mv g h term variables)
      (mv-let (g hcar ign varcar)
	      (term-as-dag-aux-l t (car term) g h variables)
	      (declare (ignore ign))
	      (mv-let (g hcdr hsl varcdr)
		      (term-as-dag-aux-l nil (cdr term) g hcar varcar)
		      (mv g hcdr (cons h hsl) varcdr))))))

(defun term-as-dag-l (term g)
  (mv-let (g h hs var)
	  (term-as-dag-aux-l t term g 0 nil)
	  (declare (ignore h hs var))
	  g))

;;; Example:

; ACL2 !>(term-as-dag-l
;            '(f (h x) y (k (g (k y) (k x z))))
;	       '(nil nil nil nil nil nil nil nil nil nil nil))
; ((F 1 3 4) (H 2) (X . T)
;            (Y . T)
;            (K 5) (G 6 8) (K 7) 3 (K 9 10) 2 (Z . T))


;;; The main auxiliary function {\tt term-as-dag-aux-l} is defined
;;; recursively in the structure of the term stored. The flag @flg@
;;; indicates if @term@ is considered to be a term or a list of terms,
;;; as usual. The argument @h@ is an index in the graph @g@; from this index
;;; on, the term is going to be stored in the graph. Since the term is
;;; stored with shared variables, the argument @variables@ stored a
;;; association list, associating variables previously stored with the
;;; corresponding index of the node where the variable is already stored
;;; in the graph.

;;; This function returns a multivalue with four values. The first one
;;; is the new graph obtained, the second is the first available index
;;; after the proccess, the third (only makes sense for the case of
;;; lists of terms) is the list of initial indices where each of the
;;; terms of the list have been stored, and the fourth is the new
;;; association list of variables to indices, extending the input
;;; association list.

;;; Our goal in this book is to verify this function, showing that it
;;; sores a term in the conditions required by the rules of
;;; transformation of a unification algorithm acting on term dags (see
;;; {\tt dag\--unification\--rules\-.lisp}. That is, we prove the
;;; following theorems (the predicate {\tt empty-graph-p} describes
;;; graphs with @nil@ in all its positions):

; (defthm term-as-dag-l-term-graph-p
;   (implies (and (empty-graph-p g) (term-p term))
;	     (term-graph-p (term-as-dag-l term g))))



; (defthm term-as-dag-l-dag-p
;   (implies (and (empty-graph-p g) (term-p term))
; 	     (dag-p (term-as-dag-l term g))))


; (defthm term-as-dag-l-stores-term
;   (implies (and (empty-graph-p g) (term-p term))
; 	     (equal (dag-as-term-l t 0 (term-as-dag-l term g))
;		    term)))


; (defthm term-as-dag-l-no-duplicatesp-variables
;    (implies (and (empty-graph-p g) (term-p term))
; 	      (no-duplicatesp
; 	         (list-of-term-dag-variables (term-as-dag-l term g))))


;;; These theorems ensure that we can use the function @term-as-dag@ to
;;; store well--formed unification problems (see the book {\tt
;;; dag-unification-rules.lisp}). In fact, in the last section of this
;;; book, we define and verify a function that (using the above function
;;; {\tt term-as-dag-l}) stores a given unification problem as directed
;;; acyclicic graph.

;;; Finally, note that for the moment we are not talking about
;;; single--threaded objects. But later (book {\tt
;;; dag-unification-st.lisp}), we will use the properties of
;;; these functions to verify an analogue functions defined using stobjs
;;; (that is, the graph obtained will be destructively updated).


;;; The following books are used, and contain information that is
;;; relevant to understand the contents of this book:

;;; *)
;;;  The book {\tt dag-unification-rules} describes the rules of
;;;  transformation of unification, done with a dag representation of
;;;  terms. It includes the book {\tt dags.lisp}, where it is described
;;;  how term graphs are represented and the meaning of the information
;;;  stored in the nodes
;;; -)


;;; ============================================================================
;;;
;;; 1) The proof strategy
;;;
;;; ============================================================================

;;; Note that verifying the function @term-as-dag-l@ may be difficult,
;;; for several reasons:

;;; *)
;;;   We use an association list to avoid duplication of variables.
;;; *)
;;;   The node corresponding to a function symbol is updated {\em after}
;;;   the list of its arguments have been stored.
;;; *)
;;;   To store a list of terms, the @cdr@ of the list is stored having
;;;   as input the graph obtained after storing the @car@ of the list.
;;; *)
;;;   We must ensure that the graph stored is a directed acyclic graph
;;;   (as defined by @dag-p@). In particular this property has to be
;;;   held in every stage of the proccess (because we need to use
;;;   induction hypothesis in the proof of the theorems).
;;; -)

;;; We tried to verify the functions directly and it turned (not very
;;; surprisingly) very difficult. So we explain now the approach we
;;; followed. The main idea is to employ some kind of compositional
;;; reasoning, in order to consider the difficult issues of the proofs
;;; separately. In particular, we reason about "shared variables" and
;;; the proccess of "storing the term", separately.

;;; For that purpose, we define a version of {\tt term-as-dag-aux-l},
;;; called {\tt term-as-dag-aux-l-ns}, storing terms (or list of terms)
;;; but without the burden of getting shared variables. Note that we do
;;; not need in this case the association list of variables to indices:


(local
 (defun term-as-dag-aux-l-ns (flg term g h)
   (if flg
       (if (variable-p term)
	   (let ((g (update-dagi-l h (cons term t) g)))
	     (mv g (1+ h) nil))
	 (mv-let (g h1 hsl)
		 (term-as-dag-aux-l-ns nil (cdr term) g (1+ h))
		 (let* ((g (update-dagi-l h (cons (car term) hsl) g)))
		   (mv g h1 nil))))
     (if (endp term)
	 (mv g h term)
       (mv-let (g hcar ign)
	       (term-as-dag-aux-l-ns t (car term) g h)
	       (declare (ignore ign))
	       (mv-let (g hcdr hsl)
		       (term-as-dag-aux-l-ns nil (cdr term) g hcar)
		       (mv g hcdr (cons h hsl))))))))


;;; We also define a function that makes variables to be shared in a
;;; given graph. More precisely, the following function {\tt
;;; make\--shared\--variables} receives as input a graph, an association
;;; list (associating variables to indices pointing where they appear
;;; for the first time in the graph) and a pair of indices (indicating a
;;; range), and returns a multivalue with a graph where all the
;;; variables in the range of indices are shared and a new computed
;;; association list. Note that during the proccess, if a variable node
;;; is found and the variable is bound in the associaton list to an
;;; index, then the node is updated to that index. If the variable node
;;; is not bound in the association list, then the node is not updated,
;;; but the association list is extended, binding the new variable to
;;; the current index.


(local
 (defun make-shared-variables (h1 h2 g variables)
   (declare (xargs :measure (nfix (- h2 h1))))
   (cond ((zp (- h2 h1))
	  (mv g variables))
	 ((term-dag-variable-p h1 g)
	  (let ((bound (assoc (term-dag-symbol h1 g) variables)))
	    (if bound
		(let ((g (update-nth h1 (cdr bound) g)))
		  (make-shared-variables (1+ h1) h2 g variables))
	      (make-shared-variables
	       (1+ h1) h2 g (acons (term-dag-symbol h1 g) h1
				   variables)))))
	 (t (make-shared-variables (1+ h1) h2 g variables)))))


;;; Our strategy is as follows:

;;; *)
;;;  Verify the properties of {\tt term-as-dag-aux-l-ns}, showing that
;;;  stores the input term as a directed acyclic graph.
;;; *)
;;;  Show that {\tt make-shared-variables} preserves those properties.
;;; *)
;;;  Prove that the composition of {\tt make-shared-variables} and {\tt
;;;  term-as-dag-aux-l-ns} is equal to {\tt term-as-dag-aux-l}
;;; *)
;;;  Combine all the above results to prove the intended properties of
;;;  {\tt term-as-dag-aux-l}. As a particular case, the properties of
;;;  {\tt term-as-dag-l}.
;;; -)

;;; There is an exception to this strategy: when we prove the {\tt
;;; term-graph-p} property of the graph obtained by {\tt
;;; term-as-dag-l} (theorem {\tt term-as-dag-l-term-graph-p} above),
;;; we will reason directly with the definition of the function {\tt
;;; term-as-dag-l}.

(local (in-theory (disable assoc-val)))


;;; ============================================================================
;;;
;;; 2) Some properties related to {\tt dag-p}
;;;
;;; ============================================================================

;;; One of the main drawbacks in the proof effort described in this book
;;; is the proof that the graphs obtained are acyclic, as defined by the
;;; function @dag-p@ (see {\tt dags.lisp}). In this section we define
;;; some conditions on graphs stronger than the @dag-p@ condition.


;;; -----------------------------------
;;;
;;; 2.1) The property {\tt init-term-dag-p}
;;;
;;; -----------------------------------

;;; The property @init-term-dag-p@ defined below describes a particular
;;; type of directed acyclicic graphs: those obtained storing terms
;;; using the function @term-as-dag-l@.

;;; Note the style of the definition: we define  {\em a property of
;;; nodes} ({\tt property\--element\--init\--term\--dag-p} in this
;;; case). Then we define a function ({\tt init\--term\--dag-p\--aux} in
;;; this case) checking if that property holds in a list of given
;;; nodes. Finally we define the function {\tt init\--term\--dag-p}
;;; checking the property in the list off all the nodes of a graph.

;;; A graph has the property {\tt init-term-dag-p} if every node is
;;; @nil@, or a compound node such that its arguments are bigger
;;; indices, or a variable node, or an "is" node pointing to a previous
;;; variable node:

(local
 (defun smaller-than-list (h l)
   (if (endp l)
       t
     (and (< h (car l))
	  (smaller-than-list h (cdr l))))))

(local
 (defun property-element-init-term-dag-p (h node g)
   (or (and (natp node) (< node h) (term-dag-variable-p node g))
       (equal node nil)
       (and (consp node)
	    (variable-p (car node)) (equal (cdr node) t))
       (and (consp node)
	    (nat-true-listp (cdr node))
	    (smaller-than-list h (cdr node))))))

(local
 (defun init-term-dag-p-aux (hs g)
   (if (endp hs)
       t
     (and (property-element-init-term-dag-p (car hs) (nth (car hs) g) g)
	  (init-term-dag-p-aux (cdr hs) g)))))


(defun list-from-to (n m)
  (declare (xargs :measure (nfix (- m n))
		  :guard (and (natp n) (natp m) (<= n m))))
  (if (zp (- m n))
      nil
    (cons n (list-from-to (1+ n) m))))



;;; First, some properties about the function {\tt list-from-to}:

(local
 (defthm list-from-to-main-property
   (implies (and (natp n) (natp m))
	    (iff (member x (list-from-to n m))
		 (and (natp x) (<= n x) (< x m))))))


(local
; Renamed from nat-listp by Matt K. after v4-3, to avoid conflict with
; books/arithmetic/nat-listp.lisp.
 (defun local-nat-listp (l)
   (if (endp l)
       t
     (and (natp (car l))
	  (local-nat-listp (cdr l))))))


(local
 (defthm list-from-to-local-nat-listp
   (implies (and (natp h1) (natp h2))
	    (local-nat-listp (list-from-to h1 h2)))))


(local
 (defthm append-list-from-to
   (implies (and (natp h1) (natp h2) (natp h3)
		 (<= h1 h2) (<= h2 h3))
	    (equal (append (list-from-to h1 h2)
			   (list-from-to h2 h3))
		   (list-from-to h1 h3)))))


(local
 (defun init-term-dag-p (g)
   (init-term-dag-p-aux (list-from-to 0 (len g)) g)))



;;; -----------------------------------
;;;
;;; 2.2) The property {\tt tree-p}
;;;
;;; -----------------------------------

;;; A particular case of {\tt init\--term\--dag\--p} is the property
;;; {\tt tree-p}. In this case we do not allow "is" nodes. This kind iof
;;; graphs are the graphs built by the function {\tt
;;; term-as-dag-aux-l-ns}:

(local
 (defun property-element-tree-p (h node)
   (or (equal node nil)
       (and (consp node)
	    (variable-p (car node)) (equal (cdr node) t))
       (and (consp node)
	    (nat-true-listp (cdr node))
	    (smaller-than-list h (cdr node))))))



(local
 (defun tree-p-aux (hs g)
   (if (endp hs)
       t
     (and (property-element-tree-p (car hs) (nth (car hs) g))
	  (tree-p-aux (cdr hs) g)))))

(local
 (defun tree-p (g)
   (tree-p-aux (list-from-to 0 (len g)) g)))




;;; -----------------------------------
;;;
;;; 2.2) The property {\tt init-term-dag-p} implies {\tt dag-p}
;;;
;;; -----------------------------------

;;; Due to the main properties of {\tt dag-p} (see {\tt dags.lisp}) we
;;; only has to show that every path in graph verifying {\tt
;;; init-term-dag-p} has no duplicates nodes. But this is true because
;;; the paths in this case are strictly increasing except (possibly) the
;;; last node (and in that case, this last node is a variable node and
;;; obvioulsy it has to be the first time it appears in a path):

;;; We first prove some properties about {\tt init-term-dag-p} before
;;; disabling the function:

(local
 (encapsulate

  ()

  (local
   (defthm init-term-dag-p-aux-member-1
     (implies (and (init-term-dag-p-aux hs g)
		   (member h hs)
		   (not (term-dag-is-p h g)))
	      (smaller-than-list h (neighbors h g)))))

  (local
   (defthm init-term-dag-p-aux-member-2
     (implies (and  (member x (neighbors h g))
		    (init-term-dag-p-aux hs g)
		    (member h hs)
		    (term-dag-is-p h g))
	      (not (consp (neighbors x g))))))


  (local
   (defthm init-term-dag-p-aux-member-3
     (implies (and (init-term-dag-p-aux hs g)
		   (member h hs))
	      (nat-true-listp (neighbors h g)))))

  (local
   (defthm nth-non-nil
     (implies (and (<= (len l) n) (natp n))
	      (not (nth n l)))))


;; If a node is not an "is" node, then is smaller than its neighbors.

  (defthm init-term-dag-p-property-1
    (implies (and (natp h)
		  (init-term-dag-p g)
		  (not (term-dag-is-p h g)))
	     (smaller-than-list h (neighbors h g)))
    :hints (("Goal" :cases ((< h (len g))))
	    ("Subgoal 1" :in-theory (disable neighbors))))

;; Non--terminal nodes are not neighbors of an "is" node:

  (defthm init-term-dag-p-property-2
    (implies (and (natp h)
		  (init-term-dag-p g)
		  (term-dag-is-p h g)
		  (consp (neighbors x g)))
	     (not (member x (neighbors h g))))
    :hints (("Goal" :cases ((< h (len g))))
	    ("Subgoal 1" :in-theory (disable neighbors))))


;; The list neighbors of every node is a true list of natural numbers:

  (defthm init-term-dag-p-property-3
    (implies (and (init-term-dag-p g) (natp h))
	     (nat-true-listp (neighbors h g)))
    :hints (("Goal" :cases ((< h (len g))))
	    ("Subgoal 1" :in-theory (disable neighbors))))))



;;; The three above properties should suffice:

(local (in-theory (disable neighbors init-term-dag-p)))


;;; The following sequence of events show that every path in graph
;;; verifying {\tt init-term-dag-p} has no duplicates. The main idea is
;;; to split every path into two pieces. The first one is a path with
;;; its nodes in a strictly increasing order. The second part is empty
;;; or a variable node that (obviusly) does not appear in the first
;;; part. Both parts has no nodes in common and no duplicate nodes:


(local
 (encapsulate
  ()

  (local
   (defun strictly-increasing-path-piece (p)
     (cond ((or (endp p) (endp (cdr p))) p)
	   ((< (first p) (second p)) (cons (car p)
					   (strictly-increasing-path-piece (cdr
									    p))))
	   (t (list (first p))))))

  (local
   (defun remaining-path-piece (p)
     (cond ((endp p) p)
	   ((endp (cdr p)) (cdr p))
	   ((< (first p) (second p)) (remaining-path-piece (cdr p)))
	   (t (cdr p)))))

  (local
   (defthm strictly-increasing-path-piece-append-remaining-path-piece
     (equal (append (strictly-increasing-path-piece p)
		    (remaining-path-piece p))
	    p)))

  (local
   (defun strictly-increasing-list (l)
     (cond ((endp l) t)
	   ((endp (cdr l)) t)
	   (t (and (< (first l) (second l))
		   (strictly-increasing-list (cdr l)))))))

  (local
   (defthm strictly-increasing-path-piece-strictly-increasing
     (strictly-increasing-list  (strictly-increasing-path-piece p))))


  (local
   (defthm smaller-than-list-main-property
     (implies (and (member y l)
		   (not (< x y)))
	      (not (smaller-than-list x l)))))

  (local
   (defthm map-nfix-true-listp
     (implies (nat-true-listp l)
	      (equal (map-nfix l) l))))

  (local
   (defthm init-term-dag-p-neighbors-main-property
     (implies (and (natp h)
		   (<= x h)
		   (consp (neighbors x g))
		   (init-term-dag-p g))
	      (not (member x (neighbors h g))))
     :hints (("Goal" :cases ((term-dag-is-p h g)))
	     ("Subgoal 2" :use init-term-dag-p-property-1))))



  (local
   (defthm remaining-path-piece-main-property
     (implies (and (path-p p g)
		   (init-term-dag-p g)
		   (not (endp (remaining-path-piece p))))
	      (not (consp (neighbors (first (remaining-path-piece p)) g))))))


  (local
   (defthm path-p-with-its-first-a-terminal-node
     (implies (and (path-p p g)
		   (not (endp (cdr p))))
	      (consp (neighbors (first p) g)))))


  (local
   (defthm remaining-path-piece-main-property-corollary
     (implies (and (path-p p g)
		   (init-term-dag-p g)
		   (not (endp (remaining-path-piece p))))
	      (not (consp (cdr (remaining-path-piece p)))))))



  (local
   (defthm strictly-increasing-path-piece-non-terminal-nodes
     (implies (and (path-p p g)
		   (member x (strictly-increasing-path-piece p))
		   (not (endp (remaining-path-piece p))))
	      (consp (neighbors x g)))))

  (local
   (defthm no-duplicatesp-strictly-increasing
     (implies (strictly-increasing-list p)
	      (no-duplicatesp p))))

  (local
   (defthm no-duplicatesp-unitary
     (implies (endp (cdr l))
	      (no-duplicatesp l))))


  (local
   (defthm disjointp-remaining-path-piece-and-strictly-increasing-path-piece
     (implies (and (consp (remaining-path-piece p))
		   (path-p p g)
		   (init-term-dag-p g))
	      (disjointp (remaining-path-piece p)
			 (strictly-increasing-path-piece p)))
     :hints (("Goal" :expand (disjointp (remaining-path-piece p)
					(strictly-increasing-path-piece
					 p))
	      :in-theory (disable disjointp-conmutative))
	     ("Goal'" :use remaining-path-piece-main-property))))


  (local
   (defthm path-p-init-term-dag-p-previous-lemma
     (implies (and (path-p p g)
		   (init-term-dag-p g))
	      (no-duplicatesp
	       (append (strictly-increasing-path-piece p)
		       (remaining-path-piece p))))
     :hints (("Goal"
	      :in-theory
	      (disable
	       strictly-increasing-path-piece-append-remaining-path-piece)
	      :cases ((endp (remaining-path-piece p)))))))



  (local
   (defthm path-p-init-term-dag-p
     (implies (and (path-p p g)
		   (init-term-dag-p g))
	      (no-duplicatesp p))
     :hints (("Goal" :use path-p-init-term-dag-p-previous-lemma))))


  (defthm init-term-dag-p-implies-dag-p
    (implies (init-term-dag-p g)
	     (dag-p g))
    :hints (("Goal" :use dag-p-soundness)))))


;;; -----------------------------------
;;;
;;; 2.3) The property {\tt tree-p-p} implies {\tt dag-p}
;;;
;;; -----------------------------------

;;; This is an easy corollary of the result of the previous section and
;;; the fact that @tree-p@  is a particular case of @init-term-dag-p@:

(local
 (defthm tree-p-aux-implies-init-term-dag-p-aux
   (implies (tree-p-aux hs g)
	    (init-term-dag-p-aux hs g))))

(local
 (defthm tree-p-implies-init-term-dag-p
   (implies (tree-p g)
	    (init-term-dag-p g))
   :hints (("Goal" :in-theory (enable init-term-dag-p)))))


;;; ============================================================================
;;;
;;; 3) About the {\tt tree-p} property of terms stored with {\tt term\--as\--dag\--aux\--l-ns}
;;;
;;; ============================================================================

;;; We now show that, under some conditions, the graph stored by the
;;; function {\tt term-as-dag-aux-l-ns} has the {\tt tree-p} property:

;;; First, we are prove some interesting properties about the function
;;; {\tt term-as-dag-aux-l-ns} that will be useful in general:

;;; The available index after storing is an integer bigger than the
;;; input index.  For terms, it is strictly bigger.

(local
 (defthm term-as-dag-aux-ns-increases-index
   (<= h (second (term-as-dag-aux-l-ns flg term g h)))
   :rule-classes :linear))

(local
 (defthm term-as-dag-aux-ns-increases-index-strictly
   (< h (second (term-as-dag-aux-l-ns t term g h)))
   :rule-classes :linear))

(local
 (defthm term-as-dag-aux-l-ns-second-value-integerp
   (implies (integerp h)
	    (integerp (second (term-as-dag-aux-l-ns flg term g h))))))

;;; The list of indices returned as the third value is a natural true
;;; list that with all its indices bigger than the input index:

(local
 (defthm  term-as-dag-aux-l-ns-nat-true-listp
   (implies (and (natp h) (term-p-aux flg term))
	    (nat-true-listp
	     (third (term-as-dag-aux-l-ns flg term g h))))))

(local
 (defthm  term-as-dag-aux-l-ns-smaller-than-list
   (implies (and (natp h) (natp h1) (< h h1))
	    (smaller-than-list h
			       (third (term-as-dag-aux-l-ns nil term g h1))))))

;;; The length of the graph finally obtained is bigger (or equal) than
;;; the the first available index returned:

(local
 (defthm len-term-as-dag-aux-l-ns
   (let* ((res (term-as-dag-aux-l-ns flg term g h))
	  (gf (first res))
	  (hf (second res)))
     (implies (and (natp h) (or flg (consp term)))
	      (<= hf (len gf))))
   :rule-classes :linear))

;;; All the indices in the list returned are smaller than the first
;;; available index returned:

(local
 (defun bigger-than-list (h l)
   (if (endp l)
       t
     (and (> h (car l))
	  (bigger-than-list h (cdr l))))))


(local
 (defthm term-as-dag-aux-l-ns-bigger-than-list
   (implies (natp h)
	    (bigger-than-list (second (term-as-dag-aux-l-ns flg term g h))
			      (third (term-as-dag-aux-l-ns flg term g h))))))



;;; The following sequence of events leads to show the intended property
;;; for {\tt term-as-dag-aux-l-ns}.

;;; The elements above the input index are not modified, so the
;;; @tree-p-aux@ property is preserved:

(local
 (defthm term-as-dag-aux-l-ns-initial-segment-nth
   (implies (and (natp h) (natp h1) (< h h1))
	    (equal (nth h (first (term-as-dag-aux-l-ns flg term g h1)))
		   (nth h g)))))




(local
 (defthm tree-p-aux-term-as-dag-aux-l-ns-initial-segment
   (implies (and (natp h) (natp h1) (<= h h1))
	    (equal (tree-p-aux (list-from-to h h1)
			       (first (term-as-dag-aux-l-ns flg term g h1)))
		   (tree-p-aux (list-from-to h h1) g)))))



;;; Concatenation of list of indices w.r.t. {\tt tree-p-aux}:

(local
 (defthm tree-p-aux-append
   (equal (tree-p-aux (append l1 l2) g)
	  (and (tree-p-aux l1 g) (tree-p-aux l2 g)))))



;;; A particular case, applied to solve one of the induction case of the
;;; theorem below:

(local
 (defthm tree-p-aux-append-particular-case
   (let* ((h1 (second (term-as-dag-aux-l-ns flg1 term1 g1 h)))
	  (h2 (second (term-as-dag-aux-l-ns flg2 term2 g2 h1))))
     (implies (natp h)
	      (equal (tree-p-aux (list-from-to h h2) g3)
		     (and (tree-p-aux (list-from-to h h1) g3)
			  (tree-p-aux (list-from-to h1 h2) g3)))))
   :hints (("Goal"
	    :in-theory (disable tree-p-aux-append)
	    :use
	    (:instance
	     tree-p-aux-append
	     (l1 (list-from-to
		  h
		  (second (term-as-dag-aux-l-ns flg1 term1 g1 h))))
	     (l2 (list-from-to
		  (second
		   (term-as-dag-aux-l-ns flg1 term1 g1 h))
		  (second
		   (term-as-dag-aux-l-ns
		    flg2 term2 g2
		    (second (term-as-dag-aux-l-ns flg1 term1 g1 h))))))

	     (g g3))))))


;;; Above the first available index, the graph is not modified:

(local
 (defthm term-as-dag-aux-l-ns-final-segment-nth
   (let* ((res (term-as-dag-aux-l-ns flg term g h1))
	  (hf (second res)))
     (implies (and (natp h) (<= hf h) (natp h1))
	      (equal (nth h (car (term-as-dag-aux-l-ns flg term g h1)))
		     (nth h g))))))

;;; And finally the intended property about {\tt term-as-dag-aux-l-ns}:

(local
 (encapsulate
  ()
  (local
   (defthm variable-p-property-element-tree-p
     (implies (variable-p term)
	      (property-element-tree-p h (cons term t)))))


  (local
   (defthm compound-node-property-element-tree-p
     (implies (and (natp h) (natp h1) (< h h1) (term-p-aux nil term))
	      (property-element-tree-p
	       h
	       (cons symb (third (term-as-dag-aux-l-ns nil term g h1)))))))


  (defthm tree-p-aux-term-as-dag-aux-l-ns-update-nth
    (implies (and (natp h) (natp h1) (< h h1))
	     (equal (tree-p-aux (list-from-to h1 h2)
				(update-nth h x g))
		    (tree-p-aux (list-from-to h1 h2)  g))))


  (defthm term-as-dag-aux-l-ns-implies-tree-p-main-lemma
    (let* ((res (term-as-dag-aux-l-ns flg term g h))
	   (gf (first res))
	   (hf (second res)))
      (implies (and (natp h) (term-p-aux flg term))
	       (tree-p-aux (list-from-to h hf) gf)))
    :hints (("Goal" :in-theory (disable property-element-tree-p))))))


;;; The above property about {\tt tree-p-aux} can be used to prove the
;;; intended property about {\tt tree-p}, but before we need some
;;; technical lemmas:

(local
 (encapsulate
  ()

  (local
   (defthm nth-non-nil
     (implies (and (<= (len l) n) (natp n))
	      (not (nth n l)))))

  (local
   (defthm tree-p-aux-member
     (implies (and (tree-p-aux l g) (member x l))
	      (property-element-tree-p x (nth x g)))
     :rule-classes nil))




  (defthm tree-p-aux-property-element-p
    (implies (and (tree-p g) (natp h))
	     (property-element-tree-p h (nth h g)))
    :hints (("Goal" :cases ((< h (len g))))
	    ("Subgoal 1" :use
	     (:instance tree-p-aux-member
			(x h)
			(l (list-from-to 0 (len g)))))))))


;;; The following sequence of events show the intended result:

(local
 (encapsulate
  ()

  (local
   (defthm term-as-dag-aux-l-ns-implies-tree-p-initial-segment
     (let* ((res (term-as-dag-aux-l-ns flg term g h))
	    (gf (first res)))
       (implies (and (natp h) (tree-p g) (subsetp l (list-from-to 0 h)))
		(tree-p-aux l gf)))
     :hints (("Goal"
	      :induct (len l)
	      :in-theory (disable property-element-tree-p)))))

  (local
   (defthm term-as-dag-aux-l-ns-implies-tree-p-final-segment
     (let* ((res (term-as-dag-aux-l-ns flg term g h))
	    (gf (first res))
	    (hf (second res)))
       (implies (and (natp h) (tree-p g) (subsetp l (list-from-to hf (len gf))))
		(tree-p-aux l gf)))
     :hints (("Goal"
	      :induct (len l)
	      :in-theory (disable property-element-tree-p)))))

  (local
   (defthm term-as-dag-aux-l-ns-implies-tree-p-almost
     (let* ((res (term-as-dag-aux-l-ns flg term g h))
	    (gf (first res))
	    (hf (second res))
	    (l1 (list-from-to 0 h))
	    (l2 (list-from-to h hf))
	    (l3 (list-from-to hf (len gf))))
       (implies (and (natp h) (tree-p g) (term-p-aux flg term))
		(tree-p-aux (append l1 (append l2 l3)) gf)))
     :rule-classes nil))


;; This is the intended result of this section: the term stored by {\tt
;; term-as-dag-aux-l-ns} verifies {\tt tree-p}:

  (defthm term-as-dag-aux-l-ns-implies-tree-p
    (implies (and (natp h)
		  (tree-p g)
		  (term-p-aux flg term))
	     (tree-p (first (term-as-dag-aux-l-ns flg term g h))))
    :hints (("Goal"
	     :cases ((and (not flg) (not (consp term)))))
	    ("Subgoal 2"
	     :in-theory (disable
			 tree-p-aux-append
			 append-list-from-to)
	     :use
	     (term-as-dag-aux-l-ns-implies-tree-p-almost
	      (:instance append-list-from-to
			 (h1 0) (h2 h)
			 (h3 (cadr (term-as-dag-aux-l-ns flg term g h))))
	      (:instance append-list-from-to
			 (h1 h)
			 (h2 (cadr (term-as-dag-aux-l-ns flg term g h)))
			 (h3 (len (car (term-as-dag-aux-l-ns flg term g h)))))
	      (:instance append-list-from-to
			 (h1 0) (h2 h)
			 (h3 (len (car (term-as-dag-aux-l-ns flg term g h)))))))))))


;;; ============================================================================
;;;
;;; 4) The term stored using {\tt term-as-dag-aux-l-ns} is the input term
;;;
;;; ============================================================================

;;; In this section we show that {\tt term-as-dag-aux-l-ns} builds a
;;; graph that represents the input term. That is, we want to show the
;;; following theorem:

;   (defthm term-as-dag-aux-l-ns-dag-as-term-aux-relationship
;     (let* ((res (term-as-dag-aux-l-ns flg term g h))
;            (g-ret (first res))
;	     (hs-ret (third res)))
;       (implies (and (natp h)
;		     (term-p-aux flg term)
;		     (tree-p g))
;		(equal (dag-as-term-l flg (if flg h hs-ret) g-ret)
;		       term))))


;;; We consider three stage in our proof:

;;; *)
;;;  First we show how the {\tt tree-p} property is related to updating
;;;  the graph. This is esssential to be able to use induction
;;;  hypothesis in the proof of the main result.
;;; *)
;;;  Then we introduce the notion of subgraph, and coincidence in a list
;;;  of indices. We show that when two graphs coincide in a subgraph,
;;;  then the terms stored are the same.
;;; *)
;;;  The above property can be used to use the induction hypothesis in
;;;  the proof of the main theorem, since, for example, it allows to use
;;;  the value of {\tt dag-as-term-l} in the rest of arguments of a list
;;;  of terms.
;;; *)
;;;  We prove the main theorem by induction on the structure of terms.
;;; -)



;;; -----------------------------------
;;;
;;; 4.1) The property {\tt tree-p} when updating a graph
;;;
;;; -----------------------------------

;;; The following sequence of events are needed to prove the theorems
;;; {\tt tree-p-update-nth-variables} and {\tt
;;; tree-p-update-nth-smaller-than-list} below, showing how the {\tt
;;; tree-p} property is preserved by  the updatings done during the
;;; proccess of {\tt terms-as-dag-l-ns}:

(local
 (encapsulate

  ()


  (local
   (defthm len-update-nth-2
     (implies (natp h) (<= (1+ h) (len (update-nth h x g))))
     :rule-classes :linear))


  (local
   (defthm property-element-tree-p-variable-p
     (implies (variable-p term)
	      (property-element-tree-p h (cons term t)))))

  (local
   (defthm tree-p-aux-update-nth-variables
     (implies (and (tree-p-aux hs g) (variable-p term))
	      (tree-p-aux hs (update-nth h (cons term t) g)))
     :hints (("Goal" :in-theory (disable property-element-tree-p)
	      :induct (len hs)))))

  (local
   (defthm nth-non-nil
     (implies (and (<= (len l) n) (natp n))
	      (not (nth n l)))))

  (local
   (defthm tree-p-aux-list-from-to-beyond-length-lemma
     (implies (and (<= (len g) h1)
		   (natp h1)
		   (subsetp hs (list-from-to (len g) h1)))
	      (tree-p-aux hs g))))

  (local
   (defthm tree-p-aux-list-from-to-beyond-length
     (implies (and (natp h1) (<= (len g) h1)
		   (tree-p-aux (list-from-to 0 (len g)) g))
	      (tree-p-aux (list-from-to 0 h1) g))
     :hints (("Goal"
	      :in-theory (disable
			  tree-p-aux-append
			  append-list-from-to)
	      :use
	      ((:instance append-list-from-to
			  (h1 0) (h2 (len g)) (h3 h1))
	       (:instance tree-p-aux-append
			  (l1 (list-from-to 0 (len g)))
			  (l2 (list-from-to (len g) h1))))))
     :rule-classes nil))

  (local
   (defthm property-element-tree-p-smaller-than-list
     (implies (and (nat-true-listp l)
		   (smaller-than-list h l))
	      (property-element-tree-p h (cons x l)))))


  (local
   (defthm tree-p-aux-update-nth-smaller-than-list
     (implies (and (tree-p-aux hs g)
		   (natp h)
		   (local-nat-listp hs)
		   (nat-true-listp l)
		   (smaller-than-list h l))
	      (tree-p-aux hs (update-nth h (cons x l) g)))
     :hints (("Goal" :in-theory (disable property-element-tree-p)
	      :induct (len hs)))))


;; These are the main theorems in this subsection, establishing that the
;; {\tt tree-p} property is preserved after updating the graph like our
;; function does:

  (defthm tree-p-update-nth-variables
    (implies (and (tree-p g) (variable-p term) (natp h))
	     (tree-p (update-nth h (cons term t) g)))
    :hints (("Goal"
	     :use
	     (:instance tree-p-aux-list-from-to-beyond-length
			(h1 (len (update-nth h (cons term t) g)))))))


  (defthm tree-p-update-nth-smaller-than-list
    (implies (and (tree-p g) (natp h)
		  (nat-true-listp l)
		  (smaller-than-list h l))
	     (tree-p (update-nth h (cons x l) g)))
    :hints (("Goal" :use
	     (:instance tree-p-aux-list-from-to-beyond-length
			(h1 (len (update-nth h (cons x l) g)))))))))

;;; All the needed properties about {\tt tree-p} are now extracted:

(local (in-theory (disable tree-p)))


;;; -----------------------------------
;;;
;;; 4.2) Subgraphs and coincidence in subgraphs.
;;;
;;; -----------------------------------

;;; When proving (by induction on the structure of terms) the main
;;; theorem of this section, one of the induction case is a to prove the
;;; result for non--empty list of terms, having as induction hypothesis
;;; the result for the first term and the result for the list of the
;;; rest of terms. To be able to use the first hypothesis, we must show
;;; that the first term after completing the process it is stored in the
;;; same way as before storing the rest of the terms. And this is true
;;; because every time a subterm is stored in the graph, it is stored in
;;; a subgraph, and subgraphs are "closed" w.r.t. the terms
;;; stored. We prove this in this subsection.


;;; Let us first define the notion of {\em subgraph}. The following
;;; function checks that that all the neighbors (in a graph @g@) of a
;;; list of nodes @hs1@ are a subset of the list of nodes @hs2@:

(local
 (defun subsetp-all-neighbors (hs1 hs2 g)
   (if (endp hs1)
       t
     (and (subsetp (neighbors (car hs1) g) hs2)
	  (subsetp-all-neighbors (cdr hs1) hs2 g)))))

;;; A list of nodes @hs@ is a subgraph of a graph @g@ if all the
;;; neighbors of the nodes of the list are in the same list.

(local
 (defun subgraph-p (hs g)
   (subsetp-all-neighbors hs hs g)))

;;; The following function define the notion of {\em coincidence} in a
;;; set of indices of two given graphs.

(local
 (defun graph-coincide (hs g1 g2)
   (if (endp hs)
       t
     (and (equal (nth (car hs) g1) (nth (car hs) g2))
	  (graph-coincide (cdr hs) g1 g2)))))


;;; Now we show that if we have two directed acyclic graphs such that a
;;; given set of indices form a subgraph and coincide in both subgraphs,
;;; then the function @dag-as-term-l@ behaves in the same way in both
;;; subgraphs, for every index of the subgraph. The following sequence
;;; of events show this (theorem {\tt
;;; dag-as-term-l-equal-when-coincide-in-subgraphs} below):

(local
 (encapsulate
  ()

  (local
   (defthm graph-coincide-forward-chaining
     (implies (and (graph-coincide hs g1 g2)
		   (member h hs))
	      (equal (nth h g1) (nth h g2)))
     :rule-classes :forward-chaining))

  (local (in-theory (enable neighbors)))

  (local
   (defthm subsetp-coincide-property-1
     (implies (and (subsetp-all-neighbors hs1 hs2 g)
		   (not (term-dag-variable-p h g))
		   (not (term-dag-is-p h g))
		   (member h hs1))
	      (subsetp (term-dag-args h g) hs2))))

  (local
   (defthm subsetp-coincide-property-2
     (implies (and (subsetp-all-neighbors hs1 hs2 g)
		   (term-dag-is-p h g)
		   (member h hs1))
	      (member (nth h g) hs2))))

  (defthm subsetp-all-neighbors-forward-chaining
    (implies (and (subsetp-all-neighbors hs1 hs2 g1)
		  (graph-coincide hs1 g1 g2))
	     (subsetp-all-neighbors hs1 hs2 g2))
    :rule-classes (:forward-chaining :rewrite))

  (local (in-theory (disable neighbors)))


  (defthm dag-as-term-l-equal-when-coincide-in-subgraphs
    (implies (and (subgraph-p hs g1)
		  (graph-coincide hs g1 g2)
		  (dag-p g1) (dag-p g2)
		  (if flg (member h hs) (subsetp h hs)))
	     (equal (dag-as-term-l flg h g1)
		    (dag-as-term-l flg h g2)))
    :rule-classes :forward-chaining)))


;;; -----------------------------------
;;;
;;; 4.3) Subgraphs and coincidence in {\tt term-as-dag-aux-l-ns}
;;;
;;; -----------------------------------

;;; We prove now that the terms stored by {\tt term-as-dag-aux-l-ns} are
;;; subgraphs of the entire graph. And we also prove that if two terms
;;; are stored sequentially, then the graph obtained after storing the
;;; first term coincide, in the subgraph corresponding to that subterm,
;;; with the graph obtained after succesively storing the first and then
;;; the second.

;;; In this way, we can use the result in the previous subsection to
;;; prove two theorems that will allow us to use the induction
;;; hypothesis in the proof of the main theorem in this section.

;;; First, we show the result about coincidence of graphs obtained after
;;; storing two terms sequentially:

(local
 (defthm graph-coincide-term-as-dag-aux-l-ns
   (implies (and (natp h)
		 (subsetp
		  hs
		  (list-from-to h (second (term-as-dag-aux-l-ns flg1 term1 g h)))))
	    (graph-coincide
	     hs
	     (first (term-as-dag-aux-l-ns flg1 term1 g h))
	     (first (term-as-dag-aux-l-ns
		     flg2 term2
		     (first (term-as-dag-aux-l-ns flg1 term1 g h))
		     (second (term-as-dag-aux-l-ns flg1 term1 g h))))))
   :hints (("Goal" :induct (len hs)))))



;;; And now, the following sequence of events show that the nodes of the
;;; graph used to store a term are a subgraph of the entire graph:

(local
 (encapsulate
  ()
  (local
   (defthm subsetp-all-neighbors-append
     (implies (and (subsetp-all-neighbors hs11 hs12 g)
		   (subsetp-all-neighbors hs21 hs22 g))
	      (subsetp-all-neighbors (append hs11 hs21)
				     (append hs12 hs22)
				     g))))

  (local
   (defthm very-ugly-lemma
     (let* ((ret-term1 (term-as-dag-aux-l-ns t term1 g h))
	    (g-term1 (first ret-term1))
	    (h1 (second ret-term1))
	    (ret-term2  (term-as-dag-aux-l-ns nil term2 g-term1 h1))
	    (g-term2 (first ret-term2))
	    (h2 (second ret-term2))
	    (l1 (list-from-to h h1))
	    (l2 (list-from-to h1 h2))
	    (l (list-from-to h h2)))
       (implies (and (natp h)
		     (subsetp-all-neighbors l1 l1 g-term2)
		     (subsetp-all-neighbors l2 l2 g-term2))
		(subsetp-all-neighbors l l g-term2)))
     :hints (("Goal" :use
	      (:instance
	       subsetp-all-neighbors-append
	       (hs11 (list-from-to h (second (term-as-dag-aux-l-ns t term1 g h))))
	       (hs12 (list-from-to h (second (term-as-dag-aux-l-ns t term1 g h))))
	       (hs21 (list-from-to
		      (second (term-as-dag-aux-l-ns t term1 g h))
		      (second (term-as-dag-aux-l-ns
			       nil term2
			       (first (term-as-dag-aux-l-ns t term1 g h))
			       (second (term-as-dag-aux-l-ns t term1 g h))))))
	       (hs22 (list-from-to
		      (second (term-as-dag-aux-l-ns t term1 g h))
		      (second (term-as-dag-aux-l-ns
			       nil term2
			       (first (term-as-dag-aux-l-ns t term1 g h))
			       (second (term-as-dag-aux-l-ns t
							     term1 g h))))))
	       (g (first (term-as-dag-aux-l-ns
			  nil term2
			  (first (term-as-dag-aux-l-ns t term1 g h))
			  (second (term-as-dag-aux-l-ns t term1 g
							h))))))))))



  (local
   (defthm subsetp-all-neighbors-coincide-particular-case
     (let* ((ret-term (term-as-dag-aux-l-ns flg term g h))
	    (g-term1 (first ret-term))
	    (h1 (second ret-term))
	    (hs (list-from-to h h1))
	    (g-term2 (first (term-as-dag-aux-l-ns flg2 term2 g-term1 h1))))
       (implies (and (natp h) (subsetp-all-neighbors hs hs g-term1))
		(subsetp-all-neighbors hs hs g-term2)))))

  (local
   (defthm subsetp-all-neighbors-cons-second-argument-lemma
     (implies (subsetp-all-neighbors l1 l2 g)
	      (subsetp-all-neighbors l1 (cons x l2) g))))

  (local
   (defthm subsetp-all-neighbors-cons-second-argument
     (implies (and (subsetp (neighbors x g) l)
		   (subsetp-all-neighbors l l g))
	      (subsetp-all-neighbors (cons x l) (cons x l) g))))

  (local
   (defthm subsetp-all-neighbors-update-nth-not-member
     (implies (and (not (member h hs1))
		   (natp h)
		   (nat-true-listp hs1)
		   (subsetp-all-neighbors hs1 hs2 g))
	      (subsetp-all-neighbors hs1 hs2 (update-nth h x g)))
     :hints (("Goal" :in-theory (enable neighbors)))))

  (local
   (defthm nat-true-listp-list-from-to
     (implies (natp h)
	      (nat-true-listp (list-from-to h h1)))))

  (defthm  term-as-dag-aux-l-ns-local-nat-listp
    (let* ((res (term-as-dag-aux-l-ns flg term g h))
	   (hs-ret (third res)))
      (implies (natp h)
	       (local-nat-listp hs-ret))))

  (defthm subsetp-list-from-to
    (implies
     (and (natp h1) (natp h2)
	  (local-nat-listp hs)
	  (smaller-than-list h1 hs)
	  (bigger-than-list h2 hs))
     (subsetp hs (list-from-to (1+ h1) h2))))

   (defthm subgraph-p-term-as-dag-aux-l-ns
     (implies (natp h)
	      (subgraph-p
	       (list-from-to
		h
		(second (term-as-dag-aux-l-ns flg term g h)))
	       (first (term-as-dag-aux-l-ns flg term g h))))
     :hints (("Goal" :in-theory (enable neighbors))))))


(local (in-theory (disable subgraph-p)))



;;; -----------------------------------
;;;
;;; 4.4) The main result of this section
;;;
;;; -----------------------------------


;;; We apply the results of the previous subsections to prove the main
;;; lemmas needed to deal with two induction hypothesis in the proof of
;;; the main result of this subsection.


(local
 (encapsulate
  ()

;; This lemma allows to use one the induction hypothesis (the one for
;; the first term of the list) in the induction case corresponding to
;; non-empty list of terms:

  (local
   (defthm equal-dag-as-term-l-composition-of-term-as-dag-l
     (let* ((ret-term1 (term-as-dag-aux-l-ns t term1 g h))
	    (g-term1 (first ret-term1))
	    (h1 (second ret-term1))
	    (ret-term2 (term-as-dag-aux-l-ns nil term2 g-term1 h1))
	    (g-term2 (first ret-term2)))
       (implies (and (natp h) (tree-p g)
		     (term-p-aux t term1) (term-p-aux nil term2))
		(equal (dag-as-term-l t h g-term2)
		       (dag-as-term-l t h g-term1))))
     :hints (("Goal" :use
	      (:instance
	       dag-as-term-l-equal-when-coincide-in-subgraphs
	       (flg t)
	       (g1 (first (term-as-dag-aux-l-ns t term1 g h)))
	       (g2 (first
		    (term-as-dag-aux-l-ns
		     nil term2
		     (first
		      (term-as-dag-aux-l-ns t term1 g h))
		     (second (term-as-dag-aux-l-ns t term1 g h)))))

	       (hs (list-from-to
		    h (second (term-as-dag-aux-l-ns t term1 g h)))))))))

  (local
   (defthm graph-coincide-update-nth
     (implies (and (not (member h hs)) (natp h) (local-nat-listp hs))
	      (graph-coincide hs g (update-nth h x g)))))

  (local
   (defthm list-from-to-local-nat-listp
     (implies (and (natp h1) (natp h2))
	      (local-nat-listp (list-from-to h1 h2)))))



;; And this lemma allows to use the induction hypothesis (the one for
;; the list of its arguments) corresponding to the induction case of
;; non-variable terms:


  (local
   (defthm equal-dag-as-term-l-update-nth
     (implies
      (and (natp h)
	   (term-p-aux nil term)
	   (tree-p g))
      (equal
       (dag-as-term-l
	nil
	(third (term-as-dag-aux-l-ns nil term g (+ 1 h)))
	(update-nth h
		    (cons f (third (term-as-dag-aux-l-ns nil term g (1+ h))))
		    (first (term-as-dag-aux-l-ns nil term g (+ 1 h)))))
       (dag-as-term-l
	nil
	(third (term-as-dag-aux-l-ns nil term g (+ 1 h)))
	(first (term-as-dag-aux-l-ns nil term g (+ 1 h))))))
     :hints
     (("Goal"
       :use
       (:instance
	dag-as-term-l-equal-when-coincide-in-subgraphs
	(flg nil)
	(h (third (term-as-dag-aux-l-ns nil term g (+ 1 h))))
	(g1 (first (term-as-dag-aux-l-ns nil term g (+ 1 h))))
	(g2 (update-nth
	     h
	     (cons f (third (term-as-dag-aux-l-ns nil term g (1+ h))))
	     (first (term-as-dag-aux-l-ns nil term g (+ 1 h)))))
	(hs (list-from-to
	     (1+ h)
	     (second (term-as-dag-aux-l-ns nil term g (+ 1 h))))))))))

;; And finally the intended result of this section:

  (local
   (defthm term-as-dag-aux-l-ns-dag-as-term-aux-relationship
     (let* ((res (term-as-dag-aux-l-ns flg term g h))
	    (g-ret (first res))
	    (hs-ret (third res)))
       (implies (and (natp h)
		     (term-p-aux flg term)
		     (tree-p g))
		(equal (dag-as-term-l flg (if flg h hs-ret) g-ret)
		       term)))
     :rule-classes nil))

;; This result is better used in form of a rewriting rule:

  (defthm term-as-dag-aux-l-ns-dag-as-term-aux-relationship-rewrite-rule
    (let* ((res (term-as-dag-aux-l-ns flg term g h))
	   (g-ret (first res))
	   (hs-ret (third res)))
      (implies (and (natp h)
		    (term-p-aux flg term)
		    (tree-p g)
		    (equal h1 (if flg h hs-ret)))
	       (equal (dag-as-term-l flg h1 g-ret)
		      term)))
    :hints (("Goal"
	     :use term-as-dag-aux-l-ns-dag-as-term-aux-relationship)))))


;;; ============================================================================
;;;
;;; 5) Relationship between {\tt terms\--as\--dag\--aux\--l-ns} and {\tt terms\--as-dag\--aux\--l}
;;;
;;; ============================================================================

;;; Our goal in this subsection is to prove the following theorem:


;  (defthm term-as-dag-aux-l-in-two-steps
;    (let* ((res1 (term-as-dag-aux-l flg term g h variables))
;	    (g-ret1 (first res1))
;	    (res2  (term-as-dag-aux-l-ns flg term g h))
;	    (g-ret2 (first res2))
;	    (h-ret2 (second res2)))
;      (implies (and (natp h)
;		    (term-p-aux flg term))
;	        (equal g-ret1
;		       (first (make-shared-variables h h-ret2 g-ret2 variables))))

;;; That is, we are going to prove that the graph obtained after storing
;;; a term using the function {\tt term-as-dag-aux-l} is the same than
;;; the graph obtained storing the same term with {\tt
;;; term-as-dag-aux-l-ns} and after that applying {\tt
;;; make-shared-variables}.



;;; The following two properties show that the second value returned by
;;; term-as-dag-aux-l is a natural number (that is, the first available
;;; index after storing the graph).

(local
 (defthm integerp-term-as-dag-aux-l
   (implies (integerp h)
	    (integerp (second (term-as-dag-aux-l flg term g h variables))))))

(local
 (defthm integerp-term-as-dag-aux-l-natp
   (implies (natp h)
	    (>= (second (term-as-dag-aux-l flg term g h variables)) 0))
   :rule-classes :linear))



;;; Now we establish some properties about the function {\tt
;;; make-shared-variables} (see its definition in Section 1):


;;; First, a property about updating the result of a {\tt
;;; make-shared-variables} outside the range where the sharing of
;;; variables occur.

(local
 (defthm update-nth-update-nth-disjoint
   (implies (and (natp h1) (natp h2) (< h2 h1))
	    (equal (update-nth h1 x (update-nth h2 y g))
		   (update-nth h2 y (update-nth h1 x g))))))


(local
 (defthm make-shared-variables-update-nth-disjoint-graph
   (implies
    (and (natp h) (natp h1) (natp h2) (< h h1) (<= h1 h2))
    (equal
     (first (make-shared-variables
	     h1 h2 (update-nth h x g) variables))
     (update-nth h x (first (make-shared-variables h1 h2 g variables)))))
   :hints (("Goal" :induct (make-shared-variables h1 h2 g variables))
	   ("Subgoal *1/4.2" :expand (make-shared-variables h1 h2 (update-nth h x g)
							    variables))
	   ("Subgoal *1/3.2" :expand (make-shared-variables h1 h2 (update-nth h x g)
							    variables))
	   ("Subgoal *1/2.2" :expand (make-shared-variables h1 h2 (update-nth h x g)
							    variables)))))

;;; A fundamental lemma, describing how make shared variables can be
;;; done in several stages:

(local
 (defthm make-shared-composition
   (implies (and (<= h1 h2) (<= h2 h3)
		 (natp h1) (natp h2) (natp h3))
	    (equal (make-shared-variables h1 h3 g variables)
		   (make-shared-variables
		    h2 h3
		    (first (make-shared-variables h1 h2 g variables))
		    (second (make-shared-variables h1 h2 g variables)))))
   :rule-classes nil))

;;; The following two lemmas show that some nodes of the original graph
;;; do not change after making the variables shared:


(local
 (defthm nth-make-shared-variables
   (implies (and (natp h) (natp h1) (natp h2) (< h h1))
	    (equal (nth h
			(car
			 (make-shared-variables h1 h2 g variables)))
		   (nth h g)))))


(local
 (defthm make-shared-variables-does-not-change-non-variables
   (implies (not (term-dag-variable-p h g))
	    (equal (nth
		    h
		    (first (make-shared-variables h1 h2 g variables)))
		   (nth h g)))))



;;; The following @encapsulate@ contains the lemmas and definitions
;;; needed to deal with all the induction cases of the goal theorem in
;;; this section.



(local
 (encapsulate
  ()
  (local
   (defun induct-hint-for-last-index-independent-of-graph
     (flg term g1 g2 h)
     (if flg
	 (if (variable-p term)
	     (list g1 g2 h)
	   (induct-hint-for-last-index-independent-of-graph
	    nil (cdr term) g1 g2 (1+ h)))
       (if (endp term)
	   t
	 (mv-let (g1-n hcar ign)
		 (term-as-dag-aux-l-ns t (car term) g1 h)
		 (declare (ignore ign))
		 (mv-let (g2-n ign1 ign2)
			 (term-as-dag-aux-l-ns t (car term) g2 h)
			 (declare (ignore ign1 ign2))
			 (and (induct-hint-for-last-index-independent-of-graph
			       t (car term) g1 g2 h)
			      (induct-hint-for-last-index-independent-of-graph
			       nil (cdr term) g1-n g2-n hcar))))))))


;; The second and third values returned by {\tt term-as-dag-aux-l-ns} do
;; not depend on the graph:

  (local
   (defthm last-index-independent-of-graph
     (and
      (equal (second (term-as-dag-aux-l-ns flg term g1 h))
	     (second (term-as-dag-aux-l-ns flg term g2 h)))
      (equal (third (term-as-dag-aux-l-ns flg term g1 h))
	     (third (term-as-dag-aux-l-ns flg term g2 h))))

     :hints (("Goal"
	      :induct
	      (induct-hint-for-last-index-independent-of-graph
	       flg term g1 g2 h)))
     :rule-classes nil))


;; The second and third values returned by {\tt term-as-dag-aux-l-ns}
;; and {\tt term-as-dag-aux-l} are the same:


  (local
   (defthm term-as-dag-aux-l-ns-term-as-dag-aux-l-equal-second-argument
     (equal (second (term-as-dag-aux-l flg term g h variables))
	    (second (term-as-dag-aux-l-ns flg term g h)))
     :hints (("Subgoal *1/4'''" :use
	      (:instance last-index-independent-of-graph
			 (flg nil) (term term2)
			 (h (cadr (term-as-dag-aux-l-ns t term1 g h)))
			 (g1 (car (term-as-dag-aux-l t term1 g h
						     variables)))
			 (g2 (car (term-as-dag-aux-l-ns t term1 g h))))))))

  (local
   (defthm term-as-dag-aux-l-ns-term-as-dag-aux-l-equal-third-argument
     (equal (third (term-as-dag-aux-l flg term g h variables))
	    (third (term-as-dag-aux-l-ns flg term g h)))
     :hints (("Subgoal *1/4'''" :use
	      (:instance last-index-independent-of-graph
			 (flg nil) (term term2)
			 (h (cadr (term-as-dag-aux-l-ns t term1 g h)))
			 (g1 (car (term-as-dag-aux-l t term1 g h
						     variables)))
			 (g2 (car (term-as-dag-aux-l-ns t term1 g h))))))))



;; The following (very long lemmas) deal with induction case 4 of the
;; induction proof of our main goal:

  (local
   (defthm last-index-independent-of-graph-particular-case
     (equal
      (cadr
       (term-as-dag-aux-l-ns nil term2
			     (car (term-as-dag-aux-l t term1 g h variables))
			     (cadr (term-as-dag-aux-l-ns t term1 g h))))

      (cadr (term-as-dag-aux-l-ns nil term2
				  (car (term-as-dag-aux-l-ns t term1 g h))
				  (cadr (term-as-dag-aux-l-ns t term1 g
							      h)))))
     :hints (("Goal"
	      :use
	      (:instance last-index-independent-of-graph
			 (flg nil) (term term2)
			 (h (cadr (term-as-dag-aux-l-ns t term1 g h)))
			 (g1 (car (term-as-dag-aux-l t term1 g h
						     variables)))
			 (g2 (car (term-as-dag-aux-l-ns t term1 g h))))))))

   (local
   (defthm update-nth-term-as-dag-aux-l-ns-below
     (implies (and (natp h1) (natp h2) (< h1 h2))
	      (equal
	       (first (term-as-dag-aux-l-ns
		       flg term (update-nth h1 x g) h2))
	       (update-nth h1 x
			   (first (term-as-dag-aux-l-ns
				   flg term g h2)))))
     :hints (("Goal" :induct (term-as-dag-aux-l-ns
			      flg term g h2))
	     ("Subgoal *1/4.1'"
	      :use (:instance last-index-independent-of-graph
			      (flg t) (term term1)
			      (h h2)
			      (g1 (update-nth h1 x g)) (g2 g)))
	     ("Subgoal *1/2.1''"
	      :use (:instance last-index-independent-of-graph
			      (flg nil) (term term2) (g1 g) (h (+ 1 h2))
			      (g2 (update-nth h1 x g)))))))

  (local
   (defthm make-shared-variables-terms-as-dag-aux-l-ns-graph
     (implies (and (natp h1) (natp h2))
	      (equal (first
		      (make-shared-variables
		       h1 h2
		       (car (term-as-dag-aux-l-ns flg term g h2))
		       variables))
		     (first
		      (term-as-dag-aux-l-ns
		       flg term (car (make-shared-variables h1 h2 g
							    variables))
		       h2))))))

  (local
   (defthm make-shared-variables-terms-as-dag-aux-l-ns-variables
     (implies (and (natp h1) (natp h2))
	      (equal (second
		      (make-shared-variables
		       h1 h2
		       (car (term-as-dag-aux-l-ns flg term g h2))
		       variables))
		     (second
		      (make-shared-variables h1 h2 g variables))))
     :hints (("Goal" :induct (make-shared-variables h1 h2 g variables)))))


  (local
   (defthm induction-case-4-term-as-dag-aux-l-in-two-steps-graph
     (implies
      (and
       (equal
	(first (make-shared-variables h
				      (second (term-as-dag-aux-l-ns t term1 g h))
				      (first (term-as-dag-aux-l-ns t term1 g h))
				      variables))
	(first (term-as-dag-aux-l t term1 g h variables)))
       (equal
	(second (make-shared-variables h
				       (second (term-as-dag-aux-l-ns t term1 g h))
				       (first (term-as-dag-aux-l-ns t term1 g h))
				       variables))
	(fourth (term-as-dag-aux-l t term1 g h variables)))
       (equal
	(first
	 (make-shared-variables
	  (second (term-as-dag-aux-l-ns t term1 g h))
	  (second
	   (term-as-dag-aux-l-ns nil term2
                              (first (term-as-dag-aux-l t term1 g h variables))
                              (second (term-as-dag-aux-l-ns t term1 g h))))
	  (first
	   (term-as-dag-aux-l-ns nil term2
				 (first (term-as-dag-aux-l t term1 g h variables))
				 (second (term-as-dag-aux-l-ns t term1 g h))))
	  (fourth (term-as-dag-aux-l t term1 g h variables))))
	(first
	 (term-as-dag-aux-l nil term2
			    (first (term-as-dag-aux-l t term1 g h variables))
			    (second (term-as-dag-aux-l-ns t term1 g h))
			    (fourth (term-as-dag-aux-l t term1 g h variables)))))
       (equal
	(second
	 (make-shared-variables
	  (second (term-as-dag-aux-l-ns t term1 g h))
	  (second
	   (term-as-dag-aux-l-ns nil term2
				 (first (term-as-dag-aux-l t term1 g h variables))
				 (second (term-as-dag-aux-l-ns t term1 g h))))
	  (first
	   (term-as-dag-aux-l-ns nil term2
				 (first (term-as-dag-aux-l t term1 g h variables))
				 (second (term-as-dag-aux-l-ns t term1 g h))))
	  (fourth (term-as-dag-aux-l t term1 g h variables))))
	(fourth
	 (term-as-dag-aux-l nil term2
			    (first (term-as-dag-aux-l t term1 g h variables))
			    (second (term-as-dag-aux-l-ns t term1 g h))
			    (fourth (term-as-dag-aux-l t term1 g h variables)))))
       (natp h))
      (equal
       (first
	(make-shared-variables
	 h
	 (second (term-as-dag-aux-l-ns nil term2
				       (first (term-as-dag-aux-l-ns t term1 g h))
				       (second (term-as-dag-aux-l-ns t term1 g h))))
	 (first (term-as-dag-aux-l-ns nil term2
				      (first (term-as-dag-aux-l-ns t term1 g h))
				      (second (term-as-dag-aux-l-ns t term1 g h))))
      variables))
       (first
	(term-as-dag-aux-l nil term2
			   (first (term-as-dag-aux-l t term1 g h variables))
			   (second (term-as-dag-aux-l-ns t term1 g h))
			   (fourth (term-as-dag-aux-l t term1 g h variables))))))
     :hints (("Goal" :use
	   (:instance make-shared-composition
		      (h1 h)
		      (h2 (cadr (term-as-dag-aux-l-ns t term1 g h)))
		      (h3 (cadr (term-as-dag-aux-l-ns
				 nil term2
				 (car (term-as-dag-aux-l-ns t term1 g h))
				 (cadr (term-as-dag-aux-l-ns t term1 g h)))))
		      (g (car (term-as-dag-aux-l-ns
			       nil term2
			       (car (term-as-dag-aux-l-ns t term1 g h))
			       (cadr (term-as-dag-aux-l-ns t term1 g h))))))))))



  (local
   (defthm induction-case-4-term-as-dag-aux-l-in-two-steps-variable
     (implies
      (and
       (equal
	(first (make-shared-variables h
				    (second (term-as-dag-aux-l-ns t term1 g h))
				    (first (term-as-dag-aux-l-ns t term1 g h))
				    variables))
	(first (term-as-dag-aux-l t term1 g h variables)))
       (equal
	(second (make-shared-variables h
				     (second (term-as-dag-aux-l-ns t term1 g h))
				     (first (term-as-dag-aux-l-ns t term1 g h))
				     variables))
	(fourth (term-as-dag-aux-l t term1 g h variables)))
       (equal
	(first
	 (make-shared-variables
	  (second (term-as-dag-aux-l-ns t term1 g h))
	  (second (term-as-dag-aux-l-ns nil term2
				      (first (term-as-dag-aux-l-ns t term1 g h))
				      (second (term-as-dag-aux-l-ns t term1 g h))))
	  (first
	   (term-as-dag-aux-l-ns nil term2
				 (first (term-as-dag-aux-l t term1 g h variables))
				 (second (term-as-dag-aux-l-ns t term1 g h))))
	  (fourth (term-as-dag-aux-l t term1 g h variables))))
	(first
	 (term-as-dag-aux-l nil term2
			    (first (term-as-dag-aux-l t term1 g h variables))
			    (second (term-as-dag-aux-l-ns t term1 g h))
			    (fourth (term-as-dag-aux-l t term1 g h variables)))))
       (equal
	(second
	 (make-shared-variables
	  (second (term-as-dag-aux-l-ns t term1 g h))
	  (second (term-as-dag-aux-l-ns nil term2
				      (first (term-as-dag-aux-l-ns t term1 g h))
				      (second (term-as-dag-aux-l-ns t term1 g h))))
	  (first
	   (term-as-dag-aux-l-ns nil term2
				 (first (term-as-dag-aux-l t term1 g h variables))
				 (second (term-as-dag-aux-l-ns t term1 g h))))
	  (fourth (term-as-dag-aux-l t term1 g h variables))))
	(fourth
	 (term-as-dag-aux-l nil term2
			    (first (term-as-dag-aux-l t term1 g h variables))
			    (second (term-as-dag-aux-l-ns t term1 g h))
			    (fourth (term-as-dag-aux-l t term1 g h variables)))))
       (natp h))
      (equal
       (second
	(make-shared-variables
	 h
	 (second (term-as-dag-aux-l-ns nil term2
				     (first (term-as-dag-aux-l-ns t term1 g h))
				     (second (term-as-dag-aux-l-ns t term1 g h))))
	 (first (term-as-dag-aux-l-ns nil term2
				    (first (term-as-dag-aux-l-ns t term1 g h))
				    (second (term-as-dag-aux-l-ns t term1 g h))))
	 variables))
       (fourth
	(term-as-dag-aux-l nil term2
			   (first (term-as-dag-aux-l t term1 g h variables))
			   (second (term-as-dag-aux-l-ns t term1 g h))
			   (fourth (term-as-dag-aux-l t term1 g h
						   variables))))))
     :hints (("Goal" :use
	      (:instance
	       make-shared-composition
	       (h1 h)
	       (h2 (second (term-as-dag-aux-l-ns t term1 g h)))
	       (h3 (second (term-as-dag-aux-l-ns
			  nil term2
			  (first (term-as-dag-aux-l-ns t term1 g h))
			  (second (term-as-dag-aux-l-ns t term1 g h)))))
	       (g (first (term-as-dag-aux-l-ns
			nil term2
			(first (term-as-dag-aux-l-ns t term1 g h))
			(second (term-as-dag-aux-l-ns t term1 g h))))))))))


;; The following deal with induction case 2 of the induction proof of
;; our main goal:


  (local
   (defthm update-nth-update-nth-at-the-same-index
     (equal (update-nth h y (update-nth h x g))
	    (update-nth h y g))))


  (local
   (defthm make-shared-variables-non-variable-term
     (implies
      (and (natp h) (natp h2) (<= h h2)
	   (nat-true-listp l))
      (equal
       (make-shared-variables
	h h2 (update-nth h (cons f l) g) variables)
       (make-shared-variables
	(1+ h) h2 (update-nth h (cons f l) g) variables)))))


  (local
   (defthm make-shared-variables-update-nth-disjoint-variables
     (implies
      (and (natp h) (natp h1) (natp h2) (< h h1) (<= h1 h2))
      (equal
       (cadr (make-shared-variables
	      h1 h2 (update-nth h x g) variables))
       (cadr (make-shared-variables h1 h2 g variables))))
     :hints (("Goal" :induct (make-shared-variables h1 h2 g variables))
	     ("Subgoal *1/4.2"
	      :expand (make-shared-variables h1 h2 (update-nth h x g) variables))
	     ("Subgoal *1/3.2"
	      :expand (make-shared-variables h1 h2 (update-nth h x g) variables))
	     ("Subgoal *1/2.2"
	      :expand (make-shared-variables h1 h2 (update-nth h x g) variables)))))


;; The following deals with base case 1 of the induction proof of our
;; main goal. Note that the lemma is needed because in some cases
;; the definition of {\tt make-shared-variables} is not expanded.

  (local
   (defthm make-shared-variables-one-position-one-variable
     (implies (and (variable-p term)
		   (natp h))
	      (equal (make-shared-variables
		      h (1+ h) (update-nth h (cons term t) g) variables)
		     (if (assoc term variables)
			 (list
			  (update-nth h (cdr (assoc term variables)) g)
			  variables)
		       (list (update-nth h (cons term t) g)
			     (cons (cons term h) variables)))))))



;; And finally, the intended result, proved by induction on the
;; structure of @term@

  (defthm term-as-dag-aux-l-in-two-steps
    (let* ((res1 (term-as-dag-aux-l flg term g h variables))
	   (g-ret1 (first res1))
	   (var-ret1 (fourth res1))
	   (res2  (term-as-dag-aux-l-ns flg term g h))
	   (g-ret2 (first res2))
	   (h-ret2 (second res2)))
      (implies (and (natp h)
		    (term-p-aux flg term))
	       (and (equal g-ret1
			   (first (make-shared-variables h h-ret2 g-ret2 variables)))
		    (equal var-ret1
			   (second (make-shared-variables h h-ret2 g-ret2
							  variables)))))))))


;;; ============================================================================
;;;
;;; 6) Sharing variables preserves {\tt dag-p}
;;;
;;; ============================================================================

;;; Our intention in this section is to prove a theorem that will allow
;;; us to conclude that after applying {\tt make-shared-variables} to
;;; the graph obtained by {\tt term-as-dag-aux-l-ns} we obtain a graph
;;; thta verifies the {\tt init-term-dag-p} property and, consequently,
;;; the {\tt dag-p} property. Since we have proved in section 3 that the
;;; graph obtained by {\tt term-as-dag-aux-l-ns} verifies {\tt tree-p}
;;; then the following theorem will be our final goal in this section:


;  (defthm init-term-dag-p-make-shared-variables
;    (implies (and (natp h1) (natp h2) (<= h1 h2) (<= h2 (len g))
;		  (tree-p g) (variables-well-stored-p h1 g variables))
;	     (init-term-dag-p
;	      (first (make-shared-variables h1 h2 g variables)))))

;;; This theorem will be a particular case of a more general theorem
;;; that can be proved by induction. The function {\tt
;;; variables-well-stored-p} defines some kind of "invariant" during the
;;; proccess of making variables shared; it defines how the association
;;; list computed by {\tt make-shared-variables} is related to the "piece"
;;; of graph already processed.

;;; -----------------------------------
;;;
;;; 6.1) Definition of the invariant
;;;
;;; -----------------------------------


;;; As we have said above, the following definition formalizes some kind
;;; of "invariant" during the proccess of making variables shared; it
;;; defines how the association list computed by {\tt
;;; make-shared-variables} is related to the "piece" of graph already
;;; processed. Intuitively, it establishes that the association list
;;; @variables@ binds the variable nodes of the graph @g@ to the index
;;; of the nodes of the graph where they appear, for all the nodes with
;;; index less than @h@. That is, if we have made variables shared in
;;; @g@ until index @h-1@ then {\tt ((variables-well-stored-p h g
;;; variables)} where @vraibles@ is the association list computed {\tt
;;; make-shared-variables} up to that moment.

(local
 (defun variables-well-stored-p (h g variables)
   (cond ((zp h) (equal variables nil))
	 ((term-dag-variable-p (1- h) g)
	  (and (equal (term-dag-symbol (1- h) g) (caar variables))
	       (equal (cdar variables) (1- h))
	       (variables-well-stored-p (1- h) g (cdr variables))))
	 (t (variables-well-stored-p (1- h) g variables)))))


;;; Now we prove some properties about @variables-well-stored-p@. For
;;; example, if {\tt (variables-well-stored-p h1 g variables)}, then in
;;; the association list @variables@ the variables are bound to
;;; natural numbers strictly less than the index @h1@, and those numbers
;;; point to variable nodes in the graph.

(local
 (defthm variables-well-stored-p-integerp
   (implies (and (variables-well-stored-p h1 g variables)
		 (assoc x variables))
	    (integerp (cdr (assoc x variables))))
   :rule-classes :type-prescription))

(local
 (defthm variables-well-stored-p-positive
   (implies (and (variables-well-stored-p h1 g variables)
		 (assoc x variables))
	    (<= 0 (cdr (assoc x variables))))
   :rule-classes :linear))



(local
 (defthm variables-well-stored-p-index-less-than
   (implies (and (variables-well-stored-p h1 g variables)
		 (natp h1)
		 (assoc x variables))
	    (< (cdr (assoc x variables)) h1))
   :rule-classes :linear))



(local
 (defthm variables-well-stored-p-index-stores-variables
   (implies (and (variables-well-stored-p h1 g variables)
		 (natp h1)
		 (assoc x variables))
	    (term-dag-variable-p (cdr (assoc x variables)) g))))


;;; These two lemmas establish how updating affects to {\tt
;;; variables-well-stored-p}:

(local
 (defthm variales-well-stored-p-update-nth-above-the-index
   (implies (and (natp h1) (natp h2) (<= h1 h2))
	    (equal (variables-well-stored-p h1 (update-nth h2 x g)
					    variables)
		   (variables-well-stored-p h1 g variables)))
   :hints (("Goal" :induct (variables-well-stored-p h1 g variables))
	   ("Subgoal *1/5"
	    :expand
	    (variables-well-stored-p h1 (update-nth h2 x g) variables)))))


(local
 (defthm variables-well-stored-p-update-nth-at-the-index
   (implies (and (assoc (car (nth h1 g)) variables)
		 (natp h1)
		 (variables-well-stored-p h1 g variables))
	    (variables-well-stored-p
	     (+ 1 h1)
	     (update-nth h1
			 (cdr (assoc (car (nth h1 g)) variables))
			 g)
	     variables))
   :hints (("Goal" :expand (variables-well-stored-p
			    (+ 1 h1)
			    (update-nth h1
					(cdr (assoc (car (nth h1 g)) variables))
					g)
			    variables)))))


;;; -----------------------------------
;;;
;;; 6.2) Proving the invariant and the intended property
;;;
;;; -----------------------------------


;;; The following sequence of events helps to prove the lemma {\tt
;;; init-term-dag-p-make-shared-variables-main-lemma}, which describes
;;; the main invariant related to the {\tt init-term-dag-p} property
;;; during the proccess. The goal theorem in this section will be a
;;; particular case of that lemma.

(local
 (encapsulate
  ()

  (local
   (defthm init-term-dag-p-aux-append
     (equal (init-term-dag-p-aux (append hs1 hs2) g)
	    (and (init-term-dag-p-aux hs1 g)
		 (init-term-dag-p-aux hs2 g)))))



;; Needed for {\tt Subgoal *1/4.3}



  (local
   (defthm property-element-tree-p-property-element-init-term-dag-p
     (implies (property-element-tree-p h node)
	      (property-element-init-term-dag-p h node g))))


  (local
   (in-theory (disable property-element-tree-p
		       property-element-init-term-dag-p)))


  (local
   (defthm init-term-dag-p-aux-property-element-tree-p-lemma
     (implies (and (natp h1) (natp h0) (<= h0 h1)
		   (init-term-dag-p-aux (list-from-to h0 h1) g)
		   (property-element-tree-p h1 (nth h1 g)))
	      (init-term-dag-p-aux (append (list-from-to h0 h1)
					   (list-from-to h1 (1+ h1)))
				   g))))

  (local
   (defthm init-term-dag-p-aux-property-element-tree-p
     (implies (and (natp h1) (natp h0) (<= h0 h1)
		   (init-term-dag-p-aux (list-from-to h0 h1) g)
		   (property-element-tree-p h1 (nth h1 g)))
	      (init-term-dag-p-aux (list-from-to h0 (1+ h1))
				  g))
     :hints (("Goal" :use init-term-dag-p-aux-property-element-tree-p-lemma
	      :in-theory (disable list-from-to)))))


  (local
   (defthm tree-p-aux-implies-property-element-tree-p
     (implies (and (natp h1) (natp h2) (< h1 h2)
		   (tree-p-aux (list-from-to h1 h2) g))
	      (property-element-tree-p h1 (nth h1 g)))))

  (local
   (defthm init-term-dag-p-aux-one-index-beyond
     (implies (and (tree-p-aux (list-from-to h1 h2) g)
		   (natp h1) (natp h2) (< h1 h2) (natp h0) (<= h0 h1)
		   (init-term-dag-p-aux (list-from-to h0 h1) g))
	      (init-term-dag-p-aux (list-from-to h0 (1+ h1)) g))
     :hints (("Goal"
	      :use init-term-dag-p-aux-property-element-tree-p))))

;; Needed for {\tt Subgoal *1/4.2}

  (local
   (defthm variables-well-stored-p-variables-one-index-beyond
     (implies (and (not (term-dag-variable-p h1 g))
		   (variables-well-stored-p h1 g variables))
	      (variables-well-stored-p (1+ h1) g variables))))



;; Needed for {\tt Subgoal *1/4.1}

  (local
   (defthm tree-p-aux-one-index-less
     (implies (and (natp h1) (natp h2) (< h1 h2)
		   (tree-p-aux (list-from-to h1 h2) g))
	      (tree-p-aux (list-from-to (1+ h1) h2) g))))


;; Needed for {\tt Subgoal *1/3.1}

  (local
   (defthm variables-well-stored-p-one-step-beyond
     (implies (and (natp h1)
		   (term-dag-variable-p h1 g)
		   (variables-well-stored-p h1 g variables))
	      (variables-well-stored-p (1+ h1) g
				       (cons (cons (car (nth h1 g)) h1)
					     variables)))))


;; Needed for {\tt Subgoal *1/2.2'}

  (local
   (defthm variables-well-stored-p-not-variable-if-stored
     (implies (and (variables-well-stored-p h1 g variables)
		   (assoc (car (nth h1 g)) variables))
	      (not (equal (cddr (assoc (car (nth h1 g)) variables))
			  t)))
     :hints (("Goal" :use (:instance variables-well-stored-p-integerp
				     (x (car (nth h1 g))))))))


;; Needed for {\tt Subgoal *1/2.1}

  (local
   (defthm variables-well-stored-p-property-element-init-term-dag-p-update-nth
     (implies (and (variables-well-stored-p h1 g variables)
		   (natp h1)
		   (assoc (car (nth h1 g)) variables))
	      (property-element-init-term-dag-p
	       h1 (cdr (assoc (car (nth h1 g)) variables))
	       (update-nth h1
			   (cdr (assoc (car (nth h1 g)) variables))
			   g)))
     :hints (("Goal" :in-theory (enable property-element-init-term-dag-p)))))

  (local
   (defthm property-element-init-term-dag-p-update-nth
     (implies (and (natp h0) (natp h2) (< h0 h2)
		   (property-element-init-term-dag-p h0 (nth h0 g)
						     g))
	      (property-element-init-term-dag-p h0 (nth h0 g)
						(update-nth h2 x g)))
     :hints (("Goal" :in-theory (enable property-element-init-term-dag-p)))))


  (local
   (defthm init-term-dag-p-aux-list-from-to-update-nth
     (implies (and (natp h0) (natp h1) (natp h2) (<= h0 h1) (<= h1 h2)
		   (init-term-dag-p-aux (list-from-to h0 h1) g))
	      (init-term-dag-p-aux (list-from-to h0 h1) (update-nth h2 x g)))))



  (local
   (defthm init-term-dag-p-aux-update-nth-one-index-beyond-almost
     (implies (and (variables-well-stored-p h1 g variables)
		   (natp h1) (natp h0) (<= h0 h1)
		   (init-term-dag-p-aux (list-from-to h0 h1) g)
		   (equal (cdr (nth h1 g)) t)
		   (assoc (car (nth h1 g)) variables))
	      (init-term-dag-p-aux (append (list-from-to h0 h1)
					   (list-from-to h1 (1+ h1)))
				   (update-nth h1
					       (cdr (assoc (car (nth h1 g)) variables))
					       g)))
     :hints (("Goal" :in-theory (disable  append-list-from-to)))))



  (local
   (defthm init-term-dag-p-aux-update-nth-one-index-beyond
     (implies (and (variables-well-stored-p h1 g variables)
		   (natp h1) (natp h0) (<= h0 h1)
		   (init-term-dag-p-aux (list-from-to h0 h1) g)
		   (equal (cdr (nth h1 g)) t)
		   (assoc (car (nth h1 g)) variables))
	      (init-term-dag-p-aux (list-from-to h0 (+ 1 h1))
				   (update-nth h1
					       (cdr (assoc (car (nth h1 g)) variables))
					       g)))
     :hints (("Goal" :use init-term-dag-p-aux-update-nth-one-index-beyond-almost
	      :in-theory (disable list-from-to)))))


;; And finally the main lemma:

  (defthm init-term-dag-p-make-shared-variables-main-lemma
    (implies (and (natp h1) (natp h2) (natp h0)
		  (<= h0 h1) (<= h1 h2)
		  (tree-p-aux (list-from-to h1 h2) g)
		  (init-term-dag-p-aux (list-from-to h0 h1) g)
		  (variables-well-stored-p h1 g variables))
	     (init-term-dag-p-aux
	      (list-from-to h0 h2)
	      (first (make-shared-variables h1 h2 g variables))))
    :hints (("Goal" :induct (make-shared-variables h1 h2 g variables))))


;; We now prove the theorem {\tt init-term-dag-p-make-shared-variables}
;; presented above, simply noting that when {\tt tree-p} is verified by
;; a graph, then it verifies {\tt tree-p-aux} for every range of natural
;; numbers:


  (local (in-theory (disable property-element-tree-p)))

  (local
   (defthm property-element-tree-p-natp-all-indices
     (implies (and (tree-p g)
		   (local-nat-listp hs))
	      (tree-p-aux hs g))))


  (local
   (defthm nth-aux-make-shared-variables-beyond-the-limit
     (implies (and (natp h1) (natp h2) (natp h3) (<= h2 h3))
	      (equal (nth h3 (first (make-shared-variables h1 h2 g
							   variables)))
		     (nth h3 g)))))



  (local
   (defthm tree-p-aux-make-shared-variables-beyond-the-limit
     (implies (and (natp h1) (natp h2) (natp h3)
		   (<= h2 h3) (natp h4)
		   (tree-p-aux (list-from-to h3 h4) g))
	      (tree-p-aux (list-from-to h3 h4)
			  (first (make-shared-variables h1 h2 g variables))))))


  (local
   (defthm init-term-dag-p-make-shared-variables-bridge-lemma
     (implies (and (natp h1) (natp h2) (<= h1 h2)
		   (tree-p g) (variables-well-stored-p h1 g variables))
	      (init-term-dag-p-aux
	       (append (list-from-to 0 h2)
		       (list-from-to h2
				     (len
				      (first
				       (make-shared-variables h1 h2 g variables)))))
	       (first
		(make-shared-variables h1 h2 g variables))))
     :rule-classes nil))


  (local
   (defthm len-update-nth-corollary
     (implies
      (and (natp h1) (natp h2) (< h1 h2)
	   (< h1 h2) (<= h2 (len g)))
      (not (< (len (update-nth h1 x g)) h2)))))


  (local
   (defthm len-make-shared-variables-auxiliar-property
     (implies (and (natp h1) (natp h2) (<= h1 h2) (<= h2 (len g)))
	      (<= h2 (len (first (make-shared-variables h1 h2 g
							variables)))))
     :rule-classes :linear))


;; Finally, the intended property:

  (defthm init-term-dag-p-make-shared-variables
    (implies (and (natp h1) (natp h2) (<= h1 h2) (<= h2 (len g))
		  (tree-p g) (variables-well-stored-p h1 g variables))
	     (init-term-dag-p
	      (first (make-shared-variables h1 h2 g variables))))
    :hints (("Goal" :use
	     init-term-dag-p-make-shared-variables-bridge-lemma
	     :in-theory (enable tree-p init-term-dag-p))))))

;;; Note that when @h1@ is @0@, then the condition {\tt
;;; (variables-well-stored-p 0 g nil)} trivially holds, so the above
;;; theorem trivially implies that whenever {\tt (tree-p g)} then the
;;; graph {\tt (first (make-shared-variables 0 (len g) g nil))} verifies
;;; the {\tt init-term-dag-p} property.

;;; ============================================================================
;;;
;;; 7) How making shared variables affects to the graph
;;;
;;; ============================================================================


;;; In this section we prove that the variables of the graph obtained
;;; after making the variables of a graph to be shared are
;;; not duplicated.  That is, our goal in this sectionis:

;  (defthm no-duplicatesp-make-shared-variables-particular-case
;    (no-duplicatesp
;     (list-of-term-dag-variables
;      (first
;       (make-shared-variables 0 (len g) g nil))))

;;; And also we show that making variables to be shared preserves the
;;; stored first--order terms. That is, the following theorem:

;  (defthm dag-as-term-l-make-shared-variables
;    (implies (and
;	      (natp h1) (natp h2) (<= h1 h2)
;	      (dag-p g)
;	      (variables-well-stored-p h1 g variables)
;	      (dag-p (first (make-shared-variables h1 h2 g variables))))
;	     (equal (dag-as-term-l
;		     flg h (first (make-shared-variables h1 h2 g
;							 variables)))
; 		    (dag-as-term-l flg h g))))


;;; The main lemma needed in this section is the lemma {\tt
;;; make-shared-variables-variables-main-lemma} below establishing the
;;; relation between the variables bound by the association list
;;; computed by {\tt make-shared-variables}, and the variables appearing
;;; in the graph computed by {\tt make-shared-variables}:

(local
 (encapsulate
  ()

  (defun nths-from-to (l h1 h2)
    (declare (xargs :measure (nfix (- h2 h1))))
    (if (zp (- h2 h1))
	nil
      (cons (nth h1 l)
	    (nths-from-to l (1+ h1) h2))))


  (defun my-subseq-list (l h1 h2)
    (declare (xargs :measure (list* (cons 1 (1+ (nfix (- h2 h1)))) (nfix h1))))
    (cond ((zp (- h2 h1)) nil)
	  ((zp h1) (cons (car l) (my-subseq-list (cdr l) h1 (- h2 1))))
	  (t (my-subseq-list (cdr l) (- h1 1) (- h2 1)))))


  (local
   (defthm update-nth-nths-from-to
     (implies (and (natp h) (natp h1) (natp h2) (< h h1))
	      (equal (nths-from-to (update-nth h x g) h1 h2)
		     (nths-from-to g h1 h2)))))

  (defun alist-to-acl2-numberp (a)
    (if (endp a)
	t
      (and (acl2-numberp (cdar a))
	   (alist-to-acl2-numberp (cdr a)))))

  (local
   (defthm alist-to-indices-does-not-provide-dag-variables
     (implies (and (alist-to-acl2-numberp a)
		   (assoc x a))
	      (acl2-numberp (cdr (assoc x a))))
     :rule-classes :type-prescription))

  (defthm make-shared-variables-variables-main-lemma
    (implies (and (natp h1)
		  (natp h2)
		  (alist-to-acl2-numberp variables))
	     (equal
	      (strip-cars
	       (second (make-shared-variables h1 h2 g variables)))
	      (revappend
	       (list-of-term-dag-variables
		(nths-from-to
		 (first (make-shared-variables h1 h2 g variables))
		 h1 h2))
	       (strip-cars variables))))
    :hints (("Goal" :induct (make-shared-variables h1 h2 g variables))))))


;;; As a particular case of this main lemma, the lemma {\tt
;;; make-shared-variables-variables} below establish the relation
;;; between the variables bound by the association list and the
;;; variables of the graph after making variables to be shared in {\em all}
;;; the graph.

(local
 (encapsulate
  ()
  (local
   (defthm equal-nths-from-to-my-subseq-list-lemma
     (implies (and (natp h1) (natp h2) (< h1 h2))
	      (equal (my-subseq-list l h1 h2)
		     (cons (nth h1 l)
			   (my-subseq-list l (+ 1 h1) h2))))))

  (local
   (defthm equal-nths-from-to-my-subseq-list
     (implies (and (natp h1) (natp h2))
	      (equal  (nths-from-to l h1 h2)
		      (my-subseq-list l h1 h2)))))

  (local
   (defthm my-subseq-list-particular-case
     (equal (my-subseq-list l 0 (len l))
	    (fix-true-list l))
     :rule-classes nil))

  (local
   (defthm nths-from-to-particular-case
     (equal (nths-from-to l 0 (len l)) (fix-true-list l))))

  (local
   (defthm list-of-term-dag-variables-fix-true-list-l
     (equal (list-of-term-dag-variables (fix-true-list l))
	    (list-of-term-dag-variables l))))

  (local
   (defthm revappend-rev-list
     (equal (revappend l ac)
	    (append (revlist l) ac))))

  (local
   (defthm len-update-nth-corollary-2
     (implies (and (natp h) (< h (len g)))
	      (equal (len (update-nth h x g)) (len g)))))

  (local
   (defthm len-make-shared-variables-arithmetic-lemma
     (implies (and (not (zp (- h2 h1)))
		   (natp h1) (natp h2)
		   (<= h1 h2)
		   (<= h2 x))
	      (< h1 x))
     :rule-classes nil))

  (local
   (defthm len-make-shared-variables
     (implies (and (natp h1) (natp h2) (<= h1 h2) (<= h2 (len g)))
	      (equal
	       (len (first (make-shared-variables h1 h2 g variables)))
	       (len g)))
     :hints (("Subgoal *1/4"
	      :use (:instance len-make-shared-variables-arithmetic-lemma
			      (x (len g)))))
     :rule-classes nil))

  (local
   (defthm make-shared-variables-variables
     (equal
      (revlist
       (list-of-term-dag-variables
	(first
	 (make-shared-variables 0 (len g) g nil))))
      (strip-cars
       (second (make-shared-variables 0 (len g) g nil))))

     :hints (("Goal" :use
	      ((:instance len-make-shared-variables
			  (h1 0) (h2 (len g)) (variables nil))
	       (:instance my-subseq-list-particular-case
			  (l (car (make-shared-variables 0 (len g) g nil)))))))))

;; Finally it is to show that the variables bound by the association
;; list are not duplicated, and consequently (using the previous lemmas)
;; we prove (theorem {\tt
;; no-duplicatesp-make-shared-variables-particular-case}) that the list
;; of variables of the graph after making variables to be shared are not
;; duplicated (that is, the main goal in this section):

  (local
   (defthm member-strip-cars-assoc
     (implies (alistp a)
	      (iff (member x (strip-cars a))
		   (assoc x a)))))

  (local
   (defthm no-duplicatesp-make-shared-variables
     (implies (and (alistp variables) (no-duplicatesp (strip-cars variables)))
	      (no-duplicatesp
	       (strip-cars
		(second
		 (make-shared-variables h1 h2 g variables)))))))

  (local
   (defthm no-duplicatesp-make-shared-variables-particular-case-lemma
     (no-duplicatesp
      (revlist
       (list-of-term-dag-variables
	(first
	 (make-shared-variables 0 (len g) g nil)))))
     :hints (("Goal"
	      :in-theory
	      (disable equal-nths-from-to-my-subseq-list
		       no-duplicatesp-revlist
		       make-shared-variables-variables-main-lemma)))))


  (defthm no-duplicatesp-make-shared-variables-particular-case
    (no-duplicatesp
     (list-of-term-dag-variables
      (first
       (make-shared-variables 0 (len g) g nil))))
    :hints (("Goal"
	     :use
	     no-duplicatesp-make-shared-variables-particular-case-lemma
	     :in-theory (disable
			 no-duplicatesp-make-shared-variables-particular-case-lemma
			 make-shared-variables-variables))))




;; {\bf We now prove the last piece of our strategy}, before assembling the
;; pieces to prove our main goals in this book.  We show now that the
;; terms computed by the function {\tt dag-as-term-l} are preserved
;; after making variables to be shared.

  (local
   (defthm variables-are-not-is
     (implies (term-dag-variable-p h g)
	      (not (term-dag-is-p h g)))))

  (local
   (defthm variables-well-stored-p-assoc
     (implies (and (assoc x variables)
		   (variables-well-stored-p h g variables))
	      (equal (car (nth (cdr (assoc x variables)) g))
		     x))))

  (local
   (defthm car-update-nth-in-strictly-positives-indices
     (implies (and (integerp h) (< 0 h))
	      (equal (car (update-nth h x g)) (car g)))))


;; Attention! The proof of the following lemma is very long, but I
;; prefer not to impose condition on @h@, since in that case I have to
;; impose conditions on @g@ and @gms@.


  (local
   (defthm make-shared-variables-dag-as-term-l-lemma
     (let* ((c1 (nth h g))
	    (gms (first (make-shared-variables h1 h2 g variables)))
	    (c2 (nth h gms)))
       (implies (and
		 (natp h1) (natp h2) (<= h1 h2)
		 (not (equal c1 c2))
		 (variables-well-stored-p h1 g variables)
		 (term-dag-variable-p h g))
		(and (integerp c2)
		     (term-dag-variable-p c2 gms)
		     (equal (term-dag-symbol c2 gms)
			    (term-dag-symbol h g)))))
     :hints (("Goal" :induct (make-shared-variables h1 h2 g variables)))))



  (local
   (defthm make-shared-variables-dag-as-term-l
     (implies (and
	       (natp h1) (natp h2) (<= h1 h2)
	       (dag-p (first (make-shared-variables h1 h2 g variables)))
	       flg
	       (variables-well-stored-p h1 g variables)
	       (term-dag-variable-p h g))
	      (equal (dag-as-term-l flg h
				    (first (make-shared-variables h1 h2 g
								  variables)))
		     (car (nth h g))))
     :hints
     (("Goal"
       :cases
       ((equal (nth h g)
	       (nth h (first (make-shared-variables h1 h2 g variables)))))))))


;; And finally one of the intended theorems in this section, showing that {\tt
;; make-shared-variables} preserves the value of {\tt dag-as-term-l}.

  (defthm dag-as-term-l-make-shared-variables
    (implies (and
	      (natp h1) (natp h2) (<= h1 h2)
	      (dag-p g)
	      (variables-well-stored-p h1 g variables)
	      (dag-p (first (make-shared-variables h1 h2 g variables))))
	     (equal (dag-as-term-l
		     flg h (first (make-shared-variables h1 h2 g
							 variables)))
		    (dag-as-term-l flg h g)))
    :hints (("Goal" :induct (dag-as-term-l flg h g))))))


;;; ============================================================================
;;;
;;; 9) Assembling all the pieces
;;;
;;; ============================================================================

;;; Recall that our goal is to prove the main properties of the graph
;;; computed by {\tt term-as-dag-l}, those properties that allows us to
;;; apply our unification algorithm acting on term dags to that
;;; graph. That is, under certain initial conditions, the following
;;; properties hold:

;;; *)
;;;   The graph is a well--formed directed acyclic graph
;;; *)
;;;   The graph has no duplicates variables
;;; *)
;;;   The term stored in the graph (at index 0) is the input term
;;; -)

;;; This is the initial conditions we will assume about the graph {\em
;;; before} storing the graph. We simply assume that all the nodes are empty:

(defun empty-graph-p-aux (hs g)
  (declare (xargs :guard (and (nat-true-listp hs) (true-listp g))))
  (if (endp hs)
      t
    (and (equal (nth (car hs) g) nil)
	 (empty-graph-p-aux (cdr hs) g))))

;;; For guard verification
(defthm nat-true-list-p-list-from-to
  (implies (and (natp x) (natp y))
	   (nat-true-listp (list-from-to x y))))

(defun empty-graph-p (g)
  (declare (xargs :guard (true-listp g)))
  (empty-graph-p-aux (list-from-to 0 (len g)) g))


;;; This condition trivially implies {\tt tree-p}:

(local
 (encapsulate
  ()
  (local
   (defthm empty-graph-p-aux-tree-p-aux
     (implies (empty-graph-p-aux hs g)
	      (tree-p-aux hs g))
     :hints (("Goal" :in-theory (enable property-element-tree-p)))))

  (defthm empty-graph-p-tree-p
    (implies (empty-graph-p g)
	     (tree-p g))
    :hints (("Goal" :in-theory (enable tree-p))))))


;;; The graph obtained by {\tt term-as-dag-l} verifies {\tt
;;; init-term-dag-p}:

(local
 (defthm term-as-dag-l-init-term-dag-p
   (implies (and (tree-p g) (term-p term))
	    (init-term-dag-p (term-as-dag-l term g)))))




;;; The graph obtained by {\tt term-as-dag-l} is a directed acyclic graph

(defthm term-as-dag-l-dag-p
  (implies (and (empty-graph-p g) (term-p term))
	   (dag-p (term-as-dag-l term g))))


;;; The graph obtained by {\tt term-as-dag-l} stores the input term (at
;;; index 0):


(defthm term-as-dag-l-stores-term
  (implies (and (empty-graph-p g) (term-p term))
	   (equal (dag-as-term-l t 0 (term-as-dag-l term g))
		  term)))


;;; And the following sequence of events shows that the list of
;;; variables of the graph obtained by {\tt term-as-dag-l} has no
;;; duplicates (theorem {\tt term-as-dag-l-no-duplicatesp-variables}
;;; below):

(encapsulate
 ()

 (local
  (defthm empty-graph-p-aux-main-property
    (implies (and (empty-graph-p-aux hs g)
		  (member h hs))
	     (equal (nth h g) nil))))

 (local
  (defthm nth-non-nil
    (implies (and (<= (len l) n) (natp n))
	     (not (nth n l)))))


 (local
  (defthm empty-graph-p-main-property
    (implies (and (empty-graph-p g) (natp h))
	     (equal (nth h g) nil))
    :hints (("Goal" :cases ((member h (list-from-to 0 (len g))))))))


 (local
  (defthm make-shared-variables-empty-graph-p-aux-1
    (implies (and (natp h2) (natp h3)
		  (<= h2 h3)
		  (empty-graph-p-aux (list-from-to h2 h3) g))
	     (equal (first (make-shared-variables h2 h3 g variables))
		    g))))

 (local
  (defthm make-shared-variables-empty-graph-p-aux-2
    (implies (and (natp h1) (natp h2) (natp h3) (natp h4)
		  (<= h1 h2) (<= h2 h3) (<= h3 h4)
		  (empty-graph-p-aux (list-from-to h3 h4) g))
	     (empty-graph-p-aux (list-from-to h3 h4)
				(first (make-shared-variables h1 h2 g variables))))))

 (local
  (defthm make-shared-variables-empty-graph-p-aux-final-segment
    (implies (and (natp h1) (natp h2) (natp h3)
		  (<= h1 h2) (<= h2 h3)
		  (empty-graph-p-aux (list-from-to h2 h3) g))
	     (equal (first (make-shared-variables h1 h3 g variables))
		    (first (make-shared-variables h1 h2 g variables))))
    :hints (("Goal" :use make-shared-composition))))

 (local
  (defthm empty-graph-p-covers-all-cases
    (implies (and (natp h1) (natp h2)
		  (empty-graph-p g))
	     (empty-graph-p-aux (list-from-to h1 h2) g))))

 (local
  (defthm empty-graph-p-covers-all-cases-version-2
    (implies (and (local-nat-listp hs)
		  (empty-graph-p g))
	     (empty-graph-p-aux hs g))))


 (local
  (defthm empty-graph-p-aux-term-as-dag-l-final-segment
    (implies (and (empty-graph-p-aux hs g)
		  (natp h)
		  (subsetp hs
			   (list-from-to
			    (cadr (term-as-dag-aux-l-ns flg term g h))
			    (len (car (term-as-dag-aux-l-ns flg term g h))))))
	     (empty-graph-p-aux
	      hs (car (term-as-dag-aux-l-ns flg term g h))))
    :hints (("Goal" :induct (empty-graph-p-aux hs g)))))



 (defthm term-as-dag-l-no-duplicatesp-variables
   (implies (and (empty-graph-p g) (term-p term))
	    (no-duplicatesp
	     (list-of-term-dag-variables (term-as-dag-l term g))))
   :hints (("Goal" :use
	    (:instance make-shared-variables-empty-graph-p-aux-final-segment
		       (h1 0) (h2 (second (term-as-dag-aux-l-ns t term g 0)))
		       (h3 (len (first (term-as-dag-aux-l-ns t term g 0))))
		       (g (first (term-as-dag-aux-l-ns t term g 0)))
		       (variables nil))
	    :in-theory (disable
			make-shared-variables-empty-graph-p-aux-final-segment)))))


(local (in-theory (disable tree-p)))

(local (in-theory (disable term-as-dag-aux-l-in-two-steps)))


;;; ============================================================================
;;;
;;; 10) The {\tt term-graph-p} property of {\tt term-as-dag-aux-l}
;;;
;;; ============================================================================


;;; We have left (intentionally) one property about {\tt
;;; term-as-dag-l}. We show in this section that the graph obtained by
;;; {\tt term-as-dag-l } acting on an empty graph is a well formed term
;;; graph.

;;; Unlike, the above properties, we will reason directly with the
;;; definition of {\tt term-as-dag-aux-l}. First, we prove that the
;;; property {\tt term-graph-p} is preserved by {\tt
;;; term-as-dag-aux-l}. And second, we show that {\tt empty-graph-p}
;;; implies {\tt term-graph-p}.

;;; -----------------------------------
;;;
;;; 10.1) The property {\tt term-graph-p} is preserved by {\tt term-as-dag-aux-l}
;;;
;;; -----------------------------------


;;; The following propeerties show how some updates to graph does not
;;; change the {\tt bounded-term-graph-p} property.

(local
 (defthm bounded-term-graph-p-update-nth-args
   (implies (and
	     (bounded-term-graph-p g n)
	     (eqlablep x)
	     (bounded-nat-true-listp l n))
	    (bounded-term-graph-p (update-nth h (cons x l) g) n))))


(local
 (defthm bounded-term-graph-p-update-nth-variable
   (implies (and
	     (bounded-term-graph-p g n)
	     (eqlablep x))
	    (bounded-term-graph-p (update-nth h (cons x t) g) n))))


(local
 (defthm bounded-nat-substitution-p-assoc
   (implies (and
	     (bounded-nat-substitution-p  a n)
	     (assoc x a))
	    (and (integerp (cdr (assoc x a)))
		 (< (cdr (assoc x a)) n)
		 (<= 0 (cdr (assoc x a)))))))



(local
 (defthm bounded-term-graph-p-update-nth-integer
   (implies (and
	     (bounded-term-graph-p g n)
	     (natp x) (< x n))
	    (bounded-term-graph-p (update-nth h x g) n))))


;;; The main result of this subsection. Note that we prove an
;;; invariant of {\tt term-as-dag-aux-l}:


(defthm term-as-dag-aux-l-term-graph-p
  (let* ((res (term-as-dag-aux-l flg term g h variables))
	 (g-res (first res))
	 (h-res (second res))
	 (hs-res (third res))
	 (var-res (fourth res)))
    (implies (and (term-graph-p g)
		  (natp h)
		  (<= (+ h (length-term flg term)) (len g))
		  (bounded-nat-substitution-p variables (len g))
		  (term-p-aux flg term))
	     (and (term-graph-p g-res)
		  (equal h-res (+ h (length-term flg term)))
		  (equal (len g-res) (len g))
		  (bounded-nat-true-listp hs-res (len g))
		  (bounded-nat-substitution-p var-res (len g))))))

;;; And the intended result of this subsection, a particular case of the
;;; above theorem:

(defthm term-as-dag-l-term-graph-p
  (implies (and (term-graph-p g)
		(term-p term)
		(<= (length-term t term) (len g)))
	   (term-graph-p (term-as-dag-l term g))))


(in-theory (disable term-as-dag-l))


;;; -----------------------------------
;;;
;;; 10.2) The property {\tt empty-graph-p} implies {\tt term-graph-p}
;;;
;;; -----------------------------------


;;; Now we prove that {\tt empty-graph-p} implies {\tt term-graph-p}
;;; (see the definition of {\tt term\--graph\--p} in the book {\tt
;;; dag-unification-rules}. Note the different styles in both
;;; definitions. The second one is defined recursively in the graph, and
;;; the first one is defined in terms of its indices. The implication
;;; can be proved by a general procedure. So we stablish the general
;;; reasoning and then obtain by functional instantiation the required
;;; property in this case.


;;; We assume the existence of a property @p-wit@ verified by all the
;;; elements (obtained with @nth@) of a list @l-wit@:

(local
 (encapsulate
  (((p-wit *) => *)
   ((l-wit) => *))

  (local (defun p-wit (x) (list x)))
  (local (defun l-wit () nil))

  (defthm p-wit-l-wit-property
    (implies (and (natp h) (< h (len (l-wit))))
	     (p-wit (nth h (l-wit)))))))


;;; We can show that, in this case, all the elements of the list (l-wit)
;;; verify p-wit, an this can be expresed in two ways (recursively on
;;; lists, and quantifying over its indices).

;;; The following function checks the property @p-wit@ recursively:

(local
 (defun p-wit-list (l)
   (if (endp l)
       t
     (and (p-wit (car l)) (p-wit-list (cdr l))))))

;;; The following sequence of events show that the the list @l-wit@
;;; verifies @p-wit-list@:

(local
 (encapsulate
  ()
  (local
   (defun not-p-wit-aux (h l)
     (cond ((endp l) nil)
	   ((p-wit (car l)) (not-p-wit-aux (1+ h) (cdr l)))
	   (t h))))

  (local
   (defthm not-p-wit-aux-bigger-than-initial-index
     (implies (and (natp h) (not (p-wit-list l)))
	     (<= h (not-p-wit-aux h l)))
     :rule-classes :linear))

  (local
   (defthm not-p-wit-aux-at-most-total-length
     (implies (and (natp h) (not (p-wit-list l)))
	      (< (not-p-wit-aux H L) (+ H (LEN L))))
     :rule-classes :linear))

  (local
  (defthm not-p-wit-aux-main-lemma
    (implies (natp h)
	     (iff (p-wit-list l)
		  (implies (and (natp (not-p-wit-aux h l))
				(< (not-p-wit-aux h l)
				   (+ h (len l))))
			   (p-wit (nth (- (not-p-wit-aux h l) h) l)))))))


 (local
  (defthm not-p-wit-aux-main-lemma-corollary
    (iff (p-wit-list l)
	 (implies (and (natp (not-p-wit-aux 0 l)) (< (not-p-wit-aux 0 l)
						     (len l)))
		  (p-wit (nth (not-p-wit-aux 0 l) l))))
    :hints (("Goal" :use
	     (:instance not-p-wit-aux-main-lemma (h 0))))))


 (defthm p-wit-list-l-wit (p-wit-list (l-wit)))))

;;; We define now the recursive version of {\tt empty-graph-p}, called
;;; {\tt empty-graph-p-rec}, which obviously implies  {\tt
;;; bounded-term-graph-p}.


(defun empty-graph-p-rec (g)
  (if (endp g)
      t
    (and (not (car g))
	 (empty-graph-p-rec (cdr g)))))

(defthm empty-graph-p-rec-bounded-term-graph-p
  (implies (empty-graph-p-rec g)
	   (bounded-term-graph-p g n)))



;;; As we said above, we can use the above general result by functional
;;; instantiation, to show that {\tt empty-graph-p} implies
;;; @empty-graph-p-rec@. We simply has to previously show that the property
;;; of the nodes of a graph that verifies {\tt empty\--graph\--p}
;;; implies the property that verify the nodes of a graph that verifies
;;; {\tt empty\--graph\--p\--rec}:




(encapsulate
 ()

 (local
  (defun property-element-empty-graph-p-rec (x)
    (not x)))

 (local
  (defthm init-term-dag-p-aux-term-dag-p
    (implies (and (natp h) (natp h1) (natp h2)
		  (<= h1 h) (< h h2)
		  (nth h g))
	     (not (empty-graph-p-aux (list-from-to h1 h2) g)))
    :rule-classes nil))


 (defthm empty-graph-p-empty-graph-p-rec
   (implies (empty-graph-p g)
	    (empty-graph-p-rec g))
   :hints (("Goal" :use (:functional-instance
			 p-wit-list-l-wit
			 (p-wit property-element-empty-graph-p-rec)
			 (p-wit-list empty-graph-p-rec)
			 (l-wit (lambda () (if (empty-graph-p g) g
					     nil)))))
	   ("Subgoal 2" :use (:instance init-term-dag-p-aux-term-dag-p
					(h1 0) (h2 (len g)))))))



(defthm empty-graph-p-implies-bounded-term-graph-p
  (implies (empty-graph-p g)
	   (bounded-term-graph-p g n)))

;;; In particular, the intended result:

(defthm empty-graph-p-implies-term-graph-p
  (implies (empty-graph-p g)
	   (term-graph-p g))
  :rule-classes nil)

(in-theory (disable empty-graph-p))


;;; ============================================================================
;;;
;;; 11) Building unification problems
;;;
;;; ============================================================================

;;; The function {\tt terms-as-dag-l} will be our main auxiliary
;;; function for building (dag) unification problems. Recall that a
;;; unification problem (see {\tt dags.lisp}) is determined by an
;;; indices system, an indices substitution and a well--formed directed
;;; acyclic graph. Initially, the indices substitution is empty. We now
;;; describe how we build the graph and the indices system for a
;;; unification problem of the form $\{t_1=t_2\}$:

;;; This function computes the graph. Note that the list that represents
;;; the graph is previously resized after computing how many nodes are
;;; needed. Also note that the term {\tt (equ t1 t2)} is stored in the
;;; graph using {\tt term-as-dag-l}:

(defun unif-two-terms-problem-l (t1 t2 g)
  (let* ((size (+ (length-term t t1) (length-term t t2) 1))
	 (g (resize-list g size nil)))
    (term-as-dag-l (list 'equ t1 t2) g)))

;;; The initial indices system is taken from the argument list of the
;;; first node:

(defun initial-to-be-solved-l (g)
  (let ((args-0 (cdr (dagi-l 0 g))))
    (list (cons (first args-0)
		(second args-0)))))

;;; Following the results proved in the book {\tt
;;; dag-unification-rules}, our goal is to prove the following:

; If (empty-graph-p g)
; let* g-t1-t2     =  (unif-two-terms-problem-l t1 t2 g)
;      S-dag-t1-t2 = (initial-to-be-solved-l g-t1-t2)
; then:
;    (well-formed-dag-system S-dag-t1-t2 g-t1-t2)
;      and
;    (equal (tbs-as-system S-dag-t1-t2 g-t1-t2)
;           (list (cons t1 t2)))


;;; If we prove the above property, we will able to use the theorems
;;; proved in {\tt dag-unification-rules} to conclude the properties of
;;; a unification algorithm that applies the rules of transformation
;;; starting with the above initial unification problem.

;;; First, we show that resizing a list preserves the {\tt
;;; empty-graph-p} property:

(local
 (encapsulate
  ()
  (local
   (defun same-element (l x)
     (if (endp l)
	 t
      (and (equal (car l) x)
	   (same-element (cdr l) x)))))

  (local
   (defthm resize-list-empty-graph-p-main-lemma
     (implies (same-element g x)
	      (same-element (resize-list g n x) x))))

  (local
   (defthm  resize-list-empty-graph-p-main-lemma-nth-version-lemma
     (implies (same-element l nil)
	      (not (nth i l)))))

  (local
   (defthm empty-graph-p-decreasing-indices-by-one
     (implies (and (natp n) (natp m) (<= n m) (< 0 n)
		   (not (empty-graph-p-aux
			 (list-from-to (- n 1) (- m 1)) g)))
	      (not (empty-graph-p-aux (list-from-to n m) (cons x g))))))

  (local
   (defthm resize-list-empty-graph-p-main-lemma-nth-version-lemma-2
     (implies (empty-graph-p g)
	      (same-element g nil))
     :hints (("Goal" :in-theory (enable empty-graph-p)))))

  (local
   (defthm resize-list-empty-graph-p-main-lemma-nth-version
     (implies (empty-graph-p g)
	      (not (nth i (resize-list g n nil))))))


  (local
   (defthm resize-list-empty-graph-p-aux
     (implies (empty-graph-p g)
	      (empty-graph-p-aux hs (resize-list g n nil)))
     :hints (("Goal" :induct (len hs)))))

   (defthm resize-list-empty-graph-p
     (implies (empty-graph-p g)
	      (empty-graph-p  (resize-list g n nil)))
     :hints (("Goal" :in-theory (enable empty-graph-p))))))

;;; Resize list and {\tt length}

(local
 (defthm len-resize-list
   (implies (natp n)
	    (equal (len (resize-list l n x)) n))))

(local (in-theory (disable resize-list)))


;;; The following sequence of events show that the initial unification
;;; problem built by the function {\tt unif-two-terms-problem-l} and
;;; {\tt initial-to-be-solved-l} is a well--formed directed acyclic
;;; graph.


;;; Luego lo meto dentro

(defthm bounded-term-graph-p-particular-case
  (implies (and (consp g) (term-graph-p g))
	   (and (< (cadar g) (len g))
		(< (caddar g) (len g)))))

(defthm consp-term-as-dag-l
  (implies (consp g)
	   (consp (term-as-dag-l term g )))
  :hints (("Goal" :in-theory (enable term-as-dag-l))))



(defthm consp-resize-list
  (implies (and (natp n) (< 0 n))
	   (consp (resize-list l n x)))
  :hints (("Goal" :in-theory (enable resize-list))
	  ("Subgoal *1/2''" :expand (RESIZE-LIST L 1 X))))



(encapsulate

 ()

 (local
  (defthm equal-nth-car
	 (equal (nth 0 g) (car g))))

 (local
  (defthm len-args-term-as-dag-aux-l
    (equal (len (third (term-as-dag-aux-l nil args g h variables)))
	   (len args))))

 (local
  (defthm term-as-dag-aux-l-first-element-len
    (equal
     (len
      (cdr
       (car
	(first
	 (term-as-dag-aux-l t (cons symb args) g 0 nil)))))
     (len args))))

 (local
  (defthm term-as-dag-l-first-element
    (equal
     (len
      (cdr
       (car
	(term-as-dag-l (list 'equ t1 t2) g))))
     2)
    :hints (("Goal" :in-theory (enable term-as-dag-l)))))

;;; Este me hace falta despues
  (defthm term-as-dag-l-first-element-nth
    (equal
     (len
      (cdr
       (nth 0
	(term-as-dag-l (list 'equ t1 t2) g))))
     2)
    :hints (("Goal" :in-theory (enable term-as-dag-l))))

 (local
  (defthm nat-true-listp-len-two-first
    (implies (and (nat-true-listp l)
		  (equal (len l) 2))
	     (and (integerp (first l))
		  (<= 0 (first l))))))

 (local
  (defthm nat-true-listp-len-two-second
    (implies (and (nat-true-listp l)
		  (equal (len l) 2))
	     (and (integerp (second l))
		  (<= 0 (second l))))))
  (local
   (defthm term-graph-p-first-element-nat-true-listp
     (implies (and (equal (len (cdr (car g))) 2)
		   (term-graph-p g))
	      (nat-true-listp (cdr (car g))))))

  (defthm unif-two-terms-problem-l-well-formed-dag-system
    (let* ((g-t1-t2 (unif-two-terms-problem-l t1 t2 g))
	   (S-dag-t1-t2 (initial-to-be-solved-l g-t1-t2)))
      (implies (and (empty-graph-p g) (term-p t1) (term-p t2))
	       (well-formed-dag-system S-dag-t1-t2 g-t1-t2)))
    :hints (("Goal"
	     :use
	     (:instance term-as-dag-l-term-graph-p
			(g (resize-list g (+ (length-term t t1) (length-term t t2)
					     1) nil))
			(term (list 'equ t1 t2))))
	    ("Subgoal 1" :in-theory (enable
				     well-formed-upl)))))



;;; And finally, the following sequence of events show that the initial
;;; unification problem built by the function {\tt
;;; unif-two-terms-problem-l} and {\tt initial-to-be-solved-l} stores
;;; (in a dag form) the initial unification problem:


(encapsulate
 ()

 (local

  (defthm unif-two-terms-problem-l-stores-the-problem-lemma-1
    (implies (and
	      (equal (len (cdr (nth h g))) 2)
	      (equal (dag-as-term-l t h g)
		     (list 'equ t1 t2)))
	     (equal (dag-as-term-l t (first (cdr (nth h g))) g)
		    t1))
    :hints (("Subgoal 1'" :expand (dag-as-term-l nil (cdr (nth h g)) g)))))



 (local
  (defthm unif-two-terms-problem-l-stores-the-problem-lemma-2
    (implies (and
	      (equal (len (cdr (nth h g))) 2)
	      (equal (dag-as-term-l t h g)
		     (list 'equ t1 t2)))
	     (equal (dag-as-term-l t (second (cdr (nth h g))) g)
		    t2))
    :hints (("Goal'''" :expand (dag-as-term-l nil (cdr (nth h g)) g))
	    ("Goal'5'" :expand (dag-as-term-l nil (cddr (nth h g)) g)))))

 (local
  (defthm unif-two-terms-problem-l-stores-the-problem-almost
    (let* ((g-t1-t2 (unif-two-terms-problem-l t1 t2 g))
	   (S-dag-t1-t2 (initial-to-be-solved-l g-t1-t2)))
      (implies (and (empty-graph-p g) (term-p t1) (term-p t2)
		    (equal (dag-as-term-l t 0 g-t1-t2)
			   (list 'equ t1 t2)))
	       (equal (tbs-as-system S-dag-t1-t2 g-t1-t2)
		      (list (cons t1 t2)))))
    :hints (("Goal" :in-theory
	     (disable nth term-as-dag-l-stores-term)))
    :rule-classes nil))


 (defthm unif-two-terms-problem-l-stores-the-problem
   (let* ((g-t1-t2 (unif-two-terms-problem-l t1 t2 g))
	  (S-dag-t1-t2 (initial-to-be-solved-l g-t1-t2)))
     (implies (and (empty-graph-p g) (term-p t1) (term-p t2))
	      (equal (tbs-as-system S-dag-t1-t2 g-t1-t2)
		     (list (cons t1 t2)))))
   :hints (("Goal" :use unif-two-terms-problem-l-stores-the-problem-almost))))




;;; ===============================================================