module.texi 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. @node Using the module system
  2. @section Using the module system
  3. Scheme48 is deeply integrated with an advanced module system. For
  4. complete detail of its module system, @pxref{Module system}. Briefly,
  5. however:
  6. @itemize
  7. @item @dfn{Packages} are top-level environments suitable for
  8. evaluating expressions and definitions, either interactively, from
  9. files loaded on-the-fly, or as the bodies of modules. They can also
  10. access bindings exported by structures by @dfn{opening} the
  11. structures.
  12. @item @dfn{Structures} are libraries, or implementations of
  13. interfaces, exporting sets of bindings that packages can access.
  14. Underlying structures are usually packages, in which the user can, in
  15. some cases, interactively evaluate code during development.
  16. @end itemize
  17. Scheme48's usual development system, the command processor, provides a
  18. number of commands for working with the module system. For complete
  19. details, @pxref{Module commands}. Chief among these commands are
  20. @command{,open} and @command{,in}. @samp{,open @var{struct} @dots{}}
  21. makes all of the bindings from each of @var{struct} @dots{} available
  22. in the interaction environment. Many of the sections in this manual
  23. describe one or more structures with the name they are given. For
  24. example, in order to use, or open, the multi-dimensional array library
  25. in the current interaction environment, one would enter
  26. @lisp
  27. ,open arrays@end lisp
  28. @noindent
  29. to the command processor. @samp{,in @var{struct}} sets the
  30. interaction environment to be the package underlying @var{struct}.
  31. For instance, if, during development, the user decides that the
  32. package of the existing structure @code{foo} should open the structure
  33. @code{bar}, he might type
  34. @lisp
  35. ,in foo
  36. ,open bar@end lisp
  37. The initial interaction environment is known as the @dfn{user
  38. package}; the interaction environment may be reverted to the user
  39. package with the @command{,user} command.
  40. Module descriptions, or code in the @embedref{Module configuration
  41. language, module configuration language} should be loaded into the
  42. special environment for that language with the @code{,config} command
  43. (@pxref{Module commands}). @Eg{}, if @file{packages.scm} contains a
  44. set of module descriptions that the user wishes to load, among which
  45. is the definition of a structure @code{frobozz} which he wishes to
  46. open, he will typically send the following to the command processor
  47. prompt:
  48. @lisp
  49. ,config ,load packages.scm
  50. ,open frobozz@end lisp
  51. @strong{Note:} These are commands for the interactive command
  52. processor, @emph{not} special directives to store in files to work
  53. with the module system. The module language is disjoint from Scheme;
  54. for complete detail on it, @pxref{Module system}.
  55. @subsection Configuration mutation
  56. @i{(This section was derived from work copyrighted @copyright{}
  57. 1993--2005 by Richard Kelsey, Jonathan Rees, and Mike Sperber.)}
  58. @cindex code reloading
  59. @cindex reloading code
  60. @texonlyindent
  61. During program development, it is often desirable to make changes to
  62. packages and interfaces. In static languages, it is usually necessary
  63. to re-compile and re-link a program in order for such changes to be
  64. reflected in a running system. Even in interactive Common Lisp
  65. systems, a change to a package's exports often requires reloading
  66. clients that have already mentioned names whose bindings change. In
  67. those systems, once @code{read} resolves a use of a name to a symbol,
  68. that resolution is fixed, so a change in the way that a name resolves
  69. to a symbol can be reflected only by re-@code{read}ing all such
  70. references.
  71. The Scheme48 development environment supports rapid turnaround in
  72. modular program development by allowing mutations to a program's
  73. configuration and giving a clear semantics to such mutation. The rule
  74. is that variable bindings in a running program are always resolved
  75. according to the current structure and interface bindings, even when
  76. these bindings change as a result of edits to the configuration. For
  77. example, consider the following:
  78. @lisp
  79. (define-interface foo-interface (export a c))
  80. (define-structure foo foo-interface
  81. (open scheme)
  82. (begin (define a 1)
  83. (define (b x) (+ a x))
  84. (define (c y) (* (b a) y))))
  85. (define-structure bar (export d)
  86. (open scheme foo)
  87. (begin (define (d w) (+ (b w) a))))@end lisp
  88. This program has a bug. The variable named @code{b}, which is free in
  89. the definition of @code{d}, has no binding in @code{bar}'s package.
  90. Suppose that @code{b} was intended to be exported by @code{foo}, but
  91. was mistakenly omitted. It is not necessary to re-process @code{bar} or
  92. any of @code{foo}'s other clients at this point. One need only change
  93. @code{foo-interface} and inform the development system of that change
  94. (using, say, an appropriate Emacs command), and @code{foo}'s binding of
  95. @code{b} will be found when the procedure @code{d} is called and its
  96. reference to @code{b} actually evaluated.
  97. Similarly, it is possible to replace a structure; clients of the old
  98. structure will be modified so that they see bindings from the new one.
  99. Shadowing is also supported in the same way. Suppose that a client
  100. package @var{C} opens a structure @code{mumble} that exports a name
  101. @code{x}, and @code{mumble}'s implementation obtains the binding of
  102. @code{x} from some other structure @code{frotz}. @var{C} will see the
  103. binding from @code{frotz}. If one then alters @code{mumble} so that it
  104. shadows @code{bar}'s binding of @code{x} with a definition of its own,
  105. procedures in @var{C} that refer to @code{x} will subsequently
  106. automatically see @code{mumble}'s definition instead of the one from
  107. @code{frotz} that they saw earlier.
  108. This semantics might appear to require a large amount of computation on
  109. every variable reference: the specified behaviour appears to require
  110. scanning the package's list of opened structures and examining their
  111. interfaces --- on every variable reference evaluated, not just at
  112. compile-time. However, the development environment uses caching with
  113. cache invalidation to make variable references fast, and most of the
  114. code is invoked only when the virtual machine traps due to a reference
  115. to an undefined variable.
  116. @subsection Listing interfaces
  117. @cindex interfaces
  118. @stindex list-interfaces
  119. The @code{list-interfaces} structure provides a utility for examining
  120. interfaces. It is usually opened into the config package with
  121. @code{,config ,open list-interfaces} in order to have access to the
  122. structures & interfaces easily.
  123. @deffn procedure list-interface struct-or-interface @returns{} unspecified
  124. Lists all of the bindings exported by @var{struct-or-interface} along
  125. with their @embedref{Static type system, static types}. For example,
  126. @lisp
  127. > ,config ,open list-interfaces
  128. > ,config (list-interface condvars)
  129. condvar-has-value? (proc (:condvar) :value)
  130. condvar-value (proc (:condvar) :value)
  131. condvar? (proc (:value) :boolean)
  132. make-condvar (proc (&rest :value) :condvar)
  133. maybe-commit-and-set-condvar! (proc (:condvar :value) :boolean)
  134. maybe-commit-and-wait-for-condvar (proc (:condvar) :boolean)
  135. set-condvar-has-value?! (proc (:condvar :value) :unspecific)
  136. set-condvar-value! (proc (:condvar :value) :unspecific)@end lisp
  137. @end deffn