Philosophical questions about collections

Richard A. O'Keefe ok at atlas.otago.ac.nz
Fri Jul 6 03:12:00 UTC 2001


	> Here's a case that seems similar to me: should 5 = 5.0 be false because
	> the numbers are of different classes?
	
	Good example!  Overall, "same as" seem  to be a vague concept, and we'll
	just have to pick for each kind of element what it means.  For numbers,
	allowing floats to equal integers is fine.
	
It's by no means that simple, and I suggest that trying to provide
a general definition of equality for Collections may be misguided.
(In effect, I'm praising Smalltalk for what it leaves OUT.)

Take the numbers.  5 and 5.0 are NOT equivalent in Smalltalk.
    [x := 5   * (2 raisedTo: 53). x + 1 = x] value is false
    [x := 5.0 * (2 raisedTo: 53). x + x = x] value is true 
If they behave differently, it doesn't make a lot of sense to regard them
as equal.

Does anyone else find it odd that
    (1 to: n) includes: x
can be true for a value x such that
    (1 to: n) anySatisfy: [:e|e = x]
is false?

Let's confine ourselves to messages that do not alter the receiver,
because I've always understood = to be about value of current state,
so that (1 to: 4) and #(1 2 3 4) are not distinguished by #at:put:.
They _are_ distinguished by the fact that
    (1 to: 4) isSorted		  => "MessageNotUnderstood: isSorted"
    #(1 2 3 4) isSorted		  => true
    (1 to: 4) rangeIncludes: 2.5  => true
    #(1 2 3 4) rangeIncludes: 2.5 => "MessageNotUnderstood: rangeIncludes:"

Since they are distinguished by these messages (which depend only on the
current value of the state) they are clearly not "equal" in any general
sense, at least as of Squeak 3.0 [updates to 3552].

Of course, it would make perfect sense to define #isSorted for Interval
and #rangeIncludes: for SequenceableCollection, so the fact that they
_happen_ to be distinguished by these two value messages is an accident.

[Aside: it turns out that "1 to: nil" is accepted!  What does it mean?]

Naturally, someone who happens not to be interested in sending
either of those selectors would say "I don't CARE", which is really my
point.  There isn't a completely general-purpose notion of equality in
Smalltalk; what we have is "equivalence for a particular task".  The
empirical question is whether there are definitions of equality for
various types of collection that are appropriate for *enough* tasks to
justify putting them in as the "standard" definitions.

One thing is clear:  the definitions of #= in Interval and
SequenceableCollection are, taken together, broken in 3.0[3552].
    (1 to: 4) = #(1 2 3 4)
is neither true nor false, it is a runtime error.  Apparently
Interval>> = thinks that
    self "an Interval" species == argument species
implies that the argument is also an interval, which it doesn't.





More information about the Squeak-dev mailing list