Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search

[REBOL] Re: On mutability and sameness

From: joel:neely:fedex at: 17-Jun-2001 8:06

Hi, Ken, Ken Anthony wrote:
> > > > EQUAL? tests whether *content* is the same, regardless of > > the data type; in the case of a reference type (your > > "indirect addressed" case) that may require following the > > reference to the actual content. > > I think we are saying the same thing here. The reason I refer > to bits is that regardless of type at some point a test of > equality has to test bits of a value that have been converted > to the same format/type (whatever is done internally.) >
I understand. FWIW the reason I wanted to rephrase it was to emphasize the issue of *which* bits were being compared in each case. Unfortunately the word "value" is sometimes ambiguous in the stuff I've read.
> > Only if there's an "address" involved... see below. > > Forgive my blockheadedness but address is always involved > somehow, but I accept your explanation about same? giving the > same result as equal? for non-referenced types. >
For non-reference types there doesn't have to be "an address". The result of evaluating 1 + 3 is identical to the result of evaluating 6 - 2 in both Mathematics sense and in REBOL.
>> same? 1 + 3 6 - 2 == true
That's true whether the (temporary) results for those expressions are held in scratch memory locations (which have addresses), in a stack (which may or may not have addresses, depending on CPU) or in registers (which usually don't have addresses).
> a: 2:30 > b: a > c: 1:30 + 1:00 > > which point... same? a c ;=True > ...varies with the... a==c: no >
In some other languages (which shall remain nameless ;-) one thinks of a variable as a label for some "cell" in memory. The value of a variable is what is stored in that cell. (A variable is a "container", and the value is its "content".) For some data types the content of the cell is not the final data value, but a pointer to it. If two variables contain "same" pointers, then the pointed-to data are guaranteed to be equal. If two variables contain different pointers, then we don't know whether the data are equal or not without following the pointers and examining the data at those destinations. Therefore we have three possible comparison scenarios: Kind of datatype Comparison to be performed ---------------- -------------------------- 1) Simple data Compare the data values 2) Reference data Compare the "pointer" values 3) Reference data Compare the "final data" values Low-level languages assume that you're interested in bits and cells and pointers and such, so they test equality of variables by seeing if what's in the cells are identical, regardless of the variables' datatype(s). In other words, they group (1) and (2) together in an equality test (such as the == in c), and require a separate test for (3) (such as strcmp in c). You, the programmer, have to keep up with the data types and figure out which kind of comparison you need to make. REBOL is a high-level language, and makes the assumption that the programmer is more frequently interested in data values than in the details of implementation. Therefore, REBOL groups (1) and (3) together in the concept of EQUAL? (regardless of data type), and leaves (2) as a separate SAME? test. Of course, that begs the question "What do we do if SAME? is applied to simple data, since it is really there to compare data structure references?" I discussed a list of possible answers and reasons in another post, so I won't repeat all of that here, but the short answer is this: since simple data values don't have references, SAME? just compares the data values themselves. That's why we got
>> same? 1 + 3 6 - 2 == true
in the example above.
> there a referenced? vs. non-referenced? type function? >
Not AFAIK. The following is a quick cut at a function that tells whether the current value of a word is of a reference type. sharable?: func ['w [word!]] [ found? any [ series? get w any-function? get w object? get w ] ] It may be used as follows:
>> a: "123" == "123" >> sharable? a == true >> a: 123 == 123 >> sharable? a == false >> a: 12:30 == 12:30 >> sharable? a == false >> a: [1 2 3] == [1 2 3] >> sharable? a == true
No warranties expressed or implied! I believe it to be true for the "ordinary data" cases, but it may require tweaking for the more esoteric REBOL-specific types. -jn- ------------------------------------------------------------ Programming languages: compact, powerful, simple ... Pick any two! joel'dot'neely'at'fedex'dot'com