0

As far as I know, the pattern&template-based Scheme macro system works by first pattern matching a macro invocation, obtaining a substitution in case of success, applying the resulted substitution to the corresponding template to build up a (maybe) partially-expanded expression, and then continuously expanding the resulted expression. If what I describe is true (please correct me otherwise), then it seems to me this building-up and expanding-again model is not efficient. Why does the expansion need to be done like this? Is it possible to finish the expansion by a run down the template once and for all?

2 Answers 2

1

Keep in mind that macros can be handled define time for procedures and compile time for a whole program.

Also, a macro expansion might turn into another (or similar) macro form that needs expanding. Eg. you can make a macro that ends up as a cond expression which of course is a macro for nested if expressions in most Schemes.

Have you seen Alexpander? It evaluates a program (in one expression) and returns an equal program without macros.

7
  • What is define time? Also, I am not sure I get the reasoning in your second paragraph. Could you describe the reason why in such a case the way I suggested (one run) could not handle?
    – day
    Jan 22, 2014 at 21:15
  • Well imagine (mymacro xxx yyy) is replaced with (cond ((eq? xxx 0) expr) ((eq? xxx 1) expr2) (else expr3)) it is not done processing since cond is a macro.. The end result might be (if (eq? xxx 0) (begin expr1) (if (eq? xxx 1) (begin expr2) (begin expr3)) but it also can be macro to the implementations underlying primitives. begin is in the result and it is a macro that needs expanding... etc.
    – Sylwester
    Jan 22, 2014 at 21:23
  • Define time==I'm thining evaluation of procedures. Usually done before procedure invocation and at compile time for compiled code.
    – Sylwester
    Jan 22, 2014 at 21:29
  • What I did not understand is that why I cannot immediately continuously expanding cond when I see it, rather than build up the expression and then expand it. This model of macro expansion is analog to the inefficient substitution-based model for the $\lambda$-calculus. There one wave of substitution is done, a new expression is built-up and reduction continues on this expression. But there a delayed-substitution can be achieved via an environment. So what I am asking is whether a similar trick can be done for macro expansion?
    – day
    Jan 22, 2014 at 21:35
  • What do you mean by building up the expression? Surely you need a whole resulting form before you check if the resulting form itself is a macro and if not if any of it's resulting parts is a macro? You need the whole to check if the pattern matches as well (it might not). You might even be able to expand these before when the syntax gets compiled (like mymacro gets a tran formation directly to if) but that will only work for the lexical variables. The pattern symbols are like variables that can be syntax as well.
    – Sylwester
    Jan 22, 2014 at 21:47
1

The semantics of the macro system are specified in the way you describe. However, implementations are free to implement that specification any way they want; in particular, they could "inline" macro expansions ahead of time to speed the process of macro expansion.

I'm not aware of any Scheme implementations that do what you describe, and I would guess it's because macro expansion is not usually a big bottleneck in compilation.

4
  • I have seen two implementations of syntax-case, one by R. Kent Dybvig (documented in two papers) and one by André van Tonder (see expander.scm) do what I described.
    – day
    Jan 22, 2014 at 21:11
  • @plmday I don't think StackOverflow is intended for rhetorical questions. If you already know the answer ("yes it can be improved"), then you should post the answer -- and mark your own answer as accepted. Jan 22, 2014 at 21:37
  • @GregHendershott: I realized I misunderstood John. By "what you describe" he was referring to my questioned way: a one run model. Whereas when I said "what I described", I was referring to my described way, that is, the build-up and then expand model. So, no, it is not a rhetorical question.
    – day
    Jan 22, 2014 at 21:43
  • It looks to me like you're confusing semantics with implementation. Let me know if that doesn't make sense to you. Jan 29, 2014 at 20:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.