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