510 likes | 660 Views
Type Reconstruction & Parametric Polymorphism. Introduction Unification and type reconstruction Let polymorphism Explicit parametric polymorphism. Introduction. Type reconstruction (inference) : Given e w/o type declarations, find: e’ with type declarations
E N D
Type Reconstruction & Parametric Polymorphism • Introduction • Unification and type reconstruction • Let polymorphism • Explicit parametric polymorphism type reconstruction
Introduction Type reconstruction (inference): Given ew/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’) type reconstruction
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 type reconstruction
Parametric polymorphism: based on the use of type variables A ground type: no type variables type reconstruction
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. type reconstruction
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 type reconstruction
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 type reconstruction
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? type reconstruction
Example: What is the type of We guess: then specialize to satisfy the constraint in the (applic)rule: type reconstruction
Substitutions, type constraints, unification type substitution: a function from type variables to types Notation: Application of a substitution: type reconstruction
A substitution can be applied also to type environments, sets of types, typed expressions type reconstruction
Composition of substitutions: Example: type reconstruction
The equivalence classes: same substitution modulo variable renaming (actual variables used are irrelevant) type reconstruction
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. type reconstruction
Some type rules impose equality constraints (type equations) on the types used in them Examples: Are there constraints in other rules? type reconstruction
How do we solve a set of type equations? Solving equations of expressions with variables, constants and constructors is unification Solving one equation orseveral --- equivalent problems (introduce a dummy root) Most general unifier, mgu --- more/as general as any other. Does one exist? Is it unique? type reconstruction
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 type reconstruction
Examples: (An equation is represented as a pair of types) type reconstruction
Comments: (iii) The algorithm assumes a freely generated domain Where? (iv) The algorithm runs in poly time (can be madelinear) type reconstruction
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 • An mgu exists • The algorithm returns an mgu (Which of the above may fail for a domain not freely generated?) type reconstruction
Now, back to a type reconstruction algorithm: The standard type checking algorithm is modified: • Top-down: Guess fresh type variables for each lambda-boundvariable • 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?) type reconstruction
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? type reconstruction
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 type reconstruction
When e is closed H is empty, we have a principal type type reconstruction
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? type reconstruction
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 ! type reconstruction
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 theseoperations) If contains type vars – every instantiation is ok for the body type reconstruction
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 type reconstruction
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? type reconstruction
Let polymorphism (due to Robin Milner): 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 type reconstruction
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 type reconstruction
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 type reconstruction
Notation: Note that U2 contains U1 A free type var – a mono type var A bound type var – a poly type var type reconstruction
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? type reconstruction
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 type reconstruction
Type reconstruction succeeds: bool int type reconstruction
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? type reconstruction
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 type reconstruction
Type reconstruction fails type reconstruction
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 type reconstruction