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