![Page 1: ML Type Inference and Unification - Computer Sciencebec/courses/csci5535-s09/slides/ML_… · Classical ML Bug: let r = ref (fun x -> x) r := (fun x -> x + 1)!r true. Solution Value](https://reader035.vdocuments.net/reader035/viewer/2022070913/5fb4d9519fe8f10ff74541f5/html5/thumbnails/1.jpg)
ML Type Inference and Unification
Arlen Cox
![Page 2: ML Type Inference and Unification - Computer Sciencebec/courses/csci5535-s09/slides/ML_… · Classical ML Bug: let r = ref (fun x -> x) r := (fun x -> x + 1)!r true. Solution Value](https://reader035.vdocuments.net/reader035/viewer/2022070913/5fb4d9519fe8f10ff74541f5/html5/thumbnails/2.jpg)
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
![Page 3: ML Type Inference and Unification - Computer Sciencebec/courses/csci5535-s09/slides/ML_… · Classical ML Bug: let r = ref (fun x -> x) r := (fun x -> x + 1)!r true. Solution Value](https://reader035.vdocuments.net/reader035/viewer/2022070913/5fb4d9519fe8f10ff74541f5/html5/thumbnails/3.jpg)
ML Type Inference
Hindley/Milner Type Inference Statically typed language with no mandatory
annotations Three phases to determining types
− Constraint generation− Unification− Annotation
![Page 4: ML Type Inference and Unification - Computer Sciencebec/courses/csci5535-s09/slides/ML_… · Classical ML Bug: let r = ref (fun x -> x) r := (fun x -> x + 1)!r true. Solution Value](https://reader035.vdocuments.net/reader035/viewer/2022070913/5fb4d9519fe8f10ff74541f5/html5/thumbnails/4.jpg)
An Example
let rec apply = fun f v t -> if t = 0 then v else apply f (f v) (t - 1) fi
![Page 5: ML Type Inference and Unification - Computer Sciencebec/courses/csci5535-s09/slides/ML_… · Classical ML Bug: let r = ref (fun x -> x) r := (fun x -> x + 1)!r true. Solution Value](https://reader035.vdocuments.net/reader035/viewer/2022070913/5fb4d9519fe8f10ff74541f5/html5/thumbnails/5.jpg)
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
![Page 6: ML Type Inference and Unification - Computer Sciencebec/courses/csci5535-s09/slides/ML_… · Classical ML Bug: let r = ref (fun x -> x) r := (fun x -> x + 1)!r true. Solution Value](https://reader035.vdocuments.net/reader035/viewer/2022070913/5fb4d9519fe8f10ff74541f5/html5/thumbnails/6.jpg)
Constraint Generation
let rec apply = fun f v t -> if t = 0 then v else apply f (f v) (t - 1) fi
Constraints
Variables
![Page 7: ML Type Inference and Unification - Computer Sciencebec/courses/csci5535-s09/slides/ML_… · Classical ML Bug: let r = ref (fun x -> x) r := (fun x -> x + 1)!r true. Solution Value](https://reader035.vdocuments.net/reader035/viewer/2022070913/5fb4d9519fe8f10ff74541f5/html5/thumbnails/7.jpg)
Constraint Generation
let rec apply = fun f v t -> if t = 0 then v else apply f (f v) (t - 1) fi
Constraints
Variablesapply: 'a
![Page 8: ML Type Inference and Unification - Computer Sciencebec/courses/csci5535-s09/slides/ML_… · Classical ML Bug: let r = ref (fun x -> x) r := (fun x -> x + 1)!r true. Solution Value](https://reader035.vdocuments.net/reader035/viewer/2022070913/5fb4d9519fe8f10ff74541f5/html5/thumbnails/8.jpg)
Constraint Generation
let rec apply = fun f v t -> if t = 0 then v else apply f (f v) (t - 1) fi
Constraints
Variablesapply: 'af: 'bv: 'ct: 'd
![Page 9: ML Type Inference and Unification - Computer Sciencebec/courses/csci5535-s09/slides/ML_… · Classical ML Bug: let r = ref (fun x -> x) r := (fun x -> x + 1)!r true. Solution Value](https://reader035.vdocuments.net/reader035/viewer/2022070913/5fb4d9519fe8f10ff74541f5/html5/thumbnails/9.jpg)
Constraint Generation
let rec apply = fun f v t -> if t = 0 then v else apply f (f v) (t - 1) fi
Constraints'a = 'b → 'e
Variablesapply: 'af: 'bv: 'ct: 'd
![Page 10: ML Type Inference and Unification - Computer Sciencebec/courses/csci5535-s09/slides/ML_… · Classical ML Bug: let r = ref (fun x -> x) r := (fun x -> x + 1)!r true. Solution Value](https://reader035.vdocuments.net/reader035/viewer/2022070913/5fb4d9519fe8f10ff74541f5/html5/thumbnails/10.jpg)
Constraint Generation
let rec apply = fun f v t -> if t = 0 then v else apply f (f v) (t - 1) fi
Constraints'a = 'b → 'e'b = 'c → 'f
Variablesapply: 'af: 'bv: 'ct: 'd
![Page 11: ML Type Inference and Unification - Computer Sciencebec/courses/csci5535-s09/slides/ML_… · Classical ML Bug: let r = ref (fun x -> x) r := (fun x -> x + 1)!r true. Solution Value](https://reader035.vdocuments.net/reader035/viewer/2022070913/5fb4d9519fe8f10ff74541f5/html5/thumbnails/11.jpg)
Constraint Generation
let rec apply = fun f v t -> if t = 0 then v else apply f (f v) (t - 1) fi
Constraints'a = 'b → 'e'b = 'c → 'f'e = 'f → 'g
Variablesapply: 'af: 'bv: 'ct: 'd
![Page 12: ML Type Inference and Unification - Computer Sciencebec/courses/csci5535-s09/slides/ML_… · Classical ML Bug: let r = ref (fun x -> x) r := (fun x -> x + 1)!r true. Solution Value](https://reader035.vdocuments.net/reader035/viewer/2022070913/5fb4d9519fe8f10ff74541f5/html5/thumbnails/12.jpg)
Constraint Generation
let rec apply = fun f v t -> if t = 0 then v else apply f (f v) (t - 1) fi
Constraints'a = 'b → 'e'b = 'c → 'f'e = 'f → 'g'd = int
Variablesapply: 'af: 'bv: 'ct: 'd
![Page 13: ML Type Inference and Unification - Computer Sciencebec/courses/csci5535-s09/slides/ML_… · Classical ML Bug: let r = ref (fun x -> x) r := (fun x -> x + 1)!r true. Solution Value](https://reader035.vdocuments.net/reader035/viewer/2022070913/5fb4d9519fe8f10ff74541f5/html5/thumbnails/13.jpg)
Constraint Generation
let rec apply = fun f v t -> if t = 0 then v else apply f (f v) (t - 1) fi
Constraints'a = 'b → 'e'b = 'c → 'f'e = 'f → 'g'd = int'g = int → 'h
Variablesapply: 'af: 'bv: 'ct: 'd
![Page 14: ML Type Inference and Unification - Computer Sciencebec/courses/csci5535-s09/slides/ML_… · Classical ML Bug: let r = ref (fun x -> x) r := (fun x -> x + 1)!r true. Solution Value](https://reader035.vdocuments.net/reader035/viewer/2022070913/5fb4d9519fe8f10ff74541f5/html5/thumbnails/14.jpg)
Constraint Generation
let rec apply = fun f v t -> if t = 0 then v else apply f (f v) (t - 1) fi
Constraints'a = 'b → 'e'b = 'c → 'f'e = 'f → 'g'd = int'g = int → 'h'd = intbool = bool
Variablesapply: 'af: 'bv: 'ct: 'd
![Page 15: ML Type Inference and Unification - Computer Sciencebec/courses/csci5535-s09/slides/ML_… · Classical ML Bug: let r = ref (fun x -> x) r := (fun x -> x + 1)!r true. Solution Value](https://reader035.vdocuments.net/reader035/viewer/2022070913/5fb4d9519fe8f10ff74541f5/html5/thumbnails/15.jpg)
Constraint Generation
let rec apply = fun f v t -> if t = 0 then v else apply f (f v) (t - 1) fi
Constraints'a = 'b → 'e'b = 'c → 'f'e = 'f → 'g'd = int'g = int → 'h'd = intbool = bool'c = 'h
Variablesapply: 'af: 'bv: 'ct: 'd
![Page 16: ML Type Inference and Unification - Computer Sciencebec/courses/csci5535-s09/slides/ML_… · Classical ML Bug: let r = ref (fun x -> x) r := (fun x -> x + 1)!r true. Solution Value](https://reader035.vdocuments.net/reader035/viewer/2022070913/5fb4d9519fe8f10ff74541f5/html5/thumbnails/16.jpg)
Constraint Generation
let rec apply = fun f v t -> if t = 0 then v else apply f (f v) (t - 1) fi
Constraints'a = 'b → 'e'b = 'c → 'f'e = 'f → 'g'd = int'g = int → 'h'd = intbool = bool'c = 'h'a = 'b → 'c → 'd → 'c
Variablesapply: 'af: 'bv: 'ct: 'd
![Page 17: ML Type Inference and Unification - Computer Sciencebec/courses/csci5535-s09/slides/ML_… · Classical ML Bug: let r = ref (fun x -> x) r := (fun x -> x + 1)!r true. Solution Value](https://reader035.vdocuments.net/reader035/viewer/2022070913/5fb4d9519fe8f10ff74541f5/html5/thumbnails/17.jpg)
Constraint Solving Unification
Constraints'a = 'b → 'e'b = 'c → 'f'e = 'f → 'g'd = int'g = int → 'h'd = intbool = bool'c = 'h'a = 'b → 'c → 'd → 'c
Mapping
![Page 18: ML Type Inference and Unification - Computer Sciencebec/courses/csci5535-s09/slides/ML_… · Classical ML Bug: let r = ref (fun x -> x) r := (fun x -> x + 1)!r true. Solution Value](https://reader035.vdocuments.net/reader035/viewer/2022070913/5fb4d9519fe8f10ff74541f5/html5/thumbnails/18.jpg)
Constraint Solving Unification
Constraints'b = 'c → 'f'e = 'f → 'g'd = int'g = int → 'h'd = intbool = bool'c = 'h'b → 'e = 'b → 'c → 'd → 'c
Mapping'a = 'b → 'e
![Page 19: ML Type Inference and Unification - Computer Sciencebec/courses/csci5535-s09/slides/ML_… · Classical ML Bug: let r = ref (fun x -> x) r := (fun x -> x + 1)!r true. Solution Value](https://reader035.vdocuments.net/reader035/viewer/2022070913/5fb4d9519fe8f10ff74541f5/html5/thumbnails/19.jpg)
Constraint Solving Unification
Constraints'e = 'f → 'g'd = int'g = int → 'h'd = intbool = bool'c = 'h('c → 'f) → 'e = ('c → 'f) → 'c → 'd → 'c
Mapping'a = ('c → 'f) → 'e'b = 'c → 'f
![Page 20: ML Type Inference and Unification - Computer Sciencebec/courses/csci5535-s09/slides/ML_… · Classical ML Bug: let r = ref (fun x -> x) r := (fun x -> x + 1)!r true. Solution Value](https://reader035.vdocuments.net/reader035/viewer/2022070913/5fb4d9519fe8f10ff74541f5/html5/thumbnails/20.jpg)
Constraint Solving Unification
Constraints'd = int'g = int → 'h'd = intbool = bool'c = 'h('c → 'f) → 'f → 'g = ('c → 'f) → 'c → 'd → 'c
Mapping'a = ('c → 'f) → 'f → 'g'b = 'c → 'f'e = 'f → 'g
![Page 21: ML Type Inference and Unification - Computer Sciencebec/courses/csci5535-s09/slides/ML_… · Classical ML Bug: let r = ref (fun x -> x) r := (fun x -> x + 1)!r true. Solution Value](https://reader035.vdocuments.net/reader035/viewer/2022070913/5fb4d9519fe8f10ff74541f5/html5/thumbnails/21.jpg)
Constraint Solving Unification
Constraints'g = int → 'hint = intbool = bool'c = 'h('c → 'f) → 'f → 'g = 'c → ('f → 'c) → int → 'c
Mapping'a = ('c → 'f) → 'f → 'g'b = 'c → 'f'e = 'f → 'g'd = int
![Page 22: ML Type Inference and Unification - Computer Sciencebec/courses/csci5535-s09/slides/ML_… · Classical ML Bug: let r = ref (fun x -> x) r := (fun x -> x + 1)!r true. Solution Value](https://reader035.vdocuments.net/reader035/viewer/2022070913/5fb4d9519fe8f10ff74541f5/html5/thumbnails/22.jpg)
Constraint Solving Unification
Constraintsint = intbool = bool'c = 'h('c → 'f) → 'f → int → 'h = ('c → 'f) → 'c → int → 'c
Mapping'a = ('c → 'f) → 'f → int → 'h'b = 'c → 'f'e = 'f → int → 'h'd = int'g = int → 'h
![Page 23: ML Type Inference and Unification - Computer Sciencebec/courses/csci5535-s09/slides/ML_… · Classical ML Bug: let r = ref (fun x -> x) r := (fun x -> x + 1)!r true. Solution Value](https://reader035.vdocuments.net/reader035/viewer/2022070913/5fb4d9519fe8f10ff74541f5/html5/thumbnails/23.jpg)
Constraint Solving Unification
Constraints'c = 'h('c → 'f) → 'f → int → 'h = ('c → 'f) → 'c → int → 'c
Mapping'a = ('c → 'f) → 'f → int → 'h'b = 'c → 'f'e = 'f → int → 'h'd = int'g = int → 'h
![Page 24: ML Type Inference and Unification - Computer Sciencebec/courses/csci5535-s09/slides/ML_… · Classical ML Bug: let r = ref (fun x -> x) r := (fun x -> x + 1)!r true. Solution Value](https://reader035.vdocuments.net/reader035/viewer/2022070913/5fb4d9519fe8f10ff74541f5/html5/thumbnails/24.jpg)
Constraint Solving Unification
Constraints('c → 'f) → 'f → int → 'c = ('c → 'f) → 'c → int → 'c
Mapping'a = ('c → 'f) → 'f → int → 'c'b = 'c → 'f'e = 'f → int → 'c'd = int'g = int → 'c'h = 'c
![Page 25: ML Type Inference and Unification - Computer Sciencebec/courses/csci5535-s09/slides/ML_… · Classical ML Bug: let r = ref (fun x -> x) r := (fun x -> x + 1)!r true. Solution Value](https://reader035.vdocuments.net/reader035/viewer/2022070913/5fb4d9519fe8f10ff74541f5/html5/thumbnails/25.jpg)
Constraint Solving Unification
Constraints'c → 'f = 'c → 'f'f = 'cint = int'c = 'c
Mapping'a = ('c → 'f) → 'f → int → 'c'b = 'c → 'f'e = 'f → int → 'c'd = int'g = int → 'c'h = 'c
![Page 26: ML Type Inference and Unification - Computer Sciencebec/courses/csci5535-s09/slides/ML_… · Classical ML Bug: let r = ref (fun x -> x) r := (fun x -> x + 1)!r true. Solution Value](https://reader035.vdocuments.net/reader035/viewer/2022070913/5fb4d9519fe8f10ff74541f5/html5/thumbnails/26.jpg)
Constraint Solving Unification
Constraints'c = 'c'f = 'f'f = 'cint = int'c = 'c
Mapping'a = ('c → 'f) → 'f → int → 'c'b = 'c → 'f'e = 'f → int → 'c'd = int'g = int → 'c'h = 'c
![Page 27: ML Type Inference and Unification - Computer Sciencebec/courses/csci5535-s09/slides/ML_… · Classical ML Bug: let r = ref (fun x -> x) r := (fun x -> x + 1)!r true. Solution Value](https://reader035.vdocuments.net/reader035/viewer/2022070913/5fb4d9519fe8f10ff74541f5/html5/thumbnails/27.jpg)
Constraint Solving Unification
Constraints'f = 'cint = int'c = 'c
Mapping'a = ('c → 'f) → 'f → int → 'c'b = 'c → 'f'e = 'f → int → 'c'd = int'g = int → 'c'h = 'c
![Page 28: ML Type Inference and Unification - Computer Sciencebec/courses/csci5535-s09/slides/ML_… · Classical ML Bug: let r = ref (fun x -> x) r := (fun x -> x + 1)!r true. Solution Value](https://reader035.vdocuments.net/reader035/viewer/2022070913/5fb4d9519fe8f10ff74541f5/html5/thumbnails/28.jpg)
Constraint Solving Unification
Constraintsint = int'c = 'c
Mapping'a = ('c → 'c) → 'c → int → 'c'b = 'c → 'c'e = 'c → int → 'c'd = int'g = int → 'c'h = 'c'f = 'c
![Page 29: ML Type Inference and Unification - Computer Sciencebec/courses/csci5535-s09/slides/ML_… · Classical ML Bug: let r = ref (fun x -> x) r := (fun x -> x + 1)!r true. Solution Value](https://reader035.vdocuments.net/reader035/viewer/2022070913/5fb4d9519fe8f10ff74541f5/html5/thumbnails/29.jpg)
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
![Page 30: ML Type Inference and Unification - Computer Sciencebec/courses/csci5535-s09/slides/ML_… · Classical ML Bug: let r = ref (fun x -> x) r := (fun x -> x + 1)!r true. Solution Value](https://reader035.vdocuments.net/reader035/viewer/2022070913/5fb4d9519fe8f10ff74541f5/html5/thumbnails/30.jpg)
Type Annotation
let rec apply = fun f v t -> if t = 0 then v else apply f (f v) (t - 1) fi
Variablesapply: 'af: 'bv: 'ct: 'd
Mapping'a = ('c → 'c) → 'c → int → 'c'b = 'c → 'c'e = 'c → int → 'c'd = int'g = int → 'c'h = 'c'f = 'c
![Page 31: ML Type Inference and Unification - Computer Sciencebec/courses/csci5535-s09/slides/ML_… · Classical ML Bug: let r = ref (fun x -> x) r := (fun x -> x + 1)!r true. Solution Value](https://reader035.vdocuments.net/reader035/viewer/2022070913/5fb4d9519fe8f10ff74541f5/html5/thumbnails/31.jpg)
Type Annotation
let rec apply = fun f v t -> if t = 0 then v else apply f (f v) (t - 1) fi
Variablesapply: ('c → 'c) → 'c → int → 'cf: 'c → 'cv: 'ct: int
Mapping'a = ('c → 'c) → 'c → int → 'c'b = 'c → 'c'e = 'c → int → 'c'd = int'g = int → 'c'h = 'c'f = 'c
![Page 32: ML Type Inference and Unification - Computer Sciencebec/courses/csci5535-s09/slides/ML_… · Classical ML Bug: let r = ref (fun x -> x) r := (fun x -> x + 1)!r true. Solution Value](https://reader035.vdocuments.net/reader035/viewer/2022070913/5fb4d9519fe8f10ff74541f5/html5/thumbnails/32.jpg)
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
Variablesapply: ('c → 'c) → 'c → int → 'cf: 'c → 'cv: 'ct: int
![Page 33: ML Type Inference and Unification - Computer Sciencebec/courses/csci5535-s09/slides/ML_… · Classical ML Bug: let r = ref (fun x -> x) r := (fun x -> x + 1)!r true. Solution Value](https://reader035.vdocuments.net/reader035/viewer/2022070913/5fb4d9519fe8f10ff74541f5/html5/thumbnails/33.jpg)
Difficulties
Polymorphic function application Matching Reference Types
![Page 34: ML Type Inference and Unification - Computer Sciencebec/courses/csci5535-s09/slides/ML_… · Classical ML Bug: let r = ref (fun x -> x) r := (fun x -> x + 1)!r true. Solution Value](https://reader035.vdocuments.net/reader035/viewer/2022070913/5fb4d9519fe8f10ff74541f5/html5/thumbnails/34.jpg)
Polymorphic Function Application
let f : 'a->'a = fun (x: 'a) -> x
let t1 = f truelet t2 = f 3
![Page 35: ML Type Inference and Unification - Computer Sciencebec/courses/csci5535-s09/slides/ML_… · Classical ML Bug: let r = ref (fun x -> x) r := (fun x -> x + 1)!r true. Solution Value](https://reader035.vdocuments.net/reader035/viewer/2022070913/5fb4d9519fe8f10ff74541f5/html5/thumbnails/35.jpg)
Polymorphic Function Application
let f : 'a->'a = fun (x: 'a) -> x
let t1 = f truelet t2 = f 3
'a = bool
'a = int
![Page 36: ML Type Inference and Unification - Computer Sciencebec/courses/csci5535-s09/slides/ML_… · Classical ML Bug: let r = ref (fun x -> x) r := (fun x -> x + 1)!r true. Solution Value](https://reader035.vdocuments.net/reader035/viewer/2022070913/5fb4d9519fe8f10ff74541f5/html5/thumbnails/36.jpg)
Solution
Copy the type of f every time f is used
let f : 'a->'a = fun (x: 'a) -> x
let t1 = f truelet t2 = f 3
'b = bool
'c = int
![Page 37: ML Type Inference and Unification - Computer Sciencebec/courses/csci5535-s09/slides/ML_… · Classical ML Bug: let r = ref (fun x -> x) r := (fun x -> x + 1)!r true. Solution Value](https://reader035.vdocuments.net/reader035/viewer/2022070913/5fb4d9519fe8f10ff74541f5/html5/thumbnails/37.jpg)
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
![Page 38: ML Type Inference and Unification - Computer Sciencebec/courses/csci5535-s09/slides/ML_… · Classical ML Bug: let r = ref (fun x -> x) r := (fun x -> x + 1)!r true. Solution Value](https://reader035.vdocuments.net/reader035/viewer/2022070913/5fb4d9519fe8f10ff74541f5/html5/thumbnails/38.jpg)
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
l : 'a list
![Page 39: ML Type Inference and Unification - Computer Sciencebec/courses/csci5535-s09/slides/ML_… · Classical ML Bug: let r = ref (fun x -> x) r := (fun x -> x + 1)!r true. Solution Value](https://reader035.vdocuments.net/reader035/viewer/2022070913/5fb4d9519fe8f10ff74541f5/html5/thumbnails/39.jpg)
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
Cons(h,t) :'a *'a list
![Page 40: ML Type Inference and Unification - Computer Sciencebec/courses/csci5535-s09/slides/ML_… · Classical ML Bug: let r = ref (fun x -> x) r := (fun x -> x + 1)!r true. Solution Value](https://reader035.vdocuments.net/reader035/viewer/2022070913/5fb4d9519fe8f10ff74541f5/html5/thumbnails/40.jpg)
Solution
Folding and Unfolding l is folded Cons(h,t) is unfolded Implicit in ML
![Page 41: ML Type Inference and Unification - Computer Sciencebec/courses/csci5535-s09/slides/ML_… · Classical ML Bug: let r = ref (fun x -> x) r := (fun x -> x + 1)!r true. Solution Value](https://reader035.vdocuments.net/reader035/viewer/2022070913/5fb4d9519fe8f10ff74541f5/html5/thumbnails/41.jpg)
Reference Types
Classical ML Bug:
let r = ref (fun x -> x)r := (fun x -> x + 1)!r true
![Page 42: ML Type Inference and Unification - Computer Sciencebec/courses/csci5535-s09/slides/ML_… · Classical ML Bug: let r = ref (fun x -> x) r := (fun x -> x + 1)!r true. Solution Value](https://reader035.vdocuments.net/reader035/viewer/2022070913/5fb4d9519fe8f10ff74541f5/html5/thumbnails/42.jpg)
Solution
Value Restriction− SML− Only allow values
Modified Value Restriction− OCaml− Value assigned at first use− Monomorphic in use, polymorphic at initial
definition
![Page 43: ML Type Inference and Unification - Computer Sciencebec/courses/csci5535-s09/slides/ML_… · Classical ML Bug: let r = ref (fun x -> x) r := (fun x -> x + 1)!r true. Solution Value](https://reader035.vdocuments.net/reader035/viewer/2022070913/5fb4d9519fe8f10ff74541f5/html5/thumbnails/43.jpg)
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
![Page 44: ML Type Inference and Unification - Computer Sciencebec/courses/csci5535-s09/slides/ML_… · Classical ML Bug: let r = ref (fun x -> x) r := (fun x -> x + 1)!r true. Solution Value](https://reader035.vdocuments.net/reader035/viewer/2022070913/5fb4d9519fe8f10ff74541f5/html5/thumbnails/44.jpg)
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/fpottierappsem2005.pdf