1 / 10

ML’s Type Inference and Polymorphism

ML’s Type Inference and Polymorphism. Examples of Type Inference Polymorphic Type Schemata Overloaded Operators Type Inference as Constraint Satisfaction Unification. Examples of Type Inference in Function Definitions. Full declaration of argument and return types by the programmer:

spence
Télécharger la présentation

ML’s Type Inference and 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. ML’s Type Inference and Polymorphism • Examples of Type Inference • Polymorphic Type Schemata • Overloaded Operators • Type Inference as Constraint Satisfaction • Unification CSE 341 -- S. Tanimoto Type Inference and Polymorphism

  2. Examples of Type Inference in Function Definitions Full declaration of argument and return types by the programmer: fun foo(x:int):int = x + 100; val fun = fn : int -> int Inference from the type of one operand: fun foo(x) = x + 100; val foo = fn : int -> int Inference from the return type: fun foo2(x,y):real = x + (y * y); val foo2 = fn : real * real -> real CSE 341 -- S. Tanimoto Type Inference and Polymorphism

  3. More Examples(using an alternative syntax for function definition) Full declaration of argument and return types by the programmer: val f = fn(x) => x + 1; val f = fn : int -> int Inference from the type of one operand: val g = fn(x) => x + 1.0; val f = fn : real -> real Inference from the return type: val h = fn(x) => ord(x); val h = fn : char -> int CSE 341 -- S. Tanimoto Type Inference and Polymorphism

  4. A Polymorphic Function fun ident(x) = x; val ident = fn : 'a -> 'a ident("Boo!"); val it = "Boo!" : string ident(abs); val it = fn : int -> int abs(~23); val it = 23 : int CSE 341 -- S. Tanimoto Type Inference and Polymorphism

  5. Polymorphism with Two Type Variables fun swap(x,y) = (y,x); val swap = fn : 'a * 'b -> 'b * 'a swap(1, "One"); val it = ("One", 1) : string * int swap(2.0, 3.0); val it = (3.0, 2.0) : real * real CSE 341 -- S. Tanimoto Type Inference and Polymorphism

  6. Generalized Types vs Overloaded Operators The operators +, *, -, <, and some others are overloaded. They represent one of two possible operations. E.g., int addition or real addition. The ambiguity must be resolved before the val can be returned. A function is a value and forces the resolution of the ambiguity. fun sqr(x) = x * x; val sqr = fn : int -> int sqr(3.3); Error CSE 341 -- S. Tanimoto Type Inference and Polymorphism

  7. Type Inference as Constraint Satisfaction Each use of a value or function may impose a constraint on the type of an expression that includes it or that is an argument to it. By propagating constraints, it is sometimes possible to establish a unique type for every expression that occurs in some larger expression. CSE 341 -- S. Tanimoto Type Inference and Polymorphism

  8. Constraint Satisfaction Outcomes 1. If the type of any expression is overconstrained, then there is an inconsistency in the constraints. There is a type error. 2. If the constraints produce a unique concrete type for each expression or subexpression, then type inference is successful. 3. If there are not enough constraints to force a concrete type for each subexpression, then the problem is underconstrained, and the ML compiler may attempt to represent the type of each subexpression with a type schema that is as general as possible. E.g. x : 'a CSE 341 -- S. Tanimoto Type Inference and Polymorphism

  9. Constraint Satisfaction Process In order to propagate the type constraints, the ML compiler uses a form of "unification" which is a specialized kind of pattern matching. E.g., if the type of x is recorded as 'a in one part of an expression and as real in another part of the expression, then real wins out (being more restrictive). Anything else in the same expression that had the type 'a must now also be real. We will study unification when we encounter the PROLOG language. CSE 341 -- S. Tanimoto Type Inference and Polymorphism

  10. What Good is Type Inference? 1. ML enforces type consistency in programs, much as is doen in languages like Pascal and ADA. This makes it much less likely that a runnable program will contain an error having to do with unnoticed type conversions. 2. ML takes most of the drudgery out of declaring types, since most of it is handled automatically. CSE 341 -- S. Tanimoto Type Inference and Polymorphism

More Related