[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

define-syntax



Ji-Yong D. Chung wrote:

> [...]
>     I was wondering of if you came across any system
> that attempts to rewrite expressions as the one above,
> using macros, to make it more efficient.
> [...]

Macros, a la Scheme, are a poor tool for expressing general optimizer
rules.  You need more control over rewriting than Scheme's mechanism
provides.  You could do it with a more general expander such as
McMicMac, but we haven't.

>     The macro will rewrite the lambda expression
> above (the inefficient one) as:
> [...]

Scheme compilers that care about efficiency will usually recognize
"inefficient" terms that may result from macro expansion as primitive
syntax and implement it more efficiently.  For instance, it is common
in Scheme systems to use the macro

  (let ([vs es] ...) bs ...) => ((lambda (vs ...) bs ...) es ...)

but LET can be implemented more directly as a stack push.  Rather than 
first expand the macro, then unexpand it, some Scheme systems will
instead recognize the pattern ((LAMBDA ...) ...) as a "primitive" and
implement it by stack pushes.  Then, it no longer matters how this
code came to be -- whether from LET, or from the user directly, or
as the consequence of the composition of other macros -- it will
always be treated suitably.

These questions are, in general, hard to answer well.  A compiler, and 
thus its optimizing sub-systems, is the result of trying to satisfy
many, potentially conflicting constraints.  Do you want to minimize
compile time?  Execution time?  Execution space?  Compile space?  Code 
size?  Power consumption?  Etc, etc.  An optimizer for power usage,
for instance, would not find Scheme's macros very useful at all.

Shriram