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