api-evaluation.texi 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  1. @c -*-texinfo-*-
  2. @c This is part of the GNU Guile Reference Manual.
  3. @c Copyright (C) 1996, 1997, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2009, 2010, 2011
  4. @c Free Software Foundation, Inc.
  5. @c See the file guile.texi for copying conditions.
  6. @node Read/Load/Eval/Compile
  7. @section Reading and Evaluating Scheme Code
  8. This chapter describes Guile functions that are concerned with reading,
  9. loading, evaluating, and compiling Scheme code at run time.
  10. @menu
  11. * Scheme Syntax:: Standard and extended Scheme syntax.
  12. * Scheme Read:: Reading Scheme code.
  13. * Scheme Write:: Writing Scheme values to a port.
  14. * Fly Evaluation:: Procedures for on the fly evaluation.
  15. * Compilation:: How to compile Scheme files and procedures.
  16. * Loading:: Loading Scheme code from file.
  17. * Character Encoding of Source Files:: Loading non-ASCII Scheme code from file.
  18. * Delayed Evaluation:: Postponing evaluation until it is needed.
  19. @end menu
  20. @node Scheme Syntax
  21. @subsection Scheme Syntax: Standard and Guile Extensions
  22. @menu
  23. * Expression Syntax::
  24. * Comments::
  25. * Block Comments::
  26. * Case Sensitivity::
  27. * Keyword Syntax::
  28. * Reader Extensions::
  29. @end menu
  30. @node Expression Syntax
  31. @subsubsection Expression Syntax
  32. An expression to be evaluated takes one of the following forms.
  33. @table @nicode
  34. @item @var{symbol}
  35. A symbol is evaluated by dereferencing. A binding of that symbol is
  36. sought and the value there used. For example,
  37. @example
  38. (define x 123)
  39. x @result{} 123
  40. @end example
  41. @item (@var{proc} @var{args}@dots{})
  42. A parenthesised expression is a function call. @var{proc} and each
  43. argument are evaluated, then the function (which @var{proc} evaluated
  44. to) is called with those arguments.
  45. The order in which @var{proc} and the arguments are evaluated is
  46. unspecified, so be careful when using expressions with side effects.
  47. @example
  48. (max 1 2 3) @result{} 3
  49. (define (get-some-proc) min)
  50. ((get-some-proc) 1 2 3) @result{} 1
  51. @end example
  52. The same sort of parenthesised form is used for a macro invocation,
  53. but in that case the arguments are not evaluated. See the
  54. descriptions of macros for more on this (@pxref{Macros}, and
  55. @pxref{Syntax Rules}).
  56. @item @var{constant}
  57. Number, string, character and boolean constants evaluate ``to
  58. themselves'', so can appear as literals.
  59. @example
  60. 123 @result{} 123
  61. 99.9 @result{} 99.9
  62. "hello" @result{} "hello"
  63. #\z @result{} #\z
  64. #t @result{} #t
  65. @end example
  66. Note that an application must not attempt to modify literal strings,
  67. since they may be in read-only memory.
  68. @item (quote @var{data})
  69. @itemx '@var{data}
  70. @findex quote
  71. @findex '
  72. Quoting is used to obtain a literal symbol (instead of a variable
  73. reference), a literal list (instead of a function call), or a literal
  74. vector. @nicode{'} is simply a shorthand for a @code{quote} form.
  75. For example,
  76. @example
  77. 'x @result{} x
  78. '(1 2 3) @result{} (1 2 3)
  79. '#(1 (2 3) 4) @result{} #(1 (2 3) 4)
  80. (quote x) @result{} x
  81. (quote (1 2 3)) @result{} (1 2 3)
  82. (quote #(1 (2 3) 4)) @result{} #(1 (2 3) 4)
  83. @end example
  84. Note that an application must not attempt to modify literal lists or
  85. vectors obtained from a @code{quote} form, since they may be in
  86. read-only memory.
  87. @item (quasiquote @var{data})
  88. @itemx `@var{data}
  89. @findex quasiquote
  90. @findex `
  91. Backquote quasi-quotation is like @code{quote}, but selected
  92. sub-expressions are evaluated. This is a convenient way to construct
  93. a list or vector structure most of which is constant, but at certain
  94. points should have expressions substituted.
  95. The same effect can always be had with suitable @code{list},
  96. @code{cons} or @code{vector} calls, but quasi-quoting is often easier.
  97. @table @nicode
  98. @item (unquote @var{expr})
  99. @itemx ,@var{expr}
  100. @findex unquote
  101. @findex ,
  102. Within the quasiquote @var{data}, @code{unquote} or @code{,} indicates
  103. an expression to be evaluated and inserted. The comma syntax @code{,}
  104. is simply a shorthand for an @code{unquote} form. For example,
  105. @example
  106. `(1 2 ,(* 9 9) 3 4) @result{} (1 2 81 3 4)
  107. `(1 (unquote (+ 1 1)) 3) @result{} (1 2 3)
  108. `#(1 ,(/ 12 2)) @result{} #(1 6)
  109. @end example
  110. @item (unquote-splicing @var{expr})
  111. @itemx ,@@@var{expr}
  112. @findex unquote-splicing
  113. @findex ,@@
  114. Within the quasiquote @var{data}, @code{unquote-splicing} or
  115. @code{,@@} indicates an expression to be evaluated and the elements of
  116. the returned list inserted. @var{expr} must evaluate to a list. The
  117. ``comma-at'' syntax @code{,@@} is simply a shorthand for an
  118. @code{unquote-splicing} form.
  119. @example
  120. (define x '(2 3))
  121. `(1 ,@@x 4) @result{} (1 2 3 4)
  122. `(1 (unquote-splicing (map 1+ x))) @result{} (1 3 4)
  123. `#(9 ,@@x 9) @result{} #(9 2 3 9)
  124. @end example
  125. Notice @code{,@@} differs from plain @code{,} in the way one level of
  126. nesting is stripped. For @code{,@@} the elements of a returned list
  127. are inserted, whereas with @code{,} it would be the list itself
  128. inserted.
  129. @end table
  130. @c
  131. @c FIXME: What can we say about the mutability of a quasiquote
  132. @c result? R5RS doesn't seem to specify anything, though where it
  133. @c says backquote without commas is the same as plain quote then
  134. @c presumably the "fixed" portions of a quasiquote expression must be
  135. @c treated as immutable.
  136. @c
  137. @end table
  138. @node Comments
  139. @subsubsection Comments
  140. @c FIXME::martin: Review me!
  141. Comments in Scheme source files are written by starting them with a
  142. semicolon character (@code{;}). The comment then reaches up to the end
  143. of the line. Comments can begin at any column, and the may be inserted
  144. on the same line as Scheme code.
  145. @lisp
  146. ; Comment
  147. ;; Comment too
  148. (define x 1) ; Comment after expression
  149. (let ((y 1))
  150. ;; Display something.
  151. (display y)
  152. ;;; Comment at left margin.
  153. (display (+ y 1)))
  154. @end lisp
  155. It is common to use a single semicolon for comments following
  156. expressions on a line, to use two semicolons for comments which are
  157. indented like code, and three semicolons for comments which start at
  158. column 0, even if they are inside an indented code block. This
  159. convention is used when indenting code in Emacs' Scheme mode.
  160. @node Block Comments
  161. @subsubsection Block Comments
  162. @cindex multiline comments
  163. @cindex block comments
  164. @cindex #!
  165. @cindex !#
  166. @c FIXME::martin: Review me!
  167. In addition to the standard line comments defined by R5RS, Guile has
  168. another comment type for multiline comments, called @dfn{block
  169. comments}. This type of comment begins with the character sequence
  170. @code{#!} and ends with the characters @code{!#}, which must appear on a
  171. line of their own. These comments are compatible with the block
  172. comments in the Scheme Shell @file{scsh} (@pxref{The Scheme shell
  173. (scsh)}). The characters @code{#!} were chosen because they are the
  174. magic characters used in shell scripts for indicating that the name of
  175. the program for executing the script follows on the same line.
  176. Thus a Guile script often starts like this.
  177. @lisp
  178. #! /usr/local/bin/guile -s
  179. !#
  180. @end lisp
  181. More details on Guile scripting can be found in the scripting section
  182. (@pxref{Guile Scripting}).
  183. @cindex R6RS block comments
  184. @cindex SRFI-30 block comments
  185. Similarly, Guile (starting from version 2.0) supports nested block
  186. comments as specified by R6RS and
  187. @url{http://srfi.schemers.org/srfi-30/srfi-30.html, SRFI-30}:
  188. @lisp
  189. (+ #| this is a #| nested |# block comment |# 2)
  190. @result{} 3
  191. @end lisp
  192. For backward compatibility, this syntax can be overridden with
  193. @code{read-hash-extend} (@pxref{Reader Extensions,
  194. @code{read-hash-extend}}).
  195. There is one special case where the contents of a comment can actually
  196. affect the interpretation of code. When a character encoding
  197. declaration, such as @code{coding: utf-8} appears in one of the first
  198. few lines of a source file, it indicates to Guile's default reader
  199. that this source code file is not ASCII. For details see @ref{Character
  200. Encoding of Source Files}.
  201. @node Case Sensitivity
  202. @subsubsection Case Sensitivity
  203. @c FIXME::martin: Review me!
  204. Scheme as defined in R5RS is not case sensitive when reading symbols.
  205. Guile, on the contrary is case sensitive by default, so the identifiers
  206. @lisp
  207. guile-whuzzy
  208. Guile-Whuzzy
  209. @end lisp
  210. are the same in R5RS Scheme, but are different in Guile.
  211. It is possible to turn off case sensitivity in Guile by setting the
  212. reader option @code{case-insensitive}. For more information on reader
  213. options, @xref{Scheme Read}.
  214. @lisp
  215. (read-enable 'case-insensitive)
  216. @end lisp
  217. Note that this is seldom a problem, because Scheme programmers tend not
  218. to use uppercase letters in their identifiers anyway.
  219. @node Keyword Syntax
  220. @subsubsection Keyword Syntax
  221. @node Reader Extensions
  222. @subsubsection Reader Extensions
  223. @deffn {Scheme Procedure} read-hash-extend chr proc
  224. @deffnx {C Function} scm_read_hash_extend (chr, proc)
  225. Install the procedure @var{proc} for reading expressions
  226. starting with the character sequence @code{#} and @var{chr}.
  227. @var{proc} will be called with two arguments: the character
  228. @var{chr} and the port to read further data from. The object
  229. returned will be the return value of @code{read}.
  230. Passing @code{#f} for @var{proc} will remove a previous setting.
  231. @end deffn
  232. @node Scheme Read
  233. @subsection Reading Scheme Code
  234. @rnindex read
  235. @deffn {Scheme Procedure} read [port]
  236. @deffnx {C Function} scm_read (port)
  237. Read an s-expression from the input port @var{port}, or from
  238. the current input port if @var{port} is not specified.
  239. Any whitespace before the next token is discarded.
  240. @end deffn
  241. The behaviour of Guile's Scheme reader can be modified by manipulating
  242. its read options.
  243. @cindex options - read
  244. @cindex read options
  245. @deffn {Scheme Procedure} read-options [setting]
  246. Display the current settings of the read options. If @var{setting} is
  247. omitted, only a short form of the current read options is printed.
  248. Otherwise if @var{setting} is the symbol @code{help}, a complete options
  249. description is displayed.
  250. @end deffn
  251. The set of available options, and their default values, may be had by
  252. invoking @code{read-options} at the prompt.
  253. @smalllisp
  254. scheme@@(guile-user)> (read-options)
  255. (square-brackets keywords #f positions)
  256. scheme@@(guile-user)> (read-options 'help)
  257. copy no Copy source code expressions.
  258. positions yes Record positions of source code expressions.
  259. case-insensitive no Convert symbols to lower case.
  260. keywords #f Style of keyword recognition: #f, 'prefix or 'postfix.
  261. r6rs-hex-escapes no Use R6RS variable-length character and string hex escapes.
  262. square-brackets yes Treat `[' and `]' as parentheses, for R6RS compatibility.
  263. hungry-eol-escapes no In strings, consume leading whitespace after an
  264. escaped end-of-line.
  265. @end smalllisp
  266. The boolean options may be toggled with @code{read-enable} and
  267. @code{read-disable}. The non-boolean @code{keywords} option must be set
  268. using @code{read-set!}.
  269. @deffn {Scheme Procedure} read-enable option-name
  270. @deffnx {Scheme Procedure} read-disable option-name
  271. @deffnx {Scheme Syntax} read-set! option-name value
  272. Modify the read options. @code{read-enable} should be used with boolean
  273. options and switches them on, @code{read-disable} switches them off.
  274. @code{read-set!} can be used to set an option to a specific value. Due
  275. to historical oddities, it is a macro that expects an unquoted option
  276. name.
  277. @end deffn
  278. For example, to make @code{read} fold all symbols to their lower case
  279. (perhaps for compatibility with older Scheme code), you can enter:
  280. @lisp
  281. (read-enable 'case-insensitive)
  282. @end lisp
  283. For more information on the effect of the @code{r6rs-hex-escapes} and
  284. @code{hungry-eol-escapes} options, see (@pxref{String Syntax}).
  285. @node Scheme Write
  286. @subsection Writing Scheme Values
  287. Any scheme value may be written to a port. Not all values may be read
  288. back in (@pxref{Scheme Read}), however.
  289. @rnindex write
  290. @rnindex print
  291. @deffn {Scheme Procedure} write obj [port]
  292. Send a representation of @var{obj} to @var{port} or to the current
  293. output port if not given.
  294. The output is designed to be machine readable, and can be read back
  295. with @code{read} (@pxref{Scheme Read}). Strings are printed in
  296. double quotes, with escapes if necessary, and characters are printed in
  297. @samp{#\} notation.
  298. @end deffn
  299. @rnindex display
  300. @deffn {Scheme Procedure} display obj [port]
  301. Send a representation of @var{obj} to @var{port} or to the current
  302. output port if not given.
  303. The output is designed for human readability, it differs from
  304. @code{write} in that strings are printed without double quotes and
  305. escapes, and characters are printed as per @code{write-char}, not in
  306. @samp{#\} form.
  307. @end deffn
  308. As was the case with the Scheme reader, there are a few options that
  309. affect the behavior of the Scheme printer.
  310. @cindex options - print
  311. @cindex print options
  312. @deffn {Scheme Procedure} print-options [setting]
  313. Display the current settings of the read options. If @var{setting} is
  314. omitted, only a short form of the current read options is
  315. printed. Otherwise if @var{setting} is the symbol @code{help}, a
  316. complete options description is displayed.
  317. @end deffn
  318. The set of available options, and their default values, may be had by
  319. invoking @code{print-options} at the prompt.
  320. @smalllisp
  321. scheme@@(guile-user)> (print-options)
  322. (quote-keywordish-symbols reader highlight-suffix "@}" highlight-prefix "@{")
  323. scheme@@(guile-user)> (print-options 'help)
  324. highlight-prefix @{ The string to print before highlighted values.
  325. highlight-suffix @} The string to print after highlighted values.
  326. quote-keywordish-symbols reader How to print symbols that have a colon
  327. as their first or last character. The
  328. value '#f' does not quote the colons;
  329. '#t' quotes them; 'reader' quotes them
  330. when the reader option 'keywords' is
  331. not '#f'.
  332. @end smalllisp
  333. These options may be modified with the print-set! syntax.
  334. @deffn {Scheme Syntax} print-set! option-name value
  335. Modify the print options. Due to historical oddities, @code{print-set!}
  336. is a macro that expects an unquoted option name.
  337. @end deffn
  338. @node Fly Evaluation
  339. @subsection Procedures for On the Fly Evaluation
  340. Scheme has the lovely property that its expressions may be represented
  341. as data. The @code{eval} procedure takes a Scheme datum and evaluates
  342. it as code.
  343. @rnindex eval
  344. @c ARGFIXME environment/environment specifier
  345. @deffn {Scheme Procedure} eval exp module_or_state
  346. @deffnx {C Function} scm_eval (exp, module_or_state)
  347. Evaluate @var{exp}, a list representing a Scheme expression,
  348. in the top-level environment specified by @var{module}.
  349. While @var{exp} is evaluated (using @code{primitive-eval}),
  350. @var{module} is made the current module. The current module
  351. is reset to its previous value when @var{eval} returns.
  352. XXX - dynamic states.
  353. Example: (eval '(+ 1 2) (interaction-environment))
  354. @end deffn
  355. @rnindex interaction-environment
  356. @deffn {Scheme Procedure} interaction-environment
  357. @deffnx {C Function} scm_interaction_environment ()
  358. Return a specifier for the environment that contains
  359. implementation--defined bindings, typically a superset of those
  360. listed in the report. The intent is that this procedure will
  361. return the environment in which the implementation would
  362. evaluate expressions dynamically typed by the user.
  363. @end deffn
  364. @xref{Environments}, for other environments.
  365. One does not always receive code as Scheme data, of course, and this is
  366. especially the case for Guile's other language implementations
  367. (@pxref{Other Languages}). For the case in which all you have is a
  368. string, we have @code{eval-string}. There is a legacy version of this
  369. procedure in the default environment, but you really want the one from
  370. @code{(ice-9 eval-string)}, so load it up:
  371. @example
  372. (use-modules (ice-9 eval-string))
  373. @end example
  374. @deffn {Scheme Procedure} eval-string string [module=#f] [file=#f] [line=#f] [column=#f] [lang=(current-language)] [compile?=#f]
  375. Parse @var{string} according to the current language, normally Scheme.
  376. Evaluate or compile the expressions it contains, in order, returning the
  377. last expression.
  378. If the @var{module} keyword argument is set, save a module excursion
  379. (@pxref{Module System Reflection}) and set the current module to
  380. @var{module} before evaluation.
  381. The @var{file}, @var{line}, and @var{column} keyword arguments can be
  382. used to indicate that the source string begins at a particular source
  383. location.
  384. Finally, @var{lang} is a language, defaulting to the current language,
  385. and the expression is compiled if @var{compile?} is true or there is no
  386. evaluator for the given language.
  387. @end deffn
  388. @deffn {C Function} scm_eval_string (string)
  389. @deffnx {C Function} scm_eval_string_in_module (string, module)
  390. These C bindings call @code{eval-string} from @code{(ice-9
  391. eval-string)}, evaluating within @var{module} or the current module.
  392. @end deffn
  393. @deftypefn {C Function} SCM scm_c_eval_string (const char *string)
  394. @code{scm_eval_string}, but taking a C string in locale encoding instead
  395. of an @code{SCM}.
  396. @end deftypefn
  397. @deffn {Scheme Procedure} apply proc arg1 @dots{} argN arglst
  398. @deffnx {C Function} scm_apply_0 (proc, arglst)
  399. @deffnx {C Function} scm_apply_1 (proc, arg1, arglst)
  400. @deffnx {C Function} scm_apply_2 (proc, arg1, arg2, arglst)
  401. @deffnx {C Function} scm_apply_3 (proc, arg1, arg2, arg3, arglst)
  402. @deffnx {C Function} scm_apply (proc, arg, rest)
  403. @rnindex apply
  404. Call @var{proc} with arguments @var{arg1} @dots{} @var{argN} plus the
  405. elements of the @var{arglst} list.
  406. @code{scm_apply} takes parameters corresponding to a Scheme level
  407. @code{(lambda (proc arg . rest) ...)}. So @var{arg} and all but the
  408. last element of the @var{rest} list make up
  409. @var{arg1}@dots{}@var{argN} and the last element of @var{rest} is the
  410. @var{arglst} list. Or if @var{rest} is the empty list @code{SCM_EOL}
  411. then there's no @var{arg1}@dots{}@var{argN} and @var{arg} is the
  412. @var{arglst}.
  413. @var{arglst} is not modified, but the @var{rest} list passed to
  414. @code{scm_apply} is modified.
  415. @end deffn
  416. @deffn {C Function} scm_call_0 (proc)
  417. @deffnx {C Function} scm_call_1 (proc, arg1)
  418. @deffnx {C Function} scm_call_2 (proc, arg1, arg2)
  419. @deffnx {C Function} scm_call_3 (proc, arg1, arg2, arg3)
  420. @deffnx {C Function} scm_call_4 (proc, arg1, arg2, arg3, arg4)
  421. @deffnx {C Function} scm_call_5 (proc, arg1, arg2, arg3, arg4, arg5)
  422. @deffnx {C Function} scm_call_6 (proc, arg1, arg2, arg3, arg4, arg5, arg6)
  423. Call @var{proc} with the given arguments.
  424. @end deffn
  425. @deffn {C Function} scm_call_n (proc, argv, nargs)
  426. Call @var{proc} with the array of arguments @var{argv}, as a
  427. @code{SCM*}. The length of the arguments should be passed in
  428. @var{nargs}, as a @code{size_t}.
  429. @end deffn
  430. @deffn {Scheme Procedure} apply:nconc2last lst
  431. @deffnx {C Function} scm_nconc2last (lst)
  432. @var{lst} should be a list (@var{arg1} @dots{} @var{argN}
  433. @var{arglst}), with @var{arglst} being a list. This function returns
  434. a list comprising @var{arg1} to @var{argN} plus the elements of
  435. @var{arglst}. @var{lst} is modified to form the return. @var{arglst}
  436. is not modified, though the return does share structure with it.
  437. This operation collects up the arguments from a list which is
  438. @code{apply} style parameters.
  439. @end deffn
  440. @deffn {Scheme Procedure} primitive-eval exp
  441. @deffnx {C Function} scm_primitive_eval (exp)
  442. Evaluate @var{exp} in the top-level environment specified by
  443. the current module.
  444. @end deffn
  445. @node Compilation
  446. @subsection Compiling Scheme Code
  447. The @code{eval} procedure directly interprets the S-expression
  448. representation of Scheme. An alternate strategy for evaluation is to
  449. determine ahead of time what computations will be necessary to
  450. evaluate the expression, and then use that recipe to produce the
  451. desired results. This is known as @dfn{compilation}.
  452. While it is possible to compile simple Scheme expressions such as
  453. @code{(+ 2 2)} or even @code{"Hello world!"}, compilation is most
  454. interesting in the context of procedures. Compiling a lambda expression
  455. produces a compiled procedure, which is just like a normal procedure
  456. except typically much faster, because it can bypass the generic
  457. interpreter.
  458. Functions from system modules in a Guile installation are normally
  459. compiled already, so they load and run quickly.
  460. @cindex automatic compilation
  461. Note that well-written Scheme programs will not typically call the
  462. procedures in this section, for the same reason that it is often bad
  463. taste to use @code{eval}. By default, Guile automatically compiles any
  464. files it encounters that have not been compiled yet (@pxref{Invoking
  465. Guile, @code{--auto-compile}}). The compiler can also be invoked
  466. explicitly from the shell as @code{guild compile foo.scm}.
  467. (Why are calls to @code{eval} and @code{compile} usually in bad taste?
  468. Because they are limited, in that they can only really make sense for
  469. top-level expressions. Also, most needs for ``compile-time''
  470. computation are fulfilled by macros and closures. Of course one good
  471. counterexample is the REPL itself, or any code that reads expressions
  472. from a port.)
  473. Automatic compilation generally works transparently, without any need
  474. for user intervention. However Guile does not yet do proper dependency
  475. tracking, so that if file @file{@var{a}.scm} uses macros from
  476. @file{@var{b}.scm}, and @var{@var{b}.scm} changes, @code{@var{a}.scm}
  477. would not be automatically recompiled. To forcibly invalidate the
  478. auto-compilation cache, pass the @code{--fresh-auto-compile} option to
  479. Guile, or set the @code{GUILE_AUTO_COMPILE} environment variable to
  480. @code{fresh} (instead of to @code{0} or @code{1}).
  481. For more information on the compiler itself, see @ref{Compiling to the
  482. Virtual Machine}. For information on the virtual machine, see @ref{A
  483. Virtual Machine for Guile}.
  484. The command-line interface to Guile's compiler is the @command{guild
  485. compile} command:
  486. @deffn {Command} {guild compile} [@option{option}...] @var{file}...
  487. Compile @var{file}, a source file, and store bytecode in the compilation cache
  488. or in the file specified by the @option{-o} option. The following options are
  489. available:
  490. @table @option
  491. @item -L @var{dir}
  492. @itemx --load-path=@var{dir}
  493. Add @var{dir} to the front of the module load path.
  494. @item -o @var{ofile}
  495. @itemx --output=@var{ofile}
  496. Write output bytecode to @var{ofile}. By convention, bytecode file
  497. names end in @code{.go}. When @option{-o} is omitted, the output file
  498. name is as for @code{compile-file} (see below).
  499. @item -W @var{warning}
  500. @itemx --warn=@var{warning}
  501. Emit warnings of type @var{warning}; use @code{--warn=help} for a list
  502. of available warnings and their description. Currently recognized
  503. warnings include @code{unused-variable}, @code{unused-toplevel},
  504. @code{unbound-variable}, @code{arity-mismatch}, and @code{format}.
  505. @item -f @var{lang}
  506. @itemx --from=@var{lang}
  507. Use @var{lang} as the source language of @var{file}. If this option is omitted,
  508. @code{scheme} is assumed.
  509. @item -t @var{lang}
  510. @itemx --to=@var{lang}
  511. Use @var{lang} as the target language of @var{file}. If this option is omitted,
  512. @code{objcode} is assumed.
  513. @end table
  514. Each @var{file} is assumed to be UTF-8-encoded, unless it contains a
  515. coding declaration as recognized by @code{file-encoding}
  516. (@pxref{Character Encoding of Source Files}).
  517. @end deffn
  518. The compiler can also be invoked directly by Scheme code using the procedures
  519. below:
  520. @deffn {Scheme Procedure} compile exp [env=#f] [from=(current-language)] [to=value] [opts=()]
  521. Compile the expression @var{exp} in the environment @var{env}. If
  522. @var{exp} is a procedure, the result will be a compiled procedure;
  523. otherwise @code{compile} is mostly equivalent to @code{eval}.
  524. For a discussion of languages and compiler options, @xref{Compiling to
  525. the Virtual Machine}.
  526. @end deffn
  527. @deffn {Scheme Procedure} compile-file file [output-file=#f] @
  528. [from=(current-language)] [to='objcode] @
  529. [env=(default-environment from)] [opts='()] @
  530. [canonicalization 'relative]
  531. Compile the file named @var{file}.
  532. Output will be written to a @var{output-file}. If you do not supply an
  533. output file name, output is written to a file in the cache directory, as
  534. computed by @code{(compiled-file-name @var{file})}.
  535. @var{from} and @var{to} specify the source and target languages.
  536. @xref{Compiling to the Virtual Machine}, for more information on these
  537. options, and on @var{env} and @var{opts}.
  538. As with @command{guild compile}, @var{file} is assumed to be
  539. UTF-8-encoded unless it contains a coding declaration.
  540. @end deffn
  541. @deffn {Scheme Procedure} compiled-file-name file
  542. Compute a cached location for a compiled version of a Scheme file named
  543. @var{file}.
  544. This file will usually be below the @file{$HOME/.cache/guile/ccache}
  545. directory, depending on the value of the @env{XDG_CACHE_HOME}
  546. environment variable. The intention is that @code{compiled-file-name}
  547. provides a fallback location for caching auto-compiled files. If you
  548. want to place a compile file in the @code{%load-compiled-path}, you
  549. should pass the @var{output-file} option to @code{compile-file},
  550. explicitly.
  551. @end deffn
  552. @defvr {Scheme Variable} %auto-compilation-options
  553. This variable contains the options passed to the @code{compile-file}
  554. procedure when auto-compiling source files. By default, it enables
  555. useful compilation warnings. It can be customized from @file{~/.guile}.
  556. @end defvr
  557. @node Loading
  558. @subsection Loading Scheme Code from File
  559. @rnindex load
  560. @deffn {Scheme Procedure} load filename [reader]
  561. Load @var{filename} and evaluate its contents in the top-level
  562. environment. The load paths are not searched.
  563. @var{reader} if provided should be either @code{#f}, or a procedure with
  564. the signature @code{(lambda (port) @dots{})} which reads the next
  565. expression from @var{port}. If @var{reader} is @code{#f} or absent,
  566. Guile's built-in @code{read} procedure is used (@pxref{Scheme Read}).
  567. The @var{reader} argument takes effect by setting the value of the
  568. @code{current-reader} fluid (see below) before loading the file, and
  569. restoring its previous value when loading is complete. The Scheme code
  570. inside @var{filename} can itself change the current reader procedure on
  571. the fly by setting @code{current-reader} fluid.
  572. If the variable @code{%load-hook} is defined, it should be bound to a
  573. procedure that will be called before any code is loaded. See
  574. documentation for @code{%load-hook} later in this section.
  575. @end deffn
  576. @deffn {Scheme Procedure} load-compiled filename
  577. Load the compiled file named @var{filename}. The load paths are not
  578. searched.
  579. Compiling a source file (@pxref{Read/Load/Eval/Compile}) and then
  580. calling @code{load-compiled} on the resulting file is equivalent to
  581. calling @code{load} on the source file.
  582. @end deffn
  583. @deffn {Scheme Procedure} load-from-path filename
  584. Similar to @code{load}, but searches for @var{filename} in the load
  585. paths. Preferentially loads a compiled version of the file, if it is
  586. available and up-to-date.
  587. @end deffn
  588. @deffn {Scheme Procedure} primitive-load filename
  589. @deffnx {C Function} scm_primitive_load (filename)
  590. Load the file named @var{filename} and evaluate its contents in
  591. the top-level environment. The load paths are not searched;
  592. @var{filename} must either be a full pathname or be a pathname
  593. relative to the current directory. If the variable
  594. @code{%load-hook} is defined, it should be bound to a procedure
  595. that will be called before any code is loaded. See the
  596. documentation for @code{%load-hook} later in this section.
  597. @end deffn
  598. @deftypefn {C Function} SCM scm_c_primitive_load (const char *filename)
  599. @code{scm_primitive_load}, but taking a C string instead of an
  600. @code{SCM}.
  601. @end deftypefn
  602. @deffn {Scheme Procedure} primitive-load-path filename [exception-on-not-found]
  603. @deffnx {C Function} scm_primitive_load_path (filename)
  604. Search @code{%load-path} for the file named @var{filename} and
  605. load it into the top-level environment. If @var{filename} is a
  606. relative pathname and is not found in the list of search paths,
  607. an error is signalled. Preferentially loads a compiled version of the
  608. file, if it is available and up-to-date.
  609. By default or if @var{exception-on-not-found} is true, an exception is
  610. raised if @var{filename} is not found. If @var{exception-on-not-found}
  611. is @code{#f} and @var{filename} is not found, no exception is raised and
  612. @code{#f} is returned. For compatibility with Guile 1.8 and earlier,
  613. the C function takes only one argument, which can be either a string
  614. (the file name) or an argument list.
  615. @end deffn
  616. @deffn {Scheme Procedure} %search-load-path filename
  617. @deffnx {C Function} scm_sys_search_load_path (filename)
  618. Search @code{%load-path} for the file named @var{filename},
  619. which must be readable by the current user. If @var{filename}
  620. is found in the list of paths to search or is an absolute
  621. pathname, return its full pathname. Otherwise, return
  622. @code{#f}. Filenames may have any of the optional extensions
  623. in the @code{%load-extensions} list; @code{%search-load-path}
  624. will try each extension automatically.
  625. @end deffn
  626. @defvar current-reader
  627. @code{current-reader} holds the read procedure that is currently being
  628. used by the above loading procedures to read expressions (from the file
  629. that they are loading). @code{current-reader} is a fluid, so it has an
  630. independent value in each dynamic root and should be read and set using
  631. @code{fluid-ref} and @code{fluid-set!} (@pxref{Fluids and Dynamic
  632. States}).
  633. Changing @code{current-reader} is typically useful to introduce local
  634. syntactic changes, such that code following the @code{fluid-set!} call
  635. is read using the newly installed reader. The @code{current-reader}
  636. change should take place at evaluation time when the code is evaluated,
  637. or at compilation time when the code is compiled:
  638. @findex eval-when
  639. @example
  640. (eval-when (compile eval)
  641. (fluid-set! current-reader my-own-reader))
  642. @end example
  643. The @code{eval-when} form above ensures that the @code{current-reader}
  644. change occurs at the right time.
  645. @end defvar
  646. @defvar %load-hook
  647. A procedure to be called @code{(%load-hook @var{filename})} whenever a
  648. file is loaded, or @code{#f} for no such call. @code{%load-hook} is
  649. used by all of the above loading functions (@code{load},
  650. @code{load-path}, @code{primitive-load} and
  651. @code{primitive-load-path}).
  652. For example an application can set this to show what's loaded,
  653. @example
  654. (set! %load-hook (lambda (filename)
  655. (format #t "Loading ~a ...\n" filename)))
  656. (load-from-path "foo.scm")
  657. @print{} Loading /usr/local/share/guile/site/foo.scm ...
  658. @end example
  659. @end defvar
  660. @deffn {Scheme Procedure} current-load-port
  661. @deffnx {C Function} scm_current_load_port ()
  662. Return the current-load-port.
  663. The load port is used internally by @code{primitive-load}.
  664. @end deffn
  665. @defvar %load-extensions
  666. A list of default file extensions for files containing Scheme code.
  667. @code{%search-load-path} tries each of these extensions when looking for
  668. a file to load. By default, @code{%load-extensions} is bound to the
  669. list @code{("" ".scm")}.
  670. @end defvar
  671. @node Character Encoding of Source Files
  672. @subsection Character Encoding of Source Files
  673. @cindex source file encoding
  674. @cindex primitive-load
  675. @cindex load
  676. Scheme source code files are usually encoded in ASCII, but, the
  677. built-in reader can interpret other character encodings. The
  678. procedure @code{primitive-load}, and by extension the functions that
  679. call it, such as @code{load}, first scan the top 500 characters of the
  680. file for a coding declaration.
  681. A coding declaration has the form @code{coding: XXXXXX}, where
  682. @code{XXXXXX} is the name of a character encoding in which the source
  683. code file has been encoded. The coding declaration must appear in a
  684. scheme comment. It can either be a semicolon-initiated comment or a block
  685. @code{#!} comment.
  686. The name of the character encoding in the coding declaration is
  687. typically lower case and containing only letters, numbers, and hyphens,
  688. as recognized by @code{set-port-encoding!} (@pxref{Ports,
  689. @code{set-port-encoding!}}). Common examples of character encoding
  690. names are @code{utf-8} and @code{iso-8859-1},
  691. @url{http://www.iana.org/assignments/character-sets, as defined by
  692. IANA}. Thus, the coding declaration is mostly compatible with Emacs.
  693. However, there are some differences in encoding names recognized by
  694. Emacs and encoding names defined by IANA, the latter being essentially a
  695. subset of the former. For instance, @code{latin-1} is a valid encoding
  696. name for Emacs, but it's not according to the IANA standard, which Guile
  697. follows; instead, you should use @code{iso-8859-1}, which is both
  698. understood by Emacs and dubbed by IANA (IANA writes it uppercase but
  699. Emacs wants it lowercase and Guile is case insensitive.)
  700. For source code, only a subset of all possible character encodings can
  701. be interpreted by the built-in source code reader. Only those
  702. character encodings in which ASCII text appears unmodified can be
  703. used. This includes @code{UTF-8} and @code{ISO-8859-1} through
  704. @code{ISO-8859-15}. The multi-byte character encodings @code{UTF-16}
  705. and @code{UTF-32} may not be used because they are not compatible with
  706. ASCII.
  707. @cindex read
  708. @cindex encoding
  709. @cindex port encoding
  710. @findex set-port-encoding!
  711. There might be a scenario in which one would want to read non-ASCII
  712. code from a port, such as with the function @code{read}, instead of
  713. with @code{load}. If the port's character encoding is the same as the
  714. encoding of the code to be read by the port, not other special
  715. handling is necessary. The port will automatically do the character
  716. encoding conversion. The functions @code{setlocale} or by
  717. @code{set-port-encoding!} are used to set port encodings
  718. (@pxref{Ports}).
  719. If a port is used to read code of unknown character encoding, it can
  720. accomplish this in three steps. First, the character encoding of the
  721. port should be set to ISO-8859-1 using @code{set-port-encoding!}.
  722. Then, the procedure @code{file-encoding}, described below, is used to
  723. scan for a coding declaration when reading from the port. As a side
  724. effect, it rewinds the port after its scan is complete. After that,
  725. the port's character encoding should be set to the encoding returned
  726. by @code{file-encoding}, if any, again by using
  727. @code{set-port-encoding!}. Then the code can be read as normal.
  728. @deffn {Scheme Procedure} file-encoding port
  729. @deffnx {C Function} scm_file_encoding port
  730. Scan the port for an Emacs-like character coding declaration near the
  731. top of the contents of a port with random-accessible contents
  732. (@pxref{Recognize Coding, how Emacs recognizes file encoding,, emacs,
  733. The GNU Emacs Reference Manual}). The coding declaration is of the form
  734. @code{coding: XXXXX} and must appear in a Scheme comment. Return a
  735. string containing the character encoding of the file if a declaration
  736. was found, or @code{#f} otherwise. The port is rewound.
  737. @end deffn
  738. @node Delayed Evaluation
  739. @subsection Delayed Evaluation
  740. @cindex delayed evaluation
  741. @cindex promises
  742. Promises are a convenient way to defer a calculation until its result
  743. is actually needed, and to run such a calculation only once.
  744. @deffn syntax delay expr
  745. @rnindex delay
  746. Return a promise object which holds the given @var{expr} expression,
  747. ready to be evaluated by a later @code{force}.
  748. @end deffn
  749. @deffn {Scheme Procedure} promise? obj
  750. @deffnx {C Function} scm_promise_p (obj)
  751. Return true if @var{obj} is a promise.
  752. @end deffn
  753. @rnindex force
  754. @deffn {Scheme Procedure} force p
  755. @deffnx {C Function} scm_force (p)
  756. Return the value obtained from evaluating the @var{expr} in the given
  757. promise @var{p}. If @var{p} has previously been forced then its
  758. @var{expr} is not evaluated again, instead the value obtained at that
  759. time is simply returned.
  760. During a @code{force}, an @var{expr} can call @code{force} again on
  761. its own promise, resulting in a recursive evaluation of that
  762. @var{expr}. The first evaluation to return gives the value for the
  763. promise. Higher evaluations run to completion in the normal way, but
  764. their results are ignored, @code{force} always returns the first
  765. value.
  766. @end deffn
  767. @c Local Variables:
  768. @c TeX-master: "guile.texi"
  769. @c End: