Spell check.
[bpt/guile.git] / doc / ref / scheme-procedures.texi
1 @page
2 @node Procedures and Macros
3 @chapter Procedures and Macros
4
5 @menu
6 * Lambda:: Basic procedure creation using lambda.
7 * Optional Arguments:: Handling keyword, optional and rest arguments.
8 * Procedure Properties:: Procedure properties and meta-information.
9 * Procedures with Setters:: Procedures with setters.
10 * Macros:: Lisp style macro definitions.
11 * Syntax Rules:: Support for R5RS @code{syntax-rules}.
12 * Syntax Case:: Support for the @code{syntax-case} system.
13 * Internal Macros:: Guile's internal representation.
14 @end menu
15
16
17 @node Lambda
18 @section Lambda: Basic Procedure Creation
19
20 @c FIXME::martin: Review me!
21
22 A @code{lambda} expression evaluates to a procedure. The environment
23 which is in effect when a @code{lambda} expression is evaluated is
24 enclosed in the newly created procedure, this is referred to as a
25 @dfn{closure} (@pxref{About Closure}).
26
27 When a procedure created by @code{lambda} is called with some actual
28 arguments, the environment enclosed in the procedure is extended by
29 binding the variables named in the formal argument list to new locations
30 and storing the actual arguments into these locations. Then the body of
31 the @code{lambda} expression is evaluation sequentially. The result of
32 the last expression in the procedure body is then the result of the
33 procedure invocation.
34
35 The following examples will show how procedures can be created using
36 @code{lambda}, and what you can do with these procedures.
37
38 @lisp
39 (lambda (x) (+ x x)) @result{} @r{a procedure}
40 ((lambda (x) (+ x x)) 4) @result{} 8
41 @end lisp
42
43 The fact that the environment in effect when creating a procedure is
44 enclosed in the procedure is shown with this example:
45
46 @lisp
47 (define add4
48 (let ((x 4))
49 (lambda (y) (+ x y))))
50 (add4 6) @result{} 10
51 @end lisp
52
53
54 @deffn syntax lambda formals body
55 @var{formals} should be a formal argument list as described in the
56 following table.
57
58 @table @code
59 @item (@var{variable1} @dots{})
60 The procedure takes a fixed number of arguments; when the procedure is
61 called, the arguments will be stored into the newly created location for
62 the formal variables.
63 @item @var{variable}
64 The procedure takes any number of arguments; when the procedure is
65 called, the sequence of actual arguments will converted into a list and
66 stored into the newly created location for the formal variable.
67 @item (@var{variable1} @dots{} @var{variablen} . @var{variablen+1})
68 If a space-delimited period precedes the last variable, then the
69 procedure takes @var{n} or more variables where @var{n} is the number
70 of formal arguments before the period. There must be at least one
71 argument before the period. The first @var{n} actual arguments will be
72 stored into the newly allocated locations for the first @var{n} formal
73 arguments and the sequence of the remaining actual arguments is
74 converted into a list and the stored into the location for the last
75 formal argument. If there are exactly @var{n} actual arguments, the
76 empty list is stored into the location of the last formal argument.
77 @end table
78
79 @var{body} is a sequence of Scheme expressions which are evaluated in
80 order when the procedure is invoked.
81 @end deffn
82
83
84 @node Optional Arguments
85 @section Optional Arguments
86
87 @c FIXME::martin: Review me!
88
89 Scheme procedures, as defined in R5RS, can either handle a fixed number
90 of actual arguments, or a fixed number of actual arguments followed by
91 arbitrarily many additional arguments. Writing procedures of variable
92 arity can be useful, but unfortunately, the syntactic means for handling
93 argument lists of varying length is a bit inconvenient. It is possible
94 to give names to the fixed number of argument, but the remaining
95 (optional) arguments can be only referenced as a list of values
96 (@pxref{Lambda}).
97
98 Guile comes with the module @code{(ice-9 optargs)}, which makes using
99 optional arguments much more convenient. In addition, this module
100 provides syntax for handling keywords in argument lists
101 (@pxref{Keywords}).
102
103 Before using any of the procedures or macros defined in this section,
104 you have to load the module @code{(ice-9 optargs)} with the statement:
105
106 @lisp
107 (use-modules (ice-9 optargs))
108 @end lisp
109
110 @menu
111 * let-optional Reference:: Locally binding optional arguments.
112 * let-keywords Reference:: Locally binding keywords arguments.
113 * lambda* Reference:: Creating advanced argument handling procedures.
114 * define* Reference:: Defining procedures and macros.
115 @end menu
116
117
118 @node let-optional Reference
119 @subsection let-optional Reference
120
121 @c FIXME::martin: Review me!
122
123 The syntax @code{let-optional} and @code{let-optional*} are for
124 destructuring rest argument lists and giving names to the various list
125 elements. @code{let-optional} binds all variables simultaneously, while
126 @code{let-optional*} binds them sequentially, consistent with @code{let}
127 and @code{let*} (@pxref{Local Bindings}).
128
129 @deffn {library syntax} let-optional rest-arg (binding @dots{}) expr @dots{}
130 @deffnx {library syntax} let-optional* rest-arg (binding @dots{}) expr @dots{}
131 These two macros give you an optional argument interface that is very
132 @dfn{Schemey} and introduces no fancy syntax. They are compatible with
133 the scsh macros of the same name, but are slightly extended. Each of
134 @var{binding} may be of one of the forms @var{var} or @code{(@var{var}
135 @var{default-value})}. @var{rest-arg} should be the rest-argument of the
136 procedures these are used from. The items in @var{rest-arg} are
137 sequentially bound to the variable names are given. When @var{rest-arg}
138 runs out, the remaining vars are bound either to the default values or
139 left unbound if no default value was specified. @var{rest-arg} remains
140 bound to whatever may have been left of @var{rest-arg}.
141
142 After binding the variables, the expressions @var{expr} @dots{} are
143 evaluated in order.
144 @end deffn
145
146
147 @node let-keywords Reference
148 @subsection let-keywords Reference
149
150 @c FIXME::martin: Review me!
151
152 @code{let-keywords} and @code{let-keywords*} are used for extracting
153 values from argument lists which use keywords instead of argument
154 position for binding local variables to argument values.
155
156 @code{let-keywords} binds all variables simultaneously, while
157 @code{let-keywords*} binds them sequentially, consistent with @code{let}
158 and @code{let*} (@pxref{Local Bindings}).
159
160 @deffn {library syntax} let-keywords rest-arg allow-other-keys? (binding @dots{}) expr @dots{}
161 @deffnx {library syntax} let-keywords rest-arg allow-other-keys? (binding @dots{}) expr @dots{}
162 These macros pick out keyword arguments from @var{rest-arg}, but do not
163 modify it. This is consistent at least with Common Lisp, which
164 duplicates keyword arguments in the rest argument. More explanation of what
165 keyword arguments in a lambda list look like can be found below in
166 the documentation for @code{lambda*}
167 (@pxref{lambda* Reference}). @var{binding}s can have the same form as
168 for @code{let-optional}. If @var{allow-other-keys?} is false, an error
169 will be thrown if anything that looks like a keyword argument but does
170 not match a known keyword parameter will result in an error.
171
172 After binding the variables, the expressions @var{expr} @dots{} are
173 evaluated in order.
174 @end deffn
175
176
177 @node lambda* Reference
178 @subsection lambda* Reference
179
180 @c FIXME::martin: Review me!
181
182 When using optional and keyword argument lists, using @code{lambda} for
183 creating procedures and using @code{let-optional} or @code{let-keywords}
184 is a bit lengthy. Therefore, @code{lambda*} is provided, which combines
185 the features of those macros into a single convenient syntax.
186
187 For quick reference, here is the syntax of the formal argument list for
188 @code{lambda*} (brackets are used to indicate grouping only):
189
190 @example
191 ext-param-list ::= [identifier]* [#:optional [ext-var-decl]+]?
192 [#:key [ext-var-decl]+ [#:allow-other-keys]?]?
193 [[#:rest identifier]|[. identifier]]?
194
195 ext-var-decl ::= identifier | ( identifier expression )
196 @end example
197
198 The characters `*', `+' and `?' are not to be taken literally; they mean
199 respectively, zero or more occurrences, one or more occurrences, and one
200 or zero occurrences.
201
202 @deffn {library syntax} lambda* formals body
203 @code{lambda*} creates a procedure that takes optional arguments. These
204 are specified by putting them inside brackets at the end of the
205 parameter list, but before any dotted rest argument. For example,
206
207 @lisp
208 (lambda* (a b #:optional c d . e) '())
209 @end lisp
210
211 creates a procedure with fixed arguments @var{a} and @var{b}, optional
212 arguments @var{c} and @var{d}, and rest argument @var{e}. If the
213 optional arguments are omitted in a call, the variables for them are
214 unbound in the procedure. This can be checked with the @code{bound?}
215 macro (documented below).
216
217 @code{lambda*} can also take keyword arguments. For example, a procedure
218 defined like this:
219
220 @lisp
221 (lambda* (#:key xyzzy larch) '())
222 @end lisp
223
224 can be called with any of the argument lists @code{(#:xyzzy 11)}
225 @code{(#:larch 13)} @code{(#:larch 42 #:xyzzy 19)} @code{()}. Whichever
226 arguments are given as keywords are bound to values.
227
228 Optional and keyword arguments can also be given default values
229 which they take on when they are not present in a call, by giving a
230 two-item list in place of an optional argument, for example in:
231
232 @lisp
233 (lambda* (foo #:optional (bar 42) #:key (baz 73))
234 (list foo bar baz))
235 @end lisp
236
237 @var{foo} is a fixed argument, @var{bar} is an optional argument with
238 default value 42, and baz is a keyword argument with default value 73.
239 Default value expressions are not evaluated unless they are needed and
240 until the procedure is called.
241
242 @code{lambda*} also supports two more special parameter list keywords.
243
244 @code{lambda*}-defined procedures now throw an error by default if a
245 keyword other than one of those specified is found in the actual
246 passed arguments. However, specifying @code{#:allow-other-keys}
247 immediately after the keyword argument declarations restores the
248 previous behavior of ignoring unknown keywords. @code{lambda*} also now
249 guarantees that if the same keyword is passed more than once, the
250 last one passed is the one that takes effect. For example,
251
252 @lisp
253 ((lambda* (#:key (heads 0) (tails 0)) (display (list heads tails)))
254 #:heads 37 #:tails 42 #:heads 99)
255 @end lisp
256
257 would result in (99 47) being displayed.
258
259 @code{#:rest} is also now provided as a synonym for the dotted syntax
260 rest argument. The argument lists @code{(a . b)} and @code{(a #:rest b)}
261 are equivalent in all respects to @code{lambda*}. This is provided for
262 more similarity to DSSSL, MIT-Scheme and Kawa among others, as well as
263 for refugees from other Lisp dialects.
264 @end deffn
265
266 @deffn {library syntax} bound? variable
267 Check if a variable is bound in the current environment.
268
269 The procedure @code{defined?} doesn't quite cut it as it stands, since
270 it only checks bindings in the top-level environment, not those in local
271 scope only.
272 @end deffn
273
274
275 @node define* Reference
276 @subsection define* Reference
277
278 @c FIXME::martin: Review me!
279
280 Just like @code{define} has a shorthand notation for defining procedures
281 (@pxref{Lambda Alternatives}), @code{define*} is provided as an
282 abbreviation of the combination of @code{define} and @code{lambda*}.
283
284 @code{define*-public} is the @code{lambda*} version of
285 @code{define-public}; @code{defmacro*} and @code{defmacro*-public} exist
286 for defining macros with the improved argument list handling
287 possibilities. The @code{-public} versions not only define the
288 procedures/macros, but also export them from the current module.
289
290 @deffn {library syntax} define* formals body
291 @deffnx {library syntax} define*-public formals body
292 @code{define*} and @code{define*-public} support optional arguments with
293 a similar syntax to @code{lambda*}. They also support arbitrary-depth
294 currying, just like Guile's define. Some examples:
295
296 @lisp
297 (define* (x y #:optional a (z 3) #:key w . u)
298 (display (list y z u)))
299 @end lisp
300 defines a procedure @code{x} with a fixed argument @var{y}, an optional
301 argument @var{a}, another optional argument @var{z} with default value 3,
302 a keyword argument @var{w}, and a rest argument @var{u}.
303
304 @lisp
305 (define-public* ((foo #:optional bar) #:optional baz) '())
306 @end lisp
307
308 This illustrates currying. A procedure @code{foo} is defined, which,
309 when called with an optional argument @var{bar}, returns a procedure
310 that takes an optional argument @var{baz}.
311
312 Of course, @code{define*[-public]} also supports @code{#:rest} and
313 @code{#:allow-other-keys} in the same way as @code{lambda*}.
314 @end deffn
315
316 @deffn {library syntax} defmacro* name formals body
317 @deffnx {library syntax} defmacro*-public name formals body
318 These are just like @code{defmacro} and @code{defmacro-public} except that they
319 take @code{lambda*}-style extended parameter lists, where @code{#:optional},
320 @code{#:key}, @code{#:allow-other-keys} and @code{#:rest} are allowed with the usual
321 semantics. Here is an example of a macro with an optional argument:
322
323 @lisp
324 (defmacro* transmorgify (a #:optional b)
325 (a 1))
326 @end lisp
327 @end deffn
328
329
330 @node Procedure Properties
331 @section Procedure Properties and Meta-information
332
333 @c FIXME::martin: Review me!
334
335 Procedures always have attached the environment in which they were
336 created and information about how to apply them to actual arguments. In
337 addition to that, properties and meta-information can be stored with
338 procedures. The procedures in this section can be used to test whether
339 a given procedure satisfies a condition; and to access and set a
340 procedure's property.
341
342 The first group of procedures are predicates to test whether a Scheme
343 object is a procedure, or a special procedure, respectively.
344 @code{procedure?} is the most general predicates, it returns @code{#t}
345 for any kind of procedure. @code{closure?} does not return @code{#t}
346 for primitive procedures, and @code{thunk?} only returns @code{#t} for
347 procedures which do not accept any arguments.
348
349 @rnindex procedure?
350 @deffn {Scheme Procedure} procedure? obj
351 @deffnx {C Function} scm_procedure_p (obj)
352 Return @code{#t} if @var{obj} is a procedure.
353 @end deffn
354
355 @deffn {Scheme Procedure} closure? obj
356 @deffnx {C Function} scm_closure_p (obj)
357 Return @code{#t} if @var{obj} is a closure.
358 @end deffn
359
360 @deffn {Scheme Procedure} thunk? obj
361 @deffnx {C Function} scm_thunk_p (obj)
362 Return @code{#t} if @var{obj} is a thunk.
363 @end deffn
364
365 @c FIXME::martin: Is that true?
366 @cindex procedure properties
367 Procedure properties are general properties to be attached to
368 procedures. These can be the name of a procedure or other relevant
369 information, such as debug hints.
370
371 @deffn {Scheme Procedure} procedure-properties proc
372 @deffnx {C Function} scm_procedure_properties (proc)
373 Return @var{obj}'s property list.
374 @end deffn
375
376 @deffn {Scheme Procedure} procedure-property obj key
377 @deffnx {C Function} scm_procedure_property (obj, key)
378 Return the property of @var{obj} with name @var{key}.
379 @end deffn
380
381 @deffn {Scheme Procedure} set-procedure-properties! proc alist
382 @deffnx {C Function} scm_set_procedure_properties_x (proc, alist)
383 Set @var{obj}'s property list to @var{alist}.
384 @end deffn
385
386 @deffn {Scheme Procedure} set-procedure-property! obj key value
387 @deffnx {C Function} scm_set_procedure_property_x (obj, key, value)
388 In @var{obj}'s property list, set the property named @var{key} to
389 @var{value}.
390 @end deffn
391
392 @cindex procedure documentation
393 Documentation for a procedure can be accessed with the procedure
394 @code{procedure-documentation}.
395
396 @deffn {Scheme Procedure} procedure-documentation proc
397 @deffnx {C Function} scm_procedure_documentation (proc)
398 Return the documentation string associated with @code{proc}. By
399 convention, if a procedure contains more than one expression and the
400 first expression is a string constant, that string is assumed to contain
401 documentation for that procedure.
402 @end deffn
403
404 @cindex source properties
405 @c FIXME::martin: Is the following true?
406 Source properties are properties which are related to the source code of
407 a procedure, such as the line and column numbers, the file name etc.
408
409 @deffn {Scheme Procedure} set-source-properties! obj plist
410 @deffnx {C Function} scm_set_source_properties_x (obj, plist)
411 Install the association list @var{plist} as the source property
412 list for @var{obj}.
413 @end deffn
414
415 @deffn {Scheme Procedure} set-source-property! obj key datum
416 @deffnx {C Function} scm_set_source_property_x (obj, key, datum)
417 Set the source property of object @var{obj}, which is specified by
418 @var{key} to @var{datum}. Normally, the key will be a symbol.
419 @end deffn
420
421 @deffn {Scheme Procedure} source-properties obj
422 @deffnx {C Function} scm_source_properties (obj)
423 Return the source property association list of @var{obj}.
424 @end deffn
425
426
427 @deffn {Scheme Procedure} source-property obj key
428 @deffnx {C Function} scm_source_property (obj, key)
429 Return the source property specified by @var{key} from
430 @var{obj}'s source property list.
431 @end deffn
432
433
434 @node Procedures with Setters
435 @section Procedures with Setters
436
437 @c FIXME::martin: Review me!
438
439 @c FIXME::martin: Document `operator struct'.
440
441 @cindex procedure with setter
442 @cindex setter
443 A @dfn{procedure with setter} is a special kind of procedure which
444 normally behaves like any accessor procedure, that is a procedure which
445 accesses a data structure. The difference is that this kind of
446 procedure has a so-called @dfn{setter} attached, which is a procedure
447 for storing something into a data structure.
448
449 Procedures with setters are treated specially when the procedure appears
450 in the special form @code{set!} (REFFIXME). How it works is best shown
451 by example.
452
453 Suppose we have a procedure called @code{foo-ref}, which accepts two
454 arguments, a value of type @code{foo} and an integer. The procedure
455 returns the value stored at the given index in the @code{foo} object.
456 Let @code{f} be a variable containing such a @code{foo} data
457 structure.@footnote{Working definitions would be:
458 @lisp
459 (define foo-ref vector-ref)
460 (define foo-set! vector-set!)
461 (define f (make-vector 2 #f))
462 @end lisp
463 }
464
465 @lisp
466 (foo-ref f 0) @result{} bar
467 (foo-ref f 1) @result{} braz
468 @end lisp
469
470 Also suppose that a corresponding setter procedure called
471 @code{foo-set!} does exist.
472
473 @lisp
474 (foo-set! f 0 'bla)
475 (foo-ref f 0) @result{} bla
476 @end lisp
477
478 Now we could create a new procedure called @code{foo}, which is a
479 procedure with setter, by calling @code{make-procedure-with-setter} with
480 the accessor and setter procedures @code{foo-ref} and @code{foo-set!}.
481 Let us call this new procedure @code{foo}.
482
483 @lisp
484 (define foo (make-procedure-with-setter foo-ref foo-set!))
485 @end lisp
486
487 @code{foo} can from now an be used to either read from the data
488 structure stored in @code{f}, or to write into the structure.
489
490 @lisp
491 (set! (foo f 0) 'dum)
492 (foo f 0) @result{} dum
493 @end lisp
494
495 @deffn {Scheme Procedure} make-procedure-with-setter procedure setter
496 @deffnx {C Function} scm_make_procedure_with_setter (procedure, setter)
497 Create a new procedure which behaves like @var{procedure}, but
498 with the associated setter @var{setter}.
499 @end deffn
500
501 @deffn {Scheme Procedure} procedure-with-setter? obj
502 @deffnx {C Function} scm_procedure_with_setter_p (obj)
503 Return @code{#t} if @var{obj} is a procedure with an
504 associated setter procedure.
505 @end deffn
506
507 @deffn {Scheme Procedure} procedure proc
508 @deffnx {C Function} scm_procedure (proc)
509 Return the procedure of @var{proc}, which must be either a
510 procedure with setter, or an operator struct.
511 @end deffn
512
513 @deffn {Scheme Procedure} setter proc
514 Return the setter of @var{proc}, which must be either a procedure with
515 setter or an operator struct.
516 @end deffn
517
518
519 @node Macros
520 @section Lisp Style Macro Definitions
521
522 @cindex macros
523 @cindex transformation
524 Macros are objects which cause the expression that they appear in to be
525 transformed in some way @emph{before} being evaluated. In expressions
526 that are intended for macro transformation, the identifier that names
527 the relevant macro must appear as the first element, like this:
528
529 @lisp
530 (@var{macro-name} @var{macro-args} @dots{})
531 @end lisp
532
533 In Lisp-like languages, the traditional way to define macros is very
534 similar to procedure definitions. The key differences are that the
535 macro definition body should return a list that describes the
536 transformed expression, and that the definition is marked as a macro
537 definition (rather than a procedure definition) by the use of a
538 different definition keyword: in Lisp, @code{defmacro} rather than
539 @code{defun}, and in Scheme, @code{define-macro} rather than
540 @code{define}.
541
542 @fnindex defmacro
543 @fnindex define-macro
544 Guile supports this style of macro definition using both @code{defmacro}
545 and @code{define-macro}. The only difference between them is how the
546 macro name and arguments are grouped together in the definition:
547
548 @lisp
549 (defmacro @var{name} (@var{args} @dots{}) @var{body} @dots{})
550 @end lisp
551
552 @noindent
553 is the same as
554
555 @lisp
556 (define-macro (@var{name} @var{args} @dots{}) @var{body} @dots{})
557 @end lisp
558
559 @noindent
560 The difference is analogous to the corresponding difference between
561 Lisp's @code{defun} and Scheme's @code{define}.
562
563 @code{false-if-exception}, from the @file{boot-9.scm} file in the Guile
564 distribution, is a good example of macro definition using
565 @code{defmacro}:
566
567 @lisp
568 (defmacro false-if-exception (expr)
569 `(catch #t
570 (lambda () ,expr)
571 (lambda args #f)))
572 @end lisp
573
574 @noindent
575 The effect of this definition is that expressions beginning with the
576 identifier @code{false-if-exception} are automatically transformed into
577 a @code{catch} expression following the macro definition specification.
578 For example:
579
580 @lisp
581 (false-if-exception (open-input-file "may-not-exist"))
582 @equiv{}
583 (catch #t
584 (lambda () (open-input-file "may-not-exist"))
585 (lambda args #f))
586 @end lisp
587
588
589 @node Syntax Rules
590 @section The R5RS @code{syntax-rules} System
591 @cindex R5RS syntax-rules system
592
593 R5RS defines an alternative system for macro and syntax transformations
594 using the keywords @code{define-syntax}, @code{let-syntax},
595 @code{letrec-syntax} and @code{syntax-rules}.
596
597 The main difference between the R5RS system and the traditional macros
598 of the previous section is how the transformation is specified. In
599 R5RS, rather than permitting a macro definition to return an arbitrary
600 expression, the transformation is specified in a pattern language that
601
602 @itemize @bullet
603 @item
604 does not require complicated quoting and extraction of components of the
605 source expression using @code{caddr} etc.
606
607 @item
608 is designed such that the bindings associated with identifiers in the
609 transformed expression are well defined, and such that it is impossible
610 for the transformed expression to construct new identifiers.
611 @end itemize
612
613 @noindent
614 The last point is commonly referred to as being @dfn{hygienic}: the R5RS
615 @code{syntax-case} system provides @dfn{hygienic macros}.
616
617 For example, the R5RS pattern language for the @code{false-if-exception}
618 example of the previous section looks like this:
619
620 @lisp
621 (syntax-rules ()
622 ((_ expr)
623 (catch #t
624 (lambda () expr)
625 (lambda args #f))))
626 @end lisp
627
628 In Guile, the @code{syntax-rules} system is provided by the @code{(ice-9
629 syncase)} module. To make these facilities available in your code,
630 include the expression @code{(use-modules (ice-9 syncase))} or
631 @code{(use-syntax (ice-9 syncase))} (@pxref{Using Guile Modules})
632 before the first usage of @code{define-syntax} etc. If you are writing
633 a Scheme module, you can alternatively use one of the keywords
634 @code{#:use-module} and @code{#:use-syntax} in your @code{define-module}
635 declaration (@pxref{Creating Guile Modules}).
636
637 @menu
638 * Pattern Language:: The @code{syntax-rules} pattern language.
639 * Define-Syntax:: Top level syntax definitions.
640 * Let-Syntax:: Local syntax definitions.
641 @end menu
642
643
644 @node Pattern Language
645 @subsection The @code{syntax-rules} Pattern Language
646
647
648 @node Define-Syntax
649 @subsection Top Level Syntax Definitions
650
651 define-syntax: The gist is
652
653 (define-syntax <keyword> <transformer-spec>)
654
655 makes the <keyword> into a macro so that
656
657 (<keyword> ...)
658
659 expands at _compile_ or _read_ time (i.e. before any
660 evaluation begins) into some expression that is
661 given by the <transformer-spec>.
662
663
664 @node Let-Syntax
665 @subsection Local Syntax Definitions
666
667
668 @node Syntax Case
669 @section Support for the @code{syntax-case} System
670
671
672
673 @node Internal Macros
674 @section Internal Representation of Macros and Syntax
675
676 Internally, Guile uses three different flavors of macros. The three
677 flavors are called @dfn{acro} (or @dfn{syntax}), @dfn{macro} and
678 @dfn{mmacro}.
679
680 Given the expression
681
682 @lisp
683 (foo @dots{})
684 @end lisp
685
686 @noindent
687 with @code{foo} being some flavor of macro, one of the following things
688 will happen when the expression is evaluated.
689
690 @itemize @bullet
691 @item
692 When @code{foo} has been defined to be an @dfn{acro}, the procedure used
693 in the acro definition of @code{foo} is passed the whole expression and
694 the current lexical environment, and whatever that procedure returns is
695 the value of evaluating the expression. You can think of this a
696 procedure that receives its argument as an unevaluated expression.
697
698 @item
699 When @code{foo} has been defined to be a @dfn{macro}, the procedure used
700 in the macro definition of @code{foo} is passed the whole expression and
701 the current lexical environment, and whatever that procedure returns is
702 evaluated again. That is, the procedure should return a valid Scheme
703 expression.
704
705 @item
706 When @code{foo} has been defined to be a @dfn{mmacro}, the procedure
707 used in the mmacro definition of `foo' is passed the whole expression
708 and the current lexical environment, and whatever that procedure returns
709 replaces the original expression. Evaluation then starts over from the
710 new expression that has just been returned.
711 @end itemize
712
713 The key difference between a @dfn{macro} and a @dfn{mmacro} is that the
714 expression returned by a @dfn{mmacro} procedure is remembered (or
715 @dfn{memoized}) so that the expansion does not need to be done again
716 next time the containing code is evaluated.
717
718 The primitives @code{procedure->syntax}, @code{procedure->macro} and
719 @code{procedure->memoizing-macro} are used to construct acros, macros
720 and mmacros respectively. However, if you do not have a very special
721 reason to use one of these primitives, you should avoid them: they are
722 very specific to Guile's current implementation and therefore likely to
723 change. Use @code{defmacro}, @code{define-macro} (@pxref{Macros}) or
724 @code{define-syntax} (@pxref{Syntax Rules}) instead. (In low level
725 terms, @code{defmacro}, @code{define-macro} and @code{define-syntax} are
726 all implemented as mmacros.)
727
728 @deffn {Scheme Procedure} procedure->syntax code
729 @deffnx {C Function} scm_makacro (code)
730 Return a @dfn{macro} which, when a symbol defined to this value
731 appears as the first symbol in an expression, returns the
732 result of applying @var{code} to the expression and the
733 environment.
734 @end deffn
735
736 @deffn {Scheme Procedure} procedure->macro code
737 @deffnx {C Function} scm_makmacro (code)
738 Return a @dfn{macro} which, when a symbol defined to this value
739 appears as the first symbol in an expression, evaluates the
740 result of applying @var{code} to the expression and the
741 environment. The value returned from @var{code} which has been
742 passed to @code{procedure->memoizing-macro} replaces the form
743 passed to @var{code}. For example:
744
745 @lisp
746 (define trace
747 (procedure->macro
748 (lambda (x env) `(set! ,(cadr x) (tracef ,(cadr x) ',(cadr x))))))
749
750 (trace @i{foo}) @equiv{} (set! @i{foo} (tracef @i{foo} '@i{foo})).
751 @end lisp
752 @end deffn
753
754 @deffn {Scheme Procedure} procedure->memoizing-macro code
755 @deffnx {C Function} scm_makmmacro (code)
756 Return a macro which, when a symbol defined to this value appears as the
757 first symbol in an expression, evaluates the result of applying
758 @var{code} to the expression and the environment.
759 @code{procedure->memoizing-macro} is the same as
760 @code{procedure->macro}, except that the expression returned by
761 @var{code} replaces the original macro expression in the memoized form
762 of the containing code.
763 @end deffn
764
765 In the following primitives, @dfn{acro} flavor macros are referred to
766 as @dfn{syntax transformers}.
767
768 @deffn {Scheme Procedure} macro? obj
769 @deffnx {C Function} scm_macro_p (obj)
770 Return @code{#t} if @var{obj} is a regular macro, a memoizing macro or a
771 syntax transformer.
772 @end deffn
773
774 @deffn {Scheme Procedure} macro-type m
775 @deffnx {C Function} scm_macro_type (m)
776 Return one of the symbols @code{syntax}, @code{macro} or
777 @code{macro!}, depending on whether @var{m} is a syntax
778 transformer, a regular macro, or a memoizing macro,
779 respectively. If @var{m} is not a macro, @code{#f} is
780 returned.
781 @end deffn
782
783 @deffn {Scheme Procedure} macro-name m
784 @deffnx {C Function} scm_macro_name (m)
785 Return the name of the macro @var{m}.
786 @end deffn
787
788 @deffn {Scheme Procedure} macro-transformer m
789 @deffnx {C Function} scm_macro_transformer (m)
790 Return the transformer of the macro @var{m}.
791 @end deffn
792
793 @deffn {Scheme Procedure} cons-source xorig x y
794 @deffnx {C Function} scm_cons_source (xorig, x, y)
795 Create and return a new pair whose car and cdr are @var{x} and @var{y}.
796 Any source properties associated with @var{xorig} are also associated
797 with the new pair.
798 @end deffn
799
800
801 @c Local Variables:
802 @c TeX-master: "guile.texi"
803 @c End: