api-modules.texi 45 KB

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