From: ·················@gmail.com
Subject: Compilation order when compiling and loading a file
Date: 
Message-ID: <8d0db54f-a172-4856-97c7-7e8bc8a78705@e25g2000prg.googlegroups.com>
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

From: Maciej Katafiasz
Subject: Re: Compilation order when compiling and loading a file
Date: 
Message-ID: <fip5ud$rov$2@news.net.uni-c.dk>
Den Fri, 30 Nov 2007 05:47:32 -0800 skrev ·················@gmail.com:

> 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)

Works perfectly fine for me (although it doesn't actually print anything 
if you don't execute (load "helper.lisp") at REPL, but with --eval).

Cheers,
Maciej
From: Zach Beane
Subject: Re: Compilation order when compiling and loading a file
Date: 
Message-ID: <m363zjhgk6.fsf@unnamed.xach.com>
Maciej Katafiasz <········@gmail.com> writes:

> Works perfectly fine for me (although it doesn't actually print anything 
> if you don't execute (load "helper.lisp") at REPL, but with --eval).

That's why the original poster discussed the problem of compilation,
not loading.

Zach
From: Edi Weitz
Subject: Re: Compilation order when compiling and loading a file
Date: 
Message-ID: <uk5nz7rhe.fsf@agharta.de>
On Fri, 30 Nov 2007 05:47:32 -0800 (PST), ··················@gmail.com" <·················@gmail.com> wrote:

> (in-package :common-lisp-user)

Don't do that.  Work in your own package.

> Is there a way to make sure helper is defined when the expansion of
> (my-macro ...) is compiled?

  http://www.lispworks.com/documentation/HyperSpec/Body/s_eval_w.htm

Edi.

-- 

Lisp is not dead, it just smells funny.

Real email: (replace (subseq ·········@agharta.de" 5) "edi")
From: ·················@gmail.com
Subject: Re: Compilation order when compiling and loading a file
Date: 
Message-ID: <f0280dda-6497-46a8-a418-58e0f8522409@b40g2000prf.googlegroups.com>
On Nov 30, 3:11 pm, Edi Weitz <········@agharta.de> wrote:
> On Fri, 30 Nov 2007 05:47:32 -0800 (PST), ··················@gmail.com" <·················@gmail.com> wrote:
>
> > (in-package :common-lisp-user)
>
> Don't do that.  Work in your own package.

Thanks for the hint; I usually do.  In fact, I only switched to the
(clean) cl-user package while trying to isolate the mistake.

> > Is there a way to make sure helper is defined when the expansion of
> > (my-macro ...) is compiled?
>
>  http://www.lispworks.com/documentation/HyperSpec/Body/s_eval_w.htm

Thanks for the pointer.  This was actually very helpful as I had no
idea what I was looking for.  I had seen eval-when's in other people's
code (and wondered what it was good for), but failed to make the
connection.

Peter.

> Edi.
>
> --
>
> Lisp is not dead, it just smells funny.
>
> Real email: (replace (subseq ·········@agharta.de" 5) "edi")
From: Rainer Joswig
Subject: Re: Compilation order when compiling and loading a file
Date: 
Message-ID: <joswig-0AFDDE.15495030112007@news-europe.giganews.com>
In article 
<····································@e25g2000prg.googlegroups.com>,
 ··················@gmail.com" <·················@gmail.com> 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/
From: ·················@gmail.com
Subject: Re: Compilation order when compiling and loading a file
Date: 
Message-ID: <670bd3bf-b501-4e2f-8cd1-5fec57e968a3@r60g2000hsc.googlegroups.com>
On Nov 30, 3:49 pm, Rainer Joswig <······@lisp.de> wrote:
> 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.

Rainer, thank you for the clarification.  Now it is crystal clear (now
that I come to think of it, it is strange I did not stumble across
this earlier)

Peter.

> 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/