Download Type Inference, Wands Algorithm - Principles Programming Language | COM S 342 and more Study notes Computer Science in PDF only on Docsity!
Type Inference Com S 342
Overview:^ ^ The type inference problem^ ^ Typed lambda terms, type assertions, and typing rules^ ^ Wand’s algorithm^ ^ Unification of type equationsReferences:^ ^ Mitchell Wand, “A Simple Algorithm
and Proof for Type Inference”,
Fundamenta Informaticae, 10:115-122, 1987 J. Roger Hindley, “Basic Simple Type Theory”, Cambridge University Press, 1997 John C. Mitchell, “Foundations for Programming Languages”, MIT Press, 1996
The Type Inference Problem Com S 342
^ The type inference problem can be stated as follows:“Given a term of the untyped lambda calculus, find all terms of the typedlambda calculus, which yield the given term when the type information onbound variables is deleted.”Since such terms can differ only in their types, this problem is sometimesreferred to as finding the “possible typings” of a term. ^ This problem was first formulated and solved by Curry (in the 1930’s) andHindley (1969). ^ Milner (1978) was the first to make the connection with the unificationproblem formulated by Robinson (1965).
Type Inference for Closed Terms Com S 342
^ The type inference problem for closed term can be stated as follows:Given a closed lambda-term^ e , find all types
t^ such that ( ∅, e , t ).
^ The set of type expressions is defined by adding type variables, written
τ, to
the set^ t^ of types. Theorem :^ ^ Given a closed lambda-term e, it is decidable whether there exists a type
t^ such that^ (^ ∅,^ e ,^ t^ ). If there is any such^ t , then there is a type expression
u^ such that the typings of^ e are precisely the types of the form^ σ u^
for all substitutions^ σ.
Wand’s Algorithm - Skeleton Com S 342
^ Input:A lambda-terme.^0 ^ Initialization:Set E =^ ∅^ and G = {(Γ,e, t)}, where t^000
is a type variable and^ Γmaps the 0 0
free variables ofe^ to other distinct type variables.^0 Loop Step:If G =^ ∅, then halt and return E. Otherwise, choose a subgoal (
Γ,e,t) from G, delete it from G, and add to E and G new verification conditions andsubgoals, as specified in an action table.End of Skeleton
^ Consider (λx. (λy. (λz. ((x z) (y z)) ) ) ){(∅, (λx. (λy. (λz. ((x z) (y z)) ) ) ),τ
The Algorithm PTPT(x) = {x :^ τ}^ ∴^ x :^ τ PT(e^ e) =^ let^ Γ ∴^ e^ ’ :^ τ^ = PT(e)^1 211 Γ’^ ∴^ e’ :^ σ^ = PT(e)^22 S = unify ({α^ =^ β^ | x :^ α ∈ Γ^ and x :^ β ∈ Γ’}^ ∪^ {τ^ = (σ^ Æ^ Com S 342
ρ)})
where^ ρ^ is a fresh type variable in SΓ ∪ SΓ’ ∴^ S(e^ ’ e^ ’) : Sρ^12 S: set of substitutions PT(λx. e) = let Γ ∴ e’ : ρ = PT(e) in if x : τ ∈ Γ then Γ – {x :^ τ}^ ∴ λx :^ τ. e’ : (τ^ Æ^ ρ) else Γ ∴ λx :^ σ. e’ : (σ^ Æ^ ρ) where^ σ^ is a fresh type variable