package kappa-library
Public internals of the Kappa tool suite. Use this package to use kappa as a lib
Install
Dune Dependency
Authors
Maintainers
Sources
v4.1.3.tar.gz
md5=1c9a8a0d79f085757817f90834e166f5
sha512=13ac40442940ba6e72d7dc5bf952e67443872f7bff63e9c76a3a699a6904c88696047fe04519b7ec6546371642f6ee7b0983117be302694aca15500b0df40de3
doc/src/kappa-library.generic/setMap.ml.html
Source file setMap.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
(******************************************************************************) (* _ __ * The Kappa Language *) (* | |/ / * Copyright 2010-2020 CNRS - Harvard Medical School - INRIA - IRIF *) (* | ' / *********************************************************************) (* | . \ * This file is distributed under the terms of the *) (* |_|\_\ * GNU Lesser General Public License Version 3 *) (******************************************************************************) module type OrderedType = sig type t val compare : t -> t -> int val print : Format.formatter -> t -> unit end type ('parameters, 'error, 'a) with_log_wrap = ('parameters -> 'error -> string -> string option -> exn -> 'error) -> 'parameters -> 'error -> 'a module type Set = sig type elt type t val empty : t val is_empty : t -> bool val singleton : elt -> t val is_singleton : t -> bool val add : elt -> t -> t val add_with_logs : ('parameters, 'error, elt -> t -> 'error * t) with_log_wrap val remove : elt -> t -> t val add_while_testing_freshness : ('parameters, 'error, elt -> t -> 'error * bool * t) with_log_wrap val remove_while_testing_existence : ('parameters, 'error, elt -> t -> 'error * bool * t) with_log_wrap val remove_with_logs : ('parameters, 'error, elt -> t -> 'error * t) with_log_wrap val split : elt -> t -> t * bool * t val union : t -> t -> t val disjoint_union : t -> t -> t option val inter : t -> t -> t val minus : t -> t -> t (** [minus a b] contains elements of [a] that are not in [b] *) val diff : t -> t -> t (** [diff a b] = [minus (union a b) (inter a b)] *) val minus_with_logs : ('parameters, 'error, t -> t -> 'error * t) with_log_wrap val union_with_logs : ('parameters, 'error, t -> t -> 'error * t) with_log_wrap val disjoint_union_with_logs : ('parameters, 'error, t -> t -> 'error * t) with_log_wrap val inter_with_logs : ('parameters, 'error, t -> t -> 'error * t) with_log_wrap val diff_with_logs : ('parameters, 'error, t -> t -> 'error * t) with_log_wrap (* val split_with_logs: ('parameters,'error,elt -> t -> 'error * ( t * bool * t)) with_log_wrap *) val size : t -> int val mem : elt -> t -> bool val exists : (elt -> bool) -> t -> bool val filter : (elt -> bool) -> t -> t val filter_with_logs : ('parameters, 'error, (elt -> bool) -> t -> 'error * t) with_log_wrap val for_all : (elt -> bool) -> t -> bool val partition : (elt -> bool) -> t -> t * t val partition_with_logs : ('parameters, 'error, (elt -> bool) -> t -> 'error * t * t) with_log_wrap val compare : t -> t -> int val equal : t -> t -> bool val subset : t -> t -> bool val iter : (elt -> unit) -> t -> unit val fold : (elt -> 'a -> 'a) -> t -> 'a -> 'a val fold_inv : (elt -> 'a -> 'a) -> t -> 'a -> 'a val elements : t -> elt list val print : Format.formatter -> t -> unit val choose : t -> elt option val random : Random.State.t -> t -> elt option val min_elt : t -> elt option val max_elt : t -> elt option end module type Map = sig type elt type set type +'a t val empty : 'a t val is_empty : 'a t -> bool val size : 'a t -> int val root : 'a t -> (elt * 'a) option val max_key : 'a t -> elt option val add : elt -> 'a -> 'a t -> 'a t val remove : elt -> 'a t -> 'a t val add_while_testing_freshness : ( 'parameters, 'error, elt -> 'a -> 'a t -> 'error * bool * 'a t ) with_log_wrap val remove_while_testing_existence : ('parameters, 'error, elt -> 'a t -> 'error * bool * 'a t) with_log_wrap val pop : elt -> 'a t -> 'a option * 'a t val merge : 'a t -> 'a t -> 'a t val min_elt : 'a t -> (elt * 'a) option val find_option : elt -> 'a t -> 'a option val find_default : 'a -> elt -> 'a t -> 'a val find_option_with_logs : ('parameters, 'error, elt -> 'a t -> 'error * 'a option) with_log_wrap val find_default_with_logs : ('parameters, 'error, 'a -> elt -> 'a t -> 'error * 'a) with_log_wrap val mem : elt -> 'a t -> bool val diff : 'a t -> 'a t -> 'a t * 'a t val union : 'a t -> 'a t -> 'a t val update : 'a t -> 'a t -> 'a t val diff_pred : ('a -> 'a -> bool) -> 'a t -> 'a t -> 'a t * 'a t val add_with_logs : ('parameters, 'error, elt -> 'a -> 'a t -> 'error * 'a t) with_log_wrap val remove_with_logs : ('parameters, 'error, elt -> 'a t -> 'error * 'a t) with_log_wrap val join_with_logs : ( 'parameters, 'error, 'a t -> elt -> 'a -> 'a t -> 'error * 'a t ) with_log_wrap val split_with_logs : ( 'parameters, 'error, elt -> 'a t -> 'error * ('a t * 'a option * 'a t) ) with_log_wrap val update_with_logs : ('parameters, 'error, 'a t -> 'a t -> 'error * 'a t) with_log_wrap val map2_with_logs : ( 'parameters, 'error, ('parameters -> 'error -> 'a -> 'error * 'c) -> ('parameters -> 'error -> 'b -> 'error * 'c) -> ('parameters -> 'error -> 'a -> 'b -> 'error * 'c) -> 'a t -> 'b t -> 'error * 'c t ) with_log_wrap val map2z_with_logs : ( 'parameters, 'error, ('parameters -> 'error -> 'a -> 'a -> 'error * 'a) -> 'a t -> 'a t -> 'error * 'a t ) with_log_wrap val fold2z_with_logs : ( 'parameters, 'error, ('parameters -> 'error -> elt -> 'a -> 'b -> 'c -> 'error * 'c) -> 'a t -> 'b t -> 'c -> 'error * 'c ) with_log_wrap val fold2_with_logs : ( 'parameters, 'error, ('parameters -> 'error -> elt -> 'a -> 'c -> 'error * 'c) -> ('parameters -> 'error -> elt -> 'b -> 'c -> 'error * 'c) -> ('parameters -> 'error -> elt -> 'a -> 'b -> 'c -> 'error * 'c) -> 'a t -> 'b t -> 'c -> 'error * 'c ) with_log_wrap val fold2_sparse_with_logs : ( 'parameters, 'error, ('parameters -> 'error -> elt -> 'a -> 'b -> 'c -> 'error * 'c) -> 'a t -> 'b t -> 'c -> 'error * 'c ) with_log_wrap val iter2_sparse_with_logs : ( 'parameters, 'error, ('parameters -> 'error -> elt -> 'a -> 'b -> 'error) -> 'a t -> 'b t -> 'error ) with_log_wrap val diff_with_logs : ('parameters, 'error, 'a t -> 'a t -> 'error * 'a t * 'a t) with_log_wrap val diff_pred_with_logs : ( 'parameters, 'error, ('a -> 'a -> bool) -> 'a t -> 'a t -> 'error * 'a t * 'a t ) with_log_wrap val merge_with_logs : ('parameters, 'error, 'a t -> 'a t -> 'error * 'a t) with_log_wrap val union_with_logs : ('parameters, 'error, 'a t -> 'a t -> 'error * 'a t) with_log_wrap val fold_restriction_with_logs : ( 'parameters, 'error, (elt -> 'a -> 'error * 'b -> 'error * 'b) -> set -> 'a t -> 'b -> 'error * 'b ) with_log_wrap val fold_restriction_with_missing_associations_with_logs : ( 'parameters, 'error, (elt -> 'a -> 'error * 'b -> 'error * 'b) -> (elt -> 'error * 'b -> 'error * 'b) -> set -> 'a t -> 'b -> 'error * 'b ) with_log_wrap val iter : (elt -> 'a -> unit) -> 'a t -> unit val fold : (elt -> 'a -> 'b -> 'b) -> 'a t -> 'b -> 'b val fold_with_interruption : (elt -> 'a -> 'b -> ('b, 'c) Stop.stop) -> 'a t -> 'b -> ('b, 'c) Stop.stop val monadic_fold2 : 'parameters -> 'exceptions_caught_and_uncaught -> ('parameters -> 'exceptions_caught_and_uncaught -> elt -> 'a -> 'b -> 'c -> 'exceptions_caught_and_uncaught * 'c) -> ('parameters -> 'exceptions_caught_and_uncaught -> elt -> 'a -> 'c -> 'exceptions_caught_and_uncaught * 'c) -> ('parameters -> 'exceptions_caught_and_uncaught -> elt -> 'b -> 'c -> 'exceptions_caught_and_uncaught * 'c) -> 'a t -> 'b t -> 'c -> 'exceptions_caught_and_uncaught * 'c val monadic_fold2_sparse : 'parameters -> 'exceptions_caught_and_uncaught -> ('parameters -> 'exceptions_caught_and_uncaught -> elt -> 'a -> 'b -> 'c -> 'exceptions_caught_and_uncaught * 'c) -> 'a t -> 'b t -> 'c -> 'exceptions_caught_and_uncaught * 'c val monadic_iter2_sparse : 'parameters -> 'exceptions_caught_and_uncaught -> ('parameters -> 'exceptions_caught_and_uncaught -> elt -> 'a -> 'b -> 'exceptions_caught_and_uncaught) -> 'a t -> 'b t -> 'exceptions_caught_and_uncaught val monadic_fold_restriction : 'parameters -> 'exceptions_caught_and_uncaught -> ('parameters -> 'exceptions_caught_and_uncaught -> elt -> 'a -> 'b -> 'exceptions_caught_and_uncaught * 'b) -> set -> 'a t -> 'b -> 'exceptions_caught_and_uncaught * 'b val mapi : (elt -> 'a -> 'b) -> 'a t -> 'b t val map : ('a -> 'b) -> 'a t -> 'b t val map2 : ('a -> 'a -> 'a) -> 'a t -> 'a t -> 'a t val for_all : (elt -> 'a -> bool) -> 'a t -> bool val filter_one : (elt -> 'a -> bool) -> 'a t -> (elt * 'a) option val compare : ('a -> 'a -> int) -> 'a t -> 'a t -> int val equal : ('a -> 'a -> bool) -> 'a t -> 'a t -> bool val bindings : 'a t -> (elt * 'a) list val print : (Format.formatter -> 'a -> unit) -> Format.formatter -> 'a t -> unit val of_json : ?lab_key:string -> ?lab_value:string -> ?error_msg:string -> (Yojson.Basic.t -> elt) -> (Yojson.Basic.t -> 'value) -> Yojson.Basic.t -> 'value t val to_json : ?lab_key:string -> ?lab_value:string -> (elt -> Yojson.Basic.t) -> ('value -> Yojson.Basic.t) -> 'value t -> Yojson.Basic.t end module type S = sig type elt module Set : Set with type elt = elt module Map : Map with type elt = elt and type set = Set.t end exception DeadCodeIsNotDead of string module Make (Ord : OrderedType) : S with type elt = Ord.t = struct type elt = Ord.t module Set = struct type elt = Ord.t module Private : sig type t = private Empty | Node of t * elt * t * int * int val empty : t val height : t -> int val size : t -> int val node : t -> elt -> t -> t end = struct type t = Empty | Node of t * elt * t * int * int let empty = Empty let height = function | Empty -> 0 | Node (_, _, _, h, _) -> h let size = function | Empty -> 0 | Node (_, _, _, _, s) -> s let node left value right = let hl = height left in let hr = height right in Node ( left, value, right, (if hl > hr then hl else hr) + 1, size left + size right + 1 ) end type t = Private.t let empty = Private.empty let height = Private.height let size = Private.size let node = Private.node let is_empty = function | Private.Empty -> true | Private.Node _ -> false let singleton value = node empty value empty let is_singleton set = match set with | Private.Empty -> false | Private.Node (set1, _, set2, _, _) -> is_empty set1 && is_empty set2 let balance left value right = let height_left = height left in let height_right = height right in if height_left > height_right + 2 then ( match left with | Private.Empty -> raise (DeadCodeIsNotDead "SetMap line 222") (* height_left > height_right + 2 >= 2 *) | Private.Node (leftleft, leftvalue, leftright, _, _) -> if height leftleft >= height leftright then node leftleft leftvalue (node leftright value right) else ( match leftright with | Private.Empty -> raise (DeadCodeIsNotDead "SetMap line 229") (* 0 <= height leftleft < height leftright *) | Private.Node (leftrightleft, leftrightvalue, leftrightright, _, _) -> node (node leftleft leftvalue leftrightleft) leftrightvalue (node leftrightright value right) ) ) else if height_right > height_left + 2 then ( match right with | Private.Empty -> raise (DeadCodeIsNotDead "SetMap line 238") (* height_right > height_left + 2 >= 2 *) | Private.Node (rightleft, rightvalue, rightright, _, _) -> if height rightright >= height rightleft then node (node left value rightleft) rightvalue rightright else ( match rightleft with | Private.Empty -> raise (DeadCodeIsNotDead "SetMap line 245") (* 0 <= height rightright < height rightleft *) | Private.Node (rightleftleft, rightleftvalue, rightleftright, _, _) -> node (node left value rightleftleft) rightleftvalue (node rightleftright rightvalue rightright) ) ) else node left value right let balance_with_logs warn parameters error left value right = try error, balance left value right with DeadCodeIsNotDead loc -> let error = warn parameters error "setMap.ml" (Some (loc ^ " Set invariant is broken, keep on with unbalanced set")) (Failure "Set_and_Map.SET.balance") in error, node left value right (** Beware some code relies on the invariant [add x s == s] iff [mem x s] *) let rec add x = function | Private.Empty -> singleton x | Private.Node (l, v, r, _, _) as t -> let c = Ord.compare x v in if c = 0 then t else if c < 0 then ( let o = add x l in if o == l then t else balance o v r ) else ( let o = add x r in if o == r then t else balance l v o ) let rec add_while_testing_freshness warn parameters error new_val set = match set with | Private.Empty -> error, true, singleton new_val | Private.Node (left, value_set, right, _, _) -> let c = Ord.compare new_val value_set in if c = 0 then error, false, set else if c < 0 then ( let error, bool, left' = add_while_testing_freshness warn parameters error new_val left in let error, set = balance_with_logs warn parameters error left' value_set right in error, bool, set ) else ( let error, bool, right' = add_while_testing_freshness warn parameters error new_val right in let error, set = balance_with_logs warn parameters error left value_set right' in error, bool, set ) let add_with_logs warn parameters error new_value set = let error, bool, set = add_while_testing_freshness warn parameters error new_value set in let error = if bool then error else warn parameters error "setMap.ml" (Some ("SetMap line 300" ^ " an already elt has been added to a set")) (Failure "Set_and_Map.SET.add") in error, set let add_even_if_it_exists warn parameters error new_value set = let error, _, set = add_while_testing_freshness warn parameters error new_value set in error, set let rec join left value right = match left, right with | Private.Empty, _ -> add value right | _, Private.Empty -> add value left | ( Private.Node (leftleft, leftvalue, leftright, leftheight, _), Private.Node (rightleft, rightvalue, rightright, rightheight, _) ) -> if leftheight > rightheight + 2 then ( let right' = join leftright value right in balance leftleft leftvalue right' ) else if rightheight > leftheight + 2 then ( let left' = join left value rightleft in balance left' rightvalue rightright ) else node left value right let rec safe_extract_min_elt left value right = match left with | Private.Empty -> value, right | Private.Node (leftleft, leftvalue, leftright, _, _) -> let min, left' = safe_extract_min_elt leftleft leftvalue leftright in min, balance left' value right let rec min_elt_with_logs warn parameters error set = match set with | Private.Empty -> let error = warn parameters error "setMap.ml" (Some "min_elt_with_logs, line 303") Not_found in error, None | Private.Node (Private.Empty, v, _, _, _) -> error, Some v | Private.Node (left, _, _, _, _) -> min_elt_with_logs warn parameters error left let rec remove_min_elt_with_logs warn parameters error set = match set with | Private.Empty -> let error = warn parameters error "setMap.ml" (Some "remove_min_elt_with_logs, line 311") Not_found in error, empty | Private.Node (Private.Empty, _, right, _, _) -> error, right | Private.Node (left, value, right, _, _) -> let error, left' = remove_min_elt_with_logs warn parameters error left in balance_with_logs warn parameters error left' value right let merge set1 set2 = match set1, set2 with | Private.Empty, _ -> set2 | _, Private.Empty -> set1 | Private.Node _, Private.Node (left2, value2, right2, _, _) -> let min2, set2' = safe_extract_min_elt left2 value2 right2 in balance set1 min2 set2' let merge_with_logs warn parameters error set1 set2 = match set1, set2 with | Private.Empty, _ -> error, set2 | _, Private.Empty -> error, set1 | Private.Node _, Private.Node _ -> let error, left2 = remove_min_elt_with_logs warn parameters error set2 in let error, elt_opt = min_elt_with_logs warn parameters error set2 in (match elt_opt with | None -> let error = warn parameters error "setMap.ml" (Some "merge_with_logs,line 339") Not_found in error, set1 | Some elt -> balance_with_logs warn parameters error set1 elt left2) let rec join_with_logs warn parameters error left value right = match left, right with | Private.Empty, _ -> add_with_logs warn parameters error value right | _, Private.Empty -> add_with_logs warn parameters error value left | ( Private.Node (leftleft, leftvalue, leftright, leftheight, _), Private.Node (rightleft, rightvalue, rightright, rightheight, _) ) -> if leftheight > rightheight + 2 then ( let error, right' = join_with_logs warn parameters error leftright value right in balance_with_logs warn parameters error leftleft leftvalue right' ) else if rightheight > leftheight + 2 then ( let error, left' = join_with_logs warn parameters error left value rightleft in balance_with_logs warn parameters error left' rightvalue rightright ) else error, node left value right let concat set1 set2 = match set1, set2 with | Private.Empty, _ -> set2 | _, Private.Empty -> set1 | Private.Node _, Private.Node (left2, value2, right2, _, _) -> let min2, set2' = safe_extract_min_elt left2 value2 right2 in join set1 min2 set2' let concat_with_logs warn parameters error set1 set2 = match set1, set2 with | Private.Empty, _ -> error, set2 | _, Private.Empty -> error, set1 | Private.Node _, Private.Node _ -> let error, left2 = remove_min_elt_with_logs warn parameters error set2 in let error, elt_opt = min_elt_with_logs warn parameters error set2 in (match elt_opt with | None -> let error = warn parameters error "setMap.ml" (Some "concat_with_logs,line 390") Not_found in error, set1 | Some elt -> join_with_logs warn parameters error set1 elt left2) let rec split_with_logs warn parameters error split_val set = match set with | Private.Empty -> error, (empty, false, empty) | Private.Node (left, set_val, right, _, _) -> let c = Ord.compare split_val set_val in if c = 0 then error, (left, true, right) else if c < 0 then ( let error, (leftleft, bool, rightleft) = split_with_logs warn parameters error split_val left in let error, rightright = join_with_logs warn parameters error rightleft set_val right in error, (leftleft, bool, rightright) ) else ( let error, (leftright, bool, rightright) = split_with_logs warn parameters error split_val right in let error, leftleft = join_with_logs warn parameters error left set_val leftright in error, (leftleft, bool, rightright) ) let rec remove value = function | Private.Empty as set -> set | Private.Node (left, value_set, right, _, _) as set -> let c = Ord.compare value value_set in if c = 0 then merge left right else if c < 0 then ( let left' = remove value left in if left == left' then set else balance left' value_set right ) else ( let right' = remove value right in if right == right' then set else balance left value_set right' ) let rec remove_while_testing_existence warn parameters error value = function | Private.Empty as set -> error, false, set | Private.Node (left, value_set, right, _, _) as set -> let c = Ord.compare value value_set in if c = 0 then ( let error, set = merge_with_logs warn parameters error left right in error, true, set ) else if c < 0 then ( let error, bool, left' = remove_while_testing_existence warn parameters error value left in if left == left' then ( let error = if bool then warn parameters error "setMap.ml" (Some "SetMap line 454") (failwith "Invariant is broken") else error in error, bool, set ) else ( let error, set = balance_with_logs warn parameters error left' value_set right in error, bool, set ) ) else ( let error, bool, right' = remove_while_testing_existence warn parameters error value right in if right == right' then ( let error = if bool then warn parameters error "setMap.ml" (Some "SetMap line 467") Not_found else error in error, bool, set ) else ( let error, set = balance_with_logs warn parameters error left value_set right' in error, bool, set ) ) let remove_with_logs warn parameters error value set = let error, bool, set = remove_while_testing_existence warn parameters error value set in if bool then error, set else ( warn parameters error "setMap.ml" (Some ("SetMap line 481" ^ "Attempt to remove an elt that does not exist")) Not_found, set ) let rec split split_value set = match set with | Private.Empty -> empty, false, empty | Private.Node (left, set_value, right, _, _) -> let c = Ord.compare split_value set_value in if c = 0 then left, true, right else if c < 0 then ( let leftleft, bool, rightleft = split split_value left in let rightright = join rightleft set_value right in leftleft, bool, rightright ) else ( let leftright, bool, rightright = split split_value right in let leftleft = join left set_value leftright in leftleft, bool, rightright ) let rec union set1 set2 = match set1, set2 with | Private.Empty, _ -> set2 | _, Private.Empty -> set1 | ( Private.Node (left1, value1, right1, height1, _), Private.Node (left2, value2, right2, height2, _) ) -> if height1 > height2 then if height2 = 1 then add value2 set1 else ( let left2, _, right2 = split value1 set2 in let left' = union left1 left2 in let right' = union right1 right2 in join left' value1 right' ) else if height1 = 1 then add value1 set2 else ( let left1, _, right1 = split value2 set1 in let left' = union left1 left2 in let right' = union right1 right2 in join left' value2 right' ) let rec disjoint_union set1 set2 = match set1, set2 with | Private.Empty, _ -> Some set2 | _, Private.Empty -> Some set1 | ( Private.Node (left1, value1, right1, height1, _), Private.Node (left2, value2, right2, height2, _) ) -> if height1 > height2 then if height2 = 1 then ( let out = add value2 set1 in if out == set1 then None else Some out ) else ( let left2, _, right2 = split value1 set2 in match disjoint_union left1 left2, disjoint_union right1 right2 with | Some left', Some right' -> Some (join left' value1 right') | _, _ -> None ) else if height1 = 1 then ( let out = add value1 set2 in if set2 == out then None else Some out ) else ( let left1, _, right1 = split value2 set1 in match disjoint_union left1 left2, disjoint_union right1 right2 with | Some left', Some right' -> Some (join left' value2 right') | _, _ -> None ) let rec union_gen add_gen warn parameters error set1 set2 = match set1, set2 with | Private.Empty, _ -> error, set2 | _, Private.Empty -> error, set1 | ( Private.Node (left1, value1, right1, height1, _), Private.Node (left2, value2, right2, height2, _) ) -> if height1 > height2 then if height2 = 1 then add_gen warn parameters error value2 set1 else ( let error, (left2, _, right2) = split_with_logs warn parameters error value1 set2 in let error, left' = union_gen add_gen warn parameters error left1 left2 in let error, right' = union_gen add_gen warn parameters error right1 right2 in join_with_logs warn parameters error left' value1 right' ) else if height1 = 1 then add_gen warn parameters error value1 set2 else ( let error, (left1, _, right1) = split_with_logs warn parameters error value2 set1 in let error, left' = union_gen add_gen warn parameters error left1 left2 in let error, right' = union_gen add_gen warn parameters error right1 right2 in join_with_logs warn parameters error left' value2 right' ) let union_with_logs w p e s s' = union_gen add_even_if_it_exists w p e s s' let disjoint_union_with_logs w p e s s' = union_gen add_with_logs w p e s s' let suture (left1, value1, right1) (left2, bool, right2) f = let left' = f left1 left2 in let right' = f right1 right2 in if bool then join left' value1 right' else concat left' right' let suture_not (left1, value1, right1) (left2, bool, right2) f = let left' = f left1 left2 in let right' = f right1 right2 in if bool then concat left' right' else join left' value1 right' let rec inter set1 set2 = match set1, set2 with | Private.Empty, _ | _, Private.Empty -> empty | Private.Node (left1, value1, right1, _, _), _ -> let triple2 = split value1 set2 in suture (left1, value1, right1) triple2 inter let suture_with_logs warn parameters error (left1, value1, right1) (left2, bool, right2) f = let error, left' = f warn parameters error left1 left2 in let error, right' = f warn parameters error right1 right2 in if bool then join_with_logs warn parameters error left' value1 right' else concat_with_logs warn parameters error left' right' let suture_not_with_logs warn parameters error (left1, value1, right1) (left2, bool, right2) f = let error, left' = f warn parameters error left1 left2 in let error, right' = f warn parameters error right1 right2 in if bool then concat_with_logs warn parameters error left' right' else join_with_logs warn parameters error left' value1 right' let rec inter_with_logs warn parameters error set1 set2 = match set1, set2 with | Private.Empty, _ | _, Private.Empty -> error, empty | Private.Node (left1, value1, right1, _, _), _ -> let mh', triple2 = split_with_logs warn parameters error value1 set2 in suture_with_logs warn parameters mh' (left1, value1, right1) triple2 inter_with_logs let rec diff set1 set2 = match set1, set2 with | Private.Empty, _ -> set2 | _, Private.Empty -> set1 | Private.Node (left1, value1, right1, _, _), _ -> let triple2 = split value1 set2 in suture_not (left1, value1, right1) triple2 diff let rec diff_with_logs warn parameters error set1 set2 = match set1, set2 with | Private.Empty, _ -> error, empty | _, Private.Empty -> error, set1 | Private.Node (left1, value1, right1, _, _), _ -> let error, triple2 = split_with_logs warn parameters error value1 set2 in suture_not_with_logs warn parameters error (left1, value1, right1) triple2 diff_with_logs let rec minus set1 set2 = match set1, set2 with | Private.Empty, _ -> empty | _, Private.Empty -> set1 | Private.Node (left1, value1, right1, _, _), _ -> let triple2 = split value1 set2 in suture_not (left1, value1, right1) triple2 minus let rec minus_with_logs warn parameters error set1 set2 = match set1, set2 with | Private.Empty, _ -> error, empty | _, Private.Empty -> error, set1 | Private.Node (left1, value1, right1, _, _), _ -> let error, triple2 = split_with_logs warn parameters error value1 set2 in suture_not_with_logs warn parameters error (left1, value1, right1) triple2 minus_with_logs let rec mem searched_value = function | Private.Empty -> false | Private.Node (left, set_value, right, _, _) -> let c = Ord.compare searched_value set_value in c == 0 || mem searched_value (if c < 0 then left else right) let filter p set = let rec filt accu = function | Private.Empty -> accu | Private.Node (left, value, right, _, _) -> filt (filt (if p value then add value accu else accu) left) right in filt empty set let filter_with_logs warn parameters error p set = let rec filt accu set = match set with | Private.Empty -> accu | Private.Node (left, value, right, _, _) -> let error, list = accu in filt (filt (if p value then add_with_logs warn parameters error value list else accu) left) right in filt (error, empty) set let partition p set = let rec part ((t, f) as accu) = function | Private.Empty -> accu | Private.Node (left, value, right, _, _) -> part (part (if p value then add value t, f else t, add value f) left) right in part (empty, empty) set let partition_with_logs warn parameters error p set = let rec part ((rh, t, f) as accu) set = match set with | Private.Empty -> accu | Private.Node (left, value, right, _, _) -> part (part (if p value then ( let a, b = add_with_logs warn parameters rh value t in a, b, f ) else ( let a, c = add_with_logs warn parameters rh value f in a, t, c )) left) right in part (error, empty, empty) set type enumeration = End | More of elt * t * enumeration let rec cons_enum enum = function | Private.Empty -> enum | Private.Node (left, value, right, _, _) -> cons_enum (More (value, right, enum)) left let rec compare_aux e1 e2 = match e1, e2 with | End, End -> 0 | End, _ -> -1 | _, End -> 1 | More (v1, r1, e1), More (v2, r2, e2) -> let c = Ord.compare v1 v2 in if c <> 0 then c else compare_aux (cons_enum e1 r1) (cons_enum e2 r2) let compare set1 set2 = compare_aux (cons_enum End set1) (cons_enum End set2) let equal set1 set2 = compare set1 set2 == 0 let rec subset set1 set2 = match set1, set2 with | Private.Empty, _ -> true | _, Private.Empty -> false | ( Private.Node (left1, value1, right1, _, _), Private.Node (left2, value2, right2, _, _) ) -> let c = Ord.compare value1 value2 in if c = 0 then subset left1 left2 && subset right1 right2 else if c < 0 then subset (node left1 value1 empty) left2 && subset right1 set2 else subset (node empty value1 right1) right2 && subset left1 set2 let rec iter f = function | Private.Empty -> () | Private.Node (left, value, right, _, _) -> let () = iter f left in let () = f value in iter f right let rec fold f set accu = match set with | Private.Empty -> accu | Private.Node (left, value, right, _, _) -> fold f right (f value (fold f left accu)) let rec fold_inv f s accu = match s with | Private.Empty -> accu | Private.Node (l, v, r, _, _) -> fold_inv f l (f v (fold_inv f r accu)) let rec for_all p = function | Private.Empty -> true | Private.Node (left, value, right, _, _) -> p value && for_all p left && for_all p right let rec exists p = function | Private.Empty -> false | Private.Node (left, value, right, _, _) -> p value || exists p left || exists p right let elements set = let rec elements_aux accu = function | Private.Empty -> accu | Private.Node (left, value, right, _, _) -> elements_aux (value :: elements_aux accu right) left in elements_aux [] set let rec aux_print f = function | Private.Empty -> () | Private.Node (Private.Empty, key, Private.Empty, _, _) -> Format.fprintf f "@[%a@]" Ord.print key | Private.Node (Private.Empty, key, right, _, _) -> Format.fprintf f "@[%a@],@ %a" Ord.print key aux_print right | Private.Node (left, key, Private.Empty, _, _) -> Format.fprintf f "%a,@ @[%a@]" aux_print left Ord.print key | Private.Node (left, key, right, _, _) -> Format.fprintf f "%a,@ @[%a@],@ %a" aux_print left Ord.print key aux_print right let print f = function | Private.Empty -> Format.pp_print_string f "\xE2\x88\x85" | Private.Node _ as m -> Format.fprintf f "@[{%a}@]" aux_print m let rec min_elt = function | Private.Empty -> None | Private.Node (Private.Empty, v, _, _, _) -> Some v | Private.Node (left, _, _, _, _) -> min_elt left let rec max_elt = function | Private.Empty -> None | Private.Node (_, v, Private.Empty, _, _) -> Some v | Private.Node (_, _, right, _, _) -> max_elt right let choose = function | Private.Empty -> None | Private.Node (_, v, _, _, _) -> Some v let rec find_acc aim_acc = function | Private.Empty -> None | Private.Node (l, key, r, _, acc) -> if aim_acc >= acc then None else ( let acc_l = size l in let acc_r = size r in if acc_l > aim_acc then find_acc aim_acc l else if acc_r + acc_l > aim_acc then find_acc (aim_acc - acc_l) r else Some key ) (* let rec find_acc k m = match m with | Private.Empty -> None | Private.Node (l, key, r, _, _) -> let s = size l in if k < s then find_acc k l else if k = s then Some key else find_acc (k - s - 1) r *) let random rs m = let s = size m in if s = 0 then None else find_acc (Random.State.int rs (size m)) m (* let add = Lift_error_logs.lift_generic_binary_for_KaSim add_with_logs let split = Lift_error_logs.lift_generic_binary_for_KaSim split_with_logs let remove = Lift_error_logs.lift_generic_binary_for_KaSim remove_with_logs let union = Lift_error_logs.lift_generic_binary_for_KaSim union_with_logs let inter = Lift_error_logs.lift_generic_binary_for_KaSim inter_with_logs let diff = Lift_error_logs.lift_generic_binary_for_KaSim diff_with_logs let minus = Lift_error_logs.lift_generic_binary_for_KaSim minus_with_logs let filter = Lift_error_logs.lift_generic_binary_for_KaSim filter_with_logs let partition = Lift_error_logs.lift_generic_binary_binary_for_KaSim partition_with_logs *) end (**************************************************************************) (* Map implementation*) module Map = struct type elt = Ord.t module Private : sig type +'data t = private | Empty | Node of 'data t * elt * 'data * 'data t * int * int val empty : 'a t val height : 'a t -> int val size : 'a t -> int val node : 'a t -> elt -> 'a -> 'a t -> 'a t end = struct type +'data t = | Empty | Node of 'data t * elt * 'data * 'data t * int * int let empty = Empty let height = function | Empty -> 0 | Node (_, _, _, _, h, _) -> h let size = function | Empty -> 0 | Node (_, _, _, _, _, s) -> s let node left key0 data right = let hl = height left in let hr = height right in Node ( left, key0, data, right, (1 + if hl > hr then hl else hr), 1 + size left + size right ) end type +'a t = 'a Private.t let empty = Private.empty let height = Private.height let size = Private.size let node = Private.node type set = Set.t let is_empty = function | Private.Empty -> true | Private.Node _ -> false let root = function | Private.Empty -> None | Private.Node (_, x, d, _, _, _) -> Some (x, d) let rec max_key = function | Private.Empty -> None | Private.Node (_, k, _, Private.Empty, _, _) -> Some k | Private.Node (_, _, _, m, _, _) -> max_key m let balance left key data right = let height_left = height left in let height_right = height right in if height_left > height_right + 2 then ( match left with | Private.Empty -> raise (DeadCodeIsNotDead "SetMap line 828") (* height_left > height_right + 2 >= 2 *) | Private.Node (left0, key0, data0, right0, _, _) -> if height left0 >= height right0 then node left0 key0 data0 (node right0 key data right) else ( match right0 with | Private.Empty -> raise (DeadCodeIsNotDead "SetMap line 835") (* 0 <= height left0 < height right0 *) | Private.Node (left1, key1, data1, right1, _, _) -> node (node left0 key0 data0 left1) key1 data1 (node right1 key data right) ) ) else if height_right > height_left + 2 then ( match right with | Private.Empty -> raise (DeadCodeIsNotDead "SetMap line 844") (* height_right > height_left + 2 >= 2 *) | Private.Node (left0, key0, data0, right0, _, _) -> if height right0 >= height left0 then node (node left key data left0) key0 data0 right0 else ( match left0 with | Private.Empty -> raise (DeadCodeIsNotDead "SetMap line 851") (* 0 <= height right0 < height left0 *) | Private.Node (left1, key1, data1, right1, _, _) -> node (node left key data left1) key1 data1 (node right1 key0 data0 right0) ) ) else node left key data right let balance_with_logs warn parameters error left key data right = try error, balance left key data right with DeadCodeIsNotDead loc -> let error = warn parameters error "setMap.ml" (Some (loc ^ " Map invariant is broken, keep on with unbalanced map")) (Failure "Set_and_Map.Map.balance") in error, node left key data right let rec add key data = function | Private.Empty -> node empty key data empty | Private.Node (left, key_map, data_map, right, _, _) -> let cmp = Ord.compare key key_map in if cmp = 0 then node left key_map data right else if cmp < 0 then balance (add key data left) key_map data_map right else balance left key_map data_map (add key data right) let rec add_while_testing_freshness warn parameter error key data = function | Private.Empty -> error, true, node empty key data empty | Private.Node (left, key_map, data_map, right, _, _) -> let cmp = Ord.compare key key_map in if cmp = 0 then error, false, node left key_map data right else if cmp < 0 then ( let error, bool, left' = add_while_testing_freshness warn parameter error key data left in let error, map = balance_with_logs warn parameter error left' key_map data_map right in error, bool, map ) else ( let error, bool, right' = add_while_testing_freshness warn parameter error key data right in let error, map = balance_with_logs warn parameter error left key_map data_map right' in error, bool, map ) let add_with_logs warn parameter error key data map = let error, bool, map = add_while_testing_freshness warn parameter error key data map in if bool then error, map else ( let a, b, _, _ = __POS__ in ( warn parameter error "setMap.ml " (Some (a ^ " line: " ^ string_of_int b ^ ": Attempt to add an association over a former one in a map")) (Failure "Set_and_Map.Map.add"), map ) ) let rec extract_min_binding map key data map' = match map with | Private.Empty -> (key, data), map' | Private.Node (left2, key2, data2, right2, _, _) -> let min, left' = extract_min_binding left2 key2 data2 right2 in min, balance left' key data map' let rec extract_min_binding_with_logs warn parameters error map key data map' = match map with | Private.Empty -> error, ((key, data), map') | Private.Node (left2, key2, data2, right2, _, _) -> let error, (min, left') = extract_min_binding_with_logs warn parameters error left2 key2 data2 right2 in error, (min, balance left' key data map') let merge map1 map2 = match map1 with | Private.Empty -> map2 | Private.Node _ -> (match map2 with | Private.Empty -> map1 | Private.Node (left2, key2, data2, right2, _, _) -> let (key3, data3), left' = extract_min_binding left2 key2 data2 right2 in balance map1 key3 data3 left') let merge_with_logs warn parameters error map1 map2 = match map1 with | Private.Empty -> error, map2 | Private.Node _ -> (match map2 with | Private.Empty -> error, map1 | Private.Node (left2, key2, data2, right2, _, _) -> let error, ((key3, data3), left') = extract_min_binding_with_logs warn parameters error left2 key2 data2 right2 in balance_with_logs warn parameters error map1 key3 data3 left') let rec remove key = function | Private.Empty -> empty | Private.Node (left, key_map, data, right, _, _) -> let cmp = Ord.compare key key_map in if cmp = 0 then merge left right else if cmp < 0 then balance (remove key left) key_map data right else balance left key_map data (remove key right) let rec remove_while_testing_existence warn parameters error key map = match map with | Private.Empty -> error, false, empty | Private.Node (left, key_map, data, right, _, _) -> let cmp = Ord.compare key key_map in if cmp = 0 then ( let error, map = merge_with_logs warn parameters error left right in error, true, map ) else if cmp < 0 then ( let error, bool, left' = remove_while_testing_existence warn parameters error key left in if left' == left then ( let error = if bool then warn parameters error "setMap.ml" (Some "SetMap line 961") (failwith "Invariant is broken") else error in error, bool, map ) else ( let error, map = balance_with_logs warn parameters error left' key_map data right in error, bool, map ) ) else ( let error, bool, right' = remove_while_testing_existence warn parameters error key right in if right' == right then ( let error = if bool then warn parameters error "setMap.ml" (Some "SetMap line 978") (failwith "Invariant is broken") else error in error, bool, map ) else ( let error, map = balance_with_logs warn parameters error left key_map data right' in error, bool, map ) ) let remove_with_logs warn parameters error key map = let error, bool, map = remove_while_testing_existence warn parameters error key map in if bool then error, map else ( warn parameters error "setMap.ml" (Some ("SetMap line 994" ^ "Try to remove an association that is not defined in Map.remove" )) (failwith "Try to remove an association that is not defined in Map.remove"), map ) let rec pop x = function | Private.Empty as m -> None, m | Private.Node (l, v, d, r, _, _) as m -> let c = Ord.compare x v in if c = 0 then Some d, merge l r else if c < 0 then ( match pop x l with | (None as o), _ -> o, m | (Some _ as o), t -> o, balance t v d r ) else ( match pop x r with | (None as o), _ -> o, m | (Some _ as o), t -> o, balance l v d t ) let rec join left key value right = match balance left key value right with | Private.Empty -> raise (DeadCodeIsNotDead "SetMap line 1013") (* By case analysis *) | Private.Node (left2, key2, data2, right2, _, _) as map2 -> let h = height left2 - height right2 in if h > 2 || h < -2 then join left2 key2 data2 right2 else map2 let rec join_with_logs warn parameters error left key value right = match balance_with_logs warn parameters error left key value right with | error, Private.Empty -> let error = warn parameters error "setMap.ml" (Some "Map.join_with_logs, line 986, the output of balance should not \ be empty") (failwith "the output of balance should not be empty") in error, empty | error, (Private.Node (left2, key2, data2, right2, _, _) as map2) -> let h = height left2 - height right2 in if h > 2 || h < -2 then join_with_logs warn parameters error left2 key2 data2 right2 else error, map2 let rec split value = function | Private.Empty -> empty, None, empty | Private.Node (left1, key1, data1, right1, _, _) -> let cmp = Ord.compare value key1 in if cmp = 0 then left1, Some data1, right1 else if cmp < 0 then ( let left2, data2, right2 = split value left1 in let right2' = join right2 key1 data1 right1 in left2, data2, right2' ) else ( let left2, data2, right2 = split value right1 in let left2' = join left1 key1 data1 left2 in left2', data2, right2 ) let rec split_with_logs warn parameters error value map = match map with | Private.Empty -> error, (empty, None, empty) | Private.Node (left1, key1, data1, right1, _, _) -> let cmp = Ord.compare value key1 in if cmp = 0 then error, (left1, Some data1, right1) else if cmp < 0 then ( let error, (left2, data2, right2) = split_with_logs warn parameters error value left1 in let error, right2' = join_with_logs warn parameters error right2 key1 data1 right1 in error, (left2, data2, right2') ) else ( let error, (left2, data2, right2) = split_with_logs warn parameters error value right1 in let error, left2' = join_with_logs warn parameters error left1 key1 data1 left2 in error, (left2', data2, right2) ) let rec diff map1 map2 = match map1 with | Private.Empty -> empty, map2 | Private.Node (left1, key1, data1, right1, _, _) -> let left2, data2, right2 = split key1 map2 in let oleft1, oleft2 = diff left1 left2 in let oright1, oright2 = diff right1 right2 in (match data2 with | Some x when x = data1 -> merge oleft1 oright1, merge oleft2 oright2 | Some data2 -> join oleft1 key1 data1 oright1, join oleft2 key1 data2 oright2 | None -> join oleft1 key1 data1 oright1, merge oleft2 oright2) let rec union map1 map2 = match map1, map2 with | Private.Empty, _ -> map2 | _, Private.Empty -> map1 | ( Private.Node (left1, value1, data1, right1, height1, _), Private.Node (left2, value2, data2, right2, height2, _) ) -> if height1 >= height2 then ( let left2, op_data2, right2 = split value1 map2 in join (union left1 left2) value1 (match op_data2 with | None -> data1 | Some d2 -> d2) (union right1 right2) ) else ( let left1, op_data1, right1 = split value2 map1 in join (union left1 left2) value1 (match op_data1 with | None -> data2 | Some d1 -> d1) (union right1 right2) ) let rec union_with_logs warn parameters error map1 map2 = match map1, map2 with | Private.Empty, _ -> error, map2 | _, Private.Empty -> error, map1 | ( Private.Node (left1, value1, data1, right1, height1, _), Private.Node (left2, value2, data2, right2, height2, _) ) -> if height1 >= height2 then ( let error, (left2, op_data2, right2) = split_with_logs warn parameters error value1 map2 in let error, left' = union_with_logs warn parameters error left1 left2 in let error, right' = union_with_logs warn parameters error right1 right2 in join_with_logs warn parameters error left' value1 (match op_data2 with | None -> data1 | Some d2 -> d2) right' ) else ( let error, (left1, op_data1, right1) = split_with_logs warn parameters error value2 map1 in let error, left' = union_with_logs warn parameters error left1 left2 in let error, right' = union_with_logs warn parameters error right1 right2 in join_with_logs warn parameters error left' value1 (match op_data1 with | None -> data2 | Some d1 -> d1) right' ) let rec update map1 map2 = if map1 == map2 then map2 else ( match map1 with | Private.Empty -> map2 | Private.Node (left1, key1, data1, right1, _, _) -> let left2, data2, right2 = split key1 map2 in join (update left1 left2) key1 (match data2 with | None -> data1 | Some d2 -> d2) (update right1 right2) ) let rec update_with_logs warn parameters error map1 map2 = if map1 == map2 then error, map2 else ( match map1 with | Private.Empty -> error, map2 | Private.Node (left1, key1, data1, right1, _, _) -> let error, (left2, data2, right2) = split_with_logs warn parameters error key1 map2 in let error, left' = update_with_logs warn parameters error left1 left2 in let error, right' = update_with_logs warn parameters error right1 right2 in join_with_logs warn parameters error left' key1 (match data2 with | None -> data1 | Some d2 -> d2) right' ) let rec diff_pred pred map1 map2 = match map1 with | Private.Empty -> empty, map2 | Private.Node (left1, key1, data1, right1, _, _) -> let left2, data2, right2 = split key1 map2 in let oleft1, oleft2 = diff_pred pred left1 left2 in let oright1, oright2 = diff_pred pred right1 right2 in (match data2 with | Some x when pred x data1 -> merge oleft1 oright1, merge oleft2 oright2 | Some data2 -> join oleft1 key1 data1 oright1, join oleft2 key1 data2 oright2 | None -> join oleft1 key1 data1 oright1, merge oleft2 oright2) let rec min_elt = function | Private.Empty -> None | Private.Node (Private.Empty, key, data, _, _, _) -> Some (key, data) | Private.Node (left, _, _, _, _, _) -> min_elt left let rec find_option key = function | Private.Empty -> None | Private.Node (left, key_map, data, right, _, _) -> let cmp = Ord.compare key key_map in if cmp = 0 then Some data else find_option key (if cmp < 0 then left else right) let rec find_default d key = function | Private.Empty -> d | Private.Node (left, key_map, data, right, _, _) -> let cmp = Ord.compare key key_map in if cmp = 0 then data else find_default d key (if cmp < 0 then left else right) let rec find_option_with_logs warn parameter error key = function | Private.Empty -> let error = warn parameter error "setMap.ml" (Some "line 659") Not_found in error, None | Private.Node (left, key_map, data, right, _, _) -> let cmp = Ord.compare key key_map in if cmp = 0 then error, Some data else find_option_with_logs warn parameter error key (if cmp < 0 then left else right) let rec find_default_with_logs warn parameter error d key = function | Private.Empty -> let error = warn parameter error "setMap.ml" (Some "line 669") Not_found in error, d | Private.Node (left, key_map, data, right, _, _) -> let cmp = Ord.compare key key_map in if cmp = 0 then error, data else find_default_with_logs warn parameter error d key (if cmp < 0 then left else right) let rec mem key = function | Private.Empty -> false | Private.Node (left, key_map, _, right, _, _) -> let cmp = Ord.compare key key_map in cmp == 0 || if cmp > 0 then mem key right else mem key left let rec filter_one p = function | Private.Empty -> None | Private.Node (left, key, value, right, _, _) -> if p key value then Some (key, value) else ( match filter_one p left with | None -> filter_one p right | out -> out ) let rec iter f = function | Private.Empty -> () | Private.Node (left, key, data, right, _, _) -> let () = iter f left in let () = f key data in iter f right let rec fold f map value = match map with | Private.Empty -> value | Private.Node (left, key, data, right, _, _) -> fold f right (f key data (fold f left value)) let rec fold_with_interruption f map value = match map with | Private.Empty -> false, Stop.success value | Private.Node (left, key, data, right, _, _) -> let outputl = fold_with_interruption f left value in let interrupted, value = outputl in if interrupted then outputl else Stop.success_or_stop (fun value -> let val_opt = try Some (f key data value) with Sys.Break -> None in match val_opt with | None -> true, Stop.success value | Some v -> Stop.success_or_stop (fun v -> fold_with_interruption f right v) (fun v -> false, Stop.stop v) v) (fun x -> false, Stop.stop x) value let fold_with_interruption f map value = snd (fold_with_interruption f map value) let rec monadic_fold param err f map value = match map with | Private.Empty -> err, value | Private.Node (left, key, data, right, _, _) -> let err', value' = monadic_fold param err f left value in let err'', value'' = f param err' key data value' in monadic_fold param err'' f right value'' let rec monadic_fold2 parameters rh f g h map1 map2 res = match map1, map2 with | Private.Empty, Private.Empty -> rh, res | Private.Empty, _ -> monadic_fold parameters rh h map2 res | _, Private.Empty -> monadic_fold parameters rh g map1 res | Private.Node (left1, key1, data1, right1, _, _), _ -> let left2, data2, right2 = split key1 map2 in (match data2 with | None -> let rh', res' = monadic_fold2 parameters rh f g h left1 left2 res in let rh'', res'' = g parameters rh' key1 data1 res' in monadic_fold2 parameters rh'' f g h right1 right2 res'' | Some data2 -> let rh', res' = monadic_fold2 parameters rh f g h left1 left2 res in let rh'', res'' = f parameters rh' key1 data1 data2 res' in monadic_fold2 parameters rh'' f g h right1 right2 res'') let monadic_fold2_sparse parameters rh f map1 map2 res = let id _ x _ _ y = x, y in monadic_fold2 parameters rh f id id map1 map2 res let monadic_iter2_sparse parameters rh f map1 map2 = let error, () = monadic_fold2_sparse parameters rh (fun p e k a b () -> f p e k a b, ()) map1 map2 () in error let rec monadic_fold_restriction parameters rh f set map res = match set with | Set.Private.Empty -> rh, res | Set.Private.Node (left1, key1, right1, _, _) -> let left2, data2, right2 = split key1 map in (match data2 with | None -> let rh', res' = monadic_fold_restriction parameters rh f left1 left2 res in monadic_fold_restriction parameters rh' f right1 right2 res' | Some data2 -> let rh', res' = monadic_fold_restriction parameters rh f left1 left2 res in let rh'', res'' = f parameters rh' key1 data2 res' in monadic_fold_restriction parameters rh'' f right1 right2 res'') let rec mapi f = function | Private.Empty -> empty | Private.Node (left, key, data, right, _, _) -> node (mapi f left) key (f key data) (mapi f right) let map f s = mapi (fun _ x -> f x) s let rec map_with_logs warn parameters errors f map = match map with | Private.Empty -> errors, empty | Private.Node (left, key, data, right, _, _) -> let errors, left' = map_with_logs warn parameters errors f left in let errors, data' = f parameters errors data in let error, right' = map_with_logs warn parameters errors f right in error, node left' key data' right' let rec map2 f map map' = match map with | Private.Empty -> map' | Private.Node (left1, key1, data1, right1, _, _) -> let left2, data2, right2 = split key1 map' in join (map2 f left1 left2) key1 (match data2 with | None -> data1 | Some d2 -> f data1 d2) (map2 f right1 right2) let rec map2_with_logs warn parameters errors f g h map1 map2 = match map1 with | Private.Empty -> (match map2 with | Private.Empty -> errors, empty | Private.Node _ -> map_with_logs warn parameters errors g map2) | Private.Node (left1, key1, data1, right1, _, _) -> let errors, (left2, data2, right2) = split_with_logs warn parameters errors key1 map2 in let errors, left' = map2_with_logs warn parameters errors f g h left1 left2 in let error, right' = map2_with_logs warn parameters errors f g h right1 right2 in let error, data' = match data2 with | None -> f parameters error data1 | Some d2 -> h parameters errors data1 d2 in join_with_logs warn parameters error left' key1 data' right' let map2z_with_logs warn parameters errors = map2_with_logs warn parameters errors (fun parameters error a -> let error = warn parameters error "setMap.ml" (Some "line 1248, incompatible maps in map2z_safe") Not_found in error, a) (fun parameters error a -> let error = warn parameters error "setMap.ml" (Some "line 1251, incompatible maps in map2z_safe") Not_found in error, a) let rec fold2_with_logs warn parameters error f g h map1 map2 res = match map1, map2 with | Private.Empty, Private.Empty -> error, res | Private.Empty, _ -> monadic_fold parameters error g map2 res | _, Private.Empty -> monadic_fold parameters error f map1 res | Private.Node (left1, key1, data1, right1, _, _), _ -> let error, (left2, data2, right2) = split_with_logs warn parameters error key1 map2 in (match data2 with | None -> let error, res' = fold2_with_logs warn parameters error f g h left1 left2 res in let error, res'' = f parameters error key1 data1 res' in fold2_with_logs warn parameters error f g h right1 right2 res'' | Some data2 -> let error, res' = fold2_with_logs warn parameters error f g h left1 left2 res in let error, res'' = h parameters error (key1 : elt) data1 data2 res' in fold2_with_logs warn parameters error f g h right1 right2 res'') let fold2z_with_logs warn parameters error = fold2_with_logs warn parameters error (fun parameters error _ _ a -> let error = warn parameters error "setMap.ml" (Some "line 1248, incompatible maps in fold2z_safe") Not_found in error, a) (fun parameters error _ _ a -> let error = warn parameters error "setMap.ml" (Some "line 1251, incompatible maps in fold2z_safe") Not_found in error, a) let rec fold2_sparse_with_logs warn parameters error f map1 map2 res = match map1, map2 with | Private.Empty, _ | _, Private.Empty -> error, res | Private.Node (left1, key1, data1, right1, _, _), _ -> let error, (left2, data2, right2) = split_with_logs warn parameters error key1 map2 in (match data2 with | None -> let error, res' = fold2_sparse_with_logs warn parameters error f left1 left2 res in fold2_sparse_with_logs warn parameters error f right1 right2 res' | Some data2 -> let error, res' = fold2_sparse_with_logs warn parameters error f left1 left2 res in let error, res'' = f parameters error key1 data1 data2 res' in fold2_sparse_with_logs warn parameters error f right1 right2 res'') let iter2_sparse_with_logs warn parameters error f map1 map2 = let error, _ = fold2_sparse_with_logs warn parameters error (fun par err a b c _ -> f par err a b c, ()) map1 map2 () in error let rec for_all p = function | Private.Empty -> true | Private.Node (left, key, data, right, _, _) -> p key data && for_all p right && for_all p left type 'a enumeration = End | More of elt * 'a * 'a t * 'a enumeration let rec cons_enum m e = match m with | Private.Empty -> e | Private.Node (l, v, d, r, _, _) -> cons_enum l (More (v, d, r, e)) let compare cmp m1 m2 = let rec compare_aux e1 e2 = match e1, e2 with | End, End -> 0 | End, _ -> -1 | _, End -> 1 | More (v1, d1, r1, e1), More (v2, d2, r2, e2) -> let c = Ord.compare v1 v2 in if c <> 0 then c else ( let c = cmp d1 d2 in if c <> 0 then c else compare_aux (cons_enum r1 e1) (cons_enum r2 e2) ) in compare_aux (cons_enum m1 End) (cons_enum m2 End) let equal cmp m1 m2 = compare (fun x y -> if cmp x y then 0 else 1) m1 m2 == 0 let rec bindings_aux accu = function | Private.Empty -> accu | Private.Node (l, v, d, r, _, _) -> bindings_aux ((v, d) :: bindings_aux accu r) l let bindings s = bindings_aux [] s let rec aux_print pr f = function | Private.Empty -> () | Private.Node (Private.Empty, key, data, Private.Empty, _, _) -> Format.fprintf f "@[%a->@,%a@]" Ord.print key pr data | Private.Node (Private.Empty, key, data, right, _, _) -> Format.fprintf f "@[%a->%a@],@ %a" Ord.print key pr data (aux_print pr) right | Private.Node (left, key, data, Private.Empty, _, _) -> Format.fprintf f "%a,@ @[%a->%a@]" (aux_print pr) left Ord.print key pr data | Private.Node (left, key, data, right, _, _) -> Format.fprintf f "%a,@ @[%a->%a@],@ %a" (aux_print pr) left Ord.print key pr data (aux_print pr) right let print pr f = function | Private.Empty -> Format.pp_print_string f "\xE2\x88\x85" | Private.Node _ as m -> Format.fprintf f "@[{%a}@]" (aux_print pr) m let rec diff_with_logs warn parameters error map1 map2 = match map1 with | Private.Empty -> error, empty, map2 | Private.Node (left1, key1, data1, right1, _, _) -> let error, (left2, data2, right2) = split_with_logs warn parameters error key1 map2 in let error, oleft1, oleft2 = diff_with_logs warn parameters error left1 left2 in let error, oright1, oright2 = diff_with_logs warn parameters error right1 right2 in (match data2 with | Some x when x = data1 -> let error, o1 = merge_with_logs warn parameters error oleft1 oright1 in let error, o2 = merge_with_logs warn parameters error oleft2 oright2 in error, o1, o2 | Some data2 -> let error, o1 = join_with_logs warn parameters error oleft1 key1 data1 oright1 in let error, o2 = join_with_logs warn parameters error oleft2 key1 data2 oright2 in error, o1, o2 | None -> let error, o1 = join_with_logs warn parameters error oleft1 key1 data1 oright1 in let error, o2 = merge_with_logs warn parameters error oleft2 oright2 in error, o1, o2) let rec diff_pred_with_logs warn parameters error pred map1 map2 = match map1 with | Private.Empty -> error, empty, map2 | Private.Node (left1, key1, data1, right1, _, _) -> let error, (left2, data2, right2) = split_with_logs warn parameters error key1 map2 in let error, oleft1, oleft2 = diff_pred_with_logs warn parameters error pred left1 left2 in let error, oright1, oright2 = diff_pred_with_logs warn parameters error pred right1 right2 in (match data2 with | Some x when pred x data1 -> let error, o1 = merge_with_logs warn parameters error oleft1 oright1 in let error, o2 = merge_with_logs warn parameters error oleft2 oright2 in error, o1, o2 | Some data2 -> let error, o1 = join_with_logs warn parameters error oleft1 key1 data1 oright1 in let error, o2 = join_with_logs warn parameters error oleft2 key1 data2 oright2 in error, o1, o2 | None -> let error, o1 = join_with_logs warn parameters error oleft1 key1 data1 oright1 in let error, o2 = merge_with_logs warn parameters error oleft2 oright2 in error, o1, o2) let rec fold_restriction_with_missing_associations_with_logs warn parameters error f g set map res = match set, map with | Set.Private.Empty, _ -> error, res | Set.Private.Node (left1, key1, right1, _, _), _ -> let error, (left2, data2, right2) = split_with_logs warn parameters error key1 map in (match data2 with | None -> let error, res' = fold_restriction_with_missing_associations_with_logs warn parameters error f g left1 left2 res in let error, res'' = g key1 (error, res') in fold_restriction_with_missing_associations_with_logs warn parameters error f g right1 right2 res'' | Some data2 -> let error, res' = fold_restriction_with_missing_associations_with_logs warn parameters error f g left1 left2 res in let error, res'' = f key1 data2 (error, res') in fold_restriction_with_missing_associations_with_logs warn parameters error f g right1 right2 res'') let fold_restriction_with_logs warn parameters error f set map res = fold_restriction_with_missing_associations_with_logs warn parameters error f (fun _ x -> x) set map res let to_json ?(lab_key = "key") ?(lab_value = "value") = JsonUtil.of_map ~lab_key ~lab_value ~fold let of_json ?(lab_key = "key") ?(lab_value = "value") ?(error_msg = JsonUtil.build_msg "map") = JsonUtil.to_map ~lab_key ~lab_value ~error_msg ~add ~empty end end module type Projection = sig type elt_a type elt_b type 'a map_a type 'a map_b type set_a type set_b val proj_map : (elt_a -> elt_b) -> 'b -> ('b -> 'a -> 'b) -> 'a map_a -> 'b map_b val proj_map_monadic : 'parameters -> 'exceptions_caught_and_uncaught -> (elt_a -> elt_b) -> 'b -> ('parameters -> 'exceptions_caught_and_uncaught -> 'b -> 'a -> 'exceptions_caught_and_uncaught * 'b) -> 'a map_a -> 'exceptions_caught_and_uncaught * 'b map_b val proj_set : (elt_a -> elt_b) -> set_a -> set_b val proj_set_monadic : 'parameters -> 'exceptions_caught_and_uncaught -> ('parameters -> 'exceptions_caught_and_uncaught -> elt_a -> 'exceptions_caught_and_uncaught * elt_b) -> set_a -> 'exceptions_caught_and_uncaught * set_b val partition_set : (elt_a -> elt_b) -> set_a -> set_a map_b val partition_set_monadic : 'parameters -> 'exceptions_caught_and_uncaught -> ('parameters -> 'exceptions_caught_and_uncaught -> elt_a -> 'exceptions_caught_and_uncaught * elt_b) -> set_a -> 'exceptions_caught_and_uncaught * set_a map_b end module Proj (A : S) (B : S) = struct module MA = A.Map module MB = B.Map module SA = A.Set module SB = B.Set type elt_a = MA.elt type elt_b = MB.elt type set_a = SA.t type set_b = SB.t type 'a map_a = 'a MA.t type 'a map_b = 'a MB.t let proj_map f identity_elt merge map = MA.fold (fun key_a data_a map_b -> let key_b = f key_a in MB.add key_b (merge (MB.find_default identity_elt key_b map_b) data_a) map_b) map MB.empty let proj_map_monadic parameter handler f identity_elt monadic_merge map = MA.fold (fun key_a data_a (handler, map_b) -> let key_b = f key_a in let handler, data' = monadic_merge parameter handler (MB.find_default identity_elt key_b map_b) data_a in handler, MB.add key_b data' map_b) map (handler, MB.empty) let proj_set f set_a = SA.fold (fun key_a -> SB.add (f key_a)) set_a SB.empty let proj_set_monadic parameter handler f set_a = SA.fold (fun key_a (handler, set_b) -> let handler, key_b = f parameter handler key_a in handler, SB.add key_b set_b) set_a (handler, SB.empty) let partition_set f set_a = SA.fold (fun key_a map_b -> let key_b = f key_a in MB.add key_b (SA.add key_a (MB.find_default SA.empty key_b map_b)) map_b) set_a MB.empty let partition_set_monadic parameter handler f set_a = SA.fold (fun key_a (handler, map_b) -> let handler, key_b = f parameter handler key_a in ( handler, MB.add key_b (SA.add key_a (MB.find_default SA.empty key_b map_b)) map_b )) set_a (handler, MB.empty) end (* todo: add the following test to the sanity tests *) (* (* for instance, the following code: *) module IntMap = Make(struct type t = int let compare = compare end) module P = Proj(IntMap)(IntMap) let f = List.fold_left (fun map (a,b) -> IntMap.Map.add a b map) IntMap.Map.empty [1,[2;3];2,[3;4];5,[6;7];8,[12;13]] (* bad implementation (quadratic time complexity) *) let g = P.proj (fun i -> i mod 2) [] (List.append) f (* good implementation (linear time complexity)*) let g' = IntMap.Map.map List.rev (P.proj (fun i -> i mod 2) [] (fun x y -> List.append (List.rev y) x) f) let dump (s:string) f = let _ = Printf.fprintf stderr "%s: \n" s in let _ = IntMap.Map.iter (fun a l -> let _ = Printf.fprintf stderr " %i:" a in let _ = List.iter (Printf.fprintf stderr "%i,") l in let _ = Printf.fprintf stderr "\n" in ()) f in () let _ = dump "f" f let _ = dump "g" g let _ = dump "g'" g' (* should dump: *) (* f: 1:2,3, 2:3,4, 5:6,7, 8:12,13, g: 0:3,4,12,13, 1:2,3,6,7, *) *) module type Projection2 = sig type elt_a type elt_b type elt_c type 'a map_a type 'a map_b type 'a map_c val proj2 : (elt_a -> elt_b) -> (elt_a -> elt_c) -> 'b -> ('b -> 'a -> 'b) -> 'a map_a -> 'b map_c map_b val proj2_monadic : 'parameters -> 'exceptions_caught_and_uncaught -> (elt_a -> elt_b) -> (elt_a -> elt_c) -> 'b -> ('parameters -> 'exceptions_caught_and_uncaught -> 'b -> 'a -> 'exceptions_caught_and_uncaught * 'b) -> 'a map_a -> 'exceptions_caught_and_uncaught * 'b map_c map_b end module Proj2 (A : S) (B : S) (C : S) = struct module MA = A.Map module MB = B.Map module MC = C.Map type elt_a = MA.elt type elt_b = MB.elt type elt_c = MC.elt type 'a map_a = 'a MA.t type 'a map_b = 'a MB.t type 'a map_c = 'a MC.t let proj2 f g identity_elt merge map = MA.fold (fun key_a data_a map_b -> let key_b = f key_a in let key_c = g key_a in let submap = MB.find_default MC.empty key_b map_b in let submap = MC.add key_c (merge (MC.find_default identity_elt key_c submap) data_a) submap in MB.add key_b submap map_b) map MB.empty let proj2_monadic parameter handler f g identity_elt merge map = MA.fold (fun key_a data_a (handler, map_b) -> let key_b = f key_a in let key_c = g key_a in let submap = MB.find_default MC.empty key_b map_b in let handler, data' = merge parameter handler (MC.find_default identity_elt key_c submap) data_a in let submap = MC.add key_c data' submap in handler, MB.add key_b submap map_b) map (handler, MB.empty) end
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>