Hygienic macro
In computer science, hygienic macros are macros whose expansion is guaranteed not to cause the accidental capture of identifiers.
≈ 7 min read
Names in other languages
No names in other languages are held for this entry yet.
Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.
In computer science, hygienic macros are macros whose expansion is guaranteed not to cause the accidental capture of identifiers. They are a feature of programming languages such as Scheme, Dylan, Rust, Nim, and Julia. The general problem of accidental capture was well known in the Lisp community before the introduction of hygienic macros.
Macro writers would use language features that would generate unique identifiers (e.g., gensym) or use obfuscated identifiers to avoid the problem. Hygienic macros are a programmatic solution to the capture problem that is integrated into the macro expander.
The term "hygiene" was coined in Kohlbecker et al.'s 1986 paper that introduced hygienic macro expansion, inspired by terminology used in mathematics.
The hygiene problem
define INCI(i) { int a=0; ++i; } int main(void) { int a = 4, b = 8; INCI(a); INCI(b); printf("a is now %d, b is now %d\n", a, b); return 0; } Running the above through the C preprocessor produces: int main(void) { int a = 4, b = 8; { int a = 0; ++a; }; { int a = 0; ++b; }; printf("a is now %d, b is now %d\n", a, b); return 0; } The variable a declared in the top scope is shadowed by the a variable in the macro, which introduces a new scope. As a result, a is never altered by the execution of the program, as the output of the compiled program shows: a is now 4, b is now 9 (defmacro my-unless (condition &body body) `(if (not ,condition) (progn ,@body))) While there are no references to variables in this macro, it assumes the symbols "if", "not", and "progn" are all bound to their usual definitions in the standard library.
If, however the above macro is used in the following code: (flet ((not (x) x)) (my-unless t (format t "This should not be printed!"))) The definition of "not" has been locally altered and so the expansion of my-unless changes. Note however that for Common Lisp this behavior is forbidden, as per 11.1.2.1.2 Constraints on the COMMON-LISP Package for Conforming Programs.
It is also possible to completely redefine functions anyway. Some implementations of Common Lisp provide Package Locks to prevent the user to change definitions in packages by mistake. (defun user-defined-operator (cond) (not cond)) (defmacro my-unless (condition &body body) `(if (user-defined-operator ,condition) (progn ,@body))) ... later ...
(flet ((user-defined-operator (x) x)) (my-unless t (format t "This should not be printed!"))) The use site redefines user-defined-operator and hence changes the behavior of the macro.
Strategies used in languages that lack hygienic macros
The hygiene problem can be resolved with conventional macros using several alternative solutions. define INCI(i) { int INCIa = 0; ++i; } int main(void) { int a = 4, b = 8; INCI(a); INCI(b); printf("a is now %d, b is now %d\n", a, b); return 0; } Until a variable named INCIa is created, this solution produces the correct output: a is now 5, b is now 9 The problem is solved for the current program, but this solution is not robust. The variables used inside the macro and those in the rest of the program have to be kept in sync by the programmer.
Specifically, using the macro INCI on a variable INCIa is going to fail in the same way that the original macro failed on a variable a .
Temporary symbol creation
In some programming languages, it is possible for a new variable name, or symbol, to be generated and bound to a temporary location. The language processing system ensures that this never clashes with another name or location in the execution environment. The responsibility for choosing to use this feature within the body of a macro definition is left to the programmer.
This method was used in MacLisp, where a function named gensym could be used to generate a new symbol name. Similar functions (usually named gensym as well) exist in many Lisp-like languages, including the widely implemented Common Lisp standard and Elisp. Although symbol creation solves the variable shadowing issue, it does not directly solve the issue of function redefinition.
However, gensym , macro facilities, and standard library functions are sufficient to embed hygienic macros in an unhygienic language. (define-syntax my-unless (syntax-rules () ((_ condition body ...) (if (not condition) (begin body ...)))))
(let ((not (lambda (x) x))) (my-unless #t (display "This should not be printed!") (newline))) The hygienic macro processor responsible for transforming the patterns of the input form into an output form detects symbol clashes and resolves them by temporarily changing the names of symbols.
The basic strategy is to identify bindings in the macro definition and replace those names with gensyms, and to identify free variables in the macro definition and make sure those names are looked up in the scope of the macro definition instead of the scope where the macro was used. (define-syntax swap!
(syntax-rules () ((_ a b) (let ((temp a)) (set! a b) (set! b temp))))) (define-syntax swap! (lambda (stx) (syntax-case stx () ((_ a b) (syntax (let ((temp a)) (set! a b) (set! b temp))))))) However, syntax-case is more powerful than syntax-rules. For example, syntax-case macros can specify side-conditions on its pattern matching rules via arbitrary Scheme functions.
Alternatively, a macro writer can choose not to use the pattern matching frontend and manipulate the syntax directly. Using the datum->syntax function, syntax-case macros can also intentionally capture identifiers, thus breaking hygiene. syntactic closures (define-syntax swap!
(sc-macro-transformer (lambda (form environment) (let ((a (close-syntax (cadr form) environment)) (b (close-syntax (caddr form) environment))) `(let ((temp ,a)) (set! ,a ,b) (set! ,b temp)))))) explicit renaming (define-syntax swap! (er-macro-transformer (lambda (form rename compare) (let ((a (cadr form)) (b (caddr form)) (temp (rename 'temp))) `(,(rename 'let) ((,temp ,a)) (,(rename 'set!) ,a ,b) (,(rename 'set!) ,b ,temp))))))
Languages with hygienic macro systems
Scheme – syntax-rules, syntax-case, syntactic closures, and others. Racket – a Scheme variant, its macro system was originally based on syntax-case, but now has more features. Nemerle Dylan Elixir Nim Rust Haxe Mary2 – scoped macro bodies in an ALGOL 68-derivative language circa 1978 Julia Raku – supports both hygienic and unhygienic macros Lean
Criticism
Hygienic macros offer safety and referential transparency at the expense of making intentional variable capture less straight-forward. Doug Hoyte, author of Let Over Lambda, writes: Many hygienic macro systems do offer escape hatches without compromising on the guarantees that hygiene provides; for instance, Racket allows you to define syntax parameters, which allow you to selectively introduce bound variables.
Gregg Hendershott gives an example at Fear of Macros of implementing an anaphoric if operator in this way.
See also
Anaphoric macro Partial evaluation Preprocessor Syntactic closure
References
On Lisp, Paul Graham syntax-rules on schemewiki syntax-case on schemewiki examples of syntax-case on schemewiki syntactic closures on schemewiki simpler-macros on schemewiki examples of simpler-macros on schemewiki Writing Hygienic Macros in Scheme with Syntax-Case
Text derived from https://en.wikipedia.org/w/index.php?oldid=1375209012 under CC BY-SA 4.0; modifications have been made.