Programming Languages (CSC-302 98S)
Outline of Class 12: Type Equivalence and Coercion
Held: Friday, February 13, 1998
- Don't forget Monday's brown-bag lunch. It's on multimedia on the
ethernet and looks to be interesting.
- assignment 3 is
- What are some questions we might ask ourselves as we design a type
- Some questions have to do with the "base types" in our system.
- Do we type objects in our language?
- If so, do we type at run time, compile time, both?
- What primitive types do we provide?
- What compound types do we provide?
- Some questions might have to do with user-definable types in our
- Can users define new types?
- Can they define new primitive types?
- Can they define new compound types?
- Can they define new type constructors?
- Some might have to do with the interpretation of types.
- Do we treat types as having fixed-size or arbitrary-size
- Can you think of any others?
- Of the many reasons we use types, one of the most important is
in ensuring that objects are only used in "appropriate" places.
- Arithmetic operations should only be applied to objects on which
you can perform arithmetic (and often only to objects of an
- Values should only be assigned to variables of an equivalent type.
- Hence, it is important to determine when two types are equivalent
(in the senses used above).
- Often, type equivalence is attacked from a practical perspective: what
equivalencies can we reasonably determine?
- To answer this question, we'll consider a few basic types and variable
declarations. It is often easiest to ask whether we can assign a
variable of one type to a variable of another type. Here are some
sample types and corresponding variables.
type intreal = record
intreal_alias = intreal;
ir = record
realint = record
irr = record
intrec = record
iarr = array[1..10] of integer;
iare = array[2..11] of integer;
rec5,rec6: record a: integer; b: real; end;
rec7: record a: integer; b: real; end;
arr3,arr4: array[1..10] of integer;
arr5: array[1..10] of integer;
- There are a number of strategies one might use to determine equivalence,
including structural equivalence, named structural equivalence, name
equivalence, and declaration equivalence.
- In structural equivalence, two types are equivalent if they
have the same structure.
rec3 are structurally equivalent as
they are both pairs of (integer,real).
rec8 are not structurally equivalent,
as they order their elements differently.
- In structural equivalence under naming, two types are equivalent if
they have the same structure and the named components of each structure
have the same names.
rec3 are not structurally
equivalent under naming as they name their pairs differently.
rec6 are structurally equivalent
- In name equivalence, two types are equivalent if they are
named and have the same name.
rec2 are not name equivalent.
rec10 are name equivalent.
- In declaration equivalence, two types are equivalent if they
lead back to the same type name. This accommodates variables.
- A related concept is assignment compatibility: can we safely
assign a variable of one type to another type? Often, we may need to
do some coercion to allow such assignments.
- Often, when performing operations on values we may need to coerce
a value from one type to another.
- When you add the integer 3 and the real number 4.5, we need to
coerce 3 to a real.
- When you print the real number 4.5, you need to coerce it to a string.
- How do we handle such issues?
- Disallow them and consider them violations of standard usage.
- Allow them, but require programmers to explicitly coerce from one
type to another.
- Allow them, and automatically determine an appropriate coercion.
- Are there reasons to make each choice? Certainly. We'll
- How do we convert, given the underlying representation?
- Interpret the bit sequence for one as if it were the bit sequence
for the other.
- Build an equivalent object.
- What kinds of coercions should we allow (explicitly or implicitly)?
- More general types to less general types? (E.g., real to int.)
- Less general types to more general types? (E.g., int to real.)
- What if neither type is more/less general than the other?
- Should we permit a loss of accuracy? If so, how do we determine
that loss of accuracy?
- If multiple coercions are possible, which should we choose? Consider
3 + "4".
- Should we notify the programmer if we coerce?