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

ml type inference and unification
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

ML Type Inference and Unification

Arlen Cox

slide-2
SLIDE 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

slide-3
SLIDE 3

ML Type Inference

 Hindley/Milner Type Inference  Statically typed language with no mandatory

annotations

 Three phases to determining types

− Constraint generation − Unification − Annotation

slide-4
SLIDE 4

An Example

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

slide-5
SLIDE 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

slide-6
SLIDE 6

Constraint Generation

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

Constraints Variables

slide-7
SLIDE 7

Constraint Generation

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

Constraints Variables apply: 'a

slide-8
SLIDE 8

Constraint Generation

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

Constraints Variables apply: 'a f: 'b v: 'c t: 'd

slide-9
SLIDE 9

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 Variables apply: 'a f: 'b v: 'c t: 'd

slide-10
SLIDE 10

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 Variables apply: 'a f: 'b v: 'c t: 'd

slide-11
SLIDE 11

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 Variables apply: 'a f: 'b v: 'c t: 'd

slide-12
SLIDE 12

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 Variables apply: 'a f: 'b v: 'c t: 'd

slide-13
SLIDE 13

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 Variables apply: 'a f: 'b v: 'c t: 'd

slide-14
SLIDE 14

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 = int bool = bool Variables apply: 'a f: 'b v: 'c t: 'd

slide-15
SLIDE 15

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 = int bool = bool 'c = 'h Variables apply: 'a f: 'b v: 'c t: 'd

slide-16
SLIDE 16

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 = int bool = bool 'c = 'h 'a = 'b → 'c → 'd → 'c Variables apply: 'a f: 'b v: 'c t: 'd

slide-17
SLIDE 17

Constraint Solving - Unification

Constraints '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 Mapping

slide-18
SLIDE 18

Constraint Solving - Unification

Constraints 'b = 'c → 'f 'e = 'f → 'g 'd = int 'g = int → 'h 'd = int bool = bool 'c = 'h 'b → 'e = 'b → 'c → 'd → 'c Mapping 'a = 'b → 'e

slide-19
SLIDE 19

Constraint Solving - Unification

Constraints 'e = 'f → 'g 'd = int 'g = int → 'h 'd = int bool = bool 'c = 'h ('c → 'f) → 'e = ('c → 'f) → 'c → 'd → 'c Mapping 'a = ('c → 'f) → 'e 'b = 'c → 'f

slide-20
SLIDE 20

Constraint Solving - Unification

Constraints 'd = int 'g = int → 'h 'd = int bool = bool 'c = 'h ('c → 'f) → 'f → 'g = ('c → 'f) → 'c → 'd → 'c Mapping 'a = ('c → 'f) → 'f → 'g 'b = 'c → 'f 'e = 'f → 'g

slide-21
SLIDE 21

Constraint Solving - Unification

Constraints 'g = int → 'h int = int bool = 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

slide-22
SLIDE 22

Constraint Solving - Unification

Constraints int = int bool = 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

slide-23
SLIDE 23

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

slide-24
SLIDE 24

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

slide-25
SLIDE 25

Constraint Solving - Unification

Constraints 'c → 'f = 'c → 'f 'f = 'c int = int 'c = 'c Mapping 'a = ('c → 'f) → 'f → int → 'c 'b = 'c → 'f 'e = 'f → int → 'c 'd = int 'g = int → 'c 'h = 'c

slide-26
SLIDE 26

Constraint Solving - Unification

Constraints 'c = 'c 'f = 'f 'f = 'c int = int 'c = 'c Mapping 'a = ('c → 'f) → 'f → int → 'c 'b = 'c → 'f 'e = 'f → int → 'c 'd = int 'g = int → 'c 'h = 'c

slide-27
SLIDE 27

Constraint Solving - Unification

Constraints 'f = 'c int = int 'c = 'c Mapping 'a = ('c → 'f) → 'f → int → 'c 'b = 'c → 'f 'e = 'f → int → 'c 'd = int 'g = int → 'c 'h = 'c

slide-28
SLIDE 28

Constraint Solving - Unification

Constraints int = 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

slide-29
SLIDE 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

slide-30
SLIDE 30

Type Annotation

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 Mapping 'a = ('c → 'c) → 'c → int → 'c 'b = 'c → 'c 'e = 'c → int → 'c 'd = int 'g = int → 'c 'h = 'c 'f = 'c

slide-31
SLIDE 31

Type Annotation

let rec apply = fun f v t -> 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 Mapping 'a = ('c → 'c) → 'c → int → 'c 'b = 'c → 'c 'e = 'c → int → 'c 'd = int 'g = int → 'c 'h = 'c 'f = 'c

slide-32
SLIDE 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

slide-33
SLIDE 33

Difficulties

 Polymorphic function application  Matching  Reference Types

slide-34
SLIDE 34

Polymorphic Function Application

let f : 'a->'a = fun (x: 'a) -> x let t1 = f true let t2 = f 3

slide-35
SLIDE 35

Polymorphic Function Application

let f : 'a->'a = fun (x: 'a) -> x let t1 = f true let t2 = f 3

'a = bool 'a = int

slide-36
SLIDE 36

Solution

 Copy the type of f every time f is used

let f : 'a->'a = fun (x: 'a) -> x let t1 = f true let t2 = f 3

'b = bool 'c = int

slide-37
SLIDE 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

slide-38
SLIDE 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 -> case l | Nil -> Nil | Cons(h,t) -> Cons(f h, map f t) esac

l : 'a list

slide-39
SLIDE 39

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

slide-40
SLIDE 40

Solution

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

slide-41
SLIDE 41

Reference Types

 Classical ML Bug:

let r = ref (fun x -> x) r := (fun x -> x + 1) !r true

slide-42
SLIDE 42

Solution

 Value Restriction

− SML − Only allow values

 Modified Value Restriction

− OCaml − Value assigned at first use − Monomorphic in use, polymorphic at initial

definition

slide-43
SLIDE 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

slide-44
SLIDE 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