Title: Universal Types
1Universal Types
- Report by
- Matthias Horbach
2Contents
- Types of Polymorphism
- System F
- Basic Properties
- Erasure
- Impredicativity
- Parametricity
3Types of Polymorphism
4Types of Polymorphism
according to Strachey (1967) and Cardelli/Wegner
(1985)
parametric inclusion (new)
universal (true)
polymorphism
overloading coercion
ad hoc (apparent)
and others and in more complex relations
5Ad Hoc Polymorphism
- Overloading
- one name for different functions
- just a convenient syntax abbreviation
- example int ? int z.B. 1 2 real ?
real z.B. 1.0 2.0 - Coercion and Casts
- convert argument to fulfill requirements
- examples ((real) 1) 1.0 or 1 1.0
- Operators only seem to be polymorphic!
6Universal Polymorphism
- Inclusion Polymorphism
- one object belongs to many classesused in
object oriented languages - modeled by subtypes
- example Dog, Bird ? Animal
7Universal Polymorphism
- Parametric Polymorphism
- Uniformity of type structure achieved by type
parameters - examples length (123nil) //parameter
int length (truetruefalsenil) //parameter
bool length (hello, worldnil) //para
meter string - let-polymorphism- ML, no polymorphic
arguments- automatic type reconstruction
possible (see Sven) - We will look at a system with explicit type
annotation.
8System F
9System F (Context)
- Polymorphic ?-calculus
- Second order ?-calculus
- Ideado lambda abstraction over type variables,
define functions over types - Girard (1972), motivation logicsReynolds
(1974), motivation programming
10System F (Whats new?)
- Extension of the simply typed ?-calculusAbstract
ion and application also for types t
?X.t (type abstraction) t T (type
application) - A new value v ?X.t (type abstraction
value) - Added types T X (type
variable) ?X.T (universal type) - Adjusted contexts ? ?, X (type
variable binding)
11System F (Rules, 1)
- New typing rules type abstraction type
application - New evaluation rules type application (1) type
application (2)
12System F (Rules, 2)
- Needed restriction Types in these rules have to
be closed, or free type variables have to be
bound
13System F (Examples)
- The polymorphic identity function (System F and
ML) id ?X. ?xX. x val id fn x gt x gt id
?X. X ? X gt a id a ? a - is applied as follows id Nat 5 id 5
- which is evaluated as
- (?X. ?xX. x) Nat 5
- ? Nat/X(?xX. x) 5
- ? (?xNat. x) 5
- ? 5/x(x)
- ? 5
14System F (Further Examples)
- Double application ( f(f(x)) )
- double ?X. ?f.X?X. ?x. f (f x) gt double
?X. (X?X) ? X ? X - (ML val double fn f gt fn x gt f(f x) gt a
double (a ? a) ? a ? a ) - doubleFun double Nat?Nat gt doubleFun
((Nat?Nat) ? Nat ? Nat) ? (Nat?Nat) ? Nat ?
Nat - doubleFun (?x. x1) 3 gt 5
15System F (Further Examples)
- Self application
- In simply typed ?-calculus, you cannot type ?x. x
x. - Now
- selfApp ?f. f f
- selfApp ?f?X.X?X. f ?X.X?X f
- gt selfApp (?X.X?X)?(?X.X?X)
- evaluation
- selfApp id
- ? (?f?X.X?X. f ?X.X?X f) id
- ? (?X. ?xX. x) ?X.X?X id
- ? (?x?X.X? X. x) id
- ? id
16Basic Properties
17Type Uniqueness, Type Preservation and Progress
- Theorem UniquenessEvery well-typed system F
term has exactly one type. - Theorem Preservation? ? t T and t ? t
implies ? ? t T. - Theorem ProgressIf t is closed and well
founded,then either t is a value or t ? t for
some t. - Proofs straightforward structural induction
18Normalization
- Theorem Every well-typed System F term is
normalizing, i.e. the evaluation of well-typed
programs terminates. - Proof very hard (Girard 1972, doctoral
thesis)(simplified later on to about 5 pages) - Amazing Normalization holds although we can code
many things.
to sorting function
19Normalization Simple Application
- There are untypable terms!
- Example (?x. x x) (?x. x x)cannot be typable,
since this term has no normal form.
20Erasure
21Erasure and Type Reconstruction
- See System F as extension of untyped ?-calculus
- erase(x) x
- erase(?xT. t) ?x.erase(t)
- erase(t t) (erase(t))(erase(t))
- erase(?X.t) erase(t)
- erase(tT) erase(t)
- Theorem (Wells, 1994) Let m be a closed term. It
is undecidable, whether there is a well typed
System F term t such that m erase(t). - Are there solutions for weaker erasure?
22Erasure and Evaluation
- Erasure operational semantics Throw away types.
- Assume existence of divergence, side effects
Then let f ?X. diverge in 0diverges,
but let f diverge in 0does. - So another reasonable erasure iserase(x)
xerase(?xT. t) ?x.erase(t)erase(t t)
(erase(t)) (erase(t))erase(?X.t)
?_.erase(t)erase(tT) erase(t) () - Type reconstruction is still undecidable
(Pfenning 1992).
23Impredicativity
24Impredicativity
- System F is impredicative
- Polymorphic types are defined by universal
quantification over the universe of all
types.This includes polymorphic types
themselves. - Polymorphic types are 1st class in the world of
types. - example(?f?X.X?X. f) id
universally quantifiedtype
25Impredicativity
- ML-polymorphism is predicative
- Polymorphic types are 2nd class,arguments do not
have polymorphic types!(prenex polymorphism) - example (fn f gt fn x gt f x) id 3
only one type /instanciated
26Parametricity
27Parametricity
- Evaluation of polymorphic applications does not
depend on the type that is supplied. - This is a strong invariant!
28Parametricity
- Examples of easy results from parametricity
- There is exactly one function of
type ?X.X?X,namely the identity function. - There are exactly two functions of type
?X.X?X?Xbehaving differently, namely those
denoted by ?X. ?aX. ?bX. a ?X. ?aX. ?bX.
b - These do not (and cannot) alter their behavior
depending on X!
29Church Encodings Booleans
- System F has hidden structure
- CBool ?X. X?X?X
- contains (as already seen)
- tru ?X. ?tX. ?fX. t (gt tru CBool) fls
?X. ?tX. ?fX. f (gt fls CBool) - and other terms, but it is intuitively clear
that theyall behave like either tru or fls. - One function on CBool is
- not ?bCBool. (?X. ?tX. ?fX. b X f t)
30Church Encodings Nat
- Elements of Nat could be encoded as
-
- The corresponding type is
- CNat ?X. (X?X)?X?X
- and a term encoding the successor function is
- csucc ?nCNat. (?X. ?sX?X. ?zX. s (n X s
z))
c0 ?X. ?sX?X. ?zX. zc1 ?X. ?sX?X. ?zX. s
zc2 ?X. ?sX?X. ?zX. s (s z)
c0 ?s. ?z. zc1 ?s. ?z. s zc2 ?s. ?z. s (s
z)
31Summary
- ? System F is highly expressive!
- ? Still, it is strongly normalizing!!!
- ? Types must not be omitted.
- In practice Trade-off between convenience
(e.g. automated type checking) and expressivity.
32References
- Barendregt Lambda Calculi with TypesHandbook of
Computer Science, Vol. 2, 1992 - Cardelli, Wegner On Understanding Types, Data
Abstraction, and PolymorphismComputing Surveys,
Vol. 17, No. 4, p. 471-522, 1985 - MacQueen Lecture NotesChicago, 2003
- Pfenning On the Undecidability of Partial
Polymorphic Type ReconstructionFundamentae
Informaticae, Vol 19, No. 1-2, p. 185-199, 1993 - Pierce Types and Programming Languages, Chapter
22MIT Press, 2002
33Questions / The END
34A Sorting Function in System F (Reynolds 1985)
- List X ?R. (X ? R ? R) ? R ? Rinsert ?X.
?leqX?X?bool. ?lList X. ?eX. let res l
List X List X (?hdX. ?accList X List
X. let rest acc.1 in let newrest
hdrest in let restwithe acc.2 in let
newrestwithe if leq e hd then
ehdrest else hdrestwithe
in (newrest, newrestwithe) ) (nilX,
enilX) in res.2sort ?X. ?leqX?X?bool.
?lList X. l List X (?hdX. ?restList X.
insert X leq rest hd) (nil X) - ? only pure calculus, w/o fix or recursion
back to normalization
35System F (Polymorphic Lists)
- List X ?R. (X ? R ? R) ? R ? R (Church
encoding) - nil ?X. List X ?X. (?R. ?cX?R?R. ?nR. n)
- cons ?X. X -gt List X -gt List X
- isnil ?X. List X -gt Bool
- head ?X. List X -gt X
- tail ?X. List X -gt List X
- map ?X. ?Y. (X -gt Y) -gt List X -gt List Y