R5RS discusses how to structure programs. Everything which is defined in Section 5 of R5RS applies also to STKLOS. To make things shorter, this aspects will not be described here (see R5RS for complete information).
STKLOS modules can be used to organize a program into separate environments (or name spaces). Modules provide a clean way to organize and enforce the barriers between the components of a program.
STKLOS provides a simple module system which is largely inspired from the one of Tung and Dybvig exposed in Tung-Dybvig-96. As their modules system, STKLOS modules are defined to be easily used in an interactive environment.
define-module <name> <expr1> <expr2> ... | STKLOS Syntax |
Define-module evaluates the expressions <expr1> , <expr2> ... which
constitute the body of the module <name> in the environment of that module.
Name must be a valid symbol. If this symbol has not already been used to
define a module, a new module, named name , is created.
Otherwise, the expressions <expr1> , <expr2> ... are evaluated in
the environment of the (old) module <name>
1.
Definitions done in a module are local to the module and do not interact with
the definitions in other modules. Consider the following definitions,
(define-module M1 (define a 1)) (define-module M2 (define a 2) (define b (* 2 x))) Here, two modules are defined and they both bind the symbol The The result of |
current-module | STKLOS Procedure |
Returns the current module.
(define-module M (display (cons (eq? (current-module) (find-module 'M)) (eq? (current-module) (find-module 'STklos))))) -| (#t . #f) |
find-module name | STKLOS Procedure |
find-module name default | STKLOS Procedure |
STKLOS modules are first class objects and find-module returns the
module associated to name if it exists. If there is no module
associated to name , an error is signaled if no default is
provided, otherwise find-module returns default .
|
module? object | STKLOS Procedure |
Returns #t if object is a module and #f otherwise.
(module? (find-module 'STklos)) => #t (module? 'STklos) => #f (module? 1 'no) => no |
export <symbol1> <symbol2> ... | STKLOS Syntax |
Specifies the symbols which are exported (i.e. visible) outside
the current module. By default, symbols defined in a module are not
visible outside this module, excepted if they appear in an export
clause.
If several The result of |
import <module1> <module2> ... | STKLOS Syntax |
Specifies the modules which are imported by the current module.
Importing a module makes the symbols it exports visible to the
importer, if not hidden by local definitions. When a symbol
is exported by several of the imported modules, the location denoted by
this symbol in the importer module correspond to the one of the first module
in the list
(<module1> <module2> ...)which exports it. If several (define-module M1 (export a b) (define a 'M1-a) (define b 'M1-b)) (define-module M2 (export b c) (define b 'M2-b) (define c 'M2-c)) (define-module M3 (import M1 M2) (display (list a b c))) -| (m1-a m1-b m2-c) Note: Importations are not transitive: when the module C imports the module B which is an importer of A, the symbols of A are not visible from C, except by explicitly importing the A module from C. Note: The module |
select-module <name> | STKLOS Syntax |
Changes the value of the current module to the module with the given name .
The expressions evaluated after select-module will take place in
module name environment. Module name must have been created
previously by a define-module . The result of select-module is void.
Select-module is particularly useful when debugging since it
allows to place toplevel evaluation in a particular module. The
following transcript shows an usage of select-module .
2:
stklos> (define foo 1) stklos> (define-module bar (define foo 2)) stklos> foo 1 stklos> (select-module bar) bar> foo 2 bar> (select-module stklos) stklos> |
symbol-value symbol module | STKLOS Procedure |
symbol-value symbol module default | STKLOS Procedure |
Returns the value bound to symbol in module . If symbol is not bound,
an error is signaled if no default is provided, otherwise symbol-value
returns default .
|
symbol-value* symbol module | STKLOS Procedure |
symbol-value* symbol module default | STKLOS Procedure |
Returns the value bound to symbol in module . If symbol is not bound,
an error is signaled if no default is provided, otherwise symbol-value
returns default .
Note that this function searches the value of |
module-name module | STKLOS Procedure |
Returns the the name (a symbol) associated to a module .
|
module-imports module | STKLOS Procedure |
Returns the list of modules that module imports.
|
module-exports module | STKLOS Procedure |
Returns the list of symbols exported by module . Note that this function
returns the list of symbols given in the module export clause and that
some of these symbols can be not yet defined.
|
module-symbols module | STKLOS Procedure |
Returns the list of symbols already defined in module .
|
all-modules | STKLOS Procedure |
Returns the list of all the living modules. |
in-module mod s | STKLOS Syntax |
in-module mod s default | STKLOS Syntax |
This form returns the value of symbol with name s in the module with name
mod . If this symbol is not bound, an error is signaled if no default is
provided, otherwise in-module returns default . Note that the value of s
is searched in mod and all the modules it imports.
This form is in fact a shortcut. In effect, (in-module my-module foo) is equivalent to (symbol-value* 'foo (find-module 'my-module)) |
In fact define-module
on a given name
defines a new module only the first time it is invoked on this name.
By this way, interactively reloading a module does not define
a new entity, and the other modules which use it are not altered.
This transcript uses the default value for the
function repl-display-prompt
(see see repl-display-prompt)
which displays the name of the current module in the evaluator
prompt.