hindley-milner type inference cse 340 principles of programming languages fall 2015 adam doup...

35
Hindley-Milner Type Inference CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University http://adamdoupe.com

Upload: anthony-stone

Post on 19-Jan-2018

223 views

Category:

Documents


0 download

DESCRIPTION

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

TRANSCRIPT

Page 1: Hindley-Milner Type Inference CSE 340  Principles of Programming Languages Fall 2015 Adam Doup Arizona State University

Hindley-Milner Type Inference

CSE 340 – Principles of Programming LanguagesFall 2015

Adam DoupéArizona State Universityhttp://adamdoupe.com

Page 2: Hindley-Milner Type Inference CSE 340  Principles of Programming Languages Fall 2015 Adam Doup Arizona State University

2Adam 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;

Page 3: Hindley-Milner Type Inference CSE 340  Principles of Programming Languages Fall 2015 Adam Doup Arizona State University

3Adam 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;

Page 4: Hindley-Milner Type Inference CSE 340  Principles of Programming Languages Fall 2015 Adam Doup Arizona State University

4Adam 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";

Page 5: Hindley-Milner Type Inference CSE 340  Principles of Programming Languages Fall 2015 Adam Doup Arizona State University

5Adam 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+

+

Page 6: Hindley-Milner Type Inference CSE 340  Principles of Programming Languages Fall 2015 Adam Doup Arizona State University

6Adam Doupé, Principles of Programming Languages

import java.util.Random;public class Chooser{ static Random rand = new Random(); public static <T> 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)); }}

Page 7: Hindley-Milner Type Inference CSE 340  Principles of Programming Languages Fall 2015 Adam Doup Arizona State University

7Adam 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)

Page 8: Hindley-Milner Type Inference CSE 340  Principles of Programming Languages Fall 2015 Adam Doup Arizona State University

8Adam 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

Page 9: Hindley-Milner Type Inference CSE 340  Principles of Programming Languages Fall 2015 Adam Doup Arizona State University

9Adam Doupé, Principles of Programming Languages

Implicit Polymorphismfun 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

Page 10: Hindley-Milner Type Inference CSE 340  Principles of Programming Languages Fall 2015 Adam Doup Arizona State University

10Adam Doupé, Principles of Programming Languages

Implicit Polymorphism

fun max(x, y) = if x < y thenyelsex

• What is the type of max?– Function of (int, int) returns int– (int,int) -> int

Page 11: Hindley-Milner Type Inference CSE 340  Principles of Programming Languages Fall 2015 Adam Doup Arizona State University

11Adam Doupé, Principles of Programming Languages

Implicit Polymorphismfun max(cmp, x, y)

= if cmp(x,y) thenyelsex

• 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")

Page 12: Hindley-Milner Type Inference CSE 340  Principles of Programming Languages Fall 2015 Adam Doup Arizona State University

12Adam 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

Page 13: Hindley-Milner Type Inference CSE 340  Principles of Programming Languages Fall 2015 Adam Doup Arizona State University

13Adam 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!

Page 14: Hindley-Milner Type Inference CSE 340  Principles of Programming Languages Fall 2015 Adam Doup Arizona State University

14Adam 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

Page 15: Hindley-Milner Type Inference CSE 340  Principles of Programming Languages Fall 2015 Adam Doup Arizona State University

15Adam 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

Page 16: Hindley-Milner Type Inference CSE 340  Principles of Programming Languages Fall 2015 Adam Doup Arizona State University

16Adam Doupé, Principles of Programming Languages

Operators

• Relational Operators a op b

• op is <, <=, >, >=, !=, ==• T1 = boolean• T2 = T3 = numeric type

(T1)op

(T2)a

(T3)b

Page 17: Hindley-Milner Type Inference CSE 340  Principles of Programming Languages Fall 2015 Adam Doup Arizona State University

17Adam Doupé, Principles of Programming Languages

Operators

• Arithmetic Operators a op b

• op is +, -, *, /• T1 = T2 = T3 = numeric type

(T1)op

(T2)a

(T3)b

Page 18: Hindley-Milner Type Inference CSE 340  Principles of Programming Languages Fall 2015 Adam Doup Arizona State University

18Adam Doupé, Principles of Programming Languages

Operators

• Array Access Operator a[b]

• T2 = array of T1

• T3 = int

(T1)[]

(T2)a

(T3)b

Page 19: Hindley-Milner Type Inference CSE 340  Principles of Programming Languages Fall 2015 Adam Doup Arizona State University

19Adam Doupé, Principles of Programming Languages

Function Application

• foo(x1, x2, …, xk)

• F = (T1, T2, …, Tk) -> R

(R)apply

(F)foo

(T1)x1

(T2)x2

(Tk)xk

Page 20: Hindley-Milner Type Inference CSE 340  Principles of Programming Languages Fall 2015 Adam Doup Arizona State University

20Adam Doupé, Principles of Programming Languages

Function Definition

• fun foo(x1, x2, …, xk) = expr

• F = (T1, T2, …, Tk) -> E

fun

F T1, T2, …, Tk

foo (x1, x2, …, xk)

(E)expr

Page 21: Hindley-Milner Type Inference CSE 340  Principles of Programming Languages Fall 2015 Adam Doup Arizona State University

21Adam Doupé, Principles of Programming Languages

If Expression

• if (cond) then expr1 else expr2

• T1 = boolean• T2 = T3 = T4

(T4)if

(T1)cond

(T2)expr

1

(T3)expr

2

Page 22: Hindley-Milner Type Inference CSE 340  Principles of Programming Languages Fall 2015 Adam Doup Arizona State University

22Adam 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 T1 and also

to have type T2, then T1 and T2 must be the same type

Page 23: Hindley-Milner Type Inference CSE 340  Principles of Programming Languages Fall 2015 Adam Doup Arizona State University

23Adam Doupé, Principles of Programming Languages

fun foo(a, b, c) = c(a[b])(1)def

foo (a, b, c)(2)apply

(3)c

(4)[]

(5)a

(6)b

fooabc(1)(2)(3)(4)(5)(6)

Page 24: Hindley-Milner Type Inference CSE 340  Principles of Programming Languages Fall 2015 Adam Doup Arizona State University

24Adam Doupé, Principles of Programming Languages

fun foo(a, b, c) = c(a[b])(1)def

foo (a, b, c)(2)apply

(3)c

(4)[]

(5)a

(6)b

fooa T1

b T2

c T3

(1)(2)(3)(4)(5)(6)

Page 25: Hindley-Milner Type Inference CSE 340  Principles of Programming Languages Fall 2015 Adam Doup Arizona State University

25Adam Doupé, Principles of Programming Languages

fun foo(a, b, c) = c(a[b])(1)def

foo (a, b, c)(2)apply

(3)c

(4)[]

(5)a

(6)b

foo (T1,T2,T3) -> T4

a T1

b T2

c T3

(1)(2)(3)(4)(5)(6)

Page 26: Hindley-Milner Type Inference CSE 340  Principles of Programming Languages Fall 2015 Adam Doup Arizona State University

26Adam Doupé, Principles of Programming Languages

fun foo(a, b, c) = c(a[b])(1)def

foo (a, b, c)(2)apply

(3)c

(4)[]

(5)a

(6)b

foo (T1,T2,T3) -> T4

a T1

b T2

c T3

(1)(2) T4

(3)(4)(5)(6)

Page 27: Hindley-Milner Type Inference CSE 340  Principles of Programming Languages Fall 2015 Adam Doup Arizona State University

27Adam Doupé, Principles of Programming Languages

fun foo(a, b, c) = c(a[b])(1)def

foo (a, b, c)(2)apply

(3)c

(4)[]

(5)a

(6)b

foo (T1,T2,T3) -> T4

a T1

b T2

c T3

(1)(2) T4

(3)(4) T5

(5)(6)

Page 28: Hindley-Milner Type Inference CSE 340  Principles of Programming Languages Fall 2015 Adam Doup Arizona State University

28Adam Doupé, Principles of Programming Languages

fun foo(a, b, c) = c(a[b])(1)def

foo (a, b, c)(2)apply

(3)c

(4)[]

(5)a

(6)b

foo (T1,T2,T3) -> T4

a T1

b T2

c T3

(1)(2) T4

(3) T5 -> T4

(4) T5

(5)(6)

Page 29: Hindley-Milner Type Inference CSE 340  Principles of Programming Languages Fall 2015 Adam Doup Arizona State University

29Adam Doupé, Principles of Programming Languages

fun foo(a, b, c) = c(a[b])(1)def

foo (a, b, c)(2)apply

(3)c

(4)[]

(5)a

(6)b

foo (T1,T2,T3) -> T4

a T1

b T2

c T5 -> T4

(1)(2) T4

(3) T5 -> T4

(4) T5

(5)(6)

Page 30: Hindley-Milner Type Inference CSE 340  Principles of Programming Languages Fall 2015 Adam Doup Arizona State University

30Adam Doupé, Principles of Programming Languages

fun foo(a, b, c) = c(a[b])(1)def

foo (a, b, c)(2)apply

(3)c

(4)[]

(5)a

(6)b

foo (T1,T2,(T5->T4)) -> T4

a T1

b T2

c T5 -> T4

(1)(2) T4

(3) T5 -> T4

(4) T5

(5)(6)

Page 31: Hindley-Milner Type Inference CSE 340  Principles of Programming Languages Fall 2015 Adam Doup Arizona State University

31Adam Doupé, Principles of Programming Languages

fun foo(a, b, c) = c(a[b])(1)def

foo (a, b, c)(2)apply

(3)c

(4)[]

(5)a

(6)b

foo (T1,T2,(T5->T4)) -> T4

a T1

b T2

c T5 -> T4

(1)(2) T4

(3) T5 -> T4

(4) T5

(5) Array of T5

(6)

Page 32: Hindley-Milner Type Inference CSE 340  Principles of Programming Languages Fall 2015 Adam Doup Arizona State University

32Adam Doupé, Principles of Programming Languages

fun foo(a, b, c) = c(a[b])(1)def

foo (a, b, c)(2)apply

(3)c

(4)[]

(5)a

(6)b

foo (T1,T2,(T5->T4)) -> T4

a T1

b T2

c T5 -> T4

(1)(2) T4

(3) T5 -> T4

(4) T5

(5) Array of T5

(6) int

Page 33: Hindley-Milner Type Inference CSE 340  Principles of Programming Languages Fall 2015 Adam Doup Arizona State University

33Adam Doupé, Principles of Programming Languages

fun foo(a, b, c) = c(a[b])(1)def

foo (a, b, c)(2)apply

(3)c

(4)[]

(5)a

(6)b

foo (Array of T5 ,T2,(T5->T4)) -> T4

a Array of T5

b T2

c T5 -> T4

(1)(2) T4

(3) T5 -> T4

(4) T5

(5) Array of T5

(6) int

Page 34: Hindley-Milner Type Inference CSE 340  Principles of Programming Languages Fall 2015 Adam Doup Arizona State University

34Adam Doupé, Principles of Programming Languages

fun foo(a, b, c) = c(a[b])(1)def

foo (a, b, c)(2)apply

(3)c

(4)[]

(5)a

(6)b

foo (Array of T5, T2,(T5->T4)) -> T4

a Array of T5

b intc T5 -> T4

(1)(2) T4

(3) T5 -> T4

(4) T5

(5) Array of T5

(6) int

Page 35: Hindley-Milner Type Inference CSE 340  Principles of Programming Languages Fall 2015 Adam Doup Arizona State University

35Adam Doupé, Principles of Programming Languages

fun foo(a, b, c) = c(a[b])(1)def

foo (a, b, c)(2)apply

(3)c

(4)[]

(5)a

(6)b

foo (Array of T5, int,(T5->T4)) -> T4

a Array of T5

b intc T5 -> T4

(1)(2) T4

(3) T5 -> T4

(4) T5

(5) Array of T5

(6) int