Type Reconstruction and Universal Types - PowerPoint PPT Presentation

About This Presentation
Title:

Type Reconstruction and Universal Types

Description:

Type Variables and Substitutions. Type variable (TVar): uninterpreted base ... side-effecting features such as mutable reference cells or exceptions, the type ... – PowerPoint PPT presentation

Number of Views:40
Avg rating:3.0/5.0
Slides: 23
Provided by: Les58
Category:

less

Transcript and Presenter's Notes

Title: Type Reconstruction and Universal Types


1
Type Reconstructionand Universal Types
  • Olesya Peshko
  • CAS 706
  • McMaster University
  • March 31, 2004

2
Type Variables and Substitutions
  • Type variable (TVar) uninterpreted base type,
    placeholder for some types we do not know
  • Substituting types for TVars
  • Type substitution describe a mapping from
    TVars to types
  • Apply to type to obtain an instance
  • Substitution a finite mapping from TVars to
    types
  • Example
  • set of TVars at left-hand sides
    of pairs in
  • set of TVars at right hand
    sides

3
Application of Substitution
4
Constraint-Based Typing
  • Constraint set is a set of equations
  • equations between type expressions
  • Substitution unifies an equation
  • if and are identical
  • unifies if it unifies every equation in
  • To check if is typable under context
  • Analyze the term and collect the constraints
  • Look for substitutions that satisfy

5
Constraint Typing Rules
6
Unification
  • Substitution is less specific (more general)
    than a substitution , written , if
  • for some substitution
  • A principal unifier (most general unifier) for a
    constraint set is a substitution that
    satisfies and such that for
    every substitution satisfying

7
Unification II
  • Theorem The algorithm always
    terminates, failing when given a non-unifiable
    constraint set as input and otherwise returning a
    principal unifier
  • halts, either by failing or by
    returning a substitution, for all
  • If , then is a
    unifier for
  • If is a unifier for , then
  • with

8
Unification Algorithm
9
Principal Types
  • Principal solution for is a
    solution
  • such that, whenever is
    also a solution for , we have
  • If is a principal solution, is a
    principal type of under
  • Theorem If has any solution,
    then it has a principal one
  • Corollary It is decidable whether has
    a solution

10
Universal Types
  • Functions that are applicable to a different type
    of arguments but share the same behavior
  • Abstract out a type from a term and later
    instantiating this abstract term with concrete
    type annotations

11
Varieties of Polymorphism
  • Parametric single piece of code is typed
    generically, variables used in place of types,
    particular types instantiated as needed
  • Impredicative (first-class)
  • ML-style (Let-polymorphism)
  • Ad-hoc polymorphic value exhibits different
    behaviors when viewed at different types
  • Overloading
  • Intensional permits restricted computations
    over types at run time
  • Sybtype gives a single term many types using
    the rule of subsumption (you can selectively
    forget information about terms behavior)

12
System F
  • System F is a straightforward extension of the
    simply typed lambda-calculus
  • Introduce new forms of abstraction and
    application
  • Type abstraction
  • Type application (or instantiation)
  • Reduction rule
  • Example polymorphic identity function

13
System F. Syntax
14
System F. Evaluation
15
System F. Typing
16
Basic Properties
  • Theorem
  • If and , then
  • Theorem
  • if is a closed, well-typed term, then
    either
  • is a value or else there is some with
  • Property of normalization the fact that the
    evaluation of every well-typed program terminates

17
Erasure
  • Type erasure function maps System F terms to
    untyped lambda-terms

18
Typability and Type Reconstruction
  • Term in the untyped lambda-calculus is
    typable if there is some well-typed term
  • such that
  • Theorem It is undecidable whether, given a
    closed term of the untyped lambda-calculus,
    there is some well-typed term in System F
    such that
  • Not only full type reconstruction but also
    various forms of partial type reconstruction are
    known to be undecidable for System F

19
Erasure and Evaluation Order
  • Many polymorphic languages adopt a type-erasure
    semantics after the typechecking phase all the
    types are erased and the resulting untyped terms
    are interpreted or compiled
  • If the language includes side-effecting features
    such as mutable reference cells or exceptions,
    the type-erasure function is defined differently
  • Example exception-raising primitive
  • Term
    evaluates to 0
  • ( is syntactic value, body is
    never evaluated)
  • Its erasure raises
    an exception when evaluated

20
Erasure and Evaluation Order II
  • New form of erasure erase a type abstraction to
    a term-abstraction
  • where is some arbitrary
    untyped value

21
Fragments of System F
  • Fragments with some possibilities of type
    reconstruction are used for language design
  • Let-polymorphism of ML (prenex polymorphism)
    type variables range only over quantifier-free
    types, quantified types are not allowed to appear
    on the left-hand sides of arrows.
  • Rank-2 polymorphism no pass from root of the
    type to a quantifier passes to the left of 2
    or more arrows, when the type is drawn as a tree
  • Rank-2
  • Not of rank-2

22
Reference
  • B.C.Pierce, Types and Programming Languages, The
    MIT Press, Cambridge (Massachusetts), London
    (England), 2002
Write a Comment
User Comments (0)
About PowerShow.com