deprecate fluid-let-syntax in favor of syntax-parameterize
[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, 2011
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 @deffn {Scheme Procedure} reload-module module
567 Revisit the source file that corresponds to @var{module}. Raises an
568 error if no source file is associated with the given module.
569 @end deffn
570
571
572 @node Included Guile Modules
573 @subsection Included Guile Modules
574
575 @c FIXME::martin: Review me!
576
577 Some modules are included in the Guile distribution; here are references
578 to the entries in this manual which describe them in more detail:
579
580 @table @strong
581 @item boot-9
582 boot-9 is Guile's initialization module, and it is always loaded when
583 Guile starts up.
584
585 @item (ice-9 expect)
586 Actions based on matching input from a port (@pxref{Expect}).
587
588 @item (ice-9 format)
589 Formatted output in the style of Common Lisp (@pxref{Formatted
590 Output}).
591
592 @item (ice-9 ftw)
593 File tree walker (@pxref{File Tree Walk}).
594
595 @item (ice-9 getopt-long)
596 Command line option processing (@pxref{getopt-long}).
597
598 @item (ice-9 history)
599 Refer to previous interactive expressions (@pxref{Value History}).
600
601 @item (ice-9 popen)
602 Pipes to and from child processes (@pxref{Pipes}).
603
604 @item (ice-9 pretty-print)
605 Nicely formatted output of Scheme expressions and objects
606 (@pxref{Pretty Printing}).
607
608 @item (ice-9 q)
609 First-in first-out queues (@pxref{Queues}).
610
611 @item (ice-9 rdelim)
612 Line- and character-delimited input (@pxref{Line/Delimited}).
613
614 @item (ice-9 readline)
615 @code{readline} interactive command line editing (@pxref{Readline
616 Support}).
617
618 @item (ice-9 receive)
619 Multiple-value handling with @code{receive} (@pxref{Multiple Values}).
620
621 @item (ice-9 regex)
622 Regular expression matching (@pxref{Regular Expressions}).
623
624 @item (ice-9 rw)
625 Block string input/output (@pxref{Block Reading and Writing}).
626
627 @item (ice-9 streams)
628 Sequence of values calculated on-demand (@pxref{Streams}).
629
630 @item (ice-9 syncase)
631 R5RS @code{syntax-rules} macro system (@pxref{Syntax Rules}).
632
633 @item (ice-9 threads)
634 Guile's support for multi threaded execution (@pxref{Scheduling}).
635
636 @item (ice-9 documentation)
637 Online documentation (REFFIXME).
638
639 @item (srfi srfi-1)
640 A library providing a lot of useful list and pair processing
641 procedures (@pxref{SRFI-1}).
642
643 @item (srfi srfi-2)
644 Support for @code{and-let*} (@pxref{SRFI-2}).
645
646 @item (srfi srfi-4)
647 Support for homogeneous numeric vectors (@pxref{SRFI-4}).
648
649 @item (srfi srfi-6)
650 Support for some additional string port procedures (@pxref{SRFI-6}).
651
652 @item (srfi srfi-8)
653 Multiple-value handling with @code{receive} (@pxref{SRFI-8}).
654
655 @item (srfi srfi-9)
656 Record definition with @code{define-record-type} (@pxref{SRFI-9}).
657
658 @item (srfi srfi-10)
659 Read hash extension @code{#,()} (@pxref{SRFI-10}).
660
661 @item (srfi srfi-11)
662 Multiple-value handling with @code{let-values} and @code{let*-values}
663 (@pxref{SRFI-11}).
664
665 @item (srfi srfi-13)
666 String library (@pxref{SRFI-13}).
667
668 @item (srfi srfi-14)
669 Character-set library (@pxref{SRFI-14}).
670
671 @item (srfi srfi-16)
672 @code{case-lambda} procedures of variable arity (@pxref{SRFI-16}).
673
674 @item (srfi srfi-17)
675 Getter-with-setter support (@pxref{SRFI-17}).
676
677 @item (srfi srfi-19)
678 Time/Date library (@pxref{SRFI-19}).
679
680 @item (srfi srfi-26)
681 Convenient syntax for partial application (@pxref{SRFI-26})
682
683 @item (srfi srfi-31)
684 @code{rec} convenient recursive expressions (@pxref{SRFI-31})
685
686 @item (ice-9 slib)
687 This module contains hooks for using Aubrey Jaffer's portable Scheme
688 library SLIB from Guile (@pxref{SLIB}).
689 @end table
690
691
692 @node R6RS Version References
693 @subsection R6RS Version References
694
695 Guile's module system includes support for locating modules based on
696 a declared version specifier of the same form as the one described in
697 R6RS (@pxref{Library form, R6RS Library Form,, r6rs, The Revised^6
698 Report on the Algorithmic Language Scheme}). By using the
699 @code{#:version} keyword in a @code{define-module} form, a module may
700 specify a version as a list of zero or more exact, nonnegative integers.
701
702 This version can then be used to locate the module during the module
703 search process. Client modules and callers of the @code{use-modules}
704 function may specify constraints on the versions of target modules by
705 providing a @dfn{version reference}, which has one of the following
706 forms:
707
708 @lisp
709 (@var{sub-version-reference} ...)
710 (and @var{version-reference} ...)
711 (or @var{version-reference} ...)
712 (not @var{version-reference})
713 @end lisp
714
715 in which @var{sub-version-reference} is in turn one of:
716
717 @lisp
718 (@var{sub-version})
719 (>= @var{sub-version})
720 (<= @var{sub-version})
721 (and @var{sub-version-reference} ...)
722 (or @var{sub-version-reference} ...)
723 (not @var{sub-version-reference})
724 @end lisp
725
726 in which @var{sub-version} is an exact, nonnegative integer as above. A
727 version reference matches a declared module version if each element of
728 the version reference matches a corresponding element of the module
729 version, according to the following rules:
730
731 @itemize @bullet
732 @item
733 The @code{and} sub-form matches a version or version element if every
734 element in the tail of the sub-form matches the specified version or
735 version element.
736
737 @item
738 The @code{or} sub-form matches a version or version element if any
739 element in the tail of the sub-form matches the specified version or
740 version element.
741
742 @item
743 The @code{not} sub-form matches a version or version element if the tail
744 of the sub-form does not match the version or version element.
745
746 @item
747 The @code{>=} sub-form matches a version element if the element is
748 greater than or equal to the @var{sub-version} in the tail of the
749 sub-form.
750
751 @item
752 The @code{<=} sub-form matches a version element if the version is less
753 than or equal to the @var{sub-version} in the tail of the sub-form.
754
755 @item
756 A @var{sub-version} matches a version element if one is @var{eqv?} to
757 the other.
758 @end itemize
759
760 For example, a module declared as:
761
762 @lisp
763 (define-module (mylib mymodule) #:version (1 2 0))
764 @end lisp
765
766 would be successfully loaded by any of the following @code{use-modules}
767 expressions:
768
769 @lisp
770 (use-modules ((mylib mymodule) #:version (1 2 (>= 0))))
771 (use-modules ((mylib mymodule) #:version (or (1 2 0) (1 2 1))))
772 (use-modules ((mylib mymodule) #:version ((and (>= 1) (not 2)) 2 0)))
773 @end lisp
774
775
776 @node R6RS Libraries
777 @subsection R6RS Libraries
778
779 In addition to the API described in the previous sections, you also
780 have the option to create modules using the portable @code{library} form
781 described in R6RS (@pxref{Library form, R6RS Library Form,, r6rs, The
782 Revised^6 Report on the Algorithmic Language Scheme}), and to import
783 libraries created in this format by other programmers. Guile's R6RS
784 library implementation takes advantage of the flexibility built into the
785 module system by expanding the R6RS library form into a corresponding
786 Guile @code{define-module} form that specifies equivalent import and
787 export requirements and includes the same body expressions. The library
788 expression:
789
790 @lisp
791 (library (mylib (1 2))
792 (import (otherlib (3)))
793 (export mybinding))
794 @end lisp
795
796 is equivalent to the module definition:
797
798 @lisp
799 (define-module (mylib)
800 #:version (1 2)
801 #:use-module ((otherlib) #:version (3))
802 #:export (mybinding))
803 @end lisp
804
805 Central to the mechanics of R6RS libraries is the concept of import
806 and export @dfn{levels}, which control the visibility of bindings at
807 various phases of a library's lifecycle --- macros necessary to
808 expand forms in the library's body need to be available at expand
809 time; variables used in the body of a procedure exported by the
810 library must be available at runtime. R6RS specifies the optional
811 @code{for} sub-form of an @emph{import set} specification (see below)
812 as a mechanism by which a library author can indicate that a
813 particular library import should take place at a particular phase
814 with respect to the lifecycle of the importing library.
815
816 Guile's library implementation uses a technique called
817 @dfn{implicit phasing} (first described by Abdulaziz Ghuloum and R.
818 Kent Dybvig), which allows the expander and compiler to automatically
819 determine the necessary visibility of a binding imported from another
820 library. As such, the @code{for} sub-form described below is ignored by
821 Guile (but may be required by Schemes in which phasing is explicit).
822
823 @deffn {Scheme Syntax} library name (export export-spec ...) (import import-spec ...) body ...
824 Defines a new library with the specified name, exports, and imports,
825 and evaluates the specified body expressions in this library's
826 environment.
827
828 The library @var{name} is a non-empty list of identifiers, optionally
829 ending with a version specification of the form described above
830 (@pxref{Creating Guile Modules}).
831
832 Each @var{export-spec} is the name of a variable defined or imported
833 by the library, or must take the form
834 @code{(rename (internal-name external-name) ...)}, where the
835 identifier @var{internal-name} names a variable defined or imported
836 by the library and @var{external-name} is the name by which the
837 variable is seen by importing libraries.
838
839 Each @var{import-spec} must be either an @dfn{import set} (see below)
840 or must be of the form @code{(for import-set import-level ...)},
841 where each @var{import-level} is one of:
842
843 @lisp
844 run
845 expand
846 (meta @var{level})
847 @end lisp
848
849 where @var{level} is an integer. Note that since Guile does not
850 require explicit phase specification, any @var{import-set}s found
851 inside of @code{for} sub-forms will be ``unwrapped'' during
852 expansion and processed as if they had been specified directly.
853
854 Import sets in turn take one of the following forms:
855
856 @lisp
857 @var{library-reference}
858 (library @var{library-reference})
859 (only @var{import-set} @var{identifier} ...)
860 (except @var{import-set} @var{identifier} ...)
861 (prefix @var{import-set} @var{identifier})
862 (rename @var{import-set} (@var{internal-identifier} @var{external-identifier}) ...)
863 @end lisp
864
865 where @var{library-reference} is a non-empty list of identifiers
866 ending with an optional version reference (@pxref{R6RS Version
867 References}), and the other sub-forms have the following semantics,
868 defined recursively on nested @var{import-set}s:
869
870 @itemize @bullet
871
872 @item
873 The @code{library} sub-form is used to specify libraries for import
874 whose names begin with the identifier ``library.''
875
876 @item
877 The @code{only} sub-form imports only the specified @var{identifier}s
878 from the given @var{import-set}.
879
880 @item
881 The @code{except} sub-form imports all of the bindings exported by
882 @var{import-set} except for those that appear in the specified list
883 of @var{identifier}s.
884
885 @item
886 The @code{prefix} sub-form imports all of the bindings exported
887 by @var{import-set}, first prefixing them with the specified
888 @var{identifier}.
889
890 @item
891 The @code{rename} sub-form imports all of the identifiers exported
892 by @var{import-set}. The binding for each @var{internal-identifier}
893 among these identifiers is made visible to the importing library as
894 the corresponding @var{external-identifier}; all other bindings are
895 imported using the names provided by @var{import-set}.
896
897 @end itemize
898
899 Note that because Guile translates R6RS libraries into module
900 definitions, an import specification may be used to declare a
901 dependency on a native Guile module --- although doing so may make
902 your libraries less portable to other Schemes.
903
904 @end deffn
905
906 @deffn {Scheme Syntax} import import-spec ...
907 Import into the current environment the libraries specified by the
908 given import specifications, where each @var{import-spec} takes the
909 same form as in the @code{library} form described above.
910 @end deffn
911
912
913 @node Accessing Modules from C
914 @subsection Accessing Modules from C
915
916 The last sections have described how modules are used in Scheme code,
917 which is the recommended way of creating and accessing modules. You
918 can also work with modules from C, but it is more cumbersome.
919
920 The following procedures are available.
921
922 @deftypefn {C Function} SCM scm_current_module ()
923 Return the module that is the @emph{current module}.
924 @end deftypefn
925
926 @deftypefn {C Function} SCM scm_set_current_module (SCM @var{module})
927 Set the current module to @var{module} and return the previous current
928 module.
929 @end deftypefn
930
931 @deftypefn {C Function} SCM scm_c_call_with_current_module (SCM @var{module}, SCM (*@var{func})(void *), void *@var{data})
932 Call @var{func} and make @var{module} the current module during the
933 call. The argument @var{data} is passed to @var{func}. The return
934 value of @code{scm_c_call_with_current_module} is the return value of
935 @var{func}.
936 @end deftypefn
937
938 @deftypefn {C Function} SCM scm_public_variable (SCM @var{module_name}, SCM @var{name})
939 @deftypefnx {C Function} SCM scm_c_public_variable ({const char *}@var{module_name}, {const char *}@var{name})
940 Find a the variable bound to the symbol @var{name} in the public
941 interface of the module named @var{module_name}.
942
943 @var{module_name} should be a list of symbols, when represented as a
944 Scheme object, or a space-separated string, in the @code{const char *}
945 case. See @code{scm_c_define_module} below, for more examples.
946
947 Signals an error if no module was found with the given name. If
948 @var{name} is not bound in the module, just returns @code{#f}.
949 @end deftypefn
950
951 @deftypefn {C Function} SCM scm_private_variable (SCM @var{module_name}, SCM @var{name})
952 @deftypefnx {C Function} SCM scm_c_private_variable ({const char *}@var{module_name}, {const char *}@var{name})
953 Like @code{scm_public_variable}, but looks in the internals of the
954 module named @var{module_name} instead of the public interface.
955 Logically, these procedures should only be called on modules you write.
956 @end deftypefn
957
958 @deftypefn {C Function} SCM scm_public_lookup (SCM @var{module_name}, SCM @var{name})
959 @deftypefnx {C Function} SCM scm_c_public_lookup ({const char *}@var{module_name}, {const char *}@var{name})
960 @deftypefnx {C Function} SCM scm_private_lookup (SCM @var{module_name}, SCM @var{name})
961 @deftypefnx {C Function} SCM scm_c_private_lookup ({const char *}@var{module_name}, {const char *}@var{name})
962 Like @code{scm_public_variable} or @code{scm_private_variable}, but if
963 the @var{name} is not bound in the module, signals an error. Returns a
964 variable, always.
965
966 @example
967 SCM my_eval_string (SCM str)
968 @{
969 static SCM eval_string_var = SCM_BOOL_F;
970
971 if (scm_is_false (eval_string_var))
972 eval_string_var =
973 scm_c_public_lookup ("ice-9 eval-string", "eval-string");
974
975 return scm_call_1 (scm_variable_ref (eval_string_var), str);
976 @}
977 @end example
978 @end deftypefn
979
980 @deftypefn {C Function} SCM scm_public_ref (SCM @var{module_name}, SCM @var{name})
981 @deftypefnx {C Function} SCM scm_c_public_ref ({const char *}@var{module_name}, {const char *}@var{name})
982 @deftypefnx {C Function} SCM scm_private_ref (SCM @var{module_name}, SCM @var{name})
983 @deftypefnx {C Function} SCM scm_c_private_ref ({const char *}@var{module_name}, {const char *}@var{name})
984 Like @code{scm_public_lookup} or @code{scm_private_lookup}, but
985 additionally dereferences the variable. If the variable object is
986 unbound, signals an error. Returns the value bound to @var{name} in
987 @var{module}.
988 @end deftypefn
989
990 In addition, there are a number of other lookup-related procedures. We
991 suggest that you use the @code{scm_public_} and @code{scm_private_}
992 family of procedures instead, if possible.
993
994 @deftypefn {C Function} SCM scm_c_lookup ({const char *}@var{name})
995 Return the variable bound to the symbol indicated by @var{name} in the
996 current module. If there is no such binding or the symbol is not
997 bound to a variable, signal an error.
998 @end deftypefn
999
1000 @deftypefn {C Function} SCM scm_lookup (SCM @var{name})
1001 Like @code{scm_c_lookup}, but the symbol is specified directly.
1002 @end deftypefn
1003
1004 @deftypefn {C Function} SCM scm_c_module_lookup (SCM @var{module}, {const char *}@var{name})
1005 @deftypefnx {C Function} SCM scm_module_lookup (SCM @var{module}, SCM @var{name})
1006 Like @code{scm_c_lookup} and @code{scm_lookup}, but the specified
1007 module is used instead of the current one.
1008 @end deftypefn
1009
1010 @deftypefn {C Function} SCM scm_module_variable (SCM @var{module}, SCM @var{name})
1011 Like @code{scm_module_lookup}, but if the binding does not exist, just
1012 returns @code{#f} instead of raising an error.
1013 @end deftypefn
1014
1015 To define a value, use @code{scm_define}:
1016
1017 @deftypefn {C Function} SCM scm_c_define ({const char *}@var{name}, SCM @var{val})
1018 Bind the symbol indicated by @var{name} to a variable in the current
1019 module and set that variable to @var{val}. When @var{name} is already
1020 bound to a variable, use that. Else create a new variable.
1021 @end deftypefn
1022
1023 @deftypefn {C Function} SCM scm_define (SCM @var{name}, SCM @var{val})
1024 Like @code{scm_c_define}, but the symbol is specified directly.
1025 @end deftypefn
1026
1027 @deftypefn {C Function} SCM scm_c_module_define (SCM @var{module}, {const char *}@var{name}, SCM @var{val})
1028 @deftypefnx {C Function} SCM scm_module_define (SCM @var{module}, SCM @var{name}, SCM @var{val})
1029 Like @code{scm_c_define} and @code{scm_define}, but the specified
1030 module is used instead of the current one.
1031 @end deftypefn
1032
1033 @deftypefn {C Function} SCM scm_module_reverse_lookup (SCM @var{module}, SCM @var{variable})
1034 Find the symbol that is bound to @var{variable} in @var{module}. When no such binding is found, return @var{#f}.
1035 @end deftypefn
1036
1037 @deftypefn {C Function} SCM scm_c_define_module ({const char *}@var{name}, void (*@var{init})(void *), void *@var{data})
1038 Define a new module named @var{name} and make it current while
1039 @var{init} is called, passing it @var{data}. Return the module.
1040
1041 The parameter @var{name} is a string with the symbols that make up
1042 the module name, separated by spaces. For example, @samp{"foo bar"} names
1043 the module @samp{(foo bar)}.
1044
1045 When there already exists a module named @var{name}, it is used
1046 unchanged, otherwise, an empty module is created.
1047 @end deftypefn
1048
1049 @deftypefn {C Function} SCM scm_c_resolve_module ({const char *}@var{name})
1050 Find the module name @var{name} and return it. When it has not
1051 already been defined, try to auto-load it. When it can't be found
1052 that way either, create an empty module. The name is interpreted as
1053 for @code{scm_c_define_module}.
1054 @end deftypefn
1055
1056 @deftypefn {C Function} SCM scm_resolve_module (SCM @var{name})
1057 Like @code{scm_c_resolve_module}, but the name is given as a real list
1058 of symbols.
1059 @end deftypefn
1060
1061 @deftypefn {C Function} SCM scm_c_use_module ({const char *}@var{name})
1062 Add the module named @var{name} to the uses list of the current
1063 module, as with @code{(use-modules @var{name})}. The name is
1064 interpreted as for @code{scm_c_define_module}.
1065 @end deftypefn
1066
1067 @deftypefn {C Function} SCM scm_c_export ({const char *}@var{name}, ...)
1068 Add the bindings designated by @var{name}, ... to the public interface
1069 of the current module. The list of names is terminated by
1070 @code{NULL}.
1071 @end deftypefn
1072
1073
1074 @node Variables
1075 @subsection Variables
1076 @tpindex Variables
1077
1078 Each module has its own hash table, sometimes known as an @dfn{obarray},
1079 that maps the names defined in that module to their corresponding
1080 variable objects.
1081
1082 A variable is a box-like object that can hold any Scheme value. It is
1083 said to be @dfn{undefined} if its box holds a special Scheme value that
1084 denotes undefined-ness (which is different from all other Scheme values,
1085 including for example @code{#f}); otherwise the variable is
1086 @dfn{defined}.
1087
1088 On its own, a variable object is anonymous. A variable is said to be
1089 @dfn{bound} when it is associated with a name in some way, usually a
1090 symbol in a module obarray. When this happens, the relationship is
1091 mutual: the variable is bound to the name (in that module), and the name
1092 (in that module) is bound to the variable.
1093
1094 (That's the theory, anyway. In practice, defined-ness and bound-ness
1095 sometimes get confused, because Lisp and Scheme implementations have
1096 often conflated --- or deliberately drawn no distinction between --- a
1097 name that is unbound and a name that is bound to a variable whose value
1098 is undefined. We will try to be clear about the difference and explain
1099 any confusion where it is unavoidable.)
1100
1101 Variables do not have a read syntax. Most commonly they are created and
1102 bound implicitly by @code{define} expressions: a top-level @code{define}
1103 expression of the form
1104
1105 @lisp
1106 (define @var{name} @var{value})
1107 @end lisp
1108
1109 @noindent
1110 creates a variable with initial value @var{value} and binds it to the
1111 name @var{name} in the current module. But they can also be created
1112 dynamically by calling one of the constructor procedures
1113 @code{make-variable} and @code{make-undefined-variable}.
1114
1115 @deffn {Scheme Procedure} make-undefined-variable
1116 @deffnx {C Function} scm_make_undefined_variable ()
1117 Return a variable that is initially unbound.
1118 @end deffn
1119
1120 @deffn {Scheme Procedure} make-variable init
1121 @deffnx {C Function} scm_make_variable (init)
1122 Return a variable initialized to value @var{init}.
1123 @end deffn
1124
1125 @deffn {Scheme Procedure} variable-bound? var
1126 @deffnx {C Function} scm_variable_bound_p (var)
1127 Return @code{#t} iff @var{var} is bound to a value.
1128 Throws an error if @var{var} is not a variable object.
1129 @end deffn
1130
1131 @deffn {Scheme Procedure} variable-ref var
1132 @deffnx {C Function} scm_variable_ref (var)
1133 Dereference @var{var} and return its value.
1134 @var{var} must be a variable object; see @code{make-variable}
1135 and @code{make-undefined-variable}.
1136 @end deffn
1137
1138 @deffn {Scheme Procedure} variable-set! var val
1139 @deffnx {C Function} scm_variable_set_x (var, val)
1140 Set the value of the variable @var{var} to @var{val}.
1141 @var{var} must be a variable object, @var{val} can be any
1142 value. Return an unspecified value.
1143 @end deffn
1144
1145 @deffn {Scheme Procedure} variable-unset! var
1146 @deffnx {C Function} scm_variable_unset_x (var)
1147 Unset the value of the variable @var{var}, leaving @var{var} unbound.
1148 @end deffn
1149
1150 @deffn {Scheme Procedure} variable? obj
1151 @deffnx {C Function} scm_variable_p (obj)
1152 Return @code{#t} iff @var{obj} is a variable object, else
1153 return @code{#f}.
1154 @end deffn
1155
1156
1157 @node provide and require
1158 @subsection provide and require
1159
1160 Aubrey Jaffer, mostly to support his portable Scheme library SLIB,
1161 implemented a provide/require mechanism for many Scheme implementations.
1162 Library files in SLIB @emph{provide} a feature, and when user programs
1163 @emph{require} that feature, the library file is loaded in.
1164
1165 For example, the file @file{random.scm} in the SLIB package contains the
1166 line
1167
1168 @lisp
1169 (provide 'random)
1170 @end lisp
1171
1172 so to use its procedures, a user would type
1173
1174 @lisp
1175 (require 'random)
1176 @end lisp
1177
1178 and they would magically become available, @emph{but still have the same
1179 names!} So this method is nice, but not as good as a full-featured
1180 module system.
1181
1182 When SLIB is used with Guile, provide and require can be used to access
1183 its facilities.
1184
1185 @node Environments
1186 @subsection Environments
1187 @cindex environment
1188
1189 Scheme, as defined in R5RS, does @emph{not} have a full module system.
1190 However it does define the concept of a top-level @dfn{environment}.
1191 Such an environment maps identifiers (symbols) to Scheme objects such
1192 as procedures and lists: @ref{About Closure}. In other words, it
1193 implements a set of @dfn{bindings}.
1194
1195 Environments in R5RS can be passed as the second argument to
1196 @code{eval} (@pxref{Fly Evaluation}). Three procedures are defined to
1197 return environments: @code{scheme-report-environment},
1198 @code{null-environment} and @code{interaction-environment} (@pxref{Fly
1199 Evaluation}).
1200
1201 In addition, in Guile any module can be used as an R5RS environment,
1202 i.e., passed as the second argument to @code{eval}.
1203
1204 Note: the following two procedures are available only when the
1205 @code{(ice-9 r5rs)} module is loaded:
1206
1207 @lisp
1208 (use-modules (ice-9 r5rs))
1209 @end lisp
1210
1211 @deffn {Scheme Procedure} scheme-report-environment version
1212 @deffnx {Scheme Procedure} null-environment version
1213 @var{version} must be the exact integer `5', corresponding to revision
1214 5 of the Scheme report (the Revised^5 Report on Scheme).
1215 @code{scheme-report-environment} returns a specifier for an
1216 environment that is empty except for all bindings defined in the
1217 report that are either required or both optional and supported by the
1218 implementation. @code{null-environment} returns a specifier for an
1219 environment that is empty except for the (syntactic) bindings for all
1220 syntactic keywords defined in the report that are either required or
1221 both optional and supported by the implementation.
1222
1223 Currently Guile does not support values of @var{version} for other
1224 revisions of the report.
1225
1226 The effect of assigning (through the use of @code{eval}) a variable
1227 bound in a @code{scheme-report-environment} (for example @code{car})
1228 is unspecified. Currently the environments specified by
1229 @code{scheme-report-environment} are not immutable in Guile.
1230 @end deffn
1231
1232
1233
1234 @c Local Variables:
1235 @c TeX-master: "guile.texi"
1236 @c End: