What should I wear? Parametric polymorphism and its decidability - - PowerPoint PPT Presentation

what should i wear
SMART_READER_LITE
LIVE PREVIEW

What should I wear? Parametric polymorphism and its decidability - - PowerPoint PPT Presentation

Introduction Type reconstruction Hindley-Milner Related work Conclusion References What should I wear? Parametric polymorphism and its decidability Michael B. Gale School of Computer Science University of Nottingham What should I wear?


slide-1
SLIDE 1

Introduction Type reconstruction Hindley-Milner Related work Conclusion References

What should I wear?

Parametric polymorphism and its decidability

Michael B. Gale

School of Computer Science University of Nottingham

What should I wear? Michael B. Gale

slide-2
SLIDE 2

Introduction Type reconstruction Hindley-Milner Related work Conclusion References

Today’s question

How do we make polymorphism practical for use in programming languages?

What should I wear? Michael B. Gale

slide-3
SLIDE 3

Introduction Type reconstruction Hindley-Milner Related work Conclusion References

System F

  • Girard (1971) and Reynolds (1974)
  • Type abstractions and universal quantifiers
  • We can write things such as

ID ≡ Λα.λx : α.x

What should I wear? Michael B. Gale

slide-4
SLIDE 4

Introduction Type reconstruction Hindley-Milner Related work Conclusion References

Why polymorphism?

  • In λ→ we would have to write:

IDBOOL ≡ λx : Bool.x IDNAT ≡ λx : Nat.x . . .

  • Infinitely-many types

What should I wear? Michael B. Gale

slide-5
SLIDE 5

Introduction Type reconstruction Hindley-Milner Related work Conclusion References

What’s wrong with System F?

  • Nothing!

What should I wear? Michael B. Gale

slide-6
SLIDE 6

Introduction Type reconstruction Hindley-Milner Related work Conclusion References

What’s wrong with System F?

  • Nothing!
  • Very inconvenient to write down lots of types

What should I wear? Michael B. Gale

slide-7
SLIDE 7

Introduction Type reconstruction Hindley-Milner Related work Conclusion References

Church-style vs. Curry-style

  • λ→ and System F given in Church-style:
  • Define types before terms
  • Explicit type annotations in λ-abstractions
  • Curry-style:
  • Define terms first, then worry about types
  • No type annotations

What should I wear? Michael B. Gale

slide-8
SLIDE 8

Introduction Type reconstruction Hindley-Milner Related work Conclusion References

What we want to do

  • Type reconstruction: take term from the untyped λ-calculus,

then assign a type to it

What should I wear? Michael B. Gale

slide-9
SLIDE 9

Introduction Type reconstruction Hindley-Milner Related work Conclusion References

What we want to do

  • Type reconstruction: take term from the untyped λ-calculus,

then assign a type to it

  • Typability: Given Γ, and a term e, can we find a type τ:

Γ ⊢ e : τ

What should I wear? Michael B. Gale

slide-10
SLIDE 10

Introduction Type reconstruction Hindley-Milner Related work Conclusion References

What we want to do

  • Type reconstruction: take term from the untyped λ-calculus,

then assign a type to it

  • Typability: Given Γ, and a term e, can we find a type τ:

Γ ⊢ e : τ

  • Type checking: Given Γ, e, and τ:

Γ ⊢ e : τ

What should I wear? Michael B. Gale

slide-11
SLIDE 11

Introduction Type reconstruction Hindley-Milner Related work Conclusion References

Type reconstruction for λ→

  • How do we know what type λx.x has?

What should I wear? Michael B. Gale

slide-12
SLIDE 12

Introduction Type reconstruction Hindley-Milner Related work Conclusion References

Type reconstruction for λ→

  • How do we know what type λx.x has?
  • Idea: assign a type variable to x:

(λx.x) : α → α

  • This is the principal type

What should I wear? Michael B. Gale

slide-13
SLIDE 13

Introduction Type reconstruction Hindley-Milner Related work Conclusion References

Principal types

  • τ is the principal type of a term e if, for every other type τ ′

which can be derived from e: τ ⊑ τ ′

  • α → α is the principal type of λx.x because e.g.

Bool → Bool is more “special”

  • We can calculate any other permissible type from the principal

type by instantiating the type variables

What should I wear? Michael B. Gale

slide-14
SLIDE 14

Introduction Type reconstruction Hindley-Milner Related work Conclusion References

Back to System F

  • Can we do the same?
  • Example (in Haskell-like syntax):

f ≡ λg.(g 4, g True)

What should I wear? Michael B. Gale

slide-15
SLIDE 15

Introduction Type reconstruction Hindley-Milner Related work Conclusion References

Back to System F

  • Can we do the same?
  • Example (in Haskell-like syntax):

f ≡ λg.(g 4, g True)

  • f : (∀α.α → α) → (Nat, Bool) is valid
  • f : (∀α.α → Nat) → (Nat, Nat) is also valid
  • No principal type!

What should I wear? Michael B. Gale

slide-16
SLIDE 16

Introduction Type reconstruction Hindley-Milner Related work Conclusion References

What’s the problem?

  • Wells (1998) proved typability and type checking for System F

to be equivalent and undecidable

What should I wear? Michael B. Gale

slide-17
SLIDE 17

Introduction Type reconstruction Hindley-Milner Related work Conclusion References

What’s the problem?

  • Wells (1998) proved typability and type checking for System F

to be equivalent and undecidable

  • We were able to derive types containing type variables before!
  • What’s different?

What should I wear? Michael B. Gale

slide-18
SLIDE 18

Introduction Type reconstruction Hindley-Milner Related work Conclusion References

The rank of a type

f : ∀α.(α → α) Rank-1 type g : ∀α.(α → (∀β.β)) Rank-1 type h : (∀α.α → α) → Bool Rank-2 type i : ((∀α.α → α) → Bool) → Bool Rank-3 type

  • Rank-1: prenex polymorphism
  • Previously, we didn’t have explicit quantifiers
  • Assumption was that there were implicit quantifiers

surrounding the principal types

What should I wear? Michael B. Gale

slide-19
SLIDE 19

Introduction Type reconstruction Hindley-Milner Related work Conclusion References

Hindley-Milner

  • Hindley (1969) and Milner (1978)
  • Damas and Milner (1982) may be easier to understand
  • Distinction between monomorphic and polymorphic types
  • Thus we only have prenex polymorphism
  • Local bindings may be polymorphic (Let-polymorphism)
  • Polymorphic types are instantiated with fresh type variables

What should I wear? Michael B. Gale

slide-20
SLIDE 20

Introduction Type reconstruction Hindley-Milner Related work Conclusion References

Types

α → Type variables T → Type constructors τ → α Type variable | T τ Type application σ → τ Monomorphic type | ∀α.σ Qualifier

What should I wear? Michael B. Gale

slide-21
SLIDE 21

Introduction Type reconstruction Hindley-Milner Related work Conclusion References

Terms

e → x Variable | e e Application | λx.e Abstraction | let x = e in e Let-binding

What should I wear? Michael B. Gale

slide-22
SLIDE 22

Introduction Type reconstruction Hindley-Milner Related work Conclusion References

Type inference

x : σ ∈ Γ Γ ⊢ x : σ

TVar

Γ ⊢ e : σ′ σ′ ⊑ σ Γ ⊢ e : σ

TInst

Γ ⊢ e : τ ′ → τ Γ ⊢ e′ : τ ′ Γ ⊢ e e′ : τ

TApp

Γ, x : τ ⊢ e : τ ′ Γ ⊢ λx.e : τ → τ ′ TAbs Γ ⊢ e : σ Γ, x : σ ⊢ e′ : τ Γ ⊢ let x = e in e′ : τ

TLet

Γ ⊢ e : σ α ∈ FV (Γ) Γ ⊢ e : ∀α.σ

TGen

What should I wear? Michael B. Gale

slide-23
SLIDE 23

Introduction Type reconstruction Hindley-Milner Related work Conclusion References

Algorithm W

W ∈ Γ × e → τ × θ

  • Sound and complete
  • Based on unification (Robinson, 1965)
  • Key ideas:
  • Always instantiate the type of a variable
  • Unify types when applying one term to another
  • Generalise types of let-bindings
  • Thus, polymorphic types only exist in Γ

What should I wear? Michael B. Gale

slide-24
SLIDE 24

Introduction Type reconstruction Hindley-Milner Related work Conclusion References

Algorithm M

M ∈ Γ × e × τ → θ

  • Lee and Yi (1998)
  • Also sound and complete
  • Same ideas as in W
  • Top-down, rather than bottom-up: type constraints
  • Terminates earlier than W if a term is ill-typed

What should I wear? Michael B. Gale

slide-25
SLIDE 25

Introduction Type reconstruction Hindley-Milner Related work Conclusion References

Applications

  • HM is used in e.g. ML and Haskell
  • Haskell98 has some extensions such as type classes (Hall

et al., 1996)

  • Success of those languages shows that it’s a good

compromise!

What should I wear? Michael B. Gale

slide-26
SLIDE 26

Introduction Type reconstruction Hindley-Milner Related work Conclusion References

What else is there?

  • HM is not a solution for every problem
  • What if we need higher-rank polymorphism?
  • What if type variables alone aren’t restrictive enough?
  • We know that Haskell has type classes for this purpose

What should I wear? Michael B. Gale

slide-27
SLIDE 27

Introduction Type reconstruction Hindley-Milner Related work Conclusion References

Arbitrary-rank types

  • Pierce and Turner (2000) argue that research has focused too

much on complete type inference

  • Suggestion: partial type inference which requires some

annotations

  • Question: how many type annotations do we need?
  • Peyton-Jones et al. (2007) show a simple technique for

arbitrary-rank polymorphism in Haskell

What should I wear? Michael B. Gale

slide-28
SLIDE 28

Introduction Type reconstruction Hindley-Milner Related work Conclusion References

Principal typings

  • Jim (1996)
  • More general than principal types
  • We calculate a type and context, e.g.:

A, B ∪ { x : σ } ⊢ x : σ

  • Naturally leads to intersection types

What should I wear? Michael B. Gale

slide-29
SLIDE 29

Introduction Type reconstruction Hindley-Milner Related work Conclusion References

Intersection types

  • We will only give a basic overview
  • General idea is that we can have types such as

Nat → Nat → Nat ∧ String → String → String

  • Many papers about them – use Google Scholar
  • Pierce (1991) discusses many aspects of them and contains a

lot of pointers to other papers

  • Reynolds (1988) gives a practical implementation
  • Recent paper at ICFP 2012: Dunfield (2012)

What should I wear? Michael B. Gale

slide-30
SLIDE 30

Introduction Type reconstruction Hindley-Milner Related work Conclusion References

Summary

  • Typability and type checking in System F are undecidable, but:
  • Let-polymorphism works pretty well in most cases
  • Arbitrary-rank polymorphism is possible with the help of some

type annotations

  • Intersection types are interesting

What should I wear? Michael B. Gale

slide-31
SLIDE 31

Introduction Type reconstruction Hindley-Milner Related work Conclusion References

References I

Damas, L. & Milner, R. Principal type-schemes for functional

  • programs. In Proceedings of the 9th ACM SIGPLAN-SIGACT

symposium on Principles of programming languages, pages 207–212. ACM, 1982. Dunfield, J. Elaborating intersection and union types. In Proceedings of the 17th ACM SIGPLAN international conference

  • n Functional programming, pages 17–28. ACM, 2012.

Girard, J. Une extension de ˇ linterpretation de G¨

  • del a ˇ

lanalyse, et son application a ˇ lelimination des coupures dans ˇ lanalyse et la theorie des types. In Fenstad, J.E., editor, Proceedings of the Second Scandinavian Logic Symposium, volume 63 of Studies in Logic and the Foundations of Mathematics, pages 63 – 92. Elsevier, 1971.

What should I wear? Michael B. Gale

slide-32
SLIDE 32

Introduction Type reconstruction Hindley-Milner Related work Conclusion References

References II

Hall, Cordelia V & Hammond, Kevin & Peyton Jones, Simon L & Wadler, Philip L. Type classes in Haskell. ACM Transactions on Programming Languages and Systems (TOPLAS), 18(2): 109–138, 1996. Hindley, R. The principal type-scheme of an object in combinatory

  • logic. Transactions of the American Mathematical Society, 146:

29–60, 1969. Jim, T. What are principal typings and what are they good for? In Proceedings of the 23rd ACM SIGPLAN-SIGACT symposium on Principles of programming languages, pages 42–53. ACM, 1996. Lee, O. & Yi, K. Proofs about a folklore let-polymorphic type inference algorithm. ACM Transactions on Programming Languages and Systems (TOPLAS), 20(4):707–723, 1998.

What should I wear? Michael B. Gale

slide-33
SLIDE 33

Introduction Type reconstruction Hindley-Milner Related work Conclusion References

References III

Milner, R. A theory of type polymorphism in programming. Journal of Computer and System Sciences, 17:348–375, 1978. Peyton-Jones, S. & Vytiniotis, D. & Weirich, S. & Shields, M. Practical type inference for arbitrary-rank types. Journal of Functional Programming, 17(1):1, 2007. Pierce, B. C. Programming with intersection types and bounded

  • polymorphism. PhD thesis, Carnegie Mellon University, 1991.

Pierce, B. C. & Turner, D. N. Local type inference. ACM Transactions on Programming Languages and Systems (TOPLAS), 22(1):1–44, 2000. Reynolds, J. C. Towards a theory of type structure. In Symposium

  • n Programming, pages 408–423, 1974.

Reynolds, J. C. Preliminary design of the programming language

  • forsythe. 1988.

What should I wear? Michael B. Gale

slide-34
SLIDE 34

Introduction Type reconstruction Hindley-Milner Related work Conclusion References

References IV

Robinson, J. A. A machine-oriented logic based on the resolution

  • principle. Journal of the ACM (JACM), 12(1):23–41, 1965.

Wells, J. B. Typability and type checking in System F are equivalent and undecidable. Annals of Pure and Applied Logic, 98:111–156, 1998.

What should I wear? Michael B. Gale