[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Chicken-users] Hash table equality pitfall

From: John Cowan
Subject: Re: [Chicken-users] Hash table equality pitfall
Date: Thu, 1 Mar 2012 12:03:25 -0500
User-agent: Mutt/1.5.18 (2008-05-17)

Peter Bex scripsit:

> Of course, from the end-user perspective this is *very* tricky
> behaviour, so I've added a note to the manual about this.  I checked
> the SRFI, but it doesn't mention anything about hash tables having to
> compare equal to eachother so Chicken's behaviour is strictly not in
> violation of any standard :)

The R5RS definition of `equal?` says it is recursive on pairs, vectors,
and strings, going on to say that `equal?` defers to `eqv?` "on
other objects such as numbers and symbols".  It is not clear whether
this means all other objects (in which case `equal?` on hashtables
must be the same as `eqv?`), or only some other objects, including
numbers and symbols but allowing implementation-defined behavior on
implementation-defined objects.  Chicken evidently assumes the latter

In R6RS, `equal?` definitely defers to `eqv?` for all objects except
pairs, vectors, strings, and bytevectors.  R7RS currently uses the same
language as R5RS, with the same ambiguity, but requires descent into
bytevectors and records.

> The only other implementation I was able to test with was Racket (why
> is SRFI-69 so uncommon in Schemes?) and there hash tables also don't
> compare with equal?

I maintain a table of what Schemes implement which SRFIs at (Spreadsheet of Scheme Systems Supporting
SRFIs).  There is a portable R6RS implementation of SRFI-69 on top of
R6RS hashtables.  These are naturally not `equal?` in accordance with
the R6RS rule given above.

In addition, the following Schemes support SRFI-69 with `equal?`
descending into hash tables:  Kawa, Chibi.  The following Schemes also
support it, with `equal?` the same as `eqv?`:  Guile, STklos.

I think the main reason SRFI 69 doesn't have much support is its fairly
recent date (2005).  It would probably be straightforward to layer it
over the native support in most Schemes, but people haven't bothered to
do so.  In addition, the reference implementation is more of a proof of
concept than a drop-in implementation like those provided with SRFI-1,
SRFI-2, SRFI-43, etc.

> Luckily, with the new version the mistake of thinking that equal?
> is defined for hash-tables is a lot harder to make since the
> randomization factor will ensure they are almost always different.

I think that's good.  The behavior of `equal?` is primarily historical
anyway; people can and should write their own equivalence predicates to
solve their own problems.

John Cowan  address@hidden
The Penguin shall hunt and devour all that is crufty, gnarly and
bogacious; all code which wriggles like spaghetti, or is infested with
blighting creatures, or is bound by grave and perilous Licences shall it
capture.  And in capturing shall it replicate, and in replicating shall
it document, and in documentation shall it bring freedom, serenity and
most cool froodiness to the earth and all who code therein.  --Gospel of Tux

reply via email to

[Prev in Thread] Current Thread [Next in Thread]