api-modules.texi 46 KB

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