Next: , Previous: Fly Evaluation, Up: Read/Load/Eval/Compile


6.17.5 Compiling Scheme Code

The eval procedure directly interprets the S-expression representation of Scheme. An alternate strategy for evaluation is to determine ahead of time what computations will be necessary to evaluate the expression, and then use that recipe to produce the desired results. This is known as compilation.

While it is possible to compile simple Scheme expressions such as (+ 2 2) or even "Hello world!", compilation is most interesting in the context of procedures. Compiling a lambda expression produces a compiled procedure, which is just like a normal procedure except typically much faster, because it can bypass the generic interpreter.

Functions from system modules in a Guile installation are normally compiled already, so they load and run quickly.

Note that well-written Scheme programs will not typically call the procedures in this section, for the same reason that it is often bad taste to use eval. The normal interface to the compiler is the command-line file compiler, which can be invoked from the shell as guile-tools compile foo.scm.

(Why are calls to eval and compile usually in bad taste? Because they are limited, in that they can only really make sense for top-level expressions. Also, most needs for “compile-time” computation are fulfilled by macros and closures. Of course one good counterexample is the REPL itself, or any code that reads expressions from a port.)

For more information on the compiler itself, see Compiling to the Virtual Machine. For information on the virtual machine, see A Virtual Machine for Guile.

The command-line interface to Guile's compiler is the guile-tools compile command:

— Command: guile-tools compile [option...] file...

Compile file, a source file, and store bytecode in the compilation cache or in the file specified by the -o option. The following options are available:

-L dir
--load-path=dir
Add dir to the front of the module load path.
-o ofile
--output=ofile
Write output bytecode to ofile. By convention, bytecode file names end in .go.
-W warning
--warn=warning
Emit warnings of type warning; use --warn=help for a list of available warnings and their description. Currently recognized warnings include unused-variable, unused-toplevel, unbound-variable, arity-mismatch, and format.
-f lang
--from=lang
Use lang as the source language of file. If this option is omitted, scheme is assumed.
-t lang
--to=lang
Use lang as the target language of file. If this option is omitted, objcode is assumed.

The compiler can also be invoked directly by Scheme code using the procedures below:

— Scheme Procedure: compile exp [env=#f] [from=(current-language)] [to=value] [opts=()]

Compile the expression exp in the environment env. If exp is a procedure, the result will be a compiled procedure; otherwise compile is mostly equivalent to eval.

For a discussion of languages and compiler options, See Compiling to the Virtual Machine.

— Scheme Procedure: compile-file file [to=objcode] [opts='()]

Compile the file named file.

Output will be written to a file in the current directory whose name is computed as (compiled-file-name file).

— Scheme Procedure: compiled-file-name file

Compute an appropriate name for a compiled version of a Scheme file named file.

Usually, the result will be the original file name with the .scm suffix replaced with .go, but the exact behavior depends on the contents of the %load-extensions and %load-compiled-extensions lists.