api-modules.texi 51 KB

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