I’m now going to depart a bit from my alphabetical walk through Lisp features, and from my normal publishing schedule, to talk about packages. This is specifically to try to better describe some of the ways that Lisp works following criticism of my terminology in articles about fboundp and fdefinition.
In trying to describe some Lisp concepts in terms familiar to C++ programmers, I tried to draw a parallel between interned symbols in Lisp and the symbol tables used, for example in ELF binary format objects, by the linker stage in C++ compilation. While there is a logical parallel, it isn’t a very good correspondence, and if you attach too much significance to the logical connections it can lead you to a misleading understanding of Lisp. I apologize for that, so here we will go into some more fine detail.
Now, you might be tempted to argue away the distinction, saying that the logical entities described are similar, but there are some very important differences, not just in the implementation, but in the language behaviour. Programmers are wisely enjoined not to program to an implementation, but rather to program to the virtual model defined by the standard or API. A language standard typically defines “as if” rules, that describe how a conforming implementation must behave as seen from within the program. This allows the standard to define a common virtual platform, and it is the responsibility of the particular hardware and software implementation to present that virtual platform to the programmer. When the programmer writes code against the standard-defined virtual platform, rather than against a particular implementation, he or she is much more likely to produce portable, readable code that remains correct as compilers are upgraded or as underlying hardware is replaced. The differences between Lisp interned symbols and C++ linker symbol tables are not implementation differences, they are logical differences that must be understood by the programmer.
So, I’m inserting this article, out of sequence, so that I can stop referring to “function symbol tables” in Lisp. That’s not what they are. And to understand what they really are, we need to describe packages, which really means understanding the intern function.
I’ve been talking about packages throughout this series, describing them a bit like C++ namespaces but with the added feature of inheritance. Now, though, it’s time to stop talking about parallels and similarities, and explain what a Lisp package really is.
So, a Lisp package is a namespace. It’s a container for interned symbols (I’ll get to what we mean by that shortly) that allows the programmer to avoid symbol collisions. Package inheritance is used to allow the reader to locate symbols from other packages when the symbol is not present in the package in which the reader is running (the “current package”). EDIT: Please see additional text below, added 2014-02-25.
The Lisp language features themselves are present in a package, the COMMON-LISP package, also available under the nickname CL. If you define a package which does not directly inherit from COMMON-LISP, you will find that the Lisp language itself appears to be unavailable within your new package! Invoking defun will produce an error declaring that defun is an unknown function. I say “appears to be unavailable” because the features are still there and reachable, but if you haven’t inherited from COMMON-LISP you will have to use an explicit CL: prefix just to invoke what you think of as the normal features of the Lisp language. That CL: prefix is an example of a package prefix. I’ll be using that term a bit in the text that follows.
So, how does a package work? Well, you create it, and you typically use the :use argument to inherit from COMMON-LISP, and zero or more other packages. You can then intern symbols in this new package. When the reader is asked to interpret a symbol without a package prefix, it looks first in the internal and external symbols of the current package. If there is no matching interned symbol in the current package, it then looks through the external symbols of the packages from which it directly inherits (it does not recurse through their :use lists). The order in which these packages is searched is not defined, conflicts must always be explicitly resolved. If the package inherits the same symbol from two other packages, the result is a correctable error. The programmer is responsible for resolving the conflict, either by interning a symbol in the current package and so shadowing both conflicting symbols, or by adjusting the inheritance in order to specify which symbol is to be used.
You know, this is starting to get dry and abstract, and I’m trying to avoid that trap in these articles. Let’s see if I can bring things back towards the concrete a bit. An example of a symbol might be ‘my-worker-function. A symbol with a package prefix could be ‘my-package:my-worker-function. When the reader encounters ‘my-worker-function, it looks for that symbol in the current package. If that symbol is present, either as an internal or external symbol, it is used, there is no ambiguity. If that symbol is not present in the current package, then the query moves on to the packages from which the current package inherits, if any. If no match is found, an error is signaled as if you had invoked an unknown function. What if more than one package exports that symbol? Well, that conflict is generally not detected at this point. Symbol conflicts are checked whenever a change occurs that makes them possible. That includes when evaluating a defpackage form, or if, in the course of the code executing, it invokes the unintern function on a symbol that was shadowing a conflict between two or more other packages. When the reader is trying to look up a symbol, it knows that conflict resolution has already been performed, so the possible outcomes are limited to a failure to find any such symbol, or a successful and unambiguous resolution of the symbol.
Let’s see what this looks like with a transcript from a Lisp session:
CL-USER> (defpackage :pkg-a (:use :CL) (:export :FCN-A)) #<PACKAGE "PKG-A"> CL-USER> (in-package :pkg-a) #<PACKAGE "PKG-A"> PKG-A> (defun fcn-a () (format t "FCN-A in PKG-A~%")) FCN-A PKG-A> (fcn-a) FCN-A in PKG-A NIL PKG-A> (in-package :CL-USER) #<PACKAGE "COMMON-LISP-USER"> CL-USER> (defpackage :pkg-b (:use :CL) (:export :FCN-A)) #<PACKAGE "PKG-B"> CL-USER> (in-package :pkg-b) #<PACKAGE "PKG-B"> PKG-B> (defun fcn-a () (format t "FCN-A in PKG-B~%")) FCN-A PKG-B> (fcn-a) FCN-A in PKG-B NIL PKG-B> (in-package :CL-USER) #<PACKAGE "COMMON-LISP-USER"> CL-USER> (defpackage :pkg-c (:use :pkg-a :pkg-b) (:export :fcn-b)) ; Evaluation aborted on #<NAME-CONFLICT {10036CD023}>. CL-USER>
I created two packages, :PKG-A and :PKG-B, both inheriting from the COMMON-LISP package. Both of these packages exported the same function, fcn-a. When I tried to create a new package, :PKG-C, inheriting from both :PKG-A and :PKG-B, I got an error due to the name conflict, as each of them was exporting its own version of fcn-a.
So, now we’ll resolve the issue:
CL-USER> (defpackage :pkg-c (:use :pkg-a :pkg-b) (:shadowing-import-from :pkg-b :fcn-a) (:export :fcn-b)) #<PACKAGE "PKG-C"> CL-USER> (in-package :pkg-c) #<COMMON-LISP:PACKAGE "PKG-C"> PKG-C> (fcn-a) FCN-A in PKG-B COMMON-LISP:NIL PKG-C> (in-package :CL-USER) ; in: IN-PACKAGE :CL-USER ; (PKG-C::IN-PACKAGE :CL-USER) ; ; caught COMMON-LISP:STYLE-WARNING: ; undefined function: IN-PACKAGE ; ; compilation unit finished ; Undefined function: ; IN-PACKAGE ; caught 1 STYLE-WARNING condition ; Evaluation aborted on #<UNDEFINED-FUNCTION IN-PACKAGE {1003DBF613}>.
Here, I told the system that when defining :PKG-C, the name conflict between the two versions of fcn-a should be resolved by allowing that from :PKG-B to shadow the one from :PKG-A. But why did I get an error when I tried to switch back to the CL-USER package? By omitting COMMON-LISP from the :use list of :PKG-C, I found myself in an environment where the bare in-package symbol, without a package prefix, was not recognized. This is what I meant earlier about symbols not being resolved recursively through the inheritance tree. Even though both :PKG-A and :PKG-B inherit from COMMON-LISP, :PKG-C does not have automatic access to the symbols in COMMON-LISP when invoked without a package prefix. If, instead, I had invoked (COMMON-LISP:in-package :CL-USER), it would have worked as expected.
OK, so that’s a rough look at packages, so what about intern? This is where the separation from C++ symbol tables becomes obvious. The intern function causes a symbol to be “known to” a package. Nothing is said about what this symbol represents, it’s simply added to the list of recognized symbols for the package. By itself, an interned symbol isn’t very interesting. You typically want the symbol to allow you to look up something, like a variable, or a function. So, an interned symbol has five associated fields. Note that these are language features, not implementation details.
- The name of the symbol, as a string
- The package in which the symbol is interned
- The property list associated with the symbol
- The value the symbol should return, when queried
- The function the symbol should return, when queried
The name of the symbol is pretty obvious. The package field indicates where the symbol is interned, so it allows the programmer to determine whether the symbol was defined in the current package or inherited from another. The property list field allows key/value pairs to be strung onto a symbol. The value and the function fields decide what the symbol represents in contexts where a value or a function are being requested. The point to notice, however, is that the assignment of the last three fields is independent of the intern operation itself. So, defining a function with defun has the effect of first intern-ing the symbol (if it is not yet interned) and then attaching a function definition to the function field of the symbol. Similarly, defining a variable with setq first interns the symbol and then attaches a value to the value-field of the symbol. This is why I mentioned earlier that the function and value namespaces are distinct in Lisp, unlike in C++. A symbol can routinely be used in both function and value contexts, and the correct value will be retrieved.
So, you might ask what the big deal is about talking about a function symbol table, if the functions are isolated from the values, then you can talk about a logical entity with the qualities of a function symbol table. The problem, however, that could mislead the novice, is in the way these fields are all tied together under the same symbol. If you unintern a symbol, you delete the reference not just the function associated with it, but also to the value and to the property list. It’s an over-simplification to talk about functions as strictly separate from values. In order to understand what is really happening and to avoid surprises when coding, this internal logical structure of packages to symbol lists to associated fields must be understood.
And so, with this lengthy overview posted, I’m going to stop talking about packages, symbols, and namespaces in round-about and imprecise ways. We have symbols, and packages, and the language defines how they behave, and that’s how I’ll be referring to them from now on.
EDIT #1: 2014-02-25
Following the discussion with Janis in the comments below, I’m adding some more text to help clarify packages and symbols and, I hope, avoid engendering confusion in newcomers.
So, let’s start by clarifying symbols. A symbol object has, as described above, 5 associated data fields. A symbol objects can be uninterned, which means that there is no package that maps to that symbol. The make-symbol command, which we have not covered at the time of this edit, can be used to construct an uninterned symbol object. You might also look over the description at gensym for examples of uninterned symbols and what it means for symbols to be “equal”.
A package implements a mapping from the names of interned and inherited symbols to the symbol objects themselves. Inherited symbols must themselves have been interned in the package from which they are inherited, one cannot inherit uninterned symbols. Modification of that mapping is through defpackage, export, import, intern, shadow, shadowing-import, unexport, unintern, unuse-package, and use-package. However packages are implemented, they behave as if a package contains mappings for all symbols that it itself interns, plus also all symbols that it imports or inherits from other packages. The Lisp instance maintains a state in a dynamic variable, *package*, which denotes the “current package”. When the Lisp instance needs to look up a symbol by name, then if that name does not contain a package prefix, the package specified by *package* is used. Naturally, if the symbol name contains a package prefix, that package is used for the lookup.
So, symbols are objects, and some symbols may be interned. A package is a mapping from the package-unique name of an interned symbol to the symbol object.