Concepts of Programming Languages

(Sean Pound) #1
6.14 Type Equivalence 305

some user-defined types, the rules are more complex. Coercion of these types
is rare, so the issue is not type compatibility, but type equivalence. That is, two
types are equivalent if an operand of one type in an expression is substituted
for one of the other type, without coercion. Type equivalence is a strict form
of type compatibility—compatibility without coercion. The central issue here
is how type equivalence is defined.
The design of the type equivalence rules of a language is important,
because it influences the design of the data types and the operations provided
for values of those types. With the types discussed here, there are very few pre-
defined operations. Perhaps the most important result of two variables being
of equivalent types is that either one can have its value assigned to the other.
There are two approaches to defining type equivalence: name type equiva-
lence and structure type equivalence. Name type equivalence means that two
variables have equivalent types if they are defined either in the same declaration
or in declarations that use the same type name. Structure type equivalence
means that two variables have equivalent types if their types have identical
structures. There are some variations of these two approaches, and many lan-
guages use combinations of them.
Name type equivalence is easy to implement but is more restrictive. Under
a strict interpretation, a variable whose type is a subrange of the integers would
not be equivalent to an integer type variable. For example, supposing Ada used
strict name type equivalence, consider the following Ada code:


type Indextype is 1..100;
count : Integer;
index : Indextype;


The types of the variables count and index would not be equivalent; count
could not be assigned to index or vice versa.
Another problem with name type equivalence arises when a structured or
user-defined type is passed among subprograms through parameters. Such a
type must be defined only once, globally. A subprogram cannot state the type
of such formal parameters in local terms. This was the case with the original
version of Pascal.
Note that to use name type equivalence, all types must have names. Most
languages allow users to define types that are anonymous—they do not have
names. For a language to use name type equivalence, such types must implicitly
be given internal names by the compiler.
Structure type equivalence is more flexible than name type equivalence, but
it is more difficult to implement. Under name type equivalence, only the two
type names must be compared to determine equivalence. Under structure type
equivalence, however, the entire structures of the two types must be compared.
This comparison is not always simple. (Consider a data structure that refers to
its own type, such as a linked list.) Other questions can also arise. For example,
are two record (or struct) types equivalent if they have the same structure but
different field names? Are two single-dimensioned array types in a Fortran or

Free download pdf