Last time System F K 1 is a kind K 2 is a kind -kind K 1 K 2 is a - - PowerPoint PPT Presentation

last time
SMART_READER_LITE
LIVE PREVIEW

Last time System F K 1 is a kind K 2 is a kind -kind K 1 K 2 is a - - PowerPoint PPT Presentation

Last time System F K 1 is a kind K 2 is a kind -kind K 1 K 2 is a kind A :: K 1 K 2 , :: K 1 A :: K 2 B :: K 1 -intro -elim :: K 1 . A :: K 1 K 2 A B :: K 2 (and encoding data


slide-1
SLIDE 1

Last time

System Fω K1 is a kind K2 is a kind ⇒-kind K1 ⇒ K2 is a kind Γ, α::K1 ⊢ A :: K2 ⇒-intro Γ ⊢ λα::K1.A :: K1 ⇒ K2 Γ ⊢ A :: K1 ⇒ K2 Γ ⊢ B :: K1 ⇒-elim Γ ⊢ A B :: K2 (and encoding data types: 1, 2, N, +, lists, nested types and ≡)

1/ 48

slide-2
SLIDE 2

This time

Γ ⊢ M : ?

2/ 48

slide-3
SLIDE 3

What is type inference?

# fun f g x -> f (g x);;

  • : (’a -> ’b) -> (’c -> ’a) -> ’c -> ’b = <fun >

3/ 48

slide-4
SLIDE 4

What is type inference?

# fun f g x -> f (g x);;

  • : (’a -> ’b) -> (’c -> ’a) -> ’c -> ’b = <fun >

Goal succinctness of annotation-free code + safety and expressiveness of System Fω

3/ 48

slide-5
SLIDE 5

What is type inference?

# fun f g x -> f (g x);;

  • : (’a -> ’b) -> (’c -> ’a) -> ’c -> ’b = <fun >

Goal succinctness of annotation-free code + safety and expressiveness of System Fω Bad news the goal is unachievable

3/ 48

slide-6
SLIDE 6

The ML calculus

4/ 48

slide-7
SLIDE 7

Prenex quantifification ∀α.α → α ∀α.∀β.α → (β → β) ∀α.(∀β.β → β) → α ∀α.α → (∀β.β → β) Let-bound polymorphism

let id = fun x -> x in id id let id x = x in id id let f id = id id in f (fun x -> x) (fun id -> id id) (fun x -> x)

5/ 48

slide-8
SLIDE 8

Prenex quantifification ∀α.α → α ✓ ∀α.∀β.α → (β → β) ∀α.(∀β.β → β) → α ∀α.α → (∀β.β → β) Let-bound polymorphism

let id = fun x -> x in id id let id x = x in id id let f id = id id in f (fun x -> x) (fun id -> id id) (fun x -> x)

5/ 48

slide-9
SLIDE 9

Prenex quantifification ∀α.α → α ✓ ∀α.∀β.α → (β → β) ✓ ∀α.(∀β.β → β) → α ∀α.α → (∀β.β → β) Let-bound polymorphism

let id = fun x -> x in id id let id x = x in id id let f id = id id in f (fun x -> x) (fun id -> id id) (fun x -> x)

5/ 48

slide-10
SLIDE 10

Prenex quantifification ∀α.α → α ✓ ∀α.∀β.α → (β → β) ✓ ∀α.(∀β.β → β) → α ✗ ∀α.α → (∀β.β → β) Let-bound polymorphism

let id = fun x -> x in id id let id x = x in id id let f id = id id in f (fun x -> x) (fun id -> id id) (fun x -> x)

5/ 48

slide-11
SLIDE 11

Prenex quantifification ∀α.α → α ✓ ∀α.∀β.α → (β → β) ✓ ∀α.(∀β.β → β) → α ✗ ∀α.α → (∀β.β → β) ✗ Let-bound polymorphism

let id = fun x -> x in id id let id x = x in id id let f id = id id in f (fun x -> x) (fun id -> id id) (fun x -> x)

5/ 48

slide-12
SLIDE 12

Prenex quantifification ∀α.α → α ✓ ∀α.∀β.α → (β → β) ✓ ∀α.(∀β.β → β) → α ✗ ∀α.α → (∀β.β → β) ✗ Let-bound polymorphism

let id = fun x -> x in id id

let id x = x in id id let f id = id id in f (fun x -> x) (fun id -> id id) (fun x -> x)

5/ 48

slide-13
SLIDE 13

Prenex quantifification ∀α.α → α ✓ ∀α.∀β.α → (β → β) ✓ ∀α.(∀β.β → β) → α ✗ ∀α.α → (∀β.β → β) ✗ Let-bound polymorphism

let id = fun x -> x in id id

let id x = x in id id

let f id = id id in f (fun x -> x) (fun id -> id id) (fun x -> x)

5/ 48

slide-14
SLIDE 14

Prenex quantifification ∀α.α → α ✓ ∀α.∀β.α → (β → β) ✓ ∀α.(∀β.β → β) → α ✗ ∀α.α → (∀β.β → β) ✗ Let-bound polymorphism

let id = fun x -> x in id id

let id x = x in id id

let f id = id id in f (fun x -> x)

(fun id -> id id) (fun x -> x)

5/ 48

slide-15
SLIDE 15

Prenex quantifification ∀α.α → α ✓ ∀α.∀β.α → (β → β) ✓ ∀α.(∀β.β → β) → α ✗ ∀α.α → (∀β.β → β) ✗ Let-bound polymorphism

let id = fun x -> x in id id

let id x = x in id id

let f id = id id in f (fun x -> x)

(fun id -> id id) (fun x -> x)

5/ 48

slide-16
SLIDE 16

Types and schemes

B-types Γ ⊢ B is a type α ∈ Γ α-types Γ ⊢ α is a type Γ ⊢ A is a type Γ ⊢ B is a type →-types Γ ⊢ A → B is a type Γ, α ⊢ A is a type scheme Γ ⊢ ∀α.A is a scheme

6/ 48

slide-17
SLIDE 17

Environments

Γ-· · is an environment Γ is an environment Γ ⊢ S is a scheme Γ-: Γ, x : S is an environment Γ is an environment Γ-:: Γ, α is an environment

7/ 48

slide-18
SLIDE 18

Typing rules for →

Γ, x : A ⊢ M : B →-intro Γ ⊢ λx.M : A → B Γ ⊢ M : A → B Γ ⊢ N : A →-elim Γ ⊢ M N : B

8/ 48

slide-19
SLIDE 19

Typing rules for schemes

Γ ⊢ M : A α ∩ fv(Γ) = Ø Γ, x : ∀α.A ⊢ N : B scheme-intro Γ ⊢ let x = M in N : B x : ∀α.A ∈ Γ Γ ⊢ B is a type (for B ∈ B) scheme-elim Γ ⊢ x : A[α := B]

9/ 48

slide-20
SLIDE 20

Milner’s algorithm

10/ 48

slide-21
SLIDE 21

Substitutions

[a1 → A1, a2 → A2, . . . an → An]

For example, let σ be {a → B, b → (B → B)} A be a → b → a Then

σA is B → (B → B) → B.

If

σA = B

(for some σ) then we say B is a substitution instance of A.

11/ 48

slide-22
SLIDE 22

Constraints

a = b a → b = B → b B = B B = B → B

12/ 48

slide-23
SLIDE 23

Unification

unify : ConstraintSet → Substitution unify(∅) = [] unify({A = A} ∪ C) = unify(C) unify({a = A} ∪ C) = unify([a → A]C) ◦ [a → A] when a / ∈ ftv(A) unify({A = a} ∪ C) = unify([a → A]C) ◦ [a → A] when a / ∈ ftv(A) unify({A → B = A′ → B′} ∪ C) = unify({A = A′, B = B′} ∪ C) unify({A = B} ∪ C) = FAIL

13/ 48

slide-24
SLIDE 24

Algorithm J

J : Environment × Expression → Type J (Γ, λx.M) = b → A where A = J (Γ,x:b, M) and b is fresh J (Γ, M N) = b where A = J (Γ, M) and B = J (Γ, N) and unify ’ ({A = B → b}) succeeds and b is fresh J (Γ, x) = A[α:=b] where Γ(x) = ∀α.A and b are fresh J (Γ, let x = M in N) = B where A = J (Γ, M) and B = J (Γ,x:∀α.A, N) and α = ftv(A) \ ftv(Γ)

14/ 48

slide-25
SLIDE 25

Algorithm J in action

J(·, let apply = λf.λx.f x in let id = λy.y in apply id) =

15/ 48

slide-26
SLIDE 26

Algorithm J in action

J(·, let apply = λf.λx.f x in let id = λy.y in apply id) = J(·, λf.λx.f x) =

16/ 48

slide-27
SLIDE 27

Algorithm J in action

J(·, let apply = λf.λx.f x in let id = λy.y in apply id) = J(·, λf.λx.f x) = J(·,f:b1, λx.f x) =

17/ 48

slide-28
SLIDE 28

Algorithm J in action

J(·, let apply = λf.λx.f x in let id = λy.y in apply id) = J(·, λf.λx.f x) = b1 → b2 → b3 J(·,f:b1, λx.f x) = b2 → b3 J(·,f:b1,x:b2, f x) = b3

18/ 48

slide-29
SLIDE 29

Algorithm J in action

J(·, let apply = λf.λx.f x in let id = λy.y in apply id) = J(·, λf.λx.f x) = b1 → b2 → b3 J(·,f:b1, λx.f x) = b2 → b3 J(·,f:b1,x:b2, f x) = b3 J(·,f:b1,x:b2, f) =

19/ 48

slide-30
SLIDE 30

Algorithm J in action

J(·, let apply = λf.λx.f x in let id = λy.y in apply id) = J(·, λf.λx.f x) = b1 → b2 → b3 J(·,f:b1, λx.f x) = b2 → b3 J(·,f:b1,x:b2, f x) = b3 J(·,f:b1,x:b2, f) = b1

20/ 48

slide-31
SLIDE 31

Algorithm J in action

J(·, let apply = λf.λx.f x in let id = λy.y in apply id) = J(·, λf.λx.f x) = b1 → b2 → b3 J(·,f:b1, λx.f x) = b2 → b3 J(·,f:b1,x:b2, f x) = b3 J(·,f:b1,x:b2, f) = b1 J(·,f:b1,x:b2, x) =

21/ 48

slide-32
SLIDE 32

Algorithm J in action

J(·, let apply = λf.λx.f x in let id = λy.y in apply id) = J(·, λf.λx.f x) = b1 → b2 → b3 J(·,f:b1, λx.f x) = b2 → b3 J(·,f:b1,x:b2, f x) = b3 J(·,f:b1,x:b2, f) = b1 J(·,f:b1,x:b2, x) = b2

22/ 48

slide-33
SLIDE 33

Algorithm J in action

J(·, let apply = λf.λx.f x in let id = λy.y in apply id) = J(·, λf.λx.f x) = b1 → b2 → b3 J(·,f:b1, λx.f x) = b2 → b3 J(·,f:b1,x:b2, f x) = b3 J(·,f:b1,x:b2, f) = b1 J(·,f:b1,x:b2, x) = b2 unify ({b1 = b2 → b3}) =

23/ 48

slide-34
SLIDE 34

Algorithm J in action

J(·, let apply = λf.λx.f x in let id = λy.y in apply id) = J(·, λf.λx.f x) = b1 → b2 → b3 J(·,f:b1, λx.f x) = b2 → b3 J(·,f:b1,x:b2, f x) = b3 J(·,f:b1,x:b2, f) = b1 J(·,f:b1,x:b2, x) = b2 unify ({b1 = b2 → b3}) = {b1 → b2 → b3}

24/ 48

slide-35
SLIDE 35

Algorithm J in action

J(·, let apply = λf.λx.f x in let id = λy.y in apply id) = J(·, λf.λx.f x) = (b2 → b3) → b2 → b3 J(·,f:b2 → b3, λx.f x) = b2 → b3 J(·,f:b2 → b3,x:b2, f x) = b3 J(·,f:b2 → b3,x:b2, f) = b2 → b3 J(·,f:b2 → b3,x:b2, x) = b2 unify ({b1 = b2 → b3}) = {b1 → b2 → b3}

25/ 48

slide-36
SLIDE 36

Algorithm J in action

J(·, let apply = λf.λx.f x in let id = λy.y in apply id) = J(·, λf.λx.f x) = (b2 → b3) → b2 → b3 J(·,f:b2 → b3, λx.f x) = b2 → b3 J(·,f:b2 → b3,x:b2, f x) = b3 J(·,f:b2 → b3,x:b2, f) = b2 → b3 J(·,f:b2 → b3,x:b2, x) = b2 ftv((b2 → b3)→ b2 → b3) = {b2, b3} ftv(·) = {} {b2, b3} \ {} = {b2, b3}

26/ 48

slide-37
SLIDE 37

Algorithm J in action

J(·, let apply = λf.λx.f x in let id = λy.y in apply id) = J(·, λf.λx.f x) = (b2 → b3) → b2 → b3 J(·,apply:∀α2α3.(α2 → α3)→ α2 → α3, let id = λy.y in apply id) =

27/ 48

slide-38
SLIDE 38

Algorithm J in action

J(·, let apply = λf.λx.f x in let id = λy.y in apply id) = J(·, λf.λx.f x) = (b2 → b3) → b2 → b3 J(·,apply:∀α2α3.(α2 → α3)→ α2 → α3, let id = λy.y in apply id) = J(·,apply:∀α2α3.(α2 → α3)→ α2 → α3, λy.y) =

28/ 48

slide-39
SLIDE 39

Algorithm J in action

J(·, let apply = λf.λx.f x in let id = λy.y in apply id) = J(·, λf.λx.f x) = (b2 → b3) → b2 → b3 J(·,apply:∀α2α3.(α2 → α3)→ α2 → α3, let id = λy.y in apply id) = J(·,apply:∀α2α3.(α2 → α3)→ α2 → α3, λy.y) = b4 → b4 J(·,apply:∀α2α3.(α2 → α3)→ α2 → α3,y:b4, y) = b4

29/ 48

slide-40
SLIDE 40

Algorithm J in action

J(·, let apply = λf.λx.f x in let id = λy.y in apply id) = J(·, λf.λx.f x) = (b2 → b3) → b2 → b3 J(·,apply:∀α2α3.(α2 → α3)→ α2 → α3, let id = λy.y in apply id) = J(·,apply:∀α2α3.(α2 → α3)→ α2 → α3, λy.y) = b4 → b4 ftv(b4 → b4) = {b4} ftv(·,apply:∀α2α3.(α2 → α3) → α2 → α3) = {} {b4} \ {} = {b4}

30/ 48

slide-41
SLIDE 41

Algorithm J in action

J(·, let apply = λf.λx.f x in let id = λy.y in apply id) = J(·, λf.λx.f x) = (b2 → b3) → b2 → b3 J(·,apply:∀α2α3.(α2 → α3)→ α2 → α3, let id = λy.y in apply id) = J(·,apply:∀α2α3.(α2 → α3)→ α2 → α3, λy.y) = b4 → b4 J(·, apply:∀α2α3.(α2 → α3) → α2 → α3,id:∀α4.α4 → α4, apply id) = b5

31/ 48

slide-42
SLIDE 42

Algorithm J in action

J(·, let apply = λf.λx.f x in let id = λy.y in apply id) = J(·, λf.λx.f x) = (b2 → b3) → b2 → b3 J(·,apply:∀α2α3.(α2 → α3)→ α2 → α3, let id = λy.y in apply id) = J(·,apply:∀α2α3.(α2 → α3)→ α2 → α3, λy.y) = b4 → b4 J(·, apply:∀α2α3.(α2 → α3) → α2 → α3,id:∀α4.α4 → α4, apply id) = b5 J(·, apply:∀α2α3.(α2 → α3) → α2 → α3, id:∀α4.α4 → α4, apply) = (b6 → b7)→ b6 → b7

32/ 48

slide-43
SLIDE 43

Algorithm J in action

J(·, let apply = λf.λx.f x in let id = λy.y in apply id) = J(·, λf.λx.f x) = (b2 → b3) → b2 → b3 J(·,apply:∀α2α3.(α2 → α3)→ α2 → α3, let id = λy.y in apply id) = J(·,apply:∀α2α3.(α2 → α3)→ α2 → α3, λy.y) = b4 → b4 J(·, apply:∀α2α3.(α2 → α3) → α2 → α3,id:∀α4.α4 → α4, apply id) = b5 J(·, apply:∀α2α3.(α2 → α3) → α2 → α3, id:∀α4.α4 → α4, apply) = (b6 → b7)→ b6 → b7 J(·, apply:∀α2α3.(α2 → α3) → α2 → α3, id:∀α4.α4 → α4, id) = b8 → b8

33/ 48

slide-44
SLIDE 44

Algorithm J in action

unify ({(b6 → b7)→ b6 → b7 = (b8 → b8) → b5})

34/ 48

slide-45
SLIDE 45

Algorithm J in action

unify ({(b6 → b7)→ b6 → b7 = (b8 → b8) → b5}) = unify ({b6 → b7 = b8 → b8, b6 → b7 = b5})

35/ 48

slide-46
SLIDE 46

Algorithm J in action

unify ({(b6 → b7)→ b6 → b7 = (b8 → b8) → b5}) = unify ({b6 → b7 = b8 → b8, b6 → b7 = b5}) = unify ({b6 = b8, b7 = b8, b6 → b7 = b5})

36/ 48

slide-47
SLIDE 47

Algorithm J in action

unify ({(b6 → b7)→ b6 → b7 = (b8 → b8) → b5}) = unify ({b6 → b7 = b8 → b8, b6 → b7 = b5}) = unify ({b6 = b8, b7 = b8, b6 → b7 = b5}) = {b6 → b8, b7 → b8, b5 → b6 → b7}

37/ 48

slide-48
SLIDE 48

Algorithm J in action

J(·, let apply = λf.λx.f x in let id = λy.y in apply id) = J(·, λf.λx.f x) = (b2 → b3) → b2 → b3 J(·,apply:∀α2α3.(α2 → α3)→ α2 → α3, let id = λy.y in apply id) = J(·,apply:∀α2α3.(α2 → α3)→ α2 → α3, λy.y) = b4 → b4 J(·, apply:∀α2α3.(α2 → α3) → α2 → α3,id:∀α4.α4 → α4, apply id) = b5 J(·, apply:∀α2α3.(α2 → α3) → α2 → α3, id:∀α4.α4 → α4, apply) = (b6 → b7)→ b6 → b7 J(·, apply:∀α2α3.(α2 → α3) → α2 → α3, id:∀α4.α4 → α4, id) = b8 → b8

38/ 48

slide-49
SLIDE 49

Algorithm J in action

J(·, let apply = λf.λx.f x in let id = λy.y in apply id) = J(·, λf.λx.f x) = (b2 → b3) → b2 → b3 J(·,apply:∀α2α3.(α2 → α3)→ α2 → α3, let id = λy.y in apply id) = J(·,apply:∀α2α3.(α2 → α3)→ α2 → α3, λy.y) = b4 → b4 J(·, apply:∀α2α3.(α2 → α3) → α2 → α3,id:∀α4.α4 → α4, apply id) = b8 → b8 J(·, apply:∀α2α3.(α2 → α3) → α2 → α3, id:∀α4.α4 → α4, apply) = (b8 → b8)→ b8 → b8 J(·, apply:∀α2α3.(α2 → α3) → α2 → α3, id:∀α4.α4 → α4, id) = b8 → b8

39/ 48

slide-50
SLIDE 50

Algorithm J in action

J(·, let apply = λf.λx.f x in let id = λy.y in apply id) = J(·, λf.λx.f x) = (b2 → b3) → b2 → b3 J(·,apply:∀α2α3.(α2 → α3)→ α2 → α3, let id = λy.y in apply id) = J(·,apply:∀α2α3.(α2 → α3)→ α2 → α3, λy.y) = b4 → b4 J(·, apply:∀α2α3.(α2 → α3) → α2 → α3,id:∀α4.α4 → α4, apply id) = b8 → b8

40/ 48

slide-51
SLIDE 51

Algorithm J in action

J(·, let apply = λf.λx.f x in let id = λy.y in apply id) = J(·, λf.λx.f x) = (b2 → b3) → b2 → b3 J(·,apply:∀α2α3.(α2 → α3)→ α2 → α3, let id = λy.y in apply id) = b8 → b8 J(·, apply:∀α2α3.(α2 → α3) → α2 → α3,id:∀α4.α4 → α4, apply id) = b8 → b8

41/ 48

slide-52
SLIDE 52

Algorithm J in action

J(·, let apply = λf.λx.f x in let id = λy.y in apply id) = b8 → b8

42/ 48

slide-53
SLIDE 53

Type inference in practice

43/ 48

slide-54
SLIDE 54

Type inference and recursion

Γ, x : A ⊢ M : A α / ∈ fv(Γ) Γ, x : ∀α.A ⊢ N : B let-rec Γ ⊢ let rec x = M in N : B

44/ 48

slide-55
SLIDE 55

Supporting imperative programming: the value restriction

type ’a ref = { mutable contents : ’a } val ref : ’a -> ’a ref val ( ! ) : ’a ref -> ’a val (:=) : ’a ref -> ’a -> unit let r = ref None in r := Some "boom "; match !r with None -> () | Some f -> f ()

45/ 48

slide-56
SLIDE 56

Relaxing the value restriction: variance

type ’a printer = ’a -> string (’a -> ’b) -> ’a printer

  • > ’b printer

list

→ → α+ β− → printer α+ list printer β−

46/ 48

slide-57
SLIDE 57

Relaxing the value restriction: the rules

Should we generalize?

▶ covariant type variables ▶ invariant type variables ▶ contravariant type variables ▶ bivariant type variables

47/ 48

slide-58
SLIDE 58

Relaxing the value restriction: the rules

Should we generalize?

▶ covariant type variables ✓ ▶ invariant type variables ▶ contravariant type variables ▶ bivariant type variables

47/ 48

slide-59
SLIDE 59

Relaxing the value restriction: the rules

Should we generalize?

▶ covariant type variables ✓ ▶ invariant type variables ✗ ▶ contravariant type variables ▶ bivariant type variables

47/ 48

slide-60
SLIDE 60

Relaxing the value restriction: the rules

Should we generalize?

▶ covariant type variables ✓ ▶ invariant type variables ✗ ▶ contravariant type variables ✗ ▶ bivariant type variables

47/ 48

slide-61
SLIDE 61

Relaxing the value restriction: the rules

Should we generalize?

▶ covariant type variables ✓ ▶ invariant type variables ✗ ▶ contravariant type variables ✗ ▶ bivariant type variables ✓

47/ 48

slide-62
SLIDE 62

Next time

Γ ⊢ M : A → B Γ ⊢ N : A Γ ⊢ M N : B Γ ⊢ A → B Γ ⊢ A Γ ⊢ B

48/ 48