api-evaluation.texi 48 KB

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