Types and Programming Languages: Simon Gay Department of Computing Science University of Glasgow
Types and Programming Languages: Simon Gay Department of Computing Science University of Glasgow
Lecture 1
Simon Gay
Department of Computing Science
University of Glasgow
2006/07
Introduction
Type definitions and declarations are essential aspects of
high-level programming languages.
Example (Java):
class Example {
int a;
void set(int x) {a=x;}
int get() {return a;}
}
Example e = new Example();
2006
e.set(Hello);
^------------^
*** Semantic Error: No applicable overload for
the method named set was found in type
Example. Perhaps you wanted the overloaded
version void set(int x); instead?
8.
e.show();
^------^
*** Semantic Error: No method named show was
found in type Example.
2006
9.
i = e + 1;
^
*** Semantic Error: The type of this
expression, Example, is not numeric.
2006
Type declarations
Organize data into high-level structures
essential for high-level programming
Document the program
basic information about the meaning of variables and
functions
Inform the compiler
example: how much storage each value needs
Specify simple aspects of the behaviour of functions
types as specifications is an important idea
2006
Typechecking
Static (compile-time) or dynamic (run-time)
static is better: finds errors sooner, doesnt degrade
performance
(we will concentrate on static typechecking)
Verifies that the programmers intentions (expressed by
declarations) are observed by the program
A program which typechecks is guaranteed to behave well
at run-time
at least: never apply an operation to the wrong type of value
more: eg. security properties
A program which typechecks respects the high-level
abstractions
eg: public/protected/private access in Java
2006
2006
2006
10
2006
11
12
Answer to exercise
if (1 == 2) {
int x = Hello * 5;
}
The Java typechecker assumes that every branch of a
conditional statement may be executed (even if the condition is
a compile-time constant or even a boolean literal).
In general it is impossible to predict the value of an arbitrary
expression at compile-time.
2006
13
Principles
Programming is difficult and we need all the automated help we
can get!
Static typechecking is one approach to program analysis.
It has been enormously beneficial.
Exact program analysis is impossible in general. Typechecking
aims for limited guarantees of correctness, and inevitably
rejects some correct programs.
A type system restricts programming style, sometimes to an
undesirable extent.
The challenge in type system design: allow flexibility in
programming, but not so much flexibility that incorrect programs
can be expressed.
2006
14
15
16
2006
17
18
19
20
Administrative details
Two lectures + one tutorial per week (Mon 11, Wed 9, Fri 10).
Copies of presentations will be handed out.
Some additional notes will be produced.
There will be an assessed exercise (worth 20%) and an exam.
A sample exam paper will be produced.
I can be contacted by email ([email protected]), or in my
office (G093) within reason.
Books: Types and Programming Languages, B. C. Pierce
compiler books
Type Systems, L. Cardelli (material for reading course)
Course web page:
2006
www.dcs.gla.ac.uk/~simon/teaching/tpl
21
22