Data Types Handout1
Data Types Handout1
09, 2014
1 Scalar Types
Built-in Types
User-Defined Ordinal Types
2 Composite Types
Array Types
String Types
Record Types
Union Types
Set Types
Pointer and Reference Types
Recursive Type
3 Type Checking
A data type is
a homogeneous collection of values and
a set of operations which manipulate these values
Uses of type system:
Conceptual organization
Error detection
Implementation
S Exponent Fraction
Single precision
11 bits 52 bits
S Exponent Fraction
Double precision
Simplest of all
Range of values: two elements, one for “true” and
one for “false”
Could be implemented as bits, but often as bytes
Readability
no need to code a color as a number
Reliability
operations (don’t allow colors to be added)
No enumeration variable can be assigned a value
outside its defined range
Better support for enumeration than C++:
enumeration type variables are not coerced into
integer types
Implemented as integers
Static
static int x[10];
Fixed Stack-dynamic
int x[10]; //inside a function
Stack-dynamic
cin »n;
int x[n];
Fixed Heap-dynamic
int[] x = new int[10];
Heap-dynamic
cin »n;
int[] x = new int[n];
rectangular jagged
a[0,0] a[0,0]
a[0,1] a[1,0]
a[0,2] a[2,0]
a[1,0] a[0,1]
a[1,1] a[1,1]
a[1,2] a[2,1]
Row-major order:
Location (a[i,j]) = α + (((i - row_lb) * n) + (j - col_lb)) * E
where α is address of a[row_lb,col_lb] and E is element
size
1 2 . . . j-1 j . . . n
1
2
..
.
i-1
N
i
..
.
m
Multidimensional array
Element type
Array Index type
Element type Number of dimensions
Index range 1
Index type
..
Index lower bound .
Index upper bound Index range n
Address Address
Single dimensional array Multi-dimensional array
A record:
heterogeneous aggregate of data elements
individual elements are identified by names
Popular in most languages, OO languages use
objects as records
Design issues:
What is the syntactic form of references to the field?
Are elliptical references allowed
Skip Record Type
Notation:
Dot-notation: Emp_Rec.Emp_Name.Mid
Keyword-based:
Mid OF Emp_Name OF Emp_Rec
Format:
Fully qualified references: include all record names
Elliptical references: may leave out some record
names as long as reference is unambiguous
Mid, Mid OF Emp_Name, Mid OF Emp_Rec
Record
Name
Type Field 1
Offset
.. ..
. .
Name
Type Field n
Offset
Address
b-byte aligned
A b-byte aligned object has an address that is a multiple
of b bytes.
Example
1 A char (one byte) will be 1-byte aligned.
2 A short (two bytes) will be 2-byte aligned.
3 A int (four bytes) will be 4-byte aligned.
4 A long (four bytes) will be 4-byte aligned.
5 A float (four bytes) will be 4-byte aligned.
Padding
when a structure member is
followed by a member with a larger alignment
requirement, or
at the end of the structure to make the structure size
be multiple of the biggest member size.
struct MyStruct {
char data1;
int data2;
char data3;
short data4;
char data5;
};
What is the size of the above struct?
Dr. Nguyen Hua Phung Data Types 39 / 78
Union Types
circle: diameter
x : set of 1 . . 1 0 ;
y : set of char ;
int *ptr;
7080
An anonymous
206 dynamic variable
ptr 7080
j 206
int ∗ ptr ;
i n t count , i n i t ;
...
ptr = &i n i t ;
count = ∗ p t r ;
int A;
i n t &rA = A ;
A = 1;
cout << rA << endl ;
rA ++;
cout << A << endl
type c h a r _ t r e e = { char v a l ;
char_tree l e f t ;
char_tree r i g h t ; }
{ A , { B , { C, n u l l , n u l l } , { D , { E , n u l l , n u l l } , n u l l } } ,
{F, null , n u l l } }
type c h a r _ b t r e e =
Tree of char ∗ c h a r _ b t r e e ∗ c h a r _ b t r e e
| Null
type ’ a b t r e e = Tree of ’ a ∗ ’ a b t r e e ∗ ’ a b t r e e
| Null
x : array [ 1 . . 1 0 ] of record
a : array [ 5 . . 1 0 ] of i n t e g e r ;
b : record
c : real ;
d : array [ 1 . . 3 ] of r e a l ;
end ;
d: string [3];
end ;
int ⇒ int
typedef int siso; ⇒ siso
int t[10]; ⇒ array(0..9,int)
int foo(int a,float b) ⇒ (int × float) → int
struct int a;int b ⇒ record((a × int) × (b × int))
int *p ⇒ pointer(int)
template <class T> struct vd T a; T b[3];
⇒ record((a × T) × (b × array(0..2,T)))
Definition
Type checking is the activity of ensuring that a program
respects the rules imposed by the type system
Definition
Type inference is the ability of a compiler to deduce type
information of program unit.
Example on Scala
def add(x:Int) = x + 1
Return type of function add is inferred to be Int
Mechanism
Assign type (built-in or variable type)to leaf nodes in
AST.
Generate type constraints in each internal node in
AST.
Resolve these type constraints
Definition
Type T is compatible with type S if a value of type T is
permitted in any context where a value of type S is
admissible
Example, int and float
A type T is compatible with type S when:
T is equivalence to S
Values of T form a subset of values of S
All operations on S are permitted on T
Values of T correspond in a canonical fashion to
values of S. (int and float)
Values of T can transform to some values of S.
Definition
Type conversion is conversing a value of this type to a
value of another type
Definition
Monomorphic: any language object has a unique type
Polymorphic: the same object can have more than
one type
class Polygon
public :
v i r t u a l f l o a t getArea ( ) = 0 ;
class Rectangle : public Polygon
public :
f l o a t getArea ( )
return height ∗ width ;
private :
f l o a t height , width ;
class T r i a n g l e : public Polygon
public :
f l o a t getArea ( )
float p = (a + b + c ) / 2;
r e t u r n s q r t ( p ∗ ( p−a ) ∗ ( p−b ) ∗ ( p−c ) ) ;
private :
float a,b, c ;
Shape ∗ s ;
s = ( . . . ) ? new Rectangle ( 3 , 4 ) : new T r i a n g l e ( 3 , 4 , 5 ) ;
s−>getArea ( ) ;
Dr. Nguyen Hua Phung Data Types 74 / 78
Example on Scala
a b s t r a c t class Stack [ A ]
def push ( x : A ) : Stack [ A ] =
new NonEmptyStack [ A ] ( x , t h i s )
def isEmpty : Boolean
def t o p : A
def pop : Stack [ A ]
class EmptyStack [ A ] extends Stack [ A ]
def isEmpty = t r u e
def t o p = e r r o r ( " EmptyStack . t o p " )
def pop = e r r o r ( " EmptyStack . pop " )
class NonEmptyStack [ A ] ( elem : A , r e s t : Stack [ A ] )
extends Stack [ A ]
def isEmpty = f a l s e
def t o p = elem
def pop = r e s t
v a l x = new EmptyStack [ I n t ]
v a l y = x . push ( 1 ) . push ( 2 )
p r i n t l n ( y . pop . t o p )