Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Common Lisp: compilation vs evaluation

On Emacs + Slime with sbcl, once I define a function (or more) in a file I have two choices:

  • Evaluation: e.g. with C-M-x eval-defun
  • Compilation: e.g. with C-c M-k compile-file

The second one produces a .fasl file, too.

What are the differences between the two?

What's going on under the hood when I compile a definition / a file?

What are the Pros and Cons of each one?

like image 884
Haile Avatar asked Aug 02 '12 17:08

Haile


1 Answers

First of all, there's a function eval[1], that allows to evaluate (i.e. execute) arbitrary CL form in the language runtime. CL implementations may have 2 different modes of operation: compilation mode and interpretation mode. Compilation mode implies, that before evaluation the form is first compiled in-memory. Also in CL evaluation happens not on the file-level, but on the level of individual forms. So eval may both compile and interpret the form, depending on the mode of operation. (For example SBCL by default always compiles, unless you instruct it not to by setting sb-ext:*evaluator-mode* to :interpret, while CLISP always interprets).

Now, there's also a convenience function compile-file[2] that allows to compile all the forms in some file and save the results in another file. This doesn't trigger evaluation of these forms.

Also CL defines 3 distinct times of program lifecycle: compile-time, load-time and execution time. And there's a possibility to control what happens when with one of the most (if not the most) cryptic CL special operators eval-when[3].

To sum up, C-M-x eval-defun will call eval on the form under cursor. It will not necessary compile it, but that is possible, depending on implementation. C-c M-k compile-file will compile-file your buffer, but not evaluate it's contents.

like image 194
Vsevolod Dyomkin Avatar answered Sep 22 '22 04:09

Vsevolod Dyomkin