/usr/share/doc/slib/slib_3.html is in slib 3b1-3.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html401/loose.dtd">
<html>
<!-- This manual is for SLIB (version 3b1, June 2010),
the portable Scheme library.
Copyright C 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.2 or
any later version published by the Free Software Foundation; with no
Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A
copy of the license is included in the section entitled "GNU Free
Documentation License."
-->
<!-- Created on June 9, 2010 by texi2html 1.82
texi2html was written by:
Lionel Cons <Lionel.Cons@cern.ch> (original author)
Karl Berry <karl@freefriends.org>
Olaf Bachmann <obachman@mathematik.uni-kl.de>
and many others.
Maintained by: Many creative people.
Send bugs and suggestions to <texi2html-bug@nongnu.org>
-->
<head>
<title>slib: 3. Scheme Syntax Extension Packages</title>
<meta name="description" content="slib: 3. Scheme Syntax Extension Packages">
<meta name="keywords" content="slib: 3. Scheme Syntax Extension Packages">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="texi2html 1.82">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.smallquotation {font-size: smaller}
pre.display {font-family: serif}
pre.format {font-family: serif}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
pre.smalldisplay {font-family: serif; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: serif; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.roman {font-family:serif; font-weight:normal;}
span.sansserif {font-family:sans-serif; font-weight:normal;}
ul.toc {list-style: none}
-->
</style>
</head>
<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
<a name="Scheme-Syntax-Extension-Packages"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="slib_2.html#Legacy" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Defmacro" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="slib_2.html#Universal-SLIB-Procedures" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="slib.html#Top" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="slib_4.html#Textual-Conversion-Packages" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="slib.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="slib_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="slib_10.html#Procedure-and-Macro-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="slib_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Scheme-Syntax-Extension-Packages-1"></a>
<h1 class="chapter">3. Scheme Syntax Extension Packages</h1>
<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top"><a href="#Defmacro">3.1 Defmacro</a></td><td> </td><td align="left" valign="top"> Supported by all implementations
</td></tr>
<tr><td align="left" valign="top"><a href="#R4RS-Macros">3.2 R4RS Macros</a></td><td> </td><td align="left" valign="top"> ’macro
</td></tr>
<tr><td align="left" valign="top"><a href="#Macro-by-Example">3.3 Macro by Example</a></td><td> </td><td align="left" valign="top"> ’macro-by-example
</td></tr>
<tr><td align="left" valign="top"><a href="#Macros-That-Work">3.4 Macros That Work</a></td><td> </td><td align="left" valign="top"> ’macros-that-work
</td></tr>
<tr><td align="left" valign="top"><a href="#Syntactic-Closures">3.5 Syntactic Closures</a></td><td> </td><td align="left" valign="top"> ’syntactic-closures
</td></tr>
<tr><td align="left" valign="top"><a href="#Syntax_002dCase-Macros">3.6 Syntax-Case Macros</a></td><td> </td><td align="left" valign="top"> ’syntax-case
</td></tr>
<tr><th colspan="3" align="left" valign="top"><pre class="menu-comment">
Syntax extensions (macros) included with SLIB.
</pre></th></tr><tr><td align="left" valign="top"><a href="#Define_002dStructure">3.7 Define-Structure</a></td><td> </td><td align="left" valign="top"> ’structure
</td></tr>
<tr><td align="left" valign="top"><a href="#Define_002dRecord_002dType">3.8 Define-Record-Type</a></td><td> </td><td align="left" valign="top"> ’define-record-type, ’srfi-9
</td></tr>
<tr><td align="left" valign="top"><a href="#Fluid_002dLet">3.9 Fluid-Let</a></td><td> </td><td align="left" valign="top"> ’fluid-let
</td></tr>
<tr><td align="left" valign="top"><a href="#Binding-to-multiple-values">3.10 Binding to multiple values</a></td><td> </td><td align="left" valign="top"> ’receive, ’srfi-8
</td></tr>
<tr><td align="left" valign="top"><a href="#Guarded-LET_002a-special-form">3.11 Guarded LET* special form</a></td><td> </td><td align="left" valign="top"> ’and-let*, ’srfi-2
</td></tr>
<tr><td align="left" valign="top"><a href="#Guarded-COND-Clause">3.12 Guarded COND Clause</a></td><td> </td><td align="left" valign="top"> ’guarded-cond-clause, ’srfi-61
</td></tr>
<tr><td align="left" valign="top"><a href="#Yasos">3.13 Yasos</a></td><td> </td><td align="left" valign="top"> ’yasos, ’oop, ’collect
</td></tr>
</table>
<hr size="6">
<a name="Defmacro"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Scheme-Syntax-Extension-Packages" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Defmacroexpand" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Scheme-Syntax-Extension-Packages" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Scheme-Syntax-Extension-Packages" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="slib_4.html#Textual-Conversion-Packages" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="slib.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="slib_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="slib_10.html#Procedure-and-Macro-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="slib_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Defmacro-1"></a>
<h2 class="section">3.1 Defmacro</h2>
<p>Defmacros are supported by all implementations.
</p>
<dl>
<dt><a name="index-gentemp"></a><u>Function:</u> <b>gentemp</b></dt>
<dd><p>Returns a new (interned) symbol each time it is called. The symbol
names are implementation-dependent
</p><table><tr><td> </td><td><pre class="lisp">(gentemp) ⇒ scm:G0
(gentemp) ⇒ scm:G1
</pre></td></tr></table>
</dd></dl>
<dl>
<dt><a name="index-defmacro_003aeval"></a><u>Function:</u> <b>defmacro:eval</b><i> e</i></dt>
<dd><p>Returns the <code>slib:eval</code> of expanding all defmacros in scheme
expression <var>e</var>.
</p></dd></dl>
<dl>
<dt><a name="index-defmacro_003aload"></a><u>Function:</u> <b>defmacro:load</b><i> filename</i></dt>
<dd><p><var>filename</var> should be a string. If filename names an existing file,
the <code>defmacro:load</code> procedure reads Scheme source code expressions
and definitions from the file and evaluates them sequentially. These
source code expressions and definitions may contain defmacro
definitions. The <code>macro:load</code> procedure does not affect the values
returned by <code>current-input-port</code> and
<code>current-output-port</code>.
</p></dd></dl>
<dl>
<dt><a name="index-defmacro_003f"></a><u>Function:</u> <b>defmacro?</b><i> sym</i></dt>
<dd><p>Returns <code>#t</code> if <var>sym</var> has been defined by <code>defmacro</code>,
<code>#f</code> otherwise.
</p></dd></dl>
<dl>
<dt><a name="index-macroexpand_002d1"></a><u>Function:</u> <b>macroexpand-1</b><i> form</i></dt>
<dt><a name="index-macroexpand"></a><u>Function:</u> <b>macroexpand</b><i> form</i></dt>
<dd><p>If <var>form</var> is a macro call, <code>macroexpand-1</code> will expand the
macro call once and return it. A <var>form</var> is considered to be a macro
call only if it is a cons whose <code>car</code> is a symbol for which a
<code>defmacro</code> has been defined.
</p>
<p><code>macroexpand</code> is similar to <code>macroexpand-1</code>, but repeatedly
expands <var>form</var> until it is no longer a macro call.
</p></dd></dl>
<dl>
<dt><a name="index-defmacro-1"></a><u>Macro:</u> <b>defmacro</b><i> name lambda-list form …</i></dt>
<dd><p>When encountered by <code>defmacro:eval</code>, <code>defmacro:macroexpand*</code>,
or <code>defmacro:load</code> defines a new macro which will henceforth be
expanded when encountered by <code>defmacro:eval</code>,
<code>defmacro:macroexpand*</code>, or <code>defmacro:load</code>.
</p></dd></dl>
<hr size="6">
<a name="Defmacroexpand"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Defmacro" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#R4RS-Macros" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Scheme-Syntax-Extension-Packages" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Defmacro" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="slib_4.html#Textual-Conversion-Packages" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="slib.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="slib_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="slib_10.html#Procedure-and-Macro-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="slib_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<h3 class="subsection">3.1.1 Defmacroexpand</h3>
<p><code>(require 'defmacroexpand)</code>
<a name="index-defmacroexpand"></a>
</p>
<dl>
<dt><a name="index-defmacro_003aexpand_002a"></a><u>Function:</u> <b>defmacro:expand*</b><i> e</i></dt>
<dd><p>Returns the result of expanding all defmacros in scheme expression
<var>e</var>.
</p></dd></dl>
<hr size="6">
<a name="R4RS-Macros"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Defmacroexpand" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Macro-by-Example" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Scheme-Syntax-Extension-Packages" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Scheme-Syntax-Extension-Packages" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="slib_4.html#Textual-Conversion-Packages" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="slib.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="slib_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="slib_10.html#Procedure-and-Macro-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="slib_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="R4RS-Macros-1"></a>
<h2 class="section">3.2 R4RS Macros</h2>
<p><code>(require 'macro)</code> is the appropriate call if you want R4RS
<a name="index-macro-1"></a>
high-level macros but don’t care about the low level implementation. If
an SLIB R4RS macro implementation is already loaded it will be used.
Otherwise, one of the R4RS macros implemetations is loaded.
</p>
<p>The SLIB R4RS macro implementations support the following uniform
interface:
</p>
<dl>
<dt><a name="index-macro_003aexpand"></a><u>Function:</u> <b>macro:expand</b><i> sexpression</i></dt>
<dd><p>Takes an R4RS expression, macro-expands it, and returns the result of
the macro expansion.
</p></dd></dl>
<dl>
<dt><a name="index-macro_003aeval"></a><u>Function:</u> <b>macro:eval</b><i> sexpression</i></dt>
<dd><p>Takes an R4RS expression, macro-expands it, evals the result of the
macro expansion, and returns the result of the evaluation.
</p></dd></dl>
<dl>
<dt><a name="index-macro_003aload"></a><u>Procedure:</u> <b>macro:load</b><i> filename</i></dt>
<dd><p><var>filename</var> should be a string. If filename names an existing file,
the <code>macro:load</code> procedure reads Scheme source code expressions and
definitions from the file and evaluates them sequentially. These source
code expressions and definitions may contain macro definitions. The
<code>macro:load</code> procedure does not affect the values returned by
<code>current-input-port</code> and <code>current-output-port</code>.
</p></dd></dl>
<hr size="6">
<a name="Macro-by-Example"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#R4RS-Macros" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Caveat" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Scheme-Syntax-Extension-Packages" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Scheme-Syntax-Extension-Packages" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="slib_4.html#Textual-Conversion-Packages" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="slib.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="slib_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="slib_10.html#Procedure-and-Macro-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="slib_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Macro-by-Example-1"></a>
<h2 class="section">3.3 Macro by Example</h2>
<p><code>(require 'macro-by-example)</code>
<a name="index-macro_002dby_002dexample-1"></a>
</p>
<p>A vanilla implementation of <cite>Macro by Example</cite> (Eugene Kohlbecker,
R4RS) by Dorai Sitaram, (dorai @ cs.rice.edu) using <code>defmacro</code>.
</p>
<ul>
<li>
generating hygienic global <code>define-syntax</code> Macro-by-Example macros
<strong>cheaply</strong>.
</li><li>
can define macros which use <code>...</code>.
</li><li>
needn’t worry about a lexical variable in a macro definition
clashing with a variable from the macro use context
</li><li>
don’t suffer the overhead of redefining the repl if <code>defmacro</code>
natively supported (most implementations)
</li></ul>
<hr size="6">
<a name="Caveat"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Macro-by-Example" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Macros-That-Work" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Scheme-Syntax-Extension-Packages" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Macro-by-Example" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="slib_4.html#Textual-Conversion-Packages" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="slib.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="slib_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="slib_10.html#Procedure-and-Macro-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="slib_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<h3 class="subsection">3.3.1 Caveat</h3>
<p>These macros are not referentially transparent
(see <a href="../r4rs/Macros.html#Macros">(r4rs)Macros</a> section ‘Macros’ in <cite>Revised(4) Scheme</cite>). Lexically scoped macros
(i.e., <code>let-syntax</code> and <code>letrec-syntax</code>) are not supported.
In any case, the problem
of referential transparency gains poignancy only when <code>let-syntax</code>
and <code>letrec-syntax</code> are used. So you will not be courting
large-scale disaster unless you’re using system-function names as local
variables with unintuitive bindings that the macro can’t use. However,
if you must have the full <cite>r4rs</cite> macro functionality, look to the
more featureful (but also more expensive) versions of syntax-rules
available in slib <a href="#Macros-That-Work">Macros That Work</a>, <a href="#Syntactic-Closures">Syntactic Closures</a>, and
<a href="#Syntax_002dCase-Macros">Syntax-Case Macros</a>.
</p>
<dl>
<dt><a name="index-define_002dsyntax"></a><u>Macro:</u> <b>define-syntax</b><i> keyword transformer-spec</i></dt>
<dd><p>The <var>keyword</var> is an identifier, and the <var>transformer-spec</var>
should be an instance of <code>syntax-rules</code>.
</p>
<p>The top-level syntactic environment is extended by binding the
<var>keyword</var> to the specified transformer.
</p>
<table><tr><td> </td><td><pre class="example">(define-syntax let*
(syntax-rules ()
((let* () body1 body2 ...)
(let () body1 body2 ...))
((let* ((name1 val1) (name2 val2) ...)
body1 body2 ...)
(let ((name1 val1))
(let* (( name2 val2) ...)
body1 body2 ...)))))
</pre></td></tr></table>
</dd></dl>
<dl>
<dt><a name="index-syntax_002drules"></a><u>Macro:</u> <b>syntax-rules</b><i> literals syntax-rule …</i></dt>
<dd><p><var>literals</var> is a list of identifiers, and each <var>syntax-rule</var>
should be of the form
</p>
<p><code>(<var>pattern</var> <var>template</var>)</code>
</p>
<p>where the <var>pattern</var> and <var>template</var> are as in the grammar above.
</p>
<p>An instance of <code>syntax-rules</code> produces a new macro transformer by
specifying a sequence of hygienic rewrite rules. A use of a macro whose
keyword is associated with a transformer specified by
<code>syntax-rules</code> is matched against the patterns contained in the
<var>syntax-rule</var>s, beginning with the leftmost <var>syntax-rule</var>.
When a match is found, the macro use is trancribed hygienically
according to the template.
</p>
<p>Each pattern begins with the keyword for the macro. This keyword is not
involved in the matching and is not considered a pattern variable or
literal identifier.
</p></dd></dl>
<hr size="6">
<a name="Macros-That-Work"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Caveat" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Definitions" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Scheme-Syntax-Extension-Packages" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Scheme-Syntax-Extension-Packages" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="slib_4.html#Textual-Conversion-Packages" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="slib.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="slib_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="slib_10.html#Procedure-and-Macro-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="slib_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Macros-That-Work-1"></a>
<h2 class="section">3.4 Macros That Work</h2>
<p><code>(require 'macros-that-work)</code>
<a name="index-macros_002dthat_002dwork-1"></a>
</p>
<p><cite>Macros That Work</cite> differs from the other R4RS macro
implementations in that it does not expand derived expression types to
primitive expression types.
</p>
<dl>
<dt><a name="index-macro_003aexpand-1"></a><u>Function:</u> <b>macro:expand</b><i> expression</i></dt>
<dt><a name="index-macwork_003aexpand"></a><u>Function:</u> <b>macwork:expand</b><i> expression</i></dt>
<dd><p>Takes an R4RS expression, macro-expands it, and returns the result of
the macro expansion.
</p></dd></dl>
<dl>
<dt><a name="index-macro_003aeval-1"></a><u>Function:</u> <b>macro:eval</b><i> expression</i></dt>
<dt><a name="index-macwork_003aeval"></a><u>Function:</u> <b>macwork:eval</b><i> expression</i></dt>
<dd><p><code>macro:eval</code> returns the value of <var>expression</var> in the current
top level environment. <var>expression</var> can contain macro definitions.
Side effects of <var>expression</var> will affect the top level
environment.
</p></dd></dl>
<dl>
<dt><a name="index-macro_003aload-1"></a><u>Procedure:</u> <b>macro:load</b><i> filename</i></dt>
<dt><a name="index-macwork_003aload"></a><u>Procedure:</u> <b>macwork:load</b><i> filename</i></dt>
<dd><p><var>filename</var> should be a string. If filename names an existing file,
the <code>macro:load</code> procedure reads Scheme source code expressions and
definitions from the file and evaluates them sequentially. These source
code expressions and definitions may contain macro definitions. The
<code>macro:load</code> procedure does not affect the values returned by
<code>current-input-port</code> and <code>current-output-port</code>.
</p></dd></dl>
<p>References:
</p>
<p>The <cite>Revised^4 Report on the Algorithmic Language Scheme</cite> Clinger
and Rees [editors]. To appear in LISP Pointers. Also available as a
technical report from the University of Oregon, MIT AI Lab, and
Cornell.
</p>
<p align="center"> Macros That Work. Clinger and Rees. POPL ’91.
</p>
<p>The supported syntax differs from the R4RS in that vectors are allowed
as patterns and as templates and are not allowed as pattern or template
data.
</p>
<table><tr><td> </td><td><pre class="example">transformer spec → (syntax-rules literals rules)
rules → ()
| (rule . rules)
rule → (pattern template)
pattern → pattern_var ; a symbol not in literals
| symbol ; a symbol in literals
| ()
| (pattern . pattern)
| (ellipsis_pattern)
| #(pattern*) ; extends R4RS
| #(pattern* ellipsis_pattern) ; extends R4RS
| pattern_datum
template → pattern_var
| symbol
| ()
| (template2 . template2)
| #(template*) ; extends R4RS
| pattern_datum
template2 → template
| ellipsis_template
pattern_datum → string ; no vector
| character
| boolean
| number
ellipsis_pattern → pattern ...
ellipsis_template → template ...
pattern_var → symbol ; not in literals
literals → ()
| (symbol . literals)
</pre></td></tr></table>
<hr size="6">
<a name="Definitions"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Macros-That-Work" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Restrictions" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Scheme-Syntax-Extension-Packages" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Macros-That-Work" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="slib_4.html#Textual-Conversion-Packages" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="slib.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="slib_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="slib_10.html#Procedure-and-Macro-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="slib_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<h3 class="subsection">3.4.1 Definitions</h3>
<dl compact="compact">
<dt> Scope of an ellipsis</dt>
<dd><p>Within a pattern or template, the scope of an ellipsis (<code>...</code>) is
the pattern or template that appears to its left.
</p>
</dd>
<dt> Rank of a pattern variable</dt>
<dd><p>The rank of a pattern variable is the number of ellipses within whose
scope it appears in the pattern.
</p>
</dd>
<dt> Rank of a subtemplate</dt>
<dd><p>The rank of a subtemplate is the number of ellipses within whose scope
it appears in the template.
</p>
</dd>
<dt> Template rank of an occurrence of a pattern variable</dt>
<dd><p>The template rank of an occurrence of a pattern variable within a
template is the rank of that occurrence, viewed as a subtemplate.
</p>
</dd>
<dt> Variables bound by a pattern</dt>
<dd><p>The variables bound by a pattern are the pattern variables that appear
within it.
</p>
</dd>
<dt> Referenced variables of a subtemplate</dt>
<dd><p>The referenced variables of a subtemplate are the pattern variables that
appear within it.
</p>
</dd>
<dt> Variables opened by an ellipsis template</dt>
<dd><p>The variables opened by an ellipsis template are the referenced pattern
variables whose rank is greater than the rank of the ellipsis template.
</p>
</dd>
</dl>
<hr size="6">
<a name="Restrictions"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Definitions" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Syntactic-Closures" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Scheme-Syntax-Extension-Packages" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Macros-That-Work" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="slib_4.html#Textual-Conversion-Packages" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="slib.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="slib_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="slib_10.html#Procedure-and-Macro-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="slib_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<h3 class="subsection">3.4.2 Restrictions</h3>
<p>No pattern variable appears more than once within a pattern.
</p>
<p>For every occurrence of a pattern variable within a template, the
template rank of the occurrence must be greater than or equal to the
pattern variable’s rank.
</p>
<p>Every ellipsis template must open at least one variable.
</p>
<p>For every ellipsis template, the variables opened by an ellipsis
template must all be bound to sequences of the same length.
</p>
<p>The compiled form of a <var>rule</var> is
</p>
<table><tr><td> </td><td><pre class="example">rule → (pattern template inserted)
pattern → pattern_var
| symbol
| ()
| (pattern . pattern)
| ellipsis_pattern
| #(pattern)
| pattern_datum
template → pattern_var
| symbol
| ()
| (template2 . template2)
| #(pattern)
| pattern_datum
template2 → template
| ellipsis_template
pattern_datum → string
| character
| boolean
| number
pattern_var → #(V symbol rank)
ellipsis_pattern → #(E pattern pattern_vars)
ellipsis_template → #(E template pattern_vars)
inserted → ()
| (symbol . inserted)
pattern_vars → ()
| (pattern_var . pattern_vars)
rank → exact non-negative integer
</pre></td></tr></table>
<p>where V and E are unforgeable values.
</p>
<p>The pattern variables associated with an ellipsis pattern are the
variables bound by the pattern, and the pattern variables associated
with an ellipsis template are the variables opened by the ellipsis
template.
</p>
<p>If the template contains a big chunk that contains no pattern variables
or inserted identifiers, then the big chunk will be copied
unnecessarily. That shouldn’t matter very often.
</p>
<hr size="6">
<a name="Syntactic-Closures"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Restrictions" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Syntactic-Closure-Macro-Facility" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Scheme-Syntax-Extension-Packages" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Scheme-Syntax-Extension-Packages" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="slib_4.html#Textual-Conversion-Packages" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="slib.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="slib_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="slib_10.html#Procedure-and-Macro-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="slib_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Syntactic-Closures-1"></a>
<h2 class="section">3.5 Syntactic Closures</h2>
<p><code>(require 'syntactic-closures)</code>
<a name="index-syntactic_002dclosures-1"></a>
</p>
<dl>
<dt><a name="index-macro_003aexpand-2"></a><u>Function:</u> <b>macro:expand</b><i> expression</i></dt>
<dt><a name="index-synclo_003aexpand"></a><u>Function:</u> <b>synclo:expand</b><i> expression</i></dt>
<dd><p>Returns scheme code with the macros and derived expression types of
<var>expression</var> expanded to primitive expression types.
</p></dd></dl>
<dl>
<dt><a name="index-macro_003aeval-2"></a><u>Function:</u> <b>macro:eval</b><i> expression</i></dt>
<dt><a name="index-synclo_003aeval"></a><u>Function:</u> <b>synclo:eval</b><i> expression</i></dt>
<dd><p><code>macro:eval</code> returns the value of <var>expression</var> in the current
top level environment. <var>expression</var> can contain macro definitions.
Side effects of <var>expression</var> will affect the top level
environment.
</p></dd></dl>
<dl>
<dt><a name="index-macro_003aload-2"></a><u>Procedure:</u> <b>macro:load</b><i> filename</i></dt>
<dt><a name="index-synclo_003aload"></a><u>Procedure:</u> <b>synclo:load</b><i> filename</i></dt>
<dd><p><var>filename</var> should be a string. If filename names an existing file,
the <code>macro:load</code> procedure reads Scheme source code expressions and
definitions from the file and evaluates them sequentially. These
source code expressions and definitions may contain macro definitions.
The <code>macro:load</code> procedure does not affect the values returned by
<code>current-input-port</code> and <code>current-output-port</code>.
</p></dd></dl>
<hr size="6">
<a name="Syntactic-Closure-Macro-Facility"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Syntactic-Closures" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Terminology" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Scheme-Syntax-Extension-Packages" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Syntactic-Closures" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="slib_4.html#Textual-Conversion-Packages" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="slib.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="slib_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="slib_10.html#Procedure-and-Macro-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="slib_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<h3 class="subsection">3.5.1 Syntactic Closure Macro Facility</h3>
<p align="center"> A Syntactic Closures Macro Facility
</p><p align="center"> by Chris Hanson
</p><p align="center"> 9 November 1991
</p>
<p>This document describes <em>syntactic closures</em>, a low-level macro
facility for the Scheme programming language. The facility is an
alternative to the low-level macro facility described in the
<cite>Revised^4 Report on Scheme.</cite> This document is an addendum to that
report.
</p>
<p>The syntactic closures facility extends the BNF rule for
<var>transformer spec</var> to allow a new keyword that introduces a
low-level macro transformer:
</p>
<table><tr><td> </td><td><pre class="example"><var>transformer spec</var> := (transformer <var>expression</var>)
</pre></td></tr></table>
<p>Additionally, the following procedures are added:
</p><table><tr><td> </td><td><pre class="lisp">make-syntactic-closure
capture-syntactic-environment
identifier?
identifier=?
</pre></td></tr></table>
<p>The description of the facility is divided into three parts. The first
part defines basic terminology. The second part describes how macro
transformers are defined. The third part describes the use of
<em>identifiers</em>, which extend the syntactic closure mechanism to be
compatible with <code>syntax-rules</code>.
</p>
<hr size="6">
<a name="Terminology"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Syntactic-Closure-Macro-Facility" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Transformer-Definition" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Scheme-Syntax-Extension-Packages" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Syntactic-Closure-Macro-Facility" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="slib_4.html#Textual-Conversion-Packages" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="slib.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="slib_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="slib_10.html#Procedure-and-Macro-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="slib_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<h4 class="subsubsection">3.5.1.1 Terminology</h4>
<p>This section defines the concepts and data types used by the syntactic
closures facility.
</p>
<ul>
<li> <em>Forms</em> are the syntactic entities out of which programs are
recursively constructed. A form is any expression, any definition, any
syntactic keyword, or any syntactic closure. The variable name that
appears in a <code>set!</code> special form is also a form. Examples of
forms:
<table><tr><td> </td><td><pre class="lisp">17
#t
car
(+ x 4)
(lambda (x) x)
(define pi 3.14159)
if
define
</pre></td></tr></table>
</li><li> An <em>alias</em> is an alternate name for a given symbol. It can
appear anywhere in a form that the symbol could be used, and when quoted
it is replaced by the symbol; however, it does not satisfy the predicate
<code>symbol?</code>. Macro transformers rarely distinguish symbols from
aliases, referring to both as identifiers.
</li><li> A <em>syntactic</em> environment maps identifiers to their
meanings. More precisely, it determines whether an identifier is a
syntactic keyword or a variable. If it is a keyword, the meaning is an
interpretation for the form in which that keyword appears. If it is a
variable, the meaning identifies which binding of that variable is
referenced. In short, syntactic environments contain all of the
contextual information necessary for interpreting the meaning of a
particular form.
</li><li> A <em>syntactic closure</em> consists of a form, a syntactic
environment, and a list of identifiers. All identifiers in the form
take their meaning from the syntactic environment, except those in the
given list. The identifiers in the list are to have their meanings
determined later. A syntactic closure may be used in any context in
which its form could have been used. Since a syntactic closure is also
a form, it may not be used in contexts where a form would be illegal.
For example, a form may not appear as a clause in the cond special form.
A syntactic closure appearing in a quoted structure is replaced by its
form.
</li></ul>
<hr size="6">
<a name="Transformer-Definition"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Terminology" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Identifiers" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Scheme-Syntax-Extension-Packages" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Syntactic-Closure-Macro-Facility" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="slib_4.html#Textual-Conversion-Packages" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="slib.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="slib_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="slib_10.html#Procedure-and-Macro-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="slib_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<h4 class="subsubsection">3.5.1.2 Transformer Definition</h4>
<p>This section describes the <code>transformer</code> special form and the
procedures <code>make-syntactic-closure</code> and
<code>capture-syntactic-environment</code>.
</p>
<dl>
<dt><a name="index-transformer"></a><u>Syntax:</u> <b>transformer</b><i> expression</i></dt>
<dd>
<p>Syntax: It is an error if this syntax occurs except as a
<var>transformer spec</var>.
</p>
<p>Semantics: The <var>expression</var> is evaluated in the standard transformer
environment to yield a macro transformer as described below. This macro
transformer is bound to a macro keyword by the special form in which the
<code>transformer</code> expression appears (for example,
<code>let-syntax</code>).
</p>
<p>A <em>macro transformer</em> is a procedure that takes two arguments, a
form and a syntactic environment, and returns a new form. The first
argument, the <em>input form</em>, is the form in which the macro keyword
occurred. The second argument, the <em>usage environment</em>, is the
syntactic environment in which the input form occurred. The result of
the transformer, the <em>output form</em>, is automatically closed in the
<em>transformer environment</em>, which is the syntactic environment in
which the <code>transformer</code> expression occurred.
</p>
<p>For example, here is a definition of a push macro using
<code>syntax-rules</code>:
</p>
<table><tr><td> </td><td><pre class="lisp">(define-syntax push
(syntax-rules ()
((push item list)
(set! list (cons item list)))))
</pre></td></tr></table>
<p>Here is an equivalent definition using <code>transformer</code>:
</p><table><tr><td> </td><td><pre class="lisp">(define-syntax push
(transformer
(lambda (exp env)
(let ((item
(make-syntactic-closure env '() (cadr exp)))
(list
(make-syntactic-closure env '() (caddr exp))))
`(set! ,list (cons ,item ,list))))))
</pre></td></tr></table>
<p>In this example, the identifiers <code>set!</code> and <code>cons</code> are closed
in the transformer environment, and thus will not be affected by the
meanings of those identifiers in the usage environment
<code>env</code>.
</p>
<p>Some macros may be non-hygienic by design. For example, the following
defines a loop macro that implicitly binds <code>exit</code> to an escape
procedure. The binding of <code>exit</code> is intended to capture free
references to <code>exit</code> in the body of the loop, so <code>exit</code> must
be left free when the body is closed:
</p>
<table><tr><td> </td><td><pre class="lisp">(define-syntax loop
(transformer
(lambda (exp env)
(let ((body (cdr exp)))
`(call-with-current-continuation
(lambda (exit)
(let f ()
,@(map (lambda (exp)
(make-syntactic-closure env '(exit)
exp))
body)
(f))))))))
</pre></td></tr></table>
<p>To assign meanings to the identifiers in a form, use
<code>make-syntactic-closure</code> to close the form in a syntactic
environment.
</p></dd></dl>
<dl>
<dt><a name="index-make_002dsyntactic_002dclosure"></a><u>Function:</u> <b>make-syntactic-closure</b><i> environment free-names form</i></dt>
<dd>
<p><var>environment</var> must be a syntactic environment, <var>free-names</var> must
be a list of identifiers, and <var>form</var> must be a form.
<code>make-syntactic-closure</code> constructs and returns a syntactic closure
of <var>form</var> in <var>environment</var>, which can be used anywhere that
<var>form</var> could have been used. All the identifiers used in
<var>form</var>, except those explicitly excepted by <var>free-names</var>, obtain
their meanings from <var>environment</var>.
</p>
<p>Here is an example where <var>free-names</var> is something other than the
empty list. It is instructive to compare the use of <var>free-names</var> in
this example with its use in the <code>loop</code> example above: the examples
are similar except for the source of the identifier being left
free.
</p><table><tr><td> </td><td><pre class="lisp">(define-syntax let1
(transformer
(lambda (exp env)
(let ((id (cadr exp))
(init (caddr exp))
(exp (cadddr exp)))
`((lambda (,id)
,(make-syntactic-closure env (list id) exp))
,(make-syntactic-closure env '() init))))))
</pre></td></tr></table>
<p><code>let1</code> is a simplified version of <code>let</code> that only binds a
single identifier, and whose body consists of a single expression. When
the body expression is syntactically closed in its original syntactic
environment, the identifier that is to be bound by <code>let1</code> must be
left free, so that it can be properly captured by the <code>lambda</code> in
the output form.
</p>
<p>To obtain a syntactic environment other than the usage environment, use
<code>capture-syntactic-environment</code>.
</p></dd></dl>
<dl>
<dt><a name="index-capture_002dsyntactic_002denvironment"></a><u>Function:</u> <b>capture-syntactic-environment</b><i> procedure</i></dt>
<dd>
<p><code>capture-syntactic-environment</code> returns a form that will, when
transformed, call <var>procedure</var> on the current syntactic environment.
<var>procedure</var> should compute and return a new form to be transformed,
in that same syntactic environment, in place of the form.
</p>
<p>An example will make this clear. Suppose we wanted to define a simple
<code>loop-until</code> keyword equivalent to
</p>
<table><tr><td> </td><td><pre class="lisp">(define-syntax loop-until
(syntax-rules ()
((loop-until id init test return step)
(letrec ((loop
(lambda (id)
(if test return (loop step)))))
(loop init)))))
</pre></td></tr></table>
<p>The following attempt at defining <code>loop-until</code> has a subtle bug:
</p><table><tr><td> </td><td><pre class="lisp">(define-syntax loop-until
(transformer
(lambda (exp env)
(let ((id (cadr exp))
(init (caddr exp))
(test (cadddr exp))
(return (cadddr (cdr exp)))
(step (cadddr (cddr exp)))
(close
(lambda (exp free)
(make-syntactic-closure env free exp))))
`(letrec ((loop
(lambda (,id)
(if ,(close test (list id))
,(close return (list id))
(loop ,(close step (list id)))))))
(loop ,(close init '())))))))
</pre></td></tr></table>
<p>This definition appears to take all of the proper precautions to prevent
unintended captures. It carefully closes the subexpressions in their
original syntactic environment and it leaves the <code>id</code> identifier
free in the <code>test</code>, <code>return</code>, and <code>step</code> expressions, so
that it will be captured by the binding introduced by the <code>lambda</code>
expression. Unfortunately it uses the identifiers <code>if</code> and
<code>loop</code> within that <code>lambda</code> expression, so if the user of
<code>loop-until</code> just happens to use, say, <code>if</code> for the
identifier, it will be inadvertently captured.
</p>
<p>The syntactic environment that <code>if</code> and <code>loop</code> want to be
exposed to is the one just outside the <code>lambda</code> expression: before
the user’s identifier is added to the syntactic environment, but after
the identifier loop has been added.
<code>capture-syntactic-environment</code> captures exactly that environment
as follows:
</p>
<table><tr><td> </td><td><pre class="lisp">(define-syntax loop-until
(transformer
(lambda (exp env)
(let ((id (cadr exp))
(init (caddr exp))
(test (cadddr exp))
(return (cadddr (cdr exp)))
(step (cadddr (cddr exp)))
(close
(lambda (exp free)
(make-syntactic-closure env free exp))))
`(letrec ((loop
,(capture-syntactic-environment
(lambda (env)
`(lambda (,id)
(,(make-syntactic-closure env '() `if)
,(close test (list id))
,(close return (list id))
(,(make-syntactic-closure env '()
`loop)
,(close step (list id)))))))))
(loop ,(close init '())))))))
</pre></td></tr></table>
<p>In this case, having captured the desired syntactic environment, it is
convenient to construct syntactic closures of the identifiers <code>if</code>
and the <code>loop</code> and use them in the body of the
<code>lambda</code>.
</p>
<p>A common use of <code>capture-syntactic-environment</code> is to get the
transformer environment of a macro transformer:
</p>
<table><tr><td> </td><td><pre class="lisp">(transformer
(lambda (exp env)
(capture-syntactic-environment
(lambda (transformer-env)
...))))
</pre></td></tr></table>
</dd></dl>
<hr size="6">
<a name="Identifiers"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Transformer-Definition" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Acknowledgements" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Scheme-Syntax-Extension-Packages" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Syntactic-Closure-Macro-Facility" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="slib_4.html#Textual-Conversion-Packages" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="slib.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="slib_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="slib_10.html#Procedure-and-Macro-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="slib_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<h4 class="subsubsection">3.5.1.3 Identifiers</h4>
<p>This section describes the procedures that create and manipulate
identifiers. Previous syntactic closure proposals did not have an
identifier data type – they just used symbols. The identifier data
type extends the syntactic closures facility to be compatible with the
high-level <code>syntax-rules</code> facility.
</p>
<p>As discussed earlier, an identifier is either a symbol or an
<em>alias</em>. An alias is implemented as a syntactic closure whose
<em>form</em> is an identifier:
</p>
<table><tr><td> </td><td><pre class="lisp">(make-syntactic-closure env '() 'a)
⇒ an <em>alias</em>
</pre></td></tr></table>
<p>Aliases are implemented as syntactic closures because they behave just
like syntactic closures most of the time. The difference is that an
alias may be bound to a new value (for example by <code>lambda</code> or
<code>let-syntax</code>); other syntactic closures may not be used this way.
If an alias is bound, then within the scope of that binding it is looked
up in the syntactic environment just like any other identifier.
</p>
<p>Aliases are used in the implementation of the high-level facility
<code>syntax-rules</code>. A macro transformer created by <code>syntax-rules</code>
uses a template to generate its output form, substituting subforms of
the input form into the template. In a syntactic closures
implementation, all of the symbols in the template are replaced by
aliases closed in the transformer environment, while the output form
itself is closed in the usage environment. This guarantees that the
macro transformation is hygienic, without requiring the transformer to
know the syntactic roles of the substituted input subforms.
</p>
<dl>
<dt><a name="index-identifier_003f"></a><u>Function:</u> <b>identifier?</b><i> object</i></dt>
<dd><p>Returns <code>#t</code> if <var>object</var> is an identifier, otherwise returns
<code>#f</code>. Examples:
</p>
<table><tr><td> </td><td><pre class="lisp">(identifier? 'a)
⇒ #t
(identifier? (make-syntactic-closure env '() 'a))
⇒ #t
(identifier? "a")
⇒ #f
(identifier? #\a)
⇒ #f
(identifier? 97)
⇒ #f
(identifier? #f)
⇒ #f
(identifier? '(a))
⇒ #f
(identifier? '#(a))
⇒ #f
</pre></td></tr></table>
<p>The predicate <code>eq?</code> is used to determine if two identifers are
“the same”. Thus <code>eq?</code> can be used to compare identifiers
exactly as it would be used to compare symbols. Often, though, it is
useful to know whether two identifiers “mean the same thing”. For
example, the <code>cond</code> macro uses the symbol <code>else</code> to identify
the final clause in the conditional. A macro transformer for
<code>cond</code> cannot just look for the symbol <code>else</code>, because the
<code>cond</code> form might be the output of another macro transformer that
replaced the symbol <code>else</code> with an alias. Instead the transformer
must look for an identifier that “means the same thing” in the usage
environment as the symbol <code>else</code> means in the transformer
environment.
</p></dd></dl>
<dl>
<dt><a name="index-identifier_003d_003f"></a><u>Function:</u> <b>identifier=?</b><i> environment1 identifier1 environment2 identifier2</i></dt>
<dd><p><var>environment1</var> and <var>environment2</var> must be syntactic
environments, and <var>identifier1</var> and <var>identifier2</var> must be
identifiers. <code>identifier=?</code> returns <code>#t</code> if the meaning of
<var>identifier1</var> in <var>environment1</var> is the same as that of
<var>identifier2</var> in <var>environment2</var>, otherwise it returns <code>#f</code>.
Examples:
</p>
<table><tr><td> </td><td><pre class="lisp">(let-syntax
((foo
(transformer
(lambda (form env)
(capture-syntactic-environment
(lambda (transformer-env)
(identifier=? transformer-env 'x env 'x)))))))
(list (foo)
(let ((x 3))
(foo))))
⇒ (#t #f)
</pre></td></tr></table>
<table><tr><td> </td><td><pre class="lisp">(let-syntax ((bar foo))
(let-syntax
((foo
(transformer
(lambda (form env)
(capture-syntactic-environment
(lambda (transformer-env)
(identifier=? transformer-env 'foo
env (cadr form))))))))
(list (foo foo)
(foobar))))
⇒ (#f #t)
</pre></td></tr></table>
</dd></dl>
<hr size="6">
<a name="Acknowledgements"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Identifiers" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Syntax_002dCase-Macros" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Scheme-Syntax-Extension-Packages" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Syntactic-Closure-Macro-Facility" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="slib_4.html#Textual-Conversion-Packages" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="slib.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="slib_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="slib_10.html#Procedure-and-Macro-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="slib_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<h4 class="subsubsection">3.5.1.4 Acknowledgements</h4>
<p>The syntactic closures facility was invented by Alan Bawden and Jonathan
Rees. The use of aliases to implement <code>syntax-rules</code> was invented
by Alan Bawden (who prefers to call them <em>synthetic names</em>). Much
of this proposal is derived from an earlier proposal by Alan
Bawden.
</p>
<hr size="6">
<a name="Syntax_002dCase-Macros"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Acknowledgements" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Notes" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Scheme-Syntax-Extension-Packages" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Scheme-Syntax-Extension-Packages" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="slib_4.html#Textual-Conversion-Packages" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="slib.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="slib_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="slib_10.html#Procedure-and-Macro-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="slib_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Syntax_002dCase-Macros-1"></a>
<h2 class="section">3.6 Syntax-Case Macros</h2>
<p><code>(require 'syntax-case)</code>
<a name="index-syntax_002dcase-1"></a>
</p>
<dl>
<dt><a name="index-macro_003aexpand-3"></a><u>Function:</u> <b>macro:expand</b><i> expression</i></dt>
<dt><a name="index-syncase_003aexpand"></a><u>Function:</u> <b>syncase:expand</b><i> expression</i></dt>
<dd><p>Returns scheme code with the macros and derived expression types of
<var>expression</var> expanded to primitive expression types.
</p></dd></dl>
<dl>
<dt><a name="index-macro_003aeval-3"></a><u>Function:</u> <b>macro:eval</b><i> expression</i></dt>
<dt><a name="index-syncase_003aeval"></a><u>Function:</u> <b>syncase:eval</b><i> expression</i></dt>
<dd><p><code>macro:eval</code> returns the value of <var>expression</var> in the current
top level environment. <var>expression</var> can contain macro definitions.
Side effects of <var>expression</var> will affect the top level
environment.
</p></dd></dl>
<dl>
<dt><a name="index-macro_003aload-3"></a><u>Procedure:</u> <b>macro:load</b><i> filename</i></dt>
<dt><a name="index-syncase_003aload"></a><u>Procedure:</u> <b>syncase:load</b><i> filename</i></dt>
<dd><p><var>filename</var> should be a string. If filename names an existing file,
the <code>macro:load</code> procedure reads Scheme source code expressions and
definitions from the file and evaluates them sequentially. These
source code expressions and definitions may contain macro definitions.
The <code>macro:load</code> procedure does not affect the values returned by
<code>current-input-port</code> and <code>current-output-port</code>.
</p></dd></dl>
<p>This is version 2.1 of <code>syntax-case</code>, the low-level macro facility
proposed and implemented by Robert Hieb and R. Kent Dybvig.
</p>
<p>This version is further adapted by Harald Hanche-Olsen
<hanche @ imf.unit.no> to make it compatible with, and easily usable
with, SLIB. Mainly, these adaptations consisted of:
</p>
<ul>
<li>
Removing white space from ‘<tt>expand.pp</tt>’ to save space in the
distribution. This file is not meant for human readers anyway…
</li><li>
Removed a couple of Chez scheme dependencies.
</li><li>
Renamed global variables used to minimize the possibility of name
conflicts.
</li><li>
Adding an SLIB-specific initialization file.
</li><li>
Removing a couple extra files, most notably the documentation (but see
below).
</li></ul>
<p>If you wish, you can see exactly what changes were done by reading the
shell script in the file ‘<tt>syncase.sh</tt>’.
</p>
<p>The two PostScript files were omitted in order to not burden the SLIB
distribution with them. If you do intend to use <code>syntax-case</code>,
however, you should get these files and print them out on a PostScript
printer. They are available with the original <code>syntax-case</code>
distribution by anonymous FTP in
‘<tt>cs.indiana.edu:/pub/scheme/syntax-case</tt>’.
</p>
<p>In order to use syntax-case from an interactive top level, execute:
</p><table><tr><td> </td><td><pre class="lisp">(require 'syntax-case)
<a name="index-syntax_002dcase-2"></a>(require 'repl)
<a name="index-repl"></a>(repl:top-level macro:eval)
</pre></td></tr></table>
<p>See the section Repl (see section <a href="slib_7.html#Repl">Repl</a>) for more information.
</p>
<p>To check operation of syntax-case get
‘<tt>cs.indiana.edu:/pub/scheme/syntax-case</tt>’, and type
</p><table><tr><td> </td><td><pre class="lisp">(require 'syntax-case)
<a name="index-syntax_002dcase-3"></a><a name="index-syncase_003asanity_002dcheck"></a>(syncase:sanity-check)
</pre></td></tr></table>
<p>Beware that <code>syntax-case</code> takes a long time to load – about 20s on
a SPARCstation SLC (with SCM) and about 90s on a Macintosh SE/30 (with
Gambit).
</p>
<hr size="6">
<a name="Notes"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Syntax_002dCase-Macros" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Define_002dStructure" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Scheme-Syntax-Extension-Packages" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Syntax_002dCase-Macros" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="slib_4.html#Textual-Conversion-Packages" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="slib.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="slib_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="slib_10.html#Procedure-and-Macro-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="slib_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<h3 class="subsection">3.6.1 Notes</h3>
<p>All R4RS syntactic forms are defined, including <code>delay</code>. Along
with <code>delay</code> are simple definitions for <code>make-promise</code> (into
which <code>delay</code> expressions expand) and <code>force</code>.
</p>
<p><code>syntax-rules</code> and <code>with-syntax</code> (described in <cite>TR356</cite>)
are defined.
</p>
<p><code>syntax-case</code> is actually defined as a macro that expands into
calls to the procedure <code>syntax-dispatch</code> and the core form
<code>syntax-lambda</code>; do not redefine these names.
</p>
<p>Several other top-level bindings not documented in TR356 are created:
</p><ul>
<li> the “hooks” in ‘<tt>hooks.ss</tt>’
</li><li> the <code>build-</code> procedures in ‘<tt>output.ss</tt>’
</li><li> <code>expand-syntax</code> (the expander)
</li></ul>
<p>The syntax of define has been extended to allow <code>(define <var>id</var>)</code>,
which assigns <var>id</var> to some unspecified value.
</p>
<p>We have attempted to maintain R4RS compatibility where possible. The
incompatibilities should be confined to ‘<tt>hooks.ss</tt>’. Please let us
know if there is some incompatibility that is not flagged as such.
</p>
<p>Send bug reports, comments, suggestions, and questions to Kent Dybvig
(dyb @ iuvax.cs.indiana.edu).
</p>
<hr size="6">
<a name="Define_002dStructure"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Notes" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Define_002dRecord_002dType" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Scheme-Syntax-Extension-Packages" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Scheme-Syntax-Extension-Packages" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="slib_4.html#Textual-Conversion-Packages" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="slib.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="slib_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="slib_10.html#Procedure-and-Macro-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="slib_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Define_002dStructure-1"></a>
<h2 class="section">3.7 Define-Structure</h2>
<p><code>(require 'structure)</code>
</p>
<p>Included with the <code>syntax-case</code> files was ‘<tt>structure.scm</tt>’
which defines a macro <code>define-structure</code>. Here is its
documentation from Gambit 4.0:
</p>
<dl>
<dt><a name="index-define_002dstructure"></a><u>special form:</u> <b>define-structure</b><i> <var>name</var> <var>field</var>…</i></dt>
<dd>
<p>Record data types similar to Pascal records and C <code>struct</code>
types can be defined using the <code>define-structure</code> special form.
The identifier <var>name</var> specifies the name of the new data type. The
structure name is followed by <var>k</var> identifiers naming each field of
the record. The <code>define-structure</code> expands into a set of definitions
of the following procedures:
</p>
<ul>
<li>
‘<tt>make-</tt><var>name</var>’ – A <var>k</var> argument procedure which constructs
a new record from the value of its <var>k</var> fields.
</li><li>
‘<var>name</var><tt>?</tt>’ – A procedure which tests if its single argument
is of the given record type.
</li><li>
‘<var>name</var><tt>-</tt><var>field</var>’ – For each field, a procedure taking
as its single argument a value of the given record type and returning
the content of the corresponding field of the record.
</li><li>
‘<var>name</var><tt>-</tt><var>field</var><tt>-set!</tt>’ – For each field, a two
argument procedure taking as its first argument a value of the given
record type. The second argument gets assigned to the corresponding
field of the record and the void object is returned.
</li></ul>
<p>Gambit record data types have a printed representation that includes
the name of the type and the name and value of each field.
</p>
<p>For example:
</p>
<table><tr><td> </td><td><pre class="smallexample">> <b>(define-structure point x y color)</b>
> <b>(define p (make-point 3 5 'red))</b>
> <b>p</b>
#<point #3 x: 3 y: 5 color: red>
> <b>(point-x p)</b>
3
> <b>(point-color p)</b>
red
> <b>(point-color-set! p 'black)</b>
> <b>p</b>
#<point #3 x: 3 y: 5 color: black>
</pre></td></tr></table>
</dd></dl>
<hr size="6">
<a name="Define_002dRecord_002dType"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Define_002dStructure" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Fluid_002dLet" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Scheme-Syntax-Extension-Packages" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Scheme-Syntax-Extension-Packages" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="slib_4.html#Textual-Conversion-Packages" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="slib.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="slib_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="slib_10.html#Procedure-and-Macro-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="slib_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Define_002dRecord_002dType-1"></a>
<h2 class="section">3.8 Define-Record-Type</h2>
<p><code>(require 'define-record-type)</code> or <code>(require 'srfi-9)</code>
<a name="index-srfi_002d9"></a>
<a name="index-define_002drecord_002dtype"></a>
</p>
<p><a href="http://srfi.schemers.org/srfi-9/srfi-9.html">http://srfi.schemers.org/srfi-9/srfi-9.html</a>
</p>
<dl>
<dt><a name="index-define_002drecord_002dtype-1"></a><u>Special Form:</u> <b>define-record-type</b><i> <type-name> (<constructor-name> <field-tag> ...) <predicate-name> <field-spec> ...</i></dt>
<dd>
<p>Where
</p><table><tr><td> </td><td><pre class="lisp"><field-spec> ≡ (<field-tag> <accessor-name>)
≡ (<field-tag> <accessor-name> <modifier-name>)
</pre></td></tr></table>
<p><code>define-record-type</code> is a syntax wrapper for the SLIB
<code>record</code> module.
</p></dd></dl>
<hr size="6">
<a name="Fluid_002dLet"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Define_002dRecord_002dType" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Binding-to-multiple-values" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Scheme-Syntax-Extension-Packages" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Scheme-Syntax-Extension-Packages" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="slib_4.html#Textual-Conversion-Packages" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="slib.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="slib_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="slib_10.html#Procedure-and-Macro-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="slib_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Fluid_002dLet-1"></a>
<h2 class="section">3.9 Fluid-Let</h2>
<p><code>(require 'fluid-let)</code>
<a name="index-fluid_002dlet"></a>
</p>
<dl>
<dt><a name="index-fluid_002dlet-1"></a><u>Syntax:</u> <b>fluid-let</b><i> <code>(<var>bindings</var> …)</code> <var>forms</var>…</i></dt>
</dl>
<table><tr><td> </td><td><pre class="lisp">(fluid-let ((<var>variable</var> <var>init</var>) …)
<var>expression</var> <var>expression</var> …)
</pre></td></tr></table>
<p>The <var>init</var>s are evaluated in the current environment (in some
unspecified order), the current values of the <var>variable</var>s are saved,
the results are assigned to the <var>variable</var>s, the <var>expression</var>s
are evaluated sequentially in the current environment, the
<var>variable</var>s are restored to their original values, and the value of
the last <var>expression</var> is returned.
</p>
<p>The syntax of this special form is similar to that of <code>let</code>, but
<code>fluid-let</code> temporarily rebinds existing <var>variable</var>s. Unlike
<code>let</code>, <code>fluid-let</code> creates no new bindings; instead it
<em>assigns</em> the values of each <var>init</var> to the binding (determined
by the rules of lexical scoping) of its corresponding
<var>variable</var>.
</p>
<hr size="6">
<a name="Binding-to-multiple-values"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Fluid_002dLet" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Guarded-LET_002a-special-form" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Scheme-Syntax-Extension-Packages" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Scheme-Syntax-Extension-Packages" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="slib_4.html#Textual-Conversion-Packages" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="slib.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="slib_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="slib_10.html#Procedure-and-Macro-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="slib_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Binding-to-multiple-values-1"></a>
<h2 class="section">3.10 Binding to multiple values</h2>
<p><code>(require 'receive)</code> or <code>(require 'srfi-8)</code>
<a name="index-srfi_002d8"></a>
<a name="index-receive"></a>
</p>
<dl>
<dt><a name="index-receive-1"></a><u>Special Form:</u> <b>receive</b><i> formals expression body …</i></dt>
<dd>
<p><a href="http://srfi.schemers.org/srfi-8/srfi-8.html">http://srfi.schemers.org/srfi-8/srfi-8.html</a>
</p></dd></dl>
<p><code>(require 'let-values)</code> or <code>(require 'srfi-11)</code>
<a name="index-srfi_002d11"></a>
<a name="index-let_002dvalues"></a>
</p>
<dl>
<dt><a name="index-let_002dvalues-1"></a><u>Special Form:</u> <b>let-values</b><i> ((formals expression) ...) body …</i></dt>
<dt><a name="index-let_002dvalues_002a"></a><u>Special Form:</u> <b>let-values*</b><i> ((formals expression) ...) body …</i></dt>
<dd>
<p><a href="http://srfi.schemers.org/srfi-11/srfi-11.html">http://srfi.schemers.org/srfi-11/srfi-11.html</a>
</p></dd></dl>
<hr size="6">
<a name="Guarded-LET_002a-special-form"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Binding-to-multiple-values" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Guarded-COND-Clause" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Scheme-Syntax-Extension-Packages" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Scheme-Syntax-Extension-Packages" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="slib_4.html#Textual-Conversion-Packages" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="slib.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="slib_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="slib_10.html#Procedure-and-Macro-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="slib_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Guarded-LET_002a-special-form-1"></a>
<h2 class="section">3.11 Guarded LET* special form</h2>
<p><code>(require 'and-let*)</code> or <code>(require 'srfi-2)</code>
<a name="index-srfi_002d2"></a>
<a name="index-and_002dlet_002a"></a>
</p>
<dl>
<dt><a name="index-and_002dlet_002a-1"></a><u>Macro:</u> <b>and-let*</b><i> claws body …</i></dt>
<dd>
<p><a href="http://srfi.schemers.org/srfi-2/srfi-2.html">http://srfi.schemers.org/srfi-2/srfi-2.html</a>
</p></dd></dl>
<hr size="6">
<a name="Guarded-COND-Clause"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Guarded-LET_002a-special-form" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Yasos" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Scheme-Syntax-Extension-Packages" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Scheme-Syntax-Extension-Packages" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="slib_4.html#Textual-Conversion-Packages" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="slib.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="slib_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="slib_10.html#Procedure-and-Macro-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="slib_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Guarded-COND-Clause-1"></a>
<h2 class="section">3.12 Guarded COND Clause</h2>
<p><code>(require 'guarded-cond-clause)</code> or <code>(require 'srfi-61)</code>
<a name="index-srfi_002d61"></a>
<a name="index-guarded_002dcond_002dclause"></a>
</p>
<p><a href="http://srfi.schemers.org/srfi-61/srfi-61.html">http://srfi.schemers.org/srfi-61/srfi-61.html</a>
</p>
<dl>
<dt><a name="index-cond"></a><u>library syntax:</u> <b>cond</b><i> <clause1> <clause2> …</i></dt>
<dd>
<p><em>Syntax:</em>
Each <span class="roman"><clause></span> should be of the form
</p>
<table><tr><td> </td><td><pre class="format"><tt>(<span class="roman"><test></span> <span class="roman"><expression1></span> …)
</tt>
</pre></td></tr></table>
<p>where <span class="roman"><test></span> is any expression. Alternatively, a <span class="roman"><clause></span> may be
of the form
</p>
<table><tr><td> </td><td><pre class="format"><tt>(<span class="roman"><test></span> => <span class="roman"><expression></span>)
</tt>
</pre></td></tr></table>
<p>The <span class="roman"><clause></span> production in the formal syntax of Scheme as
written by R5RS in section 7.1.3 is extended with a new option:
<a name="index-_003d_003e"></a>
</p>
<table><tr><td> </td><td><pre class="format"><tt><span class="roman"><clause></span> => (<span class="roman"><generator></span> <span class="roman"><guard></span> => <span class="roman"><receiver></span>)
</tt>
</pre></td></tr></table>
<p>where <span class="roman"><generator></span>, <span class="roman"><guard></span>, & <span class="roman"><receiver></span> are all
<span class="roman"><expression></span>s.
</p>
<blockquote><p>Clauses of this form have the following semantics: <span class="roman"><generator></span> is
evaluated. It may return arbitrarily many values. <span class="roman"><Guard></span> is
applied to an argument list containing the values in order that
<span class="roman"><generator></span> returned. If <span class="roman"><guard></span> returns a true value for
that argument list, <span class="roman"><receiver></span> is applied with an equivalent
argument list. If <span class="roman"><guard></span> returns a false value, however, the
clause is abandoned and the next one is tried.
</p></blockquote>
<p>The last <span class="roman"><clause></span> may be
an “else clause,” which has the form
</p>
<table><tr><td> </td><td><pre class="format"><tt>(else <span class="roman"><expression1></span> <span class="roman"><expression2></span> …)<span class="roman">.</span>
</tt>
</pre></td></tr></table>
</dd></dl>
<p>This <code>port->char-list</code> procedure accepts an input port and
returns a list of all the characters it produces until the end.
</p>
<table><tr><td> </td><td><pre class="example">(define (port->char-list port)
(cond ((read-char port) char?
=> (lambda (c) (cons c (port->char-list port))))
(else '())))
(call-with-input-string "foo" port->char-list) ==> (#\f #\o #\o)
</pre></td></tr></table>
<hr size="6">
<a name="Yasos"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Guarded-COND-Clause" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Yasos-terms" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Scheme-Syntax-Extension-Packages" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Scheme-Syntax-Extension-Packages" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="slib_4.html#Textual-Conversion-Packages" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="slib.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="slib_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="slib_10.html#Procedure-and-Macro-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="slib_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Yasos-1"></a>
<h2 class="section">3.13 Yasos</h2>
<p><code>(require 'oop)</code> or <code>(require 'yasos)</code>
<a name="index-oop"></a>
<a name="index-yasos"></a>
</p>
<p>‘Yet Another Scheme Object System’ is a simple object system for Scheme
based on the paper by Norman Adams and Jonathan Rees: <cite>Object
Oriented Programming in Scheme</cite>, Proceedings of the 1988 ACM Conference
on LISP and Functional Programming, July 1988 [ACM #552880].
</p>
<p>Another reference is:
</p>
<p>Ken Dickey.
<A HREF="ftp://ftp.cs.indiana.edu/pub/scheme-repository/doc/pubs/swob.txt">
Scheming with Objects
</A>
<cite>AI Expert</cite> Volume 7, Number 10 (October 1992), pp. 24-33.
</p>
<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top"><a href="#Yasos-terms">3.13.1 Terms</a></td><td> </td><td align="left" valign="top"> Definitions and disclaimer.
</td></tr>
<tr><td align="left" valign="top"><a href="#Yasos-interface">3.13.2 Interface</a></td><td> </td><td align="left" valign="top"> The Yasos macros and procedures.
</td></tr>
<tr><td align="left" valign="top"><a href="#Setters">3.13.3 Setters</a></td><td> </td><td align="left" valign="top"> Dylan-like setters in Yasos.
</td></tr>
<tr><td align="left" valign="top"><a href="#Yasos-examples">3.13.4 Examples</a></td><td> </td><td align="left" valign="top"> Usage of Yasos and setters.
</td></tr>
</table>
<hr size="6">
<a name="Yasos-terms"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Yasos" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Yasos-interface" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Scheme-Syntax-Extension-Packages" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Yasos" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="slib_4.html#Textual-Conversion-Packages" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="slib.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="slib_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="slib_10.html#Procedure-and-Macro-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="slib_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Terms"></a>
<h3 class="subsection">3.13.1 Terms</h3>
<dl compact="compact">
<dt> <em>Object</em></dt>
<dd><p>Any Scheme data object.
</p>
</dd>
<dt> <em>Instance</em></dt>
<dd><p>An instance of the OO system; an <em>object</em>.
</p>
</dd>
<dt> <em>Operation</em></dt>
<dd><p>A <var>method</var>.
</p></dd>
</dl>
<dl compact="compact">
<dt> <em>Notes:</em></dt>
<dd><p>The object system supports multiple inheritance. An instance can
inherit from 0 or more ancestors. In the case of multiple inherited
operations with the same identity, the operation used is that from the
first ancestor which contains it (in the ancestor <code>let</code>). An
operation may be applied to any Scheme data object—not just instances.
As code which creates instances is just code, there are no <em>classes</em>
and no meta-<var>anything</var>. Method dispatch is by a procedure call a la
CLOS rather than by <code>send</code> syntax a la Smalltalk.
</p>
</dd>
<dt> <em>Disclaimer:</em></dt>
<dd><p>There are a number of optimizations which can be made. This
implementation is expository (although performance should be quite
reasonable). See the L&FP paper for some suggestions.
</p></dd>
</dl>
<hr size="6">
<a name="Yasos-interface"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Yasos-terms" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Setters" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Scheme-Syntax-Extension-Packages" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Yasos" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="slib_4.html#Textual-Conversion-Packages" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="slib.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="slib_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="slib_10.html#Procedure-and-Macro-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="slib_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Interface"></a>
<h3 class="subsection">3.13.2 Interface</h3>
<dl>
<dt><a name="index-define_002doperation"></a><u>Syntax:</u> <b>define-operation</b><i> <code>(</code>opname self arg …<code>)</code> <var>default-body</var></i></dt>
<dd><p>Defines a default behavior for data objects which don’t handle the
operation <var>opname</var>. The default behavior (for an empty
<var>default-body</var>) is to generate an error.
</p></dd></dl>
<dl>
<dt><a name="index-define_002dpredicate"></a><u>Syntax:</u> <b>define-predicate</b><i> opname?</i></dt>
<dd><p>Defines a predicate <var>opname?</var>, usually used for determining the
<em>type</em> of an object, such that <code>(<var>opname?</var> <var>object</var>)</code>
returns <code>#t</code> if <var>object</var> has an operation <var>opname?</var> and
<code>#f</code> otherwise.
</p></dd></dl>
<dl>
<dt><a name="index-object"></a><u>Syntax:</u> <b>object</b><i> <code>((<var>name</var> <var>self</var> <var>arg</var> …) <var>body</var>)</code> …</i></dt>
<dd><p>Returns an object (an instance of the object system) with operations.
Invoking <code>(<var>name</var> <var>object</var> <var>arg</var> …</code> executes the
<var>body</var> of the <var>object</var> with <var>self</var> bound to <var>object</var> and
with argument(s) <var>arg</var>….
</p></dd></dl>
<dl>
<dt><a name="index-object_002dwith_002dancestors"></a><u>Syntax:</u> <b>object-with-ancestors</b><i> <code>((</code>ancestor1 init1<code>)</code> …<code>)</code> operation …</i></dt>
<dd><p>A <code>let</code>-like form of <code>object</code> for multiple inheritance. It
returns an object inheriting the behaviour of <var>ancestor1</var> etc. An
operation will be invoked in an ancestor if the object itself does not
provide such a method. In the case of multiple inherited operations
with the same identity, the operation used is the one found in the first
ancestor in the ancestor list.
</p></dd></dl>
<dl>
<dt><a name="index-operate_002das"></a><u>Syntax:</u> <b>operate-as</b><i> component operation self arg …</i></dt>
<dd><p>Used in an operation definition (of <var>self</var>) to invoke the
<var>operation</var> in an ancestor <var>component</var> but maintain the object’s
identity. Also known as “send-to-super”.
</p></dd></dl>
<dl>
<dt><a name="index-print"></a><u>Procedure:</u> <b>print</b><i> obj port</i></dt>
<dd><p>A default <code>print</code> operation is provided which is just <code>(format
<var>port</var> <var>obj</var>)</code> (see section <a href="slib_4.html#Format">Format (version 3.1)</a>) for non-instances and prints
<var>obj</var> preceded by ‘<samp>#<INSTANCE></samp>’ for instances.
</p></dd></dl>
<dl>
<dt><a name="index-size"></a><u>Function:</u> <b>size</b><i> obj</i></dt>
<dd><p>The default method returns the number of elements in <var>obj</var> if it is
a vector, string or list, <code>2</code> for a pair, <code>1</code> for a character
and by default id an error otherwise. Objects such as collections
(see section <a href="slib_7.html#Collections">Collections</a>) may override the default in an obvious way.
</p></dd></dl>
<hr size="6">
<a name="Setters"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Yasos-interface" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Yasos-examples" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Scheme-Syntax-Extension-Packages" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Yasos" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="slib_4.html#Textual-Conversion-Packages" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="slib.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="slib_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="slib_10.html#Procedure-and-Macro-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="slib_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Setters-1"></a>
<h3 class="subsection">3.13.3 Setters</h3>
<p><em>Setters</em> implement <em>generalized locations</em> for objects
associated with some sort of mutable state. A <em>getter</em> operation
retrieves a value from a generalized location and the corresponding
setter operation stores a value into the location. Only the getter is
named – the setter is specified by a procedure call as below. (Dylan
uses special syntax.) Typically, but not necessarily, getters are
access operations to extract values from Yasos objects (see section <a href="#Yasos">Yasos</a>).
Several setters are predefined, corresponding to getters <code>car</code>,
<code>cdr</code>, <code>string-ref</code> and <code>vector-ref</code> e.g., <code>(setter
car)</code> is equivalent to <code>set-car!</code>.
</p>
<p>This implementation of setters is similar to that in Dylan(TM)
(<cite>Dylan: An object-oriented dynamic language</cite>, Apple Computer
Eastern Research and Technology). Common LISP provides similar
facilities through <code>setf</code>.
</p>
<dl>
<dt><a name="index-setter"></a><u>Function:</u> <b>setter</b><i> getter</i></dt>
<dd><p>Returns the setter for the procedure <var>getter</var>. E.g., since
<code>string-ref</code> is the getter corresponding to a setter which is
actually <code>string-set!</code>:
</p><table><tr><td> </td><td><pre class="example">(define foo "foo")
((setter string-ref) foo 0 #\F) ; set element 0 of foo
foo ⇒ "Foo"
</pre></td></tr></table>
</dd></dl>
<dl>
<dt><a name="index-set"></a><u>Syntax:</u> <b>set</b><i> place new-value</i></dt>
<dd><p>If <var>place</var> is a variable name, <code>set</code> is equivalent to
<code>set!</code>. Otherwise, <var>place</var> must have the form of a procedure
call, where the procedure name refers to a getter and the call indicates
an accessible generalized location, i.e., the call would return a value.
The return value of <code>set</code> is usually unspecified unless used with a
setter whose definition guarantees to return a useful value.
</p><table><tr><td> </td><td><pre class="example">(set (string-ref foo 2) #\O) ; generalized location with getter
foo ⇒ "FoO"
(set foo "foo") ; like set!
foo ⇒ "foo"
</pre></td></tr></table>
</dd></dl>
<dl>
<dt><a name="index-add_002dsetter"></a><u>Procedure:</u> <b>add-setter</b><i> getter setter</i></dt>
<dd><p>Add procedures <var>getter</var> and <var>setter</var> to the (inaccessible) list
of valid setter/getter pairs. <var>setter</var> implements the store
operation corresponding to the <var>getter</var> access operation for the
relevant state. The return value is unspecified.
</p></dd></dl>
<dl>
<dt><a name="index-remove_002dsetter_002dfor"></a><u>Procedure:</u> <b>remove-setter-for</b><i> getter</i></dt>
<dd><p>Removes the setter corresponding to the specified <var>getter</var> from the
list of valid setters. The return value is unspecified.
</p></dd></dl>
<dl>
<dt><a name="index-define_002daccess_002doperation"></a><u>Syntax:</u> <b>define-access-operation</b><i> getter-name</i></dt>
<dd><p>Shorthand for a Yasos <code>define-operation</code> defining an operation
<var>getter-name</var> that objects may support to return the value of some
mutable state. The default operation is to signal an error. The return
value is unspecified.
</p></dd></dl>
<hr size="6">
<a name="Yasos-examples"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Setters" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="slib_4.html#Textual-Conversion-Packages" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="#Scheme-Syntax-Extension-Packages" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Yasos" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="slib_4.html#Textual-Conversion-Packages" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="slib.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="slib_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="slib_10.html#Procedure-and-Macro-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="slib_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Examples-1"></a>
<h3 class="subsection">3.13.4 Examples</h3>
<table><tr><td> </td><td><pre class="lisp">;;; These definitions for PRINT and SIZE are
;;; already supplied by
(require 'yasos)
(define-operation (print obj port)
(format port
(if (instance? obj) "#<instance>" "~s")
obj))
(define-operation (size obj)
(cond
((vector? obj) (vector-length obj))
((list? obj) (length obj))
((pair? obj) 2)
((string? obj) (string-length obj))
((char? obj) 1)
(else
(slib:error "Operation not supported: size" obj))))
(define-predicate cell?)
(define-operation (fetch obj))
(define-operation (store! obj newValue))
(define (make-cell value)
(object
((cell? self) #t)
((fetch self) value)
((store! self newValue)
(set! value newValue)
newValue)
((size self) 1)
((print self port)
(format port "#<Cell: ~s>" (fetch self)))))
(define-operation (discard obj value)
(format #t "Discarding ~s~%" value))
(define (make-filtered-cell value filter)
(object-with-ancestors
((cell (make-cell value)))
((store! self newValue)
(if (filter newValue)
(store! cell newValue)
(discard self newValue)))))
(define-predicate array?)
(define-operation (array-ref array index))
(define-operation (array-set! array index value))
(define (make-array num-slots)
(let ((anArray (make-vector num-slots)))
(object
((array? self) #t)
((size self) num-slots)
((array-ref self index)
(vector-ref anArray index))
((array-set! self index newValue)
(vector-set! anArray index newValue))
((print self port)
(format port "#<Array ~s>" (size self))))))
(define-operation (position obj))
(define-operation (discarded-value obj))
(define (make-cell-with-history value filter size)
(let ((pos 0) (most-recent-discard #f))
(object-with-ancestors
((cell (make-filtered-call value filter))
(sequence (make-array size)))
((array? self) #f)
((position self) pos)
((store! self newValue)
(operate-as cell store! self newValue)
(array-set! self pos newValue)
(set! pos (+ pos 1)))
((discard self value)
(set! most-recent-discard value))
((discarded-value self) most-recent-discard)
((print self port)
(format port "#<Cell-with-history ~s>"
(fetch self))))))
(define-access-operation fetch)
(add-setter fetch store!)
(define foo (make-cell 1))
(print foo #f)
⇒ "#<Cell: 1>"
(set (fetch foo) 2)
⇒
(print foo #f)
⇒ "#<Cell: 2>"
(fetch foo)
⇒ 2
</pre></td></tr></table>
<hr size="6">
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Scheme-Syntax-Extension-Packages" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="slib_4.html#Textual-Conversion-Packages" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="slib.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="slib_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="slib_10.html#Procedure-and-Macro-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="slib_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<p>
<font size="-1">
This document was generated by <em>root</em> on <em>June 9, 2010</em> using <a href="http://www.nongnu.org/texi2html/"><em>texi2html 1.82</em></a>.
</font>
<br>
</p>
</body>
</html>
|