api-evaluation.texi 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325
  1. @c -*-texinfo-*-
  2. @c This is part of the GNU Guile Reference Manual.
  3. @c Copyright (C) 1996, 1997, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2009,
  4. @c 2010, 2011, 2012, 2013, 2014 Free Software Foundation, Inc.
  5. @c See the file guile.texi for copying conditions.
  6. @node Read/Load/Eval/Compile
  7. @section Reading and Evaluating Scheme Code
  8. This chapter describes Guile functions that are concerned with reading,
  9. loading, evaluating, and compiling Scheme code at run time.
  10. @menu
  11. * Scheme Syntax:: Standard and extended Scheme syntax.
  12. * Scheme Read:: Reading Scheme code.
  13. * Scheme Write:: Writing Scheme values to a port.
  14. * Fly Evaluation:: Procedures for on the fly evaluation.
  15. * Compilation:: How to compile Scheme files and procedures.
  16. * Loading:: Loading Scheme code from file.
  17. * Load Paths:: Where Guile looks for code.
  18. * Character Encoding of Source Files:: Loading non-ASCII Scheme code from file.
  19. * Delayed Evaluation:: Postponing evaluation until it is needed.
  20. * Local Evaluation:: Evaluation in a local lexical environment.
  21. * Local Inclusion:: Compile-time inclusion of one file in another.
  22. * REPL Servers:: Serving a REPL over a socket.
  23. * Cooperative REPL Servers:: REPL server for single-threaded applications.
  24. @end menu
  25. @node Scheme Syntax
  26. @subsection Scheme Syntax: Standard and Guile Extensions
  27. @menu
  28. * Expression Syntax::
  29. * Comments::
  30. * Block Comments::
  31. * Case Sensitivity::
  32. * Keyword Syntax::
  33. * Reader Extensions::
  34. @end menu
  35. @node Expression Syntax
  36. @subsubsection Expression Syntax
  37. An expression to be evaluated takes one of the following forms.
  38. @table @nicode
  39. @item @var{symbol}
  40. A symbol is evaluated by dereferencing. A binding of that symbol is
  41. sought and the value there used. For example,
  42. @example
  43. (define x 123)
  44. x @result{} 123
  45. @end example
  46. @item (@var{proc} @var{args}@dots{})
  47. A parenthesised expression is a function call. @var{proc} and each
  48. argument are evaluated, then the function (which @var{proc} evaluated
  49. to) is called with those arguments.
  50. The order in which @var{proc} and the arguments are evaluated is
  51. unspecified, so be careful when using expressions with side effects.
  52. @example
  53. (max 1 2 3) @result{} 3
  54. (define (get-some-proc) min)
  55. ((get-some-proc) 1 2 3) @result{} 1
  56. @end example
  57. The same sort of parenthesised form is used for a macro invocation,
  58. but in that case the arguments are not evaluated. See the
  59. descriptions of macros for more on this (@pxref{Macros}, and
  60. @pxref{Syntax Rules}).
  61. @item @var{constant}
  62. Number, string, character and boolean constants evaluate ``to
  63. themselves'', so can appear as literals.
  64. @example
  65. 123 @result{} 123
  66. 99.9 @result{} 99.9
  67. "hello" @result{} "hello"
  68. #\z @result{} #\z
  69. #t @result{} #t
  70. @end example
  71. Note that an application must not attempt to modify literal strings,
  72. since they may be in read-only memory.
  73. @item (quote @var{data})
  74. @itemx '@var{data}
  75. @findex quote
  76. @findex '
  77. Quoting is used to obtain a literal symbol (instead of a variable
  78. reference), a literal list (instead of a function call), or a literal
  79. vector. @nicode{'} is simply a shorthand for a @code{quote} form.
  80. For example,
  81. @example
  82. 'x @result{} x
  83. '(1 2 3) @result{} (1 2 3)
  84. '#(1 (2 3) 4) @result{} #(1 (2 3) 4)
  85. (quote x) @result{} x
  86. (quote (1 2 3)) @result{} (1 2 3)
  87. (quote #(1 (2 3) 4)) @result{} #(1 (2 3) 4)
  88. @end example
  89. Note that an application must not attempt to modify literal lists or
  90. vectors obtained from a @code{quote} form, since they may be in
  91. read-only memory.
  92. @item (quasiquote @var{data})
  93. @itemx `@var{data}
  94. @findex quasiquote
  95. @findex `
  96. Backquote quasi-quotation is like @code{quote}, but selected
  97. sub-expressions are evaluated. This is a convenient way to construct
  98. a list or vector structure most of which is constant, but at certain
  99. points should have expressions substituted.
  100. The same effect can always be had with suitable @code{list},
  101. @code{cons} or @code{vector} calls, but quasi-quoting is often easier.
  102. @table @nicode
  103. @item (unquote @var{expr})
  104. @itemx ,@var{expr}
  105. @findex unquote
  106. @findex ,
  107. Within the quasiquote @var{data}, @code{unquote} or @code{,} indicates
  108. an expression to be evaluated and inserted. The comma syntax @code{,}
  109. is simply a shorthand for an @code{unquote} form. For example,
  110. @example
  111. `(1 2 ,(* 9 9) 3 4) @result{} (1 2 81 3 4)
  112. `(1 (unquote (+ 1 1)) 3) @result{} (1 2 3)
  113. `#(1 ,(/ 12 2)) @result{} #(1 6)
  114. @end example
  115. @item (unquote-splicing @var{expr})
  116. @itemx ,@@@var{expr}
  117. @findex unquote-splicing
  118. @findex ,@@
  119. Within the quasiquote @var{data}, @code{unquote-splicing} or
  120. @code{,@@} indicates an expression to be evaluated and the elements of
  121. the returned list inserted. @var{expr} must evaluate to a list. The
  122. ``comma-at'' syntax @code{,@@} is simply a shorthand for an
  123. @code{unquote-splicing} form.
  124. @example
  125. (define x '(2 3))
  126. `(1 ,@@x 4) @result{} (1 2 3 4)
  127. `(1 (unquote-splicing (map 1+ x))) @result{} (1 3 4)
  128. `#(9 ,@@x 9) @result{} #(9 2 3 9)
  129. @end example
  130. Notice @code{,@@} differs from plain @code{,} in the way one level of
  131. nesting is stripped. For @code{,@@} the elements of a returned list
  132. are inserted, whereas with @code{,} it would be the list itself
  133. inserted.
  134. @end table
  135. @c
  136. @c FIXME: What can we say about the mutability of a quasiquote
  137. @c result? R5RS doesn't seem to specify anything, though where it
  138. @c says backquote without commas is the same as plain quote then
  139. @c presumably the "fixed" portions of a quasiquote expression must be
  140. @c treated as immutable.
  141. @c
  142. @end table
  143. @node Comments
  144. @subsubsection Comments
  145. @c FIXME::martin: Review me!
  146. Comments in Scheme source files are written by starting them with a
  147. semicolon character (@code{;}). The comment then reaches up to the end
  148. of the line. Comments can begin at any column, and the may be inserted
  149. on the same line as Scheme code.
  150. @lisp
  151. ; Comment
  152. ;; Comment too
  153. (define x 1) ; Comment after expression
  154. (let ((y 1))
  155. ;; Display something.
  156. (display y)
  157. ;;; Comment at left margin.
  158. (display (+ y 1)))
  159. @end lisp
  160. It is common to use a single semicolon for comments following
  161. expressions on a line, to use two semicolons for comments which are
  162. indented like code, and three semicolons for comments which start at
  163. column 0, even if they are inside an indented code block. This
  164. convention is used when indenting code in Emacs' Scheme mode.
  165. @node Block Comments
  166. @subsubsection Block Comments
  167. @cindex multiline comments
  168. @cindex block comments
  169. @cindex #!
  170. @cindex !#
  171. @c FIXME::martin: Review me!
  172. In addition to the standard line comments defined by R5RS, Guile has
  173. another comment type for multiline comments, called @dfn{block
  174. comments}. This type of comment begins with the character sequence
  175. @code{#!} and ends with the characters @code{!#}.
  176. These comments are compatible with the block
  177. comments in the Scheme Shell @file{scsh} (@pxref{The Scheme shell
  178. (scsh)}). The characters @code{#!} were chosen because they are the
  179. magic characters used in shell scripts for indicating that the name of
  180. the program for executing the script follows on the same line.
  181. Thus a Guile script often starts like this.
  182. @lisp
  183. #! /usr/local/bin/guile -s
  184. !#
  185. @end lisp
  186. More details on Guile scripting can be found in the scripting section
  187. (@pxref{Guile Scripting}).
  188. @cindex R6RS block comments
  189. @cindex SRFI-30 block comments
  190. Similarly, Guile (starting from version 2.0) supports nested block
  191. comments as specified by R6RS and
  192. @url{http://srfi.schemers.org/srfi-30/srfi-30.html, SRFI-30}:
  193. @lisp
  194. (+ 1 #| this is a #| nested |# block comment |# 2)
  195. @result{} 3
  196. @end lisp
  197. For backward compatibility, this syntax can be overridden with
  198. @code{read-hash-extend} (@pxref{Reader Extensions,
  199. @code{read-hash-extend}}).
  200. There is one special case where the contents of a comment can actually
  201. affect the interpretation of code. When a character encoding
  202. declaration, such as @code{coding: utf-8} appears in one of the first
  203. few lines of a source file, it indicates to Guile's default reader
  204. that this source code file is not ASCII. For details see @ref{Character
  205. Encoding of Source Files}.
  206. @node Case Sensitivity
  207. @subsubsection Case Sensitivity
  208. @cindex fold-case
  209. @cindex no-fold-case
  210. @c FIXME::martin: Review me!
  211. Scheme as defined in R5RS is not case sensitive when reading symbols.
  212. Guile, on the contrary is case sensitive by default, so the identifiers
  213. @lisp
  214. guile-whuzzy
  215. Guile-Whuzzy
  216. @end lisp
  217. are the same in R5RS Scheme, but are different in Guile.
  218. It is possible to turn off case sensitivity in Guile by setting the
  219. reader option @code{case-insensitive}. For more information on reader
  220. options, @xref{Scheme Read}.
  221. @lisp
  222. (read-enable 'case-insensitive)
  223. @end lisp
  224. It is also possible to disable (or enable) case sensitivity within a
  225. single file by placing the reader directives @code{#!fold-case} (or
  226. @code{#!no-fold-case}) within the file itself.
  227. @node Keyword Syntax
  228. @subsubsection Keyword Syntax
  229. @node Reader Extensions
  230. @subsubsection Reader Extensions
  231. @deffn {Scheme Procedure} read-hash-extend chr proc
  232. @deffnx {C Function} scm_read_hash_extend (chr, proc)
  233. Install the procedure @var{proc} for reading expressions
  234. starting with the character sequence @code{#} and @var{chr}.
  235. @var{proc} will be called with two arguments: the character
  236. @var{chr} and the port to read further data from. The object
  237. returned will be the return value of @code{read}.
  238. Passing @code{#f} for @var{proc} will remove a previous setting.
  239. @end deffn
  240. @node Scheme Read
  241. @subsection Reading Scheme Code
  242. @rnindex read
  243. @deffn {Scheme Procedure} read [port]
  244. @deffnx {C Function} scm_read (port)
  245. Read an s-expression from the input port @var{port}, or from
  246. the current input port if @var{port} is not specified.
  247. Any whitespace before the next token is discarded.
  248. @end deffn
  249. The behaviour of Guile's Scheme reader can be modified by manipulating
  250. its read options.
  251. @cindex options - read
  252. @cindex read options
  253. @deffn {Scheme Procedure} read-options [setting]
  254. Display the current settings of the global read options. If
  255. @var{setting} is omitted, only a short form of the current read options
  256. is printed. Otherwise if @var{setting} is the symbol @code{help}, a
  257. complete options description is displayed.
  258. @end deffn
  259. The set of available options, and their default values, may be had by
  260. invoking @code{read-options} at the prompt.
  261. @smalllisp
  262. scheme@@(guile-user)> (read-options)
  263. (square-brackets keywords #f positions)
  264. scheme@@(guile-user)> (read-options 'help)
  265. copy no Copy source code expressions.
  266. positions yes Record positions of source code expressions.
  267. case-insensitive no Convert symbols to lower case.
  268. keywords #f Style of keyword recognition: #f, 'prefix or 'postfix.
  269. r6rs-hex-escapes no Use R6RS variable-length character and string hex escapes.
  270. square-brackets yes Treat `[' and `]' as parentheses, for R6RS compatibility.
  271. hungry-eol-escapes no In strings, consume leading whitespace after an
  272. escaped end-of-line.
  273. curly-infix no Support SRFI-105 curly infix expressions.
  274. r7rs-symbols no Support R7RS |...| symbol notation.
  275. @end smalllisp
  276. Note that Guile also includes a preliminary mechanism for setting read
  277. options on a per-port basis. For instance, the @code{case-insensitive}
  278. read option is set (or unset) on the port when the reader encounters the
  279. @code{#!fold-case} or @code{#!no-fold-case} reader directives.
  280. Similarly, the @code{#!curly-infix} reader directive sets the
  281. @code{curly-infix} read option on the port, and
  282. @code{#!curly-infix-and-bracket-lists} sets @code{curly-infix} and
  283. unsets @code{square-brackets} on the port (@pxref{SRFI-105}). There is
  284. currently no other way to access or set the per-port read options.
  285. The boolean options may be toggled with @code{read-enable} and
  286. @code{read-disable}. The non-boolean @code{keywords} option must be set
  287. using @code{read-set!}.
  288. @deffn {Scheme Procedure} read-enable option-name
  289. @deffnx {Scheme Procedure} read-disable option-name
  290. @deffnx {Scheme Syntax} read-set! option-name value
  291. Modify the read options. @code{read-enable} should be used with boolean
  292. options and switches them on, @code{read-disable} switches them off.
  293. @code{read-set!} can be used to set an option to a specific value. Due
  294. to historical oddities, it is a macro that expects an unquoted option
  295. name.
  296. @end deffn
  297. For example, to make @code{read} fold all symbols to their lower case
  298. (perhaps for compatibility with older Scheme code), you can enter:
  299. @lisp
  300. (read-enable 'case-insensitive)
  301. @end lisp
  302. For more information on the effect of the @code{r6rs-hex-escapes} and
  303. @code{hungry-eol-escapes} options, see (@pxref{String Syntax}).
  304. For more information on the @code{r7rs-symbols} option, see
  305. (@pxref{Symbol Read Syntax}).
  306. @node Scheme Write
  307. @subsection Writing Scheme Values
  308. Any scheme value may be written to a port. Not all values may be read
  309. back in (@pxref{Scheme Read}), however.
  310. @rnindex write
  311. @rnindex print
  312. @deffn {Scheme Procedure} write obj [port]
  313. Send a representation of @var{obj} to @var{port} or to the current
  314. output port if not given.
  315. The output is designed to be machine readable, and can be read back
  316. with @code{read} (@pxref{Scheme Read}). Strings are printed in
  317. double quotes, with escapes if necessary, and characters are printed in
  318. @samp{#\} notation.
  319. @end deffn
  320. @rnindex display
  321. @deffn {Scheme Procedure} display obj [port]
  322. Send a representation of @var{obj} to @var{port} or to the current
  323. output port if not given.
  324. The output is designed for human readability, it differs from
  325. @code{write} in that strings are printed without double quotes and
  326. escapes, and characters are printed as per @code{write-char}, not in
  327. @samp{#\} form.
  328. @end deffn
  329. As was the case with the Scheme reader, there are a few options that
  330. affect the behavior of the Scheme printer.
  331. @cindex options - print
  332. @cindex print options
  333. @deffn {Scheme Procedure} print-options [setting]
  334. Display the current settings of the read options. If @var{setting} is
  335. omitted, only a short form of the current read options is
  336. printed. Otherwise if @var{setting} is the symbol @code{help}, a
  337. complete options description is displayed.
  338. @end deffn
  339. The set of available options, and their default values, may be had by
  340. invoking @code{print-options} at the prompt.
  341. @smalllisp
  342. scheme@@(guile-user)> (print-options)
  343. (quote-keywordish-symbols reader highlight-suffix "@}" highlight-prefix "@{")
  344. scheme@@(guile-user)> (print-options 'help)
  345. highlight-prefix @{ The string to print before highlighted values.
  346. highlight-suffix @} The string to print after highlighted values.
  347. quote-keywordish-symbols reader How to print symbols that have a colon
  348. as their first or last character. The
  349. value '#f' does not quote the colons;
  350. '#t' quotes them; 'reader' quotes them
  351. when the reader option 'keywords' is
  352. not '#f'.
  353. escape-newlines yes Render newlines as \n when printing
  354. using `write'.
  355. r7rs-symbols no Escape symbols using R7RS |...| symbol
  356. notation.
  357. @end smalllisp
  358. These options may be modified with the print-set! syntax.
  359. @deffn {Scheme Syntax} print-set! option-name value
  360. Modify the print options. Due to historical oddities, @code{print-set!}
  361. is a macro that expects an unquoted option name.
  362. @end deffn
  363. @node Fly Evaluation
  364. @subsection Procedures for On the Fly Evaluation
  365. Scheme has the lovely property that its expressions may be represented
  366. as data. The @code{eval} procedure takes a Scheme datum and evaluates
  367. it as code.
  368. @rnindex eval
  369. @c ARGFIXME environment/environment specifier
  370. @deffn {Scheme Procedure} eval exp module_or_state
  371. @deffnx {C Function} scm_eval (exp, module_or_state)
  372. Evaluate @var{exp}, a list representing a Scheme expression,
  373. in the top-level environment specified by @var{module_or_state}.
  374. While @var{exp} is evaluated (using @code{primitive-eval}),
  375. @var{module_or_state} is made the current module. The current module
  376. is reset to its previous value when @code{eval} returns.
  377. XXX - dynamic states.
  378. Example: (eval '(+ 1 2) (interaction-environment))
  379. @end deffn
  380. @rnindex interaction-environment
  381. @deffn {Scheme Procedure} interaction-environment
  382. @deffnx {C Function} scm_interaction_environment ()
  383. Return a specifier for the environment that contains
  384. implementation--defined bindings, typically a superset of those
  385. listed in the report. The intent is that this procedure will
  386. return the environment in which the implementation would
  387. evaluate expressions dynamically typed by the user.
  388. @end deffn
  389. @xref{Environments}, for other environments.
  390. One does not always receive code as Scheme data, of course, and this is
  391. especially the case for Guile's other language implementations
  392. (@pxref{Other Languages}). For the case in which all you have is a
  393. string, we have @code{eval-string}. There is a legacy version of this
  394. procedure in the default environment, but you really want the one from
  395. @code{(ice-9 eval-string)}, so load it up:
  396. @example
  397. (use-modules (ice-9 eval-string))
  398. @end example
  399. @deffn {Scheme Procedure} eval-string string [#:module=#f] [#:file=#f] @
  400. [#:line=#f] [#:column=#f] @
  401. [#:lang=(current-language)] @
  402. [#:compile?=#f]
  403. Parse @var{string} according to the current language, normally Scheme.
  404. Evaluate or compile the expressions it contains, in order, returning the
  405. last expression.
  406. If the @var{module} keyword argument is set, save a module excursion
  407. (@pxref{Module System Reflection}) and set the current module to
  408. @var{module} before evaluation.
  409. The @var{file}, @var{line}, and @var{column} keyword arguments can be
  410. used to indicate that the source string begins at a particular source
  411. location.
  412. Finally, @var{lang} is a language, defaulting to the current language,
  413. and the expression is compiled if @var{compile?} is true or there is no
  414. evaluator for the given language.
  415. @end deffn
  416. @deffn {C Function} scm_eval_string (string)
  417. @deffnx {C Function} scm_eval_string_in_module (string, module)
  418. These C bindings call @code{eval-string} from @code{(ice-9
  419. eval-string)}, evaluating within @var{module} or the current module.
  420. @end deffn
  421. @deftypefn {C Function} SCM scm_c_eval_string (const char *string)
  422. @code{scm_eval_string}, but taking a C string in locale encoding instead
  423. of an @code{SCM}.
  424. @end deftypefn
  425. @deffn {Scheme Procedure} apply proc arg @dots{} arglst
  426. @deffnx {C Function} scm_apply_0 (proc, arglst)
  427. @deffnx {C Function} scm_apply_1 (proc, arg1, arglst)
  428. @deffnx {C Function} scm_apply_2 (proc, arg1, arg2, arglst)
  429. @deffnx {C Function} scm_apply_3 (proc, arg1, arg2, arg3, arglst)
  430. @deffnx {C Function} scm_apply (proc, arg, rest)
  431. @rnindex apply
  432. Call @var{proc} with arguments @var{arg} @dots{} and the
  433. elements of the @var{arglst} list.
  434. @code{scm_apply} takes parameters corresponding to a Scheme level
  435. @code{(lambda (proc arg1 . rest) ...)}. So @var{arg1} and all but the
  436. last element of the @var{rest} list make up @var{arg} @dots{}, and the
  437. last element of @var{rest} is the @var{arglst} list. Or if @var{rest}
  438. is the empty list @code{SCM_EOL} then there's no @var{arg} @dots{}, and
  439. (@var{arg1}) is the @var{arglst}.
  440. @var{arglst} is not modified, but the @var{rest} list passed to
  441. @code{scm_apply} is modified.
  442. @end deffn
  443. @deffn {C Function} scm_call_0 (proc)
  444. @deffnx {C Function} scm_call_1 (proc, arg1)
  445. @deffnx {C Function} scm_call_2 (proc, arg1, arg2)
  446. @deffnx {C Function} scm_call_3 (proc, arg1, arg2, arg3)
  447. @deffnx {C Function} scm_call_4 (proc, arg1, arg2, arg3, arg4)
  448. @deffnx {C Function} scm_call_5 (proc, arg1, arg2, arg3, arg4, arg5)
  449. @deffnx {C Function} scm_call_6 (proc, arg1, arg2, arg3, arg4, arg5, arg6)
  450. @deffnx {C Function} scm_call_7 (proc, arg1, arg2, arg3, arg4, arg5, arg6, arg7)
  451. @deffnx {C Function} scm_call_8 (proc, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8)
  452. @deffnx {C Function} scm_call_9 (proc, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9)
  453. Call @var{proc} with the given arguments.
  454. @end deffn
  455. @deffn {C Function} scm_call (proc, ...)
  456. Call @var{proc} with any number of arguments. The argument list must be
  457. terminated by @code{SCM_UNDEFINED}. For example:
  458. @example
  459. scm_call (scm_c_public_ref ("guile", "+"),
  460. scm_from_int (1),
  461. scm_from_int (2),
  462. SCM_UNDEFINED);
  463. @end example
  464. @end deffn
  465. @deffn {C Function} scm_call_n (proc, argv, nargs)
  466. Call @var{proc} with the array of arguments @var{argv}, as a
  467. @code{SCM*}. The length of the arguments should be passed in
  468. @var{nargs}, as a @code{size_t}.
  469. @end deffn
  470. @deffn {Scheme Procedure} primitive-eval exp
  471. @deffnx {C Function} scm_primitive_eval (exp)
  472. Evaluate @var{exp} in the top-level environment specified by
  473. the current module.
  474. @end deffn
  475. @node Compilation
  476. @subsection Compiling Scheme Code
  477. The @code{eval} procedure directly interprets the S-expression
  478. representation of Scheme. An alternate strategy for evaluation is to
  479. determine ahead of time what computations will be necessary to
  480. evaluate the expression, and then use that recipe to produce the
  481. desired results. This is known as @dfn{compilation}.
  482. While it is possible to compile simple Scheme expressions such as
  483. @code{(+ 2 2)} or even @code{"Hello world!"}, compilation is most
  484. interesting in the context of procedures. Compiling a lambda expression
  485. produces a compiled procedure, which is just like a normal procedure
  486. except typically much faster, because it can bypass the generic
  487. interpreter.
  488. Functions from system modules in a Guile installation are normally
  489. compiled already, so they load and run quickly.
  490. @cindex automatic compilation
  491. Note that well-written Scheme programs will not typically call the
  492. procedures in this section, for the same reason that it is often bad
  493. taste to use @code{eval}. By default, Guile automatically compiles any
  494. files it encounters that have not been compiled yet (@pxref{Invoking
  495. Guile, @code{--auto-compile}}). The compiler can also be invoked
  496. explicitly from the shell as @code{guild compile foo.scm}.
  497. (Why are calls to @code{eval} and @code{compile} usually in bad taste?
  498. Because they are limited, in that they can only really make sense for
  499. top-level expressions. Also, most needs for ``compile-time''
  500. computation are fulfilled by macros and closures. Of course one good
  501. counterexample is the REPL itself, or any code that reads expressions
  502. from a port.)
  503. Automatic compilation generally works transparently, without any need
  504. for user intervention. However Guile does not yet do proper dependency
  505. tracking, so that if file @file{@var{a}.scm} uses macros from
  506. @file{@var{b}.scm}, and @var{@var{b}.scm} changes, @code{@var{a}.scm}
  507. would not be automatically recompiled. To forcibly invalidate the
  508. auto-compilation cache, pass the @code{--fresh-auto-compile} option to
  509. Guile, or set the @code{GUILE_AUTO_COMPILE} environment variable to
  510. @code{fresh} (instead of to @code{0} or @code{1}).
  511. For more information on the compiler itself, see @ref{Compiling to the
  512. Virtual Machine}. For information on the virtual machine, see @ref{A
  513. Virtual Machine for Guile}.
  514. The command-line interface to Guile's compiler is the @command{guild
  515. compile} command:
  516. @deffn {Command} {guild compile} [@option{option}...] @var{file}...
  517. Compile @var{file}, a source file, and store bytecode in the compilation cache
  518. or in the file specified by the @option{-o} option. The following options are
  519. available:
  520. @table @option
  521. @item -L @var{dir}
  522. @itemx --load-path=@var{dir}
  523. Add @var{dir} to the front of the module load path.
  524. @item -o @var{ofile}
  525. @itemx --output=@var{ofile}
  526. Write output bytecode to @var{ofile}. By convention, bytecode file
  527. names end in @code{.go}. When @option{-o} is omitted, the output file
  528. name is as for @code{compile-file} (see below).
  529. @item -W @var{warning}
  530. @itemx --warn=@var{warning}
  531. @cindex warnings, compiler
  532. Emit warnings of type @var{warning}; use @code{--warn=help} for a list
  533. of available warnings and their description. Currently recognized
  534. warnings include @code{unused-variable}, @code{unused-toplevel},
  535. @code{unbound-variable}, @code{arity-mismatch}, @code{format},
  536. @code{duplicate-case-datum}, and @code{bad-case-datum}.
  537. @item -f @var{lang}
  538. @itemx --from=@var{lang}
  539. Use @var{lang} as the source language of @var{file}. If this option is omitted,
  540. @code{scheme} is assumed.
  541. @item -t @var{lang}
  542. @itemx --to=@var{lang}
  543. Use @var{lang} as the target language of @var{file}. If this option is omitted,
  544. @code{rtl} is assumed.
  545. @item -T @var{target}
  546. @itemx --target=@var{target}
  547. Produce code for @var{target} instead of @var{%host-type} (@pxref{Build
  548. Config, %host-type}). Target must be a valid GNU triplet, such as
  549. @code{armv5tel-unknown-linux-gnueabi} (@pxref{Specifying Target
  550. Triplets,,, autoconf, GNU Autoconf Manual}).
  551. @end table
  552. Each @var{file} is assumed to be UTF-8-encoded, unless it contains a
  553. coding declaration as recognized by @code{file-encoding}
  554. (@pxref{Character Encoding of Source Files}).
  555. @end deffn
  556. The compiler can also be invoked directly by Scheme code using the procedures
  557. below:
  558. @deffn {Scheme Procedure} compile exp [#:env=#f] @
  559. [#:from=(current-language)] @
  560. [#:to=value] [#:opts=()]
  561. Compile the expression @var{exp} in the environment @var{env}. If
  562. @var{exp} is a procedure, the result will be a compiled procedure;
  563. otherwise @code{compile} is mostly equivalent to @code{eval}.
  564. For a discussion of languages and compiler options, @xref{Compiling to
  565. the Virtual Machine}.
  566. @end deffn
  567. @deffn {Scheme Procedure} compile-file file [#:output-file=#f] @
  568. [#:from=(current-language)] [#:to='rtl] @
  569. [#:env=(default-environment from)] @
  570. [#:opts='()] @
  571. [#:canonicalization='relative]
  572. Compile the file named @var{file}.
  573. Output will be written to a @var{output-file}. If you do not supply an
  574. output file name, output is written to a file in the cache directory, as
  575. computed by @code{(compiled-file-name @var{file})}.
  576. @var{from} and @var{to} specify the source and target languages.
  577. @xref{Compiling to the Virtual Machine}, for more information on these
  578. options, and on @var{env} and @var{opts}.
  579. As with @command{guild compile}, @var{file} is assumed to be
  580. UTF-8-encoded unless it contains a coding declaration.
  581. @end deffn
  582. @deffn {Scheme Procedure} compiled-file-name file
  583. Compute a cached location for a compiled version of a Scheme file named
  584. @var{file}.
  585. This file will usually be below the @file{$HOME/.cache/guile/ccache}
  586. directory, depending on the value of the @env{XDG_CACHE_HOME}
  587. environment variable. The intention is that @code{compiled-file-name}
  588. provides a fallback location for caching auto-compiled files. If you
  589. want to place a compile file in the @code{%load-compiled-path}, you
  590. should pass the @var{output-file} option to @code{compile-file},
  591. explicitly.
  592. @end deffn
  593. @defvr {Scheme Variable} %auto-compilation-options
  594. This variable contains the options passed to the @code{compile-file}
  595. procedure when auto-compiling source files. By default, it enables
  596. useful compilation warnings. It can be customized from @file{~/.guile}.
  597. @end defvr
  598. @node Loading
  599. @subsection Loading Scheme Code from File
  600. @rnindex load
  601. @deffn {Scheme Procedure} load filename [reader]
  602. Load @var{filename} and evaluate its contents in the top-level
  603. environment.
  604. @var{reader} if provided should be either @code{#f}, or a procedure with
  605. the signature @code{(lambda (port) @dots{})} which reads the next
  606. expression from @var{port}. If @var{reader} is @code{#f} or absent,
  607. Guile's built-in @code{read} procedure is used (@pxref{Scheme Read}).
  608. The @var{reader} argument takes effect by setting the value of the
  609. @code{current-reader} fluid (see below) before loading the file, and
  610. restoring its previous value when loading is complete. The Scheme code
  611. inside @var{filename} can itself change the current reader procedure on
  612. the fly by setting @code{current-reader} fluid.
  613. If the variable @code{%load-hook} is defined, it should be bound to a
  614. procedure that will be called before any code is loaded. See
  615. documentation for @code{%load-hook} later in this section.
  616. @end deffn
  617. @deffn {Scheme Procedure} load-compiled filename
  618. Load the compiled file named @var{filename}.
  619. Compiling a source file (@pxref{Read/Load/Eval/Compile}) and then
  620. calling @code{load-compiled} on the resulting file is equivalent to
  621. calling @code{load} on the source file.
  622. @end deffn
  623. @deffn {Scheme Procedure} primitive-load filename
  624. @deffnx {C Function} scm_primitive_load (filename)
  625. Load the file named @var{filename} and evaluate its contents in the
  626. top-level environment. @var{filename} must either be a full pathname or
  627. be a pathname relative to the current directory. If the variable
  628. @code{%load-hook} is defined, it should be bound to a procedure that
  629. will be called before any code is loaded. See the documentation for
  630. @code{%load-hook} later in this section.
  631. @end deffn
  632. @deftypefn {C Function} SCM scm_c_primitive_load (const char *filename)
  633. @code{scm_primitive_load}, but taking a C string instead of an
  634. @code{SCM}.
  635. @end deftypefn
  636. @defvar current-reader
  637. @code{current-reader} holds the read procedure that is currently being
  638. used by the above loading procedures to read expressions (from the file
  639. that they are loading). @code{current-reader} is a fluid, so it has an
  640. independent value in each dynamic root and should be read and set using
  641. @code{fluid-ref} and @code{fluid-set!} (@pxref{Fluids and Dynamic
  642. States}).
  643. Changing @code{current-reader} is typically useful to introduce local
  644. syntactic changes, such that code following the @code{fluid-set!} call
  645. is read using the newly installed reader. The @code{current-reader}
  646. change should take place at evaluation time when the code is evaluated,
  647. or at compilation time when the code is compiled:
  648. @findex eval-when
  649. @example
  650. (eval-when (compile eval)
  651. (fluid-set! current-reader my-own-reader))
  652. @end example
  653. The @code{eval-when} form above ensures that the @code{current-reader}
  654. change occurs at the right time.
  655. @end defvar
  656. @defvar %load-hook
  657. A procedure to be called @code{(%load-hook @var{filename})} whenever a
  658. file is loaded, or @code{#f} for no such call. @code{%load-hook} is
  659. used by all of the loading functions (@code{load} and
  660. @code{primitive-load}, and @code{load-from-path} and
  661. @code{primitive-load-path} documented in the next section).
  662. For example an application can set this to show what's loaded,
  663. @example
  664. (set! %load-hook (lambda (filename)
  665. (format #t "Loading ~a ...\n" filename)))
  666. (load-from-path "foo.scm")
  667. @print{} Loading /usr/local/share/guile/site/foo.scm ...
  668. @end example
  669. @end defvar
  670. @deffn {Scheme Procedure} current-load-port
  671. @deffnx {C Function} scm_current_load_port ()
  672. Return the current-load-port.
  673. The load port is used internally by @code{primitive-load}.
  674. @end deffn
  675. @node Load Paths
  676. @subsection Load Paths
  677. The procedure in the previous section look for Scheme code in the file
  678. system at specific location. Guile also has some procedures to search
  679. the load path for code.
  680. @defvar %load-path
  681. List of directories which should be searched for Scheme modules and
  682. libraries. When Guile starts up, @code{%load-path} is initialized to
  683. the default load path @code{(list (%library-dir) (%site-dir)
  684. (%global-site-dir) (%package-data-dir))}. The @env{GUILE_LOAD_PATH}
  685. environment variable can be used to prepend or append additional
  686. directories (@pxref{Environment Variables}).
  687. @xref{Build Config}, for more on @code{%site-dir} and related
  688. procedures.
  689. @end defvar
  690. @deffn {Scheme Procedure} load-from-path filename
  691. Similar to @code{load}, but searches for @var{filename} in the load
  692. paths. Preferentially loads a compiled version of the file, if it is
  693. available and up-to-date.
  694. @end deffn
  695. A user can extend the load path by calling @code{add-to-load-path}.
  696. @deffn {Scheme Syntax} add-to-load-path dir
  697. Add @var{dir} to the load path.
  698. @end deffn
  699. For example, a script might include this form to add the directory that
  700. it is in to the load path:
  701. @example
  702. (add-to-load-path (dirname (current-filename)))
  703. @end example
  704. It's better to use @code{add-to-load-path} than to modify
  705. @code{%load-path} directly, because @code{add-to-load-path} takes care
  706. of modifying the path both at compile-time and at run-time.
  707. @deffn {Scheme Procedure} primitive-load-path filename [exception-on-not-found]
  708. @deffnx {C Function} scm_primitive_load_path (filename)
  709. Search @code{%load-path} for the file named @var{filename} and
  710. load it into the top-level environment. If @var{filename} is a
  711. relative pathname and is not found in the list of search paths,
  712. an error is signalled. Preferentially loads a compiled version of the
  713. file, if it is available and up-to-date.
  714. If @var{filename} is a relative pathname and is not found in the list of
  715. search paths, one of three things may happen, depending on the optional
  716. second argument, @var{exception-on-not-found}. If it is @code{#f},
  717. @code{#f} will be returned. If it is a procedure, it will be called
  718. with no arguments. (This allows a distinction to be made between
  719. exceptions raised by loading a file, and exceptions related to the
  720. loader itself.) Otherwise an error is signalled.
  721. For compatibility with Guile 1.8 and earlier, the C function takes only
  722. one argument, which can be either a string (the file name) or an
  723. argument list.
  724. @end deffn
  725. @deffn {Scheme Procedure} %search-load-path filename
  726. @deffnx {C Function} scm_sys_search_load_path (filename)
  727. Search @code{%load-path} for the file named @var{filename}, which must
  728. be readable by the current user. If @var{filename} is found in the list
  729. of paths to search or is an absolute pathname, return its full pathname.
  730. Otherwise, return @code{#f}. Filenames may have any of the optional
  731. extensions in the @code{%load-extensions} list; @code{%search-load-path}
  732. will try each extension automatically.
  733. @end deffn
  734. @defvar %load-extensions
  735. A list of default file extensions for files containing Scheme code.
  736. @code{%search-load-path} tries each of these extensions when looking for
  737. a file to load. By default, @code{%load-extensions} is bound to the
  738. list @code{("" ".scm")}.
  739. @end defvar
  740. As mentioned above, when Guile searches the @code{%load-path} for a
  741. source file, it will also search the @code{%load-compiled-path} for a
  742. corresponding compiled file. If the compiled file is as new or newer
  743. than the source file, it will be loaded instead of the source file,
  744. using @code{load-compiled}.
  745. @defvar %load-compiled-path
  746. Like @code{%load-path}, but for compiled files. By default, this path
  747. has two entries: one for compiled files from Guile itself, and one for
  748. site packages. The @env{GUILE_LOAD_COMPILED_PATH} environment variable
  749. can be used to prepend or append additional directories
  750. (@pxref{Environment Variables}).
  751. @end defvar
  752. When @code{primitive-load-path} searches the @code{%load-compiled-path}
  753. for a corresponding compiled file for a relative path it does so by
  754. appending @code{.go} to the relative path. For example, searching for
  755. @code{ice-9/popen} could find
  756. @code{/usr/lib/guile/2.2/ccache/ice-9/popen.go}, and use it instead of
  757. @code{/usr/share/guile/2.2/ice-9/popen.scm}.
  758. If @code{primitive-load-path} does not find a corresponding @code{.go}
  759. file in the @code{%load-compiled-path}, or the @code{.go} file is out of
  760. date, it will search for a corresponding auto-compiled file in the
  761. fallback path, possibly creating one if one does not exist.
  762. @xref{Installing Site Packages}, for more on how to correctly install
  763. site packages. @xref{Modules and the File System}, for more on the
  764. relationship between load paths and modules. @xref{Compilation}, for
  765. more on the fallback path and auto-compilation.
  766. Finally, there are a couple of helper procedures for general path
  767. manipulation.
  768. @deffn {Scheme Procedure} parse-path path [tail]
  769. @deffnx {C Function} scm_parse_path (path, tail)
  770. Parse @var{path}, which is expected to be a colon-separated string, into
  771. a list and return the resulting list with @var{tail} appended. If
  772. @var{path} is @code{#f}, @var{tail} is returned.
  773. @end deffn
  774. @deffn {Scheme Procedure} parse-path-with-ellipsis path base
  775. @deffnx {C Function} scm_parse_path_with_ellipsis (path, base)
  776. Parse @var{path}, which is expected to be a colon-separated string, into
  777. a list and return the resulting list with @var{base} (a list) spliced in
  778. place of the @code{...} path component, if present, or else @var{base}
  779. is added to the end. If @var{path} is @code{#f}, @var{base} is
  780. returned.
  781. @end deffn
  782. @deffn {Scheme Procedure} search-path path filename [extensions [require-exts?]]
  783. @deffnx {C Function} scm_search_path (path, filename, rest)
  784. Search @var{path} for a directory containing a file named
  785. @var{filename}. The file must be readable, and not a directory. If we
  786. find one, return its full filename; otherwise, return @code{#f}. If
  787. @var{filename} is absolute, return it unchanged. If given,
  788. @var{extensions} is a list of strings; for each directory in @var{path},
  789. we search for @var{filename} concatenated with each @var{extension}. If
  790. @var{require-exts?} is true, require that the returned file name have
  791. one of the given extensions; if @var{require-exts?} is not given, it
  792. defaults to @code{#f}.
  793. For compatibility with Guile 1.8 and earlier, the C function takes only
  794. three arguments.
  795. @end deffn
  796. @node Character Encoding of Source Files
  797. @subsection Character Encoding of Source Files
  798. @cindex source file encoding
  799. @cindex primitive-load
  800. @cindex load
  801. Scheme source code files are usually encoded in ASCII or UTF-8, but the
  802. built-in reader can interpret other character encodings as well. When
  803. Guile loads Scheme source code, it uses the @code{file-encoding}
  804. procedure (described below) to try to guess the encoding of the file.
  805. In the absence of any hints, UTF-8 is assumed. One way to provide a
  806. hint about the encoding of a source file is to place a coding
  807. declaration in the top 500 characters of the file.
  808. A coding declaration has the form @code{coding: XXXXXX}, where
  809. @code{XXXXXX} is the name of a character encoding in which the source
  810. code file has been encoded. The coding declaration must appear in a
  811. scheme comment. It can either be a semicolon-initiated comment, or the
  812. first block @code{#!} comment in the file.
  813. The name of the character encoding in the coding declaration is
  814. typically lower case and containing only letters, numbers, and hyphens,
  815. as recognized by @code{set-port-encoding!} (@pxref{Ports,
  816. @code{set-port-encoding!}}). Common examples of character encoding
  817. names are @code{utf-8} and @code{iso-8859-1},
  818. @url{http://www.iana.org/assignments/character-sets, as defined by
  819. IANA}. Thus, the coding declaration is mostly compatible with Emacs.
  820. However, there are some differences in encoding names recognized by
  821. Emacs and encoding names defined by IANA, the latter being essentially a
  822. subset of the former. For instance, @code{latin-1} is a valid encoding
  823. name for Emacs, but it's not according to the IANA standard, which Guile
  824. follows; instead, you should use @code{iso-8859-1}, which is both
  825. understood by Emacs and dubbed by IANA (IANA writes it uppercase but
  826. Emacs wants it lowercase and Guile is case insensitive.)
  827. For source code, only a subset of all possible character encodings can
  828. be interpreted by the built-in source code reader. Only those
  829. character encodings in which ASCII text appears unmodified can be
  830. used. This includes @code{UTF-8} and @code{ISO-8859-1} through
  831. @code{ISO-8859-15}. The multi-byte character encodings @code{UTF-16}
  832. and @code{UTF-32} may not be used because they are not compatible with
  833. ASCII.
  834. @cindex read
  835. @cindex encoding
  836. @cindex port encoding
  837. @findex set-port-encoding!
  838. There might be a scenario in which one would want to read non-ASCII
  839. code from a port, such as with the function @code{read}, instead of
  840. with @code{load}. If the port's character encoding is the same as the
  841. encoding of the code to be read by the port, not other special
  842. handling is necessary. The port will automatically do the character
  843. encoding conversion. The functions @code{setlocale} or by
  844. @code{set-port-encoding!} are used to set port encodings
  845. (@pxref{Ports}).
  846. If a port is used to read code of unknown character encoding, it can
  847. accomplish this in three steps. First, the character encoding of the
  848. port should be set to ISO-8859-1 using @code{set-port-encoding!}.
  849. Then, the procedure @code{file-encoding}, described below, is used to
  850. scan for a coding declaration when reading from the port. As a side
  851. effect, it rewinds the port after its scan is complete. After that,
  852. the port's character encoding should be set to the encoding returned
  853. by @code{file-encoding}, if any, again by using
  854. @code{set-port-encoding!}. Then the code can be read as normal.
  855. Alternatively, one can use the @code{#:guess-encoding} keyword argument
  856. of @code{open-file} and related procedures. @xref{File Ports}.
  857. @deffn {Scheme Procedure} file-encoding port
  858. @deffnx {C Function} scm_file_encoding (port)
  859. Attempt to scan the first few hundred bytes from the @var{port} for
  860. hints about its character encoding. Return a string containing the
  861. encoding name or @code{#f} if the encoding cannot be determined. The
  862. port is rewound.
  863. Currently, the only supported method is to look for an Emacs-like
  864. character coding declaration (@pxref{Recognize Coding, how Emacs
  865. recognizes file encoding,, emacs, The GNU Emacs Reference Manual}). The
  866. coding declaration is of the form @code{coding: XXXXX} and must appear
  867. in a Scheme comment. Additional heuristics may be added in the future.
  868. @end deffn
  869. @node Delayed Evaluation
  870. @subsection Delayed Evaluation
  871. @cindex delayed evaluation
  872. @cindex promises
  873. Promises are a convenient way to defer a calculation until its result
  874. is actually needed, and to run such a calculation only once. Also
  875. @pxref{SRFI-45}.
  876. @deffn syntax delay expr
  877. @rnindex delay
  878. Return a promise object which holds the given @var{expr} expression,
  879. ready to be evaluated by a later @code{force}.
  880. @end deffn
  881. @deffn {Scheme Procedure} promise? obj
  882. @deffnx {C Function} scm_promise_p (obj)
  883. Return true if @var{obj} is a promise.
  884. @end deffn
  885. @rnindex force
  886. @deffn {Scheme Procedure} force p
  887. @deffnx {C Function} scm_force (p)
  888. Return the value obtained from evaluating the @var{expr} in the given
  889. promise @var{p}. If @var{p} has previously been forced then its
  890. @var{expr} is not evaluated again, instead the value obtained at that
  891. time is simply returned.
  892. During a @code{force}, an @var{expr} can call @code{force} again on
  893. its own promise, resulting in a recursive evaluation of that
  894. @var{expr}. The first evaluation to return gives the value for the
  895. promise. Higher evaluations run to completion in the normal way, but
  896. their results are ignored, @code{force} always returns the first
  897. value.
  898. @end deffn
  899. @node Local Evaluation
  900. @subsection Local Evaluation
  901. Guile includes a facility to capture a lexical environment, and later
  902. evaluate a new expression within that environment. This code is
  903. implemented in a module.
  904. @example
  905. (use-modules (ice-9 local-eval))
  906. @end example
  907. @deffn syntax the-environment
  908. Captures and returns a lexical environment for use with
  909. @code{local-eval} or @code{local-compile}.
  910. @end deffn
  911. @deffn {Scheme Procedure} local-eval exp env
  912. @deffnx {C Function} scm_local_eval (exp, env)
  913. @deffnx {Scheme Procedure} local-compile exp env [opts=()]
  914. Evaluate or compile the expression @var{exp} in the lexical environment
  915. @var{env}.
  916. @end deffn
  917. Here is a simple example, illustrating that it is the variable
  918. that gets captured, not just its value at one point in time.
  919. @example
  920. (define e (let ((x 100)) (the-environment)))
  921. (define fetch-x (local-eval '(lambda () x) e))
  922. (fetch-x)
  923. @result{} 100
  924. (local-eval '(set! x 42) e)
  925. (fetch-x)
  926. @result{} 42
  927. @end example
  928. While @var{exp} is evaluated within the lexical environment of
  929. @code{(the-environment)}, it has the dynamic environment of the call to
  930. @code{local-eval}.
  931. @code{local-eval} and @code{local-compile} can only evaluate
  932. expressions, not definitions.
  933. @example
  934. (local-eval '(define foo 42)
  935. (let ((x 100)) (the-environment)))
  936. @result{} syntax error: definition in expression context
  937. @end example
  938. Note that the current implementation of @code{(the-environment)} only
  939. captures ``normal'' lexical bindings, and pattern variables bound by
  940. @code{syntax-case}. It does not currently capture local syntax
  941. transformers bound by @code{let-syntax}, @code{letrec-syntax} or
  942. non-top-level @code{define-syntax} forms. Any attempt to reference such
  943. captured syntactic keywords via @code{local-eval} or
  944. @code{local-compile} produces an error.
  945. @node Local Inclusion
  946. @subsection Local Inclusion
  947. This section has discussed various means of linking Scheme code
  948. together: fundamentally, loading up files at run-time using @code{load}
  949. and @code{load-compiled}. Guile provides another option to compose
  950. parts of programs together at expansion-time instead of at run-time.
  951. @deffn {Scheme Syntax} include file-name
  952. Open @var{file-name}, at expansion-time, and read the Scheme forms that
  953. it contains, splicing them into the location of the @code{include},
  954. within a @code{begin}.
  955. If @var{file-name} is a relative path, it is searched for relative to
  956. the path that contains the file that the @code{include} form appears in.
  957. @end deffn
  958. If you are a C programmer, if @code{load} in Scheme is like
  959. @code{dlopen} in C, consider @code{include} to be like the C
  960. preprocessor's @code{#include}. When you use @code{include}, it is as
  961. if the contents of the included file were typed in instead of the
  962. @code{include} form.
  963. Because the code is included at compile-time, it is available to the
  964. macroexpander. Syntax definitions in the included file are available to
  965. later code in the form in which the @code{include} appears, without the
  966. need for @code{eval-when}. (@xref{Eval When}.)
  967. For the same reason, compiling a form that uses @code{include} results
  968. in one compilation unit, composed of multiple files. Loading the
  969. compiled file is one @code{stat} operation for the compilation unit,
  970. instead of @code{2*@var{n}} in the case of @code{load} (once for each
  971. loaded source file, and once each corresponding compiled file, in the
  972. best case).
  973. Unlike @code{load}, @code{include} also works within nested lexical
  974. contexts. It so happens that the optimizer works best within a lexical
  975. context, because all of the uses of bindings in a lexical context are
  976. visible, so composing files by including them within a @code{(let ()
  977. ...)} can sometimes lead to important speed improvements.
  978. On the other hand, @code{include} does have all the disadvantages of
  979. early binding: once the code with the @code{include} is compiled, no
  980. change to the included file is reflected in the future behavior of the
  981. including form.
  982. Also, the particular form of @code{include}, which requires an absolute
  983. path, or a path relative to the current directory at compile-time, is
  984. not very amenable to compiling the source in one place, but then
  985. installing the source to another place. For this reason, Guile provides
  986. another form, @code{include-from-path}, which looks for the source file
  987. to include within a load path.
  988. @deffn {Scheme Syntax} include-from-path file-name
  989. Like @code{include}, but instead of expecting @code{file-name} to be an
  990. absolute file name, it is expected to be a relative path to search in
  991. the @code{%load-path}.
  992. @end deffn
  993. @code{include-from-path} is more useful when you want to install all of
  994. the source files for a package (as you should!). It makes it possible
  995. to evaluate an installed file from source, instead of relying on the
  996. @code{.go} file being up to date.
  997. @node REPL Servers
  998. @subsection REPL Servers
  999. @cindex REPL server
  1000. The procedures in this section are provided by
  1001. @lisp
  1002. (use-modules (system repl server))
  1003. @end lisp
  1004. When an application is written in Guile, it is often convenient to
  1005. allow the user to be able to interact with it by evaluating Scheme
  1006. expressions in a REPL.
  1007. The procedures of this module allow you to spawn a @dfn{REPL server},
  1008. which permits interaction over a local or TCP connection. Guile itself
  1009. uses them internally to implement the @option{--listen} switch,
  1010. @ref{Command-line Options}.
  1011. @deffn {Scheme Procedure} make-tcp-server-socket [#:host=#f] @
  1012. [#:addr] [#:port=37146]
  1013. Return a stream socket bound to a given address @var{addr} and port
  1014. number @var{port}. If the @var{host} is given, and @var{addr} is not,
  1015. then the @var{host} string is converted to an address. If neither is
  1016. given, we use the loopback address.
  1017. @end deffn
  1018. @deffn {Scheme Procedure} make-unix-domain-server-socket [#:path="/tmp/guile-socket"]
  1019. Return a UNIX domain socket, bound to a given @var{path}.
  1020. @end deffn
  1021. @deffn {Scheme Procedure} run-server [server-socket]
  1022. @deffnx {Scheme Procedure} spawn-server [server-socket]
  1023. Create and run a REPL, making it available over the given
  1024. @var{server-socket}. If @var{server-socket} is not provided, it
  1025. defaults to the socket created by calling @code{make-tcp-server-socket}
  1026. with no arguments.
  1027. @code{run-server} runs the server in the current thread, whereas
  1028. @code{spawn-server} runs the server in a new thread.
  1029. @end deffn
  1030. @deffn {Scheme Procedure} stop-server-and-clients!
  1031. Closes the connection on all running server sockets.
  1032. Please note that in the current implementation, the REPL threads are
  1033. cancelled without unwinding their stacks. If any of them are holding
  1034. mutexes or are within a critical section, the results are unspecified.
  1035. @end deffn
  1036. @node Cooperative REPL Servers
  1037. @subsection Cooperative REPL Servers
  1038. @cindex Cooperative REPL server
  1039. The procedures in this section are provided by
  1040. @lisp
  1041. (use-modules (system repl coop-server))
  1042. @end lisp
  1043. Whereas ordinary REPL servers run in their own threads (@pxref{REPL
  1044. Servers}), sometimes it is more convenient to provide REPLs that run at
  1045. specified times within an existing thread, for example in programs
  1046. utilizing an event loop or in single-threaded programs. This allows for
  1047. safe access and mutation of a program's data structures from the REPL,
  1048. without concern for thread synchronization.
  1049. Although the REPLs are run in the thread that calls
  1050. @code{spawn-coop-repl-server} and @code{poll-coop-repl-server},
  1051. dedicated threads are spawned so that the calling thread is not blocked.
  1052. The spawned threads read input for the REPLs and to listen for new
  1053. connections.
  1054. Cooperative REPL servers must be polled periodically to evaluate any
  1055. pending expressions by calling @code{poll-coop-repl-server} with the
  1056. object returned from @code{spawn-coop-repl-server}. The thread that
  1057. calls @code{poll-coop-repl-server} will be blocked for as long as the
  1058. expression takes to be evaluated or if the debugger is entered.
  1059. @deffn {Scheme Procedure} spawn-coop-repl-server [server-socket]
  1060. Create and return a new cooperative REPL server object, and spawn a new
  1061. thread to listen for connections on @var{server-socket}. Proper
  1062. functioning of the REPL server requires that
  1063. @code{poll-coop-repl-server} be called periodically on the returned
  1064. server object.
  1065. @end deffn
  1066. @deffn {Scheme Procedure} poll-coop-repl-server coop-server
  1067. Poll the cooperative REPL server @var{coop-server} and apply a pending
  1068. operation if there is one, such as evaluating an expression typed at the
  1069. REPL prompt. This procedure must be called from the same thread that
  1070. called @code{spawn-coop-repl-server}.
  1071. @end deffn
  1072. @c Local Variables:
  1073. @c TeX-master: "guile.texi"
  1074. @c End: