Re: What's up with Scheme macros?



[ comp.lang.lisp only; http://www.nhplace.com/kent/PFAQ/cross-posting.html ]

Ron Garret <rNOSPAMon@xxxxxxxxxxx> writes:

I don't understand. Why should:


(let ((x ...))
(defmacro ... () (foo x)))

be any different from:

(let ((x ...))
(defun f () (foo x)))

(defmacro ... () (f ...))

Because the binding has to happen at a known time so the init form
happens at a known time.

One could make a form that has a known time, but one would have to
define the semantics to accommodate ... it doesn't just happen for
free. And the reason they're not defined is mostly that no one was
happy with the various semantics that were discussed.

So, for example, the real issue [and the reason the operator is not
defined this way] is expressed by looking at concrete use cases. A
typical one might be:

When would

(let ((x (print ''a)))
(defmacro foo () x)
(foo))

print (QUOTE A)? At compile time? At load time? Both?

Moreover, why would the compiler know to expand (foo) at all since the
definition of FOO as a macro would not be installed until runtime, if
it were to follow what DEFUN does.

particularly in light of the fact that one can do e.g.:

(progn
'#1=#:f ; Or #:|| to really drive the point home
(let (...)
(defun #1# () (foo x)))
(defmacro ... () (#1# ...)))

I'm going to assume you wrote:

(progn
'#1=#:f ; Or #:|| to really drive the point home
(let ((x ...))
(defun #1# () (foo x)))
(defmacro g () (#1# ...)))

so I can refer to the parts more easily.

I personally would not rely on that macro to work so I don't
know if your claim is correct.

It will work in the same file, but if you ever later do any
macroexpansion of G which you don't then immediately compile but
instead externalize (e.g., by print) to a file for later compilation,
you'll find that when it reloads, the use of the gensym won't be
repatriated correctly. Not that that's your point. It's just a
lurking bug in your program that will survive a long time before being
noticed.

But as to the part about the binding of x, what you should know is
that it's permissible for calls to (g) to warn that f is undefined,
since it doesn't take on a value until runtime [when the defun is
executed], and the compiler doesn't notice that your #:F function (in
the cases where it works at all [1]) has a definition.

[1] = not to be confused with the cases where it doesn't work because
in fact it never gets a definition.

Lisp used to have COMPILER-LET, and no one liked it. But it was much
closer to the semantics you want. In mixed compiled and interpreted
code with special and lexical bindings all about, it created cases
where it was very hard to debug. So we removed it.
.



Relevant Pages

  • Re: Is DEFCONSTANT broken?
    ... (defconstant foo ...) ... If you compile a file containing defconstant which is used in that file, ... defconstant overcomes the externalizeability problem by externalizing the ...
    (comp.lang.lisp)
  • Re: Whats up with Scheme macros?
    ... which is NOT part of the spec for defmacro: ... might be effectively a bug in the spec since it really ... there is an issue because (foo) is in a context where the definition inline ... But the value of x is needed at compile time, ...
    (comp.lang.lisp)
  • Re: HashMap get/put
    ... Class A defines method foo() which is final. ... Compile both classes into files A.class and B.class. ... public void write ... explicitly tagged with the 'virtual' keyword, and the override uses the ...
    (comp.lang.java.programmer)
  • Re: OO in Forth
    ... And V-tables, late binding default. ... Indexed Dispatch Tables with the Neon Model. ... The identical source will compile and behave the same either way, linked-list look-up or dispatch table. ... Now if the definition of draw: is not yet defined that will lead to a compilation error. ...
    (comp.lang.forth)
  • Re: Is Double Dispatch really object-oriented?
    ... Compile error, in a language that supports MD properly => ... No dispatch ever fails ... Do you seriously claim that you can test Foo having just one B? ...
    (comp.object)

Loading