You are on page 1of 19

Lecture 4: Types & Type Systems

Marriette Katarahweire

CSC 3112: Principles of Programming Languages 1/19


Outline

Concept of a data type


Role of types in programming
Type Checking
Strongly Typed Languages
Type Equivalence (by name vs structural)

CSC 3112: Principles of Programming Languages 2/19


Introduction

A data type defines a collection of data values and a set of


predefined operations on those values
Computer programs produce results by manipulating data. It
is crucial that a language supports an appropriate collection of
data types and structures
An important factor in determining the ease with which
programs can perform this task is how well the data types
available in the language being used match the objects in the
real-world of the problem being addressed.

CSC 3112: Principles of Programming Languages 3/19


Uses of the type system of a PL

error detection
provides assistance for program modularization: cross-module
type checking that ensures the consistency of the interfaces
among modules
documentation: type declarations in a program document
information about its data, which provides clues about the
program’s behavior

CSC 3112: Principles of Programming Languages 4/19


Type Checking
the activity of ensuring that the operands of an operator are
of compatible types
a compatible type is one that either is legal for the operator
or is allowed under language rules to be implicitly converted
by compiler-generated code (or the interpreter) to a legal
type. This automatic conversion is called a coercion
example: if an int variable and a float variable are added in
Java, the value of the int variable is coerced to float and a
floating-point addition is done
type error: the application of an operator to an operand of
an inappropriate type
example, in original C, if an int value was passed to a function
that expected a float value, a type error would occur
If all bindings of variables to types are static in a language,
then type checking can always be done statically
Dynamic type binding requires type checking at run time =
dynamic type checking
CSC 3112: Principles of Programming Languages 5/19
Strong Typing

A PL is strongly typed if type errors are always detected


requires that the types of all operands can be determined at
compile time or at run time
Advantages:
ability to detect all misuses of variables that result in type
errors
allows the detection, at run time, of uses of the incorrect type
values in variables that can store values of more than one type

CSC 3112: Principles of Programming Languages 6/19


Type Systems

type system of a programming language defines how a type is


associated with each expression in the language
includes its rules for type equivalence and type compatibility

CSC 3112: Principles of Programming Languages 7/19


Type Equivalence

two types are equivalent if an operand of one type in an


expression is substituted for one of the other type, without
coercion
for structured types i.e arrays, records, user-defined types
result of two variables being of equivalent types is that either
one can have its value assigned to the other
two approaches i.e
name type equivalence
structure type equivalence

CSC 3112: Principles of Programming Languages 8/19


Type Equivalence

Name type equivalence: two variables have equivalent types


if they are defined either in the same declaration or in
declarations that use the same type name
Structure type equivalence: two variables have equivalent
types if their types have identical structures. They must have
the same components e.g. Fortran
Ada Example:
type Indextype is 1..100;
count : Integer;
index : Indextype;

types of the variables count and index are not equivalent


count can not be assigned to index or vice versa

CSC 3112: Principles of Programming Languages 9/19


Structure Type Equivalence

Are these types equivalent? Does the format of the


declaration matter?
typedef struct { int a, b; } foo1;

typedef struct {
int a, b;
} foo2;

CSC 3112: Principles of Programming Languages 10/19


Structure Type Equivalence

Is the order of declaration important?


typedef struct {
int a;
int b;
} foo1;

typedef struct {
int b;
int a;
} foo2;

CSC 3112: Principles of Programming Languages 11/19


Type Equivalence

typedef struct {
char *name; char *address; int age;
} student;

typedef struct {
char *name; char *address; int age;
} school;

Are they structurally equivalent?


Are they nominally equivalent?

CSC 3112: Principles of Programming Languages 12/19


Primitive Data Types

data types that are not defined in terms of other types


nearly all programming languages provide a set of primitive
data types
some of the primitive types are merely reflections of the
hardware—for example, most integer types. Others require
only a little nonhardware support for their implementation
To provide the structured types, the primitive data types of a
language are used, along with one or more type constructors
include numeric types (i.e integers, floats, complex types,
decimals), boolean types, and character types

CSC 3112: Principles of Programming Languages 13/19


Structured Data types

structured (nonscalar) data types in the imperative languages


are arrays, records, associative arrays, lists
structured data types are defined with type operators, or
constructors, which are used to form type expressions
C uses brackets and asterisks as type operators to specify
arrays and pointers

CSC 3112: Principles of Programming Languages 14/19


User-defined Types

allow a programmer to design a data structure as the need


arises
improved readability through the use of meaningful names for
types
allow type checking of the variables of a special category of
use, which would otherwise not be possible
aid modifiability: A programmer can change the type of a
category of variables in a program by changing a type
definition statement only

CSC 3112: Principles of Programming Languages 15/19


Array Types

Array: homogeneous aggregate of data elements in which an


individual element is identified by its position in the
aggregate, relative to the first elements
individual data elements are of the same type
subscript expressions specify references to individual array
elements
additional runtime calculation is needed to determine the
address of the memory location being referenced if the
subscript expressions in a reference include variables
many PLs require all elements of an array to be of the same
type namely Java, C, C++, C#, Ada

CSC 3112: Principles of Programming Languages 16/19


Array Design Issues

What types are legal for subscripts?


Are subscripting expressions in element references range
checked?
When are subscript ranges bound?
When does array allocation take place?
Are ragged or rectangular multidimensioned arrays allowed, or
both?
Can arrays be initialized when they have their storage
allocated?
What kinds of slices are allowed, if any?

CSC 3112: Principles of Programming Languages 17/19


Script Bindings and Array Categories

Binding of the subscript type to an array is usually static


though the subscript value ranges are sometimes dynamically
bound
in some languages, the lower bound of the subscript range is
implicit
in C, the lower bound of subscript ranges is fixed at 0
in Fortran 95+, the lower bound defaults to 1 but can be set
to any integer literal
in some languages, the lower bounds of the subscript ranges
must be specified by the programmer.

CSC 3112: Principles of Programming Languages 18/19


Array Representation

Read about Implementation of Array types i.e Section 6.5.9.


Differentiate between Row major order and column major order

CSC 3112: Principles of Programming Languages 19/19

You might also like