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