1 / 18

Types and Programming Languages

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:.

Download Presentation

Types and Programming Languages

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Types and Programming Languages Lecture 13 Simon Gay Department of Computing Science University of Glasgow 2006/07

  2. 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

  3. Parametric Polymorphism: Preview and Motivation Consider f = x:intint. y:int. x(x(y)) which is correctly typed and has type (intint)intint. 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

  4. Parametric Polymorphism: Preview and Motivation The term f = x:X. y:Y. x(x(y)) becomes well-typed under various substitutions: X  intint, Y  int or X  boolbool, Y  bool or X  (intbool)(intbool), Y  intbool or if we just replace X by YY we get x:YY. 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:YY. y:Y. x(x(y)) . We have the most general instance or principal type of f. Types and Programming Languages Lecture 13 - Simon Gay

  5. 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 = YZ (Z is new) and then x(y):Z . Typechecking x(x(y)) requires X = ZW (W is new) and then x(x(y)):W . Now we unify the constraints X = YZ and X = ZW . Informally we can see that Y=Z and Z=W so X=YY and the type of the term is (YY)YY. This is called type reconstruction. Types and Programming Languages Lecture 13 - Simon Gay

  6. 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: (ZZ)ZZ and then generalized to form the principal type scheme: Z.(ZZ)ZZ which means: for any type Z, f can be used with type (ZZ)ZZ Example: the principal type scheme of list reverse is X. List X  List X Types and Programming Languages Lecture 13 - Simon Gay

  7. 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

  8. 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  XX] maps X to bool and Y to XX , not boolbool . Types and Programming Languages Lecture 13 - Simon Gay

  9. 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 Xdom() int  = int bool  = bool (TU) = 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

  10. 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:Xint. yx : int is derivable applying the substitution [ X  bool ] we get x:bool  y:boolint. yx : int which is also derivable. Types and Programming Languages Lecture 13 - Simon Gay

  11. 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 Xdom() This means that S(;) = (S) . Example: [ X  bool, Y ZX ] ; [ Z int ] = [ X bool, Y intX, Z  int ] Types and Programming Languages Lecture 13 - Simon Gay

  12. 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  Yint ], int ) ( [ X  YZ, Z  int ], Z ) ( [ X  intint, Y  int ], int ) ( [ X  YZ ], Z ) ( [ X  Yintint ], intint ) 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

  13. 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 US and returning S as the type of tu, the algorithm must choose a fresh type variable X, generate the constraint T = UX , 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

  14. 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

  15. 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

  16. 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:XY. x 0 : S | C for some S and C. Types and Programming Languages Lecture 13 - Simon Gay

  17. 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:XY. x 0 : (XY)Z | { intZ = XY } The substitution  = [ X  int, Y  bool, Z  bool ] satisfies the constraint, so a possible type for the term is ((XY)Z) = (intbool)bool Types and Programming Languages Lecture 13 - Simon Gay

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

More Related