42df664ac83937381ceb5f8272e733fc358678e2
[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
4 @c Free Software Foundation, Inc.
5 @c See the file guile.texi for copying conditions.
6
7 @page
8 @node Modules
9 @section Modules
10 @cindex modules
11
12 When programs become large, naming conflicts can occur when a function
13 or global variable defined in one file has the same name as a function
14 or global variable in another file. Even just a @emph{similarity}
15 between function names can cause hard-to-find bugs, since a programmer
16 might type the wrong function name.
17
18 The approach used to tackle this problem is called @emph{information
19 encapsulation}, which consists of packaging functional units into a
20 given name space that is clearly separated from other name spaces.
21 @cindex encapsulation
22 @cindex information encapsulation
23 @cindex name space
24
25 The language features that allow this are usually called @emph{the
26 module system} because programs are broken up into modules that are
27 compiled separately (or loaded separately in an interpreter).
28
29 Older languages, like C, have limited support for name space
30 manipulation and protection. In C a variable or function is public by
31 default, and can be made local to a module with the @code{static}
32 keyword. But you cannot reference public variables and functions from
33 another module with different names.
34
35 More advanced module systems have become a common feature in recently
36 designed languages: ML, Python, Perl, and Modula 3 all allow the
37 @emph{renaming} of objects from a foreign module, so they will not
38 clutter the global name space.
39 @cindex name space - private
40
41 In addition, Guile offers variables as first-class objects. They can
42 be used for interacting with the module system.
43
44 @menu
45 * provide and require:: The SLIB feature mechanism.
46 * Environments:: R5RS top-level environments.
47 * The Guile module system:: How Guile does it.
48 * Dynamic Libraries:: Loading libraries of compiled code at run time.
49 * Variables:: First-class variables.
50 @end menu
51
52 @node provide and require
53 @subsection provide and require
54
55 Aubrey Jaffer, mostly to support his portable Scheme library SLIB,
56 implemented a provide/require mechanism for many Scheme implementations.
57 Library files in SLIB @emph{provide} a feature, and when user programs
58 @emph{require} that feature, the library file is loaded in.
59
60 For example, the file @file{random.scm} in the SLIB package contains the
61 line
62
63 @lisp
64 (provide 'random)
65 @end lisp
66
67 so to use its procedures, a user would type
68
69 @lisp
70 (require 'random)
71 @end lisp
72
73 and they would magically become available, @emph{but still have the same
74 names!} So this method is nice, but not as good as a full-featured
75 module system.
76
77 When SLIB is used with Guile, provide and require can be used to access
78 its facilities.
79
80 @node Environments
81 @subsection Environments
82 @cindex environment
83
84 Scheme, as defined in R5RS, does @emph{not} have a full module system.
85 However it does define the concept of a top-level @dfn{environment}.
86 Such an environment maps identifiers (symbols) to Scheme objects such
87 as procedures and lists: @ref{About Closure}. In other words, it
88 implements a set of @dfn{bindings}.
89
90 Environments in R5RS can be passed as the second argument to
91 @code{eval} (@pxref{Fly Evaluation}). Three procedures are defined to
92 return environments: @code{scheme-report-environment},
93 @code{null-environment} and @code{interaction-environment} (@pxref{Fly
94 Evaluation}).
95
96 In addition, in Guile any module can be used as an R5RS environment,
97 i.e., passed as the second argument to @code{eval}.
98
99 Note: the following two procedures are available only when the
100 @code{(ice-9 r5rs)} module is loaded:
101
102 @lisp
103 (use-modules (ice-9 r5rs))
104 @end lisp
105
106 @deffn {Scheme Procedure} scheme-report-environment version
107 @deffnx {Scheme Procedure} null-environment version
108 @var{version} must be the exact integer `5', corresponding to revision
109 5 of the Scheme report (the Revised^5 Report on Scheme).
110 @code{scheme-report-environment} returns a specifier for an
111 environment that is empty except for all bindings defined in the
112 report that are either required or both optional and supported by the
113 implementation. @code{null-environment} returns a specifier for an
114 environment that is empty except for the (syntactic) bindings for all
115 syntactic keywords defined in the report that are either required or
116 both optional and supported by the implementation.
117
118 Currently Guile does not support values of @var{version} for other
119 revisions of the report.
120
121 The effect of assigning (through the use of @code{eval}) a variable
122 bound in a @code{scheme-report-environment} (for example @code{car})
123 is unspecified. Currently the environments specified by
124 @code{scheme-report-environment} are not immutable in Guile.
125 @end deffn
126
127 @node The Guile module system
128 @subsection The Guile module system
129
130 The Guile module system extends the concept of environments, discussed
131 in the previous section, with mechanisms to define, use and customise
132 sets of bindings.
133
134 In 1996 Tom Lord implemented a full-featured module system for Guile which
135 allows loading Scheme source files into a private name space. This system has
136 been available since at least Guile version 1.1.
137
138 For Guile version 1.5.0 and later, the system has been improved to have better
139 integration from C code, more fine-grained user control over interfaces, and
140 documentation.
141
142 Although it is anticipated that the module system implementation will
143 change in the future, the Scheme programming interface described in this
144 manual should be considered stable. The C programming interface is
145 considered relatively stable, although at the time of this writing,
146 there is still some flux.
147
148 @menu
149 * General Information about Modules:: Guile module basics.
150 * Using Guile Modules:: How to use existing modules.
151 * Creating Guile Modules:: How to package your code into modules.
152 * Module System Reflection:: Accessing module objects at run-time.
153 * Included Guile Modules:: Which modules come with Guile?
154 * Accessing Modules from C:: How to work with modules with C code.
155 @end menu
156
157 @node General Information about Modules
158 @subsubsection General Information about Modules
159
160 A Guile module can be thought of as a collection of named procedures,
161 variables and macros. More precisely, it is a set of @dfn{bindings}
162 of symbols (names) to Scheme objects.
163
164 An environment is a mapping from identifiers (or symbols) to locations,
165 i.e., a set of bindings.
166 There are top-level environments and lexical environments.
167 The environment in which a lambda is executed is remembered as part of its
168 definition.
169
170 Within a module, all bindings are visible. Certain bindings
171 can be declared @dfn{public}, in which case they are added to the
172 module's so-called @dfn{export list}; this set of public bindings is
173 called the module's @dfn{public interface} (@pxref{Creating Guile
174 Modules}).
175
176 A client module @dfn{uses} a providing module's bindings by either
177 accessing the providing module's public interface, or by building a
178 custom interface (and then accessing that). In a custom interface, the
179 client module can @dfn{select} which bindings to access and can also
180 algorithmically @dfn{rename} bindings. In contrast, when using the
181 providing module's public interface, the entire export list is available
182 without renaming (@pxref{Using Guile Modules}).
183
184 To use a module, it must be found and loaded. All Guile modules have a
185 unique @dfn{module name}, which is a list of one or more symbols.
186 Examples are @code{(ice-9 popen)} or @code{(srfi srfi-11)}. When Guile
187 searches for the code of a module, it constructs the name of the file to
188 load by concatenating the name elements with slashes between the
189 elements and appending a number of file name extensions from the list
190 @code{%load-extensions} (@pxref{Loading}). The resulting file name is
191 then searched in all directories in the variable @code{%load-path}
192 (@pxref{Build Config}). For example, the @code{(ice-9 popen)} module
193 would result in the filename @code{ice-9/popen.scm} and searched in the
194 installation directories of Guile and in all other directories in the
195 load path.
196
197 @c FIXME::martin: Not sure about this, maybe someone knows better?
198 Every module has a so-called syntax transformer associated with it.
199 This is a procedure which performs all syntax transformation for the
200 time the module is read in and evaluated. When working with modules,
201 you can manipulate the current syntax transformer using the
202 @code{use-syntax} syntactic form or the @code{#:use-syntax} module
203 definition option (@pxref{Creating Guile Modules}).
204
205
206 @node Using Guile Modules
207 @subsubsection Using Guile Modules
208
209 To use a Guile module is to access either its public interface or a
210 custom interface (@pxref{General Information about Modules}). Both
211 types of access are handled by the syntactic form @code{use-modules},
212 which accepts one or more interface specifications and, upon evaluation,
213 arranges for those interfaces to be available to the current module.
214 This process may include locating and loading code for a given module if
215 that code has not yet been loaded, following @code{%load-path} (@pxref{Build
216 Config}).
217
218 An @dfn{interface specification} has one of two forms. The first
219 variation is simply to name the module, in which case its public
220 interface is the one accessed. For example:
221
222 @lisp
223 (use-modules (ice-9 popen))
224 @end lisp
225
226 Here, the interface specification is @code{(ice-9 popen)}, and the
227 result is that the current module now has access to @code{open-pipe},
228 @code{close-pipe}, @code{open-input-pipe}, and so on (@pxref{Included
229 Guile Modules}).
230
231 Note in the previous example that if the current module had already
232 defined @code{open-pipe}, that definition would be overwritten by the
233 definition in @code{(ice-9 popen)}. For this reason (and others), there
234 is a second variation of interface specification that not only names a
235 module to be accessed, but also selects bindings from it and renames
236 them to suit the current module's needs. For example:
237
238 @cindex binding renamer
239 @lisp
240 (use-modules ((ice-9 popen)
241 #:select ((open-pipe . pipe-open) close-pipe)
242 #:renamer (symbol-prefix-proc 'unixy:)))
243 @end lisp
244
245 Here, the interface specification is more complex than before, and the
246 result is that a custom interface with only two bindings is created and
247 subsequently accessed by the current module. The mapping of old to new
248 names is as follows:
249
250 @c Use `smallexample' since `table' is ugly. --ttn
251 @smallexample
252 (ice-9 popen) sees: current module sees:
253 open-pipe unixy:pipe-open
254 close-pipe unixy:close-pipe
255 @end smallexample
256
257 This example also shows how to use the convenience procedure
258 @code{symbol-prefix-proc}.
259
260 You can also directly refer to bindings in a module by using the
261 @code{@@} syntax. For example, instead of using the
262 @code{use-modules} statement from above and writing
263 @code{unixy:pipe-open} to refer to the @code{pipe-open} from the
264 @code{(ice-9 popen)}, you could also write @code{(@@ (ice-9 popen)
265 open-pipe)}. Thus an alternative to the complete @code{use-modules}
266 statement would be
267
268 @lisp
269 (define unixy:pipe-open (@@ (ice-9 popen) open-pipe))
270 (define unixy:close-pipe (@@ (ice-9 popen) close-pipe))
271 @end lisp
272
273 There is also @code{@@@@}, which can be used like @code{@@}, but does
274 not check whether the variable that is being accessed is actually
275 exported. Thus, @code{@@@@} can be thought of as the impolite version
276 of @code{@@} and should only be used as a last resort or for
277 debugging, for example.
278
279 Note that just as with a @code{use-modules} statement, any module that
280 has not yet been loaded yet will be loaded when referenced by a
281 @code{@@} or @code{@@@@} form.
282
283 You can also use the @code{@@} and @code{@@@@} syntaxes as the target
284 of a @code{set!} when the binding refers to a variable.
285
286 @c begin (scm-doc-string "boot-9.scm" "symbol-prefix-proc")
287 @deffn {Scheme Procedure} symbol-prefix-proc prefix-sym
288 Return a procedure that prefixes its arg (a symbol) with
289 @var{prefix-sym}.
290 @c Insert gratuitous C++ slam here. --ttn
291 @end deffn
292
293 @c begin (scm-doc-string "boot-9.scm" "use-modules")
294 @deffn syntax use-modules spec @dots{}
295 Resolve each interface specification @var{spec} into an interface and
296 arrange for these to be accessible by the current module. The return
297 value is unspecified.
298
299 @var{spec} can be a list of symbols, in which case it names a module
300 whose public interface is found and used.
301
302 @var{spec} can also be of the form:
303
304 @cindex binding renamer
305 @lisp
306 (MODULE-NAME [:select SELECTION] [:renamer RENAMER])
307 @end lisp
308
309 in which case a custom interface is newly created and used.
310 @var{module-name} is a list of symbols, as above; @var{selection} is a
311 list of selection-specs; and @var{renamer} is a procedure that takes a
312 symbol and returns its new name. A selection-spec is either a symbol or
313 a pair of symbols @code{(ORIG . SEEN)}, where @var{orig} is the name in
314 the used module and @var{seen} is the name in the using module. Note
315 that @var{seen} is also passed through @var{renamer}.
316
317 The @code{:select} and @code{:renamer} clauses are optional. If both are
318 omitted, the returned interface has no bindings. If the @code{:select}
319 clause is omitted, @var{renamer} operates on the used module's public
320 interface.
321
322 Signal error if module name is not resolvable.
323 @end deffn
324
325
326 @c FIXME::martin: Is this correct, and is there more to say?
327 @c FIXME::martin: Define term and concept `syntax transformer' somewhere.
328
329 @deffn syntax use-syntax module-name
330 Load the module @code{module-name} and use its syntax
331 transformer as the syntax transformer for the currently defined module,
332 as well as installing it as the current syntax transformer.
333 @end deffn
334
335 @deffn syntax @@ module-name binding-name
336 Refer to the binding named @var{binding-name} in module
337 @var{module-name}. The binding must have been exported by the module.
338 @end deffn
339
340 @deffn syntax @@@@ module-name binding-name
341 Refer to the binding named @var{binding-name} in module
342 @var{module-name}. The binding must not have been exported by the
343 module. This syntax is only intended for debugging purposes or as a
344 last resort.
345 @end deffn
346
347 @node Creating Guile Modules
348 @subsubsection Creating Guile Modules
349
350 When you want to create your own modules, you have to take the following
351 steps:
352
353 @itemize @bullet
354 @item
355 Create a Scheme source file and add all variables and procedures you wish
356 to export, or which are required by the exported procedures.
357
358 @item
359 Add a @code{define-module} form at the beginning.
360
361 @item
362 Export all bindings which should be in the public interface, either
363 by using @code{define-public} or @code{export} (both documented below).
364 @end itemize
365
366 @c begin (scm-doc-string "boot-9.scm" "define-module")
367 @deffn syntax define-module module-name [options @dots{}]
368 @var{module-name} is of the form @code{(hierarchy file)}. One
369 example of this is
370
371 @lisp
372 (define-module (ice-9 popen))
373 @end lisp
374
375 @code{define-module} makes this module available to Guile programs under
376 the given @var{module-name}.
377
378 The @var{options} are keyword/value pairs which specify more about the
379 defined module. The recognized options and their meaning is shown in
380 the following table.
381
382 @c fixme: Should we use "#:" or ":"?
383
384 @table @code
385 @item #:use-module @var{interface-specification}
386 Equivalent to a @code{(use-modules @var{interface-specification})}
387 (@pxref{Using Guile Modules}).
388
389 @item #:use-syntax @var{module}
390 Use @var{module} when loading the currently defined module, and install
391 it as the syntax transformer.
392
393 @item #:autoload @var{module} @var{symbol-list}
394 @cindex autoload
395 Load @var{module} when any of @var{symbol-list} are accessed. For
396 example,
397
398 @example
399 (define-module (my mod)
400 #:autoload (srfi srfi-1) (partition delete-duplicates))
401 ...
402 (if something
403 (set! foo (delete-duplicates ...)))
404 @end example
405
406 When a module is autoloaded, all its bindings become available.
407 @var{symbol-list} is just those that will first trigger the load.
408
409 An autoload is a good way to put off loading a big module until it's
410 really needed, for instance for faster startup or if it will only be
411 needed in certain circumstances.
412
413 @code{@@} can do a similar thing (@pxref{Using Guile Modules}), but in
414 that case an @code{@@} form must be written every time a binding from
415 the module is used.
416
417 @item #:export @var{list}
418 @cindex export
419 Export all identifiers in @var{list} which must be a list of symbols
420 or pairs of symbols. This is equivalent to @code{(export @var{list})}
421 in the module body.
422
423 @item #:re-export @var{list}
424 @cindex re-export
425 Re-export all identifiers in @var{list} which must be a list of
426 symbols or pairs of symbols. The symbols in @var{list} must be
427 imported by the current module from other modules. This is equivalent
428 to @code{re-export} below.
429
430 @item #:export-syntax @var{list}
431 @cindex export-syntax
432 Export all identifiers in @var{list} which must be a list of symbols
433 or pairs of symbols. The identifiers in @var{list} must refer to
434 macros (@pxref{Macros}) defined in the current module. This is
435 equivalent to @code{(export-syntax @var{list})} in the module body.
436
437 @item #:re-export-syntax @var{list}
438 @cindex re-export-syntax
439 Re-export all identifiers in @var{list} which must be a list of
440 symbols or pairs of symbols. The symbols in @var{list} must refer to
441 macros imported by the current module from other modules. This is
442 equivalent to @code{(re-export-syntax @var{list})} in the module body.
443
444 @item #:replace @var{list}
445 @cindex replace
446 @cindex replacing binding
447 @cindex overriding binding
448 @cindex duplicate binding
449 Export all identifiers in @var{list} (a list of symbols or pairs of
450 symbols) and mark them as @dfn{replacing bindings}. In the module
451 user's name space, this will have the effect of replacing any binding
452 with the same name that is not also ``replacing''. Normally a
453 replacement results in an ``override'' warning message,
454 @code{#:replace} avoids that.
455
456 This is useful for modules that export bindings that have the same
457 name as core bindings. @code{#:replace}, in a sense, lets Guile know
458 that the module @emph{purposefully} replaces a core binding. It is
459 important to note, however, that this binding replacement is confined
460 to the name space of the module user. In other words, the value of the
461 core binding in question remains unchanged for other modules.
462
463 For instance, SRFI-39 exports a binding named
464 @code{current-input-port} (@pxref{SRFI-39}) that is a function which
465 is upwardly compatible with the core @code{current-input-port}
466 function. Therefore, SRFI-39 exports its version with
467 @code{#:replace}.
468
469 SRFI-19, on the other hand, exports its own version of
470 @code{current-time} (@pxref{SRFI-19 Time}) which is not compatible
471 with the core @code{current-time} function (@pxref{Time}). Therefore,
472 SRFI-19 does not use @code{#:replace}.
473
474 The @code{#:replace} option can also be used by a module which is
475 intentionally producing a new special kind of environment and should
476 override any core or other bindings already in scope. For example
477 perhaps a logic processing environment where @code{<=} is an inference
478 instead of a comparison.
479
480 The @code{#:duplicates} (see below) provides fine-grain control about
481 duplicate binding handling on the module-user side.
482
483 @item #:duplicates @var{list}
484 @cindex duplicate binding handlers
485 @cindex duplicate binding
486 @cindex overriding binding
487 Tell Guile to handle duplicate bindings for the bindings imported by
488 the current module according to the policy defined by @var{list}, a
489 list of symbols. @var{list} must contain symbols representing a
490 duplicate binding handling policy chosen among the following:
491
492 @table @code
493 @item check
494 Raises an error when a binding is imported from more than one place.
495 @item warn
496 Issue a warning when a binding is imported from more than one place
497 and leave the responsibility of actually handling the duplication to
498 the next duplicate binding handler.
499 @item replace
500 When a new binding is imported that has the same name as a previously
501 imported binding, then do the following:
502
503 @enumerate
504 @item
505 @cindex replacing binding
506 If the old binding was said to be @dfn{replacing} (via the
507 @code{#:replace} option above) and the new binding is not replacing,
508 the keep the old binding.
509 @item
510 If the old binding was not said to be replacing and the new binding is
511 replacing, then replace the old binding with the new one.
512 @item
513 If neither the old nor the new binding is replacing, then keep the old
514 one.
515 @end enumerate
516
517 @item warn-override-core
518 Issue a warning when a core binding is being overwritten and actually
519 override the core binding with the new one.
520 @item first
521 In case of duplicate bindings, the firstly imported binding is always
522 the one which is kept.
523 @item last
524 In case of duplicate bindings, the lastly imported binding is always
525 the one which is kept.
526 @item noop
527 In case of duplicate bindings, leave the responsibility to the next
528 duplicate handler.
529 @end table
530
531 If @var{list} contains more than one symbol, then the duplicate
532 binding handlers which appear first will be used first when resolving
533 a duplicate binding situation. As mentioned above, some resolution
534 policies may explicitly leave the responsibility of handling the
535 duplication to the next handler in @var{list}.
536
537 @findex default-duplicate-binding-handler
538 The default duplicate binding resolution policy is given by the
539 @code{default-duplicate-binding-handler} procedure, and is
540
541 @lisp
542 (replace warn-override-core warn last)
543 @end lisp
544
545 @item #:no-backtrace
546 @cindex no backtrace
547 Tell Guile not to record information for procedure backtraces when
548 executing the procedures in this module.
549
550 @item #:pure
551 @cindex pure module
552 Create a @dfn{pure} module, that is a module which does not contain any
553 of the standard procedure bindings except for the syntax forms. This is
554 useful if you want to create @dfn{safe} modules, that is modules which
555 do not know anything about dangerous procedures.
556 @end table
557
558 @end deffn
559 @c end
560
561 @deffn syntax export variable @dots{}
562 Add all @var{variable}s (which must be symbols or pairs of symbols) to
563 the list of exported bindings of the current module. If @var{variable}
564 is a pair, its @code{car} gives the name of the variable as seen by the
565 current module and its @code{cdr} specifies a name for the binding in
566 the current module's public interface.
567 @end deffn
568
569 @c begin (scm-doc-string "boot-9.scm" "define-public")
570 @deffn syntax define-public @dots{}
571 Equivalent to @code{(begin (define foo ...) (export foo))}.
572 @end deffn
573 @c end
574
575 @deffn syntax re-export variable @dots{}
576 Add all @var{variable}s (which must be symbols or pairs of symbols) to
577 the list of re-exported bindings of the current module. Pairs of
578 symbols are handled as in @code{export}. Re-exported bindings must be
579 imported by the current module from some other module.
580 @end deffn
581
582 @node Module System Reflection
583 @subsubsection Module System Reflection
584
585 The previous sections have described a declarative view of the module
586 system. You can also work with it programmatically by accessing and
587 modifying various parts of the Scheme objects that Guile uses to
588 implement the module system.
589
590 At any time, there is a @dfn{current module}. This module is the one
591 where a top-level @code{define} and similar syntax will add new
592 bindings. You can find other module objects with @code{resolve-module},
593 for example.
594
595 These module objects can be used as the second argument to @code{eval}.
596
597 @deffn {Scheme Procedure} current-module
598 Return the current module object.
599 @end deffn
600
601 @deffn {Scheme Procedure} set-current-module module
602 Set the current module to @var{module} and return
603 the previous current module.
604 @end deffn
605
606 @deffn {Scheme Procedure} save-module-excursion thunk
607 Call @var{thunk} within a @code{dynamic-wind} such that the module that
608 is current at invocation time is restored when @var{thunk}'s dynamic
609 extent is left (@pxref{Dynamic Wind}).
610
611 More precisely, if @var{thunk} escapes non-locally, the current module
612 (at the time of escape) is saved, and the original current module (at
613 the time @var{thunk}'s dynamic extent was last entered) is restored. If
614 @var{thunk}'s dynamic extent is re-entered, then the current module is
615 saved, and the previously saved inner module is set current again.
616 @end deffn
617
618 @deffn {Scheme Procedure} resolve-module name
619 Find the module named @var{name} and return it. When it has not already
620 been defined, try to auto-load it. When it can't be found that way
621 either, create an empty module. The name is a list of symbols.
622 @end deffn
623
624 @deffn {Scheme Procedure} resolve-interface name
625 Find the module named @var{name} as with @code{resolve-module} and
626 return its interface. The interface of a module is also a module
627 object, but it contains only the exported bindings.
628 @end deffn
629
630 @deffn {Scheme Procedure} module-use! module interface
631 Add @var{interface} to the front of the use-list of @var{module}. Both
632 arguments should be module objects, and @var{interface} should very
633 likely be a module returned by @code{resolve-interface}.
634 @end deffn
635
636
637 @node Included Guile Modules
638 @subsubsection Included Guile Modules
639
640 @c FIXME::martin: Review me!
641
642 Some modules are included in the Guile distribution; here are references
643 to the entries in this manual which describe them in more detail:
644
645 @table @strong
646 @item boot-9
647 boot-9 is Guile's initialization module, and it is always loaded when
648 Guile starts up.
649
650 @item (ice-9 debug)
651 Mikael Djurfeldt's source-level debugging support for Guile
652 (@pxref{Tracing}).
653
654 @item (ice-9 expect)
655 Actions based on matching input from a port (@pxref{Expect}).
656
657 @item (ice-9 format)
658 Formatted output in the style of Common Lisp (@pxref{Formatted
659 Output}).
660
661 @item (ice-9 ftw)
662 File tree walker (@pxref{File Tree Walk}).
663
664 @item (ice-9 getopt-long)
665 Command line option processing (@pxref{getopt-long}).
666
667 @item (ice-9 history)
668 Refer to previous interactive expressions (@pxref{Value History}).
669
670 @item (ice-9 popen)
671 Pipes to and from child processes (@pxref{Pipes}).
672
673 @item (ice-9 pretty-print)
674 Nicely formatted output of Scheme expressions and objects
675 (@pxref{Pretty Printing}).
676
677 @item (ice-9 q)
678 First-in first-out queues (@pxref{Queues}).
679
680 @item (ice-9 rdelim)
681 Line- and character-delimited input (@pxref{Line/Delimited}).
682
683 @item (ice-9 readline)
684 @code{readline} interactive command line editing (@pxref{Readline
685 Support}).
686
687 @item (ice-9 receive)
688 Multiple-value handling with @code{receive} (@pxref{Multiple Values}).
689
690 @item (ice-9 regex)
691 Regular expression matching (@pxref{Regular Expressions}).
692
693 @item (ice-9 rw)
694 Block string input/output (@pxref{Block Reading and Writing}).
695
696 @item (ice-9 streams)
697 Sequence of values calculated on-demand (@pxref{Streams}).
698
699 @item (ice-9 syncase)
700 R5RS @code{syntax-rules} macro system (@pxref{Syntax Rules}).
701
702 @item (ice-9 threads)
703 Guile's support for multi threaded execution (@pxref{Scheduling}).
704
705 @item (ice-9 documentation)
706 Online documentation (REFFIXME).
707
708 @item (srfi srfi-1)
709 A library providing a lot of useful list and pair processing
710 procedures (@pxref{SRFI-1}).
711
712 @item (srfi srfi-2)
713 Support for @code{and-let*} (@pxref{SRFI-2}).
714
715 @item (srfi srfi-4)
716 Support for homogeneous numeric vectors (@pxref{SRFI-4}).
717
718 @item (srfi srfi-6)
719 Support for some additional string port procedures (@pxref{SRFI-6}).
720
721 @item (srfi srfi-8)
722 Multiple-value handling with @code{receive} (@pxref{SRFI-8}).
723
724 @item (srfi srfi-9)
725 Record definition with @code{define-record-type} (@pxref{SRFI-9}).
726
727 @item (srfi srfi-10)
728 Read hash extension @code{#,()} (@pxref{SRFI-10}).
729
730 @item (srfi srfi-11)
731 Multiple-value handling with @code{let-values} and @code{let*-values}
732 (@pxref{SRFI-11}).
733
734 @item (srfi srfi-13)
735 String library (@pxref{SRFI-13}).
736
737 @item (srfi srfi-14)
738 Character-set library (@pxref{SRFI-14}).
739
740 @item (srfi srfi-16)
741 @code{case-lambda} procedures of variable arity (@pxref{SRFI-16}).
742
743 @item (srfi srfi-17)
744 Getter-with-setter support (@pxref{SRFI-17}).
745
746 @item (srfi srfi-19)
747 Time/Date library (@pxref{SRFI-19}).
748
749 @item (srfi srfi-26)
750 Convenient syntax for partial application (@pxref{SRFI-26})
751
752 @item (srfi srfi-31)
753 @code{rec} convenient recursive expressions (@pxref{SRFI-31})
754
755 @item (ice-9 slib)
756 This module contains hooks for using Aubrey Jaffer's portable Scheme
757 library SLIB from Guile (@pxref{SLIB}).
758 @end table
759
760
761 @node Accessing Modules from C
762 @subsubsection Accessing Modules from C
763
764 The last sections have described how modules are used in Scheme code,
765 which is the recommended way of creating and accessing modules. You
766 can also work with modules from C, but it is more cumbersome.
767
768 The following procedures are available.
769
770 @deftypefn {C Procedure} SCM scm_current_module ()
771 Return the module that is the @emph{current module}.
772 @end deftypefn
773
774 @deftypefn {C Procedure} SCM scm_set_current_module (SCM @var{module})
775 Set the current module to @var{module} and return the previous current
776 module.
777 @end deftypefn
778
779 @deftypefn {C Procedure} SCM scm_c_call_with_current_module (SCM @var{module}, SCM (*@var{func})(void *), void *@var{data})
780 Call @var{func} and make @var{module} the current module during the
781 call. The argument @var{data} is passed to @var{func}. The return
782 value of @code{scm_c_call_with_current_module} is the return value of
783 @var{func}.
784 @end deftypefn
785
786 @deftypefn {C Procedure} SCM scm_c_lookup (const char *@var{name})
787 Return the variable bound to the symbol indicated by @var{name} in the
788 current module. If there is no such binding or the symbol is not
789 bound to a variable, signal an error.
790 @end deftypefn
791
792 @deftypefn {C Procedure} SCM scm_lookup (SCM @var{name})
793 Like @code{scm_c_lookup}, but the symbol is specified directly.
794 @end deftypefn
795
796 @deftypefn {C Procedure} SCM scm_c_module_lookup (SCM @var{module}, const char *@var{name})
797 @deftypefnx {C Procedure} SCM scm_module_lookup (SCM @var{module}, SCM @var{name})
798 Like @code{scm_c_lookup} and @code{scm_lookup}, but the specified
799 module is used instead of the current one.
800 @end deftypefn
801
802 @deftypefn {C Procedure} SCM scm_c_define (const char *@var{name}, SCM @var{val})
803 Bind the symbol indicated by @var{name} to a variable in the current
804 module and set that variable to @var{val}. When @var{name} is already
805 bound to a variable, use that. Else create a new variable.
806 @end deftypefn
807
808 @deftypefn {C Procedure} SCM scm_define (SCM @var{name}, SCM @var{val})
809 Like @code{scm_c_define}, but the symbol is specified directly.
810 @end deftypefn
811
812 @deftypefn {C Procedure} SCM scm_c_module_define (SCM @var{module}, const char *@var{name}, SCM @var{val})
813 @deftypefnx {C Procedure} SCM scm_module_define (SCM @var{module}, SCM @var{name}, SCM @var{val})
814 Like @code{scm_c_define} and @code{scm_define}, but the specified
815 module is used instead of the current one.
816 @end deftypefn
817
818 @deftypefn {C Procedure} SCM scm_module_reverse_lookup (SCM @var{module}, SCM @var{variable})
819 Find the symbol that is bound to @var{variable} in @var{module}. When no such binding is found, return @var{#f}.
820 @end deftypefn
821
822 @deftypefn {C Procedure} SCM scm_c_define_module (const char *@var{name}, void (*@var{init})(void *), void *@var{data})
823 Define a new module named @var{name} and make it current while
824 @var{init} is called, passing it @var{data}. Return the module.
825
826 The parameter @var{name} is a string with the symbols that make up
827 the module name, separated by spaces. For example, @samp{"foo bar"} names
828 the module @samp{(foo bar)}.
829
830 When there already exists a module named @var{name}, it is used
831 unchanged, otherwise, an empty module is created.
832 @end deftypefn
833
834 @deftypefn {C Procedure} SCM scm_c_resolve_module (const char *@var{name})
835 Find the module name @var{name} and return it. When it has not
836 already been defined, try to auto-load it. When it can't be found
837 that way either, create an empty module. The name is interpreted as
838 for @code{scm_c_define_module}.
839 @end deftypefn
840
841 @deftypefn {C Procedure} SCM scm_resolve_module (SCM @var{name})
842 Like @code{scm_c_resolve_module}, but the name is given as a real list
843 of symbols.
844 @end deftypefn
845
846 @deftypefn {C Procedure} SCM scm_c_use_module (const char *@var{name})
847 Add the module named @var{name} to the uses list of the current
848 module, as with @code{(use-modules @var{name})}. The name is
849 interpreted as for @code{scm_c_define_module}.
850 @end deftypefn
851
852 @deftypefn {C Procedure} SCM scm_c_export (const char *@var{name}, ...)
853 Add the bindings designated by @var{name}, ... to the public interface
854 of the current module. The list of names is terminated by
855 @code{NULL}.
856 @end deftypefn
857
858 @node Dynamic Libraries
859 @subsection Dynamic Libraries
860
861 Most modern Unices have something called @dfn{shared libraries}. This
862 ordinarily means that they have the capability to share the executable
863 image of a library between several running programs to save memory and
864 disk space. But generally, shared libraries give a lot of additional
865 flexibility compared to the traditional static libraries. In fact,
866 calling them `dynamic' libraries is as correct as calling them `shared'.
867
868 Shared libraries really give you a lot of flexibility in addition to the
869 memory and disk space savings. When you link a program against a shared
870 library, that library is not closely incorporated into the final
871 executable. Instead, the executable of your program only contains
872 enough information to find the needed shared libraries when the program
873 is actually run. Only then, when the program is starting, is the final
874 step of the linking process performed. This means that you need not
875 recompile all programs when you install a new, only slightly modified
876 version of a shared library. The programs will pick up the changes
877 automatically the next time they are run.
878
879 Now, when all the necessary machinery is there to perform part of the
880 linking at run-time, why not take the next step and allow the programmer
881 to explicitly take advantage of it from within his program? Of course,
882 many operating systems that support shared libraries do just that, and
883 chances are that Guile will allow you to access this feature from within
884 your Scheme programs. As you might have guessed already, this feature
885 is called @dfn{dynamic linking}.@footnote{Some people also refer to the
886 final linking stage at program startup as `dynamic linking', so if you
887 want to make yourself perfectly clear, it is probably best to use the
888 more technical term @dfn{dlopening}, as suggested by Gordon Matzigkeit
889 in his libtool documentation.}
890
891 As with many aspects of Guile, there is a low-level way to access the
892 dynamic linking apparatus, and a more high-level interface that
893 integrates dynamically linked libraries into the module system.
894
895 @menu
896 * Low level dynamic linking::
897 * Compiled Code Modules::
898 * Dynamic Linking and Compiled Code Modules::
899 * Compiled Code Installation::
900 @end menu
901
902 @node Low level dynamic linking
903 @subsubsection Low level dynamic linking
904
905 When using the low level procedures to do your dynamic linking, you have
906 complete control over which library is loaded when and what gets done
907 with it.
908
909 @deffn {Scheme Procedure} dynamic-link library
910 @deffnx {C Function} scm_dynamic_link (library)
911 Find the shared library denoted by @var{library} (a string) and link it
912 into the running Guile application. When everything works out, return a
913 Scheme object suitable for representing the linked object file.
914 Otherwise an error is thrown. How object files are searched is system
915 dependent.
916
917 Normally, @var{library} is just the name of some shared library file
918 that will be searched for in the places where shared libraries usually
919 reside, such as in @file{/usr/lib} and @file{/usr/local/lib}.
920 @end deffn
921
922 @deffn {Scheme Procedure} dynamic-object? obj
923 @deffnx {C Function} scm_dynamic_object_p (obj)
924 Return @code{#t} if @var{obj} is a dynamic library handle, or @code{#f}
925 otherwise.
926 @end deffn
927
928 @deffn {Scheme Procedure} dynamic-unlink dobj
929 @deffnx {C Function} scm_dynamic_unlink (dobj)
930 Unlink the indicated object file from the application. The
931 argument @var{dobj} must have been obtained by a call to
932 @code{dynamic-link}. After @code{dynamic-unlink} has been
933 called on @var{dobj}, its content is no longer accessible.
934 @end deffn
935
936 @deffn {Scheme Procedure} dynamic-func name dobj
937 @deffnx {C Function} scm_dynamic_func (name, dobj)
938 Search the dynamic object @var{dobj} for the C function
939 indicated by the string @var{name} and return some Scheme
940 handle that can later be used with @code{dynamic-call} to
941 actually call the function.
942
943 Regardless whether your C compiler prepends an underscore @samp{_} to
944 the global names in a program, you should @strong{not} include this
945 underscore in @var{function}. Guile knows whether the underscore is
946 needed or not and will add it when necessary.
947 @end deffn
948
949 @deffn {Scheme Procedure} dynamic-call func dobj
950 @deffnx {C Function} scm_dynamic_call (func, dobj)
951 Call the C function indicated by @var{func} and @var{dobj}.
952 The function is passed no arguments and its return value is
953 ignored. When @var{function} is something returned by
954 @code{dynamic-func}, call that function and ignore @var{dobj}.
955 When @var{func} is a string , look it up in @var{dynobj}; this
956 is equivalent to
957 @smallexample
958 (dynamic-call (dynamic-func @var{func} @var{dobj}) #f)
959 @end smallexample
960
961 Interrupts are deferred while the C function is executing (with
962 @code{SCM_DEFER_INTS}/@code{SCM_ALLOW_INTS}).
963 @end deffn
964
965 @deffn {Scheme Procedure} dynamic-args-call func dobj args
966 @deffnx {C Function} scm_dynamic_args_call (func, dobj, args)
967 Call the C function indicated by @var{func} and @var{dobj},
968 just like @code{dynamic-call}, but pass it some arguments and
969 return its return value. The C function is expected to take
970 two arguments and return an @code{int}, just like @code{main}:
971 @smallexample
972 int c_func (int argc, char **argv);
973 @end smallexample
974
975 The parameter @var{args} must be a list of strings and is
976 converted into an array of @code{char *}. The array is passed
977 in @var{argv} and its size in @var{argc}. The return value is
978 converted to a Scheme number and returned from the call to
979 @code{dynamic-args-call}.
980 @end deffn
981
982 When dynamic linking is disabled or not supported on your system,
983 the above functions throw errors, but they are still available.
984
985 Here is a small example that works on GNU/Linux:
986
987 @smallexample
988 (define libc-obj (dynamic-link "libc.so"))
989 libc-obj
990 @result{} #<dynamic-object "libc.so">
991 (dynamic-args-call 'rand libc-obj '())
992 @result{} 269167349
993 (dynamic-unlink libc-obj)
994 libc-obj
995 @result{} #<dynamic-object "libc.so" (unlinked)>
996 @end smallexample
997
998 As you can see, after calling @code{dynamic-unlink} on a dynamically
999 linked library, it is marked as @samp{(unlinked)} and you are no longer
1000 able to use it with @code{dynamic-call}, etc. Whether the library is
1001 really removed from you program is system-dependent and will generally
1002 not happen when some other parts of your program still use it. In the
1003 example above, @code{libc} is almost certainly not removed from your
1004 program because it is badly needed by almost everything.
1005
1006 The functions to call a function from a dynamically linked library,
1007 @code{dynamic-call} and @code{dynamic-args-call}, are not very powerful.
1008 They are mostly intended to be used for calling specially written
1009 initialization functions that will then add new primitives to Guile.
1010 For example, we do not expect that you will dynamically link
1011 @file{libX11} with @code{dynamic-link} and then construct a beautiful
1012 graphical user interface just by using @code{dynamic-call} and
1013 @code{dynamic-args-call}. Instead, the usual way would be to write a
1014 special Guile<->X11 glue library that has intimate knowledge about both
1015 Guile and X11 and does whatever is necessary to make them inter-operate
1016 smoothly. This glue library could then be dynamically linked into a
1017 vanilla Guile interpreter and activated by calling its initialization
1018 function. That function would add all the new types and primitives to
1019 the Guile interpreter that it has to offer.
1020
1021 From this setup the next logical step is to integrate these glue
1022 libraries into the module system of Guile so that you can load new
1023 primitives into a running system just as you can load new Scheme code.
1024
1025 There is, however, another possibility to get a more thorough access to
1026 the functions contained in a dynamically linked library. Anthony Green
1027 has written @file{libffi}, a library that implements a @dfn{foreign
1028 function interface} for a number of different platforms. With it, you
1029 can extend the Spartan functionality of @code{dynamic-call} and
1030 @code{dynamic-args-call} considerably. There is glue code available in
1031 the Guile contrib archive to make @file{libffi} accessible from Guile.
1032
1033 @node Compiled Code Modules
1034 @subsubsection Putting Compiled Code into Modules
1035
1036 The new primitives that you add to Guile with
1037 @code{scm_c_define_gsubr} (@pxref{Primitive Procedures}) or with any
1038 of the other mechanisms are placed into the @code{(guile-user)} module
1039 by default. However, it is also possible to put new primitives into
1040 other modules.
1041
1042 The mechanism for doing so is not very well thought out and is likely to
1043 change when the module system of Guile itself is revised, but it is
1044 simple and useful enough to document it as it stands.
1045
1046 What @code{scm_c_define_gsubr} and the functions used by the snarfer
1047 really do is to add the new primitives to whatever module is the
1048 @emph{current module} when they are called. This is analogous to the
1049 way Scheme code is put into modules: the @code{define-module} expression
1050 at the top of a Scheme source file creates a new module and makes it the
1051 current module while the rest of the file is evaluated. The
1052 @code{define} expressions in that file then add their new definitions to
1053 this current module.
1054
1055 Therefore, all we need to do is to make sure that the right module is
1056 current when calling @code{scm_c_define_gsubr} for our new primitives.
1057
1058 @node Dynamic Linking and Compiled Code Modules
1059 @subsubsection Dynamic Linking and Compiled Code Modules
1060
1061 The most interesting application of dynamically linked libraries is
1062 probably to use them for providing @emph{compiled code modules} to
1063 Scheme programs. As much fun as programming in Scheme is, every now and
1064 then comes the need to write some low-level C stuff to make Scheme even
1065 more fun.
1066
1067 Not only can you put these new primitives into their own module (see the
1068 previous section), you can even put them into a shared library that is
1069 only then linked to your running Guile image when it is actually
1070 needed.
1071
1072 An example will hopefully make everything clear. Suppose we want to
1073 make the Bessel functions of the C library available to Scheme in the
1074 module @samp{(math bessel)}. First we need to write the appropriate
1075 glue code to convert the arguments and return values of the functions
1076 from Scheme to C and back. Additionally, we need a function that will
1077 add them to the set of Guile primitives. Because this is just an
1078 example, we will only implement this for the @code{j0} function.
1079
1080 @c FIXME::martin: Change all gh_ references to their scm_ equivalents.
1081
1082 @smallexample
1083 #include <math.h>
1084 #include <libguile.h>
1085
1086 SCM
1087 j0_wrapper (SCM x)
1088 @{
1089 return scm_double2num (j0 (scm_num2dbl (x, "j0")));
1090 @}
1091
1092 void
1093 init_math_bessel ()
1094 @{
1095 scm_c_define_gsubr ("j0", 1, 0, 0, j0_wrapper);
1096 @}
1097 @end smallexample
1098
1099 We can already try to bring this into action by manually calling the low
1100 level functions for performing dynamic linking. The C source file needs
1101 to be compiled into a shared library. Here is how to do it on
1102 GNU/Linux, please refer to the @code{libtool} documentation for how to
1103 create dynamically linkable libraries portably.
1104
1105 @smallexample
1106 gcc -shared -o libbessel.so -fPIC bessel.c
1107 @end smallexample
1108
1109 Now fire up Guile:
1110
1111 @lisp
1112 (define bessel-lib (dynamic-link "./libbessel.so"))
1113 (dynamic-call "init_math_bessel" bessel-lib)
1114 (j0 2)
1115 @result{} 0.223890779141236
1116 @end lisp
1117
1118 The filename @file{./libbessel.so} should be pointing to the shared
1119 library produced with the @code{gcc} command above, of course. The
1120 second line of the Guile interaction will call the
1121 @code{init_math_bessel} function which in turn will register the C
1122 function @code{j0_wrapper} with the Guile interpreter under the name
1123 @code{j0}. This function becomes immediately available and we can call
1124 it from Scheme.
1125
1126 Fun, isn't it? But we are only half way there. This is what
1127 @code{apropos} has to say about @code{j0}:
1128
1129 @smallexample
1130 (apropos "j0")
1131 @print{} (guile-user): j0 #<primitive-procedure j0>
1132 @end smallexample
1133
1134 As you can see, @code{j0} is contained in the root module, where all
1135 the other Guile primitives like @code{display}, etc live. In general,
1136 a primitive is put into whatever module is the @dfn{current module} at
1137 the time @code{scm_c_define_gsubr} is called.
1138
1139 A compiled module should have a specially named @dfn{module init
1140 function}. Guile knows about this special name and will call that
1141 function automatically after having linked in the shared library. For
1142 our example, we replace @code{init_math_bessel} with the following code in
1143 @file{bessel.c}:
1144
1145 @smallexample
1146 void
1147 init_math_bessel (void *unused)
1148 @{
1149 scm_c_define_gsubr ("j0", 1, 0, 0, j0_wrapper);
1150 scm_c_export ("j0", NULL);
1151 @}
1152
1153 void
1154 scm_init_math_bessel_module ()
1155 @{
1156 scm_c_define_module ("math bessel", init_math_bessel, NULL);
1157 @}
1158 @end smallexample
1159
1160 The general pattern for the name of a module init function is:
1161 @samp{scm_init_}, followed by the name of the module where the
1162 individual hierarchical components are concatenated with underscores,
1163 followed by @samp{_module}.
1164
1165 After @file{libbessel.so} has been rebuilt, we need to place the shared
1166 library into the right place.
1167
1168 Once the module has been correctly installed, it should be possible to
1169 use it like this:
1170
1171 @smallexample
1172 guile> (load-extension "./libbessel.so" "scm_init_math_bessel_module")
1173 guile> (use-modules (math bessel))
1174 guile> (j0 2)
1175 0.223890779141236
1176 guile> (apropos "j0")
1177 @print{} (math bessel): j0 #<primitive-procedure j0>
1178 @end smallexample
1179
1180 That's it!
1181
1182 @deffn {Scheme Procedure} load-extension lib init
1183 @deffnx {C Function} scm_load_extension (lib, init)
1184 Load and initialize the extension designated by LIB and INIT.
1185 When there is no pre-registered function for LIB/INIT, this is
1186 equivalent to
1187
1188 @lisp
1189 (dynamic-call INIT (dynamic-link LIB))
1190 @end lisp
1191
1192 When there is a pre-registered function, that function is called
1193 instead.
1194
1195 Normally, there is no pre-registered function. This option exists
1196 only for situations where dynamic linking is unavailable or unwanted.
1197 In that case, you would statically link your program with the desired
1198 library, and register its init function right after Guile has been
1199 initialized.
1200
1201 LIB should be a string denoting a shared library without any file type
1202 suffix such as ".so". The suffix is provided automatically. It
1203 should also not contain any directory components. Libraries that
1204 implement Guile Extensions should be put into the normal locations for
1205 shared libraries. We recommend to use the naming convention
1206 libguile-bla-blum for a extension related to a module `(bla blum)'.
1207
1208 The normal way for a extension to be used is to write a small Scheme
1209 file that defines a module, and to load the extension into this
1210 module. When the module is auto-loaded, the extension is loaded as
1211 well. For example,
1212
1213 @lisp
1214 (define-module (bla blum))
1215
1216 (load-extension "libguile-bla-blum" "bla_init_blum")
1217 @end lisp
1218 @end deffn
1219
1220
1221 @node Compiled Code Installation
1222 @subsubsection Compiled Code Installation
1223
1224 The simplest way to write a module using compiled C code is
1225
1226 @example
1227 (define-module (foo bar))
1228 (load-extension "foobar-c-code" "foo_bar_init")
1229 @end example
1230
1231 When loaded with @code{(use-modules (foo bar))}, the
1232 @code{load-extension} call looks for the @file{foobar-c-code.so} (etc)
1233 object file in the standard system locations, such as @file{/usr/lib}
1234 or @file{/usr/local/lib}.
1235
1236 If someone installs your module to a non-standard location then the
1237 object file won't be found. You can address this by inserting the
1238 install location in the @file{foo/bar.scm} file. This is convenient
1239 for the user and also guarantees the intended object is read, even if
1240 stray older or newer versions are in the loader's path.
1241
1242 The usual way to specify an install location is with a @code{prefix}
1243 at the configure stage, for instance @samp{./configure prefix=/opt}
1244 results in library files as say @file{/opt/lib/foobar-c-code.so}.
1245 When using Autoconf (@pxref{Top, , Introduction, autoconf, The GNU
1246 Autoconf Manual}), the library location is in a @code{libdir}
1247 variable. Its value is intended to be expanded by @command{make}, and
1248 can by substituted into a source file like @file{foo.scm.in}
1249
1250 @example
1251 (define-module (foo bar))
1252 (load-extension "XXlibdirXX/foobar-c-code" "foo_bar_init")
1253 @end example
1254
1255 @noindent
1256 with the following in a @file{Makefile}, using @command{sed}
1257 (@pxref{Top, , Introduction, sed, SED, A Stream Editor}),
1258
1259 @example
1260 foo.scm: foo.scm.in
1261 sed 's|XXlibdirXX|$(libdir)|' <foo.scm.in >foo.scm
1262 @end example
1263
1264 The actual pattern @code{XXlibdirXX} is arbitrary, it's only something
1265 which doesn't otherwise occur. If several modules need the value, it
1266 can be easier to create one @file{foo/config.scm} with a define of the
1267 @code{libdir} location, and use that as required.
1268
1269 @example
1270 (define-module (foo config))
1271 (define-public foo-config-libdir "XXlibdirXX"")
1272 @end example
1273
1274 Such a file might have other locations too, for instance a data
1275 directory for auxiliary files, or @code{localedir} if the module has
1276 its own @code{gettext} message catalogue
1277 (@pxref{Internationalization}).
1278
1279 When installing multiple C code objects, it can be convenient to put
1280 them in a subdirectory of @code{libdir}, thus giving for example
1281 @code{/usr/lib/foo/some-obj.so}. If the objects are only meant to be
1282 used through the module, then a subdirectory keeps them out of sight.
1283
1284 It will be noted all of the above requires that the Scheme code to be
1285 found in @code{%load-path} (@pxref{Build Config}). Presently it's
1286 left up to the system administrator or each user to augment that path
1287 when installing Guile modules in non-default locations. But having
1288 reached the Scheme code, that code should take care of hitting any of
1289 its own private files etc.
1290
1291 Presently there's no convention for having a Guile version number in
1292 module C code filenames or directories. This is primarily because
1293 there's no established principles for two versions of Guile to be
1294 installed under the same prefix (eg. two both under @file{/usr}).
1295 Assuming upward compatibility is maintained then this should be
1296 unnecessary, and if compatibility is not maintained then it's highly
1297 likely a package will need to be revisited anyway.
1298
1299 The present suggestion is that modules should assume when they're
1300 installed under a particular @code{prefix} that there's a single
1301 version of Guile there, and the @code{guile-config} at build time has
1302 the necessary information about it. C code or Scheme code might adapt
1303 itself accordingly (allowing for features not available in an older
1304 version for instance).
1305
1306
1307 @node Variables
1308 @subsection Variables
1309 @tpindex Variables
1310
1311 Each module has its own hash table, sometimes known as an @dfn{obarray},
1312 that maps the names defined in that module to their corresponding
1313 variable objects.
1314
1315 A variable is a box-like object that can hold any Scheme value. It is
1316 said to be @dfn{undefined} if its box holds a special Scheme value that
1317 denotes undefined-ness (which is different from all other Scheme values,
1318 including for example @code{#f}); otherwise the variable is
1319 @dfn{defined}.
1320
1321 On its own, a variable object is anonymous. A variable is said to be
1322 @dfn{bound} when it is associated with a name in some way, usually a
1323 symbol in a module obarray. When this happens, the relationship is
1324 mutual: the variable is bound to the name (in that module), and the name
1325 (in that module) is bound to the variable.
1326
1327 (That's the theory, anyway. In practice, defined-ness and bound-ness
1328 sometimes get confused, because Lisp and Scheme implementations have
1329 often conflated --- or deliberately drawn no distinction between --- a
1330 name that is unbound and a name that is bound to a variable whose value
1331 is undefined. We will try to be clear about the difference and explain
1332 any confusion where it is unavoidable.)
1333
1334 Variables do not have a read syntax. Most commonly they are created and
1335 bound implicitly by @code{define} expressions: a top-level @code{define}
1336 expression of the form
1337
1338 @lisp
1339 (define @var{name} @var{value})
1340 @end lisp
1341
1342 @noindent
1343 creates a variable with initial value @var{value} and binds it to the
1344 name @var{name} in the current module. But they can also be created
1345 dynamically by calling one of the constructor procedures
1346 @code{make-variable} and @code{make-undefined-variable}.
1347
1348 First-class variables are especially useful for interacting with the
1349 current module system (@pxref{The Guile module system}).
1350
1351 @deffn {Scheme Procedure} make-undefined-variable
1352 @deffnx {C Function} scm_make_undefined_variable ()
1353 Return a variable that is initially unbound.
1354 @end deffn
1355
1356 @deffn {Scheme Procedure} make-variable init
1357 @deffnx {C Function} scm_make_variable (init)
1358 Return a variable initialized to value @var{init}.
1359 @end deffn
1360
1361 @deffn {Scheme Procedure} variable-bound? var
1362 @deffnx {C Function} scm_variable_bound_p (var)
1363 Return @code{#t} iff @var{var} is bound to a value.
1364 Throws an error if @var{var} is not a variable object.
1365 @end deffn
1366
1367 @deffn {Scheme Procedure} variable-ref var
1368 @deffnx {C Function} scm_variable_ref (var)
1369 Dereference @var{var} and return its value.
1370 @var{var} must be a variable object; see @code{make-variable}
1371 and @code{make-undefined-variable}.
1372 @end deffn
1373
1374 @deffn {Scheme Procedure} variable-set! var val
1375 @deffnx {C Function} scm_variable_set_x (var, val)
1376 Set the value of the variable @var{var} to @var{val}.
1377 @var{var} must be a variable object, @var{val} can be any
1378 value. Return an unspecified value.
1379 @end deffn
1380
1381 @deffn {Scheme Procedure} variable? obj
1382 @deffnx {C Function} scm_variable_p (obj)
1383 Return @code{#t} iff @var{obj} is a variable object, else
1384 return @code{#f}.
1385 @end deffn
1386
1387
1388 @c Local Variables:
1389 @c TeX-master: "guile.texi"
1390 @c End: