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

                       The Anubis Compiler.
                        Interpreting terms. 

******************************************************************/

#include <string.h>   
#include "compil.h"


#define dummy     nil
   
int show_errors = 1;    

/* prototypes */ 
   
   
Expr symbol_interpretations         (Expr lc, 
                                     Expr ttype, 
                                     Expr name, 
                                     Expr ctxt, 
                                     Expr env);  

Expr of_type_interpretations        (Expr lc, 
                                     Expr ttype, 
                                     Expr type, 
                                     Expr term, 
                                     Expr ctxt,
                                     Expr env, 
                                     Expr tvs); 

Expr lambda_interpretations         (Expr lc, 
                                     Expr ttype, 
                                     Expr fname,   /* name of function (nil if non recursive) */ 
                                     Expr args,
                                     Expr body,
                                     Expr ctxt, 
                                     Expr env, 
                                     Expr tvs); 
   
Expr app_interpretations            (Expr lc, 
                                     Expr ttype, 
                                     Expr op, 
                                     Expr args, 
                                     Expr ctxt, 
                                     Expr env, 
                                     Expr tvs,
                                     int allow_mvar_access); 

Expr app_interpretations_1          (Expr lc, 
                                     Expr op_int_head, 
                                     Expr op_int_env, 
                                     Expr args_ints,
                                     int allow_mvar_access);

Expr with_interpretations           (Expr keyword,
                                     Expr lc,
                                     Expr ttype,
                                     Expr symbol,    
                                     Expr value, 
                                     Expr body, 
                                     Expr ctxt, 
                                     Expr env,
                                     Expr tvs); 

Expr cond_interpretations           (Expr lc, 
                                     Expr ttype, 
                                     Expr test, 
                                     Expr clauses, 
                                     Expr ctxt,
                                     Expr env, 
                                     Expr tvs); 

Expr select_cond_interpretations    (Expr lc,
                                     Expr test,
                                     Expr constructor_name,
                                     Expr typed_resurg,
                                     Expr case_body,
                                     Expr else_term,
                                     Expr ctxt,
                                     Expr env,
                                     Expr tvs); 

Expr read_interpretations           (Expr lc, 
                                     Expr ttype,
                                     Expr conn, 
                                     Expr ctxt, 
                                     Expr env, 
                                     Expr tvs); 

Expr write_interpretations          (Expr lc, 
                                     Expr ttype,
                                     Expr conn, 
                                     Expr value, 
                                     Expr ctxt, 
                                     Expr env, 
                                     Expr tvs); 

Expr exchange_interpretations       (Expr lc, 
                                     Expr ttype,
                                     Expr conn, 
                                     Expr value, 
                                     Expr ctxt, 
                                     Expr env, 
                                     Expr tvs); 

Expr wait_for_interpretations       (Expr lc,
                                     Expr ttype,
                                     Expr condition,
                                     Expr milliseconds,
                                     Expr after,
                                     Expr ctxt,
                                     Expr env,
                                     Expr tvs); 

Expr delegate_interpretations       (Expr lc,
                                     Expr ttype,
                                     Expr delegated,
                                     Expr body,
                                     Expr ctxt,
                                     Expr env,
                                     Expr tvs); 
   
Expr delegatep_interpretations      (Expr lc,
                                     Expr ttype,
                                     Expr priority, 
                                     Expr delegated,
                                     Expr body,
                                     Expr ctxt,
                                     Expr env,
                                     Expr tvs); 
   
Expr serialize_interpretations      (Expr lc,
                                     int allow_serialize_Opaque, 
                                     Expr ttype,
                                     Expr datum,
                                     Expr ctxt,
                                     Expr env,
                                     Expr tvs); 

Expr unserialize_interpretations    (Expr lc,
                                     int allow_serialize_Opaque, 
                                     Expr ttype,
                                     Expr bytes,
                                     Expr ctxt,
                                     Expr env,
                                     Expr tvs); 

Expr protect_interpretations        (Expr lc,
                                     Expr target_type, 
                                     Expr term,
                                     Expr ctxt,
                                     Expr env,
                                     Expr tvs); 

Expr lock_interpretations           (Expr lc,
                                     Expr target_type, 
                                     Expr lockedfile, 
                                     Expr term,
                                     Expr ctxt,
                                     Expr env,
                                     Expr tvs); 

Expr alt_number_interpretations     (Expr lc,
                                     Expr target_type, 
                                     Expr term,
                                     Expr ctxt,
                                     Expr env,
                                     Expr tvs); 

Expr vcopy_interpretations          (Expr lc, 
                                     Expr n,
                                     Expr init, 
                                     Expr ctxt,
                                     Expr env,
                                     Expr tvs);    
   
Expr bit_width_interpretations      (Expr lc, 
                                     Expr type, 
                                     Expr ctxt,
                                     Expr env,
                                     Expr tvs);                                        

Expr load_adm_interpretations       (Expr lc,   
                                     Expr name,  
                                     Expr ctxt,
                                     Expr env,
                                     Expr tvs);

Expr type_desc_interpretations       (Expr lc,   
                                      Expr type,  
                                      Expr ctxt,
                                      Expr env,
                                      Expr tvs); 
   
Expr definition_of_type_interpretations   (Expr   lc, 
                                           Expr   type_name,        /* term of type String */ 
                                           Expr   ctxt, 
                                           Expr   env, 
                                           Expr   tvs); 

Expr definition_of_term_interpretations   (Expr   lc, 
                                           Expr   type_term,      /* term of type AnubisType */ 
                                           Expr   term_name,      /* term of type String */ 
                                           Expr   ctxt, 
                                           Expr   env, 
                                           Expr   tvs); 


   
/*
   *** How to expand conditionals with subcases. ***
   
   Conditionals needs to be expanded into a tree of conditionals without subcases. 
   
   The first  thing to do is to  gather cases/subcases belonging to  the same alternative.
   All cases/subcases from the same alternative form a 'case group'. A case group may have
   an  arbitrary number  of elements.   Case groups  are easily  established because  in a
   defined type,  not two alternatives may  have the same  name and same arity  (number of
   components).  This  is the job  of 'separate_case_groups'.  After this  separation each
   group must correspond to an alternative of the type of the test, and in the same order.

   A case group is represented by the list:
   
                                (<subcase 1> ... <subcase k>)
   
   where '<subcase i>' has the form:
   
                                     (<head> <lc> . <body>)
   
   as produced by the parser, where '<lc>' is the position of the keyword 'then'.

   Now, consider as an example, the following  case group, where the alternative is of the
   form 'a(U,Bool,Maybe(V))':
   
     a(x,false,failure)    then A
     a(x,false,success(y)) then B
     a(x,true ,z)          then C 
   
   Remark that the third component sometimes has 'heads' ('failure' and 'success(y)'), and
   sometimes has  'symbols' ('z'), while the  first component has only  symbols ('x'), and
   the second one has only heads ('false', 'true'). When symbols are used, always the same
   symbol must be used for the same component ('x' above appears three times).

   We have to  gather consecutive elements which may  be replaced by a single  one. In the
   case of the example, the first two elements may be gathered:
   
     a(x,false,z)         then if z is 
                               {
                                 failure    then A,
                                 success(y) then B
                               },
     a(x,true ,z)         then C

   Of course, we  use 'z' as the resurgent  symbol, since it is already used  in the third
   element of the case group.
   
   At that state, the last (third) component  has no more instances in the form of 'heads'
   (we have 'z'  everywhere). So, we consider the previous  (second) component and proceed
   the same way:
   
     a(x,\A31,z)            then if \A31 is 
                               {
                                 false    then if z is 
                                               {
                                                 failure    then A,
                                                 success(y) then B
                                               },
                                 true     then C
                               }
   
   and now there  are no more subcases  anywhere. Of course, if the  component (the second
   one in our example) is always an head, we have to introduce a new symbol (here '\A31'). 

   When we find  an head, the elements to  be gathered are not necessarily as  many as the
   number of alternatives the type of the component, because these heads may themselves be
   divided into subsubcases. Here is an example of this situation:
   
     [h . []]                   then A,
     [h . [i . []]]             then B,
     [h . [i . [j . t]]]        then C
   
   In this  case, the  second component has  three successive  'heads', the last  two ones
   corresponding to the second alternative of 'List(...)'. Hence the transformation gives:
   
     [h . \A31]  then if \A31 is 
                      {
                        [ ]             then A,
                        [i . []]        then B,
                        [i . [j . t]]   then C
                      }
   
   and the conditional just created also needs to be expanded (recursively). 
   
   Below is another example where the test is of type (Bool,Bool):
   
     (false, false)      then A,
     (false, true )      then B, 
     (true , false)      then C,
     (true,  true )      then D
   
   It is expanded in 3 steps:

   
   Step 1:
     (false, \A31)    then if \A31 is 
                           {
                             false then A,
                             true  then B
                           },
     (true , false)      then C,
     (true,  true )      then D
     
   Step 2:
     (false, \A31)    then if \A31 is 
                           {
                             false then A,
                             true  then B
                           },
     (true , \A31)    then if \A31 is 
                           {
                             false then C,
                             true  then D
                           }
   
   Step 3:
     (\A30, \A31)       then if \A30 is 
                        {
                          false then if \A31 is 
                                      {
                                        false then A,
                                        true  then B
                                      },
                          true  then if \A31 is 
                                      {
                                        false then C,
                                        true  then D
                                      }
                        }

   Notice that  in the case of a  single alternative in the  type of a component,  it is a
   little more difficult to find the last element to be gathered. For example:
   
     a(false, unique)   then A
     a(true,  unique)   then B
   
   must be expanded as follows:
   
   Step 1:
     a(false, \A31) then if \A31 is unique then A,
     a(true,  \A31) then if \A31 is unique then B
   
   Step 2:
     a(\A30 , \A31) then if \A30 is 
                    {
                      false then if \A31 is unique then A,
                      true  then if \A31 is unique then B
                    }
   
   Actually,  when we  are  gathering elements  considering  the k-th  component, all  the
   instances  (symbols  or  heads) in  these  elements  for  previous components  must  be
   identical. For example the following would generate an error:
   
     (false, false)     then A, 
     (true,  false)     then B, 
     (false, true)      then C, 
     (true,  true)      then D
   
   because of the first two elements whose first components are non identical. 
   
   
   So, the method for expanding a case group is the following:
   
   
   Preliminaries:
   
     1. Establish the list of resurgent symbols for  this case group. This is a mixture of
        symbols alreay present in the case group and new symbols '\A30', '\A31', ...
   
     2. Set a  column index 'ci'  to the  number of columns  (number of components  in the
        alternative corresponding  to the case  group).  The meaning  of this index  is as
        follow: all  columns starting  at number 'ci'  contain only resurgent  symbols (no
        heads).
   
   Main loop:
   
     1. If 'ci == 0' the job is finished (all heads in the case group have been expanded).
   
     2. Otherwise,  consider the column  'C' number  'ci -  1'. In  this column,  find the
        topmost case head.
   
        2.a. If there is no such case head, decrement 'ci' and restart the main loop. 
   
        2.b. Otherwise, get the alternatives of  the type of the component, and gather the
          elements of the  group, starting at the topmost head  found, checking that heads
          correspond  (name/arity) to the  alternatives, with  maybe several  elements per
          alternative, and that the instances of  components to the left of column 'C' are
          all identical. When these elements are gathered replace them by a single element
          whose body is a new conditional. Of  course, use the resurgent symbol got in the
          preliminaries.  Don't  expand this  new  conditional  (not  necessary for  depth
          recursion which will be handled by 'cond_interpretations').
       
        2.c Restart the main loop. 
   
   
   Now, we  have to  do this for  each case group.  When it's  done, we have  our expanded
   conditional.
   
 */


   
   
   
/* 
    Testing if a symbol  is the name of a singleton in a  defined type.  For example, this
    will answer '1' for 'true' of type 'Bool' and '0' for 'x' of type 'Bool'. This is used
    to decide if a  symbol found in a head of case in a  conditional is a resurgent symbol
    or a nested case head (corresponding to a singleton). 
 */ 
int is_singleton_name(Expr   lc,           // position of the conditional case
                      Expr   the_symbol,   // a Lisp string (actually a symbol from a head of case)
                      Expr   type,         // type of the symbol
                      Expr   env)          // environment for this type
{
  Expr alts; 
   
  //printf("Entering is_singleton_name.\n"); fflush(stdout); 
   
  assert(is_string(the_symbol));
   
  soft_dereference_type(type,env); 
  if (!is_sum_type(type,env)) 
    {
      //printf("Leaving is_singleton_name (0).\n"); fflush(stdout);
      return 0; 
    }
   
  alts = get_alts(type,env,lc); 
   
  while(consp(alts))
    {
      Expr alt = car(alts);   /* (("name" ...) (<type> . <symbol>) ...) */ 
      if (cdr(alt) == nil && car(car(alt)) == the_symbol)
        {
          //printf("Leaving is_singleton_name (1).\n"); fflush(stdout);
          return 1;
        }
   
      alts = cdr(alts); 
    }
  //printf("Leaving is_singleton_name (2).\n"); fflush(stdout);    
  return 0; 
}
   
   
   

/* 
    Discriminate  between resurgent  symbols and  nested case  heads.  When  we  analyse a
    case/subcase head  we have  to decide  if an instance  of a  component is  a resurgent
    symbol or a nested case head.
 */ 
int is_nested_head (Expr    lc,      // position of case/subcase
                    Expr    x,       // either a resurgent symbol or a case head
                    Expr    type,    // the type of x
                    Expr    env)     // environment for this type
{
  /* x is either:
   
       - <string>                                singleton head or untyped resurgent symbol
       - (non_singleton_nested_head . <head>)    a non singleton nested head
       - (<type> . <string>)                     typed resurgent symbol
   */ 
   
  if (is_string(x))
    return is_singleton_name(lc,x,type,env);
   
  assert(consp(x));
  if (car(x) == non_singleton_nested_head)
    return 1; 
  else
    return 0; 
}

   
   
   
/* Check if a case/subcase is modeled on an alternative (same name, same arity) */ 
int modeled_on(Expr alt,                // alternative        ((<name> ...) . <components>)
               Expr the_head_of_case)   // head of case: one of:
                                        //      <string>
                                        //      (<string> . <operands>)
                                        //      (non_singleton_nested_head <string> . <operands>)
{
  Expr alt_name = car(car(alt)); 
  Expr case_name; 

  /* a head of  case may be a  singleton. In that case it  is a string 'a',  but should be
     '(a)' instead. */ 
  if (is_string(the_head_of_case))
    the_head_of_case = list1(the_head_of_case); 
   
  /* if needed, remove the tag 'non_singleton_nested_head' in front of the case head */ 
  if (consp(the_head_of_case) && car(the_head_of_case) == non_singleton_nested_head)
    the_head_of_case = cdr(the_head_of_case); 
   
  case_name = car(the_head_of_case); 
   
  if (alt_name != case_name)                    return 0; 
  if (length(alt) != length(the_head_of_case))  return 0; 
  return 1; 
}
   
   
   

/* 
   The function  'make_case_groups' gets a list  of cases/subcases, and  separates it into
   the  corresponding  list  of case  groups.   This  operation  does  not need  any  type
   definition. It  is base only on the  names and arities of  cases/subcases. For example,
   the list of cases/subcases:
   
   (
     (("a" "g")                 <lc> . <body 1>)
     (("a" ("f" "u" "v" "w"))   <lc> . <body 2>)
     (("a" "x" "y")             <lc> . <body 3>)
     (("b" ("c" "z"))           <lc> . <body 4>)
   )
   
   corresponding to the conditional:
   
     if t is 
       {
         a(g)          then <body 1>,
         a(f(u,v,w))   then <body 2>,
         a(x,y)        then <body 2>,
         b(c(z))       then <body 3>
       }
   
   will be separated into:
   
   (
     ( // arity 1
        (("a" "g")                 <lc> . <body 1>)
        (("a" ("f" "u" "v" "w"))   <lc> . <body 2>)
     )
     ( // arity 2
        (("a" "x" "y")             <lc> . <body 3>)
     )
     (
        (("b" ("c" "z"))           <lc> . <body 4>)
     )
   )
    
   However, it is good for error generation to compare the list to the alternatives of the
   type of the test of the conditional.
   
 */ 
Expr make_case_groups(Expr     lc,       // position of whole conditional
                      Expr     alts,     // list of alternatives of type of test
                      Expr     cases)    // list of cases/subcases
{
  Expr result = nil;          // current state of result (reversed list of groups) 
  Expr group = nil;           // current state of current group (reversed list of cases)
  Expr last_used_alt = nil;   // last used alternative ('nil' when starting 
                              //      or corresponding to previous case)
   
  while(consp(cases))  // do for each case/subcase
    {
      Expr the_case = car(cases);   // the current case
   
      cases = cdr(cases);           // remaining cases
   
      if (last_used_alt != nil)
        { /* the current case is not the first case */ 
          if (modeled_on(last_used_alt,car(the_case)))
            { /* continue an already started case group */ 
              group = cons(the_case,group);   // construct the group (in reverse order)
            }
          else
            { /* start a new case group */ 
              if (alts == nil)
                {
                  if (show_errors)
                    {
                      err_line_col(lc,"E100",
                                   str_format(msgtext_too_many_subcases[0]));
                    }
                  return nil; 
                }
              else
                {
                  /* record the group just constructed */ 
                  last_used_alt = car(alts); 
                  alts = cdr(alts); 
                  result = cons(reverse(group),result);   // record a whole group 
                  group = nil; 
   
                  if (modeled_on(last_used_alt,car(the_case)))
                    {
                      group = cons(the_case,group); 
                    }
                  else
                    {
                      if (show_errors)
                        {
                          err_line_col(second(the_case),"E101a",
                                       str_format(msgtext_invalid_subcase[0]));
                        }
                      return nil; 
                    }
                }
            }
        }
      else
        { /* the current case is the first case */ 
          if (alts == nil)
            {
              if (show_errors)
                {
                  err_line_col(lc,"E101b",
                               str_format(msgtext_invalid_subcase[0])); 
                }
              return nil; 
            }
          else
            {
              last_used_alt = car(alts);   // record the current alternative
              alts = cdr(alts);            // other alternatives 
   
              if (modeled_on(last_used_alt,car(the_case)))
                { /* begin the first group */ 
                  group = cons(the_case,group); 
                }
              else
                { /* first case not modeled on first alternative */ 
                  if (show_errors)
                    {
                      err_line_col(lc,"E101c",
                                   str_format(msgtext_invalid_subcase[0])); 
                    }
                  return nil; 
                }
            }
        }
    }
   
  /* if there a group under construction, record it */ 
  if (consp(group))
    {
      result = cons(reverse(group),result);
    }
   
  if (alts != nil)
    {
      if (show_errors)
        {
          err_line_col(lc,"E099",
                       str_format(msgtext_not_enough_subcases[0]));
        }
      return nil; 
    }
   
  return reverse(result); 
}
   
      
   
   
/*  Making the list of resurgent symbols for  a an element in a case group.  This function
    takes a case/subcase, for example:
   
        a(b,y,c(z))  then <body>
   
   and tries to  determine the resurgent symbol used for each  component. In this example,
   assuming that 'b' is a singleton nested head, the result will be:
   
        (nil y nil)
   
   i.e. 'nil' is used when the symbol cannot be determined from this case. 
 */    
Expr list_symbols_for_one_case(Expr alt,       // the alternative on which the case is modeled
                               Expr the_case,
                               Expr env)
{
  Expr lc, ops, result; 
   
  /* the case is either      ((name op1 ...) <lc> . <body>) 
                     or      (name           <lc> . <body>)
   */ 
   
  lc = second(the_case); 
   
  if (is_string(car(the_case)))
    ops = nil; 
  else
    ops = cdr(car(the_case));   /* (op1 ...) */ 
   
  result = nil; // returns a list of the form (x nil ...)
                // i.e. with 'nil' when the component is not a resurgent symbol
  
  alt = cdr(alt);    // ((<type> . <sym or nil>) ...)
   
  /* construct the result in reverse */ 
  while(consp(ops))
    {
      Expr op1   = car(ops); 
      Expr type1 = car(car(alt)); 
   
      if (!is_nested_head(lc,op1,type1,env))
        {
          if (is_string(op1))
            result = cons(op1,result);
          else
            result = cons(cdr(op1),result);   // op1 is (<type> . <sym>)
        }
      else
        {
          result = cons(nil,result); 
        }
      alt = cdr(alt); 
      ops = cdr(ops); 
    }
  return reverse(result); 
}
   
   
/* Merging two lists of resurgent symbols for  elements of the same group. For example, it
   gives:
   
      merge( (x nil z   nil),
             (x y   nil nil)) 
         =   (x y   z   nil)
   
   At the  same type we  may check that  the same symbol appears  at the same  places. For
   example,
   
      merge((x, ...),(y, ...))
   
   will produce an error (actually the tag 'invalid') if x != y.
 */ 
Expr merge_case_symbols(Expr lc,    // position of second element of case group
                        Expr l1,    // the two lists are assumed of the same length
                        Expr l2)
{
  Expr result = nil; 
   
  while(consp(l1))
    {
      assert(consp(l2)); 
      if (car(l1) == nil)    // (nil,y) |-> y   (nil,nil) |-> nil
        {
          result = cons(car(l2),result);
        }
      else   //  (x,x) |-> x    (x,y) |-> error (actually 'invalid')
        {
          if (car(l2) == nil)    // the second one may be 'nil'
            result = cons(car(l1),result);
          else if (car(l1) == car(l2))
            result = cons(car(l1),result);   // both are symbols
          else
            {
              if (show_errors)
                {
                  err_line_col(lc,"E102",
                               str_format(msgtext_non_equal_resurgents[0],
                                          string_content(car(l1)),
                                          string_content(car(l2)))); 
                }
              return invalid; 
            }
        }
      l1 = cdr(l1);
      l2 = cdr(l2); 
    }
  assert(!consp(l2)); 
   
  return reverse(result); 
}

   
/* Generating a fresh resurgent symbol. */    
U32 resurgent_symbols_counter = 0;    
char frsbuf[20]; 
Expr fresh_resurgent_symbol(void)
{
  resurgent_symbols_counter++; 
  sprintf(frsbuf,"+++fresh%d",(int)resurgent_symbols_counter); 
  return new_string(frsbuf);    
}
   
   
   
/* 
    Making the list of  resurgent symbols for a case group. May  return 'invalid'. Here we
    construct  the  complete list  of  resurgent symbols  for  a  group of  cases/subcases
    corresponding to a given alternative of the type of the test.
   
    In a first step we collect the symbols  for each case in the group, and merge them. At
    the end some symbols may be not determined (there are still occurences of 'nil' in the
    result). We replace these 'nil' by fresh resurgent symbols. 
 */ 
Expr list_case_group_resurgents(Expr alt, 
                                Expr group,
                                Expr env)
{
  Expr result, aux; 
   
  /* do for the first element */ 
  assert(consp(group));  // a group is always non empty
  result = list_symbols_for_one_case(alt,
                                     car(group),  // resurgents or heads 
                                     env); 
   
  group = cdr(group);
   
  /* do for other elements */ 
  while(consp(group))
    {
   
      result = merge_case_symbols(second(car(group)),   // lc
                                  result,
                                  list_symbols_for_one_case(alt,
                                                            car(group),
                                                            env));
   
      if (result == invalid) return invalid; 
      group = cdr(group); 
    }
   
  /* replace all occurences of nil by fresh resurgent symbols */ 
  aux = result; 
  result = nil; 
   
  while(consp(aux)) // this will reverse the result
    {
      if (car(aux) == nil)
        result = cons(fresh_resurgent_symbol(),result);
      else
        result = cons(car(aux),result); 
      aux = cdr(aux); 
    }
   
   
  return reverse(result); 
}
   
   
   
/*  
    Expand a subgroup into a single element.  We have a subgroup at hand, i.e. consecutive
    elements in a group,  with the same components in the first  k columns and nested case
    heads in the k-th column. For example:
   
     [h . []]                   then A,
     [h . [i . []]]             then B,
     [h . [i . [j . t]]]        then C

   where the common components are just 'h', and the nested heads are:
   
          []
          [i . []]
          [i . [j . k]]
   
   We also got the resurgent symbol (say \A31)  of the k-th column. We have to tranform this
   subgroup into a single case like this one:
   
     [h . \A31] then if \A31 is 
                  {
                    []              then A,
                    [i . []]        then B,
                    [i . [j . k]]   then C
                  }
   
   
 */ 
Expr expand_to_single(Expr    lc,         // position of the first element in the subgroup
                      Expr    subgroup,   // the subgroup itself  
                      Expr    resurg,     // the resurgent symbol
                      int     column)     // the column concerned by this operation
{
  Expr new_cases = nil;    // list of cases/subcases in the newly generated conditional
  Expr aux = subgroup; 
  Expr alt_name = car(car(car(subgroup)));       // name of alternative
  Expr first_comps = cdr(car(car(subgroup)));    // components in the first case
  Expr new_head = nil; 
  int i; 
  
     /* Make the cases of the new conditional */ 
  while(consp(aux))
    {
      Expr the_case = car(aux);   // ((name op1 ...) <lc> . <body>)
      Expr nested_head = nth(new_integer(column-1),cdr(car(the_case))); 
   
      /* if needed, remove 'non_singleton_nested_head' before unnesting the head */ 
      if (consp(nested_head) && car(nested_head) == non_singleton_nested_head)
        nested_head = cdr(nested_head); 
   
      /* if the head is a string "a", replace it by ("a") */ 
   
      if (is_string(nested_head))
        nested_head = list1(nested_head); 
   
      new_cases = cons(cons(nested_head,   
                            cdr(the_case)),           // keep the same position and body
                       new_cases);
      aux = cdr(aux); 
    }

  /* Make a  copy of the  head of the  first case, with the  resurgent symbol in  the k-th
   column. */ 
  aux = first_comps;   // (op1 ...)
  i = 0; 
  while(consp(aux))
    {
      if (i != column-1)
        new_head = cons(car(aux),new_head);
      else 
        new_head = cons(resurg,new_head); 
      aux = cdr(aux); 
      i++; 
    }
  new_head = cons(alt_name,reverse(new_head));   // this is (for example) '[h . \A31]'
   
  /* construct the conditional and the new main case. */ 
   
  return mcons3(new_head,       // for example [h . \A31]
                lc,             // position of first case in the subgroup
                mcons4(cond,                      // the new (nested) conditional 
                       lc,                        // always the same position
                       mcons3(symbol,lc,resurg),  // the resurgent symbol as a term
                       reverse(new_cases)));  
}
   
   
   
   
   

/*  Performing the expansion of a case group in a given column. 
 */
Expr expand_case_group_one_column(Expr group, 
                                  Expr type,        // type of this column
                                  Expr type_alts,   // alternatives for this type
                                  Expr rsym,        // resurgent symbol for this column
                                  int column,       // column number 
                                  Expr env)
{
  Expr past_elements = nil;    // elements already seen (in reverse order)
  Expr the_case = nil; 
  Expr components, components_to_the_left; 
  Expr to_be_expanded; 
  Expr aux, last_alt_seen; 

  assert(consp(group)); 
   
 begin:
   
  /* skip elements with a resurgent symbol in the given column */ 
  while(consp(group))
    {
      the_case = car(group);    
   
      if (is_nested_head(second(the_case),                 // position of this case/subcase
                         nth(new_integer(column-1),cdr(car(the_case))), // resurgent symbol or nested head
                         type,                             // type of this column
                         env))
        {
          break; // found the first nested head
        }
      else
        {
          past_elements = cons(the_case,past_elements);
          group = cdr(group);
        }
    }
   
  if (!consp(group)) // no nested head found in this column
    {
      return reverse(past_elements);   
    }
   
  /* The first nested head  has been found.  We record the instances  of components on the
     left of our  column. They must be the  same for all following elements  until we have
     finished with the alternatives of the type of the column. */    
  components = cdr(car(the_case)); 
  components_to_the_left = first_elements(new_integer(column-1),components);   // defined in 'expr.cpp'
   
  /* Extract the elements to be expanded (maybe we should say 'reduced' ?) into a single case */
  to_be_expanded = list1(the_case); 
  group = cdr(group); 
  aux = type_alts; 
  last_alt_seen = car(aux); 
  aux = cdr(aux); 
  while(consp(group))
    {
      Expr element = car(group);       /* ((name comp1 ...) <lc> . <body>) */ 
      Expr comps = cdr(car(element));  /* (comp1 ...) */ 
   
      /* stop collecting elements if components on the right are changing */ 
      if (!equal(components_to_the_left,
                 first_elements(new_integer(column-1),comps)))
        {
          /* The  components  to the  left  have  changed: check  that  we  have used  all
             alternatives from the type of the column */ 
          if (!(aux == nil  ||
                (cdr(aux) == nil && !equal(car(aux),last_alt_seen))
               ))
            {
              if (show_errors)
                {
                  err_line_col(second(element),"E103",
                               str_format(msgtext_not_enough_subcases[0]));
                }
              return nil; 
            }
          else
            { /* the subgroup is complete (without the current element) */ 
              break; 
            }
        }
      else
        { /* components to the left are equal to the previous ones */ 
          if (modeled_on(last_alt_seen,nth(new_integer(column-1),comps)))
            { /* the component matches the current alternative */ 
              to_be_expanded = cons(element,to_be_expanded); 
              group = cdr(group); 
            }
          else
            { /* the component does'nt match the current alternative */ 
              if (aux == nil)
                {
                  if (show_errors)
                    {
                      err_line_col(second(element),"E104",
                                   str_format(msgtext_too_many_subcases[0]));
                    }
                  return nil; 
                }
              else
                {
                  last_alt_seen = car(aux); 
                  aux = cdr(aux); 
                  if (modeled_on(last_alt_seen,nth(new_integer(column-1),comps)))
                    {
                      to_be_expanded = cons(element,to_be_expanded);
                      group = cdr(group); 
                    }
                  else
                    { /* the element  does neither match the new  current alternative, nor
                         the previous one. */ 
                      if (show_errors)
                        {
                          err_line_col(second(element),"E105",
                                       str_format(msgtext_nested_head_doesnt_match_next_alt[0],column));
                        }
                      return nil; 
                    }
                }
            }
        }
    }
   
  /* expand the selected elements into a single element */ 
  to_be_expanded = reverse(to_be_expanded); 
  past_elements = cons(expand_to_single(second(car(to_be_expanded)),   // lc
                                        to_be_expanded,                // the subgroup 
                                        rsym,       // resurgent symbol for this column
                                        column),
                       past_elements); 
   
  /* if there are still elements continue from the beginning. */ 
  if (consp(group)) goto begin; 
  /* otherwise return the result in the right order */ 
  return reverse(past_elements); 
}
   
   
   
   
/* Expanding a case group completely */    
Expr expand_case_group(Expr    group,      // the case group
                       Expr    alt_tail,   // the corresponding alternative tail (components)
                       Expr    rsym_list,  // list of resurgent symbols
                       Expr    env)
{
  int column = length(alt_tail);    // column counter 
  
  assert(group != nil); 
   
  while(column > 0)
    {
      Expr type = car(nth(new_integer(column-1),alt_tail));  // type for this column
   
      group = expand_case_group_one_column(group,
                                           type, 
                                           get_alts(type,env,second(car(group))),
                                           nth(new_integer(column-1),rsym_list), 
                                           column,
                                           env);   
      column--; 
      if (group == nil) return nil; // an error was detected
    }
  return group; 
}
   
   
   
/* 
    Expanding (at first level only) a  conditional with subcases (i.e.  with strictly more
    cases than the number of alternatives in the type of its test).
  
    There is no need to expand more than the first level, because after the expansion, the
    expanded conditional  is passed  to the normal  circuit of conditional  checking. This
    will call  'expand_conditional' again if  needed. Notice that  'expand_conditional' is
    called only  when the number of  cases in a  conditional is strictly greater  than the
    number of alternatives in the type of the test.
   
  */ 
Expr expand_conditional(Expr     lc,            // position of conditional
                        Expr     alts_of_test,  // alternatives of the type of the test
                        Expr     the_test,      // the test itself (a term)
                        Expr     cases,         // list of case/subcases
                        Expr     env)
{
  Expr groups = make_case_groups(lc,alts_of_test,cases); 
  Expr new_cases = nil; 

  if (groups == nil) return nil;   // an error was detected
   
  /* handle each case group */ 
  while(consp(groups))
    {
      Expr group = car(groups); 
      Expr new_cases1; 
      Expr alt = car(alts_of_test); 
      Expr resurgents = list_case_group_resurgents(alt,group,env); 
   
      if (resurgents == invalid) return nil; 
   
      new_cases1 = expand_case_group(group,
                                     cdr(alt),
                                     resurgents,
                                     env); 

      //if (length(group) > 1) anb_exit(1); 
   
      if (new_cases1 == nil) return nil; 
      new_cases = append(new_cases1,new_cases); 
      groups = cdr(groups); 
      alts_of_test = cdr(alts_of_test); 
    }
   
  /* make the new conditional */ 
  return mcons4(cond, 
                lc,
                the_test,
                reverse(new_cases)); 
}

   
   
   
   
/* 
    We   have  not  finished   with  subcases   since  we   must  also   handle  selective
    conditionals. For example, if we have:
   
      if t is [h . [u . [ ]]] then A else B
   
   we would at first glance expand it into:
   
      if t is [h . \A31] then 
        if \A31 is [u . \A32] then 
          if \A32 is [ ] then A
          else B 
        else B
      else B
   
   Of  course,  the problem  comes  from  the fact  that  B  is  duplicated maybe  several
   times. This is a problem if the code for B is big.
   
   The solution is to expand as follows:
   
   if (if t is [h . \A31] then 
         if \A31 is [u . \A32] then 
           if \A32 is [ ] then success(A)
           else failure
         else failure
       else failure) is
     {
       failure    then B, 
       success(a) then a
     }
     
   In order to  do this, what do we need ?  First of all, we have to  detect the fact that
   the head contains nested heads. To that end we need the type of the test. 
   
   In order to avoid generating a new surrounding 'if':
   
   if ... is 
     {
       failure    then B,
       success(a) then a
     }
   
   at each depth level, we actually expand in the form of:
   
       if t is [h . \A31] then 
         if \A31 is [u . \A32] then 
           if \A32 is [ ] then success(A)
           else failure
         else failure
       else failure   
   
   and  only at  the  end of  this  process we  envelop the  result  into the  surrounding
   conditional. Notice that if we proceed only one depth level on our example, we get:
   
      if t is [h . \A31] then 
        if \A31 is [u . []] then success(A) 
        else failure
      else failure
   
   So, in order to avoid  repeating the 'success(A)' (generating a 'success(success(A))'),
   we use a (fresh) marker for A: \A3A
   
      if t is [h . \A31] then 
        if \A31 is [u . []] then \A3A
        else failure
      else failure
   
   so that the second expansion gives:
   
      if t is [h . \A31] then 
        if \A31 is [u . \A32] then 
          if \A32 is [] then \A3A
          else failure
        else failure
      else failure
   
   At the end, we just have to replace the marker by 'success(A)', and put the envelop. 
   
   As an exempla, here is how
   
      if t is (true,false) then A else B 
   
   will be expanded:
   
   Step 1:
      if t is (true,\A31) then
        if \A31 is false then \A3A
        else failure
      else failure
   
   Step 2:
      if t is (\A30,\A31) then
        if \A30 is true then 
          if \A31 is false then \A3A
          else failure
        else failure
      else failure
   
   Step 3:
    if (if t is (\A30,\A31) then
          if \A30 is true then 
            if \A31 is false then success(A)
            else failure
          else failure
        else failure) is 
      {
        failure then B,
        success(_) then _
      }
   
 */
   
   
   
   
   
   
/* Testing for variable hiding (when strong preemption is forbidden) */ 
static Expr    /* Returns the list of entries of the context which are hidden by 'name'. */ 
get_hidden_entries (Expr name, 
                    Expr ctxt)
{
  Expr result = nil; 
   
  if (name == pdstr__) return nil;   /* allow hidding for '_' */  
   
   /* ctxt = ((<symbol> . <type>) ...)   or 
      ctxt = ((f_micro_ctxt <fname> <ftype> (<symbol> . <type>) ...) ...) */ 
   
  while(consp(ctxt))
    {
      Expr entry = car(ctxt); 
   
      if (car(entry) == f_micro_ctxt)
        {
          ctxt = cdr3(entry); 
          continue; 
        }
   
      assert(is_string(car(entry)));    
      if (car(entry) == name)
        {
          result = cons(entry,result); 
        }
   
      ctxt = cdr(ctxt); 
    }
   
  return reverse(result); 
}
   
   
   
   
   
   
   
/* An index (hash-table) for quickly interpreting symbols. */
   
SymbolIndexArray symbol_index[256];   
   
void init_symbol_index(void)
{
  int i; 
  for (i = 0; i < 256; i++)
    {
      symbol_index[i].max = 100; 
      symbol_index[i].next = 0;
      symbol_index[i].array = (SymbolIndexEntry *)mallocz(100*sizeof(SymbolIndexEntry));
    }
}
   
   
U8 u8_symbol_hash(Expr x)   
{
  int i = 0; 
  U8 *s;
  U8 result = 0; 
   
  assert(is_string(x)); 
  s = (U8 *)string_content(x); 
  while (s[i] != 0) 
   {
     result += s[i];
     i++;
   }
  return result; 
}
   
   
/* Adding an entry */ 
void _add_symbol_index_entry(Expr symbol, SymbolSort sort, int index)
{
  U8 hash; 
  U32 next; 
  SymbolIndexEntry *array; 
   
  hash = u8_symbol_hash(symbol); 
  next = symbol_index[hash].next;
  array = symbol_index[hash].array; 
  if (symbol_index[hash].next >= symbol_index[hash].max)
    {
      //printf("Enlarging array for hash %d in symbol index.\n",hash); 
      symbol_index[hash].max += 100; 
      symbol_index[hash].array = reallocz(array,(symbol_index[hash].max)*sizeof(SymbolIndexEntry)); 
      array = symbol_index[hash].array;
    }
  array[next].symbol = symbol;
  array[next].sort = sort;
  array[next].index = index;
  (symbol_index[hash].next)++; 
}
   
   
  
   
static Expr signature_from_type(Expr type, Expr env)
{

  /* dereference type */ 
  while (is_unknown(type))
    {
      type = assoc(type,env); 
      assert(type != key_not_found); 
    }

  if (consp(type))
    switch(car(type))
      {
      case functype:
 return second(type); 
      }

  return nil;  /* because T is the same as () -> T */ 
}



   
   

/* The  next function  receives a  list  of interpretations,  and selects  among them  the
   interpretation whose type is  given by the pair (type,env). If it  does not exist or if
   there are several of them, an error  message is sent, and 'nil' is returned. Otherwise,
   the unique interpretation found is returned in the form (head,env).
*/ 
   
Expr select_unique_interpretation(Expr lc, 
                                  Expr interps,
                                  Expr type,
                                  Expr env)
{
  Expr typed_interps = nil; 
  Expr aux;
   
  aux = interps;
  while (consp(aux))
    {
      Expr interp = car(aux); 
      if (same_type(type_from_interpretation(car(interp),cdr(interp)),cdr(interp),type,env))
        typed_interps = cons(interp,typed_interps); 
      aux = cdr(aux);
    }
   
  if (typed_interps == nil)
    {
      if (show_errors)
        {
          err_line_col(lc,"E012",
                       msgtext_no_interp_of_type[0]); 
          show_type(errfile,type,env);
          fprintf(errfile,"%s",
                  msgtext_no_interp_of_type2[0]);
          show_interpretations_types(errfile,interps);
        }
      return nil; 
    }
   
  if (length(typed_interps) >= 2)
    {
      if (show_errors)
        {
          err_line_col(lc,"E013",
                       msgtext_several_interps_of_type[0]); 
          show_interpretations_types(errfile,typed_interps);
        }
      return nil; 
    }
   
  return car(typed_interps); 
}
   
   
const char *special_type_scheme_name(Expr type_ind)
{
  switch(type_ind)
    {
    default: 
      assert(0);
      return NULL; 
    }
}
   
   
   
Expr select_special_interpretation(Expr lc, 
                                   Expr interps,
                                   Expr type_ind)
{
  Expr typed_interps = nil; 
  Expr aux;
   
  aux = interps;
  while (consp(aux))
    {
      Expr interp = car(aux); 
      Expr type = type_from_interpretation(car(interp),cdr(interp));
      if (consp(type) && car(type) == type_ind)
        typed_interps = cons(interp,typed_interps); 
      aux = cdr(aux);
    }
   
  if (typed_interps == nil)
    {
      if (show_errors)
        {
          err_line_col(lc,"E014",
                       msgtext_no_interp_of_type[0]); 
          fprintf(errfile,"%s",special_type_scheme_name(type_ind));
          show_interpretations_types(errfile,interps);
        }
      return nil; 
    }
   
  if (length(typed_interps) >= 2)
    {
      if (show_errors)
        {
          err_line_col(lc,"E015",
                       msgtext_several_interps_of_type[0]); 
          show_interpretations_types(errfile,typed_interps);
        }
      return nil; 
    }
   
  return car(typed_interps); 
}
   
   
   
/********************* Interpreting terms ************************************/



/* Interpreting operations (and variable names) */ 

static Expr                              /* <operation interpretations> */ 
operation_interpretations(Expr lc,       /* <lc> */ 
                          Expr type,     /* required type for operation */
                          Expr name,     /* <operation name> */ 
                          Expr env)
{
  int i, h; 
  Expr op_ints_result = nil; 
  Expr already_refreshed, signature, opttype, parms; 
  U8 hash = u8_symbol_hash(name); 
  SymbolIndexEntry *hash_array = symbol_index[hash].array;
  U32 max_hash = symbol_index[hash].next; 

  assert(consp(lc));
  assert(is_string(name)); 

  /* explore array of operations for the given hash */ 
  for (h = 0; h < (int)max_hash; h++)
    //for (i = 0; i < next_operation; i++)
    {
      if (hash_array[h].sort != syms_operation) continue; 
      i = hash_array[h].index; 
      /* consider only operations with the right name and not private to another file */ 
      if ( !((operations[i].global)&op_public) && 
            strcmp(current_file_abs_path,string_content(operations[i].abs_file_path))) 
        continue;     
        
      /* if the operation is the last one and is either macro or inline, ignore it 
         (this forbids recursive macro/inline). */
#if 0         
      if (   (i+1 == next_operation)
          && (((operations[i].global)&inline_mask) || ((operations[i].global)&macro_mask))
         ) continue; 
#endif     
      
      /* in case of option '-read1' avoid invisible file names */ 
      if ( read1 && !(is_visible(string_content(operations[i].abs_file_path))))
        continue; 
   
      if (member(name,operations[i].names))
        {
          /* get a refreshed signature of operation */ 
          already_refreshed = nil; 
          signature = refresh(operations[i].signature,&already_refreshed);  

          /* get a refreshed target type of operation */ 
          opttype = refresh(operations[i].target_type,&already_refreshed); 
         
          /* get a refreshed list of parameters */ 
          parms = refresh(operations[i].parms,&already_refreshed); 

          /* TODO: the type found for the operation must match the
             required type, otherwise, it is not recorded. */ 

          /* record this interpretation */ 
          op_ints_result = cons(cons(mcons7(operation,
                                            lc, 
                                            new_integer(i), 
                                            name, 
                                            parms, 
                                            opttype, 
                                            signature),
                                     env),  
                                op_ints_result); 
        }
    }
    

  /* get also interpretations as global variable */ 
  for (h = 0; h < (int)max_hash; h++)
  //for (i = 0; i < next_variable; i++)
    {
      if (hash_array[h].sort != syms_variable) continue; 
      i = hash_array[h].index; 
         /* consider only variables with the right name and not private to another file */ 
      if ( !((variables[i].global)&op_public) && 
            strcmp(current_file_abs_path,string_content(variables[i].abs_file_path))) 
        continue;     
   
      /* in case of option '-read1' avoid invisible file names */ 
      if ( read1 && !(is_visible(string_content(variables[i].abs_file_path))))
        continue; 
   
      if (name == variables[i].name)
 op_ints_result = cons(cons(mcons3(global_variable,lc,new_integer(i)),
       nil),
         op_ints_result);
    }

  /* if no match found, the operation is unknown */ 
  if (op_ints_result == nil)
    {
      if (show_errors)
        {
          err_line_col(lc,"E016", str_format(
                                             msgtext_unknown_symbol[0],
                                             string_content(name))); 
        }
    }

  /* in any case, return the list of interpretations */ 
  return op_ints_result; 
}







/* interpreting a tuple of terms */ 
static    
Expr /* returns a list of tuple interpretations */
tuple_interpretations(Expr ttypes,    /* required types */ 
                      Expr terms,     /* terms to be interpreted */
                      Expr ctxts,     /* list of contexts */
                      Expr env,       /* environment */
                      Expr tvs,       /* user type variable list */ 
                      int same_types) /* if 1 keep only those interpretations where the
                                         elements of the tuple may have the same type. */ 
{ 
  Expr result = nil;  
  Expr first, aux; 
  Expr first_int, first_env; 
  Expr others_int, others_env; 
  Expr others; 

  assert(length(terms) == length(ctxts)); 
  
  if (terms == nil) 
    { 
      /* no term to  interpret, return ((() .  env)) (there is  only one interpretation of
         the empty tuple) */ 
         
      result = list1(cons(nil,env));  
    } 
  else 
    { 
      /* get all interpretations of first term */ 
      first = 
        term_interpretations(dummy,car(terms),car(ctxts),env,tvs,0);  
      if (first == nil) return nil;  
      

      /* get all interpretations of other terms */ 
      others = tuple_interpretations(dummy,
                                     cdr(terms),
                                     cdr(ctxts),
                                     env,
                                     tvs,
                                     same_types); 

      if (others == nil) return nil; 
      
      
      /* construct interpretations of original tuple:  for each interpretation (I1 . e) of
         first, and each interpretation ((I2 ... Ik) . e') of others, construct:

         ((I1 I2 ... Ik) . e'')

         where e'' is obtained by merging e and e' */ 
      while (consp(first))
        {
          first_int = car(car(first));    
          first_env = cdr(car(first)); 

          aux = others; 

          while (consp(aux))
            {
              Expr new_env;
   
              others_int = car(car(aux)); 
              others_env = cdr(car(aux)); 

              new_env = join_envs(first_env,others_env);
   
              /* others_int may be 'nil'. */ 
   
              if (new_env != not_unifiable)
                {
                  if (same_types && consp(others_int))
                    {
                      Expr e = unify(type_from_interpretation(first_int,first_env),
                                     new_env,
                                     type_from_interpretation(car(others_int),others_env),
                                     nil);
                      if (e != not_unifiable)
                        {
                          result = cons(cons(cons(first_int,
                                                  others_int),
                                             e),
                                        result);  
                        }
                    }
                  else
                    {
                      result = cons(cons(cons(first_int,
                                              others_int),
                                         new_env),
                                    result);  
                    }
                }
              aux = cdr(aux); 
            }
          first = cdr(first); 
        }
    }

  return result;  
} 


int word32_value(Expr bigits) /* transform a sequence of bigits in basis 256 into a word32 */
{
  /* the result must fits into 32 bits */ 
  int result = 0; 
  while(consp(bigits))
    {
      result = result << 8; 
      result += integer_value(car(bigits)); 
      bigits = cdr(bigits); 
    }
  return result; 
}
   

/* interpreting a term */ 
Expr                /* returns a list of interpretations */ 
term_interpretations(Expr ttype,      /* required type for that term (may contain unknowns) */ 
                     Expr term,       /* <term> */ 
                     Expr ctxt,       /* <context> */ 
                     Expr env,        /* <environment> */ 
                     Expr tvs,        /* user type variable list */ 
                     int allow_mvar_access)   /* if non zero, allow pseudo-terms mv(i) */
{
  Expr result = nil; 

   
 begin:
   
   
  switch (car(term))
    {
   
    case alt_number: /* (alt_number <lc> . <term>) */ 
      {
        result = alt_number_interpretations(second(term), 
                                            dummy,
                                            cdr2(term),
                                            ctxt,
                                            env,
                                            tvs); 
      }
      break; 
   
    case protect: /* (protect <lc> . <term>) */
      {
        result = protect_interpretations(second(term),
                                         dummy,
                                         cdr2(term),
                                         ctxt,
                                         env,
                                         tvs); 
      }
      break;
   
    case lock: /* (lock <lc> <term> . <term>) */ 
      {
        result = lock_interpretations(second(term),
                                      dummy,
                                      third(term),
                                      cdr3(term),
                                      ctxt,
                                      env,
                                      tvs); 
      }
      break; 
   
    case alert: /* (alert <lc> . <file name>) */ 
      {
        /* equivalent to the term: alert_handler(file_name,line,column), 
           with unknown type. */ 
        Expr lc = second(term); 

        term = list6(app,
                     lc,
                     mcons3(symbol,lc,pdstr_alert_handler),
                     mcons3(string,lc,cdr2(term)),
                     list3(integer_10,lc,new_integer(line_in(lc))), 
                     list3(integer_10,lc,new_integer(col_in(lc)))); 
   
        goto begin; 
      }
      break; 
      
    case todo:  /* (todo <lc> <file name> . <text>) */
      {
        Expr lc = second(term); 
        Expr fn = third(term); 
        Expr tx = cdr3(term); 
        
        term = list7(app,
                     lc,
                     mcons3(symbol,lc,pdstr_todo_handler),
                     mcons3(string,lc,fn),
                     list3(integer_10,lc,new_integer(line_in(lc))), 
                     list3(integer_10,lc,new_integer(col_in(lc))),
                     mcons3(string,lc,tx)); 
        goto begin;  
      }
      break;  

    case debug_avm: /* (debug_avm <lc> . <term>) */
      {
        Expr interps = term_interpretations(ttype,cdr2(term),ctxt,env,tvs,0);
        result = nil; 
        while (consp(interps))
          {
            result = cons(cons(mcons3(debug_avm,
                                      second(term),
                                      car(car(interps))),
                               cdr(car(interps))),
                          result); 
            interps = cdr(interps); 
          }
      }
      break; 

    case terminal: /* (terminal <lc> . <term>) */
      {
        Expr interps = term_interpretations(ttype,cdr2(term),ctxt,env,tvs,0);
        result = nil; 
        while (consp(interps))
          {
            result = cons(cons(mcons3(terminal,
                                      second(term),
                                      car(car(interps))),
                               cdr(car(interps))),
                          result); 
            interps = cdr(interps); 
          }
      }
      break; 


   
    case integer_10:
    case integer_16:
      {
        /* (integer_10 <lc> bigit ... bigit) */  
        /* (integer_16 <lc> bigit ... bigit) */
        /* in both cases, the bigits are in basis 256 */
   
        Expr bigits = cdr2(term); /* (bigit ... bigit) */
        result = nil; 

#if 0 
   Int32 is obsolete
        if ((length(bigits) < 4)
            || ((length(bigits) <= 4) && integer_value(car(bigits)) < 128))
          {
            /* (positive) number less than 2^31: add an interpretation as 'Int32' */
            result = cons(cons(mcons3(anb_int32,second(term),int32_value(bigits)),env),
                          result);
          }
#endif   
   
        /* add an interpretation as 'Int' */
        result = cons(cons(cons(car(term) == integer_10 ? anb_int_10 : anb_int_16, cdr(term)),env),
                      result);
   
        /* add an interpretation as Float */
        result = cons(cons(mcons4(fpnum,second(term),new_integer(word32_value(bigits)),new_integer(0)),
                           env),
                      result);   
   
        /* Interpretations  as words:  First compute  the interpretation  as  Word128, and
           reduce it modulo the other sizes. */ 
        {
          Expr bgts = bigits; 
          U32 word3, word2, word1, word0;   /* 32 bits words of 128 bits word (most significant first) */ 
   
          /* Keep only the 16 least significant (basis 256) bigits */ 
          while (length(bgts) > 16) { bgts = cdr(bgts); }
          /* Add leading zeros if needed */ 
          while (length(bgts) < 16) { bgts = cons(new_integer(0),bgts); }
          /* At this point 'bgts' has 16 bigits. */ 
          assert(length(bgts) == 16);
   
          /* compute the 4 32 bits words */ 
          word3 = word32_value(first_elements(new_integer(4),bgts));
          bgts = cdr4(bgts); 
          word2 = word32_value(first_elements(new_integer(4),bgts));
          bgts = cdr4(bgts); 
          word1 = word32_value(first_elements(new_integer(4),bgts));
          bgts = cdr4(bgts); 
          word0 = word32_value(first_elements(new_integer(4),bgts));
   
          /* make the Word128 interpretation */ 
          result = cons(cons(mcons5(word_128,word0,word1,word2,word3),env),result);
          /* make the Word64 interpretation */ 
          result = cons(cons(mcons3(word_64,word0,word1),env),result);
          /* make the Word32 interpretation */ 
          result = cons(cons(mcons3(small_datum,pdstr_Word32,(word0&0xFFFFFFFF)),env),result);
          /* make the Word16 interpretation */ 
          result = cons(cons(mcons3(small_datum,pdstr_Word16,(word0&0xFFFF)),env),result);
          /* make the Word8 interpretation */ 
          result = cons(cons(mcons3(small_datum,pdstr_Word8,(word0&0xFF)),env),result);
          /* make the Word4 interpretation */ 
          result = cons(cons(mcons3(small_datum,pdstr_Word4,(word0&0xF)),env),result);
        }   
      }
      break; 
   

   
   
   
    case fpnum:
      /* (fp <lc> . <word32 mantissa> . <word32 exponent>) */ 
      result = list1(cons(term,env)); 
      break; 

    case symbol:    
      /* (symbol <lc> . <string>) */ 
      term = cdr(term); 
      result = symbol_interpretations(car(term),   /* <lc> */  
                                      dummy,
                                      cdr(term),   /* <string> */
                                      ctxt,
                                      env);  
      break; 

#if 0
    case local: 
      /* (local <string> <depth> . <type>) */ 
      result = term; 
      break; 
#endif   
   
    case of_type:
      /* (of_type <lc> <type> . <term>) */ 
      term = cdr(term); 
      result = of_type_interpretations(car(term),       /* <lc> */ 
                                       dummy,
                                       car(cdr(term)),  /* <type> */
                                       cdr(cdr(term)),  /* <term> */ 
                                       ctxt,
                                       env,
                                       tvs); 
      break; 

    case constructor:
      result = list1(cons(term,env)); 
      break; 
   
   
    case lambda: 
      /* (lambda <lc> ((<type> . <sym>) ... ) . <term>) */ 
      term = cdr(term); 
      result = lambda_interpretations(car(term),     /* <lc> */ 
                                      dummy,
                                      nil,           /* anonymous function */ 
                                      second(term),  /* args */
                                      cdr2(term),    /* body */ 
                                      ctxt,
                                      env,
                                      tvs); 
      break; 
   
    case rec_lambda:
      /* (rec_lambda <lc> <func_name> ((<type> . <sym>) ... ) . <term>) */ 
      term = cdr(term); 
      result = lambda_interpretations(car(term),       /* lc */ 
                                      dummy, 
                                      second(term),    /* name of function */ 
                                      third(term),     /* args */ 
                                      cdr3(term),      /* body */ 
                                      ctxt,
                                      env,
                                      tvs); 
      break; 
   
    case app:
      /* (app <lc> <term> . <terms>) */
    {
  /* we need to transform: 
  
         ((X x ...) |-f-> body)(args)
            
     into: 
           
         with #f = (X x ...) |-f-> body, 
           #f(args)
               
     where #f is an internal symbol. In pseudo-Lisp: 

         (app <lc1> (rec_lambda <lc2> "f" ((<type> . <sym>) ... ) . <term>) . <args>)

     becomes: 
     
         (with <lc2> "#f"  (rec_lambda <lc2> "f" ((<type> . <sym>) ... ) . <term>) . 
             (app <lc1> (symbol <lc2> . "#f") . <args>))
  
  */ 
  
      Expr func = third(term); 
      Expr args = cdr3(term); 
      
        if (consp(func) && car(func) == rec_lambda)
        {
            char buf[1000]; 
            snprintf(buf,995,"#%s",string_content(third(func))); 
            term = mcons5(with,
                          second(func),       // <lc2>
                          new_string(buf),    // "#f"
                          func, 
                          mcons4(app,
                                 second(term),    // <lc1>
                                 mcons3(symbol,
                                        second(func),
                                        new_string(buf)), 
                                 args)
                          ); 
            
           result = term_interpretations(ttype,term,ctxt,env,tvs,allow_mvar_access); 
        }
        else
        {
        term = cdr(term); 
        result = app_interpretations(car(term),        /* <lc> */ 
                                     dummy,
                                     func,           //car(cdr(term)),   /* operation */
                                     args,           //cdr(cdr(term)),   /* operands */ 
                                     ctxt, 
                                     env, 
                                     tvs,
                                     allow_mvar_access); 
        }
      }
      break; 

    case cond:
      /* (cond <lc> <test> . <clauses 1>) */ 
      term = cdr(term); 
      result = cond_interpretations(car(term),      /* <lc> */ 
                                    dummy,
                                    car(cdr(term)), /* test */ 
                                    cdr(cdr(term)), /* clauses */ 
                                    ctxt, 
                                    env, 
                                    tvs);  
      break; 

    case select_cond:
      /* (select_cond <lc> <test> ((<sym> <typed resurg> ...) <lc> . <term>) . <else term>) */ 
      term = cdr(term); 
      {
        Expr clause = third(term); 
        result = select_cond_interpretations(car(term),         /* lc */
                                             second(term),      /* test */ 
                                             car(car(clause)),  /* constructor name */
                                             cdr(car(clause)),  /* typed resurgent symbols */ 
                                             cdr2(clause),      /* body of case term */ 
                                             cdr3(term),        /* else term */ 
                                             ctxt,
                                             env,
                                             tvs); 
      }
      break;

    case with: 
      /* (with <lc> <symbol> <term> . <term>) */ 
      result = with_interpretations(car(term),
                                    second(term),       /* lc */
                                    dummy,
                                    third(term),        /* symbol */
                                    forth(term),        /* defined term */ 
                                    cdr4(term),         /* body term */ 
                                    ctxt, 
                                    env,
                                    tvs); 
      break; 

    case string:
      /* (string <lc> . <string>) */ 

      /* TODO: The required type must match String */ 

      result = list1(cons(term,env)); 
      break; 

    case anb_read:
      /* (anb_read <lc> . <conn>) */
      term = cdr(term); 
      result = read_interpretations(car(term),          /* lc */
                                    dummy,
                                    cdr(term),          /* conn */ 
                                    ctxt,
                                    env,
                                    tvs); 
      break; 

    case anb_write:
      /* (anb_write <lc> <conn> . <term>) */
      term = cdr(term); 
      result = write_interpretations(car(term),          /* lc */ 
                                     dummy,
                                     second(term),       /* conn (<term>) */
                                     cdr2(term),         /* value (<term>) */ 
                                     ctxt,
                                     env,
                                     tvs); 
      break; 

    case anb_exchange:
      /* (anb_exchange <lc> <conn> . <term>) */
      term = cdr(term); 
      result = exchange_interpretations(car(term),          /* lc */ 
                                        dummy,
                                        second(term),       /* conn (<term>) */
                                        cdr2(term),         /* value (<term>) */ 
                                        ctxt,
                                        env,
                                        tvs); 
      break; 

    case wait_for:
      {
        /* (wait_for <lc> <term> <term> . <term>) */ 
        term = cdr(term); 
        result = wait_for_interpretations(car(term),    /* lc */ 
                                          dummy,
                                          second(term), /* condition */ 
                                          third(term), /* milliseconds */ 
                                          cdr3(term),   /* term after condition realized */
                                          ctxt,
                                          env,
                                          tvs); 
      }
      break; 

    case delegate:
      {
        /* (delegate <lc> <term> . <term>) */ 
        term = cdr(term); 
        result = delegate_interpretations(car(term),    /* lc */ 
                                          dummy,
                                          second(term), /* delegated */ 
                                          cdr2(term),   /* body */
                                          ctxt,
                                          env,
                                          tvs); 
      }
       break; 
       
       
    case delegatep:
      {
        /* (delegatep <lc> <term> <term> . <term>) */ 
        term = cdr(term); 
        result = delegatep_interpretations(car(term),    /* lc */ 
                                           dummy,
                                           second(term), /* priority */ 
                                           third(term),  /* delegated */ 
                                           cdr3(term),   /* body */
                                           ctxt,
                                           env,
                                           tvs); 
      }
      break; 

   
    case omega_true:
    case omega_false:
      result = list1(cons(term,env)); 
      break; 

    case serialize:
      /* (serialize <lc> . <term>) */ 
      //assert(reading_predef); 
      term = cdr(term); 
      result = serialize_interpretations(car(term),
                                         0,    /* don't allow serialize Opaque */
                                         dummy,
                                         cdr(term),
                                         ctxt,
                                         env,
                                         tvs); 
      break; 
   
    case tempserialize:
      /* (tempserialize <lc> . <term>) */ 
      //assert(reading_predef); 
      term = cdr(term); 
      result = serialize_interpretations(car(term),
                                         1,    /* allow serialize Opaque */
                                         dummy,
                                         cdr(term),
                                         ctxt,
                                         env,
                                         tvs); 
      break; 
   
    case unserialize:
      /* (unserialize <lc> . <term>) */ 
      //assert(reading_predef); 
      term = cdr(term); 
      result = unserialize_interpretations(car(term),
                                           0,  /* don't allow serialize Opaque */
                                           dummy,
                                           cdr(term),
                                           ctxt,
                                           env,
                                           tvs); 
      break; 

    case tempunserialize:
      /* (tempunserialize <lc> . <term>) */ 
      //assert(reading_predef); 
      term = cdr(term); 
      result = unserialize_interpretations(car(term),
                                           1,  /* allow serialize Opaque */
                                           dummy,
                                           cdr(term),
                                           ctxt,
                                           env,
                                           tvs); 
      break; 

    case vcopy:
      /* (vcopy <lc> <Word32 n> . <init>) */ 
      term = cdr(term); 
      result = vcopy_interpretations(car(term),     // lc
                                     second(term),  // n 
                                     cdr2(term),    // init
                                     ctxt,
                                     env,
                                     tvs); 
      break;
   
   
    case bit_width:
      /* (bit_width <lc> . <type>) */ 
      term = cdr(term); 
      result = bit_width_interpretations(car(term),  // lc
                                         cdr(term),  // type
                                         ctxt,
                                         env,
                                         tvs); 
      break; 
   

    case type_desc:
      /* (type_desc <lc> . type) */ 
      term = cdr(term);
      result = type_desc_interpretations(car(term),   // lc
                                         cdr(term),   // type
                                         ctxt,
                                         env,
                                         tvs);
      break; 

   
    case byte_array:   
      /* (byte_array <lc> . <byte array>) */ 
      
      result = list1(cons(term,env)); 
      break; 
   
   
    case definition_of_type:
      /* (definition_of_type <lc> . <term>)    the term must be of type String */ 
      term = cdr(term); 
      result = definition_of_type_interpretations(car(term),   // lc
                                                  cdr(term),   // name of type 
                                                  ctxt,
                                                  env,
                                                  tvs); 
      break; 
   
    case definition_of_term:
      /* (definition_of_type <lc> <term> . <term>)    
                    the first term must be of type AnubisType, the second one of type String */ 
      term = cdr(term); 
      result = definition_of_term_interpretations(car(term),         // lc
                                                  second(term),      // type (of type AnubisType)
                                                  cdr2(term),        // name of term (of type String)
                                                  ctxt,
                                                  env,
                                                  tvs); 
      break; 
   
    case __line__:   /* (__line__ . <lisp integer>) */
    case __file__:   /* (__file__ . <lisp string>) */
    case __dir__:
    result = list1(cons(term,env));
    break; 
    
    
    default: 
      internal_error("Unknown term to interpret",term); 
    }

  return result; 
}







/*------------ Computing the interpretations of a symbol -----------------------*/  
Expr symbol_micro_interpretations(Expr lc, 
                                  Expr ttype,   /* required type for the symbol */ 
                                  Expr name,    /* name of symbol */
                                  Expr mctxt,    /* micro context */
                                  Expr micro_depth, /* depth of micro context within stack */ 
                                  Expr env)     /* environment for type unkowns */ 
{
  int i = 0;      /* used to compute depth of symbol in context */ 

assert(consp(lc)); 

  /* try to find the symbol in the micro context (apply strong preemption principle) */ 
  while (consp(mctxt))  /* context walk */ 
    {
   
      /* mctxt = ((<symbol> . <type>) ...) */ 
      if (name == car(car(mctxt)))
        {
          /* The symbol has been found in the context. It is micro-local. By
             strong preemption principle, its required type must match
             the type found in the context. This updates the
             environment. */ 

          /* TODO: match types here (this may produce an error message) */

          return list1(cons(mcons5(micro_local,       /* the symbol is micro_local */
                                   name,              /* name of local symbol */
                                   micro_depth,       /* depth of micro context */ 
                                   new_integer(i),    /* depth within micro context */
                                   cdr(car(mctxt))),  /* type found for the symbol */
                            env));        /* environment is unchanged */

        }
      i++;                    /* next depth */ 
      mctxt = cdr(mctxt);     /* sequel of micro-context */ 
    }

  /* otherwise, the symbol must be defined globally */ 
  return operation_interpretations(lc,
                                   dummy,   /* required type for the symbol */
                                   name,    /* name of global symbol */
                                   env);
}

   
   
   
Expr symbol_interpretations(Expr lc, 
       Expr ttype,   /* required type for the symbol */ 
       Expr name,    /* name of symbol */
       Expr ctxt,    /* local context */
       Expr env)     /* environment for type unkowns */ 
{
  int i = 0;      /* used to compute depth of symbol in context */ 
   
  assert(consp(lc)); 

  if (name == pdstr__)
    {
      err_line_col(lc,"E115",
                   msgtext_symbol___not_usable[0]);
      return nil; 
    }
   
  /* try to find the symbol in the context (apply strong preemption principle) */ 
  while (consp(ctxt))  /* context walk */ 
    {
   
      /* ctxt = ((<symbol> . <type>) ...)   or 
         ctxt = ((f_micro_ctxt <fname> <ftype> (<symbol> . <type>) ...) ...) */ 
      if (car(car(ctxt)) == f_micro_ctxt)
        {
          if (name == second(car(ctxt)))
            /* the name is that of the recursive closure created by: '|-name->' */
            {
              return list1(cons(mcons4(local,
                                       name,               /* fname */ 
                                       new_integer(i),
                                       third(car(ctxt))),  /* ftype */ 
                                env)); 
            }
          else
            {
              return symbol_micro_interpretations(lc,ttype,name,cdr3(car(ctxt)),new_integer(i),env);
            }
        }
      else if (name == car(car(ctxt)))
        {
          /* The symbol has been found in the context. It is local. By
             strong preemption principle, its required type must match
             the type found in the context. This updates the
             environment. */ 

          /* TODO: match types here (this may produce an error message) */

          return list1(cons(mcons4(local,             /* the symbol is local */
                                   name,              /* name of local symbol */
                                   new_integer(i),    /* depth in context */
                                   cdr(car(ctxt))),   /* type found for the symbol */
                            env));        /* environment is unchanged */

        }
      i++;                    /* next depth */ 
      ctxt = cdr(ctxt);       /* sequel of context */ 
    }

  /* otherwise, the symbol must be defined globally */ 
  return operation_interpretations(lc,
                                   dummy,   /* required type for the symbol */
                                   name,    /* name of global symbol */
                                   env);
}






   
/* checking if a type refers to Opaque(...) */
static int has_Opaque(Expr type)
{
 begin:
  if (consp(type))
    {
      if (is_struct_ptr_type(type)) 
        {
          if (consp(cdr(type)))
            {
              assert(car(cdr(type)) == new_integer(C_struct_id(pdstr_Opaque)));
              return 1; 
            }
          else 
            return 0; 
        }
      else if (has_Opaque(car(type)))
        return 1;
      else 
        type = cdr(type); 
      goto begin; 
    }
  else
    return 0; 
}
   





/* Computing interpretations  of explicitly typed term  (T)t, where T  is a type, and  t a
   term:

   We verify first  that the given type is  known (up to unknows it may  contain). Then we
   verify that it matches the required type.

   Then we compute all  interpretations of t with that type. This  may still yield several
   interpretations.  If  no interpretation  is  compatible with  T,  an  error message  is
   sent. */ 
Expr of_type_interpretations(Expr lc, 
                             Expr ttype,   /* required type */
                             Expr type,    /* T */ 
                             Expr term,    /* t */ 
                             Expr ctxt, 
                             Expr env, 
                             Expr tvs)
{
  Expr term_ints, term_ints_before, term_int_head, term_int_env, term_type, new_env, result; 

  /* checking the type */ 
  if (!check_explicit_type(lc,type,tvs)) return nil; 

  /* get interpretations of term for that type. */ 
  term_ints = term_ints_before = term_interpretations(dummy,term,ctxt,env,tvs,0); 
  if (term_ints == nil) return nil; 

  /* keep only those interpretations whose type match. */ 
  result = nil;
  while (consp(term_ints))
    {
      term_int_head = car(car(term_ints)); 
      term_int_env = cdr(car(term_ints)); 
      term_ints = cdr(term_ints); 
      term_type = type_from_interpretation(term_int_head,term_int_env);

      /* unify given type with type of term */ 
      new_env = unify(type,
                      nil, 
                      term_type, 
                      term_int_env); 

      if (new_env != not_unifiable) 
        result = cons(cons(term_int_head,new_env),result);  // 10/08/2005 
      //result = cons(cons(mcons3(i_of_type,type,term_int_head),new_env),result);  
    }

   
  /* if no interpretation left, error message */ 
  if (result == nil)
    {
      if (show_errors)
        {
          err_line_col(lc,"E017",
                       msgtext_incompatible_explicit_type[0]); 
          show_type(errfile,type,env);
          fprintf(errfile,"%s",
                  msgtext_incompatible_explicit_type_2[0]); 
          show_interpretations_types(errfile,term_ints_before);
          fprintf(errfile,"\n");   
        }
      return nil; 
    }
   
  /* maybe cannot unserialize Opaque */
    {
      Expr aux = result; 
      while (consp(aux))
        {
          Expr term_int = car(aux); 
          aux = cdr(aux); 
   
          if (consp(car(term_int)) && car(car(term_int)) == unserialize)
            {
              if (has_Opaque(type_from_interpretation(car(term_int),cdr(term_int))))
                {
                  err_line_col(lc,"E117",
                               msgtext_Opaque_not_serializable[0]);
                  return nil; 
                }
            }
        }
    }   
   
  /* otherwise, return list of interpretations of term */ 
  return result; 
}


#ifdef toto
Expr type_rep_interpretations(Expr lc,  
         Expr type, 
         Expr ctxt, 
         Expr env, 
         Expr tvs)
{
  /* check type */ 
  if (!check_explicit_type(lc,type,tvs)) return nil; 

  /* there is only one interpretation */ 
  return list1(cons(mcons3(type_rep,lc,type),env)); 
}
#endif




/* Computing interpretations of the function of an applicative term */ 
Expr func_interpretations(Expr lc,  
                          Expr ttype, /* required type */
                          Expr op,    /* <term> */
                          int arity,  /* required arity */ 
                          Expr ctxt,
                          Expr env,
                          Expr tvs)
{
  Expr ints = term_interpretations(dummy,op,ctxt,env,tvs,0); 
  Expr result = nil; 
  Expr type, aux; 

  if (ints == nil) return nil; 

  /* we  must  keep  only  those  interpretations  which are  functional  with  the  right
     arity. Note: multiple variables are function of arity 1 at that point.  */
  aux = ints;   
   
  while (consp(aux))
    {
      type = type_from_interpretation(car(car(aux)),cdr(car(aux))); 
      soft_dereference_type(type,cdr(car(aux)));
      if (consp(type) && 
            (       
              ((car(type) == functype) && length(second(type)) == arity) ||
              ((car(type) == type_MVar) && arity == 1) 
             )
          )
        {
          result = cons(car(aux),result);
        }
      /* else do nothing */
   
      aux = cdr(aux); 
    }

  /* if no interpretation left, warn */
  if (result == nil)
    {
      if (show_errors)
        {
          err_line_col(lc,"E018", str_format(
                                             msgtext_not_a_function_of_arity[0],
                                             arity));
          show_interpretations_types(errfile,ints); 
        }
    }

  return result; 
}

   
   
Expr get_lambda_type(Expr lc,
                     Expr body,
                     Expr mctxt) 
{
  /* mctxt = ((<sym> . <type>)...) */ 
  Expr source_types = nil;
  while(consp(mctxt))
    {
      source_types = cons(cdr(car(mctxt)),source_types);
      mctxt = cdr(mctxt); 
    }
  source_types = reverse(source_types); 
  return mcons3(functype,source_types,fresh_unknown());
}

   

   
Expr lambda_interpretations         (Expr lc,  
                                     Expr ttype, 
                                     Expr fname, 
                                     Expr args,
                                     Expr body,
                                     Expr ctxt, 
                                     Expr env, 
                                     Expr tvs)
{
  Expr aux, bints, result, mctxt, lctxt, ftype; 
   
  /* check that Ti's are correct types, and construct the context of |-> declared symbols */ 
  aux = args;
  lctxt = nil; 
  while consp(aux)
    {
      if (!check_explicit_type(lc,car(car(aux)),tvs)) return nil; 
      lctxt = cons(cons(cdr(car(aux)),car(car(aux))),lctxt); 
      /* if preemption forbidden check argument's names against 'ctxt'. */
      if (no_preemption)
        {
          Expr hidden_entries = get_hidden_entries(cdr(car(aux)),ctxt);
          if (hidden_entries != nil)
            {
              if (show_errors)
                {
                  warn_line_col(lc,"W019",
                               msgtext_hidden_variables[0]); 
                  show_context(errfile,hidden_entries,env); 
                }
              //return nil; 
            }
        }
   
      aux = cdr(aux); 
    }
  lctxt = reverse(lctxt); 

   
  /* construct the micro_context */
  mctxt = get_micro_ctxt(lc,body,fname,args,ctxt);  
  /* at that point: mctxt = ((<sym> . <type>)...) */ 
   
  /* construct type of function */ 
  ftype = get_lambda_type(lc,body,lctxt); 
  /* ftype = (functype <types> . <type>) 
   Actually, the target type is an unknown at that point. */ 
   
  /* Lisp syntax: (f_micro_ctxt <fname> <ftype> (<sym> . <type>) ...) */
  mctxt = mcons4(f_micro_ctxt,fname,ftype,mctxt); 
  /* at this point: mctxt = (f_micro_ctxt fname ftype (<sym> . <type>)...) */ 
   
  /* interpret body */ 
  bints = term_interpretations(dummy,
                               body,
                               append(lctxt,
                                      list1(mctxt)),
                               env,
                               tvs,
                               0); 
   
   
  if (bints == nil) return nil; 
   
  /* For each interpretation 'body_interpretation' of  the body we construct the following
     interpretation of the function:
   
       (closure <lc> mctxt args . body_interpretation)
   
   For each one we unify the target type of the function with the type of the body. 
  */
   
  result = nil; 
  while (consp(bints))
    {
      Expr new_env;
      aux = car(bints); 
      bints = cdr(bints); 
   
      if (unused_fun_name && fname != nil && !member(fname,symbols_in_interp(car(aux))))
      {
        warn_line_col(lc, 
                      "W012",
                      str_format(msgtext_warn_rec_lambda_f_not_used[0],string_content(fname))
                      ); 
      }
   
      new_env = unify(cdr2(ftype),env,
                      type_from_interpretation(car(aux),cdr(aux)),cdr(aux));
   
      if (new_env == not_unifiable)
        {
          if (show_errors)
            {
              err_line_col(lc,"E020", 
                           msgtext_incompatible_lambda_type[0]);
              show_interpretations_types(errfile,bints); 
            }
          return nil; 
        }
   
      result = cons(cons(mcons5(closure,
                                lc,
                                mctxt,
                                lctxt,
                                car(aux)),
                         new_env),
                    result); 
    }
     
  return result; 
}
   

   /* The function below tests if the argument number 'arg_rank' has a type compatible with
      the corresponding place in the function whose interpretation is op_int. 
      'args_ints' is the list of all interpretations of the tuple of all arguments. */

int compatible_arg(Expr   op_int,     // interpretation of a function 
                   int    arg_rank,   // rank of an argument (starting at 0)
                   int    arity, 
                   Expr   args_ints)  // interpretations of the tuple of arguments
{
  Expr rank = new_integer(arg_rank); 
  Expr op_type = type_from_interpretation(car(op_int),cdr(op_int));
  Expr op_arg_type = nth(rank,car(cdr(op_type))); // type of argument of the function
  Expr op_env = cdr(op_int); 
  Expr args_int; 
  Expr arg_type; 
  Expr arg_env; 
  Expr u; 
  while(consp(args_ints))
    {
      args_int = car(args_ints);    // first interpretation of all arguments
      // args_int = ((I1 ... In) . e) 
      arg_env = cdr(args_int); 
      arg_type = type_from_interpretation(nth(rank,car(args_int)),arg_env); 
      u = unify(op_arg_type,op_env,arg_type,arg_env);
      if (u == not_unifiable)
        {
          args_ints = cdr(args_ints);   // do the same for the other interpretation of the arguments
          continue;
        }
      else return 1; 
    }
  return 0; 
}

/* Computing the list of interpretations of an applicative term:

   We have a  required type for the applicative  term. This means that the  target part of
   the type of the function must match that required type.

   Since the arity k  of the function is known, we may  construct a type like: ($1,...,$k)
   -> T, where T  is the required type.  Then, we get all interpretations  of the function
   with that type.

   It must be non empty. If it is empty, an error message, has already been sent.

   Then for  each interpretation found for  the function, we call  the auxiliary procedure
   app_interpretations_1,  to get  all interpretations  of the  applicative term  for that
   particular interpretation of the function.

   Finally, we just have to concatenate these results. */

Expr app_interpretations(Expr lc, 
                         Expr ttype, /* required type */ 
                         Expr op,    /* <term> */ 
                         Expr args,  /* <terms> */ 
                         Expr ctxt, 
                         Expr env, 
                         Expr tvs,
                         int allow_mvar_access) 
{
  Expr op_ints, args_ints, ctxts, result, aux; 
  int arity = length(args); 
  int i;  

  if (car(op) == rec_lambda)
  {
  debug(op); 
  debug(args);   
  }
  
  /* TODO: construct ($1,...,$k) -> ttype, to replace dummy
     below. Keep the list ($1,...,$k) which is the list of required
     types for the operands (in all interpretations of the function !!!) */

  /* get interpretations of function */ 
  op_ints = func_interpretations(lc,dummy,op,arity,ctxt,env,tvs); 
  if (op_ints == nil) return nil; 
   
  /* prepare a list of identical contexts */ 
  ctxts = nil; 
  for (i = 0; i < arity; i++)
    ctxts = cons(ctxt,ctxts); 

  /* get interpretation of arguments */ 
  /* TODO: suppress this. */
  args_ints = tuple_interpretations(dummy,args,ctxts,env,tvs,0); 
   
  if (args_ints == nil) return nil; 
   
  /* collect interpretations of applicative term */ 
  result = nil; 
  aux = op_ints; 
  while (consp(aux))
    {
      /* TODO: transmit ($1,...,$k), args, ctxts, env and tvs,
         instead. app_interpretations_1 has to be rewritten. */ 
      result = append(app_interpretations_1(lc, 
                                            car(car(aux)),
                                            cdr(car(aux)), 
                                            args_ints,
                                            allow_mvar_access),
                      result); 
      aux = cdr(aux); 
    }

  /* this may result in no interpretation at all */ 
  if (result == nil)
    {
      int n = 1; 
   
      if (show_errors)
        {
          err_line_col(lc,"E021",
                       msgtext_incompatible_args[0]);      
          show_interpretations_types(errfile,op_ints); 
          fprintf(errfile,"%s",msgtext_args_interpretations[0]); 
          while (consp(args))
            {
              fprintf(errfile,msgtext_argument_number[0],n++); 
              show_interpretations_types(errfile,
                                         term_interpretations(dummy,car(args),ctxt,env,tvs,0));
              args = cdr(args); 
            }
          fprintf(errfile,"\n"); 
          fprintf(errfile,"%s",msgtext_func_args_table[0]); 
          {
            int i, j; 
            Expr ops = op_ints; 
            fprintf(errfile,"                      "); 
            for (i=9; i<arity; i++) fprintf(errfile," %d", (i+1)/10); 
            fprintf(errfile,"\n    "); 
            for (i=0; i<arity; i++) fprintf(errfile," %d", (i+1)%10); 
            //fprintf(errfile,"\n"); 

            i = 1;   // used to number functions
            while(consp(ops))
              {
                fprintf(errfile,"\n %3d", i); 
                for(j=0;j<arity;j++)
                  {
                      if (compatible_arg(car(ops),j,arity,args_ints)) 
                        fprintf(errfile," ."); 
                      else 
                        fprintf(errfile," ?"); 
                  }
                ops = cdr(ops); i++; 
              }
            fprintf(errfile,"\n\n"); 
          }
        }
    }

  return result; 
}




/* Computing the interpretations of an applicative term, for a given
   interpretation of the operation */ 
Expr app_interpretations_1(Expr lc,  
                           Expr op_int_head,         /* head of interpretation of the function */ 
                           Expr op_int_env,          /* environment of interpretation of the function */
                           Expr args_ints,           /* interpertations of arguments */
                           int allow_mvar_access)
{
  Expr new_env, args_int_heads, args_int_env, args_types, aux, sign, func_type; 
  Expr result = nil; 
      
  /* we have only one interpretation for the function: 

     (op_int_head . op_int_env)

     and a list of tuple interpretations for the arguments:

     args_ints

     We  can  construct at  most  one  interpretation of  the  applicative  term for  each
     interpretation of the tuple of arguments.
   
     There is  also the  particular case in  which the  'function' is actually  a multiple
     dynamic variable.
  */  

  /* we get the signature of the function */    
  func_type = type_from_interpretation(op_int_head,
                                       op_int_env);
  dereference_type(func_type,op_int_env); 
   
  switch(car(func_type))
    {
   
    case functype: 
      sign = signature_from_type(func_type,op_int_env);
      while (consp(args_ints))
        {
          /* do the following for each interpretation of the tuple of arguments */ 
          args_int_heads = car(car(args_ints));   /* tuple of interpretations heads */ 
          args_int_env = cdr(car(args_ints));     /* environment for arguments */
          args_ints = cdr(args_ints);             

          /* collect types of actual arguments from their interpretations */ 
          args_types = nil; 
          aux = args_int_heads; 
          while (consp(aux))
            {
              args_types = cons(type_from_interpretation(car(aux),args_int_env),
                                args_types); 
              aux = cdr(aux); 
            }
          args_types = hard_reverse(args_types); 

          /* unify types of actual arguments with types of formal
             arguments. */

          new_env = unify(args_types,
                          args_int_env,
                          sign,
                          op_int_env); 

          /* if unification succeeds, record the interpretation */ 
          if (new_env != not_unifiable)
            {
              if ((car(op_int_head) == operation) && 
                  ((operations[integer_value(third(op_int_head))].global) & macro_mask))
                { /* operation is macro: perform a delta-reduction. 
                     Note: at this point, 'inline functions' have already be replaced by macros.
                
                     We proceed as follows. At this point, op_int_head has the form: 
                     
                        (operation <lc> <opid> <name> <parms> <type> . <types>)
                        
                   */
               U32 opid = integer_value(third(op_int_head));       
        
                   /* From <opid> we can get the body of the function and its target type. */ 
               Expr body = operations[opid].definition; 
               Expr body_type = operations[opid].target_type; 
               
                   /* We also need the list of types which are the values 
                      (found so far) of the parameters, and the list of original
                      parameters */ 
               Expr    parms_values = fifth(op_int_head);
               Expr  original_parms = operations[opid].parms;        
                     
                   /* The parameters ($T1 ... $Tk) in the body, must be replaced 
                      by their values (U1 ... Uk). We construct the list:
                     
                        already_refreshed = (($T1 . U1) ... ($Tk . Uk))
                        
                   */
                Expr already_refreshed = nil; 
                
                /* A macro or inline function cannot be used before it is defined. */ 
                if (body == no_term)
                {
                  err_line_col(lc,"E119",
                               str_format(msgtext_undefined_macro[0],
                                          string_content(car(operations[opid].names)),
                                          integer_value(operations[opid].line),
                                          string_content(operations[opid].abs_file_path))); 
                  return nil; 
                }
                
                while (consp(original_parms))
                {
                  already_refreshed = cons(cons(car(original_parms),
                                                car(parms_values)),
                                           already_refreshed); 
                  original_parms = cdr(original_parms); 
                  parms_values   = cdr(parms_values);                          
                }
                assert(parms_values == nil);   /* the two lists must have the same length */ 
                
                   /* Refresh the body ('refresh' is defined in 'unknowns.c') */
                body      = refresh(body,&already_refreshed);  
                body_type = refresh(body_type,&already_refreshed); 

                   /* At this point we have a new body with type parameters replaced by correct
                      types (which may still contain unknowns). 
                     
                      The next step is to replace all occurrences of the formal parameters
                      of the function by the interpretations of the arguments within this new body. 
                      We must of course avoid any capture of variable, which may lead to rename
                      some bound variables. Furthermore, all formal parameters must be replaced 
                      in parallel. 
                     
                      We don't have to worry about transitivity of macroness. Indeed, if
                      the macro functions 'f' called here itself calls another macro function
                      'g', the calls to 'g' have already been expanded within the body of 
                      'f'. 
                      
                      Before calling 'interp_replace' (defined in replace.c), we construct the needed 
                      dictionary:
                   */
                   Expr     dict = nil; 
                   Expr     ctxt = operations[opid].ctxt;    /* ((x_1 . T_1) ... (x_k . T_k)) */
                   Expr   values = args_int_heads;           /* (v_1 ... v_k) */ 
                   
                   while (consp(ctxt))
                   {
                     dict = cons(cons(car(car(ctxt)),car(values)),dict); 
                   
                     ctxt = cdr(ctxt); 
                     values = cdr(values); 
                   } 
                   assert(values == nil); 
                   
                   /*  Now, dict has the form ((x_k . v_k) ... (x_1 . v_1)) 
                       where x_i is a formal variable of the inline function, and v_i 
                       the value to be substituted to it. 
                   */ 
                   
                   //debug(dict); 
                   
                  body = interp_replace(body, 
                                        dict,
                                        op_int_head); 
   
                   /* The resulting interpretation head is wrapped into
                   
                      (inline <lc (of call)> 
                              <fname (of source file of inline function)> 
                              <line (of inline function)>  
                              . <interpretation head>) 
                      
                      so that it is possible to generate comment lines within the .sc
                      file indicating where is precisely the code put inline. */ 
                  result = cons(cons(mcons5(macro,
                                            lc,                                /* of call to inline operation */ 
                                            operations[opid].abs_file_path,    /* of inline operation definition */ 
                                            operations[opid].line,             /* of inline operation definition */ 
                                            body),
                                     new_env),
                                result); 
                  
                  //debug(result); 
                  
                }
              else  
                {
                  result = cons(cons(mcons4(app,
                                            lc,
                                            op_int_head,
                                            args_int_heads),
                                     new_env),
                                result); 
                }
            }   
        }
      break; 

    case type_MVar:
      {
        Expr word32_arg_ints; 

        if (!allow_mvar_access)
          {
            err_line_col(lc,"E0116",
                         msgtext_mvar_access[0]);
            return nil; 
          }
   
        /* keep only those interpretations of arguments which have just one argument of type
           Word32. */ 
        aux = args_ints; 
        word32_arg_ints = nil; 
        while (consp(aux))
          {
            if (
                (length(car(car(aux))) == 1) &&
                (type_from_interpretation(car(car(car(aux))),cdr(car(aux))) == pdstr_Word32)
                )
              word32_arg_ints = cons(car(aux),word32_arg_ints); 
            aux = cdr(aux); 
          }
   
        /* check if there are still interpretations */ 
        if (word32_arg_ints == nil)
          {
            if (show_errors)
              {
                err_line_col(lc,"E022",
                             msgtext_mvar_args[0]); 
                show_tuple_interpretations_types(errfile,args_ints); 
              }
            return nil; 
          }

        /* for  the  interpretation  (h  .  e)   of  the  multiple  variable  and  for  each
           interpretation (ha . ea) of the argument, construct the interpretation:
   
           ((mvar_access h . ha) . ea)
   
        */ 
        aux = word32_arg_ints; 
        while (consp(aux))
          {
            Expr new_env = join_envs(op_int_env,cdr(car(aux))); 
            if (new_env != not_unifiable)
              result = cons(cons(mcons3(mvar_access,op_int_head,car(car(car(aux)))),
                                 new_env),
                            result);
            aux = cdr(aux); 
          }
   
        /* check that there are still interpretations */ 
        if (result == nil)
          {
            if (show_errors)
              {
                err_line_col(lc,"E023",
                             msgtext_mvar_incompatible_arg[0]); 
                show_interpretations(errfile,word32_arg_ints); 
              }
          }
      }
      break; 
   
    default: internal_error("Cannot apply to arguments",func_type); break; 
   
    }
  return result; 
}





Expr with_interpretations(Expr keyword,  /* with */ 
     Expr lc, 
     Expr ttype, 
     Expr symbol,     
     Expr value,    
     Expr body,    
     Expr ctxt, 
     Expr env,
     Expr tvs)
{
  Expr value_ints, body_ints, result; 

  if (no_preemption)
    {
      Expr hidden_entries = get_hidden_entries(symbol,ctxt); 
      if (hidden_entries != nil)
        {
          if (show_errors)
            {
              warn_line_col(lc,"W024",
                           msgtext_hidden_variables[0]); 
              show_context(errfile,hidden_entries,env); 
            }
          //return nil; 
        }
    }
   
  /* we ask for one and only one interpretation for the value, with
     possibly unknown types */ 
  value_ints = term_interpretations(dummy,
                                    value,
                                    ctxt,
                                    env,
                                    tvs,
                                    0);
  if (value_ints == nil) return nil; 

  if (length(value_ints) >= 2)
    {
      if (show_errors)
        {
          err_line_col(lc,"E025", 
                       str_format(keyword == with ? msgtext_ambiguous_local_def[0]
                                  : msgtext_ambiguous_local_init[0],
                                  string_content(symbol))); 
          show_simple_ambiguity(errfile,value_ints);
          //show_interpretations_types(errfile,value_ints); 
        }
      return nil; 
    }

  /* expand the context for the interpretation of the body */ 
  ctxt =
    cons(cons(symbol,type_from_interpretation(car(car(value_ints)),cdr(car(value_ints)))),
         ctxt);

  /* interpret the body */ 
  body_ints = term_interpretations(dummy,
                                   body,
       ctxt,  /* new context */
       cdr(car(value_ints)),  /* new environment */ 
                                   tvs,0); 
  if (body_ints == nil) return nil; 

  /* construct the resulting interpretations list. Each interpretation
     head has the form: 

         (with <lc> <symbol> <int head> . <int head>) 

  */ 
  result = nil; 
  while (consp(body_ints))
    {
      result =
 cons(cons(mcons5(keyword,
    lc,
    symbol,
    car(car(value_ints)),
    car(car(body_ints))),
    cdr(car(body_ints))),
      result); 
      body_ints = cdr(body_ints); 
    }

  return result; 
}





/* test_interpertation returns either nil, or the unique interpretation
   of a term supposed to be the test of a conditional. */ 
static Expr test_interpretation(Expr lc,  
                                Expr test,
                                Expr ctxt,
                                Expr env,
                                Expr tvs,
                                Expr *already_refreshed_addr)
{
  Expr test_ints, aux, aux2, aux3, test_int_head, test_type; 
  struct Type_struct *tt; 
  int i; 
   
  /* interpret test */ 
  /* TODO: replace dummy by a fresh unknown below. */ 
  test_ints = term_interpretations(dummy,test,ctxt,env,tvs,0); 
  if (test_ints == nil) return nil; 
   
  /* drop all interpretations whose type is not a sum (alias 'defined') type */ 
  aux = test_ints; 
  aux2 = nil; 
  while (consp(aux))
    {
      aux3 = type_from_interpretation(car(car(aux)),cdr(car(aux)));
      if (is_sum_type(aux3,cdr(car(aux))))
        aux2 = cons(car(aux),aux2); 
      aux = cdr(aux); 
    }
  if (aux2 == nil)
    {
      /* test has only non sum interpretations */ 
      if (show_errors)
        {
          err_line_col(lc,"E026",
                       msgtext_test_is_not_a_sum[0]); 
          show_interpretations_types(errfile,test_ints); 
        }
      return nil; 
    }

  /* test should not have several interpretations */ 
  if ((i = length(test_ints)) >= 2)
    {
      if (show_errors)
        {
          err_line_col(lc,"E027", str_format(
                                             msgtext_test_has_several_interpretations[0],
                                             i)); 
          show_simple_ambiguity(errfile,test_ints); 
          fprintf(errfile,"\n"); 
        }
      return nil; 
    }

  /* test now has one interpretation (which may be still ambiguous,
     because we did not check for unknowns; we don't need to) */ 
  test_int_head = car(car(test_ints));
  env = cdr(car(test_ints)); 

  /* get the type of test */ 
  test_type = type_from_interpretation(test_int_head,env); 

  /* dereference test type */ 
  while (is_unknown(test_type))
    {
      aux = assoc(test_type,env); 
      if (aux == key_not_found)
        break; 
      else
        test_type = aux; 
    }
 
  /* type of test cannot be unknown */ 
  if (is_unknown(test_type))
    {
      if (show_errors)
        {
          err_line_col(lc,"E028",
                       msgtext_test_type_unknown[0]); 
        }
      return nil; 
    }

  /* type of test cannot be a parameter: $T */ 
  if (is_user_type_variable(test_type))
    {
      if (show_errors)
        {
          err_line_col(lc,"E029", str_format(
                                             msgtext_test_type_is_parameter[0],
                                             utvar_name(test_type))); 
        }
      return nil; 
    }

  /* get test type description */ 
  tt = get_type_description(test_type,lc);  
  if (tt == NULL) return nil; 

  /* check that type is complete (all alternatives defined) */ 
  if (!(tt->completed))
    {
      if (show_errors)
        {
          err_line_col(lc,"E030",
                       msgtext_test_type_not_complete[0]); 
        }
      return nil; 
    }
   
  /* unify parms in tt, with operands of type of test */ 
  if (consp(test_type))
    {
      env = unify(refresh(tt->parms,already_refreshed_addr),
                  nil, 
                  cdr(cdr(test_type)),
                  env); 
      assert(env != not_unifiable); 
    }

  /* return the unique interpretation */ 
  return cons(test_int_head,env); 
}





/* Computing the interpretations of a conditional:

   First, we compute  the interpretations of the test with an  unknown required type. This
   must  lead to  one  and only  one  interpretation, whose  type  may eventually  contain
   unknowns.

   The type of the test must be such that its alternatives are known. In other words, this
   type cannot be  an unknown or a  user type variable. It must  be a type name  or a type
   scheme  name.  In  the case  of  a  type  scheme  name,  the operands  may  still  have
   unknowns. This  is not a problem,  since the alternatives of  the type of  the test are
   well defined.

   Next, all the cases must match one alternative and only one.

   We prepare contexts  for bodies interpretations of all the cases.  The required type is
   also the required type for all the bodies.

   We interpret the bodies as a tuple whose list of required types is (T,...,T) where T is
   the required type.

   Finally, we construct the list of interpretations of the conditional. */ 
   
Expr cond_interpretations(Expr lc,  
                          Expr ttype,
                          Expr test, 
                          Expr clauses, 
                          Expr ctxt, 
                          Expr env, 
                          Expr tvs)
{
  Expr test_int_head, test_type, result, aux, alts;
  int nclauses, nalt, i, j, k; 
  Expr clauses_heads, ctxts, bodies, bodies_ints, clauses_int, new_seg; 
  Expr aux2, aux3; 
  Expr already_refreshed = nil; 
   
  /* get unique interpretation of test */ 
  aux = test_interpretation(lc,test,ctxt,env,tvs,&already_refreshed); 
  if (aux == nil) return nil; 

  /* extract informations */ 
  test_int_head = car(aux); 
  env = cdr(aux); 
  test_type = type_from_interpretation(test_int_head,env); 

  /* get alternatives of type of test */ 
  alts = get_alts(test_type,env,lc); 

  /* alts have been obtained  from the type description of the type  of the test. alts has
     the form:
   
     (
     ((<sym>...) (<type> . <sym name>) ...)
     ...
     )

     The types in alts should be unified with the types in test_type.

  */ 

      
  /* check number of clauses */ 
  nalt = length(alts); 
  nclauses = length(clauses); 
  if (nclauses < nalt)
    {
      if (show_errors)
        {
          err_line_col(lc,"E031", str_format(
                                             msgtext_wrong_number_of_cases[0],
                                             nclauses,
                                             nalt)); 
          //show_type(errfile,test_type,env); 
          fprintf(errfile,"\n"); 
          show_alts(errfile,alts,env); 
          fprintf(errfile,"\n"); 
        }
      return nil; 
    }
   
  if (nclauses > nalt)
    {
      Expr new_conditional; 
   
      new_conditional = expand_conditional(lc, 
                                           alts, 
                                           test, 
                                           clauses, 
                                           env); 
   
      if (!consp(new_conditional))
        return nil; 
   
      return cond_interpretations(lc,
                                  ttype,
                                  test,
                                  cdr3(new_conditional),
                                  ctxt,
                                  env,
                                  tvs);
    }

  /* if required, check for named (i.e. not "_") unused resurgent symbols 
     This must be done only after the expansion of subcases (done just above). 
  */
  if (unused_resurg)
  {
    Expr cs = clauses; 
    while (consp(cs))
    {
      Expr c = car(cs);                             /* c = ((<name> (<sym> . <type>) ...) <lc> . <head>) */  
      Expr rsyms = cdr(car(c));                     /* rsyms = (<sym> ... (<type>. <sym>) ...) */ 
      Expr body_syms = _symbols_in_term(cdr2(c));   /* list of symbols free in body of case */ 
      while (consp(rsyms))
      {
        Expr sym =       consp(car(rsyms)) ? cdr(car(rsyms)) : car(rsyms);  
        if (    is_string(sym)  
            &&  sym != pdstr__  
            &&  !member(sym, body_syms) 
           )
        {
          warn_line_col(second(c),   /* <lc> in conditional case (position of 'then') */ 
                        "W011",
                        str_format(msgtext_unused_named_resurgent[0],
                                   string_content(sym))); 
        }
        rsyms = cdr(rsyms); 
      }
      cs = cdr(cs); 
    }
  }   
      
  /* check each head of clause against corresponding alternative */ 
  bodies = nil; 
  ctxts = nil; 
  clauses_heads = nil; 
  i = 1; // number of the alternative

   
  while (consp(alts))
    {

      /* car(alts) = ((name ... name) (type . sym) ... (type . sym)) */ 
   
      /* check constructor name */ 
      if (!member(car(car(car(clauses))),
                  car(car(alts))  ))
        {
          assert(is_string(car(car(car(clauses)))));
          if (show_errors)
            {
              err_line_col(lc,"E032", str_format(
                                                 msgtext_bad_case_name[0], 
                                                 i,
                                                 string_content(car(car(car(clauses)))),
                                                 string_content(car(car(car(alts)))))); 
            }
          return nil; 
        }

      /* check constructor arity */ 
      if ((j = length(cdr(car(alts))) ) 
          != (k = length(cdr(car(car(clauses))))))
        {
          if (show_errors)
            {
              err_line_col(lc,"E033", str_format(
                                                 msgtext_wrong_number_of_resurgent_variables[0], 
                                                 i, 
                                                 k,
                                                 j)); 
              show_typed_resurgent_symbols(errfile,car(alts),env); 
              fprintf(errfile,"\n"); 
            }
          return nil; 
        }

      /* record clause head, adding resurgent variables types.
         Each clause head has the form: 

         (<name> <sym> ... <sym>)

         transform it into 

         (<name> (<sym> . <type>) ... (<sym> . <type>))

         For 'else' case, keep 'else_case'. 

      */
      if (car(car(clauses)) == else_case)
        {
          clauses_heads = cons(else_case,clauses_heads); 
        }
      else
        { 
          aux = cdr(car(alts));           /* ((<type> . <destructor name>) ... ) [components of alt] */ 
          aux2 = nil;                     /* ((<resur sym> . <type>) ... ) */ 
          aux3 = cdr(car(car(clauses)));  /* (<var name> ... <var name>) [resurgent symbols] */    
          j = 1; 
          while (consp(aux))
            {
              /* 
                 at this point, car(car(aux)) is the  type of the resurgent variable as it
                 is declared in the type definition.
              */
   
              /* if the resurgent symbol has a  type declared, this type must unify with
                 to the type declared in the type definition. */ 
              if (consp(car(aux3)))
                {
                  Expr drstype = car(car(aux3));   // declared type of resurgent symbol
                  Expr ctype = car(car(aux)); 
                  Expr new_env = unify(drstype,env,ctype,env); 
                  Expr the_symbol = cdr(car(aux3));   // the resurgent symbol itself
   
                  if (!is_string(the_symbol))
                    {
                      if (show_errors)
                        {
                          err_line_col(lc,"E092",
                                       str_format(msgtext_unexpected_subcase[0]));
                        }
                      return nil; 
                    }
   
                  if (is_singleton_name(lc,the_symbol,ctype,env))
                    {
                      if (show_errors)
                        {
                          err_line_col(lc,"E093",
                                       str_format(msgtext_unexpected_subcase2[0],
                                                  string_content(the_symbol)));
                        }
                      return nil; 
                    }
   
                  if (new_env == not_unifiable)
                    {
                      if (show_errors)
                        {
                          err_line_col(lc,"E034", str_format(
                                msgtext_wrong_resurgent_symbol_type_declaration1[0],i,j)); 
                          show_type(errfile,drstype,env); 
                          fprintf(errfile,"%s", 
                                  msgtext_wrong_resurgent_symbol_type_declaration2[0]); 
                          show_type(errfile,ctype,env); 
                          fprintf(errfile,"\n\n"); 
                        }
                      return nil; 
                    }
                  else
                    {
                      env = new_env; 
                    }   
                }
              else
                {
                  Expr the_symbol = car(aux3); 
                  Expr ctype = car(car(aux)); 
   
                  if (!is_string(the_symbol))
                    {
                      if (show_errors)
                        {
                          err_line_col(lc,"E092",
                                       str_format(msgtext_unexpected_subcase[0]));
                        }
                      return nil; 
                    }
   
                  if (is_singleton_name(lc,the_symbol,ctype,env))
                    {
                      if (show_errors)
                        {
                          err_line_col(lc,"E093",
                                       str_format(msgtext_unexpected_subcase2[0],
                                                  string_content(the_symbol)));
                        }
                      return nil; 
                    }
                }
   
   
   
              aux2 = cons(cons(consp(car(aux3)) ? 
                               cdr(car(aux3)) : 
                               car(aux3),
                               car(car(aux))), 

                          aux2); 
              aux = cdr(aux); 
              aux3 = cdr(aux3); 
              j++; 
            }
          aux2 = hard_reverse(aux2); /* ((<resur sym> . <type>) ... ) */ 


          clauses_heads = cons(cons(car(car(car(clauses))),  /* name of case */ 
                                    aux2),                   /* typed resurgent symbols */ 
                               clauses_heads);
        }


      /* construct context for body interpretations */ 
      new_seg = nil; 
      j = 0; 
      aux = cdr(car(car(clauses)));   /* (<var> ... <var>) */ 
      aux2 = cdr(car(alts));          /* ((<type> . <sym>) ... ) */ 
      while (consp(aux))

        {

          Expr sym = consp(car(aux)) ? cdr(car(aux)) : car(aux); 
   
          new_seg = cons(cons(sym,
                              car(car(aux2))),
                         new_seg); 

          if (no_preemption)
            {
              Expr hidden_entries = get_hidden_entries(sym,ctxt);
              if (hidden_entries != nil)
                {
                  if (show_errors)
                    {
                      warn_line_col(lc,"W035",
                                   msgtext_hidden_variables[0]); 
                      show_context(errfile,hidden_entries,env); 
                    }
                  //return nil; 
                }
            }
   
          aux = cdr(aux); 
          aux2 = cdr(aux2); 
          j++; 
        }
      ctxts = cons(rappend(new_seg,ctxt),ctxts); 

      /* record body of clause */ 
      bodies = cons(cdr(cdr(car(clauses))),bodies); 

      /* do for next clause and next alternative */
      clauses = cdr(clauses); 
      alts = cdr(alts); 
      i++; 
    }

  /* put things in the right order */ 
  bodies = hard_reverse(bodies); 
  ctxts = hard_reverse(ctxts); 
  clauses_heads = hard_reverse(clauses_heads); 

  /* interpret the tuple of bodies */ 
  bodies_ints = tuple_interpretations(dummy,bodies,ctxts,env,tvs,1); 
  if (bodies_ints == nil) 
   {
     Expr interps; 
     int i = 1; 
   
     if (show_errors && !errors)
       {
         err_line_col(lc,"E112",
                      msgtext_incompatible_case_bodies_types[0]); 
         while (consp(bodies))
           {
             show_errors = 0; 
             interps = term_interpretations(dummy,car(bodies),car(ctxts),env,tvs,0); 
             show_errors = 1; 
             fprintf(errfile,"\nCase number %d:\n",i); 
             show_interpretations_types(errfile,interps); 
             ctxts = cdr(ctxts); 
             bodies = cdr(bodies); 
             i++;
           }
       }
     return nil;
   }

  /* keep only those tuple interpretations, where the types of the
     elements of the tuple unify together. 
   
  */ 

  //#define checkforsametypeagain
#ifdef checkforsametypeagain
  aux = aux2 = bodies_ints; 
  bodies_ints = nil; 
  while (consp(aux))
    {
      bodies_int_heads = car(car(aux)); 
      bodies_int_env = cdr(car(aux)); 
      aux = cdr(aux); 

      if (consp(bodies_int_heads))
        {
          /* there is at least one case */ 
          first_type =
            type_from_interpretation(car(bodies_int_heads),bodies_int_env); 
          other_heads = cdr(bodies_int_heads); 

          /* unify other's types with type of first body. This makes
             bodies_int_env grow or vanish (if unification fails). */ 
          while (consp(other_heads))
            {
              bodies_int_env = unify(first_type,
                                     nil,
                                     type_from_interpretation(car(other_heads),bodies_int_env),
                                     bodies_int_env); 
              if (bodies_int_env == not_unifiable) break; 
              other_heads = cdr(other_heads); 
            }

          /* if bodies_int_env did not vanish, keep that interpretation
             (with its new environment) */ 
          if (bodies_int_env != not_unifiable)
            {
              bodies_ints = cons(cons(bodies_int_heads,
                                      bodies_int_env),
                                 bodies_ints); 
            }
        }
      else
        {
          /* conditional with zero case */ 
          bodies_ints = cons(cons(nil,bodies_int_env),bodies_ints); 
        }
      /* continue with next interpretation of bodies */
    }
#endif
   
   
  /* If no interpretation left for the set of bodies, warn. */
  /* TODO: no need to keep that, because already warned by
     tuple_interpretations. */ 
  if (bodies_ints == nil)
    {
      if (show_errors)
        {
          int n = 1;
          err_line_col(lc,"E036",
                       msgtext_cannot_interpret_cond_bodies[0]);
          //      show_tuple_interpretations_types(errfile,aux2); 
          while (consp(bodies))
            {
              fprintf(errfile,msgtext_case[0],n++); 
              show_interpretations_types(errfile,
                                         term_interpretations(dummy,car(bodies),car(ctxts),env,tvs,0));
              bodies = cdr(bodies);
              ctxts = cdr(ctxts); 
            }
        }
      return nil; 
    }

  /* Finally, construct interpretations of the conditional. There is
     one for each left interpretation of the tuple of bodies. */ 
  /* TODO: change that to handle 'else' case */ 
  result = nil; 
  while (consp(bodies_ints))
    {
      /* prepare clauses interpretation */ 
      aux = car(car(bodies_ints));   /* interpretation heads of bodies */ 
      aux2 = clauses_heads; 
      clauses_int = nil; 
      while (consp(aux))
        {
          clauses_int = cons(mcons3(car(aux2),       /* head */ 
                                    new_integer(0),  /* lc */ 
                                    car(aux)),       /* body */
                             clauses_int); 
          aux = cdr(aux); 
          aux2 = cdr(aux2); 
        }
      clauses_int = hard_reverse(clauses_int); 

      /* store interpretation of conditional */ 
      if (consp(clauses_int))
        result = cons(cons(mcons4(cond,
                                  lc, 
                                  test_int_head, 
                                  clauses_int), 
                           cdr(car(bodies_ints))),
                      result); 
      else
        result = cons(cons(mcons4(i_no_case_cond,
                                  lc, 
                                  fresh_unknown(), 
                                  test_int_head), 
                           cdr(car(bodies_ints))),
                      result); 

      /* next interpretation */ 
      bodies_ints = cdr(bodies_ints); 
    }
  //if (find(new_string("first_arg"),original_clauses)) 
  return result; 
}







static Expr matched_alternative(Expr alt,
                                Expr constructor_name,
                                Expr typed_resurg,
                                Expr env)
{
  Expr case_types = nil; 
  Expr alt_types = nil; 
  Expr result; 
   
  if (!member(constructor_name,car(alt)))
    return not_unifiable; 

  while (consp(typed_resurg))
    {
      if (consp(car(typed_resurg)))
        case_types = cons(car(car(typed_resurg)),case_types); 
      else
        case_types = cons(fresh_unknown(),case_types); 
      typed_resurg = cdr(typed_resurg); 
    }
  
  alt = cdr(alt); 
  while (consp(alt))
    {
      alt_types = cons(car(car(alt)),alt_types); 
      alt = cdr(alt); 
    }

  result = unify(case_types,env,alt_types,nil); 
  return result; 
}






static int select_alternative(Expr lc, 
                              Expr alts,                /* refreshed alts */ 
                              Expr constructor_name,
                              Expr typed_resurg,
                              Expr *env_addr)
{
  int i = 0; 
  Expr first_alt = nil; 
  Expr other_alts = nil; 
  Expr new_env = nil; 

  /* no alternative found yet */
  while (consp(alts) && first_alt == nil)
    {
      if ((new_env = matched_alternative(car(alts),constructor_name,typed_resurg,*env_addr)) 
          != not_unifiable)
        /* a first alternative has been found */ 
        first_alt = cons(new_integer(i),car(alts)); 
      alts = cdr(alts); 
      i++;
    }

  if (first_alt == nil)
    {
      /* no alternative has been found */ 
      if (show_errors)
        {
          err_line_col(lc,"E037",
                       msgtext_no_alt_match[0]); 
        }
      return -1; 
    }
  else
    {
      /* a first alternative has been found. Try to find others */ 
      while (consp(alts))
        {
          if (matched_alternative(car(alts),constructor_name,typed_resurg,*env_addr) != not_unifiable)
            other_alts = cons(cons(new_integer(i),car(alts)),other_alts); 
          alts = cdr(alts); 
          i++; 
        }
 
      if (other_alts == nil)
        {
          /* there is one and only one matching alternative: success */ 
          *env_addr = new_env;
          return integer_value(car(first_alt)); 
        }
      else
        {
          /* other_alts is ((i . alt) ...) */ 

          /* get all matching alternatives */ 
          other_alts = cons(first_alt,other_alts); 
          if (show_errors)
            {
              err_line_col(lc,"E038",
                           msgtext_several_alt_matches[0]);
              show_alternatives(errfile,other_alts); 
              fprintf(errfile,"%s",msgtext_several_alt_matches_2[0]); 
              fprintf(errfile,"\n"); 
            }
          return -1; 
        }
    }
}





Expr select_cond_interpretations    (Expr lc, 
                                     Expr test,
                                     Expr constructor_name,
                                     Expr typed_resurg,
                                     Expr case_body,
                                     Expr else_term,
                                     Expr ctxt,
                                     Expr env,
                                     Expr tvs)
{
  Expr already_refreshed = nil; 
  Expr aux, test_int_head, test_type, alts, alt, new_ctxt;
  Expr terms_ints, new_seg, result, aux2, type_template;  
//  Expr test_type_name; 
  int i; 


  /* if required check for unused resurgent symbols */ 
  if (unused_resurg)
  {
    Expr rsyms = typed_resurg; 
    Expr body_syms = _symbols_in_term(case_body);
    while (consp(rsyms))
    {
      Expr sym = consp(car(rsyms)) ? cdr(car(rsyms)) : car(rsyms); 
      if ( is_string(sym)  &&  sym != pdstr__  &&  !member(sym, body_syms) )
        {
          warn_line_col(lc,   /* <lc> of conditional */ 
                        "W011",
                        str_format(msgtext_unused_named_resurgent[0],
                                   string_content(sym))); 
        }      
      rsyms = cdr(rsyms);
    } 
  }

  /* get unique interpretation of test */ 
  aux = test_interpretation(lc, test, ctxt, env, tvs, &already_refreshed); 
  if (aux == nil) return nil; 

  /* extract information */ 
  test_int_head = car(aux); 
  env = cdr(aux); 
  test_type = type_from_interpretation(test_int_head,env); 

  /* get name of type of test */ 
//  if (is_string(test_type))
//    test_type_name = test_type; 
//  else if (consp(test_type))
//    test_type_name = second(test_type); 

  /* get refreshed alternatives of type of test */    
  alts = get_alts(test_type,env,lc); 
  aux = refresh(cons(test_type,alts),&already_refreshed); 
  alts = cdr(aux); 
  type_template = car(aux); 

  /* must unify type of test with type template */
  env = unify(type_template,env,test_type,nil); 

  /* we should have at least two alternatives (the user wrote 'else') */ 
  if (cdr(alts) == nil)
    {
      if (show_errors)
        {
          err_line_col(lc,"E039",
                       msgtext_select_cond_with_one_alternative[0]); 
        }
      return nil; 
    }

  /* select the right alternative */
  i = select_alternative(lc,alts,constructor_name,typed_resurg,&env);   /* this may update 'env' */
  if (i == -1) return nil;    /* selection failed */ 

  /* get the alternative */ 
  alt = nth(new_integer(i),alts); 

  /* alt = ((name ...) (type . name) ...) */ 

  /* prepare the context for body interpretation */ 
  new_seg = nil; 
  aux = cdr(alt);          /* ((type . name) ...) */ 
  aux2 = typed_resurg;     /* (name (type . name) ...) */ 
  while(consp(aux))
    {
      Expr sym = consp(car(aux2)) ? cdr(car(aux2)) : car(aux2);
      new_seg = cons(cons(sym,
     car(car(aux))),  /* type of symbol */ 
       new_seg);
   
   
      if (no_preemption)
        {
          Expr hidden_entries = get_hidden_entries(sym,ctxt);
          if (hidden_entries != nil)
            {
              if (show_errors)
                {
                  warn_line_col(lc,"W040",
                               msgtext_hidden_variables[0]); 
                  show_context(errfile,hidden_entries,env); 
                }
              //return nil; 
            }
        }
   
   
   
      aux = cdr(aux); 
      aux2 = cdr(aux2); 
    }
  new_seg = hard_reverse(new_seg); 
  new_ctxt = append(new_seg,ctxt); 

  /* now 'i' is the index of the right alternative. Interpret body of case 
     and else term. */ 

  terms_ints = tuple_interpretations(list2(dummy,dummy), 
                                     list2(case_body,else_term),
                                     list2(new_ctxt,ctxt),
                                     env,
                                     tvs,
                                     0/*1*/);      /* must have same type */ 
  if (terms_ints == nil) return nil; 

  /* each interpretation found has the form ((<head1> <head2>) . <env>). 
     Keep only those with type(<head1>) = type(<head2>). */ 

  aux = aux2 = terms_ints; 
  terms_ints = nil; 
  while (consp(aux))
    {
      Expr env2 = unify(type_from_interpretation(car(car(car(aux))),cdr(car(aux))),
   cdr(car(aux)),
   type_from_interpretation(second(car(car(aux))),cdr(car(aux))),
   nil);
      if (env2 != not_unifiable)
 terms_ints = cons(cons(car(car(aux)),env2),terms_ints); 
      aux = cdr(aux);
    }
  if (terms_ints == nil)
    {
      if (show_errors)
        {
          err_line_col(lc,"E041",
                       msgtext_select_cond_incompatible_types[0]); 
          // show_tuple_interpretations_types(errfile,aux2); 
          fprintf(errfile,"%s",msgtext_case_term[0]); 
          show_interpretations_types(errfile,
                                     term_interpretations(dummy,case_body,new_ctxt,env,tvs,0));
          fprintf(errfile,"%s",msgtext_else_term[0]); 
          show_interpretations_types(errfile,
                                     term_interpretations(dummy,else_term,ctxt,env,tvs,0));
        }
      return nil; 
    }

     /* For each one, construct:

   ((select_cond_interp <lc> <test head> i <clause head> <head1> . <head2>) . <env>)

   where <clause head> is

    (<sym> (<type> . <resurgent sym>) ...)

  */
  result = nil; 
  while (consp(terms_ints))
    {
      /* compute <clause head> */
      aux = cons(car(car(alt)),    /* name of alternative */ 
   new_seg); 

      result = cons(cons(mcons7(select_cond_interp,
    lc,
    test_int_head,
    new_integer(i), 
    aux,
    car(car(car(terms_ints))),
    second(car(car(terms_ints)))),
    cdr(car(terms_ints))),
      result); 
      terms_ints = cdr(terms_ints); 
    }

  return result; 
}







/* the following selects, amongh a list of interpretations, those
   which are acceptable as a readable connection (including local 
   variables) */ 
Expr select_readable_connection_interpretations(Expr lc,
                                                Expr conn_ints,
                                                Expr env)
{
  Expr result, aux; 

  aux = conn_ints; 
  result = nil; 
  while (consp(aux))
    {
      if (
           (consp(car(car(aux))) && (car(car(car(aux))) == mvar_access)) ||
           (is_readable_address_type(type_from_interpretation(car(car(aux)),cdr(car(aux))), cdr(car(aux)) )) 
         )
 result = cons(car(aux),result); 
      aux = cdr(aux); 
    }

  /* If no interpretation left, the 'conn' term may not be interpreted
     as a connection. */
  if (result == nil)
    {
      if (show_errors)
        {
          err_line_col(lc,"E042",
                       msgtext_cannot_interpret_as_readable_connection[0]); 
          show_interpretations_types(errfile,conn_ints); 
        }
      return nil; 
    }

  return result; 
}





Expr select_writable_connection_interpretations(Expr lc,
                                                Expr conn_ints)
{
  Expr result, aux; 

  aux = conn_ints; 
  result = nil; 
  while (consp(aux))
    {
      if (
          (consp(car(car(aux))) && (car(car(car(aux))) == mvar_access)) ||           
          (is_writable_address_type(type_from_interpretation(car(car(aux)),cdr(car(aux))),cdr(car(aux))))
         )
        result = cons(car(aux),result); 
      aux = cdr(aux); 
    }

  /* If no interpretation left, the 'conn' term may not be interpreted
     as a connection. */
  if (result == nil)
    {
      if (show_errors)
        {
          err_line_col(lc,"E043",
                       msgtext_cannot_interpret_as_writable_connection[0]); 
          show_interpretations_types(errfile,conn_ints); 
        }
      return nil; 
    }

  return result; 
}




Expr select_exchangeable_connection_interpretations(Expr lc,
      Expr conn_ints)
{
  Expr result, aux; 

  aux = conn_ints; 
  result = nil; 
  while (consp(aux))
    {
      Expr conn_type =
 type_from_interpretation(car(car(aux)),cdr(car(aux))); 
      if (is_exchangeable_address_type(conn_type))
 result = cons(car(aux),result); 
      aux = cdr(aux); 
    }

  /* If no interpretation left, the 'conn' term may not be interpreted
     as a connection. */
  if (result == nil)
    {
      if (show_errors)
        {
          err_line_col(lc,"E044",
                       msgtext_cannot_interpret_as_exchangeable_connection[0]); 
          show_interpretations_types(errfile,conn_ints); 
        }
      return nil; 
    }

  return result; 
}





/* Interpreting a 'read' term, i.e. of the form '*t', where t is a
   term which must represent a readable connection or a local variable. 
*/ 
Expr read_interpretations(Expr lc, 
     Expr ttype,
     Expr conn,
     Expr ctxt,
     Expr env,
     Expr tvs)
{
  Expr aux, conn_ints, result; 

  /* conn may arrives in the form of any term. We keep only its
     interpretations whose type is acceptable for a readable connection 
     (including local variables). */ 

  aux = term_interpretations(dummy,conn,ctxt,env,tvs,1); 

  if ((conn_ints = select_readable_connection_interpretations(lc,aux,env)) == nil)
    return nil; 

  /* for each interpretation 'Iconn' of 'conn' we have the
     interpretation '(anb_read lc . Iconn)' of the read term. */ 
  result = nil;
  while (consp(conn_ints))
    {
      result =
 cons(cons(mcons3(anb_read,lc,car(car(conn_ints))),cdr(car(conn_ints))),result);  
      conn_ints = cdr(conn_ints); 
    }

  return result; 
}







Expr write_interpretations(Expr lc, 
      Expr ttype, 
      Expr conn,     /* connection (including local variables) */ 
      Expr value,
      Expr ctxt,
      Expr env,
      Expr tvs)
{
  Expr aux, conn_ints, val_ints, result, aux2; 
  Expr conn_type, val_type, new_env; 

  /* get all interpretations of 'conn' */
  aux = term_interpretations(dummy,conn,ctxt,env,tvs,1); 

  /* keep only those which are acceptable for a writable connection. */
  if ((conn_ints = select_writable_connection_interpretations(lc,aux)) == nil)
    return nil; 

  /* 'aux' is empty */

  /* get all interpretations of the value */
  if ((val_ints = term_interpretations(dummy,value,ctxt,env,tvs,0)) ==
      nil)
    return nil; 

  /* for each interpretation of the connection, we construct the
     corresponding list of interpretations of the write term. */ 
  result = nil; 
  aux = conn_ints; 
  while (consp(aux))   /* for each interpretation of 'conn' */
    {
      aux2 = val_ints; 
      while (consp(aux2))
 {
   conn_type =
     type_from_interpretation(car(car(aux)),cdr(car(aux))); 
   val_type =
     type_from_interpretation(car(car(aux2)),cdr(car(aux2))); 

   soft_dereference_type(conn_type,cdr(car(aux)));

   assert(is_address_type(conn_type,nil)); /* (type_?Addr . T) */

   new_env = unify(cdr(conn_type),
     cdr(car(aux)),
     val_type,
     cdr(car(aux2))); 

   if (new_env != not_unifiable)
     {
       result =
  cons(cons(mcons4(anb_write,
     lc,
     car(car(aux)),
     car(car(aux2))),
     new_env),
       result);
     }

   aux2 = cdr(aux2); /* next interpretation of value */
 }
      aux = cdr(aux);  /* next interpretation of 'conn' */
    }

  if (result == nil)
    {
      if (show_errors)
        {
          err_line_col(lc,"E045",
                       msgtext_incompatible_write_type[0]); 
          show_interpretations_types(errfile,conn_ints); 
          fprintf(errfile,"%s", 
                  msgtext_incompatible_write_type2[0]);
          show_interpretations_types(errfile,val_ints);
        }
    }

  return result; 
}



Expr exchange_interpretations(Expr lc, 
      Expr ttype, 
      Expr conn,     /* connection (including local variables) */ 
      Expr value,
      Expr ctxt,
      Expr env,
      Expr tvs)
{
  Expr aux, conn_ints, val_ints, result, aux2; 
  Expr conn_type, val_type, new_env; 

  /* get all interpretations of 'conn' */
  aux = term_interpretations(dummy,conn,ctxt,env,tvs,1); 

  /* keep only those which are acceptable for a exchangeable connection. */
  if ((conn_ints = select_exchangeable_connection_interpretations(lc,aux)) == nil)
    return nil; 

  /* 'aux' is empty */

  /* get all interpretations of the value */
  if ((val_ints = term_interpretations(dummy,value,ctxt,env,tvs,0)) ==
      nil)
    return nil; 

  /* for each interpretation of the connection, we construct the
     corresponding list of interpretations of the write term. */ 
  result = nil; 
  aux = conn_ints; 
  while (consp(aux))   /* for each interpretation of 'conn' */
    {
      aux2 = val_ints; 
      while (consp(aux2))
 {
   conn_type =
     type_from_interpretation(car(car(aux)),cdr(car(aux))); 
   val_type =
     type_from_interpretation(car(car(aux2)),cdr(car(aux2))); 

   soft_dereference_type(conn_type,cdr(car(aux)));

   assert(is_address_type(conn_type,nil)); /* (type_?Addr . T) */

   new_env = unify(cdr(conn_type),
     cdr(car(aux)),
     val_type,
     cdr(car(aux2))); 

   if (new_env != not_unifiable)
     {
       result =
  cons(cons(mcons4(anb_exchange,
     lc,
     car(car(aux)),
     car(car(aux2))),
     new_env),
       result);
     }

   aux2 = cdr(aux2); /* next interpretation of value */
 }
      aux = cdr(aux);  /* next interpretation of 'conn' */
    }

  if (result == nil)
    {
      if (show_errors)
        {
          err_line_col(lc,"E046",
                       msgtext_incompatible_write_type[0]); 
          show_interpretations_types(errfile,conn_ints); 
          fprintf(errfile,"%s",
                  msgtext_incompatible_write_type2[0]);
          show_interpretations_types(errfile,val_ints);
        }
    }

  return result; 
}







Expr wait_for_interpretations (Expr lc, 
          Expr ttype,
          Expr condition,
                               Expr milliseconds,
          Expr after,
          Expr ctxt,
          Expr env,
          Expr tvs)
{
  Expr cond_ints, bool_cond_ints, after_ints, aux, result, ms_ints, word32_ms_ints; 
   
  /* interpret condition (its type must be 'Bool') */ 
  cond_ints = term_interpretations(dummy,condition,ctxt,env,tvs,0); 
  if (cond_ints == nil) return nil; 

  /* select only those interpretations which are of type 'Bool' */ 
  aux = cond_ints; 
  bool_cond_ints = nil; 
  while (consp(aux))
    {
      if (same_type(type_from_interpretation(car(car(aux)),cdr(car(aux))),
                    cdr(car(aux)),
                    pdstr_Bool,
                    nil))
 bool_cond_ints = cons(car(aux),bool_cond_ints); 
      aux = cdr(aux); 
    }

  /* we should have at least one interpretation of type 'Bool' */ 
  if (bool_cond_ints == nil)
    {
      if (show_errors)
        {
          err_line_col(lc,"E053", 
                       msgtext_wait_condition_not_boolean[0]); 
          show_interpretations_types(errfile,cond_ints); 
        }
      return nil; 
    }

  /* we should not have several interpretations of type 'Bool' */ 
  if (cdr(bool_cond_ints) != nil)
    {
      if (show_errors)
        {
          err_line_col(lc,"E054", 
                       msgtext_wait_condition_ambiguous[0]); 
          show_simple_ambiguity(errfile,bool_cond_ints); 
        }
      return nil; 
    }

  /* update 'env' with the environment of the unique interpretation of 
     the condition */ 
  env = cdr(car(bool_cond_ints)); 
   
  /* do the same for milliseconds */ 

  /* interpret condition (its type must be 'Word32') */ 
  ms_ints = term_interpretations(dummy,milliseconds,ctxt,env,tvs,0); 
  if (ms_ints == nil) return nil; 
   
  /* select only those interpretations which are of type 'Word32' */ 
  aux = ms_ints; 
  word32_ms_ints = nil; 
  while (consp(aux))
    {
      if (type_from_interpretation(car(car(aux)),cdr(car(aux))) == pdstr_Word32)
 word32_ms_ints = cons(car(aux),word32_ms_ints); 
      aux = cdr(aux); 
    }

  /* we should have at least one interpretation of type 'Word32' */ 
  if (word32_ms_ints == nil)
    {
      if (show_errors)
        {
          err_line_col(lc,"E055", 
                       msgtext_wait_milliseconds_not_integer[0]); 
          show_interpretations_types(errfile,ms_ints); 
        }
      return nil; 
    }

  /* we should not have several interpretations of type 'Word32' */ 
  if (cdr(word32_ms_ints) != nil)
    {
      if (show_errors)
        {
          err_line_col(lc,"E056", 
                       msgtext_wait_milliseconds_ambiguous[0]); 
          show_simple_ambiguity(errfile,word32_ms_ints); 
        }
      return nil; 
    }

  /* update 'env' with the environment of the unique interpretation of 
     'milliseconds' */ 
  env = cdr(car(word32_ms_ints));
   
  /* now, we have to interpret the 'after condition' term (in the same context) */ 
  after_ints = term_interpretations(dummy,after,ctxt,env,tvs,0); 
  if (after_ints == nil) return nil; 

  /* finally, we have one interpretation for each interpretation of the
     after term */ 
  result = nil; 
  while (consp(after_ints))
    {
      /* each interpretation head is (wait_for lc Icond Itime . Iafter) */ 
      result = cons(cons(mcons5(wait_for,
    lc,
    car(car(bool_cond_ints)),
                                car(car(word32_ms_ints)),
    car(car(after_ints))),
    cdr(car(after_ints))),
      result); 
      after_ints = cdr(after_ints); 
    }
  return result; 
}




Expr delegate_interpretations (Expr lc, 
                               Expr ttype,
                               Expr delegated,
                               Expr body,
                               Expr ctxt,
                               Expr env,
                               Expr tvs)
{
  /* 'delegate u, v' is correct if 'u' has a unique interpretation of type
     'One', and if 'v' may be interpreted. */ 

  Expr deleg_ints, one_deleg_ints, body_ints, aux, result; 

  /* interpret delegated (its type must be 'One') */ 
  deleg_ints = term_interpretations(dummy,delegated,ctxt,env,tvs,0); 
  if (deleg_ints == nil) return nil; 

  /* select only those interpretations which are of type 'One' */ 
  aux = deleg_ints; 
  one_deleg_ints = nil; 
  while (consp(aux))
    {
      if (type_from_interpretation(car(car(aux)),cdr(car(aux))) == pdstr_One)
        one_deleg_ints = cons(car(aux),one_deleg_ints); 
      aux = cdr(aux); 
    }

  /* we should have at least one interpretation of type 'One' */ 
  if (one_deleg_ints == nil)
    {
      if (show_errors)
        {
          err_line_col(lc,"E057", 
                       msgtext_delegated_not_one[0]); 
          show_interpretations_types(errfile,deleg_ints); 
        }
      return nil; 
    }

  /* we should not have several interpretations of type 'One' */ 
  if (cdr(one_deleg_ints) != nil)
    {
      if (show_errors)
        {
          err_line_col(lc,"E058", 
                       msgtext_delegated_ambiguous[0]); 
          show_simple_ambiguity(errfile,one_deleg_ints); 
        }
      return nil; 
    }

  /* update 'env' with the environment of the unique interpretation of 
     the delegated term */ 
  env = cdr(car(one_deleg_ints)); 

  /* now, we have to interpret the 'body' term (in the same context) */ 
  body_ints = term_interpretations(dummy,body,ctxt,env,tvs,0); 
  if (body_ints == nil) return nil; 

  /* finally, we have one interpretation for each interpretation of the
     body term */ 
  result = nil; 
  while (consp(body_ints))
    {
      /* each interpretation head is (delegate lc Ideleg . Ibody) */ 
      result = cons(cons(mcons4(delegate,
                                lc,
                                car(car(one_deleg_ints)),
                                car(car(body_ints))),
                         cdr(car(body_ints))),
                    result); 
      body_ints = cdr(body_ints); 
    }
  return result; 
}



Expr delegatep_interpretations (Expr lc, 
                                Expr ttype,
                                Expr priority, 
                                Expr delegated,
                                Expr body,
                                Expr ctxt,
                                Expr env,
                                Expr tvs)
{
  /* 'delegate u, v' is correct if 'u' has a unique interpretation of type
     'One', and if 'v' may be interpreted. */ 

  Expr priority_ints, word8_priority_ints, deleg_ints, one_deleg_ints, body_ints, aux, result; 

  /* interpert priority (its type must be Word8) */ 
  priority_ints = term_interpretations(dummy,priority,ctxt,env,tvs,0); 
  if (priority_ints == nil) return nil; 

  /* select only those interpretations which are of type 'Word8' */ 
  aux = priority_ints; 
  word8_priority_ints = nil; 
  while (consp(aux))
  {
    if (type_from_interpretation(car(car(aux)),cdr(car(aux))) == pdstr_Word8)
      word8_priority_ints = cons(car(aux),word8_priority_ints); 
    aux = cdr(aux); 
  }

  /* we should have at least one interpretation of type 'Word8' */ 
  if (word8_priority_ints == nil)
    {
      if (show_errors)
        {
          err_line_col(lc,"E123", 
                       msgtext_priority_not_word8[0]); 
          show_interpretations_types(errfile,priority_ints); 
        }
      return nil; 
    }

  /* we should not have several interpretations of type 'Word8' */ 
  if (cdr(word8_priority_ints) != nil)
    {
      if (show_errors)
        {
          err_line_col(lc,"E124", 
                       msgtext_priority_ambiguous[0]); 
          show_simple_ambiguity(errfile,word8_priority_ints); 
        }
      return nil; 
    }

  /* update 'env' with the environment of the unique interpretation of 
     the delegated term */ 
  env = cdr(car(word8_priority_ints)); 

  /* interpret delegated (its type must be 'One') */ 
  deleg_ints = term_interpretations(dummy,delegated,ctxt,env,tvs,0); 
  if (deleg_ints == nil) return nil; 

  /* select only those interpretations which are of type 'One' */ 
  aux = deleg_ints; 
  one_deleg_ints = nil; 
  while (consp(aux))
    {
      if (type_from_interpretation(car(car(aux)),cdr(car(aux))) == pdstr_One)
        one_deleg_ints = cons(car(aux),one_deleg_ints); 
      aux = cdr(aux); 
    }

  /* we should have at least one interpretation of type 'One' */ 
  if (one_deleg_ints == nil)
    {
      if (show_errors)
        {
          err_line_col(lc,"E057", 
                       msgtext_delegated_not_one[0]); 
          show_interpretations_types(errfile,deleg_ints); 
        }
      return nil; 
    }

  /* we should not have several interpretations of type 'One' */ 
  if (cdr(one_deleg_ints) != nil)
    {
      if (show_errors)
        {
          err_line_col(lc,"E058", 
                       msgtext_delegated_ambiguous[0]); 
          show_simple_ambiguity(errfile,one_deleg_ints); 
        }
      return nil; 
    }

  /* update 'env' with the environment of the unique interpretation of 
     the delegated term */ 
  env = cdr(car(one_deleg_ints)); 

  /* now, we have to interpret the 'body' term (in the same context) */ 
  body_ints = term_interpretations(dummy,body,ctxt,env,tvs,0); 
  if (body_ints == nil) return nil; 

  /* finally, we have one interpretation for each interpretation of the
     body term */ 
  result = nil; 
  while (consp(body_ints))
    {
      /* each interpretation head is (delegatep lc Ipriority Ideleg . Ibody) */ 
      result = cons(cons(mcons5(delegatep,
                                lc,
                                car(car(word8_priority_ints)), 
                                car(car(one_deleg_ints)),
                                car(car(body_ints))),
                         cdr(car(body_ints))),
                    result); 
      body_ints = cdr(body_ints); 
    }
  return result; 
}







#ifdef toto
Expr set_interpretations            (Expr lc,  
         Expr ttype,
         Expr x,
         Expr type,
         Expr body,
         Expr ctxt,
         Expr env,
         Expr tvs)
{
  Expr aux, body_ints, omega_body_ints, result; 

  /* check the declared type */ 
  if (!check_explicit_type(lc,type,tvs)) return nil; 

  /* interpret the body in the right context */ 
  body_ints = term_interpretations(dummy,body,cons(cons(x,type),ctxt),env,tvs,0); 

  /* keep only those interpretations whose type is type_Omega */ 
  aux = body_ints; 
  omega_body_ints = nil; 
  while (consp(aux))
    {
      if (type_from_interpretation(car(car(aux)),cdr(car(aux))) == type_Omega)
 omega_body_ints = cons(car(aux),omega_body_ints); 
      aux = cdr(aux); 
    }

  /* check for ambiguities */ 
  if (omega_body_ints == nil)
    {
      if (show_errors)
        {
          err_line_col(lc,"E059",
                       msgtext_no_omega_interpretation[0]); 
          show_interpretations_types(errfile,body_ints); 
        }
      return nil; 
    }

  if (length(omega_body_ints) > 1)
    {
      if (show_errors)
        {
          err_line_col(lc,"E060",
                       msgtext_several_omega_interpretations[0]);
          show_interpretations_types(errfile,omega_body_ints); 
        }
      return nil; 
    }

  /* construct the list of interpretations */ 
  result = nil; 
  while (consp(omega_body_ints))
    {
      result = cons(cons(mcons5(set,lc,x,type,car(car(omega_body_ints))),
    cdr(car(omega_body_ints))),
      result); 
      omega_body_ints = cdr(omega_body_ints); 
    }

  return result; 
}
#endif

   

Expr serialize_interpretations      (Expr lc, 
                                     int allow_serialize_Opaque, 
                                     Expr ttype,
                                     Expr datum,
                                     Expr ctxt,
                                     Expr env,
                                     Expr tvs)
{
  Expr datum_ints; 

  /* interpret the datum to be serialized */ 
  datum_ints = term_interpretations(dummy,datum,ctxt,env,tvs,0); 
  if (datum_ints == nil) return nil; 

  /*
  if (has_Opaque(type_from_interpretation(car(car(datum_ints)),cdr(car(datum_ints)))))
    {
      if (!allow_serialize_Opaque)
        {
          err_line_col(lc,"E117",
                       msgtext_Opaque_not_serializable[0]);
          return nil; 
        }
    }
  */
   
  /* Here, there  is a problem, because  at that point it  is not possible to  know if the
     type is actually  serializable, because it may contain  parameters. Hence, we content
     ourself with types which may be serializable. If after instanciating unknowns (in the
     compilation phase) the type appear as  non serializable, a message will be sent. This
     is the reason why, we must keep lc in the interpretation.
  */ 

  if (cdr(datum_ints) != nil)
    {
      if (show_errors)
        {
          err_line_col(lc,"E061",
                       msgtext_serialize_ambiguous[0]);
          show_interpretations_types(errfile,datum_ints); 
        }
      return nil; 
    }

  /* now we may construct the interpretations list, which has the form:

        (((serialize lc . head) . env))

  */
  return list1(cons(mcons3(serialize, //allow_serialize_Opaque ? tempserialize : serialize,
      lc,
      car(car(datum_ints))),
      cdr(car(datum_ints)))); 
}




Expr unserialize_interpretations    (Expr lc, 
                                     int allow_serialize_Opaque, 
                                     Expr ttype,
                                     Expr bytes,
                                     Expr ctxt,
                                     Expr env,
                                     Expr tvs)
{
  Expr bytes_ints, aux, byte_array_ints; 

  /* interpret 'bytes'. */ 
  bytes_ints = term_interpretations(dummy,bytes,ctxt,env,tvs,0); 
  if (bytes_ints == nil) return nil; 

  /* keep only those interpretations whose type is 'ByteArray' or 'TempByteArray' */ 
  aux = bytes_ints; 
  byte_array_ints = nil; 
  while (consp(aux))
    {
      Expr new_env; 
      Expr type = type_from_interpretation(car(car(aux)),cdr(car(aux)));

      new_env = unify(type,
                      cdr(car(aux)),
                      type_ByteArray, //allow_serialize_Opaque ? pdstr_TempByteArray : type_ByteArray,
                      nil); 
      if (new_env != not_unifiable)
        byte_array_ints = cons(cons(car(car(aux)),new_env),
                               byte_array_ints);

      aux = cdr(aux); 
    }


  /* 'bytes' must have one and only one interpretation of type 'ByteArray' or 'TempByteArray' */ 
  if (byte_array_ints == nil)
    {
      if (show_errors)
        {
          err_line_col(lc,"E062",
                       //allow_serialize_Opaque ? 
                       //msgtext_no_TempByteArray_interpretation[0] :
                       msgtext_no_ByteArray_interpretation[0]); 
          show_interpretations_types(errfile,bytes_ints); 
        }
      return nil; 
    }

  if (cdr(byte_array_ints) != nil)
    {
      if (show_errors)
        {
          err_line_col(lc,"E063",
                       //allow_serialize_Opaque ? 
                       //msgtext_several_TempByteArray_interpretations[0] :
                       msgtext_several_ByteArray_interpretations[0]); 
          show_interpretations_types(errfile,byte_array_ints); 
        }
      return nil; 
    }
   
  /* now 'bytes'  has one  interpretation of  type 'ByteArray'. However,  the type  of the
     result is not known. Hence, we put a fresh unknown for it. This will be resolved (for
     example) if the 'unserialize' term is explicitly typed.

     The interpretation head has the form:

       (unserialize <lc> <type> . <head>)

     where <lc> is  needed to check the serializability of <type>  later, and where <head>
     is the interpretation head of 'bytes'.
  */ 

  return list1(cons(mcons4(unserialize, //allow_serialize_Opaque ? tempunserialize : unserialize,
      lc,
      fresh_unknown(),
      car(car(byte_array_ints))),
      cdr(car(byte_array_ints)))); 
}



  
   
   
Expr protect_interpretations        (Expr lc, 
                                     Expr target_type, 
                                     Expr term,
                                     Expr ctxt,
                                     Expr env,
                                     Expr tvs)
{
  Expr ints, aux; 
   
  ints = term_interpretations(dummy,term,ctxt,env,tvs,0); 
  if (ints == nil) return nil; 
   
  aux = ints; 
  ints = nil; 
  while (consp(aux))
    {
      ints = cons(cons(mcons3(protect,lc,car(car(aux))),cdr(car(aux))),ints); 
      aux = cdr(aux); 
    }
  return ints; 
}

   

Expr lock_interpretations           (Expr lc, 
                                     Expr target_type, 
                                     Expr lockedfile, 
                                     Expr term,
                                     Expr ctxt,
                                     Expr env,
                                     Expr tvs)
{
  Expr fnints, sfnints, ints, aux, result; 
   
  fnints = term_interpretations(dummy,lockedfile,ctxt,env,tvs,0); 
  if (fnints == nil) return nil; 
   
  /* should have only one interpretation of type String */ 
  aux = fnints; 
  sfnints = nil;
  while (consp(aux))
    {
      if (type_from_interpretation(car(car(aux)),cdr(car(aux))) == type_String)
        sfnints = cons(car(aux),sfnints); 
      aux = cdr(aux); 
    }
  
  /* should have at least one String interpretation */ 
  if (sfnints == nil)
    {
      if (show_errors)
        {
          err_line_col(lc,"E064",
                       msgtext_lock_no_string_interpretation[0]); 
          show_interpretations(errfile,fnints); 
        }
      return nil; 
    }
   
  /* should not have several String interpretations */ 
  if (cdr(sfnints) != nil)
    {
      if (show_errors)
        {
          err_line_col(lc,"E065",
                       msgtext_lock_several_string_interpretations[0]); 
          show_interpretations(errfile,sfnints); 
        }
      return nil; 
    }
   
  /* interpret body of 'lock filename, body' */ 
  ints = term_interpretations(dummy,term,ctxt,cdr(car(sfnints)),tvs,0); 
  if (ints == nil) return nil; 
   
  /* construct resulting interpretations */ 
  result = nil; 
  while (consp(ints))
    {
      result = cons(cons(mcons4(lock,lc,car(car(sfnints)),car(car(ints))),
                         cdr(car(ints))),
                    result); 
      ints = cdr(ints); 
    }
  return result; 
}

   
   
   
   
   
Expr alt_number_interpretations     (Expr lc, 
                                     Expr target_type, 
                                     Expr term,
                                     Expr ctxt,
                                     Expr env,
                                     Expr tvs)
{
  Expr ints, aux, kints ; 
   
  ints = term_interpretations(dummy,term,ctxt,env,tvs,0); 
  if (ints == nil) return nil; 
   
  aux = ints; 
  kints = nil; 
  while (consp(aux))
    {
      if (is_sum_type(type_from_interpretation(car(car(aux)),cdr(car(aux))),cdr(car(aux))))
        kints = cons(cons(mcons3(alt_number,lc,car(car(aux))),cdr(car(aux))),kints); 
      aux = cdr(aux); 
    }
   
  if (kints == nil)
    {
      if (show_errors)
        {
          err_line_col(lc,"E066", 
                       msgtext_alt_number[0]);
          show_interpretations_types(errfile,ints);
        }
      return nil; 
    }
   
  return kints; 
}
   

   
   
   
   
   
   
Expr vcopy_interpretations(Expr lc, 
                           Expr n,
                           Expr init, 
                           Expr ctxt,
                           Expr env,
                           Expr tvs)
{
  Expr n_ints, n_int, init_ints, result;
   
  /* interpret n which must be an 'Word32' */ 
  n_ints = term_interpretations(dummy,n,ctxt,env,tvs,0); 
  if (n_ints == nil) return nil; 

  /* we must have only one interpretation of type Word32 */ 
  n_int = select_unique_interpretation(lc,n_ints,pdstr_Word32,env); 
  if (n_int == nil) return nil; 

  /* update env */
  env = cdr(n_int); 
   
  /* interpret 'init' */ 
  init_ints = term_interpretations(dummy,init,ctxt,env,tvs,0); 
  if (init_ints == nil) return nil; 
   
  /* if  the interpretation of  n is  (hn .  env), for  each interpretation  (hi .  ei) of
     'init', construct the interpretation ((vcopy hn . hi) . ei) */ 
  result = nil; 
  while (consp(init_ints))
    {
      result = cons(cons(mcons3(vcopy,car(n_int),car(car(init_ints))),cdr(car(init_ints))),result); 
      init_ints = cdr(init_ints); 
    }
  return result;    
}
   

   
Expr bit_width_interpretations      (Expr lc, 
                                     Expr type, 
                                     Expr ctxt,
                                     Expr env,
                                     Expr tvs)
{
  /* check the type */ 
  if (!check_explicit_type(lc,type,tvs)) return nil; 
   
  /* make the interpretation */ 
  return list1(cons(cons(bit_width,type),env));
}

   
   
/* Tool: checking if an Expr contains parameters or unknowns (aka type_variables (see compil.h) ) */   
int has_parameters_or_unknowns(Expr e)
{
  if (consp(e))
  {
    return (has_parameters_or_unknowns(car(e)) || has_parameters_or_unknowns(cdr(e))); 
  }
  else if is_type_variable(e) return 1; 
  else return 0; 
}
   

Expr type_desc_interpretations       (Expr lc,   
                                      Expr type,  
                                      Expr ctxt,
                                      Expr env,
                                      Expr tvs)
{
   /* It is not possible to compute the type description at that point, because 
      the type here is always '$T' (search for +++type_desc in 'predef.anubis') 
   
      Hence, despite the fact that the result is a string, the interpretation list is 
      just:  ((type_desc_interp <lc> . <type>) . env)
   */ 
  return list1(cons(mcons3(type_desc_interp,lc,type),env));  
}

   
   
Expr definition_of_type_interpretations   (Expr   lc, 
                                           Expr   type_name,        /* term of type String */ 
                                           Expr   ctxt, 
                                           Expr   env, 
                                           Expr   tvs)
{
  /* interpret type_name and keep only those interpretation which are of type String */
  
  assert(0); 
  return nil; 
  
  
}                                           




Expr definition_of_term_interpretations   (Expr   lc, 
                                           Expr   type_term,      /* term of type AnubisType */ 
                                           Expr   term_name,      /* term of type String */ 
                                           Expr   ctxt, 
                                           Expr   env, 
                                           Expr   tvs)
{
  assert(0); 
  return nil; 
}