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