Two types are equivalent if an operand of one type in an expression is substituted for one of the other type, without coercion. There are two approaches to defining 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.
(a) (4 marks) The Pascal language adopts name type equivalence. Consider the following declarations:
al: array [1..10] of integer;
a2: array [1..10] of integer;
According to name type equivalence, the variables a1 and a2 are considered to have distinct and non-equivalent types. In other words, values of a1 cannot be assigned to a2, and vice versa.
Suggest two ways of defining a1 and a2 so that they have the same type.
(b) (4 marks) On the other hand, structure type equivalence is more lenient. Consider the following declarations:
al: array [1..10] of integer;
a2: array [11..20] of integer;
The types of the variables a1 and a2 are considered to be equivalent, even though they have different names and different subscript ranges. It is because both types have 10 elements of the integer type.
Give two scenarios in which structure type equivalence is undesirable.