Professional Documents
Culture Documents
Type checking
the use of the information to ensure that each part of the program
makes sense under the type rules of the language.
Name equivalence:
• next and last have the same type
• p, q, r also have the same type
• But p and next do not.
Structural equivalence:
• all five variables have the same type because link is a name for
the type expression pointer(cell).
Name for Type Expression (cont.)
1. Confusion arises when an implicit type name is created for each
declared identifier.
type link = ↑ cell;
type link = ↑ cell;
np = ↑ cell;
var next: link;
nqr = ↑ cell;
last : link;
var next: link;
p : ↑ cell;
last : link;
q, r : ↑ cell;
p : np;
q : nqr;
Name equivalence: r : nqr;
• next and last have the same type
• q, r also have the same type
• But p, q and next do not have
equivalent types.
Type Graph
1. The typical implementation is to construct a type graph to
represent types.
2. Every time a type constructor or basic type is seen, a node is
created.
3. Every time a new type name is seen, a leaf is created.
4. With this representation, two type expressions are equivalent if
they are represented by the same node in the type graph.
next last p q r
cell
Cycles in Representation of Types
1. Recursive data types include lists, trees, and other structures.
2. Languages may or may not permit the direct use of recursion in
type declarations.
3. C allows recursion only indirectly, through pointers.
× ×
cell
Cycles in Representation of Types (cont.)
type link = ↑ cell;
cell = record
info : integer
× ×
4. Example;
– function “*” ( i , j : integer ) return complex
– function “*” ( i , j : complex) return complex
– Thus possible types for * include
• integer × integer → integer
• integer × integer → complex
• complex × complex → complex
E’ → E E’.types = E.types
E → id E.types =lookup(id.entry)
E: { i, c}
E: {i} E: {i}
3: {i} * 5: {i}
{i×i →i, i×i →c, c×c →c }
Narrowing the Set of Possible Types
1. Ada requires a complete expression to have a unique type.
3. Given a type from the context, we can narrow down the type choices for
each sub-expression.
5. If this process doest not result in a unique type for each sub-expression,
then a type error is declared for the expression.