Module Definition
-----------------
Two special forms are used to define modules, one for anonymous
modules, one for named modules. When storing modules in files, each
file often contains a single instance of one of these forms.
- Macro: structure interface config body...
- Macro: define-structure name interface config body...
These special forms each create a new module with interface
INTERFACE (using the syntax described in the previous section),
and configuration CONFIG.
After configuring the module as specified, the sequence of forms
BODY... is evaluated; it should include the definitions required
by the interface that the module has promised to implement.
The CONFIG form is either a list of configuration clauses, or a
single configuration clause. Each such clause must be of the
following syntax:
CLAUSE -> (open NAME ...)
| (access NAME ...)
Each NAME specifies the name of a module, in the case of `open'
clauses, the named module(s) will be loaded such that their
exported bindings may be referenced from within the current module
with no qualification (i.e. as if they had been defined within the
module itself).
Alternatively, if an `access' clause was used, the named module(s)
will be loaded, but their exported bindings will only be accessible
from within the current module using the `structure-ref' form.
E.g. if a module `foo' has been accessed and it exports a binding
named `bar', then the following form could be used to access its
value:
(structure-ref foo bar)
Since this form is used so often, the reader allows the
abbreviation `foo#bar' to be used instead, it is expanded to the
form above when read. Note that no whitespace is allowed between
the three tokens.
Note that to access the standard features of the `rep' language
described in this manual, modules need to import the `rep' module.
Alternatively, they may import the `scheme' module to load a minimal
R4RS Scheme environment.
Here is an example module definition, defining a module named `test'
that exports two functions `foo' and `bar'.
(define-structure test (export foo bar)
(open rep)
(define (foo x) (* x 42))
(define (bar x y) (+ (foo x) (1+ y))))
It is also possible to export multiple views of a single underlying
set of bindings, by using the `define-structures' form to create a
number of modules.
- Macro: define-structures ((name interface) ...) config body...
Create a module for each `(NAME INTERFACE)' pair. The module is
called NAME and exports the interface defined by INTERFACE.
The CONFIG and BODY... forms are as in `define-structure'.
Here is a trivial example:
(define-structures ((foo (export foo both))
(bar (export bar both)))
(open rep)
(define both 1)
(define foo 2)
(define bar 3))
the underlying environment has three bindings. Each created module
exports two of these.