manual-Z-H-6.html 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  1. <!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!--
  4. Generated from manual.tex by tex2page, v 2005-03-30
  5. (running on MzScheme 299.101, unix),
  6. (c) Dorai Sitaram,
  7. http://www.ccs.neu.edu/~dorai/tex2page/tex2page-doc.html
  8. -->
  9. <head>
  10. <title>
  11. The Incomplete Scheme 48 Reference Manual for release 1.3
  12. </title>
  13. <link rel="stylesheet" type="text/css" href="manual-Z-S.css" title=default>
  14. <meta name=robots content="noindex,follow">
  15. </head>
  16. <body>
  17. <div id=content>
  18. <div align=right class=navigation><i>[Go to <span><a href="manual.html">first</a>, <a href="manual-Z-H-5.html">previous</a></span><span>, <a href="manual-Z-H-7.html">next</a></span> page<span>; &nbsp;&nbsp;</span><span><a href="manual-Z-H-2.html#node_toc_start">contents</a></span><span><span>; &nbsp;&nbsp;</span><a href="manual-Z-H-13.html#node_index_start">index</a></span>]</i></div>
  19. <p></p>
  20. <a name="node_chap_4"></a>
  21. <h1 class=chapter>
  22. <div class=chapterheading><a href="manual-Z-H-2.html#node_toc_node_chap_4">Chapter 4</a></div><br>
  23. <a href="manual-Z-H-2.html#node_toc_node_chap_4">Module system</a></h1>
  24. <p></p>
  25. <p>
  26. This chapter describes Scheme&nbsp;48's module system.
  27. The module system is unique in the extent to which it
  28. supports both static linking and rapid turnaround during program
  29. development. The design was influenced by Standard ML
  30. modules[<a href="manual-Z-H-12.html#node_bib_7">7</a>] and by the module system for Scheme
  31. Xerox[<a href="manual-Z-H-12.html#node_bib_4">4</a>]. It has also been shaped by the
  32. needs of Scheme&nbsp;48, which is designed to run both on workstations and
  33. on relatively small (less than 1 Mbyte) embedded controllers.</p>
  34. <p>
  35. Except where noted, everything described here is implemented in
  36. Scheme&nbsp;48, and exercised by the Scheme&nbsp;48 implementation and some
  37. application programs.</p>
  38. <p>
  39. Unlike the Common Lisp package system, the module system described
  40. here controls the mapping of names to denotations, not the
  41. mapping of strings to symbols.</p>
  42. <p>
  43. </p>
  44. <a name="node_sec_4.1"></a>
  45. <h2><a href="manual-Z-H-2.html#node_toc_node_sec_4.1">4.1&nbsp;&nbsp;Introduction</a></h2>
  46. <p>The module system supports the structured division of a corpus of
  47. Scheme software into a set of modules. Each module has its own
  48. isolated namespace, with visibility of bindings controlled by module
  49. descriptions written in a special <em>configuration language.</em></p>
  50. <p>
  51. A module may be instantiated multiple times, producing several <em>packages</em>, just as a lambda-expression can be instantiated multiple
  52. times to produce several different procedures. Since single
  53. instantiation is the normal case, we will defer discussion of multiple
  54. instantiation until a later section. For now you can think of a
  55. package as simply a module's internal environment mapping names to
  56. denotations.</p>
  57. <p>
  58. A module exports bindings by providing views onto the underlying
  59. package. Such a view is called a <em>structure</em> (terminology from
  60. Standard ML). One module may provide several different views. A
  61. structure is just a subset of the package's bindings. The particular
  62. set of names whose bindings are exported is the structure's <em>interface</em>.</p>
  63. <p>
  64. A module imports bindings from other modules by either <em>opening</em>
  65. or <em>accessing</em> some structures that are built on other packages.
  66. When a structure is opened, all of its exported bindings are visible
  67. in the client package.
  68. </p>
  69. <p>
  70. For example:
  71. </p>
  72. <pre class=verbatim>(define-structure foo (export a c cons)
  73. (open scheme)
  74. (begin (define a 1)
  75. (define (b x) (+ a x))
  76. (define (c y) (* (b a) y))))
  77. (define-structure bar (export d)
  78. (open scheme foo)
  79. (begin (define (d w) (+ a (c w)))))
  80. </pre><p>
  81. This configuration defines two structures, <tt>foo</tt> and <tt>bar</tt>.
  82. <tt>foo</tt> is a view on a package in which the <tt>scheme</tt> structure's
  83. bindings (including <tt>define</tt> and <tt>+</tt>) are visible, together
  84. with bindings for <tt>a</tt>, <tt>b</tt>,
  85. and <tt>c</tt>. <tt>foo</tt>'s interface is <tt>(export a c cons)</tt>, so of
  86. the bindings in its underlying package, <tt>foo</tt> only exports those
  87. three. Similarly, structure <tt>bar</tt> consists of the binding of <tt>d</tt> from a package in which both <tt>scheme</tt>'s and <tt>foo</tt>'s
  88. bindings are visible. <tt>foo</tt>'s binding of <tt>cons</tt> is imported
  89. from the Scheme structure and then re-exported.</p>
  90. <p>
  91. A module's body, the part following <tt>begin</tt> in the above example,
  92. is evaluated in an isolated lexical scope completely specified by the
  93. package definition's <tt>open</tt> and <tt>access</tt> clauses. In
  94. particular, the binding of the syntactic operator <tt>define-structure</tt>
  95. is not visible unless it comes from some opened structure. Similarly,
  96. bindings from the <tt>scheme</tt> structure aren't visible unless they
  97. become so by <tt>scheme</tt> (or an equivalent structure) being opened.</p>
  98. <p>
  99. </p>
  100. <a name="node_sec_4.2"></a>
  101. <h2><a href="manual-Z-H-2.html#node_toc_node_sec_4.2">4.2&nbsp;&nbsp;The configuration language</a></h2>
  102. <p></p>
  103. <p>
  104. The configuration language consists of top-level defining forms for
  105. modules and interfaces.
  106. Its syntax is given in figure&nbsp;<a href="#node_fig_Temp_3">1</a>.</p>
  107. <p>
  108. </p>
  109. <p>
  110. </p>
  111. <p></p>
  112. <hr>
  113. <p></p>
  114. <a name="node_fig_Temp_3"></a>
  115. <div class=figure align=center><table width=100%><tr><td align=center><p>
  116. </p>
  117. <table border=0><tr><td valign=top >&lt;configuration&gt; <tt>-&gt;</tt>&nbsp;&lt;definition&gt;<sup>*</sup> </td></tr>
  118. <tr><td valign=top ><p>
  119. </p>
  120. <table border=0><tr><td valign=top >&lt;definition&gt; <tt>-&gt;</tt> </td><td valign=top ><tt>(define-structure &lt;name&gt; &lt;interface&gt;
  121. &lt;clause&gt;<sup>*</sup>) </tt></td></tr>
  122. <tr><td valign=top ><tt>-&gt;</tt> </td><td valign=top ><tt>(define-structures ((&lt;name&gt; &lt;interface&gt;)<sup>*</sup>)
  123. &lt;clause&gt;<sup>*</sup>) </tt></td></tr>
  124. <tr><td valign=top ><tt>-&gt;</tt> </td><td valign=top ><tt>(define-interface &lt;name&gt; &lt;interface&gt;) </tt></td></tr>
  125. <tr><td valign=top ><tt>-&gt;</tt> </td><td valign=top ><tt>(define-syntax &lt;name&gt; &lt;transformer-spec&gt;)
  126. </tt></td></tr></table>
  127. </td></tr>
  128. <tr><td valign=top ><table border=0><tr><td valign=top >&lt;clause&gt; <tt>-&gt;</tt> </td><td valign=top ><tt>(open &lt;structure&gt;<sup>*</sup>) </tt></td></tr>
  129. <tr><td valign=top ><tt>-&gt;</tt> </td><td valign=top ><tt>(access &lt;name&gt;<sup>*</sup>) </tt></td></tr>
  130. <tr><td valign=top ><tt>-&gt;</tt> </td><td valign=top ><tt>(begin &lt;program&gt;) </tt></td></tr>
  131. <tr><td valign=top ><tt>-&gt;</tt> </td><td valign=top ><tt>(files &lt;filespec&gt;<sup>*</sup>) </tt></td></tr>
  132. <tr><td valign=top ><tt>-&gt;</tt> </td><td valign=top ><tt>(optimize &lt;optimize-spec&gt;<sup>*</sup>) </tt></td></tr>
  133. <tr><td valign=top ><tt>-&gt;</tt> </td><td valign=top ><tt>(for-syntax &lt;clause&gt;<sup>*</sup>)
  134. </tt></td></tr></table>
  135. </td></tr>
  136. <tr><td valign=top ><table border=0><tr><td valign=top >&lt;interface&gt; <tt>-&gt;</tt> </td><td valign=top ><tt>(export &lt;item&gt;<sup>*</sup>) </tt></td></tr>
  137. <tr><td valign=top ><tt>-&gt;</tt> </td><td valign=top >&lt;name&gt; </td></tr>
  138. <tr><td valign=top ><tt>-&gt;</tt> </td><td valign=top ><tt>(compound-interface &lt;interface&gt;<sup>*</sup>)
  139. </tt></td></tr></table>
  140. </td></tr>
  141. <tr><td valign=top ><table border=0><tr><td valign=top >&lt;item&gt; <tt>-&gt;</tt>&nbsp; &lt;name&gt; </td></tr>
  142. <tr><td valign=top ><tt>-&gt;</tt> </td><td valign=top ><tt>(&lt;name&gt; &lt;type&gt;) </tt></td></tr>
  143. <tr><td valign=top ><tt>-&gt;</tt> </td><td valign=top ><tt>((&lt;name&gt;<sup>*</sup>) &lt;type&gt;)
  144. </tt></td></tr></table>
  145. </td></tr>
  146. <tr><td valign=top ><table border=0><tr><td valign=top >&lt;structure&gt; <tt>-&gt;</tt> </td><td valign=top >&lt;name&gt; </td></tr>
  147. <tr><td valign=top ><tt>-&gt;</tt> </td><td valign=top ><tt>(modify &lt;structure&gt; &lt;modifier&gt;<sup>*</sup>) </tt></td></tr>
  148. <tr><td valign=top ><tt>-&gt;</tt> </td><td valign=top ><tt>(subset &lt;structure&gt; (&lt;name&gt;<sup>*</sup>)) </tt></td></tr>
  149. <tr><td valign=top ><tt>-&gt;</tt> </td><td valign=top ><tt>(with-prefix &lt;structure&gt; &lt;name&gt;)
  150. </tt></td></tr></table>
  151. </td></tr>
  152. <tr><td valign=top ><table border=0><tr><td valign=top >&lt;modifier&gt; <tt>-&gt;</tt> </td><td valign=top ><tt>(expose &lt;name&gt;<sup>*</sup>) </tt></td></tr>
  153. <tr><td valign=top ><tt>-&gt;</tt> </td><td valign=top ><tt>(hide &lt;name&gt;<sup>*</sup>) </tt></td></tr>
  154. <tr><td valign=top ><tt>-&gt;</tt> </td><td valign=top ><tt>(rename (&lt;name&gt;<sub>0</sub> &lt;name&gt;<sub>1</sub>)<sup>*</sup>) </tt></td></tr>
  155. <tr><td valign=top ><tt>-&gt;</tt> </td><td valign=top ><tt>(alias (&lt;name&gt;<sub>0</sub> &lt;name&gt;<sub>1</sub>)<sup>*</sup>) </tt></td></tr>
  156. <tr><td valign=top ><tt>-&gt;</tt> </td><td valign=top ><tt>(prefix &lt;name&gt;)
  157. </tt></td></tr></table>
  158. </td></tr></table>
  159. </td></tr>
  160. <tr><td align=center><b>Figure 1:</b>&nbsp;&nbsp;The configuration language.</td></tr>
  161. <tr><td>
  162. </td></tr></table></div><p></p>
  163. <hr>
  164. <p></p>
  165. <p></p>
  166. <p>
  167. A <a name="node_idx_2"></a><tt>define-structure</tt> form introduces a binding of a name to a
  168. structure. A structure is a view on an underlying package which is
  169. created according to the clauses of the <tt>define-structure</tt> form.
  170. Each structure has an interface that specifies which bindings in the
  171. structure's underlying package can be seen via that structure in other
  172. packages.</p>
  173. <p>
  174. An <tt>open</tt> clause specifies which structures will be opened up for
  175. use inside the new package.
  176. At least one structure must be specified or else it will be impossible to
  177. write any useful programs inside the package, since <tt>define</tt>,
  178. <tt>lambda</tt>, <tt>cons</tt>, etc. will be unavailable.
  179. Packages typically include <tt>scheme</tt>, which exports all bindings
  180. appropriate to Revised<sup>5</sup> Scheme, in an <tt>open</tt> clause.
  181. For building structures that export structures, there is a <tt>defpackage</tt>
  182. package that exports the operators of the configuration language.
  183. Many other structures, such as record and hash table facilities, are also
  184. available in the Scheme&nbsp;48 implementation.</p>
  185. <p>
  186. The <a name="node_idx_4"></a><tt>modify</tt>, <a name="node_idx_6"></a><tt>subset</tt>, and
  187. <a name="node_idx_8"></a><tt>prefix</tt> forms produce new
  188. views on existing structures by renaming or hiding exported names.
  189. <tt>Subset</tt> returns a new structure that exports only the listed names
  190. from its &lt;structure&gt; argument.
  191. <tt>With-prefix</tt> returns a new structure that adds &lt;prefix&gt;
  192. to each of the names exported by the &lt;structure&gt; argument.
  193. For example, if structure <tt>s</tt> exports <tt>a</tt> and <tt>b</tt>,
  194. then
  195. </p>
  196. <pre class=verbatim>(subset s (a))
  197. </pre><p>
  198. exports only <tt>a</tt> and
  199. </p>
  200. <pre class=verbatim>(with-prefix s p/)
  201. </pre><p>
  202. exports <tt>a</tt> as <tt>p/a</tt> and <tt>b</tt> as <tt>p/b</tt>.</p>
  203. <p>
  204. Both <tt>subset</tt> and <tt>with-prefix</tt> are simple macros that
  205. expand into uses of <tt>modify</tt>, a more general renaming form.
  206. In a <tt>modify</tt> structure specification the &lt;command&gt;s are applied to
  207. the names exported
  208. by &lt;structure&gt; to produce a new set of names for the &lt;structure&gt;'s
  209. bindings.
  210. <tt>Expose</tt> makes only the listed names visible.
  211. <tt>Hide</tt> makes all but the listed names visible.
  212. <tt>Rename</tt> makes each &lt;name&gt;<sub>0</sub> visible as &lt;name&gt;<sub>1</sub>
  213. name and not visible as &lt;name&gt;<sub>0</sub> , while
  214. <tt>alias</tt> makes each &lt;name&gt;<sub>0</sub> visible as both &lt;name&gt;<sub>0</sub>
  215. and &lt;name&gt;<sub>1</sub>.
  216. <tt>Prefix</tt> adds &lt;name&gt; to the beginning of each exported name.
  217. The modifiers are applied from right to left. Thus
  218. </p>
  219. <pre class=verbatim>(modify scheme (prefix foo/) (rename (car bus))))
  220. </pre><p>
  221. makes <tt>car</tt> available as <tt>foo/bus</tt>..</p>
  222. <p>
  223. </p>
  224. <p>
  225. The package's body is specified by <tt>begin</tt> and/or <tt>files</tt>
  226. clauses. <tt>begin</tt> and <tt>files</tt> have the same semantics, except
  227. that for <tt>begin</tt> the text is given directly in the package
  228. definition, while for <tt>files</tt> the text is stored somewhere in the
  229. file system. The body consists of a Scheme program, that is, a
  230. sequence of definitions and expressions to be evaluated in order. In
  231. practice, we always use <tt>files</tt> in preference to <tt>begin</tt>; <tt>begin</tt> exists mainly for expository purposes.</p>
  232. <p>
  233. A name's imported binding may be lexically overridden or <em>shadowed</em>
  234. by defining the name using a defining form such as <tt>define</tt>
  235. or <tt>define-syntax</tt>. This will create a new binding without having
  236. any effect on the binding in the opened package. For example, one can
  237. do <tt>(define car 'chevy)</tt> without affecting the binding of the name
  238. <tt>car</tt> in the <tt>scheme</tt> package.</p>
  239. <p>
  240. Assignments (using <tt>set!</tt>) to imported and undefined variables
  241. are not allowed. In order to <tt>set!</tt> a top-level variable, the
  242. package body must contain a <tt>define</tt> form defining that variable.
  243. Applied to bindings from the <tt>scheme</tt> structure, this restriction
  244. is compatible with the requirements of the Revised<sup>5</sup> Scheme report.</p>
  245. <p>
  246. It is an error for two of a package's opened structures to export two
  247. different bindings for the same name. However, the current
  248. implementation does not check for this situation; a name's binding is
  249. always taken from the structure that is listed first within the <tt>open</tt> clause. This may be fixed in the future.</p>
  250. <p>
  251. File names in a <tt>files</tt> clause can be symbols, strings, or lists
  252. (Maclisp-style ``namelists''). A ``<tt>.scm</tt>'' file type suffix is
  253. assumed. Symbols are converted to file names by converting to upper
  254. or lower case as appropriate for the host operating system. A
  255. namelist is an operating-system-independent way to specify a file
  256. obtained from a subdirectory. For example, the namelist <tt>(rts
  257. record)</tt> specifies the file <tt>record.scm</tt> in the <tt>rts</tt>
  258. subdirectory.</p>
  259. <p>
  260. If the <tt>define-structure</tt> form was itself obtained from a file,
  261. then file names in <tt>files</tt> clauses are interpreted relative to the
  262. directory in which the file containing the <tt>define-structure</tt> form
  263. was found. You can't at present put an absolute path name in the <tt>files</tt> list.</p>
  264. <p>
  265. </p>
  266. <a name="node_sec_4.3"></a>
  267. <h2><a href="manual-Z-H-2.html#node_toc_node_sec_4.3">4.3&nbsp;&nbsp;Interfaces</a></h2>
  268. <p><a name="node_idx_10"></a><tt>define-interface</tt></p>
  269. <p>
  270. An interface can be thought of as the type of a structure. In its
  271. basic form it is just a list of variable names, written <tt>(export
  272. <i>name</i> <tt>...</tt>)</tt>. However, in place of
  273. a name one may write <tt>(<i>name</i> <i>type</i>)</tt>, indicating the type
  274. of <i>name</i>'s binding.
  275. The type field is optional, except
  276. that exported macros must be indicated with type <tt>:syntax</tt>.</p>
  277. <p>
  278. Interfaces may be either anonymous, as in the example in the
  279. introduction, or they may be given names by a <tt>define-interface</tt>
  280. form, for example
  281. </p>
  282. <pre class=verbatim>(define-interface foo-interface (export a c cons))
  283. (define-structure foo foo-interface <tt>...</tt>)
  284. </pre><p>
  285. In principle, interfaces needn't ever be named. If an interface
  286. had to be given at the point of a structure's use as well as at the
  287. point of its definition, it would be important to name interfaces in
  288. order to avoid having to write them out twice, with risk of mismatch
  289. should the interface ever change. But they don't.</p>
  290. <p>
  291. Still, there are several reasons to use <tt>define-interface</tt>:
  292. </p>
  293. <ol>
  294. <li><p>It is important to separate the interface definition from the
  295. package definitions when there are multiple distinct structures that
  296. have the same interface -- that is, multiple implementations of the
  297. same abstraction.</p>
  298. <p>
  299. </p>
  300. <li><p>It is conceptually cleaner, and often useful for documentation
  301. purposes, to separate a module's specification (interface) from its
  302. implementation (package).</p>
  303. <p>
  304. </p>
  305. <li><p>Our experience is that configurations that are separated into
  306. interface definitions and package definitions are easier to read; the
  307. long lists of exported bindings just get in the way most of the time.
  308. </p>
  309. </ol><p></p>
  310. <p>
  311. The <a name="node_idx_12"></a><tt>compound-interface</tt> operator forms an interface that is the
  312. union of two or more component interfaces. For example,
  313. </p>
  314. <pre class=verbatim>(define-interface bar-interface
  315. (compound-interface foo-interface (export mumble)))
  316. </pre><p>
  317. defines <tt>bar-interface</tt> to be <tt>foo-interface</tt> with the name
  318. <tt>mumble</tt> added.</p>
  319. <p>
  320. </p>
  321. <a name="node_sec_4.4"></a>
  322. <h2><a href="manual-Z-H-2.html#node_toc_node_sec_4.4">4.4&nbsp;&nbsp;Macros</a></h2>
  323. <p>Hygienic macros, as described in
  324. [<a href="manual-Z-H-12.html#node_bib_2">2</a>,&nbsp;<a href="manual-Z-H-12.html#node_bib_3">3</a>], are implemented.
  325. Structures may export macros; auxiliary names introduced into the
  326. expansion are resolved in the environment of the macro's definition.</p>
  327. <p>
  328. For example, the <tt>scheme</tt> structure's <tt>delay</tt> macro
  329. is defined by the rewrite rule
  330. </p>
  331. <pre class=verbatim>(delay <i>exp</i>) %
  332. %
  333. <tt> -- &gt;</tt>%
  334. (make-promise (lambda () <i>exp</i>)).
  335. </pre><p>
  336. The variable <tt>make-promise</tt> is defined in the <tt>scheme</tt>
  337. structure's underlying package, but is not exported. A use of the
  338. <tt>delay</tt> macro, however, always accesses the correct definition
  339. of <tt>make-promise</tt>. Similarly, the <tt>case</tt> macro expands into
  340. uses of <tt>cond</tt>, <tt>eqv?</tt>, and so on. These names are exported
  341. by <tt>scheme</tt>, but their correct bindings will be found even if they
  342. are shadowed by definitions in the client package.</p>
  343. <p>
  344. </p>
  345. <a name="node_sec_4.5"></a>
  346. <h2><a href="manual-Z-H-2.html#node_toc_node_sec_4.5">4.5&nbsp;&nbsp;Higher-order modules</a></h2>
  347. <p>There are <tt>define-module</tt> and <tt>define</tt> forms for
  348. defining modules that are intended to be instantiated multiple times.
  349. But these are pretty kludgey -- for example, compiled code isn't
  350. shared between the instantiations -- so we won't describe them yet.
  351. If you must know, figure it out from the following grammar.
  352. </p>
  353. <div align=center><table><tr><td>
  354. <table border=0><tr><td valign=top >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;definition&gt; <tt>-&gt;</tt> </td><td valign=top ><tt>(define-module (&lt;name&gt; (&lt;name&gt; &lt;interface&gt;)<sup>*</sup>) </tt></td></tr>
  355. <tr><td valign=top ></td><td valign=top >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;definition&gt;<sup>*</sup> </td></tr>
  356. <tr><td valign=top ></td><td valign=top >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;name&gt;<tt>) </tt></td></tr>
  357. <tr><td valign=top ><tt>-&gt;</tt> </td><td valign=top ><tt>(define &lt;name&gt; (&lt;name&gt; &lt;name&gt;<sup>*</sup>))
  358. </tt></td></tr></table>
  359. </td></tr></table></div>
  360. <p>
  361. </p>
  362. <a name="node_sec_4.6"></a>
  363. <h2><a href="manual-Z-H-2.html#node_toc_node_sec_4.6">4.6&nbsp;&nbsp;Compiling and linking</a></h2>
  364. <p>Scheme&nbsp;48 has a static linker that produces stand-alone heap images
  365. from module descriptions. The programmer specifies a particular procedure in a
  366. particular structure to be the image's startup procedure (entry
  367. point), and the linker traces dependency links as given by <tt>open</tt>
  368. and <tt>access</tt> clauses to determine the composition of the heap
  369. image.</p>
  370. <p>
  371. There is not currently any provision for separate compilation; the
  372. only input to the static linker is source code. However, it will not
  373. be difficult to implement separate compilation. The unit of
  374. compilation is one module (not one file). Any opened or accessed
  375. structures from which macros are obtained must be processed to the
  376. extent of extracting its macro definitions. The compiler knows from
  377. the interface of an opened or accessed structure which of its exports
  378. are macros. Except for macros, a module may be compiled without any
  379. knowledge of the implementation of its opened and accessed structures.
  380. However, inter-module optimization may be available as an option.</p>
  381. <p>
  382. The main difficulty with separate compilation is resolution of
  383. auxiliary bindings introduced into macro expansions. The module
  384. compiler must transmit to the loader or linker the search path by
  385. which such bindings are to be resolved. In the case of the <tt>delay</tt>
  386. macro's auxiliary <tt>make-promise</tt> (see example above), the loader
  387. or linker needs to know that the desired binding of <tt>make-promise</tt>
  388. is the one apparent in <tt>delay</tt>'s defining package, not in the
  389. package being loaded or linked.</p>
  390. <p>
  391. </p>
  392. <p>
  393. </p>
  394. <a name="node_sec_4.7"></a>
  395. <h2><a href="manual-Z-H-2.html#node_toc_node_sec_4.7">4.7&nbsp;&nbsp;Semantics of configuration mutation</a></h2>
  396. <p>During program development it is often desirable to make changes to
  397. packages and interfaces. In static languages it may be necessary to
  398. recompile and re-link a program in order for such changes to be
  399. reflected in a running system. Even in interactive Common Lisp
  400. implementations, a change to a package's exports often requires
  401. reloading clients that have already mentioned names whose bindings
  402. change. Once <tt>read</tt> resolves a use of a name to a symbol, that
  403. resolution is fixed, so a change in the way that a name resolves to a
  404. symbol can only be reflected by re-<tt>read</tt>ing all such references.</p>
  405. <p>
  406. The Scheme&nbsp;48 development environment supports rapid turnaround in
  407. modular program development by allowing mutations to a program's
  408. configuration, and giving a clear semantics to such mutations. The
  409. rule is that variable bindings in a running program are always
  410. resolved according to current structure and interface bindings, even
  411. when these bindings change as a result of edits to the configuration.
  412. For example, consider the following:
  413. </p>
  414. <pre class=verbatim>(define-interface foo-interface (export a c))
  415. (define-structure foo foo-interface
  416. (open scheme)
  417. (begin (define a 1)
  418. (define (b x) (+ a x))
  419. (define (c y) (* (b a) y))))
  420. (define-structure bar (export d)
  421. (open scheme foo)
  422. (begin (define (d w) (+ (b w) a))))
  423. </pre><p>
  424. This program has a bug. The variable <tt>b</tt>, which is free in the
  425. definition of <tt>d</tt>, has no binding in <tt>bar</tt>'s package. Suppose
  426. that <tt>b</tt> was supposed to be exported by <tt>foo</tt>, but was omitted
  427. from <tt>foo-interface</tt> by mistake. It is not necessary to
  428. re-process <tt>bar</tt> or any of <tt>foo</tt>'s other clients at this point.
  429. One need only change <tt>foo-interface</tt> and inform the development
  430. system of that change (using, say, an appropriate Emacs command),
  431. and <tt>foo</tt>'s binding of <tt>b</tt> will be found when procedure <tt>d</tt> is called.</p>
  432. <p>
  433. Similarly, it is also possible to replace a structure; clients of the
  434. old structure will be modified so that they see bindings from the new
  435. one. Shadowing is also supported in the same way. Suppose that a
  436. client package <em>C</em> opens a structure <tt>foo</tt> that exports a name
  437. <tt>x</tt>, and <tt>foo</tt>'s implementation obtains the binding of <tt>x</tt>
  438. as an import from some other structure <tt>bar</tt>. Then <em>C</em> will see
  439. the binding from <tt>bar</tt>. If one then alters <tt>foo</tt> so that it
  440. shadows <tt>bar</tt>'s binding of <tt>x</tt> with a definition of its own,
  441. then procedures in <em>C</em> that reference <tt>x</tt> will automatically see
  442. <tt>foo</tt>'s definition instead of the one from <tt>bar</tt> that they saw
  443. earlier.</p>
  444. <p>
  445. This semantics might appear to require a large amount of computation
  446. on every variable reference: The specified behavior requires scanning
  447. the package's list of opened structures, examining their interfaces,
  448. on every variable reference, not just at compile time. However, the
  449. development environment uses caching with cache invalidation to make
  450. variable references fast.</p>
  451. <p>
  452. </p>
  453. <a name="node_sec_4.8"></a>
  454. <h2><a href="manual-Z-H-2.html#node_toc_node_sec_4.8">4.8&nbsp;&nbsp;Command processor support</a></h2>
  455. <p></p>
  456. <p>
  457. While it is possible to use the Scheme&nbsp;48 static linker for program
  458. development, it is far more convenient to use the development
  459. environment, which supports rapid turnaround for program changes. The
  460. programmer interacts with the development environment through a <em>command processor</em>. The command processor is like the usual Lisp
  461. read-eval-print loop in that it accepts Scheme forms to evaluate.
  462. However, all meta-level operations, such as exiting the Scheme system
  463. or requests for trace output, are handled by <em>commands,</em> which are
  464. lexically distinguished from Scheme forms. This arrangement is
  465. borrowed from the Symbolics Lisp Machine system, and is reminiscent of
  466. non-Lisp debuggers. Commands are a little easier to type than Scheme
  467. forms (no parentheses, so you don't have to shift), but more
  468. importantly, making them distinct from Scheme forms ensures that
  469. programs' namespaces aren't cluttered with inappropriate bindings.
  470. Equivalently, the command set is available for use regardless of what
  471. bindings happen to be visible in the current program. This is
  472. especially important in conjunction with the module system, which puts
  473. strict controls on visibility of bindings.</p>
  474. <p>
  475. The Scheme&nbsp;48 command processor supports the module system with a
  476. variety of special commands. For commands that require structure
  477. names, these names are resolved in a designated configuration package
  478. that is distinct from the current package for evaluating Scheme forms
  479. given to the command processor. The command processor interprets
  480. Scheme forms in a particular current package, and there are commands
  481. that move the command processor between different packages.</p>
  482. <p>
  483. Commands are introduced by a comma (<tt>,</tt>) and end at the end of
  484. line. The command processor's prompt consists of the name of the
  485. current package followed by a greater-than (<tt>&gt;</tt>).</p>
  486. <p>
  487. </p>
  488. <dl><dt></dt><dd>
  489. </dd><dt></dt><dd><tt>,open <i>structure</i><sup>*</sup></tt> <br>
  490. The <tt>,open</tt> command opens new structures in the current
  491. package, as if the package's definition's <tt>open</tt> clause
  492. had listed <i>structure</i>.
  493. As with <tt>open</tt> clauses the visible names can be modified,
  494. as in
  495. <pre class=verbatim>,open (subset foo (bar baz))
  496. </pre><p>
  497. which only makes the <tt>bar</tt> and <tt>baz</tt> bindings from
  498. structure <tt>foo</tt> visible.</p>
  499. <p>
  500. </p>
  501. </dd><dt></dt><dd><tt>,config</tt> <br>
  502. The <tt>,config</tt> command sets the command processor's current
  503. package to be the current configuration package. Forms entered at
  504. this point are interpreted as being configuration language forms,
  505. not Scheme forms.<p>
  506. </p>
  507. </dd><dt></dt><dd><tt>,config <i>command</i></tt> <br>
  508. This form of the <tt>,config</tt> command executes another command in
  509. the current configuration package. For example,
  510. <pre class=verbatim>,config ,load foo.scm
  511. </pre><p>
  512. interprets configuration language forms from the file <tt>foo.scm</tt> in the current configuration package.</p>
  513. <p>
  514. </p>
  515. </dd><dt></dt><dd><tt>,config-package-is <i>struct-name</i></tt> <br>
  516. The <tt>,config-package-is</tt> command designates a new configuration
  517. package for use by the <tt>,config</tt> command and resolution of
  518. <i>struct-name</i>s for other commands such as <tt>,in</tt> and
  519. <tt>,open</tt>. See
  520. section&nbsp;<a href="#node_sec_4.9">4.9</a>
  521. for information on making new configuration packages.<p>
  522. </p>
  523. </dd><dt></dt><dd><tt>,in <i>struct-name</i></tt> <br>
  524. The <tt>,in</tt> command moves the command processor to a specified
  525. structure's underlying package. For example:
  526. <pre class=verbatim>user&gt; ,config
  527. config&gt; (define-structure foo (export a)
  528. (open scheme))
  529. config&gt; ,in foo
  530. foo&gt; (define a 13)
  531. foo&gt; a
  532. 13
  533. </pre><p>
  534. In this example the command processor starts in a package called
  535. <tt>user</tt>, but the <tt>,config</tt> command moves it into the
  536. configuration package, which has the name <tt>config</tt>. The <tt>define-structure</tt> form binds, in <tt>config</tt>, the name <tt>foo</tt> to
  537. a structure that exports <tt>a</tt>. Finally, the command <tt>,in
  538. foo</tt> moves the command processor into structure <tt>foo</tt>'s
  539. underlying package.</p>
  540. <p>
  541. A package's body isn't executed (evaluated) until the package is
  542. <em>loaded</em>, which is accomplished by the <tt>,load-package</tt>
  543. command.</p>
  544. <p>
  545. </p>
  546. </dd><dt></dt><dd><tt>,in <i>struct-name</i> <i>command</i></tt> <br>
  547. This form of the <tt>,in</tt> command executes a single command in the
  548. specified package without moving the command processor into that
  549. package. Example:
  550. <pre class=verbatim>,in mumble (cons 1 2)
  551. ,in mumble ,trace foo
  552. </pre><p></p>
  553. <p>
  554. </p>
  555. </dd><dt></dt><dd><tt>,user [<i>command</i>]</tt> <br>
  556. This is similar to the <tt>,config</tt> and <tt>,in</tt> commands. It
  557. moves to or executes a command in the user package (which is the
  558. default package when the Scheme&nbsp;48 command processor starts).<p>
  559. </p>
  560. </dd><dt></dt><dd><tt>,user-package-is <i>name</i></tt> <br>
  561. The <tt>,user-package-is</tt> command designates a new user
  562. package for use by the <tt>,user</tt> command.<p>
  563. </p>
  564. </dd><dt></dt><dd><tt>,load-package <i>struct-name</i></tt> <br>
  565. The <tt>,load-package</tt> command ensures that the specified structure's
  566. underlying package's program has been loaded. This
  567. consists of (1) recursively ensuring that the packages of any
  568. opened or accessed structures are loaded, followed by (2)
  569. executing the package's body as specified by its definition's <tt>begin</tt> and <tt>files</tt> forms.
  570. <p>
  571. </p>
  572. </dd><dt></dt><dd><tt>,reload-package <i>struct-name</i></tt> <br>
  573. This command re-executes the structure's package's program. It
  574. is most useful if the program comes from a file or files, when
  575. it will update the package's bindings after mutations to its
  576. source file.<p>
  577. </p>
  578. </dd><dt></dt><dd><tt>,load <i>filespec</i> <tt>...</tt></tt> <br>
  579. The <tt>,load</tt> command executes forms from the specified file or
  580. files in the current package. <tt>,load <i>filespec</i></tt> is similar
  581. to <tt>(load &quot;<i>filespec</i>&quot;)</tt>
  582. except that the name <tt>load</tt> needn't be bound in the current
  583. package to Scheme's <tt>load</tt> procedure.<p>
  584. </p>
  585. </dd><dt></dt><dd><tt>,for-syntax [<i>command</i>]</tt> <br>
  586. This is similar to the <tt>,config</tt> and <tt>,in</tt> commands. It
  587. moves to or executes a command in the current package's ``package
  588. for syntax,'' which is the package in which the forms <em>f</em> in
  589. <tt>(define-syntax <i>name</i> <i>f</i>)</tt> are evaluated.<p>
  590. </p>
  591. </dd><dt></dt><dd><tt>,new-package</tt> <br>
  592. The <tt>,new-package</tt> command creates a new package, in which only
  593. the standard Scheme bindings are visible, and moves the command
  594. processor to it.<p>
  595. </p>
  596. </dd><dt></dt><dd><tt>,structure <i>name</i> <i>interface</i></tt> <br>
  597. The <tt>,structure</tt> command defines <i>name</i> in the
  598. configuration package to be a structure with interface
  599. <i>interface</i> based on the current package.<p>
  600. </p>
  601. </dd></dl><p></p>
  602. <p>
  603. </p>
  604. <a name="node_sec_4.9"></a>
  605. <h2><a href="manual-Z-H-2.html#node_toc_node_sec_4.9">4.9&nbsp;&nbsp;Configuration packages</a></h2>
  606. <p></p>
  607. <p>
  608. It is possible to set up multiple configuration packages. The default
  609. configuration package opens the following structures:
  610. </p>
  611. <ul>
  612. <li><p><tt>module-system</tt>, which exports <tt>define-structure</tt> and the
  613. other configuration language keywords, as well as standard types
  614. and type constructors (<tt>:syntax</tt>, <tt>:value</tt>, <tt>proc</tt>, etc.).
  615. </p>
  616. <li><p><tt>built-in-structures</tt>, which exports structures that are
  617. built into the initial Scheme&nbsp;48 image; these include
  618. <tt>scheme</tt>, <tt>threads</tt>, <tt>tables</tt>, and <tt>records</tt>.
  619. </p>
  620. <li><p><tt>more-structures</tt>, which exports additional structures that
  621. are available in the development environment.
  622. A complete listing
  623. can be found in the definition of <tt>more-structures-interface</tt>
  624. at the end of the file <tt>scheme/packages.scm</tt>.
  625. </p>
  626. </ul><p>
  627. Note that it does not open <tt>scheme</tt>.</p>
  628. <p>
  629. You can define additional configuration packages by making a package
  630. that opens <tt>module-system</tt> and, optionally,
  631. <tt>built-in-structures</tt>,
  632. <tt>more-structures</tt>, or other structures that
  633. export structures and interfaces.</p>
  634. <p>
  635. For example:
  636. </p>
  637. <pre class=verbatim>&gt; ,config (define-structure foo (export)
  638. (open module-system
  639. built-in-structures
  640. more-structures))
  641. &gt; ,in foo
  642. foo&gt; (define-structure x (export a b)
  643. (open scheme)
  644. (files x))
  645. foo&gt;
  646. </pre><p></p>
  647. <p>
  648. Unfortunately, the above example does not work.
  649. The problem is that every environment in which
  650. <tt>define-structure</tt> is defined must also have a way to
  651. create ``reflective towers'' (a misnomer; a better name would be
  652. ``syntactic towers'').
  653. A new reflective tower is required whenever a new environment is created for
  654. compiling the source code in the package associated with a new structure.
  655. The environment's tower is used at compile time for
  656. evaluating the <i>macro-source</i> in
  657. </p>
  658. <pre class=verbatim>(define-syntax <i>name</i> <i>macro-source</i>)
  659. (let-syntax ((<i>name</i> <i>macro-source</i>) <i>...</i>) <i>body</i>)
  660. </pre><p>
  661. and so forth.
  662. It is a ``tower'' because that environment, in turn, has to say what environment
  663. to use if <tt>macro-source</tt> itself contains a use of <tt>let-syntax</tt>.</p>
  664. <p>
  665. The simplest way to provide a tower maker is to pass on the one used by
  666. an existing configuration package.
  667. The special form <tt>export-reflective-tower</tt> creates an interface
  668. that exports a configuration package's tower.
  669. The following example uses <tt>export-reflective-tower</tt> and
  670. the <tt>,structure</tt> command to obtain a tower maker and create a new
  671. configuration environment.</p>
  672. <p>
  673. </p>
  674. <pre class=verbatim>&gt; ,config ,structure t (export-reflective-tower-maker)
  675. &gt; ,config (define-structure foo (export)
  676. (open module-system
  677. t
  678. built-in-structures
  679. more-structures))
  680. </pre><p></p>
  681. <p>
  682. </p>
  683. <p>
  684. </p>
  685. <a name="node_sec_4.10"></a>
  686. <h2><a href="manual-Z-H-2.html#node_toc_node_sec_4.10">4.10&nbsp;&nbsp;Discussion</a></h2>
  687. <p>This module system was not designed as the be-all and end-all of
  688. Scheme module systems; it was only intended to help us
  689. organize the Scheme&nbsp;48 system. Not only does the module system
  690. help avoid name clashes by keeping different subsystems in different
  691. namespaces, it has also helped us to tighten up and generalize
  692. Scheme&nbsp;48's internal interfaces. Scheme&nbsp;48 is unusual among Lisp
  693. implementations in admitting many different possible modes of
  694. operation. Examples of such multiple modes include the following:
  695. </p>
  696. <ul>
  697. <li><p>Linking can be either static or dynamic.</p>
  698. <p>
  699. </p>
  700. <li><p>The development environment (compiler, debugger, and command
  701. processor) can run either in the same address space as the program
  702. being developed or in a different address space. The environment and
  703. user program may even run on different processors under different
  704. operating systems[<a href="manual-Z-H-12.html#node_bib_8">8</a>].</p>
  705. <p>
  706. </p>
  707. <li><p>The virtual machine can be supported by either
  708. of two implementations of its implementation language, Prescheme.
  709. </p>
  710. </ul><p>
  711. The module system has been helpful in organizing these multiple modes.
  712. By forcing us to write down interfaces and module dependencies, the
  713. module system helps us to keep the system clean, or at least to keep
  714. us honest about how clean or not it is.</p>
  715. <p>
  716. The need to make structures and interfaces second-class instead of
  717. first-class results from the requirements of static program analysis:
  718. it must be possible for the compiler and linker to expand macros and
  719. resolve variable bindings before the program is executed. Structures
  720. could be made first-class (as in FX[<a href="manual-Z-H-12.html#node_bib_10">10</a>]) if a
  721. type system were added to Scheme and the definitions of exported
  722. macros were defined in interfaces instead of in module bodies, but
  723. even in that case types and interfaces would remain second-class.</p>
  724. <p>
  725. The prohibition on assignment to imported bindings makes substitution
  726. a valid optimization when a module is compiled as a block. The block
  727. compiler first scans the entire module body, noting which variables
  728. are assigned. Those that aren't assigned (only <tt>define</tt>d) may be
  729. assumed never assigned, even if they are exported. The optimizer can
  730. then perform a very simple-minded analysis to determine automatically
  731. that some procedures can and should have their calls compiled in line.</p>
  732. <p>
  733. The programming style encouraged by the module system is consistent
  734. with the unextended Scheme language. Because module system features
  735. do not generally show up within module bodies, an individual module
  736. may be understood by someone who is not familiar with the module
  737. system. This is a great aid to code presentation and portability. If
  738. a few simple conditions are met (no name conflicts between packages,
  739. and use of <tt>files</tt> in preference to
  740. <tt>begin</tt>), then a multi-module program can be loaded into a Scheme
  741. implementation that does not support the module system. The Scheme&nbsp;48
  742. static linker satisfies these conditions, and can therefore run in
  743. other Scheme implementations. Scheme&nbsp;48's bootstrap process, which is
  744. based on the static linker, is therefore nonincestuous. This
  745. contrasts with most other integrated programming environments, such as
  746. Smalltalk-80, where the system can only be built using an existing
  747. version of the system itself.</p>
  748. <p>
  749. Like ML modules, but unlike Scheme Xerox modules, this module system
  750. is compositional. That is, structures are constructed by single
  751. syntactic units that compose existing structures with a body of code.
  752. In Scheme Xerox, the set of modules that can contribute to an
  753. interface is open-ended -- any module can contribute bindings to any
  754. interface whose name is in scope. The module system implementation is
  755. a cross-bar that channels definitions from modules to interfaces. The
  756. module system described here has simpler semantics and makes
  757. dependencies easier to trace. It also allows for higher-order
  758. modules, which Scheme Xerox considers unimportant.</p>
  759. <p>
  760. </p>
  761. <p>
  762. </p>
  763. <p>
  764. </p>
  765. <div align=right class=navigation><i>[Go to <span><a href="manual.html">first</a>, <a href="manual-Z-H-5.html">previous</a></span><span>, <a href="manual-Z-H-7.html">next</a></span> page<span>; &nbsp;&nbsp;</span><span><a href="manual-Z-H-2.html#node_toc_start">contents</a></span><span><span>; &nbsp;&nbsp;</span><a href="manual-Z-H-13.html#node_index_start">index</a></span>]</i></div>
  766. </div>
  767. </body>
  768. </html>