ML’s Type Inference and Polymorphism

Slides:



Advertisements
Similar presentations
Functional Programming Lecture 10 - type checking.
Advertisements

Kathleen Fisher cs242 Reading: “Concepts in Programming Languages”, Chapter 6.
ML Datatypes.1 Standard ML Data types. ML Datatypes.2 Concrete Datatypes  The datatype declaration creates new types  These are concrete data types,
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Patterns in ML functions. Formal vs. actual parameters Here's a function definition (in C): –int add (int x, int y) { return x + y; } –x and y are the.
INF 212 ANALYSIS OF PROG. LANGS Type Systems Instructors: Crista Lopes Copyright © Instructors.
CSE 341, Winter Type Systems Terms to learn about types: –Type –Type system –Statically typed language –Dynamically typed language –Type error –Strongly.
COEN Expressions and Assignment
Type Checking.
Compiler Construction
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Type Checking- Contd Compiler Design Lecture (03/02/98) Computer Science Rensselaer Polytechnic.
CSE 130 : Winter 2006 Programming Languages Ranjit Jhala UC San Diego Lecture 7: Polymorphism.
CSE S. Tanimoto Syntax and Types 1 Representation, Syntax, Paradigms, Types Representation Formal Syntax Paradigms Data Types Type Inference.
Type Checking  Legality checks  Operator determination  Overload resolution.
1 Functional Programming and ML. 2 What’s wrong with Imperative Languages? State State Introduces context sensitivity Introduces context sensitivity Harder.
CSE341: Programming Languages Lecture 11 Type Inference Dan Grossman Winter 2013.
CSE 425: Data Types II Survey of Common Types I Records –E.g., structs in C++ –If elements are named, a record is projected into its fields (e.g., via.
ML Datatypes.1 Standard ML Data types. ML Datatypes.2 Concrete Datatypes  The datatype declaration creates new types  These are concrete data types,
Patterns in OCaml functions. Formal vs. actual parameters Here's a function definition (in C): –int add (int x, int y) { return x + y; } –x and y are.
A Third Look At ML Chapter NineModern Programming Languages, 2nd ed.1.
PZ06C Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ06C - Polymorphism Programming Language Design and.
Polymorphism Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 7.3.
Homogeneous tuples What are they? –S 2 = S x S –S n = S x S x … x S Cardinalities –#(S 2 )= (#S) 2 –#(S n )= (#S) n –#(S 0 )= (#S) 0 =1 What is S 0 ? –It.
1 Static Checking and Type Systems Chapter 6 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
Static Checking and Type Systems Chapter 6. 2 Static versus Dynamic Checking Static checking: the compiler enforces programming language’s static semantics.
 Templates enable us to define generic classes and functions and thus provides support for generic programming. Generic types are used as parameters.
Hindley-Milner Type Inference CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
Cs776(Prasad)L6sml971 SML-97 Specifics SML/NJ 110.
Types John Mitchell CS 242. Type A type is a collection of computable values that share some structural property. uExamples Integers Strings int  bool.
Heath Carroll Bill Hanczaryk Rich Porter.  A Theory of Type Polymorphism in Programming ◦ Robin Milner (1977)  Milner credited with introducing the.
Type Checking and Type Inference
Programming Languages and Compilers (CS 421)
CSE341: Programming Languages Lecture 11 Type Inference
Principles of programming languages 12: Functional programming
Semantic Analysis Type Checking
Type Checking Generalizes the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the.
ML: a quasi-functional language with strong typing
Representation, Syntax, Paradigms, Types
Type Definitions cs776 (prasad) L8tdef.
Type Systems Terms to learn about types: Related concepts: Type
CSE341: Programming Languages Lecture 11 Type Inference
Representation, Syntax, Paradigms, Types
CSE S. Tanimoto Introduction to ML
CSE341: Programming Languages Lecture 11 Type Inference
ML’s Type Inference and Polymorphism
CSE-321 Programming Languages Introduction to Functional Programming
Representation, Syntax, Paradigms, Types
CSE S. Tanimoto Introduction to ML
ML’s Type Inference and Polymorphism
Representation, Syntax, Paradigms, Types
Compiler Construction
Polymorphism Programming Language Design and Implementation
CSE341: Programming Languages Lecture 11 Type Inference
CS 242 Types John Mitchell.
CS 242 Types John Mitchell Reading: Chapter 6.
CSE S. Tanimoto Introduction to ML
ML’s Type Inference and Polymorphism
Type Systems Terms to learn about types: Related concepts: Type
Polymorphism Programming Language Design and Implementation
CSE S. Tanimoto Introduction to ML
Modern Programming Languages
CSE341: Programming Languages Lecture 11 Type Inference
Polymorphism Programming Language Design and Implementation
Compiler Construction
CSE341: Programming Languages Lecture 11 Type Inference
Polymorphism Programming Language Design and Implementation
Presentation transcript:

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

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

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

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

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

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

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

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

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

What Good is Type Inference? 1. ML enforces type consistency in programs, much as is done 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