Next:
Overloading of Functions and Operators
Up:
Compiler Theory: Type Checking
Previous:
Type Equivalence
Type Conversions
Type Conversions can be
explicit (casts) if the programmer must write something to cause these conversions,
implicit (coercions) if done automatically by the compiler.
From the point of view of type checking,
explicit conversions are just like function applications,
implicit conversions requires the type checker
to detect that the two types are different and
to insert the appropriate cast.
Coercions
occur for instance with overloaded operators (say in C++) or in assignment statements and
for efficiency reasons should be done at compile time.
Example 1
Let us extend the set of expression constructs of the language from the previous section
We add a type
for floating point numbers with some fixed precision.
Elements of this new type must match the pattern
.
We also add binary infix arithmetic operators and use
in what follows to denote any of them.
These new operators are defined over the types
and
.
T
E
.
E
E
E
The translation scheme for these new rules are:
T
{
T
.
type
:=
}
E
{
E
.
type
:=
}
E
E
1
E
2
{
E
.
type
:=
if
E
1
.
type
=
and
E
2
.
type
=
then
else
if
E
1
.
type
=
and
E
2
.
type
=
then
else
if
E
1
.
type
=
and
E
2
.
type
=
then
else
if
E
1
.
type
=
and
E
2
.
type
=
then
else
}
Next:
Overloading of Functions and Operators
Up:
Compiler Theory: Type Checking
Previous:
Type Equivalence
Marc Moreno Maza
2004-12-02