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
|
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="generator" content="hevea 2.32">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1">
<link rel="stylesheet" type="text/css" href="manual.css">
<title>Chapter 1 The core language</title>
</head>
<body>
<a href="foreword.html"><img src="previous_motif.svg" alt="Previous"></a>
<a href="index.html"><img src="contents_motif.svg" alt="Up"></a>
<a href="moduleexamples.html"><img src="next_motif.svg" alt="Next"></a>
<hr>
<h1 class="chapter" id="sec7">Chapter 1 The core language</h1>
<ul>
<li><a href="coreexamples.html#s%3Abasics">1.1 Basics</a>
</li><li><a href="coreexamples.html#s%3Adatatypes">1.2 Data types</a>
</li><li><a href="coreexamples.html#s%3Afunctions-as-values">1.3 Functions as values</a>
</li><li><a href="coreexamples.html#s%3Atut-recvariants">1.4 Records and variants</a>
</li><li><a href="coreexamples.html#s%3Aimperative-features">1.5 Imperative features</a>
</li><li><a href="coreexamples.html#s%3Aexceptions">1.6 Exceptions</a>
</li><li><a href="coreexamples.html#s%3Alazy-expr">1.7 Lazy expressions</a>
</li><li><a href="coreexamples.html#s%3Asymb-expr">1.8 Symbolic processing of expressions</a>
</li><li><a href="coreexamples.html#s%3Apretty-printing">1.9 Pretty-printing</a>
</li><li><a href="coreexamples.html#s%3Aprintf">1.10 Printf formats</a>
</li><li><a href="coreexamples.html#s%3Astandalone-programs">1.11 Standalone OCaml programs</a>
</li></ul>
<p> <a id="c:core-xamples"></a>
</p><p>This part of the manual is a tutorial introduction to the
OCaml language. A good familiarity with programming in a conventional
languages (say, C or Java) is assumed, but no prior exposure to
functional languages is required. The present chapter introduces the
core language. Chapter <a href="moduleexamples.html#c%3Amoduleexamples">2</a> deals with the
module system, chapter <a href="objectexamples.html#c%3Aobjectexamples">3</a> with the
object-oriented features, chapter <a href="lablexamples.html#c%3Alabl-examples">4</a> with
extensions to the core language (labeled arguments and polymorphic
variants), and chapter <a href="advexamples.html#c%3Aadvexamples">6</a> gives some advanced examples.</p>
<h2 class="section" id="s:basics"><a class="section-anchor" href="#s:basics" aria-hidden="true"></a>1.1 Basics</h2>
<p>For this overview of OCaml, we use the interactive system, which
is started by running <span class="c003">ocaml</span> from the Unix shell, or by launching the
<span class="c003">OCamlwin.exe</span> application under Windows. This tutorial is presented
as the transcript of a session with the interactive system:
lines starting with <span class="c003">#</span> represent user input; the system responses are
printed below, without a leading <span class="c003">#</span>.</p><p>Under the interactive system, the user types OCaml phrases terminated
by <span class="c003">;;</span> in response to the <span class="c003">#</span> prompt, and the system compiles them
on the fly, executes them, and prints the outcome of evaluation.
Phrases are either simple expressions, or <span class="c003">let</span> definitions of
identifiers (either values or functions).
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> 1+2*3;;</div>
<div class="pre caml-output ok">- : int = 7</div></div>
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> pi = 4.0 *. atan 1.0;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> pi : float = 3.14159265358979312</div></div>
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> square x = x *. x;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> square : float -> float = <<span class="ocamlkeyword">fun</span>></div></div>
<div class="ocaml">
<div class="pre caml-input"> square (sin pi) +. square (cos pi);;</div>
<div class="pre caml-output ok">- : float = 1.</div></div>
</div><p>
The OCaml system computes both the value and the type for
each phrase. Even function parameters need no explicit type declaration:
the system infers their types from their usage in the
function. Notice also that integers and floating-point numbers are
distinct types, with distinct operators: <span class="c003">+</span> and <span class="c003">*</span> operate on
integers, but <span class="c003">+.</span> and <span class="c003">*.</span> operate on floats.
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlhighlight">1.0</span> * 2;;</div>
<div class="pre caml-output error"><span class="ocamlerror">Error</span>: This expression has type float but an expression was expected of type
int</div></div>
</div><p>Recursive functions are defined with the <span class="c003">let rec</span> binding:
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> <span class="ocamlkeyword">rec</span> fib n =
<span class="ocamlkeyword">if</span> n < 2 <span class="ocamlkeyword">then</span> n <span class="ocamlkeyword">else</span> fib (n-1) + fib (n-2);;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> fib : int -> int = <<span class="ocamlkeyword">fun</span>></div></div>
<div class="ocaml">
<div class="pre caml-input"> fib 10;;</div>
<div class="pre caml-output ok">- : int = 55</div></div>
</div>
<h2 class="section" id="s:datatypes"><a class="section-anchor" href="#s:datatypes" aria-hidden="true"></a>1.2 Data types</h2>
<p>In addition to integers and floating-point numbers, OCaml offers the
usual basic data types:
</p><ul class="itemize"><li class="li-itemize">booleans
<div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> (1 < 2) = <span class="ocamlkeyword">false</span>;;</div>
<div class="pre caml-output ok">- : bool = <span class="ocamlkeyword">false</span></div></div>
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> one = <span class="ocamlkeyword">if</span> <span class="ocamlkeyword">true</span> <span class="ocamlkeyword">then</span> 1 <span class="ocamlkeyword">else</span> 2;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> one : int = 1</div></div>
</div>
</li><li class="li-itemize">characters
<div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> 'a';;</div>
<div class="pre caml-output ok">- : char = 'a'</div></div>
<div class="ocaml">
<div class="pre caml-input"> int_of_char '\n';;</div>
<div class="pre caml-output ok">- : int = 10</div></div>
</div>
</li><li class="li-itemize">immutable character strings
<div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlstring">"Hello"</span> ^ <span class="ocamlstring">" "</span> ^ <span class="ocamlstring">"world"</span>;;</div>
<div class="pre caml-output ok">- : string = <span class="ocamlstring">"Hello world"</span></div></div>
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlstring">{|This is a quoted string, here, neither \ nor " are special characters|}</span>;;</div>
<div class="pre caml-output ok">- : string =
<span class="ocamlstring">"This is a quoted string, here, neither \\ nor \" are special characters"</span></div></div>
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlstring">{|"\\"|}</span>=<span class="ocamlstring">"\"\\\\\""</span>;;</div>
<div class="pre caml-output ok">- : bool = <span class="ocamlkeyword">true</span></div></div>
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlstring">{delimiter|the end of this|}quoted string is here|delimiter}</span>
= <span class="ocamlstring">"the end of this|}quoted string is here"</span>;;</div>
<div class="pre caml-output ok">- : bool = <span class="ocamlkeyword">true</span></div></div>
</div>
</li></ul><p>Predefined data structures include tuples, arrays, and lists. There are also
general mechanisms for defining your own data structures, such as records and
variants, which will be covered in more detail later; for now, we concentrate
on lists. Lists are either given in extension as a bracketed list of
semicolon-separated elements, or built from the empty list <span class="c003">[]</span>
(pronounce “nil”) by adding elements in front using the <span class="c003">::</span>
(“cons”) operator.
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> l = [<span class="ocamlstring">"is"</span>; <span class="ocamlstring">"a"</span>; <span class="ocamlstring">"tale"</span>; <span class="ocamlstring">"told"</span>; <span class="ocamlstring">"etc."</span>];;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> l : string list = [<span class="ocamlstring">"is"</span>; <span class="ocamlstring">"a"</span>; <span class="ocamlstring">"tale"</span>; <span class="ocamlstring">"told"</span>; <span class="ocamlstring">"etc."</span>]</div></div>
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlstring">"Life"</span> :: l;;</div>
<div class="pre caml-output ok">- : string list = [<span class="ocamlstring">"Life"</span>; <span class="ocamlstring">"is"</span>; <span class="ocamlstring">"a"</span>; <span class="ocamlstring">"tale"</span>; <span class="ocamlstring">"told"</span>; <span class="ocamlstring">"etc."</span>]</div></div>
</div><p>
As with all other OCaml data structures, lists do not need to be
explicitly allocated and deallocated from memory: all memory
management is entirely automatic in OCaml. Similarly, there is no
explicit handling of pointers: the OCaml compiler silently introduces
pointers where necessary.</p><p>As with most OCaml data structures, inspecting and destructuring lists
is performed by pattern-matching. List patterns have exactly the same
form as list expressions, with identifiers representing unspecified
parts of the list. As an example, here is insertion sort on a list:
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> <span class="ocamlkeyword">rec</span> sort lst =
<span class="ocamlkeyword">match</span> lst <span class="ocamlkeyword">with</span>
[] -> []
| head :: tail -> insert head (sort tail)
<span class="ocamlkeyword">and</span> insert elt lst =
<span class="ocamlkeyword">match</span> lst <span class="ocamlkeyword">with</span>
[] -> [elt]
| head :: tail -> <span class="ocamlkeyword">if</span> elt <= head <span class="ocamlkeyword">then</span> elt :: lst <span class="ocamlkeyword">else</span> head :: insert elt tail
;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> sort : 'a list -> 'a list = <<span class="ocamlkeyword">fun</span>>
<span class="ocamlkeyword">val</span> insert : 'a -> 'a list -> 'a list = <<span class="ocamlkeyword">fun</span>></div></div>
<div class="ocaml">
<div class="pre caml-input"> sort l;;</div>
<div class="pre caml-output ok">- : string list = [<span class="ocamlstring">"a"</span>; <span class="ocamlstring">"etc."</span>; <span class="ocamlstring">"is"</span>; <span class="ocamlstring">"tale"</span>; <span class="ocamlstring">"told"</span>]</div></div>
</div><p>The type inferred for <span class="c003">sort</span>, <span class="c003">'a list -> 'a list</span>, means that <span class="c003">sort</span>
can actually apply to lists of any type, and returns a list of the
same type. The type <span class="c003">'a</span> is a <em>type variable</em>, and stands for any
given type. The reason why <span class="c003">sort</span> can apply to lists of any type is
that the comparisons (<span class="c003">=</span>, <span class="c003"><=</span>, etc.) are <em>polymorphic</em> in OCaml:
they operate between any two values of the same type. This makes
<span class="c003">sort</span> itself polymorphic over all list types.
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> sort [6;2;5;3];;</div>
<div class="pre caml-output ok">- : int list = [2; 3; 5; 6]</div></div>
<div class="ocaml">
<div class="pre caml-input"> sort [3.14; 2.718];;</div>
<div class="pre caml-output ok">- : float list = [2.718; 3.14]</div></div>
</div><p>The <span class="c003">sort</span> function above does not modify its input list: it builds
and returns a new list containing the same elements as the input list,
in ascending order. There is actually no way in OCaml to modify
a list in-place once it is built: we say that lists are <em>immutable</em>
data structures. Most OCaml data structures are immutable, but a few
(most notably arrays) are <em>mutable</em>, meaning that they can be
modified in-place at any time.</p><p>The OCaml notation for the type of a function with multiple arguments is <br>
<span class="c003">arg1_type -> arg2_type -> ... -> return_type</span>. For example,
the type inferred for <span class="c003">insert</span>, <span class="c003">'a -> 'a list -> 'a list</span>, means that <span class="c003">insert</span>
takes two arguments, an element of any type <span class="c003">'a</span> and a list with elements of
the same type <span class="c003">'a</span> and returns a list of the same type.
</p>
<h2 class="section" id="s:functions-as-values"><a class="section-anchor" href="#s:functions-as-values" aria-hidden="true"></a>1.3 Functions as values</h2>
<p>OCaml is a functional language: functions in the full mathematical
sense are supported and can be passed around freely just as any other
piece of data. For instance, here is a <span class="c003">deriv</span> function that takes any
float function as argument and returns an approximation of its
derivative function:
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> deriv f dx = <span class="ocamlkeyword">function</span> x -> (f (x +. dx) -. f x) /. dx;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> deriv : (float -> float) -> float -> float -> float = <<span class="ocamlkeyword">fun</span>></div></div>
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> sin' = deriv sin 1e-6;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> sin' : float -> float = <<span class="ocamlkeyword">fun</span>></div></div>
<div class="ocaml">
<div class="pre caml-input"> sin' pi;;</div>
<div class="pre caml-output ok">- : float = -1.00000000013961143</div></div>
</div><p>
Even function composition is definable:
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> compose f g = <span class="ocamlkeyword">function</span> x -> f (g x);;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> compose : ('a -> 'b) -> ('c -> 'a) -> 'c -> 'b = <<span class="ocamlkeyword">fun</span>></div></div>
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> cos2 = compose square cos;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> cos2 : float -> float = <<span class="ocamlkeyword">fun</span>></div></div>
</div><p>Functions that take other functions as arguments are called
“functionals”, or “higher-order functions”. Functionals are
especially useful to provide iterators or similar generic operations
over a data structure. For instance, the standard OCaml library
provides a <span class="c003">List.map</span> functional that applies a given function to each
element of a list, and returns the list of the results:
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> List.map (<span class="ocamlkeyword">function</span> n -> n * 2 + 1) [0;1;2;3;4];;</div>
<div class="pre caml-output ok">- : int list = [1; 3; 5; 7; 9]</div></div>
</div><p>
This functional, along with a number of other list and array
functionals, is predefined because it is often useful, but there is
nothing magic with it: it can easily be defined as follows.
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> <span class="ocamlkeyword">rec</span> map f l =
<span class="ocamlkeyword">match</span> l <span class="ocamlkeyword">with</span>
[] -> []
| hd :: tl -> f hd :: map f tl;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> map : ('a -> 'b) -> 'a list -> 'b list = <<span class="ocamlkeyword">fun</span>></div></div>
</div>
<h2 class="section" id="s:tut-recvariants"><a class="section-anchor" href="#s:tut-recvariants" aria-hidden="true"></a>1.4 Records and variants</h2>
<p>User-defined data structures include records and variants. Both are
defined with the <span class="c003">type</span> declaration. Here, we declare a record type to
represent rational numbers.
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">type</span> ratio = {num: int; denom: int};;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">type</span> ratio = { num : int; denom : int; }</div></div>
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> add_ratio r1 r2 =
{num = r1.num * r2.denom + r2.num * r1.denom;
denom = r1.denom * r2.denom};;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> add_ratio : ratio -> ratio -> ratio = <<span class="ocamlkeyword">fun</span>></div></div>
<div class="ocaml">
<div class="pre caml-input"> add_ratio {num=1; denom=3} {num=2; denom=5};;</div>
<div class="pre caml-output ok">- : ratio = {num = 11; denom = 15}</div></div>
</div><p>
Record fields can also be accessed through pattern-matching:
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> integer_part r =
<span class="ocamlkeyword">match</span> r <span class="ocamlkeyword">with</span>
{num=num; denom=denom} -> num / denom;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> integer_part : ratio -> int = <<span class="ocamlkeyword">fun</span>></div></div>
</div><p>
Since there is only one case in this pattern matching, it
is safe to expand directly the argument <span class="c003">r</span> in a record pattern:
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> integer_part {num=num; denom=denom} = num / denom;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> integer_part : ratio -> int = <<span class="ocamlkeyword">fun</span>></div></div>
</div><p>
Unneeded fields can be omitted:
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> get_denom {denom=denom} = denom;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> get_denom : ratio -> int = <<span class="ocamlkeyword">fun</span>></div></div>
</div><p>
Optionally, missing fields can be made explicit by ending the list of
fields with a trailing wildcard <span class="c003">_</span>::
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> get_num {num=num; _ } = num;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> get_num : ratio -> int = <<span class="ocamlkeyword">fun</span>></div></div>
</div><p>
When both sides of the <span class="c003">=</span> sign are the same, it is possible to avoid
repeating the field name by eliding the <span class="c003">=field</span> part:
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> integer_part {num; denom} = num / denom;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> integer_part : ratio -> int = <<span class="ocamlkeyword">fun</span>></div></div>
</div><p>
This short notation for fields also works when constructing records:
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> ratio num denom = {num; denom};;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> ratio : int -> int -> ratio = <<span class="ocamlkeyword">fun</span>></div></div>
</div><p>
At last, it is possible to update few fields of a record at once:
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> integer_product integer ratio = { ratio <span class="ocamlkeyword">with</span> num = integer * ratio.num };;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> integer_product : int -> ratio -> ratio = <<span class="ocamlkeyword">fun</span>></div></div>
</div><p>
With this functional update notation, the record on the left-hand side
of <span class="c003">with</span> is copied except for the fields on the right-hand side which
are updated.</p><p>The declaration of a variant type lists all possible forms for values
of that type. Each case is identified by a name, called a constructor,
which serves both for constructing values of the variant type and
inspecting them by pattern-matching. Constructor names are capitalized
to distinguish them from variable names (which must start with a
lowercase letter). For instance, here is a variant
type for doing mixed arithmetic (integers and floats):
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">type</span> number = Int <span class="ocamlkeyword">of</span> int | Float <span class="ocamlkeyword">of</span> float | Error;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">type</span> number = Int <span class="ocamlkeyword">of</span> int | Float <span class="ocamlkeyword">of</span> float | Error</div></div>
</div><p>
This declaration expresses that a value of type <span class="c003">number</span> is either an
integer, a floating-point number, or the constant <span class="c003">Error</span> representing
the result of an invalid operation (e.g. a division by zero).</p><p>Enumerated types are a special case of variant types, where all
alternatives are constants:
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">type</span> sign = Positive | Negative;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">type</span> sign = Positive | Negative</div></div>
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> sign_int n = <span class="ocamlkeyword">if</span> n >= 0 <span class="ocamlkeyword">then</span> Positive <span class="ocamlkeyword">else</span> Negative;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> sign_int : int -> sign = <<span class="ocamlkeyword">fun</span>></div></div>
</div><p>To define arithmetic operations for the <span class="c003">number</span> type, we use
pattern-matching on the two numbers involved:
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> add_num n1 n2 =
<span class="ocamlkeyword">match</span> (n1, n2) <span class="ocamlkeyword">with</span>
(Int i1, Int i2) ->
<span class="ocamlcomment">(* Check for overflow of integer addition *)</span>
<span class="ocamlkeyword">if</span> sign_int i1 = sign_int i2 && sign_int (i1 + i2) <> sign_int i1
<span class="ocamlkeyword">then</span> Float(float i1 +. float i2)
<span class="ocamlkeyword">else</span> Int(i1 + i2)
| (Int i1, Float f2) -> Float(float i1 +. f2)
| (Float f1, Int i2) -> Float(f1 +. float i2)
| (Float f1, Float f2) -> Float(f1 +. f2)
| (Error, _) -> Error
| (_, Error) -> Error;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> add_num : number -> number -> number = <<span class="ocamlkeyword">fun</span>></div></div>
<div class="ocaml">
<div class="pre caml-input"> add_num (Int 123) (Float 3.14159);;</div>
<div class="pre caml-output ok">- : number = Float 126.14159</div></div>
</div><p>Another interesting example of variant type is the built-in
<span class="c003">'a option</span> type which represents either a value of type <span class="c003">'a</span> or an
absence of value:
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">type</span> 'a option = Some <span class="ocamlkeyword">of</span> 'a | None;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">type</span> 'a option = Some <span class="ocamlkeyword">of</span> 'a | None</div></div>
</div><p>
This type is particularly useful when defining function that can
fail in common situations, for instance
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> safe_square_root x = <span class="ocamlkeyword">if</span> x > 0. <span class="ocamlkeyword">then</span> Some(sqrt x) <span class="ocamlkeyword">else</span> None;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> safe_square_root : float -> float option = <<span class="ocamlkeyword">fun</span>></div></div>
</div><p>The most common usage of variant types is to describe recursive data
structures. Consider for example the type of binary trees:
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">type</span> 'a btree = Empty | Node <span class="ocamlkeyword">of</span> 'a * 'a btree * 'a btree;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">type</span> 'a btree = Empty | Node <span class="ocamlkeyword">of</span> 'a * 'a btree * 'a btree</div></div>
</div><p>
This definition reads as follows: a binary tree containing values of
type <span class="c003">'a</span> (an arbitrary type) is either empty, or is a node containing
one value of type <span class="c003">'a</span> and two subtrees also containing values of type
<span class="c003">'a</span>, that is, two <span class="c003">'a btree</span>.</p><p>Operations on binary trees are naturally expressed as recursive functions
following the same structure as the type definition itself. For
instance, here are functions performing lookup and insertion in
ordered binary trees (elements increase from left to right):
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> <span class="ocamlkeyword">rec</span> member x btree =
<span class="ocamlkeyword">match</span> btree <span class="ocamlkeyword">with</span>
Empty -> <span class="ocamlkeyword">false</span>
| Node(y, left, right) ->
<span class="ocamlkeyword">if</span> x = y <span class="ocamlkeyword">then</span> <span class="ocamlkeyword">true</span> <span class="ocamlkeyword">else</span>
<span class="ocamlkeyword">if</span> x < y <span class="ocamlkeyword">then</span> member x left <span class="ocamlkeyword">else</span> member x right;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> member : 'a -> 'a btree -> bool = <<span class="ocamlkeyword">fun</span>></div></div>
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> <span class="ocamlkeyword">rec</span> insert x btree =
<span class="ocamlkeyword">match</span> btree <span class="ocamlkeyword">with</span>
Empty -> Node(x, Empty, Empty)
| Node(y, left, right) ->
<span class="ocamlkeyword">if</span> x <= y <span class="ocamlkeyword">then</span> Node(y, insert x left, right)
<span class="ocamlkeyword">else</span> Node(y, left, insert x right);;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> insert : 'a -> 'a btree -> 'a btree = <<span class="ocamlkeyword">fun</span>></div></div>
</div>
<h3 class="subsection" id="ss:record-and-variant-disambiguation"><a class="section-anchor" href="#ss:record-and-variant-disambiguation" aria-hidden="true"></a>1.4.1 Record and variant disambiguation</h3>
<p>
( This subsection can be skipped on the first reading )</p><p>Astute readers may have wondered what happens when two or more record
fields or constructors share the same name</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">type</span> first_record = { x:int; y:int; z:int }
<span class="ocamlkeyword">type</span> middle_record = { x:int; z:int }
<span class="ocamlkeyword">type</span> last_record = { x:int };;</div></div>
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">type</span> first_variant = A | B | C
<span class="ocamlkeyword">type</span> last_variant = A;;</div></div>
</div><p>The answer is that when confronted with multiple options, OCaml tries to
use locally available information to disambiguate between the various fields
and constructors. First, if the type of the record or variant is known,
OCaml can pick unambiguously the corresponding field or constructor.
For instance:</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> look_at_x_then_z (r:first_record) =
<span class="ocamlkeyword">let</span> x = r.x <span class="ocamlkeyword">in</span>
x + r.z;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> look_at_x_then_z : first_record -> int = <<span class="ocamlkeyword">fun</span>></div></div>
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> permute (x:first_variant) = <span class="ocamlkeyword">match</span> x <span class="ocamlkeyword">with</span>
| A -> (B:first_variant)
| B -> A
| C -> C;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> permute : first_variant -> first_variant = <<span class="ocamlkeyword">fun</span>></div></div>
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">type</span> wrapped = First <span class="ocamlkeyword">of</span> first_record
<span class="ocamlkeyword">let</span> f (First r) = r, r.x;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">type</span> wrapped = First <span class="ocamlkeyword">of</span> first_record
<span class="ocamlkeyword">val</span> f : wrapped -> first_record * int = <<span class="ocamlkeyword">fun</span>></div></div>
</div><p>In the first example, <span class="c003">(r:first_record)</span> is an explicit annotation
telling OCaml that the type of <span class="c003">r</span> is <span class="c003">first_record</span>. With this
annotation, Ocaml knows that <span class="c003">r.x</span> refers to the <span class="c003">x</span> field of the first
record type. Similarly, the type annotation in the second example makes
it clear to OCaml that the constructors <span class="c003">A</span>, <span class="c003">B</span> and <span class="c003">C</span> come from the
first variant type. Contrarily, in the last example, OCaml has inferred
by itself that the type of <span class="c003">r</span> can only be <span class="c003">first_record</span> and there are
no needs for explicit type annotations.</p><p>Those explicit type annotations can in fact be used anywhere.
Most of the time they are unnecessary, but they are useful to guide
disambiguation, to debug unexpected type errors, or combined with some
of the more advanced features of OCaml described in later chapters.</p><p>Secondly, for records, OCaml can also deduce the right record type by
looking at the whole set of fields used in a expression or pattern:
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> project_and_rotate {x;y; _ } = { x= - y; y = x ; z = 0} ;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> project_and_rotate : first_record -> first_record = <<span class="ocamlkeyword">fun</span>></div></div>
</div><p>
Since the fields <span class="c003">x</span> and <span class="c003">y</span> can only appear simultaneously in the first
record type, OCaml infers that the type of <span class="c003">project_and_rotate</span> is
<span class="c003">first_record -> first_record</span>.</p><p>In last resort, if there is not enough information to disambiguate between
different fields or constructors, Ocaml picks the last defined type
amongst all locally valid choices:</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> look_at_xz {x;z} = x;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> look_at_xz : middle_record -> int = <<span class="ocamlkeyword">fun</span>></div></div>
</div><p>Here, OCaml has inferred that the possible choices for the type of
<span class="c003">{x;z}</span> are <span class="c003">first_record</span> and <span class="c003">middle_record</span>, since the type
<span class="c003">last_record</span> has no field <span class="c003">z</span>. Ocaml then picks the type <span class="c003">middle_record</span>
as the last defined type between the two possibilities.</p><p>Beware that this last resort disambiguation is local: once Ocaml has
chosen a disambiguation, it sticks to this choice, even if it leads to
an ulterior type error:</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> look_at_x_then_y r =
<span class="ocamlkeyword">let</span> x = r.x <span class="ocamlkeyword">in</span> <span class="ocamlcomment">(* Ocaml deduces [r: last_record] *)</span>
x + r.<span class="ocamlhighlight">y</span>;;</div>
<div class="pre caml-output error"><span class="ocamlerror">Error</span>: This expression has type last_record
The field y does not belong to type last_record</div></div>
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> is_a_or_b x = <span class="ocamlkeyword">match</span> x <span class="ocamlkeyword">with</span>
| A -> <span class="ocamlkeyword">true</span> <span class="ocamlcomment">(* OCaml infers [x: last_variant] *)</span>
| <span class="ocamlhighlight">B</span> -> <span class="ocamlkeyword">true</span>;;</div>
<div class="pre caml-output error"><span class="ocamlerror">Error</span>: This variant pattern is expected to have type last_variant
The constructor B does not belong to type last_variant</div></div>
</div><p>Moreover, being the last defined type is a quite unstable position that
may change surreptitiously after adding or moving around a type
definition, or after opening a module (see chapter <a href="moduleexamples.html#c%3Amoduleexamples">2</a>).
Consequently, adding explicit type annotations to guide disambiguation is
more robust than relying on the last defined type disambiguation.</p>
<h2 class="section" id="s:imperative-features"><a class="section-anchor" href="#s:imperative-features" aria-hidden="true"></a>1.5 Imperative features</h2>
<p>Though all examples so far were written in purely applicative style,
OCaml is also equipped with full imperative features. This includes the
usual <span class="c003">while</span> and <span class="c003">for</span> loops, as well as mutable data structures such
as arrays. Arrays are either created by listing semicolon-separated element
values between <span class="c003">[|</span> and <span class="c003">|]</span> brackets, or allocated and initialized with the
<span class="c003">Array.make</span> function, then filled up later by assignments. For instance, the
function below sums two vectors (represented as float arrays) componentwise.
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> add_vect v1 v2 =
<span class="ocamlkeyword">let</span> len = min (Array.length v1) (Array.length v2) <span class="ocamlkeyword">in</span>
<span class="ocamlkeyword">let</span> res = Array.make len 0.0 <span class="ocamlkeyword">in</span>
<span class="ocamlkeyword">for</span> i = 0 <span class="ocamlkeyword">to</span> len - 1 <span class="ocamlkeyword">do</span>
res.(i) <- v1.(i) +. v2.(i)
<span class="ocamlkeyword">done</span>;
res;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> add_vect : float array -> float array -> float array = <<span class="ocamlkeyword">fun</span>></div></div>
<div class="ocaml">
<div class="pre caml-input"> add_vect [| 1.0; 2.0 |] [| 3.0; 4.0 |];;</div>
<div class="pre caml-output ok">- : float array = [|4.; 6.|]</div></div>
</div><p>Record fields can also be modified by assignment, provided they are
declared <span class="c003">mutable</span> in the definition of the record type:
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">type</span> mutable_point = { <span class="ocamlkeyword">mutable</span> x: float; <span class="ocamlkeyword">mutable</span> y: float };;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">type</span> mutable_point = { <span class="ocamlkeyword">mutable</span> x : float; <span class="ocamlkeyword">mutable</span> y : float; }</div></div>
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> translate p dx dy =
p.x <- p.x +. dx; p.y <- p.y +. dy;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> translate : mutable_point -> float -> float -> unit = <<span class="ocamlkeyword">fun</span>></div></div>
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> mypoint = { x = 0.0; y = 0.0 };;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> mypoint : mutable_point = {x = 0.; y = 0.}</div></div>
<div class="ocaml">
<div class="pre caml-input"> translate mypoint 1.0 2.0;;</div>
<div class="pre caml-output ok">- : unit = ()</div></div>
<div class="ocaml">
<div class="pre caml-input"> mypoint;;</div>
<div class="pre caml-output ok">- : mutable_point = {x = 1.; y = 2.}</div></div>
</div><p>OCaml has no built-in notion of variable – identifiers whose current
value can be changed by assignment. (The <span class="c003">let</span> binding is not an
assignment, it introduces a new identifier with a new scope.)
However, the standard library provides references, which are mutable
indirection cells, with operators <span class="c003">!</span> to fetch
the current contents of the reference and <span class="c003">:=</span> to assign the contents.
Variables can then be emulated by <span class="c003">let</span>-binding a reference. For
instance, here is an in-place insertion sort over arrays:
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> insertion_sort a =
<span class="ocamlkeyword">for</span> i = 1 <span class="ocamlkeyword">to</span> Array.length a - 1 <span class="ocamlkeyword">do</span>
<span class="ocamlkeyword">let</span> val_i = a.(i) <span class="ocamlkeyword">in</span>
<span class="ocamlkeyword">let</span> j = <span class="ocamlkeyword">ref</span> i <span class="ocamlkeyword">in</span>
<span class="ocamlkeyword">while</span> !j > 0 && val_i < a.(!j - 1) <span class="ocamlkeyword">do</span>
a.(!j) <- a.(!j - 1);
j := !j - 1
<span class="ocamlkeyword">done</span>;
a.(!j) <- val_i
<span class="ocamlkeyword">done</span>;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> insertion_sort : 'a array -> unit = <<span class="ocamlkeyword">fun</span>></div></div>
</div><p>References are also useful to write functions that maintain a current
state between two calls to the function. For instance, the following
pseudo-random number generator keeps the last returned number in a
reference:
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> current_rand = <span class="ocamlkeyword">ref</span> 0;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> current_rand : int <span class="ocamlkeyword">ref</span> = {contents = 0}</div></div>
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> random () =
current_rand := !current_rand * 25713 + 1345;
!current_rand;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> random : unit -> int = <<span class="ocamlkeyword">fun</span>></div></div>
</div><p>Again, there is nothing magical with references: they are implemented as
a single-field mutable record, as follows.
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">type</span> 'a <span class="ocamlkeyword">ref</span> = { <span class="ocamlkeyword">mutable</span> contents: 'a };;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">type</span> 'a <span class="ocamlkeyword">ref</span> = { <span class="ocamlkeyword">mutable</span> contents : 'a; }</div></div>
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> ( ! ) r = r.contents;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> ( ! ) : 'a <span class="ocamlkeyword">ref</span> -> 'a = <<span class="ocamlkeyword">fun</span>></div></div>
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> ( := ) r newval = r.contents <- newval;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> ( := ) : 'a <span class="ocamlkeyword">ref</span> -> 'a -> unit = <<span class="ocamlkeyword">fun</span>></div></div>
</div><p>In some special cases, you may need to store a polymorphic function in
a data structure, keeping its polymorphism. Doing this requires
user-provided type annotations, since polymorphism is only introduced
automatically for global definitions. However, you can explicitly give
polymorphic types to record fields.
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">type</span> idref = { <span class="ocamlkeyword">mutable</span> id: 'a. 'a -> 'a };;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">type</span> idref = { <span class="ocamlkeyword">mutable</span> id : 'a. 'a -> 'a; }</div></div>
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> r = {id = <span class="ocamlkeyword">fun</span> x -> x};;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> r : idref = {id = <<span class="ocamlkeyword">fun</span>>}</div></div>
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> g s = (s.id 1, s.id <span class="ocamlkeyword">true</span>);;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> g : idref -> int * bool = <<span class="ocamlkeyword">fun</span>></div></div>
<div class="ocaml">
<div class="pre caml-input"> r.id <- (<span class="ocamlkeyword">fun</span> x -> print_string <span class="ocamlstring">"called id\n"</span>; x);;</div>
<div class="pre caml-output ok">- : unit = ()</div></div>
<div class="ocaml">
<div class="pre caml-input"> g r;;</div>
<div class="pre caml-output ok">called id
called id
- : int * bool = (1, <span class="ocamlkeyword">true</span>)</div></div>
</div>
<h2 class="section" id="s:exceptions"><a class="section-anchor" href="#s:exceptions" aria-hidden="true"></a>1.6 Exceptions</h2>
<p>OCaml provides exceptions for signalling and handling exceptional
conditions. Exceptions can also be used as a general-purpose non-local
control structure, although this should not be overused since it can
make the code harder to understand. Exceptions are declared with the
<span class="c003">exception</span> construct, and signalled with the <span class="c003">raise</span> operator. For instance,
the function below for taking the head of a list uses an exception to
signal the case where an empty list is given.
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">exception</span> Empty_list;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">exception</span> Empty_list</div></div>
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> head l =
<span class="ocamlkeyword">match</span> l <span class="ocamlkeyword">with</span>
[] -> raise Empty_list
| hd :: tl -> hd;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> head : 'a list -> 'a = <<span class="ocamlkeyword">fun</span>></div></div>
<div class="ocaml">
<div class="pre caml-input"> head [1;2];;</div>
<div class="pre caml-output ok">- : int = 1</div></div>
<div class="ocaml">
<div class="pre caml-input"> head [];;</div>
<div class="pre caml-output ok">Exception: Empty_list.</div></div>
</div><p>Exceptions are used throughout the standard library to signal cases
where the library functions cannot complete normally. For instance,
the <span class="c003">List.assoc</span> function, which returns the data associated with a
given key in a list of (key, data) pairs, raises the predefined
exception <span class="c003">Not_found</span> when the key does not appear in the list:
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> List.assoc 1 [(0, <span class="ocamlstring">"zero"</span>); (1, <span class="ocamlstring">"one"</span>)];;</div>
<div class="pre caml-output ok">- : string = <span class="ocamlstring">"one"</span></div></div>
<div class="ocaml">
<div class="pre caml-input"> List.assoc 2 [(0, <span class="ocamlstring">"zero"</span>); (1, <span class="ocamlstring">"one"</span>)];;</div>
<div class="pre caml-output ok">Exception: Not_found.</div></div>
</div><p>Exceptions can be trapped with the <span class="c003">try</span>…<span class="c003">with</span> construct:
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> name_of_binary_digit digit =
<span class="ocamlkeyword">try</span>
List.assoc digit [0, <span class="ocamlstring">"zero"</span>; 1, <span class="ocamlstring">"one"</span>]
<span class="ocamlkeyword">with</span> Not_found ->
<span class="ocamlstring">"not a binary digit"</span>;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> name_of_binary_digit : int -> string = <<span class="ocamlkeyword">fun</span>></div></div>
<div class="ocaml">
<div class="pre caml-input"> name_of_binary_digit 0;;</div>
<div class="pre caml-output ok">- : string = <span class="ocamlstring">"zero"</span></div></div>
<div class="ocaml">
<div class="pre caml-input"> name_of_binary_digit (-1);;</div>
<div class="pre caml-output ok">- : string = <span class="ocamlstring">"not a binary digit"</span></div></div>
</div><p>The <span class="c003">with</span> part does pattern matching on the
exception value with the same syntax and behavior as <span class="c003">match</span>. Thus,
several exceptions can be caught by one
<span class="c003">try</span>…<span class="c003">with</span> construct:
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> <span class="ocamlkeyword">rec</span> first_named_value values names =
<span class="ocamlkeyword">try</span>
List.assoc (head values) names
<span class="ocamlkeyword">with</span>
| Empty_list -> <span class="ocamlstring">"no named value"</span>
| Not_found -> first_named_value (List.tl values) names;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> first_named_value : 'a list -> ('a * string) list -> string = <<span class="ocamlkeyword">fun</span>></div></div>
<div class="ocaml">
<div class="pre caml-input"> first_named_value [ 0; 10 ] [ 1, <span class="ocamlstring">"one"</span>; 10, <span class="ocamlstring">"ten"</span>];;</div>
<div class="pre caml-output ok">- : string = <span class="ocamlstring">"ten"</span></div></div>
</div><p>Also, finalization can be performed by
trapping all exceptions, performing the finalization, then re-raising
the exception:
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> temporarily_set_reference <span class="ocamlkeyword">ref</span> newval funct =
<span class="ocamlkeyword">let</span> oldval = !<span class="ocamlkeyword">ref</span> <span class="ocamlkeyword">in</span>
<span class="ocamlkeyword">try</span>
<span class="ocamlkeyword">ref</span> := newval;
<span class="ocamlkeyword">let</span> res = funct () <span class="ocamlkeyword">in</span>
<span class="ocamlkeyword">ref</span> := oldval;
res
<span class="ocamlkeyword">with</span> x ->
<span class="ocamlkeyword">ref</span> := oldval;
raise x;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> temporarily_set_reference : 'a <span class="ocamlkeyword">ref</span> -> 'a -> (unit -> 'b) -> 'b = <<span class="ocamlkeyword">fun</span>></div></div>
</div><p>An alternative to <span class="c003">try</span>…<span class="c003">with</span> is to catch the exception while
pattern matching:
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> assoc_may_map f x l =
<span class="ocamlkeyword">match</span> List.assoc x l <span class="ocamlkeyword">with</span>
| <span class="ocamlkeyword">exception</span> Not_found -> None
| y -> f y;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> assoc_may_map : ('a -> 'b option) -> 'c -> ('c * 'a) list -> 'b option =
<<span class="ocamlkeyword">fun</span>></div></div>
</div><p>
Note that this construction is only useful if the exception is raised
between <span class="c003">match</span>…<span class="c003">with</span>. Exception patterns can be combined
with ordinary patterns at the toplevel,
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> flat_assoc_opt x l =
<span class="ocamlkeyword">match</span> List.assoc x l <span class="ocamlkeyword">with</span>
| None | <span class="ocamlkeyword">exception</span> Not_found -> None
| Some _ <span class="ocamlkeyword">as</span> v -> v;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> flat_assoc_opt : 'a -> ('a * 'b option) list -> 'b option = <<span class="ocamlkeyword">fun</span>></div></div>
</div><p>
but they cannot be nested inside other patterns. For instance,
the pattern <span class="c003">Some (exception A)</span> is invalid.</p><p>When exceptions are used as a control structure, it can be useful to make
them as local as possible by using a locally defined exception.
For instance, with
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> fixpoint f x =
<span class="ocamlkeyword">let</span> <span class="ocamlkeyword">exception</span> Done <span class="ocamlkeyword">in</span>
<span class="ocamlkeyword">let</span> x = <span class="ocamlkeyword">ref</span> x <span class="ocamlkeyword">in</span>
<span class="ocamlkeyword">try</span> <span class="ocamlkeyword">while</span> <span class="ocamlkeyword">true</span> <span class="ocamlkeyword">do</span>
<span class="ocamlkeyword">let</span> y = f !x <span class="ocamlkeyword">in</span>
<span class="ocamlkeyword">if</span> !x = y <span class="ocamlkeyword">then</span> raise Done <span class="ocamlkeyword">else</span> x := y
<span class="ocamlkeyword">done</span>; <span class="ocamlkeyword">assert</span> <span class="ocamlkeyword">false</span>
<span class="ocamlkeyword">with</span> Done -> !x;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> fixpoint : ('a -> 'a) -> 'a -> 'a = <<span class="ocamlkeyword">fun</span>></div></div>
</div><p>
the function <span class="c003">f</span> cannot raise a <span class="c003">Done</span> exception, which removes an
entire class of misbehaving functions.</p>
<h2 class="section" id="s:lazy-expr"><a class="section-anchor" href="#s:lazy-expr" aria-hidden="true"></a>1.7 Lazy expressions</h2>
<p>OCaml allows us to defer some computation until later when we need the result of
that computation. </p><p>We use <span class="c003">lazy (expr)</span> to delay the evaluation of some expression <span class="c003">expr</span>. For
example, we can defer the computation of <span class="c003">1+1</span> until we need the result of that
expression, <span class="c003">2</span>. Let us see how we initialize a lazy expression. </p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> lazy_two = <span class="ocamlkeyword">lazy</span> ( print_endline <span class="ocamlstring">"lazy_two evaluation"</span>; 1 + 1 );;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> lazy_two : int lazy_t = <<span class="ocamlkeyword">lazy</span>></div></div>
</div><p>We added <span class="c003">print_endline "lazy_two evaluation"</span> to see when the lazy
expression is being evaluated.</p><p>The value of <span class="c003">lazy_two</span> is displayed as <span class="c003"><lazy></span>, which means the expression
has not been evaluated yet, and its final value is unknown.</p><p>Note that <span class="c003">lazy_two</span> has type <span class="c003">int lazy_t</span>. However, the type <span class="c003">'a lazy_t</span> is an
internal type name, so the type <span class="c003">'a Lazy.t</span> should be preferred when possible.</p><p>When we finally need the result of a lazy expression, we can call <span class="c003">Lazy.force</span>
on that expression to force its evaluation. The function <span class="c003">force</span> comes from
standard-library module <a href="libref/Lazy.html"><span class="c003">Lazy</span></a>.</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> Lazy.force lazy_two;;</div>
<div class="pre caml-output ok">lazy_two evaluation
- : int = 2</div></div>
</div><p>Notice that our function call above prints “lazy_two evaluation” and then
returns the plain value of the computation. </p><p>Now if we look at the value of <span class="c003">lazy_two</span>, we see that it is not displayed as
<span class="c003"><lazy></span> anymore but as <span class="c003">lazy 2</span>.</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> lazy_two;;</div>
<div class="pre caml-output ok">- : int lazy_t = <span class="ocamlkeyword">lazy</span> 2</div></div>
</div><p>This is because <span class="c003">Lazy.force</span> memoizes the result of the forced expression. In other
words, every subsequent call of <span class="c003">Lazy.force</span> on that expression returns the
result of the first computation without recomputing the lazy expression. Let us
force <span class="c003">lazy_two</span> once again. </p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> Lazy.force lazy_two;;</div>
<div class="pre caml-output ok">- : int = 2</div></div>
</div><p>The expression is not evaluated this time; notice that “lazy_two evaluation” is
not printed. The result of the initial computation is simply returned. </p><p>Lazy patterns provide another way to force a lazy expression. </p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> lazy_l = <span class="ocamlkeyword">lazy</span> ([1; 2] @ [3; 4]);;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> lazy_l : int list lazy_t = <<span class="ocamlkeyword">lazy</span>></div></div>
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> <span class="ocamlkeyword">lazy</span> l = lazy_l;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> l : int list = [1; 2; 3; 4]</div></div>
</div><p>We can also use lazy patterns in pattern matching.</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> maybe_eval lazy_guard lazy_expr =
<span class="ocamlkeyword">match</span> lazy_guard, lazy_expr <span class="ocamlkeyword">with</span>
| <span class="ocamlkeyword">lazy</span> <span class="ocamlkeyword">false</span>, _ -> <span class="ocamlstring">"matches if (Lazy.force lazy_guard = false); lazy_expr not forced"</span>
| <span class="ocamlkeyword">lazy</span> <span class="ocamlkeyword">true</span>, <span class="ocamlkeyword">lazy</span> _ -> <span class="ocamlstring">"matches if (Lazy.force lazy_guard = true); lazy_expr forced"</span>;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> maybe_eval : bool lazy_t -> 'a lazy_t -> string = <<span class="ocamlkeyword">fun</span>></div></div>
</div><p>The lazy expression <span class="c003">lazy_expr</span> is forced only if the <span class="c003">lazy_guard</span> value yields
<span class="c003">true</span> once computed. Indeed, a simple wildcard pattern (not lazy) never forces
the lazy expression’s evaluation. However, a pattern with keyword <span class="c003">lazy</span>, even
if it is wildcard, always forces the evaluation of the deferred computation.</p>
<h2 class="section" id="s:symb-expr"><a class="section-anchor" href="#s:symb-expr" aria-hidden="true"></a>1.8 Symbolic processing of expressions</h2>
<p>We finish this introduction with a more complete example
representative of the use of OCaml for symbolic processing: formal
manipulations of arithmetic expressions containing variables. The
following variant type describes the expressions we shall manipulate:
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">type</span> expression =
Const <span class="ocamlkeyword">of</span> float
| Var <span class="ocamlkeyword">of</span> string
| Sum <span class="ocamlkeyword">of</span> expression * expression <span class="ocamlcomment">(* e1 + e2 *)</span>
| Diff <span class="ocamlkeyword">of</span> expression * expression <span class="ocamlcomment">(* e1 - e2 *)</span>
| Prod <span class="ocamlkeyword">of</span> expression * expression <span class="ocamlcomment">(* e1 * e2 *)</span>
| Quot <span class="ocamlkeyword">of</span> expression * expression <span class="ocamlcomment">(* e1 / e2 *)</span>
;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">type</span> expression =
Const <span class="ocamlkeyword">of</span> float
| Var <span class="ocamlkeyword">of</span> string
| Sum <span class="ocamlkeyword">of</span> expression * expression
| Diff <span class="ocamlkeyword">of</span> expression * expression
| Prod <span class="ocamlkeyword">of</span> expression * expression
| Quot <span class="ocamlkeyword">of</span> expression * expression</div></div>
</div><p>We first define a function to evaluate an expression given an
environment that maps variable names to their values. For simplicity,
the environment is represented as an association list.
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">exception</span> Unbound_variable <span class="ocamlkeyword">of</span> string;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">exception</span> Unbound_variable <span class="ocamlkeyword">of</span> string</div></div>
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> <span class="ocamlkeyword">rec</span> eval env exp =
<span class="ocamlkeyword">match</span> exp <span class="ocamlkeyword">with</span>
Const c -> c
| Var v ->
(<span class="ocamlkeyword">try</span> List.assoc v env <span class="ocamlkeyword">with</span> Not_found -> raise (Unbound_variable v))
| Sum(f, g) -> eval env f +. eval env g
| Diff(f, g) -> eval env f -. eval env g
| Prod(f, g) -> eval env f *. eval env g
| Quot(f, g) -> eval env f /. eval env g;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> eval : (string * float) list -> expression -> float = <<span class="ocamlkeyword">fun</span>></div></div>
<div class="ocaml">
<div class="pre caml-input"> eval [(<span class="ocamlstring">"x"</span>, 1.0); (<span class="ocamlstring">"y"</span>, 3.14)] (Prod(Sum(Var <span class="ocamlstring">"x"</span>, Const 2.0), Var <span class="ocamlstring">"y"</span>));;</div>
<div class="pre caml-output ok">- : float = 9.42</div></div>
</div><p>Now for a real symbolic processing, we define the derivative of an
expression with respect to a variable <span class="c003">dv</span>:
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> <span class="ocamlkeyword">rec</span> deriv exp dv =
<span class="ocamlkeyword">match</span> exp <span class="ocamlkeyword">with</span>
Const c -> Const 0.0
| Var v -> <span class="ocamlkeyword">if</span> v = dv <span class="ocamlkeyword">then</span> Const 1.0 <span class="ocamlkeyword">else</span> Const 0.0
| Sum(f, g) -> Sum(deriv f dv, deriv g dv)
| Diff(f, g) -> Diff(deriv f dv, deriv g dv)
| Prod(f, g) -> Sum(Prod(f, deriv g dv), Prod(deriv f dv, g))
| Quot(f, g) -> Quot(Diff(Prod(deriv f dv, g), Prod(f, deriv g dv)),
Prod(g, g))
;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> deriv : expression -> string -> expression = <<span class="ocamlkeyword">fun</span>></div></div>
<div class="ocaml">
<div class="pre caml-input"> deriv (Quot(Const 1.0, Var <span class="ocamlstring">"x"</span>)) <span class="ocamlstring">"x"</span>;;</div>
<div class="pre caml-output ok">- : expression =
Quot (Diff (Prod (Const 0., Var <span class="ocamlstring">"x"</span>), Prod (Const 1., Const 1.)),
Prod (Var <span class="ocamlstring">"x"</span>, Var <span class="ocamlstring">"x"</span>))</div></div>
</div>
<h2 class="section" id="s:pretty-printing"><a class="section-anchor" href="#s:pretty-printing" aria-hidden="true"></a>1.9 Pretty-printing</h2>
<p>As shown in the examples above, the internal representation (also
called <em>abstract syntax</em>) of expressions quickly becomes hard to
read and write as the expressions get larger. We need a printer and a
parser to go back and forth between the abstract syntax and the <em>concrete syntax</em>, which in the case of expressions is the familiar
algebraic notation (e.g. <span class="c003">2*x+1</span>).</p><p>For the printing function, we take into account the usual precedence
rules (i.e. <span class="c003">*</span> binds tighter than <span class="c003">+</span>) to avoid printing unnecessary
parentheses. To this end, we maintain the current operator precedence
and print parentheses around an operator only if its precedence is
less than the current precedence.
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> print_expr exp =
<span class="ocamlcomment">(* Local function definitions *)</span>
<span class="ocamlkeyword">let</span> open_paren prec op_prec =
<span class="ocamlkeyword">if</span> prec > op_prec <span class="ocamlkeyword">then</span> print_string <span class="ocamlstring">"("</span> <span class="ocamlkeyword">in</span>
<span class="ocamlkeyword">let</span> close_paren prec op_prec =
<span class="ocamlkeyword">if</span> prec > op_prec <span class="ocamlkeyword">then</span> print_string <span class="ocamlstring">")"</span> <span class="ocamlkeyword">in</span>
<span class="ocamlkeyword">let</span> <span class="ocamlkeyword">rec</span> print prec exp = <span class="ocamlcomment">(* prec is the current precedence *)</span>
<span class="ocamlkeyword">match</span> exp <span class="ocamlkeyword">with</span>
Const c -> print_float c
| Var v -> print_string v
| Sum(f, g) ->
open_paren prec 0;
print 0 f; print_string <span class="ocamlstring">" + "</span>; print 0 g;
close_paren prec 0
| Diff(f, g) ->
open_paren prec 0;
print 0 f; print_string <span class="ocamlstring">" - "</span>; print 1 g;
close_paren prec 0
| Prod(f, g) ->
open_paren prec 2;
print 2 f; print_string <span class="ocamlstring">" * "</span>; print 2 g;
close_paren prec 2
| Quot(f, g) ->
open_paren prec 2;
print 2 f; print_string <span class="ocamlstring">" / "</span>; print 3 g;
close_paren prec 2
<span class="ocamlkeyword">in</span> print 0 exp;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> print_expr : expression -> unit = <<span class="ocamlkeyword">fun</span>></div></div>
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> e = Sum(Prod(Const 2.0, Var <span class="ocamlstring">"x"</span>), Const 1.0);;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> e : expression = Sum (Prod (Const 2., Var <span class="ocamlstring">"x"</span>), Const 1.)</div></div>
<div class="ocaml">
<div class="pre caml-input"> print_expr e; print_newline ();;</div>
<div class="pre caml-output ok">2. * x + 1.
- : unit = ()</div></div>
<div class="ocaml">
<div class="pre caml-input"> print_expr (deriv e <span class="ocamlstring">"x"</span>); print_newline ();;</div>
<div class="pre caml-output ok">2. * 1. + 0. * x + 0.
- : unit = ()</div></div>
</div>
<h2 class="section" id="s:printf"><a class="section-anchor" href="#s:printf" aria-hidden="true"></a>1.10 Printf formats</h2>
<p>There is a <span class="c003">printf</span> function in the <a href="libref/Printf.html"><span class="c003">Printf</span></a> module
(see chapter <a href="moduleexamples.html#c%3Amoduleexamples">2</a>) that allows you to make formatted
output more concisely.
It follows the behavior of the <span class="c003">printf</span> function from the C standard library.
The <span class="c003">printf</span> function takes a format string that describes the desired output
as a text interspered with specifiers (for instance <span class="c003">%d</span>, <span class="c003">%f</span>).
Next, the specifiers are substituted by the following arguments in their order
of apparition in the format string:
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> Printf.printf <span class="ocamlstring">"%i + %i is an integer value, %F * %F is a float, %S\n"</span>
3 2 4.5 1. <span class="ocamlstring">"this is a string"</span>;;</div>
<div class="pre caml-output ok">3 + 2 is an integer value, 4.5 * 1. is a float, <span class="ocamlstring">"this is a string"</span>
- : unit = ()</div></div>
</div><p>
The OCaml type system checks that the type of the arguments and the specifiers are
compatible. If you pass it an argument of a type that does not correspond to
the format specifier, the compiler will display an error message:
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> Printf.printf <span class="ocamlstring">"Float value: %F"</span> <span class="ocamlhighlight">42</span>;;</div>
<div class="pre caml-output error"><span class="ocamlerror">Error</span>: This expression has type int but an expression was expected of type
float
Hint: Did you mean `42.'?</div></div>
</div><p>
The <span class="c003">fprintf</span> function is like <span class="c003">printf</span> except that it takes an output channel as
the first argument. The <span class="c003">%a</span> specifier can be useful to define custom printer
(for custom types). For instance, we can create a printing template that converts
an integer argument to signed decimal:
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> pp_int ppf n = Printf.fprintf ppf <span class="ocamlstring">"%d"</span> n;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> pp_int : out_channel -> int -> unit = <<span class="ocamlkeyword">fun</span>></div></div>
<div class="ocaml">
<div class="pre caml-input"> Printf.printf <span class="ocamlstring">"Outputting an integer using a custom printer: %a "</span> pp_int 42;;</div>
<div class="pre caml-output ok">Outputting an integer using a custom printer: 42 - : unit = ()</div></div>
</div><p>
The advantage of those printers based on the <span class="c003">%a</span> specifier is that they can be
composed together to create more complex printers step by step.
We can define a combinator that can turn a printer for <span class="c003">'a</span> type into a printer
for <span class="c003">'a optional</span>:
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> pp_option printer ppf = <span class="ocamlkeyword">function</span>
| None -> Printf.fprintf ppf <span class="ocamlstring">"None"</span>
| Some v -> Printf.fprintf ppf <span class="ocamlstring">"Some(%a)"</span> printer v;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> pp_option :
(out_channel -> 'a -> unit) -> out_channel -> 'a option -> unit = <<span class="ocamlkeyword">fun</span>></div></div>
<div class="ocaml">
<div class="pre caml-input"> Printf.fprintf stdout
<span class="ocamlstring">"The current setting is %a. \nThere is only %a\n"</span>
(pp_option pp_int) (Some 3)
(pp_option pp_int) None
;;</div>
<div class="pre caml-output ok">The current setting is Some(3).
There is only None
- : unit = ()</div></div>
</div><p>
If the value of its argument its <span class="c003">None</span>, the printer returned by pp_option
printer prints <span class="c003">None</span> otherwise it uses the provided printer to print <span class="c003">Some </span>.</p><p>Here is how to rewrite the pretty-printer using <span class="c003">fprintf</span>:
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> pp_expr ppf expr =
<span class="ocamlkeyword">let</span> open_paren prec op_prec output =
<span class="ocamlkeyword">if</span> prec > op_prec <span class="ocamlkeyword">then</span> Printf.fprintf output <span class="ocamlstring">"%s"</span> <span class="ocamlstring">"("</span> <span class="ocamlkeyword">in</span>
<span class="ocamlkeyword">let</span> close_paren prec op_prec output =
<span class="ocamlkeyword">if</span> prec > op_prec <span class="ocamlkeyword">then</span> Printf.fprintf output <span class="ocamlstring">"%s"</span> <span class="ocamlstring">")"</span> <span class="ocamlkeyword">in</span>
<span class="ocamlkeyword">let</span> <span class="ocamlkeyword">rec</span> print prec ppf expr =
<span class="ocamlkeyword">match</span> expr <span class="ocamlkeyword">with</span>
| Const c -> Printf.fprintf ppf <span class="ocamlstring">"%F"</span> c
| Var v -> Printf.fprintf ppf <span class="ocamlstring">"%s"</span> v
| Sum(f, g) ->
open_paren prec 0 ppf;
Printf.fprintf ppf <span class="ocamlstring">"%a + %a"</span> (print 0) f (print 0) g;
close_paren prec 0 ppf
| Diff(f, g) ->
open_paren prec 0 ppf;
Printf.fprintf ppf <span class="ocamlstring">"%a - %a"</span> (print 0) f (print 1) g;
close_paren prec 0 ppf
| Prod(f, g) ->
open_paren prec 2 ppf;
Printf.fprintf ppf <span class="ocamlstring">"%a * %a"</span> (print 2) f (print 2) g;
close_paren prec 2 ppf
| Quot(f, g) ->
open_paren prec 2 ppf;
Printf.fprintf ppf <span class="ocamlstring">"%a / %a"</span> (print 2) f (print 3) g;
close_paren prec 2 ppf
<span class="ocamlkeyword">in</span> print 0 ppf expr;;</div>
<div class="pre caml-output ok"><span class="ocamlkeyword">val</span> pp_expr : out_channel -> expression -> unit = <<span class="ocamlkeyword">fun</span>></div></div>
<div class="ocaml">
<div class="pre caml-input"> pp_expr stdout e; print_newline ();;</div>
<div class="pre caml-output ok">2. * x + 1.
- : unit = ()</div></div>
<div class="ocaml">
<div class="pre caml-input"> pp_expr stdout (deriv e <span class="ocamlstring">"x"</span>); print_newline ();;</div>
<div class="pre caml-output ok">2. * 1. + 0. * x + 0.
- : unit = ()</div></div>
</div><p>Due to the way that format string are build, storing a format string requires
an explicit type annotation:
</p><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> <span class="ocamlkeyword">let</span> str : _ format =
<span class="ocamlstring">"%i is an integer value, %F is a float, %S\n"</span>;;</div></div>
</div><div class="caml-example toplevel">
<div class="ocaml">
<div class="pre caml-input"> Printf.printf str 3 4.5 <span class="ocamlstring">"string value"</span>;;</div>
<div class="pre caml-output ok">3 is an integer value, 4.5 is a float, <span class="ocamlstring">"string value"</span>
- : unit = ()</div></div>
</div>
<h2 class="section" id="s:standalone-programs"><a class="section-anchor" href="#s:standalone-programs" aria-hidden="true"></a>1.11 Standalone OCaml programs</h2>
<p>All examples given so far were executed under the interactive system.
OCaml code can also be compiled separately and executed
non-interactively using the batch compilers <span class="c003">ocamlc</span> and <span class="c003">ocamlopt</span>.
The source code must be put in a file with extension <span class="c003">.ml</span>. It
consists of a sequence of phrases, which will be evaluated at runtime
in their order of appearance in the source file. Unlike in interactive
mode, types and values are not printed automatically; the program must
call printing functions explicitly to produce some output. The <span class="c003">;;</span> used
in the interactive examples is not required in
source files created for use with OCaml compilers, but can be helpful
to mark the end of a top-level expression unambiguously even when
there are syntax errors.
Here is a
sample standalone program to print the greatest common divisor
(gcd) of two numbers:
</p><pre>(* File gcd.ml *)
let rec gcd a b =
if b = 0 then a
else gcd b (a mod b);;
let main () =
let a = int_of_string Sys.argv.(1) in
let b = int_of_string Sys.argv.(2) in
Printf.printf "%d\n" (gcd a b);
exit 0;;
main ();;
</pre><p><span class="c003">Sys.argv</span> is an array of strings containing the command-line
parameters. <span class="c003">Sys.argv.(1)</span> is thus the first command-line parameter.
The program above is compiled and executed with the following shell
commands:
</p><pre>$ ocamlc -o gcd gcd.ml
$ ./gcd 6 9
3
$ ./fib 7 11
1
</pre><p>
More complex standalone OCaml programs are typically composed of
multiple source files, and can link with precompiled libraries.
Chapters <a href="comp.html#c%3Acamlc">9</a> and <a href="native.html#c%3Anativecomp">12</a> explain how to use the
batch compilers <span class="c003">ocamlc</span> and <span class="c003">ocamlopt</span>. Recompilation of
multi-file OCaml projects can be automated using third-party
build systems, such as the
<a href="https://github.com/ocaml/ocamlbuild/">ocamlbuild</a>
compilation manager.
</p>
<hr>
<a href="foreword.html"><img src="previous_motif.svg" alt="Previous"></a>
<a href="index.html"><img src="contents_motif.svg" alt="Up"></a>
<a href="moduleexamples.html"><img src="next_motif.svg" alt="Next"></a>
</body>
</html>
|