comparing vectors

42 views
Skip to first unread message

Peter Čerman

unread,
Sep 6, 2022, 3:31:19 AM9/6/22
to chez-scheme
I am testing chez scheme version 9.5.8 under Windows 10.

The next expression evaluates to #f:
 (equal? (let ((v '#(1 2 3))) (vector-set! v 1 100) v) '#(1 100 3))

On the other hand, more or less the same expression evaluates to the value #t:
 (equal? (let ((v '#(1 2 3))) (vector-set! v 1 100) v) (vector 1 100 3))

I don't understand why this is because the following expression evaluates to #t:
 (equal? '#(1 2 3) (vector 1 2 3))

Is this a bug or am I misunderstanding something?

Thanks for any answer.

Jamie Taylor

unread,
Sep 6, 2022, 11:47:57 AM9/6/22
to Peter Čerman, chez-scheme
On Sep 6, 2022, at 3:31 AM, Peter Čerman <cerman...@gmail.com> wrote:
The next expression evaluates to #f:
 (equal? (let ((v '#(1 2 3))) (vector-set! v 1 100) v) '#(1 100 3))

Mutating a quoted constant is undefined behavior.  See https://scheme.com/tspl4/objects.html#./objects:s2

Quoted and self-evaluating constants are immutable. That is, programs should not alter a constant via set-car!string-set!, etc., and implementations are permitted to raise an exception with condition type &assertion if such an alteration is attempted. If an attempt to alter an immutable object is undetected, the behavior of the program is unspecified. An implementation may choose to share storage among different constants to save space.

The optimizer assumes it's a constant, and in this case determines that it is comparing two constant vectors and pre-computes the result:

> (expand/optimize '(equal? (let ((v '#(1 2 3))) (vector-set! v 1 100) v) '#(1 100 3)))
(begin (#2%vector-set! '#(1 2 3) 1 100) #f)

The documentation is a bit confusing, though, since there is this statement in https://cisco.github.io/ChezScheme/csug9.5/objects.html#./objects:s34

All vectors are mutable by default, including constants. A program can create immutable vectors via vector->immutable-vector. Any attempt to modify an immutable vector causes an exception to be raised.


This is talking about immutable vectors, which is a distinct concept (introduced much more recently), and not related to the immutability described in the documentation for quote.  It would be more accurate to say that while constant vectors can be mutated they should not be, and that it may have unexpected results.  I'd say it's a bug in the documentation that these two concepts are not more clearly distinguished.

An alternative approach would be to make quoted vector constants immutable (as if with vector->immutable-vector), but that is a backwards incompatible change (and we tend to avoid those).

Hope this clears things up.
Jamie

Peter Čerman

unread,
Sep 7, 2022, 5:22:24 AM9/7/22
to chez-scheme
Thank you for your response. Now it's a little more understandable for me. I will have to be careful when I use quoted vectors (...).

Dátum: utorok 6. septembra 2022, čas: 17:47:57 UTC+2, odosielateľ: jamie....@pobox.com
Reply all
Reply to author
Forward
0 new messages