[plt-scheme] When does eqv? differ from eq? , from equal?

From: Carl Eastlund (cce at ccs.neu.edu)
Date: Mon Oct 6 20:40:16 EDT 2008

On Mon, Oct 6, 2008 at 6:53 PM, Alan Watson <alan at alan-watson.org> wrote:
> Assuming that "it takes time" means "is not a pointer comparison", then:
>> it takes time to equate 10000000000 with 10000000000
> Not with 61-bit fixnums. :-)
>> but not (list 1 2 3) with (list 1 2 3)
> Not if all exact integers are represented as bignums or otherwise boxed. :-)

By "it takes time", I meant "eqv? will take more than constant time,
if it has to".  That is, eqv? will take an arbitrary amount of time to
compare big enough bignums, but other structures -- even if immutable
-- it will not.

>> However, we can observe the differences in
>> space consumption between a list of bignums that are all eq? (not much
>> space used), versus a list of bignums that are all eqv? but not eq?
>> (potentially lots of space used).
> Whether two numerical values that are eqv? are also eq? depends on the
> implementation. If an implementation hashes boxed numerical values, they can
> easily be eq? even if they are the result of different expressions. And I am
> not making this up; MACLISP did precisely this.

Hashing breaks down the extensional/intensional distinction.  If your
implementation hashes its values, a lot of these issues become moot
and more cases of eq?, eqv?, and equal? become indistinguishable.

>> I only use eq? when I really, really mean it
> But what do you really, really mean? For any arguments for which eq? gives a
> reliable result (by which I mean, a result that does not depend on the
> implementation), eqv? will give you the same result.

What I meant was, I only use eq? when I really mean "pointer equality"
-- generally for comparing two mutable structures, though I also use
it for symbols.  In those cases I don't really care about bignum
equality, so eqv? is a non-issue.  When I want to compare the contents
of values, I use a typed comparison like = or string=? if I can,
otherwise I use equal?.

Carl Eastlund

Posted on the users mailing list.