1 Syntax and Bindings
Synopsis: Hygiene defines the principles by which scoping interacts with macro expansion.
“Hygiene” is the macro-specific term for proper lexical scoping. It can be summarized, at great loss of accuracy and precision, as the following rule:
The binding structure of a program is as it seems.
Here, “as it seems” stands for the sorts of conclusions about binding structure that one arrives at using the normal reasoning methods for lexical scope: To find a reference’s matching binding occurrence, search outward from the occurrence of the reference looking for the innermost binding of the reference’s name. A binding occurrence captures all of the references of the same name enclosed by the binding form – except those for which there is a closer binding of the same name.
Applied to macros, the hygiene principle says that the references within a macro’s template refer to the nearest enclosing binding, either within the macro template itself or, more commonly, in the context of the macro definition. The set of bindings visible at a macro definition site is called the macro’s lexical context. More generally, lexical context analysis provides a way of reasoning about the meanings of references within syntax templates, whether directly inside of macro definitions or not.
Hygiene also guarantees the freshness of binding occurrences whose names originate from the macro (as opposed to names coming from the macro’s arguments). Unlike the guarantee for references, the freshness of binding occurrences does not generalize beyond macros to arbitrary syntax templates.