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
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
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
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
Introduction Type reconstruction Hindley-Milner Related work Conclusion References What’s wrong with System F? • Nothing! What should I wear? Michael B. Gale
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Introduction Type reconstruction Hindley-Milner Related work Conclusion References Type inference σ ′ ⊑ σ Γ ⊢ e : σ ′ x : σ ∈ Γ TVar TInst Γ ⊢ x : σ Γ ⊢ e : σ Γ ⊢ e : τ ′ → τ Γ ⊢ e ′ : τ ′ TApp Γ ⊢ e e ′ : τ Γ , x : σ ⊢ e ′ : τ Γ , x : τ ⊢ e : τ ′ Γ ⊢ λ x . e : τ → τ ′ TAbs Γ ⊢ e : σ TLet Γ ⊢ let x = e in e ′ : τ Γ ⊢ e : σ α �∈ FV (Γ) TGen Γ ⊢ e : ∀ α.σ What should I wear? Michael B. Gale
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
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
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
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
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
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
Recommend
More recommend