api-modules.texi 44 KB

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