I don’t understand how Lisp can be compiled and dynamic. For a language to be able to manipulate and modify and generate code, isn’t it a requirement to be interpreted? Is it possible for a language to be completely compiled and still be dynamic? Or am I missing something? What is Lisp doing that allows it to be both compiled and dynamic?
Share
Lisp is a wide family of language and implementations.
Dynamic in the context of Lisp means that the code has a certain flexibility at runtime. It can be changed or replaced for example. This is not the same as dynamically typed.
Compilation in Lisp
Often Lisp implementations have a compiler available at runtime. When this compiler is incremental, it does not need whole programs, but can compile single Lisp forms. Then we say that the compiler supports incremental compilation.
Note that most Lisp compilers are not Just In Time compilers. You as a programmer can invoke the compiler, for example in Common Lisp with the functions
COMPILEandCOMPILE-FILE. Then Lisp code gets compiled.Additionally most Lisp systems with both a compiler and an interpreter allow the execution of interpreted and compiled code to be freely mixed.
In Common Lisp the compiler can also be instructed how dynamic the compiled code should be. A more advanced Lisp compiler like the compiler of SBCL (or many others) can then generate different code.
Example
Above function
foocalls the functionbar.If we have a global function
barand redefine it, then we expect in Lisp usually that the new functionbarwill be called byfoo. We don’t have to recompilefoo.Let’s look at GNU CLISP. It compiles to byte code for a virtual machine. It’s not native machine code, but for our purpose here it is easier to read.
Runtime lookup
So you see that the call to
BARdoes a runtime lookup. It looks at the symbolBARand then calls the symbol’s function. Thus the symbol table serves as a registry for global functions.This runtime lookup in combination with an incremental compiler – available at runtime – allows us to generate Lisp code, compile it, load it into the current Lisp system and have it modify the Lisp program piece by piece.
This is done by using an indirection. At runtime the Lisp system looks up the current function named
bar. But note, this has nothing to do with compilation or interpretation. If your compiler compilesfooand the generated code uses this mechanism, then it is dynamic. So you would have the lookup overhead both in the interpreted and the compiled code.Since the 70s the Lisp community put a lot of effort into making the semantics of compiler and interpreter as similar as possible.
A language like Common Lisp also allows the compiler to make the compiled code less dynamic. For example by not looking up functions at run time for certain parts of the code.