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