1 / 51

Type Reconstruction & Parametric Polymorphism

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

olin
Download Presentation

Type Reconstruction & Parametric Polymorphism

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. Type Reconstruction & Parametric Polymorphism • Introduction • Unification and type reconstruction • Let polymorphism • Explicit parametric polymorphism type reconstruction

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

  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 type reconstruction

  4. Parametric polymorphism: based on the use of type variables A ground type: no type variables 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. 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 type reconstruction

  7. 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 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? type reconstruction

  10. Example: What is the type of We guess: then specialize to satisfy the constraint in the (applic)rule: type reconstruction

  11. Substitutions, type constraints, unification type substitution: a function from type variables to types Notation: Application of a substitution: type reconstruction

  12. A substitution can be applied also to type environments, sets of types, typed expressions type reconstruction

  13. Composition of substitutions: Example: type reconstruction

  14. The equivalence classes: same substitution modulo variable renaming (actual variables used are irrelevant) 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. type reconstruction

  16. Some type rules impose equality constraints (type equations) on the types used in them Examples: Are there constraints in other rules? 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 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

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

  20. type reconstruction

  21. type reconstruction

  22. Examples: (An equation is represented as a pair of types) type reconstruction

  23. type reconstruction

  24. Comments: (iii) The algorithm assumes a freely generated domain Where? (iv) The algorithm runs in poly time (can be madelinear) 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 • An mgu exists • The algorithm returns an mgu (Which of the above may fail for a domain not freely generated?) 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-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

  27. 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? 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 type reconstruction

  30. When e is closed H is empty, we have a principal type 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? 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 ! 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 theseoperations) If contains type vars – every instantiation is ok for the body type reconstruction

  34. 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 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? type reconstruction

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

  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 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 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 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? 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 type reconstruction

  43. type reconstruction

  44. Type reconstruction succeeds: bool  int 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? 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 type reconstruction

  47. type reconstruction

  48. type reconstruction

  49. Type reconstruction fails 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 type reconstruction

More Related