api-evaluation.texi 51 KB

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