Cyclic Lambda Graph Rewriting Zena M. Ariola
Jan Willem Klop
Computer & Information Science Dept. University of Oregon Eugene, OR 97401
Dept. of Software Technology, CWI Dept. of Computer Science, Free University Amsterdam, The Netherlands
[email protected]
[email protected]
Abstract This paper is concerned with the study of cyclic graphs. The starting point is to treat a -graph as a system of recursion equations involving -terms, and to manipulate such systems in an unrestricted manner, using equational logic, just as is possible for rstorder term rewriting. Surprisingly, now the con uence property breaks down in an essential way. Con uence can be restored by introducing a restraining mechanism on the `copying' operation. This leads to a family of -graph calculi, which are inspired by the family of -calculi (-calculi with explicit substitution). However, these concern acyclic expressions only. In this paper we are not concerned with optimality questions for acyclic -reduction. We also indicate how Wadsworth's interpreter can be simulated in the -graph rewrite rules that we propose.
Introduction As shown in recent years, rst-order orthogonal term rewriting [8, 19] has quite pleasant con uent extensions to the case where cycles are admitted (term graph rewriting [26]) and to the case where in nite terms and trans nitely long rewrite sequences are admitted (in nitary term rewriting [17]). This paper is concerned with similar questions for -calculus, and attempts to formulate a con uent calculus for possibly cyclic -graphs. The starting point is to treat a -graph as a system of recursion equations involving -terms, and to manipulate such systems in an unrestricted manner, using equational logic, just as is possible for rst-order term rewriting [3]. Surprisingly, now the con uence property breaks down in an essential way. To understand this phenomenon better, the in nitary version of -calculus, called 1 , that we intro-
duce here turns out to be of great value. Also, we use 1 as semantics for the -graph calculus, providing us with a soundness criterion. We then signal the paradox that -calculus (i.e., -calculus extended with the -rule) also embodies much of cyclic -graph rewriting, but yet is con uent. This leads us to some restrained but con uent -graph calculi, able to simulate -calculus, but also supporting the indispensable feature of subtermsharing that lacks. The restraining mechanism is given by nesting of recursion equations, just like the letrec construct. The restraints are in the fact that the `copying' operation is more controlled than in the naive or unrestricted version for which we demonstrate non-con uence. The restrained -graph calculi that we discuss are inspired by the family of -calculi (-calculi with explicit substitution) [11, 1, 7, 22]. However, these concern acyclic expressions only. A related system for `explicit cyclic substitutions' has been given by Rose [25]. In this paper we are not concerned with optimality questions for acyclic -reduction as in the work of [21, 15, 10, 9]. We conclude with an indication that Wadsworth's interpreter [28] can be simulated in the -graph rewrite rules that we propose.
1 Lambda graphs as systems of recursion equations In the rst and most extensive part of the paper (Sections 1-7) we will consider -graph rewriting in a `naive' or `unrestricted' format. In the nal sections we consider a constraining mechanism on graph rewriting aiming at the con uence property a property that will be seen to break down in the unrestricted version.
We will express -graphs as systems of recursion equations. Thus we may write: = x:x:
-calculus that will not be repeated here. However, also the x in F(x) is to be considered as bound by x, even though not `directly' in its scope. This is not a problematic matter. More subtle is the following:
This is an object whose unwinding is an `in nite normal form', also known as Bohm-tree [6]. We also may consider mutual recursion as in
= (x:F(; Sx))Sx1:
= (x: xx); = (y:y) : We will always use ; ; , for recursion variables. For the time being, variables bound by are denoted by x; y; z; . (In Section 9 this notation convention will be changed.) Note that the in nite tree unwinding of the last recursion system is not a Bohm-tree, as it contains many -redexes. An important feature of these systems of recursion equations is that it allows to express `horizontal sharing' - i.e., sharing as in a `dag', as opposed to the `vertical sharing' as in the examples above. E.g., : = (x: )C; = F(0): Note that we admit next to pure -terms, extended with recursion variables, also operators from a rstorder signature, like F; C; 0 above. Thus we use a harmless mixture of applicative notation (with the application operator @ as usual suppressed, except in gures of -graphs), and `functional' notation where operators have some arity (like the unary F above). In rendering a recursion system, it is understood that the rst (or topmost) equation is the leading equation, displaying the root of the -graph. When we want to be more precise, we will render the last example as h j = (x: )C; = F(0)i: The order of the equations in the `body' of the h j i construct is not important then. Furthermore, we will consider recursion systems obtained from each other by 1-1 renaming of recursion variables, as identical. Thus, h j = (x:
)C; = F(0)i is the same expression as the previous one.
1.1 Free and bound variables We now consider the notion of a variable (x; y; ) bound by a . This is a matter of some delicacy. First, consider = x:x ; = F(x): Clearly, the visible occurrence of x in x is bound by x, according to the usual binding rules for ordinary
Should the second occurrence of x, superscripted by 1, be considered bound by x? On the one hand, it seems to be free in , but then, the second occurrence of is in the direct scope of x - so it might be considered bound by x. Now the following heuristic consideration decides the issue. Let us de ne for the moment by using the xed point combinator Y, with result Y(:((x:F(; Sx))Sx)). The right-hand side can be correctly renamed to Y(:((y:F(; Sy))Sx)). This suggests that we should also rename the original recursion equation to = (y:F(; Sy))Sx. So the occurrence of x in question should not be considered bound by x. Indeed, this convention is consistent with other ways of introducing cyclic -graphs (or as we will say interchangeably, systems of recursion equations). (One alternative way, introduced in [28], is to render cyclic -graphs with pointers to binding 's instead of using variables x; y; z. But this way is convenient for pictures, not for linear textual representation.) It is not hard now to give a precise de nition of free and bound variables x; y; . In this informal setting we will refrain from doing that, also because in the nal part we will consider a framework with nested systems of recursion equations (at present they are un-nested) where the above `problem' does not occur at all. Henceforth, we will always adopt the `variable convention' saying that bound variables x; y; will always be renamed apart as far as possible. In particular, a recursion system will not contain two occurrences of one x; and the above discussed variable confusion will not be present. And a system containing a free x will not be allowed to have occurrences of x.
1.2 Correspondence with graphs It is straightforward to assign actual graphs to the recursion systems as introduced above. In the sequel there will be several examples. One feature should be mentioned explicitly: the nodes of the graph contain rst-order operators (F), or application (@), or x, or a variable x; y; z; . Other than that, a node may have a name ; ; ; . These correspond to the recursion variables in the recursion system. Note
that also unnamed nodes may be present in the graph (corresponding to subterms in the system that have no name, like x in h j = x:xi). In the present setting, the root node of the -graph will always have a name (not so in the framework at the end of the paper).
2 Lambda graph rewriting We now turn to the issue of de ning -reduction on -graphs. Due to the possible presence of cycles, it may not immediately be clear what the `right' notion of -reduction is. In order to decide what is a right notion, we will later on introduce an in nitary version of -calculus, against which any notion of -reduction for cyclic recursion systems can be (and should be) compared as to soundness. First, we will proceed in an intuitive fashion. We give some examples: h j = (x: xx); = (y:y) i ! h j = ; = (y:y) i ! h j = ; = i Here there is no problem. The next example involves the argument of a -redex to be percolated downwards through the system: h j = (x:x )F(0); = G(x; ; ); = H(x; )i ! h j = F(0) ; = G(F(0); ; ); = H(F(0); )i: As a third example, consider
h j = x: (Sx); = y:(Sy)i: Here are two `potential' redexes, (Sx) and (Sy). Now we will require that such redexes rst must be made `actual' or `explicit', by substitution of the righthand side of an equation for a recursion variable. The act of substitution will be denoted by !s . Thus: h j = x: (Sx); = y:(Sy)i !s h j = x:(y:(Sy))(Sx); = y:(Sy)i ! h j = x:(S(Sx)); = y:(Sy)i h j = x:(S(Sx))i Here in the last step we have applied garbage collection, i.e., the removal of an equation that has become inaccessible from the root . Our stipulation that -reduction can only be performed on `explicit' -redexes in a system (even though the body of the -redex may be dispersed through the system) is a matter of choice; de nitions of -reduction directly on `implicit' -redexes (such
as (Sx) in the last example) are thinkable. However, this stipulation makes it more clear, intuitively, what goes on. More important is that making -redexes explicit involves making a copy of part of the graph in a way that is often necessary. An example is: h j = F( 0; 1); = x:xi !s h j = F((x:x)0; (1)); = x:xi ! h j = F(0; 1); = x:xi Note that the substitution has performed a copy of x:x, as is in this case anyway necessary.
3 The collapse problem In orthogonal term graph rewriting (rewriting with an orthogonal rst-order term rewriting system, admitting cyclic term graphs) and in nitary term rewriting (admitting in nite trees) it has been a matter of some discussion what to do with `collapsing operators' such as a unary operator I with the rule I(x) ! x. Speci cally, what should `cyclic-I', that is, h j = I()i, rewrite to? If this object rewrites to itself, then non-con uence arises. For, let J be another collapsing operator with J(x) ! x. Then h j = I(J())i rewrites to both h j = I()i and h j = J()i. The simple solution is to proceed with rewriting; both of these last two expressions rewrite to h j = i which has to be perceived as a new object, that we will call (black hole), corresponding to the unde ned -expression :. We have used a new symbol to denote this result of a cyclic collapse, rather than use e.g., ?, as represents a very speci c and `very unde ned' kind of expression; it is a special case of expressions being unde ned by lack of a head normal form. For a comparison of notions of unde nedness in orthogonal term (graph) rewriting, see [4]. Also in the present setting we need the introduction of the singularity point . E.g., , consider the -graph: = (x: )A; = (x:)B: Contracting the -redex yields = ; = (x:)B, which is equivalent to = (x:)B. Contracting the -redex yields = (x: )A; = , which is equivalent to = (x:)A: Both contracted graphs yield after one more reduction = , and this is equivalent to = . Note that also mutual vacuous dependencies of recursion variables have to be replaced by . E.g., h j = ; = i ! . Or, inside a system: h j = F((x:x) ); = (y:y) i ?! ! h j = F( ); = i ?! h j = F()i.
The calculus that we have informally introduced now, will be referred to as ?-calculus. Now we want to prove con uence of ? in analogy with the situation for orthogonal term graph rewriting involving cycles. However, there are some nasty surprises.
4 A counterexample to con uence α λx @
β λy
S
@
x S y
α λx
α λx
@
@ S
β λy
S
@
S x S
x S y
Failure of confluence after reduction of spine-cyclic redexes
Figure 1. Remarkably, orthogonal term graph rewriting is con uent [26], while -graph rewriting is not, as shown in the following example. Consider the reductions (displayed in Figure 1) : = x: (Sx); = y:(Sy)
.s
= x:(y:(Sy))(Sx)
.
= x:(S(Sx))
&s
= x: (Sx); = y:(x: (Sx))(Sy)
&
= x: (Sx); = y: (S(Sy)) Now we cannot obtain a common reduct because the two expressions are clearly out-of-synch; at each point in time the one on the left will have an even number of S0s, while the one on the right will contain an odd number of S0 s. (The full paper contains an elaborated analysis of this counterexample.)
This phenomenon may be thought to be dependent of our particular choice of the -reduction rule. However, we claim that it is robust: this can be seen by an excursion to what we perceive as the underlying semantics of graph rewriting, i.e. in nitary rewriting, explained in the next section. Actually, one may wonder whether the noncon uence observed above is not caused already on a more basic level rather than by -reduction. For, consider the recursion system over a rst-order signature, without any rewrite rules: h j = S( ); = S()i: By substitution (!s) this expression yields on the one hand h j = S(S())i, on the other hand h j = S( ); = S(S( ))i. These two results cannot be made convergent by further substitutions, by the same parity argument as above. However, the non-con uence of the recursion system involving terms is more `serious', less curable, than the present one. Namely, suppose we allow the re-introduction of names (recursion variables). Then we can restore h j = S(S())i to h j = S( ); = S()i and converge again. In [3] we have elaborated this matter distinguishing some simple operations like !s on recursion systems over a rst-order signature; with reintroduction of names a con uence result holds there, but not without, due to `cyclic substitutions' as above. However, in the present -graph counterexample, also re-introduction of names does not help. The two expressions h j = x:(S(Sx))i and h j = x: (Sx); = y: (S(Sy))i, are irreversibly separated with respect to any set of operations on -graphs that is `sound' in a sense that we will elaborate now.
5 In nitary lambda calculus: the 1calculus We will now give a short exposition of the in nitary version of -calculus, analogous to the in nitary version of orthogonal term rewriting as developed in [17]. We will need in nitary -calculus as the `semantics' of -graph rewriting, providing us with a notion of correctness of proposed de nitions of -reduction of graphs, and explaining the counterexamples for ( nitary) con uence of -reduction of such graphs. The de nitions of in nite -term (or -tree), bound and free variable, in nite reduction, even trans nite reduction, are entirely straightforward, with the trans nite version of term rewriting in mind. The crucial notion of strongly convergent reduction sequences, which
λx
λx
2
@1
Cauchy converging reduction sequence: activity may occur everywhere
@1
@ 1
λy
S
@2
x
λx
λx
2
λy
λy
S x
S x
S y
@1
Strongly converging reduction sequence, with descendant relations
λy @2 λx
Figure 2. may be of ordinal length, is the same for in nitary -calculus as it was for term rewriting. In short, a strongly convergent reduction is such that the stable pre x of the term is increasing (see Figure 2), with the depth of contracted redexes tending to in nity. Note the analogy between the nitary and in nitary settings; a nite reduction will correspond to a (strongly) convergent reduction, while an in nite reduction will correspond to a non-convergent reduction. In trans nite orthogonal term rewriting there is a single source of failure of in nitary con uence: the presence of collapsing operators, such as the I or K combinators, enabling one to build trees that consist of an in nite tower of collapsing operators, or rather collapsing contexts. This is proved in [17]. In the present case, that is also a source of non-con uence. But here the matter is more complicated, and there is another phenomenon that causes in nitary noncon uence, not due to collapsing contexts. To explain this, we rst need the following concept.
5.1
! -developments
The classical notions in -calculus of `development' and `complete development' generalize in the obvious way to the in nite case. De nition 5.1 Let M be a -tree, and S be a possibly in nite set of redexes in M. (i) An !-development of S is a converging reduction, possibly in nite, in which only (descendants of) members of S are contracted. (ii) A complete !-development of S is an !development after which no descendant of a redex of S is left. A classical lemma in -calculus is the Finite Developments Lemma, stating that any development must terminate (see Barendregt [6]). Of course, we cannot have that for 1 -calculus, since we admit in nitely
@1
@1
S
λy
S
x
@2
S
S
y
y
λx
S
λy
S
S
y
y
@1
@1
@1
λy
S
λy
S
@2
x
@2
x
S
S
y
y
λy
S S y
Figure 3. many redexes to be developed. But there is an analogous statement, bearing in mind the analogy that we hinted at before between nite -reductions and converging 1 -reductions on the one hand, and in nite -reductions and diverging 1 -reductions on the other hand. This would be that any !-development converges. This is however not the case, see example soon. But if the result of a complete !-development exists, it is unique:
Proposition 5.2 Let S be a set of redexes in the tree M . Then any two complete !-developments of S converge to the same -tree. De nition 5.3 A set of redexes in a -tree M is consistent, if it can be !-developed.
Surprisingly, the union of two consistent sets of redexes need not be consistent. And this gives rise to a failure of in nitary con uence. Consider the in nite unwindings of the term h = x: (Sx); = y:(Sy)i, which, as was discussed in the previous section, was leading to two non-converging reductions. (See Figures 3 and 4). Let S1 and S2 be the two sets of redexes descending from the two redexes (Sy), (Sx) in that in nite term. The results of the two !-developments have no common reduct, not even in the in nitary sense, as is easily seen. (A proof is in the full paper.) Using 1 we can now formulate a soundness criterion for transformations of ?-expressions. If g = h j
λx
λx
1
λx
1
YI ?! ! (x:xx)(x:xx)
@1 λy
S
@2 λx
x
@2 λx
S
@1
λy
S
λy
S
@2
x
@2
x
λx
S
@2 λx
S
y @1
λx
S
S
S
x
x
@2 λx
S
y
y
@1
S S
@1 x
λy @2
S
λy
x
@2
xed point combinator f:(x:f(xx))(x:f(xx)) and I x:x. Then
S x
S
S
y
y
@2 S S x
Figure 4. :::i, let T(g) be the tree unwinding of g; here nodes have no names ; ; Now let g; g0 be two recursion systems. We will say that a transformation g ! g0 is sound (with respect to 1 ) if T(g) !! T(g0 ). Here !! denotes possibly in nitary reduction in 1 , that is a sequence of ! or less (possibly 0) -steps. Note that if g ?! s g0 , then T(g) = T(g0 ). A sharper soundness criterion is to insist that moreover a notion of -reduction in ? corresponds to a complete !-development of the residuals originating after unwinding from the redex in the -graph (recursion system). Our notions of -reduction on -graphs all satisfy this criterion; also the various transformations in the nal sections 9, 10 are sound.
Remark 5.4 A noteworthy dierence between or-
thogonal in nitary term rewriting as developed in [16] and 1 -calculus is that in the former the in nitary version of the well-known Parallel Moves Lemma (PML1 ) holds, whereas it fails in the latter. Here PML1 describes the situation of a one step reduction t0 ?! s0 versus an in nite reduction t0 ?! t1 ?! t2 ?! . If the latter is convergent (has a limit in the strong convergent sense), say to t1 , then a common reduct of s0 and t1 can be found in the usual canonical way, viz., by developing the (possibly in nite) set of residuals of the redex contracted in the step t0 ?! s0 . However, in 1 we have the following failure of PML1 . Consider YI where Y is Curry's
YI ?! I(I(I( I! :
Now I! and have no common reduct, also not in the in nitary sense. This example moreover shows that the in nitary non-con uence of 1 cannot be restored by putting all in nitary collapsing terms (trees), called `hyper-collapsing' in [16], equal to . (In orthogonal in nitary term rewriting this is sucient for obtaining in nitary con uence.) However, identifying the larger class of terms without head normal form does restore con uence as the next theorem states.
5.2 Head normal forms A term M has a head normal form it it reduces to some term of the form x1 xn :yN1 Nm , (n; m 0). All the non-con uence in 1 resides in the set of terms without hnf:
Theorem 5.5 In 1 , -reduction extended with the rule: \M ! if M has no hnf" is in nitary con uent.
The normal forms of this \1
-calculus" are the wellknown Bohm-trees.
6 Regular developments and another counterexample It may be thought that non-con uence in ?calculus only arises because of expressions that after unwinding to the corresponding in nite -tree have no head normal form. Or, equivalently, that con uence can be restored by equating all ?-expressions that have no head normal form (cf. the situation in 1 , described in Section 5). However, this is not the case: non-con uence in ? also may arise for expressions that have an in nitary normal form. The following is an example:
h = x:F( (Sx); Sx); = y:G((Sy); Sy)i: Unwinding these recursion equations yields the in nite -tree in Figure 5. Now a development of the redexes with function part yields a regular tree, as in the gure. Likewise a development of the redexes with function part yields a regular tree. But developing both sets (whose union is still a consistent set of redexes) yields a non-regular tree. Hence we have
acyclic redexes are rewritten, we have con uence. α
α ω
α λx F β
@
β
Sx
G α
@ λx
Sx G
Sy
SSx
Sy
con uent.
In the constrained calculi of the next sections however, we are able to reduce spine-cyclic redexes.
Sy
Sx
β
Sx
G @
Sx
Sx
F
F
@ λy
@ λy
Theorem 7.1 In ?, spine-acyclic -reduction is
λx F
G
F β
λx F
Sx
λy
α
β ω
@ λy
SSSx
SSy SSy G
G Sy
SSSSx
Sy
Sy
spine F
F β
@ λy
SSSSSx
SSy
@
λx
SSy G
G Sy
SSSSSSx cycle through spine
Figure 5. another counterexample to con uence of ?, after reducing the (Sx) and the (Sy) redexes, analogous to the counterexample in Section 4. (To complete the argument we use the soundness of ? with respect to 1 .) In the above, a -tree is regular, according to the usual de nition, if it contains modulo isomorphism only nitely many dierent sub-trees. A development is regular, if it is a development of a regular set of redexes in a regular -tree, and the result of the development exists and yields again a regular -tree.
7 Con uence for spine-acyclic lambda graph rewriting A way of avoiding the loss of con uence would be to forbid root-cyclic redexes, or at least reduction of them. But that is hardly satisfactory. Indeed, we can work with some root-cyclic redexes. The possibly harmful ones are those where not only the root is cyclic, but the root as well as `the' are on a cycle. See Figure 6. The two-node constellation of root-@ and head- will be called the spine of the redex. A redex is spine-cyclic when there is a cycle through its spine, and spine-acyclic otherwise (but the root-@ or the head- may be cyclic). Indeed, when only spine-
Figure 6.
8 The -calculus: a paradox An interesting calculus arises by extending `pure' -calculus with the -rule: : (x:Z(x))Z 0 ! Z(Z 0 ) : x:Z(x) ! Z(x:Z(x)) This -calculus was also introduced and analyzed in Rose [25]. Con uence follows at once since it is an orthogonal CRS (Combinatory Reduction System) [18, 20]. The -calculus obviously is already a form of cyclic -graph rewriting; it is straightforward how to assign a -graph to a -term. A -term however can only express `vertical sharing'; and not also `horizontal sharing' as present in ?. In fact, there are canonical translations between and ?-expressions, where in one direction the horizontal sharing is removed (the graph is expanded by unsharing). These translations preserve -steps: a step in gives rise to a -step in ?, a -step in ? gives rise to a sequence of -steps in (because of possible duplication by unsharing). Furthermore, a step in will correspond to a `copying' step (?!c ) in ?. Copying is the well-known transformation on graphs that is the inverse of a rooted graph homomorphism (see Ariola and Klop [3]), also referred to
as unsharing or unwinding. Substitution (?! s ) is a particular form of copying. In view of these connections between and ? it is somewhat paradoxical that the former is con uent while the latter is not. The explanation is that the copying operation allowed in ? is unrestricted and cannot be matched in , where the copying mechanism embodied in the -rule is more restricted. An attempt to translate the previous ?-counterexamples to con uence into is instructive.
9 Modular lambda graph rewriting The last observation gives rise to the following question: how can we extend -calculus, with its lack of horizontal sharing, to include this feature that is indispensable for ecient graph rewriting, while retaining con uence? Such a con uent extension is also proposed in the work of Rose [25]. As in Rose [25] inspiration comes from the family of -calculi (-calculi with explicit substitution), see [11, 1, 7, 22]. We now in a sequence of extensions develop a series of calculi, leading to a very general and exible calculus which incorporates -calculus, ordinary rstorder term rewriting, -graph rewriting with vertical and horizontal sharing. The distinctive feature of this family of calculi is the presence of nested recursion equations. The nesting allows to solve the variable confusion discussed in Section 1; for example, by writing h j = (x:h j = F(; Sx)i)Sxi it is clear that the second occurrence of variable x is free. In other words, dierently from ?-expressions, the body of a lambda is not dispersed through the system. Moreover, the root of a term is not restricted to be a variable, as in hF j = G0i. We start with the basic system 0 . The general form of 0 expressions is ht j E i, where t is a term and E is a set of equations. We can see E as the environment associated to t. For example, we can read the expression h: j = F0i, as being the term : , where is bound to F0, or equivalently in terms of the Let expression: Let
= F0
in : end
The above expression in the calculus [1] is : [(F(0)= ):id]. However, the 0 -calculus diers from the -calculi in that it allows `cyclic substitutions' also. For example, we could have h j = :F(; ); = :G(; )i, which corresponds to the
(:t)s ! ht j = si hC[] j = s; E i ! hC[s] j = s; E i tE;F ! tE if F ortho. to t and E ht j i ! t (:t)E ! :tE
Ext. subs. G.c. Empty box Distr.
Table 1. Reduction rules of 0 letrec expression: Letrec
= :F(; ) = :G(; ) in end
In short, the -calculi treat the Let-construct as rst class citizen, while the -calculi support the Letrec. Thus, the -calculi follow the tradition of providing `enriched -calculi' to capture more precisely the operational semantics of functional languages [2, 24, 23]. We will still assume the variable convention, that is, both free and bound variables have to be distinct. So for example, we will write h: j = F i instead of h: j = Fi.
De nition 9.1 Let be a rst-order signature. Then 0 (), the set of 0 -expressions over , is given by: (i) ; ; ; 2 0 (); (ii) t1 ; ; tn 2 0 () =) Fn (t1; ; tn) 2 0 (); (iii) t 2 0 () =) :t 2 0 (); (iv) t0 ; t1 2 0 () =) t0t1 2 0 (); (v) t0 ; t1; ; tn 2 0 () =) ht0 j 1 = t1 ; ; n = tn i 2 0(); with i 6= j ; 1 i < j n. Notation: in order to simplify the reading of the reduction rules we denote by tE the term ht j E i. The rewrite rules of 0 are given in Table 1. Here \F orthogonal to an expression E or to a term t" means that the bound recursion variables of F do not intersect with the free variables of E and t. The -rule, exactly presented like this in the (acyclic) -calculi, is remarkable in its role change of the bound variable, previously bound by , afterwards bound by the recursion construct h j i. For uniformity, we will now write also variables bound by as ; ; ; . External substitution is important as it allows us to `extract' a tree-like pre x of the expression at hand without duplicating the environment E. The notation C[ ] denotes a context with one hole.
Theorem 9.2 0 is con uent.
F(t1; ; tn)E
! F(tE1 ; ; tEn ) F F ht j E i ! ht j E F i Table 2. Further distribution rules of 1
hC[tE ] j F i ! hC[t] j E; F i ht j = C[sE ]; F i ! ht j = C[s]; E; F i if E is acyclic in the lhs box
Table 3. Flattening rules of 2 It is easy to see that pure -calculus can be simulated in 0 . Also, 0 contains horizontal sharing. Note that `internal substitution' as allowed in ? is now absent: h j = F( ); = G()i does not rewrite in 0 to h j = F( ); = G(F( ))i. However, -calculus can not yet be simulated in 0 . To that end we extend 0 with further distribution rules as in Table 2, and call the result 1 . Here E F means: if E is 1 = t1; ; n = tn then E F is 1 = tF1 ; ; n = tFn . (Actually, we can describe these rules in one single rule: C[t]E ! C[tE ]E .) Now it is an easy exercise to express a -step in 1 .
Theorem 9.3 1 is con uent.
10 Flattening rules Yet, from the point of view of eciency, 1 is not quite satisfactory. Consider the following reduction: h(3) + (4) j = h : j = M ii ?! ! h3 j = 0 0 0 M i + h4 j = M i. In other words, an unnecessary copy of M has been performed; no occurrence of the bound variable can occur in M, therefore, M can be safely shared between the two applications. In fact, this is the essence of lazy and fully lazy interpreters [12, 28, 27, 5]. This leads us to add the attening rules displayed in Table 3; the new system is called 2 . The proviso in Table 3 is necessary for con uence. The box sE may be on a cycle, but not on a cycle within the outermost box in the left-hand side. Furthermore, the box E that is removed in the right-hand side should be a \direct sub-box" of the outermost box in the lefthand side. Studying the overlaps between the rules thus far, we are led moreover to include the explicit operation of copying (?!c ). For a precise de nition see Ariola and Klop [3]. Copying consists of creating some new
names and using these to expand the box-construct in an arbitrary way, by simple use of Equational Logic. For example, given h j = F( ); = G()i we introduce two new names 0 and 0 obtaining h j = F( ); = G(); = 0; = 0 i, then we derive e.g., h j = F( 0 ); 0 = G(0); 0 = F( 0 )i. Our present concern is to obtain a con uence proof for 2 , consisting of the rules in Section 9, 10 plus explicit copying. Moreover, these -graph rewrite rules seem compatible (in the sense of retaining con uence) with rstorder orthogonal term rewriting. Remark 10.1 If we start from a -calculus expression such that each -abstraction does not have trivial maximal free expressions (mfe's) than the 2 -calculus is able to simulate the so-called Wadsworth's interpreter. The trick is played by the -rule and the above
attening rules: a redex (:M)A will be reduced to hM j = Ai, that is, A is put in the environment, as in [12] or, following the terminology of [5], A is \ agged" so that it will not be copied in case the redex is shared. This suggests that in order to avoid the extra complication of detecting mfe's at run time as in [28], an expression can be rst pre-processed by well-known techniques [13, 14]. Then doing sharing of arguments is enough to capture the amount of sharing oered by Wadsworth's interpreter.
Acknowledgements This work was done at the Dept. of Software Technology of CWI, and at the Dept. of Computer Science of the Free University. Z. Ariola thanks both CWI and the Free University to make her visit possible. Funding for this work has been provided by the ESPRIT Working Group 6345 Semagraph, CWI and the Free University. We thank Ronan Sleep, Richard Kennaway and Fer-Jan de Vries for stimulating discussions about a draft of this paper.
References [1] M. Abadi, L. Cardelli, P.-L. Curien, and J.-J. Levy. Explicit Substitutions. Journal of Functional Programming, 4(1):375{416, 1991. [2] Z. M. Ariola. An Algebraic Approach to the Compilation and Operational Semantics of Functional Languages with I-structures. PhD thesis, Harvard
University, 1992.
[3] Z. M. Ariola and J. W. Klop. Equational Term Graph Rewriting. Technical Report Draft, 1993. [4] Z. M. Ariola, J. W. Klop, J. R. Kennaway, F. J. de Vries, and M. R. Sleep. Syntactic De nitions of Unde ned: On De ning the Unde ned. In Proc. TACS 94, Sendai, Japan, 1994. [5] Arvind, V. Kathail, and K. Pingali. Sharing of Computation in Functional Language Implementations. In Proc. Internal Workshop on HighLevel Computer Architecture, 1984. [6] H. P. Barendregt. The Lambda Calculus: Its Syntax and Semantics. North-Holland, Amsterdam, 1984. [7] P.-L. Curien. Categorical Combinators, Sequential algorithms, and Functional Programming. Birkhauser Boston. Second Edition, 1993. [8] N. Dershowitz and J. P. Jouannaud. Rewrite systems. In J. van Leeuwen, editor, Handbook of Theoretical Computer Science, volume B, pages 243{320. Elsevier - The MIT Press, 1990. [9] J. Field. On Laziness and Optimality in Lambda Interpreters: Tools for Speci cation and Analysis. In Proc. ACM Conference on Principles of Programming Languages, San Francisco, 1990. [10] G. Gonthier, M. Abadi, and J.-J. Levy. The geometry of optimal lambda reduction. In Proc. ACM Conference on Principles of Programming Languages, 1992.
[11] T. Hardin and J.-J. Levy. A con uent calculus of substitutions. In France-Japan Arti cial Intelligence and Computer Science Symposium, 1989. [12] P. Henderson and J. Morris. A lazy evaluator. In Proc. ACM Conference on Principles of Programming Languages, 1976. [13] R. J. M. Hughes. Super-combinators. In Proc. of Lisp and Functional Programming, 1982. [14] T. Johnsson. Lambda Lifting: Transforming Programs to Recursive Equations. In Proc. of Functional Programming Languages and Computer Architecture Conference, Nancy, France, Springer-Verlag LNCS 201, 1985. [15] V. K. Kathail. Optimal Interpreters for Lambdacalculus Based Funtional Languages. PhD the-
sis, Dept. of Electrical Engineering and Computer Science, MIT, 1990.
[16] J. R. Kennaway, J. W. Klop, M. R. Sleep, and F. J. de Vries. An In nitary Church-Rosser property for Non-collapsing Orthogonal Term Rewriting Systems. In [26], pages 47{59. [17] J. R. Kennaway, J. W. Klop, M. R. Sleep, and F. J. de Vries. Trans nite Reductions in Orthogonal Term Rewriting Systems. In R. Book, editor, Proc. RTA '91, Springer-Verlag LNCS 488, pages 1{12, 1991. [18] J. W. Klop. Combinatory Reduction Systems. PhD thesis, Mathematical Center, Amsterdam, 1980. [19] J. W. Klop. Term Rewriting Systems. In Abramsky, Gabbay and Maibaum, editor, Handbook of Logic in Computer Science, volume 2, pages 1{ 116. Oxford University Press, 1992. [20] J. W. Klop, V. van Oostrom, and F. van Raamsdonk. Combinatory reduction systems: Introduction and survey. In Theoretical Computer Science, Vol. 121, Nrs. 1-2, pages 279{308, 1993. [21] J. Lamping. An algorithm for optimal lambda calculus reduction. In Proc. ACM Conference on Principles of Programming Languages, San Francisco, 1990.
[22] P. Lescanne. From to a Journey Through Calculi of Explicit Substitutions. In Proc. ACM
Conference on Principles of Programming Languages, Portland, 1993.
[23] A. Meyer, L. Justin, and B. So. Substitution model: formal de nitions. Handout for Computability, Programming and Logic. MIT Laboratory for Computer Science. 1993. [24] S. L. Peyton Jones. The implementation of Functional Programming Languages. Prentice-Hall International, Englewood Clis, N.J., 1987. [25] K. H. Rose. Explicit Cyclic Substitution. Technical Report D-166, DIKU, 1993. [26] M. R. Sleep, M. J. Plasmeijer, and M. C. D. J. van Eekelen, editors. Term Graph Rewriting: Theory and Practice. John Wiley & Sons, 1993. [27] D. A. Turner. A new implementation technique for applicative languages. Software Practice and Experience, 9:31{49, 1979. [28] C. Wadsworth. Semantics And Pragmatics Of The Lambda-Calculus. Ph.D. thesis, University of Oxford, September 1971.