Hindley-Milner Type Inference CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University

Slides:



Advertisements
Similar presentations
Chapter 6 Type Checking. The compiler should report an error if an operator is applied to an incompatible operand. Type checking can be performed without.
Advertisements

Programming Languages and Paradigms The C Programming Language.
Lecture # 21 Chapter 6 Uptill 6.4. Type System A type system is a collection of rules for assigning type expressions to the various parts of the program.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
Type checking © Marcelo d’Amorim 2010.
Types, Variables and Operators Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2013.
INF 212 ANALYSIS OF PROG. LANGS Type Systems Instructors: Crista Lopes Copyright © Instructors.
Chapter 5: Elementary Data Types Properties of types and objects –Data objects, variables and constants –Data types –Declarations –Type checking –Assignment.
Type Checking.
Compiler Construction
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming;
Lecture 27 Exam outline Boxing of primitive types in Java 1.5 Generic types in Java 1.5.
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.
Comp 205: Comparative Programming Languages User-Defined Types Enumerated types Parameterised types Recursive types Lecture notes, exercises, etc., can.
Advanced Programming Handout 9 Qualified Types (SOE Chapter 12)
Introduction to Computers and Programming Lecture 5 Boolean type; if statement Professor: Evan Korth New York University.
1 Type Type system for a programming language = –set of types AND – rules that specify how a typed program is allowed to behave Why? –to generate better.
Semantics for MinML COS 441 Princeton University Fall 2004.
Type Inference: CIS Seminar, 11/3/2009 Type inference: Inside the Type Checker. A presentation by: Daniel Tuck.
Chapter 12 Qualified Types. Motivation  What should the principal type of (+) be? Int -> Int -> Int-- too specific a -> a -> a-- too general  It seems.
CSE341: Programming Languages Lecture 11 Type Inference Dan Grossman Winter 2013.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
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.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Semantics CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
Java Structure import java_packages; class JavaClass { member variables declarations; void otherMethod( ) { } public static void main(String[]
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
CSE 425: Data Types I Data and Data Types Data may be more abstract than their representation –E.g., integer (unbounded) vs. 64-bit int (bounded) A language.
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L06-1 September 26, 2006http:// Type Inference September.
12/9/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
RUBY by Ryan Chase.
1 Static Checking and Type Systems Chapter 6 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
Lambda Calculus CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Programmeren 1 6 september 2010 HOORCOLLEGE 2: INTERACTIE EN CONDITIES PROGRAMMEREN 1 6 SEPTEMBER 2009 Software Systems - Programming - Week.
Type Systems CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
Data Types (3) 1 Programming Languages – Principles and Practice by Kenneth C Louden.
A Introduction to Computing II Lecture 1: Java Review Fall Session 2000.
 Array ◦ Single & Multi-dimensional  Java Operators ◦ Assignment ◦ Arithmetic ◦ Relational ◦ Logical ◦ Bitwise & other.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Current Assignments Project 3 has been posted, due next Tuesday. Write a contact manager. Homework 6 will be posted this afternoon and will be due Friday.
Georgia Institute of Technology More on Creating Classes Barb Ericson Georgia Institute of Technology June 2006.
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L05-1 September 21, 2006http:// Types and Simple Type.
Dr. M. Al-Mulhem Introduction 1 Chapter 6 Type Systems.
Types Type Errors Static and Dynamic Typing Basic Types NonBasic Types
Type Checking and Type Inference
CSE341: Programming Languages Lecture 11 Type Inference
Principles of programming languages 12: Functional programming
Semantic Analysis Type Checking
Data Types.
Chapter 4: Types.
Java so far Week 7.
CSE341: Programming Languages Lecture 11 Type Inference
Type Systems CSE 340 – Principles of Programming Languages Fall 2016
CSE341: Programming Languages Lecture 11 Type Inference
ML’s Type Inference and Polymorphism
ML’s Type Inference and Polymorphism
Compiler Construction
CSE341: Programming Languages Lecture 11 Type Inference
CS 242 Types John Mitchell.
CS 242 Types John Mitchell Reading: Chapter 6.
ML’s Type Inference and Polymorphism
ML’s Type Inference and Polymorphism
CSE341: Programming Languages Lecture 11 Type Inference
PROGRAMMING IN HASKELL
CSE341: Programming Languages Lecture 11 Type Inference
Presentation transcript:

Hindley-Milner Type Inference CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University

Adam Doupé, Principles of Programming Languages Type Systems In what we have seen so far, the programmer must declare the types of the variables array [0..5] of int a; int i; a[i] = 1; 2

Adam Doupé, Principles of Programming Languages Type Systems In what we have seen so far, the programmer must declare the types of the variables array [0..5] of int a; string i; a[i] = 1; 3

Adam Doupé, Principles of Programming Languages Type Systems In what we have seen so far, the programmer must declare the types of the variables array [0..5] of int a; int i; a[i] = "testing"; 4

Adam Doupé, Principles of Programming Languages Parameterized Types Some languages allow the programmer to declare parameterized types –Instead of being specific to a given type, the specific type is given as a parameter Generics in Java and C#, templates in C++ 5

Adam Doupé, Principles of Programming Languages import java.util.Random; public class Chooser{ static Random rand = new Random(); public static T choose(T first, T second) { return ((rand.nextInt() % 2) == 0)? first: second; } } class ParameterizedTypes{ public static void main(String [] args) { int x = 100; int y = 999; System.out.println(Chooser.choose(x, y)); String a = "foo"; String b = "bar"; System.out.println(Chooser.choose(a, b)); } } 6

Adam Doupé, Principles of Programming Languages Explicit Polymorphism Note that in the previous example, the programmer must declare the parameterized types explicitly Slightly different polymorphism than what is used in the object orientation context The compiler/interpreter will allow a function to be called with different types (while still checking for type compatibility) 7

Adam Doupé, Principles of Programming Languages Implicit Polymorphism The programmer does not need to specify the type parameters explicitly –Dynamic languages have this property too However, the type checker will, statically, attempt to assign the most general type to every construct in the program 8

Adam Doupé, Principles of Programming Languages Implicit Polymorphism fun foo(x) = x What is the type of foo? –Function of T returns T –(T) -> T fun foo(x) = x; fun bar(y) = foo(y); What is the type of bar and foo? –foo: Function of T returns T (T) -> T –bar: Function of T returns T (T) -> T 9

Adam Doupé, Principles of Programming Languages Implicit Polymorphism fun max(x, y) = if x < y then y else x What is the type of max? –Function of (int, int) returns int –(int,int) -> int 10

Adam Doupé, Principles of Programming Languages Implicit Polymorphism fun max(cmp, x, y) = if cmp(x,y) then y else x What is the type of max? –Function of (Function of (T, T) returns bool, T, T) returns T –((T, T) -> bool, T, T) -> T max(<, 10, 200) max(strcmp, "foo", "bar") 11

Adam Doupé, Principles of Programming Languages Implicit Polymorphism fun foo(a, b, c) = c(a[b]) What is the type of foo? –Function of (Array of T, int, Function of (T) returns U) returns U –(Array of T, int, (T -> U)) -> U 12

Adam Doupé, Principles of Programming Languages Implicit Polymorphism fun foo(a, b, c) = a = 10; a(b[c]); What is the type of foo? –Type error! 13

Adam Doupé, Principles of Programming Languages Hindley-Milner Type Checking Hindley-Milner type checking is a general type inference approach –It infers the types of constructs that are not explicitly declared –It leverages the constraints of the various constructs –It applies these constraints together with type unification to find the most general type for each construct (or can find a type error if there is one) Full Hindley-Milner type checking is used in OCaml, F#, and Haskell 14

Adam Doupé, Principles of Programming Languages Type Constraints To apply Hindley-Milner, we must first define the type constraints Constant integers –…, -1, 0, 1, 2,... –Type = int Constant real numbers –..., 0.1, 2.2,... other floating point numbers –Type = real Constant booleans –true or false –Type = boolean Constant strings –"foo", "bar",... –Type = string 15

Adam Doupé, Principles of Programming Languages Operators Relational Operators a op b op is, >=, !=, == T 1 = boolean T 2 = T 3 = numeric type 16 (T 1 ) op (T 2 ) a (T 3 ) b

Adam Doupé, Principles of Programming Languages Operators Arithmetic Operators a op b op is +, -, *, / T 1 = T 2 = T 3 = numeric type 17 (T 1 ) op (T 2 ) a (T 3 ) b

Adam Doupé, Principles of Programming Languages Operators Array Access Operator a[b] T 2 = array of T 1 T 3 = int 18 (T 1 ) [] (T 2 ) a (T 3 ) b

Adam Doupé, Principles of Programming Languages Function Application foo(x 1, x 2, …, x k ) F = (T 1, T 2, …, T k ) -> R 19 (R) apply (F) foo (T 1 ) x 1 (T 2 ) x 2 (T k ) x k …

Adam Doupé, Principles of Programming Languages Function Definition fun foo(x 1, x 2, …, x k ) = expr F = (T 1, T 2, …, T k ) -> E 20 fun F T 1, T 2, …, T k foo (x 1, x 2, …, x k ) (E) expr

Adam Doupé, Principles of Programming Languages If Expression if (cond) then expr 1 else expr 2 T 1 = boolean T 2 = T 3 = T 4 21 (T4) if (T1) cond (T 2 ) expr 1 (T 3 ) expr 2

Adam Doupé, Principles of Programming Languages Type Unification Type unification is the process by which the constraints are propagated Basic idea is simple –Start from the top of the tree –Every time you see a construct with unconstrained types, create a new type –If a construct is found to have type T 1 and also to have type T 2, then T 1 and T 2 must be the same type 22

Adam Doupé, Principles of Programming Languages fun foo(a, b, c) = c(a[b]) 23 (1) def foo (a, b, c) (2) apply (3) c (4) [] (5) a (6) b foo a b c (1) (2) (3) (4) (5) (6)

Adam Doupé, Principles of Programming Languages fun foo(a, b, c) = c(a[b]) 24 (1) def foo (a, b, c) (2) apply (3) c (4) [] (5) a (6) b foo aT1T1 bT2T2 cT3T3 (1) (2) (3) (4) (5) (6)

Adam Doupé, Principles of Programming Languages fun foo(a, b, c) = c(a[b]) 25 (1) def foo (a, b, c) (2) apply (3) c (4) [] (5) a (6) b foo(T 1,T 2,T 3 ) -> T 4 aT1T1 bT2T2 cT3T3 (1) (2) (3) (4) (5) (6)

Adam Doupé, Principles of Programming Languages fun foo(a, b, c) = c(a[b]) 26 (1) def foo (a, b, c) (2) apply (3) c (4) [] (5) a (6) b foo(T 1,T 2,T 3 ) -> T 4 aT1T1 bT2T2 cT3T3 (1) (2)T4T4 (3) (4) (5) (6)

Adam Doupé, Principles of Programming Languages fun foo(a, b, c) = c(a[b]) 27 (1) def foo (a, b, c) (2) apply (3) c (4) [] (5) a (6) b foo(T 1,T 2,T 3 ) -> T 4 aT1T1 bT2T2 cT3T3 (1) (2)T4T4 (3) (4)T5T5 (5) (6)

Adam Doupé, Principles of Programming Languages fun foo(a, b, c) = c(a[b]) 28 (1) def foo (a, b, c) (2) apply (3) c (4) [] (5) a (6) b foo(T 1,T 2,T 3 ) -> T 4 aT1T1 bT2T2 cT3T3 (1) (2)T4T4 (3)T 5 -> T 4 (4)T5T5 (5) (6)

Adam Doupé, Principles of Programming Languages fun foo(a, b, c) = c(a[b]) 29 (1) def foo (a, b, c) (2) apply (3) c (4) [] (5) a (6) b foo(T 1,T 2,T 3 ) -> T 4 aT1T1 bT2T2 cT 5 -> T 4 (1) (2)T4T4 (3)T 5 -> T 4 (4)T5T5 (5) (6)

Adam Doupé, Principles of Programming Languages fun foo(a, b, c) = c(a[b]) 30 (1) def foo (a, b, c) (2) apply (3) c (4) [] (5) a (6) b foo(T 1,T 2,(T 5 ->T 4 )) -> T 4 aT1T1 bT2T2 cT 5 -> T 4 (1) (2)T4T4 (3)T 5 -> T 4 (4)T5T5 (5) (6)

Adam Doupé, Principles of Programming Languages fun foo(a, b, c) = c(a[b]) 31 (1) def foo (a, b, c) (2) apply (3) c (4) [] (5) a (6) b foo(T 1,T 2,(T 5 ->T 4 )) -> T 4 aT1T1 bT2T2 cT 5 -> T 4 (1) (2)T4T4 (3)T 5 -> T 4 (4)T5T5 (5)Array of T 5 (6)

Adam Doupé, Principles of Programming Languages fun foo(a, b, c) = c(a[b]) 32 (1) def foo (a, b, c) (2) apply (3) c (4) [] (5) a (6) b foo(T 1,T 2,(T 5 ->T 4 )) -> T 4 aT1T1 bT2T2 cT 5 -> T 4 (1) (2)T4T4 (3)T 5 -> T 4 (4)T5T5 (5)Array of T 5 (6)int

Adam Doupé, Principles of Programming Languages fun foo(a, b, c) = c(a[b]) 33 (1) def foo (a, b, c) (2) apply (3) c (4) [] (5) a (6) b foo(Array of T 5,T 2,(T 5 - >T 4 )) -> T 4 aArray of T 5 bT2T2 cT 5 -> T 4 (1) (2)T4T4 (3)T 5 -> T 4 (4)T5T5 (5)Array of T 5 (6)int

Adam Doupé, Principles of Programming Languages fun foo(a, b, c) = c(a[b]) 34 (1) def foo (a, b, c) (2) apply (3) c (4) [] (5) a (6) b foo(Array of T 5, T 2,(T 5 ->T 4 )) -> T 4 aArray of T 5 bint cT 5 -> T 4 (1) (2)T4T4 (3)T 5 -> T 4 (4)T5T5 (5)Array of T 5 (6)int

Adam Doupé, Principles of Programming Languages fun foo(a, b, c) = c(a[b]) 35 (1) def foo (a, b, c) (2) apply (3) c (4) [] (5) a (6) b foo(Array of T 5, int,(T 5 ->T 4 )) -> T 4 aArray of T 5 bint cT 5 -> T 4 (1) (2)T4T4 (3)T 5 -> T 4 (4)T5T5 (5)Array of T 5 (6)int