type systems
play

Type Systems 2. System F 3. Properties of System F 4. System - PDF document

Today Parametric Polymorphism 1. Recall Let-Polymorphism Type Systems 2. System F 3. Properties of System F 4. System F-sub Lecture 9 Dec. 15th, 2004 5. Properties of F-sub Sebastian Maneth


  1. Today Parametric Polymorphism 1. Recall Let-Polymorphism Type Systems 2. System F 3. Properties of System F 4. System F-sub Lecture 9 Dec. 15th, 2004 5. Properties of F-sub Sebastian Maneth http://lampwww.epfl.ch/teaching/typeSystems/2004 1. Recall Let-Polymorphism 1. Recall Let-Polymorphism Γ ` t 1 :T 1 Γ ` [x � t 1 ]t 2 :T 2 In simply-typed lambda-calculus, we can leave out ALL type annotations: Γ ` � insert new type variables ` let x=t 1 in t 2 :T 2 � do type reconstruction (using unification) let double = λ x. λ y. x(x(y)) in In this way, changing the let-rule, we obtain { let a = double ( λ x:int. x+2) 2 in { Let-Polymorphism let b = double ( λ x:bool. x) false in {..} } } � Simple form of polymorphism � Introduced by [ Milner 1978 ] in ML CAN be typed now!! Because the new let rule creates two copies � also known as Damas-Milner polymorphism of double, and the rule for abstraction assigns a different type variable to each one. � in ML, basis of powerful generic libraries (e.g., lists, arrays, trees, hash tables, …) 1. Recall Let-Polymorphism 2. System F Aka polymorphic lambda-calculus or second-order lambda-calculus. Limits of Let-Polymorphism? � Only let-bound variables can be used polymorphically! � do lambda-abstraction over type variables, � NOT lambda-bound variables define functions over types Ex.: let f = λ g. … g(1) … g(true) … in { f( λ x.x) } Invented by is not typable: when typechecking the def. of f, g has type X (fresh) Which is then constrained by X = int � Y and X = bool � Z. � Girard (1972) motivated by logics � Reynolds (1974) motivated by programming. Functions cannot take polymorphic functions as parameters. (= no polymorphic arguments!) 1

  2. 2. System F 2. System F Aka polymorphic lambda-calculus or second-order lambda-calculus. Aka polymorphic lambda-calculus or second-order lambda-calculus. � Add (universal) quantification over TYPEs! � Add (universal) quantification over TYPEs! � Straightforward extension of simply typed lambda-calculus by � Straightforward extension of simply typed lambda-calculus by two new constructs: two new constructs: Type Abstraction: λ X. t Type Abstraction: λ X. t Type Application: t [T] Type Application: t [T] For example, the polymorphic identity function For example, the polymorphic identity function id = λ X. λ x:X. x id = λ X. λ x:X. x can be applied to Nat by writing id [Nat] . The result is [X/Nat]( λ x:X. x) = λ x:Nat.x 2. System F 2. System F What is the type of What is the type of id = λ X. λ x:X. x id = λ X. λ x:X. x � If applied to a type T, id yields function of type T � T. � If applied to a type T, id yields function of type T � T. Therefore denote its type by: ∀ X.X � X Typing rules for type abstraction and type application: Γ, X ` ` t 2 :T 2 Γ ` ` t 1 : ∀ X.T 12 Γ ` λ X.t 2 : ∀ X.T 2 Γ ` t 1 [T 2 ] : [X/T 2 ]T 12 2. System F 2. System F Examples. Evaluation, like simply typed lambda (3 rules), plus two new rules: Polymorphic identity function: id = λ X. λ x:X. x t 1 � t 2 ’ ( λ X.t 12 )[T 2 ] � [X/T 2 ]t 12 t 1 [T 2 ] � t 1 ’[T 2 ] Apply it: id [Nat] 5 = ( λ X. λ x:X. x) [Nat] 5 Values (in the pure system) are � [X/Nat]( λ x:X. x) 5 � ( λ x:Nat. x) 5 λ x:T.t � abstraction value � [x/5](x) � λ X.t type abstraction value � 5 As we saw, the type of id is ∀ X. X � X. Contexts contain x:T term variable binding and X type variable binding Can you find a function different from id , with the SAME TYPE?? 2

  3. 2. System F 2. System F Examples. Examples. Polymorphic doubling function: In simply typed lambda-calculus double = λ X. λ f:X � X. λ a:X. f (f a) omega = ( λ x.x x) ( λ x. x x) canNOT be typed! double [Nat] ( λ x:Nat. succ(succ(x))) 3 � 7 Neither can the self-application fragment ( λ x.x x) In System-F we CAN type it: quadruple = λ X. double [X � X] (double [X]) self = λ x: ( ∀ X. X � X). x [ ∀ X. X � X] x self: ( ∀ X.X � X) � ( ∀ X.X � X) What’s the type of quadruple? 2. System F 2. System F Examples. Main advantage of polymorphism: In simply typed lambda-calculus � many things need not be built into the language, but can be moved into libraries omega = ( λ x.x x) ( λ x. x x) Example. Lists. canNOT be typed! Before: Neither can the self-application fragment ( λ x.x x) In System-F we CAN type it: For a type T: List T describes finite-length lists of elements from T. self = λ x: ( ∀ X. X � X). x [ ∀ X. X � X] x New syntactic forms: nil[T] cons[T] t1 t2 self: ( ∀ X.X � X) � ( ∀ X.X � X) isnil[T] t head[T] t Apply self to some function; e.g., to ( λ Y. λ y:Y. y) tail[T] t Polym. Fu’s are “first class citizen” 2. System F 2. System F Main advantage of polymorphism: Now we can build a library of polymorphic operations on lists. For example, a polymorphic map function. � many things need not be built into the language, but can be moved into libraries map = λ X. λ Y. λ f:X � Y. Example. Lists. (fix ( λ m:List X � List Y. λ l:List X. Now: if isnil[X] l then nil[Y] else cons[Y](f (head[X] l)) List X describes finite-length lists of elements of type X. (m (tail[X] l)))) New syntactic forms: nil: ∀ X. List X What is the type of map? cons: ∀ X. X � List X � List X isnil: ∀ X. List X � Bool head: ∀ X. List X � X tail: ∀ X. List X � List X 3

  4. 2. System F 3. Properties of System F Now we can build a library of polymorphic operations on lists. System F is impredicative : For example, a polymorphic map function. � Polymorphic types are universally quantified over the universe of ALL types. This includes polymorphic types themselves! map = λ X. λ Y. λ f:X � Y. � Polymorphic types are “1 st class” citizens in the world of types (fix ( λ m:List X � List Y. λ l:List X. if isnil[X] l then nil[Y] E.g. ( λ f: ( ∀ X.X � X). f) id else cons[Y](f (head[X] l)) (m (tail[X] l)))) ML (let) – polymorphims is predicative : What is the type of map? � Polymorphic types are 2 nd class. Arguments do not have l = cons[Nat] 4 (cons[Nat] 3 (cons[Nat] 2 (nil[Nat]))) polymorphic types! (prenex polymorphism) head[Nat](map[Nat][Nat] ( λ x:Nat. succ x) l) E.g. (fn f => fn x => f x) id 3 � 5 3. Properties of System F 3. Properties of System F Parametricity System F is impredicative : Evaluation of polymorphic applications does not depend � Polymorphic types are universally quantified over the on the type that is supplied! universe of ALL types. This includes polymorphic types themselves! � Polymorphic types are “1 st class” citizens in the world of types � There is exactly one function of type ∀ X.X � X (namely, the identity) � Type variables range only over E.g. ( λ f: ( ∀ X.X � X). f) id quantifier-free types ( monotypes ) � There are exactly two functions of type ∀ X.X � X � X � Quantified types ( polytypes , or type schemes ) not allowed on left of arrow which have different behavior. Namely, λ X. λ a:X. λ b:X. a ML (let) – polymorphims is predicative : and λ X. λ a:X. λ b:X. b � Polymorphic types are 2 nd class. Arguments do not have polymorphic types! prenex polymorphism These do not (and cannot) alter their behavior depending on X! E.g. (fn f => fn x => f x) id 3 3. Properties of System F Erasure and Type Reconstruction Parametricity erase(x) = x Evaluation of polymorphic applications does not depend erase( λ x:T.t) = λ x.erase(t) on the type that is supplied! erase(t t’) = erase(t) erase(t’) erase( λ X.t) = erase(t) Nevertheless, we defined a type-passing semantics : erase(t [T]) = erase(t) ( λ X.t 12 )[T 2 ] � [X/T 2 ]t 12 Theorem . (Wells, 1994): Let u be a closed term. It is undecidable, whether there is a well-typed system-F-term t with erase(t)=u. Why do this, if eval. does not depend on it? � Type Reconstruction for system F is not possible! � type-erasure semantics: After the typechecking phase, all types are erased! 4

Recommend


More recommend