ml type inference and unification
play

ML Type Inference and Unification Arlen Cox Research Goals Easy - PowerPoint PPT Presentation

ML Type Inference and Unification Arlen Cox Research Goals Easy to use, high performance parallel programming Primary contributions in backend and runtime Need a front end to target backend ML offers ease of use and safety ML


  1. ML Type Inference and Unification Arlen Cox

  2. Research Goals  Easy to use, high performance parallel programming  Primary contributions in backend and runtime  Need a front end to target backend  ML offers ease of use and safety

  3. ML Type Inference  Hindley/Milner Type Inference  Statically typed language with no mandatory annotations  Three phases to determining types − Constraint generation − Unification − Annotation

  4. An Example let rec apply = fun f v t -> if t = 0 then v else apply f (f v) (t - 1) fi

  5. An Example let rec apply = fun f v t -> if t = 0 then v else apply f (f v) (t - 1) fi val apply: ('a->'a)->'a->int->'a

  6. Constraint Generation Constraints let rec apply = fun f v t -> if t = 0 then v else apply f (f v) (t - 1) fi Variables

  7. Constraint Generation Constraints let rec apply = fun f v t -> if t = 0 then v else apply f (f v) (t - 1) fi Variables apply: 'a

  8. Constraint Generation Constraints let rec apply = fun f v t -> if t = 0 then v else apply f (f v) (t - 1) fi Variables apply: 'a f: 'b v: 'c t: 'd

  9. Constraint Generation Constraints let rec apply = fun f v t -> 'a = 'b → 'e if t = 0 then v else apply f (f v) (t - 1) fi Variables apply: 'a f: 'b v: 'c t: 'd

  10. Constraint Generation Constraints let rec apply = fun f v t -> 'a = 'b → 'e if t = 0 then 'b = 'c → 'f v else apply f (f v) (t - 1) fi Variables apply: 'a f: 'b v: 'c t: 'd

  11. Constraint Generation Constraints let rec apply = fun f v t -> 'a = 'b → 'e if t = 0 then 'b = 'c → 'f 'e = 'f → 'g v else apply f (f v) (t - 1) fi Variables apply: 'a f: 'b v: 'c t: 'd

  12. Constraint Generation Constraints let rec apply = fun f v t -> 'a = 'b → 'e if t = 0 then 'b = 'c → 'f 'e = 'f → 'g v 'd = int else apply f (f v) (t - 1) fi Variables apply: 'a f: 'b v: 'c t: 'd

  13. Constraint Generation Constraints let rec apply = fun f v t -> 'a = 'b → 'e if t = 0 then 'b = 'c → 'f 'e = 'f → 'g v 'd = int else 'g = int → 'h apply f (f v) (t - 1) fi Variables apply: 'a f: 'b v: 'c t: 'd

  14. Constraint Generation Constraints let rec apply = fun f v t -> 'a = 'b → 'e if t = 0 then 'b = 'c → 'f 'e = 'f → 'g v 'd = int else 'g = int → 'h apply f (f v) (t - 1) 'd = int bool = bool fi Variables apply: 'a f: 'b v: 'c t: 'd

  15. Constraint Generation Constraints let rec apply = fun f v t -> 'a = 'b → 'e if t = 0 then 'b = 'c → 'f 'e = 'f → 'g v 'd = int else 'g = int → 'h apply f (f v) (t - 1) 'd = int bool = bool fi 'c = 'h Variables apply: 'a f: 'b v: 'c t: 'd

  16. Constraint Generation Constraints let rec apply = fun f v t -> 'a = 'b → 'e if t = 0 then 'b = 'c → 'f 'e = 'f → 'g v 'd = int else 'g = int → 'h apply f (f v) (t - 1) 'd = int bool = bool fi 'c = 'h 'a = 'b → 'c → 'd → 'c Variables apply: 'a f: 'b v: 'c t: 'd

  17. Constraint Solving - Unification Constraints Mapping 'a = 'b → 'e 'b = 'c → 'f 'e = 'f → 'g 'd = int 'g = int → 'h 'd = int bool = bool 'c = 'h 'a = 'b → 'c → 'd → 'c

  18. Constraint Solving - Unification Constraints Mapping 'b = 'c → 'f 'a = 'b → 'e 'e = 'f → 'g 'd = int 'g = int → 'h 'd = int bool = bool 'c = 'h 'b → 'e = 'b → 'c → 'd → 'c

  19. Constraint Solving - Unification Constraints Mapping 'e = 'f → 'g 'a = ('c → 'f) → 'e 'b = 'c → 'f 'd = int 'g = int → 'h 'd = int bool = bool 'c = 'h ('c → 'f) → 'e = ('c → 'f) → 'c → 'd → 'c

  20. Constraint Solving - Unification Constraints Mapping 'a = ('c → 'f) → 'f → 'g 'd = int 'b = 'c → 'f 'g = int → 'h 'e = 'f → 'g 'd = int bool = bool 'c = 'h ('c → 'f) → 'f → 'g = ('c → 'f) → 'c → 'd → 'c

  21. Constraint Solving - Unification Constraints Mapping 'g = int → 'h 'a = ('c → 'f) → 'f → 'g 'b = 'c → 'f int = int 'e = 'f → 'g bool = bool 'c = 'h 'd = int ('c → 'f) → 'f → 'g = 'c → ('f → 'c) → int → 'c

  22. Constraint Solving - Unification Constraints Mapping 'a = ('c → 'f) → 'f → int → 'h int = int 'b = 'c → 'f bool = bool 'e = 'f → int → 'h 'c = 'h ('c → 'f) → 'f → int → 'h = ('c → 'f) → 'c → int → 'c 'd = int 'g = int → 'h

  23. Constraint Solving - Unification Constraints Mapping 'a = ('c → 'f) → 'f → int → 'h 'c = 'h ('c → 'f) → 'f → int → 'h = ('c → 'f) → 'c → int → 'c 'b = 'c → 'f 'e = 'f → int → 'h 'd = int 'g = int → 'h

  24. Constraint Solving - Unification Constraints Mapping ('c → 'f) → 'f → int → 'c = ('c → 'f) → 'c → int → 'c 'a = ('c → 'f) → 'f → int → 'c 'b = 'c → 'f 'e = 'f → int → 'c 'd = int 'g = int → 'c 'h = 'c

  25. Constraint Solving - Unification Constraints Mapping 'c → 'f = 'c → 'f 'a = ('c → 'f) → 'f → int → 'c 'b = 'c → 'f 'f = 'c 'e = 'f → int → 'c int = int 'c = 'c 'd = int 'g = int → 'c 'h = 'c

  26. Constraint Solving - Unification Constraints Mapping 'a = ('c → 'f) → 'f → int → 'c 'c = 'c 'b = 'c → 'f 'f = 'f 'e = 'f → int → 'c 'f = 'c int = int 'd = int 'g = int → 'c 'c = 'c 'h = 'c

  27. Constraint Solving - Unification Constraints Mapping 'a = ('c → 'f) → 'f → int → 'c 'f = 'c 'b = 'c → 'f int = int 'e = 'f → int → 'c 'c = 'c 'd = int 'g = int → 'c 'h = 'c

  28. Constraint Solving - Unification Constraints Mapping 'a = ('c → 'c) → 'c → int → 'c int = int 'b = 'c → 'c 'c = 'c 'e = 'c → int → 'c 'd = int 'g = int → 'c 'h = 'c 'f = 'c

  29. Constraint Solving - Unification Constraints Mapping 'a = ('c → 'c) → 'c → int → 'c 'b = 'c → 'c 'e = 'c → int → 'c 'd = int 'g = int → 'c 'h = 'c 'f = 'c

  30. Type Annotation Mapping let rec apply = fun f v t -> 'a = ('c → 'c) → 'c → int → 'c if t = 0 then 'b = 'c → 'c 'e = 'c → int → 'c v 'd = int else 'g = int → 'c apply f (f v) (t - 1) 'h = 'c 'f = 'c fi Variables apply: 'a f: 'b v: 'c t: 'd

  31. Type Annotation Mapping let rec apply = fun f v t -> 'a = ('c → 'c) → 'c → int → 'c if t = 0 then 'b = 'c → 'c 'e = 'c → int → 'c v 'd = int else 'g = int → 'c apply f (f v) (t - 1) 'h = 'c 'f = 'c fi Variables apply: ('c → 'c) → 'c → int → 'c f: 'c → 'c v: 'c t: int

  32. Type Annotation let rec apply : ('c -> 'c) -> 'c -> int -> 'c = fun (f:'c -> 'c) (v:'c) (t:int) -> if t = 0 then v else apply f (f v) (t - 1) fi Variables apply: ('c → 'c) → 'c → int → 'c f: 'c → 'c v: 'c t: int

  33. Difficulties  Polymorphic function application  Matching  Reference Types

  34. Polymorphic Function Application let f : 'a->'a = fun (x: 'a) -> x let t1 = f true let t2 = f 3

  35. Polymorphic Function Application let f : 'a->'a = fun (x: 'a) -> x let t1 = f true 'a = bool let t2 = f 3 'a = int

  36. Solution  Copy the type of f every time f is used let f : 'a->'a = fun (x: 'a) -> x let t1 = f true 'b = bool let t2 = f 3 'c = int

  37. Matching  Different types for expression being matched and that used with unions: type 'a list = | Nil | Cons of 'a * 'a list let map = fun f l -> case l | Nil -> Nil | Cons(h,t) -> Cons(f h, map f t) esac

  38. Matching  Different types for expression being matched and that used with unions: type 'a list = | Nil | Cons of 'a * 'a list let map = fun f l -> l : 'a list case l | Nil -> Nil | Cons(h,t) -> Cons(f h, map f t) esac

  39. Matching  Different types for expression being matched and that used with unions: type 'a list = | Nil | Cons of 'a * 'a list Cons(h,t) : let map = fun f l -> 'a *'a list case l | Nil -> Nil | Cons(h,t) -> Cons(f h, map f t) esac

  40. Solution  Folding and Unfolding  l is folded  Cons(h,t) is unfolded  Implicit in ML

  41. Reference Types  Classical ML Bug: let r = ref (fun x -> x) r := (fun x -> x + 1) !r true

  42. Solution  Value Restriction − SML − Only allow values  Modified Value Restriction − OCaml − Value assigned at first use − Monomorphic in use, polymorphic at initial definition

  43. Conclusion  In restricted type systems, full inference can be performed through unification − Allows code compactness and static type safety  Type rules contain constraint generation  Unification uses constraints to reduce potential solutions to the one correct one

  44. References  Krishnamurthi, Shriram, Programming Languages: Application and Interpretation, http://www.cs.brown.edu/~sk/Publications/Books/ProgLangs/  Benjamin C. Pierce, Types and Programming Languages  SML/NJ Type Checking Documentation, http://www.smlnj.org/doc/Conversion/types.html  Francois Pottier, A modern eye on ML type inference, September 2005, http://gallium.inria.fr/~fpottier/publis/fpottier-appsem-2005.pdf

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