principal typing-schemes in a polyadic -calculus department of
TRANSCRIPT
92-004
Principal Typing-Schemes
in a Polyadic �-Calculus
Vasco Thudichum Vasconcelos
Kohei Honda
November 1992
Department of Computer Science
Graduate School of Science and Technology
Keio University
3{14{1 Hiyoshi, Kohoku-ku, Yokohama 223 Japan
For information on this technical report series,
send e-mail to \[email protected]".
Abstract
The present report introduces a typing system for a version of Milner's polyadic
�-calculus, and a typing inference algorithm. The central concept underlying the
typing system is the notion of type assignment, where each free name in a term is as-
signed a type, the term itself being given multiple name-type pairs. This observation
leads to a clean typing system for Milner's sorting, and induces an e�cient algorithm
to infer the typing of a term. The typing system enjoys a subject-reduction property
and possesses a notion of principal typing-scheme. Furthermore, the algorithm to
reconstruct the principal typing-scheme of a process, or to detect its inexistence, is
proved correct with respect to the typing system.
1.
Introduction
Type discipline helps programmers not only in writing type-correct programs but
especially in writing them in a principled and clear way. Concurrent programming,
however, has long lacked such discipline, in contrast to functional and algebraic
frameworks in sequential programs.
The present report proposes a system of implicit typing for untyped polyadic
�-terms along the lines of type systems for untyped �-terms [7, 17, 3]. In particular
we concentrate on a process calculus equipped with the capability to pass arbitrary
sequences of names over channels, namely a straightforward generalization of the
monadic �-calculus as presented in [13], where ordered tuples of names replace
single names as the object of communications.
The typing system here presented was developed based on that of N
!
[8]. The
essential idea underlying N
!
(and that of the system here presented) is the notion
of attributing types to names, not to terms, resulting in a scenario where a term
is assigned multiple name-type scheme pairs. We leave to [8] discussions on more
general type structures for dyadic interactions, among which Milner's sorting [14, 15]
can be regarded as a particular instance.
The only primitive operation for forming types, builds from types �
1
; . . .�
n
the
type (�
1
� � ��
n
)
n
, n � 0, representing a property of a name carrying a sequence
of names of types �
1
; . . .�
n
. In this setting, a term is well-typed, if whenever one
of its names y carries a sequence of names x
1
; . . .x
n
belonging to types �
1
; . . .�
n
respectively, then y belongs to type (�
1
� � ��
n
)
n
. A well-typing for a term is a set
of assignments of types to names that makes the term well-typed. An algorithm is
presented, which veri�es if a term is well-typed, and in this case infers the principal
typing of the term (cf. [5]), from which all typings that make the term well-typed
can be extracted. A version of the algorithm is implemented in Standard ML and
has been in use for almost a year.
The outline of the report is as follows. The next section introduces our version
of the polyadic �-calculus. Section 3 presents types, a basic typing system and
its extension with recursive types. Section 4 presents the algorithm to infer the
principal typing scheme of a term (cf. [6, 5]) along with a proof of its correctness.
Section 5 compares our typing system to Milner's sorting program.
1
2.
A polyadic �-calculus
This section introduces a polyadic �-calculus to the extent needed for typing con-
siderations. The particular calculus proposed is a straightforward generalization of
the monadic �-calculus, as presented in [13]. Structural congruence on terms caters
for equivalence of terms over concrete syntax and, together with normal forms of
terms introduced in [9], make the formulation of the transition relation quite con-
cise. Summation is not considered; though its incorporation in the present system
is straightforward.
The syntax of polyadic �-processes is obtained from the monadic �-calculus [13],
by simply replacing scalar communications by vector communications. Parenthesis
around input names were omitted.
De�nition 2.0.1 (Syntax) Let N be an in�nite set of names and N
�
the set of
(�nite) sequences overN. The set of polyadic �-processesC is given by the following
grammar.
P ::= 0 j x~y:P j x~y:P j P j Q j �xP j !P
where a; b; . . . and v; x; y; . . . range over N; ~v; ~x; ~y; . . . range over N
�
and P;Q; . . .
range over C.
We will call receptors to process of the form x~y:P , and emitters to processes of
the form x~y:P . Emitters of the form a~v:0 will be simply written a~v. Multiple name
restrictions on a process �x
1
� � � �x
n
P will be written �~xP . len(~x) denotes the length
of the sequence of names ~x, and if len(~x) = n we call ~x a n-ary communication. f~xg
denotes the set of names occurring in ~x. There are two kinds of bindings in the
calculus.
i. In a~x:P , names in ~x occurring bound themselves, bind free occurrences of the
same names in P .
ii. In �xP , x occurring bound itself, binds free occurrences of the same name in
P .
De�nition 2.0.2 (Free names) The set of free names in P , denoted by FN (P ),
is inductively de�ned by: FN (0) = ;, FN (a~v:P ) = fag[f~vg[FN (P ), FN (a~x:P ) =
fag [ (FN (P ) n f~xg), FN (P j Q) = FN (P ) [ FN (Q), FN (�xP ) = FN (P ) n fxg,
FN (!P ) = FN (P ).
2
A notion of substitution of free occurrences of x by y in P , denoted P [x=y], is
de�ned in the usual way, and so is �-conversion. Also, whenever len(~y) = len(~x)
and the names in ~x are all distinct, Pf~y=~xg denotes the result of the simultaneous
replacement of free occurrences of ~x by ~y in P (with change of bound names where
necessary, as usual.)
De�nition 2.0.3 (Structural congruence) � is the smallest congruence rela-
tion over processes de�ned by the following rules.
i. P � Q whenever P �-convertible to Q
ii. P j Q � Q j P
iii. (P j Q) j R � P j (Q j R)
iv. �xP j Q � �x(P j Q) whenever x 62 FN (Q)
v. !P � P j !P
vi. If P � Q, then P j R � Q j R and �xP � �xQ
There is a certain normal form into which all terms can be transformed. Let us
call base terms to processes of the form a~v:P , a~x:P , !P and 0. @; @
0
. . . will denote
concurrent composition of base terms.
Proposition 2.0.4 (Normal form [9]) For any process P in C there is a process
P
0
in C, P � P
0
, such that, for some m � 0,
P
0
def
= �~u(P
1
j � � � j P
m
)
where P
1
; . . .P
m
denote base terms. P
0
(usually not unique) is called a normal form
of P .
Reduction models the computing mechanism of the calculus. By using structural
congruence and normal forms, it can be concisely de�ned.
De�nition 2.0.5 (Reduction)
i. One-step reduction, denoted by !, is the smallest relation generated by the
following rules.
COM �~u(@ j a~vP j a~x:Q j @
0
)! �~u(@ j P j Qf~v=~xg j @
0
) (len(~v) = len(~x))
STRUCT
P
0
� P; P ! Q; Q � Q
0
P
0
! Q
0
ii. The reduction relation !! is the re exive and transitive closure of one-step
reduction.
3
3.
Types and typing assignment
This section introduces a basic typing system to assign types to free names in pro-
cesses, and a notion of principal typing schemes. The system is then generalized in
order to accommodate recursive types.
3.1 The system TA
()
De�nition 3.1.1 (Type schemes) Let K be a set of type-constants and V an
in�nite set of type-variables. The set of type schemes T
()
is de�ned by the following
grammar.
� ::= k j t j (�
1
� � ��
n
)
n
where k; k
0
; . . . range over K; t; t
0
; . . . range over V ; �; �; ; . . . range over T
()
and
n � 0. A type is a type scheme containing no variables.
Informally, an expression of the form (�
1
� � ��
n
)
n
is intended to denote some
collection of names carrying a n-ary communication whose elements belong to types
�
1
; . . .�
n
. We will often omit the subscript in composed types, and write (�
1
� � ��
n
)
for (�
1
� � ��
n
)
n
.
Type assignment formulas are expressions x :�, for x a name in N and � a type
scheme in T
()
. x is called the formula's subject and � its predicate. Typings are sets
of formulas of the form fx
1
:�
1
; . . . x
n
: �
n
g, where no two formulas have the same
name as subject. �;�;�; . . . will denote typings.
Let ~x = x
1
� � �x
n
be a sequence of names, ~� = �
1
� � ��
n
a sequence of type
schemes and � a typing. Then, f~x : ~�g denotes the typing fx
1
: �
1
; . . . x
n
: �
n
g;
� � ~x : ~� denotes typing � [ f~x : ~�g, provided names in ~x do not occur in �; �=~x
denotes the typing � with formulas with subjects in ~x removed; N (�) denotes the
set of names occurring in �.
We say typings � and � are compatible in T
()
, and denote by � � �, if
x :� 2 � and x :� 2 � implies � = �:
Proposition 3.1.2 If � � � and � � �, then � � �.
4
Typing assignment statements are expressions P � � for all processes P and
typings �. We will write `
()
P � � if P � � is provable using the axioms and
the rules of TA
()
below. Whenever `
()
P � � we say P is well-typed, and call � a
well-typing for P .
De�nition 3.1.3 (Typing scheme assignment system TA
()
) TA
()
is de�ned
by the following rules.
Nil ` � � ; Rcpt
` P � � � ~x: ~�
` a~x:P � � [ fa : (~�)g
(� � fa : (~�)g)
Emt
` P � �
` a~v:P � � [ fa : (~�)g
(f~v : ~�g � � and � � fa : (~�)g)
Scop
` P � �
` �xP � �=x
Comp
` P � � ` Q � �
` P j Q � � [�
(� � �)
Repl
` P � �
` !P � �
Weak
` P � �
` P � � [ fx :�g
(x 62 N (�))
Whenever a process P is well-typed, there exists a TA
()
derivation which produces
a typing containing only assignments on the free names of P . If � is a typing, let
�j�P be the restriction of � to the free names in P . We shall call typings of this form
P -typings.
Theorem 3.1.4 If `
()
P � �, then FN (P ) � N (�) and `
()
P � �j�P .
The following lemma ensures that structural congruent processes have the same
typing.
Lemma 3.1.5 If `
()
P � � and P � Q, then `
()
Q � �.
The following fundamental property of the type assignment system TA
()
ensures
that the typing of a process does not change as it is reduced and is closely related
with the lack of runtime errors.
Theorem 3.1.6 (Subject Reduction) If `
()
P � � and P !! Q, then `
()
Q �
�.
Notice that the converse of subject-reduction does not hold, since non well-typed
terms can be reduced to well-typed ones (e.g. aa j ax!! 0), and also because free-
names may be lost in the course of reduction (e.g. `
()
0 � ; and a j a !! 0 but
6 `
()
a j a � ;). Also due to the loss of free names during reduction, if `
()
P � �,
P !! Q, and `
()
Q � �, then �j�P � �j�Q.
A consequence of the subject-reduction property is that a well-typed programs
will not run into type errors during execution. We say P contains a possible runtime
error, and write P 2 Err, if there exists a term Q such that P !! Q � �~u(@ j
a~v:Q
1
j a~x:Q
2
j @
0
) and len(~v) 6= len(~x).
Corollary 3.1.7 If P is well-typed, then P 62 Err.
5
3.2 Principal typing schemes
The main interest in the introduction of type-variables is that we can see a typing
for a given process as a scheme that can generate new typings for the process, by
instantiating variables in the typing with types. More precisely, a substitution on
types is a mapping from type-variables to type schemes, s : V ! T
()
. If � is a
type scheme and s a substitution, then s� is the type scheme obtained by replacing
each variable t in � with st. A substitution s applied to a typing � is the typing s�
de�ned by
s� = fx :s� j x :� 2 �g
Compatibility of typings is preserved by substitution.
Proposition 3.2.1 If � � �, then s� � s�, for any substitution s.
A typing � is an instance of � if there is a substitution s such that s� = �.
Every instance of a well-typing is also a well-typing.
Lemma 3.2.2 Let � be an instance of �. If `
()
P � �, then `
()
P � �.
All possible typings for a given process are instances of its principal typing scheme.
De�nition 3.2.3 (Principal typing scheme) A P-typing �
p
is principal for a
process P if
i. `
()
P � �
p
and
ii. if `
()
P � �, � a P -typing, then � is an instance of �
p
.
It is easy to see that the principal typing scheme, when it exists, is unique up to
renaming of type-variables.
Theorem 3.2.4 (Principal typing scheme property for TA
()
) If P is well-
typed, then there exists a principal P-typing �
p
. Moreover, �
p
can be found in an
e�ective way.
The principal typing scheme can be derived in a syntax-direct way by observing
that any deduction of a typing � for a process P parallels the construction tree
of the process. The general idea of the algorithm follows the one used to extract
the principal type scheme of a type-free �-term [12, 5], using Robinson's uni�cation
algorithm. An algorithm to extract the typing of a process in a more general setting
is described in section 4.
6
( )
k
( )
( )
.
.
.
( )
k
( )
( )
Figure 3.1 The in�nite tree associated with equation � = ((k�)())
3.3 The system TA
()�
We would like to have some means for de�ning circular type expressions. For this
purpose we can use equations between type expressions whose solutions will be
in�nite types, i.e. type expressions whose construction trees are in�nite labelled
trees. So, for example to a type expression � de�ned by the equation � = ((k�)())
where k is some type-constant, we associate the in�nite tree depicted in �g. 3.1.
Instead of using an equation for recursively specifying a type, we can introduce a
special notation for denoting its solution: if t is a type variable and � is an arbitrary
type, then �t:� will stand for the solution of the type equation t = �.
De�nition 3.3.1 (Recursive type schemes) The set of recursive type schemes
T
()�
is de�ned by adding to the grammar of De�nition 3.1.1, the production
� ::= �t:�
We can identify a (possibly in�nite) labelled tree � with a partial function from
the set of �nite strings over the alphabet of positive integers (describing paths in the
tree), to a ranked alphabet L = V [K [ f()
i
j i � 0g, where the rank of v 2 V [K
is 0 and the rank of ()
i
is i. Let T
1
be the set of in�nite trees over L, and let �; �; . . .
range over T
1
. Introducing a new label ?, we can de�ne a partial ordering on the
set of in�nite trees satisfying
i. ?v �, for all in�nite trees �;
ii. if �
1
v �
0
1
; . . . �
n
v �
0
n
, then (�
1
� � � �
n
)
n
v (�
0
1
� � � �
0
n
)
n
, for all n � 0.
With respect to this ordering, the set T
1
is a c.p.o. and any in�nite tree can be seen
as the least upper bound of a denumerable sequence of in�nite approximations.
Substitution on in�nite trees is a mapping s : V ! T
1
. Given a substitution s
and an in�nite tree �, we obtain the in�nite tree s� as the result of the simultaneous
7
substitution of the tree st for each occurrence of variable t in �. We shall write
�[t := �] for the tree s�, when substitution s assigns � to the variable t and leaves
unchanged all other variables. Any such substitution s is a continuous function from
T
1
to T
1
. A translation ( )
�
from recursive types to in�nite trees may be de�ned
as follows:
k
�
= k for k a type-constant
t
�
= t for t a type-variable
(�
1
� � ��
n
)
�
n
= (�
�
1
; . . .�
�
n
)
n
for n � 0
�t:�
�
= �x(��:�
�
[t := �]) for � an in�nite tree
Trees associated with recursive types are regular, i.e. they have �nitely many
subtrees. An interpretation of recursive types as in�nite trees naturally induces an
equivalence relation � on T
()�
by putting
� � � i� �
�
= �
�
Using the equivalence relation � on recursive types, we now say that two typings
� and � are compatible in T
()�
, and (still) write � � �, if
x :� 2 � and x :� 2 � implies � � �
De�nition 3.3.2 (Recursive typing scheme assignment system TA
()�
) TA
()�
is de�ned by the rules in De�nition 3.1.3 with the addition of the rule
(�)
` P � � � x :�
` P � � � x :�
(� � �)
Example 3.3.3 A list may be de�ned with two constructors, Cons and Nil.
Cons(lfr)
def
= lcn:cfr
Nil(l)
def
= lcn:n
In both constructors, link l carries a pair of names: c intended to reply fr (the �rst
and the rest of the list) in case the list is a Cons cell, and n intended to signal back
the empty list, in case the list is a Nil cell. In order to deduce the sorting of a list, we
must handle both constructors simultaneously, and since the list and its rest must
be of the same sort, we shall deduce the sorting of the process
Cons(lvl) j Nil(l)
A deduction is depicted below, where t is a sort-variable and � denotes the sort
�u:((tu)()). Notice that � and ((�t)()) represent the same in�nite tree, i.e. � �
8
((�t)()).
`
()�
0 � ;
(Weak)
3
`
()�
0 � v : t � l :� � n : ()
(Emt)
`
()�
cvl � v : t � l :� � c : (t�) � n : ()
(�)
`
()�
cvl � v : t � l : ((t�)()) � c : (t�) � n : ()
(Rcpt)
`
()�
lcn:cvl � v : t � l : ((t�)())
`
()�
0 � ;
(Emt)
`
()�
n � n : ()
(Weak)
`
()�
n � n : () � c(�t)
(Rcpt)
`
()�
lcn:n � l : ((�t)())
(Comp)
`
()�
lcn:cvl j lcn:n � v : t � l : ((t�)())
The tree associated with ((t�)()) � �u:((tu)()) is the one depicted in �g. 3.1, with
k replaced by t.
Many syntactical properties of the system TA
()
continue to hold when formulated
in the extended system TA
()�
, notably the subject-reduction theorem 3.1.6, which
implies that processes whose names can be assigned in�nite typings do not encounter
runtime errors. Also, well-typed processes have a principal typing in TA
()�
, and this
can be found in an e�ective way.
Theorem 3.3.4 (Principal typing scheme property for TA
()�
) If P is a well-
typed process, then there exists a principal P-typing �
p
. Moreover, there is an algo-
rithm to infer �
p
.
An algorithm to extract the principal recursive typing scheme of a process P , or
to detect its inexistence, is presented and validated in the next section.
9
4.
Typing inference in TA
()�
This section presents an algorithm to extract the principal recursive typing scheme of
a process. We rely on the existence of a procedure to unify labelled (possibly regular
in�nite) trees. According to Robinson [19] such an algorithm was hit independently
by Huet [11], Baxter [2] and Robinson [18]. See [19] for an illustrated description of
the algorithm.
4.1 The TA algorithm
Given a process P , the algorithm TA either succeeds producing a typing � such
that `
()�
P � � or, fails. When TA(P ) succeeds, it produces a principal typing for
P , and if TA(P ) fails, there is no well-typing for P .
We assume a uni�cation algorithm U accepts a set of pairs of trees to be uni�ed
and returns a substitution that makes all pairs uni�able. Uni�cation of typings is
de�ned as follows.
Unify (�;�) = Uf(�; �) j x :� 2 � and x :� 2 �g
Given a sequence of names x
1
� � �x
n
and a typing �, the Comm function builds
a sequence of trees �
1
� � � �
n
such that, for all x
i
2 f~xg, �
i
is � if x
i
: � 2 �, or a
fresh variable t
i
otherwise. The TA function (�g. 4.1) constructs a typing on the
free-names of a process by recursively descending the structure of the process. TA
closely follows the rules of the typing assignment system, with the di�erence that
1) whenever, in rules Emt and Rcpt, a predicate on a name is needed but not yet
available, a fresh variable is created to the e�ect by function Comm, and 2) side
conditions on compatibility of typings are ensured by uni�cation.
As it happens in type inference algorithms for �-calculus, substitutions do not
need to be explicitly computed, and so a more e�cient algorithm can be imple-
mented.
4.2 Correctness of TA
Since the use of in�nite trees to deal with recursive types somewhat simpli�es the
treatment of syntactical properties of the typing inference system, results on this
subsection are all related to in�nite trees.
10
TA(0) = ;
TA(a~v:P ) = let � = TA(P )
~� = Comm(~v;�)
s = Unify (� [ f~v : ~�g;fa : (~�)g)
in s(� [ f~v : ~�g) [ sfa : (~�)g
TA(a~x:P ) = let � = TA(P )
~� = Comm(~x;�)
s = Unify (�=~x;fa : (~�)g)
in s(�=~x) [ sfa : (~�)g
TA(P j Q)= let � = TA(P )
� = TA(Q)
s = Unify (�;�)
in s� [ s�
TA(�xP ) = TA(P )=x
TA(!P ) = TA(P )
Figure 4.1 An algorithm to extract the principal typing of a process.
A uni�er is a substitution which makes two in�nite trees syntactically equal.
More precisely, a uni�er of � and � is a substitution s : V ! T
1
such that s� = s�.
A substitution s is regular if st is a regular in�nite tree for all variables t in V . As
usual, we say a uni�er s of � and � is more general then a uni�er rs and say s is a
most general uni�er of � and � if, for each uni�er r of � and �, there is a substitution
u such that r = su. The following theorem, due to Huet [11], extends Robinson's
result on uni�cation of �nite trees to in�nite trees.
Theorem 4.2.1 (Uni�cation of in�nite trees [4]) Let � and � be two trees in
T
1
.
i. If � and � are uni�able, they have a most general uni�er s : V ! T
1
. It
involves variables only in � and in � and is unique up to variable renaming.
ii. If � and � are regular and uni�able then their most general uni�er is regular.
It can be e�ectively computed.
Let � and � be two typings with subjects in N and predicates in T
1
. A uni�er
of � and � is a substitution s : V ! T
1
applied to a typing, such that s� � s�.
When such a substitution s exists, we say that s uni�es � and �, and that � and
� are uni�able. The most general uni�er of two types is de�ned similarly to trees.
It is easy to prove that if � and � are uni�able, then Unify (�;�) succeeds, and
if Unify (�;�) = s, then s is the most general uni�er of � and �. The following
theorem says that whenever TA(P ) succeeds for some process P , then it produces
a well-typing for P .
11
Theorem 4.2.2 (Soundness) If TA(P ) succeeds, then `
()�
P � TA(P ).
It follows by lemma 3.2.2 that every instance of TA(P ) is a well-typing. Con-
versely, we can prove that every well-typing for the free-names of P is an instance
of TA(P ).
Theorem 4.2.3 (Completeness) Suppose `
()�
P � �, for some typing �. Then,
i. TA(P ) succeeds;
ii. there is a substitution s such that sTA(P ) = �j�P .
4.3 Complexity of TA
We now analyze the time complexity of the typing assignment algorithm.
The sequential version of the uni�cation algorithm is known to be linear on the
number of subtrees in the trees to be uni�ed. (In fact, Unify executes n union and
�nd instructions in at most cnAck
�1
(n) time, where c is a constant and Ack
�1
is
the inverse of the Ackerman function [1]. Ack
�1
(n) � 5 for all \practical" values of
n, i.e., for all n � 2
65536
.) Since each receptor and emitter introduces a subtree in
a type, it is easy to prove that the number of subtrees in a type is bound by the
number of receptors and emitters in a process.
Also, it is easy to see that there will be a call to Unify for each pair of occurrences
of the same name in a process. Finally, since both the number of occurrences of any
name and the number of receptors and emitters in a process are bound by p, the
textual representation of the process, we have that O(p
2
) is an upper bound on the
time taken to calculate the most general typing of a process.
12
5.
Comparison to Milner's sorting program
Robin Milner ([15]) introduced a di�erent version of polyadic �-calculus along with
the notion of sorts. In this section we prove that sorts are in a one-to-one corre-
spondence with in�nite regular trees; and that well-sorted (normal-form, located)
agents in the Milner's polyadic �-calculus are in a one-to-one correspondence with
well-typed processes in C.
5.1 Sorts and regular in�nite trees
As it is current practice in typed functional programming languages (e.g. SML [16]
and Haskell [10]), Milner's sorting program tacitly avoids explicit in�nite sorts.
Instead, it assumes a collection S of basic sorts, and de�nes a sorting over S as a
partial function
ob : S ! S
�
Sorts are assigned to names, which in turn are equated via ob to labelled regular
in�nite trees. We assume ob is de�ned only for a �nite subset of S. Similarly to
types, we write a :s to mean that sort s is assigned to name a and, if ~x = x
1
� � �x
n
,
~s = s
1
� � � s
n
, then we write ~x: ~s to mean x
1
:s
1
; . . . x
n
:s
n
. In order to prove our �rst
result, we make use of the theory of in�nite trees [4].
De�nition 5.1.1 (System of regular equations) A system of regular equations
over a ranked alphabet L is a �nite system of the form
U = hx
1
= u
1
; . . .x
n
= u
n
i
where x
1
; . . . x
n
are the unknowns and u
1
; . . .u
n
are of the form l for l in L
0
or
l(x
i
1
; . . .x
i
k
) for l in L
k
, k � 1 and 1 � i
1
; . . . i
k
� n. Let T
1
be the set of in�nite
trees over L. A solution of U is a n-tuple of trees (�
1
; . . . �
n
) in T
n
1
satisfying the
equations.
The second part of the following theorem is a slight generalization of a theorem
in [4].
Theorem 5.1.2 (Regular systems)
13
i. A system of regular equations has a unique solution in T
1
; all components of
this solution are regular trees.
ii. Trees in a �nite set of regular trees are components of the unique solution of a
single system of regular equations.
We show how to construct a typing � with predicates in T
1
, from a set of basic
sorts S and a sorting ob de�ned on fs
1
; . . . s
n
g � S . First, build a system of regular
equations with equations of the form s
i
= (s
i
1
; . . . s
i
k
)
k
, for ob(s
i
) = s
i
1
� � � s
i
k
. Such
a system has a unique solution (�
1
; . . . �
n
) 2 T
n
1
. Then, whenever a : s, place in �
the assignment
a :
(
�
i
if s = s
i
; 1 � i � n
t otherwise, t a fresh variable
Conversely, to build S and ob from �, �rst let hs
1
= u
1
; . . . s
n
= u
n
i be a system
of equations whose solution contains the set of regular trees in �. Then, make
S = fs
1
; . . . s
n
g [K [ V and
ob(s
i
) =
(
u
i
if u
i
2 K [ V
s
i
1
� � � s
i
k
if u
i
= (s
i
1
; . . . s
i
k
)
k
Correctness of the constructions is ensured by the following theorem.
Theorem 5.1.3 Let S,ob and � be related as above. Then, a :s and ~x :ob(s) if and
only if a : (~�) and ~x : ~� are in �.
Example 5.1.4 We solve the system of regular equations induced by the sorting
to handle lists as in [15].
8
>
<
>
:
list = (cons, nil)
2
cons = (val, list)
2
nil = ()
0
The system is solved equation by equation. By substituting the right-hand side of the
second and third equations in the �rst, we obtain list = ((val list)
2
()
0
)
2
, whose
solution, as usual, we represent in TA
()�
by �t:((val t)()) (subscripts omitted.)
Compare it with the type scheme of Example 3.3.3.
Conversely, to deduce the system of regular equations whose solution contains
the in�nite tree � associated with the recursive type �t:((kt)()), we �rst identify
the subtrees of �. They are f�; �; k; ()g where � is such that �
�
= �t:(k(t())) (cf.
�g. 3.1). Then, we introduce one unknown x
i
(i = 1; . . . 4) for each subtree of �, and
de�ne a system of regular equations of the form x
i
= u
i
as follows,
8
>
>
>
<
>
>
>
:
x
1
= (x
2
; x
4
)
2
x
2
= (x
3
; x
1
)
2
x
3
= k
x
4
= ()
0
The original system is obtained by making x
1
= list, x
2
= cons and x
4
= nil.
14
5.2 Well-sorted located agents and well-typed processes
In [15], processes are called agents and are either abstractions or concretions. The
idea is to factor an input (output) guarded process of the form a(~x):P (a~x:P ) into
a location a (co-location a) and an abstraction (�~x):P (concretion [~v]:P .) An ab-
straction of the form �~x:P interacts with a concretion [~v]:Q to produce P j Qf~v=~xg.
Once located at some port, an abstraction becomes a usual input guarded process.
Similarly, a concretion located at some port becomes a simple output guarded pro-
cess.
Since processes in C do not encompass the notions of abstraction and concretion,
we restrict the syntax of agents to located agents in standard form [15], by dropping
from the grammar of agents the categories of abstraction and concretion. Again, we
do not consider summation.
De�nition 5.2.1 (Syntax of located agents) The syntax of located agents in
standard form (located agents for short) P is given by the following grammar.
A ::= 0 j a:�~xA j a:�~y[~x]A j A j B j �xA j !A
where A;B; . . . range over P.
Milner's program proposes a collection of formation rules ([15] pp. 34) under
which an agent is said to be well-sorted, with respect to a given a sorting and an
assignment of sorts to names. Bound names under di�erent bindings are assumed
to be distinct and disjoint from free names. The system below is an admissible
subsystem of this collection of rules.
Lemma 5.2.2 The following system, FR, is an admissible subsystem of the system
of formation rules.
a : s ~x : ob(s) A : ()
a:�~xA : ()
a : s ~x : ob(s) A : ()
a:�~y[~x]:A : ()
0 : ()
A : () B : ()
A j B : ()
A : ()
�xA : ()
A : ()
!A : ()
De�nition 5.2.3 (Mappings between located agents and processes)
i. [[�]]
P
: C ! P, is de�ned by: [[0]]
P
= 0, [[a~x:P ]]
P
= a:�~x[[P ]]
P
, [[a~x:P ]]
P
=
a:[~x][[P ]]
P
, [[P j Q]]
P
= [[P ]]
P
j [[Q]]
P
, [[�xP ]]
P
= �x[[P ]]
P
, and [[!P ]]
P
=![[P ]]
P
.
ii. [[�]]
C
: P ! C, is de�ned by: [[0]]
C
= 0, [[a:�~xA]]
C
= a~x:[[A]]
C
, [[a:�~y[~x]A]]
C
=
�~ya~x:[[A]]
C
if a 62 f~yg, [[A j B]]
C
= [[A]]
C
j [[B]]
C
, [[�xA]]
C
= �x[[A]]
C
, and [[!A]]
C
=
![[A]]
C
.
The following theorem states the equivalence between the notion of well-typing
in TA
()�
and that of well-sorting in Milner's system, the latter restricted to located
agents.
15
Theorem 5.2.4
i. If A is well-sorted in FR, then [[A]]
C
is well-typed in TA
()�
.
ii. Conversely, if P is well-typed in TA
()�
, then [[P ]]
P
is well-sorted in FR.
16
6.
Conclusion
We presented a typing system for a version of the polyadic �-calculus, where type
schemes are assigned to (free) names in a process, the process itself being assigned
multiple name-type scheme pairs. Well-typed processes in the system proposed
were proved not to run into type errors. An e�cient algorithm to compute the most
general typing scheme (or to detect its inexistence) was presented and proved correct
with respect to the typing system.
An interesting extension of the system encompasses the introduction of a ML-like
let constructor enabling to de�ne polymorphic processes and still keep the inference
algorithm e�cient.
17
Bibliography
[1] Alfred V. Aho, John E. Hopcroft, and Je�rey D. Ullman. The design and
analysis of computer algorithms. Addison-Wesley Pub., 1974.
[2] L.D. Baxter. The complexity of uni�cation. University of Waterloo, 1976. PhD
thesis.
[3] Felice Cardone and Mario Coppo. Two Extensions of Curry's Type Inference
System. In Logic and Computer Science, pages 19{75, Academic Press limited,
1990.
[4] Bruno Courcelle. Fundamental properties of in�nite trees. Theoretical Computer
Science, 25:95{169, 1983.
[5] Luis Damas and Robin Milner. Principal type-schemes for functional programs.
In 9th ACM Symposium on Principles of Programming Languages, pages 207{
212, 1982.
[6] Roger Hindley. The principal type-scheme of an object in combinatoric logic.
Transactions of the American Mathematical Society, 146:29{60, December 1969.
[7] Roger Hindley and Jonathan Seldin. Introduction to Combinators and �-
Calculus. Cambridge University Press, 1986.
[8] Kohei Honda. Types for Dyadic Interaction. CS 92-3, Keio University, 1992.
[9] Kohei Honda and Mario Tokoro. On Asynchronous Communication Semantics.
In Object-Based Concurrent Computing, pages 21{51, Springer-Verlag, 1992.
LNCS 612.
[10] Paul Hudak, Joseph Fasel, and al. Report on the Programming Language
Haskell: A Non-strict, Purely Functional Language. Version 1.2. Volume 27,
ACM Sigplan Notices, May 1992. 5.
[11] G. Huet. R�esolution d'�equations dans langages d'ordre 1; 2; . . .!. These d'Etat,
University Paris VII, 1976.
[12] Robin Milner. A theory of type polymorphism in programming. Journal of
Computer and System Sciences, 17:348{375, 1978.
18
[13] Robin Milner. Functions as Processes. Automata, Language and Programming,
Springer-Verlag, LNCS 443, 1990. Also as Rapport de Recherche No 1154,
INRIA-Sophia Antipolis, February 1990.
[14] Robin Milner. Sorts and Types in the �-Calculus. December 1990. University
of Edinburgh.
[15] Robin Milner. The Polyadic �-Calculus: a Tutorial. ECS-LFCS 91-180, Uni-
versity of Edinburgh, October 1991.
[16] Robin Milner and Mads Tofte. The de�nition of Standard ML. The MIT Press,
Cambridge, Massachusetts, 1991.
[17] John C. Mitchell. Handbook of Theoretical Computer Science, chapter Type
Systems for Programming Languages, pages 366{358. Elsevier Science Publish-
ers B.V., 1990.
[18] J.A. Robinson. Fast Uni�cation. In Tagung �uber Automatisches Beweisen,
Mathematisches Forschungsinstitut Oberwolfach, 1976.
[19] J.A. Robinson. Logic And Logic Programming. Communications of the ACM,
35(3):40{65, March 1992.
19
Appendix A.
Proofs
3 Types and typing assignment
Proposition 3.1.2 (Typings)
Directly from the de�nition of typing compatibility. 2
Theorem 3.1.4 (Well-typings and free names)
i FN (P ) � N (�) by a simple induction on the length of deductions.
ii `
()
P � �j�P by induction on the length of deductions. Cases the last rule is
Nil, Scop and Repl are trivial. Since cases Emt and Rcpt present the same kind
of technical di�culties, we will consider just one of them.
Case the last rule is Emt.
Suppose that `
()
P � �, f~v : ~�g � � and that � � fa : (~�)g. The induction
hypothesis is `
()
P � �j�P . The application of the last rule yields `
()
a~v:P � �[fa :
(~�)g. Using theWeak rule as many times as needed to introduce assignments v
i
:�
i
whenever v
i
2 f~vg not in �j�P , we obtain `
()
P � �j�P[f~v : ~�g. By proposition 3.1.2,
�j�P [f~v : ~�g � fa : (~�)g, and by rule Emt, `
()
a~v:P � �j�P [f~v : ~�g[fa : (~�)g. But
�j�P [ f~v : ~�g [ fa : (~�)g = (� [ fa : (~�)g)j�a~v:P .
Case the last rule is Comp.
Suppose that `
()
P � �, `
()
Q � � and that � � �. The application of the last
rule yields `
()
P j Q � � [ �. By proposition 3.1.2, �j�P � �j�Q, and by rule
Comp, `
()
P j Q � �j�P [�j�Q. By the �rst part of this theorem, all the free names
of P are in �, and similarly for Q and �. Hence, �j�P [�j�Q = (� [�)j�(P j Q).
Case the last rule is Weak.
Suppose that `
()
P � � and that x is not in �. The induction hypothesis is
`
()
P � �j�P . The application of the last rule yields `
()
P � � � x :�, for some type
scheme �. Since x is not in �, by the �rst part of this theorem, x is not free in P .
Hence, �j�P = (� � x :�)j�P . 2
The following corollary enables to consider, without loss of generality, only de-
ductions of the form `
()
P � �j�P .
Corollary A.0.5 A deduction of `
()
P � � can be factored out in two parts:
i. a deduction of `
()
P � �j�P , followed by
20
ii. a series of applications of the Weak rule.
Proof: Let `
()
P � �j�P [ fx
1
: �
1
; . . .x
n
: �
n
g. By theorem 3.1.4ii there is a
deduction `
()
P � �j�P . Use the Weak rule n times to introduce type assignments
x
i
:�
i
, in the typing of P . 2
The following lemma will be used in the proof of the structural congruence lemma
and the subject-reduction theorem.
Lemma A.0.6 (Substitution Lemma) Let `
()
P � � and x 2 FN (P ).
If z 62 N (�) or fx : �; z : �g � �, for some type scheme �, then `
()
P [z=x] �
�[z=x].
Proof: By induction on the structure of deductions. The most interesting cases
are described below.
Case P is �yQ.
Suppose that `
()
Q[z=x] � �[z=x]. By rule Scop `
()
�yP [z=x] � �[z=x]=y. Since x
is free in �yP , x 6= y and hence, �[z=x]=y = (�=y)[z=x].
Case P is a~y:Q.
Suppose that `
()
Q � � � ~y : ~�, � � fa : (~�)g, and that `
()
Q[z=x] � (� � ~y : ~�)[z=x].
By proposition 3.2.1, �[z=x] � fa : (~�)g[z=x]. Since x is free in a~y:Q, x 62 f~yg,
and hence, by rule Rcpt `
()
a[z=x]~y:Q[z=x] � �[z=x] [ fa : (~�)g[z=x], that is
`
()
(a~y:Q)[z=x] � (� [ fa : (~�)g)[z=x]. 2
Lemma 3.1.5 (Structural congruence and typings) Without loss of gen-
erality, assume all deductions are of the form `
()
P � �j�P .
Case P �
�
Q.
By induction on the structure of P . The non-trivial cases are when P is �xR or P
is a~x:R.
Subcase �xP �
�
�zP [z=x], z not free in P .
Suppose that `
()
P � �. Using the Scop rule we have `
()
�xP � �=x. Since z is not
free in P , by assumption z is not in �. Also, since x is free in P , by the substitution
lemma, `
()
P [z=x] � �[z=x]. Using the Scop rule `
()
�zP [z=x] � �[z=x]=z = �=x.
Subcase a~x:P �
�
a~x[z=x
i
]:P [z=x
i
], x
i
2 f~xg and z not free in P .
Suppose that `
()
P � � � ~x : ~� and that � � fa : (~�)g. Using the Rcpt rule we have
`
()
a~x:P � � [ fa : (~�)g. Since z in not free in P , by assumption z is not in � � ~x: ~�.
Also, since x
i
is free in P , by the substitution lemma, `
()
P [z=x
i
] � � � (~x[z=x
i
] : ~�).
Using the Rcpt rule `
()
a~x[z=x
i
]:P [z=x
i
] � � [ fa : (~�)g.
Case P j Q � Q j P .
Follows directly from the commutativity of set union.
Case (P
1
j P
2
) j P
3
� P
1
j (P
2
j P
3
).
We have `
()
P
i
� �
i
, for i = 1; 2; 3, with �
i
� �
j
. The result follows from the
associativity of set union.
Case �xP j Q � �x(P j Q), x 62 FN (Q).
Suppose that `
()
P � �, `
()
Q � � and � � �. ()) Using the Scop followed by
the Comp rule, we have `
()
�xP j Q � �=x[�, since �=x � � by proposition 3.1.2.
21
(() Using the Comp followed by the Scop rule, we have `
()
�x(P j Q) � (�[�)=x.
Since x is not free in Q, by assumption x does not occur in �, and thus (�[�)=x =
�=x [�.
Case !P � P j !P .
Suppose `
()
P � �. ()) By rule Repl, `
()
!P � �. (() Since, by proposition 3.1.2,
� � �, by rule Comp `
()
P j !P � � [ � = �.
Case P � Q implies P j R � Q j R and Case P � Q implies �xP � �xQ are
trivial. 2
Theorem 3.1.6 (Subject Reduction)
By induction on the lenght of deductions. If reduction ends with the STRUCT rule,
the result is immediate from lemma 3.1.5. We concentrate on the case reduction
ends with rule COM. Then we have a sequent of the form `
()
� ~w(@ j a~x:P j a~v:Q j
@
0
) � �. We safely assume that we have a deduction for `
()
a~x:P j a~v:Q � �, by
lemma 3.1.5. Now we argue that the following holds.
`
()
a~x:P j a~v:Q � � implies `
()
Pfv=xg j Q � �
Once this holds, we replace the deduction for a~x:P j a~v:Q with that for Pfv=xg j Q,
which in e�ect leads to the deduction of the sequent `
()
� ~w(@ j Pfv=xg j Q j @
0
) �
�.
So assume `
()
a~x:P j a~v:Q � �. At some preceding stage of the derivation, we
have a sequent of the form `
()
P � �
1
� ~x: ~� and �
1
� �. Similarly we have, at some
preceding stage of the deduction, `
()
Q � �
2
with f~v : ~�g � �
2
and �
2
� �. Note
that we also have �
1
� �
2
.
Now we inspect the typing for Pfv=xg. At some preceding stage of the derivation,
we have `
()
P � �
0
1
, where �
0
1
= (�
1
� ~x : ~�)j�P . By theorem 3.1.4, if x
i
2 N (�
0
1
),
then x
i
2 FN (P ), and hence, by the substitution lemma `
()
P [v
i
=x
i
] � �
0
1
[v
i
=x
i
],
where either v
i
62 N (�
0
1
) or v
i
: �
i
2 �
0
1
. Noting that if x
i
62 N (�
0
1
), then the
substitution for x
i
has no e�ect, we get `
()
Pfv=xg � �
0
1
fv=xg = (�
1
� ~v : ~�)j�P .
Since f~v : ~�g � �
2
, by proposition 3.1.2, (�
1
� ~v : ~�)j�P � �
2
, and by rule Comp
`
()
Pfv=xg j Q � �
1
j�P [ �
2
. Then we apply the Weak rule as many times as
needed to obtain `
()
Pfv=xg j Q � �, thus concluding the proof. 2
Corollary 3.1.7 (Runtime errors)
Suppose that P !! P
0
and that P is typable. By the subject-reduction theorem,
P
0
is typable. Let P
0
� � ~w(@ j a~x:Q
1
j a~v:Q
2
j @
0
). Then, at some preceding
stage of the deduction, we have sequents `
()
a~x:Q
1
� �
1
and `
()
�a~v:Q
2
� �
2
with
a 2 N (�
1
) \N (�
2
) and �
1
� �
2
. This implies len(~x) = len(~v). 2
Proposition 3.2.1 (Typings and substitutions)
Directly from the de�nition of typing compatibility. 2
Lemma 3.2.2 (Well-typings and typing instances)
(Outline) By induction on the length of deductions. Since compatibility of typings
is preserved by substitution, replace in the deduction of P � �, all occurrences of
x:� by x :s�, to obtain a deduction of P � s�. 2
22
4 Typing inference in TA
()�
Theorem 4.2.2 (Soundness of the typing algorithm)
A simple induction on the cases analyzed by TA. Cases TA(0), TA(�xP ) and
TA(!Q) are trivial. Since cases TA(a~v:P ) and TA(a~x:P) present the same kind of
technical di�culties, we will consider just one of them.
Case TA(a~x:P ).
Suppose that TA(P ) = �, Comm(~x;�) = ~� and that Unify (�=~x;fa : (~�)g) = s.
The induction hypothesis is `
()�
P � �. Use the Weak rule as many times
as needed to introduce assignments v
i
: �
i
(v
i
2 f~vg) whenever v
i
not in �. We
obtain `
()
P � �=~x [ f~v : ~�g. By lemma 3.2.2, `
()�
P � s(�=~x [ f~v : ~�g). Since
s uni�es �=~x and fa : (~�)g, we have s�=~x � sfa : (~�)g. Hence, by rule Rcpt,
`
()�
a~x:P � s�=~x [ sfa : (~�)g.
Case TA(P j Q).
Suppose that TA(P ) = �, TA(Q) = � and Unify (�;�) = s. The induction
hypothesis is `
()�
P � � and `
()�
Q � �. By lemma 3.2.2, `
()�
P � s� and
`
()�
Q � s�, and by proposition 3.2.1, s� � s�. Hence, by rule Comp, `
()
P j Q �
s� [ s�. 2
Theorem 4.2.3 (Completeness of the typing algorithm)
By induction on the length of deductions. Cases the last rule is Nil, Scop and
Repl are trivial. Since cases Emt and Rcpt present the same kind of technical
di�culties, we will consider just one of them.
Case the last rule is Emt.
Suppose that `
()�
P � �, f~v : ~�g � �, and that � � fa : (~�)g. The induc-
tion hypothesis are that TA(P ) = � and that there is a substitution s
0
such that
s
0
� = �j�P .
i Let Comm(~v;�) = ~�. According to the de�nition of Comm, extend s
0
to assign-
ments v
i
: t
i
(v
i
2 f~vg and t
i
a fresh variable) whenever v
i
does not occur in �, by
making s
0
t
i
= �
i
. We now have s
0
~� = ~� and thus s
0
(� [ f~v : ~�g) = (�j�P ) [ f~v : ~�g.
By proposition 3.1.2, s
0
(� [ f~v : ~�g) = (�j�P) [ f~v : ~�g � � � fa : (~�)g = s
0
fa : (~�)g.
Hence s
0
uni�es � [ f~v : ~�g and fa : (~�)g, Unify (� [ f~v : ~�g;fa : (~�)g) succeeds and so
does TA(a~v:P ).
ii Since � [ f~v : ~�g and fa : (~�)g are uni�able, let Unify (� [ f~v : ~�g;fa : (~�)g) = r
where r is the m.g.u. of � [ f~v : ~�g and fa : (~�)g. Since s
0
also uni�es � [ f~v : ~�g and
fa : (~�)g, there is a substitution u such that s
0
= ur. Let s be u. We successively
have sTA(a~v:P ) = s(r(� [ f~v : ~�g) [ r(fa : (~�)g)) = s
0
(� [ f~v : ~�g) [ s
0
fa : (~�g) =
(�j�P ) [ f~v : ~�g [ fa : (~�)g = (� [ fa : (~�)g)j�a~v:P .
Case the last rule is Comp.
Suppose that `
()�
P
i
� �
i
, for i = 1; 2, and that �
1
� �
2
. The induction hypothesis
are that TA(P
i
) = �
i
and that there are substitutions s
i
such that s
i
�
i
= �
i
j�P
i
.
i By proposition 3.1.2, s
1
�
1
= �
1
j�P
1
� �
1
� �
2
� �
2
j�P
2
= s
2
�
2
. Let s
0
be the
substitution s
1
[ s
2
. Then s
0
uni�es �
1
and �
2
, Unify (�
1
;�
2
) succeeds and so does
TA(P
1
j P
2
).
ii Since �
1
and �
2
are uni�able, let Unify (�
1
;�
2
) = r where r is the m.g.u. of �
1
23
and �
2
. Since s
0
also uni�es �
1
and �
2
, there is a substitution u such that s
0
= ur.
Let s be u. We successively have sTA(P
1
j P
2
) = s(r�
1
[ r�
2
) = s
0
�
1
[ s
0
�
2
=
�
1
j�P
1
[�
2
j�P
2
= (�
1
[�
2
)j�(P
1
j P
2
), since by theorem 3.1.4.i all free names of P
i
are in �
i
.
Case the last rule is Weak.
Suppose that `
()�
P � �. The induction hypothesis are that TA(P ) succeeds and
that there is a substitution s such that sTA(P ) = �j�P .
i Directly from the induction hypothesis.
ii Suppose that the application of the last rule yields `
()�
P � � �x :�, for x not in
� and � some type scheme. By theorem 3.1.4.i all free names of P are in �. Hence
x is not free in P and thus �j�P = (� � x :�)j�P . 2
5 Comparison to Milner's sorting program
Theorem 5.1.2 (Regular Systems)
i Corresponds to the �rst part of theorem 4.2.1. of [4]. ii The proof of theorem
4.2.1. of [4] still holds if u is an element of fSubtree(t) j t 2 Tg, T a �nite set of
trees. 2
Theorem 5.1.3 (Sortings and typings)
()) Directly from the method of solving the system of equations corresponding to
S and ob (cf. 4.4 of [4].) (() Directly from the method of building the system of
equations corresponding to the regular trees in � (cf. 4.2.1 of [4].) 2
Lemma 5.2.2 (Admissibility of the system FR)
The rule for a located abstraction of the form a:�x
1
� � �x
n
A can be replaced by a
deduction of the form,
a : s
x
n
: s
n
x
1
: s
1
A : ()
(�x
1
)A : (s
1
)
.
.
.
(�x
1
� � �x
n�1
)A : (s
1
� � � s
n�1
)
(�x
1
� � �x
n
)A : (s
1
� � � s
n
)
(ob(s) = s
1
� � � s
n
)
a:�x
1
� � �x
n
A : ()
A similar construction proves the admissibility of the rule for a located concretion.
The remaining rules are already in the original system. 2
Theorem 5.2.4 (Well-sortedness and well-typedness)
(Outline) i From ob and the assignment of names to sorts, build �. A deduction of
[[A]]
C
can be obtained by replacing all occurrences of 0 : () in the deduction of A : ()
by `
()�
0 � � (by using the Weak rule as many times as needed) and by replacing
every occurrence of any other rule in FR by the corresponding one in TA
()�
. The
side conditions on compatibility of typings hold from the fact that all typings in the
24
deduction are subsets of �. Conditions ~x: ~� and a : (~�) in Rcpt and Emt rules hold
from theorem 5.1.3.
ii We assume bound names under di�erent bindings in P are all distinct and disjoint
from the free names. Collect in � all type assignments appearing in the deduction of
P and, from this typing, build S and ob. In order to build a deduction of [[P ]]
P
, erase
all occurrences of theWeak and the (�) rules form the deduction of P , and replace
occurrences of the other rules by the corresponding rules in FR. Conditions a :s and
~x:ob(s) in the rules corresponding to Rcpt and Emt hold from theorem 5.1.3. 2
25