env.texi 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166
  1. \input texinfo @c -*-texinfo-*-
  2. @c %**start of header
  3. @setfilename env.info
  4. @settitle Top-level Environments in Guile
  5. @c %**end of header
  6. @setchapternewpage odd
  7. @c Changes since Jost's implementation:
  8. @c "finite environments" -> "leaf environments"
  9. @c "scm_foo_internal" -> "scm_c_foo"
  10. @c To do:
  11. @c add spec for soft environments
  12. @c When merged into the main manual, add cross-references for:
  13. @c weak references
  14. @c smobs (esp. module's mark and free functions)
  15. [[add refs for all conditions signalled]]
  16. @ifinfo
  17. Copyright 1999, 2006 Free Software Foundation, Inc.
  18. @end ifinfo
  19. @titlepage
  20. @sp 10
  21. @comment The title is printed in a large font.
  22. @center @titlefont{Top-level Environments in Guile}
  23. @c The following two commands start the copyright page.
  24. @page
  25. @vskip 0pt plus 1filll
  26. Copyright @copyright{} 1999, 2006 Free Software Foundation, Inc.
  27. @end titlepage
  28. @node Top, Motivation, (dir), (dir)
  29. @menu
  30. * Motivation::
  31. * Top-Level Environments in Guile::
  32. * Modules::
  33. @end menu
  34. @node Motivation, Top-Level Environments in Guile, Top, Top
  35. @chapter Motivation
  36. @example
  37. $Id: env.texi,v 1.1.10.1 2006-02-12 13:42:50 mvo Exp $
  38. @end example
  39. This is a draft proposal for a new datatype for representing top-level
  40. environments in Guile. Upon completion, this proposal will be posted to
  41. the mailing list @samp{guile@@cygnus.com} for discussion, revised in
  42. light of whatever insights that may produce, and eventually implemented.
  43. Note that this is @emph{not} a proposal for a module system; rather, it
  44. is a proposal for a data structure which encapsulates the ideas one
  45. needs when writing a module system, and, most importantly, a fixed
  46. interface which insulates the interpreter from the details of the module
  47. system. Using these environments, one could implement any module system
  48. one pleased, without changing the interpreter.
  49. I hope this text will eventually become a chapter of the Guile manual;
  50. thus, the description of environments in written in the present tense,
  51. as if it were already implemented, not in the future tense. However,
  52. this text does not actually describe the present state of Guile.
  53. I'm especially interested in improving the vague, rambling presentation
  54. of environments in the section "Modules and Environments". I'm trying
  55. to orient the user for the discussion that follows, but I wonder if I'm
  56. just confusing the issue. I would appreciate suggestions if they are
  57. concrete --- please provide new wording.
  58. Note also: I'm trying out a convention I'm considering for use in the
  59. manual. When a Scheme procedure which is directly implemented by a C
  60. procedure, and both are useful to call from their respective languages,
  61. we document the Scheme procedure only, and call it a "Primitive". If a
  62. Scheme function is marked as a primitive, you can derive the name of the
  63. corresponding C function by changing @code{-} to @code{_}, @code{!} to
  64. @code{_x}, @code{?} to @code{_p}, and prepending @code{scm_}. The C
  65. function's arguments will be all of the Scheme procedure's argumements,
  66. both required and optional; if the Scheme procedure takes a ``rest''
  67. argument, that will be a final argument to the C function. The C
  68. function's arguments, as well as its return type, will be @code{SCM}.
  69. Thus, a procedure documented like this:
  70. @deffn Primitive set-car! pair value
  71. @end deffn
  72. has a corresponding C function which would be documented like this:
  73. @deftypefn {Libguile function} SCM scm_set_car_x (SCM @var{pair}, SCM @var{value})
  74. @end deftypefn
  75. The hope is that this will be an uncluttered way to document both the C
  76. and Scheme interfaces, without unduly confusing users interested only in
  77. the Scheme level.
  78. When there is a C function which provides the same functionality as a
  79. primitive, but with a different interface tailored for C's needs, it
  80. usually has the same name as the primitive's C function, but with the
  81. prefix @code{scm_c_} instead of simply @code{scm_}. Thus,
  82. @code{scm_c_environment_ref} is almost identical to
  83. @code{scm_environment_ref}, except that it indicates an unbound variable
  84. in a manner friendlier to C code.
  85. @node Top-Level Environments in Guile, Modules, Motivation, Top
  86. @chapter Top-Level Environments in Guile
  87. In Guile, an environment is a mapping from symbols onto variables, and
  88. a variable is a location containing a value. Guile uses the datatype
  89. described here to represent its top-level environments.
  90. @menu
  91. * Modules and Environments:: Modules are environments, with bookkeeping.
  92. * Common Environment Operations:: Looking up bindings, creating bindings, etc.
  93. * Standard Environment Types:: Guile has some fundamental environment types.
  94. * Implementing Environments:: You can extend Guile with new kinds of
  95. environments.
  96. * Switching to Environments:: Changes needed to today's Guile to
  97. implement the features described here.
  98. @end menu
  99. @node Modules and Environments, Common Environment Operations, Top-Level Environments in Guile, Top-Level Environments in Guile
  100. @section Modules and Environments
  101. Guile distinguishes between environments and modules. A module is a
  102. unit of code sharing; it has a name, like @code{(math random)}, an
  103. implementation (e.g., Scheme source code, a dynamically linked library,
  104. or a set of primitives built into Guile), and finally, an environment
  105. containing the definitions which the module exports for its users.
  106. An environment, by contrast, is simply an abstract data type
  107. representing a mapping from symbols onto variables which the Guile
  108. interpreter uses to look up top-level definitions. The @code{eval}
  109. procedure interprets its first argument, an expression, in the context
  110. of its second argument, an environment.
  111. Guile uses environments to implement its module system. A module
  112. created by loading Scheme code might be built from several environments.
  113. In addition to the environment of exported definitions, such a module
  114. might have an internal top-level environment, containing both exported
  115. and private definitions, and perhaps environments for imported
  116. definitions alone and local definitions alone.
  117. The interface described here includes a full set of functions for
  118. mutating environments, and the system goes to some length to maintain
  119. its consistency as environments' bindings change. This is necessary
  120. because Guile is an interactive system. The user may create new
  121. definitions or modify and reload modules while Guile is running; the
  122. system should handle these changes in a consistent and predictable way.
  123. A typical Guile system will have several distinct top-level
  124. environments. (This is why we call them ``top-level'', and not
  125. ``global''.) For example, consider the following fragment of an
  126. interactive Guile session:
  127. @example
  128. guile> (use-modules (ice-9 regex))
  129. guile> (define pattern "^(..+)\\1+$")
  130. guile> (string-match pattern "xxxx")
  131. #("xxxx" (0 . 4) (0 . 2))
  132. guile> (string-match pattern "xxxxx")
  133. #f
  134. guile>
  135. @end example
  136. @noindent
  137. Guile evaluates the expressions the user types in a top-level
  138. environment reserved for that purpose; the definition of @code{pattern}
  139. goes there. That environment is distinct from the one holding the
  140. private definitions of the @code{(ice-9 regex)} module. At the Guile
  141. prompt, the user does not see the module's private definitions, and the
  142. module is unaffected by definitions the user makes at the prompt. The
  143. @code{use-modules} form copies the module's public bindings into the
  144. user's environment.
  145. All Scheme evaluation takes place with respect to some top-level
  146. environment. Just as the procedure created by a @code{lambda} form
  147. closes over any local scopes surrounding that form, it also closes over
  148. the surrounding top-level environment. Thus, since the
  149. @code{string-match} procedure is defined in the @code{(ice-9 regex)}
  150. module, it closes over that module's top-level environment. Thus, when
  151. the user calls @code{string-match} from the Guile prompt, any free
  152. variables in @code{string-match}'s definition are resolved with respect
  153. to the module's top-level environment, not the user's.
  154. Although the Guile interaction loop maintains a ``current'' top-level
  155. environment in which it evaluates the user's input, it would be
  156. misleading to extend the concept of a ``current top-level environment''
  157. to the system as a whole. Each procedure closes over its own top-level
  158. environment, in which that procedure will find bindings for its free
  159. variables. Thus, the top-level environment in force at any given time
  160. depends on the procedure Guile happens to be executing. The global
  161. ``current'' environment is a figment of the interaction loop's
  162. imagination.
  163. Since environments provide all the operations the Guile interpreter
  164. needs to evaluate code, they effectively insulate the interpreter from
  165. the details of the module system. Without changing the interpreter, you
  166. can implement any module system you like, as long as its efforts produce
  167. an environment object the interpreter can consult.
  168. Finally, environments may prove a convenient way for Guile to access the
  169. features of other systems. For example, one might export the The GIMP's
  170. Procedural Database to Guile as a custom environment type; this
  171. environment could create Scheme procedure objects corresponding to GIMP
  172. procedures, as the user referenced them.
  173. @node Common Environment Operations, Standard Environment Types, Modules and Environments, Top-Level Environments in Guile
  174. @section Common Environment Operations
  175. This section describes the common set of operations that all environment
  176. objects support. To create an environment object, or to perform an
  177. operation specific to a particular kind of environment, see
  178. @ref{Standard Environment Types}.
  179. In this section, the following names for formal parameters imply that
  180. the actual parameters must have a certain type:
  181. @table @var
  182. @item env
  183. an environment
  184. @item symbol
  185. a symbol
  186. @item proc
  187. a procedure
  188. @item value
  189. @itemx object
  190. an arbitrary Scheme value
  191. @end table
  192. @menu
  193. * Examining Environments::
  194. * Changing Environments::
  195. * Caching Environment Lookups::
  196. * Observing Changes to Environments ::
  197. * Environment Errors::
  198. @end menu
  199. @node Examining Environments, Changing Environments, Common Environment Operations, Common Environment Operations
  200. @subsection Examining Environments
  201. @deffn Primitive environment? object
  202. Return @code{#t} if @var{object} is an environment, or @code{#f} otherwise.
  203. @end deffn
  204. @deffn Primitive environment-ref env symbol
  205. Return the value of the location bound to @var{symbol} in @var{env}.
  206. If @var{symbol} is unbound in @var{env}, signal an @code{environment:unbound}
  207. error (@pxref{Environment Errors}).
  208. @end deffn
  209. @deffn Primitive environment-bound? env symbol
  210. Return @code{#t} if @var{symbol} is bound in @var{env}, or @code{#f}
  211. otherwise.
  212. @end deffn
  213. @deffn Primitive environment-fold env proc init
  214. Iterate over all the bindings in an environment, accumulating some value.
  215. For each binding in @var{env}, apply @var{proc} to the symbol bound, its
  216. value, and the result from the previous application of @var{proc}. Use
  217. @var{init} as @var{proc}'s third argument the first time @var{proc} is
  218. applied.
  219. If @var{env} contains no bindings, this function simply returns @var{init}.
  220. If @var{env} binds the symbol @var{sym1} to the value @var{val1},
  221. @var{sym2} to @var{val2}, and so on, then this procedure computes:
  222. @example
  223. (@var{proc} @var{sym1} @var{val1}
  224. (@var{proc} @var{sym2} @var{val2}
  225. ...
  226. (@var{proc} @var{symn} @var{valn}
  227. @var{init})))
  228. @end example
  229. Each binding in @var{env} is processed at most once.
  230. @code{environment-fold} makes no guarantees about the order in which the
  231. bindings are processed.
  232. If @var{env} is not modified while the iteration is taking place,
  233. @code{environment-fold} will apply @var{proc} to each binding in
  234. @var{env} exactly once.
  235. If @var{env} is modified while the iteration is taking place, we need to
  236. be more subtle in describing @code{environment-fold}'s behavior.
  237. @code{environment-fold} repeatedly applies @var{proc} to a binding which
  238. was present in @var{env} when @code{environment-fold} was invoked and is
  239. still present in @var{env}, until there are no such bindings remaining.
  240. (If no mutations take place, this definition is equivalent to the
  241. simpler one given above.) By this definition, bindings added during the
  242. iteration will not be passed to @var{proc}.
  243. Here is a function which, given an environment, constructs an
  244. association list representing that environment's bindings, using
  245. @code{environment-fold}:
  246. @example
  247. (define (environment->alist env)
  248. (environment-fold env
  249. (lambda (sym val tail)
  250. (cons (cons sym val) tail))
  251. '()))
  252. @end example
  253. @end deffn
  254. @deftypefn {Libguile macro} int SCM_ENVP (@var{object})
  255. Return non-zero if @var{object} is an environment.
  256. @end deftypefn
  257. @deftypefn {Libguile function} SCM scm_c_environment_ref (SCM @var{env}, SCM @var{symbol})
  258. This C function is identical to @code{environment-ref}, except that if
  259. @var{symbol} is unbound in @var{env}, it returns the value
  260. @code{SCM_UNDEFINED}, instead of signalling an error.
  261. @end deftypefn
  262. @deftypefn {Libguile function} SCM scm_c_environment_fold (SCM @var{env}, scm_environment_folder *@var{proc}, SCM @var{data}, SCM @var{init})
  263. This is the C-level analog of @code{environment-fold}. For each binding in
  264. @var{env}, make the call:
  265. @example
  266. (*@var{proc}) (@var{data}, @var{symbol}, @var{value}, @var{previous})
  267. @end example
  268. @noindent
  269. where @var{previous} is the value returned from the last call to
  270. @code{*@var{proc}}, or @var{init} for the first call. If @var{env}
  271. contains no bindings, return @var{init}.
  272. @end deftypefn
  273. @deftp {Libguile data type} scm_environment_folder SCM (SCM @var{data}, SCM @var{symbol}, SCM @var{value}, SCM @var{tail})
  274. The type of a folding function to pass to @code{scm_c_environment_fold}.
  275. @end deftp
  276. @node Changing Environments, Caching Environment Lookups, Examining Environments, Common Environment Operations
  277. @subsection Changing Environments
  278. Here are functions for changing symbols' bindings and values.
  279. Although it is common to say that an environment binds a symbol to a
  280. value, this is not quite accurate; an environment binds a symbol to a
  281. location, and the location contains a value. In the descriptions below,
  282. we will try to make clear how each function affects bindings and
  283. locations.
  284. Note that some environments may contain some immutable bindings, or may
  285. bind symbols to immutable locations. If you attempt to change an
  286. immutable binding or value, these functions will signal an
  287. @code{environment:immutable-binding} or
  288. @code{environment:immutable-location} error. However, simply because a
  289. binding cannot be changed via these functions does @emph{not} imply that
  290. it is constant. Mechanisms outside the scope of this section (say,
  291. re-loading a module's source code) may change a binding or value which
  292. is immutable via these functions.
  293. @deffn Primitive environment-define env symbol value
  294. Bind @var{symbol} to a new location containing @var{value} in @var{env}.
  295. If @var{symbol} is already bound to another location in @var{env}, that
  296. binding is replaced. The new binding and location are both mutable.
  297. The return value is unspecified.
  298. If @var{symbol} is already bound in @var{env}, and the binding is
  299. immutable, signal an @code{environment:immutable-binding} error.
  300. @end deffn
  301. @deffn Primitive environment-undefine env symbol
  302. Remove any binding for @var{symbol} from @var{env}. If @var{symbol} is
  303. unbound in @var{env}, do nothing. The return value is unspecified.
  304. If @var{symbol} is already bound in @var{env}, and the binding is
  305. immutable, signal an @code{environment:immutable-binding} error.
  306. @end deffn
  307. @deffn Primitive environment-set! env symbol value
  308. If @var{env} binds @var{symbol} to some location, change that location's
  309. value to @var{value}. The return value is unspecified.
  310. If @var{symbol} is not bound in @var{env}, signal an
  311. @code{environment:unbound} error. If @var{env} binds @var{symbol} to an
  312. immutable location, signal an @code{environment:immutable-location}
  313. error.
  314. @end deffn
  315. @node Caching Environment Lookups, Observing Changes to Environments , Changing Environments, Common Environment Operations
  316. @subsection Caching Environment Lookups
  317. Some applications refer to variables' values so frequently that the
  318. overhead of @code{environment-ref} and @code{environment-set!} is
  319. unacceptable. For example, variable reference speed is a critical
  320. factor in the performance of the Guile interpreter itself. If an
  321. application can tolerate some additional complexity, the
  322. @code{environment-cell} function described here can provide very
  323. efficient access to variable values.
  324. In the Guile interpreter, most variables are represented by pairs; the
  325. @sc{cdr} of the pair holds the variable's value. Thus, a variable
  326. reference corresponds to taking the @sc{cdr} of one of these pairs, and
  327. setting a variable corresponds to a @code{set-cdr!} operation. A pair
  328. used to represent a variable's value in this manner is called a
  329. @dfn{value cell}. Value cells represent the ``locations'' to which
  330. environments bind symbols.
  331. The @code{environment-cell} function returns the value cell bound to a
  332. symbol. For example, an interpreter might make the call
  333. @code{(environment-cell @var{env} @var{symbol} #t)} to find the value
  334. cell which @var{env} binds to @var{symbol}, and then use @code{cdr} and
  335. @code{set-cdr!} to reference and assign to that variable, instead of
  336. calling @code{environment-ref} or @var{environment-set!} for each
  337. variable reference.
  338. There are a few caveats that apply here:
  339. @itemize @bullet
  340. @item
  341. Environments are not required to represent variables' values using value
  342. cells. An environment is free to return @code{#f} in response to a
  343. request for a symbol's value cell; in this case, the caller must use
  344. @code{environment-ref} and @code{environment-set!} to manipulate the
  345. variable.
  346. @item
  347. An environment's binding for a symbol may change. For example, the user
  348. could override an imported variable with a local definition, associating
  349. a new value cell with that symbol. If an interpreter has used
  350. @code{environment-cell} to obtain the variable's value cell, it no
  351. longer needs to use @code{environment-ref} and @code{environment-set!}
  352. to access the variable, and it may not see the new binding.
  353. Thus, code which uses @code{environment-cell} should almost always use
  354. @code{environment-observe} to track changes to the symbol's binding;
  355. this is the additional complexity hinted at above. @xref{Observing
  356. Changes to Environments}.
  357. @item
  358. Some variables should be immutable. If a program uses
  359. @code{environment-cell} to obtain the value cell of such a variable,
  360. then it is impossible for the environment to prevent the program from
  361. changing the variable's value, using @code{set-cdr!}. However, this is
  362. discouraged; it is probably better to redesign the interface than to
  363. disregard such a request. To make it easy for programs to honor the
  364. immutability of a variable, @code{environment-cell} takes an argument
  365. indicating whether the caller intends to mutate the cell's value; if
  366. this argument is true, then @code{environment-cell} signals an
  367. @code{environment:immutable-location} error.
  368. Programs should therefore make separate calls to @code{environment-cell}
  369. to obtain value cells for reference and for assignment. It is incorrect
  370. for a program to call @code{environment-cell} once to obtain a value
  371. cell, and then use that cell for both reference and mutation.
  372. @end itemize
  373. @deffn Primitive environment-cell env symbol for-write
  374. Return the value cell which @var{env} binds to @var{symbol}, or
  375. @code{#f} if the binding does not live in a value cell.
  376. The argument @var{for-write} indicates whether the caller intends to
  377. modify the variable's value by mutating the value cell. If the variable
  378. is immutable, then @code{environment-cell} signals an
  379. @code{environment:immutable-location} error.
  380. If @var{symbol} is unbound in @var{env}, signal an @code{environment:unbound}
  381. error.
  382. If you use this function, you should consider using
  383. @code{environment-observe}, to be notified when @code{symbol} gets
  384. re-bound to a new value cell, or becomes undefined.
  385. @end deffn
  386. @deftypefn {Libguile function} SCM scm_c_environment_cell (SCM @var{env}, SCM @var{symbol}, int for_write)
  387. This C function is identical to @code{environment-cell}, except that if
  388. @var{symbol} is unbound in @var{env}, it returns the value
  389. @code{SCM_UNDEFINED}, instead of signalling an error.
  390. @end deftypefn
  391. [[After we have some experience using this, we may find that we want to
  392. be able to explicitly ask questions like, "Is this variable mutable?"
  393. without the annoyance of error handling. But maybe this is fine.]]
  394. @node Observing Changes to Environments , Environment Errors, Caching Environment Lookups, Common Environment Operations
  395. @subsection Observing Changes to Environments
  396. The procedures described here allow you to add and remove @dfn{observing
  397. procedures} for an environment.
  398. @menu
  399. * Registering Observing Procedures::
  400. * Observations and Garbage Collection::
  401. * Observing Environments from C Code::
  402. @end menu
  403. @node Registering Observing Procedures, Observations and Garbage Collection, Observing Changes to Environments , Observing Changes to Environments
  404. @subsubsection Registering Observing Procedures
  405. A program may register an @dfn{observing procedure} for an environment,
  406. which will be called whenever a binding in a particular environment
  407. changes. For example, if the user changes a module's source code and
  408. re-loads the module, other parts of the system may want to throw away
  409. information they have cached about the bindings of the older version of
  410. the module. To support this, each environment retains a set of
  411. observing procedures which it will invoke whenever its bindings change.
  412. We say that these procedures @dfn{observe} the environment's bindings.
  413. You can register new observing procedures for an environment using
  414. @code{environment-observe}.
  415. @deffn Primitive environment-observe env proc
  416. Whenever @var{env}'s bindings change, apply @var{proc} to @var{env}.
  417. This function returns an object, @var{token}, which you can pass to
  418. @code{environment-unobserve} to remove @var{proc} from the set of
  419. procedures observing @var{env}. The type and value of @var{token} is
  420. unspecified.
  421. @end deffn
  422. @deffn Primitive environment-unobserve token
  423. Cancel the observation request which returned the value @var{token}.
  424. The return value is unspecified.
  425. If a call @code{(environment-observe @var{env} @var{proc})} returns
  426. @var{token}, then the call @code{(environment-unobserve @var{token})}
  427. will cause @var{proc} to no longer be called when @var{env}'s bindings
  428. change.
  429. @end deffn
  430. There are some limitations on observation:
  431. @itemize @bullet
  432. @item
  433. These procedures do not allow you to observe specific bindings; you
  434. can only observe an entire environment.
  435. @item
  436. These procedures observe bindings, not locations. There is no way
  437. to receive notification when a location's value changes, using these
  438. procedures.
  439. @item
  440. These procedures do not promise to call the observing procedure for each
  441. individual binding change. However, if multiple bindings do change
  442. between calls to the observing procedure, those changes will appear
  443. atomic to the entire system, not just to a few observing procedures.
  444. @item
  445. Since a single environment may have several procedures observing it, a
  446. correct design obviously may not assume that nothing else in the system
  447. has yet observed a given change.
  448. @end itemize
  449. (One weakness of this observation architecture is that observing
  450. procedures make no promises to the observer. That's fine if you're just
  451. trying to implement an accurate cache, but too weak to implement things
  452. that walk the environment tree.)
  453. @node Observations and Garbage Collection, Observing Environments from C Code, Registering Observing Procedures, Observing Changes to Environments
  454. @subsubsection Observations and Garbage Collection
  455. When writing observing procedures, pay close attention to garbage
  456. collection issues. If you use @code{environment-observe} to register
  457. observing procedures for an environment, the environment will hold a
  458. reference to those procedures; while that environment is alive, its
  459. observing procedures will live, as will any data they close over. If
  460. this is not appropriate, you can use the @code{environment-observe-weak}
  461. procedure to create a weak reference from the environment to the
  462. observing procedure.
  463. For example, suppose an interpreter uses @code{environment-cell} to
  464. reference variables efficiently, as described above in @ref{Caching
  465. Environment Lookups}. That interpreter must register observing
  466. procedures to track changes to the environment. If those procedures
  467. retain any reference to the data structure representing the program
  468. being interpreted, then that structure cannot be collected as long as
  469. the observed environment lives. This is almost certainly incorrect ---
  470. if there are no other references to the structure, it can never be
  471. invoked, so it should be collected. In this case, the interpreter
  472. should register its observing procedure using
  473. @code{environment-observe-weak}, and retain a pointer to it from the
  474. code it updates. Thus, when the code is no longer referenced elsewhere
  475. in the system, the weak link will be broken, and Guile will collect the
  476. code (and its observing procedure).
  477. @deffn Primitive environment-observe-weak env proc
  478. This function is the same as @code{environment-observe}, except that the
  479. reference @var{env} retains to @var{proc} is a weak reference. This
  480. means that, if there are no other live, non-weak references to
  481. @var{proc}, it will be garbage-collected, and dropped from @var{env}'s
  482. list of observing procedures.
  483. @end deffn
  484. @node Observing Environments from C Code, , Observations and Garbage Collection, Observing Changes to Environments
  485. @subsubsection Observing Environments from C Code
  486. It is also possible to write code that observes an environment in C.
  487. The @code{scm_c_environment_observe} function registers a C
  488. function to observe an environment. The typedef
  489. @code{scm_environment_observer} is the type a C observer function must
  490. have.
  491. @deftypefn {Libguile function} SCM scm_c_environment_observe (SCM @var{env}, scm_environment_observer *proc, SCM @var{data}, int weak_p)
  492. This is the C-level analog of the Scheme function
  493. @code{environment-observe}. Whenever @var{env}'s bindings change, call
  494. the function @var{proc}, passing it @var{env} and @var{data}. If
  495. @var{weak_p} is non-zero, @var{env} will retain only a weak reference to
  496. @var{data}, and if @var{data} is garbage collected, the entire
  497. observation will be dropped.
  498. This function returns a token, with the same meaning as those returned
  499. by @code{environment-observe}.
  500. @end deftypefn
  501. @deftp {Libguile data type} scm_environment_observer void (SCM @var{env}, SCM @var{data})
  502. The type for observing functions written in C. A function meant to be
  503. passed to @code{scm_c_environment_observe} should have the type
  504. @code{scm_environment_observer}.
  505. @end deftp
  506. Note that, like all other primitives, @code{environment-observe} is also
  507. available from C, under the name @code{scm_environment_observe}.
  508. @node Environment Errors, , Observing Changes to Environments , Common Environment Operations
  509. @subsection Environment Errors
  510. Here are the error conditions signalled by the environment routines
  511. described above. In these conditions, @var{func} is a string naming a
  512. particular procedure.
  513. @deffn Condition environment:unbound func message args env symbol
  514. By calling @var{func}, the program attempted to retrieve the value of
  515. @var{symbol} in @var{env}, but @var{symbol} is unbound in @var{env}.
  516. @end deffn
  517. @deffn Condition environment:immutable-binding func message args env symbol
  518. By calling @var{func}, the program attempted to change the binding of
  519. @var{symbol} in @var{env}, but that binding is immutable.
  520. @end deffn
  521. @deffn Condition environment:immutable-location func message args env symbol
  522. By calling @var{func}, the program attempted to change the value of
  523. the location to which @var{symbol} is bound in @var{env}, but that
  524. location is immutable.
  525. @end deffn
  526. @node Standard Environment Types, Implementing Environments, Common Environment Operations, Top-Level Environments in Guile
  527. @section Standard Environment Types
  528. Guile supports several different kinds of environments. The operations
  529. described above are actually only the common functionality provided by
  530. all the members of a family of environment types, each designed for a
  531. separate purpose.
  532. Each environment type has a constructor procedure for building elements
  533. of that type, and extends the set of common operations with its own
  534. procedures, providing specialized functions. For an example of how
  535. these environment types work together, see @ref{Modules of Interpreted
  536. Scheme Code}.
  537. Guile allows users to define their own environment types. Given a set
  538. of procedures that implement the common environment operations, Guile
  539. will construct a new environment object based on those procedures.
  540. @menu
  541. * Leaf Environments:: A simple set of bindings.
  542. * Eval Environments:: Local definitions, shadowing
  543. imported definitions.
  544. * Import Environments:: The union of a list of environments.
  545. * Export Environments:: A selected subset of an environment.
  546. * General Environments:: Environments implemented by user
  547. functions.
  548. @end menu
  549. @node Leaf Environments, Eval Environments, Standard Environment Types, Standard Environment Types
  550. @subsection Leaf Environments
  551. A @dfn{leaf} environment is simply a mutable set of definitions. A mutable
  552. environment supports no operations beyond the common set.
  553. @deffn Primitive make-leaf-environment
  554. Create a new leaf environment, containing no bindings. All bindings
  555. and locations in the new environment are mutable.
  556. @end deffn
  557. @deffn Primitive leaf-environment? object
  558. Return @code{#t} if @var{object} is a leaf environment, or @var{#f}
  559. otherwise.
  560. @end deffn
  561. In Guile, each module of interpreted Scheme code uses a leaf
  562. environment to hold the definitions made in that module.
  563. Leaf environments are so named because their bindings are not computed
  564. from the contents of other environments. Most other environment types
  565. have no bindings of their own, but compute their binding sets based on
  566. those of their operand environments. Thus, the environments in a
  567. running Guile system form a tree, with interior nodes computing their
  568. contents from their child nodes. Leaf environments are the leaves of
  569. such trees.
  570. @node Eval Environments, Import Environments, Leaf Environments, Standard Environment Types
  571. @subsection Eval Environments
  572. A module's source code refers to definitions imported from other
  573. modules, and definitions made within itself. An @dfn{eval} environment
  574. combines two environments --- a @dfn{local} environment and an
  575. @dfn{imported} environment --- to produce a new environment in which
  576. both sorts of references can be resolved.
  577. @deffn Primitive make-eval-environment local imported
  578. Return a new environment object @var{eval} whose bindings are the union
  579. of the bindings in the environments @var{local} and @var{imported}, with
  580. bindings from @var{local} taking precedence. Definitions made in
  581. @var{eval} are placed in @var{local}.
  582. Applying @code{environment-define} or @code{environment-undefine} to
  583. @var{eval} has the same effect as applying the procedure to @var{local}.
  584. This means that applying @code{environment-undefine} to a symbol bound
  585. in @var{imported} and free in @var{local} has no effect on the bindings
  586. visible in @var{eval}, which may be surprising.
  587. Note that @var{eval} incorporates @var{local} and @var{imported}
  588. @emph{by reference} --- if, after creating @var{eval}, the program
  589. changes the bindings of @var{local} or @var{imported}, those changes
  590. will be visible in @var{eval}.
  591. Since most Scheme evaluation takes place in @var{eval} environments,
  592. they transparenty cache the bindings received from @var{local} and
  593. @var{imported}. Thus, the first time the program looks up a symbol in
  594. @var{eval}, @var{eval} may make calls to @var{local} or @var{imported}
  595. to find their bindings, but subsequent references to that symbol will be
  596. as fast as references to bindings in leaf environments.
  597. In typical use, @var{local} will be a leaf environment, and
  598. @var{imported} will be an import environment, described below.
  599. @end deffn
  600. @deffn Primitive eval-environment? object
  601. Return @code{#t} if @var{object} is an eval environment, or @code{#f}
  602. otherwise.
  603. @end deffn
  604. @deffn Primitive eval-environment-local env
  605. @deffnx Primitive eval-environment-imported env
  606. Return the @var{local} or @var{imported} environment of @var{env};
  607. @var{env} must be an eval environment.
  608. @end deffn
  609. @node Import Environments, Export Environments, Eval Environments, Standard Environment Types
  610. @subsection Import Environments
  611. An @dfn{import} environment combines the bindings of a set of
  612. argument environments, and checks for naming clashes.
  613. @deffn Primitive make-import-environment imports conflict-proc
  614. Return a new environment @var{imp} whose bindings are the union of the
  615. bindings from the environments in @var{imports}; @var{imports} must be a
  616. list of environments. That is, @var{imp} binds @var{symbol} to
  617. @var{location} when some element of @var{imports} does.
  618. If two different elements of @var{imports} have a binding for the same
  619. symbol, apply @var{conflict-proc} to the two environments. If the bindings
  620. of any of the @var{imports} ever changes, check for conflicts again.
  621. All bindings in @var{imp} are immutable. If you apply
  622. @code{environment-define} or @code{environment-undefine} to @var{imp},
  623. Guile will signal an @code{environment:immutable-binding} error.
  624. However, notice that the set of bindings in @var{imp} may still change,
  625. if one of its imported environments changes.
  626. @end deffn
  627. @deffn Primitive import-environment? object
  628. Return @code{#t} if @var{object} is an import environment, or @code{#f}
  629. otherwise.
  630. @end deffn
  631. @deffn Primitive import-environment-imports env
  632. Return the list of @var{env}'s imported environments; @var{env} must be
  633. an import env.
  634. @end deffn
  635. @deffn Primitive import-environment-set-imports! env imports
  636. Change @var{env}'s list of imported environments to @var{imports}, and
  637. check for conflicts.
  638. @end deffn
  639. I'm not at all sure about the way @var{conflict-proc} works. I think
  640. module systems should warn you if it seems you're likely to get the
  641. wrong binding, but exactly how and when those warnings should be
  642. generated, I don't know.
  643. @node Export Environments, General Environments, Import Environments, Standard Environment Types
  644. @subsection Export Environments
  645. An export environment restricts an environment a specified set of
  646. bindings.
  647. @deffn Primitive make-export-environment private signature
  648. Return a new environment @var{exp} containing only those bindings in
  649. @var{private} whose symbols are present in @var{signature}. The
  650. @var{private} argument must be an environment.
  651. The environment @var{exp} binds @var{symbol} to @var{location} when
  652. @var{env} does, and @var{symbol} is exported by @var{signature}.
  653. @var{Signature} is a list specifying which of the bindings in
  654. @var{private} should be visible in @var{exp}. Each element of
  655. @var{signature} should be a list of the form:
  656. @example
  657. (@var{symbol} @var{attribute} ...)
  658. @end example
  659. @noindent
  660. where each @var{attribute} is one of the following:
  661. @table @asis
  662. @item the symbol @code{mutable-location}
  663. @var{exp} should treat the location bound to @var{symbol} as mutable.
  664. That is, @var{exp} will pass calls to @var{env-set!} or
  665. @code{environment-cell} directly through to @var{private}.
  666. @item the symbol @code{immutable-location}
  667. @var{exp} should treat the location bound to @var{symbol} as immutable.
  668. If the program applies @code{environment-set!} to @var{exp} and
  669. @var{symbol}, or calls @code{environment-cell} to obtain a writable
  670. value cell, @code{environment-set!} will signal an
  671. @code{environment:immutable-location} error.
  672. Note that, even if an export environment treats a location as immutable,
  673. the underlying environment may treat it as mutable, so its value may
  674. change.
  675. @end table
  676. It is an error for an element of @var{signature} to specify both
  677. @code{mutable-location} and @code{immutable-location}. If neither is
  678. specified, @code{immutable-location} is assumed.
  679. As a special case, if an element of @var{signature} is a lone symbol
  680. @var{sym}, it is equivalent to an element of the form
  681. @code{(@var{sym})}.
  682. All bindings in @var{exp} are immutable. If you apply
  683. @code{environment-define} or @code{environment-undefine} to @var{exp},
  684. Guile will signal an @code{environment:immutable-binding} error.
  685. However, notice that the set of bindings in @var{exp} may still change,
  686. if the bindings in @var{private} change.
  687. @end deffn
  688. @deffn Primitive export-environment? object
  689. Return @code{#t} if @var{object} is an export environment, or @code{#f}
  690. otherwise.
  691. @end deffn
  692. @deffn Primitive export-environment-private env
  693. @deffnx Primitive export-environment-set-private! env
  694. @deffnx Primitive export-environment-signature env
  695. @deffnx Primitive export-environment-set-signature! env
  696. Accessors and mutators for the private environment and signature of
  697. @var{env}; @var{env} must be an export environment.
  698. @end deffn
  699. @node General Environments, , Export Environments, Standard Environment Types
  700. @subsection General Environments
  701. [[user provides the procedures]]
  702. [[A observers B and C; B observes C; C changes; A should only be
  703. notified once, right?]]
  704. [[observation loops?]]
  705. @node Implementing Environments, Switching to Environments, Standard Environment Types, Top-Level Environments in Guile
  706. @section Implementing Environments
  707. This section describes how to implement new environment types in Guile.
  708. Guile's internal representation of environments allows you to extend
  709. Guile with new kinds of environments without modifying Guile itself.
  710. Every environment object carries a pointer to a structure of pointers to
  711. functions implementing the common operations for that environment. The
  712. procedures @code{environment-ref}, @code{environment-set!}, etc. simply
  713. find this structure and invoke the appropriate function.
  714. [[It would be nice to have an example around here. How about a
  715. persistent environment, bound to a directory, where ref and set actually
  716. access files? Ref on a directory would return another
  717. environment... Hey, let's import my home directory!]]
  718. @menu
  719. * Environment Function Tables::
  720. * Environment Data::
  721. * Environment Example::
  722. @end menu
  723. @node Environment Function Tables, Environment Data, Implementing Environments, Implementing Environments
  724. @subsection Environment Function Tables
  725. An environment object is a smob whose @sc{cdr} is a pointer to a pointer
  726. to a @code{struct environment_funcs}:
  727. @example
  728. struct environment_funcs @{
  729. SCM (*ref) (SCM self, SCM symbol);
  730. SCM (*fold) (SCM self, scm_environment_folder *proc, SCM data, SCM init);
  731. void (*define) (SCM self, SCM symbol, SCM value);
  732. void (*undefine) (SCM self, SCM symbol);
  733. void (*set) (SCM self, SCM symbol, SCM value);
  734. SCM (*cell) (SCM self, SCM symbol, int for_write);
  735. SCM (*observe) (SCM self, scm_environment_observer *proc, SCM data, int weak_p);
  736. void (*unobserve) (SCM self, SCM token);
  737. SCM (*mark) (SCM self);
  738. scm_sizet (*free) (SCM self);
  739. int (*print) (SCM self, SCM port, scm_print_state *pstate);
  740. @};
  741. @end example
  742. You can use the following macro to access an environment's function table:
  743. @deftypefn {Libguile macro} struct environment_funcs *SCM_ENVIRONMENT_FUNCS (@var{env})
  744. Return a pointer to the @code{struct environment_func} for the environment
  745. @var{env}. If @var{env} is not an environment object, the behavior of
  746. this macro is undefined.
  747. @end deftypefn
  748. Here is what each element of @var{env_funcs} must do to correctly
  749. implement an environment. In all of these calls, @var{self} is the
  750. environment whose function is being invoked.
  751. @table @code
  752. @item SCM ref (SCM @var{self}, SCM @var{symbol});
  753. This function must have the effect described above for the C call:
  754. @example
  755. scm_c_environment_ref (@var{self}, @var{symbol})
  756. @end example
  757. @xref{Examining Environments}.
  758. Note that the @code{ref} element of a @code{struct environment_funcs}
  759. may be zero if a @code{cell} function is provided.
  760. @item SCM fold (SCM self, scm_environment_folder *proc, SCM data, SCM init);
  761. This function must have the effect described above for the C call:
  762. @example
  763. scm_c_environment_fold (@var{self}, @var{proc}, @var{data}, @var{init})
  764. @end example
  765. @xref{Examining Environments}.
  766. @item void define (SCM self, SCM symbol, SCM value);
  767. This function must have the effect described above for the Scheme call:
  768. @example
  769. (environment-define @var{self} @var{symbol} @var{value})
  770. @end example
  771. @xref{Changing Environments}.
  772. @item void undefine (SCM self, SCM symbol);
  773. This function must have the effect described above for the Scheme call:
  774. @example
  775. (environment-undefine @var{self} @var{symbol})
  776. @end example
  777. @xref{Changing Environments}.
  778. @item void set (SCM self, SCM symbol, SCM value);
  779. This function must have the effect described above for the Scheme call:
  780. @example
  781. (environment-set! @var{self} @var{symbol} @var{value})
  782. @end example
  783. @xref{Changing Environments}.
  784. Note that the @code{set} element of a @code{struct environment_funcs}
  785. may be zero if a @code{cell} function is provided.
  786. @item SCM cell (SCM self, SCM symbol, int for_write);
  787. This function must have the effect described above for the C call:
  788. @example
  789. scm_c_environment_cell (@var{self}, @var{symbol})
  790. @end example
  791. @xref{Caching Environment Lookups}.
  792. @item SCM observe (SCM self, scm_environment_observer *proc, SCM data, int weak_p);
  793. This function must have the effect described above for the C call:
  794. @example
  795. scm_c_environment_observe (@var{env}, @var{proc}, @var{data}, @var{weak_p})
  796. @end example
  797. @xref{Observing Changes to Environments}.
  798. @item void unobserve (SCM self, SCM token);
  799. Cancel the request to observe @var{self} that returned @var{token}.
  800. @xref{Observing Changes to Environments}.
  801. @item SCM mark (SCM self);
  802. Set the garbage collection mark all Scheme cells referred to by
  803. @var{self}. Assume that @var{self} itself is already marked. Return a
  804. final object to be marked recursively.
  805. @item scm_sizet free (SCM self);
  806. Free all non-cell storage associated with @var{self}; return the number
  807. of bytes freed that were obtained using @code{scm_must_malloc} or
  808. @code{scm_must_realloc}.
  809. @item SCM print (SCM self, SCM port, scm_print_state *pstate);
  810. Print an external representation of @var{self} on @var{port}, passing
  811. @var{pstate} to any recursive calls to the object printer.
  812. @end table
  813. @node Environment Data, Environment Example, Environment Function Tables, Implementing Environments
  814. @subsection Environment Data
  815. When you implement a new environment type, you will likely want to
  816. associate some data of your own design with each environment object.
  817. Since ANSI C promises that casts will safely convert between a pointer
  818. to a structure and a pointer to its first element, you can have the
  819. @sc{cdr} of an environment smob point to your structure, as long as your
  820. structure's first element is a pointer to a @code{struct
  821. environment_funcs}. Then, your code can use the macro below to retrieve
  822. a pointer to the structure, and cast it to the appropriate type.
  823. @deftypefn {Libguile macro} struct environment_funcs **SCM_ENVIRONMENT_DATA (@var{env})
  824. Return the @sc{cdr} of @var{env}, as a pointer to a pointer to an
  825. @code{environment_funcs} structure.
  826. @end deftypefn
  827. @node Environment Example, , Environment Data, Implementing Environments
  828. @subsection Environment Example
  829. [[perhaps a simple environment based on association lists]]
  830. @node Switching to Environments, , Implementing Environments, Top-Level Environments in Guile
  831. @section Switching to Environments
  832. Here's what we'd need to do to today's Guile to install the system
  833. described above. This work would probably be done on a branch, because
  834. it involves crippling Guile while a lot of work gets done. Also, it
  835. could change the default set of bindings available pretty drastically,
  836. so the next minor release should not contain these changes.
  837. After each step here, we should have a Guile that we can at least
  838. interact with, perhaps with some limitations.
  839. @itemize @bullet
  840. @item
  841. For testing purposes, make an utterly minimal version of
  842. @file{boot-9.scm}: no module system, no R5RS, nothing. I think a simple
  843. REPL is all we need.
  844. @item
  845. Implement the environment datatypes in libguile, and test them using
  846. this utterly minimal system.
  847. @item
  848. Change the interpreter to use the @code{environment-cell} and
  849. @code{environment-observe} instead of the symbol value slots,
  850. first-class variables, etc. Modify the rest of libguile as necessary to
  851. register all the primitives in a single environment. We'll segregate
  852. them into modules later.
  853. @item
  854. Reimplement the current module system in terms of environments. It
  855. should still be in Scheme.
  856. @item
  857. Reintegrate the rest of @file{boot-9.scm}. This might be a good point
  858. to move it into modules.
  859. @item
  860. Do some profiling and optimization.
  861. @end itemize
  862. Once this is done, we can make the following simplifications to Guile:
  863. @itemize @bullet
  864. @item
  865. A good portion of symbols.c can go away. Symbols no longer need value
  866. slots. The mismash of @code{scm_sym2ovcell},
  867. @code{scm_intern_obarray_soft}, etc. can go away. @code{intern} becomes
  868. simpler.
  869. @item
  870. Remove first-class variables: @file{variables.c} and @file{variables.h}.
  871. @item
  872. Organize the primitives into environments.
  873. @item
  874. The family of environment types is clearly an abstract class/concrete
  875. subclass arrangement. We should provide GOOPS classes/metaclasses that
  876. make defining new environment types easy and consistent.
  877. @end itemize
  878. @node Modules, , Top-Level Environments in Guile, Top
  879. @chapter Modules
  880. The material here is just a sketch. Don't take it too seriously. The
  881. point is that environments allow us to experiment without getting
  882. tangled up with the interpreter.
  883. @menu
  884. * Modules of Guile Primitives::
  885. * Modules of Interpreted Scheme Code::
  886. @end menu
  887. @node Modules of Guile Primitives, Modules of Interpreted Scheme Code, Modules, Modules
  888. @section Modules of Guile Primitives
  889. @node Modules of Interpreted Scheme Code, , Modules of Guile Primitives, Modules
  890. @section Modules of Interpreted Scheme Code
  891. If a module is implemented by interpreted Scheme code, Guile represents
  892. it using several environments:
  893. @table @asis
  894. @item the @dfn{local} environment
  895. This environment holds all the definitions made locally by the module,
  896. both public and private.
  897. @item the @dfn{import} environment
  898. This environment holds all the definitions this module imports from
  899. other modules.
  900. @item the @dfn{evaluation} environment
  901. This is the environment in which the module's code is actually
  902. evaluated, and the one closed over by the module's procedures, both
  903. public and private. Its bindings are the union of the @var{local} and
  904. @var{import} environments, with local bindings taking precedence.
  905. @item the @dfn{exported} environment
  906. This environment holds the module's public definitions. This is the
  907. only environment that the module's users have access to. It is the
  908. @var{evaluation} environment, restricted to the set of exported
  909. definitions.
  910. @end table
  911. Each of these environments is implemented using a separate environment
  912. type. Some of these types, like the evaluation and import environments,
  913. actually just compute their bindings by consulting other environments;
  914. they have no bindings in their own right. They implement operations
  915. like @code{environment-ref} and @code{environment-define} by passing
  916. them through to the environments from which they are derived. For
  917. example, the evaluation environment will pass definitions through to the
  918. local environment, and search for references and assignments first in
  919. the local environment, and then in the import environment.
  920. @bye