
fd643a14e77ce1d906b3f1368844578d.ppt
- Количество слайдов: 35
Universal Types Report by Matthias Horbach
Contents n Types of Polymorphism n System F l Basic Properties l Erasure l Impredicativity l Parametricity
Types of Polymorphism
Types of Polymorphism according to Strachey (1967) and Cardelli/Wegner (1985) universal (true) polymorphism ad hoc (apparent) parametric inclusion (new) overloading coercion and others and in more complex relations…
Ad Hoc Polymorphism Overloading n one name for different functions n just a convenient syntax abbreviation n example: +: int +: real z. B. 1 + 2 z. B. 1. 0 + 2. 0 Coercion and Casts n convert argument to fulfill requirements n examples: ((real) 1) + 1. 0 or 1 + 1. 0 Operators only seem to be polymorphic!
Universal Polymorphism Inclusion Polymorphism n one “object” belongs to many “classes” used in object oriented languages n modeled by subtypes n example: Dog, Bird Animal
Universal Polymorphism Parametric Polymorphism n Uniformity of type structure achieved by type parameters n examples: length (1: : 2: : 3: : nil) length (true: : false: : nil) length (“hello”: : “, ”: : “world”: : nil) //parameter: int //parameter: bool //parameter: string n let-polymorphism: - ML, no polymorphic arguments - automatic type reconstruction possible (see Sven) n We will look at a system with explicit type annotation.
System F
System F (Context) n Polymorphic -calculus n Second order -calculus n Idea: do lambda abstraction over type variables, define functions over types n Girard (1972), motivation: logics Reynolds (1974), motivation: programming
System F (What’s new? ) n Extension of the simply typed -calculus: Abstraction and application also for types: t : : = … X. t (type abstraction) t [T] (type application) n A new value: v : : = n Added types: T : : = … X. t (type abstraction value) … X X. T (type variable) (universal type) n Adjusted contexts: : : = … , X (type variable binding)
System F (Rules, 1) n New typing rules: type abstraction , X t : T X. t : X. T n New evaluation rules: type application (1) t t’ t [T] t’ [T] type application t : X. T’ t [T] : [T/X] T’ type application (2) ( X. t) [T] [T/X] t
System F (Rules, 2) n 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 :
System F (Examples) The polymorphic identity function (System F and ML) id = X. x: X. x val id = fn x => x > id: X. X X > ‘a id: ‘a is applied as follows: id [Nat] 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 id 5
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 ) double. Fun = double [Nat Nat] > double. Fun: ((Nat Nat) (Nat Nat) Nat double. Fun ( x. x+1) 3 >5
System F (Further Examples) Self application: In simply typed -calculus, you cannot type x. x x. Now: self. App = f. f f self. App = f: X. X X. f [ X. X X] f > self. App: ( X. X X) evaluation: self. App 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
Basic Properties
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
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
Normalization – Simple Application n There are untypable terms! n Example: ( x. x x) cannot be typable, since this term has no normal form.
Erasure
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, whethere is a well typed System F term t such that m = erase(t). Are there solutions for weaker erasure?
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).
Impredicativity
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 „ 1 st class“ in the world of types. example: ( f: X. X X. f) id universally quantified type
Impredicativity ML-polymorphism is predicative: Polymorphic types are 2 nd class, arguments do not have polymorphic types! („prenex polymorphism“) example: (fn f => fn x => f x) id 3 only one type / instanciated
Parametricity
Parametricity n Evaluation of polymorphic applications does not depend on the type that is supplied. n This is a strong invariant!
Parametricity n Examples of easy results from parametricity: l There is exactly one function of type X. X X, namely the identity function. l 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!
Church Encodings: Booleans System F has hidden structure: CBool = X. X X X contains (as already seen) tru = X. t: X. f: X. t fls = X. t: X. f: X. f (> tru: CBool) (> 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)
Church Encodings: Nat Elements of Nat could be encoded as c 0 = X. z. z s. s: X X. z: X. z c 1 = X. z. s z s. s: X X. z: X. s z c 2 = X. z. s (s z) z: X. s (s z) s. s: X X. 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))
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.
References n Barendregt: Lambda Calculi with Types Handbook of Computer Science, Vol. 2, 1992 n Cardelli, Wegner: On Understanding Types, Data Abstraction, and Polymorphism Computing Surveys, Vol. 17, No. 4, p. 471 -522, 1985 n Mac. Queen: Lecture Notes Chicago, 2003 n Pfenning: On the Undecidability of Partial Polymorphic Type Reconstruction Fundamentae Informaticae, Vol 19, No. 1 -2, p. 185 -199, 1993 n Pierce: Types and Programming Languages, Chapter 22 MIT Press, 2002
Questions / The END
A Sorting Function in System F (Reynolds 1985) n 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]) n only pure calculus, w/o fix or recursion back to normalization
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 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
fd643a14e77ce1d906b3f1368844578d.ppt