hindley-milner type inference cse 340 principles of programming languages fall 2015 adam doup...
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; 3TRANSCRIPT
Hindley-Milner Type Inference
CSE 340 – Principles of Programming LanguagesFall 2015
Adam DoupéArizona State Universityhttp://adamdoupe.com
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;
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;
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";
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+
+
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)); }}
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)
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
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
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
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")
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
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!
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
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
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
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
18Adam Doupé, Principles of Programming Languages
Operators
• Array Access Operator a[b]
• T2 = array of T1
• T3 = int
(T1)[]
(T2)a
(T3)b
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
…
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
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
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
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)
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)
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)
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)
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)
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)
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)
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)
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)
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
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
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
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