96da7645a130450015ae2d33544ad04909ca6a53
[bpt/guile.git] / doc / ref / scheme-modules.texi
1 @page
2 @node Modules
3 @chapter Modules
4 @cindex modules
5
6 When programs become large, naming conflicts can occur when a function
7 or global variable defined in one file has the same name as a function
8 or global variable in another file. Even just a @emph{similarity}
9 between function names can cause hard-to-find bugs, since a programmer
10 might type the wrong function name.
11
12 The approach used to tackle this problem is called @emph{information
13 encapsulation}, which consists of packaging functional units into a
14 given name space that is clearly separated from other name spaces.
15 @cindex encapsulation
16 @cindex information encapsulation
17 @cindex name space
18
19 The language features that allow this are usually called @emph{the
20 module system} because programs are broken up into modules that are
21 compiled separately (or loaded separately in an interpreter).
22
23 Older languages, like C, have limited support for name space
24 manipulation and protection. In C a variable or function is public by
25 default, and can be made local to a module with the @code{static}
26 keyword. But you cannot reference public variables and functions from
27 another module with different names.
28
29 More advanced module systems have become a common feature in recently
30 designed languages: ML, Python, Perl, and Modula 3 all allow the
31 @emph{renaming} of objects from a foreign module, so they will not
32 clutter the global name space.
33 @cindex name space - private
34
35 In addition, Guile offers variables as first-class objects. They can
36 be used for interacting with the module system.
37
38 @menu
39 * provide and require:: The SLIB feature mechanism.
40 * Environments:: R5RS top-level environments.
41 * The Guile module system:: How Guile does it.
42 * Dynamic Libraries:: Loading libraries of compiled code at run time.
43 * Variables:: First-class variables.
44 @end menu
45
46 @node provide and require
47 @section provide and require
48
49 Aubrey Jaffer, mostly to support his portable Scheme library SLIB,
50 implemented a provide/require mechanism for many Scheme implementations.
51 Library files in SLIB @emph{provide} a feature, and when user programs
52 @emph{require} that feature, the library file is loaded in.
53
54 For example, the file @file{random.scm} in the SLIB package contains the
55 line
56
57 @smalllisp
58 (provide 'random)
59 @end smalllisp
60
61 so to use its procedures, a user would type
62
63 @smalllisp
64 (require 'random)
65 @end smalllisp
66
67 and they would magically become available, @emph{but still have the same
68 names!} So this method is nice, but not as good as a full-featured
69 module system.
70
71 When SLIB is used with Guile, provide and require can be used to access
72 its facilities.
73
74 @node Environments
75 @section Environments
76 @cindex environment
77
78 Scheme, as defined in R5RS, does @emph{not} have a full module system.
79 However it does define the concept of a top-level @dfn{environment}.
80 Such an environment maps identifiers (symbols) to Scheme objects such
81 as procedures and lists: @ref{About Closure}. In other words, it
82 implements a set of @dfn{bindings}.
83
84 Environments in R5RS can be passed as the second argument to
85 @code{eval} (@pxref{Fly Evaluation}). Three procedures are defined to
86 return environments: @code{scheme-report-environment},
87 @code{null-environment} and @code{interaction-environment} (@pxref{Fly
88 Evaluation}).
89
90 In addition, in Guile any module can be used as an R5RS environment,
91 i.e., passed as the second argument to @code{eval}.
92
93 Note: the following two procedures are available only when the
94 @code{(ice-9 r5rs)} module is loaded:
95
96 @smalllisp
97 (use-modules (ice-9 r5rs))
98 @end smalllisp
99
100 @deffn {Scheme Procedure} scheme-report-environment version
101 @deffnx {Scheme Procedure} null-environment version
102 @var{version} must be the exact integer `5', corresponding to revision
103 5 of the Scheme report (the Revised^5 Report on Scheme).
104 @code{scheme-report-environment} returns a specifier for an
105 environment that is empty except for all bindings defined in the
106 report that are either required or both optional and supported by the
107 implementation. @code{null-environment} returns a specifier for an
108 environment that is empty except for the (syntactic) bindings for all
109 syntactic keywords defined in the report that are either required or
110 both optional and supported by the implementation.
111
112 Currently Guile does not support values of @var{version} for other
113 revisions of the report.
114
115 The effect of assigning (through the use of @code{eval}) a variable
116 bound in a @code{scheme-report-environment} (for example @code{car})
117 is unspecified. Currently the environments specified by
118 @code{scheme-report-environment} are not immutable in Guile.
119 @end deffn
120
121 @node The Guile module system
122 @section The Guile module system
123
124 The Guile module system extends the concept of environments, discussed
125 in the previous section, with mechanisms to define, use and customise
126 sets of bindings.
127
128 In 1996 Tom Lord implemented a full-featured module system for Guile which
129 allows loading Scheme source files into a private name space. This system has
130 been in available since at least Guile version 1.1.
131
132 For Guile version 1.5.0 and later, the system has been improved to have better
133 integration from C code, more fine-grained user control over interfaces, and
134 documentation.
135
136 Although it is anticipated that the module system implementation will
137 change in the future, the Scheme programming interface described in this
138 manual should be considered stable. The C programming interface is
139 considered relatively stable, although at the time of this writing,
140 there is still some flux.
141
142 @menu
143 * General Information about Modules:: Guile module basics.
144 * Using Guile Modules:: How to use existing modules.
145 * Creating Guile Modules:: How to package your code into modules.
146 * Module System Quirks:: Strange things to be aware of.
147 * Included Guile Modules:: Which modules come with Guile?
148 * Accessing Modules from C:: How to work with modules with C code.
149 @end menu
150
151 @node General Information about Modules
152 @subsection General Information about Modules
153
154 A Guile module can be thought of as a collection of named procedures,
155 variables and macros. More precisely, it is a set of @dfn{bindings}
156 of symbols (names) to Scheme objects.
157
158 An environment is a mapping from identifiers (or symbols) to locations,
159 i.e., a set of bindings.
160 There are top-level environments and lexical environments.
161 The environment in which a lambda is executed is remembered as part of its
162 definition.
163
164 Within a module, all bindings are visible. Certain bindings
165 can be declared @dfn{public}, in which case they are added to the
166 module's so-called @dfn{export list}; this set of public bindings is
167 called the module's @dfn{public interface} (@pxref{Creating Guile
168 Modules}).
169
170 A client module @dfn{uses} a providing module's bindings by either
171 accessing the providing module's public interface, or by building a
172 custom interface (and then accessing that). In a custom interface, the
173 client module can @dfn{select} which bindings to access and can also
174 algorithmically @dfn{rename} bindings. In contrast, when using the
175 providing module's public interface, the entire export list is available
176 without renaming (@pxref{Using Guile Modules}).
177
178 To use a module, it must be found and loaded. All Guile modules have a
179 unique @dfn{module name}, which is a list of one or more symbols.
180 Examples are @code{(ice-9 popen)} or @code{(srfi srfi-11)}. When Guile
181 searches for the code of a module, it constructs the name of the file to
182 load by concatenating the name elements with slashes between the
183 elements and appending a number of file name extensions from the list
184 @code{%load-extensions} (@pxref{Loading}). The resulting file name is
185 then searched in all directories in the variable @code{%load-path}
186 (@pxref{Build Config}). For example, the @code{(ice-9 popen)} module
187 would result in the filename @code{ice-9/popen.scm} and searched in the
188 installation directories of Guile and in all other directories in the
189 load path.
190
191 @c FIXME::martin: Not sure about this, maybe someone knows better?
192 Every module has a so-called syntax transformer associated with it.
193 This is a procedure which performs all syntax transformation for the
194 time the module is read in and evaluated. When working with modules,
195 you can manipulate the current syntax transformer using the
196 @code{use-syntax} syntactic form or the @code{#:use-syntax} module
197 definition option (@pxref{Creating Guile Modules}).
198
199 Please note that there are some problems with the current module system
200 you should keep in mind (@pxref{Module System Quirks}). We hope to
201 address these eventually.
202
203
204 @node Using Guile Modules
205 @subsection Using Guile Modules
206
207 To use a Guile module is to access either its public interface or a
208 custom interface (@pxref{General Information about Modules}). Both
209 types of access are handled by the syntactic form @code{use-modules},
210 which accepts one or more interface specifications and, upon evaluation,
211 arranges for those interfaces to be available to the current module.
212 This process may include locating and loading code for a given module if
213 that code has not yet been loaded, following %load-path (@pxref{Build
214 Config}).
215
216 An @dfn{interface specification} has one of two forms. The first
217 variation is simply to name the module, in which case its public
218 interface is the one accessed. For example:
219
220 @smalllisp
221 (use-modules (ice-9 popen))
222 @end smalllisp
223
224 Here, the interface specification is @code{(ice-9 popen)}, and the
225 result is that the current module now has access to @code{open-pipe},
226 @code{close-pipe}, @code{open-input-pipe}, and so on (@pxref{Included
227 Guile Modules}).
228
229 Note in the previous example that if the current module had already
230 defined @code{open-pipe}, that definition would be overwritten by the
231 definition in @code{(ice-9 popen)}. For this reason (and others), there
232 is a second variation of interface specification that not only names a
233 module to be accessed, but also selects bindings from it and renames
234 them to suit the current module's needs. For example:
235
236 @smalllisp
237 (use-modules ((ice-9 popen)
238 :select ((open-pipe . pipe-open) close-pipe)
239 :renamer (symbol-prefix-proc 'unixy:)))
240 @end smalllisp
241
242 Here, the interface specification is more complex than before, and the
243 result is that a custom interface with only two bindings is created and
244 subsequently accessed by the current module. The mapping of old to new
245 names is as follows:
246
247 @c Use `smallexample' since `table' is ugly. --ttn
248 @smallexample
249 (ice-9 popen) sees: current module sees:
250 open-pipe unixy:pipe-open
251 close-pipe unixy:close-pipe
252 @end smallexample
253
254 This example also shows how to use the convenience procedure
255 @code{symbol-prefix-proc}.
256
257 You can also directly refer to bindings in a module by using the
258 @code{@@} syntax. For example, instead of using the
259 @code{use-modules} statement from above and writing
260 @code{unixy:pipe-open} to refer to the @code{pipe-open} from the
261 @code{(ice-9 popen)}, you could also write @code{(@@ (ice-9 popen)
262 open-pipe)}. Thus an alternative to the complete @code{use-modules}
263 statement would be
264
265 @smalllisp
266 (define unixy:pipe-open (@@ (ice-9 popen) open-pipe))
267 (define unixy:close-pipe (@@ (ice-9 popen) close-pipe))
268 @end smalllisp
269
270 There is also @code{@@@@}, which can be used like @code{@@}, but does
271 not check whether the variable that is being accessed is actually
272 exported. Thus, @code{@@@@} can be thought of as the impolite version
273 of @code{@@} and should only be used as a last resort or for
274 debugging, for example.
275
276 Note that just as with a @code{use-modules} statement, any module that
277 has not yet been loaded yet will be loaded when referenced by a
278 @code{@@} or @code{@@@@} form.
279
280 You can also use the @code{@@} and @code{@@@@} syntaxes as the target
281 of a @code{set!} when the binding refers to a variable.
282
283 @c begin (scm-doc-string "boot-9.scm" "symbol-prefix-proc")
284 @deffn {Scheme Procedure} symbol-prefix-proc prefix-sym
285 Return a procedure that prefixes its arg (a symbol) with
286 @var{prefix-sym}.
287 @c Insert gratuitous C++ slam here. --ttn
288 @end deffn
289
290 @c begin (scm-doc-string "boot-9.scm" "use-modules")
291 @deffn syntax use-modules spec @dots{}
292 Resolve each interface specification @var{spec} into an interface and
293 arrange for these to be accessible by the current module. The return
294 value is unspecified.
295
296 @var{spec} can be a list of symbols, in which case it names a module
297 whose public interface is found and used.
298
299 @var{spec} can also be of the form:
300
301 @smalllisp
302 (MODULE-NAME [:select SELECTION] [:renamer RENAMER])
303 @end smalllisp
304
305 in which case a custom interface is newly created and used.
306 @var{module-name} is a list of symbols, as above; @var{selection} is a
307 list of selection-specs; and @var{renamer} is a procedure that takes a
308 symbol and returns its new name. A selection-spec is either a symbol or
309 a pair of symbols @code{(ORIG . SEEN)}, where @var{orig} is the name in
310 the used module and @var{seen} is the name in the using module. Note
311 that @var{seen} is also passed through @var{renamer}.
312
313 The @code{:select} and @code{:renamer} clauses are optional. If both are
314 omitted, the returned interface has no bindings. If the @code{:select}
315 clause is omitted, @var{renamer} operates on the used module's public
316 interface.
317
318 Signal error if module name is not resolvable.
319 @end deffn
320
321
322 @c FIXME::martin: Is this correct, and is there more to say?
323 @c FIXME::martin: Define term and concept `system transformer' somewhere.
324
325 @deffn syntax use-syntax module-name
326 Load the module @code{module-name} and use its system
327 transformer as the system transformer for the currently defined module,
328 as well as installing it as the current system transformer.
329 @end deffn
330
331 @deffn syntax @@ module-name binding-name
332 Refer to the binding named @var{binding-name} in module
333 @var{module-name}. The binding must have been exported by the module.
334 @end deffn
335
336 @deffn syntax @@@@ module-name binding-name
337 Refer to the binding named @var{binding-name} in module
338 @var{module-name}. The binding must not have been exported by the
339 module. This syntax is only intended for debugging purposes or as a
340 last resort.
341 @end deffn
342
343 @node Creating Guile Modules
344 @subsection Creating Guile Modules
345
346 When you want to create your own modules, you have to take the following
347 steps:
348
349 @itemize @bullet
350 @item
351 Create a Scheme source file and add all variables and procedures you wish
352 to export, or which are required by the exported procedures.
353
354 @item
355 Add a @code{define-module} form at the beginning.
356
357 @item
358 Export all bindings which should be in the public interface, either
359 by using @code{define-public} or @code{export} (both documented below).
360 @end itemize
361
362 @c begin (scm-doc-string "boot-9.scm" "define-module")
363 @deffn syntax define-module module-name [options @dots{}]
364 @var{module-name} is of the form @code{(hierarchy file)}. One
365 example of this is
366
367 @smalllisp
368 (define-module (ice-9 popen))
369 @end smalllisp
370
371 @code{define-module} makes this module available to Guile programs under
372 the given @var{module-name}.
373
374 The @var{options} are keyword/value pairs which specify more about the
375 defined module. The recognized options and their meaning is shown in
376 the following table.
377
378 @c fixme: Should we use "#:" or ":"?
379
380 @table @code
381 @item #:use-module @var{interface-specification}
382 Equivalent to a @code{(use-modules @var{interface-specification})}
383 (@pxref{Using Guile Modules}).
384
385 @item #:use-syntax @var{module}
386 Use @var{module} when loading the currently defined module, and install
387 it as the syntax transformer.
388
389 @item #:autoload @var{module} @var{symbol}
390 Load @var{module} whenever @var{symbol} is accessed.
391
392 @item #:export @var{list}
393 Export all identifiers in @var{list}, which must be a list of symbols.
394 This is equivalent to @code{(export @var{list})} in the module body.
395
396 @item #:no-backtrace
397 Tell Guile not to record information for procedure backtraces when
398 executing the procedures in this module.
399
400 @item #:pure
401 Create a @dfn{pure} module, that is a module which does not contain any
402 of the standard procedure bindings except for the syntax forms. This is
403 useful if you want to create @dfn{safe} modules, that is modules which
404 do not know anything about dangerous procedures.
405 @end table
406
407 @end deffn
408 @c end
409
410 @deffn syntax export variable @dots{}
411 Add all @var{variable}s (which must be symbols) to the list of exported
412 bindings of the current module.
413 @end deffn
414
415 @c begin (scm-doc-string "boot-9.scm" "define-public")
416 @deffn syntax define-public @dots{}
417 Equivalent to @code{(begin (define foo ...) (export foo))}.
418 @end deffn
419 @c end
420
421
422 @node Module System Quirks
423 @subsection Module System Quirks
424
425 Although the programming interfaces are relatively stable, the Guile
426 module system itself is still evolving. Here are some situations where
427 usage surpasses design.
428
429 @itemize @bullet
430
431 @item
432 When using a module which exports a macro definition, the other module
433 must export all bindings the macro expansion uses, too, because the
434 expanded code would otherwise not be able to see these definitions and
435 issue a ``variable unbound'' error, or worse, would use another binding
436 which might be present in the scope of the expansion.
437
438 @item
439 When two or more used modules export bindings with the same names, the
440 last accessed module wins, and the exported binding of that last module
441 will silently be used. This might lead to hard-to-find errors because
442 wrong procedures or variables are used. To avoid this kind of
443 @dfn{name-clash} situation, use a custom interface specification
444 (@pxref{Using Guile Modules}). (We include this entry for the possible
445 benefit of users of Guile versions previous to 1.5.0, when custom
446 interfaces were added to the module system.)
447
448 @item
449 [Add other quirks here.]
450
451 @end itemize
452
453
454 @node Included Guile Modules
455 @subsection Included Guile Modules
456
457 @c FIXME::martin: Review me!
458
459 Some modules are included in the Guile distribution; here are references
460 to the entries in this manual which describe them in more detail:
461
462 @table @strong
463 @item boot-9
464 boot-9 is Guile's initialization module, and it is always loaded when
465 Guile starts up.
466
467 @item (ice-9 debug)
468 Mikael Djurfeldt's source-level debugging support for Guile
469 (@pxref{Debugging Features}).
470
471 @item (ice-9 threads)
472 Guile's support for multi threaded execution (@pxref{Scheduling}).
473
474 @item (ice-9 rdelim)
475 Line- and character-delimited input (@pxref{Line/Delimited}).
476
477 @item (ice-9 rw)
478 Block string input/output (@pxref{Block Reading and Writing}).
479
480 @item (ice-9 documentation)
481 Online documentation (REFFIXME).
482
483 @item (srfi srfi-1)
484 A library providing a lot of useful list and pair processing
485 procedures (@pxref{SRFI-1}).
486
487 @item (srfi srfi-2)
488 Support for @code{and-let*} (@pxref{SRFI-2}).
489
490 @item (srfi srfi-4)
491 Support for homogeneous numeric vectors (@pxref{SRFI-4}).
492
493 @item (srfi srfi-6)
494 Support for some additional string port procedures (@pxref{SRFI-6}).
495
496 @item (srfi srfi-8)
497 Multiple-value handling with @code{receive} (@pxref{SRFI-8}).
498
499 @item (srfi srfi-9)
500 Record definition with @code{define-record-type} (@pxref{SRFI-9}).
501
502 @item (srfi srfi-10)
503 Read hash extension @code{#,()} (@pxref{SRFI-10}).
504
505 @item (srfi srfi-11)
506 Multiple-value handling with @code{let-values} and @code{let-values*}
507 (@pxref{SRFI-11}).
508
509 @item (srfi srfi-13)
510 String library (@pxref{SRFI-13}).
511
512 @item (srfi srfi-14)
513 Character-set library (@pxref{SRFI-14}).
514
515 @item (srfi srfi-17)
516 Getter-with-setter support (@pxref{SRFI-17}).
517
518 @item (srfi srfi-26)
519 Convenient syntax for partial application (@pxref{SRFI-26})
520
521 @item (ice-9 slib)
522 This module contains hooks for using Aubrey Jaffer's portable Scheme
523 library SLIB from Guile (@pxref{SLIB}).
524
525 @c FIXME::martin: This module is not in the distribution. Remove it
526 @c from here?
527 @item (ice-9 jacal)
528 This module contains hooks for using Aubrey Jaffer's symbolic math
529 package Jacal from Guile (@pxref{JACAL}).
530 @end table
531
532
533 @node Accessing Modules from C
534 @subsection Accessing Modules from C
535
536 The last sections have described how modules are used in Scheme code,
537 which is the recommended way of creating and accessing modules. You
538 can also work with modules from C, but it is more cumbersome.
539
540 The following procedures are available.
541
542 @deftypefn {C Procedure} SCM scm_current_module ()
543 Return the module that is the @emph{current module}.
544 @end deftypefn
545
546 @deftypefn {C Procedure} SCM scm_set_current_module (SCM @var{module})
547 Set the current module to @var{module} and return the previous current
548 module.
549 @end deftypefn
550
551 @deftypefn {C Procedure} SCM scm_c_call_with_current_module (SCM @var{module}, SCM (*@var{func})(void *), void *@var{data})
552 Call @var{func} and make @var{module} the current module during the
553 call. The argument @var{data} is passed to @var{func}. The return
554 value of @code{scm_c_call_with_current_module} is the return value of
555 @var{func}.
556 @end deftypefn
557
558 @deftypefn {C Procedure} SCM scm_c_lookup (const char *@var{name})
559 Return the variable bound to the symbol indicated by @var{name} in the
560 current module. If there is no such binding or the symbol is not
561 bound to a variable, signal an error.
562 @end deftypefn
563
564 @deftypefn {C Procedure} SCM scm_lookup (SCM @var{name})
565 Like @code{scm_c_lookup}, but the symbol is specified directly.
566 @end deftypefn
567
568 @deftypefn {C Procedure} SCM scm_c_module_lookup (SCM @var{module}, const char *@var{name})
569 @deftypefnx {C Procedure} SCM scm_module_lookup (SCM @var{module}, SCM @var{name})
570 Like @code{scm_c_lookup} and @code{scm_lookup}, but the specified
571 module is used instead of the current one.
572 @end deftypefn
573
574 @deftypefn {C Procedure} SCM scm_c_define (const char *@var{name}, SCM @var{val})
575 Bind the symbol indicated by @var{name} to a variable in the current
576 module and set that variable to @var{val}. When @var{name} is already
577 bound to a variable, use that. Else create a new variable.
578 @end deftypefn
579
580 @deftypefn {C Procedure} SCM scm_define (SCM @var{name}, SCM @var{val})
581 Like @code{scm_c_define}, but the symbol is specified directly.
582 @end deftypefn
583
584 @deftypefn {C Procedure} SCM scm_c_module_define (SCM @var{module}, const char *@var{name}, SCM @var{val})
585 @deftypefnx {C Procedure} SCM scm_module_define (SCM @var{module}, SCM @var{name}, SCM @var{val})
586 Like @code{scm_c_define} and @code{scm_define}, but the specified
587 module is used instead of the current one.
588 @end deftypefn
589
590 @deftypefn {C Procedure} SCM scm_module_reverse_lookup (SCM @var{module}, SCM @var{variable})
591 Find the symbol that is bound to @var{variable} in @var{module}. When no such binding is found, return @var{#f}.
592 @end deftypefn
593
594 @deftypefn {C Procedure} SCM scm_c_define_module (const char *@var{name}, void (*@var{init})(void *), void *@var{data})
595 Define a new module named @var{name} and make it current while
596 @var{init} is called, passing it @var{data}. Return the module.
597
598 The parameter @var{name} is a string with the symbols that make up
599 the module name, separated by spaces. For example, @samp{"foo bar"} names
600 the module @samp{(foo bar)}.
601
602 When there already exists a module named @var{name}, it is used
603 unchanged, otherwise, an empty module is created.
604 @end deftypefn
605
606 @deftypefn {C Procedure} SCM scm_c_resolve_module (const char *@var{name})
607 Find the module name @var{name} and return it. When it has not
608 already been defined, try to auto-load it. When it can't be found
609 that way either, create an empty module. The name is interpreted as
610 for @code{scm_c_define_module}.
611 @end deftypefn
612
613 @deftypefn {C Procedure} SCM scm_resolve_module (SCM @var{name})
614 Like @code{scm_c_resolve_module}, but the name is given as a real list
615 of symbols.
616 @end deftypefn
617
618 @deftypefn {C Procedure} SCM scm_c_use_module (const char *@var{name})
619 Add the module named @var{name} to the uses list of the current
620 module, as with @code{(use-modules @var{name})}. The name is
621 interpreted as for @code{scm_c_define_module}.
622 @end deftypefn
623
624 @deftypefn {C Procedure} SCM scm_c_export (const char *@var{name}, ...)
625 Add the bindings designated by @var{name}, ... to the public interface
626 of the current module. The list of names is terminated by
627 @code{NULL}.
628 @end deftypefn
629
630 @node Dynamic Libraries
631 @section Dynamic Libraries
632
633 Most modern Unices have something called @dfn{shared libraries}. This
634 ordinarily means that they have the capability to share the executable
635 image of a library between several running programs to save memory and
636 disk space. But generally, shared libraries give a lot of additional
637 flexibility compared to the traditional static libraries. In fact,
638 calling them `dynamic' libraries is as correct as calling them `shared'.
639
640 Shared libraries really give you a lot of flexibility in addition to the
641 memory and disk space savings. When you link a program against a shared
642 library, that library is not closely incorporated into the final
643 executable. Instead, the executable of your program only contains
644 enough information to find the needed shared libraries when the program
645 is actually run. Only then, when the program is starting, is the final
646 step of the linking process performed. This means that you need not
647 recompile all programs when you install a new, only slightly modified
648 version of a shared library. The programs will pick up the changes
649 automatically the next time they are run.
650
651 Now, when all the necessary machinery is there to perform part of the
652 linking at run-time, why not take the next step and allow the programmer
653 to explicitly take advantage of it from within his program? Of course,
654 many operating systems that support shared libraries do just that, and
655 chances are that Guile will allow you to access this feature from within
656 your Scheme programs. As you might have guessed already, this feature
657 is called @dfn{dynamic linking}.@footnote{Some people also refer to the
658 final linking stage at program startup as `dynamic linking', so if you
659 want to make yourself perfectly clear, it is probably best to use the
660 more technical term @dfn{dlopening}, as suggested by Gordon Matzigkeit
661 in his libtool documentation.}
662
663 As with many aspects of Guile, there is a low-level way to access the
664 dynamic linking apparatus, and a more high-level interface that
665 integrates dynamically linked libraries into the module system.
666
667 @menu
668 * Low level dynamic linking::
669 * Compiled Code Modules::
670 * Dynamic Linking and Compiled Code Modules::
671 @end menu
672
673 @node Low level dynamic linking
674 @subsection Low level dynamic linking
675
676 When using the low level procedures to do your dynamic linking, you have
677 complete control over which library is loaded when and what gets done
678 with it.
679
680 @deffn {Scheme Procedure} dynamic-link library
681 @deffnx {C Function} scm_dynamic_link (library)
682 Find the shared library denoted by @var{library} (a string) and link it
683 into the running Guile application. When everything works out, return a
684 Scheme object suitable for representing the linked object file.
685 Otherwise an error is thrown. How object files are searched is system
686 dependent.
687
688 Normally, @var{library} is just the name of some shared library file
689 that will be searched for in the places where shared libraries usually
690 reside, such as in @file{/usr/lib} and @file{/usr/local/lib}.
691 @end deffn
692
693 @deffn {Scheme Procedure} dynamic-object? obj
694 @deffnx {C Function} scm_dynamic_object_p (obj)
695 Return @code{#t} if @var{obj} is a dynamic library handle, or @code{#f}
696 otherwise.
697 @end deffn
698
699 @deffn {Scheme Procedure} dynamic-unlink dobj
700 @deffnx {C Function} scm_dynamic_unlink (dobj)
701 Unlink the indicated object file from the application. The
702 argument @var{dobj} must have been obtained by a call to
703 @code{dynamic-link}. After @code{dynamic-unlink} has been
704 called on @var{dobj}, its content is no longer accessible.
705 @end deffn
706
707 @deffn {Scheme Procedure} dynamic-func name dobj
708 @deffnx {C Function} scm_dynamic_func (name, dobj)
709 Search the dynamic object @var{dobj} for the C function
710 indicated by the string @var{name} and return some Scheme
711 handle that can later be used with @code{dynamic-call} to
712 actually call the function.
713
714 Regardless whether your C compiler prepends an underscore @samp{_} to
715 the global names in a program, you should @strong{not} include this
716 underscore in @var{function}. Guile knows whether the underscore is
717 needed or not and will add it when necessary.
718 @end deffn
719
720 @deffn {Scheme Procedure} dynamic-call func dobj
721 @deffnx {C Function} scm_dynamic_call (func, dobj)
722 Call the C function indicated by @var{func} and @var{dobj}.
723 The function is passed no arguments and its return value is
724 ignored. When @var{function} is something returned by
725 @code{dynamic-func}, call that function and ignore @var{dobj}.
726 When @var{func} is a string , look it up in @var{dynobj}; this
727 is equivalent to
728 @smallexample
729 (dynamic-call (dynamic-func @var{func} @var{dobj}) #f)
730 @end smallexample
731
732 Interrupts are deferred while the C function is executing (with
733 @code{SCM_DEFER_INTS}/@code{SCM_ALLOW_INTS}).
734 @end deffn
735
736 @deffn {Scheme Procedure} dynamic-args-call func dobj args
737 @deffnx {C Function} scm_dynamic_args_call (func, dobj, args)
738 Call the C function indicated by @var{func} and @var{dobj},
739 just like @code{dynamic-call}, but pass it some arguments and
740 return its return value. The C function is expected to take
741 two arguments and return an @code{int}, just like @code{main}:
742 @smallexample
743 int c_func (int argc, char **argv);
744 @end smallexample
745
746 The parameter @var{args} must be a list of strings and is
747 converted into an array of @code{char *}. The array is passed
748 in @var{argv} and its size in @var{argc}. The return value is
749 converted to a Scheme number and returned from the call to
750 @code{dynamic-args-call}.
751 @end deffn
752
753 When dynamic linking is disabled or not supported on your system,
754 the above functions throw errors, but they are still available.
755
756 Here is a small example that works on GNU/Linux:
757
758 @smallexample
759 (define libc-obj (dynamic-link "libc.so"))
760 libc-obj
761 @result{} #<dynamic-object "libc.so">
762 (dynamic-args-call 'rand libc-obj '())
763 @result{} 269167349
764 (dynamic-unlink libc-obj)
765 libc-obj
766 @result{} #<dynamic-object "libc.so" (unlinked)>
767 @end smallexample
768
769 As you can see, after calling @code{dynamic-unlink} on a dynamically
770 linked library, it is marked as @samp{(unlinked)} and you are no longer
771 able to use it with @code{dynamic-call}, etc. Whether the library is
772 really removed from you program is system-dependent and will generally
773 not happen when some other parts of your program still use it. In the
774 example above, @code{libc} is almost certainly not removed from your
775 program because it is badly needed by almost everything.
776
777 The functions to call a function from a dynamically linked library,
778 @code{dynamic-call} and @code{dynamic-args-call}, are not very powerful.
779 They are mostly intended to be used for calling specially written
780 initialization functions that will then add new primitives to Guile.
781 For example, we do not expect that you will dynamically link
782 @file{libX11} with @code{dynamic-link} and then construct a beautiful
783 graphical user interface just by using @code{dynamic-call} and
784 @code{dynamic-args-call}. Instead, the usual way would be to write a
785 special Guile<->X11 glue library that has intimate knowledge about both
786 Guile and X11 and does whatever is necessary to make them inter-operate
787 smoothly. This glue library could then be dynamically linked into a
788 vanilla Guile interpreter and activated by calling its initialization
789 function. That function would add all the new types and primitives to
790 the Guile interpreter that it has to offer.
791
792 From this setup the next logical step is to integrate these glue
793 libraries into the module system of Guile so that you can load new
794 primitives into a running system just as you can load new Scheme code.
795
796 There is, however, another possibility to get a more thorough access to
797 the functions contained in a dynamically linked library. Anthony Green
798 has written @file{libffi}, a library that implements a @dfn{foreign
799 function interface} for a number of different platforms. With it, you
800 can extend the Spartan functionality of @code{dynamic-call} and
801 @code{dynamic-args-call} considerably. There is glue code available in
802 the Guile contrib archive to make @file{libffi} accessible from Guile.
803
804 @node Compiled Code Modules
805 @subsection Putting Compiled Code into Modules
806
807 The new primitives that you add to Guile with
808 @code{scm_c_define_gsubr} (@pxref{Primitive Procedures}) or with any
809 of the other mechanisms are placed into the @code{(guile-user)} module
810 by default. However, it is also possible to put new primitives into
811 other modules.
812
813 The mechanism for doing so is not very well thought out and is likely to
814 change when the module system of Guile itself is revised, but it is
815 simple and useful enough to document it as it stands.
816
817 What @code{scm_c_define_gsubr} and the functions used by the snarfer
818 really do is to add the new primitives to whatever module is the
819 @emph{current module} when they are called. This is analogous to the
820 way Scheme code is put into modules: the @code{define-module} expression
821 at the top of a Scheme source file creates a new module and makes it the
822 current module while the rest of the file is evaluated. The
823 @code{define} expressions in that file then add their new definitions to
824 this current module.
825
826 Therefore, all we need to do is to make sure that the right module is
827 current when calling @code{scm_c_define_gsubr} for our new primitives.
828
829 @node Dynamic Linking and Compiled Code Modules
830 @subsection Dynamic Linking and Compiled Code Modules
831
832 The most interesting application of dynamically linked libraries is
833 probably to use them for providing @emph{compiled code modules} to
834 Scheme programs. As much fun as programming in Scheme is, every now and
835 then comes the need to write some low-level C stuff to make Scheme even
836 more fun.
837
838 Not only can you put these new primitives into their own module (see the
839 previous section), you can even put them into a shared library that is
840 only then linked to your running Guile image when it is actually
841 needed.
842
843 An example will hopefully make everything clear. Suppose we want to
844 make the Bessel functions of the C library available to Scheme in the
845 module @samp{(math bessel)}. First we need to write the appropriate
846 glue code to convert the arguments and return values of the functions
847 from Scheme to C and back. Additionally, we need a function that will
848 add them to the set of Guile primitives. Because this is just an
849 example, we will only implement this for the @code{j0} function.
850
851 @c FIXME::martin: Change all gh_ references to their scm_ equivalents.
852
853 @smallexample
854 #include <math.h>
855 #include <libguile.h>
856
857 SCM
858 j0_wrapper (SCM x)
859 @{
860 return scm_double2num (j0 (scm_num2dbl (x, "j0")));
861 @}
862
863 void
864 init_math_bessel ()
865 @{
866 scm_c_define_gsubr ("j0", 1, 0, 0, j0_wrapper);
867 @}
868 @end smallexample
869
870 We can already try to bring this into action by manually calling the low
871 level functions for performing dynamic linking. The C source file needs
872 to be compiled into a shared library. Here is how to do it on
873 GNU/Linux, please refer to the @code{libtool} documentation for how to
874 create dynamically linkable libraries portably.
875
876 @smallexample
877 gcc -shared -o libbessel.so -fPIC bessel.c
878 @end smallexample
879
880 Now fire up Guile:
881
882 @smalllisp
883 (define bessel-lib (dynamic-link "./libbessel.so"))
884 (dynamic-call "init_math_bessel" bessel-lib)
885 (j0 2)
886 @result{} 0.223890779141236
887 @end smalllisp
888
889 The filename @file{./libbessel.so} should be pointing to the shared
890 library produced with the @code{gcc} command above, of course. The
891 second line of the Guile interaction will call the
892 @code{init_math_bessel} function which in turn will register the C
893 function @code{j0_wrapper} with the Guile interpreter under the name
894 @code{j0}. This function becomes immediately available and we can call
895 it from Scheme.
896
897 Fun, isn't it? But we are only half way there. This is what
898 @code{apropos} has to say about @code{j0}:
899
900 @smallexample
901 (apropos "j0")
902 @print{} (guile-user): j0 #<primitive-procedure j0>
903 @end smallexample
904
905 As you can see, @code{j0} is contained in the root module, where all
906 the other Guile primitives like @code{display}, etc live. In general,
907 a primitive is put into whatever module is the @dfn{current module} at
908 the time @code{scm_c_define_gsubr} is called.
909
910 A compiled module should have a specially named @dfn{module init
911 function}. Guile knows about this special name and will call that
912 function automatically after having linked in the shared library. For
913 our example, we replace @code{init_math_bessel} with the following code in
914 @file{bessel.c}:
915
916 @smallexample
917 void
918 init_math_bessel (void *unused)
919 @{
920 scm_c_define_gsubr ("j0", 1, 0, 0, j0_wrapper);
921 scm_c_export ("j0", NULL);
922 @}
923
924 void
925 scm_init_math_bessel_module ()
926 @{
927 scm_c_define_module ("math bessel", init_math_bessel, NULL);
928 @}
929 @end smallexample
930
931 The general pattern for the name of a module init function is:
932 @samp{scm_init_}, followed by the name of the module where the
933 individual hierarchical components are concatenated with underscores,
934 followed by @samp{_module}.
935
936 After @file{libbessel.so} has been rebuilt, we need to place the shared
937 library into the right place.
938
939 Once the module has been correctly installed, it should be possible to
940 use it like this:
941
942 @smallexample
943 guile> (load-extension "./libbessel.so" "scm_init_math_bessel_module")
944 guile> (use-modules (math bessel))
945 guile> (j0 2)
946 0.223890779141236
947 guile> (apropos "j0")
948 @print{} (math bessel): j0 #<primitive-procedure j0>
949 @end smallexample
950
951 That's it!
952
953 @node Variables
954 @section Variables
955 @tpindex Variables
956
957 Each module has its own hash table, sometimes known as an @dfn{obarray},
958 that maps the names defined in that module to their corresponding
959 variable objects.
960
961 A variable is a box-like object that can hold any Scheme value. It is
962 said to be @dfn{undefined} if its box holds a special Scheme value that
963 denotes undefined-ness (which is different from all other Scheme values,
964 including for example @code{#f}); otherwise the variable is
965 @dfn{defined}.
966
967 On its own, a variable object is anonymous. A variable is said to be
968 @dfn{bound} when it is associated with a name in some way, usually a
969 symbol in a module obarray. When this happens, the relationship is
970 mutual: the variable is bound to the name (in that module), and the name
971 (in that module) is bound to the variable.
972
973 (That's the theory, anyway. In practice, defined-ness and bound-ness
974 sometimes get confused, because Lisp and Scheme implementations have
975 often conflated --- or deliberately drawn no distinction between --- a
976 name that is unbound and a name that is bound to a variable whose value
977 is undefined. We will try to be clear about the difference and explain
978 any confusion where it is unavoidable.)
979
980 Variables do not have a read syntax. Most commonly they are created and
981 bound implicitly by @code{define} expressions: a top-level @code{define}
982 expression of the form
983
984 @lisp
985 (define @var{name} @var{value})
986 @end lisp
987
988 @noindent
989 creates a variable with initial value @var{value} and binds it to the
990 name @var{name} in the current module. But they can also be created
991 dynamically by calling one of the constructor procedures
992 @code{make-variable} and @code{make-undefined-variable}.
993
994 First-class variables are especially useful for interacting with the
995 current module system (@pxref{The Guile module system}).
996
997 @deffn {Scheme Procedure} make-undefined-variable
998 @deffnx {C Function} scm_make_undefined_variable ()
999 Return a variable that is initially unbound.
1000 @end deffn
1001
1002 @deffn {Scheme Procedure} make-variable init
1003 @deffnx {C Function} scm_make_variable (init)
1004 Return a variable initialized to value @var{init}.
1005 @end deffn
1006
1007 @deffn {Scheme Procedure} variable-bound? var
1008 @deffnx {C Function} scm_variable_bound_p (var)
1009 Return @code{#t} iff @var{var} is bound to a value.
1010 Throws an error if @var{var} is not a variable object.
1011 @end deffn
1012
1013 @deffn {Scheme Procedure} variable-ref var
1014 @deffnx {C Function} scm_variable_ref (var)
1015 Dereference @var{var} and return its value.
1016 @var{var} must be a variable object; see @code{make-variable}
1017 and @code{make-undefined-variable}.
1018 @end deffn
1019
1020 @deffn {Scheme Procedure} variable-set! var val
1021 @deffnx {C Function} scm_variable_set_x (var, val)
1022 Set the value of the variable @var{var} to @var{val}.
1023 @var{var} must be a variable object, @var{val} can be any
1024 value. Return an unspecified value.
1025 @end deffn
1026
1027 @deffn {Scheme Procedure} variable? obj
1028 @deffnx {C Function} scm_variable_p (obj)
1029 Return @code{#t} iff @var{obj} is a variable object, else
1030 return @code{#f}.
1031 @end deffn
1032
1033
1034 @c Local Variables:
1035 @c TeX-master: "guile.texi"
1036 @c End: