Re: Compilation order when compiling and loading a file



In article
<8d0db54f-a172-4856-97c7-7e8bc8a78705@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
"peter.hildebrandt@xxxxxxxxx" <peter.hildebrandt@xxxxxxxxx> wrote:

Hello *,

I'd like to do the following within one file

- defun a helper function
- defmacro a macro which uses the helper function
- use the macro

Just like this
-----------
(in-package :common-lisp-user)

(defun helper (p)
`(print ,p))

(defmacro my-macro (a)
(helper a))

(my-macro 'done)
------------

However, at the time my-macro is compiled, it complains about helper
being undefined. (In case it matters: This happens in SBCL 1.0.11
with today's slime cvs)

Is there a way to make sure helper is defined when the expansion of
(my-macro ...) is compiled? (other than splitting the source into
two files and using e.g. asdf:defsystem)

Any hints are highly appreciated.

Peter

Edi gave you the pointer to EVAL-WHEN.

Just to mention it, above is to expect, when you compile that
file. The Common Lisp compiler sees all the definitions
when compiling the file. It will note that there is a
function HELPER in the file, but will not store its code.
If it sees a macro, it will store the macro code during
the compilation, since the macro can be used later in
the file and then the compiler needs to run the macro.
After compilation the macro is gone. It is only remembered
during compilation of the file. If you want the compiler
to execute code when it compiles a file, you need
to wrap EVAL-WHEN with :COMPILE-TOPLEVEL around that code.
When you wrap it around the HELPER definition, the
compiler will run the DEFUN during compilation.

The mechanism is useful to understand. The interaction
of file compilation with the Lisp system is a bit
unique to Common Lisp. The effect is that the compiler
itself is a full Common Lisp environment which you
can use for computation during compile time. Two
different scenarios are typical:

* the file compiler runs inside the environment. You develop
everything from one Lisp system. The file compiler
is running in the same Lisp where you load the code.
You compile and load file after file into the Lisp system.

* the file compiler is running as batch or slave compiler.
Then you have the effect that for each compilation
session you have a fresh Lisp compiler and every time you
call the file compiler you need to make sure that the compiler
knows ALL the definitions it needs at compile time.

--
http://lispm.dyndns.org/
.



Relevant Pages

  • Re: Java connected Lisp
    ... versions of GNU Classpath and SBCL, ... Invent a nice and fast interface for calls from Lisp to Java and back. ... The trick could be to hack the compiler so that it recognizes accesses ... Rewrite the "compiler" ...
    (comp.lang.lisp)
  • Re: Reflections on a classic Lisp Paper
    ... its Lisp-1 semantics than defmacro, but would not serve CL as well. ... fexprs into those languages. ... this was the Lisp Machine's philosphy that led to offering ... do changes that the compiler cannot know, ...
    (comp.lang.lisp)
  • Re: Which programming language is better to start
    ... How does Lisp handle monomorphic types? ... If you don't specify a variable to have a type then the compiler ... ML does a lot of pattern match optimisations which, ... These operation would be done by representing the data as lists ...
    (comp.programming)
  • Places=lvalues and Pop11 (was months ago: Requesting advice how to clean up C code for vali
    ... there are equivalence classes of values modulo ... In fact the optimizing compiler could eliminate the ... character, or EBCDIC character, or IBM-PC character, or Latin-1 ... and I'm pleased you've essentially adopted the lisp ...
    (comp.lang.c)
  • Re: Yet another when to use macros question....
    ... the implementation language/target language is Lisp, ... Lisp compiler, or whether they are data structures ... Essence is in macro expanding time that has no ... Execution time cannot be eliminated, only thing you can do is ...
    (comp.lang.lisp)