textual names into symbols: if you want two occurrences of the same name to be considered the same by the evaluator, you need to make sure the reader uses the same symbol to represent each name. Conversely, if you want two names to be considered distinct, even if they happen to have the same textual name, you need the reader to create different symbols to represent each name.
In Chapter 4 I discussed briefly how the Lisp reader translates names into symbols, but I glossed over most of the details—now it's time to take a closer look at what actually happens.
I'll start by describing the syntax of names understood by the reader and how that syntax relates to packages. For the moment you can think of a package as a table that maps strings to symbols. As you'll see in the next section, the actual mapping is slightly more flexible than a simple lookup table but not in ways that matter much to the reader. Each package also has a name, which can be used to find the package using the function FIND-PACKAGE
.
The two key functions that the reader uses to access the name-to-symbol mappings in a package are FIND-SYMBOL
and INTERN
. Both these functions take a string and, optionally, a package. If not supplied, the package argument defaults to the value of the global variable *PACKAGE*
, also called the
FIND-SYMBOL
looks in the package for a symbol with the given string for a name and returns it, or NIL
if no symbol is found. INTERN
also will return an existing symbol; otherwise it creates a new symbol with the string as its name and adds it to the package.
Most names you use are INTERN
. Thus, each time the reader reads the same name in the same package, it'll get the same symbol object. This is important because the evaluator uses the object identity of symbols to determine which function, variable, or other program element a given symbol refers to. Thus, the reason an expression such as (hello-world)
results in calling a particular hello-world
function is because the reader returns the same symbol when it reads the function call as it did when it read the DEFUN
form that defined the function.
A name containing either a single colon or a double colon is a package-qualified name. When the reader reads a package-qualified name, it splits the name on the colon(s) and uses the first part as the name of a package and the second part as the name of the symbol. The reader looks up the appropriate package and uses it to translate the symbol name to a symbol object.
A name containing only a single colon must refer to an
Two other bits of symbol syntax the reader understands are those for keyword symbols and uninterned symbols. Keyword symbols are written with names starting with a colon. Such symbols are interned in the package named KEYWORD
and automatically exported. Additionally, when the reader interns a symbol in the KEYWORD
, it also defines a constant variable with the symbol as both its name and value. This is why you can use keywords in argument lists without quoting them— when they appear in a value position, they evaluate to themselves. Thus:
(eql ':foo :foo) ==> T
The names of keyword symbols, like all symbols, are converted to all uppercase by the reader before they're interned. The name doesn't include the leading colon.
(symbol-name :foo) ==> 'FOO'
Uninterned symbols are written with a leading #:
. These names (minus the #:
) are converted to uppercase as normal and then translated into symbols, but the symbols aren't interned in any package; each time the reader reads a #:
name, it creates a new symbol. Thus:
(eql '#:foo '#:foo) ==> NIL
You'll rarely, if ever, write this syntax yourself, but will sometimes see it when you print an s-expression containing symbols returned by the function GENSYM
.
(gensym) ==> #:G3128
As I mentioned previously, the mapping from names to symbols implemented by a package is slightly more flexible than a simple lookup table. At its core, every package contains a name-to-symbol lookup table, but a symbol can be made accessible via an unqualified name in a given package in other ways. To talk sensibly about these other mechanisms, you'll need a little bit of vocabulary.
To start with, all the symbols that can be found in a given package using FIND- SYMBOL
are said to be
A symbol can be accessible in two ways. The first is for the package's name-to-symbol table to contain an entry for the symbol, in which case the symbol is said to be
The other way a symbol can be accessible in a package is if the package
To keep the mappings from names to symbols deterministic, the package system allows only one symbol to be accessible in a given package for each name. That is, a package can't have a present symbol and an inherited symbol with the same name or inherit two different symbols, from different packages, with the same name. However, you can resolve conflicts by making one of the accessible symbols a
An existing symbol can be