Commit | Line | Data |
---|---|---|
07d83abe MV |
1 | @c -*-texinfo-*- |
2 | @c This is part of the GNU Guile Reference Manual. | |
d12f974b | 3 | @c Copyright (C) 1996, 1997, 2000, 2001, 2002, 2003, 2004, 2007, 2008, 2009, 2010 |
07d83abe MV |
4 | @c Free Software Foundation, Inc. |
5 | @c See the file guile.texi for copying conditions. | |
6 | ||
07d83abe MV |
7 | @node Modules |
8 | @section Modules | |
9 | @cindex modules | |
10 | ||
11 | When programs become large, naming conflicts can occur when a function | |
12 | or global variable defined in one file has the same name as a function | |
13 | or global variable in another file. Even just a @emph{similarity} | |
14 | between function names can cause hard-to-find bugs, since a programmer | |
15 | might type the wrong function name. | |
16 | ||
17 | The approach used to tackle this problem is called @emph{information | |
18 | encapsulation}, which consists of packaging functional units into a | |
19 | given name space that is clearly separated from other name spaces. | |
20 | @cindex encapsulation | |
21 | @cindex information encapsulation | |
22 | @cindex name space | |
23 | ||
24 | The language features that allow this are usually called @emph{the | |
25 | module system} because programs are broken up into modules that are | |
26 | compiled separately (or loaded separately in an interpreter). | |
27 | ||
28 | Older languages, like C, have limited support for name space | |
29 | manipulation and protection. In C a variable or function is public by | |
30 | default, and can be made local to a module with the @code{static} | |
31 | keyword. But you cannot reference public variables and functions from | |
32 | another module with different names. | |
33 | ||
34 | More advanced module systems have become a common feature in recently | |
35 | designed languages: ML, Python, Perl, and Modula 3 all allow the | |
36 | @emph{renaming} of objects from a foreign module, so they will not | |
37 | clutter the global name space. | |
38 | @cindex name space - private | |
39 | ||
40 | In addition, Guile offers variables as first-class objects. They can | |
41 | be used for interacting with the module system. | |
42 | ||
07d83abe MV |
43 | @menu |
44 | * General Information about Modules:: Guile module basics. | |
45 | * Using Guile Modules:: How to use existing modules. | |
46 | * Creating Guile Modules:: How to package your code into modules. | |
cdf1ad3b | 47 | * Module System Reflection:: Accessing module objects at run-time. |
07d83abe | 48 | * Included Guile Modules:: Which modules come with Guile? |
dca14012 | 49 | * R6RS Version References:: Using version numbers with modules. |
71194485 | 50 | * R6RS Libraries:: The library and import forms. |
07d83abe | 51 | * Accessing Modules from C:: How to work with modules with C code. |
726b8ba3 AW |
52 | * Variables:: First-class variables. |
53 | * provide and require:: The SLIB feature mechanism. | |
54 | * Environments:: R5RS top-level environments. | |
07d83abe MV |
55 | @end menu |
56 | ||
57 | @node General Information about Modules | |
726b8ba3 | 58 | @subsection General Information about Modules |
07d83abe MV |
59 | |
60 | A Guile module can be thought of as a collection of named procedures, | |
61 | variables and macros. More precisely, it is a set of @dfn{bindings} | |
62 | of symbols (names) to Scheme objects. | |
63 | ||
64 | An environment is a mapping from identifiers (or symbols) to locations, | |
65 | i.e., a set of bindings. | |
66 | There are top-level environments and lexical environments. | |
67 | The environment in which a lambda is executed is remembered as part of its | |
68 | definition. | |
69 | ||
70 | Within a module, all bindings are visible. Certain bindings | |
71 | can be declared @dfn{public}, in which case they are added to the | |
72 | module's so-called @dfn{export list}; this set of public bindings is | |
73 | called the module's @dfn{public interface} (@pxref{Creating Guile | |
74 | Modules}). | |
75 | ||
76 | A client module @dfn{uses} a providing module's bindings by either | |
77 | accessing the providing module's public interface, or by building a | |
78 | custom interface (and then accessing that). In a custom interface, the | |
79 | client module can @dfn{select} which bindings to access and can also | |
80 | algorithmically @dfn{rename} bindings. In contrast, when using the | |
81 | providing module's public interface, the entire export list is available | |
82 | without renaming (@pxref{Using Guile Modules}). | |
83 | ||
84 | To use a module, it must be found and loaded. All Guile modules have a | |
85 | unique @dfn{module name}, which is a list of one or more symbols. | |
86 | Examples are @code{(ice-9 popen)} or @code{(srfi srfi-11)}. When Guile | |
87 | searches for the code of a module, it constructs the name of the file to | |
88 | load by concatenating the name elements with slashes between the | |
89 | elements and appending a number of file name extensions from the list | |
90 | @code{%load-extensions} (@pxref{Loading}). The resulting file name is | |
91 | then searched in all directories in the variable @code{%load-path} | |
92 | (@pxref{Build Config}). For example, the @code{(ice-9 popen)} module | |
93 | would result in the filename @code{ice-9/popen.scm} and searched in the | |
94 | installation directories of Guile and in all other directories in the | |
95 | load path. | |
96 | ||
dca14012 JG |
97 | A slightly different search mechanism is used when a client module |
98 | specifies a version reference as part of a request to load a module | |
99 | (@pxref{R6RS Version References}). Instead of searching the directories | |
100 | in the load path for a single filename, Guile uses the elements of the | |
101 | version reference to locate matching, numbered subdirectories of a | |
102 | constructed base path. For example, a request for the | |
103 | @code{(rnrs base)} module with version reference @code{(6)} would cause | |
104 | Guile to discover the @code{rnrs/6} subdirectory (if it exists in any of | |
105 | the directories in the load path) and search its contents for the | |
106 | filename @code{base.scm}. | |
107 | ||
108 | When multiple modules are found that match a version reference, Guile | |
109 | sorts these modules by version number, followed by the length of their | |
110 | version specifications, in order to choose a ``best'' match. | |
111 | ||
07d83abe MV |
112 | @c FIXME::martin: Not sure about this, maybe someone knows better? |
113 | Every module has a so-called syntax transformer associated with it. | |
114 | This is a procedure which performs all syntax transformation for the | |
115 | time the module is read in and evaluated. When working with modules, | |
116 | you can manipulate the current syntax transformer using the | |
117 | @code{use-syntax} syntactic form or the @code{#:use-syntax} module | |
118 | definition option (@pxref{Creating Guile Modules}). | |
119 | ||
07d83abe MV |
120 | |
121 | @node Using Guile Modules | |
726b8ba3 | 122 | @subsection Using Guile Modules |
07d83abe MV |
123 | |
124 | To use a Guile module is to access either its public interface or a | |
125 | custom interface (@pxref{General Information about Modules}). Both | |
126 | types of access are handled by the syntactic form @code{use-modules}, | |
127 | which accepts one or more interface specifications and, upon evaluation, | |
128 | arranges for those interfaces to be available to the current module. | |
129 | This process may include locating and loading code for a given module if | |
5c132e68 | 130 | that code has not yet been loaded, following @code{%load-path} (@pxref{Build |
07d83abe MV |
131 | Config}). |
132 | ||
133 | An @dfn{interface specification} has one of two forms. The first | |
134 | variation is simply to name the module, in which case its public | |
135 | interface is the one accessed. For example: | |
136 | ||
aba0dff5 | 137 | @lisp |
07d83abe | 138 | (use-modules (ice-9 popen)) |
aba0dff5 | 139 | @end lisp |
07d83abe MV |
140 | |
141 | Here, the interface specification is @code{(ice-9 popen)}, and the | |
142 | result is that the current module now has access to @code{open-pipe}, | |
143 | @code{close-pipe}, @code{open-input-pipe}, and so on (@pxref{Included | |
144 | Guile Modules}). | |
145 | ||
146 | Note in the previous example that if the current module had already | |
147 | defined @code{open-pipe}, that definition would be overwritten by the | |
148 | definition in @code{(ice-9 popen)}. For this reason (and others), there | |
149 | is a second variation of interface specification that not only names a | |
150 | module to be accessed, but also selects bindings from it and renames | |
151 | them to suit the current module's needs. For example: | |
152 | ||
46bb559d | 153 | @cindex binding renamer |
aba0dff5 | 154 | @lisp |
07d83abe | 155 | (use-modules ((ice-9 popen) |
8d9cb14e NJ |
156 | #:select ((open-pipe . pipe-open) close-pipe) |
157 | #:renamer (symbol-prefix-proc 'unixy:))) | |
aba0dff5 | 158 | @end lisp |
07d83abe MV |
159 | |
160 | Here, the interface specification is more complex than before, and the | |
161 | result is that a custom interface with only two bindings is created and | |
162 | subsequently accessed by the current module. The mapping of old to new | |
163 | names is as follows: | |
164 | ||
165 | @c Use `smallexample' since `table' is ugly. --ttn | |
166 | @smallexample | |
167 | (ice-9 popen) sees: current module sees: | |
168 | open-pipe unixy:pipe-open | |
169 | close-pipe unixy:close-pipe | |
170 | @end smallexample | |
171 | ||
172 | This example also shows how to use the convenience procedure | |
173 | @code{symbol-prefix-proc}. | |
174 | ||
175 | You can also directly refer to bindings in a module by using the | |
176 | @code{@@} syntax. For example, instead of using the | |
177 | @code{use-modules} statement from above and writing | |
178 | @code{unixy:pipe-open} to refer to the @code{pipe-open} from the | |
179 | @code{(ice-9 popen)}, you could also write @code{(@@ (ice-9 popen) | |
180 | open-pipe)}. Thus an alternative to the complete @code{use-modules} | |
181 | statement would be | |
182 | ||
aba0dff5 | 183 | @lisp |
07d83abe MV |
184 | (define unixy:pipe-open (@@ (ice-9 popen) open-pipe)) |
185 | (define unixy:close-pipe (@@ (ice-9 popen) close-pipe)) | |
aba0dff5 | 186 | @end lisp |
07d83abe MV |
187 | |
188 | There is also @code{@@@@}, which can be used like @code{@@}, but does | |
189 | not check whether the variable that is being accessed is actually | |
190 | exported. Thus, @code{@@@@} can be thought of as the impolite version | |
191 | of @code{@@} and should only be used as a last resort or for | |
192 | debugging, for example. | |
193 | ||
194 | Note that just as with a @code{use-modules} statement, any module that | |
195 | has not yet been loaded yet will be loaded when referenced by a | |
196 | @code{@@} or @code{@@@@} form. | |
197 | ||
198 | You can also use the @code{@@} and @code{@@@@} syntaxes as the target | |
199 | of a @code{set!} when the binding refers to a variable. | |
200 | ||
201 | @c begin (scm-doc-string "boot-9.scm" "symbol-prefix-proc") | |
202 | @deffn {Scheme Procedure} symbol-prefix-proc prefix-sym | |
203 | Return a procedure that prefixes its arg (a symbol) with | |
204 | @var{prefix-sym}. | |
205 | @c Insert gratuitous C++ slam here. --ttn | |
206 | @end deffn | |
207 | ||
208 | @c begin (scm-doc-string "boot-9.scm" "use-modules") | |
209 | @deffn syntax use-modules spec @dots{} | |
210 | Resolve each interface specification @var{spec} into an interface and | |
211 | arrange for these to be accessible by the current module. The return | |
212 | value is unspecified. | |
213 | ||
214 | @var{spec} can be a list of symbols, in which case it names a module | |
215 | whose public interface is found and used. | |
216 | ||
217 | @var{spec} can also be of the form: | |
218 | ||
46bb559d | 219 | @cindex binding renamer |
aba0dff5 | 220 | @lisp |
07d83abe | 221 | (MODULE-NAME [:select SELECTION] [:renamer RENAMER]) |
aba0dff5 | 222 | @end lisp |
07d83abe MV |
223 | |
224 | in which case a custom interface is newly created and used. | |
225 | @var{module-name} is a list of symbols, as above; @var{selection} is a | |
226 | list of selection-specs; and @var{renamer} is a procedure that takes a | |
227 | symbol and returns its new name. A selection-spec is either a symbol or | |
228 | a pair of symbols @code{(ORIG . SEEN)}, where @var{orig} is the name in | |
229 | the used module and @var{seen} is the name in the using module. Note | |
230 | that @var{seen} is also passed through @var{renamer}. | |
231 | ||
232 | The @code{:select} and @code{:renamer} clauses are optional. If both are | |
233 | omitted, the returned interface has no bindings. If the @code{:select} | |
234 | clause is omitted, @var{renamer} operates on the used module's public | |
235 | interface. | |
236 | ||
dca14012 JG |
237 | In addition to the above, @var{spec} can also include a @code{:version} |
238 | clause, of the form: | |
239 | ||
240 | @lisp | |
241 | :version VERSION-SPEC | |
242 | @end lisp | |
243 | ||
244 | where @var{version-spec} is an R6RS-compatible version reference. The | |
245 | presence of this clause changes Guile's search behavior as described in | |
246 | the section on module name resolution | |
247 | (@pxref{General Information about Modules}). An error will be signaled | |
248 | in the case in which a module with the same name has already been | |
249 | loaded, if that module specifies a version and that version is not | |
250 | compatible with @var{version-spec}. | |
251 | ||
07d83abe MV |
252 | Signal error if module name is not resolvable. |
253 | @end deffn | |
254 | ||
255 | ||
256 | @c FIXME::martin: Is this correct, and is there more to say? | |
c8779dde | 257 | @c FIXME::martin: Define term and concept `syntax transformer' somewhere. |
07d83abe MV |
258 | |
259 | @deffn syntax use-syntax module-name | |
c8779dde LC |
260 | Load the module @code{module-name} and use its syntax |
261 | transformer as the syntax transformer for the currently defined module, | |
262 | as well as installing it as the current syntax transformer. | |
07d83abe MV |
263 | @end deffn |
264 | ||
265 | @deffn syntax @@ module-name binding-name | |
266 | Refer to the binding named @var{binding-name} in module | |
267 | @var{module-name}. The binding must have been exported by the module. | |
268 | @end deffn | |
269 | ||
270 | @deffn syntax @@@@ module-name binding-name | |
271 | Refer to the binding named @var{binding-name} in module | |
272 | @var{module-name}. The binding must not have been exported by the | |
273 | module. This syntax is only intended for debugging purposes or as a | |
274 | last resort. | |
275 | @end deffn | |
276 | ||
277 | @node Creating Guile Modules | |
726b8ba3 | 278 | @subsection Creating Guile Modules |
07d83abe MV |
279 | |
280 | When you want to create your own modules, you have to take the following | |
281 | steps: | |
282 | ||
283 | @itemize @bullet | |
284 | @item | |
285 | Create a Scheme source file and add all variables and procedures you wish | |
286 | to export, or which are required by the exported procedures. | |
287 | ||
288 | @item | |
289 | Add a @code{define-module} form at the beginning. | |
290 | ||
291 | @item | |
292 | Export all bindings which should be in the public interface, either | |
293 | by using @code{define-public} or @code{export} (both documented below). | |
294 | @end itemize | |
295 | ||
296 | @c begin (scm-doc-string "boot-9.scm" "define-module") | |
297 | @deffn syntax define-module module-name [options @dots{}] | |
298 | @var{module-name} is of the form @code{(hierarchy file)}. One | |
299 | example of this is | |
300 | ||
aba0dff5 | 301 | @lisp |
07d83abe | 302 | (define-module (ice-9 popen)) |
aba0dff5 | 303 | @end lisp |
07d83abe MV |
304 | |
305 | @code{define-module} makes this module available to Guile programs under | |
306 | the given @var{module-name}. | |
307 | ||
308 | The @var{options} are keyword/value pairs which specify more about the | |
309 | defined module. The recognized options and their meaning is shown in | |
310 | the following table. | |
311 | ||
312 | @c fixme: Should we use "#:" or ":"? | |
313 | ||
314 | @table @code | |
315 | @item #:use-module @var{interface-specification} | |
316 | Equivalent to a @code{(use-modules @var{interface-specification})} | |
317 | (@pxref{Using Guile Modules}). | |
318 | ||
319 | @item #:use-syntax @var{module} | |
320 | Use @var{module} when loading the currently defined module, and install | |
321 | it as the syntax transformer. | |
322 | ||
950f97ac | 323 | @item #:autoload @var{module} @var{symbol-list} |
65f1345f | 324 | @cindex autoload |
950f97ac KR |
325 | Load @var{module} when any of @var{symbol-list} are accessed. For |
326 | example, | |
327 | ||
328 | @example | |
329 | (define-module (my mod) | |
330 | #:autoload (srfi srfi-1) (partition delete-duplicates)) | |
331 | ... | |
332 | (if something | |
333 | (set! foo (delete-duplicates ...))) | |
334 | @end example | |
335 | ||
a236df75 | 336 | When a module is autoloaded, all its bindings become available. |
950f97ac KR |
337 | @var{symbol-list} is just those that will first trigger the load. |
338 | ||
339 | An autoload is a good way to put off loading a big module until it's | |
340 | really needed, for instance for faster startup or if it will only be | |
341 | needed in certain circumstances. | |
342 | ||
343 | @code{@@} can do a similar thing (@pxref{Using Guile Modules}), but in | |
344 | that case an @code{@@} form must be written every time a binding from | |
345 | the module is used. | |
07d83abe MV |
346 | |
347 | @item #:export @var{list} | |
65f1345f | 348 | @cindex export |
78c22f5e JG |
349 | Export all identifiers in @var{list} which must be a list of symbols |
350 | or pairs of symbols. This is equivalent to @code{(export @var{list})} | |
351 | in the module body. | |
07d83abe | 352 | |
46bb559d KR |
353 | @item #:re-export @var{list} |
354 | @cindex re-export | |
355 | Re-export all identifiers in @var{list} which must be a list of | |
78c22f5e JG |
356 | symbols or pairs of symbols. The symbols in @var{list} must be |
357 | imported by the current module from other modules. This is equivalent | |
358 | to @code{re-export} below. | |
46bb559d KR |
359 | |
360 | @item #:export-syntax @var{list} | |
361 | @cindex export-syntax | |
78c22f5e JG |
362 | Export all identifiers in @var{list} which must be a list of symbols |
363 | or pairs of symbols. The identifiers in @var{list} must refer to | |
364 | macros (@pxref{Macros}) defined in the current module. This is | |
365 | equivalent to @code{(export-syntax @var{list})} in the module body. | |
46bb559d KR |
366 | |
367 | @item #:re-export-syntax @var{list} | |
368 | @cindex re-export-syntax | |
369 | Re-export all identifiers in @var{list} which must be a list of | |
78c22f5e JG |
370 | symbols or pairs of symbols. The symbols in @var{list} must refer to |
371 | macros imported by the current module from other modules. This is | |
372 | equivalent to @code{(re-export-syntax @var{list})} in the module body. | |
46bb559d KR |
373 | |
374 | @item #:replace @var{list} | |
375 | @cindex replace | |
376 | @cindex replacing binding | |
377 | @cindex overriding binding | |
378 | @cindex duplicate binding | |
78c22f5e JG |
379 | Export all identifiers in @var{list} (a list of symbols or pairs of |
380 | symbols) and mark them as @dfn{replacing bindings}. In the module | |
381 | user's name space, this will have the effect of replacing any binding | |
382 | with the same name that is not also ``replacing''. Normally a | |
383 | replacement results in an ``override'' warning message, | |
384 | @code{#:replace} avoids that. | |
46bb559d | 385 | |
d68a81e0 AW |
386 | In general, a module that exports a binding for which the @code{(guile)} |
387 | module already has a definition should use @code{#:replace} instead of | |
388 | @code{#:export}. @code{#:replace}, in a sense, lets Guile know that the | |
389 | module @emph{purposefully} replaces a core binding. It is important to | |
390 | note, however, that this binding replacement is confined to the name | |
391 | space of the module user. In other words, the value of the core binding | |
392 | in question remains unchanged for other modules. | |
393 | ||
394 | Note that although it is often a good idea for the replaced binding to | |
395 | remain compatible with a binding in @code{(guile)}, to avoid surprising | |
396 | the user, sometimes the bindings will be incompatible. For example, | |
397 | SRFI-19 exports its own version of @code{current-time} (@pxref{SRFI-19 | |
398 | Time}) which is not compatible with the core @code{current-time} | |
399 | function (@pxref{Time}). Guile assumes that a user importing a module | |
400 | knows what she is doing, and uses @code{#:replace} for this binding | |
401 | rather than @code{#:export}. | |
e724644d MV |
402 | |
403 | The @code{#:duplicates} (see below) provides fine-grain control about | |
404 | duplicate binding handling on the module-user side. | |
46bb559d | 405 | |
dca14012 JG |
406 | @item #:version @var{list} |
407 | @cindex module version | |
408 | Specify a version for the module in the form of @var{list}, a list of | |
409 | zero or more exact, nonnegative integers. The corresponding | |
410 | @code{#:version} option in the @code{use-modules} form allows callers | |
411 | to restrict the value of this option in various ways. | |
412 | ||
46bb559d KR |
413 | @item #:duplicates @var{list} |
414 | @cindex duplicate binding handlers | |
415 | @cindex duplicate binding | |
416 | @cindex overriding binding | |
417 | Tell Guile to handle duplicate bindings for the bindings imported by | |
418 | the current module according to the policy defined by @var{list}, a | |
419 | list of symbols. @var{list} must contain symbols representing a | |
420 | duplicate binding handling policy chosen among the following: | |
421 | ||
422 | @table @code | |
423 | @item check | |
424 | Raises an error when a binding is imported from more than one place. | |
425 | @item warn | |
426 | Issue a warning when a binding is imported from more than one place | |
427 | and leave the responsibility of actually handling the duplication to | |
428 | the next duplicate binding handler. | |
429 | @item replace | |
430 | When a new binding is imported that has the same name as a previously | |
431 | imported binding, then do the following: | |
432 | ||
433 | @enumerate | |
434 | @item | |
435 | @cindex replacing binding | |
436 | If the old binding was said to be @dfn{replacing} (via the | |
437 | @code{#:replace} option above) and the new binding is not replacing, | |
438 | the keep the old binding. | |
439 | @item | |
440 | If the old binding was not said to be replacing and the new binding is | |
441 | replacing, then replace the old binding with the new one. | |
442 | @item | |
443 | If neither the old nor the new binding is replacing, then keep the old | |
444 | one. | |
445 | @end enumerate | |
446 | ||
447 | @item warn-override-core | |
448 | Issue a warning when a core binding is being overwritten and actually | |
449 | override the core binding with the new one. | |
450 | @item first | |
451 | In case of duplicate bindings, the firstly imported binding is always | |
452 | the one which is kept. | |
453 | @item last | |
454 | In case of duplicate bindings, the lastly imported binding is always | |
455 | the one which is kept. | |
456 | @item noop | |
457 | In case of duplicate bindings, leave the responsibility to the next | |
458 | duplicate handler. | |
459 | @end table | |
460 | ||
461 | If @var{list} contains more than one symbol, then the duplicate | |
462 | binding handlers which appear first will be used first when resolving | |
463 | a duplicate binding situation. As mentioned above, some resolution | |
464 | policies may explicitly leave the responsibility of handling the | |
465 | duplication to the next handler in @var{list}. | |
466 | ||
467 | @findex default-duplicate-binding-handler | |
468 | The default duplicate binding resolution policy is given by the | |
469 | @code{default-duplicate-binding-handler} procedure, and is | |
470 | ||
aba0dff5 | 471 | @lisp |
46bb559d | 472 | (replace warn-override-core warn last) |
aba0dff5 | 473 | @end lisp |
46bb559d | 474 | |
07d83abe | 475 | @item #:no-backtrace |
65f1345f | 476 | @cindex no backtrace |
07d83abe MV |
477 | Tell Guile not to record information for procedure backtraces when |
478 | executing the procedures in this module. | |
479 | ||
480 | @item #:pure | |
65f1345f | 481 | @cindex pure module |
07d83abe MV |
482 | Create a @dfn{pure} module, that is a module which does not contain any |
483 | of the standard procedure bindings except for the syntax forms. This is | |
484 | useful if you want to create @dfn{safe} modules, that is modules which | |
485 | do not know anything about dangerous procedures. | |
486 | @end table | |
487 | ||
488 | @end deffn | |
489 | @c end | |
490 | ||
491 | @deffn syntax export variable @dots{} | |
78c22f5e JG |
492 | Add all @var{variable}s (which must be symbols or pairs of symbols) to |
493 | the list of exported bindings of the current module. If @var{variable} | |
494 | is a pair, its @code{car} gives the name of the variable as seen by the | |
495 | current module and its @code{cdr} specifies a name for the binding in | |
496 | the current module's public interface. | |
07d83abe MV |
497 | @end deffn |
498 | ||
499 | @c begin (scm-doc-string "boot-9.scm" "define-public") | |
500 | @deffn syntax define-public @dots{} | |
501 | Equivalent to @code{(begin (define foo ...) (export foo))}. | |
502 | @end deffn | |
503 | @c end | |
504 | ||
46bb559d | 505 | @deffn syntax re-export variable @dots{} |
78c22f5e JG |
506 | Add all @var{variable}s (which must be symbols or pairs of symbols) to |
507 | the list of re-exported bindings of the current module. Pairs of | |
508 | symbols are handled as in @code{export}. Re-exported bindings must be | |
509 | imported by the current module from some other module. | |
46bb559d KR |
510 | @end deffn |
511 | ||
cdf1ad3b | 512 | @node Module System Reflection |
726b8ba3 | 513 | @subsection Module System Reflection |
cdf1ad3b MV |
514 | |
515 | The previous sections have described a declarative view of the module | |
516 | system. You can also work with it programmatically by accessing and | |
517 | modifying various parts of the Scheme objects that Guile uses to | |
518 | implement the module system. | |
519 | ||
520 | At any time, there is a @dfn{current module}. This module is the one | |
521 | where a top-level @code{define} and similar syntax will add new | |
522 | bindings. You can find other module objects with @code{resolve-module}, | |
523 | for example. | |
524 | ||
525 | These module objects can be used as the second argument to @code{eval}. | |
526 | ||
527 | @deffn {Scheme Procedure} current-module | |
528 | Return the current module object. | |
529 | @end deffn | |
530 | ||
531 | @deffn {Scheme Procedure} set-current-module module | |
532 | Set the current module to @var{module} and return | |
533 | the previous current module. | |
534 | @end deffn | |
535 | ||
b1f57ea4 LC |
536 | @deffn {Scheme Procedure} save-module-excursion thunk |
537 | Call @var{thunk} within a @code{dynamic-wind} such that the module that | |
538 | is current at invocation time is restored when @var{thunk}'s dynamic | |
539 | extent is left (@pxref{Dynamic Wind}). | |
540 | ||
541 | More precisely, if @var{thunk} escapes non-locally, the current module | |
542 | (at the time of escape) is saved, and the original current module (at | |
543 | the time @var{thunk}'s dynamic extent was last entered) is restored. If | |
544 | @var{thunk}'s dynamic extent is re-entered, then the current module is | |
545 | saved, and the previously saved inner module is set current again. | |
546 | @end deffn | |
547 | ||
cdf1ad3b MV |
548 | @deffn {Scheme Procedure} resolve-module name |
549 | Find the module named @var{name} and return it. When it has not already | |
550 | been defined, try to auto-load it. When it can't be found that way | |
551 | either, create an empty module. The name is a list of symbols. | |
552 | @end deffn | |
553 | ||
554 | @deffn {Scheme Procedure} resolve-interface name | |
555 | Find the module named @var{name} as with @code{resolve-module} and | |
556 | return its interface. The interface of a module is also a module | |
557 | object, but it contains only the exported bindings. | |
558 | @end deffn | |
559 | ||
560 | @deffn {Scheme Procedure} module-use! module interface | |
561 | Add @var{interface} to the front of the use-list of @var{module}. Both | |
562 | arguments should be module objects, and @var{interface} should very | |
563 | likely be a module returned by @code{resolve-interface}. | |
564 | @end deffn | |
07d83abe | 565 | |
cdab9fc6 AW |
566 | @deffn {Scheme Procedure} reload-module module |
567 | Revisit the source file that corresponds to @var{module}. Raises an | |
568 | error if no source file is associated with the given module. | |
569 | @end deffn | |
570 | ||
07d83abe MV |
571 | |
572 | @node Included Guile Modules | |
726b8ba3 | 573 | @subsection Included Guile Modules |
07d83abe MV |
574 | |
575 | @c FIXME::martin: Review me! | |
576 | ||
577 | Some modules are included in the Guile distribution; here are references | |
578 | to the entries in this manual which describe them in more detail: | |
579 | ||
580 | @table @strong | |
581 | @item boot-9 | |
582 | boot-9 is Guile's initialization module, and it is always loaded when | |
583 | Guile starts up. | |
584 | ||
54a3c992 KR |
585 | @item (ice-9 expect) |
586 | Actions based on matching input from a port (@pxref{Expect}). | |
587 | ||
588 | @item (ice-9 format) | |
589 | Formatted output in the style of Common Lisp (@pxref{Formatted | |
590 | Output}). | |
591 | ||
592 | @item (ice-9 ftw) | |
593 | File tree walker (@pxref{File Tree Walk}). | |
594 | ||
595 | @item (ice-9 getopt-long) | |
596 | Command line option processing (@pxref{getopt-long}). | |
597 | ||
598 | @item (ice-9 history) | |
599 | Refer to previous interactive expressions (@pxref{Value History}). | |
600 | ||
601 | @item (ice-9 popen) | |
602 | Pipes to and from child processes (@pxref{Pipes}). | |
603 | ||
604 | @item (ice-9 pretty-print) | |
605 | Nicely formatted output of Scheme expressions and objects | |
606 | (@pxref{Pretty Printing}). | |
607 | ||
608 | @item (ice-9 q) | |
609 | First-in first-out queues (@pxref{Queues}). | |
610 | ||
07d83abe MV |
611 | @item (ice-9 rdelim) |
612 | Line- and character-delimited input (@pxref{Line/Delimited}). | |
613 | ||
54a3c992 KR |
614 | @item (ice-9 readline) |
615 | @code{readline} interactive command line editing (@pxref{Readline | |
616 | Support}). | |
617 | ||
618 | @item (ice-9 receive) | |
619 | Multiple-value handling with @code{receive} (@pxref{Multiple Values}). | |
620 | ||
621 | @item (ice-9 regex) | |
622 | Regular expression matching (@pxref{Regular Expressions}). | |
623 | ||
07d83abe MV |
624 | @item (ice-9 rw) |
625 | Block string input/output (@pxref{Block Reading and Writing}). | |
626 | ||
54a3c992 KR |
627 | @item (ice-9 streams) |
628 | Sequence of values calculated on-demand (@pxref{Streams}). | |
629 | ||
630 | @item (ice-9 syncase) | |
631 | R5RS @code{syntax-rules} macro system (@pxref{Syntax Rules}). | |
632 | ||
aca55ba9 KR |
633 | @item (ice-9 threads) |
634 | Guile's support for multi threaded execution (@pxref{Scheduling}). | |
635 | ||
07d83abe MV |
636 | @item (ice-9 documentation) |
637 | Online documentation (REFFIXME). | |
638 | ||
639 | @item (srfi srfi-1) | |
640 | A library providing a lot of useful list and pair processing | |
641 | procedures (@pxref{SRFI-1}). | |
642 | ||
643 | @item (srfi srfi-2) | |
644 | Support for @code{and-let*} (@pxref{SRFI-2}). | |
645 | ||
646 | @item (srfi srfi-4) | |
647 | Support for homogeneous numeric vectors (@pxref{SRFI-4}). | |
648 | ||
649 | @item (srfi srfi-6) | |
650 | Support for some additional string port procedures (@pxref{SRFI-6}). | |
651 | ||
652 | @item (srfi srfi-8) | |
653 | Multiple-value handling with @code{receive} (@pxref{SRFI-8}). | |
654 | ||
655 | @item (srfi srfi-9) | |
656 | Record definition with @code{define-record-type} (@pxref{SRFI-9}). | |
657 | ||
658 | @item (srfi srfi-10) | |
659 | Read hash extension @code{#,()} (@pxref{SRFI-10}). | |
660 | ||
661 | @item (srfi srfi-11) | |
cdc4f3db | 662 | Multiple-value handling with @code{let-values} and @code{let*-values} |
07d83abe MV |
663 | (@pxref{SRFI-11}). |
664 | ||
665 | @item (srfi srfi-13) | |
666 | String library (@pxref{SRFI-13}). | |
667 | ||
668 | @item (srfi srfi-14) | |
669 | Character-set library (@pxref{SRFI-14}). | |
670 | ||
e376f9e5 KR |
671 | @item (srfi srfi-16) |
672 | @code{case-lambda} procedures of variable arity (@pxref{SRFI-16}). | |
673 | ||
07d83abe MV |
674 | @item (srfi srfi-17) |
675 | Getter-with-setter support (@pxref{SRFI-17}). | |
676 | ||
e376f9e5 KR |
677 | @item (srfi srfi-19) |
678 | Time/Date library (@pxref{SRFI-19}). | |
679 | ||
07d83abe MV |
680 | @item (srfi srfi-26) |
681 | Convenient syntax for partial application (@pxref{SRFI-26}) | |
682 | ||
e376f9e5 KR |
683 | @item (srfi srfi-31) |
684 | @code{rec} convenient recursive expressions (@pxref{SRFI-31}) | |
685 | ||
07d83abe MV |
686 | @item (ice-9 slib) |
687 | This module contains hooks for using Aubrey Jaffer's portable Scheme | |
688 | library SLIB from Guile (@pxref{SLIB}). | |
07d83abe MV |
689 | @end table |
690 | ||
691 | ||
dca14012 | 692 | @node R6RS Version References |
726b8ba3 | 693 | @subsection R6RS Version References |
dca14012 JG |
694 | |
695 | Guile's module system includes support for locating modules based on | |
696 | a declared version specifier of the same form as the one described in | |
697 | R6RS (@pxref{Library form, R6RS Library Form,, r6rs, The Revised^6 | |
698 | Report on the Algorithmic Language Scheme}). By using the | |
699 | @code{#:version} keyword in a @code{define-module} form, a module may | |
700 | specify a version as a list of zero or more exact, nonnegative integers. | |
701 | ||
702 | This version can then be used to locate the module during the module | |
703 | search process. Client modules and callers of the @code{use-modules} | |
704 | function may specify constraints on the versions of target modules by | |
705 | providing a @dfn{version reference}, which has one of the following | |
706 | forms: | |
707 | ||
708 | @lisp | |
709 | (@var{sub-version-reference} ...) | |
710 | (and @var{version-reference} ...) | |
711 | (or @var{version-reference} ...) | |
712 | (not @var{version-reference}) | |
713 | @end lisp | |
714 | ||
715 | in which @var{sub-version-reference} is in turn one of: | |
716 | ||
717 | @lisp | |
718 | (@var{sub-version}) | |
719 | (>= @var{sub-version}) | |
720 | (<= @var{sub-version}) | |
721 | (and @var{sub-version-reference} ...) | |
722 | (or @var{sub-version-reference} ...) | |
723 | (not @var{sub-version-reference}) | |
724 | @end lisp | |
725 | ||
726 | in which @var{sub-version} is an exact, nonnegative integer as above. A | |
727 | version reference matches a declared module version if each element of | |
728 | the version reference matches a corresponding element of the module | |
729 | version, according to the following rules: | |
730 | ||
731 | @itemize @bullet | |
732 | @item | |
733 | The @code{and} sub-form matches a version or version element if every | |
734 | element in the tail of the sub-form matches the specified version or | |
735 | version element. | |
736 | ||
737 | @item | |
738 | The @code{or} sub-form matches a version or version element if any | |
739 | element in the tail of the sub-form matches the specified version or | |
740 | version element. | |
741 | ||
742 | @item | |
743 | The @code{not} sub-form matches a version or version element if the tail | |
744 | of the sub-form does not match the version or version element. | |
745 | ||
746 | @item | |
747 | The @code{>=} sub-form matches a version element if the element is | |
748 | greater than or equal to the @var{sub-version} in the tail of the | |
749 | sub-form. | |
750 | ||
751 | @item | |
752 | The @code{<=} sub-form matches a version element if the version is less | |
753 | than or equal to the @var{sub-version} in the tail of the sub-form. | |
754 | ||
755 | @item | |
756 | A @var{sub-version} matches a version element if one is @var{eqv?} to | |
757 | the other. | |
758 | @end itemize | |
759 | ||
760 | For example, a module declared as: | |
761 | ||
762 | @lisp | |
763 | (define-module (mylib mymodule) #:version (1 2 0)) | |
764 | @end lisp | |
765 | ||
766 | would be successfully loaded by any of the following @code{use-modules} | |
767 | expressions: | |
768 | ||
769 | @lisp | |
770 | (use-modules ((mylib mymodule) #:version (1 2 (>= 0)))) | |
771 | (use-modules ((mylib mymodule) #:version (or (1 2 0) (1 2 1)))) | |
772 | (use-modules ((mylib mymodule) #:version ((and (>= 1) (not 2)) 2 0))) | |
773 | @end lisp | |
774 | ||
775 | ||
71194485 JG |
776 | @node R6RS Libraries |
777 | @subsection R6RS Libraries | |
778 | ||
779 | In addition to the API described in the previous sections, you also | |
43d6eb75 | 780 | have the option to create modules using the portable @code{library} form |
71194485 JG |
781 | described in R6RS (@pxref{Library form, R6RS Library Form,, r6rs, The |
782 | Revised^6 Report on the Algorithmic Language Scheme}), and to import | |
783 | libraries created in this format by other programmers. Guile's R6RS | |
43d6eb75 JG |
784 | library implementation takes advantage of the flexibility built into the |
785 | module system by expanding the R6RS library form into a corresponding | |
786 | Guile @code{define-module} form that specifies equivalent import and | |
787 | export requirements and includes the same body expressions. The library | |
788 | expression: | |
71194485 JG |
789 | |
790 | @lisp | |
791 | (library (mylib (1 2)) | |
792 | (import (otherlib (3))) | |
793 | (export mybinding)) | |
794 | @end lisp | |
795 | ||
796 | is equivalent to the module definition: | |
797 | ||
798 | @lisp | |
799 | (define-module (mylib) | |
800 | #:version (1 2) | |
801 | #:use-module ((otherlib) #:version (3)) | |
802 | #:export (mybinding)) | |
803 | @end lisp | |
804 | ||
805 | Central to the mechanics of R6RS libraries is the concept of import | |
43d6eb75 | 806 | and export @dfn{levels}, which control the visibility of bindings at |
71194485 JG |
807 | various phases of a library's lifecycle --- macros necessary to |
808 | expand forms in the library's body need to be available at expand | |
809 | time; variables used in the body of a procedure exported by the | |
810 | library must be available at runtime. R6RS specifies the optional | |
811 | @code{for} sub-form of an @emph{import set} specification (see below) | |
812 | as a mechanism by which a library author can indicate that a | |
813 | particular library import should take place at a particular phase | |
814 | with respect to the lifecycle of the importing library. | |
815 | ||
43d6eb75 JG |
816 | Guile's libraries implementation uses a technique called |
817 | @dfn{implicit phasing} (first described by Abdulaziz Ghuloum and R. | |
818 | Kent Dybvig), which allows the expander and compiler to automatically | |
819 | determine the necessary visibility of a binding imported from another | |
820 | library. As such, the @code{for} sub-form described below is ignored by | |
821 | Guile (but may be required by Schemes in which phasing is explicit). | |
71194485 | 822 | |
43d6eb75 | 823 | @deffn {Scheme Syntax} library name (export export-spec ...) (import import-spec ...) body ... |
71194485 JG |
824 | Defines a new library with the specified name, exports, and imports, |
825 | and evaluates the specified body expressions in this library's | |
826 | environment. | |
827 | ||
828 | The library @var{name} is a non-empty list of identifiers, optionally | |
829 | ending with a version specification of the form described above | |
830 | (@pxref{Creating Guile Modules}). | |
831 | ||
832 | Each @var{export-spec} is the name of a variable defined or imported | |
833 | by the library, or must take the form | |
834 | @code{(rename (internal-name external-name) ...)}, where the | |
835 | identifier @var{internal-name} names a variable defined or imported | |
836 | by the library and @var{external-name} is the name by which the | |
837 | variable is seen by importing libraries. | |
838 | ||
43d6eb75 | 839 | Each @var{import-spec} must be either an @dfn{import set} (see below) |
71194485 JG |
840 | or must be of the form @code{(for import-set import-level ...)}, |
841 | where each @var{import-level} is one of: | |
842 | ||
843 | @lisp | |
844 | run | |
845 | expand | |
846 | (meta @var{level}) | |
847 | @end lisp | |
848 | ||
849 | where @var{level} is an integer. Note that since Guile does not | |
850 | require explicit phase specification, any @var{import-set}s found | |
851 | inside of @code{for} sub-forms will be ``unwrapped'' during | |
852 | expansion and processed as if they had been specified directly. | |
853 | ||
854 | Import sets in turn take one of the following forms: | |
855 | ||
856 | @lisp | |
857 | @var{library-reference} | |
858 | (library @var{library-reference}) | |
859 | (only @var{import-set} @var{identifier} ...) | |
860 | (except @var{import-set} @var{identifier} ...) | |
861 | (prefix @var{import-set} @var{identifier}) | |
862 | (rename @var{import-set} (@var{internal-identifier} @var{external-identifier}) ...) | |
863 | @end lisp | |
864 | ||
865 | where @var{library-reference} is a non-empty list of identifiers | |
866 | ending with an optional version reference (@pxref{R6RS Version | |
867 | References}), and the other sub-forms have the following semantics, | |
868 | defined recursively on nested @var{import-set}s: | |
869 | ||
870 | @itemize @bullet | |
871 | ||
872 | @item | |
873 | The @code{library} sub-form is used to specify libraries for import | |
874 | whose names begin with the identifier ``library.'' | |
875 | ||
876 | @item | |
877 | The @code{only} sub-form imports only the specified @var{identifier}s | |
878 | from the given @var{import-set}. | |
879 | ||
880 | @item | |
881 | The @code{except} sub-form imports all of the bindings exported by | |
882 | @var{import-set} except for those that appear in the specified list | |
883 | of @var{identifier}s. | |
884 | ||
885 | @item | |
886 | The @code{prefix} sub-form imports all of the bindings exported | |
887 | by @var{import-set}, first prefixing them with the specified | |
888 | @var{identifier}. | |
889 | ||
890 | @item | |
891 | The @code{rename} sub-form imports all of the identifiers exported | |
892 | by @var{import-set}. The binding for each @var{internal-identifier} | |
893 | among these identifiers is made visible to the importing library as | |
894 | the corresponding @var{external-identifier}; all other bindings are | |
895 | imported using the names provided by @var{import-set}. | |
896 | ||
897 | @end itemize | |
898 | ||
899 | Note that because Guile translates R6RS libraries into module | |
900 | definitions, an import specification may be used to declare a | |
901 | dependency on a native Guile module --- although doing so may make | |
902 | your libraries less portable to other Schemes. | |
903 | ||
904 | @end deffn | |
905 | ||
43d6eb75 | 906 | @deffn {Scheme Syntax} import import-spec ... |
71194485 JG |
907 | Import into the current environment the libraries specified by the |
908 | given import specifications, where each @var{import-spec} takes the | |
43d6eb75 | 909 | same form as in the @code{library} form described above. |
71194485 JG |
910 | @end deffn |
911 | ||
912 | ||
07d83abe | 913 | @node Accessing Modules from C |
726b8ba3 | 914 | @subsection Accessing Modules from C |
07d83abe MV |
915 | |
916 | The last sections have described how modules are used in Scheme code, | |
917 | which is the recommended way of creating and accessing modules. You | |
918 | can also work with modules from C, but it is more cumbersome. | |
919 | ||
920 | The following procedures are available. | |
921 | ||
922 | @deftypefn {C Procedure} SCM scm_current_module () | |
923 | Return the module that is the @emph{current module}. | |
924 | @end deftypefn | |
925 | ||
926 | @deftypefn {C Procedure} SCM scm_set_current_module (SCM @var{module}) | |
927 | Set the current module to @var{module} and return the previous current | |
928 | module. | |
929 | @end deftypefn | |
930 | ||
931 | @deftypefn {C Procedure} SCM scm_c_call_with_current_module (SCM @var{module}, SCM (*@var{func})(void *), void *@var{data}) | |
932 | Call @var{func} and make @var{module} the current module during the | |
933 | call. The argument @var{data} is passed to @var{func}. The return | |
934 | value of @code{scm_c_call_with_current_module} is the return value of | |
935 | @var{func}. | |
936 | @end deftypefn | |
937 | ||
938 | @deftypefn {C Procedure} SCM scm_c_lookup (const char *@var{name}) | |
939 | Return the variable bound to the symbol indicated by @var{name} in the | |
940 | current module. If there is no such binding or the symbol is not | |
941 | bound to a variable, signal an error. | |
942 | @end deftypefn | |
943 | ||
944 | @deftypefn {C Procedure} SCM scm_lookup (SCM @var{name}) | |
945 | Like @code{scm_c_lookup}, but the symbol is specified directly. | |
946 | @end deftypefn | |
947 | ||
948 | @deftypefn {C Procedure} SCM scm_c_module_lookup (SCM @var{module}, const char *@var{name}) | |
949 | @deftypefnx {C Procedure} SCM scm_module_lookup (SCM @var{module}, SCM @var{name}) | |
950 | Like @code{scm_c_lookup} and @code{scm_lookup}, but the specified | |
951 | module is used instead of the current one. | |
952 | @end deftypefn | |
953 | ||
954 | @deftypefn {C Procedure} SCM scm_c_define (const char *@var{name}, SCM @var{val}) | |
955 | Bind the symbol indicated by @var{name} to a variable in the current | |
956 | module and set that variable to @var{val}. When @var{name} is already | |
957 | bound to a variable, use that. Else create a new variable. | |
958 | @end deftypefn | |
959 | ||
960 | @deftypefn {C Procedure} SCM scm_define (SCM @var{name}, SCM @var{val}) | |
961 | Like @code{scm_c_define}, but the symbol is specified directly. | |
962 | @end deftypefn | |
963 | ||
964 | @deftypefn {C Procedure} SCM scm_c_module_define (SCM @var{module}, const char *@var{name}, SCM @var{val}) | |
965 | @deftypefnx {C Procedure} SCM scm_module_define (SCM @var{module}, SCM @var{name}, SCM @var{val}) | |
966 | Like @code{scm_c_define} and @code{scm_define}, but the specified | |
967 | module is used instead of the current one. | |
968 | @end deftypefn | |
969 | ||
970 | @deftypefn {C Procedure} SCM scm_module_reverse_lookup (SCM @var{module}, SCM @var{variable}) | |
971 | Find the symbol that is bound to @var{variable} in @var{module}. When no such binding is found, return @var{#f}. | |
972 | @end deftypefn | |
973 | ||
974 | @deftypefn {C Procedure} SCM scm_c_define_module (const char *@var{name}, void (*@var{init})(void *), void *@var{data}) | |
975 | Define a new module named @var{name} and make it current while | |
976 | @var{init} is called, passing it @var{data}. Return the module. | |
977 | ||
978 | The parameter @var{name} is a string with the symbols that make up | |
979 | the module name, separated by spaces. For example, @samp{"foo bar"} names | |
980 | the module @samp{(foo bar)}. | |
981 | ||
982 | When there already exists a module named @var{name}, it is used | |
983 | unchanged, otherwise, an empty module is created. | |
984 | @end deftypefn | |
985 | ||
986 | @deftypefn {C Procedure} SCM scm_c_resolve_module (const char *@var{name}) | |
987 | Find the module name @var{name} and return it. When it has not | |
988 | already been defined, try to auto-load it. When it can't be found | |
989 | that way either, create an empty module. The name is interpreted as | |
990 | for @code{scm_c_define_module}. | |
991 | @end deftypefn | |
992 | ||
993 | @deftypefn {C Procedure} SCM scm_resolve_module (SCM @var{name}) | |
994 | Like @code{scm_c_resolve_module}, but the name is given as a real list | |
995 | of symbols. | |
996 | @end deftypefn | |
997 | ||
998 | @deftypefn {C Procedure} SCM scm_c_use_module (const char *@var{name}) | |
999 | Add the module named @var{name} to the uses list of the current | |
1000 | module, as with @code{(use-modules @var{name})}. The name is | |
1001 | interpreted as for @code{scm_c_define_module}. | |
1002 | @end deftypefn | |
1003 | ||
1004 | @deftypefn {C Procedure} SCM scm_c_export (const char *@var{name}, ...) | |
1005 | Add the bindings designated by @var{name}, ... to the public interface | |
1006 | of the current module. The list of names is terminated by | |
1007 | @code{NULL}. | |
1008 | @end deftypefn | |
1009 | ||
ef5f9163 | 1010 | |
07d83abe MV |
1011 | @node Variables |
1012 | @subsection Variables | |
1013 | @tpindex Variables | |
1014 | ||
1015 | Each module has its own hash table, sometimes known as an @dfn{obarray}, | |
1016 | that maps the names defined in that module to their corresponding | |
1017 | variable objects. | |
1018 | ||
1019 | A variable is a box-like object that can hold any Scheme value. It is | |
1020 | said to be @dfn{undefined} if its box holds a special Scheme value that | |
1021 | denotes undefined-ness (which is different from all other Scheme values, | |
1022 | including for example @code{#f}); otherwise the variable is | |
1023 | @dfn{defined}. | |
1024 | ||
1025 | On its own, a variable object is anonymous. A variable is said to be | |
1026 | @dfn{bound} when it is associated with a name in some way, usually a | |
1027 | symbol in a module obarray. When this happens, the relationship is | |
1028 | mutual: the variable is bound to the name (in that module), and the name | |
1029 | (in that module) is bound to the variable. | |
1030 | ||
1031 | (That's the theory, anyway. In practice, defined-ness and bound-ness | |
1032 | sometimes get confused, because Lisp and Scheme implementations have | |
1033 | often conflated --- or deliberately drawn no distinction between --- a | |
1034 | name that is unbound and a name that is bound to a variable whose value | |
1035 | is undefined. We will try to be clear about the difference and explain | |
1036 | any confusion where it is unavoidable.) | |
1037 | ||
1038 | Variables do not have a read syntax. Most commonly they are created and | |
1039 | bound implicitly by @code{define} expressions: a top-level @code{define} | |
1040 | expression of the form | |
1041 | ||
1042 | @lisp | |
1043 | (define @var{name} @var{value}) | |
1044 | @end lisp | |
1045 | ||
1046 | @noindent | |
1047 | creates a variable with initial value @var{value} and binds it to the | |
1048 | name @var{name} in the current module. But they can also be created | |
1049 | dynamically by calling one of the constructor procedures | |
1050 | @code{make-variable} and @code{make-undefined-variable}. | |
1051 | ||
07d83abe MV |
1052 | @deffn {Scheme Procedure} make-undefined-variable |
1053 | @deffnx {C Function} scm_make_undefined_variable () | |
1054 | Return a variable that is initially unbound. | |
1055 | @end deffn | |
1056 | ||
1057 | @deffn {Scheme Procedure} make-variable init | |
1058 | @deffnx {C Function} scm_make_variable (init) | |
1059 | Return a variable initialized to value @var{init}. | |
1060 | @end deffn | |
1061 | ||
1062 | @deffn {Scheme Procedure} variable-bound? var | |
1063 | @deffnx {C Function} scm_variable_bound_p (var) | |
1064 | Return @code{#t} iff @var{var} is bound to a value. | |
1065 | Throws an error if @var{var} is not a variable object. | |
1066 | @end deffn | |
1067 | ||
1068 | @deffn {Scheme Procedure} variable-ref var | |
1069 | @deffnx {C Function} scm_variable_ref (var) | |
1070 | Dereference @var{var} and return its value. | |
1071 | @var{var} must be a variable object; see @code{make-variable} | |
1072 | and @code{make-undefined-variable}. | |
1073 | @end deffn | |
1074 | ||
1075 | @deffn {Scheme Procedure} variable-set! var val | |
1076 | @deffnx {C Function} scm_variable_set_x (var, val) | |
1077 | Set the value of the variable @var{var} to @var{val}. | |
1078 | @var{var} must be a variable object, @var{val} can be any | |
1079 | value. Return an unspecified value. | |
1080 | @end deffn | |
1081 | ||
626e36e5 AW |
1082 | @deffn {Scheme Procedure} variable-unset! var |
1083 | @deffnx {C Function} scm_variable_unset_x (var) | |
1084 | Unset the value of the variable @var{var}, leaving @var{var} unbound. | |
1085 | @end deffn | |
1086 | ||
07d83abe MV |
1087 | @deffn {Scheme Procedure} variable? obj |
1088 | @deffnx {C Function} scm_variable_p (obj) | |
1089 | Return @code{#t} iff @var{obj} is a variable object, else | |
1090 | return @code{#f}. | |
1091 | @end deffn | |
1092 | ||
1093 | ||
726b8ba3 AW |
1094 | @node provide and require |
1095 | @subsection provide and require | |
1096 | ||
1097 | Aubrey Jaffer, mostly to support his portable Scheme library SLIB, | |
1098 | implemented a provide/require mechanism for many Scheme implementations. | |
1099 | Library files in SLIB @emph{provide} a feature, and when user programs | |
1100 | @emph{require} that feature, the library file is loaded in. | |
1101 | ||
1102 | For example, the file @file{random.scm} in the SLIB package contains the | |
1103 | line | |
1104 | ||
1105 | @lisp | |
1106 | (provide 'random) | |
1107 | @end lisp | |
1108 | ||
1109 | so to use its procedures, a user would type | |
1110 | ||
1111 | @lisp | |
1112 | (require 'random) | |
1113 | @end lisp | |
1114 | ||
1115 | and they would magically become available, @emph{but still have the same | |
1116 | names!} So this method is nice, but not as good as a full-featured | |
1117 | module system. | |
1118 | ||
1119 | When SLIB is used with Guile, provide and require can be used to access | |
1120 | its facilities. | |
1121 | ||
1122 | @node Environments | |
1123 | @subsection Environments | |
1124 | @cindex environment | |
1125 | ||
1126 | Scheme, as defined in R5RS, does @emph{not} have a full module system. | |
1127 | However it does define the concept of a top-level @dfn{environment}. | |
1128 | Such an environment maps identifiers (symbols) to Scheme objects such | |
1129 | as procedures and lists: @ref{About Closure}. In other words, it | |
1130 | implements a set of @dfn{bindings}. | |
1131 | ||
1132 | Environments in R5RS can be passed as the second argument to | |
1133 | @code{eval} (@pxref{Fly Evaluation}). Three procedures are defined to | |
1134 | return environments: @code{scheme-report-environment}, | |
1135 | @code{null-environment} and @code{interaction-environment} (@pxref{Fly | |
1136 | Evaluation}). | |
1137 | ||
1138 | In addition, in Guile any module can be used as an R5RS environment, | |
1139 | i.e., passed as the second argument to @code{eval}. | |
1140 | ||
1141 | Note: the following two procedures are available only when the | |
1142 | @code{(ice-9 r5rs)} module is loaded: | |
1143 | ||
1144 | @lisp | |
1145 | (use-modules (ice-9 r5rs)) | |
1146 | @end lisp | |
1147 | ||
1148 | @deffn {Scheme Procedure} scheme-report-environment version | |
1149 | @deffnx {Scheme Procedure} null-environment version | |
1150 | @var{version} must be the exact integer `5', corresponding to revision | |
1151 | 5 of the Scheme report (the Revised^5 Report on Scheme). | |
1152 | @code{scheme-report-environment} returns a specifier for an | |
1153 | environment that is empty except for all bindings defined in the | |
1154 | report that are either required or both optional and supported by the | |
1155 | implementation. @code{null-environment} returns a specifier for an | |
1156 | environment that is empty except for the (syntactic) bindings for all | |
1157 | syntactic keywords defined in the report that are either required or | |
1158 | both optional and supported by the implementation. | |
1159 | ||
1160 | Currently Guile does not support values of @var{version} for other | |
1161 | revisions of the report. | |
1162 | ||
1163 | The effect of assigning (through the use of @code{eval}) a variable | |
1164 | bound in a @code{scheme-report-environment} (for example @code{car}) | |
1165 | is unspecified. Currently the environments specified by | |
1166 | @code{scheme-report-environment} are not immutable in Guile. | |
1167 | @end deffn | |
1168 | ||
1169 | ||
1170 | ||
07d83abe MV |
1171 | @c Local Variables: |
1172 | @c TeX-master: "guile.texi" | |
1173 | @c End: |