[r6rs-discuss] [Formal] Add symbol=? and boolean=?

From: Lauri Alanko <la>
Date: Sat Feb 10 04:37:03 2007

John Cowan wrote:

> Lauri Alanko scripsit:
>
>> It is customary for all (non-singleton) types to have their own
>> dedicated equivalence predicates. Symbols and booleans seem to be the
>> only exceptions.
>
> This turns out not to be the case.

Indeed. My wording was sloppy. I should have qualified this with "when a
meaningful computable equivalence (other than by-location) is
available". Procedures are an obvious exception, and hence also anything
that may contain arbitrary scheme objects. However,

>> Currently, symbol comparisons are often done with eq?, which seems
>> like bad style, since eq? is a low-level optimization primitive with
>> implementation-dependent semantics.
>
> eqv?, however, is entirely appropriate for the job, and probably is as
> efficient, since it would be very surprising if an implementation of
> eqv? did not immediately call eq?.

By the same token eqv? would also be entirely appropriate in the place
of char=?, and equal? would be appropriate in the place of string=?. The
only difference is that in the other cases, the specialized predicates
are likely to be more efficient than the general one, whereas symbol=?
and boolean=? are likely to be less efficient than eq?. IMHO, such
considerations should not direct the design of the language.


William Clinger wrote:

> The behavior of eq? on booleans and symbols is the same in all
> implementations. If boolean=? and symbol=? were slower than eq?, then
> many programmers would continue to use eq? as an optimization, just as
> many now use eq? instead of eqv? when comparing booleans or symbols.

True. Whenever (possibly dangerous) optimizations are available, some
people will use them even when they are not really required. But I think
a language should be foremost designed for those who are more concerned
about clarity and correctness.

And I think some people (at least myself) are currently using eq?
instead of eqv? because of clarity: the use of eq? tacitly indicates
that the arguments are not expected to be numbers or characters or at
least that the return value is not significant when they are. Still,
this condition is not machine-checked (which endangers correctness), and
the common case when the arguments are expected to be symbols isn't
distinguished in code.


I consider eq? to be a hack. Now, I have nothing against it as such:
hacks are useful, and sometimes necessary. It is nice that there's a way
for the programmer to tell the compiler: "Trust me, I know what I'm
doing. Give me this little bit of performance, and if things go subtly
wrong, I'll carry the blame." However, I think hacks should only be
extraneous _additions_ to the language, and the language's design should
make sense even without them, for those who don't wish to take risks.
Without eq?, the lack of symbol=? is much more obvious, especially since
it would then be the fastest way of comparing symbols.

(All this, of course, pales in the face of the fact that every
procedure call is a similar hack telling the compiler "trust me, I
know what I'm doing". But that battle, I concede, has already been
lost...)


Lauri
Received on Sat Feb 10 2007 - 04:36:56 UTC

This archive was generated by hypermail 2.3.0 : Wed Oct 23 2024 - 09:15:01 UTC