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