180 likes | 256 Views
Types and Programming Languages. Lecture 13. Simon Gay Department of Computing Science University of Glasgow. 2006/07. Polymorphism. Generally: the idea that an operation can be applied to values of different types. Three kinds of polymorphism:.
E N D
Types and Programming Languages Lecture 13 Simon Gay Department of Computing Science University of Glasgow 2006/07
Polymorphism Generally: the idea that an operation can be applied to values of different types. Three kinds of polymorphism: Ad hoc polymorphism, aka overloading. Examples: + operator or more generally overloaded methods (resolved at compile time on the basis of parameter types); overridden methods (resolved at runtime on the basis of the actual type of an object). Subtype polymorphism in OO languages: e.g. the example with the Object[] parameter. Used together with overriding. Parametric polymorphism: functions which work uniformly across a range of types. E.g. list reverse in Haskell. Only requires static typechecking. We will look at parametric polymorphism. Types and Programming Languages Lecture 13 - Simon Gay
Parametric Polymorphism: Preview and Motivation Consider f = x:intint. y:int. x(x(y)) which is correctly typed and has type (intint)intint. If we remove the type annotations: f = x. y. x(x(y)) then what can we say about the type of f ? Introduce type variables (think of them as uninterpreted base types): f = x:X. y:Y. x(x(y)) This is not well-typed (X is not a function type) but we can look for substitution instances (replacing X and Y by types) which make it well-typed. Types and Programming Languages Lecture 13 - Simon Gay
Parametric Polymorphism: Preview and Motivation The term f = x:X. y:Y. x(x(y)) becomes well-typed under various substitutions: X intint, Y int or X boolbool, Y bool or X (intbool)(intbool), Y intbool or if we just replace X by YY we get x:YY. y:Y. x(x(y)) which is well-typed for every type Y and in fact every typing of f is an instance of x:YY. y:Y. x(x(y)) . We have the most general instance or principal type of f. Types and Programming Languages Lecture 13 - Simon Gay
Parametric Polymorphism: Preview and Motivation How do we discover the principal type of x:X. y:Y. x(x(y)) ? Typecheck the term, accumulating constraints on the type variables. Typechecking x(y) requires X = YZ (Z is new) and then x(y):Z . Typechecking x(x(y)) requires X = ZW (W is new) and then x(x(y)):W . Now we unify the constraints X = YZ and X = ZW . Informally we can see that Y=Z and Z=W so X=YY and the type of the term is (YY)YY. This is called type reconstruction. Types and Programming Languages Lecture 13 - Simon Gay
Parametric Polymorphism: Preview and Motivation Languages such as ML and Haskell use type reconstruction as the basis for let-polymorphism, which is a form of parametric polymorphism. For example if we define f = x:X. y:Y. x(x(y)) then the most general type is calculated: (ZZ)ZZ and then generalized to form the principal type scheme: Z.(ZZ)ZZ which means: for any type Z, f can be used with type (ZZ)ZZ Example: the principal type scheme of list reverse is X. List X List X Types and Programming Languages Lecture 13 - Simon Gay
Type Reconstruction and Let-Polymorphism Our task now is to understand the details: Formalize substitution of types for type variables. Formalize constraint-based typing which generates constraints on type variables. Define an algorithm for unification in order to solve constraints. Understand exactly what let-polymorphism means. Then we will look at a more general form of polymorphism. Types and Programming Languages Lecture 13 - Simon Gay
Type Reconstruction / Substitution We will work with the simply typed lambda-calculus (function types, int and bool) with the addition of type variables X, Y, ... Definition: a type substitution is a finite mapping from type variables to types. Example: [X T, Y U] Use to stand for a substitution. dom() is {X,Y} in this example. range() is { T,U }. Substitutions are simultaneous, so [X bool, Y XX] maps X to bool and Y to XX , not boolbool . Types and Programming Languages Lecture 13 - Simon Gay
Substitution If T is a type and is a substitution then we define the application of to T, written T, as follows. T if (X T) X = X if Xdom() int = int bool = bool (TU) = T U Substitution is extended to environments: A substitution is applied to a term t by applying it to all types appearing in annotations in t. Write t. Types and Programming Languages Lecture 13 - Simon Gay
Type Substitution Preserves Typing Theorem: if t:T and is any type substitution then t :T . This can be proved by induction on the structure of t. Example: x:X y:Xint. yx : int is derivable applying the substitution [ X bool ] we get x:bool y:boolint. yx : int which is also derivable. Types and Programming Languages Lecture 13 - Simon Gay
Composition of Substitutions If and are substitutions then we write ; for the substitution consisting of followed by . The definition is: X T for each (X T) ; = X T for each (X T) with Xdom() This means that S(;) = (S) . Example: [ X bool, Y ZX ] ; [ Z int ] = [ X bool, Y intX, Z int ] Types and Programming Languages Lecture 13 - Simon Gay
Solutions Definition: let be an environment and t a term. A solution for (,t) is a pair (,T) such that t : T . Example: let = f:X, a:Y and t = f a . Then the following are all solutions for (,t) . ( [ X Yint ], int ) ( [ X YZ, Z int ], Z ) ( [ X intint, Y int ], int ) ( [ X YZ ], Z ) ( [ X Yintint ], intint ) Exercise: find three different solutions for the term x:X. y:Y. z:Z. (x z) (y z) in the empty environment. Types and Programming Languages Lecture 13 - Simon Gay
Constraint-Based Typing We can modify the typechecking algorithm so that instead of checking constraints on types, it generates constraints which will be solved later. Example: given an application tu with t:T and u:U , instead of checking that T has the form US and returning S as the type of tu, the algorithm must choose a fresh type variable X, generate the constraint T = UX , and return X as the type of tu. Definition: a constraint set C is a set of equations A substitution unifies an equation S=T if S and T are identical. unifiesorsatisfies C if it unifies every equation in C. Types and Programming Languages Lecture 13 - Simon Gay
Constraint-Based Typing We define the constraint typing relation t:T | C by inference rules. t:T | C means “term t has type T under assumptions whenever the constraints C are satisfied”. (CT-Var) no constraints (CT-Abs) no new constraints (CT-App) X is a fresh type variable, different from all other type variables occurring anywhere in the derivation Types and Programming Languages Lecture 13 - Simon Gay
Constraint-Based Typing true : bool | { } false : bool | { } v : int | { } if v is an integer literal (CT-Plus) (CT-Eq) (CT-And) (CT-If) Types and Programming Languages Lecture 13 - Simon Gay
Examples of Constraint-Based Typing Example 1 Construct a constraint typing derivation whose conclusion is x:X. y:Y. z:Z. (x z) (y z) : S | C for some S and C. Example 2 Construct a constraint typing derivation whose conclusion is x:XY. x 0 : S | C for some S and C. Types and Programming Languages Lecture 13 - Simon Gay
Constraint-Based Typing Definition: Suppose that t:S | C . A solution for (,t,S,C) is a pair (,T) such that satisfies C and S = T . Example: from the previous slide we have x:XY. x 0 : (XY)Z | { intZ = XY } The substitution = [ X int, Y bool, Z bool ] satisfies the constraint, so a possible type for the term is ((XY)Z) = (intbool)bool Types and Programming Languages Lecture 13 - Simon Gay
Correctness of Constraint-Based Typing Given an environment and a term t we have two different ways of characterizing the possible ways of instantiating type variables in and t to produce a valid typing: Declarative: as the set of all solutions for (,t) in the sense of the definition on slide 12. Algorithmic: via the constraint typing relation, by finding S and C such that t:S | C and then taking the set of solutions for (,t,S,C). It is possible to prove that these characterizations are equivalent (Pierce 22.3). Types and Programming Languages Lecture 13 - Simon Gay