(Bitwise Operations): In logtest and logbit?, describe
[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.
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
7@page
8@node Read/Load/Eval
9@section 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@subsection 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@subsubsection Expression Syntax
40
41An expression to be evaluated takes one of the following forms.
42
43@table @nicode
44
45@item @var{symbol}
46A symbol is evaluated by dereferencing. A binding of that symbol is
47sought and the value there used. For example,
48
49@example
50(define x 123)
51x @result{} 123
52@end example
53
54@item (@var{proc} @var{args}@dots{})
55A parenthesised expression is a function call. @var{proc} and each
56argument are evaluated, then the function (which @var{proc} evaluated
57to) is called with those arguments.
58
59The order in which @var{proc} and the arguments are evaluated is
60unspecified, so be careful when using expressions with side effects.
61
62@example
63(max 1 2 3) @result{} 3
64
65(define (get-some-proc) min)
66((get-some-proc) 1 2 3) @result{} 1
67@end example
68
69The same sort of parenthesised form is used for a macro invocation,
70but in that case the arguments are not evaluated. See the
71descriptions of macros for more on this (@pxref{Macros}, and
72@pxref{Syntax Rules}).
73
74@item @var{constant}
75Number, string, character and boolean constants evaluate ``to
76themselves'', so can appear as literals.
77
78@example
79123 @result{} 123
8099.9 @result{} 99.9
81"hello" @result{} "hello"
82#\z @result{} #\z
83#t @result{} #t
84@end example
85
86Note that an application must not attempt to modify literal strings,
87since they may be in read-only memory.
88
89@item (quote @var{data})
90@itemx '@var{data}
91@findex quote
92@findex '
93Quoting is used to obtain a literal symbol (instead of a variable
94reference), a literal list (instead of a function call), or a literal
95vector. @nicode{'} is simply a shorthand for a @code{quote} form.
96For example,
97
98@example
99'x @result{} x
100'(1 2 3) @result{} (1 2 3)
101'#(1 (2 3) 4) @result{} #(1 (2 3) 4)
102(quote x) @result{} x
103(quote (1 2 3)) @result{} (1 2 3)
104(quote #(1 (2 3) 4)) @result{} #(1 (2 3) 4)
105@end example
106
107Note that an application must not attempt to modify literal lists or
108vectors obtained from a @code{quote} form, since they may be in
109read-only memory.
110
111@item (quasiquote @var{data})
112@itemx `@var{data}
113@findex quasiquote
114@findex `
115Backquote quasi-quotation is like @code{quote}, but selected
116sub-expressions are evaluated. This is a convenient way to construct
117a list or vector structure most of which is constant, but at certain
118points should have expressions substituted.
119
120The same effect can always be had with suitable @code{list},
121@code{cons} or @code{vector} calls, but quasi-quoting is often easier.
122
123@table @nicode
124
125@item (unquote @var{expr})
126@itemx ,@var{expr}
127@findex unquote
128@findex ,
129Within the quasiquote @var{data}, @code{unquote} or @code{,} indicates
130an expression to be evaluated and inserted. The comma syntax @code{,}
131is simply a shorthand for an @code{unquote} form. For example,
132
133@example
134`(1 2 ,(* 9 9) 3 4) @result{} (1 2 81 3 4)
135`(1 (unquote (+ 1 1)) 3) @result{} (1 2 3)
136`#(1 ,(/ 12 2)) @result{} #(1 6)
137@end example
138
139@item (unquote-splicing @var{expr})
140@itemx ,@@@var{expr}
141@findex unquote-splicing
142@findex ,@@
143Within the quasiquote @var{data}, @code{unquote-splicing} or
144@code{,@@} indicates an expression to be evaluated and the elements of
145the returned list inserted. @var{expr} must evaluate to a list. The
146``comma-at'' syntax @code{,@@} is simply a shorthand for an
147@code{unquote-splicing} form.
148
149@example
150(define x '(2 3))
151`(1 ,@@x 4) @result{} (1 2 3 4)
152`(1 (unquote-splicing (map 1+ x))) @result{} (1 3 4)
153`#(9 ,@@x 9) @result{} #(9 2 3 9)
154@end example
155
156Notice @code{,@@} differs from plain @code{,} in the way one level of
157nesting is stripped. For @code{,@@} the elements of a returned list
158are inserted, whereas with @code{,} it would be the list itself
159inserted.
160@end table
161
162@c
163@c FIXME: What can we say about the mutability of a quasiquote
164@c result? R5RS doesn't seem to specify anything, though where it
165@c says backquote without commas is the same as plain quote then
166@c presumably the "fixed" portions of a quasiquote expression must be
167@c treated as immutable.
168@c
169
170@end table
171
172
173@node Comments
174@subsubsection Comments
175
176@c FIXME::martin: Review me!
177
178Comments in Scheme source files are written by starting them with a
179semicolon character (@code{;}). The comment then reaches up to the end
180of the line. Comments can begin at any column, and the may be inserted
181on the same line as Scheme code.
182
183@lisp
184; Comment
185;; Comment too
186(define x 1) ; Comment after expression
187(let ((y 1))
188 ;; Display something.
189 (display y)
190;;; Comment at left margin.
191 (display (+ y 1)))
192@end lisp
193
194It is common to use a single semicolon for comments following
195expressions on a line, to use two semicolons for comments which are
196indented like code, and three semicolons for comments which start at
197column 0, even if they are inside an indented code block. This
198convention is used when indenting code in Emacs' Scheme mode.
199
200
201@node Block Comments
202@subsubsection Block Comments
456f797b
KR
203@cindex multiline comments
204@cindex block comments
205@cindex #!
206@cindex !#
07d83abe
MV
207
208@c FIXME::martin: Review me!
209
07d83abe
MV
210In addition to the standard line comments defined by R5RS, Guile has
211another comment type for multiline comments, called @dfn{block
212comments}. This type of comment begins with the character sequence
213@code{#!} and ends with the characters @code{!#}, which must appear on a
214line of their own. These comments are compatible with the block
215comments in the Scheme Shell @file{scsh} (@pxref{The Scheme shell
216(scsh)}). The characters @code{#!} were chosen because they are the
217magic characters used in shell scripts for indicating that the name of
218the program for executing the script follows on the same line.
219
220Thus a Guile script often starts like this.
221
222@lisp
223#! /usr/local/bin/guile -s
224!#
225@end lisp
226
227More details on Guile scripting can be found in the scripting section
228(@pxref{Guile Scripting}).
229
230
231@node Case Sensitivity
232@subsubsection Case Sensitivity
233
234@c FIXME::martin: Review me!
235
236Scheme as defined in R5RS is not case sensitive when reading symbols.
237Guile, on the contrary is case sensitive by default, so the identifiers
238
239@lisp
240guile-whuzzy
241Guile-Whuzzy
242@end lisp
243
244are the same in R5RS Scheme, but are different in Guile.
245
246It is possible to turn off case sensitivity in Guile by setting the
247reader option @code{case-insensitive}. More on reader options can be
248found at (@pxref{Reader options}).
249
250@lisp
251(read-enable 'case-insensitive)
252@end lisp
253
254Note that this is seldom a problem, because Scheme programmers tend not
255to use uppercase letters in their identifiers anyway.
256
257
258@node Keyword Syntax
259@subsubsection Keyword Syntax
260
261
262@node Reader Extensions
263@subsubsection Reader Extensions
264
265@deffn {Scheme Procedure} read-hash-extend chr proc
266@deffnx {C Function} scm_read_hash_extend (chr, proc)
267Install the procedure @var{proc} for reading expressions
268starting with the character sequence @code{#} and @var{chr}.
269@var{proc} will be called with two arguments: the character
270@var{chr} and the port to read further data from. The object
271returned will be the return value of @code{read}.
272@end deffn
273
274
275@node Scheme Read
276@subsection Reading Scheme Code
277
278@rnindex read
279@deffn {Scheme Procedure} read [port]
280@deffnx {C Function} scm_read (port)
281Read an s-expression from the input port @var{port}, or from
282the current input port if @var{port} is not specified.
283Any whitespace before the next token is discarded.
284@end deffn
285
286The behaviour of Guile's Scheme reader can be modified by manipulating
287its read options. For more information about options, @xref{User level
288options interfaces}. If you want to know which reader options are
289available, @xref{Reader options}.
290
291@c FIXME::martin: This is taken from libguile/options.c. Is there
292@c actually a difference between 'help and 'full?
293
294@deffn {Scheme Procedure} read-options [setting]
295Display the current settings of the read options. If @var{setting} is
296omitted, only a short form of the current read options is printed.
297Otherwise, @var{setting} should be one of the following symbols:
298@table @code
299@item help
300Display the complete option settings.
301@item full
302Like @code{help}, but also print programmer options.
303@end table
304@end deffn
305
306@deffn {Scheme Procedure} read-enable option-name
307@deffnx {Scheme Procedure} read-disable option-name
308@deffnx {Scheme Procedure} read-set! option-name value
309Modify the read options. @code{read-enable} should be used with boolean
310options and switches them on, @code{read-disable} switches them off.
311@code{read-set!} can be used to set an option to a specific value.
312@end deffn
313
314@deffn {Scheme Procedure} read-options-interface [setting]
315@deffnx {C Function} scm_read_options (setting)
316Option interface for the read options. Instead of using
317this procedure directly, use the procedures @code{read-enable},
318@code{read-disable}, @code{read-set!} and @code{read-options}.
319@end deffn
320
321
322@node Fly Evaluation
323@subsection Procedures for On the Fly Evaluation
324
325@xref{Environments}.
326
327@rnindex eval
328@c ARGFIXME environment/environment specifier
329@deffn {Scheme Procedure} eval exp module
330@deffnx {C Function} scm_eval (exp, module)
331Evaluate @var{exp}, a list representing a Scheme expression,
332in the top-level environment specified by @var{module}.
333While @var{exp} is evaluated (using @code{primitive-eval}),
334@var{module} is made the current module. The current module
335is reset to its previous value when @var{eval} returns.
336@end deffn
337
338@rnindex interaction-environment
339@deffn {Scheme Procedure} interaction-environment
340@deffnx {C Function} scm_interaction_environment ()
341Return a specifier for the environment that contains
342implementation--defined bindings, typically a superset of those
343listed in the report. The intent is that this procedure will
344return the environment in which the implementation would
345evaluate expressions dynamically typed by the user.
346@end deffn
347
348@deffn {Scheme Procedure} eval-string string [module]
349@deffnx {C Function} scm_eval_string (string)
350@deffnx {C Function} scm_eval_string_in_module (string, module)
351Evaluate @var{string} as the text representation of a Scheme form or
352forms, and return whatever value they produce. Evaluation takes place
353in the given module, or in the current module when no module is given.
354While the code is evaluated, the given module is made the current one.
355The current module is restored when this procedure returns.
356@end deffn
357
358@deffn {Scheme Procedure} apply proc arg1 @dots{} argN arglst
359@deffnx {C Function} scm_apply_0 (proc, arglst)
360@deffnx {C Function} scm_apply_1 (proc, arg1, arglst)
361@deffnx {C Function} scm_apply_2 (proc, arg1, arg2, arglst)
362@deffnx {C Function} scm_apply_3 (proc, arg1, arg2, arg3, arglst)
363@deffnx {C Function} scm_apply (proc, arg, rest)
364@rnindex apply
365Call @var{proc} with arguments @var{arg1} @dots{} @var{argN} plus the
366elements of the @var{arglst} list.
367
368@code{scm_apply} takes parameters corresponding to a Scheme level
369@code{(lambda (proc arg . rest) ...)}. So @var{arg} and all but the
370last element of the @var{rest} list make up
371@var{arg1}@dots{}@var{argN} and the last element of @var{rest} is the
372@var{arglst} list. Or if @var{rest} is the empty list @code{SCM_EOL}
373then there's no @var{arg1}@dots{}@var{argN} and @var{arg} is the
374@var{arglst}.
375
376@var{arglst} is not modified, but the @var{rest} list passed to
377@code{scm_apply} is modified.
378@end deffn
379
380@deffn {C Function} scm_call_0 (proc)
381@deffnx {C Function} scm_call_1 (proc, arg1)
382@deffnx {C Function} scm_call_2 (proc, arg1, arg2)
383@deffnx {C Function} scm_call_3 (proc, arg1, arg2, arg3)
384Call @var{proc} with the given arguments.
385@end deffn
386
387@deffn {Scheme Procedure} apply:nconc2last lst
388@deffnx {C Function} scm_nconc2last (lst)
389@var{lst} should be a list (@var{arg1} @dots{} @var{argN}
390@var{arglst}), with @var{arglst} being a list. This function returns
391a list comprising @var{arg1} to @var{argN} plus the elements of
392@var{arglst}. @var{lst} is modified to form the return. @var{arglst}
393is not modified, though the return does share structure with it.
394
395This operation collects up the arguments from a list which is
396@code{apply} style parameters.
397@end deffn
398
399@deffn {Scheme Procedure} primitive-eval exp
400@deffnx {C Function} scm_primitive_eval (exp)
401Evaluate @var{exp} in the top-level environment specified by
402the current module.
403@end deffn
404
405
406@node Loading
407@subsection Loading Scheme Code from File
408
409@rnindex load
410@deffn {Scheme Procedure} load filename
411Load @var{filename} and evaluate its contents in the top-level
412environment. The load paths are not searched. If the variable
413@code{%load-hook} is defined, it should be bound to a procedure that
414will be called before any code is loaded. See documentation for
415@code{%load-hook} later in this section.
416@end deffn
417
418@deffn {Scheme Procedure} load-from-path filename
419Similar to @code{load}, but searches for @var{filename} in the load
420paths.
421@end deffn
422
423@deffn {Scheme Procedure} primitive-load filename
424@deffnx {C Function} scm_primitive_load (filename)
425Load the file named @var{filename} and evaluate its contents in
426the top-level environment. The load paths are not searched;
427@var{filename} must either be a full pathname or be a pathname
428relative to the current directory. If the variable
429@code{%load-hook} is defined, it should be bound to a procedure
430that will be called before any code is loaded. See the
431documentation for @code{%load-hook} later in this section.
432@end deffn
433
434@deffn {Scheme Procedure} primitive-load-path filename
435@deffnx {C Function} scm_primitive_load_path (filename)
436Search @var{%load-path} for the file named @var{filename} and
437load it into the top-level environment. If @var{filename} is a
438relative pathname and is not found in the list of search paths,
439an error is signalled.
440@end deffn
441
442@deffn {Scheme Procedure} %search-load-path filename
443@deffnx {C Function} scm_sys_search_load_path (filename)
444Search @var{%load-path} for the file named @var{filename},
445which must be readable by the current user. If @var{filename}
446is found in the list of paths to search or is an absolute
447pathname, return its full pathname. Otherwise, return
448@code{#f}. Filenames may have any of the optional extensions
449in the @code{%load-extensions} list; @code{%search-load-path}
450will try each extension automatically.
451@end deffn
452
453@defvar %load-hook
454A procedure to be run whenever @code{primitive-load} is called. If this
455procedure is defined, it will be called with the filename argument that
456was passed to @code{primitive-load}.
457
458@example
459(define %load-hook (lambda (file)
460 (display "Loading ")
461 (display file)
462 (write-line "...."))) @result{} undefined
463(load-from-path "foo.scm")
464@print{} Loading /usr/local/share/guile/site/foo.scm....
465@end example
466
467@end defvar
468
469@deffn {Scheme Procedure} current-load-port
470@deffnx {C Function} scm_current_load_port ()
471Return the current-load-port.
472The load port is used internally by @code{primitive-load}.
473@end deffn
474
475@defvar %load-extensions
476A list of default file extensions for files containing Scheme code.
477@code{%search-load-path} tries each of these extensions when looking for
478a file to load. By default, @code{%load-extensions} is bound to the
479list @code{("" ".scm")}.
480@end defvar
481
482
483@node Delayed Evaluation
484@subsection Delayed Evaluation
485@cindex delayed evaluation
486@cindex promises
487
488Promises are a convenient way to defer a calculation until its result
489is actually needed, and to run such a calculation only once.
490
491@deffn syntax delay expr
492@rnindex delay
493Return a promise object which holds the given @var{expr} expression,
494ready to be evaluated by a later @code{force}.
495@end deffn
496
497@deffn {Scheme Procedure} promise? obj
498@deffnx {C Function} scm_promise_p (obj)
499Return true if @var{obj} is a promise.
500@end deffn
501
502@rnindex force
503@deffn {Scheme Procedure} force p
504@deffnx {C Function} scm_force (p)
505Return the value obtained from evaluating the @var{expr} in the given
506promise @var{p}. If @var{p} has previously been forced then its
507@var{expr} is not evaluated again, instead the value obtained at that
508time is simply returned.
509
510During a @code{force}, an @var{expr} can call @code{force} again on
511its own promise, resulting in a recursive evaluation of that
512@var{expr}. The first evaluation to return gives the value for the
513promise. Higher evaluations run to completion in the normal way, but
514their results are ignored, @code{force} always returns the first
515value.
516@end deffn
517
518
519@node Local Evaluation
520@subsection Local Evaluation
521
522[the-environment]
523
524@deffn {Scheme Procedure} local-eval exp [env]
525@deffnx {C Function} scm_local_eval (exp, env)
526Evaluate @var{exp} in its environment. If @var{env} is supplied,
527it is the environment in which to evaluate @var{exp}. Otherwise,
528@var{exp} must be a memoized code object (in which case, its environment
529is implicit).
530@end deffn
531
532
533@node Evaluator Behaviour
534@subsection Evaluator Behaviour
535
536@c FIXME::martin: Maybe this node name is bad, but the old name clashed with
537@c `Evaluator options' under `Options and Config'.
538
539The behaviour of Guile's evaluator can be modified by manipulating the
540evaluator options. For more information about options, @xref{User level
541options interfaces}. If you want to know which evaluator options are
542available, @xref{Evaluator options}.
543
544@c FIXME::martin: This is taken from libguile/options.c. Is there
545@c actually a difference between 'help and 'full?
546
547@deffn {Scheme Procedure} eval-options [setting]
548Display the current settings of the evaluator options. If @var{setting}
549is omitted, only a short form of the current evaluator options is
550printed. Otherwise, @var{setting} should be one of the following
551symbols:
552@table @code
553@item help
554Display the complete option settings.
555@item full
556Like @code{help}, but also print programmer options.
557@end table
558@end deffn
559
560@deffn {Scheme Procedure} eval-enable option-name
561@deffnx {Scheme Procedure} eval-disable option-name
562@deffnx {Scheme Procedure} eval-set! option-name value
563Modify the evaluator options. @code{eval-enable} should be used with boolean
564options and switches them on, @code{eval-disable} switches them off.
565@code{eval-set!} can be used to set an option to a specific value.
566@end deffn
567
568@deffn {Scheme Procedure} eval-options-interface [setting]
569@deffnx {C Function} scm_eval_options_interface (setting)
570Option interface for the evaluation options. Instead of using
571this procedure directly, use the procedures @code{eval-enable},
572@code{eval-disable}, @code{eval-set!} and @code{eval-options}.
573@end deffn
574
575@c FIXME::martin: Why aren't these procedure named like the other options
576@c procedures?
577
578@deffn {Scheme Procedure} traps [setting]
579Display the current settings of the evaluator traps options. If
580@var{setting} is omitted, only a short form of the current evaluator
581traps options is printed. Otherwise, @var{setting} should be one of the
582following symbols:
583@table @code
584@item help
585Display the complete option settings.
586@item full
587Like @code{help}, but also print programmer options.
588@end table
589@end deffn
590
591@deffn {Scheme Procedure} trap-enable option-name
592@deffnx {Scheme Procedure} trap-disable option-name
593@deffnx {Scheme Procedure} trap-set! option-name value
594Modify the evaluator options. @code{trap-enable} should be used with boolean
595options and switches them on, @code{trap-disable} switches them off.
596@code{trap-set!} can be used to set an option to a specific value.
597@end deffn
598
599@deffn {Scheme Procedure} evaluator-traps-interface [setting]
600@deffnx {C Function} scm_evaluator_traps (setting)
601Option interface for the evaluator trap options.
602@end deffn
603
604
605@c Local Variables:
606@c TeX-master: "guile.texi"
607@c End: