Other general post, much more in-depth
- We can use homoiconicity for non-hygienic Lisp-like macros: functions that run at expansion / parse time, take in a list and return a list that will be parsed as code.
- Unfortunately that doesn't work for hygienic macro, because there is now a need to tell the parser about more than just raw symbols. Scope must be somehow attached to the symbols.
gensym
or some other restrictions can be used to solve some of these issues but are not very nice. discussion
- Scheme has a native hygienic macro system based on pattern-matching,
syntax-rules
. It's nice and all, but can't express non-hygienic macros and even for hygienic ones it's not always trivial to use because it's purely pattern matching instead of allowing arbitrary code to be run to generate the resulting macro - Typically speaking,
syntax-rules
is implemented on top of a lower level system short discussion on HN. There seem to be three of them in common use:syntax-case
, also hygienic (unclear to me if totally or not), complex to implement,syntax-rules
is easy to define on top of it (it's basically a subset). Officially endorsed by R6RS, which nobody cared about, and which was superseded by R7RS which removed it.- Used by Guile (page is for end users).
- A more detailed user guide
- Long paper about implementation.
- A portable implementation (on top of R5RS + other primitives)? which I haven't looked at.
- Syntactic closures, used in Chibi and MIT scheme
- schemewiki page on syntactic closures
- Used in MIT scheme. Here's the implementation of
syntax-rules
on top of it. - Chibi implements methods like those of MIT scheme (
er-macro-transformer
,sc-macro-transformer
,rsc-macro-transformer
) here. (simpler, older version). - Chibi implements
syntax-rules
here. - Chibi macro system doc
- Implicit/explicit renaming, used in R4RS and Chicken
- implicit: In R4RS, it's the suggested low-level implementation.
- explicit: Short paper by an R4RS author. Also formerly used in Chicken.
- It is a bit unclear which of these three are more powerful than others. It is apparently possible to implement
syntax-case
on top syntactic closures (+ some helpers?), as Chibi does, but it's a bit complicated and apparently needed to add some stuff to the interpreter. Git PR with discussion. Many systems which claim (and I believe them) to use syntactic closures end up with functions very much like those described in renaming systems. - There are also pure-syntax-rules implementations. This one takes in a scheme program with macros and returns one without macros, and implements syntax-rules only.