hindley milner type checking automatic type inference
play

Hindley-Milner Type Checking Automatic Type Inference What can be - PowerPoint PPT Presentation

CSE340 Principles of Programming Languages Hindley-Milner Type Checking Automatic Type Inference What can be inferred about type of f or x from this definition? f(x) = x Automatic Type Inference f(x) = x f is a function that takes a single


  1. CSE340 Principles of Programming Languages Hindley-Milner Type Checking

  2. Automatic Type Inference What can be inferred about type of f or x from this definition? f(x) = x

  3. Automatic Type Inference f(x) = x f is a function that takes a single argument. So the type of f can be described as: T1(*)(T2)

  4. Automatic Type Inference f(x) = x The return value of f is equal to its input, so their types must match: T1 = T2

  5. Automatic Type Inference f(x) = x So f is a function that takes one argument and its return type is the same as its argument’s type. Therefore type of f is: T1(*)(T1)

  6. Automatic Type Inference And we don’t know anything about the type of x f(x) = x So f is a function that takes one argument and its return type is the same as its argument’s type. Therefore type of f is: T1(*)(T1)

  7. Automatic Type Inference How about function g ? g(x) = x + 1

  8. Automatic Type Inference g(x) = x + 1 What can be inferred from this term?

  9. Automatic Type Inference g(x) = x + 1 x is used in an arithmetic expression involving the integer constant 1. So x must be of integer type

  10. Automatic Type Inference g(x) = x + 1 So the type of function g should be further restricted to: int(*)(int)

  11. To perform Hindley-Milner type checking: • Start by generating the abstract syntax tree of the function • Assume unknown types for arguments: T1, T2, … • Examine the tree nodes and apply type constraints to further restrict the types • The type constraints that can be applied depend on the programming language used. In the following examples we use simple rules similar to those in functional languages like OCaml

  12. Examples

  13. Example #1 f(a,b,c,d) = if b = 1 then if a[1](0) then c(1) else c(d) else if a[b](0) then c(b) else f(a, b - 1, c, d)

  14. Example #1 f(a,b,c,d) = if b = 1 then if a[1](0) then c(1) else c(d) else if a[b](0) then c(b) else f(a, b - 1, c, d) def f(a,b,c,d) if if = apply apply b 1 [] 0 f d a c a b - apply if b 1 c b apply apply [] 0 c d apply a 1 c 1

  15. def f(a,b,c,d) if if = apply apply b 1 [] 0 f d a c a b - apply if b 1 c b apply apply [] 0 c d apply a 1 c 1

  16. def f(a,b,c,d) if Else if = Condition apply apply b 1 [] 0 f d a c Then a b - apply if b 1 c b apply apply [] 0 c d apply a 1 c 1

  17. def f(a,b,c,d) if Else if Condition = apply apply b 1 [] 0 f d a c a b - apply if b 1 c b apply apply [] 0 c d apply Then a 1 c 1

  18. def f(a,b,c,d) if if = apply apply b 1 [] 0 f d a c a b - apply Condition if b 1 c b apply apply [] 0 c d apply a 1 c 1 Else Then

  19. def f(a,b,c,d) if The function if = apply apply b 1 [] 0 f d a c a b - apply if b 1 c b apply apply [] 0 c d apply Parameters in order from left to right a 1 c 1

  20. f T1(*)(T2,T3,T4,T5) a T2 b T3 c T4 def d T5 f(a,b,c,d) if if = apply apply b 1 [] 0 f d a c a b - apply if b 1 c b apply apply [] 0 c d apply a 1 c 1

  21. Top-Down order

  22. f T1(*)(T2,T3,T4,T5) a T2 b T3 c T4 def d T5 T1 f(a,b,c,d) if if = The return type of the function should apply apply b 1 be the same as the type of the if node [] 0 f d a c a b - apply if b 1 c b apply apply [] 0 c d apply a 1 c 1

  23. f T1(*)(T2,T3,T4,T5) a T2 b T3 c T4 def d T5 T1 f(a,b,c,d) if bool if = apply apply b 1 [] 0 f d a c a b - apply if The condition should be of type boolean b 1 c b apply apply [] 0 c d apply a 1 c 1

  24. f T1(*)(T2,T3,T4,T5) a T2 b T3 c T4 def d T5 T1 f(a,b,c,d) if bool if = apply apply b 1 [] 0 f d The then node should be of the same T1 a c type as the if node a b - apply if b 1 c b apply apply [] 0 c d apply a 1 c 1

  25. f T1(*)(T2,T3,T4,T5) a T2 b T3 c T4 def d T5 T1 f(a,b,c,d) if T1 bool if = apply apply b 1 [] 0 f d The else node should be of the same T1 a c type as the if node a b - apply if b 1 c b apply apply [] 0 c d apply a 1 c 1

  26. f T1(*)(T2, int ,T4,T5) a T2 b int c T4 def d T5 T1 f(a,b,c,d) if T1 bool if = int int apply apply b 1 [] 0 f d T1 a c a b - apply if The operands of a comparison b 1 operator (=) should be of the same c b apply apply type. The right operand is int , so b should be int too i.e. T3 = int [] 0 c d apply a 1 c 1

  27. f T1(*)(T2,int,T4,T5) a T2 b int c T4 def d T5 T1 f(a,b,c,d) if T1 bool if = int int apply apply b 1 [] 0 f d T1 a c a b - apply if bool b 1 c b apply apply [] 0 c d apply a 1 c 1 The condition should be of type boolean

  28. f T1(*)(T2,int,T4,T5) a T2 b int c T4 def d T5 T1 f(a,b,c,d) if T1 bool if = int int apply apply b 1 [] 0 f d T1 a c a b - apply if bool b 1 c b apply apply T1 [] 0 c d apply a 1 The then node should be of the same c 1 type as the if node

  29. f T1(*)(T2,int,T4,T5) a T2 b int c T4 def d T5 T1 f(a,b,c,d) if T1 bool if = int int The else node should be of the same apply apply b 1 type as the if node [] 0 f d T1 a c a b - apply if bool T1 b 1 c b apply apply T1 [] 0 c d apply a 1 c 1

  30. f T1(*)(T2,int,T4,T5) a T2 b int c T4 def d T5 T1 f(a,b,c,d) if T1 bool if = bool int int apply apply b 1 [] 0 f d T1 a c a b - apply if bool T1 The condition should be of type boolean b 1 c b apply apply T1 [] 0 c d apply a 1 c 1

  31. f T1(*)(T2,int,T4,T5) a T2 b int c T4 def d T5 T1 f(a,b,c,d) if T1 bool if = bool int int apply apply b 1 [] 0 f d T1 T1 a c a b - apply if bool T1 b 1 c b apply apply T1 The then node should be of the same [] 0 c d apply type as the if node a 1 c 1

  32. f T1(*)(T2,int,T4,T5) a T2 b int c T4 def d T5 T1 f(a,b,c,d) if T1 bool if = bool T1 int int apply apply b 1 [] 0 f d T1 T1 a c a b - apply if bool T1 b 1 c b apply apply The else node should be of the same T1 type as the if node [] 0 c d apply a 1 c 1

  33. f T1(*)(T2,int,T4,T5) a T2 b int c T4 def d T5 T1 f(a,b,c,d) if The left-most child node of an T1 bool apply node must be a function , the other if = bool T1 int int children are the parameters passed to that apply apply b 1 function. The return type of the function is the type of the apply node [] 0 f d T1 T1 a c a b - apply if bool T1 b 1 c b apply apply bool(*)(int) int T1 [] 0 c d apply a 1 c 1

  34. f T1(*)(T2,int, T1(*)(int) ,T5) a T2 b int c T1(*)(int) def d T5 T1 f(a,b,c,d) if T1 bool if = bool T1 int int apply apply b 1 [] 0 f d T1 c must be a function that takes one T1 a c a b - integer argument and returns a value of type T1, apply if bool T1 i.e. T4 = T1(*)(int) b 1 c b apply apply bool(*)(int) int T1 [] 0 c d apply a 1 c 1 T1(*)(int) int

  35. f T1(*)(T2,int,T1(*)(int), int ) a T2 b int c T1(*)(int) def d int T1 f(a,b,c,d) if T1 bool if = bool T1 We know that c is a function that int int takes an integer as argument and returns apply apply b 1 T1. The argument passed here is d , so d [] 0 f d must be of type int, i.e. T5 = int T1 T1 a c a b - apply if bool T1 b 1 c b apply apply bool(*)(int) int T1 [] 0 c d apply int a 1 c 1 T1(*)(int) int

  36. f T1(*)(T2,int,T1(*)(int),int) a T2 b int c T1(*)(int) def The type of [] node must be a function d int T1 that takes an integer as argument and returns f(a,b,c,d) if boolean, i.e. bool(*)(int) T1 bool if = bool T1 int int apply apply b 1 bool(*)(int) int [] 0 f d T1 T1 a c a b - apply if bool T1 b 1 c b apply apply bool(*)(int) int T1 [] 0 c d apply int a 1 c 1 T1(*)(int) int

  37. f T1(*)(T2,int,T1(*)(int),int) a T2 b int c T1(*)(int) def d int T1 f(a,b,c,d) if T1 bool if = bool T1 int int apply apply b 1 bool(*)(int) int [] 0 f d T1 T1 a c a b - apply if bool T1 b 1 c b apply apply bool(*)(int) int T1 [] 0 c d apply int We know that c is a function that a 1 c 1 takes an integer as argument and returns T1(*)(int) int T1, we also know that b is integer

  38. f T1(*)(T2,int,T1(*)(int),int) a T2 b int c T1(*)(int) def d int T1 f(a,b,c,d) if T1 bool if = bool T1 int int apply apply b 1 bool(*)(int) int [] 0 f d T1 int T1 a c a b - apply if bool T1 b 1 c b apply apply bool(*)(int) int T1 [] 0 c d apply We know that f is a function that takes 4 int arguments of types T2, int, T1(*)(int) and int and a 1 c 1 returns a value of type T1. We know that a is of type T1(*)(int) int T2, and c is of type T1(*)(int) and d is of type int. The - node should be of type int

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend