Packages

Packages provide separate name spaces for groups of symbols. Common Lisp introduced the package system in order to reduce the symbol (function and variable name) conflict problems in the course of developing huge software systems which require more than one programmer to work together. Each package may have internal symbols and external symbols. When a symbol is created in a package, it is always internal, and it becomes external by export. External symbols in different packages are referenced by prefixing the package name and a single colon, as x:*display*, while referencing internal symbols in other packages requires double colons, as sys::free-threads. In order to omit this package prefixing, a package may import symbols from other packages. Moreover, use-package allows importing all external symbols from another package at once. When symbols are exported or imported, symbol name conflicts can be detected, since every symbol in any packages must have the unique print name. Shadow allows creating a symbol with the same print name as the existing symbol in a package by virtually removing the old symbol from the package.

EusLisp defines following eight packages;

lisp:
All the lisp functions, macros, constants, etc.
keyword:
keyword symbols
unix:
unix system calls and library functions
system:
system management or dangerous functions; nicknames=sys,si
compiler:
EusLisp compiler; nicknames=comp
user:
User's work space
geometry:
geometric classes and functions
xwindow:
X-window interface; nickname=x

These packages and user-defined packages are linked in the system's package list, which can be obtained by list-all-packages. Each package manages two hash tables to find and locate internal and external symbols. Also, a package records its name (string or symbol) and a list of nick names, and a list of other packages that the package is using. *Package* is a special variable that holds the current package for read and print. If *package* is not user:, top-level prompt changes to indicate the current package, like mypkg:eus$.



*lisp-package* [constant]

Lisp package.


*user-package* [constant]
User package.


*unix-package* [constant]
Unix package.


*system-package* [constant]
System Package.


*keyword-package* [constant]
Keyword Package.


find-symbol string &optional (package *package*) [function]

finds and locates the symbol which has string as its print name in pacakge. If found, the symbol is returned, NIL otherwise.


make-symbol string [function]

makes a new uninterned symbol by the print name of string.


intern string &optional (package *package*) (klass symbol) [function]

tries to find a symbol whose print-name is same with string. If the search succeeds, the symbol is returned. If fails, a symbol whose print-name is string is newly made, and is located in package.


list-all-packages [function]

returns the list of all packages ever made.


find-package name [function]

find the package whose name or nickname is equal to the name string.


make-package name &key nicknames (use '(lisp)) [function]

makes a new package by the name of name. Name can either be a string or a symbol. If the package already exists, error is reported.


in-package pkg &key nicknames (uses '(lisp)) [function]

changes the current pacakge (the value of *pacakge*) to pkg.


package-name pkg [function]

returns the string name of the pkg package.


package-nicknames pkg [function]

returns a list of nicknames of pkg.


rename-package pkg new-name &optional new-nicknames [function]

changes the name of pkg to new-name and its nicknames to new-nicknames, which can either be a symbol, a string, or a list of symbols or strings.


package-use-list pkg [function]

returns the list of packages which are used by pkg.


packagep pkg [function]

T if pkg is a package.


use-package pkg &optional (curpkg *package*) [function]

adds pkg to curpkg's use-list. Once added, symbols in pkg become visible from curpkg without package prefix.


unuse-package pkg &optional (curpkg *package*) [function]

removes pkg from curpkg's use-list.


shadow sym &optional(pkg *package*) [function]

makes a symbol interned in pkg, by hiding existing sym.


export sym &optional (pkg *package*) [function]

sym is a symbol or a list of symbols. export makes sym accessible from other packages as external symbol(s). Actually, sym is registered as an external symbol in pkg. If a symbol is exported, it becomes accessible using a single colon ":" as package marker, whereas unexported symbols require double colons. In addition, exported symbols do not need colons when they are used by use-package or they are imported into the package. Whether a symbol is exported or not is attributed to packages where it is interned, not to each symbol. So, a symbol can be internal in a package and external in another. Export checks sym to have name conflict with symbols in other packages using pkg. If there is a symbol having the same print name with sym, ``symbol conflict" error is reported.


unexport sym &optional pkg [function]

If sym is an external symbol in pkg, it is unexported and becomes an internal symbol.


import sym &optional (pkg *package*) [function]

sym is a symbol or a list of symbols. import makes symbols defined in other packages visible in pkg as an internal symbol without package prefix. If there is already a symbol that has the same print name as sym, then an ``name conflict" error is reported.


do-symbols (var pkg) &rest forms [macro]

repeats evaluatiing forms for each binding of var to symbols (internal or external) in pkg.


do-external-symbols (var pkg) &rest forms [macro]

repeats evaluating forms for each binding of var to external symbols in pkg.


do-all-symbols (var [result]) &rest forms [macro]

repeats evaluating forms for each binding of var to symbols in all packages. Note that forms may be evaluated more than once to a symbol if it appears more than one package.


k-okada 2013-05-21