Types, Polymorphism and Overloading: Department of Informatics - University of Oslo
Types, Polymorphism and Overloading: Department of Informatics - University of Oslo
Types, Polymorphism and Overloading: Department of Informatics - University of Oslo
1
Before starting... Some clarifications
2
ML lectures
1. 05.09: A quick introduction to ML
2. 12.09: The Algol Family and more on ML
(Mitchell’s Chapter 5 + more)
3
Outline
Types in programming
Type safety
Type inference
Type declaration
4
Type
A type is a collection of computational entities
sharing some common property
6
Type errors
Hardware error
• Function call x() (where x is not a function) may
cause jump to instruction that does not contain a
Unintended semantics
• int_add(3, 4.5): Not a hardware error, since bit
pattern of float 4.5 can be interpreted as an integer
7
General definition of type error
A type error occurs when execution of program
is not faithful to the intended semantics
Type errors depend on the concepts defined in
Type safety
Type inference
Type declaration
11
Type safety
A Prog. Lang. is type safe if no program can
violate its type distinction (e.g. functions and integer)
Examples of not type safe language features:
12
Relative type-safety of languages
Not safe: BCPL family, including C and C++
• Casts; pointer arithmetic
15
Outline
Types in programming
Type safety
Type inference
Type declaration
16
Polymorphism: three forms
Parametric polymorphism
• Single function may be given (infinitely) many types
• The type expression involves type variables
Subtype polymorphism
• The subtype relation allows an expression to have
many possible types
20
Parametric Polymorphism: ML vs. C++
Instantiations:
• int i,j; … swap(i,j); //use swap with T replaced with int
• float a,b;… swap(a,b); //use swap with T replaced with
float
• string s,t;… swap(s,t); //use swap with T replaced with
string
22
Example: swap two values
ML
- fun swap(x,y) =
23
Parametric Polymorphism: Implementation
C++
• Templates are instantiated at program link time
• Swap template may be stored in one file and the
24
Parametric Polymorphism: Implementation
27
Outline
Types in programming
Type safety
Type inference
Type declaration
28
Type checking and type inference
Type checking: The process of checking
whether the types declared by the programmer
“agrees” with the language constraints/
30
Type inference algorithm: some history
31
ML Type Inference
Example
- fun f(x) = 2+x;
val f = fn : int → int
@ : r (s = t→ r) λ : s →t
f :s x :t x :s e :t
34
Types with type variables
Example ’a is syntax for “type variable” (t in the graph)
Graph for λg. (g 2)
- fun f(g) = g(2);
val f = fn : (int→’a)→’a
3. Solve by substitution
35
Use of Polymorphic Function
Function
- fun f(g) = g(2);
val f = fn : (int→’a)→’a
Function
- fun f(g) = g(2);
val f = fn : (int→’a)→’a
37
Another type inference example
Function Definition
Graph for λ〈g,x〉. g(g x)
- fun f(g,x) = g(g(x));
val f = fn : (’a→’a)*’a → ’a
40
Information from type inference
An interesting function on lists
fun reverse (nil) = nil
| reverse (x::lst) = reverse(lst);
41
Outline
Types in programming
Type safety
Type inference
Type declaration
42
Type declaration
Transparent: alternative name to a type that
can be expressed without this name
43
Type declaration: Examples
Transparent (”type” declaration)
- toCelsius(60.4);
val it = 15.77904 : Celsius
44
Type declaration: Examples
Opaque (”datatype” declaration)
- datatype A = C of int;
- datatype B = C of int;
48