0% found this document useful (0 votes)
48 views21 pages

Type Checking

The document discusses type checking in programming languages. It describes what types are, how type checking verifies that expressions match expected types, and provides examples of type systems and type expressions. It also discusses type equivalence, performing type checking on an example program, and type inference.

Uploaded by

shvdo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views21 pages

Type Checking

The document discusses type checking in programming languages. It describes what types are, how type checking verifies that expressions match expected types, and provides examples of type systems and type expressions. It also discusses type equivalence, performing type checking on an example program, and type inference.

Uploaded by

shvdo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Type Checking

What is a Type?
• Short for “data type”
• Classification identifying kinds of data
• A set of possible values which a variable can
possess
• Operations that can be done on member values
• A representation (perhaps in memory)
Type Intuition

You can’t do this:


int a = 0;
int * pointer = &a;
float fraction = 1.2;
a = pointer + fraction;
Type Checking
• Type checking is a part of context-sensitive analysis
• A type-checker verifies that the type of a construct matches the type that is
expected by its context
• Examples
– dereference operation is only applied to pointers
– indexing is only done for an array
– in a method call, number of arguments and their types match the declaration
– arguments of modulo operation are both integers
• Type checking can be
– static: done at compile time
– dynamic: done at execution time
• A language in which every expression can be assigned an unambiguous type is
called a strongly-typed language
Type Systems
A type system consists of a set of base types and a set of type-constructors
– array, function, pointer, product
• Base types
– Programming languages typically include base types for: numbers (int, float), characters,
Booleans
• type constructors
– Programmers need higher-level abstractions than the base types, such as lists, graphs,
trees, tables, etc.
– Programming languages provide mechanisms to combine and aggregate objects and to
derive types for the resulting objects
– arrays, structures, class, enumerated sets, function, prodect, pointers
• Using base types and type-constructors each expression in a program can be
represented with a type expression
Example Type Expressions
Type Systems
• A type system is a collection of rules for assigning type-expressions to
language constructs
• A type-expression is either a base type or is formed by applying a type
constructor to a type-expression
• Inference rules for type-expressions:
– (Pascal) If both operands of the arithmetic operators addition,
subtraction and multiplication are of type integer than the result is of
type integer
– (C, C++) The results of the unary & operator is a pointer to the object
referred to by the operand. If the operand is of type “foo”, then the type
of the result is a “pointer to foo”
• A sound type system eliminates the need for dynamic checking because it
statically determines whether type errors will occur or not
Type Equivalence

• Structural equivalence
– Two types are equivalent if they have the same
structure
– Two type-expressions are structurally equivalent
if either they are the same basic type or they are
formed by applying the same type constructor to
structurally equivalent types
• Name equivalence
– Each type name is viewed as a distinct type
Checking Structural Equivalence
Type-Checking Example
Type-Checking Example
• We will use the following type constructors
– array(I,T) : creates a type expression for an array of type T with index set I
– pointer(T) : creates a type expression of type pointer to type T
– function(T,T) : creates a type expression of type function from type T to type T
• We will also use the following:
– id.entry : this attribute gives the location of the corresponding identifier in the symbol
table
– addtype(id.entry, type) : enters the type information to the symbol table
– lookup(id.entry) : returns the type information stored in the symbol table
• If we detect an error, we will set the type of the corresponding program segment
to type-error
Type Checking: How should it work?
• Let’s examine a program that is part of the language defined above
x: char; //this declares a new variable “ x ” of type char
y: pointer char; //this declares “ y ” to be of type pointer(char)
x := ‘ c ’ ; //this assignment is a literal (which should be a char) to a
//char
x := * y; //this de-references y (of type pointer(char)) to get
//something of type char, and then assigns it to x
(which is of //type char)
• Let us now draw the parse tree for this program
• We will walk the parse tree (depth first walk – walk down the left-
most un-touched branch and then back up)
• Show how the type-checking should progress
Type-Checking Example: Parse Tree
Type Checking Example: Simple Example
Type Checking Example: Declarations
Type Checking Example: Expressions
Type Checking Example: Expressions
Type Checking: Statements
Type Inference

• Inference rules that specify mapping between operand types


and result type
• X = Y; X should have a type that is compatible with Y
– In Pascal: an integer is compatible with the sub-range
1...100
– In C: a float + a double results in a double
– A float can be assigned to a double, but a double cannot be
assigned to a float (the relationship is asymmetric)
• These rules need to be handled by the compiler
– How and when they are enforced is important
Types of Typed Languages
• A language in which every expression can be assigned an
unambiguous type is called a strongly typed language
– If every expression can be typed at compile time, the language
is called statically typed
– If some expressions can only be typed at compile time, the
language is dynamically typed
• Untyped languages have no notion of type at all
– Everything is just bit patterns, floating point + has to be
distinguished from integer +! (in BCPL #+ is floating point
addition)
• In weakly typed languages, there is a notion of type, but it is not
strictly enforced for all expressions

You might also like