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