α ∈ α ∈ τ ∈ τ ∈ τ τ ∈ τ τ τ τ

advertisement
PROCEEDINGS OF THE YEREVAN STATE UNIVERSITY
Physical and Mathematical Sciences
2009, № 3, p. 42–51
Informatics
ON THE TYPE CORRECTNESS OF POLYMORPHIC λ -TERMS. 1
A. H. ARAKELYAN∗
Chair of Programming and Information Thechnologies, YSU
In this paper polymorphic lambda terms are considered, where no type
information is provided for the variables. The aim of this work is to extend the
algorithm of typification [1] of such terms introducing type constants and term
constants.
Keywords: type, term, constraint, skeleton, expansion, typing.
1. Introduction. Types are used in programming languages to analyze
programs without executing them, for purposes such as detecting programming
errors earlier, for doing optimizations etc. In some programming languages no
explicit type information is provided by the programmer, hence some system of
type inference is required to recover the lost information and do compile time type
checking. One of such type inference systems is the well known Hindley/Milner
system [2], used in languages such as Haskell, SML, OCaml etc. Important
property of type systems is the property of principal typings [3, 4], which allows
the compiler to do compositional analysis, i.e. analysis of modules in absence of
information about other modules [3, 4]. Unfortunately Hindley/Milner system
doesn't support the property of principal typings [3]. In this paper we consider a
type inference system, called System E [1, 5]. In section 2 an extended version of
System E is presented, which adds type constants and term constants to the original
System E.
2. Definitions Used and Previous Results.
2.1. Definitions used. Let TypeVariable be a countable set of type
variables, TypeConstant be a finite set of built-in types, TermVariable be a
countable set of term variables, Constant be a countable set of constants and
( ExpansionVariable, U ) be a countable totally ordered set of expansion variables.
Definition 2.1. The set of types Type is defined as follows:
1. ω ∈ Type ; 2. If α ∈ TypeVariable , then α ∈ Type ; 3. If s ∈ TypeConstant ,
then s ∈ Type ; 4. If e ∈ ExpansionVariable and τ ∈ Type , then eτ ∈ Type ;
5. If τ 1 ,τ 2 ∈ Type , then (τ 1 → τ 2 ) ∈ Type and (τ 1 ∩ τ 2 ) ∈ Type .
The set of expansions Expansion is defined as follows:
∗
E-mail: ara_arakelyan@yahoo.com
Proc. of the Yerevan State Univ. Phys. and Mathem. Sci., 2009, № 3, p. 42–51.
43
1. ω ∈ Expansion ; 2. If σ is a substitution (we will define substitutions
later), then σ ∈ Expansion ; 3. If e ∈ ExpansionVariable and E ∈ Expansion , then
eE ∈ Expansion ; 4. If E1 , E2 ∈ Expansion , then ( E1 ∩ E2 ) ∈ Expansion .
The set of terms Term is defined as follows:
1. If x ∈ TermVariable , then x ∈ Term ; 2. If c ∈ Constant , then c ∈ Term ;
3. If x ∈ TermVariable and M ∈ Term, then (λ x.M ) ∈ Term; 4. If (λ x.M ) ∈ Term,
then ( M 1M 2 ) ∈ Term .
The set of constraints Constraint is defined as follows:
1. ω ∈ Constraint ; 2. If τ 1 ,τ 2 ∈ Type , then (τ 1 = τ 2 ) ∈ Constraint ;
3. If e ∈ ExpansionVariable and Δ ∈ Constraint , then eΔ ∈ Constraint ;
4. If Δ1 , Δ 2 ∈ Constraint , then (Δ1 ∩ Δ 2 ) ∈ Constraint .
The set of skeletons Skeleton is defined as follows:
1. If M ∈ Term , then ω M ∈ Skeleton ; 2. If c ∈ Constant and τ ∈ Type ,
then c:τ ∈ Skeleton ; 3. If x ∈ TermVariable and τ ∈ Type , then x:τ ∈ Skeleton ;
4. If e ∈ ExpansionVariable and Q∈Skeleton , then eQ ∈ Skeleton ; 5. If x ∈TermVariable
and Q ∈ Skeleton , then (λ x.Q ) ∈ Skeleton ; 6. If Q1 , Q2 ∈ Skeleton , then
(Q1 ∩ Q2 ) ∈ Skeleton ; 7. If Q1 , Q2 ∈ Skeleton and τ ∈ Type , then (Q1Q2 ):τ ∈ Skeleton .
We assume that:
1. ((T1 ∩ T2 ) ∩ T3 ) = (T1 ∩ (T2 ∩ T3 )) ; 2. (T1 ∩ T2 ) = (T2 ∩ T1 ) ; 3. (ω ∩ T ) = T ;
4. e(T1 ∩ T2 ) = (eT1 ∩ eT2 ) ; 5. eω=e, where T1 , T2 , T3 ∈Type or T1 , T2 , T3 ∈ Constraint
and e ∈ ExpansionVariable .
Definition 2.2. Let α1 ,…,α n ∈ TypeVariable, e1 ,…, em ∈ ExpansionVariable,
τ 1 ,…,τ n ∈ Type, E1 ,…, Em ∈ Expansion, n ≥ 0, m ≥ 0 . The set of pairs
{α1 := τ1 ,…,α n := τ n , e1 := E1 ,… , em := Em } is called a substitution, if the following
conditions are satisfied:
1. i ≠ j ⇒ α i ≠ α j , where i, j=1,… , n ; 2. i ≠ j ⇒ ei ≠ e j , where i, j=1,… ,m .
We denote by ε the empty substitution.
Definition 2.3. Let e1 ,… , en ∈ ExpansionVariable, n ≥ 0 . Then e1e2 … en is
called E-path and is denoted by e . In case of n=0 E-path will be denoted by ε .
Definition 2.4. Let e1=e1e2 … en and e2 =e1′e2′ … em′ , where n, m ≥ 0 . If ∃i s.t.
1 ≤ i ≤ min(n, m) and e j =e′j ∀j=1,… , i − 1 and ei ≺ ei′ ( ei ei′ ), then e1 U e2
( e1 V e2 ). Else if n ≤ m (n ≥ m) , then e1 U e2 ( e1 V e2 ).
It is easy to see that the set of E-paths with order U is a totally ordered set.
Definition 2.5. A constraint Δ is singular, if it was constructed without using
operation ∩ .
Remark 2.1. Taking into account the definition of constraints, it is
easy to see that each constraint has one of the following forms:
Δ=e1 (τ11 = τ 21 ) ∩ … ∩ en (τ1n = τ 2n ) , n ≥ 1 , or Δ=ω , i.e. each constraint is an
intersection of zero or more singular constraints.
44
Proc. of the Yerevan State Univ. Phys. and Mathem. Sci., 2009, № 3, p. 42–51.
Let us introduce the following notation: E − Path(e (τ 1 = τ 2 )) = e .
Definition 2.6. A constraint Δ is solved, iff it is of the form
Δ=e1 (τ1 = τ 1 ) ∩ … ∩ en (τ n = τ n ) n ≥ 1 or Δ=ω . The unsolved part of a constraint
Δ , written unsolved (Δ ) , is the smallest part of Δ such that Δ=unsolved ( Δ) ∩ Δ′
and Δ′ is solved. Consequently Δ′ is the greatest solved part of a Δ , it is called
solved part of a constraint Δ and is written solved (Δ ) . So each constraint is an
intersection of its solved and unsolved parts: Δ=unsolved (Δ ) ∩ solved (Δ ).
As we will see later, the Skeleton is an object, that contains all information
about the type inference tree of some term. We can calculate the term
corresponding to the Skeleton, using the following function.
Definition 2.7. The function term:Skeleton → Term is defined as follows:
1. term(ω M ) = M ; 2. term(c:τ ) = c ; 3. term( x:τ ) = x ; 4. term(eQ) = term(Q) ;
6. term((Q1Q2 ):τ ) = (term(Q1 )term(Q2 )) ;
5. term((λ x.Q)) = (λ x.term(Q)) ;
7. If term(Q1 )=term(Q2 ) , then term((Q1 ∩ Q2 ))=term(Q1 ) , else term((Q1 ∩ Q2 ))
is undefined.
Definition 2.8. The skeleton Q is well formed, iff term(Q ) is defined, i.e.
the corresponding term of skeleton exists.
Convention 2.1. Henceforth only well formed skeletons are considered.
The following two definitions define the application of expansions to types,
constraints, expansions and skeletons.
Definition 2.9. Let X ∈ Type ∪ Constraint ∪ Expansion ∪ Skeleton and σ
be a substitution. Then the application of σ to X is denoted by [σ ] X and is
obtained from σ and X by the following rules:
1. If α := τ ∈ σ , then [σ ]α =τ ; 2. If α := τ ∉ σ ∀τ ∈ Type , then [σ ]α =α ;
3. [σ ]s=s ; 4. [σ ]ω=ω ; 5. If e := E ∈ σ , then [σ ] eY=[ E ]Y ; 6. If e := E ∉ σ
∀E ∈ Expansion , then [σ ]eY=eY ; 7. [σ ](τ1 →τ 2 ) = ([σ ]τ1 → [σ ]τ 2 ) ; 8. [σ ]( X1 ∩ X 2 ) =
=([σ ] X 1 ∩ [σ ] X 2 ) ; 9. [σ ]{α1 :=τ1,…,αn :=τ n , e1 := E1,…, em := Em} = {α1 := [σ ]τ1 ,…,α n :=
= [σ ]τ n , e1 := [σ ]E1 ,… , em := [σ ]Em } ∪ {α := τ |α ∉ {α1 ,… ,α n } and α := τ ∈σ } ∪
∪{e := E|e ∉{e1 ,…, em } , e := E ∈σ } ; 10. [σ ](τ1 = τ 2 ) = ([σ ]τ1 = [σ ]τ 2 ) ; 11. [σ ]ω M =ω M ;
12. [σ ]x:τ =x:[σ ]τ ; 13. [σ ]c:τ =c:[σ ]τ ; 14. [σ ](λ x.Q) = (λ x.[σ ]Q); 15. [σ ](Q1Q2 ):τ =
=([σ ]Q1[σ ]Q2 ):[σ ]τ , where α1,…,αn ,α ∈TypeVariable, e1,…, em , e ∈ ExpansionVariable,
c ∈ Constant , s ∈ TypeConstant , E1 ,… , Em , E ∈ Expansion, τ1,…,τ n ,τ ,τ1,τ 2 ∈Type,
Y, X1, X2 ∈Type ∪Constraint ∪ Expansion ∪ Skeleton, M ∈Term, n, m ≥ 0, Q,Q1,Q2 ∈Skeleton,
and [ E ]Y will be defined now.
Definition 2.10. Let X ∈ Type ∪ Constraint ∪ Expansion ∪ Skeleton and
E ∈ Expansion . Then the application of E to X is denoted by [ E ] X and is
obtained from E and X by the following rules:
1. If E=ω , then [ E ]Y=ω , where Y ∈ Type ∪ Constraint ∪ Expansion ;
2. If E=ω , then [ E ]Q=ω term(Q) , where Q ∈ Skeleton ; 3. If E=σ , then [ E ] X=[σ ] X ,
where σ is a substitution; 4. If E=eE ′ , then [ E ] X=e[ E ′] X , where
Proc. of the Yerevan State Univ. Phys. and Mathem. Sci., 2009, № 3, p. 42–51.
45
e ∈ ExpansionVariable and E ′ ∈ Expansion ; 5. If E=( E1 ∩ E2 ) , then
[ E ] X=([ E1 ] X ∩ [ E2 ] X ) , where E1 , E2 ∈ Expansion .
Let us introduce the following notations:
1. e/ σ ={e := eσ } ; 2. If e=e1e2 … en , then e / σ =e1 /e2 / … /en / σ , n ≥ 0 ,
where e1 ,…, en , e ∈ ExpansionVariable and σ is a substitution. It is easy to see
that [e/ σ ]eX=e[σ ] X , [e / σ ]eX=e[σ ] X , where
X ∈ Type ∪ Constraint ∪ Expansion ∪ Skeleton.
Lemma 2.1 (property of expansions). Let E1 , E2 , E ∈ Expansion and
X ∈ Type ∪ Constraint ∪ Expansion ∪ Skeleton , then [[ E1 ]E2 ] X=[ E1 ][ E2 ] X and
[ E ]ε =E .
Definition 2.11. A total function A : TermVariable → Type is called environment, if the following set is finite: {x|x ∈ TermVariable and A( x) ≠ ω} . Environment A can be written also as a set of pairs A={( x, A( x )) | x ∈ TermVariable} .
Let us introduce the following notations:
1. A[ x → τ ] = {( y, A( y )) | y ∈ TermVariable and y ≠ x} ∪ {( x,τ )} ; 2. A ∩ B=
3.
={( x,( A( x) ∩ B( x))) | x ∈ TermVariable} ;
eA={( x, eA( x)) | x ∈TermVariable} ;
4. [ E ] A={( x,[ E ] A( x)) | x ∈ TermVariable} ; 5. envω ={( x, ω )|x ∈ TermVariable} ,
E ∈ Expansion , e ∈ ExpansionVariable ,
where A, B are environments,
x ∈ TermVariable and τ ∈ Type .
Definition 2.12. The set CType ⊂ Type is the set, satisfying the following
conditions:
1. If s ∈ TypeConstant , then s ∈ CType ; 2. If s ∈ TypeConstant and
τ ∈ CType , then ( s → τ ) ∈ CType .
Definition 2.13. The mapping Σ : Constant → CType is called a constant
table.
Convention 2.2. In order not to mention the constant table later, let us
suppose that henceforth we are using some fixed constant table.
2.2. Type inference rules.
Definition 2.14. The quintuple of term, skeleton, environment, type and
constraint, written ( M Q ) : ( A A τ ) / Δ , is called a judgement. The intended
meaning of judgement is that Q is a proof that M has typing ( A A τ ) , provided
that the constraint Δ is solved.
Now let us introduce type inference rules, that are used to derive
judgements. Type inference rules are the following:
, [CONST]
,
( x x:τ ) : (envω [ x → τ ] A τ ) / ω
(c c:τ ) : (envω A τ ) / ω
where τ = Σ (c) ,
( M Q) : ( A A τ ) / Δ
,
[OMEGA]
,
[E-VAR]
( M eQ ) : (eA A eτ ) / eΔ
( M ω M ) : (envω A ω ) / ω
( M Q) : ( A A τ ) / Δ
[ABS]
,
((λ x. M ) (λ x. Q )) : ( A[ x → ω ] A ( A( x) → τ )) / Δ
[VAR]
Proc. of the Yerevan State Univ. Phys. and Mathem. Sci., 2009, № 3, p. 42–51.
46
( M 1 Q1 ) : ( A1 A τ 1 ) / Δ1 and ( M 2 Q2 ) : ( A2 A τ 2 ) / Δ 2
,
(( M 1M 2 ) (Q1Q2 ):τ ) : ( A1 ∩ A2 A τ ) / Δ1 ∩ Δ 2 ∩ (τ 1 = (τ 2 → τ ))
( M Q1 ) : ( A1 A τ 1 ) / Δ1 and ( M Q2 ) : ( A2 A τ 2 ) / Δ 2
[INT]
.
( M (Q1 ∩ Q2 )) : ( A1 ∩ A2 A (τ 1 ∩ τ 2 )) / Δ1 ∩ Δ 2
Definition 2.15. The pair ( A A τ ) of an environment and a type is called
typing of the term M , if ∃Q ∈ Skeleton and ∃Δ ∈ Constraint s.t. judgement
( M Q ) : ( A A τ ) / Δ is inferable and Δ is solved.
Definition 2.16. The pair ( A A τ ) of an environment and a type is called a
principal typing of the term M , if ( A A τ ) is a typing of M , and if ( A′ A τ ′) is a
typing of M , then ∃E ∈ Expansion s.t. A′=[ E ] A and τ ′=[ E ]τ . In other words,
all typings of the term are obtained from principal typing through expansion.
Lemma 2.2. If the judgement ( M Q) : ( A A τ ) / Δ is inferable, then the judgement ( M [ E ]Q ) : ([ E ] A A [ E ]τ ) / [ E ]Δ is also inferable for any expansion E .
The next lemma shows that each skeleton contains information about one
and only one inferable judgement, i.e. represents one and only one derivation tree
of some judgement.
Lemma 2.3. Let Q ∈ Skeleton . Then there exist one and only one term M ,
an environment A , a type τ and a constraint Δ s.t. the judgement
( M Q ) : ( A A τ ) / Δ is inferable and M=term(Q ) .
This Lemma allows us to introduce the following functions: env(Q) = A ,
type(Q ) = τ , constraint (Q)=Δ , typing (Q ) = ( A A τ ) . It is easy to present algorithms of calculating functions env , type , constraint and typing .
2.3. Initial skeleton. Type inference algorithm, that will be introduced in
section 2.6, starts term typification by constructing initial skeleton of that term.
Definition 2.17. Let us fix a type variable a0 and expansion variables
e0 , e1 , e2 such that e0 ≺ e1 ≺ e2 . The function initial : Term → Skeleton maps
[APP]
terms
to
skeletons
as
follows:
initial ( x) = x:a0 ,
initial (c) = c:Σ ( c ) ,
initial ((λ x.M ))=(λ x.e0initial ( M )) , initial (( M1M 2 )) = (e1initial ( M1 ) e2initial (M 2 )):a0 ,
where x ∈ TermVariable, c ∈ Constant and M , M 1 , M 2 ∈ Term . The range of
initial is denoted by InitialSkeleton and elements of InitialSkeleton are called
initial skeletons.
Lemma 2.4. Let P ∈ InitialSkeleton . Then solved (constraint ( P )) = ω , and
∀x ∈ TermVariable ∃n ≥ 0 and ∃e1 ,…, en E-paths s.t. env( P)( x)=e1a0 ∩ … ∩ en a0 ,
and no ei is a proper prefix of e j , i, j ∈{1,… , n} .
From the first part of Lemma 2.4 it is easy to see that all singular constraints,
which are a part of constraint ( P ) , are unsolved, where P is an initial skeleton of
some term. In section 2.6 we will see, that the type inference algorithm tries to
solve some singular constraints by applying substitutions on them, and it starts
solving the process from singular constraints, which are a part of constraint ( P) .
Unification rules, introduced in the next section, are used to produce substitutions
for solving singular constraints.
Proc. of the Yerevan State Univ. Phys. and Mathem. Sci., 2009, № 3, p. 42–51.
47
2.4. Unification rules. In this section three unification rules will be
presented.
Definition 2.18. The set Type′ ⊂ Type is the set of types that are constructed
without using type constants and operation → .
Definition 2.19. The function Extract E : Type′ → Expansion maps types
from the set Type′ to expansions as follows: Extract E (ω )=ω , Extract E (α )=ε ,
Extract E (eτ )=eExtract E (τ ),
Extract E ((τ 1 ∩ τ 2 ))=( Extract E (τ 1 ) ∩ Extract E (τ 2 )) ,
where α ∈ TypeVariable , e ∈ ExpansionVariable and τ ,τ1 ,τ 2 ∈ Type′ .
Definition 2.20. The function ExtractS : Type′ × Type → Substitution maps
pairs of type from Type′ , and type to substitutions as follows: ExtractS (ω ,τ ′)=ε ,
ExtractS (α ,τ ′)={α := τ ′} , ExtractS (eτ ,τ ′) = e/ExtractS (τ ,τ ′) , ExtractS ((τ1 ∩ τ 2 ),τ ′) =
= [ ExtractS (τ 2 ,τ ′)]ExtractS (τ 1 ,τ ′) ,
where
τ ′ ∈ Type, e ∈ ExpansionVariable ,
α ∈ TypeVariable and τ ,τ1 ,τ 2 ∈ Type′ .
Definition 2.21 ( unify β rule). Let Δ=e (e1 (e0τ 0 → e0τ 1 ) = (e2τ 2 → a0 )) be a
singular constraint, where τ 0 ∈ Type′ and τ 1 ,τ 2 ∈ Type . Then the rule unifyβ is
applicable to Δ , and the result of this application is the following substitution: σ =e / {a0 := [σ ′]τ 1 , e1 := {e0 := σ ′},e2 := E ′} , where E ′ = Extract E (τ 0 ) and
σ ′=ExtractS (τ 0 ,τ 2 ).
unify
β
The application of the rule unifyβ is written as Δ ⎯⎯⎯
→σ .
Now let us explain the meaning of the rule unifyβ . Let ((λ x.M 1 ( M 2 )) be a
subterm of some term M , where x ∈ TermVariable and M 1 , M 2 ∈ Term . The
( M1 )
initial skeleton of that subterm will be (e1 (λ x.e0 P1 )e2 P2 ):a0 , where P=initial
1
and P2 =initial ( M 2 ) . The part of constraint (initial ( M )) that corresponds the initial
skeleton of subterm mentioned above will be e (e1 (e0τ 0 → e0τ 1 ) = (e2τ 2 → a0 )) ,
where τ 1 corresponds to the type of M1 (τ 1=type( P1 )) , τ 2 corresponds to the type
of M 2 (τ 2 =type( P2 )) and τ 0 corresponds to the type of x in term M 1
(τ 0 =env( P1 )( x)) . Before applying substitution created by the rule unifyβ , the type
a0 is associated with any free occurrence of variable x in the term M 1 . After
applying substitution, all that a0 type variables will be replaced with the type τ 2
(this replacement is done by the substitution created by the function ExtractS ), and
the type of x in term M 1 will be changed. The same type is obtained, when
applying substitution created by the function Extract E to the type τ 2 (it makes as
many copies of type τ 2 as there are free occurrences of variable x in term M1 ). It
is easy to see that the process described above is very similar to the one step β reduction. Next two lemmas show the exact correspondence of the rule unifyβ
with β -reduction.
Lemma 2.5 (correspondence with β -reduction). Let M ∈ Term and
Proc. of the Yerevan State Univ. Phys. and Mathem. Sci., 2009, № 3, p. 42–51.
48
P=initial ( M ) . If
constraint ( P ) = Δ ∩ Δ′ , where Δ is a singular constraint, to
unify
β
→ σ , then ∃M ′ ∈ Term s.t.
which the rule unifyβ is applicable and Δ ⎯⎯⎯
constraint ( P′)=[σ ]Δ′ , env( P′) = [σ ]env( P) , type( P′) = [σ ]type( P) and M →β M ′ ,
where P′ = initial ( M ′) .
Lemma 2.6 (correspondence with
β -reduction). Let M , M ′ ∈ Term ,
P=initial ( M ) and P′ = initial ( M ′) . If M →β M ′ , then ∃Δ singular constraint s.t.
unify
β
→σ ,
constraint ( P) = Δ ∩ Δ′ the rule unifyβ is applicable to Δ and Δ ⎯⎯⎯
constraint ( P′) = [σ ]Δ′ , env( P′) = [σ ]env( P ) and type( P′) = [σ ]type( P ) .
Definition 2.22 ( unifyx rule). Let Δ=e (e1a0 = (e2τ → a0 )) be a singular
constraint, where τ ∈ Type . Then the rule unifyx is applicable to Δ ,
and
the
result
of
application
is
the
following
substitution:
σ =e / {e1 := {a0 := (e2τ → a0 ), e1 := e1e1ε , e2 := e1e2ε }} .
unify x
The application of the rule unifyx is written as Δ ⎯⎯⎯
→σ .
Now let us explain the meaning of the rule unifyx . Let ( M 1M 2 ) be a
subterm of some term M , where M 1 , M 2 ∈ Term . During the work of the type
:a0
,
inference algorithm the corresponding skeleton of that subterm can be (e1 Pe
1 2 P2 )
where P1 , P2 ∈ Skeleton and type( P1 ) = a0 . The singular constraint corresponding to
the skeleton mentioned above is e (e1a0 = (e2τ → a0 )) , where τ corresponds to the
type of M 2 , and a0 corresponds to the type of M1 in the current stage of the work
of type inference algorithm. After applying substitution σ the type of M1 will be
replaced with (e2τ → a0 ) , and the skeleton mentioned above will have the
:a0
, where type( P1′)=(e2τ → a0 ) .
following form: ( Pe
1′ 2 P2 )
Definition 2.23 ( unifyc rule). Let Δ=e (e1τ 0 = (e2τ → a0 )) be a singular constraint, where τ 0 ∈ CType and τ ∈ Type . Then the rule unifyc is applicable to Δ :
1. If τ 0 =s or τ ≠ s and τ ≠ a0 , where s ∈ TypeConstant , then application
of the rule unifyc fails;
2. If τ 0 =( s → τ ′) and τ =s , where s ∈ TypeConstant and τ ′ ∈ CType , then
the result of applying the rule unifyc is
σ=e / {a0 := τ ′, e1 := {a0 := e1a0 , e1 := e1e1ε , e2 := e1e2ε }, e2 := {a0 := e2a0 , e1 := e2e1ε , e2 := e2e2ε}};
3. If τ 0 =( s → τ ′) and τ =a0 , where s ∈ TypeConstant and τ ′ ∈ CType , then
the result of applying the rule unifyc is
σ =e / {a0 := τ ′, e1 := {a0 := e1a0 , e1 := e1e1ε , e2 := e1e2ε }, e2 := {a0 := s, e1 := e2e1ε , e2 := e2e2ε }}.
unifyc
In cases 2 and 3 application of the rule unifyc is written as Δ ⎯⎯⎯
→σ .
Now let us explain the meaning of the rule unifyc . Let ( M 1M 2 ) be a
subterm of some term M , where M 1 , M 2 ∈ Term . During the work of the type
:a0
inference algorithm the corresponding skeleton of that subterm can be (e1 Pe
,
1 2 P2 )
Proc. of the Yerevan State Univ. Phys. and Mathem. Sci., 2009, № 3, p. 42–51.
49
where P1 , P2 ∈ Skeleton and type( P1 ) = τ 0 ∈ CType . The singular constraint
corresponding the skeleton mentioned above will be e (e1τ 0 = (e2τ → a0 )) , where
τ corresponds to the type of M 2 , and τ 0 corresponds to the type of M 1 in the
current stage of work of the type inference algorithm. After applying substitution
σ the type of ( M 1M 2 ) will be replaced with τ ′ , and the type of M 2 will be
replaced with s , if needed, and skeleton mentioned above will have the following
form: ( P1′P2′):τ ′ , where type( P1′) = ( s → τ ′) and type( P2′) = s .
Next lemma shows, that the substitution created by the rule unifyβ , unify x
or unifyc solves the corresponding singular constraint.
Lemma 2.7. Let Δ be a singular constraint, to which the rule unify y is
y
→ σ , where y ∈ {β , x, c} . Then [σ ]Δ is solved.
applicable and Δ ⎯⎯⎯
unify
Let us now consider singular constraints that are a part of constraint
corresponding to some initial skeleton. Next lemma shows, that one and only one
unificitation rule is applicable to each of that singular constraints.
Lemma 2.8. Let M ∈ Term , P=initial ( M ) and constraint (P) = Δ1 ∩…∩Δn ,
n ≥ 1 , where Δ i is a singular constraint, i=1,… , n . Then one and only one rule
from rules unifyβ , unify x and unifyc is applicable to each Δi , i = 1,… , n .
2.5. Unification algorithm. The unification algorithm tries to solve the given
constraint that initially corresponds to some initial skeleton. It is called from the
type inference algorithm and in fact is does the main work of type inference.
Definition 2.24. Let Δ=Δ1 ∩ … ∩ Δ n ∈ Constraint , n ≥ 1 , where Δ1 ,… , Δ n
are singular constraints and E − Path( Δ i ) ≠ E − Path(Δ j ) , i, j=1,… , n . Then
leftmost/outermost constraint of Δ , written as LO (Δ ) , is a singular constraint from
Δ1 ,… , Δ n that has the least E-path, i.e. LO(Δ ) = Δ k , where k ∈{1,…, n} and
E − Path(Δ k ) ≺ E − Path(Δ i ) ∀i ∈{1,… , n} \ {k} .
Definition 2.25. Let Δ=Δ1 ∩ … ∩ Δ n ∈ Constraint , n ≥ 1 , where Δ1 ,… , Δ n
are singular constraints and E − Path( Δ i ) ≠ E − Path(Δ j ) , i, j=1,… , n . Then
rightmost/innermost constraint of Δ , written as RI (Δ ) , is a singular constraint
from Δ1 ,… , Δ n that has the greatest E-path, i.e. RI (Δ) = Δ k , where k ∈{1,… , n}
and E − Path(Δ i ) ≺ E − Path(Δ k ) ∀i ∈{1,… , n} \ {k} .
Let us explain the meaning of LO (Δ) and RI (Δ ) . Looking at the type
inference rules, we can say that a new singular constraint is added to the constraint
part of skeleton only after applying rule [APP]. Hence each singular constraint
corresponds to one subterm of the form ( M 1M 2 ) , where M 1 , M 2 ∈ Term . Let us
mention without proving that LO (Δ ) corresponds to the leftmost, outermost
subterm of the form ( M 1M 2 ) and RI (Δ ) corresponds to the rightmost, innermost
subterm of the form ( M 1M 2 ) .
Proc. of the Yerevan State Univ. Phys. and Mathem. Sci., 2009, № 3, p. 42–51.
50
Definition 2.26. Let Δ=Δ1 ∩ … ∩ Δ n ∈ Constraint , n ≥ 1 , where Δ1 ,… , Δ n
are singular constraints, I={i|, 1 ≤ i ≤ n , and rule unifyβ is applicable to Δ i } . Then
filterβ (Δ )=∩ i∈I Δ i (we suppose that filterβ (Δ ) = ω for I=∅ ).
Algorithm of unification(Unify).
Input: constraint Δ such that solved (Δ ) = ω .
Output: either returns the substitution that solves constraint or fails, or never
returns.
1. If Δ=ω , then return ε .
2.
If
filterβ (Δ ) ≠ ω ,
then
unify
β
LO ( filterβ ( Δ)) ⎯⎯⎯
→σ
and
returns
[Unify (unsolved ([σ ]Δ ))]σ .
unify
β
→σ and
3. If the rule unify x is applicable to RI (Δ ) , then RI (Δ) ⎯⎯⎯
returns [Unify (unsolved ([σ ]Δ ))]σ .
4. If the rule unifyc is applicable to RI (Δ ) and this application doesn’t fail,
unify
β
→σ and returns [Unify (unsolved ([σ ]Δ ))]σ , else fail.
then RI (Δ ) ⎯⎯⎯
Lemma 2.9 (correctness of the unification algorithm). Let M ∈ Term and
Δ=constraint (initial ( M )) . Then if Unify (Δ ) = σ , [σ ]Δ is solved.
It is easy to see that the unification algorithm first tries to solve singular
constraints, to which the rule unifyβ is applicable. It means that during his work
the unification algorithm does implicit β -reductions in initial term until reducing
the initial term to β -normal form, which happens when the unification algorithm
first time arrives in point 3 or ends his work at point 1.
Lemma 2.10. Let M ∈ Term , P = initial ( M ) , M \β M ′ ∈ β − NF and
Δ=constraint ( P ) . Then for input Δ the unification algorithm does a finite number
of recursive calles from point 2 and tries to return the following:
[Unify (Δ′)][σ m ]…[σ 2 ]σ 1 , where σ 1 ,… ,σ m are created by the rule unifyβ during
the work of the algorithm and Δ′ = constraint (initial ( M ′)) .
Remark 2.2. It is very important that in point 2 the unification algorithm
applies the rule unifyβ to LO ( filterβ (Δ )) . This choice ensures that in each step of
the implicit β -reduction the unification algorithm will treat the leftmost, outermost
β -redex. It is known that in this case β -normal form is reachable, if it exists.
Lemma 2.11. Let M ∈ Term , P=initial ( M ) , Δ=constraint ( P ) and M
hasn't a β -normal form. Then for input Δ the unification algorithm will infinitely
call himself recursively in point 2 and will never return.
Now let us consider the situation, when the term has a β -normal form. In
this case we have no singular constraint, to which the rule unifyβ is applicable, and
the unification algorithm tries to solve singular constraints, to which the rule unify x
or rule unifyc is applicable. It is important that in each step of work the unification
algorithm tries to solve the singular constraint RI ( Δ) . This choice ensures that
Proc. of the Yerevan State Univ. Phys. and Mathem. Sci., 2009, № 3, p. 42–51.
51
during the next recursive calls of unification algorithm the rule unify x or the rule
unifyc will be applicable to each singular constraint.
Lemma 2.12. Let M ∈ Term , P=initial ( M ) , Δ=constraint ( P ) and M ∈ β –
– NF . Then for input Δ the unification algorithm does a finite number of recursive
calls from point 3 or 4, and succeeds in point 1 or failed in point 4, because the
application of the rule unifyc was failed.
2.6. Type inference algorithm. Now let us present the type inference
algorithm.
Type inference algorithm(Typify).
Input: term M .
Output: either returns the typing of M or fails, or never returns.
1. P=initial(M) . 2. σ=Unify(constraint(P)) . 3. Return ([σ ]env( P) A [σ
]type( P)) .
T h e o r e m 2 . 1 (correctness of the Typify algorithm).
Let M ∈ Term . Then if Typify ( M ) = ( A A τ ) , then ( A A τ ) is the typing of
term M .
Received 06.04.2009
REFERENCES
1. Carlier S., Wells J.B. Type Inference with Expansion Variables and Intersection Types in System
E and an Exact Correspondence with β-eduction. In Proc. 6th Int'l Conf. Principles & Practice
Declarative Programming, 2004.
2. Milner R. Journal of Computer and System Sciences, 1978, № 17, p. 348–375.
3. Wells J.B. The Essence of Principal Typings. In Proc. 29th Int'l Coll. Automata, Languages and
Programming. Springer-Verlag, 2002, v. 2380 of LNCS.
4. Jim T. What are Principal Typings and What are They Good for? In Conf. Rec. POPL '96: 23rd
ACM Symp. Princ. of Prog. Langs., 1996.
5. Carlier S., Polakow J., Wells J.B., Kfoury A.J. System E: Expansion Variables for Flexible
Typing with Linear and Non-linear Types and Intersection Types. In Programming Languages &
Systems, 13th European Symp. Programming. Springer-Verlag, 2004, v. 2986 of LNCS.
6. Barendregt H.P. The Lambda Calculus: Its Syntax and Semantics. Amsterdam, North Holand,
1981.
52
Proc. of the Yerevan State Univ. Phys. and Mathem. Sci., 2009, № 3, p. 42–51.
λ -թերմերի տիպային կոռեկտության մասին: 1
Աշխատանքում դիտարկվում են պոլիմորֆ λ -թերմերը, որոնցում չկա
ինֆորմացիա փոփոխականների տիպերի մասին: Աշխատանքի նպատակն է
ընդլայնել այդպիսի թերմերի տիպայնացման ալգորիթմը [1] տիպերի
հաստատունների և թերմերի հաստատունների գաղափարների ներմուծմամբ:
О типовой корректности полиморфных λ -термов. 1
В работе рассматриваются полиморфные λ -термы, в которых отсутствует информация о типах переменных. Цель даной работы – расширить
алгоритм типизации таких термов [1] введением понятий констант типов и
констант термов.
Download