basis.anubis 53.2 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506

   *Project*                             The Anubis Project
   
   *Title*                             Some basic definitions.
    
   *Copyright*                     Copyright (c) Alain Prouté 2001-2007. 


   *Author*       Alain Prouté
   *Author*       Olivier Duvernois
   *Author*       David René

   
   
   ----------------------------------- Table of Content ----------------------------------
   
   *** (1) Boolean functions
   
   *** (2) Int32 functions
      *** (2.1) Absolute value
      *** (2.2) Maximum
      *** (2.3) Minimum
      *** (2.4) Sum
      *** (2.5) Greatest
      *** (2.6) An alternative division which reports */0 = 0. 
      *** (2.7) Euclidian division
      *** (2.8) Raising an integer to an integer power. 
      *** (2.9) (Bool) int32_less
   
   *** (3) String functions
      *** (3.1) String + Int32
      *** (3.2) String + Word8
      *** (3.3) Bool -> String
      *** (3.4) a variant of the primitive 'sub_string'. 
      *** (3.5) '<' as another name for 'string_less'. 
      *** (3.6) Test if is an empty string
   
   *** (4) Generic functions on lists
      *** (4.1) Find element(s)
         *** (4.1.1) as nth from a list
         *** (4.1.2) Get a List($A) from a List($B)
         *** (4.1.3) Getting an element of List($T)
         *** (4.1.4) Getting elements of List($T)
      *** (4.2) member
         *** (4.2.1) 'member' with a non standard notion of equality 
         *** (4.2.2) 'member' for characters in strings. 
         *** (4.2.3) Lenght
      *** (4.3) A syntactical variant for 'append'
      *** (4.4) Merging two lists
         *** (4.4.1) with standrad comparison
         *** (4.4.2) with a non standard comparison:
      *** (4.5) Quick sorting of lists.
      *** (4.6) Randomly shuffling a list. 
         *** (4.6.1) Basic Shuffling
         *** (4.6.2) Shuffling a non empty list. 
      *** (4.7) Concatenating the elements of a list of strings.
      *** (4.8) Mapping functions
         *** (4.8.1) 'map' for non empty lists. 
         *** (4.8.2) 'map' with a parameter:
         *** (4.8.3) 'map and append'
         *** (4.8.4) 'map and select'. 
         *** (4.8.5) 'map and iterate'.
         *** (4.8.6) 'map select and iterate'. 
      *** (4.9) Check that all elements of a list have a given property:
      *** (4.10) Replace element in a list by its new_value
      *** (4.12) Getting the last element in a list. 
      *** (4.13) Transforming a non empty list into a list. 
      *** (4.14) List without doubles
   
   *** (5) Function facilities.
      *** (5.1) A generic 'identity' function.
      *** (5.2) Writing 'x..f' instead of 'f(x)'.  
   
   *** (6) Printable trees. 
      *** (6.1) Definition
      *** (6.2) Length
      *** (6.3) Reverse
      *** (6.4) 'member' for printable trees:
      *** (6.5) Printing a printable tree to the standard output. 
      *** (6.6) Thousands separators
   
   *** (7) A variant of 'write_to_file' able to write a printable tree. 
   
   *** (8) Putting a virtual machine at bed. 
   
   *** (9) Reliably  sending data
   
   *** (10) Transmitting serializable data. 
   
   *** (11) Time
      *** (11.1) Time conversions. 
      *** (11.2) Calculations with UTime.
   
   *** (12) Files and directories
      *** (12.1) Get the size of a file knowing its name.
      *** (12.2)  A predefined 'default' directory mode. 
      *** (12.3) Making a chain of directories. 
      *** (12.4) Make a directory with default mode
      *** (12.5) Testing if a file exists. 
      *** (12.6) Searching directories and subdirectories recursively. 
   
   *** (13) Manipulating characters
      *** (13.1)  Putting a character to lower case. 
      *** (13.2) Putting a string to lower case. 
      *** (13.3) 'crlf'
   
   *** (14) Prompting and reading from standard input. 
      *** (14.1) Reading a line from the standard input. 
      *** (14.2) Printing a prompt and reading the answer. 
      *** (14.3) The same one for a password
   
   *** (15) IP address
   
   *** (16) Graphical tools
      *** (16.1) lighten
      *** (16.2) darken
      *** (16.3) System font names
   
   ---------------------------------------------------------------------------------------

   
   *** (1) Boolean functions
   
public define Bool
  $T x /= $T y
   =
  if x = y then false else true. 
   
public define Bool 
  not
    (
      Bool x
    ) =
  if x then false else true. 

public define Bool 
  ~ Bool x
    =
  if x then false else true. 

public define $T -> Bool
   not
     (
       $T -> Bool f
     ) =
   ($T t) |-> not(f(t)). 
   
public define Bool
  xor
    (
      Bool x,
      Bool y
    ) =
   if x = y then false else true. 
   

public define Bool
   Bool x  | Bool y = 
  if x then true else y. 

public define $T -> Bool
   $T -> Bool f | $T -> Bool g 
     =
   ($T x) |-> if f(x) then true else g(x). 
   
public define $T -> Bool
   $T -> Bool f & $T -> Bool g 
     =
   ($T x) |-> if f(x) then g(x) else false. 
   


   *** (2) Int32 functions

      *** (2.1) Absolute value
      
public define Int32
   abs
     (
      Int32  x
     ) =
   if x < 0 then (x * (0 - 1)) else x.
   

      *** (2.2) Maximum
   
public define Int32
  max
    (
      Int32 x, 
      Int32 y
    ) =
  if x < y then y else x. 

   
   max and min for lists of integers. 
   
define Int32
   max             // auxilliary function 
     (
       Int32 x, 
       List(Int32) l
     ) =
   if l is 
     {
       [ ] then x,
       [h . t] then 
         if x < h
         then max(h,t)
         else max(x,t)
     }.
   
public define Int32
   max
     (
       NonEmptyList(Int32) l
     ) =
   if l is [h . t] then max(h,t). 

   
   
      *** (2.3) Minimum
   
   
public define Int32
  min
    (
      Int32 x, 
      Int32 y
    ) =
  if x < y then x else y. 

   
define Int32
   min             // auxilliary function 
     (
       Int32 x, 
       List(Int32) l
     ) =
   if l is 
     {
       [ ] then x,
       [h . t] then 
         if x < h
         then max(x,t)
         else max(h,t)
     }.
   
public define Int32
   min
     (
       NonEmptyList(Int32) l
     ) =
   if l is [h . t] then min(h,t). 
   

   
      *** (2.4) Sum
   
public define Int32
   sum
     (
       List(Int32) l
     ) =
   if l is 
     {
       [ ] then 0, 
       [h . t] then 
         h + sum(t)
     }.

   
      *** (2.5) Greatest
     
public define Int32
   greatest
     (
       List(Int32) l,
       Int32       i
     ) =
   if l is 
     {
       []      then i,
       [h . t] then 
         if i > h then greatest(t,i)
                  else greatest(t,h)
     }.

   
      *** (2.6) An alternative division which reports */0 = 0. 
   
public define Int32 
  Int32 d / Int32 a =
    if (Maybe(Int32))(d/a) is 
      { 
        failure then 0, 
        success(q) then q 
      }. 


public define Int32
   Int32 x % Int32 y
     =
   x(mod y). 
   
   
      *** (2.7) Euclidian division
   
   
   An  Euclidian division (with  positive remainder  as dictated  by the  theory). Indeed,
   mathematically, we must have:
   
         a = q*b + r
         0 =< r < |b|
   
public define (Int32,        // quotient
               Int32)        // remainder
   euclid
     (
       Int32 a,       // divide 'a' by 'b'
       Int32 b 
     ) =
   if (Maybe(Int32))(a/b) is 
     {
       failure then (0,0),
       success(q) then with r = a-q*b, 
         if r >= 0
         then (q,r)
         else (q-1,b+r)      // a = (q-1)b + (b+r)
     }.
   

   
   
      *** (2.8) Raising an integer to an integer power. 
   
public define Int32
   Int32 x ^ Int32 y
     = 
   if y = 0 then 1 else            // x^0 = 1
   if y = 1 then x else            // x^1 = x
   if y < 0 then alert else        // works for positive exponents only
   with z = x^(y>>1),              // z = x^p where x = 2p or x = 2p+1
   if ((y&1) = 0)                  // if y even
   then z*z                        // x^(2p)   = (x^p)*(x^p)
   else z*z*x.                     // x^(2p+1) = (x^p)*(x^p)*x
   
public define Int32 
  force_to_Int32
  (
    String value
  )=
  if string_to_integer(value) is
  {
    failure     then  0,
    success(n)  then n
  }.
  
      *** (2.9) (Bool) int32_less

public define Bool
   int32_less
     (
       Int32 i1,
       Int32 i2
     ) =
   i1 < i2.
   
   
   
   *** (3) String functions

      *** (3.1) String + Int32

public define String
   String s + Int32 i =
      s + integer_to_string(i). 

public define String
   Int32 i + String s =
      integer_to_string(i) + s. 

      *** (3.2) String + Word8

public define String
   String s + Word8 i =
      s + integer_to_string(word8_to_int32(i)). 

public define String
   Word8 i + String s =
      integer_to_string(word8_to_int32(i)) + s. 

      


   

    
      *** (3.4) a variant of the primitive 'sub_string'. 
   
public define String
  substr
    (
      String s, 
      Int32 start, 
      Int32 length
    ) =
  if sub_string(s,start,length) is 
    {
      failure then "",
      success(t) then t
    }. 
   
   
   
      *** (3.5) '<' as another name for 'string_less'. 
   
public define Bool
   String x < String y
     =
   string_less(x,y). 

   
   
      *** (3.6) Test if is an empty string

    empty means no characters or only spaces
   
define Bool
   is_empty
     (
       List(Word8) l
     ) =
   if l is 
     {
       []      then true,
       [h . t] then if h = ' ' then is_empty(t) else false,
     }.
   
public define Bool
   is_empty
     (
       String  s
     ) =
   is_empty(explode(s)).   

   
   
   
   
   
   *** (4) Generic functions on lists
   

      *** (4.1) Find element(s)

         *** (4.1.1) as nth from a list
   
public define Maybe($T)
  nth
    (
      Int32 n,
      List($T) l
    ) =
  if l is 
    {
      [ ] then failure, 
      [h . t] then 
        if n < 0 then failure else
        if n = 0 then success(h) else
        nth(n-1,t)
    }.
   

public define List($T)
  nthcdr
    (
      Int32 n, 
      List($T) l
    ) =
  if n =< 0 then l else
  if l is 
    {
      [ ] then [ ], 
      [h . t] then nthcdr(n-1,t)
    }.
   

public define $T
   force_nth
     (
       Int32 n, 
       List($T) l
     ) =
   if l is 
     {
       [ ] then alert, 
       [h . t] then 
         if n = 0 
         then h
         else force_nth(n-1,t)
     }. 

   
         *** (4.1.2) Get a List($A) from a List($B)

public define List($A)
   find_elements
     (
       List($B)             l,
       $B -> Bool        test,
       $B -> List($A)    to_a
     ) =
   if l is 
     {
       []       then [],
       [h . t]  then 
         if test(h)
           then append(to_a(h),find_elements(t,test,to_a))
           else find_elements(t,test,to_a)
     }.
   
public define List($A)
   find_elements
     (
       List($B)       l,
       $B -> Bool  test,
       $B -> $A    to_a
     ) =
   if l is 
     {
       []       then [],
       [h . t]  then 
         if test(h)
           then [to_a(h) . find_elements(t,test,to_a)]
           else find_elements(t,test,to_a)
     }.
   
   
public define List($A)
   find_elements
     (
       List($B)       l,
       $B -> $A    to_a
     ) =
   if l is 
     {
       []       then [],
       [h . t]  then [to_a(h) . find_elements(t,to_a)]
     }.
   
   
         *** (4.1.3) Getting an element from List($T)
   
public define Maybe($T)
   find_element
     (
       List($T)       l,
       $T -> Bool  test
     ) =
   if l is 
     {
       []       then failure,
       [h . t]  then if test(h) then success(h) else find_element(t,test)
     }.
   
   
         *** (4.1.4) Getting elements from List($T)
   
public define List($T)
   find_elements
     (
       List($T)       l,
       $T -> Bool  test
     ) =
   if l is 
     {
       []       then [],
       [h . t]  then if test(h) then [h . find_elements(t,test)] else find_elements(t,test)
     }.
   
   
   
   
      *** (4.2) member

   
         *** (4.2.1) 'member' with a non standard notion of equality 

public define Bool
  member
    (
      List($T) l, 
      $T       x, 
      ($T,$T) -> Bool compare
    ) = 
  if l is 
    {
      [ ] then false, 
      [h . t] then 
        if compare(h,x)
        then true
        else member(t,x,compare)
    }.


 
         *** (4.2.2) 'member' for characters in strings. 
   
define Bool
  member
    (
      Word8 c, 
      String s,
      Int32 n   
    ) =
  if nth(n,s) is 
    {
      failure then false, 
      success(d) then 
        if d = c
        then true
        else member(c,s,n+1)
    }.
   
public define Bool
  member
    (
      Word8 c, 
      String s
    ) =
  member(c,s,0). 
   

   
   A syntactical variant  for member, using the sign ':'  (remind of 'membership' relation
   in set theory).  'a:b' means that 'a' belongs to the list 'b'.
   
public define Bool
   $T a : List($T) b
     =
   member(b,a). 
   
   
   
   
   

         *** (4.2.3) Length
   
define Int32 
  length
    (
      List($T) l,
      Int32 n
    ) = 
  if l is 
    {
      [ ] then n, 
      [h . t] then length(t,n+1)
    }. 
  
public define Int32 
  length
    (
      List($T) l
    ) =
  length(l,0).

   
   
   
   Compute the average of a  list of integers. The average of a1,...,an is:
   
                 a1 + ... + an
            m =  -------------
                       n
   
   We  don't  want to  compute  a1+...+an  because of  the  limited  capacity  of 32  bits
   integers. Hence we compute as follows:
   
            m = a1/n + (a2+...+an)/n
              = a1/n + ((n-1)/n)*b
              = a1/n + (1 - 1/n)*b
              = (a1 - b)/n + b
   
   where b is the average (a2+...+an)/(n-1). 
   
define Int32
   average
     (
       List(Int32)   l,
       Int32         n     // length of l
     ) =
   if n < 10 then sum(l)/n else 
   if l is 
     {
       [ ]     then 0, 
       [h . t] then // here n is not 0
         with b = average(t,n-1),
           ((h-b)/n) + b 
     }.
   
public define Int32
   average
     (
       List(Int32)   l
     ) =
   average(l,length(l)). 
   

   

   
      *** (4.3) A syntactical variant for 'append'
   
   Given two lists L1 and L2, we would  like to write 'L1 + L2' instead of 'append(L1,L2)'
   (defined in 'predefined.anubis').   This is particularly useful when  we want to append
   more than two lists.
   
public define List($T)
  List($T) l1 + List($T) l2 
    =
  append(l1,l2). 
   
   Remark than since  '+' associates to the right, 'L1 +  L2 + L3' is read as  'L1 + (L2 +
   L3)'. Hence, L2 is first appended to  L3. Since, appending requires a copy of the first
   list, this is more efficient than if '+'  was associating to the left. In this case, L1
   is copied one time, not two times.
   
   
   Difference of two lists. 
   
public define List($T)
   List($T) a - List($T) b
     =
   if a is 
     {
       [ ] then [ ], 
       [h . t] then 
         if member(b,h)
         then t - b 
         else [h . t - b]
     }.
   
   The same one but with a given test of equality:
   
define List($T)
   substract
     (
       List($T)             a, 
       List($T)             b, 
       ($T,$T) ->  Bool     compare
     ) =
   if a is 
     {
       [ ] then [ ], 
       [h . t] then 
         if member(b,h,compare)
         then substract(t,b,compare)
         else [h . substract(t,b,compare)]
     }.

   
   A more convenient interface for this substraction:
   
public define List($T)
   List($T) a - (($T,$T) -> Bool, List($T)) b
     =
   if b is (compare,c) then 
   substract(a,c,compare). 
   
   
   
   

      *** (4.4) Merging two lists
   
         *** (4.4.1) with standrad comparison

   This is quite the same thing as  appending two lists, except that elements which are in
   both lists are put only one time in the result. Order of elements is not respected.

public define List($T)
  merge
    (
      List($T) l1, 
      List($T) l2
    ) =
  if l1 is 
    {
      [ ] then l2,
      [h . t] then 
        if member(l2,h)
        then merge(t,l2)
        else merge(t,[h . l2])
    }. 

   
   
         *** (4.4.2) with a non standard comparison:

public define List($T)
  merge
    (
      List($T) l1, 
      List($T) l2,
      ($T,$T) -> Bool compare
    ) =
  if l1 is 
    {
      [ ] then l2,
      [h . t] then 
        if member(l2,h,compare)
        then merge(t,l2)
        else merge(t,[h . l2])
    }. 


   
      *** (4.5) Quick sorting of lists.

define (List($H),List($H))
  separ
    (
      $H pivot,
      List($H) l, 
      ($H,$H) -> Bool less
    ) =
  if l is
    {
      [ ] then 
        ([ ],[ ]), 

      [h . t] then 
        with p = separ(pivot,t,less), 
          if p is
            {
              (f,s) then
                if less(h,pivot) is
                  {
                    false then
                      (f,[h . s]),
                    true then
                      ([h . f],s)
                  }
            }
    }.      

public define List($I) 
  qsort_aux
    (
      List($I) l,
      ($I, $I) -> Bool less
    ) = 
  if l is
    {
      [ ] then
        [ ],

      [h . t] then 
        with p = separ(h,t,less), 
          if p is (f,s) then
            append(qsort_aux(f,less),[h . qsort_aux(s,less)])                   
    }.


define Bool
   already_sorted
     (
       List($I) l, 
      ($I, $I) -> Bool less
     ) =
   if l is 
     {
       [ ] then true, 
       [h . t] then 
         if already_sorted(t,less)
         then if t is 
           {
             [ ] then true, 
             [u . v] then less(h,u)
           }
         else false
     }.
   
   
public define List($I) 
  qsort
    (
      List($I) l,
      ($I, $I) -> Bool less
    ) = 
  if already_sorted(l,less)
  then l
  else qsort_aux(l,less). 
   

   
   
      *** (4.6) Randomly shuffling a list. 

         *** (4.6.1) Basic Shuffling
   
define List($T)
  insert_at_position
    (
      Int32 pos, 
      $T h, 
      List($T) t
    ) =
  if pos = 0
  then [h . t]
  else if t is 
     {
       [ ] then [h], 
       [a . b] then 
         [a . insert_at_position(pos-1,h,b)]
     }.
   
define List($T)
  random_insert
    (
      $T h,
      List($T) t
    ) =
  with n = length(t), 
       r = random(n+1), 
    insert_at_position(r,h,t). 
   
public define List($T)
  shuffle
    (
      List($T) l
    ) =
  if l is
    {
      [ ] then [ ],
      [h . t] then random_insert(h,shuffle(t))
    }.
    
   
         *** (4.6.2) Shuffling a non empty list. 
   
   
public define NonEmptyList($T)
  shuffle
    (
      NonEmptyList($T) l
    ) =
  if l is [h . t] then 
  if shuffle((List($T))[h . t]) is 
    {
      [ ] then alert, 
      [h2 . t2] then [h2 . t2]
    }. 
   


      *** (4.7) Concatenating the elements of a list of strings.
   
public define String
   concat
     (
       List(String) l
     ) =
   if l is 
     {
       [ ] then "",
       [h . t] then h+concat(t)
     }. 

   with a separator between 2 strings : 
   
public define String
   concat
     (
       List(String) l,
       String       separator
     ) =
   if l is 
     {
       [ ]     then "",
       [h . t] then h+if t is [] then "" else (separator+concat(t,separator))
     }. 
   
   
   
      *** (4.8) Mapping functions
   
   
public define One
   map_forget
     (
       $A -> $B f, 
       List($A) l
     ) =
   if l is
     {
       [ ] then unique, 
       [h . t] then 
         forget(f(h)); map_forget(f,t)
     }.
   
   
         *** (4.8.1) 'map' for non empty lists. 
   
public define NonEmptyList($U)
   map
     (
       $T -> $U          f, 
       NonEmptyList($T)  l
     ) =
   if l is [h . t] then [f(h) . map(f,t)]. 
   
   
   
         *** (4.8.2) 'map' with a parameter:

public define List($C)
  map
    (
      ($A,$B) -> $C   f, 
      $A              p,
      List($B)        l
    ) =
  if l is 
    {
      [ ] then [ ],
      [h . t] then 
        with x = f(p,h), 
        [x . map(f,p,t)]
    }. 

   
   
         *** (4.8.3) 'map and append'
   
public define List($T)
   map_append
     (
       $A -> List($T)  f,
       List($A)        l
     ) =
   if l is 
     {
       []       then [],
       [h . t]  then append(f(h),map_append(f,t))
     }.

   
   
         *** (4.8.4) 'map and select'. 
   
public define List($B)
   map_select
     (
       $A -> Maybe($B)    f, 
       List($A)           l
     ) =
   if l is 
     {
       [ ] then [ ], 
       [h . t] then 
         if f(h) is 
           {
             failure      then map_select(f,t),
             success(x)   then [x . map_select(f,t)]
           }
     }.
   
   
   
         *** (4.8.5) 'map and iterate'.
   
public define List($B)
   map_iterate
     (
       ($T,$A) -> $B      f, 
       List($A)           l,
       $T                 init,
       $T -> $T           next
     ) =
   if l is
     {
       [ ] then [ ], 
       [h . t] then 
         with x = f(init,h), 
         [x . map_iterate(f,t,next(init),next)]
     }. 
   
   
   
   
         *** (4.8.6) 'map select and iterate'. 
   
   
public define List($B)
   map_select_iterate
     (
       ($T,$A) -> Maybe($B)      f, 
       List($A)                  l,
       $T                        init,
       $T -> $T                  next
     ) =
   if l is
     {
       [ ] then [ ], 
       [h . t] then 
         if f(init,h) is 
           {
             failure     then 
               map_select_iterate(f,t,init,next),
             success(x)  then 
               [x . map_select_iterate(f,t,next(init),next)]
           }
     }. 
   
   
   
   

      *** (4.9) Check that all elements of a list have a given property:
   
       mapand(test,l) is true if and only if test(x) is true for all elements x of l. 

public define Bool
   mapand
     (
       $T -> Bool   test, 
       List($T)     l
     ) =
   if l is 
     {
       [ ] then true, 
       [h . t] then 
         if test(h)
         then mapand(test,t)
         else false
     }.
     
   
public define Bool
   mapor
     (
       $T -> Bool    test, 
       List($T)      l
     ) =
   if l is 
     {
       [ ] then false, 
       [h . t] then 
         if test(h) 
         then true
         else mapor(test,t)
     }. 

   
      *** (4.10) Replace element in a list by its new_value

public define List($T)
   replace_element
     (
       List($T)             l,
       $T -> Bool        test,
       $T           new_value
     ) =
   if l is 
     {
       []       then [],
       [h . t]  then 
          if test(h)
            then [new_value . t]
            else [h . replace_element(t,test,new_value)]
     }.
   
public define List($T)
   replace_element
     (
       List($T)             l,
       $T -> Bool        test,
       $T -> $T     new_value
     ) =
   if l is 
     {
       []       then [],
       [h . t]  then 
          if test(h)
            then [new_value(h) . t]
            else [h . replace_element(t,test,new_value)]
     }.
   
   
  
 
   
   
         *** (4.12) Getting the last element in a list. 
   
public define Maybe($T)
   last
     (
       List($T) l
     ) =
   if l is 
     {
       [ ] then failure, 
       [h . t] then if t is 
         {
           [ ] then success(h), 
           [_ . _] then last(t)
         }
     }.
   
define List($T)
  remove_last
  (
    List($T)  l,
    List($T)  so_far
  )=
  if l is
  {
    [] then [],
    [h. t] then if t is
      {
        []  then reverse(so_far),
        [_ . _] then remove_last(t, [h . so_far])
      }
  }
  .
  
public define List($T)
   remove_last
     (
       List($T) l
     ) =
   remove_last(l, []). 
   
   

      *** (4.13) Transforming a non empty list into a list. 
   
public define List($T)
   to_list
     (
       NonEmptyList($T) nel
     ) =
   if nel is [h . t] then [h . t]. 
   

   
      *** (4.14) List without doubles
   
public define List($T)
   no_doubles
     (
       List($T)          l,
       ($T,$T) -> Bool   is_equal
     ) =
   if l is 
     {
       []      then [],
       [h . t] then if member(t,h,is_equal)
                      then no_doubles(t,is_equal)
                      else [h . no_doubles(t,is_equal)]
     }.
   
   
   
   *** (5) Function facilities. 
   
      *** (5.1) A generic 'identity' function 
   
public define $T
   identity
     (
       $T x
     ) =
   x. 
   
   
      *** (5.2) Writing 'x.f' instead of 'f(x)'.

   The next definition enables to write 'x.f' instead of 'f(x)' for applying a function to
   its argument (works for functions of 1 argument).  In particular, if 'f' is an implicit
   destructor (which  has always 1 argument) of  some type, this makes  the application of
   this  destructor look  like  the C  syntax 'x.f'  for  extracting a  component from  an
   agglomeration (analogous to a field from a structure).
      
   The binary operator  '.' has been introduced 2006/11/09 in  version 1.7.10. Because the
   fact that paragraphs must end by a dot (necessarily followed by either a white space, a
   line feed, a carriage return or a tabulator), this binary operator needs its right hand
   operand  glued onto  it.   So 'x. f'  will  not  be  correct, because  the  dot may  be
   understood as an end of paragraph.
   
   
public define $U
   $T x .$T -> $U f
     =
   f(x). 
   
   
   
   
   
   *** (6) Printable trees. 

      *** (6.1) Definition
   
   Trees whose leaves are strings, integers, ...

public type Printable_tree:
  [ ],   
  str_pt   (String,Printable_tree),     
  ba_pt    (ByteArray,Printable_tree), 
  int8_pt  (Word8,Printable_tree), 
  int32_pt (Int32,Printable_tree),
  pt_pt    (Printable_tree,Printable_tree).  

   
   Convenience functions, so that printable trees may be manipulated like lists. 
   
public define Printable_tree [String         x . Printable_tree y] = str_pt(x,y).
public define Printable_tree [ByteArray      x . Printable_tree y] = ba_pt(x,y).
public define Printable_tree [Word8           x . Printable_tree y] = int8_pt(x,y).
public define Printable_tree [Int32          x . Printable_tree y] = int32_pt(x,y).
public define Printable_tree [Printable_tree x . Printable_tree y] = pt_pt(x,y).
   
      *** (6.2) Length

public define Int32
   length
     (
       Printable_tree tr
     ) =
   if tr is 
     {
       [ ] then 0, 
       str_pt(s,t)      then length(s) + length(t), 
       ba_pt(b,t)       then length(b) + length(t),
       int8_pt(n,t)     then length(integer_to_string(word8_to_int32(n)))+length(t),
       int32_pt(n,t)    then length(integer_to_string(n))+length(t),
       pt_pt(u,v)       then length(u)+length(v)
     }.
   

      *** (6.3) Reverse
   
public define Printable_tree
  reverse
    (
      Printable_tree tr
    ) =
  if tr is 
    {
      [ ]               then (Printable_tree) [ ], 
      str_pt(s,t)       then (Printable_tree) [t , s], 
      ba_pt(b,t)        then (Printable_tree) [t , b], 
      int8_pt(i,t)      then (Printable_tree) [t , i], 
      int32_pt(n,t)     then (Printable_tree) [t , n], 
      pt_pt(u,v)        then (Printable_tree) [v . u]
    }. 
   
   
      *** (6.4) 'member' for printable trees:
   
   
public define Bool
  member
    (
      Word8 c, 
      Printable_tree tr
    ) = 
  if tr is 
    {
      [ ]                 then false, 
      str_pt(s,t)         then if member(c,s) then true else member(c,t),
      ba_pt(b,t)          then member(c,t), 
      int8_pt(d,t)        then if c = d then true else member(c,t), 
      int32_pt(n,t)       then member(c,t), 
      pt_pt(u,v)          then if member(c,u) then true else member(c,v)
    }. 
   
   
   
public define Printable_tree
   flat
     (
       List(Printable_tree) l
     ) =
   if l is 
     {
       [ ] then [ ], 
       [h . t] then [h . flat(t)]
     }. 
   
   
public define List($T)
   flat
     (
       List(List($T)) l
     ) =
   if l is 
     {
       [ ] then [ ], 
       [h . t] then h + flat(t)
     }.
   

   Dividing  a list  into a  list  of lists  of the  same  length (except  maybe the  last
   one). For example,
   
     [1,2,3,4,5,6,7,8,9]/4       and        [1,2,3,4,5,6,7,8,9]/3
   
   will give respectively:
   
     [[1,2,3,4],                            [[1,2,3],
      [5,6,7,8],                             [4,5,6],
      [9]]                                   [7,8,9]]
   
public define List(List($T))
   List($T) list / Int32 d          // d is supposed to be at least 1
     =
   if d < 1 then alert else
   with div = ((List($T) l, Int32 a) |-div->  // auxiliary recursive function 
     if l is 
       {
         [ ] then [ ],
         [h . t] then 
           if a = 1
           then [[h] . div(t,d)]
           else if (List(List($T)))div(t,a-1) is 
             {
               [ ]     then [[h]],
               [u . v] then [[h . u] . v]
             }
       }),div(list,d). 
   
   
   
   
   
   
      *** (6.5) Printing a printable tree to the standard output. 

public define Bool 
  print
    (
      Printable_tree tr
    ) =
  if tr is
    {
      [ ] then true, 
      str_pt(s,u) then if print_string(s) is
        { false then false, true then print(u) },
      ba_pt(b,u)     then if print_string(to_string(b)) is 
        { false then false, true then print(u) }, 
      int8_pt(a,u) then if print_int32(word8_to_int32(a)) is
        { false then false, true then print(u) }, 
      int32_pt(n,u) then if print_int32(n) is
        { false then false, true then print(u) }, 
      pt_pt(u,v) then if print(u) is
        { false then false, true then print(v) }
    }.



public define Bool
  print
    (
      WStream file, 
      Word8 c
    ) =
  if file <- c is 
    {
      failure then false, 
      success(_) then true
    }. 

public define Bool
  print
    (
      WStream file, 
      List(Word8) l
    ) =
  if l is 
    {
      [ ] then true, 
      [h . t] then
        if file <- h is 
         { 
           failure then false, 
           success(_) then print(file,t)
         }
    }. 

public define Bool
  print
    (
      WStream file, 
      String s
    ) =
  with l = (List(Word8))explode(s), print(file,l). 
  


public define Bool
  print
    (
      WStream file, 
      Int32 n
    ) = 
  print(file,integer_to_string(n)). 

public define Bool 
  print
    (
      WStream file, 
      Printable_tree t
    ) =
  if t is
    {
      [ ] then true, 
      str_pt(s,u) then if (Bool)print(file,s) is
        { false then false, true then (Bool)print(file,u) },
      ba_pt(b,u)     then alert, 
      int8_pt(a,u) then if (Bool)print(file,word8_to_int32(a)) is
        { false then false, true then (Bool)print(file,u) }, 
      int32_pt(n,u) then if (Bool)print(file,n) is
        { false then false, true then (Bool)print(file,u) }, 
      pt_pt(u,v) then if (Bool)print(file,u) is
        { false then false, true then (Bool)print(file,v) }
    }.

   
      *** (6.6) Thousands separators
   
public define Printable_tree 
  three_digits(Int32 n) =
    if n < 10 then ["00" , n]
  else if n < 100 then ["0", n]
  else [n]. 

public define Printable_tree
  two_digits(Int32 n) =
    if n < 10 then ["0", n]
    else [n]. 



public define Printable_tree
  thousands_separators(Int32 n) =
    if n < 1000 then [n]
    else [thousands_separators(n/1000),
          " ",
          three_digits(n (mod 1000)) ]. 

    Example: print(thousands_separator(1760050530)) will produce:
             1 760 050 530 



public define Int32
  dot_position (List(Word8) l) = 
    if l is 
      {
        [ ] then 0,      /* dot not found => integer => may end with .0 */ 
        [h . t] then 
          if h = '.' | h = ',' then 0 else 1 + dot_position(t)
      }. 


public define List(Word8)
  insert_separators (
                     List(Word8) l,      /* characters in number */ 
                     Int32 n            /* number of digits for integral part mod 3 */ 
                    ) =
      if l is
        {
          [ ] then [ ],
          [h . t] then 
            if h = '.' | h = ',' then 
                 l // [h . insert_separators(t,0)]
            else
                 ( if n = 0 then [h . insert_separators(t,2)]
              else if n = 1 then if t is
                                   {
                                     [ ] then [h], 
                                     [u . v] then (if u = '.' | u = ',' then 
                                              [h . insert_separators(t,0)]
                                         else [h , ' ' . insert_separators(t,0)])
                                   }
              else               [h . insert_separators(t,1)] )
        }. 


public define Printable_tree
  thousands_separators(String s) =
  with 
       l = (List(Word8))explode(s),     /* explode the string */ 
       n = dot_position(l) (mod 3),    /* find the number of digits before dot or comma (mod 3) */ 
       [implode(insert_separators(l,n))]. 



   
   *** (7) A variant of 'write_to_file' able to write a printable tree. 
   
public define WriteFileResult
   write_to_file
     (
       String             file_path, 
       Printable_tree     content
     ) =
   if (Maybe(RWStream))file(file_path,new) is 
     {
       failure then cannot_open_file, 
       success(f) then 
         if print(weaken(f),content)
         then ok
         else write_error(0)
     }. 
       
   
   
   *** (8) Putting a virtual machine at bed. 
   
   The  next   function  forces  the  virtual   machine  to  sleep  during   a  number  of
   milliseconds. This  works because  the virtual machine  waits 'ms'  milliseconds before
   computing 'true' for  the first time. Since 'true' is  always 'satisfied', the function
   returns 'unique' immediatly after this first computation of 'true'. 
   
public define One
  sleep
    (
      Int32 ms       // number of milliseconds to sleep
    ) =
  checking every ms milliseconds, wait for true then unique. 
   

   
   
   *** (9) Reliably  sending data
   
   
   We need  a function  for reliably  sending data, because  'write_file' does  not always
   write all the bytes of the given byte array.
   
read tools/connections.anubis   
   
define Maybe(Int32)
   reliable_write_aux
     (
       Connection connection,
       ByteArray ba,                                        // partial byte array
       Int32 already_sent,                                  // number of bytes already sent
       Int32 retries
     ) =
   if retries = 0 then failure else
   if write(connection,ba) is 
     {
       failure then 
         sleep(100);
         reliable_write_aux(connection,ba,already_sent,retries-1),
   
       success(n) then 
         if length(ba) = n
         then success(already_sent+n)
         else reliable_write_aux(connection,
                                 extract(ba,n,length(ba)),
                                 already_sent+n,
                                 20)
     }.
   
     
public define Maybe(Int32)
   reliable_write
     (
       Connection connection, 
       ByteArray ba
     ) =
   reliable_write_aux(connection,ba,0,20). 
   

   
   Debug version: 
   
 public define Maybe(Int32)
   reliable_write
     (
       Connection connection, 
       ByteArray ba
     ) =
   print("** ["+virtual_machine_id+"] Trying to write "+length(ba)+" bytes.\n");
   with result = reliable_write_aux(connection,ba,0),
     if result is 
       {
         failure then print("** ["+virtual_machine_id+"] Not all bytes written.\n"),
         success(n) then print("** ["+virtual_machine_id+"] "+n+" bytes written.\n")
       };
     result. 

   
   
   
   
   
public define Maybe(Int32)
   reliable_write
     (
       Connection conn, 
       Printable_tree t
     ) =
   if t is 
    {
      [ ] then success(0), 
   
      str_pt(s,u) then 
         if reliable_write(conn,to_byte_array(s)) is 
           {
             failure then failure,
             success(n) then if reliable_write(conn,u) is 
               {
                 failure then failure, 
                 success(m) then success(n+m)
               }
           },   
   
      ba_pt(b,u) then 
         if reliable_write(conn,b) is 
           {
             failure then failure,
             success(n) then if reliable_write(conn,u) is 
               {
                 failure then failure, 
                 success(m) then success(n+m)
               }
           },   
   
      int8_pt(a,u) then 
         if reliable_write(conn,to_byte_array(integer_to_string(word8_to_int32(a)))) is 
           {
             failure then failure,
             success(n) then if reliable_write(conn,u) is 
               {
                 failure then failure, 
                 success(m) then success(n+m)
               }
           },   

      int32_pt(a,u) then 
         if reliable_write(conn,to_byte_array(integer_to_string(a))) is 
           {
             failure then failure,
             success(n) then if reliable_write(conn,u) is 
               {
                 failure then failure, 
                 success(m) then success(n+m)
               }
           },   
   
      pt_pt(u,v) then 
         if reliable_write(conn,u) is 
           {
             failure then failure,
             success(n) then if reliable_write(conn,v) is 
               {
                 failure then failure, 
                 success(m) then success(n+m)
               }
           }   
    }.
   


   
   * Reliable read and write for TCP connections and files. 
   
   
public define Maybe(One)
   reliable_write
     (
       WStream conn, 
       ByteArray ba,
       Int32 already_sent, 
       Int32 retries
     ) = 
   if retries = 0 then failure else
   if write(conn,ba) is 
     {
       failure then 
         sleep(100); 
         reliable_write(conn,ba,already_sent,retries-1),
   
       success(n) then with l = length(ba), 
         if l = n
         then success(unique)
         else reliable_write(conn,
                             extract(ba,n,l),
                             already_sent+n,
                             20)
     }.
   
   
public define Maybe(One)
   reliable_write
     (
       RWStream conn, 
       ByteArray ba,
     ) = reliable_write(weaken(conn),ba,0,20). 
   
   
define Maybe(ByteArray)
   reliable_read
     (
       RStream conn,
       Int32 n,
       Int32 timeout,
       ByteArray so_far
     ) =
   if now > timeout then failure else
   if read(conn,n,1) is 
     {
       error    then failure,
       timeout  then failure,
       ok(ba)   then 
        with l = length(ba),
         if l = n
         then success(so_far+ba)
         else reliable_read(conn,n-l,timeout,so_far+ba)
     }. 

public define Maybe(ByteArray)
   reliable_read
     (
       RStream conn,
       Int32 n,
       Int32 timeout
     ) =
   reliable_read(conn,n,now+timeout,constant_byte_array(0,0)). 

public define Maybe(ByteArray)
   reliable_read
     (
       RWStream conn,
       Int32 n,
       Int32 timeout
     ) =
   reliable_read(weaken(conn),n,now+timeout,constant_byte_array(0,0)). 

   
   
   
   
   
   *** (10) Transmitting serializable data. 
   
   The  problem when  reading  a byte  array produced  by  a serialization  from a  TCP/IP
   connection is that  we must know how many  bytes to read before we  can unserialize the
   datum.  Hence, such  byte arrays must be transmitted prefixed by  their size.  The size
   itself (an  Int32) is serialized  before transmission. The  byte array obtained  by the
   serialization of an Int32 has 4  bytes (actually the Int32 itself in little-endian byte
   order).
   
   The next  two functions,  for sending  and receiving a  serializable datum,  handle the
   prefixing of the size together with the serialization and unserialization of the datum.
   Of  course,  any  serializable datum  sent  with  'send_datum'  must be  received  with
   'receive_datum'.
   
   
public define Maybe(One)
   send_datum
     (
       RWStream connection,
       $Datum datum
     ) =
   with sdatum = serialize(datum),
          size = length(sdatum),
   if reliable_write(connection,serialize(size)) is 
     {
       failure then failure, 
       success(_) then reliable_write(connection,sdatum)
     }.
   
   
public define Maybe($Datum)
   receive_datum
     (
       RWStream connection, 
     ) =
   if reliable_read(connection,4,10) is 
     {
       failure then failure, 
       success(ba_size) then if (Maybe(Int32))unserialize(ba_size) is 
         {
           failure then failure, 
           success(size) then if reliable_read(connection,size,10) is
             {
               failure then failure, 
               success(ba_datum) then (Maybe($Datum))unserialize(ba_datum)
             }
         }
     }.
   
   


   *** (11) Time
   
      *** (11.1) Time conversions. 

public define Int32 year(Int32 t)     = year(convert_time(t)). 
public define Int32 month(Int32 t)    = month(convert_time(t)). 
public define Int32 day(Int32 t)      = day(convert_time(t)). 
public define Int32 hour(Int32 t)     = hour(convert_time(t)). 
public define Int32 minute(Int32 t)   = minute(convert_time(t)). 
public define Int32 second(Int32 t)   = second(convert_time(t)). 
public define Int32 week_day(Int32 t) = week_day(convert_time(t)). 
public define Int32 year_day(Int32 t) = year_day(convert_time(t)). 
public define Bool  daylight_saving_time(Int32 t) = daylight_saving_time(convert_time(t)). 

public define Int32 
  date_and_time
    (
      Int32 y, 
      Int32 mo, 
      Int32 d, 
      Int32 h, 
      Int32 mi, 
      Int32 s,
      Bool  dst
    ) =
  convert_time(date_and_time(y,mo,d,h,mi,s,0,0,dst)). 
  

      *** (11.2) Calculations with UTime.

   A datum  of type  'UTime' is a  pair 'utime(s,m)' of  two 'Int32's.  Such a pair  is in
   'normal form' if either:
   
       - s >= 0 and 0    =< m  < 10^6
       - s =< 0 and -10^6 < m =< 0
   
   Notice that 'utime(s,m)' represents always '(10^6)s + m' microseconds even if it is not
   in normal form, and regardless of the signs of 's' and 'm'.
   
   All our fonctions below assume that their arguments are in normal form, and (under this
   assumption) return results in normal form.
   
   
define UTime
   normalize
     (
       Int32 s, 
       Int32 m
     ) =
   if s >= 0
   then if m < 0 
        then normalize(s-1,m+1000000)
        else if m >= 1000000 
             then normalize(s+1,m-1000000)
             else utime(s,m)
   else if m < -1000000 
        then normalize(s-1,m+1000000)
        else if m > 0 
             then normalize(s+1,m-1000000) 
             else utime(s,m). 
   
public define UTime
   normalize
     (
       UTime u
     ) =
   if u is utime(s,m) then normalize(s,m). 
   
public define Bool
   UTime u1 < UTime u2
     =
   if normalize(u1) is utime(s1,m1) then
   if normalize(u2) is utime(s2,m2) then 
   if s1 < s2 then true else 
   if s1 = s2 then m1 < m2 else
   false. 
   
   
public define Bool
   UTime u1 =< UTime u2
     =
   if normalize(u1) is utime(s1,m1) then
   if normalize(u2) is utime(s2,m2) then 
   if s1 =< s2 then true else 
   if s1 = s2 then m1 =< m2 else
   false. 
   
   
   
public define UTime
   UTime u1 - UTime u2
     =
   if u1 is utime(s1,m1) then 
   if u2 is utime(s2,m2) then
   normalize(s1-s2,m1-m2). 
   
public define UTime
   UTime u1 + UTime u2
     =
   if u1 is utime(s1,m1) then 
   if u2 is utime(s2,m2) then
   normalize(s1+s2,m1+m2). 
   
   
   
   
   
   *** (12) Files and directories
   
   
      *** (12.1) Get the size of a file knowing its name.
   
public define Int32
  file_size
    (
      String filename
    ) =
  if (Maybe(RStream))file(filename,read) is 
    {
      failure then 0, 
      success(file) then file_size(file)
    }.
   
   
      *** (12.2)  A predefined 'default' directory mode. 
   
public define DirectoryMode
  default_directory_mode
    = directory(readable,writable,searchable,
                non_readable,non_writable,non_searchable,
                non_readable,non_writable,non_searchable). 

  

      *** (12.3) Making a chain of directories. 
   
   The next function accepts  a directory path and a list of  string as its arguments, and
   constructs  in the  given  directory the  sequence  of directories  represented by  the
   elements of the list. For example,
   
      make_directories("/home/georges", ["first","second","third"])  
   
   will create the 3 directories:
   
       /home/georges/first
       /home/georges/first/second
       /home/georges/first/second/third
   
   If any  of them already exist,  there is no problem.  The function does  not return any
   information, and you may want to test if the directories have been created.
   
public define One
   make_directories
     (
       String dir, 
       List(String) path
     ) =
   if path is 
     {
       [ ]      then unique, 
       [h . t]  then 
         with new = if length(dir) = 0 then
                      if host_system is windows then
                        h
                      else
                        "/"+h
                    else
                      dir+"/"+h,
         forget(make_directory(new,default_directory_mode));
         make_directories(new,t)
     }.

   

      *** (12.4) Make a directory with default mode
   
define One
   make_directory_error
     (
       String path
     ) =
   print("Cannot create directory '"+path+"'.\n"). 
   
   
   
   
public define String     // returns the path of the directory
   make_directory
     (
       String path
     ) =
   if make_directory(path,default_directory_mode) is 
     {
       permission_denied      then make_directory_error(path), 
       name_already_exists    then unique,
       too_many_links         then make_directory_error(path),
       not_enough_room        then make_directory_error(path), 
       read_only_file_system  then make_directory_error(path),
       ok                     then unique
     };
   path. 
   
   
   
   
   
      *** (12.5) Testing if a file exists. 
   
public define Bool
   file_exists
     (
       String path
     ) =
   if (Maybe(RStream))file(path,read) is 
     {
       failure    then false, 
       success(_) then true
     }.
   
   
   
      *** (12.6) Searching directories and subdirectories recursively. 
   
public define List(String)
   recursive_directory_list
     (
       String directory_name,
       String file_name_mask
     ).
   
define List(String)   
   get_more_files
     (
       String dir, 
       String mask, 
       List(FileDescription) files
     ) =
   //print("*** "+dir+" "+mask+" ***\n"); 
   if files is 
     {
       [ ] then [ ], 
       [h . t] then 
         if h is 
           {
             no_info(_)             then get_more_files(dir,mask,t), 
             file(_,_,_,_)          then get_more_files(dir,mask,t),
             link(_,_,_,_)          then get_more_files(dir,mask,t), 
             directory(name,_,_)    then if name = "."  then get_more_files(dir,mask,t) else
                                         if name = ".." then get_more_files(dir,mask,t) else
                                         (recursive_directory_list(dir+"/"+name,mask)+
                                            get_more_files(dir,mask,t))
           }
     }. 
   
   
public define List(String)
   recursive_directory_list
     (
       String directory_name,
       String file_name_mask
     ) =
   with this_dir_files = (List(String))
     map((String s) |-> directory_name+"/"+s,reverse(directory_list(directory_name,file_name_mask))), 
   
   //print("Mask: "+file_name_mask+"\n"); 
   
   this_dir_files
   +
   get_more_files(directory_name,file_name_mask,directory_full_list(directory_name,"","","*")). 
   
   
   

   *** (13) Manipulating characters
   
      *** (13.1)  Putting a character to lower case. 
   
public define Word8
  to_lower
    (
      Word8 c
    ) =
  with n = word8_to_int32(c), 
  if ('A' =< n & n =< 'Z')
  then truncate_to_word8(n - 'A' + 'a')
  else c. 
   
public define Word8
  to_upper
    (
      Word8 c
    ) =
  with n = word8_to_int32(c), 
  if ('a' =< n & n =< 'z')
  then truncate_to_word8(n - 'a' + 'A')
  else c. 
   
   
      *** (13.2) Putting a string to lower case. 
   
define String 
  to_lower   
    (
      String s,
      Int32 n, 
      List(Word8) so_far
    ) =
  if nth(n,s) is 
    {
      failure then implode(reverse(so_far)), 
      success(c) then
        to_lower(s,n+1,[to_lower(c) . so_far])
    }.
   

public define String
  to_lower
    (
      String s
    ) =
  to_lower(s,0,[]). 
   
   
   
   
      *** (13.3) 'crlf'
   
   'crlf'  is a  string which  is required  by  the HTTP  protocol to  mark the  end of  a
   line. Any HTTP request or HTTP answer has  two parts (the second one may be empty): the
   header and the body. The header is  made of several lines (at least one), and separated
   from the body by a blank line. In other words, the header is separated from the body by
   a ''double crlf''.
   
public define String 
  crlf = implode([(Word8)13,10]).     i.e ASCII characters:   13 10  (in this order)
      
public define String crlfcrlf = implode([(Word8)13,10,13,10]).   
   
   
   
   

   *** (14) Prompting and reading from standard input. 

      *** (14.1) Reading a line from the standard input. 
   
define String
  read_line_from_stdin
    (
      List(Word8) so_far
    ) =
  if *stdin is 
    {
      failure then implode(reverse(so_far)), 
      success(c) then 
        if c = '\n'
        then implode(reverse(so_far))
        else read_line_from_stdin([c . so_far])
    }. 
   
   
   
      *** (14.2) Printing a prompt and reading the answer. 
   
public define String
  prompt
    (
      String header
    ) =
  print(header); 
  read_line_from_stdin([]). 
   

   
      *** (14.3) The same one for a password:
   
public define String
   prompt_for_password
     (
       String header
     ) =
   print(header);
   get_password.        // see 'predefined.anubis'
   
   
   
   
   
   *** (15) IP address
   
   
   An IP address is a Int32 which may be displayed numerically by the following function:
      
public define String
  ip_addr_to_string
    (
      Int32 addr
    ) =
  integer_to_string(addr&255) + "." +
  integer_to_string((addr>>8)&255) + "." +
  integer_to_string((addr>>16)&255) + "." +
  integer_to_string((addr>>24)&255).

   
   
   The following is used  when the bytes are not in network byte order  (as it is the case
   in RFC 1035 DNS messages).
   
public define String
  reverse_ip_addr_to_string
    (
      Int32 addr
    ) =
  integer_to_string((addr>>24)&255) + "." +
  integer_to_string((addr>>16)&255) + "." +
  integer_to_string((addr>>8)&255) + "." +
  integer_to_string((addr)&255).

   
   We can also construct IP addresses with:
   
public define Int32
   ip_address
     (
       (Word8,Word8,Word8,Word8) addr
     ) =
   if addr is (b3,b2,b1,b0) then
   (word8_to_int32(b0) << 24) |
   (word8_to_int32(b1) << 16) |
   (word8_to_int32(b2) << 8)  |
   (word8_to_int32(b3)).
   
   
   
   
   
   We may also need to transform a string like "123.456.789.123" into an integer IP address. 
   
define Bool
   is_digit
     (
       Word8 c
     ) =
   with n = word8_to_int32(c), 
   if n < '0' then false else n =< '9'. 
   
     
define Maybe((Int32,Int32))    // returns the next position for reading and a label. 
   read_label
     (
       String s,
       Int32 i,
       Int32 so_far
     ) =
   if nth(i,s) is 
     {
       failure then success((i,so_far)),
       success(c) then 
         if c = '.'
         then success((i+1,so_far))
         else if is_digit(c)
              then read_label(s,i+1,10*so_far+word8_to_int32(c)-'0')
              else failure
     }.
   
public define Maybe(Int32)
   ip_address
     (
       String s
     ) =
   if read_label(s,0,0) is 
     {
       failure then failure, 
       success(p1) then if p1 is (i2,lab1) then 
         if read_label(s,i2,0) is 
           {
             failure then failure,
             success(p2) then if p2 is (i3,lab2) then 
               if read_label(s,i3,0) is 
                 {
                   failure then failure, 
                   success(p3) then if p3 is (i4,lab3) then 
                     if read_label(s,i4,0) is 
                       {
                         failure then failure, 
                         success(p4) then if p4 is (i4a,lab4) then 
                           if i4a = length(s)
                           then success((lab4<<24)|(lab3<<16)|(lab2<<8)|lab1)
                           else failure
                       }
                 }
           }
     }.
   

   
   
   *** (16) Graphical tools
   
   
      *** (16.1) lighten

define Word8
   lighten
     (
       Word8 c,
       Int32 d
     ) =
   with n = word8_to_int32(c),
   truncate_to_word8(max(0,n+d)). 

public define RGBA
   lighten
     (
       RGBA color,
       Int32 d
     ) =
   if color is rgba(r,g,b,a) then 
   rgba(lighten(r,d),lighten(g,d),lighten(b,d),a). 
      
public define RGB
   lighten
     (
       RGB color,
       Int32 d
     ) =
   if color is rgb(r,g,b) then 
   rgb(lighten(r,d),lighten(g,d),lighten(b,d)). 

   
   
      *** (16.2) darken
   
public define RGBA
   darken
     (
       RGBA color,
       Int32 d
     ) =
   lighten(color,-d). 
   
public define RGB
   darken
     (
       RGB color,
       Int32 d
     ) =
   lighten(color,-d). 

   
   
      *** (16.3) System font names
   
   
public define List(String)   
   get_system_font_names
     =
   map((String fn) |-> substr(fn,0,length(fn)-3),
       directory_list(anubis_directory+"/library/system_fonts","*.sf")). 
   

   
      *** (16.4) Reading a color as an hexadecimal or decimal number. 
      
public define RGB
   rgb
     (
       Int32 x
     ) =
   rgb(truncate_to_word8(x>>16),
       truncate_to_word8(x>>8),
       truncate_to_word8(x)). 
   
   Examples:   rgb(5665433)
               rgb(0x3355cc)
   

   The same one for RGBA
   
public define RGBA
   rgba
     (
       Int32 x
     ) =
   rgba(truncate_to_word8(x>>24), 
        truncate_to_word8(x>>16),
        truncate_to_word8(x>>8),
        truncate_to_word8(x)). 
   
   
  Print the given string argument and adding newline. This is a convenience function to avoid type
  + "\n" at each end of print call.
   
public define One
  println
  (
    String line
  )=
  print(line + "\n").