194d2dd9980abfdcc568c159be97089afbd99cab
[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 metainformation.
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 variablesm 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 wither 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 {libary 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 occurences, one or more occurences, and one
200 or zero occurences.
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 paramater 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 agument @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 paramter 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 Metainformation
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 metainformation 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 primitive procedure? obj
351 Return @code{#t} if @var{obj} is a procedure.
352 @end deffn
353
354 @deffn primitive closure? obj
355 Return @code{#t} if @var{obj} is a closure.
356 @end deffn
357
358 @deffn primitive thunk? obj
359 Return @code{#t} if @var{obj} is a thunk.
360 @end deffn
361
362 @c FIXME::martin: Is that true?
363 @cindex procedure properties
364 Procedure properties are general properties to be attached to
365 procedures. These can be the name of a procedure or other relevant
366 information, such as debug hints.
367
368 @deffn primitive procedure-properties proc
369 Return @var{obj}'s property list.
370 @end deffn
371
372 @deffn primitive procedure-property p k
373 Return the property of @var{obj} with name @var{key}.
374 @end deffn
375
376 @deffn primitive set-procedure-properties! proc new_val
377 Set @var{obj}'s property list to @var{alist}.
378 @end deffn
379
380 @deffn primitive set-procedure-property! p k v
381 In @var{obj}'s property list, set the property named @var{key} to
382 @var{value}.
383 @end deffn
384
385 @cindex procedure documentation
386 Documentation for a procedure can be accessed with the procedure
387 @code{procedure-documentation}.
388
389 @deffn primitive procedure-documentation proc
390 Return the documentation string associated with @code{proc}. By
391 convention, if a procedure contains more than one expression and the
392 first expression is a string constant, that string is assumed to contain
393 documentation for that procedure.
394 @end deffn
395
396 @cindex source properties
397 @c FIXME::martin: Is the following true?
398 Source properties are properties which are related to the source code of
399 a procedure, such as the line and column numbers, the file name etc.
400
401 @deffn primitive set-source-properties! obj plist
402 Install the association list @var{plist} as the source property
403 list for @var{obj}.
404 @end deffn
405
406 @deffn primitive set-source-property! obj key datum
407 Set the source property of object @var{obj}, which is specified by
408 @var{key} to @var{datum}. Normally, the key will be a symbol.
409 @end deffn
410
411 @deffn primitive source-properties obj
412 Return the source property association list of @var{obj}.
413 @end deffn
414
415
416 @deffn primitive source-property obj key
417 Return the source property specified by @var{key} from
418 @var{obj}'s source property list.
419 @end deffn
420
421
422 @node Procedures with Setters
423 @section Procedures with Setters
424
425 @c FIXME::martin: Review me!
426
427 @c FIXME::martin: Document `operator struct'.
428
429 @cindex procedure with setter
430 @cindex setter
431 A @dfn{procedure with setter} is a special kind of procedure which
432 normally behaves like any accesor procedure, that is a procedure which
433 accesses a data structure. The difference is that this kind of
434 procedure has a so-called @dfn{setter} attached, which is a procedure
435 for storing something into a data structure.
436
437 Procedures with setters are treated specially when the procedure appears
438 in the special form @code{set!} (REFFIXME). How it works is best shown
439 by example.
440
441 Suppose we have a procedure called @code{foo-ref}, which accepts two
442 arguments, a value of type @code{foo} and an integer. The procedure
443 returns the value stored at the given index in the @code{foo} object.
444 Let @code{f} be a variable containing such a @code{foo} data
445 structure.@footnote{Working definitions would be:
446 @lisp
447 (define foo-ref vector-ref)
448 (define foo-set! vector-set!)
449 (define f (make-vector 2 #f))
450 @end lisp
451 }
452
453 @lisp
454 (foo-ref f 0) @result{} bar
455 (foo-ref f 1) @result{} braz
456 @end lisp
457
458 Also suppose that a corresponding setter procedure called
459 @code{foo-set!} does exist.
460
461 @lisp
462 (foo-set! f 0 'bla)
463 (foo-ref f 0) @result{} bla
464 @end lisp
465
466 Now we could create a new procedure called @code{foo}, which is a
467 procedure with setter, by calling @code{make-procedure-with-setter} with
468 the accessor and setter procedures @code{foo-ref} and @code{foo-set!}.
469 Let us call this new procedure @code{foo}.
470
471 @lisp
472 (define foo (make-procedure-with-setter foo-ref foo-set!))
473 @end lisp
474
475 @code{foo} can from now an be used to either read from the data
476 structure stored in @code{f}, or to write into the structure.
477
478 @lisp
479 (set! (foo f 0) 'dum)
480 (foo f 0) @result{} dum
481 @end lisp
482
483 @deffn primitive make-procedure-with-setter procedure setter
484 Create a new procedure which behaves like @var{procedure}, but
485 with the associated setter @var{setter}.
486 @end deffn
487
488 @deffn primitive procedure-with-setter? obj
489 Return @code{#t} if @var{obj} is a procedure with an
490 associated setter procedure.
491 @end deffn
492
493 @deffn primitive procedure proc
494 Return the procedure of @var{proc}, which must be either a
495 procedure with setter, or an operator struct.
496 @end deffn
497
498 @deffn primitive setter proc
499 Return the setter of @var{proc}, which must be either a procedure with
500 setter or an operator struct.
501 @end deffn
502
503
504 @node Macros
505 @section Lisp Style Macro Definitions
506
507 @cindex macros
508 @cindex transformation
509 Macros are objects which cause the expression that they appear in to be
510 transformed in some way @emph{before} being evaluated. In expressions
511 that are intended for macro transformation, the identifier that names
512 the relevant macro must appear as the first element, like this:
513
514 @lisp
515 (@var{macro-name} @var{macro-args} @dots{})
516 @end lisp
517
518 In Lisp-like languages, the traditional way to define macros is very
519 similar to procedure definitions. The key differences are that the
520 macro definition body should return a list that describes the
521 transformed expression, and that the definition is marked as a macro
522 definition (rather than a procedure definition) by the use of a
523 different definition keyword: in Lisp, @code{defmacro} rather than
524 @code{defun}, and in Scheme, @code{define-macro} rather than
525 @code{define}.
526
527 @fnindex defmacro
528 @fnindex define-macro
529 Guile supports this style of macro definition using both @code{defmacro}
530 and @code{define-macro}. The only difference between them is how the
531 macro name and arguments are grouped together in the definition:
532
533 @lisp
534 (defmacro @var{name} (@var{args} @dots{}) @var{body} @dots{})
535 @end lisp
536
537 @noindent
538 is the same as
539
540 @lisp
541 (define-macro (@var{name} @var{args} @dots{}) @var{body} @dots{})
542 @end lisp
543
544 @noindent
545 The difference is analogous to the corresponding difference between
546 Lisp's @code{defun} and Scheme's @code{define}.
547
548 @code{false-if-exception}, from the @file{boot-9.scm} file in the Guile
549 distribution, is a good example of macro definition using
550 @code{defmacro}:
551
552 @lisp
553 (defmacro false-if-exception (expr)
554 `(catch #t
555 (lambda () ,expr)
556 (lambda args #f)))
557 @end lisp
558
559 @noindent
560 The effect of this definition is that expressions beginning with the
561 identifier @code{false-if-exception} are automatically transformed into
562 a @code{catch} expression following the macro definition specification.
563 For example:
564
565 @lisp
566 (false-if-exception (open-input-file "may-not-exist"))
567 @equiv{}
568 (catch #t
569 (lambda () (open-input-file "may-not-exist"))
570 (lambda args #f))
571 @end lisp
572
573
574 @node Syntax Rules
575 @section The R5RS @code{syntax-rules} System
576 @cindex R5RS syntax-rules system
577
578 R5RS defines an alternative system for macro and syntax transformations
579 using the keywords @code{define-syntax}, @code{let-syntax},
580 @code{letrec-syntax} and @code{syntax-rules}.
581
582 The main difference between the R5RS system and the traditional macros
583 of the previous section is how the transformation is specified. In
584 R5RS, rather than permitting a macro definition to return an arbitrary
585 expression, the transformation is specified in a pattern language that
586
587 @itemize @bullet
588 @item
589 does not require complicated quoting and extraction of components of the
590 source expression using @code{caddr} etc.
591
592 @item
593 is designed such that the bindings associated with identifiers in the
594 transformed expression are well defined, and such that it is impossible
595 for the transformed expression to construct new identifiers.
596 @end itemize
597
598 @noindent
599 The last point is commonly referred to as being @dfn{hygienic}: the R5RS
600 @code{syntax-case} system provides @dfn{hygienic macros}.
601
602 For example, the R5RS pattern language for the @code{false-if-exception}
603 example of the previous section looks like this:
604
605 @lisp
606 (syntax-rules ()
607 ((_ expr)
608 (catch #t
609 (lambda () expr)
610 (lambda args #f))))
611 @end lisp
612
613 In Guile, the @code{syntax-rules} system is provided by the @code{(ice-9
614 syncase)} module. To make these facilities available in your code,
615 include the expression @code{(use-modules (ice-9 syncase))} or
616 @code{(use-syntax (ice-9 syncase))} (@pxref{Using Guile Modules})
617 before the first usage of @code{define-syntax} etc. If you are writing
618 a Scheme module, you can alternatively use one of the keywords
619 @code{#:use-module} and @code{#:use-syntax} in your @code{define-module}
620 declaration (@pxref{Creating Guile Modules}).
621
622 @menu
623 * Pattern Language:: The @code{syntax-rules} pattern language.
624 * Define-Syntax:: Top level syntax definitions.
625 * Let-Syntax:: Local syntax definitions.
626 @end menu
627
628
629 @node Pattern Language
630 @subsection The @code{syntax-rules} Pattern Language
631
632
633 @node Define-Syntax
634 @subsection Top Level Syntax Definitions
635
636 define-syntax: The gist is
637
638 (define-syntax <keyword> <transformer-spec>)
639
640 makes the <keyword> into a macro so that
641
642 (<keyword> ...)
643
644 expands at _compile_ or _read_ time (i.e. before any
645 evaluation begins) into some expression that is
646 given by the <transformer-spec>.
647
648
649 @node Let-Syntax
650 @subsection Local Syntax Definitions
651
652
653 @node Syntax Case
654 @section Support for the @code{syntax-case} System
655
656
657
658 @node Internal Macros
659 @section Internal Representation of Macros and Syntax
660
661 Internally, Guile uses three different flavours of macros. The three
662 flavours are called @dfn{acro} (or @dfn{syntax}), @dfn{macro} and
663 @dfn{mmacro}.
664
665 Given the expression
666
667 @lisp
668 (foo @dots{})
669 @end lisp
670
671 @noindent
672 with @code{foo} being some flavour of macro, one of the following things
673 will happen when the expression is evaluated.
674
675 @itemize @bullet
676 @item
677 When @code{foo} has been defined to be an @dfn{acro}, the procedure used
678 in the acro definition of @code{foo} is passed the whole expression and
679 the current lexical environment, and whatever that procedure returns is
680 the value of evaluating the expression. You can think of this a
681 procedure that receives its argument as an unevaluated expression.
682
683 @item
684 When @code{foo} has been defined to be a @dfn{macro}, the procedure used
685 in the macro definition of @code{foo} is passed the whole expression and
686 the current lexical environment, and whatever that procedure returns is
687 evaluated again. That is, the procedure should return a valid Scheme
688 expression.
689
690 @item
691 When @code{foo} has been defined to be a @dfn{mmacro}, the procedure
692 used in the mmacro definition of `foo' is passed the whole expression
693 and the current lexical environment, and whatever that procedure returns
694 replaces the original expression. Evaluation then starts over from the
695 new expression that has just been returned.
696 @end itemize
697
698 The key difference between a @dfn{macro} and a @dfn{mmacro} is that the
699 expression returned by a @dfn{mmacro} procedure is remembered (or
700 @dfn{memoized}) so that the expansion does not need to be done again
701 next time the containing code is evaluated.
702
703 The primitives @code{procedure->syntax}, @code{procedure->macro} and
704 @code{procedure->memoizing-macro} are used to construct acros, macros
705 and mmacros respectively. However, if you do not have a very special
706 reason to use one of these primitives, you should avoid them: they are
707 very specific to Guile's current implementation and therefore likely to
708 change. Use @code{defmacro}, @code{define-macro} (@pxref{Macros}) or
709 @code{define-syntax} (@pxref{Syntax Rules}) instead. (In low level
710 terms, @code{defmacro}, @code{define-macro} and @code{define-syntax} are
711 all implemented as mmacros.)
712
713 @deffn primitive procedure->syntax code
714 Return a macro which, when a symbol defined to this value appears as the
715 first symbol in an expression, returns the result of applying @var{code}
716 to the expression and the environment.
717 @end deffn
718
719 @deffn primitive procedure->macro code
720 Return a macro which, when a symbol defined to this value appears as the
721 first symbol in an expression, evaluates the result of applying
722 @var{code} to the expression and the environment. For example:
723
724 @lisp
725 (define trace
726 (procedure->macro
727 (lambda (x env)
728 `(set! ,(cadr x) (tracef ,(cadr x) ',(cadr x))))))
729
730 (trace @i{foo})
731 @equiv{}
732 (set! @i{foo} (tracef @i{foo} '@i{foo})).
733 @end lisp
734 @end deffn
735
736 @deffn primitive procedure->memoizing-macro code
737 Return a macro which, when a symbol defined to this value appears as the
738 first symbol in an expression, evaluates the result of applying
739 @var{code} to the expression and the environment.
740 @code{procedure->memoizing-macro} is the same as
741 @code{procedure->macro}, except that the expression returned by
742 @var{code} replaces the original macro expression in the memoized form
743 of the containing code.
744 @end deffn
745
746 In the following primitives, @dfn{acro} flavour macros are referred to
747 as @dfn{syntax transformers}.
748
749 @deffn primitive macro? obj
750 Return @code{#t} if @var{obj} is a regular macro, a memoizing macro or a
751 syntax transformer.
752 @end deffn
753
754 @deffn primitive macro-type m
755 Return one of the symbols @code{syntax}, @code{macro} or
756 @code{macro!}, depending on whether @var{m} is a syntax
757 transformer, a regular macro, or a memoizing macro,
758 respectively. If @var{m} is not a macro, @code{#f} is
759 returned.
760 @end deffn
761
762 @deffn primitive macro-name m
763 Return the name of the macro @var{m}.
764 @end deffn
765
766 @deffn primitive macro-transformer m
767 Return the transformer of the macro @var{m}.
768 @end deffn
769
770 @deffn primitive cons-source xorig x y
771 Create and return a new pair whose car and cdr are @var{x} and @var{y}.
772 Any source properties associated with @var{xorig} are also associated
773 with the new pair.
774 @end deffn
775
776
777 @c Local Variables:
778 @c TeX-master: "guile.texi"
779 @c End: