A start at syntax-rules docs
[bpt/guile.git] / doc / ref / api-procedures.texi
CommitLineData
07d83abe
MV
1@c -*-texinfo-*-
2@c This is part of the GNU Guile Reference Manual.
e4955559 3@c Copyright (C) 1996, 1997, 2000, 2001, 2002, 2003, 2004, 2009, 2010
07d83abe
MV
4@c Free Software Foundation, Inc.
5@c See the file guile.texi for copying conditions.
6
7@page
e4955559
AW
8@node Procedures
9@section Procedures
07d83abe
MV
10
11@menu
12* Lambda:: Basic procedure creation using lambda.
13* Primitive Procedures:: Procedures defined in C.
00ce5125 14* Compiled Procedures:: Scheme procedures can be compiled.
07d83abe 15* Optional Arguments:: Handling keyword, optional and rest arguments.
f916cbc4 16* Case-lambda:: One function, multiple arities.
07d83abe
MV
17* Procedure Properties:: Procedure properties and meta-information.
18* Procedures with Setters:: Procedures with setters.
07d83abe
MV
19@end menu
20
21
22@node Lambda
23@subsection Lambda: Basic Procedure Creation
24@cindex lambda
25
07d83abe
MV
26A @code{lambda} expression evaluates to a procedure. The environment
27which is in effect when a @code{lambda} expression is evaluated is
28enclosed in the newly created procedure, this is referred to as a
29@dfn{closure} (@pxref{About Closure}).
30
31When a procedure created by @code{lambda} is called with some actual
32arguments, the environment enclosed in the procedure is extended by
33binding the variables named in the formal argument list to new locations
34and storing the actual arguments into these locations. Then the body of
2e4ef7ed 35the @code{lambda} expression is evaluated sequentially. The result of
07d83abe
MV
36the last expression in the procedure body is then the result of the
37procedure invocation.
38
39The following examples will show how procedures can be created using
40@code{lambda}, and what you can do with these procedures.
41
42@lisp
43(lambda (x) (+ x x)) @result{} @r{a procedure}
44((lambda (x) (+ x x)) 4) @result{} 8
45@end lisp
46
47The fact that the environment in effect when creating a procedure is
48enclosed in the procedure is shown with this example:
49
50@lisp
51(define add4
52 (let ((x 4))
53 (lambda (y) (+ x y))))
54(add4 6) @result{} 10
55@end lisp
56
57
58@deffn syntax lambda formals body
59@var{formals} should be a formal argument list as described in the
60following table.
61
62@table @code
63@item (@var{variable1} @dots{})
64The procedure takes a fixed number of arguments; when the procedure is
65called, the arguments will be stored into the newly created location for
66the formal variables.
67@item @var{variable}
68The procedure takes any number of arguments; when the procedure is
69called, the sequence of actual arguments will converted into a list and
70stored into the newly created location for the formal variable.
71@item (@var{variable1} @dots{} @var{variablen} . @var{variablen+1})
72If a space-delimited period precedes the last variable, then the
73procedure takes @var{n} or more variables where @var{n} is the number
74of formal arguments before the period. There must be at least one
75argument before the period. The first @var{n} actual arguments will be
76stored into the newly allocated locations for the first @var{n} formal
77arguments and the sequence of the remaining actual arguments is
78converted into a list and the stored into the location for the last
79formal argument. If there are exactly @var{n} actual arguments, the
80empty list is stored into the location of the last formal argument.
81@end table
82
83The list in @var{variable} or @var{variablen+1} is always newly
84created and the procedure can modify it if desired. This is the case
85even when the procedure is invoked via @code{apply}, the required part
86of the list argument there will be copied (@pxref{Fly Evaluation,,
87Procedures for On the Fly Evaluation}).
88
89@var{body} is a sequence of Scheme expressions which are evaluated in
90order when the procedure is invoked.
91@end deffn
92
93@node Primitive Procedures
94@subsection Primitive Procedures
95@cindex primitives
96@cindex primitive procedures
97
98Procedures written in C can be registered for use from Scheme,
99provided they take only arguments of type @code{SCM} and return
100@code{SCM} values. @code{scm_c_define_gsubr} is likely to be the most
101useful mechanism, combining the process of registration
102(@code{scm_c_make_gsubr}) and definition (@code{scm_define}).
103
104@deftypefun SCM scm_c_make_gsubr (const char *name, int req, int opt, int rst, fcn)
105Register a C procedure @var{FCN} as a ``subr'' --- a primitive
106subroutine that can be called from Scheme. It will be associated with
107the given @var{name} but no environment binding will be created. The
108arguments @var{req}, @var{opt} and @var{rst} specify the number of
109required, optional and ``rest'' arguments respectively. The total
110number of these arguments should match the actual number of arguments
4adf9a7e 111to @var{fcn}, but may not exceed 10. The number of rest arguments should be 0 or 1.
07d83abe
MV
112@code{scm_c_make_gsubr} returns a value of type @code{SCM} which is a
113``handle'' for the procedure.
114@end deftypefun
115
116@deftypefun SCM scm_c_define_gsubr (const char *name, int req, int opt, int rst, fcn)
117Register a C procedure @var{FCN}, as for @code{scm_c_make_gsubr}
118above, and additionally create a top-level Scheme binding for the
119procedure in the ``current environment'' using @code{scm_define}.
120@code{scm_c_define_gsubr} returns a handle for the procedure in the
121same way as @code{scm_c_make_gsubr}, which is usually not further
122required.
123@end deftypefun
124
125@code{scm_c_make_gsubr} and @code{scm_c_define_gsubr} automatically
126use @code{scm_c_make_subr} and also @code{scm_makcclo} if necessary.
127It is advisable to use the gsubr variants since they provide a
128slightly higher-level abstraction of the Guile implementation.
129
00ce5125
AW
130@node Compiled Procedures
131@subsection Compiled Procedures
132
f916cbc4
AW
133In Guile, procedures can be executed by directly interpreting their
134source code. Scheme source code is a set of nested lists, after all,
135with each list representing a procedure call.
136
137Most procedures are compiled, however. This means that Guile has done
138some pre-computation on the procedure, to determine what it will need
139to do each time the procedure runs. Compiled procedures run faster
140than interpreted procedures.
00ce5125 141
f916cbc4
AW
142Loading files is the normal way that compiled procedures come to
143being. If Guile sees that a file is uncompiled, or that its compiled
144file is out of date, it will attempt to compile the file when it is
145loaded, and save the result to disk. Procedures can be compiled at
146runtime as well. @xref{Read/Load/Eval/Compile}, for more information
147on runtime compilation.
00ce5125 148
5a069042
AW
149Compiled procedures, also known as @dfn{programs}, respond all
150procedures that operate on procedures. In addition, there are a few
46d666d4 151more accessors for low-level details on programs.
5a069042 152
46d666d4
AW
153Most people won't need to use the routines described in this section,
154but it's good to have them documented. You'll have to include the
155appropriate module first, though:
5a069042
AW
156
157@example
158(use-modules (system vm program))
159@end example
160
46d666d4
AW
161@deffn {Scheme Procedure} program? obj
162@deffnx {C Function} scm_program_p (obj)
163Returns @code{#t} iff @var{obj} is a compiled procedure.
164@end deffn
165
81fd3152
AW
166@deffn {Scheme Procedure} program-objcode program
167@deffnx {C Function} scm_program_objcode (program)
73643339
AW
168Returns the object code associated with this program. @xref{Bytecode
169and Objcode}, for more information.
46d666d4
AW
170@end deffn
171
172@deffn {Scheme Procedure} program-objects program
173@deffnx {C Function} scm_program_objects (program)
174Returns the ``object table'' associated with this program, as a
175vector. @xref{VM Programs}, for more information.
5a069042
AW
176@end deffn
177
46d666d4
AW
178@deffn {Scheme Procedure} program-module program
179@deffnx {C Function} scm_program_module (program)
81fd3152
AW
180Returns the module that was current when this program was created. Can
181return @code{#f} if the compiler could determine that this information
182was unnecessary.
46d666d4
AW
183@end deffn
184
f916cbc4
AW
185@deffn {Scheme Procedure} program-free-variables program
186@deffnx {C Function} scm_program_free_variables (program)
187Returns the set of free variables that this program captures in its
188closure, as a vector. If a closure is code with data, you can get the
189code from @code{program-objcode}, and the data via
190@code{program-free-variables}.
191
192Some of the values captured are actually in variable ``boxes''.
193@xref{Variables and the VM}, for more information.
46d666d4
AW
194
195Users must not modify the returned value unless they think they're
196really clever.
197@end deffn
198
46d666d4 199@deffn {Scheme Procedure} program-meta program
f916cbc4 200@deffnx {C Function} scm_program_meta (program)
46d666d4
AW
201Return the metadata thunk of @var{program}, or @code{#f} if it has no
202metadata.
203
204When called, a metadata thunk returns a list of the following form:
f916cbc4 205@code{(@var{bindings} @var{sources} @var{arities} . @var{properties})}. The format
46d666d4
AW
206of each of these elements is discussed below.
207@end deffn
208
5a069042 209@deffn {Scheme Procedure} program-bindings program
f916cbc4 210@deffnx {Scheme Procedure} make-binding name boxed? index start end
5a069042 211@deffnx {Scheme Procedure} binding:name binding
f916cbc4 212@deffnx {Scheme Procedure} binding:boxed? binding
5a069042
AW
213@deffnx {Scheme Procedure} binding:index binding
214@deffnx {Scheme Procedure} binding:start binding
215@deffnx {Scheme Procedure} binding:end binding
216Bindings annotations for programs, along with their accessors.
217
218Bindings declare names and liveness extents for block-local variables.
219The best way to see what these are is to play around with them at a
f916cbc4 220REPL. @xref{VM Concepts}, for more information.
5a069042 221
81fd3152
AW
222Note that bindings information is stored in a program as part of its
223metadata thunk, so including it in the generated object code does not
224impose a runtime performance penalty.
5a069042
AW
225@end deffn
226
227@deffn {Scheme Procedure} program-sources program
228@deffnx {Scheme Procedure} source:addr source
229@deffnx {Scheme Procedure} source:line source
230@deffnx {Scheme Procedure} source:column source
231@deffnx {Scheme Procedure} source:file source
232Source location annotations for programs, along with their accessors.
233
46d666d4
AW
234Source location information propagates through the compiler and ends
235up being serialized to the program's metadata. This information is
236keyed by the offset of the instruction pointer within the object code
237of the program. Specifically, it is keyed on the @code{ip} @emph{just
238following} an instruction, so that backtraces can find the source
239location of a call that is in progress.
5a069042
AW
240@end deffn
241
f916cbc4
AW
242@deffn {Scheme Procedure} program-arities program
243@deffnx {C Function} scm_program_arities (program)
244@deffnx {Scheme Procedure} program-arity program ip
245@deffnx {Scheme Procedure} arity:start arity
246@deffnx {Scheme Procedure} arity:end arity
247@deffnx {Scheme Procedure} arity:nreq arity
248@deffnx {Scheme Procedure} arity:nopt arity
249@deffnx {Scheme Procedure} arity:rest? arity
250@deffnx {Scheme Procedure} arity:kw arity
251@deffnx {Scheme Procedure} arity:allow-other-keys? arity
252Accessors for a representation of the ``arity'' of a program.
253
254The normal case is that a procedure has one arity. For example,
255@code{(lambda (x) x)}, takes one required argument, and that's it. One
256could access that number of required arguments via @code{(arity:nreq
257(program-arities (lambda (x) x)))}. Similarly, @code{arity:nopt} gets
258the number of optional arguments, and @code{arity:rest?} returns a true
259value if the procedure has a rest arg.
260
261@code{arity:kw} returns a list of @code{(@var{kw} . @var{idx})} pairs,
262if the procedure has keyword arguments. The @var{idx} refers to the
263@var{idx}th local variable; @xref{Variables and the VM}, for more
264information. Finally @code{arity:allow-other-keys?} returns a true
265value if other keys are allowed. @xref{Optional Arguments}, for more
266information.
267
268So what about @code{arity:start} and @code{arity:end}, then? They
269return the range of bytes in the program's bytecode for which a given
270arity is valid. You see, a procedure can actually have more than one
271arity. The question, ``what is a procedure's arity'' only really makes
272sense at certain points in the program, delimited by these
273@code{arity:start} and @code{arity:end} values.
274@end deffn
275
5a069042 276@deffn {Scheme Procedure} program-properties program
46d666d4
AW
277Return the properties of a @code{program} as an association list,
278keyed by property name (a symbol).
279
280Some interesting properties include:
281@itemize
282@item @code{name}, the name of the procedure
283@item @code{documentation}, the procedure's docstring
284@end itemize
285@end deffn
286
287@deffn {Scheme Procedure} program-property program name
288Access a program's property by name, returning @code{#f} if not found.
289@end deffn
290
291@deffn {Scheme Procedure} program-documentation program
5a069042 292@deffnx {Scheme Procedure} program-name program
46d666d4 293Accessors for specific properties.
5a069042 294@end deffn
00ce5125 295
07d83abe
MV
296@node Optional Arguments
297@subsection Optional Arguments
298
07d83abe
MV
299Scheme procedures, as defined in R5RS, can either handle a fixed number
300of actual arguments, or a fixed number of actual arguments followed by
301arbitrarily many additional arguments. Writing procedures of variable
302arity can be useful, but unfortunately, the syntactic means for handling
303argument lists of varying length is a bit inconvenient. It is possible
f916cbc4 304to give names to the fixed number of arguments, but the remaining
07d83abe
MV
305(optional) arguments can be only referenced as a list of values
306(@pxref{Lambda}).
307
f916cbc4
AW
308For this reason, Guile provides an extension to @code{lambda},
309@code{lambda*}, which allows the user to define procedures with
310optional and keyword arguments. In addition, Guile's virtual machine
311has low-level support for optional and keyword argument dispatch.
312Calls to procedures with optional and keyword arguments can be made
313cheaply, without allocating a rest list.
07d83abe
MV
314
315@menu
f916cbc4
AW
316* lambda* and define*:: Creating advanced argument handling procedures.
317* ice-9 optargs:: (ice-9 optargs) provides some utilities.
07d83abe
MV
318@end menu
319
320
f916cbc4
AW
321@node lambda* and define*
322@subsubsection lambda* and define*.
07d83abe 323
f916cbc4
AW
324@code{lambda*} is like @code{lambda}, except with some extensions to
325allow optional and keyword arguments.
07d83abe 326
edcd3e83
KR
327@deffn {library syntax} lambda* ([var@dots{}] @* [#:optional vardef@dots{}] @* [#:key vardef@dots{} [#:allow-other-keys]] @* [#:rest var | . var]) @* body
328@sp 1
329Create a procedure which takes optional and/or keyword arguments
330specified with @code{#:optional} and @code{#:key}. For example,
07d83abe
MV
331
332@lisp
333(lambda* (a b #:optional c d . e) '())
334@end lisp
335
edcd3e83
KR
336is a procedure with fixed arguments @var{a} and @var{b}, optional
337arguments @var{c} and @var{d}, and rest argument @var{e}. If the
07d83abe
MV
338optional arguments are omitted in a call, the variables for them are
339bound to @code{#f}.
340
f916cbc4
AW
341@fnindex define*
342Likewise, @code{define*} is syntactic sugar for defining procedures
343using @code{lambda*}.
344
345@code{lambda*} can also make procedures with keyword arguments. For
346example, a procedure defined like this:
07d83abe
MV
347
348@lisp
f916cbc4
AW
349(define* (sir-yes-sir #:key action how-high)
350 (list action how-high))
07d83abe
MV
351@end lisp
352
f916cbc4
AW
353can be called as @code{(sir-yes-sir #:action 'jump)},
354@code{(sir-yes-sir #:how-high 13)}, @code{(sir-yes-sir #:action
355'lay-down #:how-high 0)}, or just @code{(sir-yes-sir)}. Whichever
356arguments are given as keywords are bound to values (and those not
357given are @code{#f}).
07d83abe 358
edcd3e83
KR
359Optional and keyword arguments can also have default values to take
360when not present in a call, by giving a two-element list of variable
361name and expression. For example in
07d83abe
MV
362
363@lisp
f916cbc4
AW
364(define* (frob foo #:optional (bar 42) #:key (baz 73))
365 (list foo bar baz))
07d83abe
MV
366@end lisp
367
368@var{foo} is a fixed argument, @var{bar} is an optional argument with
369default value 42, and baz is a keyword argument with default value 73.
edcd3e83
KR
370Default value expressions are not evaluated unless they are needed,
371and until the procedure is called.
07d83abe 372
edcd3e83
KR
373Normally it's an error if a call has keywords other than those
374specified by @code{#:key}, but adding @code{#:allow-other-keys} to the
375definition (after the keyword argument declarations) will ignore
376unknown keywords.
07d83abe 377
edcd3e83
KR
378If a call has a keyword given twice, the last value is used. For
379example,
07d83abe
MV
380
381@lisp
f916cbc4
AW
382(define* (flips #:key (heads 0) (tails 0))
383 (display (list heads tails)))
384
385(flips #:heads 37 #:tails 42 #:heads 99)
edcd3e83 386@print{} (99 42)
07d83abe
MV
387@end lisp
388
edcd3e83
KR
389@code{#:rest} is a synonym for the dotted syntax rest argument. The
390argument lists @code{(a . b)} and @code{(a #:rest b)} are equivalent
391in all respects. This is provided for more similarity to DSSSL,
392MIT-Scheme and Kawa among others, as well as for refugees from other
393Lisp dialects.
394
395When @code{#:key} is used together with a rest argument, the keyword
396parameters in a call all remain in the rest list. This is the same as
397Common Lisp. For example,
07d83abe 398
edcd3e83
KR
399@lisp
400((lambda* (#:key (x 0) #:allow-other-keys #:rest r)
401 (display r))
402 #:x 123 #:y 456)
403@print{} (#:x 123 #:y 456)
404@end lisp
405
406@code{#:optional} and @code{#:key} establish their bindings
f916cbc4
AW
407successively, from left to right. This means default expressions can
408refer back to prior parameters, for example
edcd3e83
KR
409
410@lisp
411(lambda* (start #:optional (end (+ 10 start)))
412 (do ((i start (1+ i)))
413 ((> i end))
414 (display i)))
415@end lisp
f916cbc4
AW
416
417The exception to this left-to-right scoping rule is the rest argument.
418If there is a rest argument, it is bound after the optional arguments,
419but before the keyword arguments.
07d83abe
MV
420@end deffn
421
422
f916cbc4
AW
423@node ice-9 optargs
424@subsubsection (ice-9 optargs)
07d83abe 425
f916cbc4
AW
426Before Guile 2.0, @code{lambda*} and @code{define*} were implemented
427using macros that processed rest list arguments. This was not optimal,
428as calling procedures with optional arguments had to allocate rest
429lists at every procedure invocation. Guile 2.0 improved this
430situation by bringing optional and keyword arguments into Guile's
431core.
07d83abe 432
f916cbc4
AW
433However there are occasions in which you have a list and want to parse
434it for optional or keyword arguments. Guile's @code{(ice-9 optargs)}
435provides some macros to help with that task.
07d83abe 436
f916cbc4
AW
437The syntax @code{let-optional} and @code{let-optional*} are for
438destructuring rest argument lists and giving names to the various list
439elements. @code{let-optional} binds all variables simultaneously, while
440@code{let-optional*} binds them sequentially, consistent with @code{let}
441and @code{let*} (@pxref{Local Bindings}).
442
443@deffn {library syntax} let-optional rest-arg (binding @dots{}) expr @dots{}
444@deffnx {library syntax} let-optional* rest-arg (binding @dots{}) expr @dots{}
445These two macros give you an optional argument interface that is very
446@dfn{Schemey} and introduces no fancy syntax. They are compatible with
447the scsh macros of the same name, but are slightly extended. Each of
448@var{binding} may be of one of the forms @var{var} or @code{(@var{var}
449@var{default-value})}. @var{rest-arg} should be the rest-argument of the
450procedures these are used from. The items in @var{rest-arg} are
451sequentially bound to the variable names are given. When @var{rest-arg}
452runs out, the remaining vars are bound either to the default values or
453@code{#f} if no default value was specified. @var{rest-arg} remains
454bound to whatever may have been left of @var{rest-arg}.
07d83abe 455
f916cbc4
AW
456After binding the variables, the expressions @var{expr} @dots{} are
457evaluated in order.
458@end deffn
07d83abe 459
f916cbc4
AW
460Similarly, @code{let-keywords} and @code{let-keywords*} extract values
461from keyword style argument lists, binding local variables to those
462values or to defaults.
07d83abe 463
f916cbc4
AW
464@deffn {library syntax} let-keywords args allow-other-keys? (binding @dots{}) body @dots{}
465@deffnx {library syntax} let-keywords* args allow-other-keys? (binding @dots{}) body @dots{}
466@var{args} is evaluated and should give a list of the form
467@code{(#:keyword1 value1 #:keyword2 value2 @dots{})}. The
468@var{binding}s are variables and default expressions, with the
469variables to be set (by name) from the keyword values. The @var{body}
470forms are then evaluated and the last is the result. An example will
471make the syntax clearest,
472
473@example
474(define args '(#:xyzzy "hello" #:foo "world"))
07d83abe 475
f916cbc4
AW
476(let-keywords args #t
477 ((foo "default for foo")
478 (bar (string-append "default" "for" "bar")))
479 (display foo)
480 (display ", ")
481 (display bar))
482@print{} world, defaultforbar
483@end example
07d83abe 484
f916cbc4
AW
485The binding for @code{foo} comes from the @code{#:foo} keyword in
486@code{args}. But the binding for @code{bar} is the default in the
487@code{let-keywords}, since there's no @code{#:bar} in the args.
488
489@var{allow-other-keys?} is evaluated and controls whether unknown
490keywords are allowed in the @var{args} list. When true other keys are
491ignored (such as @code{#:xyzzy} in the example), when @code{#f} an
492error is thrown for anything unknown.
493@end deffn
494
495@code{(ice-9 optargs)} also provides some more @code{define*} sugar,
496which is not so useful with modern Guile coding, but still supported:
497@code{define*-public} is the @code{lambda*} version of
498@code{define-public}; @code{defmacro*} and @code{defmacro*-public}
499exist for defining macros with the improved argument list handling
500possibilities. The @code{-public} versions not only define the
501procedures/macros, but also export them from the current module.
502
503@deffn {library syntax} define*-public formals body
504Like a mix of @code{define*} and @code{define-public}.
07d83abe
MV
505@end deffn
506
507@deffn {library syntax} defmacro* name formals body
508@deffnx {library syntax} defmacro*-public name formals body
509These are just like @code{defmacro} and @code{defmacro-public} except that they
510take @code{lambda*}-style extended parameter lists, where @code{#:optional},
511@code{#:key}, @code{#:allow-other-keys} and @code{#:rest} are allowed with the usual
512semantics. Here is an example of a macro with an optional argument:
513
514@lisp
515(defmacro* transmorgify (a #:optional b)
f916cbc4 516 (a 1))
07d83abe
MV
517@end lisp
518@end deffn
519
f916cbc4
AW
520@node Case-lambda
521@subsection Case-lambda
522@cindex SRFI-16
523@cindex variable arity
524@cindex arity, variable
525
526R5RS's rest arguments are indeed useful and very general, but they
527often aren't the most appropriate or efficient means to get the job
528done. For example, @code{lambda*} is a much better solution to the
529optional argument problem than @code{lambda} with rest arguments.
530
531@fnindex case-lambda
532Likewise, @code{case-lambda} works well for when you want one
533procedure to do double duty (or triple, or ...), without the penalty
534of consing a rest list.
535
536For example:
537
538@lisp
539(define (make-accum n)
540 (case-lambda
541 (() n)
542 ((m) (set! n (+ n m)) n)))
543
544(define a (make-accum 20))
545(a) @result{} 20
546(a 10) @result{} 30
547(a) @result{} 30
548@end lisp
549
550The value returned by a @code{case-lambda} form is a procedure which
551matches the number of actual arguments against the formals in the
552various clauses, in order. The first matching clause is selected, the
553corresponding values from the actual parameter list are bound to the
554variable names in the clauses and the body of the clause is evaluated.
555If no clause matches, an error is signalled.
556
557The syntax of the @code{case-lambda} form is defined in the following
558EBNF grammar. @dfn{Formals} means a formal argument list just like
559with @code{lambda} (@pxref{Lambda}).
560
561@example
562@group
563<case-lambda>
564 --> (case-lambda <case-lambda-clause>)
565<case-lambda-clause>
566 --> (<formals> <definition-or-command>*)
567<formals>
568 --> (<identifier>*)
569 | (<identifier>* . <identifier>)
570 | <identifier>
571@end group
572@end example
573
574Rest lists can be useful with @code{case-lambda}:
575
576@lisp
577(define plus
578 (case-lambda
579 (() 0)
580 ((a) a)
581 ((a b) (+ a b))
582 ((a b . rest) (apply plus (+ a b) rest))))
583(plus 1 2 3) @result{} 6
584@end lisp
585
586@fnindex case-lambda*
587Also, for completeness. Guile defines @code{case-lambda*} as well,
588which is like @code{case-lambda}, except with @code{lambda*} clauses.
589A @code{case-lambda*} clause matches if the arguments fill the
590required arguments, but are not too many for the optional and/or rest
591arguments.
592
f916cbc4
AW
593Keyword arguments are possible with @code{case-lambda*}, but they do
594not contribute to the ``matching'' behavior. That is to say,
595@code{case-lambda*} matches only on required, optional, and rest
596arguments, and on the predicate; keyword arguments may be present but
597do not contribute to the ``success'' of a match. In fact a bad keyword
598argument list may cause an error to be raised.
07d83abe
MV
599
600@node Procedure Properties
601@subsection Procedure Properties and Meta-information
602
f916cbc4
AW
603In addition to the information that is strictly necessary to run,
604procedures may have other associated information. For example, the
605name of a procedure is information not for the procedure, but about
606the procedure. This meta-information can be accessed via the procedure
607properties interface.
07d83abe 608
f916cbc4
AW
609The first group of procedures in this meta-interface are predicates to
610test whether a Scheme object is a procedure, or a special procedure,
611respectively. @code{procedure?} is the most general predicates, it
612returns @code{#t} for any kind of procedure. @code{closure?} does not
613return @code{#t} for primitive procedures, and @code{thunk?} only
614returns @code{#t} for procedures which do not accept any arguments.
07d83abe
MV
615
616@rnindex procedure?
617@deffn {Scheme Procedure} procedure? obj
618@deffnx {C Function} scm_procedure_p (obj)
619Return @code{#t} if @var{obj} is a procedure.
620@end deffn
621
622@deffn {Scheme Procedure} closure? obj
623@deffnx {C Function} scm_closure_p (obj)
f916cbc4
AW
624Return @code{#t} if @var{obj} is a closure. This category somewhat
625misnamed, actually, as it applies only to interpreted procedures, not
626compiled procedures. But since it has historically been used more to
627select on implementation details than on essence (closure or not), we
628keep it here for compatibility. Don't use it in new code, though.
07d83abe
MV
629@end deffn
630
631@deffn {Scheme Procedure} thunk? obj
632@deffnx {C Function} scm_thunk_p (obj)
633Return @code{#t} if @var{obj} is a thunk.
634@end deffn
635
07d83abe 636@cindex procedure properties
f916cbc4
AW
637Procedure properties are general properties associated with
638procedures. These can be the name of a procedure or other relevant
07d83abe
MV
639information, such as debug hints.
640
641@deffn {Scheme Procedure} procedure-name proc
642@deffnx {C Function} scm_procedure_name (proc)
643Return the name of the procedure @var{proc}
644@end deffn
645
646@deffn {Scheme Procedure} procedure-source proc
647@deffnx {C Function} scm_procedure_source (proc)
f916cbc4
AW
648Return the source of the procedure @var{proc}. Returns @code{#f} if
649the source code is not available.
07d83abe
MV
650@end deffn
651
652@deffn {Scheme Procedure} procedure-environment proc
653@deffnx {C Function} scm_procedure_environment (proc)
f916cbc4 654Return the environment of the procedure @var{proc}. Very deprecated.
07d83abe
MV
655@end deffn
656
657@deffn {Scheme Procedure} procedure-properties proc
658@deffnx {C Function} scm_procedure_properties (proc)
f916cbc4
AW
659Return the properties associated with @var{proc}, as an association
660list.
07d83abe
MV
661@end deffn
662
f916cbc4
AW
663@deffn {Scheme Procedure} procedure-property proc key
664@deffnx {C Function} scm_procedure_property (proc, key)
665Return the property of @var{proc} with name @var{key}.
07d83abe
MV
666@end deffn
667
668@deffn {Scheme Procedure} set-procedure-properties! proc alist
669@deffnx {C Function} scm_set_procedure_properties_x (proc, alist)
f916cbc4 670Set @var{proc}'s property list to @var{alist}.
07d83abe
MV
671@end deffn
672
f916cbc4
AW
673@deffn {Scheme Procedure} set-procedure-property! proc key value
674@deffnx {C Function} scm_set_procedure_property_x (proc, key, value)
675In @var{proc}'s property list, set the property named @var{key} to
07d83abe
MV
676@var{value}.
677@end deffn
678
679@cindex procedure documentation
680Documentation for a procedure can be accessed with the procedure
681@code{procedure-documentation}.
682
683@deffn {Scheme Procedure} procedure-documentation proc
684@deffnx {C Function} scm_procedure_documentation (proc)
685Return the documentation string associated with @code{proc}. By
686convention, if a procedure contains more than one expression and the
687first expression is a string constant, that string is assumed to contain
688documentation for that procedure.
689@end deffn
690
07d83abe
MV
691
692@node Procedures with Setters
693@subsection Procedures with Setters
694
695@c FIXME::martin: Review me!
696
697@c FIXME::martin: Document `operator struct'.
698
699@cindex procedure with setter
700@cindex setter
701A @dfn{procedure with setter} is a special kind of procedure which
702normally behaves like any accessor procedure, that is a procedure which
703accesses a data structure. The difference is that this kind of
704procedure has a so-called @dfn{setter} attached, which is a procedure
705for storing something into a data structure.
706
707Procedures with setters are treated specially when the procedure appears
708in the special form @code{set!} (REFFIXME). How it works is best shown
709by example.
710
711Suppose we have a procedure called @code{foo-ref}, which accepts two
712arguments, a value of type @code{foo} and an integer. The procedure
713returns the value stored at the given index in the @code{foo} object.
714Let @code{f} be a variable containing such a @code{foo} data
715structure.@footnote{Working definitions would be:
716@lisp
717(define foo-ref vector-ref)
718(define foo-set! vector-set!)
719(define f (make-vector 2 #f))
720@end lisp
721}
722
723@lisp
724(foo-ref f 0) @result{} bar
725(foo-ref f 1) @result{} braz
726@end lisp
727
728Also suppose that a corresponding setter procedure called
729@code{foo-set!} does exist.
730
731@lisp
732(foo-set! f 0 'bla)
733(foo-ref f 0) @result{} bla
734@end lisp
735
736Now we could create a new procedure called @code{foo}, which is a
737procedure with setter, by calling @code{make-procedure-with-setter} with
738the accessor and setter procedures @code{foo-ref} and @code{foo-set!}.
739Let us call this new procedure @code{foo}.
740
741@lisp
742(define foo (make-procedure-with-setter foo-ref foo-set!))
743@end lisp
744
745@code{foo} can from now an be used to either read from the data
746structure stored in @code{f}, or to write into the structure.
747
748@lisp
749(set! (foo f 0) 'dum)
750(foo f 0) @result{} dum
751@end lisp
752
753@deffn {Scheme Procedure} make-procedure-with-setter procedure setter
754@deffnx {C Function} scm_make_procedure_with_setter (procedure, setter)
755Create a new procedure which behaves like @var{procedure}, but
756with the associated setter @var{setter}.
757@end deffn
758
759@deffn {Scheme Procedure} procedure-with-setter? obj
760@deffnx {C Function} scm_procedure_with_setter_p (obj)
761Return @code{#t} if @var{obj} is a procedure with an
762associated setter procedure.
763@end deffn
764
765@deffn {Scheme Procedure} procedure proc
766@deffnx {C Function} scm_procedure (proc)
3323ec06
NJ
767Return the procedure of @var{proc}, which must be an
768applicable struct.
07d83abe
MV
769@end deffn
770
771@deffn {Scheme Procedure} setter proc
772Return the setter of @var{proc}, which must be either a procedure with
773setter or an operator struct.
774@end deffn
775
776
07d83abe
MV
777@c Local Variables:
778@c TeX-master: "guile.texi"
779@c End: