How does defmacro expansion in Scheme and Common Lisp work?

Whatever is on your mind, whether Lisp related or not.

How does defmacro expansion in Scheme and Common Lisp work?

Postby Ali Clark » Sat Jan 10, 2009 4:08 pm

Hi, I've been trying to use a "defmacro" form in some Scheme code lately, and it appears that Scheme defmacro doesn't have access to run-time bindings (at least not in MzScheme or Gambit), so to get my macros to work in Gambit, I need to have a repl open and insert the run-time code before the defmacro code.

On the other hand, as far as I can gather, CL has no problem with using run-time bindings at macro-time to perform expansions.

So I'm a bit confused... Am I doing it wrong by trying to use run-time bindings in Scheme? Or maybe because CL uses defun to define functions that makes it possible to use them in macro definitions? If Scheme and CL do treat defmacro usage differently, could anyone tell me why? Is there a way I can use defmacro in Scheme as I can for CL? Finally, how do CL macros access these run time definitions without accidentally calling run-time code at compile-time?

Hope someone can help clear this up, I tried searching but it seems like one of those things which are hard to find the right search query for.

Thanks for any help and sorry for bombarding all these questions!
Ali Clark
 
Posts: 15
Joined: Mon Aug 25, 2008 10:23 am

Re: How does defmacro expansion in Scheme and Common Lisp work?

Postby Ramarren » Sun Jan 11, 2009 4:36 am

First, Scheme, as a language, doesn't have defmacro. I have never really looked into Scheme, but this is usually brought up as one of more important differences between Scheme and CL, that Scheme has a system of pattern substitution hygienic macros, which are more complex, but avoid variable capture, which is more important in Lisp-1. From what I read most Scheme implementations do include an unhygienic defmacro equivalent, but it might not be a good idea to use it everywhere in Scheme...

Anyway, CL macros do not have access to run time bindings, unless by access you mean something different than I, since macros are expanded during macroexpansion time, which is part of compile time, which is before run time, and having access to runtime bindings would require time travel.

What CL does allow, and Scheme probably has some equivalent, is mixing compile time, load time and run time with eval-when. Although usually if one wants macros to use functions, the functions will be defined in another file, which will be compiled and loaded before the file containing the macros.

I am not sure if I understood your questions, but I hope this helps.
Ramarren
 
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland

Re: How does defmacro expansion in Scheme and Common Lisp work?

Postby Ali Clark » Sun Jan 11, 2009 12:44 pm

Thanks for your reply, I think that clears it up well for me.
I've tried to use the Schemey hygienic macros, but found it pretty hard to write powerful macros using them.
Besides which, I think I might have found a way to write hygienic lisp style macros in Scheme - so long as the macros used to define this system have access to a hash table to store variable-name => gensym relations.

Thanks again for your explanation!
Ali Clark
 
Posts: 15
Joined: Mon Aug 25, 2008 10:23 am


Return to The Lounge

Who is online

Users browsing this forum: No registered users and 1 guest

cron