eval-when expand for use-modules and export
[bpt/guile.git] / doc / ref / api-modules.texi
CommitLineData
07d83abe
MV
1@c -*-texinfo-*-
2@c This is part of the GNU Guile Reference Manual.
d12f974b 3@c Copyright (C) 1996, 1997, 2000, 2001, 2002, 2003, 2004, 2007, 2008, 2009, 2010
07d83abe
MV
4@c Free Software Foundation, Inc.
5@c See the file guile.texi for copying conditions.
6
07d83abe
MV
7@node Modules
8@section Modules
9@cindex modules
10
11When programs become large, naming conflicts can occur when a function
12or global variable defined in one file has the same name as a function
13or global variable in another file. Even just a @emph{similarity}
14between function names can cause hard-to-find bugs, since a programmer
15might type the wrong function name.
16
17The approach used to tackle this problem is called @emph{information
18encapsulation}, which consists of packaging functional units into a
19given name space that is clearly separated from other name spaces.
20@cindex encapsulation
21@cindex information encapsulation
22@cindex name space
23
24The language features that allow this are usually called @emph{the
25module system} because programs are broken up into modules that are
26compiled separately (or loaded separately in an interpreter).
27
28Older languages, like C, have limited support for name space
29manipulation and protection. In C a variable or function is public by
30default, and can be made local to a module with the @code{static}
31keyword. But you cannot reference public variables and functions from
32another module with different names.
33
34More advanced module systems have become a common feature in recently
35designed languages: ML, Python, Perl, and Modula 3 all allow the
36@emph{renaming} of objects from a foreign module, so they will not
37clutter the global name space.
38@cindex name space - private
39
40In addition, Guile offers variables as first-class objects. They can
41be used for interacting with the module system.
42
07d83abe
MV
43@menu
44* General Information about Modules:: Guile module basics.
45* Using Guile Modules:: How to use existing modules.
46* Creating Guile Modules:: How to package your code into modules.
cdf1ad3b 47* Module System Reflection:: Accessing module objects at run-time.
07d83abe 48* Included Guile Modules:: Which modules come with Guile?
dca14012 49* R6RS Version References:: Using version numbers with modules.
07d83abe 50* Accessing Modules from C:: How to work with modules with C code.
726b8ba3
AW
51* Variables:: First-class variables.
52* provide and require:: The SLIB feature mechanism.
53* Environments:: R5RS top-level environments.
07d83abe
MV
54@end menu
55
56@node General Information about Modules
726b8ba3 57@subsection General Information about Modules
07d83abe
MV
58
59A Guile module can be thought of as a collection of named procedures,
60variables and macros. More precisely, it is a set of @dfn{bindings}
61of symbols (names) to Scheme objects.
62
63An environment is a mapping from identifiers (or symbols) to locations,
64i.e., a set of bindings.
65There are top-level environments and lexical environments.
66The environment in which a lambda is executed is remembered as part of its
67definition.
68
69Within a module, all bindings are visible. Certain bindings
70can be declared @dfn{public}, in which case they are added to the
71module's so-called @dfn{export list}; this set of public bindings is
72called the module's @dfn{public interface} (@pxref{Creating Guile
73Modules}).
74
75A client module @dfn{uses} a providing module's bindings by either
76accessing the providing module's public interface, or by building a
77custom interface (and then accessing that). In a custom interface, the
78client module can @dfn{select} which bindings to access and can also
79algorithmically @dfn{rename} bindings. In contrast, when using the
80providing module's public interface, the entire export list is available
81without renaming (@pxref{Using Guile Modules}).
82
83To use a module, it must be found and loaded. All Guile modules have a
84unique @dfn{module name}, which is a list of one or more symbols.
85Examples are @code{(ice-9 popen)} or @code{(srfi srfi-11)}. When Guile
86searches for the code of a module, it constructs the name of the file to
87load by concatenating the name elements with slashes between the
88elements and appending a number of file name extensions from the list
89@code{%load-extensions} (@pxref{Loading}). The resulting file name is
90then searched in all directories in the variable @code{%load-path}
91(@pxref{Build Config}). For example, the @code{(ice-9 popen)} module
92would result in the filename @code{ice-9/popen.scm} and searched in the
93installation directories of Guile and in all other directories in the
94load path.
95
dca14012
JG
96A slightly different search mechanism is used when a client module
97specifies a version reference as part of a request to load a module
98(@pxref{R6RS Version References}). Instead of searching the directories
99in the load path for a single filename, Guile uses the elements of the
100version reference to locate matching, numbered subdirectories of a
101constructed base path. For example, a request for the
102@code{(rnrs base)} module with version reference @code{(6)} would cause
103Guile to discover the @code{rnrs/6} subdirectory (if it exists in any of
104the directories in the load path) and search its contents for the
105filename @code{base.scm}.
106
107When multiple modules are found that match a version reference, Guile
108sorts these modules by version number, followed by the length of their
109version specifications, in order to choose a ``best'' match.
110
07d83abe
MV
111@c FIXME::martin: Not sure about this, maybe someone knows better?
112Every module has a so-called syntax transformer associated with it.
113This is a procedure which performs all syntax transformation for the
114time the module is read in and evaluated. When working with modules,
115you can manipulate the current syntax transformer using the
116@code{use-syntax} syntactic form or the @code{#:use-syntax} module
117definition option (@pxref{Creating Guile Modules}).
118
07d83abe
MV
119
120@node Using Guile Modules
726b8ba3 121@subsection Using Guile Modules
07d83abe
MV
122
123To use a Guile module is to access either its public interface or a
124custom interface (@pxref{General Information about Modules}). Both
125types of access are handled by the syntactic form @code{use-modules},
126which accepts one or more interface specifications and, upon evaluation,
127arranges for those interfaces to be available to the current module.
128This process may include locating and loading code for a given module if
5c132e68 129that code has not yet been loaded, following @code{%load-path} (@pxref{Build
07d83abe
MV
130Config}).
131
132An @dfn{interface specification} has one of two forms. The first
133variation is simply to name the module, in which case its public
134interface is the one accessed. For example:
135
aba0dff5 136@lisp
07d83abe 137(use-modules (ice-9 popen))
aba0dff5 138@end lisp
07d83abe
MV
139
140Here, the interface specification is @code{(ice-9 popen)}, and the
141result is that the current module now has access to @code{open-pipe},
142@code{close-pipe}, @code{open-input-pipe}, and so on (@pxref{Included
143Guile Modules}).
144
145Note in the previous example that if the current module had already
146defined @code{open-pipe}, that definition would be overwritten by the
147definition in @code{(ice-9 popen)}. For this reason (and others), there
148is a second variation of interface specification that not only names a
149module to be accessed, but also selects bindings from it and renames
150them to suit the current module's needs. For example:
151
46bb559d 152@cindex binding renamer
aba0dff5 153@lisp
07d83abe 154(use-modules ((ice-9 popen)
8d9cb14e
NJ
155 #:select ((open-pipe . pipe-open) close-pipe)
156 #:renamer (symbol-prefix-proc 'unixy:)))
aba0dff5 157@end lisp
07d83abe
MV
158
159Here, the interface specification is more complex than before, and the
160result is that a custom interface with only two bindings is created and
161subsequently accessed by the current module. The mapping of old to new
162names is as follows:
163
164@c Use `smallexample' since `table' is ugly. --ttn
165@smallexample
166(ice-9 popen) sees: current module sees:
167open-pipe unixy:pipe-open
168close-pipe unixy:close-pipe
169@end smallexample
170
171This example also shows how to use the convenience procedure
172@code{symbol-prefix-proc}.
173
174You can also directly refer to bindings in a module by using the
175@code{@@} syntax. For example, instead of using the
176@code{use-modules} statement from above and writing
177@code{unixy:pipe-open} to refer to the @code{pipe-open} from the
178@code{(ice-9 popen)}, you could also write @code{(@@ (ice-9 popen)
179open-pipe)}. Thus an alternative to the complete @code{use-modules}
180statement would be
181
aba0dff5 182@lisp
07d83abe
MV
183(define unixy:pipe-open (@@ (ice-9 popen) open-pipe))
184(define unixy:close-pipe (@@ (ice-9 popen) close-pipe))
aba0dff5 185@end lisp
07d83abe
MV
186
187There is also @code{@@@@}, which can be used like @code{@@}, but does
188not check whether the variable that is being accessed is actually
189exported. Thus, @code{@@@@} can be thought of as the impolite version
190of @code{@@} and should only be used as a last resort or for
191debugging, for example.
192
193Note that just as with a @code{use-modules} statement, any module that
194has not yet been loaded yet will be loaded when referenced by a
195@code{@@} or @code{@@@@} form.
196
197You can also use the @code{@@} and @code{@@@@} syntaxes as the target
198of a @code{set!} when the binding refers to a variable.
199
200@c begin (scm-doc-string "boot-9.scm" "symbol-prefix-proc")
201@deffn {Scheme Procedure} symbol-prefix-proc prefix-sym
202Return a procedure that prefixes its arg (a symbol) with
203@var{prefix-sym}.
204@c Insert gratuitous C++ slam here. --ttn
205@end deffn
206
207@c begin (scm-doc-string "boot-9.scm" "use-modules")
208@deffn syntax use-modules spec @dots{}
209Resolve each interface specification @var{spec} into an interface and
210arrange for these to be accessible by the current module. The return
211value is unspecified.
212
213@var{spec} can be a list of symbols, in which case it names a module
214whose public interface is found and used.
215
216@var{spec} can also be of the form:
217
46bb559d 218@cindex binding renamer
aba0dff5 219@lisp
07d83abe 220 (MODULE-NAME [:select SELECTION] [:renamer RENAMER])
aba0dff5 221@end lisp
07d83abe
MV
222
223in which case a custom interface is newly created and used.
224@var{module-name} is a list of symbols, as above; @var{selection} is a
225list of selection-specs; and @var{renamer} is a procedure that takes a
226symbol and returns its new name. A selection-spec is either a symbol or
227a pair of symbols @code{(ORIG . SEEN)}, where @var{orig} is the name in
228the used module and @var{seen} is the name in the using module. Note
229that @var{seen} is also passed through @var{renamer}.
230
231The @code{:select} and @code{:renamer} clauses are optional. If both are
232omitted, the returned interface has no bindings. If the @code{:select}
233clause is omitted, @var{renamer} operates on the used module's public
234interface.
235
dca14012
JG
236In addition to the above, @var{spec} can also include a @code{:version}
237clause, of the form:
238
239@lisp
240 :version VERSION-SPEC
241@end lisp
242
243where @var{version-spec} is an R6RS-compatible version reference. The
244presence of this clause changes Guile's search behavior as described in
245the section on module name resolution
246(@pxref{General Information about Modules}). An error will be signaled
247in the case in which a module with the same name has already been
248loaded, if that module specifies a version and that version is not
249compatible with @var{version-spec}.
250
07d83abe
MV
251Signal error if module name is not resolvable.
252@end deffn
253
254
255@c FIXME::martin: Is this correct, and is there more to say?
c8779dde 256@c FIXME::martin: Define term and concept `syntax transformer' somewhere.
07d83abe
MV
257
258@deffn syntax use-syntax module-name
c8779dde
LC
259Load the module @code{module-name} and use its syntax
260transformer as the syntax transformer for the currently defined module,
261as well as installing it as the current syntax transformer.
07d83abe
MV
262@end deffn
263
264@deffn syntax @@ module-name binding-name
265Refer to the binding named @var{binding-name} in module
266@var{module-name}. The binding must have been exported by the module.
267@end deffn
268
269@deffn syntax @@@@ module-name binding-name
270Refer to the binding named @var{binding-name} in module
271@var{module-name}. The binding must not have been exported by the
272module. This syntax is only intended for debugging purposes or as a
273last resort.
274@end deffn
275
276@node Creating Guile Modules
726b8ba3 277@subsection Creating Guile Modules
07d83abe
MV
278
279When you want to create your own modules, you have to take the following
280steps:
281
282@itemize @bullet
283@item
284Create a Scheme source file and add all variables and procedures you wish
285to export, or which are required by the exported procedures.
286
287@item
288Add a @code{define-module} form at the beginning.
289
290@item
291Export all bindings which should be in the public interface, either
292by using @code{define-public} or @code{export} (both documented below).
293@end itemize
294
295@c begin (scm-doc-string "boot-9.scm" "define-module")
296@deffn syntax define-module module-name [options @dots{}]
297@var{module-name} is of the form @code{(hierarchy file)}. One
298example of this is
299
aba0dff5 300@lisp
07d83abe 301(define-module (ice-9 popen))
aba0dff5 302@end lisp
07d83abe
MV
303
304@code{define-module} makes this module available to Guile programs under
305the given @var{module-name}.
306
307The @var{options} are keyword/value pairs which specify more about the
308defined module. The recognized options and their meaning is shown in
309the following table.
310
311@c fixme: Should we use "#:" or ":"?
312
313@table @code
314@item #:use-module @var{interface-specification}
315Equivalent to a @code{(use-modules @var{interface-specification})}
316(@pxref{Using Guile Modules}).
317
318@item #:use-syntax @var{module}
319Use @var{module} when loading the currently defined module, and install
320it as the syntax transformer.
321
950f97ac 322@item #:autoload @var{module} @var{symbol-list}
65f1345f 323@cindex autoload
950f97ac
KR
324Load @var{module} when any of @var{symbol-list} are accessed. For
325example,
326
327@example
328(define-module (my mod)
329 #:autoload (srfi srfi-1) (partition delete-duplicates))
330...
331(if something
332 (set! foo (delete-duplicates ...)))
333@end example
334
a236df75 335When a module is autoloaded, all its bindings become available.
950f97ac
KR
336@var{symbol-list} is just those that will first trigger the load.
337
338An autoload is a good way to put off loading a big module until it's
339really needed, for instance for faster startup or if it will only be
340needed in certain circumstances.
341
342@code{@@} can do a similar thing (@pxref{Using Guile Modules}), but in
343that case an @code{@@} form must be written every time a binding from
344the module is used.
07d83abe
MV
345
346@item #:export @var{list}
65f1345f 347@cindex export
78c22f5e
JG
348Export all identifiers in @var{list} which must be a list of symbols
349or pairs of symbols. This is equivalent to @code{(export @var{list})}
350in the module body.
07d83abe 351
46bb559d
KR
352@item #:re-export @var{list}
353@cindex re-export
354Re-export all identifiers in @var{list} which must be a list of
78c22f5e
JG
355symbols or pairs of symbols. The symbols in @var{list} must be
356imported by the current module from other modules. This is equivalent
357to @code{re-export} below.
46bb559d
KR
358
359@item #:export-syntax @var{list}
360@cindex export-syntax
78c22f5e
JG
361Export all identifiers in @var{list} which must be a list of symbols
362or pairs of symbols. The identifiers in @var{list} must refer to
363macros (@pxref{Macros}) defined in the current module. This is
364equivalent to @code{(export-syntax @var{list})} in the module body.
46bb559d
KR
365
366@item #:re-export-syntax @var{list}
367@cindex re-export-syntax
368Re-export all identifiers in @var{list} which must be a list of
78c22f5e
JG
369symbols or pairs of symbols. The symbols in @var{list} must refer to
370macros imported by the current module from other modules. This is
371equivalent to @code{(re-export-syntax @var{list})} in the module body.
46bb559d
KR
372
373@item #:replace @var{list}
374@cindex replace
375@cindex replacing binding
376@cindex overriding binding
377@cindex duplicate binding
78c22f5e
JG
378Export all identifiers in @var{list} (a list of symbols or pairs of
379symbols) and mark them as @dfn{replacing bindings}. In the module
380user's name space, this will have the effect of replacing any binding
381with the same name that is not also ``replacing''. Normally a
382replacement results in an ``override'' warning message,
383@code{#:replace} avoids that.
46bb559d 384
e724644d
MV
385This is useful for modules that export bindings that have the same
386name as core bindings. @code{#:replace}, in a sense, lets Guile know
387that the module @emph{purposefully} replaces a core binding. It is
388important to note, however, that this binding replacement is confined
389to the name space of the module user. In other words, the value of the
390core binding in question remains unchanged for other modules.
391
392For instance, SRFI-39 exports a binding named
393@code{current-input-port} (@pxref{SRFI-39}) that is a function which
394is upwardly compatible with the core @code{current-input-port}
395function. Therefore, SRFI-39 exports its version with
396@code{#:replace}.
397
398SRFI-19, on the other hand, exports its own version of
399@code{current-time} (@pxref{SRFI-19 Time}) which is not compatible
400with the core @code{current-time} function (@pxref{Time}). Therefore,
401SRFI-19 does not use @code{#:replace}.
402
403The @code{#:replace} option can also be used by a module which is
404intentionally producing a new special kind of environment and should
405override any core or other bindings already in scope. For example
406perhaps a logic processing environment where @code{<=} is an inference
407instead of a comparison.
408
409The @code{#:duplicates} (see below) provides fine-grain control about
410duplicate binding handling on the module-user side.
46bb559d 411
dca14012
JG
412@item #:version @var{list}
413@cindex module version
414Specify a version for the module in the form of @var{list}, a list of
415zero or more exact, nonnegative integers. The corresponding
416@code{#:version} option in the @code{use-modules} form allows callers
417to restrict the value of this option in various ways.
418
46bb559d
KR
419@item #:duplicates @var{list}
420@cindex duplicate binding handlers
421@cindex duplicate binding
422@cindex overriding binding
423Tell Guile to handle duplicate bindings for the bindings imported by
424the current module according to the policy defined by @var{list}, a
425list of symbols. @var{list} must contain symbols representing a
426duplicate binding handling policy chosen among the following:
427
428@table @code
429@item check
430Raises an error when a binding is imported from more than one place.
431@item warn
432Issue a warning when a binding is imported from more than one place
433and leave the responsibility of actually handling the duplication to
434the next duplicate binding handler.
435@item replace
436When a new binding is imported that has the same name as a previously
437imported binding, then do the following:
438
439@enumerate
440@item
441@cindex replacing binding
442If the old binding was said to be @dfn{replacing} (via the
443@code{#:replace} option above) and the new binding is not replacing,
444the keep the old binding.
445@item
446If the old binding was not said to be replacing and the new binding is
447replacing, then replace the old binding with the new one.
448@item
449If neither the old nor the new binding is replacing, then keep the old
450one.
451@end enumerate
452
453@item warn-override-core
454Issue a warning when a core binding is being overwritten and actually
455override the core binding with the new one.
456@item first
457In case of duplicate bindings, the firstly imported binding is always
458the one which is kept.
459@item last
460In case of duplicate bindings, the lastly imported binding is always
461the one which is kept.
462@item noop
463In case of duplicate bindings, leave the responsibility to the next
464duplicate handler.
465@end table
466
467If @var{list} contains more than one symbol, then the duplicate
468binding handlers which appear first will be used first when resolving
469a duplicate binding situation. As mentioned above, some resolution
470policies may explicitly leave the responsibility of handling the
471duplication to the next handler in @var{list}.
472
473@findex default-duplicate-binding-handler
474The default duplicate binding resolution policy is given by the
475@code{default-duplicate-binding-handler} procedure, and is
476
aba0dff5 477@lisp
46bb559d 478(replace warn-override-core warn last)
aba0dff5 479@end lisp
46bb559d 480
07d83abe 481@item #:no-backtrace
65f1345f 482@cindex no backtrace
07d83abe
MV
483Tell Guile not to record information for procedure backtraces when
484executing the procedures in this module.
485
486@item #:pure
65f1345f 487@cindex pure module
07d83abe
MV
488Create a @dfn{pure} module, that is a module which does not contain any
489of the standard procedure bindings except for the syntax forms. This is
490useful if you want to create @dfn{safe} modules, that is modules which
491do not know anything about dangerous procedures.
492@end table
493
494@end deffn
495@c end
496
497@deffn syntax export variable @dots{}
78c22f5e
JG
498Add all @var{variable}s (which must be symbols or pairs of symbols) to
499the list of exported bindings of the current module. If @var{variable}
500is a pair, its @code{car} gives the name of the variable as seen by the
501current module and its @code{cdr} specifies a name for the binding in
502the current module's public interface.
07d83abe
MV
503@end deffn
504
505@c begin (scm-doc-string "boot-9.scm" "define-public")
506@deffn syntax define-public @dots{}
507Equivalent to @code{(begin (define foo ...) (export foo))}.
508@end deffn
509@c end
510
46bb559d 511@deffn syntax re-export variable @dots{}
78c22f5e
JG
512Add all @var{variable}s (which must be symbols or pairs of symbols) to
513the list of re-exported bindings of the current module. Pairs of
514symbols are handled as in @code{export}. Re-exported bindings must be
515imported by the current module from some other module.
46bb559d
KR
516@end deffn
517
cdf1ad3b 518@node Module System Reflection
726b8ba3 519@subsection Module System Reflection
cdf1ad3b
MV
520
521The previous sections have described a declarative view of the module
522system. You can also work with it programmatically by accessing and
523modifying various parts of the Scheme objects that Guile uses to
524implement the module system.
525
526At any time, there is a @dfn{current module}. This module is the one
527where a top-level @code{define} and similar syntax will add new
528bindings. You can find other module objects with @code{resolve-module},
529for example.
530
531These module objects can be used as the second argument to @code{eval}.
532
533@deffn {Scheme Procedure} current-module
534Return the current module object.
535@end deffn
536
537@deffn {Scheme Procedure} set-current-module module
538Set the current module to @var{module} and return
539the previous current module.
540@end deffn
541
b1f57ea4
LC
542@deffn {Scheme Procedure} save-module-excursion thunk
543Call @var{thunk} within a @code{dynamic-wind} such that the module that
544is current at invocation time is restored when @var{thunk}'s dynamic
545extent is left (@pxref{Dynamic Wind}).
546
547More precisely, if @var{thunk} escapes non-locally, the current module
548(at the time of escape) is saved, and the original current module (at
549the time @var{thunk}'s dynamic extent was last entered) is restored. If
550@var{thunk}'s dynamic extent is re-entered, then the current module is
551saved, and the previously saved inner module is set current again.
552@end deffn
553
cdf1ad3b
MV
554@deffn {Scheme Procedure} resolve-module name
555Find the module named @var{name} and return it. When it has not already
556been defined, try to auto-load it. When it can't be found that way
557either, create an empty module. The name is a list of symbols.
558@end deffn
559
560@deffn {Scheme Procedure} resolve-interface name
561Find the module named @var{name} as with @code{resolve-module} and
562return its interface. The interface of a module is also a module
563object, but it contains only the exported bindings.
564@end deffn
565
566@deffn {Scheme Procedure} module-use! module interface
567Add @var{interface} to the front of the use-list of @var{module}. Both
568arguments should be module objects, and @var{interface} should very
569likely be a module returned by @code{resolve-interface}.
570@end deffn
07d83abe 571
07d83abe
MV
572
573@node Included Guile Modules
726b8ba3 574@subsection Included Guile Modules
07d83abe
MV
575
576@c FIXME::martin: Review me!
577
578Some modules are included in the Guile distribution; here are references
579to the entries in this manual which describe them in more detail:
580
581@table @strong
582@item boot-9
583boot-9 is Guile's initialization module, and it is always loaded when
584Guile starts up.
585
586@item (ice-9 debug)
587Mikael Djurfeldt's source-level debugging support for Guile
24dbb5ed 588(@pxref{Tracing}).
07d83abe 589
54a3c992
KR
590@item (ice-9 expect)
591Actions based on matching input from a port (@pxref{Expect}).
592
593@item (ice-9 format)
594Formatted output in the style of Common Lisp (@pxref{Formatted
595Output}).
596
597@item (ice-9 ftw)
598File tree walker (@pxref{File Tree Walk}).
599
600@item (ice-9 getopt-long)
601Command line option processing (@pxref{getopt-long}).
602
603@item (ice-9 history)
604Refer to previous interactive expressions (@pxref{Value History}).
605
606@item (ice-9 popen)
607Pipes to and from child processes (@pxref{Pipes}).
608
609@item (ice-9 pretty-print)
610Nicely formatted output of Scheme expressions and objects
611(@pxref{Pretty Printing}).
612
613@item (ice-9 q)
614First-in first-out queues (@pxref{Queues}).
615
07d83abe
MV
616@item (ice-9 rdelim)
617Line- and character-delimited input (@pxref{Line/Delimited}).
618
54a3c992
KR
619@item (ice-9 readline)
620@code{readline} interactive command line editing (@pxref{Readline
621Support}).
622
623@item (ice-9 receive)
624Multiple-value handling with @code{receive} (@pxref{Multiple Values}).
625
626@item (ice-9 regex)
627Regular expression matching (@pxref{Regular Expressions}).
628
07d83abe
MV
629@item (ice-9 rw)
630Block string input/output (@pxref{Block Reading and Writing}).
631
54a3c992
KR
632@item (ice-9 streams)
633Sequence of values calculated on-demand (@pxref{Streams}).
634
635@item (ice-9 syncase)
636R5RS @code{syntax-rules} macro system (@pxref{Syntax Rules}).
637
aca55ba9
KR
638@item (ice-9 threads)
639Guile's support for multi threaded execution (@pxref{Scheduling}).
640
07d83abe
MV
641@item (ice-9 documentation)
642Online documentation (REFFIXME).
643
644@item (srfi srfi-1)
645A library providing a lot of useful list and pair processing
646procedures (@pxref{SRFI-1}).
647
648@item (srfi srfi-2)
649Support for @code{and-let*} (@pxref{SRFI-2}).
650
651@item (srfi srfi-4)
652Support for homogeneous numeric vectors (@pxref{SRFI-4}).
653
654@item (srfi srfi-6)
655Support for some additional string port procedures (@pxref{SRFI-6}).
656
657@item (srfi srfi-8)
658Multiple-value handling with @code{receive} (@pxref{SRFI-8}).
659
660@item (srfi srfi-9)
661Record definition with @code{define-record-type} (@pxref{SRFI-9}).
662
663@item (srfi srfi-10)
664Read hash extension @code{#,()} (@pxref{SRFI-10}).
665
666@item (srfi srfi-11)
cdc4f3db 667Multiple-value handling with @code{let-values} and @code{let*-values}
07d83abe
MV
668(@pxref{SRFI-11}).
669
670@item (srfi srfi-13)
671String library (@pxref{SRFI-13}).
672
673@item (srfi srfi-14)
674Character-set library (@pxref{SRFI-14}).
675
e376f9e5
KR
676@item (srfi srfi-16)
677@code{case-lambda} procedures of variable arity (@pxref{SRFI-16}).
678
07d83abe
MV
679@item (srfi srfi-17)
680Getter-with-setter support (@pxref{SRFI-17}).
681
e376f9e5
KR
682@item (srfi srfi-19)
683Time/Date library (@pxref{SRFI-19}).
684
07d83abe
MV
685@item (srfi srfi-26)
686Convenient syntax for partial application (@pxref{SRFI-26})
687
e376f9e5
KR
688@item (srfi srfi-31)
689@code{rec} convenient recursive expressions (@pxref{SRFI-31})
690
07d83abe
MV
691@item (ice-9 slib)
692This module contains hooks for using Aubrey Jaffer's portable Scheme
693library SLIB from Guile (@pxref{SLIB}).
07d83abe
MV
694@end table
695
696
dca14012 697@node R6RS Version References
726b8ba3 698@subsection R6RS Version References
dca14012
JG
699
700Guile's module system includes support for locating modules based on
701a declared version specifier of the same form as the one described in
702R6RS (@pxref{Library form, R6RS Library Form,, r6rs, The Revised^6
703Report on the Algorithmic Language Scheme}). By using the
704@code{#:version} keyword in a @code{define-module} form, a module may
705specify a version as a list of zero or more exact, nonnegative integers.
706
707This version can then be used to locate the module during the module
708search process. Client modules and callers of the @code{use-modules}
709function may specify constraints on the versions of target modules by
710providing a @dfn{version reference}, which has one of the following
711forms:
712
713@lisp
714 (@var{sub-version-reference} ...)
715 (and @var{version-reference} ...)
716 (or @var{version-reference} ...)
717 (not @var{version-reference})
718@end lisp
719
720in which @var{sub-version-reference} is in turn one of:
721
722@lisp
723 (@var{sub-version})
724 (>= @var{sub-version})
725 (<= @var{sub-version})
726 (and @var{sub-version-reference} ...)
727 (or @var{sub-version-reference} ...)
728 (not @var{sub-version-reference})
729@end lisp
730
731in which @var{sub-version} is an exact, nonnegative integer as above. A
732version reference matches a declared module version if each element of
733the version reference matches a corresponding element of the module
734version, according to the following rules:
735
736@itemize @bullet
737@item
738The @code{and} sub-form matches a version or version element if every
739element in the tail of the sub-form matches the specified version or
740version element.
741
742@item
743The @code{or} sub-form matches a version or version element if any
744element in the tail of the sub-form matches the specified version or
745version element.
746
747@item
748The @code{not} sub-form matches a version or version element if the tail
749of the sub-form does not match the version or version element.
750
751@item
752The @code{>=} sub-form matches a version element if the element is
753greater than or equal to the @var{sub-version} in the tail of the
754sub-form.
755
756@item
757The @code{<=} sub-form matches a version element if the version is less
758than or equal to the @var{sub-version} in the tail of the sub-form.
759
760@item
761A @var{sub-version} matches a version element if one is @var{eqv?} to
762the other.
763@end itemize
764
765For example, a module declared as:
766
767@lisp
768 (define-module (mylib mymodule) #:version (1 2 0))
769@end lisp
770
771would be successfully loaded by any of the following @code{use-modules}
772expressions:
773
774@lisp
775 (use-modules ((mylib mymodule) #:version (1 2 (>= 0))))
776 (use-modules ((mylib mymodule) #:version (or (1 2 0) (1 2 1))))
777 (use-modules ((mylib mymodule) #:version ((and (>= 1) (not 2)) 2 0)))
778@end lisp
779
780
07d83abe 781@node Accessing Modules from C
726b8ba3 782@subsection Accessing Modules from C
07d83abe
MV
783
784The last sections have described how modules are used in Scheme code,
785which is the recommended way of creating and accessing modules. You
786can also work with modules from C, but it is more cumbersome.
787
788The following procedures are available.
789
790@deftypefn {C Procedure} SCM scm_current_module ()
791Return the module that is the @emph{current module}.
792@end deftypefn
793
794@deftypefn {C Procedure} SCM scm_set_current_module (SCM @var{module})
795Set the current module to @var{module} and return the previous current
796module.
797@end deftypefn
798
799@deftypefn {C Procedure} SCM scm_c_call_with_current_module (SCM @var{module}, SCM (*@var{func})(void *), void *@var{data})
800Call @var{func} and make @var{module} the current module during the
801call. The argument @var{data} is passed to @var{func}. The return
802value of @code{scm_c_call_with_current_module} is the return value of
803@var{func}.
804@end deftypefn
805
806@deftypefn {C Procedure} SCM scm_c_lookup (const char *@var{name})
807Return the variable bound to the symbol indicated by @var{name} in the
808current module. If there is no such binding or the symbol is not
809bound to a variable, signal an error.
810@end deftypefn
811
812@deftypefn {C Procedure} SCM scm_lookup (SCM @var{name})
813Like @code{scm_c_lookup}, but the symbol is specified directly.
814@end deftypefn
815
816@deftypefn {C Procedure} SCM scm_c_module_lookup (SCM @var{module}, const char *@var{name})
817@deftypefnx {C Procedure} SCM scm_module_lookup (SCM @var{module}, SCM @var{name})
818Like @code{scm_c_lookup} and @code{scm_lookup}, but the specified
819module is used instead of the current one.
820@end deftypefn
821
822@deftypefn {C Procedure} SCM scm_c_define (const char *@var{name}, SCM @var{val})
823Bind the symbol indicated by @var{name} to a variable in the current
824module and set that variable to @var{val}. When @var{name} is already
825bound to a variable, use that. Else create a new variable.
826@end deftypefn
827
828@deftypefn {C Procedure} SCM scm_define (SCM @var{name}, SCM @var{val})
829Like @code{scm_c_define}, but the symbol is specified directly.
830@end deftypefn
831
832@deftypefn {C Procedure} SCM scm_c_module_define (SCM @var{module}, const char *@var{name}, SCM @var{val})
833@deftypefnx {C Procedure} SCM scm_module_define (SCM @var{module}, SCM @var{name}, SCM @var{val})
834Like @code{scm_c_define} and @code{scm_define}, but the specified
835module is used instead of the current one.
836@end deftypefn
837
838@deftypefn {C Procedure} SCM scm_module_reverse_lookup (SCM @var{module}, SCM @var{variable})
839Find the symbol that is bound to @var{variable} in @var{module}. When no such binding is found, return @var{#f}.
840@end deftypefn
841
842@deftypefn {C Procedure} SCM scm_c_define_module (const char *@var{name}, void (*@var{init})(void *), void *@var{data})
843Define a new module named @var{name} and make it current while
844@var{init} is called, passing it @var{data}. Return the module.
845
846The parameter @var{name} is a string with the symbols that make up
847the module name, separated by spaces. For example, @samp{"foo bar"} names
848the module @samp{(foo bar)}.
849
850When there already exists a module named @var{name}, it is used
851unchanged, otherwise, an empty module is created.
852@end deftypefn
853
854@deftypefn {C Procedure} SCM scm_c_resolve_module (const char *@var{name})
855Find the module name @var{name} and return it. When it has not
856already been defined, try to auto-load it. When it can't be found
857that way either, create an empty module. The name is interpreted as
858for @code{scm_c_define_module}.
859@end deftypefn
860
861@deftypefn {C Procedure} SCM scm_resolve_module (SCM @var{name})
862Like @code{scm_c_resolve_module}, but the name is given as a real list
863of symbols.
864@end deftypefn
865
866@deftypefn {C Procedure} SCM scm_c_use_module (const char *@var{name})
867Add the module named @var{name} to the uses list of the current
868module, as with @code{(use-modules @var{name})}. The name is
869interpreted as for @code{scm_c_define_module}.
870@end deftypefn
871
872@deftypefn {C Procedure} SCM scm_c_export (const char *@var{name}, ...)
873Add the bindings designated by @var{name}, ... to the public interface
874of the current module. The list of names is terminated by
875@code{NULL}.
876@end deftypefn
877
ef5f9163 878
07d83abe
MV
879@node Variables
880@subsection Variables
881@tpindex Variables
882
883Each module has its own hash table, sometimes known as an @dfn{obarray},
884that maps the names defined in that module to their corresponding
885variable objects.
886
887A variable is a box-like object that can hold any Scheme value. It is
888said to be @dfn{undefined} if its box holds a special Scheme value that
889denotes undefined-ness (which is different from all other Scheme values,
890including for example @code{#f}); otherwise the variable is
891@dfn{defined}.
892
893On its own, a variable object is anonymous. A variable is said to be
894@dfn{bound} when it is associated with a name in some way, usually a
895symbol in a module obarray. When this happens, the relationship is
896mutual: the variable is bound to the name (in that module), and the name
897(in that module) is bound to the variable.
898
899(That's the theory, anyway. In practice, defined-ness and bound-ness
900sometimes get confused, because Lisp and Scheme implementations have
901often conflated --- or deliberately drawn no distinction between --- a
902name that is unbound and a name that is bound to a variable whose value
903is undefined. We will try to be clear about the difference and explain
904any confusion where it is unavoidable.)
905
906Variables do not have a read syntax. Most commonly they are created and
907bound implicitly by @code{define} expressions: a top-level @code{define}
908expression of the form
909
910@lisp
911(define @var{name} @var{value})
912@end lisp
913
914@noindent
915creates a variable with initial value @var{value} and binds it to the
916name @var{name} in the current module. But they can also be created
917dynamically by calling one of the constructor procedures
918@code{make-variable} and @code{make-undefined-variable}.
919
07d83abe
MV
920@deffn {Scheme Procedure} make-undefined-variable
921@deffnx {C Function} scm_make_undefined_variable ()
922Return a variable that is initially unbound.
923@end deffn
924
925@deffn {Scheme Procedure} make-variable init
926@deffnx {C Function} scm_make_variable (init)
927Return a variable initialized to value @var{init}.
928@end deffn
929
930@deffn {Scheme Procedure} variable-bound? var
931@deffnx {C Function} scm_variable_bound_p (var)
932Return @code{#t} iff @var{var} is bound to a value.
933Throws an error if @var{var} is not a variable object.
934@end deffn
935
936@deffn {Scheme Procedure} variable-ref var
937@deffnx {C Function} scm_variable_ref (var)
938Dereference @var{var} and return its value.
939@var{var} must be a variable object; see @code{make-variable}
940and @code{make-undefined-variable}.
941@end deffn
942
943@deffn {Scheme Procedure} variable-set! var val
944@deffnx {C Function} scm_variable_set_x (var, val)
945Set the value of the variable @var{var} to @var{val}.
946@var{var} must be a variable object, @var{val} can be any
947value. Return an unspecified value.
948@end deffn
949
950@deffn {Scheme Procedure} variable? obj
951@deffnx {C Function} scm_variable_p (obj)
952Return @code{#t} iff @var{obj} is a variable object, else
953return @code{#f}.
954@end deffn
955
956
726b8ba3
AW
957@node provide and require
958@subsection provide and require
959
960Aubrey Jaffer, mostly to support his portable Scheme library SLIB,
961implemented a provide/require mechanism for many Scheme implementations.
962Library files in SLIB @emph{provide} a feature, and when user programs
963@emph{require} that feature, the library file is loaded in.
964
965For example, the file @file{random.scm} in the SLIB package contains the
966line
967
968@lisp
969(provide 'random)
970@end lisp
971
972so to use its procedures, a user would type
973
974@lisp
975(require 'random)
976@end lisp
977
978and they would magically become available, @emph{but still have the same
979names!} So this method is nice, but not as good as a full-featured
980module system.
981
982When SLIB is used with Guile, provide and require can be used to access
983its facilities.
984
985@node Environments
986@subsection Environments
987@cindex environment
988
989Scheme, as defined in R5RS, does @emph{not} have a full module system.
990However it does define the concept of a top-level @dfn{environment}.
991Such an environment maps identifiers (symbols) to Scheme objects such
992as procedures and lists: @ref{About Closure}. In other words, it
993implements a set of @dfn{bindings}.
994
995Environments in R5RS can be passed as the second argument to
996@code{eval} (@pxref{Fly Evaluation}). Three procedures are defined to
997return environments: @code{scheme-report-environment},
998@code{null-environment} and @code{interaction-environment} (@pxref{Fly
999Evaluation}).
1000
1001In addition, in Guile any module can be used as an R5RS environment,
1002i.e., passed as the second argument to @code{eval}.
1003
1004Note: the following two procedures are available only when the
1005@code{(ice-9 r5rs)} module is loaded:
1006
1007@lisp
1008(use-modules (ice-9 r5rs))
1009@end lisp
1010
1011@deffn {Scheme Procedure} scheme-report-environment version
1012@deffnx {Scheme Procedure} null-environment version
1013@var{version} must be the exact integer `5', corresponding to revision
10145 of the Scheme report (the Revised^5 Report on Scheme).
1015@code{scheme-report-environment} returns a specifier for an
1016environment that is empty except for all bindings defined in the
1017report that are either required or both optional and supported by the
1018implementation. @code{null-environment} returns a specifier for an
1019environment that is empty except for the (syntactic) bindings for all
1020syntactic keywords defined in the report that are either required or
1021both optional and supported by the implementation.
1022
1023Currently Guile does not support values of @var{version} for other
1024revisions of the report.
1025
1026The effect of assigning (through the use of @code{eval}) a variable
1027bound in a @code{scheme-report-environment} (for example @code{car})
1028is unspecified. Currently the environments specified by
1029@code{scheme-report-environment} are not immutable in Guile.
1030@end deffn
1031
1032
1033
07d83abe
MV
1034@c Local Variables:
1035@c TeX-master: "guile.texi"
1036@c End: