Environment Snapshot 3:
3.4 Completeness of Algorithm W 1
3.4.2 Completeness of Type Computation
The above pair of lemmas embody a simple pattern that is at the heart of the incremental property computation. Starting with no information, we can rene our knowledge about inter-esting global properties as we are given more details about the program. Now, we apply the same principle to the type property.
Lemma 3.7
Starting from TE0 (equation 3.6), if TEi and TEj (j > i
) are the type environ-ments obtained after thei
-th and thej
-th call toW
1 then TEiTEj.Proof:
We prove this by induction oni
.Base Step:
It is clear from equation 3.6 that TE0TEj for everyj >
0 since8:
for any.Inductive Step:
Assume that TEi,1 TEj for everyj >
(i
,1). Letx
be the identier being typed at thei
-th call.Looking at algorithm
W
1 (gure 3.5), we see that TEi,1 is the same as TEi ex-cept for the identiersz
k 2SCC
ix, where it has been updated to the new type schemes izk = close(U
iizk). So the inductive assumption carries over to TEi for all other iden-tiers except these. For any identierz
k 2SCC
ix, let TEj (j > i
) be some later typeenvironment in which the type-scheme for
z
k is updated again to jzk = close(U
jjzk)20. We need to show that izk jzk for eachz
k 2SCC
ix, so that we can combine this in-formation with the inductive assumption and arrive at TEiTEj for every suchj > i
. For convenience, we denote the number of elements in setsSCC
ix andSCC
jx byn
andm
respectively.In order to prove that
izk jzk for eachz
k 2SCC
ix, we need to construct an alternate typing for eachz
k in terms of the types obtained at thej
-th call, and then use the completeness theorems 2.6 and 2.8 over the types computed at thei
-th call to give the above instantiation relationship.The following typings are obtained through the internal calls to
W
0 and theU
junication at the
j
-th call toW
1. For everyz
k 2SCC
jxwe have,New-TEjk = TEj,1nSCCjx +f
z
1 7!k1;:::;z
m 7!kmgW
0(New-TEjk;e
k) = (S
kj;
jzk;A
jk)=)
S
kj(New-TEjk) `e
k :jzk Using theorem 2.7.=)
U
jS
kj(New-TEjk) `e
k :U
jjzk Using lemma 2.3.which when expanded yields the following typing for each RHS expression
e
k corre-sponding to each top-level identierz
k 2SCC
jx.TEj,1nSCCjx +f
z
1 7!U
jjz1;:::;z
m7!U
jjzmg`e
k :U
jjzk (3:
20) Note that by construction, there are no free type variables in the environment TEj,1, and theU
j substitution ultimately reduces each termS
kj(k1);:::;S
kj(km) for each 1k
m
to the same termU
jjzk by unication.We compare the type environment in the typing 3.20 above with the environments New-TEik setup during the
i
-th call for eachk
(1k
n
).New-TEik = TEi,1nSCCix +f
z
17!k1;:::;z
n7!kngLet
S
kj,i = fk1 7!U
jjz1;:::;
kn 7!U
jjzng=)
S
kj,i(New-TEik) = TEi,1nSCCix +fz
17!U
jjz1;:::;z
n7!U
jjzng=)
S
kj,i(New-TEik) `e
k :U
jjzk (3.21)The last typing 3.21 is obtained by comparing its environment with that in typ-ing 3.20. We use the inductive assumption to get TEi TEj and lemma 3.5
to ensure that
SCC
ixSCC
jx so that lemma 2.4 can be used for the identiers inSCC
jx,SCC
ix. This equation species an alternate typing for eachz
k 2SCC
ix which we now show to be an instance of the type-scheme computed at thei
-th call.For each of the internal calls to
W
0 during thei
-th call toW
1, we can use the com-pleteness theorem 2.8 over its corresponding alternate typing 3.21 to give the following,9
R
k:
(R
kS
ik)(New-TEik) =S
kj,i(New-TEik) (1k
n
):
(3.22)R
kclose(S
ik(New-TEik);
izk)U
jjzk 8z
k 2SCC
ix:
(3.23) In equation 3.22 above, the domains of each of theR
k substitutions (1k
n
) are completely disjoint because none of the internal calls toW
0 share any non-generic type variables. Furthermore for eachk
, Dom(R
k) tyvars(S
ik(New-TEik)), so that it involves only the free type variables of izk. Therefore, for each type izk (z
k 2SCC
ix), the instance relationship of 3.23 can be explicitly written as a substitution equation (R
k jI
k)izk =U
jjzk whereI
k is the instantiation substitution operating only on the generic type variables ofizk.Finally, we combine all these disjoint substitutions into one giant substitution
U
j,ithat unies the column terms of the unication matrix in the second phase of the
i
-th call into the types obtained in thej
-th call.U
j,i =R
1 jI
1 jjR
njI
n=)
U
j,iizk =U
jjzk 8z
k 2SCC
ix:
Now by the completeness theorem of unication (theorem 2.6)
U
i is the most general unier for its nodes21, and all other uniers are substitution instances ofU
i. I.e.,9
R: R
U
i =U
j,i=) (
R
U
i)izk =U
j,iizk 8z
k 2SCC
ix:
=) (
R
U
i)izk =U
jjzk 8z
k 2SCC
ix:
=) close(
U
iizk) = izk close(U
jjzk) = jzk 8z
k 2SCC
ix:
The last equation is what we wanted to prove.2
21Theorem 2.6 deals with the unication of a pair of type terms whereas here we have several groups of terms to be unied under the same unifying substitution. But it is easy to construct a pair of giant tuple-type terms from the given set of terms whose unication yields the same substitutionUi. So the completeness result is valid for this case also.
Lemma 3.8
If TEt is the nal type environment in the complete program case with the type-scheme obtained for the nal program query being assigned to the special variable \it", then for everyi
0, TEiTEt.Proof:
The basic strategy of the proof is same as in proving the last lemma. We construct an alternate typing for eachz
k 2SCC
ix at thei
-th call in terms of the types obtained in the complete program system and then use the completeness theorems to show that izk tzk for eachz
k 2SCC
ix. Since only the identiers fromSCC
ix get updated at thei
-th call, we combine this with the inductive assumption to obtain the desired result just like in the previous lemma.Base Step:
From equation 3.6, TE0TEt since 8:
for any .Inductive Step:
Assume that TEi,1TEt. As before, letx
be the identier being typed at thei
-th call. Also, let the number of elements in setsSCC
ix andSCC
tx be denoted byn
andm
respectively, whereSCC
tx stands for the strongly connected component ofx
in the complete call graph.In the complete program system, the algorithm
W
is called over the complete program expression as described in section 2.4.2 with the initial library environment TElib. The type-schemetx forx
appearing in the nal type environment TEtis actually computed during an internal recursive call toW
when its correspondinglet-binding is being typed.In the general case,
x
appears together with the identiers in its strongly connected componentSCC
txin alet-binding,let
z
1;:::;x;:::;z
m = xz
1x
z
m:
(e
z1;:::;e
x;:::;e
zm) ine
(3:
24) We traverse the derivation tree generated by the overall program typing down to the branch where the inner tuple of the RHS expressions (e
z1;:::;e
x;:::;e
zm) is being typed to obtain the following typing for each RHS expressione
k corresponding to each top-level identierz
k 2SCC
tx.TEprev+f
z
17!tz1;:::;z
m 7!tzmg`e
k :tzk (3:
25) The type environment TEprev comes from previously typed let-bindings in addition to the initial library environment TElib. The nal type-schemestzkfor eachz
kSCC
txareobtained by completely closing their corresponding types obtained in typing 3.25 above.
TEt(
z
k) = tzk = close(tzk) 8z
k 2SCC
tx:
Typing 3.25 is similar to typing 3.20 of the last lemma. As before, we can setup conversion substitutionsS
kt,i for the environments New-TEik created during thei
-th call that produce an alternate typing for eachz
k 2SCC
ix in the same spirit as typing 3.21.We only note that TEprev is a subset map of TEt so the inductive assumption on TEi,1
yields TEi,1 TEprev22 and lemma 3.6 ensures that
SCC
ixSCC
tx as required in the proof. We omit the details.So just as in the previous lemma, we obtain,
close(
U
iizk) = izk close(tzk) = tzk 8z
k 2SCC
ix:
2
The above two lemmas together show that starting with the most general type-scheme8