Using mutable data as hash table keys in Common Lisp?












2















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?










share|improve this question



























    2















    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?










    share|improve this question

























      2












      2








      2








      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?










      share|improve this question














      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 18 '18 at 16:23









      ByteByte

      232313




      232313
























          1 Answer
          1






          active

          oldest

          votes


















          4














          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 O1 is used as a key in a hash table H and is then visibly modified with regard to the equivalence test of H, then the consequences are unspecified if O1, or any object O2 equivalent to O1 under the equivalence test (either before or after the modification), is used as a key in further operations on H. The consequences of using O1 as a key are unspecified even if O1 is 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!






          share|improve this answer


























          • 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











          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
          });


          }
          });














          draft saved

          draft discarded


















          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









          4














          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 O1 is used as a key in a hash table H and is then visibly modified with regard to the equivalence test of H, then the consequences are unspecified if O1, or any object O2 equivalent to O1 under the equivalence test (either before or after the modification), is used as a key in further operations on H. The consequences of using O1 as a key are unspecified even if O1 is 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!






          share|improve this answer


























          • 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
















          4














          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 O1 is used as a key in a hash table H and is then visibly modified with regard to the equivalence test of H, then the consequences are unspecified if O1, or any object O2 equivalent to O1 under the equivalence test (either before or after the modification), is used as a key in further operations on H. The consequences of using O1 as a key are unspecified even if O1 is 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!






          share|improve this answer


























          • 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














          4












          4








          4







          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 O1 is used as a key in a hash table H and is then visibly modified with regard to the equivalence test of H, then the consequences are unspecified if O1, or any object O2 equivalent to O1 under the equivalence test (either before or after the modification), is used as a key in further operations on H. The consequences of using O1 as a key are unspecified even if O1 is 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!






          share|improve this answer















          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 O1 is used as a key in a hash table H and is then visibly modified with regard to the equivalence test of H, then the consequences are unspecified if O1, or any object O2 equivalent to O1 under the equivalence test (either before or after the modification), is used as a key in further operations on H. The consequences of using O1 as a key are unspecified even if O1 is 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!







          share|improve this answer














          share|improve this answer



          share|improve this answer








          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 and EXT: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













          • See fast/stable hash keys and EXT: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


















          draft saved

          draft discarded




















































          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.




          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          How to pass form data using jquery Ajax to insert data in database?

          National Museum of Racing and Hall of Fame

          Guess what letter conforming each word