language.texi 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. @node Module configuration language
  2. @section Module configuration language
  3. @cindex configuration language
  4. @cindex module language
  5. Scheme48's module system is used through a @dfn{module configuration
  6. language}. @emph{The configuration language is entirely separate from
  7. Scheme.} Typically, in one configuration, or set of components that
  8. compose a program, there is an @file{interfaces.scm} file that defines
  9. all of the interfaces used by the configuration, and there is also a
  10. @file{packages.scm} file that defines all of the packages & structures
  11. that compose it. Note that modules are not necessarily divided into
  12. files or restricted to one file: modules may include arbitrarily many
  13. files, and modules' code may also be written in-line to structure
  14. expressions (see the @code{begin} package clause below), although that
  15. is usually only for expository purposes and trivial modules.
  16. @cindex package clauses
  17. Structures are always created with corresponding @dfn{package clauses}.
  18. Each clause specifies an attribute of the package that underlies the
  19. structure or structures created using the clauses. There are several
  20. different types of clauses:
  21. @cindex opening structures
  22. @cindex structures, opening
  23. @cindex accessing structures
  24. @cindex structures, accessing
  25. @stindex structure-refs
  26. @bnindex structure-ref
  27. @deffn {package clause} open structure @dots{}
  28. @deffnx {package clause} access structure @dots{}
  29. @code{Open} specifies that the package should open each of the listed
  30. structures, whose packages will be loaded if necessary. @code{Access}
  31. specifies that each listed structure should be accessible using the
  32. @code{(structure-ref @var{structure} @var{identifier})} special form,
  33. which evaluates to the value of @var{identifier} exported by the
  34. accessed structure @var{structure}. @code{Structure-ref} is available
  35. from the @code{structure-refs} structure. Each @var{structure} passed
  36. to @code{access} is not opened, however; the bindings exported thereby
  37. are available only using @code{structure-ref}. While the qualified
  38. @code{structure-ref} mechanism is no longer useful in the presence of
  39. modified structures (see below on @code{modify}, @code{subset}, &
  40. @code{with-prefix}), some old code still uses it, and @code{access} is
  41. also useful to force that the listed structures' packages be loaded
  42. without cluttering the namespace of the package whose clauses the
  43. @code{access} clause is among.
  44. @end deffn
  45. @deffn {package clause} for-syntax package-clause @dots{}
  46. Specifies a set of package clauses for the next floor of the reflective
  47. tower; @pxref{Macros in concert with modules}.
  48. @end deffn
  49. @deffn {package clause} files file-specifier @dots{}
  50. @deffnx {package clause} begin code @dots{}
  51. @code{Files} and @code{begin} specify the package's code. @code{Files}
  52. takes a sequence of namelists for the filenames of files that contain
  53. code; @pxref{Filenames}. @code{Begin} accepts in-line program code.
  54. @end deffn
  55. @cindex compiler optimization
  56. @cindex optimizer
  57. @deffn {package clause} optimize optimizer-specifier @dots{}
  58. @deffnx {package clause} integrate [on?]
  59. @code{Optimize} clauses request that specified compiler optimizers be
  60. applied to the code. (Actually, `optimizer' is a misnomer. The
  61. @code{optimize} clause may specify arbitrary passes that the compiler
  62. can be extended with.)
  63. @c @xref{Extending the compiler}.)
  64. @code{Integrate} clauses specify whether or not integrable procedures
  65. from other modules, most notably Scheme primitives such as @code{car}
  66. or @code{vector-ref}, should actually be integrated in this package.
  67. This is by default on. Most modules should leave it on for any
  68. reasonable performance; only a select few, into which code is intended
  69. to be dynamically loaded frequently and in which redefinition of
  70. imported procedures is common, need turn this off. The value of the
  71. argument to @code{integrate} clauses should be a literal boolean, @ie{}
  72. @code{#t} or @code{#f}; if no argument is supplied, integration is
  73. enabled by default.
  74. @cindex @code{auto-integrate} optimizer
  75. @cindex procedure integration
  76. @cindex integrated procedures
  77. @cindex in-line procedures
  78. @cindex @code{flat-environments} optimizer
  79. @cindex closure flattening
  80. @cindex flat closures
  81. @cindex environment flattening
  82. @cindex flat environments
  83. Currently, the only optimizer built-in to Scheme48 is the automatic
  84. procedure integrator, or @code{auto-integrate}, which attempts stronger
  85. type reconstruction than is attempted with most code (@pxref{Static
  86. type system}) and selects procedures below a certain size to be made
  87. integrable (so that the body will be compiled in-line in all known call
  88. sites). Older versions of Scheme48 also provided another optimizer,
  89. @code{flat-environments}, which would flatten certain lexical closure
  90. environments, rather than using a nested environment structure. Now,
  91. however, Scheme48's byte code compiler always flattens environments;
  92. specifying @code{flat-environments} in an @code{optimize} clause does
  93. nothing.
  94. @end deffn
  95. @cindex structure definition forms
  96. A configuration is a sequence of definitions. There are definition
  97. forms for only structures and interfaces.
  98. @deffn {configuration form} define-structure name interface package-clause @dots{}
  99. @deffnx {configuration form} define-structures ((name interface) @dots{}) package-clause @dots{}
  100. @code{Define-structure} creates a package with the given package
  101. clauses and defines @var{name} to be the single view atop it, with the
  102. interface @var{interface}. @code{Define-structures} also creates a
  103. package with the given package clauses; upon that package, it defines
  104. each @var{name} to be a view on it with the corresponding interface.
  105. @end deffn
  106. @deffn {configuration form} define-module (name parameter @dots{}) definition @dots{} result
  107. @deffnx {configuration form} def name @dots{} (parameterized-module argument @dots{})
  108. @code{Define-module} defines @var{name} to be a parameterized module
  109. that accepts the given parameters.
  110. @end deffn
  111. @cindex interface definition forms
  112. @deffn {configuration form} define-interface name interface
  113. Defines @var{name} to be the interface that @var{interface} evaluates
  114. to. @var{Interface} may either be an interface constructor application
  115. or simply a name defined to be an interface by some prior
  116. @code{define-interface} form.
  117. @end deffn
  118. @cindex simple interfaces
  119. @deffn {interface constructor} export export-specifier @dots{}
  120. @code{Export} constructs a simple interface with the given export
  121. specifiers. The export specifiers specify names to export and their
  122. corresponding static types. Each @var{export-specifier} should have
  123. one of the following forms:
  124. @table @code
  125. @item @var{symbol}
  126. in which case @var{symbol} is exported with the most general value type;
  127. @item (@var{symbol} @var{type})
  128. in which case @var{symbol} is exported with the given type; or
  129. @item ((@var{symbol} @dots{}) @var{type})
  130. in which case each @var{symbol} is exported with the same given type
  131. @end table
  132. For details on the valid forms of @var{type}, @pxref{Static type
  133. system}. @strong{Note:} All macros listed in interfaces @emph{must} be
  134. explicitly annotated with the type @code{:syntax}; otherwise they would
  135. be exported with a Scheme value type, which would confuse the compiler,
  136. because it would not realize that they are macros: it would instead
  137. treat them as ordinary variables that have regular run-time values.
  138. @end deffn
  139. @cindex compound interfaces
  140. @deffn {interface constructor} compound-interface interface @dots{}
  141. This constructs an interface that contains all of the export specifiers
  142. from each @var{interface}.
  143. @end deffn
  144. @cindex anonymous structures
  145. Structures may also be constructed anonymously; this is typically most
  146. useful in passing them to or returning them from parameterized modules.
  147. @deffn {structure constructor} structure interface package-clauses
  148. @deffnx {structure constructor} structures (interface @dots{}) package-clauses
  149. @code{Structure} creates a package with the given clauses and evaluates
  150. to a structure over it with the given interface. @code{Structures}
  151. does similarly, but it evaluates to a number of structures, each with
  152. the corresponding @var{interface}.
  153. @end deffn
  154. @cindex modified structures
  155. @cindex modified interfaces
  156. @deffn {structure constructor} subset structure (name @dots{})
  157. @deffnx {structure constructor} with-prefix structure name
  158. @deffnx {structure constructor} modify structure modifier @dots{}
  159. These modify the interface of @var{structure}. @code{Subset} evaluates
  160. to a structure that exports only @var{name} @dots{}, excluding any
  161. other names that @var{structure} exported. @code{With-prefix} adds a
  162. prefix @var{name} to every name listed in @var{structure}'s interface.
  163. Both @code{subset} and @code{with-prefix} are syntactic sugar for the
  164. more general @code{modify}, which applies the modifier commands in a
  165. strictly right-to-left or last-to-first order. @strong{Note:} These
  166. all @emph{denote new structures with new interfaces}; they do not
  167. destructively modify existing structures' interfaces.
  168. @end deffn
  169. @deffn {modifier command} prefix name
  170. @deffnx {modifier command} expose name @dots{}
  171. @deffnx {modifier command} hide name @dots{}
  172. @deffnx {modifier command} alias (from to) @dots{}
  173. @deffnx {modifier command} rename (from to) @dots{}
  174. @code{Prefix} adds the prefix @var{name} to every exported name in the
  175. structure's interface. @code{Expose} exposes only @var{name} @dots{};
  176. any other names are hidden. @code{Hide} hides @var{name} @dots{}.
  177. @code{Alias} exports each @var{to} as though it were the corresponding
  178. @var{from}, as well as each @var{from}. @code{Rename} exports each
  179. @var{to} as if it were the corresponding @var{from}, but it also hides
  180. the corresponding @var{from}.
  181. Examples:
  182. @lisp
  183. (modify @var{structure}
  184. (prefix foo:)
  185. (expose bar baz quux))@end lisp
  186. @noindent
  187. makes only @code{foo:bar}, @code{foo:baz}, and @code{foo:quux},
  188. available.
  189. @lisp
  190. (modify @var{structure}
  191. (hide baz:quux)
  192. (prefix baz:)
  193. (rename (foo bar)
  194. (mumble frotz))
  195. (alias (gargle mumph)))@end lisp
  196. @noindent
  197. exports @code{baz:gargle} as what was originally @code{mumble},
  198. @code{baz:mumph} as an alias for what was originally @code{gargle},
  199. @code{baz:frotz} as what was originally @code{mumble}, @code{baz:bar}
  200. as what was originally @code{foo}, @emph{not} @code{baz:quux} --- what
  201. was originally simply @code{quux} ---, and everything else that
  202. @var{structure} exported, but with a prefix of @code{baz:}.
  203. @end deffn
  204. There are several simple utilities for binding variables to structures
  205. locally and returning multiple structures not necessarily over the same
  206. package (@ie{} not with @code{structures}). These are all valid in the
  207. bodies of @code{define-module} and @code{def} forms, and in the
  208. arguments to parameterized modules and @code{open} package clauses.
  209. @deffn syntax begin body
  210. @deffnx syntax let ((name value) @dots{}) body
  211. @deffnx syntax receive (name @dots{}) producer body
  212. @deffnx syntax values value @dots{}
  213. These are all as in ordinary Scheme. Note, however, that there is no
  214. reasonable way by which to use @code{values} except to call it, so it
  215. is considered a syntax; also note that @code{receive} may not receive
  216. a variable number of values --- @ie{} there are no `rest lists' ---,
  217. because list values in the configuration language are nonsensical.
  218. @end deffn
  219. @cindex macros in the module configuration language
  220. @cindex configuration language macros
  221. @cindex module language macros
  222. Finally, the configuration language also supports syntactic extensions,
  223. or macros, as in Scheme.
  224. @deffn {configuration form} define-syntax name transformer-specifier
  225. Defines the syntax transformer @var{name} to be the transformer
  226. specified by @var{transformer-specifier}. @var{Transformer-specifier}
  227. is exactly the same as in Scheme code; it is evaluated as ordinary
  228. Scheme.
  229. @end deffn