[plt-scheme] Getting Serious with Learning Macros

From: Jens Axel Søgaard (jensaxel at soegaard.net)
Date: Wed Sep 16 13:45:34 EDT 2009

2009/9/16 Doug Williams <m.douglas.williams at gmail.com>:
> I'm finally getting serious about using complex macros for (properly)
> parsing and processing domain specific languages in PLT Scheme. To date, I
> have just been using syntax-rules to do the minimum parsing needed to break
> out the basic elements of a construct and generate code that passes them to
> the code that does the real work at run-time. This is relatively easy to
> write, but pretty much limits error messages to 'bad syntax', which isn't
> real useful. But, I haven't found any really good tutorial material to help
> me get started.
> So first, can anyone point me to some good material to get started with
> syntax-case, define-for-syntax, and so on for doing this right? I've gotten
> started by looking at things like package.ss (in the scheme collection) that
> have some complex macros.

If I had to pick three:

Syntactic abstraction: the syntax-case expander.
R. Kent Dybvig. In Andy Oram and Greg Wilson, Beautiful Code: Leading
Programmers Explain How They Think, 407--428, June 2007

Writing Hygenic Macros in Scheme with Syntax-Case - Dybvig

ICFP 2002 Flatt.
Composable and Compilable Macros: You Want it When?

An exaxmple/exercise I learned a lot from, seems simple,
but an understanding of modules and phases is necessary.

  (define-constant foo 42)
  (+ foo 1)

should expand to (+ 42 1).

The tricky part is to handle the following sitatutions with custom error


  (define-constant foo 42)
  (define-constant foo 43)
  (+ foo 1)
Error: The same constant is defined twice.

In module A: (define-constant foo 42)
In module B: (define-constant foo 43)
In module C importing both A and B: Error message: The constant foo is
defined twice.

(defiine-constat foo 42)
(set! foo 42)
Error: foo is a constant.

Jens Axel Søgaard

Posted on the users mailing list.