Packages
Packages exist to avoid the usual danger of overwriting when two programmers accidently choose the same symbols
in their programs to identify different values. The polyadic function package has the form (package S L E1 ... En)
where
1. S is a symbol beginning in lowercase which is the name of a package; (e.g mypackage).
2. A list L (possibly empty) of non-variable symbols.
3. E1 ... En are a series of Shen expressions.
The Shen reader prepends the package symbol S followed by dot (.) before all the symbols when evaluating E1 ... En
apart from those symbols which are external, which are
- symbols listed as belonging to the system (such as ->, define, cons, append etc) or ... These are in fact the external symbols of the Shen package in which Shen is coded and may be called up by the expression (external shen).
- symbols which are variables or ...
- ... symbols which are listed in L or .....
- ... symbols internal to the Shen package (shen) or ....
- ... symbols declared as system functions using 'systemf'.
- ... symbols consisting entirely of underscores or entirely of equals signs.
- ... symbols already packaged under S (a rule introduced in Shen 19).
Symbols which are prepended we say are internal (to the package). Symbols which are not are
external .
Hence (package mypackage [main] (define main ...) ....) will cause Shen to make all user functions read
in its scope to be internal to mypackage apart from the symbol main which will be external.
The philosophy of Shen is that once a programmer has decided a symbol is internal to a package
and is hidden from view that decision cannot be overridden except by changing the definition of the
package. Hence the complexities of IMPORT and EXPORT found in Common Lisp are not reproduced in Shen.
It is possible to declare a package in a package.
The null package written (package null ....) has no effect on its contents. This is used in
certain advanced applications involving reader macros.
The function 'external' takes a package name and returns the list of all those symbols which
have been declared external to that package at the point the function is called. If the package does
not exist, an error is raised.
The function 'internal' takes a package name and returns the list of all those symbols which
have been found to be internal to that package at the point the function is called. If the package
does not exist, an error is raised. This function was introduced in Shen 19.
Note that Shen will allow you to reference symbols that are internal to a package by citing
the package name (e.g. mypackage.foo).
Symbols which are external to the Shen package may not be redefined; the user cannot redefine
'append' for instance. The function systemf of type symbol --> symbol applied to any symbol gives that
symbol the authority of an external symbol of the Shen package and the functional definition attached
to that symbol cannot thereafter be overwritten. From version 17, systemf returns its argument.
Version 17 introduced 'package?' of type symbol --> boolean which returns true when the package
exists and false otherwise.
The S series kernels allow the user to place the REPL inside a package. The command
'(in-package package name)' will do this. All symbols heading applications or in the
scope of 'fn' will be renamed according to the package conventions. '(in-package null)' restores
the default behaviour. |