Presentation is loading. Please wait.

Presentation is loading. Please wait.

Universal Types Report by Matthias Horbach. Contents Types of Polymorphism System F Basic Properties Erasure Impredicativity Parametricity.

Similar presentations


Presentation on theme: "Universal Types Report by Matthias Horbach. Contents Types of Polymorphism System F Basic Properties Erasure Impredicativity Parametricity."— Presentation transcript:

1 Universal Types Report by Matthias Horbach

2 Contents Types of Polymorphism System F Basic Properties Erasure Impredicativity Parametricity

3 Types of Polymorphism

4 polymorphism universal (true) ad hoc (apparent) parametric inclusion (new) overloading coercion according to Strachey (1967) and Cardelli/Wegner (1985) and others and in more complex relations…

5 Ad 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!

6 Universal Polymorphism Inclusion Polymorphism one “object” belongs to many “classes” used in object oriented languages modeled by subtypes example: Dog, Bird  Animal

7 Universal Polymorphism Parametric Polymorphism Uniformity of type structure achieved by type parameters examples: length (1::2::3::nil)//parameter: int length (true::true::false::nil)//parameter: bool length (“hello”::“, ”::“world”::nil)//parameter: string let-polymorphism: - ML, no polymorphic arguments - automatic type reconstruction possible (see Sven) We will look at a system with explicit type annotation.

8 System F

9 System F (Context) Polymorphic -calculus Second order -calculus Idea: do lambda abstraction over type variables, define functions over types Girard (1972), motivation: logics Reynolds (1974), motivation: programming

10 System F (What’s new?) Extension of the simply typed -calculus: Abstraction 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)

11 System F (Rules, 1) New typing rules: type abstractiontype application New evaluation rules: type application (1)type application (2) t  t’ t [T]  t’ [T] ( X.t) [T]  [T/X] t ,X  t : T   X.t :  X.T   t :  X.T’   t [T] : [T/X] T’

12 System F (Rules, 2) Needed restriction: Types in these rules have to be closed, or free type variables have to be bound: X    X :  ,X  T :     X.T :    T 1 :    T 2 :    T 1  T 2 : 

13 System F (Examples) The polymorphic identity function (System F and ML) id = X. x:X. xval id = fn x => x > id:  X. X  X> ‘a id: ‘a  ‘a is applied as follows: id [Nat] 5id 5 which is evaluated as ( X. x:X. x) [Nat] 5  [Nat/X]( x:X. x) 5  ( x:Nat. x) 5  [5/x](x)  5

14 System F (Further Examples) Double application ( f(f(x)) ): double = X. f.X  X. x. f (f x) > double:  X. (X  X)  X  X (ML: val double = fn f => fn x => f(f x) > ‘a double: (‘a  ’a)  ‘a  ‘a ) doubleFun = double [Nat  Nat] > doubleFun: ((Nat  Nat)  Nat  Nat)  (Nat  Nat)  Nat  Nat doubleFun ( x. x+1) 3 > 5

15 System 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 > selfApp: (  X.X  X)  (  X.X  X) evaluation: selfApp id  ( f:  X.X  X. f [  X.X  X] f) id  ( X. x:X. x) [  X.X  X] id  ( x:  X.X  X. x) id  id

16 Basic Properties

17 Type Uniqueness, Type Preservation and Progress Theorem [Uniqueness]: Every well-typed system F term has exactly one type. Theorem [Preservation]:   t : T and t  t’ implies   t’ : T. Theorem [Progress]: If t is closed and well founded, then either t is a value or t  t’ for some t’. Proofs: straightforward structural induction

18 Normalization 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

19 Normalization – Simple Application There are untypable terms! Example: ( x. x x) ( x. x x) cannot be typable, since this term has no normal form.

20 Erasure

21 Erasure and Type Reconstruction See System F as extension of untyped -calculus: erase(x)= x erase( x:T. t)= x.erase(t) erase(t t‘)= (erase(t))(erase(t‘)) erase( X.t)= erase(t) erase(t[T])= 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?

22 Erasure and Evaluation Erasure operational semantics: Throw away types. Assume existence of divergence, side effects… Then let f = X. diverge in 0 diverges, but let f = diverge in 0 does. So another reasonable erasure is: erase(x)= x erase( x:T. t)= x.erase(t) erase(t t‘)= (erase(t)) (erase(t‘)) erase( X.t)= _.erase(t) erase(t[T])= erase(t) () Type reconstruction is still undecidable (Pfenning 1992).

23 Impredicativity

24 Impredicativity 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 quantified type

25 Impredicativity ML-polymorphism is predicative: Polymorphic types are 2nd class, arguments do not have polymorphic types! („prenex polymorphism“) example: (fn f => fn x => f x) id 3 only one type / instanciated

26 Parametricity

27 Parametricity Evaluation of polymorphic applications does not depend on the type that is supplied. This is a strong invariant!

28 Parametricity 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  X behaving differently, namely those denoted by X. a:X. b:X. a X. a:X. b:X. b These do not (and cannot) alter their behavior depending on X!

29 Church Encodings: Booleans System F has hidden structure: CBool =  X. X  X  X contains (as already seen) tru = X. t:X. f:X. t(> tru: CBool) fls = X. t:X. f:X. f(> fls: CBool) and other terms, but it is intuitively clear that they all behave like either tru or fls. One function on CBool is not = b:CBool. ( X. t:X. f:X. b [X] f t)

30 Church 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 = n:CNat. ( X. s:X  X. z:X. s (n [X] s z) ) c0 = X. s:X  X. z:X. z c1 = X. s:X  X. z:X. s z c2 = X. s:X  X. z:X. s (s z) c0 = s. z. z c1 = s. z. s z c2 = s. z. s (s z)

31 Summary  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.

32 References Barendregt: Lambda Calculi with Types Handbook of Computer Science, Vol. 2, 1992 Cardelli, Wegner: On Understanding Types, Data Abstraction, and Polymorphism Computing Surveys, Vol. 17, No. 4, p. 471-522, 1985 MacQueen: Lecture Notes Chicago, 2003 Pfenning: On the Undecidability of Partial Polymorphic Type Reconstruction Fundamentae Informaticae, Vol 19, No. 1-2, p. 185-199, 1993 Pierce: Types and Programming Languages, Chapter 22 MIT Press, 2002

33 Questions / The END

34 A Sorting Function in System F (Reynolds 1985) List X =  R. (X  R  R)  R  R insert = X. leq:X  X  bool. l:List X. e:X. let res = l [List X * List X] ( hd:X. acc:List X * List X. let rest = acc.1 in let newrest = hd::rest in let restwithe = acc.2 in let newrestwithe = if leq e hd then e::hd::rest else hd::restwithe in (newrest, newrestwithe) ) (nil[X], e::nil[X]) in res.2 sort = X. leq:X  X  bool. l:List X. l [List X] ( hd:X. rest:List X. insert [X] leq rest hd) (nil [X])  only pure calculus, w/o fix or recursion back to normalization

35 System F (Polymorphic Lists) List X =  R. (X  R  R)  R  R (Church encoding) nil:  X. List X X. ( R. c:X  R  R. n:R. n) cons:  X. X -> List X -> List X isnil:  X. List X -> Bool head:  X. List X -> X tail:  X. List X -> List X map:  X.  Y. (X -> Y) -> List X -> List Y


Download ppt "Universal Types Report by Matthias Horbach. Contents Types of Polymorphism System F Basic Properties Erasure Impredicativity Parametricity."

Similar presentations


Ads by Google