How is Lisp compiled ahead of time?

65 views
Skip to first unread message

CuppoJava

unread,
Oct 21, 2009, 11:58:35 AM10/21/09
to Clojure
Hi,
Clojure started my interest in programming languages, and I'm
wondering exactly how LISP-like languages get compiled ahead of time?
A link to a tutorial would be much appreciated.

The part that I'm having trouble understanding is the fact that
functions can be defined at runtime. How do you compile a function
that's not defined until after you run the program?

Thanks for your help
-Patrick

eyeris

unread,
Oct 21, 2009, 12:22:16 PM10/21/09
to Clojure
I don't know how detailed of an explanation you want. The most basic
explanation is that not everything is compiled to byte code in the
manner that a C compiler usually generates machine code. Instead the
runtime, which would interpret most lisps, is bundled with the code as
a library.

Jeff Sapp

unread,
Oct 21, 2009, 12:41:54 PM10/21/09
to clo...@googlegroups.com
I'm not aware of a good tutorial online, but Peter Norvig does
excellent treatment in his book "Paradigms of Artificial Intelligence
Programming". Chapter 23 is devoted to writing a simple compiler. The
source for his compiler is online at:

http://norvig.com/paip/compile1.lisp
http://norvig.com/paip/compile2.lisp
http://norvig.com/paip/compile3.lisp
http://norvig.com/paip/compopt.lisp

Norvig's book is also full of lots of other cool stuff too, it's worth
owning I'd say.

Christian Queinnec's "Lisp In Small Pieces" is also supposed to be
very good. So I hear anyway, I haven't managed to finish it yet...

Hope that helps.

~jeff

Laurent PETIT

unread,
Oct 21, 2009, 12:42:28 PM10/21/09
to clo...@googlegroups.com
Hello,

2009/10/21 CuppoJava <patrick...@hotmail.com>

AFAIC, functions cannot be defined at runtime.
I mean, code can be generated at compile time by macros.

With AOT compilation, everything possible has been compiled.
If you want to add new code, you need to 'eval it, so you need to go through the compiler again.
 

Konrad Hinsen

unread,
Oct 21, 2009, 1:52:28 PM10/21/09
to clo...@googlegroups.com
On 21 Oct 2009, at 17:58, CuppoJava wrote:

> The part that I'm having trouble understanding is the fact that
> functions can be defined at runtime. How do you compile a function
> that's not defined until after you run the program?

It is possible to define functions at runtime only by calling eval on
a form that evaluates to a function. These functions are obviously not
compiled "ahead of time" (a rather strange expression), they are
compiled when eval is called. This is why the Clojure compiler is part
of the run-time system.

Perhaps you were thinking of functions that return functions. These
functions are perfectly well defined statically, they just usually
capture variables from their surrounding functions, i.e. they are
closures. Closures are compiled into classes with fields that are
filled in when the closure object is created, so they pose no problem
for AOT compilation.

Konrad.

CuppoJava

unread,
Oct 21, 2009, 2:19:00 PM10/21/09
to Clojure
Of course! Thanks for that explanation Konrad Hinsen. I indeed was
thinking about functions that return functions, and was attempting to
reason about it with my C-compiler knowledge. It never occurred to me
that closures are well defined statically. That's all that I needed to
know.

Thanks everyone
-Patrick
Reply all
Reply to author
Forward
0 new messages