1
0
Fork 0
cl-sites/HyperSpec-7-0/HyperSpec/Issues/iss187_w.htm
2024-04-01 10:24:07 +02:00

265 lines
19 KiB
HTML

<!-- Common Lisp HyperSpec (TM), version 7.0 generated by Kent M. Pitman on Mon, 11-Apr-2005 2:31am EDT -->
<HTML>
<HEAD>
<TITLE>CLHS: Issue HASH-TABLE-KEY-MODIFICATION Writeup</TITLE>
<LINK HREF="../Data/clhs.css" REL="stylesheet" TYPE="text/css" />
<META HTTP-EQUIV="Author" CONTENT="Kent M. Pitman">
<META HTTP-EQUIV="Organization" CONTENT="LispWorks Ltd.">
<LINK REL=TOP HREF="../Front/index.htm">
<LINK REL=COPYRIGHT HREF="../Front/Help.htm#Legal">
<LINK REL=DISCLAIMER HREF="../Front/Help.htm#Disclaimer">
<LINK REL=PREV HREF="../Issues/iss186_w.htm">
<LINK REL=UP HREF="../Issues/iss187.htm">
<LINK REL=NEXT HREF="../Issues/iss188_w.htm">
</HEAD>
<BODY>
<H1><A REV=MADE HREF="http://www.lispworks.com/"><IMG WIDTH=80 HEIGHT=65 ALT="[LISPWORKS]" SRC="../Graphics/LWSmall.gif" ALIGN=Bottom></A><A REL=TOP HREF="../Front/index.htm"><IMG WIDTH=237 HEIGHT=65 ALT="[Common Lisp HyperSpec (TM)]" SRC="../Graphics/CLHS_Sm.gif" ALIGN=Bottom></A> <A REL=PREV HREF="../Issues/iss186_w.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Previous]" SRC="../Graphics/Prev.gif" ALIGN=Bottom></A><A REL=UP HREF="../Issues/iss187.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Up]" SRC="../Graphics/Up.gif" ALIGN=Bottom></A><A REL=NEXT HREF="../Issues/iss188_w.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Next]" SRC="../Graphics/Next.gif" ALIGN=Bottom></A></H1>
<HR>
<H2>Issue HASH-TABLE-KEY-MODIFICATION Writeup</H2>
<PRE><B>Issue:</B> <A HREF="iss187.htm">HASH-TABLE-KEY-MODIFICATION</A><P>
<B>References:</B> CLtL page 282, page 168 (last paragraph in 10.2)<P>
X3J13 Proposal <A HREF="iss143.htm">EQUAL-STRUCTURE:MAYBE-STATUS-QUO</A> (passed 6/89)<P>
<B>Category:</B> CLARIFICATION<P>
<B>Edit history:</B> Version 1, 12-Sep-88, Moon, for discussion<P>
Version 2, 05-Feb-90, by Kim A. Barrett,<P>
rewrite with more explicit constraints<P>
Version 3, 16-Feb-90, by Kim Barrett, KMP (respond to comments from Moon)<P>
<P>
<B>Problem description:<P>
</B><P>
CLtL is silent about what happens if you modify a component of an object that<P>
is used as a <A REL=DEFINITION HREF="../Body/t_hash_t.htm#hash-table"><B>hash-table</B></A> key.<P>
<P>
<B>Proposal (HASH-TABLE-KEY-MODIFICATION:SPECIFY):<P>
</B><P>
Define that the function supplied as the :TEST argument to <A REL=DEFINITION HREF="../Body/f_mk_has.htm#make-hash-table"><B>MAKE-HASH-TABLE</B></A><P>
specifies the `equivalence test' for the new hash table.<P>
<P>
Define that an object is `visibly modified' with regard to an equivalence test<P>
if there exists some set of objects (or potential objects) which are equivalent<P>
to the object before the modification but are no longer equivalent afterwards.<P>
<P>
If an object is used as a key in a hash table and is then visibly modified<P>
with regard to the equivalence test of the hash table, then using the object<P>
as a key in further operations on the hash table has unspecified consequences.<P>
Moreover, this applies for other objects which either are or were equivalent<P>
to the key object. Further, undoing the modification does not remove this<P>
effect.<P>
<P>
Following are specifications of the modifications which are visible to the<P>
equivalence tests which must be supported by hash tables. The modifications<P>
are described in terms of modification of components, and are defined<P>
recursively. Visible modifications of components of the object are visible<P>
modifications of the object.<P>
<P>
1. <A REL=DEFINITION HREF="../Body/f_eq.htm#eq"><B>EQ</B></A> and <A REL=DEFINITION HREF="../Body/a_eql.htm#eql"><B>EQL</B></A><P>
Common Lisp does not specify any <A REL=DEFINITION HREF="../Body/t_method.htm#method"><B>method</B></A> for visibly modifying any data type<P>
with regard to either of these equivalence tests.<P>
<P>
2. <A REL=DEFINITION HREF="../Body/f_equal.htm#equal"><B>EQUAL</B></A><P>
As a consequence of the behavior for <A REL=DEFINITION HREF="../Body/f_equal.htm#equal"><B>EQUAL</B></A> specified by Issue<P>
<A HREF="iss143.htm">EQUAL-STRUCTURE</A>, the specification for this case reverts to the previous<P>
description for <A REL=DEFINITION HREF="../Body/f_eq.htm#eq"><B>EQ</B></A> and <A REL=DEFINITION HREF="../Body/a_eql.htm#eql"><B>EQL</B></A> for many data types. The exceptions are<P>
<P>
a. <A REL=DEFINITION HREF="../Body/a_cons.htm#cons"><B>CONS</B></A><P>
The <A REL=DEFINITION HREF="../Body/f_car_c.htm#car"><B>car</B></A> and <A REL=DEFINITION HREF="../Body/f_car_c.htm#cdr"><B>cdr</B></A>.<P>
<P>
b. <A REL=DEFINITION HREF="../Body/t_bt_vec.htm#bit-vector"><B>BIT-VECTOR</B></A><P>
Elements of the vector, limited by the <A REL=DEFINITION HREF="../Body/f_fill_p.htm#fill-pointer"><B>fill-pointer</B></A> if present.<P>
The length of the vector, if adjustable or a <A REL=DEFINITION HREF="../Body/f_fill_p.htm#fill-pointer"><B>fill-pointer</B></A> is present.<P>
<P>
c. <A REL=DEFINITION HREF="../Body/a_string.htm#string"><B>STRING</B></A><P>
Same as for <A REL=DEFINITION HREF="../Body/t_bt_vec.htm#bit-vector"><B>BIT-VECTOR</B></A>.<P>
<P>
d. <A REL=DEFINITION HREF="../Body/a_pn.htm#pathname"><B>PATHNAME</B></A><P>
Common Lisp does not specify any <A REL=DEFINITION HREF="../Body/t_method.htm#method"><B>method</B></A> for visibly modifying a <A REL=DEFINITION HREF="../Body/a_pn.htm#pathname"><B>PATHNAME</B></A><P>
with regard to <A REL=DEFINITION HREF="../Body/f_equal.htm#equal"><B>EQUAL</B></A>.<P>
<P>
3. <A REL=DEFINITION HREF="../Body/f_equalp.htm#equalp"><B>EQUALP</B></A><P>
As a consequence of the behavior for <A REL=DEFINITION HREF="../Body/f_equalp.htm#equalp"><B>EQUALP</B></A> specified by Issue<P>
<A HREF="iss143.htm">EQUAL-STRUCTURE</A>, the specification for this case reverts to the previous<P>
description for <A REL=DEFINITION HREF="../Body/f_equal.htm#equal"><B>EQUAL</B></A> for many data types. The exceptions are<P>
<P>
a. <A REL=DEFINITION HREF="../Body/t_number.htm#number"><B>NUMBER</B></A><P>
Common Lisp does not specify any <A REL=DEFINITION HREF="../Body/t_method.htm#method"><B>method</B></A> for visibly modifying a <A REL=DEFINITION HREF="../Body/t_number.htm#number"><B>NUMBER</B></A><P>
with regard to <A REL=DEFINITION HREF="../Body/f_equalp.htm#equalp"><B>EQUALP</B></A>.<P>
<P>
b. <A REL=DEFINITION HREF="../Body/a_ch.htm#character"><B>CHARACTER</B></A><P>
Common Lisp does not specify any <A REL=DEFINITION HREF="../Body/t_method.htm#method"><B>method</B></A> for visibly modifying a <A REL=DEFINITION HREF="../Body/a_ch.htm#character"><B>CHARACTER</B></A><P>
with regard to <A REL=DEFINITION HREF="../Body/f_equalp.htm#equalp"><B>EQUALP</B></A>.<P>
<P>
c. <A REL=DEFINITION HREF="../Body/f_docume.htm#structure"><B>STRUCTURE</B></A><P>
The values of the slots.<P>
<P>
d. <A REL=DEFINITION HREF="../Body/a_vector.htm#vector"><B>VECTOR</B></A><P>
Elements of the vector, limited by the <A REL=DEFINITION HREF="../Body/f_fill_p.htm#fill-pointer"><B>fill-pointer</B></A> if present.<P>
The length of the vector, if adjustable or a <A REL=DEFINITION HREF="../Body/f_fill_p.htm#fill-pointer"><B>fill-pointer</B></A> is present.<P>
<P>
e. <A REL=DEFINITION HREF="../Body/t_array.htm#array"><B>ARRAY</B></A><P>
Elements of the array, limited by the <A REL=DEFINITION HREF="../Body/f_fill_p.htm#fill-pointer"><B>fill-pointer</B></A> if present.<P>
The value of the <A REL=DEFINITION HREF="../Body/f_fill_p.htm#fill-pointer"><B>fill-pointer</B></A>, if present.<P>
The dimensions, if adjustable.<P>
<P>
f. <A REL=DEFINITION HREF="../Body/t_hash_t.htm#hash-table"><B>HASH-TABLE</B></A><P>
The count of entries in the table.<P>
The keys. Note that the visibility of modifications to the keys depends<P>
on the equivalence test of the hash table, not on the specification of<P>
<A REL=DEFINITION HREF="../Body/f_equalp.htm#equalp"><B>EQUALP</B></A>. <P>
The values associated with the keys.<P>
<P>
Implementations which extend the language by providing additional mutator<P>
functions (or additional behavior for existing mutator functions) must<P>
document how the use of these extensions interacts with equivalence tests and<P>
hash table searches.<P>
<P>
Implementations which extend the language by defining additional acceptable<P>
equivalence tests for hash tables (allowing additional values for the :TEST<P>
argument to <A REL=DEFINITION HREF="../Body/f_mk_has.htm#make-hash-table"><B>MAKE-HASH-TABLE</B></A>) must document the visible components of these<P>
tests.<P>
<P>
<B>Test Cases/Examples:<P>
</B><P>
(<A REL=DEFINITION HREF="../Body/a_setf.htm#setf"><B>setf</B></A> ht (<A REL=DEFINITION HREF="../Body/f_mk_has.htm#make-hash-table"><B>make-hash-table</B></A> :test #'<A REL=DEFINITION HREF="../Body/f_eq.htm#eq"><B>eq</B></A>))<P>
(<A REL=DEFINITION HREF="../Body/a_setf.htm#setf"><B>setf</B></A> a (<A REL=DEFINITION HREF="../Body/a_cons.htm#cons"><B>cons</B></A> 1 2))<P>
(<A REL=DEFINITION HREF="../Body/a_setf.htm#setf"><B>setf</B></A> (<A REL=DEFINITION HREF="../Body/f_gethas.htm#gethash"><B>gethash</B></A> a ht) 'win)<P>
(<A REL=DEFINITION HREF="../Body/a_setf.htm#setf"><B>setf</B></A> (<A REL=DEFINITION HREF="../Body/f_car_c.htm#cdr"><B>cdr</B></A> a) 3)<P>
(<A REL=DEFINITION HREF="../Body/f_gethas.htm#gethash"><B>gethash</B></A> a ht 'lose) =&gt; win t<P>
<P>
The same example with :test #'equal in the first line would be an error.<P>
<P>
The following example is not an error, because <A REL=DEFINITION HREF="../Body/f_equal.htm#equal"><B>EQUAL</B></A> does not examine the<P>
components of general vectors:<P>
<P>
(<A REL=DEFINITION HREF="../Body/a_setf.htm#setf"><B>setf</B></A> ht (<A REL=DEFINITION HREF="../Body/f_mk_has.htm#make-hash-table"><B>make-hash-table</B></A> :test #'<A REL=DEFINITION HREF="../Body/f_equal.htm#equal"><B>equal</B></A>))<P>
(<A REL=DEFINITION HREF="../Body/a_setf.htm#setf"><B>setf</B></A> a (<A REL=DEFINITION HREF="../Body/a_vector.htm#vector"><B>vector</B></A> 1 2))<P>
(<A REL=DEFINITION HREF="../Body/a_setf.htm#setf"><B>setf</B></A> (<A REL=DEFINITION HREF="../Body/f_gethas.htm#gethash"><B>gethash</B></A> a ht) 'win)<P>
(<A REL=DEFINITION HREF="../Body/a_setf.htm#setf"><B>setf</B></A> (<A REL=DEFINITION HREF="../Body/f_aref.htm#aref"><B>aref</B></A> a 1) 3)<P>
(<A REL=DEFINITION HREF="../Body/f_gethas.htm#gethash"><B>gethash</B></A> a ht 'lose) =&gt; win t<P>
<P>
The following example is not an error, because <A REL=DEFINITION HREF="../Body/f_equalp.htm#equalp"><B>EQUALP</B></A> is limited by the<P>
<A REL=DEFINITION HREF="../Body/f_fill_p.htm#fill-pointer"><B>fill-pointer</B></A> when examining the elements of a vector:<P>
<P>
(<A REL=DEFINITION HREF="../Body/a_setf.htm#setf"><B>setf</B></A> ht (<A REL=DEFINITION HREF="../Body/f_mk_has.htm#make-hash-table"><B>make-hash-table</B></A> :test #'<A REL=DEFINITION HREF="../Body/f_equalp.htm#equalp"><B>equalp</B></A>))<P>
(<A REL=DEFINITION HREF="../Body/a_setf.htm#setf"><B>setf</B></A> a (<A REL=DEFINITION HREF="../Body/f_mk_ar.htm#make-array"><B>make-array</B></A> 3 :fill-pointer 2 :initial-contents '(1 2 3)))<P>
(<A REL=DEFINITION HREF="../Body/a_setf.htm#setf"><B>setf</B></A> (<A REL=DEFINITION HREF="../Body/f_gethas.htm#gethash"><B>gethash</B></A> a ht) 'win)<P>
(<A REL=DEFINITION HREF="../Body/a_setf.htm#setf"><B>setf</B></A> (<A REL=DEFINITION HREF="../Body/f_aref.htm#aref"><B>aref</B></A> a 2) 4)<P>
(<A REL=DEFINITION HREF="../Body/f_gethas.htm#gethash"><B>gethash</B></A> a ht 'lose) =&gt; win t<P>
<P>
The following example is an error, because <A REL=DEFINITION HREF="../Body/f_equalp.htm#equalp"><B>EQUALP</B></A> may examine the dimensions<P>
of arrays:<P>
<P>
(<A REL=DEFINITION HREF="../Body/a_setf.htm#setf"><B>setf</B></A> ht (<A REL=DEFINITION HREF="../Body/f_mk_has.htm#make-hash-table"><B>make-hash-table</B></A> :test #'<A REL=DEFINITION HREF="../Body/f_equalp.htm#equalp"><B>equalp</B></A>))<P>
(<A REL=DEFINITION HREF="../Body/a_setf.htm#setf"><B>setf</B></A> a (<A REL=DEFINITION HREF="../Body/f_mk_ar.htm#make-array"><B>make-array</B></A> '(2 3) :adjustable t))<P>
(<A REL=DEFINITION HREF="../Body/a_setf.htm#setf"><B>setf</B></A> (<A REL=DEFINITION HREF="../Body/f_gethas.htm#gethash"><B>gethash</B></A> a ht) 'win)<P>
(<A REL=DEFINITION HREF="../Body/a_setf.htm#setf"><B>setf</B></A> a (<A REL=DEFINITION HREF="../Body/f_adjust.htm#adjust-array"><B>adjust-array</B></A> a '(3 2)))<P>
(<A REL=DEFINITION HREF="../Body/f_gethas.htm#gethash"><B>gethash</B></A> a ht 'lose) =&gt; `unspecified'<P>
<P>
The following example is not an error, because <A REL=DEFINITION HREF="../Body/f_equalp.htm#equalp"><B>EQUALP</B></A> is case insensitive:<P>
<P>
(<A REL=DEFINITION HREF="../Body/a_setf.htm#setf"><B>setf</B></A> ht (<A REL=DEFINITION HREF="../Body/f_mk_has.htm#make-hash-table"><B>make-hash-table</B></A> :test #'<A REL=DEFINITION HREF="../Body/f_equalp.htm#equalp"><B>equalp</B></A>))<P>
(<A REL=DEFINITION HREF="../Body/a_setf.htm#setf"><B>setf</B></A> a &quot;ABC&quot;)<P>
(<A REL=DEFINITION HREF="../Body/a_setf.htm#setf"><B>setf</B></A> (<A REL=DEFINITION HREF="../Body/f_gethas.htm#gethash"><B>gethash</B></A> a ht) 'win)<P>
(<A REL=DEFINITION HREF="../Body/a_setf.htm#setf"><B>setf</B></A> (<A REL=DEFINITION HREF="../Body/f_char_.htm#char"><B>char</B></A> a 0) #\a)<P>
(<A REL=DEFINITION HREF="../Body/f_gethas.htm#gethash"><B>gethash</B></A> a ht 'lose) =&gt; win t<P>
<P>
The same example with :test #'equal in the first line would be an error.<P>
<P>
<B>Rationale:<P>
</B><P>
<A REL=DEFINITION HREF="../Body/f_eq.htm#eq"><B>EQ</B></A> and <A REL=DEFINITION HREF="../Body/a_eql.htm#eql"><B>EQL</B></A> hash tables use the identity of the object as the key, while <A REL=DEFINITION HREF="../Body/f_equal.htm#equal"><B>EQUAL</B></A><P>
and <A REL=DEFINITION HREF="../Body/f_equalp.htm#equalp"><B>EQUALP</B></A> hash tables use the <A REL=DEFINITION HREF="../Body/f_docume.htm#structure"><B>structure</B></A> of the object as the key. Component<P>
modification changes the <A REL=DEFINITION HREF="../Body/f_docume.htm#structure"><B>structure</B></A> of an object, while the identity of an<P>
object cannot be changed in Common Lisp.<P>
<P>
Specifying `unspecified consequences' provides implementation freedom, while<P>
still providing guidance to users.<P>
<P>
This is a generalization of the warning on p.168 of CLtL.<P>
<P>
Note that this proposal implies that it is invalid to use an overly general<P>
hash function, such as <A REL=DEFINITION HREF="../Body/f_sxhash.htm#sxhash"><B>SXHASH</B></A>, as the hash function for <A REL=DEFINITION HREF="../Body/f_eq.htm#eq"><B>EQ</B></A> or <A REL=DEFINITION HREF="../Body/a_eql.htm#eql"><B>EQL</B></A> hash tables.<P>
The value of <A REL=DEFINITION HREF="../Body/f_sxhash.htm#sxhash"><B>SXHASH</B></A> can be affected by component modifications, and this is<P>
likely to cause hash table entries to become inaccessible. The general rule<P>
for this is that a hash function must not depend on the value of some<P>
property of an object if modification of that property is not visible to the<P>
associated equivalence function.<P>
<P>
The documentation requirements for extensions seems like the minimal necessary<P>
thing, assuming we want to talk about extensions at all.<P>
<P>
<B>Current practice:<P>
</B><P>
I am not aware of any implementations that do not conform to the proposal.<P>
<P>
Some implementations which <A REL=DEFINITION HREF="../Body/f_provid.htm#provide"><B>provide</B></A> any of the described extensions may not<P>
conform to the documentation requirement.<P>
<P>
<B>Cost to Implementors:<P>
</B><P>
Most implementations probably already conform. It is possible that some<P>
implementations might have to use a different hash function in their<P>
implementation of some hash tables in order to conform.<P>
<P>
<B>Cost to Users:<P>
</B><P>
None.<P>
<P>
<B>Cost of non-adoption:<P>
</B><P>
Users would not be sure whether they were allowed to perform side effects on<P>
objects that might have been used as keys of hash tables.<P>
<P>
<B>Benefits:<P>
</B><P>
More specific language semantics.<P>
<P>
<B>Esthetics:<P>
</B> <P>
More specific language semantics.<P>
<P>
<B>Discussion:<P>
</B><P>
Version 1<P>
Discussion on the Common-Lisp mailing list was in favor of this.<P>
<P>
Version 2<P>
Barrett:<P>
The two paragraphs about language extensions could be stricken without really<P>
affecting the proposal.<P>
<P>
This proposal does not deal with any of the performance issues addressed by<P>
Issue HASH-TABLE-STABILITY. Rather, it provides guidance to users and<P>
implementors as to the requirements for conforming programs.<P>
<P>
`Unspecified consequences' could be changed to 'undefined consequences',<P>
though it seems unlikely that this is actually a 'crash and burn' situation.<P>
(The program which was depending on <A REL=DEFINITION HREF="../Body/s_the.htm#the"><B>the</B></A> result of <A REL=DEFINITION HREF="../Body/s_the.htm#the"><B>the</B></A> operation may exhibit<P>
undefined consequences as a result of <A REL=DEFINITION HREF="../Body/s_the.htm#the"><B>the</B></A> <A REL=DEFINITION HREF="../Body/t_hash_t.htm#hash-table"><B>hash-table</B></A> operation's having<P>
unspecified consequences, but that's <A REL=DEFINITION HREF="../Body/a_not.htm#not"><B>not</B></A> <A REL=DEFINITION HREF="../Body/s_the.htm#the"><B>the</B></A> same thing.)<P>
<P>
Moon:<P>
Re: Define that an object is 'visibly modified' with regard to an<P>
equivalence test if there exists some <A REL=DEFINITION HREF="../Body/t_class.htm#class"><B>class</B></A> of objects which were <P>
equivalent to the object before the modification but are no longer <P>
equivalent afterwards.<P>
Except for ... the use of &quot;class&quot; to mean &quot;set&quot; rather than the<P>
usual CLOS meaning, it looks okay. Actually where it says &quot;some<P>
<A REL=DEFINITION HREF="../Body/t_class.htm#class"><B>class</B></A> of objects&quot;, a single object would be sufficient. X is<P>
visibly modified if there exists any Y such that (<A REL=DEFINITION HREF="../Body/f_funcal.htm#funcall"><B>funcall</B></A> test X Y)<P>
produces a different result, provided (this is missing from Kim's<P>
writeup) Y has not been modified [whatever that means exactly].<P>
<P>
Pitman and Barrett support this proposal (v3).<P>
</PRE>
<HR>
<A REL=NAVIGATOR HREF="../Front/StartPts.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Starting Points]" SRC="../Graphics/StartPts.gif" ALIGN=Bottom></A><A REL=TOC HREF="../Front/Contents.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Contents]" SRC="../Graphics/Contents.gif" ALIGN=Bottom></A><A REL=INDEX HREF="../Front/X_Master.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Index]" SRC="../Graphics/Index.gif" ALIGN=Bottom></A><A REL=INDEX HREF="../Front/X_Symbol.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Symbols]" SRC="../Graphics/Symbols.gif" ALIGN=Bottom></A><A REL=GLOSSARY HREF="../Body/26_a.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Glossary]" SRC="../Graphics/Glossary.gif" ALIGN=Bottom></A><A HREF="../Front/X3J13Iss.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Issues]" SRC="../Graphics/Issues.gif" ALIGN=Bottom></A><BR>
<A REL=COPYRIGHT HREF="../Front/Help.htm#Legal"><I>Copyright 1996-2005, LispWorks Ltd. All rights reserved.</I></A><P>
</BODY>
</HTML>