Resources: The slides of this lecture were derived from [Järvi], with permission of the original author, by copy & x = 1 let x = 1 in... paste or by selection, annotation, or rewording. [Järvi] is in turn based on [Pierce] as the underlying textbook. x(1).!x(1) x.set(1) Programming Language Theory Lambda Calculi With Polymorphism Ralf Lämmel [Järvi] Slides by J. Järvi: Programming Languages, CPSC 604 @ TAMU (2009) [Pierce] B.C. Pierce: Types and Programming Languages, MIT Press, 2002
Polymorphism -- Why? What s the identity function? In the simply typed lambda calculus, we need many! Examples λx:bool. x λx:nat. x λx:bool bool. x λx:bool nat. x... In need of polymorphic functions, i.e., functions that accept many types of arguments. 87
Kinds of polymorphism Parametric polymorphism ( all types ) Bounded polymorphism ( subtypes ) Ad-hoc polymorphism ( some types ) Font size correlates with coverage in lecture. Existential types ( exists as opposed to for all ) 88
Kinds of polymorphism Parametric polymorphism ( all types ) Bounded polymorphism ( subtypes ) Ad-hoc polymorphism ( some types ) Existential types ( exists as opposed to for all ) 89
System F -- Syntax tax Type variable t ::=x v tt t[t ] v ::=λx :T.t ΛX.t T ::=X T T X.T System F [Girard72,Reynolds74] = (simply-typed) lambda calculus + type abstraction & application Type application Type abstraction Polymorphic type Example: id : X.X X id =ΛX.λx :X.x 90
System F -- Typing rules Type variables are held the context. T-Variable x : T Γ Γ x : T T-Application Γ t : U T T-Abstraction Γ, x : T u : U Γ λx : T.u : T U Γ tu: T Γ u : U Type variables are subject to alpha conversion. T-TypeAbstraction Γ, X t : T Γ ΛX.t : X.T T-TypeApplication Γ t : X.T Γ t[t 1 ]:[T 1 /X ]T 91 Example: id : X.X X id =ΛX.λx :X.x
System F -- Evaluation rules E-AppFun t 1 t 1 t 1 t 2 t 1 t 2 E-TypeApp E-TypeApp t 1 t 1 t 1 [T ] t 1 [T ] E-AppArg t t vt vt E-AppAbs (λx :T.t) v [v/x]t E-TypeAppAbs (ΛX.t)[T ] [T /X ]t Example: id : X.X X id =ΛX.λx :X.x 92
Examples Term id =ΛX.λx :X.x id[bool] id[bool] true id true Type : X.X X : bool bool : bool type error In actual programming languages, type application may be implicit. 93
Instantiated with nat The doubling function double=λx.λf :X X.λx :X.f (f x) double_nat = double [nat] : (nat nat) nat nat Instantiated with nat nat double_nat_arrow_nat = double [nat nat] : ((nat nat) nat nat) (nat nat) nat nat Invoking double double [nat] (λx : nat.succ (succ x)) 5 * 9 94
Functions on polymorphic functions Consider the polymorphic identity function: id : X. X X id = ΛX.λx : X.x Use id to construct a pair of Boolean and String: pairid : (Bool, String) pairid = (id true, id true ) Abstract over id: pairapply : ( X. X X) (Bool, String) pairapply = λf : X. X X. (f true, f true ) Type application left implicit. Argument must be polymorphic! 95
Self application Not typeable in the simply-typed lambda calculus λx :?. x x Typeable in System F selfapp : ( X.X X) ( X.X X) selfapp = λx : X.X X.x [ X.X X] x 96
The fix operator (Y) Not typeable in the simply-typed lambda calculus Extension required Γ t : T T Typeable in System F. Γ fix t : T fix : X.(X X) X Encodeable in System F with recursive types. fix =? See [TAPL] 97
Meaning of all types In the type X...., we quantify over all types. Predicative polymorphism X ranges over simple types. Polymorphic types are type schemes. Type inference is decidable. Impredicative polymorphism X also ranges over polymorphic types. Type inference is undecidable. type:type polymorphism X ranges over all types, including itself. Computations on types are expressible. Type checking is undecidable. Not covered by this lecture Generality is used for selfapp. 98
Kinds of polymorphism Parametric polymorphism ( all types ) Bounded polymorphism ( subtypes ) Ad-hoc polymorphism ( some types ) Existential types ( exists as opposed to for all ) 99
What is subtyping anyway? We say S is a subtype of T. S <: T Liskov substitution principle: For each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of P is unchanged when o1 is substituted for o2. Practical type checking: Any expression of type S can be used in any context that expects an expression of type T, and no type error will occur. Subtype preserves behavior. Subtype preserves type safety. 100
Why subtyping Function in near-to-c: void foo( struct { int a; } r) { r.a = 0; } Function application in near-to-c: struct K { int a; int b: } K k; foo(k); // error Intuitively, it is safe to pass k. Subtyping allows it. 101
Subsumption (Subsititutability of supertypes by subtypes) Γ t : U Γ t : T U <: T This rule implies, for example, that the actual argument s type in a function application can also be a subtype of the function s argument type. 102
Structural subtyping for records A subtype may have additional label/type pairs. A subtype may permute the label/type pairs. A subtype may use subtypes for label/type pairs. 103
A subtype may have additional label/type pairs. S-RecordNewFields {l i : T i i 1...n+k } <: {l i : T i i 1...n } Example: {key : bool, value : int, map : int int} <: {key : bool, value : int} 104
A subtype may permute the label/type pairs. S-RecordPermutation {l i : T i i 1...n } is a permutation of {k j : U j j 1...n } {l i : T i i 1...n } <: {k j : U j j 1...n } Example: {key : bool, value : int} <: {value : int, key : bool} 105
A subtype may use subtypes for label/type pairs. Example: S-RecordElements for each i T i <: U i {l i : T i i 1...n } <: {l i : U i i 1...n } {field1 : bool, field2 : {val : bool}} <: {field1 : bool, field2 : {}} 106
General rules for subtyping Subsumption Reflexivity of subtyping Transitivity of subtyping Subtyping for function types Supertype of everything Up and down cast 107
General rules for subtyping Reflexivity T <: T Transitivity T <: U T <: V U <: V Example which needs transitivity Prove that {a : bool, b : int, c : {l : int}} <: {c : {}} 108
Subtyping of functions Subsumption provides subtyping for function arguments and results. An additional rule is needed to provide subtyping for function types. Compare this to the need for subtyping rules for record types. Consider the following function application: (λf : {a:int, b:int} {c:int}. f {a:42, b:88}) g Several types can be permitted for g: g : {a:int, b:int} {c:int} g : {a:int} {c:int, d:int}... The actual function may use less fields and return more fields. 109
Subtyping of functions Function subtyping covariant on return types contravariant on parameter types T 2 <: T 1 U 2 <: U 1 T 1 U 2 <: T 2 U 1 110
Supertype of everything T ::=... top The most general type T <: top The supertype of all types 111
Remember type annotation? Syntax: t ::=... t as T Typing rule: Γ t : T Γ t as T : T Evaluation rules: t u t as T u as T v as T v 112
Annotation as up-casting Illustrative type derivation:.. Γ t : U U <: T Γ t : T Γ t as T : T That is, type annotation automagically works as upcast because of the subsumption rule. Example: (λx :bool.{a = x, b = false}) true as {a : bool} 113
Annotation as down-casting Typing rule: Γ t : U Γ t as T : T Potentially too liberal Evaluation rules: t u t as T u as T Runtime type check v : T v as T v 114
Algorithmic subtyping Reminder: A type system is a tractable syntactic method for proving the absence of certain program behaviors by classifying phrases according to the kinds of values they compute. [B.C. Pierce] We violate this definition because some rules are not syntax-driven! 115
Violation of syntax direction Consider an application: t u where t of type U V and u of type S. A type checker would need to figure out that S <: U. This is hard with the rules so far (transitivity, subsumption). The rules need to be redesigned. 116
Analysis of subsumption The term in the conclusion can be anything. It is just a metavariable. T-Subsumption Γ t : U U <: T Γ t : T Example: Which rule should you apply here? Γ (λx :U.t) :? T-Abstraction or T-Subsumption? 117
Analysis of transitivity S-Transitivity T <: U T <: V U does not appear in conclusion. U <: V Thus, to show T <: V, we need to guess a U. For instance, try to show the following: {y :int, x :int} <: {x :int} 118
Analysis of transitivity What is the purpose of transitivity? Chaining together separate subtyping rules for records! S-RecordPermutation {l i : T i i 1...n } is a permutation of {k j : U j j 1...n } {l i : T i i 1...n } <: {k j : U j j 1...n } S-RecordElements for each i T i <: U i {l i : T i i 1...n } <: {l i : U i i 1...n } S-RecordNewFields {l i : T i i 1...n+k } <: {l i : T i i 1...n } 119
Algorithmic subtyping Replace all previous rules by a single rule. S-Record {l i i 1...n } {k j j 1...m } l i = k j implies U i <: T j e d need to prove: {k j : U j i 1...m } <: {l i : T i i 1...n } Correctness / completeness of new rule can be shown. Maintain extra rule for function types. } { S-Function T 1 <: T 2 U 1 <: U 2 T 2 U 1 <: T 1 U 2 120
Algorithmic subtyping The subsumption rule is still not syntax-directed. The rule is essentially used in function application. Express subsumption through an extra premise. Retire subsumption rule. T-Application Γ t : U T Γ u : V V <: U Γ tu: T 121
Kinds of polymorphism Parametric polymorphism ( all types ) Bounded polymorphism ( subtypes ) Ad-hoc polymorphism ( some types ) Existential types ( exists as opposed to for all ) 122
Kinds of polymorphism Parametric polymorphism ( all types ) Bounded polymorphism ( subtypes ) Ad-hoc polymorphism ( some types ) Existential types ( exists as opposed to for all ) 123
Universal versus existential quantification Existential types serve a specific purpose: will not show up in the exam! However, it s very healthy to know this stuff! A means for information hiding (encapsulation). Remember predicate logic. x.p(x) ( x. P(x)) Existential types can be encoded as universal types; see [TAPL]. 124
Overview Syntax of types: T ::= { X, T} Normal forms: v ::= { *T, v } Terms: t ::= { *T, t } as T let {X, x} = t in t Existentially quantified type Hidden type Package Package with type Unpacking 125
Constructing existentials A record: r : { a : nat, b : nat nat } r = { a = 1, b = λ x : nat. pred x } Illustration of using the record: r. b r.a A package with nat as hidden type: p : { X, {a : X, b : X nat}} p = { *nat, r } In fact, we need to assign this or another type. The type system makes sure that nat is inaccessible from outside. 126
Multiple types make sense for the package. Hence, the programmer must provide an annotation upon construction. r : { a : nat, b : nat nat } r = { a = 1, b = λ x : nat. pred x } p = {*nat, r } as { X, {a:x, b:x X}} p has type: { X, {a:x, b:x X}} preferred p = {*nat, r } as { X, {a:x, b:x nat}} p has type: { X, {a:x, b:x nat}} 127
We can have the same existential type with different representation types. p1 = {*nat, {a = 1, b = λx:nat. iszero x}} as { X, {a:x, b:x bool}} p2 = {*bool, {a = false, b = λx:bool. if x then false else true}} as { X, {a:x, b:x bool}} 128
Unpacking existentials (Opening package, importing module) Example -- apply b to a: let {X,x} = p2 in (x.b x.a) * true : bool Syntax: let {X,x} = t in t The value x of the existential becomes available. The representation type is not accessible (only X). 129
Typing rules T-PackExistential Γ t :[U/X ]T Γ { U, t} as { X, T } : { X, T } Substitution checks that the abstracted type of t can be instantiated with the hidden type to the actual type of t. T-UnpackExistential Γ t 1 : { X, T 12 } Γ, X, x : T 12 t 2 : T 2 Only expose abstract type of existential! Γ let {X, x} = t 1 in t 2 : T 2 130
Evaluation rules E-Unpack E-Pack t t { T, t} as U { T, t } as U t 1 t 1 let {X, x} = t 1 in t 2 let {X, x} = t 1 in t 2 E-UnpackPack let {X, x} =({ T, v} as U) in t 2 [T /X ][v/x]t 2 The hidden type is known to the evaluation, but the type system did not expose it; so t2 cannot exploit it. 131
Illustration of information hiding The type can be used in the scope of the unpacked package. let {X, x} = t in (λ y:x. x.b y) x.a * false : bool The representation type must remain abstract. t = {*nat, {a = 1, b = λx:nat. iszero x} as { X, {a:x, b:x bool}}} let {X,x} = t in pred x.a // Type error! The type must not leak into the resulting type: let {X, x} = t in x.a // Type error! 132
Summary: Lambdas with somewhat sexy types Done:,, <:,... Not done: μ,... Prepping: Types and Programming Languages Chapters 15, 16, 22, 23, 24 Outlook: Object calculi Process calculi More paradigms 133