25
UNIVERSITY OF SUSSEX COMPUTER SCIENCE A chemical abstract machine for graph reduction Alan Jeffrey Report 3/92 August 1992 Computer Science School of Cognitive and Computing Sciences University of Sussex Brighton BN1 9QH ISSN 1350–3170

A chemical abstract machine for graph reduction Alan Jeffreyfpl.cs.depaul.edu/ajeffrey/papers/cs0392.pdf · A chemical abstract machine for graph reduction Alan Jeffrey Report 3/92

Embed Size (px)

Citation preview

UNIVERSITY OF SUSSEX

COMPUTER SCIENCE

A chemical abstract machinefor graph reduction

Alan Jeffrey

Report 3/92 August 1992

Computer ScienceSchool of Cognitive and Computing Sciences

University of SussexBrighton BN1 9QH

ISSN 1350–3170

A chemical abstract machinefor graph reduction

ALAN JEFFREY

ABSTRACT. Graph reduction is an implementation technique for the lazy λ-calculus. It has beenused to implement many non-strict functional languages, such as lazy ML, Gofer and Miranda. Par-allel graph reduction allows for concurrent evaluation. In this paper, we present parallel graph re-duction as a Chemical Abstract Machine, and show that the resulting testing semantics is adequatewrt testing equivalence for the lazy λ-calculus. We also present a π-calculus implementation of thegraph reduction machine, and show that the resulting testing semantics is also adequate.

1 Introduction

The lazy reduction strategy for the λ-calculus investigated by ABRAMSKY (1989)has only two reduction rules:

�λx � E � F � E � F � x � E � E �

EF � E � FThis can be compared with the full evaluation strategy of BARENDREGT (1984):

�λx � E � F ��� E � F � x � E ��� E �

EF ��� E � FF ��� F �

EF ��� EF �E ��� E �

λx � E ��� λx � E �If the full evaluation strategy can terminate, then the lazy evaluation strategywill. For example, if we define: λxy � x� λx � x� λx � � � λy � x � yy � � � λy � x � yy � ���then

��� � ∞ but

� � ��� ���� ∞ , whereas

� � ��� ����� ∞ . However, the lazy evaluationstrategy is very inefficient, since it may duplicate arguments when applying afunction. For example, if we define:�

0 ��

i � 1 �

λx � xx ��

i

Copyright c�

1992 Alan Jeffrey.Author’s address: COGS, University of Sussex, Brighton BN1 9QH, UK.EMail: [email protected] work was supported by SERC project GR/H 16537.Miranda is a trademark of Research Software Limited.

2 Alan Jeffrey

Then

�i � � 2i � but

�i � 2i

�1 � 2 �

, that is the lazy strategy can be exponential-ly worse than the full strategy. Thus, the early functional languages, such asLISP (MCCARTHY et al., 1962) used a strict reduction scheme rather than thelazy reduction scheme.

Graph reduction was introduced by WADSWORTH (1971) as a means of ef-ficiently implementing the lazy reduction strategy. Rather than reducing syntaxtrees, we reduce syntax graphs which allows a more efficient representation ofsharing. For example, we can represent the reduction of

�i � 1 as:

@� �λx

�i�

@� �x x

� @����i

� 2i@���� � �

Graph reduction has been used to implement non-strict functional languagessuch as JOHNSSON’s lazy ML (1984), JONES’s Gofer (1992) and TURNER’s Mi-randa (1985). It is discussed in PEYTON JONES’s textbook (1987).

However, there has been little work in the formal semantics of graph reduc-tion. BARENDREGT et al. (1987) have shown that graph reduction is sound andcomplete with respect to term reduction. LESTER (1989) has shown that theG-machine of AUGUSTSSON (1984) and JOHNSSON (1984) is adequate wrt a de-notational model of the lazy λ-calculus. In this paper, we provide an alternativepresentation of graph reduction, as a Chemical Abstract Machine (CHAM), in thestyle of BERRY and BOUDOL (1990).

The CHAM was introduced as a way of presenting the operational semanticsof parallel languages in a clean fashion. It has been used to give a semantics forMILNER’s CCS (1989) and MILNER, PARROW and WALKER’s π-calculus (1989).

Here, we shall give a semantics for parallel graph reduction with blocking,as described by PEYTON JONES (1987). We will show that this is an adequatesemantics for the lazy λ-calculus, and that it can be implemented in a variant ofthe π-calculus.

2 The lazy lambda-calculus

The λ-calculus, introduced by CHURCH (1941), has the following syntax:

E :: x � EE � λx � Ewhere x ranges over an infinite set of variables. This can be given a number ofoperational semantics, but we shall only look at two of these. We shall call these

A CHAM for graph reduction 3

the lazy semantics:

�λx � E � F � E � F � x � E � E �

EF � E � Fand the full semantics:

�λx � E � F ��� E � F � x � E ��� E �

EF ��� E � FF ��� F �

EF ��� EF �E ��� E �

λx � E ��� λx � E �Here, E �F � x � is E, with every free occurrence of x replaced by F , up to the usualrenaming of bound variables.

PROPOSITION 1.

1. If E � E � and E � E � � then E � E � � .2. If E � ∞ and E � � E � then E � � ∞ .3. If E

� � � ��� � ∞ then E � ∞ .

PROOF. Part 1 is a simple induction. Part 2 follows from Theorem 13.2.2 ofBARANDREGT (1984), since E � E � is the leftmost full reduction of E. Part 3follows from Theorem 13.2.6 of BARANDREGT (1984).

We can define a variant of MORRIS’s testing pre-order (BARENDREGT, 1984,Exercise 16.5.5):

E � F iff � C � C � F � � ∞ � C �E � � ∞

We can also define a variant of the λ-calculus with recursive declarations andstrictness annotations:

M :: x � xy � λx �M ����� x : D �� M

D :: ?M � !MHere:� ��� x : ?M �� N declares x recursively to be M in the context N. For example,

a fixed point of f is ��� x : ? f �� ����� y : ?xy �� y.� ��� x : !M �� N is the same, except that x is strict in N, and so evaluation ofM can be sparked off as a parallel computation.

We shall let bound variables be α-converted. The free variables of M are fvM:

fvx �� x �fv�xy � �� x � y �

fv�λx �M � fvM � � x �

fv� ���� x : D �� M � �

fvD � fvM ��� � x �fv�!M � fvM

fv�?M � fvM

4 Alan Jeffrey

There is a translation � from the λ-calculus to the λ-calculus with ���� :x x

EF ��� x : !E �� ���� y : ?F �� xy

λx � E λx � ENote that in the translation of EF, we know that E will be used, and so it can beevaluated strictly. On the other hand, we do not know if F will be used or not, soit cannot be annotated.

3 The chemical abstract machine

The Chemical Abstract Machine (CHAM) of BERRY and BOUDOL (1990) is away of presenting the operational semantics of parallel systems. We shall use itto give a semantics for parallel graph reduction of the λ-calculus with ��� .

A CHAM gives reductions between solutions, which are multisets (or bags)of molecules. The definition of molecules is specific to each CHAM, but a solu-tion can always be regarded as a molecule. In such a molecule, � �m1 � ��� � � mn � � ,the multiset brackets � ������� � � are called a membrane. There are three types ofreduction:� Heating rules, of the form S � S � .� Cooling rules, of the form S � S � .� Reaction rules, of the form S �� S � .

Heating and cooling rules are always given in pairs S��

S � , whereas reactionrules are irreversible. We shall write

�� � for the transitive, reflexive, symmetricclosure of

��, write � for

�� � �� �� � , and let � range over � , � and �� . AllCHAMs have the following structural rules, where m ��� � is a molecule containingprecisely one hole:

S � S �S

S � � � S � S � �S � S �

� �m � S � � � � � �m � S � � � �In addition, the CHAMs we shall consider in this paper allow the outermostmembrane of any solution to be ignored. This allows us to write m1 � � ��� � mn

�m �1 ��� � � � m �n for � �m1 � � ��� � mn � � � � �m �1 ��� ��� � m �n � � :

� � S � � ��S

The molecules and reduction rules are specific to each CHAM. In the case of thegraph reduction CHAM, molecules are defined:

m :: x : D � S � νx � SThe free variables of m are fvm:

fv�x : D � � x � � fvD

A CHAM for graph reduction 5

fv � �m1 ��� ��� � mn � � fvm1 � ����� � fvmn

fv�νx �m � fvm � � x �

The defined variables of m are dvm:

dv�x : D � � x �

dv � �m1 � ��� � � mn � � dvm1 � ����� � dvmn

dv�νx �m � dvm � � x �

We shall only consider solutions which do not define any variables twice, so inany solution � �m1 � ��� � � mn � � , the defined variables of each mi are distinct. For ex-ample, we do not allow solutions such as νx � � � x : !λw �M � x : !λw � N � y : !xw � �which could reduce nondeterministically to � � y : !M � � or to � � y : !N � � . Ifx x1 ��� � � � xn then we can write νx �m for νx � � �m � � and νx �m for νx1 ��� � νxn �m.Define:

� a molecule is a positive ion with valency x iff it is x : ?M or x : !λy �M.� a molecule is a negative ion with valency y iff it is x : !y or x : !yz.� a molecule is ionic iff it is a positive or negative ion.� a solution is plasmic iff it is � � νy � � �m1 � ��� � � mn � � � � or � �m1 � ��� � � mn � � whereeach mi is ionic. A plasma is positive (negative) iff it contains only positive(negative) ions

Plasmas can be regarded as graphs, for example the graph reduction:

z!@� �

!λx ?

�i�

!@� �!x ?x

�z

!@� �!∇ ?∇� �

?

�i

�z

!@� �!∇ ?∇� �

!

�i

� 7i

z!@� �

!∇ ?∇� �!λx�!x

z!@� �

!λx ?∇� �!x !λx�

!x

z!∇�?∇�!λx�!x

z!∇�!∇�!λx�!x

�z

!∇�!λx�!x

�z

!λx�!x

6 Alan Jeffrey

is represented by the CHAM reduction:

νxy � � � z : !xy � y : ?

�i � x : !λw �ww � �

� νuvy � � � z : !uv � y : ?

�i � v : ?y � u : !y � �

� νuvy � � � z : !uv � y : !

�i � v : ?y � u : !y � �

� 7i νuvy � � � z : !uv � y : !� � v : ?y � u : !y � �

� νuvy � � � z : !uv � y : !� � v : ?y � u : !

� � �� νvy � � � z : !v � y : !

� � v : ?y � �� νvy � � � z : !v � y : !

� � v : !y � �� νv � � � z : !v � v : !

� � �� � � z : !

� � �In these diagrams:� Tagged nodes x : !M are labelled with a !.� Untagged nodes x : ?M are labelled with a ?.� Application nodes x : yz are labelled with a @.� Indirection nodes x : y are labelled with a y, if y is free, and with ∇ other-

wise.� Function nodes x : λy �M are labelled with a λy, and have the graph for� � z : !M � � drawn beneath them, for some fresh variable z.

The supercombinators of HUGHES (1984) can be regarded as a form of solution.PEYTON JONES (1987) presents supercombinators as a collection of definitions,plus a variable to be evaluated. These are drawn:

DefinitionsVariable

These can be regarded as solutions. For example, the supercombinator program:

$X0 x0 E0

$X1 x1 E1

...

$Xn xn En

$X0

is equivalent to the solution:

νX0 ��� � Xn� �X0 : !λx0 � E0 � X1 : ?λx1 � E1 � ��� � � Xn : ?λxn � En � �

The most important heating rule allows recursive declarations to become part ofa solution, whilst hiding the bound variable. This is only valid when it would

A CHAM for graph reduction 7

not cause the free variable x to become bound by y, which we can achieve byα-converting y first.

x : ! ��� y : D �� M��

νy � � � x : !M � y : D � � �x � y �

The scope of a hidden variable can migrate, as long as this does not result invariable capture:

m � νx �m � �� νx � � �m � m � � � �x �� fvm �

Hidden variables may be α-converted, exchanged and evaporated:

νx �m ��νy � � m � y � x � � �

y �� fvm �νxy �m ��

νyx �mνx � � � � � �� � � � �

Finally, we can perform garbage collection on positive plasmas, since a hiddenpositive plasma can never make any reductions:

νx � � � x : D � � �� � � � � � � � x : D � � is a positive plama �We shall sometimes write

��γ for this thermal action, and

����� γ for any other

thermal action. For example, the graph reduction:

z!@� �

!�

?� �

z!∇�?�

can be derived:

νyx � � � x : !� � y : ?

� � z : !xy � ��� νyx � � � x : !

� � y : ?� � z : !y � �

� νyx � � � x : !� � � � y : ?

� � z : !y � � � �� νy � � � νx � � � x : !

� � ��� � � y : ?� � z : !y � � � �

�γ νy � � � � � y : ?

� � z : !y � � � �� νy � � � y : ?

� � z : !y � �A reaction can occur whenever one positive and one negative ion with the samevalency exist in a solution. Since there are two kinds of positive ion and twokinds of negative ion, there are four reaction rules. The first two allow untaggedmolecules to become tagged:

x : !y � y : ?M �� x : !y � y : !M

x : !yz � y : ?M �� x : !yz � y : !M

8 Alan Jeffrey

These can be drawn:

!∇�?M

�� !∇�!M

!@�?M

�� !@�!M

This models the first phase of graph reduction—we search along the spine of agraph, tagging nodes for evaluation. Note that strict reactions do not occur:

x : !yz � z : ?M ��� x : !yz � z : !M

If a tagged indirection node points to a function, we can just copy the function.The SKIM (STOYE et al., 1984) and G-machine (JOHNNSON, 1984) use this as amethod of eliminating indirection nodes. It was shown by LESTER (1989) to beadequate:

x : !y � y : !λw �M �� x : !λw �M � y : !λw �MThis can be drawn:

!∇�!λw �M

�� !λw �M!λw �M

If an application node points to a function, it can be β-reduced:

x : !yz � y : !λw �M �� x : !M � z � w � � y : !λw �MThis can be drawn:

!@� �!λw N�M� �

w ����� w

��

M� �!λw ∇ ����� ∇� � �M N� �

w ����� w

We shall sometimes write �� β for this reaction, and �� �� β for any other reaction.This CHAM is summarized in Table 1.

This CHAM implements the algorithm for parallel graph reduction describedby PEYTON JONES (1987). A process is assigned to evaluating a node, whichis tagged. It then searches along the spine, tagging each node as it passes. If

A CHAM for graph reduction 9���

S� ����

S

x : � ! ��� y : � D � M��

νy � ��� x : � !M � y : � D� � �

x �� y �m � νx � m �� νx � ���m � m � � �

x �� fvm �νx � m ��

νy � � m � y � x ��� �y �� fvm �

νxy �m ��νyx � m

νx � ��� � � ������ � �ν x � ��� x : � D � � ������ � � �����

x : � D� �

is a positive plasma �x : � !y � y : � ?M �� x : � !y � y : � !M

x : � !yz � y : � ?M �� x : � !yz � y : � !M

x : � !y � y : � !λw�M �� x : � !λw�M � y : � !λw�Mx : � !yz � y : � !λw�M �� x : � !M � z � w ��� y : � !λw� M

TABLE 1. Summary of the graph reduction CHAM

it reaches a function node which can be β-reduced, it does so. If it reaches afunction node which cannot be β-reduced, this is returned as the result. If itreaches a previously tagged application or indirection node, it is blocked untilthe tagged node is evaluated. For example, in the graph:

x!@� �

!∇ !@� � �?M ?N

only one process will evaluate M. This is mirrored in the CHAM by the fact thatM will only be reduced once. However, this algorithm produces some surprisingresults with cyclic graphs. The solution � � y : ! ���� x : !x �� x � � heats to becomethe plasma � � νx � � � y : !x � x : !x � � � � and the graph:

y!∇�!∇� �

This has no reductions, because it is negative. This is mirrored in the parallelgraph reduction algorithm, since the process evaluating y will discover that theindirection node at x has already been tagged. Thus, it is possible for evaluationsto deadlock, when a sequential algorithm would diverge.

Our translation of the λ-calculus will not produce cyclic graphs, although it

10 Alan Jeffrey

can still produce divergent terms. For example, the translation of�λx � xx � � λx � xx �

has the reductions:

z!@� �

λx λx� �!@ !@� � � �

x x x x

z!@� �λx�!@� �

x x

z!@���λx�!@� �

x x

� �����

Since E has no tight loops, we will be able to show that the CHAM semanticsfor the λ-calculus is adequate. To do this, we define the testing preorder onmolecules:

m � m � iff � C � C �m � � � ∞ � C �m � � ∞

and show in the next section that the CHAM semantics is adequate, that is if�x : ?E � � �

x : ?F � then E � F .

PROPOSITION 2.

1. For any S there is a plasmic S � such that S � �� γ S � .2. If S is plasmic and S � S � then S �� �� � S � .3. S � S � iff S � � �� γ

�� �� � S �4. S

� � � �� γ�� � ∞ iff S � ∞

5. S � ∞�� β iff S � ∞�� β�� γ

6. S �� ∞�� β7. If S � S � and S � S � � then S � �� � S � � or S � � S � � � and S � � � S � � � .8. S �� ∞ iff S ��� ��PROOF.

1. Prove by induction on S.

2. Prove by case analysis that if S is plasmic and S�� �� S � then S �� ��

S � . Thenshow by induction on

�� � that if S�� � �� S � that S �� �� � S � .

3. If S � S � then by parts 1 and 2, S � � �� γ S � � �� �� � S � for some plasmic S � � . ThusS � �� γ

�� � S � .4. If S � ∞ then define Si by induction:

� S0 S.� Assuming Si � ∞ , Si � S � � ∞ , so by part 3, Si

� � �� γ�� �� γ Si � 1

�� � S � , soSi � 1 � ∞ .

A CHAM for graph reduction 11

Thus, S S0� � �� γ

�� S1� � �� γ

�� ����� and so S � ∞ implies S� � � �� γ

�� � ∞ . Thereverse implication is trivial.

5. Is proved similarly.

6. Define #m by:

# � �m1 � ��� � � mn � � #m1 � ����� � #mn

#�νx �m � #m

#�x : D � #D

#�!M � #M

#�?M � #M � 1

#�λx �M � 0

#y 1

#yz 1

#� ���� x : D �� M � #D � #M

Then:

S�� �� γ S � � #S #S �

S �� �� β S � � #S � #S �Thus, S �� ∞�� β

�� γ , so by part 5, S �� ∞�� β .

7. Show by case analysis that if S is plasmic and S �� S � and S �� S � � then S � S � �or S � �� S � � � and S � � �� S � � � .

Then for any S, if S � S � and S � S � � then by parts 1 and 2 there is aplasmic S1 such that S

�� � S1�� S �1 �� � S � and S1

�� S � �1 �� � S � � . Then eitherS �1 S � �1 , so S � �� � S � � , or S �1 �� S � � � and S � �1 �� S � � � , so S � � S � � � and S � � � S � � � .

8. If S � ∞ and S � � �� then we must have:

S S00 � S0

1 � ����� � S0n � S0

n � 1

S S00 � S1

1 � ����� � Snn ��

Then for 0 � i � j � n � 1, define Sij such that Si � 1

j � 1 � Sij and Si

j � 1 � Sij:

� If Si � 1j � 1

�� � Sij � 1 then let Si

j Si � 1

j .� Otherwise, Si � 1

j � 2 � Si � 1j � 1 and Si � 1

j � 2 � Sij � 1 so by part 7, we can find Si

j

such that Si � 1j � 1 � Si

j and Sij � 1 � Si

j.

Then Snn � Sn

n � 1 which contradicts our hypothesis. Thus, if S � � �� thenS �� ∞ .

Note that Proposition 2.7 shows that this CHAM is Church-Rosser.

12 Alan Jeffrey

4 The CHAM semantics is adequate

MILNER, PARROW and WALKER (1989) have shown a very close relationshipbetween the lazy reduction strategy and their translation of the λ-calculus intothe π-calculus, that E � F iff � � E � � � � �F � � .

This is not be true of the CHAM semantics for graph reduction, where there isno one-to-one correspondence between CHAM reductions and λ-calculus reduc-tions. For example, the CHAM reduction:

x!@� �

?@ ?E� �?G ?F

�x

!@� �!@ ?E� �

?G ?F

corresponds to the null reduction GFE � 0 GFE, and the CHAM reduction:

!@���!@���!λx�x

!@���!∇�!λx�x

corresponds to� � � � � � � ��� � �

. In general, each CHAM reduction S � �� β S � cor-responds to a λ-calculus reduction E � 0 E, and each CHAM reduction S � β S �corresponds to a λ-calculus reduction E � � � � E � .

In order to formalize this, we need to know when a CHAM solution can im-plement a λ-term. For example, some possible implementations of

� � � � � � are:

x!@� �

!@ !@� � � �!�

!�

!�

!�

x!@� �

!@ !@��� ���!�

!�

x!@� �

!@ !@� � � �!�

!�

!�

x!@���!@���!�

We will write S � M � E iff M implements E in the solution S. For example,S � x � � � � � � � for any of the above solutions. S � M � E is defined in Table 2.

PROPOSITION 3.

A CHAM for graph reduction 13

�x �

S � x � x� x �� dvS �

�xy � S � x � E S � y � F

S � xy � EF

�λx � S � M � E

S � λx � M � λx � E � x �� fvS ��νx � S � M � E

νx � S � M � E� x �� fvM � E �

� � � S � M � ES � M � E

� S � S ��? � S � x : � !M � N � E

S � x : � ?M � N � E

�: � E � S � x : � !M � x � E

S � M � E� x �� fvE � M � S �

�: � I � S � M � E

S � x : � !M � x � E� x �� fvE � M � S �

TABLE 2. The definition of S � M � E.

1. If S � M � E and S � M � F then E F .2. If S � x : !yz � x � E then S � y � F, S � z � G and E FG.3. If S � x : !λy �M � x � E then S � M � F and E λy � F .4. If νx � S � M � E and x �� fvM � E then S � M � E5. If S � x : !M � x � E and S � x : !M � N � F then S � N � G and F G � E � x � .6. If S � x : !y � y : !M � N � E then S � x : !M � y : !M � N � E.7. If x �� fvE then x : !E � x � E.

PROOF.

1. First, prove by induction on the proof of S � x � E that if x �� dvS and S � x � Ethen E x. Then prove by induction on the proof of S � M � E that if S �M � E and S � M � F then E F . The only tricky case is if S � M � E followsfrom

�x � , in which case M E x, and since S � x � F the above gives that

F x.

2. Prove by induction on the proof that if νw � � S � x : !yz � � x � E or S � yz � Ethen S � y � F and S � z � G and E FG.

3. Prove by induction on the proof that if νw � � S � x : !λy �M � � x � E or S �λy �M � E then for any z, S � z � y � � M � F and E λy � F . The only tricky case isif S � λy �M � E follows from

�νy � , in which case S νy � S � and S � � λy �M � E.

By induction, S � � u � y � � M � F and E λy � F for some fresh u, so by�νu � ,

νu � � S � � u � y � � � M � F so by� � � , νy � S � � M � F , so S � M � F, so since y �� fvS,

14 Alan Jeffrey

S � z � y � � M � F.

4. Prove by induction on the proof of νxx � S � M � E that if νxx � S � M � E andx �� fvM � E that νx � S � M � E.

5. Prove by induction on the proof of νw � � S � x : !M � � N � F that if νw � � S � x : !M � � x � E and νw � � S � x : !M � � N � F then S � N � G and F G � E � x � .

6. First, prove by induction on the proof of S � M � E that if y �� fvE � M � S andS � M � E then S � y : !N � M � E. Then prove by induction on the proof ofS � M � E that if νw

�S � x : !y � y : !M � � N � E then S � x : !M � y : !M �

N � E and if νw�S � y : !M � � y � E then S � y : !M � M � E.

7. Proved by induction on E.�

However, the definition of S � M � E ignores whether declarations are tagged oruntagged. In order to reason about the reductions of a solution, we shall need totake tagging into account.

The sequence x0 : D0 ��� ��� � xn : Dn is an x-spine in S iff:

S�� � νy � � � x : D � S � � �

where x x0 and each xi : Di is a negative ion with valency xi � 1. The variablex is lazy in a plasma S iff the negative ions of S form an x-spine. The variable xis always lazy in S iff for any S � � S � , x is lazy in S � .

For example, the z-spine of the plasma:

νyz � � � z : !z0 � z0 : !z1y1 ��� ��� � zn � 1 : !znyn �zn : ?M � y0 : ?N1 ��� ��� � yn : ?Nn � �

which can be drawn:

z!∇�!@� �

...

?N1�!@� �

?M ?Nn

is z : !z0 � z0 : !z1y1 ��� � � � zn � 1 : !znyn. Since these are the only negative ions,this solution is lazy.

PROPOSITION 4. x is always lazy in � � x : !E � � .

A CHAM for graph reduction 15

PROOF. Define x is invariantly lazy (or i-lazy) in S by:� x is i-lazy in � � � �� If x is i-lazy in S and x � y then x is i-lazy in νy � S� If y is i-lazy in S then x is i-lazy in � � x : !y � S � � .� If y is i-lazy in S then x is i-lazy in � � x : !yz � S � � .� If x is i-lazy in S and y is i-lazy in � � y : !M � � then x is i-lazy in � � y : ?M � S � � .� If x is i-lazy in S and for all z, y is i-lazy in � � y : !M � z � w � � � then x is i-lazy

in � � y : !λw �M � S � � .� If x is i-lazy in S and S��

S � then x is i-lazy in S � .Then we can prove:� If x is i-lazy in S then x is lazy in S.� If x is i-lazy in S and S � S � then x is i-lazy in S � .� x is i-lazy in � � x : !E � � .

Thus, if x is i-lazy in S then x is always lazy in S, and so x is always lazy in� � x : !E � � .�

Then we can show the important properties, that link CHAM reductions to λ-calculus reductions.

PROPOSITION 5. If S � x � E and x is always lazy in S then:

1. If S � �� β S � then S � � x � E.2. If S � β S � then E � � ��� E � and S � � x � E � .3. If E � E � then S � � �� β � β S � , E � ��� � E � � and S � � x � E � � .PROOF.

1. If:

S�� � νy � � u : !v � v : ?M � ��� � � �

S � �� � νy � � u : !v � v : !M ��� � � � �then S � � x � E. If:

S�� � νy � � u : !vw � v : ?M ��� � � � �

S � �� � νy � � u : !vw � v : !M ��� ��� � �then S � � x � E. If:

S�� � νy � � u : !v � v : !λy �M ��� � � � �

S � �� � νy � � u : !λy �M � v : !λy �M � � ��� � �then S � � x � E. These are the only three rules for S � �� β S � , so S � � x � E

2. Since x is lazy in S, and S � β S � , we know that:

S�� � νz � � � xn � 1 : !λw �M � S � � � �

16 Alan Jeffrey

S � �� � νz � � � xn � 1 : !M � yn � w � � S � � � �S � � �� � � � x0 : !M0 � � ��� � xn : !Mn ��� ��� � �Mi

xi � 1 or xi � 1yi � 1

x0 x

We shall prove the case when each Mi xi � 1yi � 1, since indirection nodes

make little difference. By Propositions 3.4, 3.2 and 3.3 we have:

E �λw � F � Fn ����� F0

xn � 1 : !λw �M � S � � � xi ��λw � F � Fn ����� Fi

xn � 1 : !λw �M � S � � � M � F

xn � 1 : !λw �M � S � � � yi � Fi

S � � � xn � 1 � λw � FS � � � yn � Fn

and by Proposition 3.5:

S � � � yi � Gi

S � � � xi ��λw � F � FnGn � 1 ����� Gi

Fi Gi � � λw � F � Fn � xn �

So:

S � � M � F

S � � M � yn � w � � F �Fn � w �S � � xn � F � Fn � w �S � � yn � Fn

S � � yi � Hi

S � � xi � F � Fn � w � Hn � 1 ����� Hi

S � � x � E �Hi

Gi � F � Fn � w � � xn �E � F � Fn � w � Hn � 1 ����� H0

Since�λw � F � Fn � F � Fn � w � :

Fi Gi � � λw � F � Fn � xn � � � � Gi � F � Fn � w � � xn � Hi

so:E �

λw � F � Fn ����� F0 � F � Fn � w � Fn � 1 ����� F0� � � F � Fn � w � Hn � 1 ����� H0 E �

Thus, S � � x � E � and E � � � � E � .

A CHAM for graph reduction 17

3. Define Si by induction:� S0

S.� Let Si

νz � � � x0 : D0 � � ��� � xm : Dm � � and let x0 � � ��� � xn be the x-spine ofSi. If Dn � 1

?M then define:

Si � 1 νz � � � x0 : D0 ��� � � � xn : Dn �

xn � 1 : !M � xn � 2 : Dn � 2 � � ��� � xm : Dm � �so Si � �� β Si � 1. If Dn

!xn � 1 and Dn � 1 !λw �M then define:

Si � 1 νz � � � x0 : D0 � ��� � � xn � 1 : Dn � 1 �

xn : Dn � 1 � xn � 1 : Dn � 1 � ��� � � xm : Dm � �so Si � �� β Si � 1. If Dn

!xn � 1y and Dn � 1 !λw �M then define:

S � νz � � � x0 : D0 ��� ��� � xn � 1 : Dn � 1 �xn : !M � y � w � � xn � 1 : Dn � 1 � ��� � � xm : Dm � �

so Si � β S � .Thus, either S � ∞�� β (which is impossible, by Proposition 2.6) or S � � �� β � βS � . By parts 1 and 2, E � E � � � ��� � E � � and S � � x � E � � . Since E � E � andE � E � � � , by Proposition 1.1 E � E � � � , and so E � E � ��� � E � � . �

From the above propositions, we can show that a λ-term diverges iff its CHAM

implementation diverges.

PROPOSITION 6. If x �� fvE then E � ∞ iff � � x : !E � � � ∞

PROOF.� Define Si and Ei by induction:

� S0 � � x : E � � and E0

E.� By induction, Si � x � Ei and Ei � ∞ . Then by Proposition 5.3, Si � �Si � 1, Ei � � ��� Ei � 1 and Si � 1 � x � Ei � 1. By Proposition 1.2, since Ei � ∞

, we know Ei � 1 � ∞ .

Thus � � x : !E � � S0 � � S1 � � S2 � � ����� .� Since � � x : !E � � � ∞ , by Proposition 2.6:

� � x : !E � � S0 � � �� β � β S1 � � �� β � β �����

so by Propositions 5.1 and 5.2:

E E0 � ��� � E1 � ��� � �����

so E� � ��� � � ∞ , so by Proposition 1.3, E � ∞ .

18 Alan Jeffrey

Thus, we can show that the CHAM semantics is adequate.

THEOREM 7 (ADEQUACY). If�x : ?E ��� �

x : ?F � then E � F.

PROOF. For any context C:

C �F � � ∞ � �λw �C �w � � F � ∞

� � � z : !�λw �C �w � � F � � � ∞

� νxy � � � z : !yx � y : !λw � C �w � � x : ?F � � � ∞

� νxy � � � z : !yx � y : !λw � C �w � � x : ?E � � � ∞

� � � z : !�λw �C �w � � E � � � ∞

� �λw �C �w � � E � ∞

� C � E � � ∞

Thus, E � F .�

However, it is not fully abstract.

THEOREM 8. E � F does not imply�x : ?E ��� �

x : ?F � .PROOF. The CHAM semantics is not fully abstract because we can define ABRAM-SKY’s (1989) � combinator in the λ-calculus with ���� :

� λy � ���� x : !y �� �� cannot be defined in the λ-calculus, and provides extra testing power to theλ-calculus with ��� . Define:

E λx � x � λy � x ��� y ���F λx � x � x ��� ���� λx � � �

� ���C ��� � νxy � � � � � y : ! � � z : !xy � �

ONG (1988, Theorem 4.5.1.1) has shown that E and F are applicative bisimi-lar (ABRAMSKY, 1989) and so E � F . However:

C � x : ?E � � � � � z : ! � � � ��C � x : ?F � � � νxy � � � x : ! � � y : !x � z : ! � � � � ∞

Thus, by Proposition 2.8, C � x : ?E � �� ∞ , so�x : ?E � �� �

x : ?F � . �

It is an open problem as to whether the CHAM semantics is fully abstract wrtthe λ-calculus with � , and as to whether the canonical semantics for the lazyλ-calculus D �

�D � D ��� is adequate wrt the CHAM semantics.

A CHAM for graph reduction 19���

S� � ��

S

P�Q��

P� Qνx � P ��

νx � ���P � �m � νx � m �� νx � ���m � m � � �

x �� fvm �νx � m ��

νy � � m � y � x � � �y �� fvm �

νxy � m ��νyx � m

νxx � m ��νx �m

� x � x � P ��P

� x �� y � P ��P

�x �� y �

A�

x � �� P � x � y � �A�

y �def� P �x � yz �� x � vw � � P �� P � y � v� z � w �

TABLE 3. CHAM for the π-calculus

5 The asynchronous pi-calculus

The π-calculus, introduced by MILNER, PARROW and WALKER (1989) is a pro-cess algebra in which scope is considered important. MILNER has shown that itcan be used to model pointer-structures (1991) and the lazy λ-calculus (1992),which has been further investigated by SANGIORNI (1991).

Since the π-calculus was designed with pointer structures and the λ-calculusin mind, it seems natural to use it to encode a parallel graph reduction algorithm.We shall consider a variant of BOUDOL’s asynchronous π-calculus (1992). Thishas the syntax:

P :: x � yz � � x � yz � � P � P � P � νx � P � � x y � P � � x � y � P � A � x �Here:� x � yz � is the process which outputs the pair

�y � z � along channel x.� x

�yz � � P is the process which inputs a pair

�y � � z � � along channel x, then be-

haves like P � x � � x � y � � y � .� P � Q places P and Q in parallel.� νx � P creates a new channel x for use in P.� � x y � P acts like P whenever x y, and deadlocks otherwise.� � x � y � P acts like P whenever x � y, and deadlocks otherwise.� A�x � is a recursive definition, in the style of MILNER (1989). We shall assume

an environment of definitions A�x � def P, where fvP � x.

The CHAM for this variant of the asynchronous π-calculus is given in Table 3,and is very similar to BOUDOL’s CHAM for the asynchronous π-calculus (1992).The only new rules are:� � � S � � ��

S, which is missing from BOUDOL’s paper. This rule is required to

20 Alan Jeffrey

prove the result that for any solution S there is a process P such that S�� �� �P � � . For example, we cannot show � � � � x � yz � � � � � �� � � � x � yz � � � without this

rule.� � x x � P ��P and � x � y � P ��

P whenever x � y, which gives semantics forthe conditional operators missing from BOUDOL’s paper.

� A�x � ��

P � x � y� whenever A�y � def P, which gives semantics for recursive def-

initions which were not used in BOUDOL’s paper.

We can define much of the same vocabulary for this CHAM as we did for thegraph reduction CHAM. In Proposition 10 we shall see that this abuse of notationis justified.� A molecule is a positive ion with valency x iff it is x

�yz � � P.� A molecule is a negative ion with valency x iff it is x � yz � .� A molecule is ionic iff it is a positive or negative ion.� A solution is plasmic iff it is � � νx � � �P1 � ��� � � Pn � � � � or � �P1 � � ��� � Pn � � and each Pi

is ionic. A plasma is positive (negative) iff it contains only positive (negative)ions.

We can give a translation of each molecule of the graph reduction CHAM into theπ-calculus. This uses a special variable � , which we shall use to represent a func-tion which is being evaluated, but which has not (yet) been given an argument.The semantics for terms is:

� � x � � z def x � � z �� � xy � � z def x � yz �

� � λx �M � � z def !z�xy � � � � x � � � � λx �M � � y � � x � � � � �M � � y �

� ����� x : D �� M � � z def νx� � �D � � x � � �M � � z � �

x � z �where MILNER’s (1991) replication operator is defined:

!Pdef !P � P

Note that the definition of � � λx �M � � is recursive, which is why we are taking re-cursion to be primitive, rather than replication. It is not obvious whether onecould define semantics using replication for which Proposition 11 would hold.Note also that � � ��� x : D �� M � � z is defined only when x � z, but we can useα-conversion on x to assure this. The semantics for declarations is:

� � !M � � z def � �M � � z� � ?M � � z def z

�xy � � � z � xy � � � �M � � z �

The semantics for molecules is:

� � x : D � � def � �D � � x

A CHAM for graph reduction 21

z��� � �!@������ ��!λx � xx

��� ��? � i�

z��� ��!@������ ��!∇�

��� ��?∇���� ��? � i

z��� � �!@������ ��!∇�

��� ��?∇��� � ��! � i

� 7i

z��� ��!@������ ��!∇�

��� ��?∇���� � �! �

z�� � ��!@������ ��! �

��� ��?∇���� ��! �

z��� ��!∇���� � �?∇���� ��! �

z��� � �!∇���� � �!∇��� � ��! �

�z��� ��!∇���� � �! �

� z��� � �! �

TABLE 4. A sample graph reduction in the π-calculus

� � νx �m � � def νx � � �m � �� � � �m1 ��� ��� � mn � � � � def�� � � �m1 � ����� ��� � � �mn � � � �

This semantics can be drawn with process graphs. For example, if we draw:z�����D for � �D � � z

z��� !∇�x

for � � !x � � z z��� ?∇�x

for � � ?x � � zz� �

!@���x y

for � � !xy � � z z� � ?@���

x yfor � � ?xy � � z

Then the reduction of

�i given on page 5 can be drawn (with some extraneous

processes removed to account for garbage collection) in Table 4. This is exactlythe same reduction as given on page 5.

In general, we can show that each CHAM reduction is matched by exactly oneπ-calculus reduction, and thus that a CHAM solution diverges iff its π-calculus se-mantics diverges. This will allow us to show that the π-calculus semantics is ad-equate wrt the CHAM semantics for graph reduction (and so wrt the λ-calculus).

PROPOSITION 9.

1. For any S there is a plasmic S � such that S � S � .

22 Alan Jeffrey

2. If S is plasmic and S � S � then S �� �� � S � .3. S � ∞ iff S

� � � �� � ∞PROOF. As for Proposition 2.

6 The pi-calculus semantics is adequate

We will show that the π-calculus semantics models the CHAM semantics forgraph reduction without garbage collection. To begin with, we can justify anabuse of notation:

PROPOSITION 10.

1. m is a (positive) (negative) ion iff � �m � � is a (positive) (negative) ion.2. S is a (positive) (negative) plasma iff � � S � � is a (positive) (negative) plasma.

PROOF. A simple case analysis.�

Then we can show that each reduction of the graph reduction CHAM can bematched by a π-calculus reduction:

PROPOSITION 11.

1. If � � S � � � T then T � � � � S � � � and S � �� γ S � .2. If S � �� γ S � then � � S � � � � � � � S � � � .3. If � � S � � �� T then T � � � � S � � � and S �� S � .4. If S �� S � then � � S � � �� � � � � S � � � .5. � � S � � � ∞ iff S � ∞

PROOF. Parts 1–4 are simple case analyses. From parts 1–4, � � S � � � � � �� � ∞ iffS� � � �� β

�� � ∞ , so by Propositions 2.4 and 9.3, � � S � � � ∞ iff S � ∞ , which provespart 5.

This means that the π-calculus semantics is adequate.

THEOREM 12 (ADEQUACY). If � � S � � � � � S � � � then S � S � .PROOF. For anyC, if C � S � � � ∞ then � �C � S � � � � � ∞ so � �C � � � � � S � � � � � ∞ so � �C � � � � � S � � � � ∞

so � �C � S � � � � ∞ so C � S � � ∞ . Thus, S � S � . �

However, it is not fully abstract.

THEOREM 13. m � m � does not imply � �m � ��� � �m � � � .PROOF. Define R to be a simulation up to

�� � , similar to (MILNER, 1989, Def-inition 4.4), iff whenever m R m � and m � � m � � � then m � m � � and m � � �� � R �� �m � � � . Let � be the largest simulation up to

�� � , and let � C be the largest pre-congruence contained in � :

m � C m � iff � C � C �m ��� C �m � �

A CHAM for graph reduction 23

Then if m � m � and m � � ∞ then m � ∞ , so if m � C m � then m � m � . Define:

m � � x : !z � y : !z � �m � � � x : !y � y : !z � �R � � C � x : !z � y : !z �

� x : !z � y : !λw1 �M1 � z : !λw1 �M1 � ������ x : !z � y : !λwn �Mn � z : !λwn �Mn � �C � x : !y � y : !z �

� x : !y � y : !λw1 �M1 � z : !λw1 �M1 � ������ x : !y � y : !λwn �Mn � z : !λwn �Mn � �� C is an n � 1-place context �

Then R is a simulation up to�� � , so for any context C, C �m � � C �m � � , so m � C m � ,

so m � m � . We will now show that � �m � � �� � �m � � � . Define:

C ��� � � � � � y � uv � � � � �where � is a divergent process such as:

� def νw � � w �ww � � w � uv � � � �Then:

C � � �m � � � �� � � � z � � x � � z � � y � � y � uv � � � � � ��whereas:

C � � �m � � � � �� � � � y � � x � � z � � y � � y � uv � � � � � � � � z � � y � � � � � � ∞

so � �m � � �� � �m � � � . �

SANGIORNI (1991) has investigated λ-calculi for which MILNER’s π-calculustranslation is fully abstract. It is an open problem as to whether similar resultscan be shown for the CHAM for graph reduction.

Acknowledgements

Many thanks to Lennart Augustsson for getting the ball rolling, to Thomas Hall-gren, Magnus Carlsson and Niklas Rojemo for conversations about the innardsof LML, and to K. V. S. Prasad, Jenny Petersson, Matthew Hennessy, EdmundRobinson, Steve Schneider and Mark Jones for many discussions. This work wasconceived at Chalmers University and written at Sussex University and DriveCottage, Ampney Knowle, Cirencester.

ReferencesABRAMSKY, S. (1989). The lazy lambda calculus. In TURNER, D., editor, Declarative Programming.

Addison-Wesley.

24 Alan Jeffrey

AUGUSTSSON, L. (1984). A compiler for lazy ML. In Proc. ACM Symp. Lisp and FunctionalProgramming, pages 218–227.

BARENDREGT, H. (1984). The Lambda Calculus. North-Holland. Studies in logic 103.

BARENDREGT, H. P., VAN EEKELEN, M. C. J. D., GLAUERT, J. R. W., KENNAWAY, J. R., PLASMEI-JER, M. J., and SLEEP, M. R. (1987). Term graph rewriting. In Proc. PARLE 87, volume 2, pages141–158. Springer-Verlag. LNCS 259.

BERRY, G. and BOUDOL, G. (1990). The chemical abstract machine. In Proc. 17th Ann. Symp.Principles of Programming Languages.

BOUDOL, G. (1992). Asynchrony and the pi-calculus. INRIA Sophia-Antipolis.

CHURCH, A. (1941). The Calculi of Lambda Conversion. Princeton University Press.

HUGHES, R. J. M. (1984). The Design and Implementation of Programming Languages. D.Philthesis, Oxford University.

JOHNNSON, T. (1984). Effecient compilation of lazy evaluation. In Proc. Sigplan 84 Symp. CompilerConstruction, pages 58–69.

JONES, M. (1992). The Gofer technical manual. Part of the Gofer distribution.

LESTER, D. (1989). Combinator Graph Reduction: A Congruence and its Applications. D.Philthesis, Oxford University.

MCCARTHY, J., ABRAHAMS, P. W., EDWARDS, D. J., HART, T. P., and LEVIN, M. I. (1962). The Lisp1.5 Programmers Kit. MIT Press.

MILNER, R. (1989). Communication and Concurrency. Prentice-Hall.

MILNER, R. (1991). The polyadic π-calculus: a tutorial. In Proc. International Summer School onLogic and Algebra of Specification, Marktoberdorf.

MILNER, R. (1992). Functions as processes. Math. Struct. in Comput. Science, 2:119–141.

MILNER, R., PARROW, J., and WALKER, D. (1989). A calculus of mobile processes. Technicalreports ECS-LFCS-89-86 and -87, LFCS, University of Edinburgh.

ONG, C.-H. L. (1988). The Lazy Lambda Calculus: An Investigation into the Foundations of Func-tional Programming. PhD thesis, Imperial College, London University.

PEYTON JONES, S. (1987). The Implementation of Functional Programming Languages. Prentice-Hall.

SANGIORGI, D. (1991). The lazy lambda calculus in a concurrency scenario. Technical ReportECS-LFCS-91-189, LFCS, Edinburgh University.

STOYE, W. R., CLARKE, T. J. W., and NORMAN, A. C. (1984). Some practical methods for rapidcombinator reduction. In Proc. ACM Symp. Lisp and Functional Programming, pages 159–166.

TURNER, D. (1985). Miranda: A non-strict functional language with polymorphic types. In Proc.IFIP Conf. Functional Programming Languages and Computer Architecture. Springer-Verlag.LNCS 201.

WADSWORTH, C. P. (1971). Semantics and Pragmatics of the Lambda Calculus. D.Phil thesis,Oxford University.