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