Re: Symbol-macros?
On 10 Mar 2010, at 19:25, David McClain wrote:
>
> Hi List,
>
> Sometimes it would be really nice to be able to import symbols from other packages in the style of Dylan, where local renaming can occur. Lisp seems to have made the decision to bind values and functions to the name instead of indirecting through a reference cell. That is probably good for runtime speed, although I'm not convinced it would matter if the compiler simply hooked to the ref cell directly.
>
> I tried using DEFINE-SYMBOL-MACRO, but find that it only has effect for symbols in non-function position. And this is true across the several Lisps that I tried. After scouring the Hyperspec, I can't find any place that dictates this behavior, though I'm sure there must be.
>
> As a simple example, suppose I wanted to alias "+" with "add".
>
> (define-symbol-macro add '+)
>
> But this refuses to translate the "add" in (add 1 2 3). However, it willingly does so with (list add).
>
> Can anyone tell me why symbol-macros are subverted for function position symbols?
Consider:
(defun f (x) (+ x x))
(define-symbol-macro g f)
(defmacro g (x) 'screw-you)
What should be the result of (g 5) under such definitions? Why?
If you want to 'import' function bindings by way of your own macro definitions, you could do the following:
(defun f (x) (+ x x))
(define-symbol-macro g (function f))
(defmacro g (x) `(f ,x))
....and turn this into a macro that defines the macros and symbol-macros for you. Effectively, you would get Lisp-1 semantics for such imported functions, but maybe that's good enough in practice.
You could also decide to shadow 'cl:function, then it would be possible to import the function binding in such a way that also (function g) yields (cl:function g) - however, that is much more complex (I have done this for an experimental extension of Common Lisp). One reason is that you have to keep track of everything you imported, and make this available in the macroexpansion environment. The other reason is that you still didn't modify the #' abbreviation, which still turns into cl:function, so you would also have to hack the read table. I think for practical purposes, this is not worth the effort. YMMV.
Best,
Pascal
--
Pascal Costanza, mailto:pc@p-cos.net, http://p-cos.net
Vrije Universiteit Brussel
Software Languages Lab
Pleinlaan 2, B-1050 Brussel, Belgium