Real Macros

Macros with the same power as LispMacros, but in any HomoiconicLanguage, not just dialects of Lisp.

This definition seems to exclude the HygienicMacros of SchemeLanguage, and is intended to require features such as variable capture from the scope in which the macro is evaluated. Correct?

Actually, syntax-rules can be used to define macros which capture variables, as well as perform arbitrary basic sexp operations (car, cdr, cons, quote) on their arguments, therefore they have the same power as defmacro. Of course, most implementations handle them extremely slowly, and some even do not optimize macro "tail calls". See http://repository.readscheme.org/ftp/papers/sw2002/kiselyov.pdf for non-hygienic macros. -- anon

Is there a way with syntax-rules to make a macro like (defcounter foo) define the functions foo-up and foo-down? This is trivial with defmacro. I am not aware of any way to do it with syntax-rules.

Yes, there is. See the discussion of "with-pi" in DefineSyntax.


The with-pi example does not show how to do what I am talking about. Let me be more explicit.

I want to be able to type:

 (defcounter foo)
And have it expand to:
 (begin
  (define foo 0)
  (define (foo-up)   (set! foo (+ foo 1)))
  (define (foo-down) (set! foo (- foo 1))))
How can this be done with syntax-rules?

-- Eleven days later, and no response. Is this not possible with syntax-rules?

You want define-syntax. Here's defstruct in scheme, which defines its methods on the fly: http://www-2.cs.cmu.edu/~dst/Lisp/defstruct.scheme

This appears to be a CommonLisp/defmacro like system, where the full power of SchemeLanguage can be used in creating the expansion. It is well established that this sort of thing can be done with a defmacro like system. The question was about the hygienic syntax-rules system. Can this sort of thing be done with syntax-rules?

I'm thinking no. It can't.

That's why PLT Scheme uses syntax-case, with which defmacro can be written: http://people.csail.mit.edu/gregs/ll1-discuss-archive-html/msg01105.html


A parable, if I may...

A while ago, I was writing a scoreboard system for a game; the platform was a text-only MU*. It came with an embedded scripting language that was a little like a twisted mix of Lisp and Tcl, which could have been good if someone had actually designed. Regardless, I wanted to be able to have this program read scores from a persistent object, write/update new scores, and be able to add new users to the listing.

The read and write code was pretty straightforward. However, there was no real equivalent of a function one could pass the store object to ("stupid language" would be an understatement). To add a new user record I needed a piece of code that would, in addition to creating the new score slot and initializing it, write new pieces of code that could access the appropriate scores for the new player, hard-wired to the particular slot.

At this moment, I understood macros.

-- Simon Heath

"An embedded scripting language"... MushCode??


CategoryLanguageFeature


EditText of this page (last edited January 23, 2007) or FindPage with title or text search