Download presentation

Presentation is loading. Please wait.

Published byGabriella Gibson Modified over 4 years ago

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

2
Types and Programming Languages Lecture 13 - Simon Gay2 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.

3
2006/07Types and Programming Languages Lecture 13 - Simon Gay3 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.

4
2006/07Types and Programming Languages Lecture 13 - Simon Gay4 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.

5
2006/07Types and Programming Languages Lecture 13 - Simon Gay5 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.

6
2006/07Types and Programming Languages Lecture 13 - Simon Gay6 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

7
2006/07Types and Programming Languages Lecture 13 - Simon Gay7 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.

8
2006/07Types and Programming Languages Lecture 13 - Simon Gay8 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.

9
2006/07Types and Programming Languages Lecture 13 - Simon Gay9 Substitution If T is a type and is a substitution then we define the application of to T, written T, as follows. X = T if (X T) 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.

10
2006/07Types and Programming Languages Lecture 13 - Simon Gay10 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.

11
2006/07Types and Programming Languages Lecture 13 - Simon Gay11 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 ]

12
2006/07Types and Programming Languages Lecture 13 - Simon Gay12 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.

13
2006/07Types and Programming Languages Lecture 13 - Simon Gay13 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. unifies or satisfies C if it unifies every equation in C.

14
2006/07Types and Programming Languages Lecture 13 - Simon Gay14 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) (CT-Abs) no constraints no new constraints (CT-App) X is a fresh type variable, different from all other type variables occurring anywhere in the derivation

15
2006/07Types and Programming Languages Lecture 13 - Simon Gay15 Constraint-Based Typing true : bool | { } false : bool | { } v : int | { } if v is an integer literal (CT-Plus) (CT-Eq) (CT-And) (CT-If)

16
2006/07Types and Programming Languages Lecture 13 - Simon Gay16 Examples of Constraint-Based Typing Example 1 x:X. y:Y. z:Z. (x z) (y z) : S | C Construct a constraint typing derivation whose conclusion is for some S and C. Example 2 x:X Y. x 0 : S | C Construct a constraint typing derivation whose conclusion is for some S and C.

17
2006/07Types and Programming Languages Lecture 13 - Simon Gay17 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

18
2006/07Types and Programming Languages Lecture 13 - Simon Gay18 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).

Similar presentations

OK

Haskell user defined types data Temp = Cold|Hot|Warm deriving (Show,Eq, Ord, Enum) -- to enable printing to screen -- comparing for equality -- comparison.

Haskell user defined types data Temp = Cold|Hot|Warm deriving (Show,Eq, Ord, Enum) -- to enable printing to screen -- comparing for equality -- comparison.

© 2018 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on ideal gas law formula Ppt on review of literature sample Ppt on active and passive voice Ppt on mammals and egg laying animals name Ppt on surface water runoff Ppt on different solid figures worksheet Ppt on triangles for class 9th Ppt on mars one finalists Ppt on international financial markets By appt only movie theater