[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

LISP-SYMBOL-REDEFINITION



This issue was discussed on this mail list over a year ago, and has come up from
time to time on the general Common Lisp list. For a while, there was a volunteer
for creating a writup, but the mail to the volunteer was returned. So...
finally....

!

Issue:         LISP-SYMBOL-REDEFINITION

References:    Cleanup issue PACKAGE-CLUTTER
		CLtL pp 67-69 Defining named functions

Category:      CLARIFICATION

Edit history:  Masinter, Version 1, 17-Sep-88

Problem description:

CLtL allows the redefinition of functions exported from other packages.
Unexperienced programmers may redefine a system function unintentional  which
may result into an inconsistent state of the system and cause to  abort. This
has happened, for example, with a beginner follows an introductory 
book where an exercise asks to define a function make-list. After the
redefinition of make-list the system crashs without returning a message that the
function has been  redefined.

CLtL only says that special forms can not be redefined. But this doesn't 
solve the general problem of redefining system functions.

Proposal LISP-SYMBOL-REDEFINITION:DISALLOW
 
The results of redefining as a function or variable any of the functions,
macros, special forms, or constants defined in the LISP package are unspecified;
similarly, the result of locally binding (with LAMBDA, LET, LET* etc) any
constant, or lexically defining (with FLET or LABELS) any function in the LISP
package is unspecified.

The results of applying TRACE to any function in the LISP package is
unspecified.

Following the proposed definition of "results are unspecified", this means that
is, implementations may signal an error, or other unspecified behavior may
ensue. For example, programming environments may warn the user about
redefinition of LISP symbols and then allow them. Some environments may
distinguish between functions that are safe to redefine and those that are not.

Examples:

The behavior of the construct:

(FLET ((OPEN (filename &key direction) (format t "Open called....") 
			(OPEN filename :direction direction)))
    (with-open-file (x "frob" :direction ':output) 
		(format t "was Open called?")))

is unspecified; for example, the macro expansion of with-open-file might refer
to the OPEN function and might not.

(defun car (x) (cdr x))

might signal an error.

Rationale:

Allowing arbitrary redefinition of symbols in the LISP package would place
severe restrictions on implementations not to actually use those symbols in
macro expansions of other LISP symbols, in function calls, etc. While some
looser restrictions might do for any particular Common Lisp implementation,
there seems to be no good way to distinguish between those symbols that are
redefinable and those that are not.

In general, programs can redefine functions safely by creating new symbols in
their own package, possibly shadowing the name.

Current practice:

Many Lisp environments have some mechanism for warning about redefinition of
Lisp symbols and preventing accidental redefinition while allowing it where
necessary (e.g., to patch the Lisp system itself, fix a bug, add an
optimization.)

Fewer check for lexical redefinition, since such redefinition is not as
dangerous. Certainly, there are some symbols that are never used in macro
expansions of the standard Common Lisp macros. However, implementations do
differ on the behavior of macro expansions.

Cost to Implementors:

This proposal clarifies the status quo -- that the results are unspecified. It
allows and encourages implementors to check for such redefinition, but does not
require it.

Cost to Users:

This proposal clarifies that implementations are free to check for a condition
that they might not have before, and may clarify that some current user code is
non-portable.

Benefits:

This issue frequently arises. Adopting this proposal would clarify a frequent
source of question about Common Lisp. 

Cost of non-adoption:

Continued questions.

Esthetics:

Disallowing all redefinition is the simplest way of disallowing the ones that
really are trouble. 

Discussion:

There have been various proposals for allowing users to extend the "protection"
mechanism to their own macros, functions, packages. These proposals seem like
they are environment issues and not language ones, however.