update traps documentation (unfinished)
[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 In general, a module that exports a binding for which the @code{(guile)}
387 module already has a definition should use @code{#:replace} instead of
388 @code{#:export}. @code{#:replace}, in a sense, lets Guile know that the
389 module @emph{purposefully} replaces a core binding. It is important to
390 note, however, that this binding replacement is confined to the name
391 space of the module user. In other words, the value of the core binding
392 in question remains unchanged for other modules.
393
394 Note that although it is often a good idea for the replaced binding to
395 remain compatible with a binding in @code{(guile)}, to avoid surprising
396 the user, sometimes the bindings will be incompatible. For example,
397 SRFI-19 exports its own version of @code{current-time} (@pxref{SRFI-19
398 Time}) which is not compatible with the core @code{current-time}
399 function (@pxref{Time}). Guile assumes that a user importing a module
400 knows what she is doing, and uses @code{#:replace} for this binding
401 rather than @code{#:export}.
402
403 The @code{#:duplicates} (see below) provides fine-grain control about
404 duplicate binding handling on the module-user side.
405
406 @item #:version @var{list}
407 @cindex module version
408 Specify a version for the module in the form of @var{list}, a list of
409 zero or more exact, nonnegative integers. The corresponding
410 @code{#:version} option in the @code{use-modules} form allows callers
411 to restrict the value of this option in various ways.
412
413 @item #:duplicates @var{list}
414 @cindex duplicate binding handlers
415 @cindex duplicate binding
416 @cindex overriding binding
417 Tell Guile to handle duplicate bindings for the bindings imported by
418 the current module according to the policy defined by @var{list}, a
419 list of symbols. @var{list} must contain symbols representing a
420 duplicate binding handling policy chosen among the following:
421
422 @table @code
423 @item check
424 Raises an error when a binding is imported from more than one place.
425 @item warn
426 Issue a warning when a binding is imported from more than one place
427 and leave the responsibility of actually handling the duplication to
428 the next duplicate binding handler.
429 @item replace
430 When a new binding is imported that has the same name as a previously
431 imported binding, then do the following:
432
433 @enumerate
434 @item
435 @cindex replacing binding
436 If the old binding was said to be @dfn{replacing} (via the
437 @code{#:replace} option above) and the new binding is not replacing,
438 the keep the old binding.
439 @item
440 If the old binding was not said to be replacing and the new binding is
441 replacing, then replace the old binding with the new one.
442 @item
443 If neither the old nor the new binding is replacing, then keep the old
444 one.
445 @end enumerate
446
447 @item warn-override-core
448 Issue a warning when a core binding is being overwritten and actually
449 override the core binding with the new one.
450 @item first
451 In case of duplicate bindings, the firstly imported binding is always
452 the one which is kept.
453 @item last
454 In case of duplicate bindings, the lastly imported binding is always
455 the one which is kept.
456 @item noop
457 In case of duplicate bindings, leave the responsibility to the next
458 duplicate handler.
459 @end table
460
461 If @var{list} contains more than one symbol, then the duplicate
462 binding handlers which appear first will be used first when resolving
463 a duplicate binding situation. As mentioned above, some resolution
464 policies may explicitly leave the responsibility of handling the
465 duplication to the next handler in @var{list}.
466
467 @findex default-duplicate-binding-handler
468 The default duplicate binding resolution policy is given by the
469 @code{default-duplicate-binding-handler} procedure, and is
470
471 @lisp
472 (replace warn-override-core warn last)
473 @end lisp
474
475 @item #:no-backtrace
476 @cindex no backtrace
477 Tell Guile not to record information for procedure backtraces when
478 executing the procedures in this module.
479
480 @item #:pure
481 @cindex pure module
482 Create a @dfn{pure} module, that is a module which does not contain any
483 of the standard procedure bindings except for the syntax forms. This is
484 useful if you want to create @dfn{safe} modules, that is modules which
485 do not know anything about dangerous procedures.
486 @end table
487
488 @end deffn
489 @c end
490
491 @deffn syntax export variable @dots{}
492 Add all @var{variable}s (which must be symbols or pairs of symbols) to
493 the list of exported bindings of the current module. If @var{variable}
494 is a pair, its @code{car} gives the name of the variable as seen by the
495 current module and its @code{cdr} specifies a name for the binding in
496 the current module's public interface.
497 @end deffn
498
499 @c begin (scm-doc-string "boot-9.scm" "define-public")
500 @deffn syntax define-public @dots{}
501 Equivalent to @code{(begin (define foo ...) (export foo))}.
502 @end deffn
503 @c end
504
505 @deffn syntax re-export variable @dots{}
506 Add all @var{variable}s (which must be symbols or pairs of symbols) to
507 the list of re-exported bindings of the current module. Pairs of
508 symbols are handled as in @code{export}. Re-exported bindings must be
509 imported by the current module from some other module.
510 @end deffn
511
512 @node Module System Reflection
513 @subsection Module System Reflection
514
515 The previous sections have described a declarative view of the module
516 system. You can also work with it programmatically by accessing and
517 modifying various parts of the Scheme objects that Guile uses to
518 implement the module system.
519
520 At any time, there is a @dfn{current module}. This module is the one
521 where a top-level @code{define} and similar syntax will add new
522 bindings. You can find other module objects with @code{resolve-module},
523 for example.
524
525 These module objects can be used as the second argument to @code{eval}.
526
527 @deffn {Scheme Procedure} current-module
528 Return the current module object.
529 @end deffn
530
531 @deffn {Scheme Procedure} set-current-module module
532 Set the current module to @var{module} and return
533 the previous current module.
534 @end deffn
535
536 @deffn {Scheme Procedure} save-module-excursion thunk
537 Call @var{thunk} within a @code{dynamic-wind} such that the module that
538 is current at invocation time is restored when @var{thunk}'s dynamic
539 extent is left (@pxref{Dynamic Wind}).
540
541 More precisely, if @var{thunk} escapes non-locally, the current module
542 (at the time of escape) is saved, and the original current module (at
543 the time @var{thunk}'s dynamic extent was last entered) is restored. If
544 @var{thunk}'s dynamic extent is re-entered, then the current module is
545 saved, and the previously saved inner module is set current again.
546 @end deffn
547
548 @deffn {Scheme Procedure} resolve-module name
549 Find the module named @var{name} and return it. When it has not already
550 been defined, try to auto-load it. When it can't be found that way
551 either, create an empty module. The name is a list of symbols.
552 @end deffn
553
554 @deffn {Scheme Procedure} resolve-interface name
555 Find the module named @var{name} as with @code{resolve-module} and
556 return its interface. The interface of a module is also a module
557 object, but it contains only the exported bindings.
558 @end deffn
559
560 @deffn {Scheme Procedure} module-use! module interface
561 Add @var{interface} to the front of the use-list of @var{module}. Both
562 arguments should be module objects, and @var{interface} should very
563 likely be a module returned by @code{resolve-interface}.
564 @end deffn
565
566
567 @node Included Guile Modules
568 @subsection Included Guile Modules
569
570 @c FIXME::martin: Review me!
571
572 Some modules are included in the Guile distribution; here are references
573 to the entries in this manual which describe them in more detail:
574
575 @table @strong
576 @item boot-9
577 boot-9 is Guile's initialization module, and it is always loaded when
578 Guile starts up.
579
580 @item (ice-9 expect)
581 Actions based on matching input from a port (@pxref{Expect}).
582
583 @item (ice-9 format)
584 Formatted output in the style of Common Lisp (@pxref{Formatted
585 Output}).
586
587 @item (ice-9 ftw)
588 File tree walker (@pxref{File Tree Walk}).
589
590 @item (ice-9 getopt-long)
591 Command line option processing (@pxref{getopt-long}).
592
593 @item (ice-9 history)
594 Refer to previous interactive expressions (@pxref{Value History}).
595
596 @item (ice-9 popen)
597 Pipes to and from child processes (@pxref{Pipes}).
598
599 @item (ice-9 pretty-print)
600 Nicely formatted output of Scheme expressions and objects
601 (@pxref{Pretty Printing}).
602
603 @item (ice-9 q)
604 First-in first-out queues (@pxref{Queues}).
605
606 @item (ice-9 rdelim)
607 Line- and character-delimited input (@pxref{Line/Delimited}).
608
609 @item (ice-9 readline)
610 @code{readline} interactive command line editing (@pxref{Readline
611 Support}).
612
613 @item (ice-9 receive)
614 Multiple-value handling with @code{receive} (@pxref{Multiple Values}).
615
616 @item (ice-9 regex)
617 Regular expression matching (@pxref{Regular Expressions}).
618
619 @item (ice-9 rw)
620 Block string input/output (@pxref{Block Reading and Writing}).
621
622 @item (ice-9 streams)
623 Sequence of values calculated on-demand (@pxref{Streams}).
624
625 @item (ice-9 syncase)
626 R5RS @code{syntax-rules} macro system (@pxref{Syntax Rules}).
627
628 @item (ice-9 threads)
629 Guile's support for multi threaded execution (@pxref{Scheduling}).
630
631 @item (ice-9 documentation)
632 Online documentation (REFFIXME).
633
634 @item (srfi srfi-1)
635 A library providing a lot of useful list and pair processing
636 procedures (@pxref{SRFI-1}).
637
638 @item (srfi srfi-2)
639 Support for @code{and-let*} (@pxref{SRFI-2}).
640
641 @item (srfi srfi-4)
642 Support for homogeneous numeric vectors (@pxref{SRFI-4}).
643
644 @item (srfi srfi-6)
645 Support for some additional string port procedures (@pxref{SRFI-6}).
646
647 @item (srfi srfi-8)
648 Multiple-value handling with @code{receive} (@pxref{SRFI-8}).
649
650 @item (srfi srfi-9)
651 Record definition with @code{define-record-type} (@pxref{SRFI-9}).
652
653 @item (srfi srfi-10)
654 Read hash extension @code{#,()} (@pxref{SRFI-10}).
655
656 @item (srfi srfi-11)
657 Multiple-value handling with @code{let-values} and @code{let*-values}
658 (@pxref{SRFI-11}).
659
660 @item (srfi srfi-13)
661 String library (@pxref{SRFI-13}).
662
663 @item (srfi srfi-14)
664 Character-set library (@pxref{SRFI-14}).
665
666 @item (srfi srfi-16)
667 @code{case-lambda} procedures of variable arity (@pxref{SRFI-16}).
668
669 @item (srfi srfi-17)
670 Getter-with-setter support (@pxref{SRFI-17}).
671
672 @item (srfi srfi-19)
673 Time/Date library (@pxref{SRFI-19}).
674
675 @item (srfi srfi-26)
676 Convenient syntax for partial application (@pxref{SRFI-26})
677
678 @item (srfi srfi-31)
679 @code{rec} convenient recursive expressions (@pxref{SRFI-31})
680
681 @item (ice-9 slib)
682 This module contains hooks for using Aubrey Jaffer's portable Scheme
683 library SLIB from Guile (@pxref{SLIB}).
684 @end table
685
686
687 @node R6RS Version References
688 @subsection R6RS Version References
689
690 Guile's module system includes support for locating modules based on
691 a declared version specifier of the same form as the one described in
692 R6RS (@pxref{Library form, R6RS Library Form,, r6rs, The Revised^6
693 Report on the Algorithmic Language Scheme}). By using the
694 @code{#:version} keyword in a @code{define-module} form, a module may
695 specify a version as a list of zero or more exact, nonnegative integers.
696
697 This version can then be used to locate the module during the module
698 search process. Client modules and callers of the @code{use-modules}
699 function may specify constraints on the versions of target modules by
700 providing a @dfn{version reference}, which has one of the following
701 forms:
702
703 @lisp
704 (@var{sub-version-reference} ...)
705 (and @var{version-reference} ...)
706 (or @var{version-reference} ...)
707 (not @var{version-reference})
708 @end lisp
709
710 in which @var{sub-version-reference} is in turn one of:
711
712 @lisp
713 (@var{sub-version})
714 (>= @var{sub-version})
715 (<= @var{sub-version})
716 (and @var{sub-version-reference} ...)
717 (or @var{sub-version-reference} ...)
718 (not @var{sub-version-reference})
719 @end lisp
720
721 in which @var{sub-version} is an exact, nonnegative integer as above. A
722 version reference matches a declared module version if each element of
723 the version reference matches a corresponding element of the module
724 version, according to the following rules:
725
726 @itemize @bullet
727 @item
728 The @code{and} sub-form matches a version or version element if every
729 element in the tail of the sub-form matches the specified version or
730 version element.
731
732 @item
733 The @code{or} sub-form matches a version or version element if any
734 element in the tail of the sub-form matches the specified version or
735 version element.
736
737 @item
738 The @code{not} sub-form matches a version or version element if the tail
739 of the sub-form does not match the version or version element.
740
741 @item
742 The @code{>=} sub-form matches a version element if the element is
743 greater than or equal to the @var{sub-version} in the tail of the
744 sub-form.
745
746 @item
747 The @code{<=} sub-form matches a version element if the version is less
748 than or equal to the @var{sub-version} in the tail of the sub-form.
749
750 @item
751 A @var{sub-version} matches a version element if one is @var{eqv?} to
752 the other.
753 @end itemize
754
755 For example, a module declared as:
756
757 @lisp
758 (define-module (mylib mymodule) #:version (1 2 0))
759 @end lisp
760
761 would be successfully loaded by any of the following @code{use-modules}
762 expressions:
763
764 @lisp
765 (use-modules ((mylib mymodule) #:version (1 2 (>= 0))))
766 (use-modules ((mylib mymodule) #:version (or (1 2 0) (1 2 1))))
767 (use-modules ((mylib mymodule) #:version ((and (>= 1) (not 2)) 2 0)))
768 @end lisp
769
770
771 @node R6RS Libraries
772 @subsection R6RS Libraries
773
774 In addition to the API described in the previous sections, you also
775 have the option to create modules using the portable @code{library} form
776 described in R6RS (@pxref{Library form, R6RS Library Form,, r6rs, The
777 Revised^6 Report on the Algorithmic Language Scheme}), and to import
778 libraries created in this format by other programmers. Guile's R6RS
779 library implementation takes advantage of the flexibility built into the
780 module system by expanding the R6RS library form into a corresponding
781 Guile @code{define-module} form that specifies equivalent import and
782 export requirements and includes the same body expressions. The library
783 expression:
784
785 @lisp
786 (library (mylib (1 2))
787 (import (otherlib (3)))
788 (export mybinding))
789 @end lisp
790
791 is equivalent to the module definition:
792
793 @lisp
794 (define-module (mylib)
795 #:version (1 2)
796 #:use-module ((otherlib) #:version (3))
797 #:export (mybinding))
798 @end lisp
799
800 Central to the mechanics of R6RS libraries is the concept of import
801 and export @dfn{levels}, which control the visibility of bindings at
802 various phases of a library's lifecycle --- macros necessary to
803 expand forms in the library's body need to be available at expand
804 time; variables used in the body of a procedure exported by the
805 library must be available at runtime. R6RS specifies the optional
806 @code{for} sub-form of an @emph{import set} specification (see below)
807 as a mechanism by which a library author can indicate that a
808 particular library import should take place at a particular phase
809 with respect to the lifecycle of the importing library.
810
811 Guile's libraries implementation uses a technique called
812 @dfn{implicit phasing} (first described by Abdulaziz Ghuloum and R.
813 Kent Dybvig), which allows the expander and compiler to automatically
814 determine the necessary visibility of a binding imported from another
815 library. As such, the @code{for} sub-form described below is ignored by
816 Guile (but may be required by Schemes in which phasing is explicit).
817
818 @deffn {Scheme Syntax} library name (export export-spec ...) (import import-spec ...) body ...
819 Defines a new library with the specified name, exports, and imports,
820 and evaluates the specified body expressions in this library's
821 environment.
822
823 The library @var{name} is a non-empty list of identifiers, optionally
824 ending with a version specification of the form described above
825 (@pxref{Creating Guile Modules}).
826
827 Each @var{export-spec} is the name of a variable defined or imported
828 by the library, or must take the form
829 @code{(rename (internal-name external-name) ...)}, where the
830 identifier @var{internal-name} names a variable defined or imported
831 by the library and @var{external-name} is the name by which the
832 variable is seen by importing libraries.
833
834 Each @var{import-spec} must be either an @dfn{import set} (see below)
835 or must be of the form @code{(for import-set import-level ...)},
836 where each @var{import-level} is one of:
837
838 @lisp
839 run
840 expand
841 (meta @var{level})
842 @end lisp
843
844 where @var{level} is an integer. Note that since Guile does not
845 require explicit phase specification, any @var{import-set}s found
846 inside of @code{for} sub-forms will be ``unwrapped'' during
847 expansion and processed as if they had been specified directly.
848
849 Import sets in turn take one of the following forms:
850
851 @lisp
852 @var{library-reference}
853 (library @var{library-reference})
854 (only @var{import-set} @var{identifier} ...)
855 (except @var{import-set} @var{identifier} ...)
856 (prefix @var{import-set} @var{identifier})
857 (rename @var{import-set} (@var{internal-identifier} @var{external-identifier}) ...)
858 @end lisp
859
860 where @var{library-reference} is a non-empty list of identifiers
861 ending with an optional version reference (@pxref{R6RS Version
862 References}), and the other sub-forms have the following semantics,
863 defined recursively on nested @var{import-set}s:
864
865 @itemize @bullet
866
867 @item
868 The @code{library} sub-form is used to specify libraries for import
869 whose names begin with the identifier ``library.''
870
871 @item
872 The @code{only} sub-form imports only the specified @var{identifier}s
873 from the given @var{import-set}.
874
875 @item
876 The @code{except} sub-form imports all of the bindings exported by
877 @var{import-set} except for those that appear in the specified list
878 of @var{identifier}s.
879
880 @item
881 The @code{prefix} sub-form imports all of the bindings exported
882 by @var{import-set}, first prefixing them with the specified
883 @var{identifier}.
884
885 @item
886 The @code{rename} sub-form imports all of the identifiers exported
887 by @var{import-set}. The binding for each @var{internal-identifier}
888 among these identifiers is made visible to the importing library as
889 the corresponding @var{external-identifier}; all other bindings are
890 imported using the names provided by @var{import-set}.
891
892 @end itemize
893
894 Note that because Guile translates R6RS libraries into module
895 definitions, an import specification may be used to declare a
896 dependency on a native Guile module --- although doing so may make
897 your libraries less portable to other Schemes.
898
899 @end deffn
900
901 @deffn {Scheme Syntax} import import-spec ...
902 Import into the current environment the libraries specified by the
903 given import specifications, where each @var{import-spec} takes the
904 same form as in the @code{library} form described above.
905 @end deffn
906
907
908 @node Accessing Modules from C
909 @subsection Accessing Modules from C
910
911 The last sections have described how modules are used in Scheme code,
912 which is the recommended way of creating and accessing modules. You
913 can also work with modules from C, but it is more cumbersome.
914
915 The following procedures are available.
916
917 @deftypefn {C Procedure} SCM scm_current_module ()
918 Return the module that is the @emph{current module}.
919 @end deftypefn
920
921 @deftypefn {C Procedure} SCM scm_set_current_module (SCM @var{module})
922 Set the current module to @var{module} and return the previous current
923 module.
924 @end deftypefn
925
926 @deftypefn {C Procedure} SCM scm_c_call_with_current_module (SCM @var{module}, SCM (*@var{func})(void *), void *@var{data})
927 Call @var{func} and make @var{module} the current module during the
928 call. The argument @var{data} is passed to @var{func}. The return
929 value of @code{scm_c_call_with_current_module} is the return value of
930 @var{func}.
931 @end deftypefn
932
933 @deftypefn {C Procedure} SCM scm_c_lookup (const char *@var{name})
934 Return the variable bound to the symbol indicated by @var{name} in the
935 current module. If there is no such binding or the symbol is not
936 bound to a variable, signal an error.
937 @end deftypefn
938
939 @deftypefn {C Procedure} SCM scm_lookup (SCM @var{name})
940 Like @code{scm_c_lookup}, but the symbol is specified directly.
941 @end deftypefn
942
943 @deftypefn {C Procedure} SCM scm_c_module_lookup (SCM @var{module}, const char *@var{name})
944 @deftypefnx {C Procedure} SCM scm_module_lookup (SCM @var{module}, SCM @var{name})
945 Like @code{scm_c_lookup} and @code{scm_lookup}, but the specified
946 module is used instead of the current one.
947 @end deftypefn
948
949 @deftypefn {C Procedure} SCM scm_c_define (const char *@var{name}, SCM @var{val})
950 Bind the symbol indicated by @var{name} to a variable in the current
951 module and set that variable to @var{val}. When @var{name} is already
952 bound to a variable, use that. Else create a new variable.
953 @end deftypefn
954
955 @deftypefn {C Procedure} SCM scm_define (SCM @var{name}, SCM @var{val})
956 Like @code{scm_c_define}, but the symbol is specified directly.
957 @end deftypefn
958
959 @deftypefn {C Procedure} SCM scm_c_module_define (SCM @var{module}, const char *@var{name}, SCM @var{val})
960 @deftypefnx {C Procedure} SCM scm_module_define (SCM @var{module}, SCM @var{name}, SCM @var{val})
961 Like @code{scm_c_define} and @code{scm_define}, but the specified
962 module is used instead of the current one.
963 @end deftypefn
964
965 @deftypefn {C Procedure} SCM scm_module_reverse_lookup (SCM @var{module}, SCM @var{variable})
966 Find the symbol that is bound to @var{variable} in @var{module}. When no such binding is found, return @var{#f}.
967 @end deftypefn
968
969 @deftypefn {C Procedure} SCM scm_c_define_module (const char *@var{name}, void (*@var{init})(void *), void *@var{data})
970 Define a new module named @var{name} and make it current while
971 @var{init} is called, passing it @var{data}. Return the module.
972
973 The parameter @var{name} is a string with the symbols that make up
974 the module name, separated by spaces. For example, @samp{"foo bar"} names
975 the module @samp{(foo bar)}.
976
977 When there already exists a module named @var{name}, it is used
978 unchanged, otherwise, an empty module is created.
979 @end deftypefn
980
981 @deftypefn {C Procedure} SCM scm_c_resolve_module (const char *@var{name})
982 Find the module name @var{name} and return it. When it has not
983 already been defined, try to auto-load it. When it can't be found
984 that way either, create an empty module. The name is interpreted as
985 for @code{scm_c_define_module}.
986 @end deftypefn
987
988 @deftypefn {C Procedure} SCM scm_resolve_module (SCM @var{name})
989 Like @code{scm_c_resolve_module}, but the name is given as a real list
990 of symbols.
991 @end deftypefn
992
993 @deftypefn {C Procedure} SCM scm_c_use_module (const char *@var{name})
994 Add the module named @var{name} to the uses list of the current
995 module, as with @code{(use-modules @var{name})}. The name is
996 interpreted as for @code{scm_c_define_module}.
997 @end deftypefn
998
999 @deftypefn {C Procedure} SCM scm_c_export (const char *@var{name}, ...)
1000 Add the bindings designated by @var{name}, ... to the public interface
1001 of the current module. The list of names is terminated by
1002 @code{NULL}.
1003 @end deftypefn
1004
1005
1006 @node Variables
1007 @subsection Variables
1008 @tpindex Variables
1009
1010 Each module has its own hash table, sometimes known as an @dfn{obarray},
1011 that maps the names defined in that module to their corresponding
1012 variable objects.
1013
1014 A variable is a box-like object that can hold any Scheme value. It is
1015 said to be @dfn{undefined} if its box holds a special Scheme value that
1016 denotes undefined-ness (which is different from all other Scheme values,
1017 including for example @code{#f}); otherwise the variable is
1018 @dfn{defined}.
1019
1020 On its own, a variable object is anonymous. A variable is said to be
1021 @dfn{bound} when it is associated with a name in some way, usually a
1022 symbol in a module obarray. When this happens, the relationship is
1023 mutual: the variable is bound to the name (in that module), and the name
1024 (in that module) is bound to the variable.
1025
1026 (That's the theory, anyway. In practice, defined-ness and bound-ness
1027 sometimes get confused, because Lisp and Scheme implementations have
1028 often conflated --- or deliberately drawn no distinction between --- a
1029 name that is unbound and a name that is bound to a variable whose value
1030 is undefined. We will try to be clear about the difference and explain
1031 any confusion where it is unavoidable.)
1032
1033 Variables do not have a read syntax. Most commonly they are created and
1034 bound implicitly by @code{define} expressions: a top-level @code{define}
1035 expression of the form
1036
1037 @lisp
1038 (define @var{name} @var{value})
1039 @end lisp
1040
1041 @noindent
1042 creates a variable with initial value @var{value} and binds it to the
1043 name @var{name} in the current module. But they can also be created
1044 dynamically by calling one of the constructor procedures
1045 @code{make-variable} and @code{make-undefined-variable}.
1046
1047 @deffn {Scheme Procedure} make-undefined-variable
1048 @deffnx {C Function} scm_make_undefined_variable ()
1049 Return a variable that is initially unbound.
1050 @end deffn
1051
1052 @deffn {Scheme Procedure} make-variable init
1053 @deffnx {C Function} scm_make_variable (init)
1054 Return a variable initialized to value @var{init}.
1055 @end deffn
1056
1057 @deffn {Scheme Procedure} variable-bound? var
1058 @deffnx {C Function} scm_variable_bound_p (var)
1059 Return @code{#t} iff @var{var} is bound to a value.
1060 Throws an error if @var{var} is not a variable object.
1061 @end deffn
1062
1063 @deffn {Scheme Procedure} variable-ref var
1064 @deffnx {C Function} scm_variable_ref (var)
1065 Dereference @var{var} and return its value.
1066 @var{var} must be a variable object; see @code{make-variable}
1067 and @code{make-undefined-variable}.
1068 @end deffn
1069
1070 @deffn {Scheme Procedure} variable-set! var val
1071 @deffnx {C Function} scm_variable_set_x (var, val)
1072 Set the value of the variable @var{var} to @var{val}.
1073 @var{var} must be a variable object, @var{val} can be any
1074 value. Return an unspecified value.
1075 @end deffn
1076
1077 @deffn {Scheme Procedure} variable? obj
1078 @deffnx {C Function} scm_variable_p (obj)
1079 Return @code{#t} iff @var{obj} is a variable object, else
1080 return @code{#f}.
1081 @end deffn
1082
1083
1084 @node provide and require
1085 @subsection provide and require
1086
1087 Aubrey Jaffer, mostly to support his portable Scheme library SLIB,
1088 implemented a provide/require mechanism for many Scheme implementations.
1089 Library files in SLIB @emph{provide} a feature, and when user programs
1090 @emph{require} that feature, the library file is loaded in.
1091
1092 For example, the file @file{random.scm} in the SLIB package contains the
1093 line
1094
1095 @lisp
1096 (provide 'random)
1097 @end lisp
1098
1099 so to use its procedures, a user would type
1100
1101 @lisp
1102 (require 'random)
1103 @end lisp
1104
1105 and they would magically become available, @emph{but still have the same
1106 names!} So this method is nice, but not as good as a full-featured
1107 module system.
1108
1109 When SLIB is used with Guile, provide and require can be used to access
1110 its facilities.
1111
1112 @node Environments
1113 @subsection Environments
1114 @cindex environment
1115
1116 Scheme, as defined in R5RS, does @emph{not} have a full module system.
1117 However it does define the concept of a top-level @dfn{environment}.
1118 Such an environment maps identifiers (symbols) to Scheme objects such
1119 as procedures and lists: @ref{About Closure}. In other words, it
1120 implements a set of @dfn{bindings}.
1121
1122 Environments in R5RS can be passed as the second argument to
1123 @code{eval} (@pxref{Fly Evaluation}). Three procedures are defined to
1124 return environments: @code{scheme-report-environment},
1125 @code{null-environment} and @code{interaction-environment} (@pxref{Fly
1126 Evaluation}).
1127
1128 In addition, in Guile any module can be used as an R5RS environment,
1129 i.e., passed as the second argument to @code{eval}.
1130
1131 Note: the following two procedures are available only when the
1132 @code{(ice-9 r5rs)} module is loaded:
1133
1134 @lisp
1135 (use-modules (ice-9 r5rs))
1136 @end lisp
1137
1138 @deffn {Scheme Procedure} scheme-report-environment version
1139 @deffnx {Scheme Procedure} null-environment version
1140 @var{version} must be the exact integer `5', corresponding to revision
1141 5 of the Scheme report (the Revised^5 Report on Scheme).
1142 @code{scheme-report-environment} returns a specifier for an
1143 environment that is empty except for all bindings defined in the
1144 report that are either required or both optional and supported by the
1145 implementation. @code{null-environment} returns a specifier for an
1146 environment that is empty except for the (syntactic) bindings for all
1147 syntactic keywords defined in the report that are either required or
1148 both optional and supported by the implementation.
1149
1150 Currently Guile does not support values of @var{version} for other
1151 revisions of the report.
1152
1153 The effect of assigning (through the use of @code{eval}) a variable
1154 bound in a @code{scheme-report-environment} (for example @code{car})
1155 is unspecified. Currently the environments specified by
1156 @code{scheme-report-environment} are not immutable in Guile.
1157 @end deffn
1158
1159
1160
1161 @c Local Variables:
1162 @c TeX-master: "guile.texi"
1163 @c End: