api-modules.texi 47 KB

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