Relating Graph and Term Rewriting via Bohm Models Zena M. Ariola Computer & Information Science Department, University of Oregon Eugene, OR 97403-1202
Abstract. Dealing properly with sharing is important for expressing some of the common compiler optimizations, such as common subexpressions elimination, lifting of free expressions and removal of invariants from a loop, as source-to-source transformations. Graph rewriting is a suitable vehicle to accommodate these concerns. In [4] we have presented a term model for graph rewriting systems (GRSs) without interfering rules, and shown the partial correctness of the aforementioned optimizations. In this paper we de ne a dierent model for GRSs, which allows us to prove total correctness of those optimizations. Dierently from [4] we will discard sharing from our observations and introduce more restrictions on the rules. We will introduce the notion of Bohm tree for GRSs, and show that in a system without interfering and non-left linear rules (orthogonal GRSs), Bohm tree equivalence de nes a congruence. Total correctness then follows in a straightforward way from showing that if a program M contains less sharing than a program N , then both M and N have the same Bohm tree. We will also show that orthogonal GRSs are a correct implementation of orthogonal TRSs. The basic idea of the proof is to show that the behavior of a graph can be deduced from its nite approximations, that is, graph rewriting is a continuous operation. Our approach diers from that of other researchers [6, 9], which is based on in nite rewriting.
1 Introduction Dealing properly with sharing is important in a framework to reason about some compiler optimizations, such as common subexpressions elimination, lifting of free expressions and removal of invariants from a loop. All these optimizations can be characterized as merely increasing the sharing of sub-computations in a program. If we want to express these optimizations as source-to-source transformations, we need a formalism which can distinguish between, for example, the following two programs: x = e; y = x + x y = e+e Graph rewriting is a computational model to accommodate these concerns. Moreover, due to its implicit parallelism it is also suitable as an intermediate language for compilation on parallel machines. We have successfully described the operational semantics and the compilation of Id [11] using two dierent graph rewriting systems, Kid and P-TAC [1, 2, 3].
Graph rewriting has usually been described in terms of labeled graphs and homomorphisms [5]. Instead, in [4] we have introduced a novel approach to graph rewriting systems (GRSs), where the graph is represented as a set of mutually recursive equations, i.e., a letrec expression, and reduction is explained in a way similar to term rewriting. In [4] we have also presented a term model for GRSs without interfering (i.e., overlapping) rules, where the terms in the model are graphs and showed that a less-sharing relation corresponds to the pre-congruence relation induced by the model. Using the less-sharing relation we are able to prove the partial correctness of the aforementioned optimizations. In this paper we are interested in de ning a dierent model for GRSs, which will allow us to show total correctness of those optimizations. We also use this model to compare GRSs and TRSs. As in the earlier paper, our emphasis is on what a graph computes from an operational point of view, i.e., its answer, and on a suitable notion of equality on the set of graph terms. As an example, consider the following terms M and N , respectively. M: Cons
N: Cons
F Cons
F Cons
0
0
F Nil
Nil
Notice that M < N , where < is the sharing relation, and as such, according to the model in [4], M v N , where v is the relation induced by the model. In other words, we would say that M will produce less observable information than N in any context. However, we would like to equate them because, if the internal representation of lists is ignored by an observer, they both represent the same unfolded list, F(0) : F(0) : Nil. We are also interested in equating terms without normal forms to terms with normal forms. Consider the following two terms M1 and N1 , M1 x = Cons(1; x) N1 F(1) (where F(y) ! Cons(y; F(y)) ) Suppose Cons does not have any rule associated with it, then, M1 is in normal form, while N1 does not have one. However, intuitively it can be said that both M1 and N1 represent the in nite list of one's. M1 has a nite representation of it, while N1 does not. (This representation issue is similar to the representation issue for real numbers, where the number with decimal expansion :999999 is the same as 1). We will de ne the answer of a graph in terms of its Bohm tree, and discuss the restrictions we have to impose on the rules to turn the Bohm tree equivalence into a congruence. Correctness of optimizations can then be formalized in terms of Bohm tree equivalence, that is, if a term M gets optimized into a term N then we say that the optimization is correct if M and N have the same Bohm tree. Moreover, we will show that the relation of less-sharing introduced in [4] guarantees the Bohm tree equivalence, and thus showing the total correctness of the optimizations that only aect sharing. The Bohm tree model also constitutes an adequate notion for comparing GRSs and TRSs. Barendregt et al. [5] base the relation between GRSs and TRSs
on the notion of normal form, which leads to some undesirable conclusions. We believe that the notion of normal form is inadequate for such a comparison. This point was already stressed by Wadsworth in his analysis of the relation between the syntactic and the semantic aspects of the -calculus [13, 14]. On the other hand, our approach is based on showing that the behavior of a graph can be deduced from its nite approximations. In other words, we show that graph rewriting is a continuous operation. This approach is dierent from that of other researchers [6, 9]. For example, Farmer and Watro [6] have shown the soundness of the cyclic Y-rule based on in nite rewriting. Similarly the approach taken by Klop et al. [9] is also based on rewriting in nite terms. In Section 2 we will brie y introduce the reader to graph rewriting systems (GRSs). We restrict our attention to GRSs which are adequate to describe sharing in combinatory systems. In Section 3 we introduce a function P rint which given a term M returns the nite stable information associated with M , and we collect the information gathered by reducing M in a set called P rint (M ), which represents the answer computed by M . For example, considering the terms M1 and N1 given earlier, P rint (N1 ) is the in nite list of 1's. What term does M1 compute? It is in normal form, but by performing its expansion, we can say that P rint (M1 ) is also the in nite list of 1's. Thus, by taking the answer as our criterion for equating terms, we can say that terms M1 and N1 are the same. We will show that P rint equality is a congruence for a subclass of GRSs, namely those without interfering, and non-left linear rules (orthogonal GRSs). Finally, using the notion of Bohm tree, in Section 4 we will show that orthogonal GRSs are a correct implementation of orthogonal term rewriting systems (TRSs). The notion of Bohm tree allows us to consider cyclic graphs, dierently from [5]. We conclude the paper with our thoughts on future work. This paper is self contained and only requires some understanding of TRSs. In particular, the model presented can be understood without reading [4].
2 Overview of a GRS We express graph rewriting through a novel system, called a GRS. The objects of a GRS are simply a set of mutually recursive equations, i.e., letrec expressions, instead of being complicated objects such as labeled graphs. For example, the following graph: G +
is expressed in a GRS system as ft1 = +(1; 2); t2 = G(t1; t1 ) In t2 g, where the name following the keyword In denotes the root of the graph. In other words, the terms of a GRS are such that each subexpression has a name. The syntax of GRS terms is given in Figure 1. The variable names on the left hand side of bindings in a block are required to be pairwise distinct. Furthermore, the order of bindings in a block does not matter. Substitution is allowed for constants and variables only, thus no duplication of work occurs during reduction. For 1
2
example, the expression +(1; 2) will be substituted for each occurrence of t1 only when +(1; 2) becomes a value, i.e., 3. The term obtained after all the substitutions are performed is called a canonical term. In that respect notice that if a term contains a binding of the kind \x = x" then we will substitute a constant, , for each occurrence of x; stands for a circular binding. As shown in [4] this guarantees the con uence of a certain subclass of GRSs. Furthermore, the internal block structure of a term is merely syntactic sugar, that is, all internal blocks are attened to compute the canonical form. Thus, analogously to the -calculus, we have a notion of -equivalence, which consists in equating, up to renaming, all terms having the same canonical form. GRS rules are SE Simple Expression E Expression k Fk Constant 0 SE ::= V ariable Constant E ::= SE Fk (SE1 ; ; SEk ) Block Block ::= [Binding; ] In SE Binding ::= V ariable = E Term ::= E 2
2
2 F
2 F
j
j
f
Fig. 1. Syntax of terms of a GRS with signature
j
g
F
not expressed in terms of multi-rooted graphs but as familiar TRS rules plus a precondition. The presence of a precondition is based on the observation that, dierently from term rewriting, the root of a term plays a special role during its reduction. For example, consider the rule F(G(x)) ?! 0 and the term F(G(1)). During graph reduction only the pointers to F (i.e., the root) are redirected to 0, that is, the subterm G(1) remains unaected. Thus, we call the subterm G(x) the precondition of the above rule, which will be written in GRS notation as follows: x1 = G(y) x = F(x ) ?! x = 0 . We will call x; x1 and y the variables and metavariables, 1
respectively, of the above rule. In general, a GRS rule is a set of preconditions, x1 = e1 ; ; xn = en , and a left-hand-side, l, and a right-hand-side, r, and is j xn = en , where e ; 1 i n, and l are terms of the written as: x1x==el1 j? ! i x=r
form Fk (y1 ; ; yk ), where each yi is either a variable or a constant and k > 0; r is a term such that its free variables appear either in the left hand side or in the precondition. The term fx1 = e1 ; xn = en In xg is referred to as the pattern of the rule. A redex is then de ned in terms of a substitution function from all the variables and metavariables of a rule to the set of variables and constants of a term. Take, for example, the following rule and term M , respectively: ) : x = G(x x;1x=) F(?y! x=y 1 1
M : f t1 = F(0); t2 = G(t1 ; t1) In t2 g
the subterm G(t1; t1) is a redex because G(t1; t1) G(x1; x1) , where is: x = t2; x1 = t1 ; y = 0. Reduction consists in replacing the redex by an instantiation M [t2 r ] of the right hand side of rule ; that is, we will say that M ?t! 2 M [t2 0] ft1 = F(0) In 0g. We will also make use of the notation M [F e], where F is a set of redexes, indicating that all redexes in F are replaced by the expression e. GRSs are formally introduced in [4], where GRSs without interfering rules are proved to be con uent. The notion of interference extends the familiar overlapping notion of TRSs; we will say that rule 1 interferes with rules 2 if there exists an instance of a non-variable subterm of the pattern of rule 1 which is the same, up to -equivalence, to an instance of rule 2 . A rule is said to be non-left linear if the pattern of the rule contains two distinct paths to a node; notice that left linearity implies that the left hand side of rules has to be acyclic. GRSs without interfering and non-left linear rules are called orthogonal GRSs. Hereafter, we will only consider orthogonal GRSs.
3 Bohm Model for Acyclic Orthogonal GRSs 3.1 Printable Value of a Term Given a term we de ne its printable value as the stable part of the term, that is, the part which will never change during its reduction. The printable value corresponds to the notions of instant semantics [15] and direct approximation [10, 12] introduced for the -calculus. As for the -calculus, all redexes must rst be substituted by a new constant , which stands for no information. However, this is not enough to guarantee the monotonicity of the information content with respect to reduction. The problem is due to the upward creation of redexes; that is, even though a term M is not a redex, it can become so when some redexes under it are performed. To cope with this phenomenon in the -calculus, both Wadsworth [12] and Levy [10] have introduced the notion of !-rule, which states P ?! . Huet and Levy have applied the same concept to orthogonal TRSs [8]. Analogously, for GRSs we introduce a set of !-rules. For example, given the GRS rules: x = F(0) ?! x = 1 and x = G(y) ?! x = 0, we will generate the !-rule: x = F( ) ?! x = , thus, reducing to the term ft1 = F(t2 ); t2 = G(y) In t1g. Notice that the above term will also be reduced to
if we replace the second rule by x = G(y) ?! x = 1, that is, the !-rules are generated without analyzing the right hand side of rules. In [4] the stable part of a term was computed by applying the !-reductions on the term obtained by replacing all its redexes by , that is, sharing was part of our observations. Instead, in this paper we go one step further by considering the expansions or nite information. For example, consider the rule: x1 = A(y) x = F(x ) ?! x = 0 , and the term M ft = F(t) In tg. Since M does not 1
contain a redex, following [4], we will consider M as stable information. However, since we want to guarantee the continuity of graph rewriting with respect to graph expansions, we are forced to infer that the printable value of M is , because none of the expansions, i.e., f ; F( ); F(F( )); g, constitute stable information. For example, F( ) is not stable because it becomes a redex by replacing by the term A(x). As another example, the printable value of the term M fx = +(y; y); y = +(x; x) In xg will also be . In other words, in both the previous two examples the terms contain only in nite information but they do not contain any nite information. To obtain the printable value of a term, we rst replace all its redexes by
and then perform !-reductions on each expansion of the term so obtained. The expansions are obtained by applying Kleene recursion. In Kleene recursion the kth expansion, M k , of a term M fx1 = e1 ; ; xn = en In xg is de ned as follows: M 0 is the expression bound to x0 in fx01 = ; ; x0n = In x0g .. . M k+1 is the expression bound to xk+1 in fxk1 +1 = e1 [xk1 =x1; ; xkn=xn]; ; xnk+1 = en [xk1 =x1; ; xkn=xn] In xk+1g where xki stands for the kth approximation of the value associated with the variable xi, (1 i n). The printable value of M is then the chain of stable trees corresponding to variable x; all the other trees, i.e., xkj (xj 6= x), are needed only to compute xk . Notice that the expansions are TRS terms de ned over a signature extended with the constant . Let us call T the set of such terms, and let us de ne the following pre x ordering: De nition1 Pre x Order on T . (T ,t) is a partial order, where t is de ned as follows: (i) t M , 8 M 2 T ; (ii) x t x, 8 x 2 V ariable; (iii) c t c, 8 c 2 Constant; (iv) F n(M1 ; ; Mn) t F n(N1 ; ; Nn ), if Mi t Ni 1 i n. Remark 2 Monotonicity of Expansions. Given a GRS term M and its two expansions M k and M k0 , if k k0 then M k t M k0 . In order to generate the !-rules let us rst introduce the following function Fl, which given an acyclic term M produces its attened version. Fl[ x] = x Fl[ Constant] = Constant Fl[ Fn (y1 ; ; yn )]] = Fn (y1 ; ; yn ) Fl[ fx1 = e1 ; xn = en In xi g] = ei [Fl(fx1 = e1 ; xn = en In x01g)=x01; ; Fl(fx1 = e1 ; xn = en In x0m g)=x0m ] where x01 x0m are free variables of ei Fl[ fx1 = e1 ; xn = en In xg] = x where x is either a constant or x 62 fx1; ; xng
j xn = en , a rule 0 : De nition3 !-rule. Given a GRS rule : x =x =e lj? ! x=r 0l ?! is said to be a !-rule i l0