api-modules.texi 50 KB

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