Using mutable data as hash table keys in Common Lisp?
It appears that Common Lisp allows mutable data to be used as hash table keys.
(defparameter *dict* (make-hash-table))
(defparameter *a* (make-hash-table))
(setf (gethash *a* *dict*) 5)
(loop for key being the hash-keys of *dict*
do (progn
(print key)
(print (gethash key *dict*))))
Here a hash table is used as a key in another hash table.
I'm a bit confused by this behavior. It is my understanding that keys that are mutable can mess with the hash if the key object is mutated.
How does the hash table maintain its integrity, and more importantly - is there anything one should know when dealing with mutable hash table keys in CL? Is this something to be avoided?
common-lisp
add a comment |
It appears that Common Lisp allows mutable data to be used as hash table keys.
(defparameter *dict* (make-hash-table))
(defparameter *a* (make-hash-table))
(setf (gethash *a* *dict*) 5)
(loop for key being the hash-keys of *dict*
do (progn
(print key)
(print (gethash key *dict*))))
Here a hash table is used as a key in another hash table.
I'm a bit confused by this behavior. It is my understanding that keys that are mutable can mess with the hash if the key object is mutated.
How does the hash table maintain its integrity, and more importantly - is there anything one should know when dealing with mutable hash table keys in CL? Is this something to be avoided?
common-lisp
add a comment |
It appears that Common Lisp allows mutable data to be used as hash table keys.
(defparameter *dict* (make-hash-table))
(defparameter *a* (make-hash-table))
(setf (gethash *a* *dict*) 5)
(loop for key being the hash-keys of *dict*
do (progn
(print key)
(print (gethash key *dict*))))
Here a hash table is used as a key in another hash table.
I'm a bit confused by this behavior. It is my understanding that keys that are mutable can mess with the hash if the key object is mutated.
How does the hash table maintain its integrity, and more importantly - is there anything one should know when dealing with mutable hash table keys in CL? Is this something to be avoided?
common-lisp
It appears that Common Lisp allows mutable data to be used as hash table keys.
(defparameter *dict* (make-hash-table))
(defparameter *a* (make-hash-table))
(setf (gethash *a* *dict*) 5)
(loop for key being the hash-keys of *dict*
do (progn
(print key)
(print (gethash key *dict*))))
Here a hash table is used as a key in another hash table.
I'm a bit confused by this behavior. It is my understanding that keys that are mutable can mess with the hash if the key object is mutated.
How does the hash table maintain its integrity, and more importantly - is there anything one should know when dealing with mutable hash table keys in CL? Is this something to be avoided?
common-lisp
common-lisp
asked Nov 18 '18 at 16:23
ByteByte
232313
232313
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
See 18.1.2 Modifying Hash Table Keys:
An object is visibly modified with regard to an equivalence test if there exists some set of objects (or potential objects) which are equivalent to the object before the modification but are no longer equivalent afterwards.
If an object
O1is used as a key in a hash tableHand is then visibly modified with regard to the equivalence test ofH, then the consequences are unspecified ifO1, or any objectO2equivalent toO1under the equivalence test (either before or after the modification), is used as a key in further operations onH. The consequences of usingO1as a key are unspecified even ifO1is visibly modified and then later modified again in such a way as to undo the visible modification.
In your example, the hash table test is
eql, which means that modifying
the key (adding elements to *a*) does not change the hash code (i.e., this is not a "visible modification"):
(defparameter *ht-1* (make-hash-table :test 'eql))
(defparameter *key* (cons nil nil))
(setf (gethash *key* *ht-1*) 10)
*ht-1*
==> #S(HASH-TABLE :TEST FASTHASH-EQL ((NIL) . 10))
(setf (car *key*) 42)
*ht-1*
==> #S(HASH-TABLE :TEST FASTHASH-EQL ((42) . 10))
(gethash *key* *ht-1*)
==> 10; T
(gethash '(42) *ht-1*)
==> NIL; NIL
Thus you can see that *ht-1* is keyed on the specific object rather than on anything that looks like it.
On the other hand, consider an equal hash table:
(defparameter *ht-2* (make-hash-table :test 'equal))
(setf (gethash *key* *ht-2*) 20)
*ht-2*
==> #S(HASH-TABLE :TEST FASTHASH-EQUAL ((42) . 20))
(gethash *key* *ht-2*)
==> 20; T
(setf (car *key*) 7) ; **visible modification**!
(gethash '(7) *ht-2*)
==> unspecified!
(gethash *key* *ht-2*)
==> unspecified!
(setf (car *key*) 42) ; restore key
(gethash '(42) *ht-2*)
==> unspecified!
(gethash *key* *ht-2*)
==> unspecified!
The bottom line is: do not modify hash table keys visibly!
So according to the spec, restoring (car key) to 42 is still undefined behavior, it just happens to work here, right? Also, why does 'eql/'equal become 'fasthash-eql/'fasthash-equal? Does this imply that only predetermined test functions can be used in hash tables?
– Byte
Nov 18 '18 at 18:38
See fast/stable hash keys andEXT:DEFINE-HASH-TABLE-TEST.
– sds
Nov 18 '18 at 18:55
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53363008%2fusing-mutable-data-as-hash-table-keys-in-common-lisp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
See 18.1.2 Modifying Hash Table Keys:
An object is visibly modified with regard to an equivalence test if there exists some set of objects (or potential objects) which are equivalent to the object before the modification but are no longer equivalent afterwards.
If an object
O1is used as a key in a hash tableHand is then visibly modified with regard to the equivalence test ofH, then the consequences are unspecified ifO1, or any objectO2equivalent toO1under the equivalence test (either before or after the modification), is used as a key in further operations onH. The consequences of usingO1as a key are unspecified even ifO1is visibly modified and then later modified again in such a way as to undo the visible modification.
In your example, the hash table test is
eql, which means that modifying
the key (adding elements to *a*) does not change the hash code (i.e., this is not a "visible modification"):
(defparameter *ht-1* (make-hash-table :test 'eql))
(defparameter *key* (cons nil nil))
(setf (gethash *key* *ht-1*) 10)
*ht-1*
==> #S(HASH-TABLE :TEST FASTHASH-EQL ((NIL) . 10))
(setf (car *key*) 42)
*ht-1*
==> #S(HASH-TABLE :TEST FASTHASH-EQL ((42) . 10))
(gethash *key* *ht-1*)
==> 10; T
(gethash '(42) *ht-1*)
==> NIL; NIL
Thus you can see that *ht-1* is keyed on the specific object rather than on anything that looks like it.
On the other hand, consider an equal hash table:
(defparameter *ht-2* (make-hash-table :test 'equal))
(setf (gethash *key* *ht-2*) 20)
*ht-2*
==> #S(HASH-TABLE :TEST FASTHASH-EQUAL ((42) . 20))
(gethash *key* *ht-2*)
==> 20; T
(setf (car *key*) 7) ; **visible modification**!
(gethash '(7) *ht-2*)
==> unspecified!
(gethash *key* *ht-2*)
==> unspecified!
(setf (car *key*) 42) ; restore key
(gethash '(42) *ht-2*)
==> unspecified!
(gethash *key* *ht-2*)
==> unspecified!
The bottom line is: do not modify hash table keys visibly!
So according to the spec, restoring (car key) to 42 is still undefined behavior, it just happens to work here, right? Also, why does 'eql/'equal become 'fasthash-eql/'fasthash-equal? Does this imply that only predetermined test functions can be used in hash tables?
– Byte
Nov 18 '18 at 18:38
See fast/stable hash keys andEXT:DEFINE-HASH-TABLE-TEST.
– sds
Nov 18 '18 at 18:55
add a comment |
See 18.1.2 Modifying Hash Table Keys:
An object is visibly modified with regard to an equivalence test if there exists some set of objects (or potential objects) which are equivalent to the object before the modification but are no longer equivalent afterwards.
If an object
O1is used as a key in a hash tableHand is then visibly modified with regard to the equivalence test ofH, then the consequences are unspecified ifO1, or any objectO2equivalent toO1under the equivalence test (either before or after the modification), is used as a key in further operations onH. The consequences of usingO1as a key are unspecified even ifO1is visibly modified and then later modified again in such a way as to undo the visible modification.
In your example, the hash table test is
eql, which means that modifying
the key (adding elements to *a*) does not change the hash code (i.e., this is not a "visible modification"):
(defparameter *ht-1* (make-hash-table :test 'eql))
(defparameter *key* (cons nil nil))
(setf (gethash *key* *ht-1*) 10)
*ht-1*
==> #S(HASH-TABLE :TEST FASTHASH-EQL ((NIL) . 10))
(setf (car *key*) 42)
*ht-1*
==> #S(HASH-TABLE :TEST FASTHASH-EQL ((42) . 10))
(gethash *key* *ht-1*)
==> 10; T
(gethash '(42) *ht-1*)
==> NIL; NIL
Thus you can see that *ht-1* is keyed on the specific object rather than on anything that looks like it.
On the other hand, consider an equal hash table:
(defparameter *ht-2* (make-hash-table :test 'equal))
(setf (gethash *key* *ht-2*) 20)
*ht-2*
==> #S(HASH-TABLE :TEST FASTHASH-EQUAL ((42) . 20))
(gethash *key* *ht-2*)
==> 20; T
(setf (car *key*) 7) ; **visible modification**!
(gethash '(7) *ht-2*)
==> unspecified!
(gethash *key* *ht-2*)
==> unspecified!
(setf (car *key*) 42) ; restore key
(gethash '(42) *ht-2*)
==> unspecified!
(gethash *key* *ht-2*)
==> unspecified!
The bottom line is: do not modify hash table keys visibly!
So according to the spec, restoring (car key) to 42 is still undefined behavior, it just happens to work here, right? Also, why does 'eql/'equal become 'fasthash-eql/'fasthash-equal? Does this imply that only predetermined test functions can be used in hash tables?
– Byte
Nov 18 '18 at 18:38
See fast/stable hash keys andEXT:DEFINE-HASH-TABLE-TEST.
– sds
Nov 18 '18 at 18:55
add a comment |
See 18.1.2 Modifying Hash Table Keys:
An object is visibly modified with regard to an equivalence test if there exists some set of objects (or potential objects) which are equivalent to the object before the modification but are no longer equivalent afterwards.
If an object
O1is used as a key in a hash tableHand is then visibly modified with regard to the equivalence test ofH, then the consequences are unspecified ifO1, or any objectO2equivalent toO1under the equivalence test (either before or after the modification), is used as a key in further operations onH. The consequences of usingO1as a key are unspecified even ifO1is visibly modified and then later modified again in such a way as to undo the visible modification.
In your example, the hash table test is
eql, which means that modifying
the key (adding elements to *a*) does not change the hash code (i.e., this is not a "visible modification"):
(defparameter *ht-1* (make-hash-table :test 'eql))
(defparameter *key* (cons nil nil))
(setf (gethash *key* *ht-1*) 10)
*ht-1*
==> #S(HASH-TABLE :TEST FASTHASH-EQL ((NIL) . 10))
(setf (car *key*) 42)
*ht-1*
==> #S(HASH-TABLE :TEST FASTHASH-EQL ((42) . 10))
(gethash *key* *ht-1*)
==> 10; T
(gethash '(42) *ht-1*)
==> NIL; NIL
Thus you can see that *ht-1* is keyed on the specific object rather than on anything that looks like it.
On the other hand, consider an equal hash table:
(defparameter *ht-2* (make-hash-table :test 'equal))
(setf (gethash *key* *ht-2*) 20)
*ht-2*
==> #S(HASH-TABLE :TEST FASTHASH-EQUAL ((42) . 20))
(gethash *key* *ht-2*)
==> 20; T
(setf (car *key*) 7) ; **visible modification**!
(gethash '(7) *ht-2*)
==> unspecified!
(gethash *key* *ht-2*)
==> unspecified!
(setf (car *key*) 42) ; restore key
(gethash '(42) *ht-2*)
==> unspecified!
(gethash *key* *ht-2*)
==> unspecified!
The bottom line is: do not modify hash table keys visibly!
See 18.1.2 Modifying Hash Table Keys:
An object is visibly modified with regard to an equivalence test if there exists some set of objects (or potential objects) which are equivalent to the object before the modification but are no longer equivalent afterwards.
If an object
O1is used as a key in a hash tableHand is then visibly modified with regard to the equivalence test ofH, then the consequences are unspecified ifO1, or any objectO2equivalent toO1under the equivalence test (either before or after the modification), is used as a key in further operations onH. The consequences of usingO1as a key are unspecified even ifO1is visibly modified and then later modified again in such a way as to undo the visible modification.
In your example, the hash table test is
eql, which means that modifying
the key (adding elements to *a*) does not change the hash code (i.e., this is not a "visible modification"):
(defparameter *ht-1* (make-hash-table :test 'eql))
(defparameter *key* (cons nil nil))
(setf (gethash *key* *ht-1*) 10)
*ht-1*
==> #S(HASH-TABLE :TEST FASTHASH-EQL ((NIL) . 10))
(setf (car *key*) 42)
*ht-1*
==> #S(HASH-TABLE :TEST FASTHASH-EQL ((42) . 10))
(gethash *key* *ht-1*)
==> 10; T
(gethash '(42) *ht-1*)
==> NIL; NIL
Thus you can see that *ht-1* is keyed on the specific object rather than on anything that looks like it.
On the other hand, consider an equal hash table:
(defparameter *ht-2* (make-hash-table :test 'equal))
(setf (gethash *key* *ht-2*) 20)
*ht-2*
==> #S(HASH-TABLE :TEST FASTHASH-EQUAL ((42) . 20))
(gethash *key* *ht-2*)
==> 20; T
(setf (car *key*) 7) ; **visible modification**!
(gethash '(7) *ht-2*)
==> unspecified!
(gethash *key* *ht-2*)
==> unspecified!
(setf (car *key*) 42) ; restore key
(gethash '(42) *ht-2*)
==> unspecified!
(gethash *key* *ht-2*)
==> unspecified!
The bottom line is: do not modify hash table keys visibly!
edited Nov 18 '18 at 18:53
answered Nov 18 '18 at 17:07
sdssds
39.1k1494168
39.1k1494168
So according to the spec, restoring (car key) to 42 is still undefined behavior, it just happens to work here, right? Also, why does 'eql/'equal become 'fasthash-eql/'fasthash-equal? Does this imply that only predetermined test functions can be used in hash tables?
– Byte
Nov 18 '18 at 18:38
See fast/stable hash keys andEXT:DEFINE-HASH-TABLE-TEST.
– sds
Nov 18 '18 at 18:55
add a comment |
So according to the spec, restoring (car key) to 42 is still undefined behavior, it just happens to work here, right? Also, why does 'eql/'equal become 'fasthash-eql/'fasthash-equal? Does this imply that only predetermined test functions can be used in hash tables?
– Byte
Nov 18 '18 at 18:38
See fast/stable hash keys andEXT:DEFINE-HASH-TABLE-TEST.
– sds
Nov 18 '18 at 18:55
So according to the spec, restoring (car key) to 42 is still undefined behavior, it just happens to work here, right? Also, why does 'eql/'equal become 'fasthash-eql/'fasthash-equal? Does this imply that only predetermined test functions can be used in hash tables?
– Byte
Nov 18 '18 at 18:38
So according to the spec, restoring (car key) to 42 is still undefined behavior, it just happens to work here, right? Also, why does 'eql/'equal become 'fasthash-eql/'fasthash-equal? Does this imply that only predetermined test functions can be used in hash tables?
– Byte
Nov 18 '18 at 18:38
See fast/stable hash keys and
EXT:DEFINE-HASH-TABLE-TEST.– sds
Nov 18 '18 at 18:55
See fast/stable hash keys and
EXT:DEFINE-HASH-TABLE-TEST.– sds
Nov 18 '18 at 18:55
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53363008%2fusing-mutable-data-as-hash-table-keys-in-common-lisp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown