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