Talk:Hygienic macro
From Wikipedia, the free encyclopedia
Macros transform arguments into call by reference? Weird. orthogonal 19:25, 20 Nov 2003 (UTC)
-- No, macros transform code before it hits the compiler or interpreter. There's no references at all, at macro expansion time, all you have available as arguments are tokens. -BrianCully 14:54, 2 September 2006 (UTC)
[edit] Examples
It's great that there's a C example here, but I've never heard anybody speak of hygienic macros in the context of C -- only in dialects of Lisp. Could we get (1) a similar example of the hygiene problem in Common Lisp, (2) the same example in Scheme (which claims to have hygienic macros), and (3) how a Common Lisper would resolve the problem? Thanks!
I have to admit, I don't follow any of these examples. It seems to me, in the lisp example, it's doing exactly what you would want and expect. Consider the following:
(defun foo (x) (- x 3)) (flet ((foo (x) x)) (foo 4)) => 4
So, wouldn't you expect that when you over-ride not to not be not at all, your unless breaks? The only reason this works with not is because its special, but you can still over-ride it with macrolet:
(macrolet ((not (x) x)) (unless t (format t "This should not be printed.~%")))
On top of that, trying to run the example code in OpenMCL produces:
;Compiler warnings : ; Attempt to bind compiler special name: NOT. Result undefined, in an anonymous lambda form.
-BrianCully 14:54, 2 September 2006 (UTC)
The CL examples are not valid Common Lisp code. The standard prohibits rebinding of symbols external to the COMMON-LISP package, see Section 11.1.2.1.2. Many compilers (like OpenMCL above, or SBCL) will signal an error.
82.75.4.249 14:36, 21 October 2007 (UTC) michaelw@foldr.org
[edit] Why the hygiene problem is a problem
Brian,
Yes, it is doing what you would expect, if you always knew when it was going to happen; consider if you were using a software package that included macros written by someone else; you wouldn't likely read the implementations of all of them (and whether you would or not, a user shouldn't have to). And the source might not be available for whatever reason.
Suppose one of these macros happened to use a symbol you were rebinding for another purpose in your code? You'd be quite surprised and disappointed after finally figuring out what was going wrong.
One of the reasons why hygiene matters a lot in LISP and its kin is the heavy usage macros get in that language, mostly because they are very sophisticated and offer many useful features beyond what you see in the C family macros.
Elugelab 13:01, 24 April 2007 (UTC)