8 ms·
I know both Lisp and Scheme pretty well. Scheme's macros are hygienic, Lisp's macros however are much easier to handle. The hygiene problem can be solved by usi
by progman 10y ago
I know both Lisp and Scheme pretty well. Scheme's macros are hygienic, Lisp's macros however are much easier to handle. The hygiene problem can be solved by using (gensym) for local identifiers. Example:
http://stackoverflow.com/questions/267862/what-makes-lisp-macros-so-special http://stackoverflow.com/questions/267862/what-makes-lisp-ma...
- kazinator 10y agoTo completely solve the hygiene problem on an academic level, you have to use gensyms for all references generated by the macro, including all function symbols. Lisp macros don't bother doing this because the Lisp-2 separation provides adequate hygiene. Programs which choose to lexically shadow standard functions like cons or list in the function namespace are considered to be sticking a fork in the proverbial toaster, and so that is not handled. That would't work as well in Scheme because the mere presence of a variable called list in the user's code would interefere with a (list ...) function call generated by a macro. Also providing hygiene are packages. If macro-generated code references private symbols in the macro's own package, everything is cool. Packages take care of the problem for nonstandard functions. You just don't use another package's symbols for local functions and variables, and that's that.
- groovy2shoes 10y agoHow are Lisp's macros easier to handle than syntax-rules? On the one hand, you have a function that receives an s-expression, manually deconstructs and transforms the s-expression while (hopefully) carefully, manually aliasing any symbols to avoid inadvertent capture, and constructs a replacement s-expression. On the other hand, you have a pattern-matching facility that automatically takes care of deconstruction and hygiene for you. Yes, syntax-rules is strictly less powerful than defmacro, but in practice it covers the vast majority of use cases very well. I'd also like to mention that avoiding variable capture is only one part of macro hygiene. The other part is referential transparency in the face of shadowing. Common Lisp partly provides this because names in the standard package can't be redefined, but it's still wide open when it comes to other names. Consider this (admittedly contrived, for simplicity) example: (defun square (x) (* x x)) (defmacro hypotenuse (x y) `(sqrt (+ (square ,x) (square ,y)))) ... (let* ((square (make-square s)) (diagonal (hypotenuse s s))) ; ERROR! tried to apply ...) ; #'* to a square! In the expansion of `hypotenuse`, `square` refers to the local variable bound on the line above it, while the macro author clearly intended it to refer to the "global" variable up top. This is a situation that's fairly easy to run into, and horribly confusing when it occurs (especially if the user of the macro didn't write it). Hygiene ensures that the reference to `square` in `hypotenuse` will always refer to the binding of `square` that was in scope at the time the macro was defined, thus avoiding the problem. On its own, `gensym` is necessary but not sufficient for hygiene.
- junke 10y agoReally, an ERROR? Could you show the actual message that was printed? You can't, because you did not even bother to try. Here is an actual test: (defun square (x) (* x x)) (defmacro hypotenuse (x y) `(sqrt (+ (square ,x) (square ,y)))) ;; whatever (defun make-square (s) (list :square s)) (defun foo (s) (let* ((square (make-square s)) (diagonal (hypotenuse s s))) diagonal)) Now, let's compile (SBCL): ; caught STYLE-WARNING: ; The variable SQUARE is defined but never used. Not really what you expected, right? (foo 10) => 14.142136 It looks like it works. You are trying to apply Scheme's semantics to CL. You forgot to take into account CL namespaces, which consist of (1) function/classes/type/variable namespaces and (2) namespaces defined by packages (or custom mappings). > namespace n. 1. bindings whose denotations are restricted to a particular kind. ``The bindings of names to tags is the tag namespace.'' 2. any mapping whose domain is a set of names. ``A package defines a namespace.'' (http://clhs.lisp.se/Body/26_glo_n.htm http://clhs.lisp.se/Body/26_glo_n.htm) If you defined a local function named square, that could have been the beginning of a point. However, I don't buy the idea that inside a single package, someone is going to shadow a function that belongs to the same package, in a macro (if this happens, it is more likely to be done on purpose). As for shadowing function bindings from other packages, this is even more improbable. Please check your facts and avoid posting made-up error messages that can easily be mistaken for actual ones.
- groovy2shoes 10y agoMy example wasn't meant to be in Common Lisp, just a nebulous "Lisp" dialect. See my response to the sibling here: https://news.ycombinator.com/item?id=13223290 https://news.ycombinator.com/item?id=13223290 I'm not trying to be critical of CL. I thoroughly enjoy working in CL. I'm just trying to clear up some confusion about what it means for a macro to be hygienic, that's all.
- junke 10y ago> My example wasn't meant to be in Common Lisp, just a nebulous "Lisp" dialect. And how can I believe you? this is just too late. > I'm just trying to clear up some confusion [...] You were spreading confusion by using a dialect of Lisp known only by you, that looked exactly like CL, without giving proper warnings. > I'm not trying to be critical of CL. I thoroughly enjoy working in CL. Fine, but this would have been the same if you used a nebulous "Scheme" without saying it so.