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