package elpi
ELPI - Embeddable λProlog Interpreter
Install
Dune Dependency
Authors
Maintainers
Sources
elpi-3.0.0.tbz
sha256=424e5a4631f5935a1436093b614917210b00259d16700912488ba4cd148115d1
sha512=fa54ce05101fafe905c6db2e5fa7ad79d714ec3b580add4ff711bad37fc9545a58795f69056d62f6c18d8c87d424acc1992ab7fb667652e980d182d4ed80ba16
doc/src/elpi.runtime/runtime_trace_off.ml.html
Source file runtime_trace_off.ml
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
(* elpi: embedded lambda prolog interpreter *) (* license: GNU Lesser General Public License Version 2.1 or later *) (* ------------------------------------------------------------------------- *) open Elpi_util open Elpi_parser module Fmt = Format module F = Ast.Func open Util open Data (* Internal notion of constraint *) type constraint_def = { cdepth : int; prog : prolog_prog [@equal fun _ _ -> true] [@printer (fun fmt _ -> Fmt.fprintf fmt "<prolog_prog>")]; context : clause_src list; conclusion : term; cgid : UUID.t [@trace]; } type 'unification_def stuck_goal_kind += | Constraint of constraint_def let get_suspended_goal blockers = function | Constraint { cdepth; conclusion; context; _ } -> Some { context ; goal = (cdepth, conclusion); blockers } | _ -> None (* Constants *) module C : sig type t = Constants.t module Map = Constants.Map module Set = Constants.Set val show : ?table:symbol_table -> t -> string val pp : ?table:symbol_table -> Fmt.formatter -> t -> unit val mkConst : constant -> term val mkAppL : constant -> term list -> term val fresh_global_constant : unit -> constant * term (* mkinterval d n 0 = [d; ...; d+n-1] *) val mkinterval : int -> int -> int -> term list val dummy : term val table : symbol_table Fork.local_ref end = struct type t = Constants.t let dummy = Data.dummy let table = Fork.new_local { c2s = Constants.Map.empty; c2t = Hashtbl.create 37; frozen_constants = 0; } (* let () = at_exit (fun () -> let open Hashtbl in let s = stats !table.c2t in Array.iter (fun i -> Printf.eprintf "%d\n" i) s.bucket_histogram) *) let show ?(table = !table) n = try (Constants.Map.find n Global_symbols.table.c2s) |> Symbol.get_str with Not_found -> try Constants.Map.find n table.c2s |> Symbol.get_str with Not_found -> if n >= 0 then "c" ^ string_of_int n else "SYMBOL" ^ string_of_int n let pp ?table fmt n = Format.fprintf fmt "%s" (show ?table n) let mkConst x = try Hashtbl.find !table.c2t x with Not_found -> let xx = Const x in Hashtbl.add !table.c2t x xx; xx [@@inline] let fresh_global_constant () = !table.frozen_constants <- !table.frozen_constants - 1; let n = !table.frozen_constants in let xx = Const n in !table.c2s <- Constants.Map.add n (Symbol.make (File (Loc.initial "(chr)")) (F.from_string @@ "frozen-" ^ string_of_int n)) !table.c2s ; Hashtbl.add !table.c2t n xx; n, xx module Map = Constants.Map module Set = Constants.Set (* - negative constants are global names - constants are hashconsed (terms) - we use special constants to represent !, pi, sigma *) (* mkinterval d n 0 = [d; ...; d+n-1] *) let rec mkinterval depth argsno n = if n = argsno then [] else mkConst (n+depth)::mkinterval depth argsno (n+1) ;; let mkAppL c = function | [] -> mkConst c | x::xs -> mkApp c x xs [@@inline] end let mkConst = C.mkConst (* Dereferencing an UVar(r,args) when !!r != dummy requires a function of this kind. The pretty printer needs one but will only be defined later, since we need, for example, beta reduction to implement it *) type 'args deref_fun = ?avoid:uvar_body -> from:int -> to_:int -> 'args -> term -> term module Pp : sig (* Low level printing *) val ppterm : ?pp_ctx:pp_ctx -> ?min_prec:int -> (*depth:*)int -> (*names:*)string list -> argsdepth:int -> env -> Fmt.formatter -> term -> unit (* For user consumption *) val uppterm : ?pp_ctx:pp_ctx -> ?min_prec:int -> (*depth:*)int -> (*names:*)string list -> argsdepth:int -> env -> Fmt.formatter -> term -> unit (* To be assigned later, used to dereference an UVar/AppUVar *) val do_deref : int deref_fun ref val do_app_deref : term list deref_fun ref (* To put it in the solution *) val uv_names : (string IntMap.t * int) Fork.local_ref val pp_oref : ?pp_ctx:pp_ctx -> Format.formatter -> (Util.UUID.t * Obj.t) -> unit val pp_constant : ?pp_ctx:pp_ctx -> Format.formatter -> constant -> unit end = struct (* {{{ *) let do_deref = ref (fun ?avoid ~from ~to_ _ _ -> assert false);; let do_app_deref = ref (fun ?avoid ~from ~to_ _ _ -> assert false);; let uv_names = Fork.new_local (IntMap.empty, 0) let min_prec = Elpi_parser.Parser_config.min_precedence let appl_prec = Elpi_parser.Parser_config.appl_precedence let lam_prec = Elpi_parser.Parser_config.lam_precedence let inf_prec = Elpi_parser.Parser_config.inf_precedence let xppterm ~nice ?(pp_ctx = { Data.uv_names; table = ! C.table }) ?(min_prec=min_prec) depth0 names ~argsdepth env f t = let pp_app f pphd pparg ?pplastarg (hd,args) = if args = [] then pphd f hd else Fmt.fprintf f "@[<hov 1>%a@ %a@]" pphd hd (pplist pparg ?pplastelem:pplastarg " ") args in let ppconstant f c = Fmt.fprintf f "%s" (C.show ~table:pp_ctx.table c) in (* let ppconstant f c = Fmt.fprintf f "%s/%d" (C.show ~table:pp_ctx.table c) c in *) let ppbuiltin f b = Fmt.fprintf f "%s" @@ show_builtin_predicate ~table:pp_ctx.table C.show b in let string_of_uvar_body r = try IntMap.find (uvar_id r) (fst !(pp_ctx.uv_names)) with Not_found -> let m, n = !(pp_ctx.uv_names) in let s = "X" ^ string_of_int n in let n = n + 1 in let m = IntMap.add (uvar_id r) s m in pp_ctx.uv_names := (m,n); s in let rec pp_uvar prec depth vardepth args f r = if !!r == C.dummy then begin let s = string_of_uvar_body r in Fmt.fprintf f "%s%s%s" s (if vardepth=0 then "" else "^" ^ string_of_int vardepth) (if args=0 then "" else "+" ^ string_of_int args) end else if nice then begin aux prec depth f (!do_deref ~from:vardepth ~to_:depth args !!r) end else Fmt.fprintf f "<%s|%a>_%d" (string_of_uvar_body r) (aux min_prec vardepth) !!r vardepth and pp_arg prec depth f n = let name= try List.nth names n with Failure _ -> "A" ^ string_of_int n in if try env.(n) == C.dummy with Invalid_argument _ -> true then Fmt.fprintf f "%s" name else if nice then begin if argsdepth <= depth then let dereffed = !do_deref ~from:argsdepth ~to_:depth 0 env.(n) in aux prec depth f dereffed else (* The instantiated Args live at an higher depth, and that's ok. But if we try to deref we make an imperative mess *) Fmt.fprintf f "≪%a≫_%d" (aux min_prec argsdepth) env.(n) argsdepth end else Fmt.fprintf f "≪%a≫_%d" (aux min_prec argsdepth) env.(n) argsdepth and with_parens prec ?(test=true) myprec h = if test && myprec < prec then (Fmt.fprintf f "(" ; h () ; Fmt.fprintf f ")") else h () and pp_mixfix : type a. int-> int -> _ option -> int -> a -> (Format.formatter -> a -> unit) -> _ = fun depth cprec prec hdlvl p pp_p x xs -> with_parens cprec hdlvl (fun _ -> let open Elpi_lexer_config.Lexer_config in match prec with | Some Infix (*when List.length xs = 1*) -> let sep = Fmt.asprintf " %a " pp_p p in Fmt.fprintf f "@[<hov 1>%a@]" (pplist (aux (hdlvl+1) depth) sep) (x::xs) | Some Infixl when List.length xs = 1 -> Fmt.fprintf f "@[<hov 1>%a@ %a@ %a@]" (aux hdlvl depth) x pp_p p (aux (hdlvl+1) depth) (List.hd xs) | Some Infixr when List.length xs = 1 -> Fmt.fprintf f "@[<hov 1>%a@ %a@ %a@]" (aux (hdlvl+1) depth) x pp_p p (aux hdlvl depth) (List.hd xs) | Some Prefix when xs = [] -> Fmt.fprintf f "@[<hov 1>%a@ %a@]" pp_p p (aux hdlvl depth) x | Some Postfix when xs = [] -> Fmt.fprintf f "@[<hov 1>%a@ %a@]" (aux hdlvl depth) x pp_p p | _ -> pp_app f pp_p (aux inf_prec depth) ~pplastarg:(aux_last inf_prec depth) (p,x::xs)) (* ::(a, ::(b, nil)) --> [a,b] *) and flat_cons_to_list depth acc t = match t with UVar (r,vardepth,terms) when !!r != C.dummy -> flat_cons_to_list depth acc (!do_deref ~from:vardepth ~to_:depth terms !!r) | AppUVar (r,vardepth,terms) when !!r != C.dummy -> flat_cons_to_list depth acc (!do_app_deref ~from:vardepth ~to_:depth terms !!r) | Cons (x,y) -> flat_cons_to_list depth (x::acc) y | _ -> List.rev acc, t and is_lambda depth = function Lam _ -> true | UVar (r,vardepth,terms) when !!r != C.dummy -> is_lambda depth (!do_deref ~from:vardepth ~to_:depth terms !!r) | AppUVar (r,vardepth,terms) when !!r != C.dummy -> is_lambda depth (!do_app_deref ~from:vardepth ~to_:depth terms !!r) | _ -> false and aux_last prec depth f t = if nice then begin match t with Lam _ -> aux min_prec depth f t | UVar (r,vardepth,terms) when !!r != C.dummy -> aux_last prec depth f (!do_deref ~from:vardepth ~to_:depth terms !!r) | AppUVar (r,vardepth,terms) when !!r != C.dummy -> aux_last prec depth f (!do_app_deref ~from:vardepth ~to_:depth terms !!r) | _ -> aux prec depth f t end else aux inf_prec depth f t and aux prec depth f t = match t with | (Cons _ | Nil) -> let prefix,last = flat_cons_to_list depth [] t in Fmt.fprintf f "[" ; pplist ~boxed:true (aux Elpi_parser.Parser_config.comma_precedence depth) ", " f prefix ; if last != Nil then begin Fmt.fprintf f " | " ; aux prec 1 f last end; Fmt.fprintf f "]" | Const s -> ppconstant f s | App (hd,x,xs) -> (try let pprec, hdlvl = Elpi_parser.Parser_config.precedence_of (C.show ~table:pp_ctx.table hd) in pp_mixfix depth prec pprec hdlvl hd ppconstant x xs with Not_found -> let lastarg = List.nth (x::xs) (List.length xs) in let hdlvl = if is_lambda depth lastarg then lam_prec else appl_prec in with_parens prec hdlvl (fun _ -> pp_app f ppconstant (aux inf_prec depth) ~pplastarg:(aux_last inf_prec depth) (hd,x::xs))) | Builtin (Eq,[a;b]) -> let _, hdlvl = Elpi_parser.Parser_config.precedence_of (F.show F.eqf) in with_parens prec hdlvl (fun _ -> Fmt.fprintf f "@[<hov 1>%a@ %a@ %a@]" (aux (hdlvl+1) depth) a F.pp F.eqf (aux (hdlvl+1) depth) b) | Builtin(b,[]) -> Fmt.fprintf f "%a" ppbuiltin b | Builtin(b,x::xs) -> (try let pprec, hdlvl = if b == And then Some Elpi_lexer_config.Lexer_config.Infix, Elpi_parser.Parser_config.comma_precedence else let hd = func_of_builtin_predicate (fun x -> C.show ~table:pp_ctx.table x |> F.from_string) b in Elpi_parser.Parser_config.precedence_of (F.show hd) in pp_mixfix depth prec pprec hdlvl b ppbuiltin x xs with Not_found -> let lastarg = List.nth (x::xs) (List.length xs) in let hdlvl = if is_lambda depth lastarg then lam_prec else appl_prec in with_parens prec hdlvl (fun _ -> pp_app f ppbuiltin (aux inf_prec depth) ~pplastarg:(aux_last inf_prec depth) (b,x::xs))) | UVar (r,vardepth,argsno) when not nice -> let args = List.map destConst (C.mkinterval vardepth argsno 0) in with_parens prec ~test:(args <> []) appl_prec (fun _ -> Fmt.fprintf f "." ; pp_app f (pp_uvar inf_prec depth vardepth 0) ppconstant (r,args)) | UVar (r,vardepth,argsno) when !!r == C.dummy -> let diff = vardepth - depth0 in let diff = if diff >= 0 then diff else 0 in let vardepth = vardepth - diff in let argsno = argsno + diff in let args = List.map destConst (C.mkinterval vardepth argsno 0) in with_parens prec ~test:(args <> []) appl_prec (fun _ -> pp_app f (pp_uvar inf_prec depth vardepth 0) ppconstant (r,args)) | UVar (r,vardepth,argsno) -> pp_uvar prec depth vardepth argsno f r | AppUVar (r,vardepth,terms) when !!r != C.dummy && nice -> aux prec depth f (!do_app_deref ~from:vardepth ~to_:depth terms !!r) | AppUVar (r,vardepth,terms) -> with_parens prec appl_prec (fun _ -> pp_app f (pp_uvar inf_prec depth vardepth 0) (aux inf_prec depth) ~pplastarg:(aux_last inf_prec depth) (r,terms)) | Arg (n,argsno) -> let args = List.map destConst (C.mkinterval argsdepth argsno 0) in with_parens prec ~test:(args <> []) appl_prec (fun _ -> pp_app f (pp_arg prec depth) ppconstant (n,args)) | AppArg (v,terms) -> with_parens prec appl_prec (fun _ -> pp_app f (pp_arg inf_prec depth) (aux inf_prec depth) ~pplastarg:(aux_last inf_prec depth) (v,terms)) | Lam t -> with_parens prec lam_prec (fun _ -> let c = mkConst depth in Fmt.fprintf f "%a \\@ %a" (aux inf_prec depth) c (aux min_prec (depth+1)) t) | CData d -> CData.pp f d | Discard -> Fmt.fprintf f "_" in try aux min_prec depth0 f t with e -> Fmt.fprintf f "EXN PRINTING: %s" (Printexc.to_string e) ;; let ppterm = xppterm ~nice:false let uppterm = xppterm ~nice:true let pp_oref ?pp_ctx fmt (id,t) = if not (UUID.equal id id_term) then anomaly "pp_oref" else let t : term = Obj.obj t in if t == C.dummy then Fmt.fprintf fmt "_" else uppterm ?pp_ctx 0 [] ~argsdepth:0 empty_env fmt t let pp_constant ?pp_ctx fmt c = let table = match pp_ctx with | None -> None | Some { table } -> Some table in C.pp ?table fmt c end (* }}} *) open Pp let rid, max_runtime_id = Fork.new_local 0, ref 0 (****************************************************************************** Stores for: - backtracking (trail) - constraints (delayed goals) - constraint handling rules (propagation rules) Note: propagation code for CHR rules is later ******************************************************************************) (* Together to hide low level APIs of ConstraintStore needed by Trail *) module ConstraintStoreAndTrail : sig type propagation_item = { cstr : constraint_def; cstr_blockers : uvar_body list; } val new_delayed : propagation_item list Fork.local_ref val to_resume : stuck_goal list Fork.local_ref val blockers_map : stuck_goal list IntMap.t Fork.local_ref val blocked_by : uvar_body -> stuck_goal list val declare_new : stuck_goal -> unit val remove_old : stuck_goal -> unit val remove_old_constraint : constraint_def -> unit val contents : ?overlapping:uvar_body list -> unit -> (constraint_def * blockers) list (* val print : ?pp_ctx:pp_ctx -> Fmt.formatter -> (constraint_def * blockers) list -> unit *) val print1 : ?pp_ctx:pp_ctx -> Fmt.formatter -> constraint_def * blockers -> unit [@@warning "-32"] val print_gid: Fmt.formatter -> constraint_def * blockers -> unit [@@warning "-32"] val pp_stuck_goal : ?pp_ctx:pp_ctx -> Fmt.formatter -> stuck_goal -> unit val state : State.t Fork.local_ref val initial_state : State.t Fork.local_ref (* ---------------------------------------------------- *) type trail val empty : trail val initial_trail : trail Fork.local_ref val trail : trail Fork.local_ref val cut_trail : unit -> unit (* If true, no need to trail an imperative action. Not part of trial_this * because you can save allocations and a function call by testing locally *) val last_call : bool ref (* add an item to the trail *) val trail_assignment : uvar_body -> unit (* backtrack *) val undo : old_trail:trail -> ?old_state:State.t -> unit -> unit val print_trail : Fmt.formatter -> unit [@@warning "-32"] (* ---------------------------------------------------- *) (* This is only used to know if the program execution is really over, I.e. to implement the last component of the runtime data type *) module Ugly : sig val delayed : stuck_goal list ref end end = struct (* {{{ *) type propagation_item = { cstr : constraint_def; cstr_blockers : uvar_body list; } let state = Fork.new_local State.dummy let initial_state = Fork.new_local State.dummy type trail_item = | Assignement of uvar_body | StuckGoalAddition of stuck_goal | StuckGoalRemoval of stuck_goal [@@deriving show] type trail = trail_item list [@@deriving show] let empty = [] let trail = Fork.new_local [] let initial_trail = Fork.new_local [] let last_call = Fork.new_local false;; let cut_trail () = trail := !initial_trail [@@inline];; let blockers_map = Fork.new_local (IntMap.empty : stuck_goal list IntMap.t) let blocked_by r = IntMap.find (uvar_id r) !blockers_map module Ugly = struct let delayed : stuck_goal list ref = Fork.new_local [] end open Ugly let contents ?overlapping () = let overlap : uvar_body list -> bool = match overlapping with | None -> fun _ -> true | Some l -> fun b -> List.exists (fun x -> List.memq x l) b in map_filter (function | { kind = Constraint c; blockers } when overlap blockers -> Some (c,blockers) | _ -> None) !delayed let trail_assignment x = [%spy "dev:trail:assign" ~rid Fmt.pp_print_bool !last_call pp_trail_item (Assignement x)]; if not !last_call then trail := Assignement x :: !trail [@@inline] ;; let trail_stuck_goal_addition x = [%spy "dev:trail:add-constraint" ~rid Fmt.pp_print_bool !last_call pp_trail_item (StuckGoalAddition x)]; if not !last_call then trail := StuckGoalAddition x :: !trail [@@inline] ;; let trail_stuck_goal_removal x = [%spy "dev:trail:remove-constraint" ~rid Fmt.pp_print_bool !last_call pp_trail_item (StuckGoalRemoval x)]; if not !last_call then trail := StuckGoalRemoval x :: !trail [@@inline] ;; let append_blocked blocked map r = let blocker = uvar_id r in try let old = IntMap.find blocker map in (*assert(b.uid <= 0);*) IntMap.add blocker (blocked :: old) map with Not_found -> uvar_set_blocker r; IntMap.add blocker (blocked :: []) map let remove_blocked blocked map r = let blocker = uvar_id r in (*try*) let old = IntMap.find blocker map in (*assert(b.uid <= 0);*) let l = remove_from_list blocked old in if l = [] then begin uvar_unset_blocker r; IntMap.remove blocker map end else IntMap.add blocker l map (*with Not_found -> assert(b.uid >= 0); map*) let remove ({ blockers } as sg : stuck_goal) = [%spy "dev:constraint:remove" ~rid pp_stuck_goal sg]; delayed := remove_from_list sg !delayed; blockers_map := List.fold_left (remove_blocked sg) !blockers_map blockers let add ({ blockers } as sg : stuck_goal) = [%spy "dev:constraint:add" ~rid pp_stuck_goal sg]; delayed := sg :: !delayed; blockers_map := List.fold_left (append_blocked sg) !blockers_map blockers let new_delayed = Fork.new_local [] let to_resume = Fork.new_local [] let print_trail fmt = pp_trail fmt !trail; Fmt.fprintf fmt "to_resume:%d new_delayed:%d\n%!" (List.length !to_resume) (List.length !new_delayed) let declare_new (sg : stuck_goal) = let blockers = uniqq sg.blockers in let sg = { sg with blockers } in add sg ; begin match sg.kind with | Unification _ -> () | Constraint cstr -> new_delayed := { cstr; cstr_blockers = sg.blockers } :: !new_delayed | _ -> assert false end; (trail_stuck_goal_addition[@inlined]) sg let remove_cstr_if_exists x l = let rec aux acc = function | [] -> l | { cstr = y } :: tl when x == y -> List.rev acc @ tl | y :: tl -> aux (y :: acc) tl in aux [] l let remove_old cstr = remove cstr ; begin match cstr.kind with | Unification _ -> () | Constraint c -> new_delayed := remove_cstr_if_exists c !new_delayed | _ -> assert false end; (trail_stuck_goal_removal[@inlined]) cstr ;; let remove_old_constraint cd = let c = List.find (function { kind = Constraint x } -> x == cd | _ -> false) !delayed in remove_old c let undo ~old_trail ?old_state () = (* Invariant: to_resume and new_delayed are always empty when a choice point is created. This invariant is likely to break in the future, when we allow more interesting constraints and constraint propagation rules. *) to_resume := []; new_delayed := []; [%spy "dev:trail:undo" ~rid pp_trail !trail pp_string "->" pp_trail old_trail]; while !trail != old_trail do match !trail with | Assignement r :: rest -> r.contents <- C.dummy; trail := rest | StuckGoalAddition sg :: rest -> remove sg; trail := rest | StuckGoalRemoval sg :: rest -> add sg; trail := rest | [] -> anomaly "undo to unknown trail" done; match old_state with | Some old_state -> state := old_state | None -> () ;; let print1 ?pp_ctx fmt x = let pp_depth fmt d = if d > 0 then Fmt.fprintf fmt "{%a} :@ " (pplist (uppterm ?pp_ctx d [] ~argsdepth:0 empty_env) " ") (C.mkinterval 0 d 0) in let pp_hyps fmt ctx = if ctx <> [] then Fmt.fprintf fmt "@[<hov 2>%a@]@ ?- " (pplist (fun fmt { hdepth = d; hsrc = t } -> uppterm ?pp_ctx d [] ~argsdepth:0 empty_env fmt t) ", ") ctx in let pp_goal depth = uppterm ?pp_ctx depth [] ~argsdepth:0 empty_env in let { cdepth=depth;context=pdiff; conclusion=g }, blockers = x in Fmt.fprintf fmt " @[<h>@[<hov 2>%a%a%a@]@ /* suspended on %a */@]" pp_depth depth pp_hyps pdiff (pp_goal depth) g (pplist (uppterm ?pp_ctx 0 [] ~argsdepth:0 empty_env) ", ") (List.map (fun r -> UVar(r,0,0)) blockers) let print_gid fmt x = let { cgid = gid [@trace]; cdepth = _ }, _ = x in let () [@trace] = Fmt.fprintf fmt "%a" UUID.pp gid in () let print ?pp_ctx fmt x = pplist (print1 ?pp_ctx) " " fmt x let pp_stuck_goal ?pp_ctx fmt { kind; blockers } = match kind with | Unification { adepth = ad; env = e; bdepth = bd; a; b } -> Fmt.fprintf fmt " @[<h>@[<hov 2>^%d:%a@ == ^%d:%a@]@ /* suspended on %a */@]" ad (uppterm ?pp_ctx ad [] ~argsdepth:0 empty_env) a bd (uppterm ?pp_ctx ad [] ~argsdepth:ad e) b (pplist ~boxed:false (uppterm ?pp_ctx 0 [] ~argsdepth:0 empty_env) ", ") (List.map (fun r -> UVar(r,0,0)) blockers) | Constraint c -> print ?pp_ctx fmt [c,blockers] | _ -> assert false end (* }}} *) module T = ConstraintStoreAndTrail module CS = ConstraintStoreAndTrail (* Assigning an UVar wakes up suspended goals/constraints *) let (@:=) r v = (T.trail_assignment[@inlined]) r; if uvar_is_a_blocker r then begin let blocked = CS.blocked_by r in [%spy "user:assign:resume" ~rid (fun fmt l -> let l = map_filter (function | { kind = Constraint { cgid; _ } ; _ } -> Some cgid | _ -> None) l in Fmt.fprintf fmt "%a" (pplist ~boxed:true UUID.pp " ") l) blocked]; CS.to_resume := List.fold_right (fun x acc -> if List.memq x acc then acc else x::acc) blocked !CS.to_resume end; r.contents <- v ;; (* Low level, trail but no wakeup, used in freeze *) let (@::==) r v = (T.trail_assignment[@inlined]) r; r.contents <- v (****************************************************************************** Unification (dereferencing and lift, to_heap) ******************************************************************************) module HO : sig val unif : argsdepth:int -> matching:bool -> (UUID.t[@trace]) -> int -> env -> int -> term -> term -> bool (* lift/restriction/heapification with occur_check *) val move : argsdepth:int -> env -> ?avoid:uvar_body -> from:int -> to_:int -> term -> term (* like move but for heap terms (no heapification) *) val hmove : ?avoid:uvar_body -> from:int -> to_:int -> term -> term (* simultaneous substitution *) val subst : int -> term list -> term -> term val deref_uv : int deref_fun val deref_appuv : term list deref_fun val mkAppUVar : uvar_body -> int -> term list -> term val mkAppArg : int -> int -> term list -> term val is_flex : depth:int -> term -> uvar_body option val list_to_lp_list : term list -> term val full_deref : adepth:int -> env -> depth:int -> term -> term (* freshens all uvars (unless keep_if_outside), discards blocker status *) val copy_heap_drop_csts : depth:int -> ?keep_if_outside:uvar_body IntMap.t -> term -> term * uvar_body IntMap.t (* Head of an heap term *) val deref_head : depth:int -> term -> term val eta_contract_flex : depth:int -> term -> term option (* Put a flexible term in canonical expanded form: X^0 args. * It returns the canonical term and an assignment if needed. * (The first term is the result of dereferencing after the assignment) *) type assignment = uvar_body * int * term val expand_uv : depth:int -> uvar_body -> lvl:int -> ano:int -> term * assignment option val expand_appuv : depth:int -> uvar_body -> lvl:int -> args:term list -> term * assignment option val shift_bound_vars : depth:int -> to_:int -> term -> term val map_free_vars : map:constant C.Map.t -> depth:int -> to_:int -> term -> term val delay_hard_unif_problems : bool Fork.local_ref end = struct (* {{{ *) (* {{{ ************** move/hmove/deref_* ******************** *) exception NotInTheFragment (* in_fragment n [n;...;n+m-1] = m it must be called either on heap terms or on stack terms whose Args are non instantiated *) let rec in_fragment expected = function [] -> 0 | Const c::tl when c = expected -> 1 + in_fragment (expected+1) tl | UVar ({ contents = t} ,_,_)::tl -> (* XXX *) in_fragment expected (t :: tl) (* Invariant not true anymore, since we may not deref aggressively to avoid occur-check | UVar (r,_,_)::_ | AppUVar (r,_,_)::_ when !!r != C.dummy -> anomaly "non dereferenced terms in in_fragment" *) | _ -> raise NotInTheFragment exception NonMetaClosed let deterministic_restriction e ~args_safe t = let rec aux = function Lam f -> aux f | App (_,t,l) -> aux t; List.iter aux l | Cons (x,xs) -> aux x; aux xs | Builtin (_,l) -> List.iter aux l | UVar (r,_,_) | AppUVar (r,_,_) when !!r == C.dummy -> raise NonMetaClosed | UVar (r,_,_) -> aux !!r | AppUVar (r,_,l) -> aux !!r ; List.iter aux l | Arg (i,_) when e.(i) == C.dummy && not args_safe -> raise NonMetaClosed | AppArg (i,_) when e.(i) == C.dummy -> raise NonMetaClosed | Arg (i,_) -> if e.(i) != C.dummy then aux e.(i) | AppArg (i,l) -> aux e.(i) ; List.iter aux l | Const _ | Nil | Discard | CData _ -> () in try aux t ; true with NonMetaClosed -> false exception RestrictionFailure let occurr_check r1 r2 = match r1 with | None -> () | Some r1 -> (* It may be the case that in `X = Y` where `Y = f X` the term Y has to be moved/derefd in two steps, eg: else maux empty_env depth (deref_uv ~from:vardepth ~to_:(from+depth) args t) here maux carries the r1 = X (the ?avoid), while deref_uv does not, to avoid occur checking things twice. But deref_uv, if Y contains X, may assign X' to X (or x..\ X' x.. t oX). If this happens, then r1 is no more a dummy (unassigned variable) *) if !!r1 != C.dummy || r1 == r2 then raise RestrictionFailure let rec make_lambdas destdepth args = if args <= 0 then UVar(oref C.dummy,destdepth,0) else Lam (make_lambdas (destdepth+1) (args-1)) let rec mknLam n x = if n = 0 then x else Lam (mknLam (n-1) x) let mkAppUVar r lvl l = try UVar(r,lvl,in_fragment lvl l) with NotInTheFragment -> AppUVar(r,lvl,l) let mkAppArg i fromdepth xxs' = try Arg(i,in_fragment fromdepth xxs') with NotInTheFragment -> AppArg (i,xxs') let expand_uv r ~lvl ~ano = let args = C.mkinterval 0 (lvl+ano) 0 in if lvl = 0 then AppUVar(r,lvl,args), None else let r1 = oref C.dummy in let t = AppUVar(r1,0,args) in let assignment = mknLam ano t in t, Some (r,lvl,assignment) let expand_uv ~depth r ~lvl ~ano = [%spy "dev:expand_uv:in" ~rid (uppterm depth [] ~argsdepth:0 empty_env) (UVar(r,lvl,ano))]; let t, ass as rc = expand_uv r ~lvl ~ano in [%spy "dev:expand_uv:out" ~rid (uppterm depth [] ~argsdepth:0 empty_env) t (fun fmt -> function | None -> Fmt.fprintf fmt "no assignment" | Some (_,_,t) -> Fmt.fprintf fmt "%a := %a" (uppterm depth [] ~argsdepth:0 empty_env) (UVar(r,lvl,ano)) (uppterm lvl [] ~argsdepth:0 empty_env) t) ass]; rc let expand_appuv r ~lvl ~args = if lvl = 0 then AppUVar(r,lvl,args), None else let args_lvl = C.mkinterval 0 lvl 0 in let r1 = oref C.dummy in let t = AppUVar(r1,0,args_lvl @ args) in let nargs = List.length args in let assignment = mknLam nargs (AppUVar(r1,0,args_lvl @ C.mkinterval lvl nargs 0)) in t, Some (r,lvl,assignment) let expand_appuv ~depth r ~lvl ~args = [%spy "dev:expand_appuv:in" ~rid (uppterm depth [] ~argsdepth:0 empty_env) (AppUVar(r,lvl,args))]; let t, ass as rc = expand_appuv r ~lvl ~args in [%spy "dev:expand_appuv:out" ~rid (uppterm depth [] ~argsdepth:0 empty_env) t (fun fmt -> function | None -> Fmt.fprintf fmt "no assignment" | Some (_,_,t) -> Fmt.fprintf fmt "%a := %a" (uppterm depth [] ~argsdepth:0 empty_env) (AppUVar(r,lvl,args)) (uppterm lvl [] ~argsdepth:0 empty_env) t) ass]; rc let deoptimize_uv_w_args = function | UVar(r,lvl,args) when args > 0 -> AppUVar(r,lvl,C.mkinterval lvl args 0) | x -> x (* move performs at once: 1) refreshing of the arguments into variables (heapifycation) 2) restriction/occur-check hmove is the variant that only does 2 when from = to, i.e. delta = 0: (-infty,+infty) -> (-infty,+infty) when from < to, i.e. delta < 0: (-infty,from) -> (-infty,from) free variables [from,+infty) -> [to,+infty) bound variables when from > to, i.e. delta > 0: (-infty,to) -> (-infty,to) free variables [to,from) -> error free restricted variables [from,+infty) -> [to,+infty) bound variables Invariant: when from=to, to_heap is to be called only for terms that are not in the heap Notes: - remember that the depth of a stack term is the depth when the clause entered the program, whereas the argsdepth of the variables is the depth when the clause is used. Therefore it is normal to have arguments, possibly instantiated, whose depth is greater than the surrounding term. Calling to_heap on them with the wrong depth (e.g. the depth of the surrounding term) can therefore trigger restrictions that are meaningless. - it is possible for delta to be >0 (restriction) and the second term be a stack term. Therefore sometimes restrictions of heap terms are meaningful. Example: p A. |- pi x \ p X The unification problem becomes X^0 1^=^0 A^1 that triggers the assignment X^0 := to_heap ~from:1 ~to_:0 A^1 - On the other hand *I think* that it is not possible to have argsdepth < to_ in to_heap. It would mean that I am doing an assignment X^to_ := ... A^argsdepth.... that is triggered by an unification problem .... X^to_ .... == .... A^argsdepth .... that is triggered by calling a clause at depth argsdepth with a formal parameter containing an UVar whose depth is higher than the current depth of the program. How could such a variable enters the formal parameters without being restricted earlier? Therefore, in the case of heap terms, the invariant is from <= depthargs <= to_ the first inequality because a clause always enter a program at a depth (from) smaller than when it is used (depthargs). *) (* [move ~from ~to ?avoid t] relocates a term t by amount (= to - from). In the diagrams below, c is a constant, l is locally bound variable (i.e. bound after from). The resulting term lives in the heap and avoid, if passed, is occur checked. - If amount is > 0 then Const nodes >= from are lifted by amount c1 ---> c1 c2 from ---> c2 l3 - to l4 -`-> l4 `-> l5 - If amount is < 0 then Const nodes between to and from are ensured not to occur in the result, otherwise RestrictionFailure is raised. Const nodes >= from are de-lifted by amount c1 ---> c1 to c1 ---> c1 to c2 from ---> Error from ,-> l2 l3 l3 -,-> l3 l4 l4 - * Property: move ~to ~from (move ~from ~to t) = t - if from <= to - if to > from and no Const >= to and < from occurs in t where occurs also means in the scope of an Uv * Args: - when defined in e the corresponding term lives in adepth - when undefined they are turned into Uv (heapification) * Uv occur check is performed only if avoid is passed * Deref under binders: from:2 to_:4 G |- 3\ X^1 3 X := 2\f 1 2 phase 1 G |- 3\ (4\f 1 4) 3 G |- 3\ f 1 3 phase 2 (the lambda is anonymous, so 3\ is like 5\) G |- 5\ f 1 5 *) let rec move ~argsdepth (e:env) ?avoid ~from ~to_ t = (* TODO: to disable occur_check add something like: let avoid = None in *) let delta = from - to_ in let rc = if delta = 0 && e == empty_env && avoid == None then t (* Nothing to do! *) else begin (*if delta = 0 && e == empty_env && avoid <> None then prerr_endline "# EXPENSIVE OCCUR CHECK";*) let rec maux e depth x = [%trace "move" ~rid ("adepth:%d depth:%d from:%d to:%d x:%a" argsdepth depth from to_ (ppterm (from+depth) [] ~argsdepth e) x) begin match x with | Const c -> if delta == 0 then x else (* optimization *) if c >= from then mkConst (c - delta) else (* locally bound *) if c < to_ then x else (* constant *) raise RestrictionFailure | Lam f -> let f' = maux e (depth+1) f in if f == f' then x else Lam f' | App (c,t,l) when delta == 0 || c < from && c < to_ -> let t' = maux e depth t in let l' = smart_map3 maux e depth l in if t == t' && l == l' then x else App (c,t',l') | App (c,t,l) when c >= from -> App(c-delta, maux e depth t, smart_map3 maux e depth l) | App _ -> raise RestrictionFailure | Builtin (c,l) -> let l' = smart_map3 maux e depth l in if l == l' then x else Builtin (c,l') | CData _ -> x | Cons(hd,tl) -> let hd' = maux e depth hd in let tl' = maux e depth tl in if hd == hd' && tl == tl' then x else Cons(hd',tl') | Nil -> x | Discard when avoid = None -> x | Discard -> let r = oref C.dummy in UVar(r,to_,0) (* fast path with no deref... *) | UVar _ when delta == 0 && avoid == None -> x (* deref *) | UVar ({ contents = t }, vardepth, args) when t != C.dummy -> if depth == 0 then deref_uv ?avoid ~from:vardepth ~to_ args t else maux empty_env depth (deref_uv ~from:vardepth ~to_:(from+depth) args t) | AppUVar ({ contents = t }, vardepth, args) when t != C.dummy -> (* wrong, args escape occur check: if depth == 0 then deref_appuv ?avoid ~from:vardepth ~to_ args t else *) maux empty_env depth (deref_appuv ~from:vardepth ~to_:(from+depth) args t) | Arg (i, argsno) when e.(i) != C.dummy -> if to_ = argsdepth then deref_uv ?avoid ~from:argsdepth ~to_:(to_+depth) argsno e.(i) else let args = C.mkinterval from argsno 0 in let args = try smart_map3 maux e depth args with RestrictionFailure -> anomaly "move: could check if unrestrictable args are unused" in deref_appuv ?avoid ~from:argsdepth ~to_:(to_+depth) args e.(i) | AppArg(i, args) when e.(i) != C.dummy -> let args = try smart_map3 maux e depth args with RestrictionFailure -> anomaly "move: could check if unrestrictable args are unused" in deref_appuv ?avoid ~from:argsdepth ~to_:(to_+depth) args e.(i) (* heapification/restriction of Arg and AppArg *) | Arg (i, args) -> let to_ = min argsdepth to_ in let r = oref C.dummy in let v = UVar(r,to_,0) in e.(i) <- v; if args == 0 then v else UVar(r,to_,args) | AppArg(i, args) when List.for_all (deterministic_restriction e ~args_safe:(argsdepth=to_)) args -> let to_ = min argsdepth to_ in let args = try smart_map (maux e depth) args with RestrictionFailure -> anomaly "TODO: implement deterministic restriction" in let r = oref C.dummy in let v = UVar(r,to_,0) in e.(i) <- v; mkAppUVar r to_ args | AppArg _ -> Fmt.fprintf Fmt.str_formatter "Non deterministic pruning, delay to be implemented: t=%a, delta=%d%!" (ppterm depth [] ~argsdepth e) x delta; anomaly (Fmt.flush_str_formatter ()) (* restriction/lifting of UVar *) | UVar (r,vardepth,argsno) -> occurr_check avoid r; if delta <= 0 then (* to_ >= from *) if vardepth + argsno <= from then x else let r,vardepth,argsno = decrease_depth r ~from:vardepth ~to_:from argsno in let args = C.mkinterval vardepth argsno 0 in let args = smart_map (maux empty_env depth) args in mkAppUVar r vardepth args else if vardepth + argsno <= to_ then x else let t, assignment = expand_uv ~depth:(from+depth) r ~lvl:vardepth ~ano:argsno in option_iter (fun (r,_,assignment) -> [%spy "user:assign:expand" ~rid (fun fmt () -> Fmt.fprintf fmt "%a := %a" (uppterm (from+depth) [] ~argsdepth e) x (uppterm vardepth [] ~argsdepth e) assignment) ()]; r @:= assignment) assignment; maux e depth t | AppUVar (r,vardepth,args) -> occurr_check avoid r; if delta < 0 then let r,vardepth,argsno = decrease_depth r ~from:vardepth ~to_:from 0 in let args0= C.mkinterval vardepth argsno 0 in let args = try smart_map (maux e depth) (args0@args) with RestrictionFailure -> anomaly "impossible, delta < 0" in mkAppUVar r vardepth args else if delta == 0 then AppUVar (r,vardepth,smart_map (maux e depth) args) else if List.for_all (deterministic_restriction e ~args_safe:(argsdepth=to_)) args then (* Code for deterministic restriction *) let pruned = ref (vardepth > to_) in let orig_argsno = List.length args in let filteredargs_vardepth = ref [] in let filteredargs_to = let rec filter i acc = function | [] -> filteredargs_vardepth := List.rev acc; [] | arg :: args -> try let arg = maux e depth arg in arg :: filter (succ i) (mkConst (vardepth + i) :: acc) args with RestrictionFailure -> pruned := true; filter (succ i) acc args in filter 0 [] args in if !pruned then begin let vardepth' = min vardepth to_ in let r' = oref C.dummy in let newvar = mkAppUVar r' vardepth' !filteredargs_vardepth in let assignment = mknLam orig_argsno newvar in [%spy "user:assign:restrict" ~rid (fun fmt () -> Fmt.fprintf fmt "%d %a := %a" vardepth (ppterm (from+depth) [] ~argsdepth e) x (ppterm (vardepth) [] ~argsdepth e) assignment) ()]; r @:= assignment; mkAppUVar r' vardepth' filteredargs_to end else mkAppUVar r vardepth filteredargs_to else begin Fmt.fprintf Fmt.str_formatter "Non deterministic pruning, delay to be implemented: t=%a, delta=%d%!" (ppterm depth [] ~argsdepth e) x delta; anomaly (Fmt.flush_str_formatter ()) end end] in maux e 0 t end in [%spy "dev:move:out" ~rid (ppterm to_ [] ~argsdepth e) rc]; rc (* Hmove is like move for heap terms. By setting env to empty_env, it triggers fast paths in move (no need to heapify, the term already lives in the heap)*) and hmove ?avoid ~from ~to_ t = [%trace "hmove" ~rid ("@[<hov 1>from:%d@ to:%d@ %a@]" from to_ (uppterm from [] ~argsdepth:0 empty_env) t) begin move ?avoid ~argsdepth:0 ~from ~to_ empty_env t end] (* UVar(_,from,argsno) -> Uvar(_,to_,argsno+from-to_) *) and decrease_depth r ~from ~to_ argsno = if from <= to_ then r,from,argsno else let newr = oref C.dummy in let newargsno = argsno+from-to_ in let newvar = UVar(newr,to_,from-to_) in r @:= newvar; newr,to_,newargsno (* simultaneous substitution of ts for [depth,depth+|ts|) the substituted term must be in the heap the term is delifted by |ts| every t in ts must be either an heap term or an Arg(i,0) the ts are lifted as usual *) and subst fromdepth ts t = [%trace "subst" ~rid ("@[<hov 2>fromdepth:%d t: %a@ ts: %a@]" fromdepth (uppterm (fromdepth) [] ~argsdepth:0 empty_env) t (pplist (uppterm fromdepth [] ~argsdepth:0 empty_env) ", ") ts) begin if ts == [] then t else let len = List.length ts in let fromdepthlen = fromdepth + len in let rec aux depth tt = [%trace "subst-aux" ~rid ("@[<hov 2>t: %a@]" (uppterm (fromdepth+1) [] ~argsdepth:0 empty_env) tt) begin match tt with | Const c as x -> if c >= fromdepth && c < fromdepthlen then match List.nth ts (len-1 - (c-fromdepth)) with | Arg(i,0) as t -> t | t -> hmove ~from:fromdepth ~to_:(depth-len) t else if c < fromdepth then x else mkConst (c-len) (* NOT LIFTED *) | Arg _ | AppArg _ -> anomaly "subst takes a heap term" | App(c,x,xs) as orig -> let x' = aux depth x in let xs' = smart_map (aux depth) xs in let xxs' = x'::xs' in if c >= fromdepth && c < fromdepthlen then match List.nth ts (len-1 - (c-fromdepth)) with | Arg(i,0) -> mkAppArg i fromdepth xxs' | t -> let t = hmove ~from:fromdepth ~to_:(depth-len) t in beta (depth-len) [] t xxs' else if c < fromdepth then if x == x' && xs == xs' then orig else App(c,x',xs') else App(c-len,x',xs') | Builtin(c,xs) as orig -> let xs' = smart_map (aux depth) xs in if xs == xs' then orig else Builtin(c,xs') | Cons(hd,tl) as orig -> let hd' = aux depth hd in let tl' = aux depth tl in if hd == hd' && tl == tl' then orig else Cons(hd',tl') | Nil -> tt | Discard -> tt | UVar({contents=g},vardepth,argsno) when g != C.dummy -> [%tcall aux depth (deref_uv ~from:vardepth ~to_:depth argsno g)] | UVar(r,vardepth,argsno) as orig -> if vardepth+argsno <= fromdepth then orig else let r,vardepth,argsno = decrease_depth r ~from:vardepth ~to_:fromdepth argsno in let args = C.mkinterval vardepth argsno 0 in let args = smart_map (aux depth) args in mkAppUVar r vardepth args | AppUVar({ contents = t },vardepth,args) when t != C.dummy -> [%tcall aux depth (deref_appuv ~from:vardepth ~to_:depth args t)] | AppUVar(r,vardepth,args) -> let r,vardepth,argsno = decrease_depth r ~from:vardepth ~to_:fromdepth 0 in let args0 = C.mkinterval vardepth argsno 0 in let args = smart_map (aux depth) (args0@args) in mkAppUVar r vardepth args | Lam t -> Lam (aux (depth+1) t) | CData _ as x -> x end] in aux fromdepthlen t end] and beta depth sub t args = [%trace "beta" ~rid ("@[<hov 2>subst@ t: %a@ args: %a@]" (uppterm (depth+List.length sub) [] ~argsdepth:0 empty_env) t (pplist (uppterm depth [] ~argsdepth:0 empty_env) ", ") args) begin match t, args with | UVar ({contents=g},vardepth,argsno), _ when g != C.dummy -> [%tcall beta depth sub (deref_uv ~from:vardepth ~to_:(depth+List.length sub) argsno g) args] | AppUVar({ contents=g },vardepth,uargs), _ when g != C.dummy -> [%tcall beta depth sub (deref_appuv ~from:vardepth ~to_:(depth+List.length sub) uargs g) args] | Lam t', hd::tl -> [%tcall beta depth (hd::sub) t' tl] | _ -> let t' = subst depth sub t in [%spy "dev:subst:out" ~rid (ppterm depth [] ~argsdepth:0 empty_env) t']; match args with | [] -> t' | ahd::atl -> match t' with | Const c -> App (c,ahd,atl) | Arg _ | AppArg _ -> anomaly "beta takes only heap terms" | App (c,arg,args1) -> App (c,arg,args1@args) | Builtin (c,args1) -> Builtin (c,args1@args) | UVar (r,n,m) -> begin try UVar(r,n,m + in_fragment (n+m) args) with NotInTheFragment -> let args1 = C.mkinterval n m 0 in AppUVar (r,n,args1@args) end | AppUVar (r,depth,args1) -> AppUVar (r,depth,args1@args) | Lam _ -> anomaly "beta: some args and some lambdas" | CData x -> type_error (Printf.sprintf "beta: '%s'" (CData.show x)) | Nil -> type_error "beta: Nil" | Cons _ -> type_error "beta: Cons" | Discard -> type_error "beta: Discard" end] (* eat_args n [n ; ... ; n+k] (Lam_0 ... (Lam_k t)...) = n+k+1,[],t eat_args n [n ; ... ; n+k]@l (Lam_0 ... (Lam_k t)...) = n+k+1,l,t if t != Lam or List.hd l != n+k+1 *) and eat_args depth l t = match t with | Lam t' when l > 0 -> eat_args (depth+1) (l-1) t' | UVar ({contents=t},origdepth,args) when t != C.dummy -> eat_args depth l (deref_uv ~from:origdepth ~to_:depth args t) | AppUVar ({contents=t},origdepth,args) when t != C.dummy -> eat_args depth l (deref_appuv ~from:origdepth ~to_:depth args t) | _ -> depth,l,t (* CSC: The following set of comments are to be revised. I found them scattered around and they refer to old names. They are also somehow duplicated *) (* Deref is to be called only on heap terms and with from <= to *) (* Deref is to be called only on heap terms and with from <= to * The args must live in to_. *) (* deref_uv performs lifting only and with from <= to if called on non-heap terms, it does not turn them to heap terms (if from=to_) Note: when deref_uv is called inside restrict, it may be from > to_ t lives in from; args already live in to_ *) and deref_appuv ?avoid ~from ~to_ args t = beta to_ [] (deref_uv ?avoid ~from ~to_ 0 t) args and deref_uv ?avoid ~from ~to_ args t = [%trace "deref_uv" ~rid ("from:%d to:%d %a @@ %d" from to_ (ppterm from [] ~argsdepth:0 empty_env) t args) begin if args == 0 then hmove ?avoid ~from ~to_ t else (* O(1) reduction fragment tested here *) let from,args',t = eat_args from args t in let t = deref_uv ?avoid ~from ~to_ 0 t in if args' == 0 then t else match t with | Lam _ -> anomaly "eat_args went crazy" | Const c -> let args = C.mkinterval (from+1) (args'-1) 0 in App (c,mkConst from, args) | App (c,arg,args2) -> let args = C.mkinterval from args' 0 in App (c,arg,args2 @ args) | Builtin (c,args2) -> let args = C.mkinterval from args' 0 in Builtin (c,args2 @ args) (* TODO: when the UVar/Arg is not C.dummy, we call deref_uv that will call move that will call_deref_uv again. Optimize the path *) | UVar(t,depth,0) when from = depth -> UVar(t,depth,args') | AppUVar (r,depth,args2) -> let args = C.mkinterval from args' 0 in AppUVar (r,depth,args2 @ args) | UVar (r,vardepth,argsno) -> let args1 = C.mkinterval vardepth argsno 0 in let args2 = C.mkinterval from args' 0 in let args = args1 @ args2 in mkAppUVar r vardepth args | Cons _ | Nil -> type_error "deref_uv: applied list" | Discard -> type_error "deref_uv: applied Discard" | CData _ -> t | Arg _ | AppArg _ -> assert false (* Uv gets assigned only heap term *) end] ;; let copy_heap_drop_csts ~depth ?keep_if_outside x = let m : uvar_body IntMap.t ref = match keep_if_outside with | None -> ref IntMap.empty | Some m -> ref m in let alloc_copy r = try IntMap.find (uvar_id r) !m with Not_found -> if keep_if_outside <> None then r else let r' = oref C.dummy in m := IntMap.add (uvar_id r) r' !m; r' in let alloc_copy r = let r' = alloc_copy r in (* Format.eprintf " copy uv %a -> %a\n%!" (uppterm depth [] ~argsdepth:0 empty_env) (UVar(r,0,0)) (uppterm depth [] ~argsdepth:0 empty_env) (UVar(r',0,0)); *) r' in let rec maux depth x = match x with | Const _ -> x | Lam f -> let f' = maux depth f in if f == f' then x else Lam f' | App (c, t, l) -> let t' = maux depth t in let l' = smart_map2 maux depth l in if t == t' && l == l' then x else App(c,t',l') | Builtin (c,l) -> let l' = smart_map2 maux depth l in if l == l' then x else Builtin (c,l') | CData _ -> x | Cons(hd,tl) -> let hd' = maux depth hd in let tl' = maux depth tl in if hd == hd' && tl == tl' then x else Cons(hd',tl') | Nil -> x | Discard -> let r = oref C.dummy in UVar(r,depth,0) (* deref *) | UVar ({ contents = t }, vardepth, args) when t != C.dummy -> maux depth (deref_uv ~from:vardepth ~to_:depth args t) | AppUVar ({ contents = t }, vardepth, args) when t != C.dummy -> maux depth (deref_appuv ~from:vardepth ~to_:depth args t) | Arg _ | AppArg _ -> anomaly "not a heap term" | UVar (r,vardepth,argsno) -> let r' = alloc_copy r in UVar (r',vardepth,argsno) | AppUVar (r,vardepth,args) -> let r' = alloc_copy r in AppUVar (r',vardepth,smart_map2 maux depth args) in let x' = maux depth x in (* Format.eprintf "copy %a -> %a\n%!" (uppterm depth [] ~argsdepth:0 empty_env) x (uppterm depth [] ~argsdepth:0 empty_env) x'; *) x', !m (* }}} *) (* {{{ ************** unification ******************************* *) (* is_flex is to be called only on heap terms *) let rec is_flex ~depth = function | Arg _ | AppArg _ -> anomaly "is_flex called on Args" | UVar ({ contents = t }, vardepth, args) when t != C.dummy -> is_flex ~depth (deref_uv ~from:vardepth ~to_:depth args t) | AppUVar ({ contents = t }, vardepth, args) when t != C.dummy -> is_flex ~depth (deref_appuv ~from:vardepth ~to_:depth args t) | UVar (r, _, _) | AppUVar (r, _, _) -> Some r | Const _ | Lam _ | App _ | Builtin _ | CData _ | Cons _ | Nil | Discard -> None (* Invariants: | adepth: depth of a (query, heap term) - bdepth b bdepth: depth of b (clause hd, stack term in env e) | b-only . adepth >= bdepth - adepth a = . | local x\ x\ - a' = b' Spec: (-infy,bdepth) = (-infty,bdepth) common free variables [bdepth,adepth) free variable only visible by one:fail [adepth,+infty) = [bdepth,+infy) bound variables Names: delta = adepth - bdepth i.e. size of forbidden region heap = ? Note about dereferencing UVar(r,origdepth,args): - args live *here* (a/bdepth + depth) - !!r lives in origdepth + we deref_uv to move everything to a/bdepth + depth Note about dereferencing Arg(i,args): - args live *here* (bdepth + depth) - e.(i) lives in bdepth + we deref_uv to move everything to adepth + depth *) (* Tests is args are in the L_\lambda fragment and produces a map from * constants to int. The idea being that an UVar of level lvl also sees * all the constants in the map, and the associated int is their position * in the list of arguments starting from 0 *) let is_llam lvl args adepth bdepth depth left e = let to_ = if left then adepth+depth else bdepth+depth in let get_con = function Const x -> x | _ -> raise RestrictionFailure in let deref_to_const = function | UVar ({ contents = t }, from, args) when t != C.dummy -> get_con (deref_uv ~from ~to_ args t) | AppUVar ({ contents = t }, from, args) when t != C.dummy -> get_con (deref_appuv ~from ~to_ args t) | Arg (i,args) when e.(i) != C.dummy -> get_con (deref_uv ~from:adepth ~to_ args e.(i)) | AppArg (i,args) when e.(i) != C.dummy -> get_con (deref_appuv ~from:adepth ~to_ args e.(i)) | Const x -> if not left && x >= bdepth then x + (adepth-bdepth) else x | _ -> raise RestrictionFailure in let rec aux n = function | [] -> [] | x :: xs -> if x >= lvl && not (List.mem x xs) then (x,n) :: aux (n+1) xs else raise RestrictionFailure in try true, aux 0 (List.map deref_to_const args) with RestrictionFailure -> false, [] let is_llam lvl args adepth bdepth depth left e = let res = is_llam lvl args adepth bdepth depth left e in [%spy "dev:is_llam" ~rid (fun fmt () -> let (b,map) = res in Fmt.fprintf fmt "%d + %a = %b, %a" lvl (pplist (ppterm adepth [] ~argsdepth:bdepth e) " ") args b (pplist (fun fmt (x,n) -> Fmt.fprintf fmt "%d |-> %d" x n) " ") map) ()]; res let rec mknLam n t = if n = 0 then t else mknLam (n-1) (Lam t) (* [bind r args ... t] generates a term \args.t' to be assigned to r. * r is the variable being assigned * gamma is the level of the variable we assigning * l is its extended domain as a map (see is_llam) * a is adepth * b is bdepth * d is depth (local lamdas traversed during unif * w is the same but during bind, hence current depth is (d+w) * delta is (a-b) * left is true when the variable being assigned is on the left (goal) * t is the term we are assigning to r * e is the env for args *) let bind ~argsdepth r gamma l a d delta b left t e = let new_lams = List.length l in let pos x = try List.assoc x l with Not_found -> raise RestrictionFailure in (* hmove = false makes the code insensitive to left/right, i.e. no hmove from b * to a is performed *) let cst ?(hmove=true) c b delta = (* The complex thing (DBL) *) if left then begin if c < gamma && c < b then c else let c = if hmove && c >= b then c + delta else c in if c < gamma then c else if c >= a + d then c + new_lams - (a+d - gamma) else pos c + gamma end else begin if c < gamma then c else if c >= a + d then c + new_lams - (a+d - gamma) else pos c + gamma end in let cst ?hmove c b delta = let n = cst ?hmove c b delta in [%spy "dev:bind:constant-mapping" ~rid (fun fmt () -> let (n,m) = c,n in Fmt.fprintf fmt "%d -> %d (c:%d b:%d gamma:%d delta:%d d:%d)" n m c b gamma delta d) ()]; n in let rec bind b delta w t = [%trace "bind" ~rid ("%b gamma:%d + %a = t:%a a:%d delta:%d d:%d w:%d b:%d" left gamma (pplist (fun fmt (x,n) -> Fmt.fprintf fmt "%a |-> %d" (ppterm a [] ~argsdepth e) (mkConst x) n) " ") l (ppterm a [] ~argsdepth empty_env) t a delta d w b) begin match t with | UVar (r1,_,_) | AppUVar (r1,_,_) when r == r1 -> raise RestrictionFailure | Const c -> let n = cst c b delta in if n < 0 then mkConst n else Const n | Lam t -> Lam (bind b delta (w+1) t) | App (c,t,ts) -> App (cst c b delta, bind b delta w t, smart_map (bind b delta w) ts) | Cons(hd,tl) -> Cons(bind b delta w hd, bind b delta w tl) | Nil -> t | Builtin (c, tl) -> Builtin(c, smart_map (bind b delta w) tl) | CData _ -> t (* deref_uv *) | Arg (i,args) when e.(i) != C.dummy -> bind a 0 w (deref_uv ~from:argsdepth ~to_:(a+d+w) args e.(i)) | AppArg (i,args) when e.(i) != C.dummy -> bind a 0 w (deref_appuv ~from:argsdepth ~to_:(a+d+w) args e.(i)) | UVar ({ contents = t }, from, args) when t != C.dummy -> bind b delta w (deref_uv ~from ~to_:((if left then b else a)+d+w) args t) | AppUVar ({ contents = t }, from, args) when t != C.dummy -> bind b delta w (deref_appuv ~from ~to_:((if left then b else a)+d+w) args t) (* pruning *) | (UVar _ | AppUVar _ | Arg _ | AppArg _ | Discard) as orig -> (* We deal with all flexible terms in a uniform way *) let r, lvl, (is_llam, args), orig_args = match orig with | UVar(r,lvl,0) -> r, lvl, (true, []), [] | UVar(r,lvl,args) -> let r' = oref C.dummy in let v = UVar(r',lvl+args,0) in r @:= mknLam args v; r', (lvl+args), (true,[]), [] | AppUVar (r,lvl, orig_args) -> r, lvl, is_llam lvl orig_args a b (d+w) left e, orig_args | Discard -> let r = oref C.dummy in r, a+d+w, (true,[]), [] | Arg (i,0) -> let r = oref C.dummy in let v = UVar(r,a,0) in e.(i) <- v; r, a, (true,[]), [] | Arg (i,args) -> let r = oref C.dummy in let v = UVar(r,a,0) in e.(i) <- v; let r' = oref C.dummy in let v' = UVar(r',a+args,0) in r @:= mknLam args v'; r', a+args, (true, []), [] | AppArg (i,orig_args) -> let is_llam, args = is_llam a orig_args a b (d+w) false e in let r = oref C.dummy in let v = UVar(r,a,0) in e.(i) <- v; r, a, (is_llam, args), orig_args | _ -> assert false in [%spy "dev:bind:maybe-prune" ~rid (fun fmt () -> Fmt.fprintf fmt "lvl:%d is_llam:%b args:%a orig_args:%a orig:%a" lvl is_llam (pplist (fun fmt (x,n) -> Fmt.fprintf fmt "%a->%d" (ppterm a [] ~argsdepth:b e) (mkConst x) n) " ") args (pplist (ppterm a [] ~argsdepth e) " ") orig_args (ppterm a [] ~argsdepth e) orig) ()]; if is_llam then begin let n_args = List.length args in if lvl > gamma then (* All orig args go away, but some between gamma and lvl can stay * if they are in l or locally bound [d,w] *) let args_gamma_lvl_abs, args_gamma_lvl_here = let mk_arg i = mkConst i, mkConst (cst ~hmove:false i b delta) in let rec mk_interval d argsno n = if n = argsno then [] else if n+d >= lvl then let i = n+d in (* cut&paste from below *) (try let nn = List.assoc i args in (mkConst (lvl+nn), mkConst (gamma+List.length l+n)) :: mk_interval d argsno (n+1) with Not_found -> mk_interval d argsno (n+1)) else mk_arg (n+d)::mk_interval d argsno (n+1) in let rec keep_cst_for_lvl = function | [] -> mk_interval (d + if left then a else b) w 0 | (i,mm) :: rest -> if i < lvl then ( mk_arg i :: keep_cst_for_lvl rest ) else (try let nn = List.assoc i args in (mkConst (lvl+nn), mkConst mm) :: keep_cst_for_lvl rest with Not_found -> keep_cst_for_lvl rest) in List.split (keep_cst_for_lvl (List.sort Stdlib.compare l)) in let r' = oref C.dummy in r @:= mknLam n_args (mkAppUVar r' gamma args_gamma_lvl_abs); mkAppUVar r' gamma args_gamma_lvl_here else (* given that we need to make lambdas to prune some args, * we also permute to make the restricted meta eventually * fall inside the small fragment (sort the args) *) let args = List.sort Stdlib.compare args in let args_lvl, args_here = List.fold_right (fun (c, c_p) (a_lvl, a_here as acc) -> try let i = if c < gamma then c else if c >= (if left then b else a) + d then c + new_lams - (a+d - gamma) else pos c + gamma in mkConst (c_p + lvl) :: a_lvl, mkConst i :: a_here with RestrictionFailure -> acc) args ([],[]) in if n_args = List.length args_here then (* no pruning, just binding the args as a normal App *) mkAppUVar r lvl (smart_map (bind b delta w) orig_args) else (* we need to project away some of the args *) let r' = oref C.dummy in let v = mkAppUVar r' lvl args_lvl in r @:= mknLam n_args v; (* This should be the beta reduct. One could also * return the non reduced but bound as in the other if branch *) mkAppUVar r' lvl args_here end else begin mkAppUVar r lvl (smart_map (bind b delta w) orig_args) end end] in let v = mknLam new_lams (bind b delta 0 t) in [%spy "user:assign:HO" ~rid (fun fmt () -> Fmt.fprintf fmt "%a := %a" (uppterm gamma [] ~argsdepth e) (UVar(r,gamma,0)) (uppterm gamma [] ~argsdepth e) v) ()]; r @:= v; true ;; (* exception Non_linear *) let rec list_to_lp_list = function | [] -> Nil | x::xs -> Cons(x,list_to_lp_list xs) ;; let delay_hard_unif_problems = Fork.new_local false let error_msg_hard_unif a b = "Unification problem outside the pattern fragment. ("^ show_term a^ " == " ^ show_term b^ ") Pass -delay-problems-outside-pattern-fragment (elpi command line utility) "^ "or set delay_outside_fragment to true (Elpi_API) in order to delay "^ "(deprecated, for Teyjus compatibility)." let rec deref_head ~depth = function | UVar ({ contents = t }, from, ano) when t != C.dummy -> deref_head ~depth (deref_uv ~from ~to_:depth ano t) | AppUVar ({contents = t}, from, args) when t != C.dummy -> deref_head ~depth (deref_appuv ~from ~to_:depth args t) | x -> x (* constant x occurs in term t with level d? *) let occurs x d adepth e t = let rec aux d t = match deref_head ~depth:d t with | Const c -> c = x | Lam t -> aux (d+1) t | App (c, v, vs) -> c = x || aux d v || auxs d vs | UVar (r, lvl, ano) -> let t, assignment = expand_uv ~depth:d r ~lvl ~ano in option_iter (fun (r,_,assignment) -> r @:= assignment) assignment; aux d t | AppUVar (_, _, args) -> auxs d args | Builtin (_, vs) -> auxs d vs | Cons (v1, v2) -> aux d v1 || aux d v2 | Arg(i,args) when e.(i) != C.dummy -> aux d (deref_uv ~from:adepth ~to_:d args e.(i)) | AppArg(i,args) when e.(i) != C.dummy -> aux d (deref_appuv ~from:adepth ~to_:d args e.(i)) | Nil | CData _ | Discard | Arg _ | AppArg _ -> false and auxs d = function | [] -> false | t :: ts -> aux d t || auxs d ts in x < d && aux d t let rec eta_contract_args ~orig_depth ~depth r args eat ~argsdepth e = match args, eat with | _, [] -> [%spy "eta_contract_flex" ~rid (fun fmt () -> Fmt.fprintf fmt "all eaten") ()]; begin try Some (AppUVar(r,0,smart_map (move ~argsdepth ~from:depth ~to_:orig_depth e) (List.rev args))) with RestrictionFailure -> None end | Const x::xs, y::ys when x == y && not (List.exists (occurs y depth argsdepth e) xs) -> [%spy "eta_contract_flex" ~rid (fun fmt -> Fmt.fprintf fmt "eat %d") y]; eta_contract_args ~orig_depth ~depth r xs ys ~argsdepth e | _, y::_ -> [%spy "eta_contract_flex" ~rid (fun fmt -> Fmt.fprintf fmt "cannot eat %d") y]; None ;; let rec eta_contract_flex orig_depth depth xdepth ~argsdepth e t eat = [%trace "eta_contract_flex" ~rid ("@[<hov 2>eta_contract_flex %d+%d:%a <- [%a]%!" xdepth depth (ppterm (xdepth+depth) [] ~argsdepth e) t (pplist (fun fmt i -> Fmt.fprintf fmt "%d" i) " ") eat) begin match deref_head ~depth:(xdepth+depth) t with | AppUVar(r,0,args) -> eta_contract_args ~orig_depth:(xdepth+orig_depth) ~depth:(xdepth+depth) r (List.rev args) eat ~argsdepth e | Lam t -> eta_contract_flex orig_depth (depth+1) xdepth ~argsdepth e t (depth+xdepth::eat) | UVar(r,lvl,ano) -> let t, assignment = expand_uv ~depth r ~lvl ~ano in option_iter (fun (r,_,assignment) -> r @:= assignment) assignment; eta_contract_flex orig_depth depth xdepth ~argsdepth e t eat | AppUVar(r,lvl,args) -> let t, assignment = expand_appuv ~depth r ~lvl ~args in option_iter (fun (r,_,assignment) -> r @:= assignment) assignment; eta_contract_flex orig_depth depth xdepth ~argsdepth e t eat | Arg (i, args) when e.(i) != C.dummy -> eta_contract_flex orig_depth depth xdepth ~argsdepth e (deref_uv ~from:argsdepth ~to_:(xdepth+depth) args e.(i)) eat | AppArg(i, args) when e.(i) != C.dummy -> eta_contract_flex orig_depth depth xdepth ~argsdepth e (deref_appuv ~from:argsdepth ~to_:(xdepth+depth) args e.(i)) eat | Arg (i, args) -> let to_ = argsdepth in let r = oref C.dummy in let v = UVar(r,to_,0) in e.(i) <- v; eta_contract_flex orig_depth depth xdepth ~argsdepth e (if args == 0 then v else UVar(r,to_,args)) eat | AppArg(i, args) -> let to_ = argsdepth in let r = oref C.dummy in let v = UVar(r,to_,0) in e.(i) <- v; eta_contract_flex orig_depth depth xdepth ~argsdepth e (mkAppUVar r to_ args) eat | _ -> None end] ;; let eta_contract_flex depth xdepth ~argsdepth e t = eta_contract_flex depth depth xdepth ~argsdepth e t [] [@@inline] let uvar_uvar_assignment_order r1 r2 = uvar_id r1 > uvar_id r2 let isLam = function Lam _ -> true | _ -> false let rec unif argsdepth matching depth adepth a bdepth b e = [%trace "unif" ~rid ("@[<hov 2>^%d:%a@ =%d%s= ^%d:%a@]%!" adepth (ppterm (adepth+depth) [] ~argsdepth empty_env) a depth (if matching then "m" else "") bdepth (ppterm (bdepth+depth) [] ~argsdepth:bdepth e) b) begin let delta = adepth - bdepth in (delta = 0 && a == b) || match a,b with | (Discard, _ | _, Discard) -> true (* _ as X binding *) | _, App(c,arg,[as_this]) when c == Global_symbols.asc -> unif argsdepth matching depth adepth a bdepth arg e && unif argsdepth matching depth adepth a bdepth as_this e | _, App(c,arg,_) when c == Global_symbols.asc -> error "syntax error in as" | App(c,arg,_), _ when c == Global_symbols.asc -> unif argsdepth matching depth adepth arg bdepth b e (* TODO: test if it is better to deref_uv first or not, i.e. the relative order of the clauses below *) | UVar(r1,_,args1), UVar(r2,_,args2) when r1 == r2 && !!r1 == C.dummy -> args1 == args2 (* XXX this would be a type error *) | UVar(r1,vd,xs), AppUVar(r2,_,ys) when r1 == r2 && !!r1 == C.dummy -> unif argsdepth matching depth adepth (AppUVar(r1,vd,C.mkinterval vd xs 0)) bdepth b e | AppUVar(r1,vd,xs), UVar(r2,_,ys) when r1 == r2 && !!r1 == C.dummy -> unif argsdepth matching depth adepth a bdepth (AppUVar(r1,vd,C.mkinterval vd ys 0)) e | AppUVar(r1,vd,xs), AppUVar(r2,_,ys) when r1 == r2 && !!r1 == C.dummy -> let pruned = ref false in let filtered_args_rev = fold_left2i (fun i args x y -> let b = unif argsdepth matching depth adepth x bdepth y e in if not b then (pruned := true; args) else x :: args ) [] xs ys in if !pruned then begin let len = List.length xs in let r = oref C.dummy in r1 @:= mknLam len (mkAppUVar r vd (List.rev filtered_args_rev)); end; true (* deref_uv *) | UVar ({ contents = t }, from, args), _ when t != C.dummy -> unif argsdepth matching depth adepth (deref_uv ~from ~to_:(adepth+depth) args t) bdepth b e | AppUVar ({ contents = t }, from, args), _ when t != C.dummy -> unif argsdepth matching depth adepth (deref_appuv ~from ~to_:(adepth+depth) args t) bdepth b e | _, UVar ({ contents = t }, from, args) when t != C.dummy -> unif argsdepth matching depth adepth a bdepth (deref_uv ~from ~to_:(bdepth+depth) args t) empty_env | _, AppUVar ({ contents = t }, from, args) when t != C.dummy -> unif argsdepth matching depth adepth a bdepth (deref_appuv ~from ~to_:(bdepth+depth) args t) empty_env | _, Arg (i,args) when e.(i) != C.dummy -> (* e.(i) is a heap term living at argsdepth wich can be > bdepth (e.g. the clause is at 0 and we are under a pi x\. As a result we do the deref to and the rec call at adepth *) unif argsdepth matching depth adepth a adepth (deref_uv ~from:argsdepth ~to_:(adepth+depth) args e.(i)) empty_env | _, AppArg (i,args) when e.(i) != C.dummy -> (* e.(i) is a heap term living at argsdepth wich can be > bdepth (e.g. the clause is at 0 and we are under a pi x\. As a result we do the deref to and the rec call at adepth *) let args = smart_map (move ~argsdepth ~from:bdepth ~to_:adepth e) args in unif argsdepth matching depth adepth a adepth (deref_appuv ~from:argsdepth ~to_:(adepth+depth) args e.(i)) empty_env (* UVar introspection (matching) *) | (UVar _ | AppUVar _), Const c when c == Global_symbols.uvarc && matching -> true | UVar(r,vd,ano), App(c,hd,[]) when c == Global_symbols.uvarc && matching -> unif argsdepth matching depth adepth (UVar(r,vd,ano)) bdepth hd e | AppUVar(r,vd,_), App(c,hd,[]) when c == Global_symbols.uvarc && matching -> unif argsdepth matching depth adepth (UVar(r,vd,0)) bdepth hd e | UVar(r,vd,ano), App(c,hd,[arg]) when c == Global_symbols.uvarc && matching -> let r_exp = oref C.dummy in let exp = UVar(r_exp,0,0) in r @:= UVar(r_exp,0,vd); unif argsdepth matching depth adepth exp bdepth hd e && let args = list_to_lp_list (C.mkinterval 0 (vd+ano) 0) in unif argsdepth matching depth adepth args bdepth arg e | AppUVar(r,vd,args), App(c,hd,[arg]) when c == Global_symbols.uvarc && matching -> let r_exp = oref C.dummy in let exp = UVar(r_exp,0,0) in r @:= UVar(r_exp,0,vd); unif argsdepth matching depth adepth exp bdepth hd e && let args = list_to_lp_list (C.mkinterval 0 vd 0 @ args) in unif argsdepth matching depth adepth args bdepth arg e | Lam _, (Const c | App(c,_,_)) when c == Global_symbols.uvarc && matching -> begin match eta_contract_flex depth adepth ~argsdepth:adepth e a with | None -> false | Some a -> unif argsdepth matching depth adepth a bdepth b e end | (App _ | Const _ | Builtin _ | Nil | Cons _ | CData _), (Const c | App(c,_,[])) when c == Global_symbols.uvarc && matching -> false (* On purpose we let the fully applied uvarc pass, so that at the * meta level one can unify fronzen constants. One can use the var builtin * to discriminate the two cases, as in "p (uvar F L as X) :- var X, .." *) (* assign *) | _, Arg (i,0) -> begin try let v = hmove ~from:(adepth+depth) ~to_:argsdepth a in [%spy "user:assign" ~rid (fun fmt () -> Fmt.fprintf fmt "%a := %a" (uppterm adepth [] ~argsdepth e) b (uppterm adepth [] ~argsdepth e) v) ()]; e.(i) <- v; true with RestrictionFailure -> false end | UVar(r1,_,0), UVar (r2,_,0) when uvar_uvar_assignment_order r1 r2-> unif argsdepth matching depth bdepth b adepth a e | AppUVar(r1,_,_), UVar (r2,_,0) when uvar_uvar_assignment_order r1 r2 -> unif argsdepth matching depth bdepth b adepth a e | _, UVar (r,origdepth,0) -> begin try let t = if depth = 0 then hmove ~avoid:r ~from:adepth ~to_:origdepth a else (* First step: we lift the l.h.s. to the r.h.s. level *) let a = hmove ~avoid:r ~from:(adepth+depth) ~to_:(bdepth+depth) a in (* Second step: we restrict the l.h.s. *) hmove ~from:(bdepth+depth) ~to_:origdepth a in [%spy "user:assign" ~rid (fun fmt () -> Fmt.fprintf fmt "%a := %a" (uppterm depth [] ~argsdepth e) b (uppterm depth [] ~argsdepth e) t) ()]; r @:= t; true with RestrictionFailure when isLam a -> (* avoid fail occur-check on: x\A x = A | x\y\A y x = A we eta expand and see how it is under the lambdas *) let b = (mkLam @@ mkAppUVar r origdepth [mkConst @@ depth + bdepth]) in let a = hmove ~from:(adepth+depth) ~to_:(bdepth+depth) a in unif argsdepth matching depth bdepth a bdepth b e | RestrictionFailure -> false end | UVar (r,origdepth,0), _ when not matching -> begin try let t = if depth=0 then move ~avoid:r ~argsdepth ~from:bdepth ~to_:origdepth e b else (* First step: we lift the r.h.s. to the l.h.s. level *) let b = move ~avoid:r ~argsdepth ~from:(bdepth+depth) ~to_:(adepth+depth) e b in (* Second step: we restrict the r.h.s. *) hmove ~from:(adepth+depth) ~to_:origdepth b in [%spy "user:assign" ~rid (fun fmt () -> Fmt.fprintf fmt "%a := %a" (uppterm origdepth [] ~argsdepth:0 empty_env) a (uppterm origdepth [] ~argsdepth:0 empty_env) t) ()]; r @:= t; true with RestrictionFailure when isLam b -> (* avoid fail occur-check on: x\A x = A | x\y\A y x = A we eta expand and see how it is under the lambdas *) let a = (mkLam @@ mkAppUVar r origdepth [mkConst @@ depth + adepth]) in let b = move ~argsdepth ~from:(bdepth+depth) ~to_:(adepth+depth) e b in unif argsdepth matching depth adepth a adepth b e | RestrictionFailure -> false end (* XXX simplify either generated UVar(_,_,0) or AppUVar XXX *) | _, Arg (i,args) -> let v = make_lambdas adepth (args - depth) in [%spy "user:assign:simplify:stack:arg" ~rid (fun fmt () -> Fmt.fprintf fmt "%a := %a" (uppterm depth [] ~argsdepth e) (Arg(i,0)) (uppterm depth [] ~argsdepth e) v) ()]; e.(i) <- v; let bdepth = adepth in (* like in deref for arg *) let b = deoptimize_uv_w_args @@ deref_uv ~from:argsdepth ~to_:(bdepth+depth) args v in unif argsdepth matching depth adepth a bdepth b e | UVar(r1,_,a1), UVar (r2,_,a2) when uvar_uvar_assignment_order r1 r2 -> unif argsdepth matching depth bdepth b adepth a e (* TODO argsdepth *) | AppUVar(r1,_,_), UVar (r2,_,a2) when uvar_uvar_assignment_order r1 r2 -> unif argsdepth matching depth bdepth b adepth a e | _, UVar (r,origdepth,args) when args > 0 && match a with UVar(r1,_,_) | AppUVar(r1,_,_) -> r != r1 | _ -> true -> let v = make_lambdas origdepth (args - depth) in [%spy "user:assign:simplify:stack:uvar" ~rid (fun fmt () -> Fmt.fprintf fmt "%a := %a" (uppterm depth [] ~argsdepth e) (UVar(r,origdepth,0)) (uppterm depth [] ~argsdepth e) v) ()]; r @:= v; let b = deoptimize_uv_w_args @@ deref_uv ~from:origdepth ~to_:(bdepth+depth) args v in unif argsdepth matching depth adepth a bdepth b e | UVar (r,origdepth,args), _ when args > 0 && match b with UVar(r1,_,_) | AppUVar(r1,_,_) -> r != r1 | _ -> true -> let v = make_lambdas origdepth (args - depth) in [%spy "user:assign:simplify:heap" ~rid (fun fmt () -> Fmt.fprintf fmt "%a := %a" (uppterm depth [] ~argsdepth e) (UVar(r,origdepth,0)) (uppterm depth [] ~argsdepth e) v) ()]; r @:= v; let a = deoptimize_uv_w_args @@ deref_uv ~from:origdepth ~to_:(adepth+depth) args v in unif argsdepth matching depth adepth a bdepth b e (* HO *) | other, AppArg(i,args) -> let is_llam, args = is_llam adepth args adepth bdepth depth false e in if is_llam then let r = oref C.dummy in e.(i) <- UVar(r,adepth,0); try bind ~argsdepth r adepth args adepth depth delta bdepth false other e with RestrictionFailure -> false else if !delay_hard_unif_problems then begin Fmt.fprintf Fmt.std_formatter "HO unification delayed: %a = %a\n%!" (uppterm depth [] ~argsdepth empty_env) a (uppterm depth [] ~argsdepth e) b ; let r = oref C.dummy in e.(i) <- UVar(r,adepth,0); let kind = Unification {adepth = adepth+depth; env = e; bdepth = bdepth+depth; a; b; matching} in let blockers = match is_flex ~depth:(adepth+depth) other with | None -> [r] | Some r' -> if r==r' then [r] else [r;r'] in CS.declare_new { kind; blockers }; true end else error (error_msg_hard_unif a b) | AppUVar(r2,_,_), (AppUVar (r1,_,_) | UVar (r1,_,_)) when uvar_uvar_assignment_order r1 r2 -> unif argsdepth matching depth bdepth b adepth a e | AppUVar (r, lvl,(args as oargs)), other when not matching -> let is_llam, args = is_llam lvl args adepth bdepth depth true e in if is_llam then try bind ~argsdepth r lvl args adepth depth delta bdepth true other e with RestrictionFailure when isLam other -> let a = mkLam @@ mkAppUVar r lvl (smart_map (move ~argsdepth e ~from:(depth+adepth) ~to_:(depth+adepth+1)) oargs @ [mkConst @@ depth + adepth]) in let b = move ~from:(depth+bdepth) ~to_:(depth+adepth) ~argsdepth e b in unif argsdepth matching depth adepth a adepth b e | RestrictionFailure -> false else if !delay_hard_unif_problems then begin Fmt.fprintf Fmt.std_formatter "HO unification delayed: %a = %a\n%!" (uppterm depth [] ~argsdepth empty_env) a (uppterm depth [] ~argsdepth empty_env) b ; let kind = Unification {adepth = adepth+depth; env = e; bdepth = bdepth+depth; a; b; matching} in let blockers = match is_flex ~depth:(bdepth+depth) other with | None -> [r] | Some r' -> [r;r'] in CS.declare_new { kind; blockers }; true end else error (error_msg_hard_unif a b) | other, AppUVar (r, lvl,(args as oargs)) -> let is_llam, args = is_llam lvl args adepth bdepth depth false e in if is_llam then try bind ~argsdepth r lvl args adepth depth delta bdepth false other e with RestrictionFailure when isLam other -> let b = mkLam @@ mkAppUVar r lvl (smart_map (move ~argsdepth e ~from:(depth+bdepth) ~to_:(depth+bdepth+1)) oargs @ [mkConst @@ depth + bdepth]) in let a = hmove ~from:(depth+adepth) ~to_:(depth+bdepth) a in unif argsdepth matching depth bdepth a bdepth b e | RestrictionFailure -> false else if !delay_hard_unif_problems then begin Fmt.fprintf Fmt.std_formatter "HO unification delayed: %a = %a\n%!" (uppterm depth [] ~argsdepth empty_env) a (uppterm depth [] ~argsdepth e) b ; let kind = Unification {adepth = adepth+depth; env = e; bdepth = bdepth+depth; a; b; matching} in let blockers = match is_flex ~depth:(adepth+depth) other with | None -> [r] | Some r' -> if r==r' then [r] else [r;r'] in CS.declare_new { kind; blockers }; true end else error (error_msg_hard_unif a b) (* recursion *) | App (c1,x2,xs), App (c2,y2,ys) -> (* Compressed cut&past from Const vs Const case below + delta=0 optimization for <c1,c2> and <x2,y2> *) ((delta=0 || c1 < bdepth) && c1=c2 || c1 >= adepth && c1 = c2 + delta) && (delta=0 && x2 == y2 || unif argsdepth matching depth adepth x2 bdepth y2 e) && for_all2 (fun x y -> unif argsdepth matching depth adepth x bdepth y e) xs ys | Builtin (c1,xs), Builtin (c2,ys) -> (* Inefficient comparison *) c1 = c2 && for_all2 (fun x y -> unif argsdepth matching depth adepth x bdepth y e) xs ys | Lam t1, Lam t2 -> unif argsdepth matching (depth+1) adepth t1 bdepth t2 e | Const c1, Const c2 -> if c1 < bdepth then c1=c2 else c1 >= adepth && c1 = c2 + delta (*| Const c1, Const c2 when c1 < bdepth -> c1=c2 | Const c, Const _ when c >= bdepth && c < adepth -> false | Const c1, Const c2 when c1 = c2 + delta -> true*) | CData d1, CData d2 -> CData.equal d1 d2 | Cons(hd1,tl1), Cons(hd2,tl2) -> unif argsdepth matching depth adepth hd1 bdepth hd2 e && unif argsdepth matching depth adepth tl1 bdepth tl2 e | Nil, Nil -> true (* eta *) | Lam t, Const c -> eta_expand_stack argsdepth matching depth adepth t bdepth b c [] e | Const c, Lam t -> eta_expand_heap argsdepth matching depth adepth c [] bdepth b t e | Lam t, App (c,x,xs) -> eta_expand_stack argsdepth matching depth adepth t bdepth b c (x::xs) e | App (c,x,xs), Lam t -> eta_expand_heap argsdepth matching depth adepth c (x::xs) bdepth b t e | _ -> false end] and eta_expand_stack argsdepth matching depth adepth x bdepth b c args e = let extra = mkConst (bdepth+depth) in let motion = move ~argsdepth ~from:(bdepth+depth) ~to_:(bdepth+depth+1) e in let args = smart_map motion args in let eta = C.mkAppL c (args @ [extra]) in unif argsdepth matching (depth+1) adepth x bdepth eta e and eta_expand_heap argsdepth matching depth adepth c args bdepth b x e = let extra = mkConst (adepth+depth) in let motion = hmove ~from:(adepth+depth) ~to_:(adepth+depth+1) in let args = smart_map motion args in let eta= C.mkAppL c (args @ [extra]) in unif argsdepth matching (depth+1) adepth eta bdepth x e ;; (* FISSA PRECEDENZA PER AS e FISSA INDEXING per AS e fai coso generale in unif *) let unif ~argsdepth ~matching (gid[@trace]) adepth e bdepth a b = let res = unif argsdepth matching 0 adepth a bdepth b e in [%spy "dev:unif:out" ~rid Fmt.pp_print_bool res]; [%spy "user:backchain:fail-to" ~rid ~gid ~cond:(not res) (fun fmt () -> let op = if matching then "match" else "unify" in Fmt.fprintf fmt "@[<hov 2>%s@ %a@ with@ %a@]" op (uppterm (adepth) [] ~argsdepth:bdepth empty_env) a (uppterm (bdepth) [] ~argsdepth:bdepth e) b) ()]; res ;; (* Look in Git for Enrico's partially tail recursive but slow unification. The tail recursive version is even slower. *) (* }}} *) let full_deref ~adepth env ~depth t = let rec deref d = function | Const _ as x -> x | Lam r as orig -> let r' = deref (d+1) r in if r == r' then orig else Lam r' | App (n,x,xs) as orig -> let x' = deref d x in let xs' = smart_map (deref d) xs in if x == x' && xs == xs' then orig else App(n, x', xs') | Cons(hd,tl) as orig -> let hd' = deref d hd in let tl' = deref d tl in if hd == hd' && tl == tl' then orig else Cons(hd', tl') | Nil as x -> x | Discard as x -> x | Arg (i,ano) when env.(i) != C.dummy -> deref d (deref_uv ~from:adepth ~to_:d ano env.(i)) | Arg _ as x -> x | AppArg (i,args) when env.(i) != C.dummy -> deref d (deref_appuv ~from:adepth ~to_:d args env.(i)) | AppArg (i,args) as orig -> let args' = smart_map (deref d) args in if args == args' then orig else AppArg (i, args') | UVar (r,lvl,ano) when !!r != C.dummy -> deref d (deref_uv ~from:lvl ~to_:d ano !!r) | UVar _ as x -> x | AppUVar (r,lvl,args) when !!r != C.dummy -> deref d (deref_appuv ~from:lvl ~to_:d args !!r) | AppUVar (r,lvl,args) as orig -> let args' = smart_map (deref d) args in if args == args' then orig else AppUVar (r,lvl,args') | Builtin(c,xs) as orig -> let xs' = smart_map (deref d) xs in if xs == xs' then orig else Builtin(c,xs') | CData _ as x -> x in deref depth t type assignment = uvar_body * int * term let shift_bound_vars ~depth ~to_ t = let shift_db d n = if n < depth then n else n + to_ - depth in let rec shift d = function | Const n as x -> let m = shift_db d n in if m = n then x else mkConst m | Lam r as orig -> let r' = shift (d+1) r in if r == r' then orig else Lam r' | App (n,x,xs) as orig -> let x' = shift d x in let xs' = smart_map (shift d) xs in if x == x' && xs == xs' then orig else App(shift_db d n, x', xs') | Cons(hd,tl) as orig -> let hd' = shift d hd in let tl' = shift d tl in if hd == hd' && tl == tl' then orig else Cons(hd', tl') | Nil as x -> x | Discard as x -> x | Arg _ as x -> x | AppArg (i,args) as orig -> let args' = smart_map (shift d) args in if args == args' then orig else AppArg (i, args') | (UVar (r,_,_) | AppUVar(r,_,_)) when !!r != C.dummy -> anomaly "shift_bound_vars: non-derefd term" | UVar _ as x -> x | AppUVar (r,lvl,args) as orig -> let args' = smart_map (shift d) args in if args == args' then orig else AppUVar (r,lvl,args') | Builtin(c,xs) as orig -> let xs' = smart_map (shift d) xs in if xs == xs' then orig else Builtin(c,xs') | CData _ as x -> x in if depth = to_ then t else shift depth t let map_free_vars ~map ~depth ~to_ t = let shift_bound = depth - to_ in let shift_db d n = if n < 0 then n else if n < depth then try C.Map.find n map with Not_found -> error (Printf.sprintf "The term cannot be put in the desired context since it contains name: %s" @@ C.show n) else if n >= depth && n - depth <= d then n - shift_bound else error (Printf.sprintf "The term cannot be put in the desired context since it contains name: %s" @@ C.show n) in let rec shift d = function | Const n as x -> let m = shift_db d n in if m = n then x else mkConst m | Lam r as orig -> let r' = shift (d+1) r in if r == r' then orig else Lam r' | App (n,x,xs) as orig -> let x' = shift d x in let xs' = smart_map (shift d) xs in if x == x' && xs == xs' then orig else App(shift_db d n, x', xs') | Cons(hd,tl) as orig -> let hd' = shift d hd in let tl' = shift d tl in if hd == hd' && tl == tl' then orig else Cons(hd', tl') | Nil as x -> x | Discard as x -> x | Arg _ as x -> x | AppArg (i,args) as orig -> let args' = smart_map (shift d) args in if args == args' then orig else AppArg (i, args') | (UVar (r,_,_) | AppUVar(r,_,_)) when !!r != C.dummy -> anomaly "map_free_vars: non-derefd term" | UVar(r,lvl,ano) -> UVar(r,min lvl to_,ano) | AppUVar (r,lvl,args) -> AppUVar (r,min lvl to_,smart_map (shift d) args) | Builtin(c,xs) as orig -> let xs' = smart_map (shift d) xs in if xs == xs' then orig else Builtin(c,xs') | CData _ as x -> x in if depth = to_ && C.Map.is_empty map then t else shift 0 t let eta_contract_flex ~depth t = eta_contract_flex depth 0 ~argsdepth:0 empty_env t [@@inline] end (* }}} *) open HO let _ = do_deref := deref_uv;; let _ = do_app_deref := deref_appuv;; (* Built-in predicates and their FFI *************************************** *) module FFI = struct let builtins : Data.BuiltInPredicate.builtin_table ref = Fork.new_local (Hashtbl.create 17) let lookup c = Hashtbl.find !builtins c let type_err ~depth bname n ty t = type_error begin "builtin " ^ bname ^ ": " ^ (if n = 0 then "" else string_of_int n ^ "th argument: ") ^ "expected: " ^ ty ^ " got" ^ match t with | None -> "_" | Some t -> match t with | CData c -> Format.asprintf " %s: %a" (CData.name c) (Pp.uppterm depth [] ~argsdepth:0 empty_env) t | _ -> Format.asprintf ": %a" (Pp.uppterm depth [] ~argsdepth:0 empty_env) t end let wrap_type_err bname n f x = try f x with Conversion.TypeErr(ty,depth,t) -> type_err ~depth bname n (Conversion.show_ty_ast ty) (Some t) let arity_err ~depth bname n t = type_error ("builtin " ^ bname ^ ": " ^ match t with | None -> string_of_int n ^ "th argument is missing" | Some t -> "too many arguments at: " ^ Format.asprintf "%a" (Pp.uppterm depth [] ~argsdepth:0 empty_env) t) let out_of_term ~depth readback n bname state t = match deref_head ~depth t with | Discard -> Data.BuiltInPredicate.Discard | _ -> Data.BuiltInPredicate.Keep let in_of_term ~depth readback n bname state t = wrap_type_err bname n (readback ~depth state) t let inout_of_term ~depth readback n bname state t = wrap_type_err bname n (readback ~depth state) t let mk_out_assign ~depth embed bname state input v output = match output, input with | None, Data.BuiltInPredicate.Discard -> state, [] | Some _, Data.BuiltInPredicate.Discard -> state, [] (* We could warn that such output was generated without being required *) | Some t, Data.BuiltInPredicate.Keep -> let state, t, extra = embed ~depth state t in state, extra @ [Conversion.Unify(v,t)] | None, Data.BuiltInPredicate.Keep -> state, [] let mk_inout_assign ~depth embed bname state input v output = match output with | None -> state, [] | Some t -> let state, t, extra = embed ~depth state (Data.BuiltInPredicate.Data t) in state, extra @ [Conversion.Unify(v,t)] let in_of_termC ~depth readback n bname hyps constraints state t = wrap_type_err bname n (readback ~depth hyps constraints state) t let inout_of_termC = in_of_termC let mk_out_assignC ~depth embed bname hyps constraints state input v output = match output, input with | None, Data.BuiltInPredicate.Discard -> state, [] | Some _, Data.BuiltInPredicate.Discard -> state, [] (* We could warn that such output was generated without being required *) | Some t, Data.BuiltInPredicate.Keep -> let state, t, extra = embed ~depth hyps constraints state t in state, extra @ [Conversion.Unify(v,t)] | None, Data.BuiltInPredicate.Keep -> state, [] let mk_inout_assignC ~depth embed bname hyps constraints state input v output = match output with | Some t -> let state, t, extra = embed ~depth hyps constraints state (Data.BuiltInPredicate.Data t) in state, extra @ [Conversion.Unify(v,t)] | None -> state, [] let map_acc f s l = let rec aux acc extra s = function | [] -> s, List.rev acc, List.(concat (rev extra)) | x :: xs -> let s, x, gls = f s x in aux (x :: acc) (gls :: extra) s xs in aux [] [] s l let call (Data.BuiltInPredicate.Pred(bname,ffi,compute)) ~once ~depth hyps constraints state data = let rec aux : type i o h c. (i,o,h,c) Data.BuiltInPredicate.ffi -> h -> c -> compute:i -> reduce:(State.t -> o -> State.t * Conversion.extra_goals) -> term list -> int -> State.t -> Conversion.extra_goals list -> State.t * Conversion.extra_goals = fun ffi ctx constraints ~compute ~reduce data n state extra -> match ffi, data with | Data.BuiltInPredicate.Easy _, [] -> let result = wrap_type_err bname 0 (fun () -> compute ~depth) () in let state, l = reduce state result in state, List.(concat (rev extra) @ rev l) | Data.BuiltInPredicate.Read _, [] -> let result = wrap_type_err bname 0 (compute ~depth ctx constraints) state in let state, l = reduce state result in state, List.(concat (rev extra) @ rev l) | Data.BuiltInPredicate.Full _, [] -> let state, result, gls = wrap_type_err bname 0 (compute ~depth ctx constraints) state in let state, l = reduce state result in state, List.(concat (rev extra)) @ gls @ List.rev l | Data.BuiltInPredicate.FullHO _, [] -> let state, result, gls = wrap_type_err bname 0 (compute ~once ~depth ctx constraints) state in let state, l = reduce state result in state, List.(concat (rev extra)) @ gls @ List.rev l | Data.BuiltInPredicate.VariadicIn(_,{ ContextualConversion.readback }, _), data -> let state, i, gls = map_acc (in_of_termC ~depth readback n bname ctx constraints) state data in let state, rest = wrap_type_err bname 0 (compute i ~depth ctx constraints) state in let state, l = reduce state rest in state, List.(gls @ concat (rev extra) @ rev l) | Data.BuiltInPredicate.VariadicOut(_,{ ContextualConversion.embed; readback }, _), data -> let i = List.map (out_of_term ~depth readback n bname state) data in let state, (rest, out) = wrap_type_err bname 0 (compute i ~depth ctx constraints) state in let state, l = reduce state rest in begin match out with | Some out -> let state, ass = map_acc3 (mk_out_assignC ~depth embed bname ctx constraints) state i data out in state, List.(concat (rev extra) @ rev (concat ass) @ l) | None -> state, List.(concat (rev extra) @ rev l) end | Data.BuiltInPredicate.VariadicInOut(_,{ ContextualConversion.embed; readback }, _), data -> let state, i, gls = map_acc (inout_of_termC ~depth readback n bname ctx constraints) state data in let state, (rest, out) = wrap_type_err bname 0 (compute i ~depth ctx constraints) state in let state, l = reduce state rest in begin match out with | Some out -> let state, ass = map_acc3 (mk_inout_assignC ~depth embed bname ctx constraints) state i data out in state, List.(gls @ concat (rev extra) @ rev (concat ass) @ l) | None -> state, List.(gls @ concat (rev extra) @ rev l) end | Data.BuiltInPredicate.CIn({ ContextualConversion.readback }, _, ffi), t :: rest -> let state, i, gls = in_of_termC ~depth readback n bname ctx constraints state t in aux ffi ctx constraints ~compute:(compute i) ~reduce rest (n + 1) state (gls :: extra) | Data.BuiltInPredicate.COut({ ContextualConversion.embed; readback }, _, ffi), t :: rest -> let i = out_of_term ~depth readback n bname state t in let reduce state (rest, out) = let state, l = reduce state rest in let state, ass = mk_out_assignC ~depth embed bname ctx constraints state i t out in state, ass @ l in aux ffi ctx constraints ~compute:(compute i) ~reduce rest (n + 1) state extra | Data.BuiltInPredicate.CInOut({ ContextualConversion.embed; readback }, _, ffi), t :: rest -> let state, i, gls = inout_of_termC ~depth readback n bname ctx constraints state t in let reduce state (rest, out) = let state, l = reduce state rest in let state, ass = mk_inout_assignC ~depth embed bname ctx constraints state i t out in state, ass @ l in aux ffi ctx constraints ~compute:(compute i) ~reduce rest (n + 1) state (gls :: extra) | Data.BuiltInPredicate.In({ Conversion.readback }, _, ffi), t :: rest -> let state, i, gls = in_of_term ~depth readback n bname state t in aux ffi ctx constraints ~compute:(compute i) ~reduce rest (n + 1) state (gls :: extra) | Data.BuiltInPredicate.Out({ Conversion.embed; readback }, _, ffi), t :: rest -> let i = out_of_term ~depth readback n bname state t in let reduce state (rest, out) = let state, l = reduce state rest in let state, ass = mk_out_assign ~depth embed bname state i t out in state, ass @ l in aux ffi ctx constraints ~compute:(compute i) ~reduce rest (n + 1) state extra | Data.BuiltInPredicate.InOut({ Conversion.embed; readback }, _, ffi), t :: rest -> let state, i, gls = inout_of_term ~depth readback n bname state t in let reduce state (rest, out) = let state, l = reduce state rest in let state, ass = mk_inout_assign ~depth embed bname state i t out in state, ass @ l in aux ffi ctx constraints ~compute:(compute i) ~reduce rest (n + 1) state (gls :: extra) | _, t :: _ -> arity_err ~depth bname n (Some t) | _, [] -> arity_err ~depth bname n None in let rec aux_ctx : type i o h c. (i,o,h,c) Data.BuiltInPredicate.ffi -> (h,c) ContextualConversion.ctx_readback = function | Data.BuiltInPredicate.FullHO(f,_) -> f | Data.BuiltInPredicate.Full(f,_) -> f | Data.BuiltInPredicate.Read(f,_) -> f | Data.BuiltInPredicate.VariadicIn(f,_,_) -> f | Data.BuiltInPredicate.VariadicOut(f,_,_) -> f | Data.BuiltInPredicate.VariadicInOut(f,_,_) -> f | Data.BuiltInPredicate.Easy _ -> ContextualConversion.unit_ctx | Data.BuiltInPredicate.In(_,_,rest) -> aux_ctx rest | Data.BuiltInPredicate.Out(_,_,rest) -> aux_ctx rest | Data.BuiltInPredicate.InOut(_,_,rest) -> aux_ctx rest | Data.BuiltInPredicate.CIn(_,_,rest) -> aux_ctx rest | Data.BuiltInPredicate.COut(_,_,rest) -> aux_ctx rest | Data.BuiltInPredicate.CInOut(_,_,rest) -> aux_ctx rest in let reduce state _ = state, [] in let state, ctx, csts, gls_ctx = aux_ctx ffi ~depth hyps constraints state in let state, gls = aux ffi ctx csts ~compute ~reduce data 1 state [] in state, gls_ctx @ gls ;; end (****************************************************************************** Indexing ******************************************************************************) module Indexing = struct (* {{{ *) (* These are sorted wrt lex_insertion -2 -1 0 1.-1 1.-2 1 1.+2 1.+1 2 The idea being that clause "a" to be inserted w.r.t. "b" takes as timestamp the one of "b" followerd by the timestamp. If "a" has to be before, the timestamp is made negative. *) let lex_insertion l1 l2 = let rec lex_insertion fst l1 l2 = match l1, l2 with | [], [] -> 0 | x :: l1, y :: l2 when not fst -> let r = if x < 0 && y < 0 || x > 0 && y > 0 then y - x else x - y in if r = 0 then lex_insertion false l1 l2 else r | x1 :: l1, x2 :: l2 -> let x = x1 - x2 in if x = 0 then lex_insertion false l1 l2 else x | [], ys -> lex_insertion false [0] ys | xs, [] -> lex_insertion false xs [0] in lex_insertion true l1 l2 let mustbevariablec = min_int (* uvar or uvar t or uvar l t *) let ppclause f ~hd { depth; args = args; hyps = hyps } = Fmt.fprintf f "@[<hov 1>%a :- %a.@]" (uppterm ~min_prec:(Elpi_parser.Parser_config.appl_precedence+1) depth [] ~argsdepth:0 empty_env) (C.mkAppL hd args) (pplist (uppterm ~min_prec:(Elpi_parser.Parser_config.appl_precedence+1) depth [] ~argsdepth:0 empty_env) ", ") hyps (** [tail_opt L] returns: [match L with [] -> [] | x :: xs -> xs] *) let tail_opt = function | [] -> [] | _ :: xs -> xs let hd_opt = function | x :: _ -> Mode.get_head x | _ -> Output type clause_arg_classification = | Variable | MustBeVariable | Rigid of constant * Mode.t let rec classify_clause_arg ~depth matching t = (* Format.eprintf "index %a\n%!" (ppterm depth [] ~argsdepth:depth empty_env) t; *) match deref_head ~depth t with | Const k when k == Global_symbols.uvarc -> MustBeVariable | Const k -> Rigid(k,matching) | Nil -> Rigid (Global_symbols.nilc,matching) | Cons _ -> Rigid (Global_symbols.consc,matching) | App (k,_,_) when k == Global_symbols.uvarc -> MustBeVariable | App (k,a,_) when k == Global_symbols.asc -> classify_clause_arg ~depth matching a | App (k,_,_) -> Rigid (k,matching) | Builtin (k,_) -> Rigid (const_of_builtin_predicate k,matching) | Lam _ -> Variable (* loose indexing to enable eta *) | Arg _ | UVar _ | AppArg _ | AppUVar _ | Discard -> Variable | CData d -> let hash = -(CData.hash d) in if hash > mustbevariablec then Rigid (hash,matching) else Rigid (hash+1,matching) (** [classify_clause_argno ~depth N mode L] where L is the arguments of the clause. Returns the classification of the Nth element of L wrt to the Nth mode for the TwoLevelIndex *) let rec classify_clause_argno ~depth argno mode = function | [] -> Variable | x :: _ when argno == 0 -> classify_clause_arg ~depth (hd_opt mode) x | _ :: xs -> classify_clause_argno ~depth (argno-1) (tail_opt mode) xs let dec_to_bin2 num = let rec aux x = if x==1 then ["1"] else if x==0 then ["0"] else if x mod 2 == 1 then "1" :: aux (x/2) else "0" :: aux (x/2) in String.concat "" (List.rev (aux num)) [@@warning "-32"] let hash_bits = Sys.int_size - 1 (* the sign *) (** Hashing function for clause and queries depending on the boolean [is_goal]. This is done by hashing the parameters wrt to Sys.int_size - 1 (see [hash_bits]) *) let hash_arg_list is_goal hd ~depth args mode spec = let nargs = List.(length (filter (fun x -> x > 0) spec)) in (* we partition equally, that may not be smart, but is simple ;-) *) let arg_size = hash_bits / nargs in let hash size k = let modulus = 1 lsl size in let kabs = Hashtbl.hash k in let h = kabs mod modulus in [%spy "dev:index:subhash-const" ~rid C.pp k pp_string (dec_to_bin2 h)]; h in let all_1 size = max_int lsr (hash_bits - size) in let all_0 size = 0 in let shift slot_size position x = x lsl (slot_size * position) in let rec aux off acc args mode spec = match args, spec with | _, [] -> acc | [], _ -> acc | x::xs, n::spec -> let h = aux_arg arg_size (hd_opt mode) n x in aux (off + arg_size) (acc lor (h lsl off)) xs (tail_opt mode) spec and aux_arg size mode deep arg = let h = match deref_head ~depth arg with | App (k,a,_) when k == Global_symbols.asc -> aux_arg size mode deep a | Const k when k == Global_symbols.uvarc -> hash size mustbevariablec | App(k,_,_) when k == Global_symbols.uvarc && deep = 1 -> hash size mustbevariablec | Const k -> hash size k | App(k,_,_) when deep = 1 -> hash size k | App(k,x,xs) -> let size = size / (List.length xs + 2) in let self = aux_arg size mode (deep-1) in let shift = shift size in (hash size k) lor (shift 1 (self x)) lor List.(fold_left (lor) 0 (mapi (fun i x -> shift (i+2) (self x)) xs)) | (UVar _ | AppUVar _) when mode == Input && is_goal -> hash size mustbevariablec | (UVar _ | AppUVar _) when mode == Input -> all_1 size | (UVar _ | AppUVar _) -> if is_goal then all_0 size else all_1 size | (Arg _ | AppArg _) -> all_1 size | Nil -> hash size Global_symbols.nilc | Cons (x,xs) -> let size = size / 2 in let self = aux_arg size mode (deep-1) in let shift = shift size in (hash size Global_symbols.consc) lor (shift 1 (self x)) | CData s -> hash size (CData.hash s) | Lam _ -> all_1 size | Discard -> all_1 size | Builtin(k,xs) -> let size = size / (List.length xs + 1) in let self = aux_arg size mode (deep-1) in let shift = shift size in (hash size (const_of_builtin_predicate k)) lor List.(fold_left (lor) 0 (mapi (fun i x -> shift (i+1) (self x)) xs)) in [%spy "dev:index:subhash" ~rid (fun fmt () -> Fmt.fprintf fmt "%s: %d: %s: %a" (if is_goal then "goal" else "clause") size (dec_to_bin2 h) (uppterm depth [] ~argsdepth:0 empty_env) arg) ()]; h in let h = aux 0 0 args mode spec in [%spy "dev:index:hash" ~rid (fun fmt () -> Fmt.fprintf fmt "%s: %s: %a" (if is_goal then "goal" else "clause") (dec_to_bin2 h) (pplist ~boxed:true (uppterm depth [] ~argsdepth:0 empty_env) " ") (Const hd :: args)) ()]; h let hash_clause_arg_list = hash_arg_list false let hash_goal_arg_list = hash_arg_list true (* returns the maximal length of any sub_list this operation is done at compile time per each clause being index for example the term (app['a','b',app['c','d','e'], 'f', app['a','b','c','d','e','f']]) has three lists L1 = ['a','b',app['c','d','e'], 'f', app['a','b','c','d','e','f'] L2 = ['c','d','e'] L3 = app['a','b','c','d','e','f'] and the longest one has length 6 *) let rec max_list_length acc = function | Nil -> acc | Cons (a, (UVar (_, _, _) | AppUVar (_, _, _) | Arg (_, _) | AppArg (_, _) | Discard)) -> let alen = max_list_length 0 a in max (acc+2) alen | Cons (a, b)-> let alen = max_list_length 0 a in let blen = max_list_length (acc+1) b in max blen alen | App (_,x,xs) -> List.fold_left (fun acc x -> max acc (max_list_length 0 x)) acc (x::xs) | Builtin (_, xs) -> List.fold_left (fun acc x -> max acc (max_list_length 0 x)) acc xs | Lam t -> max_list_length acc t | Discard | Const _ |CData _ | UVar (_, _, _) | AppUVar (_, _, _) | Arg (_, _) | AppArg (_, _) -> acc let max_lists_length = List.fold_left (fun acc e -> max (max_list_length 0 e) acc) 0 (** [arg_to_trie_path ~safe ~depth ~is_goal args arg_depths arg_modes mp max_list_length] returns the path represetation of a term to be used in indexing with trie. args, args_depths and arg_modes are the lists of respectively the arguments, the depths and the modes of the current term to be indexed. ~is_goal is used to know if we are encoding the path for instance retriaval or for clause insertion in the trie. In the former case, each argument we add a special mkInputMode/mkOutputMode node before each argument to be indexed. This special node is used during instance retrival to know the mode of the current argument - mp is the max_path size of any path in the index used to truncate the goal - max_list_length is the length of the longest sublist in each term of args *) type check_mut_excl = | Off of Mode.hos | On [@@deriving show] let arg_to_trie_path ~check_mut_excl ~safe ~depth ~is_goal args arg_depths args_depths_ar mp (max_list_length':int) : Discrimination_tree.Path.t = let open Discrimination_tree in let path_length = mp + Array.length args_depths_ar + 1 in let path = Path.make path_length mkPathEnd in let current_ar_pos = ref 0 in let current_user_depth = ref 0 in let current_min_depth = ref max_int in let update_current_min_depth d = if not is_goal then current_min_depth := min !current_min_depth d in (* Invariant: !current_ar_pos < Array.length args_depths_ar *) let update_ar depth = if not is_goal then begin let old_max = Array.unsafe_get args_depths_ar !current_ar_pos in let current_max = !current_user_depth - depth in if old_max < current_max then Array.unsafe_set args_depths_ar !current_ar_pos current_max end in let rec list_to_trie_path ~safe ~depth ~h path_depth (len: int) (t: term) : unit = match deref_head ~depth t with | Nil -> Path.emit path mkListEnd; update_current_min_depth path_depth | Cons (a, b) -> (* heuristic: we limit the size of the list to at most 30 *) (* this aims to control the width of the path, *) (* or equivalently the number of children of *) (* a specific node. *) (* for example, the term `app[1,...,100]` with depth 2, *) (* has the node `app` with arity `1` as first*) (* cell, then come the elment of the list *) (* up to the 30^th elemebt *) if is_goal && h >= max_list_length' then (Path.emit path mkListTailVariableUnif; update_current_min_depth path_depth) else (main ~safe ~depth a path_depth; list_to_trie_path ~depth ~safe ~h:(h+1) path_depth (len+1) b) (* These cases can come from terms like `[_ | _]`, `[_ | A]` ... *) | UVar _ | AppUVar _ | Arg _ | AppArg _ | Discard -> Path.emit path mkListTailVariable; update_current_min_depth path_depth (* One could write the following: type f list int. p [1,2,3,4 | f]. *) | App _ | Const _ -> Path.emit path mkListTailVariable; update_current_min_depth path_depth | Builtin _ | CData _ | Lam _ -> type_error (Format.asprintf "[DT]: not a list: %a" (Pp.ppterm depth [] ~argsdepth:0 Data.empty_env) (deref_head ~depth t)) and emit_mode is_goal mode = if is_goal then Path.emit path mode (** gives the path representation of a list of sub-terms *) and arg_to_trie_path_aux ~safe ~depth t_list path_depth : unit = if path_depth = 0 then update_current_min_depth path_depth else match t_list with | [] -> update_current_min_depth path_depth | hd :: tl -> main ~safe ~depth hd path_depth; arg_to_trie_path_aux ~safe ~depth tl path_depth (** gives the path representation of a term *) and main ~safe ~depth t path_depth : unit = if path_depth = 0 then update_current_min_depth path_depth else if is_goal && Path.get_builder_pos path >= path_length then (Path.emit path mkAny; update_current_min_depth path_depth) else let path_depth = path_depth - 1 in match deref_head ~depth t with | Const k when k == Global_symbols.uvarc -> Path.emit path mkUvarVariable; update_current_min_depth path_depth | Const k when safe -> Path.emit path @@ mkConstant ~safe ~data:k ~arity:0; update_current_min_depth path_depth | Const k -> Path.emit path @@ mkConstant ~safe ~data:k ~arity:0; update_current_min_depth path_depth | CData d -> Path.emit path @@ mkPrimitive d; update_current_min_depth path_depth | App (k,_,_) when k == Global_symbols.uvarc -> Path.emit path @@ mkVariable; update_current_min_depth path_depth | App (k,a,_) when k == Global_symbols.asc -> main ~safe ~depth a (path_depth+1) | Lam _ -> Path.emit path mkAny; update_current_min_depth path_depth (* loose indexing to enable eta *) | Arg _ | UVar _ | AppArg _ | AppUVar _ | Discard -> Path.emit path @@ mkVariable; update_current_min_depth path_depth | Builtin (k,tl) -> Path.emit path @@ mkConstant ~safe ~data:(const_of_builtin_predicate k) ~arity:(if path_depth = 0 then 0 else List.length tl); arg_to_trie_path_aux ~safe ~depth tl path_depth | App (k, x, xs) -> let arg_length = if path_depth = 0 then 0 else List.length xs + 1 in Path.emit path @@ mkConstant ~safe ~data:k ~arity:arg_length; main ~safe ~depth x path_depth; arg_to_trie_path_aux ~safe ~depth xs path_depth | Nil -> Path.emit path mkListNil; update_current_min_depth path_depth | Cons (x,xs) -> Path.emit path mkListHead; main ~safe ~depth x (path_depth + 1); list_to_trie_path ~safe ~depth ~h:1 (path_depth + 1) 0 xs (** builds the sub-path of a sublist of arguments of the current clause *) and make_sub_path arg_hd arg_tl arg_depth_hd arg_depth_tl (mode_hd:Mode.t) mode_tl = emit_mode is_goal (match mode_hd with Input -> mkInputMode | _ -> mkOutputMode); begin if not is_goal then begin current_user_depth := arg_depth_hd; current_min_depth := max_int; main ~safe ~depth arg_hd arg_depth_hd; update_ar !current_min_depth; end else main ~safe ~depth arg_hd (Array.unsafe_get args_depths_ar !current_ar_pos) end; incr current_ar_pos; aux ~safe ~depth is_goal arg_tl arg_depth_tl mode_tl (** main function: build the path of the arguments received in entry *) and aux ~safe ~depth is_goal args arg_depths mode = match args, arg_depths, mode with | _, [], _ -> () | arg_hd :: arg_tl, arg_depth_hd :: arg_depth_tl, (On | Off []) -> make_sub_path arg_hd arg_tl arg_depth_hd arg_depth_tl Output On | arg_hd :: arg_tl, arg_depth_hd :: arg_depth_tl, Off (mode_hd :: mode_tl) -> make_sub_path arg_hd arg_tl arg_depth_hd arg_depth_tl (Mode.get_head mode_hd) (Off mode_tl) | _, _ :: _,_ -> anomaly "Invalid Index length" in begin if args == [] then emit_mode is_goal mkOutputMode else aux ~safe ~depth is_goal args (if is_goal then Array.to_list args_depths_ar else arg_depths) check_mut_excl end; Path.stop path let make_new_Map_snd_level_index argno mode = TwoLevelIndex { argno; mode; all_clauses = Bl.empty (); flex_arg_clauses = Bl.empty (); arg_idx = Ptmap.empty; } let path_for_dtree ~depth ~check_mut_excl args_idx args = let max_depths = Discrimination_tree.max_depths args_idx in let max_path = Discrimination_tree.max_path args_idx in let max_list_length = max_lists_length args in let arg_depths = Discrimination_tree.user_upper_bound_depth args_idx in (* Format.printf "[%d] Going to index [%a]\n%!" max_list_length (pplist pp_int ",") arg_depths; *) let path = arg_to_trie_path ~depth ~safe:true ~is_goal:false args arg_depths max_depths ~check_mut_excl max_path max_list_length in (* Format.eprintf "Path is %a@." Discrimination_tree.Path.pp path; *) path, max_list_length, max_depths let add_clause_to_snd_lvl_idx ~depth ~insert predicate clause = function | TwoLevelIndex { all_clauses; argno; mode; flex_arg_clauses; arg_idx; } -> begin match classify_clause_argno ~depth argno mode clause.args with | Variable -> (* X: matches both rigid and flexible terms *) TwoLevelIndex { argno; mode; all_clauses = insert clause all_clauses; flex_arg_clauses = insert clause flex_arg_clauses; arg_idx = Ptmap.map (fun l_rev -> insert clause l_rev) arg_idx; } | MustBeVariable -> (* uvar: matches only flexible terms (or itself at the meta level) *) let clauses = try Ptmap.find mustbevariablec arg_idx with Not_found -> Bl.empty () in TwoLevelIndex { argno; mode; all_clauses = insert clause all_clauses; flex_arg_clauses; arg_idx = Ptmap.add mustbevariablec (insert clause clauses) arg_idx; } | Rigid (arg_hd,arg_mode) -> (* t: a rigid term matches flexible terms only in unification mode *) let clauses = try Ptmap.find arg_hd arg_idx with Not_found -> flex_arg_clauses in let all_clauses = if arg_mode = Input then all_clauses else insert clause all_clauses in TwoLevelIndex { argno; mode; all_clauses; flex_arg_clauses; arg_idx = Ptmap.add arg_hd (insert clause clauses) arg_idx; } end | BitHash { mode; args; args_idx } -> let hash = hash_clause_arg_list predicate ~depth clause.args mode args in let clauses = try Ptmap.find hash args_idx with Not_found -> Bl.empty () in BitHash { mode; args; args_idx = Ptmap.add hash (insert clause clauses) args_idx } | IndexWithDiscriminationTree {mode; arg_depths; args_idx; } -> let path, max_list_length, max_depths = path_for_dtree ~depth ~check_mut_excl:(Off mode) args_idx clause.args in [%spy "dev:disc-tree:depth-path" ~rid pp_string "Inst: MaxDepths " (pplist pp_int "") (Array.to_list max_depths)]; IndexWithDiscriminationTree { mode; arg_depths; args_idx = Discrimination_tree.index args_idx path clause ~max_list_length } let compile_time_tick x = x + 1 let run_time_tick x = x - 1 let rec add1clause_runtime ~depth { idx; time; times } predicate clause = try let snd_lvl_idx = Ptmap.find predicate idx in let time = run_time_tick time in clause.timestamp <- [time]; let snd_lvl_idx = add_clause_to_snd_lvl_idx ~depth ~insert:Bl.cons predicate clause snd_lvl_idx in { times; time; idx = Ptmap.add predicate snd_lvl_idx idx } with | Not_found -> (* Unknown predicate, we could detect this statically and forbid it *) let idx = Ptmap.add predicate (make_new_Map_snd_level_index 0 []) idx in add1clause_runtime ~depth { idx; time; times } predicate clause let add_clauses ~depth clauses idx = (* pplist (fun fmt (hd, b) -> ppclause fmt hd b) ";" Fmt.std_formatter clauses; *) (* let t1 = Unix.gettimeofday () in *) let idx = List.fold_left (fun m (p,c) -> add1clause_runtime ~depth m p c) idx clauses in (* let t2 = Unix.gettimeofday () in *) (* pp_string Fmt.std_formatter (Printf.sprintf "\nTime taken by add_clauses is %f\n" (t2-.t1)); *) idx let add_clauses ~depth clauses clauses_src { index; src } = { index = add_clauses ~depth clauses index; src = List.rev clauses_src @ src } let add_to_times loc name time predicate times = match name with | None -> times | Some id -> if StrMap.mem id times then error ?loc ("duplicate clause name " ^ id) else StrMap.add id (time,predicate) times let time_of loc x times = try StrMap.find x times with Not_found -> error ?loc ("cannot graft, clause " ^ x ^ " not found") let remove_from_times id times = StrMap.remove id times let remove_clause_in_snd_lvl_idx p = function | TwoLevelIndex { argno; mode; all_clauses; flex_arg_clauses; arg_idx; } -> TwoLevelIndex { argno; mode; all_clauses = Bl.remove p all_clauses; flex_arg_clauses = Bl.remove p flex_arg_clauses; arg_idx = Ptmap.map (Bl.remove p) arg_idx; } | BitHash { mode; args; args_idx } -> BitHash { mode; args; args_idx = Ptmap.map (Bl.remove p) args_idx } | IndexWithDiscriminationTree {mode; arg_depths; args_idx; } -> IndexWithDiscriminationTree { mode; arg_depths; args_idx = Discrimination_tree.remove p args_idx; } let has_bang (t : term list) : bool = let rec aux t = match t with | [] -> false | Builtin (Cut, []) :: _ -> true | Builtin (Pi, [Lam b]) :: xs | Builtin (Sigma, [Lam b]) :: xs -> aux [b] || aux xs | Builtin (Impl, [h; l]) :: xs -> aux [l] || aux xs | Builtin (And, l) :: xs -> aux l || aux xs | _ :: xs -> aux xs in aux t let add1clause_overlap_runtime ~depth (idx:pred_info C.Map.t) predicate ~time clause = match C.Map.find predicate idx with | { overlap = Allowed } -> None, idx | { overlap = Forbidden dt } as pred_info -> clause.timestamp <- [time]; let path, max_list_length, max_depths = path_for_dtree ~depth ~check_mut_excl:(Off pred_info.mode) dt clause.args in let cl_overlap = { overlap_loc = clause.loc; has_cut=has_bang clause.hyps; timestamp=clause.timestamp } in let dt = Discrimination_tree.index dt ~max_list_length path cl_overlap in Some cl_overlap, C.Map.add predicate ({pred_info with overlap = Forbidden dt}) idx | exception Not_found -> None, idx let update_overlap ~det_check overlap ~depth index clause = match det_check with | None -> None, overlap | Some time -> let t0 = Unix.gettimeofday () in let rc = match overlap.overlap with | Allowed -> None, overlap | Forbidden dt -> let arg_depths, mode = match index with | TwoLevelIndex { mode; argno } -> List.init (argno+1) (fun j -> if j = argno-1 then 1 else 0), mode | BitHash { mode; args = arg_depths } -> arg_depths, mode | IndexWithDiscriminationTree {mode; arg_depths} -> arg_depths, mode in let path, max_list_length, max_depths = path_for_dtree ~depth ~check_mut_excl:(Off mode) dt clause.args in let has_cut = has_bang clause.hyps in let overlap_clause = { overlap_loc = clause.loc; has_cut; timestamp=clause.timestamp } in let dt = Discrimination_tree.index dt path overlap_clause ~max_list_length in Some overlap_clause, { overlap with overlap = Forbidden dt } in let t1 = Unix.gettimeofday () in time := !time +. (t1 -. t0); rc let rec add1clause_compile_time ~det_check ~depth { idx; time; times } overlap ~graft predicate clause name = try let snd_lvl_idx = Ptmap.find predicate idx in let time = compile_time_tick time in match graft with | None -> let timestamp = [time] in let times = add_to_times clause.loc name timestamp predicate times in clause.timestamp <- timestamp; let snd_lvl_idx = add_clause_to_snd_lvl_idx ~depth ~insert:Bl.rcons predicate clause snd_lvl_idx in { times; time; idx = Ptmap.add predicate snd_lvl_idx idx }, update_overlap ~det_check overlap ~depth snd_lvl_idx clause | Some (Ast.Structured.Remove x) -> let reference, predicate1 = time_of clause.loc x times in if predicate1 <> predicate then error ?loc:clause.loc ("cannot remove a clause for another predicate"); let times = remove_from_times x times in clause.timestamp <- reference; let snd_lvl_idx = remove_clause_in_snd_lvl_idx (fun x -> x.timestamp = reference) snd_lvl_idx in { times; time; idx = Ptmap.add predicate snd_lvl_idx idx }, (None, overlap) | Some (Ast.Structured.Replace x) -> let reference, predicate1 = time_of clause.loc x times in if predicate1 <> predicate then error ?loc:clause.loc ("cannot replace a clause for another predicate"); let times = remove_from_times x times in clause.timestamp <- reference; let snd_lvl_idx = remove_clause_in_snd_lvl_idx (fun x -> x.timestamp = reference) snd_lvl_idx in let snd_lvl_idx = add_clause_to_snd_lvl_idx ~depth ~insert:Bl.(insert (fun x -> lex_insertion x.timestamp reference)) predicate clause snd_lvl_idx in { times; time; idx = Ptmap.add predicate snd_lvl_idx idx }, update_overlap ~det_check overlap ~depth snd_lvl_idx clause | Some (Ast.Structured.Insert gr) -> let timestamp = match gr with | Ast.Structured.Before x -> (fst @@ time_of clause.loc x times) @ [-time] | Ast.Structured.After x -> (fst @@ time_of clause.loc x times) @ [+time] in let times = add_to_times clause.loc name timestamp predicate times in clause.timestamp <- timestamp; let snd_lvl_idx = add_clause_to_snd_lvl_idx ~depth ~insert:Bl.(insert (fun x -> lex_insertion x.timestamp timestamp)) predicate clause snd_lvl_idx in { times; time; idx = Ptmap.add predicate snd_lvl_idx idx }, update_overlap ~det_check overlap ~depth snd_lvl_idx clause with Not_found -> let idx = Ptmap.add predicate (make_new_Map_snd_level_index 0 []) idx in add1clause_compile_time ~det_check ~depth { idx; time; times } overlap ~graft predicate clause name let update_indexing (indexing : pred_info Constants.Map.t) (index : index) : index = let idx = C.Map.fold (fun predicate ({mode; indexing}) m -> Ptmap.add predicate begin match indexing with | Hash args -> BitHash { args; mode; args_idx = Ptmap.empty; } | MapOn argno -> make_new_Map_snd_level_index argno mode | DiscriminationTree arg_depths -> IndexWithDiscriminationTree { arg_depths; mode; args_idx = Discrimination_tree.empty_dt arg_depths; } end m) indexing index.idx in { index with idx } let add_to_index_compile_time ~det_check ~depth ~predicate ~graft clause name index overlap_index = add1clause_compile_time ~det_check ~depth ~graft index overlap_index predicate clause name let make_empty_index ~depth ~indexing = let index = update_indexing indexing { idx = Ptmap.empty; time = 0; times = StrMap.empty } in let index = close_index index in { index; src = [] } type goal_arg_classification = | Variable | Rigid of constant let rec classify_goal_arg ~depth t = (* Format.eprintf "goal %a\n%!" (ppterm depth [] ~argsdepth:depth empty_env) t; *) match deref_head ~depth t with | Const k when k == Global_symbols.uvarc -> Rigid mustbevariablec | Const k -> Rigid(k) | Nil -> Rigid (Global_symbols.nilc) | Cons _ -> Rigid (Global_symbols.consc) | App (k,_,_) when k == Global_symbols.uvarc -> Rigid mustbevariablec | App (k,a,_) when k == Global_symbols.asc -> classify_goal_arg ~depth a | App (k,_,_) -> Rigid (k) | Builtin (k,_) -> Rigid (const_of_builtin_predicate k) | Lam t -> classify_goal_arg ~depth:(depth+1) t (* eta *) | Arg _ | UVar _ | AppArg _ | AppUVar _ | Discard -> Variable | CData d -> let hash = -(CData.hash d) in if hash > mustbevariablec then Rigid (hash) else Rigid (hash+1) let classify_goal_argno ~depth argno = function | Const _ -> Variable | App(_,x,_) when argno == 0 -> classify_goal_arg ~depth x | App(_,_,xs) -> let x = try List.nth xs (argno-1) with Invalid_argument _ -> type_error ("The goal is not applied enough") in classify_goal_arg ~depth x | _ -> assert false let hash_goal_args ~depth mode args goal = match goal with | Const _ -> 0 | App(k,x,xs) -> hash_goal_arg_list k ~depth (x::xs) mode args | _ -> assert false let trie_goal_args goal : term list = match goal with | Const _ -> [] | App(_, x, xs) -> x :: xs | _ -> assert false let cmp_timestamp { timestamp = tx } { timestamp = ty } = lex_insertion tx ty let get_clauses_dt ~(check_mut_excl:check_mut_excl) ~depth goal ~cmp_clause args_idx = let max_depths = Discrimination_tree.max_depths args_idx in let max_path = Discrimination_tree.max_path args_idx in let max_list_length = Discrimination_tree.max_list_length args_idx in let arg_depths = Discrimination_tree.user_upper_bound_depth args_idx in (* Format.eprintf "Args depths is [%a]@." (pplist pp_int "-- ") arg_depths; *) let path = arg_to_trie_path ~check_mut_excl ~safe:false ~depth ~is_goal:true (trie_goal_args goal) arg_depths max_depths max_path max_list_length in [%spy "dev:disc-tree:depth-path" ~rid pp_string "Goal: MaxDepths " (pplist pp_int ";") (Array.to_list max_depths)]; [%spy "dev:disc-tree:list-size-path" ~rid pp_string "Goal: MaxListSize " pp_int max_list_length]; (* Format.(printf "Goal: MaxDepth is %a\n" (pp_print_list ~pp_sep:(fun fmt _ -> pp_print_string fmt " ") pp_print_int) (Discrimination_tree.max_depths args_idx |> Array.to_list)); *) [%spy "dev:disc-tree:path" ~rid Discrimination_tree.Path.pp path (pplist pp_int ";") arg_depths (*Discrimination_tree.(pp (fun fmt x -> pp_string fmt "+")) args_idx*)]; (* Format.eprintf "@[<hov 2>Path is@ @[%a@]]@." Discrimination_tree.Path.pp path; Format.eprintf "@[<hov 2>Discrimination tree is@ @[%a@]@." (Discrimination_tree.pp pp_clause) args_idx; *) (* Format.eprintf "@[<hov 4>Going to retrieve with %a, path is@ %a -- goal is@ %a@]@." pp_check_mut_excl check_mut_excl Discrimination_tree.Path.pp path (uppterm 0 [] [||] ~argsdepth:0) goal; *) let candidates = Discrimination_tree.retrieve cmp_clause path args_idx in (* Format.eprintf "Candidates len is %d -->@ @[[%a]@]@." (Bl.length candidates) (pplist pp_clause ",@.") (Bl.to_list candidates); *) [%spy "dev:disc-tree:candidates" ~rid pp_int (Bl.length candidates)]; candidates let get_clauses ~depth predicate goal { idx = m } = let rc = try match Ptmap.find predicate m with | TwoLevelIndex { all_clauses; argno; mode; flex_arg_clauses; arg_idx } -> begin match classify_goal_argno ~depth argno goal with | Variable -> all_clauses | Rigid arg_hd -> try Ptmap.find arg_hd arg_idx with Not_found -> flex_arg_clauses end |> Bl.to_scan | BitHash { args; mode; args_idx } -> let hash = hash_goal_args ~depth mode args goal in let cl = Ptmap.find_unifiables hash args_idx |> List.map Bl.to_scan |> List.map Bl.to_list |> List.flatten in Bl.of_list @@ List.sort cmp_timestamp cl | IndexWithDiscriminationTree {arg_depths; mode; args_idx} -> get_clauses_dt ~check_mut_excl:(Off mode) ~cmp_clause:cmp_timestamp ~depth goal args_idx with Not_found -> Bl.of_list [] in [%log "get_clauses" ~rid (C.show predicate) (Bl.length rc)]; [%spy "dev:get_clauses" ~rid C.pp predicate pp_int (Bl.length rc)]; rc let local_prog { src } = src end (* }}} *) open Indexing (* Used to pass the program to the CHR runtime *) let orig_prolog_program = Fork.new_local (make_empty_index ~depth:0 ~indexing:C.Map.empty) (****************************************************************************** Dynamic Prolog program ******************************************************************************) module Clausify : sig val clausify : tail_cut:bool -> loc:Loc.t option -> prolog_prog -> depth:int -> term -> (constant*clause) list * clause_src list * int val clausify1 : tail_cut:bool -> loc:Loc.t -> modes:(constant -> Mode.hos) -> nargs:int -> depth:int -> term -> (constant*clause) * clause_src * int (* Utilities that deref on the fly *) val lp_list_to_list : depth:int -> term -> term list val get_lambda_body : depth:int -> term -> term val split_conj : depth:int -> term -> term list end = struct (* {{{ *) let rec split_conj ~depth = function | Builtin(And, hd :: args) -> split_conj ~depth hd @ List.(flatten (map (split_conj ~depth) args)) | Nil -> [] | Cons(x,xs) -> split_conj ~depth x @ split_conj ~depth xs | UVar ({ contents=g },from,args) when g != C.dummy -> split_conj ~depth (deref_uv ~from ~to_:depth args g) | AppUVar ({contents=g},from,args) when g != C.dummy -> split_conj ~depth (deref_appuv ~from ~to_:depth args g) | Discard -> [] | _ as f -> [ f ] ;; let rec lp_list_to_list ~depth = function | Cons(hd, tl) -> hd :: lp_list_to_list ~depth tl | Nil -> [] | UVar ({ contents=g },from,args) when g != C.dummy -> lp_list_to_list ~depth (deref_uv ~from ~to_:depth args g) | AppUVar ({contents=g},from,args) when g != C.dummy -> lp_list_to_list ~depth (deref_appuv ~from ~to_:depth args g) | x -> error (Fmt.sprintf "%s is not a list" (show_term x)) ;; let rec get_lambda_body ~depth = function | UVar ({ contents=g },from,args) when g != C.dummy -> get_lambda_body ~depth (deref_uv ~from ~to_:depth args g) | AppUVar ({contents=g},from,args) when g != C.dummy -> get_lambda_body ~depth (deref_appuv ~from ~to_:depth args g) | Lam b -> b | _ -> error "pi/sigma applied to something that is not a Lam" ;; (* BUG: the following clause is rejected because (Z c d) is not in the fragment. However, once X and Y becomes arguments, (Z c d) enters the fragment. r :- (pi X\ pi Y\ q X Y :- pi c\ pi d\ q (Z c d) (X c d) (Y c)) => ... *) (* Takes the source of an implication and produces the clauses to be added to * the program and the number of existentially quantified constants turned * into globals. * * Invariants: * - lts is the number of lambdas crossed and it is always = List.length ts * - lcs is the number of new constants to be generated because a sigma * has been crossed * - the argument lives in (depth+lts) * - the clause will live in (depth+lcs) *) let rec claux1 loc get_mode vars depth hyps ts lts lcs t = [%trace "clausify" ~rid ("%a %d %d %d %d\n%!" (ppterm (depth+lts) [] ~argsdepth:0 empty_env) t depth lts lcs (List.length ts)) begin match t with | Discard -> error ?loc "ill-formed hypothetical clause: discard in head position" | Builtin(RImpl, [g2;g1]) -> claux1 loc get_mode vars depth ((ts,g1)::hyps) ts lts lcs g2 | Builtin(RImpl, []) -> error ?loc "ill-formed hypothetical clause: :-" | Builtin(Impl, [g1;g2])-> claux1 loc get_mode vars depth ((ts,g1)::hyps) ts lts lcs g2 | Builtin(Impl, []) -> error ?loc "ill-formed hypothetical clause: =>" | Builtin(Sigma, [arg]) -> let b = get_lambda_body ~depth:(depth+lts) arg in let args = List.rev (List.filter (function (Arg _) -> true | _ -> false) ts) in let cst = match args with [] -> Const (depth+lcs) | hd::rest -> App (depth+lcs,hd,rest) in claux1 loc get_mode vars depth hyps (cst::ts) (lts+1) (lcs+1) b | Builtin(Pi, [arg]) -> let b = get_lambda_body ~depth:(depth+lts) arg in claux1 loc get_mode (vars+1) depth hyps (Arg(vars,0)::ts) (lts+1) lcs b | Builtin(And, _) -> error ?loc "Conjunction in the head of a clause is not supported" | Const _ | App _ as g -> let hyps = List.(flatten (rev_map (fun (ts,g) -> let g = hmove ~from:(depth+lts) ~to_:(depth+lts+lcs) g in let g = subst depth ts g in split_conj ~depth:(depth+lcs) g ) hyps)) in let g = hmove ~from:(depth+lts) ~to_:(depth+lts+lcs) g in let g = subst depth ts g in (* Fmt.eprintf "@[<hov 1>%a@ :-@ %a.@]\n%!" (ppterm (depth+lcs) [] ~argsdepth:0 empty_env) g (pplist (ppterm (depth+lcs) [] ~argsdepth:0 empty_env) " , ") hyps ;*) let hd, args = match g with Const h -> h, [] | App(h,x,xs) -> h, x::xs | UVar _ | AppUVar _ | Arg _ | AppArg _ | Discard -> raise Flex_head | Lam _ -> type_error ?loc "The head of a clause cannot be a lambda abstraction" | Builtin(c,_) -> raise @@ CannotDeclareClauseForBuiltin(loc,c) | CData _ -> type_error ?loc "The head of a clause cannot be a builtin data type" | Cons _ | Nil -> assert false in let c = { depth = depth+lcs; args; hyps; mode = get_mode hd; vars; loc; timestamp = [] } in [%spy "dev:claudify:extra-clause" ~rid (ppclause ~hd) c]; (hd,c), { hdepth = depth; hsrc = g }, lcs | UVar ({ contents=g },from,args) when g != C.dummy -> claux1 loc get_mode vars depth hyps ts lts lcs (deref_uv ~from ~to_:(depth+lts) args g) | AppUVar ({contents=g},from,args) when g != C.dummy -> claux1 loc get_mode vars depth hyps ts lts lcs (deref_appuv ~from ~to_:(depth+lts) args g) | Arg _ | AppArg _ -> raise Flex_head | Builtin (c,_) -> raise @@ CannotDeclareClauseForBuiltin(loc,c) | (Lam _ | CData _ ) as x -> type_error ?loc ("Assuming a string or int or float or function:" ^ show_term x) | UVar _ | AppUVar _ -> raise Flex_head | Nil | Cons _ -> error ?loc "ill-formed hypothetical clause: [] / ::" end] let add_tail_cut ~tail_cut = if tail_cut then [[],Builtin(Cut,[])] else [] let clausify ~tail_cut ~loc { index = { idx = index } } ~depth t = let get_mode x = match Ptmap.find x index with | TwoLevelIndex { mode } -> mode | BitHash { mode } -> mode | IndexWithDiscriminationTree { mode } -> mode | exception Not_found -> [] in let l = split_conj ~depth t in let clauses, program, lcs = List.fold_left (fun (clauses, programs, lcs) t -> let clause, program, lcs = try claux1 loc get_mode 0 depth (add_tail_cut ~tail_cut) [] 0 lcs t with | Flex_head -> error ?loc "The head of a clause cannot be flexible" | CannotDeclareClauseForBuiltin(loc,c) -> error ?loc ("Declaring a clause for built in predicate " ^ show_builtin_predicate C.show c) in clause :: clauses, program :: programs, lcs) ([],[],0) l in clauses, program, lcs ;; let clausify1 ~tail_cut ~loc ~modes ~nargs ~depth t = claux1 (Some loc) modes nargs depth (add_tail_cut ~tail_cut) [] 0 0 t end (* }}} *) open Clausify (****************************************************************************** Choice stack ******************************************************************************) type goal = { depth : int; program : prolog_prog; goal : term; gid : UUID.t [@trace] } let make_subgoal_id ogid ((depth,goal)[@trace]) = let gid = UUID.make () in [%spy "user:subgoal" ~rid ~gid: ogid UUID.pp gid]; [%spy "user:newgoal" ~rid ~gid (uppterm depth [] ~argsdepth:0 empty_env) goal]; gid [@@inline] [@@warning "-32"] let make_subgoal (gid[@trace]) ~depth program goal = let gid[@trace] = make_subgoal_id gid ((depth,goal)[@trace]) in { depth ; program ; goal ; gid = gid [@trace] } [@@inline] let repack_goal (gid[@trace]) ~depth program goal = { depth ; program ; goal ; gid = gid [@trace] } [@@inline] (* The activation frames points to the choice point that cut should backtrack to, i.e. the first one not to be removed. We call it catto_alts in the code. *) type frame = | FNil | FCons of (*lvl:*)alternative * goal list * frame and alternative = { cutto_alts : alternative; program : prolog_prog; adepth : int; agoal_hd : constant; ogoal_arg : term; ogoal_args : term list; agid : UUID.t; [@trace] goals : goal list; stack : frame; trail : T.trail; state : State.t; clauses : clause Bl.scan; next : alternative; } let noalts : alternative = Obj.magic (Sys.opaque_identity 0) (****************************************************************************** Constraint propagation ******************************************************************************) (* A runtime is a function to search for the first solution to the query * plus a function to find the next one. * The low level part of the runtime give access to the runtime memory. * One can for example execute code before starting the runtime and such * code may have effects "outside" the runtime that are trailed. The destroy * function takes care of cleaning up the mess. * * The supported workflow is * exec optional, many times * search mandatory, 1 time * next optional, repeat until No_clause is thrown * destroy optional, 1 time, useful in nested runtimes *) type runtime = { search : unit -> alternative; next_solution : alternative -> alternative; (* low level part *) destroy : unit -> unit; exec : 'a 'b. ('a -> 'b) -> 'a -> 'b; get : 'a. 'a Fork.local_ref -> 'a; } let do_make_runtime : (?max_steps:int -> ?delay_outside_fragment:bool -> executable -> runtime) ref = ref (fun ?max_steps ?delay_outside_fragment _ -> anomaly "do_make_runtime not initialized") module Constraints : sig type new_csts = { new_goals : goal list; some_rule_was_tried : bool; [@trace] } val propagation : unit -> new_csts val resumption : constraint_def list -> goal list val chrules : CHR.t Fork.local_ref (* out of place *) val exect_builtin_predicate : once:(depth:int -> term -> State.t -> State.t) -> constant -> depth:int -> prolog_prog -> (UUID.t[@trace]) -> term list -> term list val declare_constraint : depth:constant -> prolog_prog -> (UUID.t[@trace]) -> term list -> unit end = struct (* {{{ *) type new_csts = { new_goals : goal list; some_rule_was_tried : bool; [@trace] } exception NoMatch module Ice : sig type freezer val empty_freezer : freezer (* The input term lives in depth, the sequent lives in ground * (the same as depth for the goal, while >= for ctx items) * * shift = relocate only bound variables * relocate = increment all db indexes * * The input term is fully dereferenced, flexible terms are frozen, * it is shifted to its ground, then relocated to new base, then * shifted to the maxbase *) val freeze : depth:int -> term -> ground:int -> newground:int -> maxground:int -> freezer -> freezer * term val defrost : from:int -> term -> env -> to_:int -> map:constant C.Map.t -> freezer -> term val assignments : freezer -> assignment list end = struct (* {{{ *) type freezer = { c2uv : uvar_body C.Map.t; uv2c : (uvar_body * term) list; assignments : assignment list; (* assignment to lower the level to 0 *) } let empty_freezer = { c2uv = C.Map.empty; uv2c = []; assignments = [] } let freeze ~depth t ~ground ~newground ~maxground f = let f = ref f in let log_assignment = function | t, None -> t | t, Some (r,_,nt as s) -> f := { !f with assignments = s :: !f.assignments }; r @::== nt; t in let freeze_uv r = try List.assq r !f.uv2c with Not_found -> let n, c = C.fresh_global_constant () in [%spy "dev:freeze_uv:new" ~rid (fun fmt () -> let tt = UVar (r,0,0) in Fmt.fprintf fmt "%s == %a" (C.show n) (ppterm 0 [] ~argsdepth:0 empty_env) tt) ()]; f := { !f with c2uv = C.Map.add n r !f.c2uv; uv2c = (r,c) :: !f.uv2c }; c in let rec faux d = function | (Const _ | CData _ | Nil | Discard) as x -> x | Cons(hd,tl) as orig -> let hd' = faux d hd in let tl' = faux d tl in if hd == hd' && tl == tl' then orig else Cons(hd',tl') | App(c,x,xs) as orig -> let x' = faux d x in let xs' = smart_map (faux d) xs in if x == x' && xs == xs' then orig else App(c,x',xs') | Builtin(c,args) as orig -> let args' = smart_map (faux d) args in if args == args' then orig else Builtin(c,args') | (Arg _ | AppArg _) -> error "only heap terms can be frozen" | Lam t as orig -> let t' = faux (d+1) t in if t == t' then orig else Lam t' (* freeze *) | AppUVar(r,0,args) when !!r == C.dummy -> let args = smart_map (faux d) args in App(Global_symbols.uvarc, freeze_uv r, [list_to_lp_list args]) (* expansion *) | UVar(r,lvl,ano) when !!r == C.dummy -> faux d (log_assignment(expand_uv ~depth:d r ~lvl ~ano)) | AppUVar(r,lvl,args) when !!r == C.dummy -> faux d (log_assignment(expand_appuv ~depth:d r ~lvl ~args)) (* deref *) | UVar(r,lvl,ano) -> faux d (deref_uv ~from:lvl ~to_:d ano !!r) | AppUVar(r,lvl,args) -> faux d (deref_appuv ~from:lvl ~to_:d args !!r) in [%spy "dev:freeze:in" ~rid (fun fmt () -> Fmt.fprintf fmt "depth:%d ground:%d newground:%d maxground:%d %a" depth ground newground maxground (uppterm depth [] ~argsdepth:0 empty_env) t) ()]; let t = faux depth t in [%spy "dev:freeze:after-faux" ~rid (uppterm depth [] ~argsdepth:0 empty_env) t]; let t = shift_bound_vars ~depth ~to_:ground t in [%spy "dev:freeze:after-shift->ground" ~rid (uppterm ground [] ~argsdepth:0 empty_env) t]; let t = shift_bound_vars ~depth:0 ~to_:(newground-ground) t in [%spy "dev:freeze:after-reloc->newground" ~rid (uppterm newground [] ~argsdepth:0 empty_env) t]; let t = shift_bound_vars ~depth:newground ~to_:maxground t in [%spy "dev:freeze:out" ~rid (uppterm maxground [] ~argsdepth:0 empty_env) t]; !f, t let defrost ~from t env ~to_ ~map f = [%spy "dev:defrost:in" ~rid (fun fmt () -> Fmt.fprintf fmt "from:%d to:%d %a" from to_ (uppterm from [] ~argsdepth:from env) t) ()]; let t = full_deref ~adepth:from env ~depth:from t in [%spy "dev:defrost:fully-derefd" ~rid (fun fmt ()-> Fmt.fprintf fmt "from:%d to:%d %a" from to_ (uppterm from [] ~argsdepth:0 empty_env) t) ()]; let t = map_free_vars ~map ~depth:from ~to_ t in [%spy "dev:defrost:shifted" ~rid (fun fmt () -> Fmt.fprintf fmt "from:%d to:%d %a" from to_ (uppterm to_ [] ~argsdepth:0 empty_env) t) ()]; let rec daux d = function | Const c when C.Map.mem c f.c2uv -> UVar(C.Map.find c f.c2uv,0,0) | (Const _ | CData _ | Nil | Discard) as x -> x | Cons(hd,tl) as orig -> let hd' = daux d hd in let tl' = daux d tl in if hd == hd' && tl == tl' then orig else Cons(hd',tl') | App(c,UVar(r,lvl,ano),a) when !!r != C.dummy -> daux d (App(c,deref_uv ~to_:d ~from:lvl ano !!r,a)) | App(c,AppUVar(r,lvl,args),a) when !!r != C.dummy -> daux d (App(c,deref_appuv ~to_:d ~from:lvl args !!r,a)) | App(c,Const x,[args]) when c == Global_symbols.uvarc -> let r = C.Map.find x f.c2uv in let args = lp_list_to_list ~depth:d args in mkAppUVar r 0 (smart_map (daux d) args) | App(c,UVar(r,_,_),[args]) when c == Global_symbols.uvarc -> let args = lp_list_to_list ~depth:d args in mkAppUVar r 0 (smart_map (daux d) args) | App(c,AppUVar(r,_,_),[args]) when c == Global_symbols.uvarc -> let args = lp_list_to_list ~depth:d args in mkAppUVar r 0 (smart_map (daux d) args) | App(c,x,xs) as orig -> let x' = daux d x in let xs' = smart_map (daux d) xs in if x == x' && xs == xs' then orig else App(c,x', xs') | Builtin(c,args) as orig -> let args' = smart_map (daux d) args in if args == args' then orig else Builtin(c,args') | Arg(i,ano) when env.(i) != C.dummy -> daux d (deref_uv ~from:to_ ~to_:d ano env.(i)) | AppArg (i,args) when env.(i) != C.dummy -> daux d (deref_appuv ~from:to_ ~to_:d args env.(i)) | (Arg(i,_) | AppArg(i,_)) as x -> env.(i) <- UVar(oref C.dummy, to_, 0); daux d x | Lam t as orig -> let t' = daux (d+1) t in if t == t' then orig else Lam t' | UVar _ as x -> x | AppUVar(r,lvl,args) as orig -> let args' = smart_map (daux d) args in if args == args' then orig else AppUVar(r,lvl,args') in daux to_ t let assignments { assignments } = assignments end (* }}} *) let match_goal (gid[@trace]) goalno maxground env freezer (newground,depth,t) pattern = let freezer, t = Ice.freeze ~depth t ~ground:depth ~newground ~maxground freezer in [%trace "match_goal" ~rid ("@[<hov>%a ===@ %a@]" (uppterm maxground [] ~argsdepth:maxground env) t (uppterm 0 [] ~argsdepth:maxground env) pattern) begin if unif ~argsdepth:maxground ~matching:false (gid[@trace]) maxground env 0 t pattern then freezer else raise NoMatch end] let match_context (gid[@trace]) goalno maxground env freezer (newground,ground,lt) pattern = if pattern == Discard then freezer else let freezer, lt = map_acc (fun freezer { hdepth = depth; hsrc = t } -> Ice.freeze ~depth t ~ground ~newground ~maxground freezer) freezer lt in let t = list_to_lp_list lt in [%trace "match_context" ~rid ("@[<hov>%a ===@ %a@]" (uppterm maxground [] ~argsdepth:maxground env) t (uppterm 0 [] ~argsdepth:maxground env) pattern) begin if unif ~argsdepth:maxground ~matching:false (gid[@trace]) maxground env 0 t pattern then freezer else raise NoMatch end] (* To avoid matching the same propagation rule against the same ordered list * of constraints *) type chrattempt = { propagation_rule : CHR.rule; constraints : constraint_def list } module HISTORY = Hashtbl.Make(struct type t = chrattempt let hash = Hashtbl.hash let equal { propagation_rule = p ; constraints = lp } { propagation_rule = p'; constraints = lp'} = p == p' && for_all2 (==) lp lp' end) let chrules = Fork.new_local CHR.empty let make_constraint_def ~rid ~gid:(gid[@trace]) depth prog pdiff conclusion = { cdepth = depth; prog = prog; context = pdiff; cgid = gid [@trace]; conclusion } let delay_goal ?(filter_ctx=fun _ -> true) ~depth prog ~goal:g (gid[@trace]) ~on:keys = let pdiff = local_prog prog in let pdiff = List.filter filter_ctx pdiff in let gid[@trace] = make_subgoal_id gid (depth,g) in let kind = Constraint (make_constraint_def ~rid ~gid:(gid[@trace]) depth prog pdiff g) in CS.declare_new { kind ; blockers = keys } ;; let rec head_of = function | Const x -> x | Builtin(Pi,[Lam f]) -> head_of f | Builtin(RImpl,hd :: _) -> head_of hd | Builtin(And,hd :: _) -> head_of hd (* FIXME *) | App(x,_,_) -> x | Builtin(x,_) -> const_of_builtin_predicate x | AppUVar(r,_,_) | UVar(r,_,_) when !!r != C.dummy -> head_of !!r | CData _ -> type_error "A constraint cannot be a primitive data" | Cons(x,_) -> head_of x | Nil -> type_error "A constraint cannot be a list" | (UVar _ | AppUVar _) -> type_error "A constraint cannot have flexible head" | (Arg _ | AppArg _) -> anomaly "head_of on non-heap term" | Discard -> type_error "A constraint cannot be _" | Lam _ -> type_error "A constraint cannot be a function" let declare_constraint ~depth prog (gid[@trace]) args = let g, keys = match args with | t1 :: more -> let err t = "The Key arguments of declare_constraint must be variables or list of variables. Got: " ^ show_term t in let rec collect_keys t = match deref_head ~depth t with | UVar (r, _, _) | AppUVar (r, _, _) -> [r] | Discard -> [dummy_uvar_body] | Lam _ -> begin match HO.eta_contract_flex ~depth t with | None -> type_error (err t) | Some t -> collect_keys t end | _ -> type_error (err t) and collect_keys_list t = match deref_head ~depth t with | Nil -> [] | Cons(hd,tl) -> collect_keys hd @ collect_keys_list tl | x -> collect_keys x in t1, List.flatten (List.map collect_keys_list more) | _ -> type_error "declare_constraint takes at least one argument" in match CHR.clique_of (head_of g) !chrules with | Some (clique, ctx_filter) -> (* real constraint *) (* XXX head_of is weak because no clausify ? XXX *) delay_goal ~filter_ctx:(fun { hsrc = x } -> C.Set.mem (head_of x) clique || C.Set.mem (head_of x) ctx_filter) ~depth prog ~goal:g (gid[@trace]) ~on:keys | None -> delay_goal ~depth prog ~goal:g (gid[@trace]) ~on:keys let exect_builtin_predicate ~once c ~depth idx (gid[@trace]) args = let b = try FFI.lookup c with Not_found -> anomaly ("no built-in predicated named " ^ C.show c) in let constraints = !CS.Ugly.delayed in let state = !CS.state in let state, gs = FFI.call b ~once ~depth (local_prog idx) constraints state args in let state, gs = State.get Data.Conversion.extra_goals_postprocessing state gs state in CS.state := state; List.map Data.Conversion.term_of_extra_goal gs ;; let match_head { conclusion = x; cdepth } p = match deref_head ~depth:cdepth x with | Const x -> x == p | App(x,_,_) -> x == p | _ -> false ;; let pp_opt start f fmt = function | None -> () | Some x -> Fmt.fprintf fmt "%s%a" start f x let pp_optl start f fmt = function | [] -> () | x -> Fmt.fprintf fmt "%s%a" start (pplist f " ") x let pp_optterm stop fmt x = match deref_head ~depth:0 x with | Discard -> () | _ -> Fmt.fprintf fmt "%a%s" (uppterm 0 [] ~argsdepth:0 empty_env) x stop let pp_sequent fmt { CHR.eigen; context; conclusion } = Fmt.fprintf fmt "@[<hov 2>(%a%a%a)@]" (pp_optterm " : ") eigen (pp_optterm " ?- ") context (uppterm 0 [] ~argsdepth:0 empty_env) conclusion let pp_urule fmt { CHR. to_match; to_remove; new_goal; guard } = Fmt.fprintf fmt "@[<hov 2>%a@ %a@ %a@ %a@]" (pplist pp_sequent " ") to_match (pp_optl "\\ " pp_sequent) to_remove (pp_opt "| " (uppterm ~min_prec:Elpi_parser.Parser_config.inf_precedence 0 [] ~argsdepth:0 empty_env)) guard (pp_opt "<=> " pp_sequent) new_goal [@@warning "-32"] let try_fire_rule (gid[@trace]) rule (constraints as orig_constraints) = let { CHR. to_match = pats_to_match; to_remove = pats_to_remove; patsno; new_goal; guard; nargs; pattern = quick_filter; rule_name } = rule in if patsno < 1 then error "CHR propagation must mention at least one constraint"; (* Quick filtering using just the head symbol *) if not(List.for_all2 match_head constraints quick_filter) then None else (* max depth of rule and constraints involved in the matching *) let max_depth, constraints = (* Goals are lifted at different depths to avoid collisions *) let max_depth,constraints = List.fold_left (fun (md,res) c -> let md = md + c.cdepth in md, (md,c)::res) (0,[]) constraints in max_depth, List.rev constraints in let constraints_depts, constraints_contexts, constraints_goals = List.fold_right (fun (dto,{context = c; cdepth = d; conclusion = g}) (ds, ctxs, gs) -> (dto,d,d) :: ds, (dto,d,c) :: ctxs, (dto,d,g) :: gs) constraints ([],[],[]) in let env = Array.make nargs C.dummy in let patterns_eigens, patterns_contexts, patterns_goals = List.fold_right (fun { CHR.eigen; context; conclusion } (es, cs, gs) -> eigen :: es, context :: cs, conclusion :: gs) (pats_to_match @ pats_to_remove) ([],[],[]) in let match_eigen i m (dto,d,eigen) pat = match_goal (gid[@trace]) i max_depth env m (dto,d,list_to_lp_list @@ C.mkinterval 0 eigen 0) pat in let match_conclusion i m g pat = match_goal (gid[@trace]) i max_depth env m g pat in let match_context i m ctx pctx = match_context (gid[@trace]) i max_depth env m ctx pctx in let guard = match guard with | Some g -> g | None -> mkBuiltin Eq [mkNil;mkNil] in let initial_program = !orig_prolog_program in let executable = { (* same program *) compiled_program = initial_program; (* no meta meta level *) chr = CHR.empty; initial_goal = (* TODO: maybe we don't need to do this if we don't reach the guard *) move ~argsdepth:max_depth ~from:max_depth ~to_:max_depth env (shift_bound_vars ~depth:0 ~to_:max_depth guard); assignments = StrMap.empty; initial_depth = max_depth; initial_runtime_state = !CS.initial_state; symbol_table = !C.table; builtins = !FFI.builtins; } in let { search; get; exec; destroy } = !do_make_runtime executable in let check_guard () = try let _ = search () in if get CS.Ugly.delayed <> [] then error "propagation rules must not declare constraint(s)" with No_clause -> raise NoMatch in let result = try (* matching *) let m = exec (fun m -> [%spy "dev:CHR:candidate" ~rid (pplist (fun f x -> let dto,dt,t = x in Format.fprintf f "(lives-at:%d, to-be-lifted-to:%d) %a" dt dto (uppterm dt [] ~argsdepth:0 empty_env) t) ";") constraints_goals]; let m = fold_left2i match_conclusion m constraints_goals patterns_goals in let m = fold_left2i match_context m constraints_contexts patterns_contexts in let m = fold_left2i match_eigen m constraints_depts patterns_eigens in [%spy "dev:CHR:matching-assignments" ~rid (pplist (uppterm max_depth [] ~argsdepth:0 empty_env) ~boxed:false ",") (Array.to_list env)]; T.to_resume := []; assert(!T.new_delayed = []); m) Ice.empty_freezer in [%spy "dev:CHR:maxdepth" ~rid Fmt.pp_print_int max_depth]; check_guard (); (* result *) let _, constraints_to_remove = let len_pats_to_match = List.length pats_to_match in partition_i (fun i _ -> i < len_pats_to_match) orig_constraints in let new_goals = match new_goal with | None -> None | Some { CHR.eigen; context; conclusion } -> let eigen = match full_deref ~adepth:max_depth env ~depth:max_depth eigen with | (Nil | Cons _) as x -> lp_list_to_list ~depth:max_depth x | Discard -> C.mkinterval 0 max_depth 0 | _ -> error "eigen not resolving to a list of names" in let max_eigen,map = List.fold_left (fun (i,m) c -> i+1,C.Map.add (Data.destConst c) i m) (0,C.Map.empty) eigen in let conclusion = Ice.defrost ~from:max_depth ~to_:max_eigen ~map (Builtin(Impl,[context;conclusion])) env m in (* TODO: check things make sense in heigen *) let prog = initial_program in Some (make_constraint_def ~rid ~gid:((make_subgoal_id gid (max_eigen,conclusion))[@trace]) max_eigen prog [] conclusion) in [%spy "dev:CHR:try-rule:success" ~rid]; Some(rule_name, constraints_to_remove, new_goals, Ice.assignments m) with NoMatch -> [%spy "dev:CHR:try-rule:fail" ~rid]; None in destroy (); result ;; let resumption to_be_resumed_rev = List.map (fun { cdepth = d; prog; conclusion = g; cgid = gid [@trace] } -> (repack_goal[@inlined]) ~depth:d (gid[@trace]) prog g) (List.rev to_be_resumed_rev) (* all permutations of pivot+rest of length len where the * pivot is in pivot_position. pivot may be part of rest, it is automatically * ignored *) let mk_permutations len pivot pivot_position rest = let open List in let rec insert x = function | [] -> [[x]] | (hd::tl) as l -> (x::l) :: map (fun y -> hd :: y) (insert x tl) in let rec aux n l = if n = 0 then [[]] else match l with | [] -> [] | hd :: tl when hd == pivot -> aux n tl | hd :: tl-> flatten (map (insert hd) (aux (n-1) tl)) @ aux n tl in let permutations_no_pivot = aux (len - 1) rest in permutations_no_pivot |> map begin fun l -> let before, after = partition_i (fun i _ -> i < pivot_position) l in before @ pivot :: after end ;; let propagation () = let to_be_resumed_rev = ref [] in let removed = ref [] in let outdated cs = List.exists (fun x -> List.memq x !removed) cs in let some_rule_fired[@trace] = ref false in let some_rule_was_tried[@trace] = ref false in let orig_store_contents[@trace] = CS.contents () in while !CS.new_delayed <> [] do match !CS.new_delayed with | [] -> anomaly "Empty list" | { CS.cstr = active; cstr_blockers = overlapping } :: rest -> (* new_delayed may be changed by, CS.remove_old_constraint *) CS.new_delayed := rest; let rules = CHR.rules_for (head_of active.conclusion) !chrules in rules |> List.iter (fun rule -> for position = 0 to rule.CHR.patsno - 1 do (* don't generate perms if the pivot is already out of place *) if not (match_head active (List.nth rule.CHR.pattern position)) then () else let permutations = mk_permutations rule.CHR.patsno active position (List.map fst (CS.contents ~overlapping ())) in permutations |> List.iter (fun constraints -> (* a constraint just removed may occur in a permutation (that * was generated before the removal *) if outdated constraints then () else begin let ()[@trace] = some_rule_was_tried := true in [%spy "user:CHR:try" ~rid ~gid:active.cgid Loc.pp rule.rule_loc pp_urule rule]; match try_fire_rule (active.cgid[@trace]) rule constraints with | None -> [%spy "user:CHR:rule-failed" ~rid ] | Some (rule_name, to_be_removed, to_be_added, assignments) -> let ()[@trace] = some_rule_fired := true in [%spy "user:CHR:rule-fired" ~rid ~gid: (active.cgid[@trace]) pp_string rule_name]; [%spyl "user:CHR:rule-remove-constraints" ~rid ~gid: (active.cgid[@trace]) (fun fmt { cgid } -> UUID.pp fmt cgid) to_be_removed]; removed := to_be_removed @ !removed; List.iter CS.remove_old_constraint to_be_removed; List.iter (fun (r,_lvl,t) -> r @:= t) assignments; match to_be_added with | None -> () | Some to_be_added -> to_be_resumed_rev := to_be_added :: !to_be_resumed_rev end) done); done; let ()[@trace] = if !some_rule_fired then begin orig_store_contents |> List.iter (fun it -> [%spy "user:CHR:store:before" ~rid CS.print_gid it CS.print1 it] ); CS.contents () |> List.iter (fun it -> [%spy "user:CHR:store:after" ~rid CS.print_gid it CS.print1 it] ); end; in { new_goals = resumption !to_be_resumed_rev; some_rule_was_tried = !some_rule_was_tried [@trace] } end (* }}} *) (****************************************************************************** Main loop = SLD + delay/resumption ******************************************************************************) module Mainloop : sig val make_runtime : ?max_steps:int -> ?delay_outside_fragment:bool -> executable -> runtime end = struct (* {{{ *) let steps_bound = Fork.new_local None let steps_made = Fork.new_local 0 let pred_of g = match g with | App(c,_,_) -> Some(C.show c) | Const c -> Some(C.show c) | Builtin(c,_) -> Some(show_builtin_predicate C.show c) | _ -> None [@@warning "-32"] let pp_candidate ~depth ~k fmt ({ loc } as cl) = match loc with | Some x -> Util.CData.pp fmt (Ast.cloc.Util.CData.cin x) | None -> Fmt.fprintf fmt "hypothetical clause: %a" (ppclause ~hd:k) cl [@@warning "-32"] let hd_c_of = function | Const _ as x -> x | App(x,_,_) -> C.mkConst x | Builtin(x,_) -> C.mkConst (const_of_builtin_predicate x) | _ -> C.dummy [@@warning "-32"] let pp_resumed_goal { depth; program; goal; gid = gid[@trace] } = [%spy "user:rule:resume:resumed" ~rid ~gid (uppterm depth [] ~argsdepth:0 empty_env) goal] [@@warning "-32"] let pp_CHR_resumed_goal { depth; program; goal; gid = gid[@trace] } = [%spy "user:CHR:resumed" ~rid ~gid (uppterm depth [] ~argsdepth:0 empty_env) goal] [@@warning "-32"] (* The block of recursive functions spares the allocation of a Some/None * at each iteration in order to know if one needs to backtrack or continue *) let make_runtime : ?max_steps: int -> ?delay_outside_fragment: bool -> executable -> runtime = (* Input to be read as the orl (((p,g)::gs)::next)::alts depth >= 0 is the number of variables in the context. *) let rec run depth p g (gid[@trace]) gs (next : frame) alts cutto_alts = [%cur_pred (pred_of g)]; [%trace "run" ~rid begin begin match !steps_bound with | Some bound -> incr steps_made; if !steps_made > bound then raise No_more_steps | None -> () end; match resume_all () with | None -> [%tcall next_alt alts] | Some ({ depth = ndepth; program; goal; gid = ngid [@trace] } :: goals) -> [%tcall run ndepth program goal (ngid[@trace]) (goals @ (repack_goal[@inlined]) (gid[@trace]) ~depth p g :: gs) next alts cutto_alts] | Some [] -> [%spyl "user:curgoal" ~rid ~gid (uppterm depth [] ~argsdepth:0 empty_env) [hd_c_of g;g]]; match g with | Builtin(Cut,[]) -> [%tcall cut (gid[@trace]) gs next (alts[@trace]) cutto_alts] | Builtin(Findall,[q;sol]) -> [%tcall findall depth p q sol (gid[@trace]) gs next alts cutto_alts] | Builtin(And, g :: gs') -> [%spy "user:rule" ~rid ~gid pp_string "and"]; let gid'[@trace] = make_subgoal_id gid ((depth,g)[@trace]) in let gs' = List.map (fun x -> (make_subgoal[@inlined]) ~depth (gid[@trace]) p x) gs' in [%spy "user:rule:and" ~rid ~gid pp_string "success"]; [%tcall run depth p g (gid'[@trace]) (gs' @ gs) next alts cutto_alts] | Builtin(Eq,[l;r]) -> [%spy "user:rule" ~rid ~gid pp_string "eq"]; [%spy "user:rule:builtin:name" ~rid ~gid pp_string (show_builtin_predicate C.show Eq)]; if unif ~argsdepth:depth ~matching:false (gid[@trace]) depth empty_env depth l r then begin [%spy "user:rule:eq" ~rid ~gid pp_string "success"]; match gs with | [] -> [%tcall pop_andl alts next cutto_alts] | { depth; program; goal; gid = gid [@trace] } :: gs -> [%tcall run depth program goal (gid[@trace]) gs next alts cutto_alts] end else begin [%spy "user:rule:eq" ~rid ~gid pp_string "fail"]; [%tcall next_alt alts] end | Builtin(Match,[p;r]) -> [%spy "user:rule" ~rid ~gid pp_string "eq"]; [%spy "user:rule:builtin:name" ~rid ~gid pp_string (show_builtin_predicate C.show Match)]; if unif ~argsdepth:depth ~matching:true (gid[@trace]) depth empty_env depth r p then begin [%spy "user:rule:eq" ~rid ~gid pp_string "success"]; match gs with | [] -> [%tcall pop_andl alts next cutto_alts] | { depth; program; goal; gid = gid [@trace] } :: gs -> [%tcall run depth program goal (gid[@trace]) gs next alts cutto_alts] end else begin [%spy "user:rule:eq" ~rid ~gid pp_string "fail"]; [%tcall next_alt alts] end | Builtin(RImpl, [g2;g1]) -> [%spy "user:rule" ~rid ~gid pp_string "implication"]; let [@warning "-26"]loc = None in let loc[@trace] = Some (Loc.initial ("(context step_id:" ^ string_of_int (Trace_ppx_runtime.Runtime.get_cur_step ~runtime_id:!rid "run") ^")")) in let clauses, pdiff, lcs = clausify ~tail_cut:false ~loc p ~depth g1 in let g2 = hmove ~from:depth ~to_:(depth+lcs) g2 in let gid[@trace] = make_subgoal_id gid ((depth,g2)[@trace]) in [%spy "user:rule:implication" ~rid ~gid pp_string "success"]; [%tcall run (depth+lcs) (add_clauses ~depth clauses pdiff p) g2 (gid[@trace]) gs next alts cutto_alts] | Builtin((Impl|ImplBang) as ik, [g1; g2]) -> [%spy "user:rule" ~rid ~gid pp_string "implication"]; let [@warning "-26"]loc = None in let loc[@trace] = Some (Loc.initial ("(context step_id:" ^ string_of_int (Trace_ppx_runtime.Runtime.get_cur_step ~runtime_id:!rid "run") ^")")) in let clauses, pdiff, lcs = clausify ~tail_cut:(ik = ImplBang) ~loc p ~depth g1 in let g2 = hmove ~from:depth ~to_:(depth+lcs) g2 in let gid[@trace] = make_subgoal_id gid ((depth,g2)[@trace]) in [%spy "user:rule:implication" ~rid ~gid pp_string "success"]; [%tcall run (depth+lcs) (add_clauses ~depth clauses pdiff p) g2 (gid[@trace]) gs next alts cutto_alts] | Builtin(Pi, [arg]) -> [%spy "user:rule" ~rid ~gid pp_string "pi"]; let f = get_lambda_body ~depth arg in let gid[@trace] = make_subgoal_id gid ((depth+1,f)[@trace]) in [%spy "user:rule:pi" ~rid ~gid pp_string "success"]; [%tcall run (depth+1) p f (gid[@trace]) gs next alts cutto_alts] | Builtin(Sigma, [arg]) -> [%spy "user:rule" ~rid ~gid pp_string "sigma"]; let f = get_lambda_body ~depth arg in let v = UVar(oref C.dummy, depth, 0) in let fv = subst depth [v] f in let gid[@trace] = make_subgoal_id gid ((depth,fv)[@trace]) in [%spy "user:rule:sigma" ~rid ~gid pp_string "success"]; [%tcall run depth p fv (gid[@trace]) gs next alts cutto_alts] | Builtin(Delay,args) -> [%spy "user:rule" ~rid ~gid pp_string "builtin"]; [%spy "user:rule:builtin:name" ~rid ~gid pp_string (show_builtin_predicate C.show Delay)]; begin match Constraints.declare_constraint ~depth p (gid[@trace]) args with | () -> [%spy "user:rule:builtin" ~rid ~gid pp_string "success"]; (match gs with | [] -> [%tcall pop_andl alts next cutto_alts] | { depth; program; goal; gid = gid [@trace] } :: gs -> [%tcall run depth program goal (gid[@trace]) gs next alts cutto_alts]) | exception No_clause -> [%spy "user:rule:builtin" ~rid ~gid pp_string "fail"]; [%tcall next_alt alts] end | Builtin(Host c, args) -> [%spy "user:rule" ~rid ~gid pp_string "builtin"]; [%spy "user:rule:builtin:name" ~rid ~gid pp_string (C.show c)]; let once ~depth g state = CS.state := state; let { depth; program; goal; gid = gid [@trace] } = (make_subgoal[@inlined]) (gid[@trace]) ~depth p g in let _alts = run depth program goal (gid[@trace]) [] FNil noalts noalts in !CS.state in begin match Constraints.exect_builtin_predicate ~once c ~depth p (gid[@trace]) args with | gs' -> [%spy "user:rule:builtin" ~rid ~gid pp_string "success"]; (match List.map (fun g -> (make_subgoal[@inlined]) (gid[@trace]) ~depth p g) gs' @ gs with | [] -> [%tcall pop_andl alts next cutto_alts] | { depth; program; goal; gid = gid [@trace] } :: gs -> [%tcall run depth program goal (gid[@trace]) gs next alts cutto_alts]) | exception No_clause -> [%spy "user:rule:builtin" ~rid ~gid pp_string "fail"]; [%tcall next_alt alts] end | Cons (g,gs') -> [%spy "user:rule" ~rid ~gid pp_string "and"]; let gid'[@trace] = make_subgoal_id gid ((depth,g)[@trace]) in let gs' = (make_subgoal[@inlined]) ~depth (gid[@trace]) p gs' in [%spy "user:rule:and" ~rid ~gid pp_string "success"]; [%tcall run depth p g (gid'[@trace]) (gs' :: gs) next alts cutto_alts] | Nil -> [%spy "user:rule" ~rid ~gid pp_string "true"]; [%spy "user:rule:true" ~rid ~gid pp_string "success"]; begin match gs with | [] -> [%tcall pop_andl alts next cutto_alts] | { depth; program; goal; gid = gid [@trace] } :: gs -> [%tcall run depth program goal (gid[@trace]) gs next alts cutto_alts] end | UVar ({ contents = g }, from, args) when g != C.dummy -> [%spy "user:rule" ~rid ~gid pp_string "deref"]; [%spy "user:rule:deref" ~rid ~gid pp_string "success"]; [%tcall run depth p (deref_uv ~from ~to_:depth args g) (gid[@trace]) gs next alts cutto_alts] | AppUVar ({contents = t}, from, args) when t != C.dummy -> [%spy "user:rule" ~rid ~gid pp_string "deref"]; [%spy "user:rule:deref" ~rid ~gid pp_string "success"]; [%tcall run depth p (deref_appuv ~from ~to_:depth args t) (gid[@trace]) gs next alts cutto_alts] | Const k -> let clauses = get_clauses ~depth k g p.index in [%spy "user:rule" ~rid ~gid pp_string "backchain"]; [%spyl "user:rule:backchain:candidates" ~rid ~gid (pp_candidate ~depth ~k) (Bl.to_list clauses)]; [%tcall backchain depth p (k, C.dummy, [], gs) (gid[@trace]) next alts cutto_alts clauses] | App (k,x,xs) -> let clauses = get_clauses ~depth k g p.index in [%spy "user:rule" ~rid ~gid pp_string "backchain"]; [%spyl "user:rule:backchain:candidates" ~rid ~gid (pp_candidate ~depth ~k) (Bl.to_list clauses)]; [%tcall backchain depth p (k, x, xs, gs) (gid[@trace]) next alts cutto_alts clauses] | Builtin(Cut,_) -> anomaly "cut with arguments" | Builtin(And,[]) -> anomaly "and without arguments" | Builtin(Eq,_) -> anomaly "eq without 2 arguments" | Builtin(Match,_) -> anomaly "match without 2 arguments" | Builtin(Impl,_) -> anomaly "impl without 2 arguments" | Builtin(RImpl,_) -> anomaly "rimpl without 2 arguments" | Builtin(ImplBang,_) -> anomaly "implbang without 2 arguments" | Builtin(Pi,_) -> anomaly "pi without 1 argument" | Builtin(Sigma,_) -> anomaly "sigma without 1 argument" | Builtin(Findall,_) -> anomaly "findall without 2 arguments" | Arg _ | AppArg _ -> anomaly "The goal is not a heap term" | Lam _ | CData _ -> type_error ("The goal is not a predicate:" ^ (show_term g)) | UVar _ | AppUVar _ | Discard -> error "The goal is a flexible term" end] (* We pack some arguments into a tuple otherwise we consume too much stack *) and backchain depth p (k, arg, args_of_g, gs) (gid[@trace]) next alts cutto_alts cp = [%trace "select" ~rid begin if Bl.is_empty cp then begin [%spy "user:rule:backchain" ~rid ~gid pp_string "fail"]; [%tcall next_alt alts] end else let { depth = c_depth; mode = c_mode; args = c_args; hyps = c_hyps; vars = c_vars; loc }, cs = Bl.next cp in [%spy "user:rule:backchain:try" ~rid ~gid (pp_option Util.CData.pp) (Util.option_map Ast.cloc.Util.CData.cin loc) (ppclause ~hd:k) { depth = c_depth; mode = c_mode; args = c_args; hyps = c_hyps; vars = c_vars; loc; timestamp = [] }]; let old_trail = !T.trail in T.last_call := alts == noalts && Bl.is_empty cs; let env = Array.make c_vars C.dummy in match match c_args with | [] -> arg == C.dummy && args_of_g == [] | x :: xs -> arg != C.dummy && match c_mode with | [] -> unif ~argsdepth:depth ~matching:false (gid[@trace]) depth env c_depth arg x && for_all23 ~argsdepth:depth (unif (gid[@trace])) depth env c_depth args_of_g xs | arg_mode :: ms -> unif ~argsdepth:depth ~matching:(Mode.get_head arg_mode == Input) (gid[@trace]) depth env c_depth arg x && for_all3b3 ~argsdepth:depth (unif (gid[@trace])) depth env c_depth args_of_g xs ms false with | false -> T.undo ~old_trail (); [%tcall backchain depth p (k, arg, args_of_g, gs) (gid[@trace]) next alts cutto_alts cs] | true -> let oldalts = alts in let alts = if Bl.is_empty cs then alts else { program = p; adepth = depth; agoal_hd = k; ogoal_arg = arg; ogoal_args = args_of_g; agid = gid[@trace]; goals = gs; stack = next; trail = old_trail; state = !CS.state; clauses = cs; cutto_alts = cutto_alts ; next = alts} in begin match c_hyps with | [] -> [%spy "user:rule:backchain" ~rid ~gid pp_string "success"]; begin match gs with | [] -> [%tcall pop_andl alts next cutto_alts] | { depth ; program; goal; gid = gid [@trace] } :: gs -> [%tcall run depth program goal (gid[@trace]) gs next alts cutto_alts] end | h::hs -> let next = if gs = [] then next else FCons (cutto_alts,gs,next) in let h = move ~argsdepth:depth ~from:c_depth ~to_:depth env h in let gid[@trace] = make_subgoal_id gid ((depth,h)[@trace]) in let hs = List.map (fun x-> (make_subgoal[@inlined]) (gid[@trace]) ~depth p (move ~argsdepth:depth ~from:c_depth ~to_:depth env x)) hs in [%spy "user:rule:backchain" ~rid ~gid pp_string "success"]; [%tcall run depth p h (gid[@trace]) hs next alts oldalts] end end] and cut (gid[@trace]) gs next (alts[@trace]) cutto_alts = [%spy "user:rule" ~rid ~gid pp_string "cut"]; let ()[@trace] = let rec prune ({ agid = agid[@trace]; clauses; adepth = depth; agoal_hd = hd } as alts) = if alts != cutto_alts then begin List.iter (fun c -> [%spy "user:rule:cut:branch" ~rid UUID.pp agid (pp_option Util.CData.pp) (Util.option_map Ast.cloc.Util.CData.cin c.loc) (ppclause ~hd) c]) (clauses |> Bl.to_list); prune alts.next end in prune alts in if cutto_alts == noalts then (T.cut_trail[@inlined]) (); [%spy "user:rule:cut" ~rid ~gid pp_string "success"]; match gs with | [] -> pop_andl cutto_alts next cutto_alts | { depth; program; goal; gid = gid [@trace] } :: gs -> run depth program goal (gid[@trace]) gs next cutto_alts cutto_alts and findall depth p g s (gid[@trace]) gs next alts cutto_alts = [%spy "user:rule" ~rid ~gid pp_string "findall"]; let g, fresh_g = copy_heap_drop_csts ~depth g in (* so that Discard becomes a variable *) [%trace "findall" ~rid ("@[<hov 2>query: %a@]" (uppterm depth [] ~argsdepth:0 empty_env) g) begin let executable = { (* same program *) compiled_program = p; (* no meta meta level *) chr = CHR.empty; initial_goal = g; assignments = StrMap.empty; initial_depth = depth; initial_runtime_state = !CS.initial_state; symbol_table = !C.table; builtins = !FFI.builtins; } in let { search; next_solution; destroy; get; _ } = !do_make_runtime executable in let solutions = ref [] in let add_sol () = if get CS.Ugly.delayed <> [] then error "findall search must not declare constraint(s)"; let sol, _ = HO.copy_heap_drop_csts ~depth ~keep_if_outside:fresh_g g in [%spy "findall solution:" ~rid ~gid (ppterm depth [] ~argsdepth:0 empty_env) g]; solutions := sol :: !solutions in let alternatives = ref noalts in try alternatives := search (); add_sol (); while true do alternatives := next_solution !alternatives; add_sol (); done; raise No_clause with No_clause -> let solutions = list_to_lp_list (List.rev !solutions) in [%spy "findall solutions:" ~rid ~gid (ppterm depth [] ~argsdepth:0 empty_env) solutions]; destroy (); [%spy "findall solutions:" ~rid ~gid (ppterm depth [] ~argsdepth:0 empty_env) solutions]; match unif ~argsdepth:depth ~matching:false (gid[@trace]) depth empty_env depth s solutions with | false -> [%spy "user:rule:findall" ~rid ~gid pp_string "fail"]; [%tcall next_alt alts] | true -> [%spy "user:rule:findall" ~rid ~gid pp_string "success"]; begin match gs with | [] -> [%tcall pop_andl alts next cutto_alts] | { depth ; program; goal; gid = gid [@trace] } :: gs -> [%tcall run depth program goal (gid[@trace]) gs next alts cutto_alts] end end] and pop_andl alts next cutto_alts = match next with | FNil -> (match resume_all () with None -> Fmt.fprintf Fmt.std_formatter "Undo triggered by goal resumption\n%!"; [%tcall next_alt alts] | Some ({ depth; program; goal; gid = gid [@trace] } :: gs) -> run depth program goal (gid[@trace]) gs FNil alts cutto_alts | Some [] -> alts) | FCons (_,[],_) -> anomaly "empty stack frame" | FCons(cutto_alts, { depth; program; goal; gid = gid [@trace] } :: gs, next) -> run depth program goal (gid[@trace]) gs next alts cutto_alts and resume_all () : goal list option = (* if fm then Some [] else *) (*if (!to_resume <> []) then begin prerr_endline ("## RESUME ALL R " ^ string_of_int (List.length !to_resume)); prerr_endline ("## RESUME ALL D " ^ string_of_int (List.length !delayed)); end;*) let ok = ref true in let to_be_resumed = ref [] in (* Phase 1: we analyze the goals to be resumed *) while !ok && !CS.to_resume <> [] do match !CS.to_resume with | { kind = Unification { adepth; bdepth; env; a; b; matching } } as dg :: rest -> CS.remove_old dg; CS.to_resume := rest; [%spy "user:resume-unif" ~rid (fun fmt () -> Fmt.fprintf fmt "@[<hov 2>^%d:%a@ == ^%d:%a@]\n%!" adepth (uppterm adepth [] ~argsdepth:0 empty_env) a bdepth (uppterm bdepth [] ~argsdepth:adepth env) b) ()]; ok := unif ~argsdepth:adepth ~matching ((UUID.make ())[@trace]) adepth env bdepth a b | { kind = Constraint dpg } as c :: rest -> CS.remove_old c; CS.to_resume := rest; (*Fmt.fprintf Fmt.std_formatter "Resuming goal: @[<hov 2> ...@ ⊢^%d %a@]\n%!" (*"Resuming goal: @[<hov 2> %a@ ⊢^%d %a@]\n%!"*) (*(pplist (uppterm depth [] ~argsdepth:0 empty_env) ",") pdiff*) depth (uppterm depth [] ~argsdepth:0 empty_env) g ;*) to_be_resumed := dpg :: !to_be_resumed | _ -> anomaly "Unknown constraint type" done ; (* Phase 2: we propagate the constraints *) if !ok then let to_be_resumed = Constraints.resumption !to_be_resumed in let ()[@trace] = if to_be_resumed != [] then begin [%spy "user:rule" ~rid pp_string "resume"]; List.iter pp_resumed_goal to_be_resumed; [%spy "user:rule:resume" ~rid pp_string "success"]; end in (* Optimization: check here new_delayed *) if !CS.new_delayed <> [] then begin let ()[@trace] = if to_be_resumed != [] then begin Trace_ppx_runtime.Runtime.incr_cur_step ~runtime_id:!rid "run"; end in let { Constraints.new_goals; some_rule_was_tried = some_rule_was_tried[@trace] } = Constraints.propagation () in let ()[@trace] = List.iter pp_CHR_resumed_goal new_goals; if some_rule_was_tried && new_goals = [] then begin Trace_ppx_runtime.Runtime.incr_cur_step ~runtime_id:!rid "run"; end in Some (to_be_resumed @ new_goals) end else Some to_be_resumed else begin [%spy "user:rule" ~rid pp_string "constraint-failure"]; None end and next_alt alts = if alts == noalts then raise No_clause else let { program = p; clauses; agoal_hd = k; ogoal_arg = arg; ogoal_args = args; agid = gid [@trace]; goals = gs; stack = next; trail = old_trail; state = old_state; adepth = depth; cutto_alts = cutto_alts; next = alts} = alts in T.undo ~old_trail ~old_state (); [%trace "run" ~rid begin [%cur_pred (Some (C.show k))]; [%spyl "user:curgoal" ~rid ~gid (uppterm depth [] ~argsdepth:0 empty_env) [Const k;App(k,arg,args)]]; [%spy "user:rule" ~rid ~gid pp_string "backchain"]; [%spyl "user:rule:backchain:candidates" ~rid ~gid (pp_candidate ~depth ~k) (Bl.to_list clauses)]; [%tcall backchain depth p (k, arg, args, gs) (gid[@trace]) next alts cutto_alts clauses] end] in (* Finally the runtime *) fun ?max_steps ?(delay_outside_fragment = false) { compiled_program; chr; initial_depth; initial_goal; initial_runtime_state; assignments; symbol_table; builtins; } -> let { Fork.exec = exec ; get = get ; set = set } = Fork.fork () in set orig_prolog_program compiled_program; set Constraints.chrules chr; set T.initial_trail T.empty; set T.trail T.empty; set T.last_call false; set CS.new_delayed []; set CS.to_resume []; set CS.blockers_map IntMap.empty; set CS.Ugly.delayed []; set steps_bound max_steps; set delay_hard_unif_problems delay_outside_fragment; set steps_made 0; set CS.state initial_runtime_state; set CS.initial_state initial_runtime_state; set C.table symbol_table; set FFI.builtins builtins; set rid !max_runtime_id; let search = exec (fun () -> [%spy "dev:trail:init" ~rid (fun fmt () -> T.print_trail fmt) ()]; let gid[@trace] = UUID.make () in [%spy "user:newgoal" ~rid ~gid (uppterm initial_depth [] ~argsdepth:0 empty_env) initial_goal]; T.initial_trail := !T.trail; run initial_depth !orig_prolog_program initial_goal (gid[@trace]) [] FNil noalts noalts) in let destroy () = exec (fun () -> T.undo ~old_trail:T.empty ()) () in let next_solution = exec next_alt in incr max_runtime_id; { search; next_solution; destroy; exec; get } ;; do_make_runtime := make_runtime;; end (* }}} *) open Mainloop (****************************************************************************** API ******************************************************************************) let mk_outcome search get_cs assignments depth = try let alts = search () in let syn_csts, reloc_state, final_state, pp_ctx = get_cs () in let solution = { assignments; constraints = syn_csts; state = final_state; pp_ctx = pp_ctx; state_for_relocation = reloc_state; } in Success solution, alts with | No_clause (*| Non_linear*) -> Failure, noalts | No_more_steps -> NoMoreSteps, noalts let execute_once ?max_steps ?delay_outside_fragment exec = let { search; get } = make_runtime ?max_steps ?delay_outside_fragment exec in try let result = fst (mk_outcome search (fun () -> get CS.Ugly.delayed, (exec.initial_depth,get C.table), get CS.state |> State.end_execution, { Data.uv_names = ref (get Pp.uv_names); table = get C.table }) exec.assignments exec.initial_depth) in [%end_trace "execute_once" ~rid]; result with e -> [%end_trace "execute_once" ~rid]; raise e ;; let execute_loop ?delay_outside_fragment exec ~more ~pp = let { search; next_solution; get; destroy = _ } = make_runtime ?delay_outside_fragment exec in let k = ref noalts in let do_with_infos f = let time0 = Unix.gettimeofday() in let o, alts = mk_outcome f (fun () -> get CS.Ugly.delayed, (exec.initial_depth,get C.table), get CS.state |> State.end_execution, { Data.uv_names = ref (get Pp.uv_names); table = get C.table }) exec.assignments exec.initial_depth in let time1 = Unix.gettimeofday() in k := alts; pp (time1 -. time0) o in do_with_infos search; while !k != noalts do if not(more()) then k := noalts else try do_with_infos (fun () -> next_solution !k) with | No_clause -> pp 0.0 Failure; k := noalts; [%end_trace "execute_loop" ~rid] | e -> pp 0.0 Failure; k := noalts; [%end_trace "execute_loop" ~rid]; raise e done ;; let pp_stuck_goal ?pp_ctx fmt s = CS.pp_stuck_goal ?pp_ctx fmt s let is_flex = HO.is_flex let deref_uv = HO.deref_uv let deref_appuv = HO.deref_appuv let deref_head = HO.deref_head let full_deref = HO.full_deref ~adepth:0 empty_env let eta_contract_flex = HO.eta_contract_flex let lp_list_to_list = Clausify.lp_list_to_list let list_to_lp_list = HO.list_to_lp_list let split_conj = Clausify.split_conj let mkAppArg = HO.mkAppArg let subst ~depth = HO.subst depth let move = HO.move let hmove = HO.hmove let mkinterval = C.mkinterval let mkAppL = C.mkAppL let lex_insertion = lex_insertion let expand_uv ~depth r ~lvl ~ano = let t, assignment = HO.expand_uv ~depth r ~lvl ~ano in option_iter (fun (r,_,assignment) -> r @:= assignment) assignment; t let expand_appuv ~depth r ~lvl ~args = let t, assignment = HO.expand_appuv ~depth r ~lvl ~args in option_iter (fun (r,_,assignment) -> r @:= assignment) assignment; t module CompileTime = struct let update_indexing = update_indexing let add_to_index = add_to_index_compile_time let clausify1 = Clausify.clausify1 let get_clauses ~depth goal args_idx : overlap_clause Bl.scan = get_clauses_dt ~check_mut_excl:On ~cmp_clause:(fun (c1:overlap_clause) c2 -> lex_insertion c1.timestamp c2.timestamp) ~depth goal args_idx let fresh_uvar () = oref C.dummy end
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>