Next: , Previous: Syntax Case, Up: Procedures and Macros


23.8 Internal Representation of Macros and Syntax

Internally, Guile uses three different flavors of macros. The three flavors are called acro (or syntax), macro and mmacro.

Given the expression

     (foo ...)

with foo being some flavor of macro, one of the following things will happen when the expression is evaluated.

The key difference between a macro and a mmacro is that the expression returned by a mmacro procedure is remembered (or memoized) so that the expansion does not need to be done again next time the containing code is evaluated.

The primitives procedure->syntax, procedure->macro and procedure->memoizing-macro are used to construct acros, macros and mmacros respectively. However, if you do not have a very special reason to use one of these primitives, you should avoid them: they are very specific to Guile's current implementation and therefore likely to change. Use defmacro, define-macro (see Macros) or define-syntax (see Syntax Rules) instead. (In low level terms, defmacro, define-macro and define-syntax are all implemented as mmacros.)

— Scheme Procedure: procedure->syntax code
— C Function: scm_makacro (code)

Return a macro which, when a symbol defined to this value appears as the first symbol in an expression, returns the result of applying code to the expression and the environment.

— Scheme Procedure: procedure->macro code
— C Function: scm_makmacro (code)

Return a macro which, when a symbol defined to this value appears as the first symbol in an expression, evaluates the result of applying code to the expression and the environment. The value returned from code which has been passed to procedure->macro replaces the form passed to code. For example:

          (define trace
            (procedure->macro
             (lambda (x env)
               `(set! ,(cadr x)
                      (tracef ,(cadr x)
                              ',(cadr x))))))
          
          (trace foo) == (set! foo (tracef foo 'foo))
— Scheme Procedure: procedure->memoizing-macro code
— C Function: scm_makmmacro (code)

Return a macro which, when a symbol defined to this value appears as the first symbol in an expression, evaluates the result of applying proc to the expression and the environment. The value returned from proc which has been passed to procedure->memoizing-macro replaces the form passed to proc. For example:

          (define trace
            (procedure->macro
             (lambda (x env)
               `(set! ,(cadr x)
                      (tracef ,(cadr x)
                              ',(cadr x))))))
          
          (trace foo) == (set! foo (tracef foo 'foo))

In the following primitives, acro flavor macros are referred to as syntax transformers.

— Scheme Procedure: macro? obj
— C Function: scm_macro_p (obj)

Return #t if obj is a regular macro, a memoizing macro or a syntax transformer.

— Scheme Procedure: macro-type obj
— C Function: scm_macro_type (obj)

Return one of the symbols syntax, macro or macro!, depending on whether obj is a syntax tranformer, a regular macro, or a memoizing macro, respectively. If obj is not one of these, return #f.

— Scheme Procedure: macro-name m
— C Function: scm_macro_name (m)

Return the name of the macro m.

— Scheme Procedure: macro-transformer m
— C Function: scm_macro_transformer (m)

Return the transformer of the macro m.

— Scheme Procedure: cons-source xorig x y
— C Function: scm_cons_source (xorig, x, y)

Return a new pair whose car and cdr are x and y. Any source properties associated with xorig are also associated with the new pair.