Explicit renaming

Explicit renaming (ER) macros provide an alternative macro transformer spec that allows a macro to execute the full Scheme language. Note that, unlike syntax-rules, er-macro-transformer is a low-level macro system that does not support hygiene unless identifiers are explicitly renamed using the rename function parameter.

The explicit renaming transformer has the following form:

(er-macro-transformer (lambda ({expression} rename compare) {body}) ) syntax

Expression is the code to be transformed by the macro.

rename is a procedure passed to the macro that takes a single symbol as its argument. The function will explicitly rename the symbol by returning another unique symbol.

compare is a function that takes two symbols and returns a boolean indicating whether the symbols refer to the same object.

Body is the Scheme code that will be executed when the macro is called, to transform the given expression.

For example, here is an explicit renaming macro (swap! x y) to swap the contents of two variables:

(define-syntax swap! (er-macro-transformer (lambda (form rename compare?) (let ( (x (cadr form)) (y (caddr form)) (%tmp (rename 'tmp)) (%let (rename 'let)) (%set! (rename 'set!)) ) `(,%let ((,%tmp ,x)) (,%set! ,x ,y) (,%set! ,y ,%tmp))))))

Husk's implementation of explicit renaming macros is based on the paper Hygienic Macros Through Explicit Renaming by William Clinger, which explains them in more detail.

husk-scheme online documentation rev 3.19.3 (2016.07.10)