| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
When programs become large, naming conflicts can occur when a function or global variable defined in one file has the same name as a function or global variable in another file. Even just a similarity between function names can cause hard-to-find bugs, since a programmer might type the wrong function name.
The approach used to tackle this problem is called information encapsulation, which consists of packaging functional units into a given name space that is clearly separated from other name spaces.
The language features that allow this are usually called the module system because programs are broken up into modules that are compiled separately (or loaded separately in an interpreter).
Older languages, like C, have limited support for name space
manipulation and protection. In C a variable or function is public by
default, and can be made local to a module with the static
keyword. But you cannot reference public variables and functions from
another module with different names.
More advanced module systems have become a common feature in recently designed languages: ML, Python, Perl, and Modula 3 all allow the renaming of objects from a foreign module, so they will not clutter the global name space.
In addition, Guile offers variables as first-class objects. They can be used for interacting with the module system.
31.1 provide and require The SLIB feature mechanism. 31.2 Environments R5RS top-level environments. 31.3 The Guile module system How Guile does it. 31.4 Dynamic Libraries Loading libraries of compiled code at run time. 31.5 Variables First-class variables.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Aubrey Jaffer, mostly to support his portable Scheme library SLIB, implemented a provide/require mechanism for many Scheme implementations. Library files in SLIB provide a feature, and when user programs require that feature, the library file is loaded in.
For example, the file `random.scm' in the SLIB package contains the line
(provide 'random) |
so to use its procedures, a user would type
(require 'random) |
and they would magically become available, but still have the same names! So this method is nice, but not as good as a full-featured module system.
When SLIB is used with Guile, provide and require can be used to access its facilities.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Scheme, as defined in R5RS, does not have a full module system. However it does define the concept of a top-level environment. Such an environment maps identifiers (symbols) to Scheme objects such as procedures and lists: 14.4 The Concept of Closure. In other words, it implements a set of bindings.
Environments in R5RS can be passed as the second argument to
eval (see section 28.3 Procedures for On the Fly Evaluation). Three procedures are defined to
return environments: scheme-report-environment,
null-environment and interaction-environment (see section 28.3 Procedures for On the Fly Evaluation).
In addition, in Guile any module can be used as an R5RS environment,
i.e., passed as the second argument to eval.
scheme-report-environment returns a specifier for an
environment that is empty except for all bindings defined in the
report that are either required or both optional and supported by the
implementation. null-environment returns a specifier for an
environment that is empty except for the (syntactic) bindings for all
syntactic keywords defined in the report that are either required or
both optional and supported by the implementation.
Currently Guile does not support values of version for other revisions of the report.
The effect of assigning (through the use of eval) a variable
bound in a scheme-report-environment (for example car)
is unspecified. Currently the environments specified by
scheme-report-environment are not immutable in Guile.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The Guile module system extends the concept of environments, discussed in the previous section, with mechanisms to define, use and customise sets of bindings.
In 1996 Tom Lord implemented a full-featured module system for Guile which allows loading Scheme source files into a private name space. This system has been in available since at least Guile version 1.1.
For Guile version 1.5.0 and later, the system has been improved to have better integration from C code, more fine-grained user control over interfaces, and documentation.
Although it is anticipated that the module system implementation will change in the future, the Scheme programming interface described in this manual should be considered stable. The C programming interface is considered relatively stable, although at the time of this writing, there is still some flux.
31.3.1 General Information about Modules Guile module basics. 31.3.2 Using Guile Modules How to use existing modules. 31.3.3 Creating Guile Modules How to package your code into modules. 31.3.4 Module System Quirks Strange things to be aware of. 31.3.5 Included Guile Modules Which modules come with Guile? 31.3.6 Accessing Modules from C How to work with modules with C code.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
A Guile module can be thought of as a collection of named procedures, variables and macros. More precisely, it is a set of bindings of symbols (names) to Scheme objects.
An environment is a mapping from identifiers (or symbols) to locations, i.e., a set of bindings. There are top-level environments and lexical environments. The environment in which a lambda is executed is remembered as part of its definition.
Within a module, all bindings are visible. Certain bindings can be declared public, in which case they are added to the module's so-called export list; this set of public bindings is called the module's public interface (see section 31.3.3 Creating Guile Modules).
A client module uses a providing module's bindings by either accessing the providing module's public interface, or by building a custom interface (and then accessing that). In a custom interface, the client module can select which bindings to access and can also algorithmically rename bindings. In contrast, when using the providing module's public interface, the entire export list is available without renaming (see section 31.3.2 Using Guile Modules).
To use a module, it must be found and loaded. All Guile modules have
a unique module name, which is a list of one or more symbols.
Examples are (ice-9 popen) or (srfi srfi-11). When
Guile searches for the code of a module, it constructs the name of the
file to load by concatenating the name elements with slashes between
the elements and appending a number of file name extensions from the
list %load-extensions (see section 28.4 Loading Scheme Code from File). The resulting file
name is then searched in all directories in the variable
%load-path (see section 33.8 Installation and Configuration Data). For example, the
(ice-9 popen) module would result in the filename
ice-9/popen.scm and searched in the installation directories of
Guile and in all other directories in the load path.
Every module has a so-called syntax transformer associated with it.
This is a procedure which performs all syntax transformation for the
time the module is read in and evaluated. When working with modules,
you can manipulate the current syntax transformer using the
use-syntax syntactic form or the #:use-syntax module
definition option (see section 31.3.3 Creating Guile Modules).
Please note that there are some problems with the current module system you should keep in mind (see section 31.3.4 Module System Quirks). We hope to address these eventually.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
To use a Guile module is to access either its public interface or a
custom interface (see section 31.3.1 General Information about Modules). Both
types of access are handled by the syntactic form use-modules,
which accepts one or more interface specifications and, upon
evaluation, arranges for those interfaces to be available to the
current module. This process may include locating and loading code
for a given module if that code has not yet been loaded, following
%load-path (see section 33.8 Installation and Configuration Data).
An interface specification has one of two forms. The first variation is simply to name the module, in which case its public interface is the one accessed. For example:
(use-modules (ice-9 popen)) |
Here, the interface specification is (ice-9 popen), and the
result is that the current module now has access to open-pipe,
close-pipe, open-input-pipe, and so on (see section 31.3.5 Included Guile Modules).
Note in the previous example that if the current module had already
defined open-pipe, that definition would be overwritten by the
definition in (ice-9 popen). For this reason (and others), there
is a second variation of interface specification that not only names a
module to be accessed, but also selects bindings from it and renames
them to suit the current module's needs. For example:
(use-modules ((ice-9 popen)
:select ((open-pipe . pipe-open) close-pipe)
:renamer (symbol-prefix-proc 'unixy:)))
|
Here, the interface specification is more complex than before, and the result is that a custom interface with only two bindings is created and subsequently accessed by the current module. The mapping of old to new names is as follows:
(ice-9 popen) sees: current module sees: open-pipe unixy:pipe-open close-pipe unixy:close-pipe |
This example also shows how to use the convenience procedure
symbol-prefix-proc.
spec can be a list of symbols, in which case it names a module whose public interface is found and used.
spec can also be of the form:
(MODULE-NAME [:select SELECTION] [:renamer RENAMER]) |
in which case a custom interface is newly created and used.
module-name is a list of symbols, as above; selection is a
list of selection-specs; and renamer is a procedure that takes a
symbol and returns its new name. A selection-spec is either a symbol or
a pair of symbols (ORIG . SEEN), where orig is the name in
the used module and seen is the name in the using module. Note
that seen is also passed through renamer.
The :select and :renamer clauses are optional. If both are
omitted, the returned interface has no bindings. If the :select
clause is omitted, renamer operates on the used module's public
interface.
Signal error if module name is not resolvable.
module-name and use its system
transformer as the system transformer for the currently defined module,
as well as installing it as the current system transformer.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
When you want to create your own modules, you have to take the following steps:
define-module form at the beginning.
define-public or export (both documented below).
(hierarchy file). One
example of this is
(define-module (ice-9 popen)) |
define-module makes this module available to Guile programs under
the given module-name.
The options are keyword/value pairs which specify more about the defined module. The recognized options and their meaning is shown in the following table.
#:use-module interface-specification
(use-modules interface-specification)
(see section 31.3.2 Using Guile Modules).
#:use-syntax module
#:autoload module symbol
#:export list
(export list) in the module body.
#:no-backtrace
#:pure
(begin (define foo ...) (export foo)).
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Although the programming interfaces are relatively stable, the Guile module system itself is still evolving. Here are some situations where usage surpasses design.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Some modules are included in the Guile distribution; here are references to the entries in this manual which describe them in more detail:
and-let* (see section 39.4 SRFI-2 - and-let*).
receive (see section 39.7 SRFI-8 - receive).
define-record-type (see section 39.8 SRFI-9 - define-record-type).
#,() (see section 39.9 SRFI-10 - Hash-Comma Reader Extension).
let-values and let-values*
(see section 39.10 SRFI-11 - let-values).
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The last sections have described how modules are used in Scheme code, which is the recommended way of creating and accessing modules. You can also work with modules from C, but it is more cumbersome.
The following procedures are available.
scm_c_call_with_current_module is the return value of
func.
scm_c_lookup, but the symbol is specified directly.
scm_c_lookup and scm_lookup, but the specified
module is used instead of the current one.
scm_c_define, but the symbol is specified directly.
scm_c_define and scm_define, but the specified
module is used instead of the current one.
The parameter name is a string with the symbols that make up the module name, separated by spaces. For example, `"foo bar"' names the module `(foo bar)'.
When there already exists a module named name, it is used unchanged, otherwise, an empty module is created.
scm_c_define_module.
scm_c_resolve_module, but the name is given as a real list
of symbols.
(use-modules name). The name is
interpreted as for scm_c_define_module.
NULL.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Most modern Unices have something called shared libraries. This ordinarily means that they have the capability to share the executable image of a library between several running programs to save memory and disk space. But generally, shared libraries give a lot of additional flexibility compared to the traditional static libraries. In fact, calling them `dynamic' libraries is as correct as calling them `shared'.
Shared libraries really give you a lot of flexibility in addition to the memory and disk space savings. When you link a program against a shared library, that library is not closely incorporated into the final executable. Instead, the executable of your program only contains enough information to find the needed shared libraries when the program is actually run. Only then, when the program is starting, is the final step of the linking process performed. This means that you need not recompile all programs when you install a new, only slightly modified version of a shared library. The programs will pick up the changes automatically the next time they are run.
Now, when all the necessary machinery is there to perform part of the linking at run-time, why not take the next step and allow the programmer to explicitly take advantage of it from within his program? Of course, many operating systems that support shared libraries do just that, and chances are that Guile will allow you to access this feature from within your Scheme programs. As you might have guessed already, this feature is called dynamic linking.(13)
As with many aspects of Guile, there is a low-level way to access the dynamic linking apparatus, and a more high-level interface that integrates dynamically linked libraries into the module system.
31.4.1 Low level dynamic linking 31.4.2 Writing Dynamically Loadable Extensions
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
When using the low level procedures to do your dynamic linking, you have complete control over which library is loaded when and what gets done with it.
Normally, library is just the name of some shared library file that will be searched for in the places where shared libraries usually reside, such as in `/usr/lib' and `/usr/local/lib'.
#t if obj is a dynamic library handle, or #f
otherwise.
dynamic-link. After dynamic-unlink has been
called on dobj, its content is no longer accessible.
dynamic-call to
actually call the function.
Regardless whether your C compiler prepends an underscore `_' to the global names in a program, you should not include this underscore in function. Guile knows whether the underscore is needed or not and will add it when necessary.
dynamic-func, call that function and ignore dobj.
When func is a string , look it up in dynobj; this
is equivalent to
(dynamic-call (dynamic-func func dobj) #f) |
Interrupts are deferred while the C function is executing (with
SCM_DEFER_INTS/SCM_ALLOW_INTS).
dynamic-call, but pass it some arguments and
return its return value. The C function is expected to take
two arguments and return an int, just like main:
int c_func (int argc, char **argv); |
The parameter args must be a list of strings and is
converted into an array of char *. The array is passed
in argv and its size in argc. The return value is
converted to a Scheme number and returned from the call to
dynamic-args-call.
When dynamic linking is disabled or not supported on your system, the above functions throw errors, but they are still available.
Here is a small example that works on GNU/Linux:
(define libc-obj (dynamic-link "libc.so")) libc-obj => #<dynamic-object "libc.so"> (dynamic-args-call 'rand libc-obj '()) => 269167349 (dynamic-unlink libc-obj) libc-obj => #<dynamic-object "libc.so" (unlinked)> |
As you can see, after calling dynamic-unlink on a dynamically
linked library, it is marked as `(unlinked)' and you are no longer
able to use it with dynamic-call, etc. Whether the library is
really removed from you program is system-dependent and will generally
not happen when some other parts of your program still use it. In the
example above, libc is almost certainly not removed from your
program because it is badly needed by almost everything.
The functions to call a function from a dynamically linked library,
dynamic-call and dynamic-args-call, are not very powerful.
They are mostly intended to be used for calling specially written
initialization functions that will then add new primitives to Guile.
For example, we do not expect that you will dynamically link
`libX11' with dynamic-link and then construct a beautiful
graphical user interface just by using dynamic-call and
dynamic-args-call. Instead, the usual way would be to write a
special Guile<->X11 glue library that has intimate knowledge about both
Guile and X11 and does whatever is necessary to make them inter-operate
smoothly. This glue library could then be dynamically linked into a
vanilla Guile interpreter and activated by calling its initialization
function. That function would add all the new types and primitives to
the Guile interpreter that it has to offer.
From this setup the next logical step is to integrate these glue libraries into the module system of Guile so that you can load new primitives into a running system just as you can load new Scheme code.
There is, however, another possibility to get a more thorough access to
the functions contained in a dynamically linked library. Anthony Green
has written `libffi', a library that implements a foreign
function interface for a number of different platforms. With it, you
can extend the Spartan functionality of dynamic-call and
dynamic-args-call considerably. There is glue code available in
the Guile contrib archive to make `libffi' accessible from Guile.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
XXX - document load-extension, scm_register_extension
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Each module has its own hash table, sometimes known as an obarray, that maps the names defined in that module to their corresponding variable objects.
A variable is a box-like object that can hold any Scheme value. It is
said to be undefined if its box holds a special Scheme value that
denotes undefined-ness (which is different from all other Scheme values,
including for example #f); otherwise the variable is
defined.
On its own, a variable object is anonymous. A variable is said to be bound when it is associated with a name in some way, usually a symbol in a module obarray. When this happens, the relationship is mutual: the variable is bound to the name (in that module), and the name (in that module) is bound to the variable.
(That's the theory, anyway. In practice, defined-ness and bound-ness sometimes get confused, because Lisp and Scheme implementations have often conflated -- or deliberately drawn no distinction between -- a name that is unbound and a name that is bound to a variable whose value is undefined. We will try to be clear about the difference and explain any confusion where it is unavoidable.)
Variables do not have a read syntax. Most commonly they are created and
bound implicitly by define expressions: a top-level define
expression of the form
(define name value) |
creates a variable with initial value value and binds it to the
name name in the current module. But they can also be created
dynamically by calling one of the constructor procedures
make-variable and make-undefined-variable.
First-class variables are especially useful for interacting with the current module system (see section 31.3 The Guile module system).
#t iff var is bound to a value.
Throws an error if var is not a variable object.
make-variable
and make-undefined-variable.
#t iff obj is a variable object, else
return #f.
| [ << ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |