Do you know of an ELI5 level definition somewhere of what it means for a macro system to be hygienic? I’m a non CS-educated programmer, and this is one of those things that’s always stuck in my “mentioned continuously, figure it out someday when you have spare time (ha)” file 😊
Conversation
I'm a cs educated programmer, and I couldn't give a proper definition. In this context, a macro is something which takes an input in the source code, which is then transformed into other source code. A kind of metaprogramming. But it might have a broader meaning than that.
1
Thanks! Yeah I get the colloquial definition of general macros, but it’s the distinctions between a hygienic vs non-hygienic macro system that I haven’t been able to chase down a layperson’s definition ☺️
1
Basically, hygienic macros are guaranteed not to generate code which conflicts with the code around the macro. Eg. a=5 in your code and the macro uses an internal variable a, with hygienic macros, the macro won't shadow the value of a you defined, while standard macros might
1
2
To be honest, I think this explanation—like almost all explanations about hygiene—puts the cart before the horse. As I’ve said repeatedly in the Rust issue thread, macro hygiene is about making macros respect the language’s lexical scoping, nothing more and nothing less.
1
4
13
Lexical scoping just means that the scope of variables should depend on those variables’ locations in the *source code*. For the same reasons we don’t want dynamic scope, where scope depends on how functions are called, we don’t want scope to depend on how macros expand.
1
1
10
That’s it, that’s the whole concept! I give several examples/illustrations of that concept throughout the Rust issue thread. All the other stuff about “automatic renaming of variables” or “rich identifiers” is just an implementation technique towards that end of respecting scope.
1
1
10
Thank you Alexis! I'll give that thread a close read, I didn't realize that it had fundamentals-level info :)
2
2
It's such a good thread, so many great thoughts there!
1
And here too, I might add! I'm learning so much haha
Not sure how related this is, but one thing I've enjoyed in the Lean 4 tooling is how see the shadowing going on in the tactic state. Tactics are hygienic, but you can see variables that can't currently be accessed marked with little crosses (ie. ‘tombstone variables’).
read image description
ALT



