Presentation is loading. Please wait.

Presentation is loading. Please wait.

Catriel Beeri Pls/Winter 2004/5 type reconstruction 1 Type Reconstruction & Parametric Polymorphism  Introduction  Unification and type reconstruction.

Similar presentations


Presentation on theme: "Catriel Beeri Pls/Winter 2004/5 type reconstruction 1 Type Reconstruction & Parametric Polymorphism  Introduction  Unification and type reconstruction."— Presentation transcript:

1 Catriel Beeri Pls/Winter 2004/5 type reconstruction 1 Type Reconstruction & Parametric Polymorphism  Introduction  Unification and type reconstruction  Let polymorphism  Explicit parametric polymorphism

2 Catriel Beeri Pls/Winter 2004/5 type reconstruction 2  Introduction Type reconstruction (inference): Given e w/o type declarations, find: e’ with type declarations H for freevars(e) (= freevars(e’)) a type s.t. Erase (e’) = e and We then say that (in practice, no need to compute e’)

3 Catriel Beeri Pls/Winter 2004/5 type reconstruction 3 In a monomorphic system – reconstruction is problematic: Polymorphism: allow many types for a value/variable/expression Reconstruction makes sense in a polymorphic system Can ask for (a finite representation of) all

4 Catriel Beeri Pls/Winter 2004/5 type reconstruction 4 Parametric polymorphism: based on the use of type variables A ground type: no type variables

5 Catriel Beeri Pls/Winter 2004/5 type reconstruction 5 A type with type variables: a finite representation of a set of mono-types parametric polymorphic type system  we can hope to find for expression e a finite representation of all the pairs s.t.

6 Catriel Beeri Pls/Winter 2004/5 type reconstruction 6  Unification and type reconstruction we study type reconstruction using : FL with let Type rules for type reconstruction in a poly system, 1st try: the “same” rule as the monomorphic system Differences: no type annotations for declared variables Types may contain type variables

7 Catriel Beeri Pls/Winter 2004/5 type reconstruction 7

8 Catriel Beeri Pls/Winter 2004/5 type reconstruction 8 Note: we guess types for declared variables – this is how type assignments get into type environments Q: Where in a type derivation do we guess? A: in the root-to-leaves phase, in a lambda node

9 Catriel Beeri Pls/Winter 2004/5 type reconstruction 9 Q: Which guess is right? best? A: The approach of type reconstruction: We guess the most general types, then we specialize as needed to satisfy the constraints (from the type rules), implied by the given expression The hope: to obtain the most general typing – that represents all the typings of the expression Q: Which guess is the most general?

10 Catriel Beeri Pls/Winter 2004/5 type reconstruction 10 Example: What is the type of We guess: then specialize to satisfy the constraint in the (applic) rule:

11 Catriel Beeri Pls/Winter 2004/5 type reconstruction 11 Substitutions, type constraints, unification type substitution: a function from type variables to types Notation: Application of a substitution:

12 Catriel Beeri Pls/Winter 2004/5 type reconstruction 12 A substitution can be applied also to type environments, sets of types, typed expressions

13 Catriel Beeri Pls/Winter 2004/5 type reconstruction 13 Composition of substitutions: Example:

14 Catriel Beeri Pls/Winter 2004/5 type reconstruction 14 The equivalence classes: same substitution modulo variable renaming (actual variables used are irrelevant)

15 Catriel Beeri Pls/Winter 2004/5 type reconstruction 15 Type reconstruction : Given un-typed e, we guess distinct type variables for all free & lambda-bound variables in e, yielding: H for freevars(e) and typed e’ The restriction of the initial guess is needed because of equality constraints (type equations) in the rules Note: all uses of a program variable have the same type  using a substitution to instantiate the initial guess.

16 Catriel Beeri Pls/Winter 2004/5 type reconstruction 16 Some type rules impose equality constraints (type equations) on the types used in them Examples: Are there constraints in other rules?

17 Catriel Beeri Pls/Winter 2004/5 type reconstruction 17 How do we solve a set of type equations? Solving equations of expressions with variables, constants and constructors is unification Solving one equation or several --- equivalent problems (introduce a dummy root) Most general unifier, mgu --- more/as general as any other. Does one exist? Is it unique?

18 Catriel Beeri Pls/Winter 2004/5 type reconstruction 18

19 Catriel Beeri Pls/Winter 2004/5 type reconstruction 19 The unification algorithm: Input: a set C of type equations Output: a substitution - a unifier (an mgu), or fail Intuition: In each step: One equation is resolved, resulting in: An incremental change to substitution Possibly addition of some equations

20 Catriel Beeri Pls/Winter 2004/5 type reconstruction 20

21 Catriel Beeri Pls/Winter 2004/5 type reconstruction 21

22 Catriel Beeri Pls/Winter 2004/5 type reconstruction 22 Examples: (An equation is represented as a pair of types)

23 Catriel Beeri Pls/Winter 2004/5 type reconstruction 23

24 Catriel Beeri Pls/Winter 2004/5 type reconstruction 24 Comments: (iii) The algorithm assumes a freely generated domain Where? (iv) The algorithm runs in poly time (can be made linear)

25 Catriel Beeri Pls/Winter 2004/5 type reconstruction 25 Properties of the algorithm: Termination: is guaranteed (why?) Soundness: if the algorithm succeeds, it returns a unifier (that is, if there is no unifier, it fails) Corollary: if C has a unifier, then (i)An mgu exists (ii)The algorithm returns an mgu (Which of the above may fail for a domain not freely generated?)

26 Catriel Beeri Pls/Winter 2004/5 type reconstruction 26 Now, back to a type reconstruction algorithm: The standard type checking algorithm is modified: Top-down: Guess fresh type variables for each lambda-bound variable Bottom-up: –Guess fresh type variable for each free use –Climb up the expression tree from the leaves, solve constraints using unification, whenever sub-trees are merged (collecting equations and solving at the end is cheaper, but this formulation is easier to understand – is it?)

27 Catriel Beeri Pls/Winter 2004/5 type reconstruction 27

28 Catriel Beeri Pls/Winter 2004/5 type reconstruction 28 The application case involves guessing a range type The other cases (if, let) are left to you How would the algorithm look like if equations were collected to the end?

29 Catriel Beeri Pls/Winter 2004/5 type reconstruction 29 The guesses at the top down phase are redundant: Since we unify at nodes on the way up in the bottom up phase, no need to make a guess for lambda when going down; can make the guesses at the leaves Here is the modified case for lambda: The else case – when x does not occur in the body

30 Catriel Beeri Pls/Winter 2004/5 type reconstruction 30 When e is closed H is empty, we have a principal type

31 Catriel Beeri Pls/Winter 2004/5 type reconstruction 31 Proposition: The algorithm tRecon Always terminates Fails iff there is no typing If it returns a typing, then it is principal Works in poly time (can be implemented in linear time) All is well that ends well But, is it really the end?

32 Catriel Beeri Pls/Winter 2004/5 type reconstruction 32 Two ideas are used in type reconstruction: Type constraints are local: each originates in the merging of sub-trees at a proof tree node The solution of each constraint applies globally: all occurrences of a type variable are substituted We examine the necessity and consequences of these via the concept of polymorphic function These are the main reason we are in this business !

33 Catriel Beeri Pls/Winter 2004/5 type reconstruction 33 Polymorphic functions:  The input type represents the intersection of the sets of (poly)types that are compatible with the operations on the parameter in the body (I.e. the set of types that are compatible with all these operations) If contains type vars – every instantiation is ok for the body

34 Catriel Beeri Pls/Winter 2004/5 type reconstruction 34

35 Catriel Beeri Pls/Winter 2004/5 type reconstruction 35 A consequence: We have polymorphic types, how nice! BUT We cannot use polymorphic values polymorphically I.e., use the same value in the same program/expression, in different places, that require different types Thus, we cannot use the function polymorphically The argument applies to all scenarios using polymorphic functions

36 Catriel Beeri Pls/Winter 2004/5 type reconstruction 36 The point: In the bottom-up phase, when sub-trees are merged, their environments are unioned, and types for same variables forced to be equal  a function value cannot have conflicting types such as  differently typed incompatible uses of a function parameter lead to a failure! (and same for let) What shall we do now?

37 Catriel Beeri Pls/Winter 2004/5 type reconstruction 37 when the function is applied, f may be replaced by any instance of its inferred type If we decide it is then both Each creates a type error in the body  f’s param type represents a set of mono types ; each must be compatible with all uses of param  Let polymorphism (due to Robin Milner):

38 Catriel Beeri Pls/Winter 2004/5 type reconstruction 38 f in the let is associated now with a value that is known to be a poly function that can be applied to either a bool or an int If we type it by no type error will occur in the evaluation of the body  We would like the type for f to mean that its actual value is indeed a poly value of this type, not of an instance type -- hence not to instantiate it by all the constraints in the body together

39 Catriel Beeri Pls/Winter 2004/5 type reconstruction 39 Now, we need to use type variables in two different meanings, as “Mono-types” -- all uses are same type Poly-types different uses are unrelated If we have notation for that, we can use let for real polymorphism

40 Catriel Beeri Pls/Winter 2004/5 type reconstruction 40 Notation: Note that U2 contains U1 A free type var – a mono type var A bound type var – a poly type var

41 Catriel Beeri Pls/Winter 2004/5 type reconstruction 41 New type rules (poly types in red) First part: (if and tuple are like applic –omitted) A problem: How can we generate/use poly-types?

42 Catriel Beeri Pls/Winter 2004/5 type reconstruction 42 Second part: First rule allows to generate a polytype for a variable defined in a let Second rule allows to use it in the body In algorithm, 2nd rule used thus: replace t by a fresh type var

43 Catriel Beeri Pls/Winter 2004/5 type reconstruction 43

44 Catriel Beeri Pls/Winter 2004/5 type reconstruction 44 Type reconstruction succeeds: bool  int

45 Catriel Beeri Pls/Winter 2004/5 type reconstruction 45 What would happen in previous example, the second branch was #1 f(x) (rather than 5)? An issue: how to ensure that the type of x is the same across the expression?

46 Catriel Beeri Pls/Winter 2004/5 type reconstruction 46 Solution: When the type for f in is reconstructed, return for f both type and type environment This, in particular, tell us that is mono and keeps it as the type of x (when H is empty, this reduces to previous case) Replace input H in tRecon by A --- a set of such bindings; change the algorithm to deal with such A’s

47 Catriel Beeri Pls/Winter 2004/5 type reconstruction 47

48 Catriel Beeri Pls/Winter 2004/5 type reconstruction 48

49 Catriel Beeri Pls/Winter 2004/5 type reconstruction 49 Type reconstruction fails

50 Catriel Beeri Pls/Winter 2004/5 type reconstruction 50  Explicit parametric polymorphism what is the explicit parametric type system of ML? (if programmer did declare type for variables, how would these look like?) There is more than one answer! All contain: functions with type arguments: Application to types

51 Catriel Beeri Pls/Winter 2004/5 type reconstruction 51 Example: end


Download ppt "Catriel Beeri Pls/Winter 2004/5 type reconstruction 1 Type Reconstruction & Parametric Polymorphism  Introduction  Unification and type reconstruction."

Similar presentations


Ads by Google