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