Added Copyright notice.
[bpt/guile.git] / doc / ref / scheme-evaluation.texi
CommitLineData
2da09c3f
MV
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
4@c Free Software Foundation, Inc.
5@c See the file guile.texi for copying conditions.
6
a0e07ba4
NJ
7@page
8@node Read/Load/Eval
9@chapter Reading and Evaluating Scheme Code
10
11This chapter describes Guile functions that are concerned with reading,
12loading and evaluating Scheme code at run time.
13
14@menu
15* Scheme Syntax:: Standard and extended Scheme syntax.
16* Scheme Read:: Reading Scheme code.
17* Fly Evaluation:: Procedures for on the fly evaluation.
18* Loading:: Loading Scheme code from file.
19* Delayed Evaluation:: Postponing evaluation until it is needed.
20* Local Evaluation:: Evaluation in a local environment.
21* Evaluator Behaviour:: Modifying Guile's evaluator.
22@end menu
23
24
25@node Scheme Syntax
26@section Scheme Syntax: Standard and Guile Extensions
27
28@menu
29* Expression Syntax::
30* Comments::
31* Block Comments::
32* Case Sensitivity::
33* Keyword Syntax::
34* Reader Extensions::
35@end menu
36
37
38@node Expression Syntax
39@subsection Expression Syntax
40
41
42@node Comments
43@subsection Comments
44
45@c FIXME::martin: Review me!
46
47Comments in Scheme source files are written by starting them with a
48semicolon character (@code{;}). The comment then reaches up to the end
49of the line. Comments can begin at any column, and the may be inserted
50on the same line as Scheme code.
51
52@lisp
53; Comment
54;; Comment too
55(define x 1) ; Comment after expression
56(let ((y 1))
57 ;; Display something.
58 (display y)
59;;; Comment at left margin.
60 (display (+ y 1)))
61@end lisp
62
63It is common to use a single semicolon for comments following
64expressions on a line, to use two semicolons for comments which are
65indented like code, and three semicolons for comments which start at
66column 0, even if they are inside an indented code block. This
67convention is used when indenting code in Emacs' Scheme mode.
68
69
70@node Block Comments
71@subsection Block Comments
72
73@c FIXME::martin: Review me!
74
75@cindex multiline comments
76In addition to the standard line comments defined by R5RS, Guile has
77another comment type for multiline comments, called @dfn{block
78comments}. This type of comment begins with the character sequence
79@code{#!} and ends with the characters @code{!#}, which must appear on a
80line of their own. These comments are compatible with the block
81comments in the Scheme Shell @file{scsh} (@pxref{The Scheme shell
82(scsh)}). The characters @code{#!} were chosen because they are the
83magic characters used in shell scripts for indicating that the name of
84the program for executing the script follows on the same line.
85
86Thus a Guile script often starts like this.
87
88@lisp
89#! /usr/local/bin/guile -s
90!#
91@end lisp
92
93More details on Guile scripting can be found in the scripting section
94(@pxref{Guile Scripting}).
95
96
97@node Case Sensitivity
98@subsection Case Sensitivity
99
100@c FIXME::martin: Review me!
101
102Scheme as defined in R5RS is not case sensitive when reading symbols.
103Guile, on the contrary is case sensitive by default, so the identifiers
104
105@lisp
106guile-whuzzy
107Guile-Whuzzy
108@end lisp
109
110are the same in R5RS Scheme, but are different in Guile.
111
112It is possible to turn off case sensitivity in Guile by setting the
113reader option @code{case-insensitive}. More on reader options can be
114found at (@pxref{Reader options}).
115
116@lisp
117(read-enable 'case-insensitive)
118@end lisp
119
120Note that this is seldom a problem, because Scheme programmers tend not
121to use uppercase letters in their identifiers anyway.
122
123
124@node Keyword Syntax
125@subsection Keyword Syntax
126
127
128@node Reader Extensions
129@subsection Reader Extensions
130
8f85c0c6
NJ
131@deffn {Scheme Procedure} read-hash-extend chr proc
132@deffnx {C Function} scm_read_hash_extend (chr, proc)
a0e07ba4
NJ
133Install the procedure @var{proc} for reading expressions
134starting with the character sequence @code{#} and @var{chr}.
135@var{proc} will be called with two arguments: the character
136@var{chr} and the port to read further data from. The object
137returned will be the return value of @code{read}.
138@end deffn
139
140
141@node Scheme Read
142@section Reading Scheme Code
143
144@rnindex read
8f85c0c6
NJ
145@deffn {Scheme Procedure} read [port]
146@deffnx {C Function} scm_read (port)
a0e07ba4
NJ
147Read an s-expression from the input port @var{port}, or from
148the current input port if @var{port} is not specified.
149Any whitespace before the next token is discarded.
150@end deffn
151
152The behaviour of Guile's Scheme reader can be modified by manipulating
c936bede
NJ
153its read options. For more information about options, @xref{User level
154options interfaces}. If you want to know which reader options are
a0e07ba4
NJ
155available, @xref{Reader options}.
156
157@c FIXME::martin: This is taken from libguile/options.c. Is there
158@c actually a difference between 'help and 'full?
159
8f85c0c6 160@deffn {Scheme Procedure} read-options [setting]
a0e07ba4
NJ
161Display the current settings of the read options. If @var{setting} is
162omitted, only a short form of the current read options is printed.
163Otherwise, @var{setting} should be one of the following symbols:
164@table @code
165@item help
166Display the complete option settings.
167@item full
168Like @code{help}, but also print programmer options.
169@end table
170@end deffn
171
8f85c0c6
NJ
172@deffn {Scheme Procedure} read-enable option-name
173@deffnx {Scheme Procedure} read-disable option-name
174@deffnx {Scheme Procedure} read-set! option-name value
a0e07ba4
NJ
175Modify the read options. @code{read-enable} should be used with boolean
176options and switches them on, @code{read-disable} switches them off.
177@code{read-set!} can be used to set an option to a specific value.
178@end deffn
179
8f85c0c6
NJ
180@deffn {Scheme Procedure} read-options-interface [setting]
181@deffnx {C Function} scm_read_options (setting)
a0e07ba4
NJ
182Option interface for the read options. Instead of using
183this procedure directly, use the procedures @code{read-enable},
413d32b6 184@code{read-disable}, @code{read-set!} and @code{read-options}.
a0e07ba4
NJ
185@end deffn
186
187
188@node Fly Evaluation
189@section Procedures for On the Fly Evaluation
190
c3164ca8
GH
191@xref{Environments}.
192
a0e07ba4
NJ
193@rnindex eval
194@c ARGFIXME environment/environment specifier
8f85c0c6
NJ
195@deffn {Scheme Procedure} eval exp module
196@deffnx {C Function} scm_eval (exp, module)
197Evaluate @var{exp}, a list representing a Scheme expression,
198in the top-level environment specified by @var{module}.
199While @var{exp} is evaluated (using @code{primitive-eval}),
200@var{module} is made the current module. The current module
201is reset to its previous value when @var{eval} returns.
a0e07ba4
NJ
202@end deffn
203
204@rnindex interaction-environment
8f85c0c6
NJ
205@deffn {Scheme Procedure} interaction-environment
206@deffnx {C Function} scm_interaction_environment ()
a0e07ba4
NJ
207Return a specifier for the environment that contains
208implementation--defined bindings, typically a superset of those
209listed in the report. The intent is that this procedure will
210return the environment in which the implementation would
211evaluate expressions dynamically typed by the user.
212@end deffn
213
21fabda1 214@deffn {Scheme Procedure} eval-string string [module]
8f85c0c6 215@deffnx {C Function} scm_eval_string (string)
21fabda1
MV
216@deffnx {C Function} scm_eval_string_in_module (string, module)
217Evaluate @var{string} as the text representation of a Scheme form or
218forms, and return whatever value they produce. Evaluation takes place
219in the given module, or in the current module when no module is given.
220While the code is evaluated, the given module is made the current one.
221The current module is restored when this procedure returns.
a0e07ba4
NJ
222@end deffn
223
0e97e58d
KR
224@deffn {Scheme Procedure} apply proc arg1 @dots{} argN arglst
225@deffnx {C Function} scm_apply_0 (proc, arglst)
226@deffnx {C Function} scm_apply_1 (proc, arg1, arglst)
227@deffnx {C Function} scm_apply_2 (proc, arg1, arg2, arglst)
228@deffnx {C Function} scm_apply_3 (proc, arg1, arg2, arg3, arglst)
229@deffnx {C Function} scm_apply (proc, arg, rest)
230@rnindex apply
231Call @var{proc} with arguments @var{arg1} @dots{} @var{argN} plus the
232elements of the @var{arglst} list.
233
234@code{scm_apply} takes parameters corresponding to a Scheme level
235@code{(lambda (proc arg . rest) ...)}. So @var{arg} and all but the
236last element of the @var{rest} list make up
237@var{arg1}@dots{}@var{argN} and the last element of @var{rest} is the
238@var{arglst} list. Or if @var{rest} is the empty list @code{SCM_EOL}
239then there's no @var{arg1}@dots{}@var{argN} and @var{arg} is the
240@var{arglst}.
241
242@var{arglst} is not modified, but the @var{rest} list passed to
243@code{scm_apply} is modified.
a0e07ba4
NJ
244@end deffn
245
0e97e58d
KR
246@deffn {C Function} scm_call_0 (proc)
247@deffnx {C Function} scm_call_1 (proc, arg1)
248@deffnx {C Function} scm_call_2 (proc, arg1, arg2)
249@deffnx {C Function} scm_call_3 (proc, arg1, arg2, arg3)
250Call @var{proc} with the given arguments.
251@end deffn
252
253@deffn {Scheme Procedure} apply:nconc2last lst
254@deffnx {C Function} scm_nconc2last (lst)
255@var{lst} should be a list (@var{arg1} @dots{} @var{argN}
256@var{arglst}), with @var{arglst} being a list. This function returns
257a list comprising @var{arg1} to @var{argN} plus the elements of
258@var{arglst}. @var{lst} is modified to form the return. @var{arglst}
259is not modified, though the return does share structure with it.
260
261This operation collects up the arguments from a list which is
262@code{apply} style parameters.
a0e07ba4
NJ
263@end deffn
264
8f85c0c6
NJ
265@deffn {Scheme Procedure} primitive-eval exp
266@deffnx {C Function} scm_primitive_eval (exp)
a0e07ba4
NJ
267Evaluate @var{exp} in the top-level environment specified by
268the current module.
269@end deffn
270
a0e07ba4
NJ
271
272@node Loading
273@section Loading Scheme Code from File
274
275@rnindex load
8f85c0c6 276@deffn {Scheme Procedure} load filename
a0e07ba4
NJ
277Load @var{filename} and evaluate its contents in the top-level
278environment. The load paths are not searched. If the variable
279@code{%load-hook} is defined, it should be bound to a procedure that
280will be called before any code is loaded. See documentation for
281@code{%load-hook} later in this section.
282@end deffn
283
8f85c0c6 284@deffn {Scheme Procedure} load-from-path filename
a0e07ba4
NJ
285Similar to @code{load}, but searches for @var{filename} in the load
286paths.
287@end deffn
288
8f85c0c6
NJ
289@deffn {Scheme Procedure} primitive-load filename
290@deffnx {C Function} scm_primitive_load (filename)
a0e07ba4
NJ
291Load the file named @var{filename} and evaluate its contents in
292the top-level environment. The load paths are not searched;
293@var{filename} must either be a full pathname or be a pathname
294relative to the current directory. If the variable
295@code{%load-hook} is defined, it should be bound to a procedure
296that will be called before any code is loaded. See the
297documentation for @code{%load-hook} later in this section.
298@end deffn
299
8f85c0c6
NJ
300@deffn {Scheme Procedure} primitive-load-path filename
301@deffnx {C Function} scm_primitive_load_path (filename)
a0e07ba4
NJ
302Search @var{%load-path} for the file named @var{filename} and
303load it into the top-level environment. If @var{filename} is a
304relative pathname and is not found in the list of search paths,
305an error is signalled.
306@end deffn
307
8f85c0c6
NJ
308@deffn {Scheme Procedure} %search-load-path filename
309@deffnx {C Function} scm_sys_search_load_path (filename)
a0e07ba4
NJ
310Search @var{%load-path} for the file named @var{filename},
311which must be readable by the current user. If @var{filename}
312is found in the list of paths to search or is an absolute
313pathname, return its full pathname. Otherwise, return
314@code{#f}. Filenames may have any of the optional extensions
315in the @code{%load-extensions} list; @code{%search-load-path}
316will try each extension automatically.
317@end deffn
318
319@defvar %load-hook
320A procedure to be run whenever @code{primitive-load} is called. If this
321procedure is defined, it will be called with the filename argument that
322was passed to @code{primitive-load}.
323
324@example
325(define %load-hook (lambda (file)
326 (display "Loading ")
327 (display file)
328 (write-line "...."))) @result{} undefined
329(load-from-path "foo.scm")
330@print{} Loading /usr/local/share/guile/site/foo.scm....
331@end example
332
333@end defvar
334
8f85c0c6
NJ
335@deffn {Scheme Procedure} current-load-port
336@deffnx {C Function} scm_current_load_port ()
a0e07ba4
NJ
337Return the current-load-port.
338The load port is used internally by @code{primitive-load}.
339@end deffn
340
341@defvar %load-extensions
342A list of default file extensions for files containing Scheme code.
343@code{%search-load-path} tries each of these extensions when looking for
344a file to load. By default, @code{%load-extensions} is bound to the
345list @code{("" ".scm")}.
346@end defvar
347
348
349@node Delayed Evaluation
350@section Delayed Evaluation
f0d1cf6f
KR
351@cindex delayed evaluation
352@cindex promises
a0e07ba4 353
f0d1cf6f
KR
354Promises are a convenient way to defer a calculation until its result
355is actually needed, and to run such a calculation only once.
356
357@deffn syntax delay expr
358@rnindex delay
359Return a promise object which holds the given @var{expr} expression,
360ready to be evaluated by a later @code{force}.
361@end deffn
a0e07ba4 362
8f85c0c6
NJ
363@deffn {Scheme Procedure} promise? obj
364@deffnx {C Function} scm_promise_p (obj)
f0d1cf6f 365Return true if @var{obj} is a promise.
a0e07ba4
NJ
366@end deffn
367
368@rnindex force
f0d1cf6f
KR
369@deffn {Scheme Procedure} force p
370@deffnx {C Function} scm_force (p)
371Return the value obtained from evaluating the @var{expr} in the given
372promise @var{p}. If @var{p} has previously been forced then its
373@var{expr} is not evaluated again, instead the value obtained at that
374time is simply returned.
375
376During a @code{force}, an @var{expr} can call @code{force} again on
377its own promise, resulting in a recursive evaluation of that
378@var{expr}. The first evaluation to return gives the value for the
379promise. Higher evaluations run to completion in the normal way, but
380their results are ignored, @code{force} always returns the first
a0e07ba4
NJ
381value.
382@end deffn
383
384
385@node Local Evaluation
386@section Local Evaluation
387
388[the-environment]
389
8f85c0c6
NJ
390@deffn {Scheme Procedure} local-eval exp [env]
391@deffnx {C Function} scm_local_eval (exp, env)
a0e07ba4
NJ
392Evaluate @var{exp} in its environment. If @var{env} is supplied,
393it is the environment in which to evaluate @var{exp}. Otherwise,
394@var{exp} must be a memoized code object (in which case, its environment
395is implicit).
396@end deffn
397
398
399@node Evaluator Behaviour
400@section Evaluator Behaviour
401
402@c FIXME::martin: Maybe this node name is bad, but the old name clashed with
403@c `Evaluator options' under `Options and Config'.
404
405The behaviour of Guile's evaluator can be modified by manipulating the
c936bede
NJ
406evaluator options. For more information about options, @xref{User level
407options interfaces}. If you want to know which evaluator options are
a0e07ba4
NJ
408available, @xref{Evaluator options}.
409
410@c FIXME::martin: This is taken from libguile/options.c. Is there
411@c actually a difference between 'help and 'full?
412
8f85c0c6 413@deffn {Scheme Procedure} eval-options [setting]
a0e07ba4
NJ
414Display the current settings of the evaluator options. If @var{setting}
415is omitted, only a short form of the current evaluator options is
416printed. Otherwise, @var{setting} should be one of the following
417symbols:
418@table @code
419@item help
420Display the complete option settings.
421@item full
422Like @code{help}, but also print programmer options.
423@end table
424@end deffn
425
8f85c0c6
NJ
426@deffn {Scheme Procedure} eval-enable option-name
427@deffnx {Scheme Procedure} eval-disable option-name
428@deffnx {Scheme Procedure} eval-set! option-name value
a0e07ba4
NJ
429Modify the evaluator options. @code{eval-enable} should be used with boolean
430options and switches them on, @code{eval-disable} switches them off.
431@code{eval-set!} can be used to set an option to a specific value.
432@end deffn
433
8f85c0c6
NJ
434@deffn {Scheme Procedure} eval-options-interface [setting]
435@deffnx {C Function} scm_eval_options_interface (setting)
a0e07ba4
NJ
436Option interface for the evaluation options. Instead of using
437this procedure directly, use the procedures @code{eval-enable},
413d32b6 438@code{eval-disable}, @code{eval-set!} and @code{eval-options}.
a0e07ba4
NJ
439@end deffn
440
441@c FIXME::martin: Why aren't these procedure named like the other options
442@c procedures?
443
8f85c0c6 444@deffn {Scheme Procedure} traps [setting]
a0e07ba4
NJ
445Display the current settings of the evaluator traps options. If
446@var{setting} is omitted, only a short form of the current evaluator
447traps options is printed. Otherwise, @var{setting} should be one of the
448following symbols:
449@table @code
450@item help
451Display the complete option settings.
452@item full
453Like @code{help}, but also print programmer options.
454@end table
455@end deffn
456
8f85c0c6
NJ
457@deffn {Scheme Procedure} trap-enable option-name
458@deffnx {Scheme Procedure} trap-disable option-name
459@deffnx {Scheme Procedure} trap-set! option-name value
a0e07ba4
NJ
460Modify the evaluator options. @code{trap-enable} should be used with boolean
461options and switches them on, @code{trap-disable} switches them off.
462@code{trap-set!} can be used to set an option to a specific value.
463@end deffn
464
8f85c0c6
NJ
465@deffn {Scheme Procedure} evaluator-traps-interface [setting]
466@deffnx {C Function} scm_evaluator_traps (setting)
a0e07ba4
NJ
467Option interface for the evaluator trap options.
468@end deffn
469
470
471@c Local Variables:
472@c TeX-master: "guile.texi"
473@c End: