api-evaluation.texi 48 KB

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