Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Hindley-Milner Type Inference CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University"— Presentation transcript:

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

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; int i; a[i] = 1; 2

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; string i; a[i] = 1; 3

4 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

5 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

6 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

7 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

8 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

9 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

10 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

11 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

12 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

13 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

14 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

15 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

16 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

17 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

18 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

19 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 …

20 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

21 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

22 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

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

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

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

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

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

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

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

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

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

32 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

33 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

34 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

35 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


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

Similar presentations


Ads by Google