arch.texi 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. @node Module system architecture
  2. @section Module system architecture
  3. @cindex configuration language
  4. @cindex module language
  5. The fundamental mechanism by which Scheme code is evaluated is the
  6. lexical environment. Scheme48's module system revolves around this
  7. fundamental concept. Its purpose is to control the denotation of names
  8. in code@footnote{This is in contrast to, for example, Common Lisp's
  9. package system, which controls the mapping from strings to names.} in a
  10. structured, modular manner. The module system is manipulated by a
  11. static @dfn{configuration language}, described in the next section;
  12. this section describes the concepts in the architecture of the module
  13. system.
  14. @cindex packages
  15. @cindex structures
  16. @cindex interfaces
  17. @cindex modules
  18. The @dfn{package} is the entity internal to the module system that
  19. maps a set of names to denotations. For example, the package that
  20. represents the Scheme language maps @code{lambda} to a descriptor for
  21. the special form that the compiler interprets to construct a procedure,
  22. @code{car} to the procedure that accesses the car of a pair, @etcT{}.
  23. Packages are not explicitly manipulated by the configuration language,
  24. but they lie underneath structures, which are described below. A
  25. package also contains the code of a module and controls the visibility
  26. of names within that code. It also includes some further information,
  27. such as optimizer switches. A @dfn{structure} is a view on a package;
  28. that is, it contains a package and an @dfn{interface} that lists all of
  29. the names it exports to the outside. Multiple structures may be
  30. constructed atop a single package; this mechanism is often used to offer
  31. multiple abstraction levels to the outside. A @dfn{module} is an
  32. abstract entity: it consists of some code, the namespace visible to the
  33. code, and the set of abstractions or views upon that code.
  34. @cindex opening structures
  35. @cindex structures, opening
  36. A package contains a list of the structures whose bindings should be
  37. available in the code of that package. If a structure is referred to
  38. in a such a list of a package, the package is said to @dfn{open} that
  39. structure. It is illegal for a package to open two structures whose
  40. interfaces contain the same name.@footnote{The current implementation,
  41. however, does not detect this. Instead it uses the left-most structure
  42. in the list of a package's @code{open} clause; see the next section for
  43. details on this.} Packages may also modify the names of the bindings
  44. that they import. They may import only selected bindings, exclude
  45. certain bindings from structures, rename imported bindings, create
  46. alias bindings, and add prefixes to names.
  47. @stindex scheme
  48. @stindex prescheme
  49. Most packages will open the standard @code{scheme} structure, although
  50. it is not implicitly opened, and the module system allows not opening
  51. @code{scheme}. It may seem to be not very useful to not open it, but
  52. this is necessary if some bindings from it are intended to be shadowed
  53. by another structure, and it allows for entirely different languages
  54. from Scheme to be used in a package's code. For example, Scheme48's
  55. byte code interpreter virtual machine is implemented in a subset of
  56. Scheme called Pre-Scheme, which is described in a later chapter in this
  57. manual. The modules that compose the VM all open not the @code{scheme}
  58. structure but the @emph{@code{prescheme}} structure. The configuration
  59. language itself is controlled by the module system, too. In another
  60. example, from Scsh, the Scheme shell, there is a structure @code{scsh}
  61. that contains all of the Unix shell programming facilities. However,
  62. the @code{scsh} structure necessarily modifies some of the bindings
  63. related to I/O that the @code{scheme} structure exports. Modules could
  64. not open both @code{scheme} and @code{scsh}, because they both provide
  65. several bindings with the same names, so Scsh defines a more convenient
  66. @code{scheme-with-scsh} structure that opens both @code{scheme}, but
  67. with all of the shadowed bindings excluded, and @code{scsh}; modules
  68. that use Scsh would open neither @code{scsh} nor @code{scheme}: they
  69. instead open just @code{scheme-with-scsh}.
  70. @cindex interface re@"use
  71. @cindex compound interfaces
  72. @cindex interface abstraction
  73. Interfaces are separated from structures in order that they may be
  74. re@"used and combined. For example, several different modules may
  75. implement the same abstractions differently. The structures that they
  76. include would, in such cases, re@"use the same interfaces. Also, it is
  77. sometimes desirable to combine several interfaces into a @dfn{compound
  78. interface}; see the @code{compound-interface} form in the next section.
  79. Furthermore, during interactive development, interface definitions may
  80. be reloaded, and the structures that use them will automatically begin
  81. using the new interfaces; @pxref{Using the module system}.
  82. @cindex parameterized modules
  83. @cindex generic modules
  84. @cindex higher-order modules
  85. @cindex functors
  86. Scheme48's module system also supports @dfn{parameterized modules}.
  87. Parameterized modules, sometimes known as @dfn{generic modules},
  88. @dfn{higher-order modules} or @dfn{functors}, are essentially functions
  89. at the module system level that map structures to structures. They may
  90. be instantiated or applied arbitrarily many times, and they may accept
  91. and return arbitrarily many structures. Parameterized modules may also
  92. accept and return other parameterized modules.