Type Reconstruction - McMaster University

Download Report

Transcript Type Reconstruction - McMaster University

Type Reconstruction
and Universal Types
Olesya Peshko
CAS 706
McMaster University
March 31, 2004
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
to types
Apply
to type
from TVars
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
Application of Substitution
Constraint-Based Typing


Constraint set
is a set of equations
– equations between type expressions
Substitution
if
and
unifies


To check if


unifies an equation
are identical
if it unifies every equation in
is typable under context
Analyze the term and collect the constraints
Look for substitutions
that satisfy
:
Constraint Typing Rules
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
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
If
is a unifier for
with
, then
, then
is a unifier for
Unification Algorithm
Principal Types




Principal solution for
is a solution
such that, whenever
is also a
solution for
, we have
If
type of
is a principal solution,
under
Theorem: If
has a principal one
is a principal
has any solution, then it
Corollary: It is decidable whether
solution
has a
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
Varieties of Polymorphism

Parametric – single piece of code is typed “generically”,
variables used in place of types, particular types instantiated
as needed



Ad-hoc – polymorphic value exhibits different behaviors when
“viewed” at different types



Impredicative (first-class)
ML-style (Let-polymorphism)
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
term’s behavior)
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
System F. Syntax
System F. Evaluation
System F. Typing
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
Erasure

Type erasure function maps System F terms to
untyped lambda-terms
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
Erasure and Evaluation Order



Many polymorphic languages adopt a typeerasure 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
when evaluated
raises an exception
Erasure and Evaluation Order II

New form of erasure – erase a type abstraction to a
term-abstraction
where
is some arbitrary untyped value
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:
Reference

B.C.Pierce, Types and Programming Languages,
The MIT Press, Cambridge (Massachusetts),
London (England), 2002