[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: (SYMBOL? #t) ==> ?
I believe there are two uses for type predicates: (1) to determine
if an object is of a given type, and (2) to determine if an object is
*not* of a given type.
(1) is useful for writing type-safe code, since it allows us to verify,
for example, that an object is a pair before calling "car". In this
case it doesn't matter whether the object happens also to be a string
or symbol, so under Andy's proposal the type predicates would still be
useful. ("doesn't matter" may be a little strong---since it would
matter if I want to be sure not to apply "car" to a string represented
as a list of characters. Therefore, "useful" may be a little strong,
at least if a predicate such as "pair?" returns true for all objects.
The point is we can still use the predicates to write "type-safe" code,
though maybe not "type-sensible" code).
(2) is useful when the type itself determines what operation we want to
perform, as with "write" and "display". It would not be friendly if a
system printed all objects as integers, as it might if they were all
considered integers by "integer?". (I already dislike that I can enter
#\a and receive 97 back.) One might argue that printing objects is for
the system to worry about, but what if I want to write a portable pretty
printer or editor? Or a generic "sequence" mapping procedure, defined
(lambda (proc seq)
[(list? seq) ... code for mapping over lists ...]
[(vector? seq) ... code for mapping over vectors ...]
[(string? seq) ... code for mapping over strings ...]
I'm likely to get burned if vectors are represented as tagged lists.
If a "minimal" implementation is to be built from pairs using the first
element of the list as a tag, then it should have a tag for pairs as
well as for symbols, vectors, strings, etc. In this way, it can
maintain the illusion of disjoint types, even though the underlying
structure (at one level) is the same. The "pair?", "cons", "car", and
"cdr" procedures provided to the programmer would have to take account
of the tag, of course. This is no different from a standard tagged
object typing system at the machine level.