Search code examples
common-lispsbcl

SBCL REPL Wrong Reuse of Cons Cells?


What's going on in the interaction with SBCL REPL below (the last form)?

TEST> (acons 'k 'v1 nil)
((K . V1))
TEST> (assoc 'k *)
(K . V1)
TEST> (push 'v2 (cdr *))
(V2 . V1)
TEST> (cdr '(K . V1))
(V2 . V1)

This is a bug, I guess?


Solution

  • One possible interaction problem might be caused by using presentations in SLIME.

    I'm using SLIME and SBCL.

    CL-USER> (acons 'k 'v1 nil)
    ((K . V1))
    CL-USER> (assoc 'k *)
    (K . V1)
    CL-USER> (push 'v2 (cdr *))
    (V2 . V1)
    CL-USER> (cdr '(K . V1))
    (V2 . V1)
    CL-USER> 
    

    So, I can get the same result as you. Strange, isn't it? But there might be an explanation for it. What did I do?

    First: SLIME can use presentations and not just textual data. A presentation is a textual representation of the actual data. The text then is still linked to the underlying data.

    I'm typing: (acons 'k 'v1 nil)

    CL-USER> (acons 'k 'v1 nil)
    

    SLIME presents the result list:

    ((K . V1))
    

    I'm typing: (assoc 'k *)

    CL-USER> (assoc 'k *)
    

    SLIME presents the result list:

    (K . V1)
    

    I'm typing: (push 'v2 (cdr *))

    CL-USER> (push 'v2 (cdr *))
    

    SLIME presents the result list:

    (V2 . V1)
    

    Now it gets interesting. I'm typing (cdr ' and then I'm copying the presentation (K . V1) from above by copy/paste. I'm not typing the list, I'm copying it. Then I type ).

    CL-USER> (cdr '(K . V1))
    

    The underlying data of the presentation has changed. SLIME tells SBCL to evaluate (cdr '(K V2 . V1)). SLIME uses the value of the presentation, not it's actually displayed characters.

    Thus the result is:

    (V2 . V1)
    

    Next, we can see the actual changed presentation's value: I'm writing a quote character first, then I again copy the (K . V1) presentation from above.

    CL-USER> '(K . V1)
    (K V2 . V1)
    

    The result above shows that what you see is not what you get.