



















































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




















































-^
-^
Possibilities^ –
Int
if you can prove that
expression
always evaluates to
true
-^
0
as a very small type; likewise for
"foo"
-^
String
if you can prove that
expression
always evaluates
to
false
; or if you treat
0
as the null string pointer
-^
Int
∪
String
-^
Error
because the two arms have different types
-^
Object
as the least common ancestor
-^
Undefined
(⊥
) if
expression
is undefined
The type system for a programming language givesanswers to questions like this
A type system is a tractable syntactic method forproving the absence of certain program behaviors byclassifying phrases according to the kinds of valuesthey compute.
-- Pierce
Syntactic
implies statically analyzable
Proving
implies that a type system is a formal
semantic system
Tractable
implies efficient algorithms
Absence
implies conservative estimation
-^
-^
given names and properties
-^
-^
-^
-^
-^
-^
c 2007 by Spencer Rugaber
downcasting
), e.g. int to char
type punning
)
-^
-^
-^
type
T = record a: integer; b: char end; var
x,y: array [1..2] of record a: integer; b: char end;z:
array [1..2] of T;
u,v: array [1..2] of record a: integer; b: char end; type
T = record a: integer; b: char end;Anon1 = array [1..2] record a: integer; b: char end;Anon2 = array [1..2] of T; var
x,y: Anon1;z:
Anon2;
u,v: Anon1;
type T1 is record
X: INTEGER;N: access T1; end record;type T2 is record
X: INTEGER;N: access T2; end record;
type T3 is record
X: INTEGER;N: access record
X: INTEGER;N: access T3; end record; end record;
c 2007 by Spencer Rugaber
type T1 is record
X: INTEGER;N: access T1; end record;type T2 is record
N: access T2;X: INTEGER; end record;
Note that fieldordering is significantfor C
structs
in
terms of memoryalignment andportability
c 2007 by Spencer Rugaber
Conversion:
treatment of a value of a given type as if
it had a different type– May actually change the underlying representation
(e.g.
int
real
int
long
(void *
char *)
Casting:
explicit type conversion
Coercion:
implicit type conversion
c 2007 by Spencer Rugaber
type (T
) as a refinement of another (T 2
) 1
relation on types such that if T
2
< T
1
then all values of type T
2
are also legal
values of type T
1
1
2
c 2007 by Spencer Rugaber
-^
1
1
2
1
2
If types are sets of values, consider the set of legaltelephone area codes represented as integers
-^
This is also a set of values, but we would notnormally consider it a type
-^
We might still want to check whether a value was inthis set or not. In principle, we could do this atcompile time, but we are more likely to add a run-timecheck
-^
More generally, there exist subsets of values that canonly be checked at run-time and that we wouldnormally refrain from calling types