Lesson 10 Type Reconstruction - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

Lesson 10 Type Reconstruction

Description:

typing with constraint sets (type equations) unification: solving constraint sets ... We correct this unsoundness by only allowing polymorphic ... – PowerPoint PPT presentation

Number of Views:21
Avg rating:3.0/5.0
Slides: 25
Provided by: davidma84
Category:

less

Transcript and Presenter's Notes

Title: Lesson 10 Type Reconstruction


1
Lesson 10Type Reconstruction
  • 2/26
  • Chapter 22

2
Type Reconstruction
  • substitutions
  • typing with constraint sets (type equations)
  • unification solving constraint sets
  • principlal types
  • let polymorphism

3
Type substitutions
Language ??Bool, Nat with type variables A
type substitution ? is a finite mapping from type
variables to types. ? X gt Nat -gt Nat, Y
gt Bool, Z gt X -gt Nat Type substitutions can
be applied to types ?T ?(X -gt Z) (Nat
-gt Nat) -gt (X -gt Nat) This extends pointwise to
contexts ? ?
4
Type substitutions
Language ??Bool, Nat with type variables A
type substitution ? is a finite mapping from type
variables to types. ? X gt Nat -gt Nat, Y
gt Bool, Z gt X -gt Nat Type substitutions can
be applied to types ?T ?(X -gt Z) (Nat
-gt Nat) -gt (X -gt Nat) This extends pointwise to
contexts ? ? Composition of substitutions
? o ?(X) ? (? X) if X ? dom ?
? X otherwise
5
Substitutions and typing
Thm If ? - t T, then ?? - ?t ?T for any
type subst. ?. Prf induction on type derivation
for ? - t T.
6
"Solving" typing problems
  • Given ? and t, we can ask
  • For every ?, does there exist a T s.t. ?? - ?t
    T?
  • Does there exist a ? and a T s.t. ?? - ?t T?
  • Question 1 leads to polymorphism, where T ?T'
    and
  • - t T'. The type variables are "quantified".
  • Question 2 is the basis for type reconstuction
    we think
  • of the type variables as unknowns to be solved
    for.
  • Defn A solution for (?,t) is a pair (?, T) s.t.
    ?? - ?t T.

7
Example solutions of a typing problem
(?, ?xX. ?yY. ?zZ . (x z) (y z)) has
solutions X gt Nat -gt Bool -gt Nat, Y gt
Nat -gt Bool, Z gt Nat X gt X1 -gt X2 -gt
X3, Y gt X1 -gt X2, Z gt X1
8
Constraints
A constraint set C is a set of equations between
types. C Si Ti i ? 1,..,n. A
substitution ? unifies (or satisfies) a
constraint set C if ?Si ?Ti for every equation
Si Ti in C. A constraint typing relation ?
- t T C ? where ? is a set of "fresh" type
variables used in the constraint set C. This
relation (or judgement) is defined by a set of
inference rules.
9
Constraint inference rules
Inference rule for application
  • - t1 T1 C1 ? 1 ? - t2 T2 C2 ? 2
  • ? 1 ? ? 2 ? 1 ? FV(T2) ? 2 ? FV(T1) ?
  • X ? ? 1, ? 2, t1, t2, T1, T2, C1, C2, ?
  • C C1 ? C2 ? T1 T2 -gt X
  • ? ? 1 ? ? 2 ? X
  • - t1 t2 X C ?

10
Constraint solutions
Defn Suppose ? - t S C ?. A solution for
(?, t, S, C) is a pair (?, T) s.t. ? satisfies C
and T ?S. Thm Soundness of Constraint
Typing Suppose ? - t S C X. If (?, T) is a
solution for (?, t, S, C) then it is also a
solution for (?, t), i.e. ?? - ?t T. Thm
Completeness of Constraint Typing Suppose ? -
t S C ?. If (?, T) is a solution for (?, t)
then there is a solution (?', T) for (?, t, S, C)
s.t. ?'\? ?. Cor Suppose ? - t S C ?.
There is a soln for (?, t) iff there is a
solution for (?, t, S, C).
11
Unification
Defn ? lt ?' if ?' ? o ? for some ?. Defn
A principle unifier (most general unifier) for a
constraint set C is a substitution ? that
satisfies C s.t. ? lt ?' for any other ?' that
satifies C.
12
Unification algorithm
unify C if C ? then else let S T ?
C' C in if S T then unify(C') else if S
X and X ? FV(T) then unify(X gt TC') o X
gt T else if T X and X ? FV(S) then
unify(X gt SC') o X gt S else if S S1 -gt
S2 and T T1 -gt T2 then unify(C' ? S1
T1, S2 T2) else fail Thm unify always
terminates, and either fails or returns the
principal unifier if a unifier exists.
13
Principal Types
Defn A principal solution for (?, t, S, C) is a
solution (?, T) s.t. for any other solution (?',
T') we gave ? lt ?'. Thm Principal Types If
(?, t, S, C) has a solution, then it has a
principal one. The unify algorithm can be used
to determine whether (?, t, S, C) has a solution,
and if so it calculates a principal one.
14
Implicit Annotations
We can extend the syntax to allow lambda
abstractions without type annotations
?x.t. The corresponding type constraint rule
supplies a fresh type variable as an implicit
annotation.
X ? ?
?, x X - t1 T C ?
(CT-AbsInf)
?, - ?x X. t1 X -gt T C (? ? X)
15
Let Polymorphism
let double ?f Nat -gt Nat. ?x Nat. f(f x) in
double (?x Nat. succ x) 2 let double ?f Bool
-gt Bool. ?x Bool. f(f x) in double (?x not x)
false An attempt at a generic double let
double ?f X -gt X. ?x X. f(f x) in let a
double (?x Nat. succ x) 2 in let b double (?x
not x) false gt X -gt X Nat -gt Nat Bool
-gt Bool
16
Macro-like let rule
let double ?f X -gt X. ?x X. f(f x) in let a
double (?x Nat. succ x) 2 in let b double (?x
not x) false could be typed as let a (?f X
-gt X. ?x X. f(f x)) (?x Nat. succ x) 2 in let b
(?f X' -gt X'. ?x X'. f(f x)) (?x not x)
false or, using implicit type annotations let
a (?f. ?x. f(f x)) (?x Nat. succ x) 2 in let b
(?f. ?x. f(f x)) (?x not x) false
17
Macro-like let rule
? - x gt t1t2 T2
? - t1 T1
(T-LetPoly)
? - let x t1 in t2 T2
The substitution can create multiple independent
copies of t1, each of which can be typed
independently (assuming implicit annotations,
which introduce separate type variables for each
copy).
18
Type schemes
Add quantified type schemes T X
Bool Nat T -gt T P T ?X .
P Contexts become finite mappings from term
variables to type schemes ? ? ?,
x P Examples of type schemes Nat, X -gt
Nat, ?X. X -gt Nat, ?X.?Y. X -gt Y -gt X
19
let-polymorphism rules
?, x ?? .T1 - t2 T2
? - t1 T1
(T-LetPoly)
? - let x t1 in t2 T2
where ? ' are the type variables free in T1 but
not free in ?
(T-PolyInst)
?, x ?? .T - x ? gt ? 'T where ? ' is a
set of fresh type variables
20
let-polymorphism example
double ?X. (X -gt X) -gt X -gt X
let double ?f. ?x. f(f x) in let a double
(?x Nat. succ x) 2 in let b double (?x not x)
false in (a,b)
(Y -gt Y) -gt Y -gt Y
(Z -gt Z) -gt Z -gt Z
Then unification yields Y gt Nat, Z gt Bool.
21
let-polymorphism and references
Let ref, !, and be polymorphic functions with
types ref ?X. X -gt Ref(X) ! ?X.
Ref(X) -gt X ?X. Ref(X) X -gt Unit let
r ref(?x. x) in let a r (?x Nat. succ
x) in let b !r false in ()
r ?X. Ref(X -gt X)
Ref(Nat -gt Nat)
Ref(Bool -gt Bool)
We've managed to apply (?x Nat. succ x) to
false!
22
The value restriction
We correct this unsoundness by only allowing
polymorphic generalization at let declarations if
the expression is a value. This is called the
value restriction. let r ref(?x. x) in let a
r (?x Nat. succ x) in let b !r false in ()
r Ref(X -gt X)
Ref(Nat -gt Nat) X gt Nat
Ref(Nat -gt Nat)
Now we get a type error in " !r false ".
23
Let polymorphism with recursive values
Another problem comes when we add recursive
value definitions. let rec f ?x. t in
... is typed as though it were written let
f fix(?f. ?x. t) in ... where fix ?X. (X -gt
X) -gt X except that the type of the outer f can
be generalized. Note that the inner f is
?-bound, not let bound, so it cannot be
polymorphic within the body t.
24
Polymorphic Recursion
What can we do about recursive function
definitions where the function is polymorphic and
is used polymorphically in the body of it's
definition? (This is called polymorphic recursion
.) let rec f ?x. (f true f 3 x) Have
to use a fancier form of type reconstruction
the iterative Mycroft-Milner algorithm.
Write a Comment
User Comments (0)
About PowerShow.com