Implementing Term Rewriting by Jungle Evaluation€¦ · term rewriting. The computation of term...

23
Implementing Term Rewriting by Jungle Evaluation Berthold Hoffmann and Detlef Plump Fachbereich Mathematik und Informatik Universit¨ at Bremen Postfach 330 440, D-2800 Bremen 33 Usenet: {hof,det}%Informatik.Uni-Bremen.DE Abstract Jungles are acyclic hypergraphs which represent sets of terms such that common sub- terms can be shared. Term rewrite rules are translated into jungle evaluation rules which implement parallel term rewriting steps. By using additional hypergraph rules which “fold” equal subterms, even non-left-linear term rewriting systems can be implemented. As a side effect, these folding rules can speed up the evaluation process considerably. It is shown that terminating term rewriting systems result in terminating jungle evaluation systems which are capable to normalize every term. Moreover, confluent and terminating term rewriting systems give rise to confluent and terminating jungle evaluation systems, provided that the “garbage” produced by the evaluation steps is ignored. 1 Introduction Term rewriting is an interesting way of “computing by replacement” which is used in various areas of computing science: for the interpretation of functional and logical programming languages, for theorem proving, and for executing algebraic specifications of abstract data types and checking properties of specifications like consistency and completeness (see [Klo90] and [DJ90] for surveys on term rewriting). Classically, terms are represented by trees and rewriting is realized by subtree replace- ment. Unfortunately, this way of rewriting may be very expensive, both in time and space: the application of a rule may require large subterms to be copied, and each copy of a term has to be rewritten anew. In this paper we investigate an improved model for implementing term rewriting where this source of inefficiency is avoided: * This work is partly supported by the Commission of the European Communities under Contract 390 (PROSPECTRA Project) in the ESPRIT Programme. 1

Transcript of Implementing Term Rewriting by Jungle Evaluation€¦ · term rewriting. The computation of term...

Page 1: Implementing Term Rewriting by Jungle Evaluation€¦ · term rewriting. The computation of term normal forms by jungle evaluation is the subject of section 6. In section 7, it is

Implementing Term Rewriting

by Jungle Evaluation∗

Berthold Hoffmann and Detlef Plump

Fachbereich Mathematik und Informatik

Universitat Bremen

Postfach 330 440, D-2800 Bremen 33

Usenet: {hof,det}%Informatik.Uni-Bremen.DE

Abstract

Jungles are acyclic hypergraphs which represent sets of terms such that common sub-terms can be shared. Term rewrite rules are translated into jungle evaluation rules whichimplement parallel term rewriting steps. By using additional hypergraph rules which“fold” equal subterms, even non-left-linear term rewriting systems can be implemented.As a side effect, these folding rules can speed up the evaluation process considerably. Itis shown that terminating term rewriting systems result in terminating jungle evaluationsystems which are capable to normalize every term. Moreover, confluent and terminatingterm rewriting systems give rise to confluent and terminating jungle evaluation systems,provided that the “garbage” produced by the evaluation steps is ignored.

1 Introduction

Term rewriting is an interesting way of “computing by replacement” which is usedin various areas of computing science: for the interpretation of functional and logicalprogramming languages, for theorem proving, and for executing algebraic specificationsof abstract data types and checking properties of specifications like consistency andcompleteness (see [Klo90] and [DJ90] for surveys on term rewriting).

Classically, terms are represented by trees and rewriting is realized by subtree replace-ment. Unfortunately, this way of rewriting may be very expensive, both in time andspace: the application of a rule may require large subterms to be copied, and each copyof a term has to be rewritten anew.

In this paper we investigate an improved model for implementing term rewriting wherethis source of inefficiency is avoided:

∗This work is partly supported by the Commission of the European Communities under Contract390 (PROSPECTRA Project) in the ESPRIT Programme.

1

Page 2: Implementing Term Rewriting by Jungle Evaluation€¦ · term rewriting. The computation of term normal forms by jungle evaluation is the subject of section 6. In section 7, it is

• Rather than by trees, terms are represented by acyclic hypergraphs so that mul-tiple occurrences of terms can be shared. These hypergraphs are called jungles,a name coined in [Plu86] and [HKP88].

• Rewriting is performed by hypergraph replacement, specified by evaluation rulesaccording to the algebraic theory of graph grammars (see, e.g., [Ehr79]). Insteadof copying subterms, evaluation rules create a sharing of subterms.

• Additional hypergraph rules which “fold” equal subterms can be used to achievea complete sharing of equal subterms; so multiple evaluation of the same termcan be avoided.

Example 1.1 (Fibonacci Numbers) Consider the term rewrite rules

fib(0)→ 0

fib(succ(0))→ succ(0)fib(succ(succ(n)))→ fib(succ(n)) + fib(n)

specifying a function fib that computes Fibonacci Numbers, based on natural numberswith the constant 0, successor function succ, and addition +.

The first two steps for computing the Fibonacci Number of 4 by term rewriting are

fib(succ4(0))

→ fib(succ3(0)) + fib(succ2(0))

→ fib(succ2(0)) + fib(succ(0)) + fib(succ2(0))

In both steps, the arguments of fib are copied. In particular, the resulting term containstwo copies of fib(succ2(0)); each of them must be rewritten anew. As a consequence,rewriting a term fib(succn(0)) to normal form requires time and space exponentialin n (we just consider the three rules shown above; rules defining the addition areneglected).

Figure 1 shows two corresponding jungle evaluation steps, where the arguments areshared instead of being copied. A twofold folding step identifies the nodes representingfib(succ2(0)), so this subterm has to be evaluated only once (dashed boxes indicate“garbage”).

By performing evaluation and folding steps in this order, the evaluation of a termfib(succn(0)) requires only a number of steps and space linear in n. ✷

The first graph grammar approach to the evaluation of functional expressions has beenundertaken by Ehrig, Rosen, and Padawitz ([ER76], [Pad82]). However, they use anextension of the algebraic theory of graph grammars having the drawback that mostof the results for “standard graph grammars” are not applicable. The graph reductionapproaches of Staples [Sta80], Raoult [Rao84] and Barendregt et. al. [BvEG*87]use formalisms of graph rewriting for which no such comprehensive theory as for thealgebraic approach is available.

In [Hof83] and [Plu86], the authors started an approach to model term rewriting bystandard graph grammars. H-J. Kreowski, A. Habel, and D. Plump continued this

2

Page 3: Implementing Term Rewriting by Jungle Evaluation€¦ · term rewriting. The computation of term normal forms by jungle evaluation is the subject of section 6. In section 7, it is

tfib

❄tsucc

❄tsucc

❄tsucc

❄tsucc

❄t0

t+

❞ tsucc fib

❄ ❄❞ t tsucc succ fib

✖✲ ✕✛❄tsucc

❄tsucc

❄t0

t+

❞ tsucc + ☞

✌✛

❄ ❄❞ ❞ t tsucc succ fib fib

❄✖✲ ❄✆✞t t t

succ succ fib

✖✲ ❄ ✕✛tsucc

❄t0

2⇒

t+

✆✛

❞ tsucc + ✥

❄ ❄❞ ❞ tsucc succ fib

✖ ✖✲ ❄t tsucc fib

❄ ✕✛tsucc

❄t0

Figure 1: Jungle evaluation steps followed by a twofold folding step

work in [HKP88], where hypergraphs are used instead of graphs, in order to make thetechnical treatment easier.

With this paper we carry forth this research by using a slightly extended class of hyper-graph grammars which allows for a translation of term rewrite rules into hypergraphrules without need for “indirect pointers”. We show how jungle evaluation can beused to compute term normal forms, and investigate conditions for termination andconfluence of jungle evaluation.

The paper is organized as follows:We recall basic notions of term rewriting in section 2. In section 3, we define jungles andstudy their relationship to terms. We discuss properties of morphisms between jungles,and we consider special jungles in order to give sufficient criteria for the existence ofsuch morphisms. Rules for folding multiple representations of terms are introducedin section 4. They allow to compute minimal jungle representations for sets of terms.In section 5 we construct jungle evaluation rules, and show that these rules performterm rewriting. The computation of term normal forms by jungle evaluation is thesubject of section 6. In section 7, it is demonstrated that termination of term rewritingcarries over to jungle evaluation. Section 8 is devoted to the investigation of confluencecriteria for jungle evaluation. Finally, in section 9 we summarize our results, compareour approach with that of Barendregt et al., and point out possible directions of futureresearch.

Acknowledgement. We thank the anonymous referee for helpful comments.

2 Preliminaries

Strings. A∗ denotes the set of all strings over some set A, including the empty string

3

Page 4: Implementing Term Rewriting by Jungle Evaluation€¦ · term rewriting. The computation of term normal forms by jungle evaluation is the subject of section 6. In section 7, it is

λ. f ∗ : A∗→B∗ denotes the homomorphic extension of a function f : A→B.

Abstract Reductions. Let → be a binary relation on some set A.

We write+→ and

∗→ for the transitive and transitive-reflexive closure of→ , respectively.

The n-fold composition of → (for n ≥ 0) is denoted byn→; in particular,

0→ is the

equality on A.

Some a ∈ A is a normal form (with respect to →) if there is no b ∈ A with a→ b. Anormal form a is called a normal form of b ∈ A if b

∗→ a.

We say→ is terminating (noetherian in [Hue80]) if there is no infinite chain a0→ a1→ a2→ . . .

The relation → is confluent if for all a, b1, b2 ∈ A, b1∗← a

∗→ b2 implies b1

∗→ c

∗← b2 for

some c ∈ A.

Signatures and Terms. Let S be a set and (OPw,s)w∈S∗, s∈S be a family of pairwisedisjoint sets. Then SIG = (S,OP) is a signature where OP =

w∈S∗, s∈SOPw,s. The

elements of S and OP are called sorts and operation symbols, respectively. An operationsymbol op ∈ OPw,s is also written op : w → s.

Let (Xs)s∈S be a family of pairwise disjoint sets. Then X =⋃

s∈S

Xs is a variable set

provided that X ∩OP = ∅.

Let s ∈ S. A string t ∈ (OP ∪X)∗ is a term of sort s (over X) if

(i) t ∈ Xs or

(ii) t = op t1 . . . tk where op ∈ OP s1...sk,s and ti is a term of sort si for i = 1, . . . , k, k ≥0.

TSIG(X) denotes the set of all terms over X. We write sort(t) for the sort of a term t.

Substitutions. Let X, Y be variable sets. A function σ : TSIG(X) → TSIG(Y ) iscalled a substitution if

sort(σ(x)) = sort(x) for all variables x ∈ X,σ(op t1 . . . tk) = op σ(t1) . . . σ(tk) for all other terms op t1 . . . tk.

Term Rewriting. A rewrite rule is a pair l→ r of terms of equal sort such that l isnot a variable and all variables in r occur also in l. A set R of rewrite rules is called aterm rewriting system.

Given a rewriting system R, we say that t rewrites to u, written t→R

u, if there is a

rewrite rule l→ r and a substitution σ such that t has a subterm t′ = σ(l), and u isobtained from t by replacing t′ by σ(r).

General AssumptionFor the rest of this paper, we fix a signature SIG and an infinite variable set X.

4

Page 5: Implementing Term Rewriting by Jungle Evaluation€¦ · term rewriting. The computation of term normal forms by jungle evaluation is the subject of section 6. In section 7, it is

3 Jungles and Jungle Morphisms

Representing terms such that common subterms can be shared requires graph-likestructures. We use hypergraphs, an extension of graphs where hyperedges point fromstrings of source nodes to strings of target nodes.

Definition 3.1 (Hypergraph) A hypergraph G = (VG, EG, sG, tG, lG, mG) over SIGconsists of a finite set VG of nodes, a finite set EG of hyperedges (or edges for short),two mappings sG : EG → V ∗

G and tG : EG → V ∗G, assigning a string of source nodes

and a string of target nodes to each hyperedge, and two mappings lG : VG → S andmG : EG → OP, labeling nodes with sorts, and hyperedges with operation symbols.

For a hypergraph G, indegreeG(v) and outdegreeG(v) denote the number of occurrencesof a node v in the target strings resp. source strings of all edges in G.

Let v1, v2 be two nodes in a hypergraph G. Then v1 >G v2 denotes that there is anon-empty path from v1 to v2 in G; v1 ≥G v2 means v1 >G v2 or v1 = v2. G is acyclicif there is no node v ∈ VG such that v >G v. ✷

Not all hypergraphs over SIG are suited to represent terms: The sources and targetsof hyperedges must conform to the typing of SIG, each node should have at most oneoutgoing hyperedge (in order to represent a unique term), and cycles must not occur(in order to stay with finite terms).

Definition 3.2 (Jungle) A hypergraph G is a jungle (over SIG) if

1. for each e ∈ EG, mG(e) = op : s1 . . . sk → s implies l∗G(sG(e)) = s and l∗G(tG(e)) =s1 . . . sk,

2. outdegreeG(v) ≤ 1 for each v ∈ VG,

3. G is acyclic. ✷

RemarksGeneration. In addition to the graph-theoretic definition above, the set of all junglescan be characterized by a set of generating jungle rules: each jungle can be constructedfrom the empty jungle by application of these rules. This characterization is used in[HKP88] to establish a structural induction principle for jungles.

Induction on nodes. Since jungles are acyclic, two induction techniques are availablefor showing that all nodes of a given jungle have a property P : one shows that allnodes without ingoing edges fulfill P and that all target nodes of an edge satisfy P ifthe source node satisfies P . Alternatively, it is sufficient to show that all nodes withoutoutgoing edges have property P and that the source node of an edge satisfies P if allthe target nodes satisfy P . ✷

Nodes without outgoing hyperedges are places where a jungle is “incomplete”. Thusthese nodes are considered as variables.

5

Page 6: Implementing Term Rewriting by Jungle Evaluation€¦ · term rewriting. The computation of term normal forms by jungle evaluation is the subject of section 6. In section 7, it is

Definition 3.3 (Variables in a Jungle) Let G be a jungle. Then VARG = {v ∈VG | outdegreeG(v) = 0} is the set of all variables in G.

In the following, we assume VARG ⊆ X for every jungle G. ✷

The term represented by some node in a jungle is obtained by descending along thehyperedges and collecting the hyperedge labels. This process terminates because jun-gles are acyclic, and yields a unique term because all nodes have at most one outgoinghyperedge.

Definition 3.4 (Term Representation Function) Let G be a jungle. Then

termG(v) =

v if v ∈ VARG,

mG(e)term∗G(tG(e)) otherwise, where e is the unique

edge with sG(e) = v

defines a function termG : VG → TSIG(VARG) with sort(termG(v)) = lG(v) for allv ∈ VG.

The set termG(VG) of all terms represented by a jungle G is denoted by TERMG. ✷

Example 3.5 Let SIG contain a sort nat and operation symbols

0 :→ nat succ, fib : nat→ nat + : nat nat→ nat

In figure 2 below we show three jungles representing the term fib(n)+fib(n). (Inexamples we write terms with additional parentheses and use infix notation, for betterreadability.)

G1 =

t t+ +

❄ ❄❍❍❍❥

✟✟✟✙t tfib fib

✖ ✕✲ ✛tn

G2 =

t+

✆ ✝✛ ✲t tfib fib

✖ ✕✲ ✛tn

G3 =

t+

✝ ✆✲ ✛tfib

❄tn

Figure 2: Jungles representing fib(n)+fib(n)

Nodes are drawn as circles (in most cases, we omit their labels; by default all nodes areassumed to be labelled by “nat”); hyperedges are drawn as boxes which are connectedby lines with their unique source nodes, and by arrows with their target nodes (if thereare any). The arrows are arranged from left to right in the order given by the targetmapping. ✷

It is easy to see that jungles are suited to represent arbitrary finite sets of terms.

Fact 3.6 For each finite set T of terms there is a jungle G with T ⊆ TERMG. ✷

6

Page 7: Implementing Term Rewriting by Jungle Evaluation€¦ · term rewriting. The computation of term normal forms by jungle evaluation is the subject of section 6. In section 7, it is

Definition 3.7 (Fully Collapsed Jungle) A jungle G is called fully collapsed iftermG is injective (i.e. if each term in TERMG is represented by a unique node).

For example, the jungle G3 in figure 2 is fully collapsed.

Morphisms between jungles are essential for describing jungle manipulating rules andderivations; their role corresponds to that of substitutions for term rewriting.

Definition 3.8 (Jungle Morphism) Let G, H be jungles. A jungle morphism f :G→H is a pair of mappings f = (fV : VG→VH , fE : EG→EH) which preserve sources,targets, and labels, i.e.,sH ◦ fE = fV ◦ sG, tH ◦ fE = f ∗

V ◦ tG, lH ◦ fV = lG, and mH ◦ fE = mG.

f is called injective (surjective), if both fV and fE are injective (surjective). If f

is injective and surjective, it is called an isomorphism and G and H are said to beisomorphic, written G ∼= H. ✷

Due to the jungle structure, jungle morphisms are uniquely determined by their valuesfor roots.

NotationROOTG = {v ∈ VG | indegreeG(v) = 0} denotes the set of roots of a jungle G. ✷

Fact 3.9 (Uniqueness of Jungle Morphisms) Let f, g : G → H be jungle mor-phisms. Then f = g if and only if fV (r) = gV (r) for all r ∈ ROOTG. ✷

Each jungle morphism f : G→H induces a substitution compatible with f .

Definition 3.10 (Induced Substitution) Let f : G → H be a jungle morphism.Then the induced substitutionσ : TSIG(VARG)→ TSIG(VARH) is defined for all x ∈ VARG by

σ(x) = termH(fV (x)). ✷

Fact 3.11 For each jungle morphism f : G→ H the induced substitution σ satisfies

σ ◦ termG = termH ◦ fV . ✷

RemarkAccording to the above fact, the existence of a jungle morphism G → H implies thatσ(TERMG) ⊆ TERMH for some substitution σ. The reverse does not hold as figure 2shows: although TERMG1

= TERMG2= TERMG3

, there is no jungle morphism fromG3 to G1 or to G2. ✷

Let G and H be jungles where all roots of G represent terms that occur in H up tosome substitution. Then a sufficient condition for the existence of a jungle morphismG→H is that H is fully collapsed.

Theorem 3.12 (Existence of Jungle Morphisms) Let G, H be jungles and fROOT :ROOTG→VH be a mapping such that

7

Page 8: Implementing Term Rewriting by Jungle Evaluation€¦ · term rewriting. The computation of term normal forms by jungle evaluation is the subject of section 6. In section 7, it is

termH(fROOT (r)) = σ(termG(r)) for all r ∈ ROOTG

where σ : TSIG(VARG)→TSIG(VARH) is some substitution.

If H is fully collapsed, then fROOT uniquely extends to a jungle morphism f : G→H

such that σ is the substitution induced by f .

ProofIt is sufficient to show that fROOT can be extended to a jungle morphism with inducedsubstitution σ. Uniqueness follows from fact 3.9.

For each v ∈ VG there is some r ∈ ROOTG such that termG(v) is a subterm oftermG(r). Consequently, σ(termG(v)) is a subterm of termH(fROOT (r)) and, by subterm-closedness of TERMH , is represented by some v′ ∈ VH . v′ is uniquely determined sinceH is fully collapsed. Hence the assignment v 7→ v′ defines a mapping fV : VG→VH

which satisfiestermH(fV (v)) = σ(termG(v)) for all v ∈ VG.(1)

We also havelH ◦ fV = lG(2)

as each node is labeled with the sort of its associated term.

Now consider some e ∈ EG. Since termG(sG(e)) is not a variable, termH(fV (sG(e)))is not a variable, too. So there is (uniquely) e′ ∈ EH with sH(e′) = fV (sG(e)). I.e., theassignment e 7→ e′ defines a mapping fE : VG→ VH with

sH ◦ fE = fV ◦ sG.(3)

To complete the proof that (fV , fE) is a jungle morphism we must show

mH ◦ fE = mG(4)

andtH ◦ fE = f ∗

V ◦ tG.(5)

For each e ∈ EG we have

mH(fE(e))term∗H(tH(fE(e)))

= termH(sH(fE(e))) definition 3.4= termH(fV (sG(e))) (3)= σ(termG(sG(e))) (1)= σ(mG(e)term∗

G(tG(e))) definition 3.4= mG(e)σ∗(term∗

G(tG(e)))= mG(e)term∗

H(f ∗V (tG(e))) (1)

which implies (4) and, by injectivity of termH , (5). ✷

As a corollary we obtain that fully collapsed jungles which represent the same termsare isomorphic.

8

Page 9: Implementing Term Rewriting by Jungle Evaluation€¦ · term rewriting. The computation of term normal forms by jungle evaluation is the subject of section 6. In section 7, it is

Corollary 3.13 (Uniqueness of Fully Collapsed Jungles) Let G, H be fully col-lapsed jungles. If TERMG = TERMH , then G and H are isomorphic.

ProofLet gROOT : ROOTG→VH and hROOT : ROOTH→ VG map each root to a node repre-senting the same term. By theorem 3.12, these mappings extend to jungle morphismsg : G→H and h : H→G such that termG = termH ◦ gV and termH = termG ◦ hV .Hence termG = termG ◦ hV ◦ gV which by injectivity of termG implies hV ◦ gV = idVG

(where idVGis the identity on VG). Analogously we get gV ◦ hV = idVH

, so gV and hV

are bijections. This implies injectivity of gE and hE while surjectivity follows from thefact that gV and hV preserve represented terms. Thus g and h are isomorphisms. ✷

4 Folding

Given a jungle, we want to generate the fully collapsed jungle representing the sameterms by application of folding rules. To explain the idea behind these rules, considera jungle G and two hyperedges e1, e2 in G which have the same labels and target nodesbut different sources. Then sG(e1) and sG(e2) represent the same term. Deleting one ofthe edges and identifying both source nodes yields a jungle which represents the sameterms as G.

Definition 4.1 (Folding Rule) Let op : s1 . . . sk→ s ∈ OP be an operation symbolwith k ≥ 0.

The folding rule for op is given by a pair (L ← Kb→ R) of jungle morphisms as depicted

below (“x = y” indicates that b identifies the roots of K; note that L and R have novariables if op is a constant).

x y♠ ♠s s

op op

❄ ❄✖✌☞✎ · · ·· · ·♠ ♠s1 · · · sk

x y♠ ♠s s

op

❄✖☞· · ·

❄♠ ♠s1 · · · sk

x = y♠sop

❄✖☞· · ·

❄♠ ♠s1 · · · sk

F denotes the set of folding rules for OP. ✷

Definition 4.2 (Folding Step) Let G be a jungle. A folding step G⇒F

H from G to

a hypergraph H is constructed as follows:

1. Find a morphism g : L→G for some folding rule (L ← Kb→ R) such that gE is

injective.

2. Obtain D from G by removing gE(e), where e is the unique edge in L−K.

3. Obtain H from D by identifying gV (x) and gV (y), where x and y are the roots inL.

9

Page 10: Implementing Term Rewriting by Jungle Evaluation€¦ · term rewriting. The computation of term normal forms by jungle evaluation is the subject of section 6. In section 7, it is

We do not want to distinguish between H and hypergraphs that differ from H onlyin their names for edges or non-variable nodes. That is, we stipulate that G⇒

FH ′

whenever there is an isomorphism f : H→H ′ with fV (x) = x for all x ∈ VARH . ✷

RemarkThe jungles involved in the construction of a folding step are related by morphisms asfollows:

L ← Kb→ R

g ↓ ↓ ↓G ← D

c→ H

This diagram actually represents two pushouts in the category of hypergraphs andhypergraph morphisms (see [Ehr79] for a categorical approach to graph grammars). ✷

Definition 4.3 (Track Function) For each folding step G⇒F

H there is a track func-

tion tr : VG→VH which coincides with cV in the above diagram (note that VD = VG).✷

Example 4.4 Below we show the folding rules for the operation symbol fib and theconstant 0.

x yt tfib fib

✖ ✕✲ ✛t←

x yt tfib

✖✲ t→

x = ytfib

✖✲ t

x yt t0 0 ←

x yt t0 →

x = yt0

In example 3.5, the folding rule for fib transforms G2 into G3. ✷

Lemma 4.5 (Folding Steps Preserve Jungles) Given a jungle G and a foldingstep G⇒

FH, H is a jungle, too.

ProofLet G⇒

FH be given with a diagram as shown in the remark below definition 4.1. H is

obtained from D by identifying a node v with a variable x where v and x are the imagesof the two roots in K. Clearly H satisfies the outdegree and the labeling condition ofdefinition 3.2. To show that H is acylic suppose that there is a cycle in H. Since D

is acyclic this cycle is the result of identifying v and x, i.e., there must be a path inD from v to x. So in particular v >G x because D is a subjungle of G. But by thestructure of L, v >G v′ implies x >G v′ for all v′ ∈ VG. Thus x >G x which contradictsthe acyclicity of G. ✷

Lemma 4.6 (Folding Steps Preserve Terms) Let G⇒F

H be a folding step. Then

termG = termH ◦ tr and TERMG = TERMH .

ProofIt is easy to see that the construction of folding rules ensures that termG(v) = termH(tr(v))for all nodes v in G. Then TERMG = TERMH follows from the observation that tr issurjective. ✷

10

Page 11: Implementing Term Rewriting by Jungle Evaluation€¦ · term rewriting. The computation of term normal forms by jungle evaluation is the subject of section 6. In section 7, it is

Theorem 4.7 (Characterization of Fully Collapsed Jungles) A jungle G is fullycollapsed if and only if there is no folding step G⇒

FH.

ProofLet G be fully collapsed and L be the left-hand side of some folding rule. Then eachjungle morphism L→G identifies the two edges in L. This violates the condition thatgE must be injective, so there is no folding step G⇒

FH.

Conversely, assume that G is not fully collapsed. Then there are v1, v2 ∈ VG withv1 6= v2 but termG(v1) = termG(v2). (Note that v1 and v2 cannot be variables.) Inparticular, we can choose v1 and v2 such that they are minimal with respect >G, i.e., foreach pair (v′

1, v′2) with vi >G v′

i (i = 1, 2) we have that termG(v′1) = termG(v′

2) impliesv′1 = v′

2. Then there are edges e1,e2 with sG(ei) = vi (i = 1, 2) and tG(e1) = tG(e2).e1 and e2 are labeled with the same operation symbol op. Hence the folding rule for op

can be applied. ✷

Theorem 4.8 ⇒F

is terminating and confluent.

ProofSince each folding step decreases the number of nodes by one, folding is terminating.Thus, given derivations H1

∗⇐F

G∗⇒F

H2, we have derivations Hi∗⇒F

H ′i where H ′

i is a

normal form (i = 1, 2). With theorem 4.7 and lemma 4.6 it follows that H ′1 and H ′

2 arefully collapsed and represent the same terms. Hence H ′

1∼= H ′

2 by corollary 3.13. ✷

5 Jungle Evaluation

In this section we study how term rewriting can be implemented by hypergraph re-placement. We define rules which manipulate jungles such that the represented termsare rewritten according to a given term rewriting system.

General AssumptionLet R be an arbitrary but fixed term rewriting system over SIG .

Definition 5.1 (Evaluation Rule) The evaluation rule for a rewrite rule l→ r is

given by a pair (L ← Kb→ R) of jungle morphisms as follows:

1. L is a variable-collapsed tree with termL(rootL) = l. (A jungle G is a variable-collapsed tree if it has a single root rootG, and if indegreeG(v) > 1 impliesv ∈ VARG, for all v ∈ VG.)

2. K is the subjungle of L obtained by removing the edge with source rootL.

3. R is obtained from K as follows: if r is a variable, then the node rootL is identifiedwith the node r; otherwise, R is the disjoint union of K and the variable-collapsedtree R′ with termR′(rootR′) = r where rootR′ is identified with rootL and eachx ∈ VARR′ is identified with its counterpart in VARL.

11

Page 12: Implementing Term Rewriting by Jungle Evaluation€¦ · term rewriting. The computation of term normal forms by jungle evaluation is the subject of section 6. In section 7, it is

4. K → L and Kb→ R are inclusions with the exception that b maps rootL to r if

r is a variable. ✷

Definition 5.2 (Evaluation Step) Let G be a jungle and p = (L ← Kb→ R) the

evaluation rule for l→ r.

An evaluation step G⇒p

H via p is constructed as follows:

1. Find a morphism g : L→G.

2. Remove the edge with source gV (rootL) from G to obtain a jungle D.

3. If r is a variable, identify the node gV (rootL) with the node gV (r) to get H;otherwise, construct the disjoint union D+R′ and identify rootR′ with gV (rootL),and each x ∈ VARR′ with gV (x) to obtain H.

As for folding steps, we stipulate that G⇒p

H implies G⇒p

H ′ if there is an isomorphism

f : H→H ′ with fV (x) = x for all x ∈ VARH . ✷

RemarkThe jungles involved in the construction of evaluation steps are related by morphismsas in the case of folding steps (see the remark below definition 4.1). Since VD = VG

holds for evaluation steps as well, there is again a track function tr : VG→VH . tr isinjective on VARG so that we still may assume that tr does not rename variables. ✷

Example 5.3 (Evaluation Rules and Evaluation Step) In figure 3 we show twoevaluation rules; note that the rule for 0+ n→ n identifies the root m with the variablen.

mt+

t0

tn

mt

t0

tn

→ t0

tn=m

tfib

❄tsucc

❄tsucc

❄tn

t

tsucc

❄tsucc

❄tn

t+

ttsucc fib

❄ ❄t t tsucc succ fib

✖✲ ❄ ✕t✛n

0 + n→ n fib(succ(succ(n)))→ fib(succ(n)) + fib(n)

Figure 3: Two evaluation rules

Figure 4 shows an evaluation step via the left rule (the occurrence of the left-hand sideis drawn with dashed boxes).

The first two steps of figure 1 in the introduction show applications of the second eval-uation rule of figure 3. ✷

12

Page 13: Implementing Term Rewriting by Jungle Evaluation€¦ · term rewriting. The computation of term normal forms by jungle evaluation is the subject of section 6. In section 7, it is

Theorem 5.4 (Evaluation Steps Preserve Jungles) If G is a jungle and G⇒p

H

via some evaluation rule p, then H is a jungle too.

ProofLet p be the evaluation rule for l→ r, and let g : L→G be the morphism used toconstruct H.

Case 1: r is a variable. By definition 5.2, H is obtained from G by identifying gV (rootL)with some node gV (v). From outdegreeD(gV (rootL)) = 0 we conclude that no node inH has an outdegree greater than one.

To show that H is acylic suppose that there is a cycle in H. Then the construction ofH implies gV (v) >G gV (rootL). However, gV (rootL) >G gV (v) holds because there is apath from rootL to every other node in L. Thus there is a cycle in G, contradicting thefact that G is a jungle.

Case 2: r is not a variable. Then K is a subjungle of R. If p is a jungle rule in thesense of [HKP88], then H is a jungle by the jungle preservation theorem presented inthat paper. We have to show

(i) VARL ⊆ VARR and(ii) for each v ∈ VK and each x ∈ VARL, v >R x implies v >L x.

(i) follows from the definition of p. Let therefore v ∈ VK and x ∈ VARL with v >R x.By definition of p, each new path in R between two nodes in K starts at rootL. Sowe only have to consider the case rootL >R x. Since L is a variable-collapsed tree, wehave rootL >L v for each v ∈ VL − {rootL} and in particular rootL >L x. ✷

evaltheorem[theorem]Evaluation Theorem Let p = (L ← Kb→ R) be the evaluation

rule for some rewrite rule l→ r, and let G⇒p

H be an evaluation step under some

morphism g : L→G.

Then for each v ∈ VG,

termG(v)n−→{l→ r}

termH(tr(v)) where n is the number of pathsfrom v to gV (rootL).

(1)

t+✞✝✲☎✆✛tx

+✞✝✲☎✆✛ty

0

t+✞

✝✲

✆✛tx = y

0

Figure 4: Application of the evaluation rule for 0 + n→ n

13

Page 14: Implementing Term Rewriting by Jungle Evaluation€¦ · term rewriting. The computation of term normal forms by jungle evaluation is the subject of section 6. In section 7, it is

ProofLet the evaluation step have the diagram

L ← Kb→ R

g ↓ ↓ ↓ h

G ← Dc→ H

We proceed by bottom-up induction over VG.

Induction Base: Let x be a variable in G. By construction of p no edge is appended tox. Furthermore, x is identified at most with gV (rootL) where the edge outgoing fromgV (rootL) is removed. So tr(x) is a variable, and we get termG(x) = x = termH(tr(x)).Since there is no path from x to gV (rootL) we conclude that (1) holds with n = 0.

Induction Hypothesis: Let e ∈ EG such that all v ∈ VG with sG(e) >G v satisfy (1).

Induction Step: We distinguish two cases.

Case 1: sG(e) = gV (rootL). Let x ∈ VARL. Then gV (rootL) >G gV (x) because L isa variable-collapsed tree. Since G is acyclic, this implies that there is no path fromgV (x) to gV (sG(e)). So termG(gV (x)) = termH(tr(gV (x))) by induction hypothesis.For the substitutions σg and σh induced by g and h, respectively, this means thatσg(x) = σh(x) for all x ∈ VARL since tr(gV (x)) = hV (bV (x)) for all x ∈ VARL (notethat VARL = VARR).

Thus we obtain

termG(gV (rootL)) = σg(l) −→{l→ r}

σh(r). = termH(tr(gV (rootL))).

So gV (rootL) satisfies (1) with n = 1 since there is only one path from gV (rootL) toitself (viz. the empty path).

Case 2: sG(e) 6= gV (rootL). Then e ∈ ED. Let e′ = cE(e). The morphism propertiesof c imply

mG(e) = mH(e′), tr(sG(e)) = sH(e′), and tr∗(tG(e)) = tH(e′).(2)

Let tG(e) = v1 . . . vk for some k ≥ 0 and ni be the number of paths from vi to gV (rootL),for i = 1 . . . k. Then

termG(sG(e)) = mG(e)termG(v1) . . . termG(vk)n1−→

{l→ r}mG(e)termH(tr(v1)) . . . termG(vk) ind. hyp.

......

...nk−→

{l→ r}mG(e)termH(tr(v1)) . . . termH(tr(vk)) ind. hyp.

= mH(e′) term∗H(tH(e′)) (2)

= termH(sH(e′))= termH(tr(sG(e))). (2)

Thus (1) holds because there are n =∑i=1

k ni paths from sG(e) to gV (rootL). ✷

14

Page 15: Implementing Term Rewriting by Jungle Evaluation€¦ · term rewriting. The computation of term normal forms by jungle evaluation is the subject of section 6. In section 7, it is

Example 5.5 (Term Rewriting Performed by Evaluation Steps) In the evalu-ation step of figure 4, the terms represented by the roots of the jungles are related by a

two-step rewriting (0 + 0) + (0 + 0)2

→R

0 + 0. ✷

6 Computing Term Normal Forms

This section is about the completeness of jungle evaluation. We investigate the follow-ing question: given a term t, can we compute a normal form of t by jungle evaluation(provided there exists one)?

In the first place, we consider the applicability of evaluation rules.

Example 6.1 (Non-Left-Linear Rewrite Rules) Figure 5 shows a tree T and afully collapsed jungle G, both representing f(0, 0), as well as the evaluation rule for therewrite rule f(n, n)→ 0 (where n is a variable).

tf

✁ ✂✛ ✲t t0 0

tf

✝ ✆✲ ✛t0

tf

✝ ✆✲ ✛tn

t

tn

t0

tn

T G Evaluation rule for f(n, n)→ 0

Figure 5: Application of non-left-linear rules

Although f(0,0) can be rewritten with f(n, n)→ 0, the evaluation rule cannot be appliedto T : there is no jungle morphism from the left-hand side to T . This problem arisesonly with non-left-linear rewrite rules, i.e. rewrite rules which contain more than oneoccurrence of some variable on their left-hand side.

Theorem 3.12 suggests the following solution to this problem: prior to evaluation steps,jungles are transformed into fully collapsed jungles by application of folding steps. Inthis example, one application of the folding rule for 0 transforms T into G so that theevaluation rule can be applied subsequently. ✷

Lemma 6.2 (Applicability of Evaluation Rules) Let p = (L ← Kb→ R) be an

evaluation rule for some rewrite rule l→ r, and G be a jungle such that l→ r is applica-ble to some term t in TERMG. Then there are jungles G and H such that G

∗⇒F

G⇒p

H.

ProofBy theorems 4.7 and 4.8 there is a sequence of folding steps G

∗⇒F

G such that G is fully

collapsed. Since TERMG = TERMG there is a node v in G such that termG(v) = σ(l)for some substitution σ. Hence, by theorem 3.12 there is a jungle morphism g : L→ G

which maps rootL to v. So there is an evaluation step G⇒p

H. ✷

NotationG(R) denotes the set of all evaluation and folding rules forR. A reduction step G =⇒

G(R)H

15

Page 16: Implementing Term Rewriting by Jungle Evaluation€¦ · term rewriting. The computation of term normal forms by jungle evaluation is the subject of section 6. In section 7, it is

is either an evaluation or a folding step. A sequence of reduction steps G∗

=⇒G(R)

H is called

a reduction and tr : VG→VH is the composition of the track functions of all reductionsteps in this sequence. ✷

ntheorem[theorem]Normal Form Theorem A jungle G is a normal form with respect to=⇒G(R)

if and only if all terms in TERMG are normal forms and G is fully collapsed.

ProofLet G be fully collapsed and all terms in TERMG be normal forms and suppose thatthat there is a reduction step G =⇒

G(R)H . If this is an evaluation step, the evaluation

theorem 5 yields the contradiction that t→R

t′ for some t ∈ TERMG and some term t′.

Otherwise, if this is a folding step, theorem 4.7 leads to the contradiction that G is notfully collapsed.

Conversely, let G be a normal form. Then G is fully collapsed by theorem 4.7. More-over, if some t ∈ TERMG would not be a normal form, then lemma 6.2 would lead tothe contradiction that G

∗⇒F

G⇒E

H . ✷

Corollary 6.3 Let G∗

=⇒G(R)

H be a reduction such that H is a normal form. Then for

each v ∈ VG termH(tr(v)) is a normal form of termG(v).

ProofBy the evaluation theorem 5 and lemma 4.6 we have termG(v)

∗→R

termH(tr(v)) for each

v ∈ VG. Theorem 6 shows that termH(tr(v)) is a normal form. ✷

Example 6.4 (Incompleteness of Jungle Evaluation) Assume that R consists ofthe rewrite rules

f(x)→ g(x, x) c→ d d→ c g(c, d)→ e

where c, d, and e are constant symbols, and x is a variable.

Then f(c) has a normal form obtained by f(c)→Rg(c, c)→

Rg(c, d)→

Re. This rewrite

sequence cannot be simulated by a jungle reduction sequence because the applicationof the evaluation rule for f(x)→ g(x, x) to a jungle representing f(c) yields a junglewhich represents g(c,c) such that the two occurrences of c are shared. Hence theseoccurrences cannot be evaluated independently and the evaluation rule for g(c,d) →e can never be applied. As a consequence, no jungle representing f(c) has a normalform.

For similar reasons it may happen that jungle evaluation terminates but fails to computeall term normal forms. Consider the rewrite system

f(x)→ g(x, x) c→ d c→ e

Jungle evaluation can compute only g(d,d) or g(e,e) as normal forms of f(c) al-though g(d,e) and g(e,d) are further normal forms. ✷

16

Page 17: Implementing Term Rewriting by Jungle Evaluation€¦ · term rewriting. The computation of term normal forms by jungle evaluation is the subject of section 6. In section 7, it is

7 Termination

In this section we prove that =⇒G(R)

is terminating provided that the underlying rewrite

system R is terminating. The proof is somewhat involved since in contrast to termrewriting, jungle evaluation steps may produce “garbage” which gives rise to additionalevaluation steps.

Termination Theorem 7.1 If →R

is terminating, then =⇒G(R)

is terminating, too.

ProofLet →

Rbe terminating. In a first step we extend

+→R

to the relation > by defining

t > u if u is a subterm of some t′ with t+→R

t′.

Like+→R

, > is a strict ordering. Moreover, > is terminating as for each sequence

t1 > t2 > t3 > . . . there is a sequence t1+→R

t2+→R

t3+→R

. . . with ti being a subterm of ti.

In a second step > is extended to an ordering ≫ on finite multisets of terms. (Themultisets we consider are functions M : TSIG(X)→N with M(t) = 0 almost everywhere.Then M ∪ N(t) = M(t) + N(t) and M − N(t) = max(0, M(t) − N(t)) for all multisetsM, N.) Following Dershowitz and Manna [DM79], we define ≫ by

A≫ B if B = (A− REM) ∪ ADD where

• ∅ 6= REM ⊆ A, and

• for each u ∈ ADD there is t ∈ REM with t > u.

In [DM79] it is shown that the termination of > carries over to ≫.

We assign a multiset TERMG to each jungle G by

TERMG(t) = |{v ∈ VG | termG(v) = t}|.

It remains to be shown that for all jungles G, H,

G =⇒G(R)

H implies TERMG ≫ TERMH .

For G⇒F

H we have TERMH = TERMG − {t} where t is the term represented by the

two nodes in G which are identified. Let therefore G⇒p

H for some evaluation rule p =

(L ← K → R) and let v be the image of rootL in G. Then TERMH = (TERMG − REM) ∪ ADD

where

REM(t) = |{v ∈ VG | v ≥G v and termG(v) = t}| andADD = OLD ∪ NEW withOLD(t) = |{o ∈ VH | o ≥H tr(v) and termH(o) = t}| andNEW(t) = |{n ∈ VH | n 6∈ tr(VG) and termH(n) = t}|.

17

Page 18: Implementing Term Rewriting by Jungle Evaluation€¦ · term rewriting. The computation of term normal forms by jungle evaluation is the subject of section 6. In section 7, it is

Clearly we have REM 6= ∅ because termG(v) ∈ REM. Now consider some u ∈ ADD.

Case 1: u ∈ OLD. Then there are v ∈ VG with v ≥G v and o ∈ VH such that tr(v) = o

and termH(o) = u. By theorem 5 we have termG(v)+→R

u, so REM ∋ termG(v) > u.

Case 2: u ∈ NEW. Then there is n ∈ VH − tr(VG) with termH(n) = u. n 6∈ tr(VG) im-plies tr(v) >H n, i.e., u is a subterm of termH(tr(v)). We conclude REM ∋ termG(v) >

u since termG(v)+→R

termH(tr(v)) (by theorem 5).

Thus TERMG ≫ TERMH which shows that =⇒G(R)

is terminating. ✷

Theorem 7.1 together with corollary 6.3 suggests that jungle evaluation is particularlysuited for the implementation of terminating and confluent term rewriting systems.Each term represented by some jungle can be evaluated through the exhaustive ap-plication of evaluation and folding rules. For the implementation one may choose anyevaluation strategy since every jungle reduction computes the unique normal form ofthe input term.

8 Confluence

In this section we show by a counterexample that confluence does not carry over fromterm rewriting to jungle evaluation. It turns out, though, that confluence is preservedif the given term rewriting system is also terminating, and if the “garbage” producedby evaluation steps is ignored.

Example 8.1 Consider the following rewrite system :

Sorts s, t Operations c :→ s Rules c→ d

d :→ s d→ c

e :→ t f(c)→ e

f : s→ t f(x)→ g(x, x)g : s s→ t g(c, d)→ e

Figure 6 demonstrates that the rewrite relation of this system is confluent while jungleevaluation is not confluent.

The rewriting step g(c, c)→ g(c, d) cannot be simulated since the evaluation step cor-responding to f(c)→ g(c, c) generates a jungle in which both occurrences of c are rep-resented by the same node. Analogously, neither g(d, d)→ g(c, d) can be simulated.

The above rewriting system is neither terminating nor non-overlapping. It turns outthat these conditions are essential for the confluence of jungle evaluation. (See thelong version of [HP88] for the case of non-overlapping term rewrite systems.) Below weconsider the case that →

Ris terminating. For achieving confluence we have to ignore

the “garbage” produced by evaluation rules.

18

Page 19: Implementing Term Rewriting by Jungle Evaluation€¦ · term rewriting. The computation of term normal forms by jungle evaluation is the subject of section 6. In section 7, it is

f(c) f(d)

e g(c,d)

g(c,c) g(d,d)

g(d,c)

ցտ

ցտ

ւր

ւր

↓ ↓

←−−→

←−ւ�

��

��

���

♠te

♠sc

⇐=

♠tf❄♠sc

⇐⇒

♠tf❄♠sd

⇓ ⇓♠tg

✂ ✁✲ ✛♠sc

⇐⇒

♠tg

✂ ✁✲ ✛♠sd

Figure 6: Non-confluence caused by sharing

Example 8.2 (Non-Confluence Caused by Garbage) Let c → d and f(c) →f(d) be rewrite rules for an operation symbol f and constant symbols c and e. Figure 7demonstrates that term rewriting is confluent, whereas jungle reduction yields distinctnormal forms.

f(c)

↓f(d)

tf

❄td

⇐=

tf

❄tc

=⇒

tf

✖✲❞ tc d

Figure 7: Non-confluence caused by garbage

This is because the argument c is removed when the rewrite rule f(c) → f(d) isapplied, while the corresponding subjungle (drawn with dashed box and unfilled circle)is preserved by the evaluation step. This subjungle is garbage with respect to the termrewriting performed at the root. ✷

In the following we distinguish certain nodes in a jungle as “points of interest”, andconsider the nodes and edges that are not reachable from these points as garbage. Itis possible to remove garbage from jungles explicitly by garbage collecting rules (whichare the inverses of the jungle generating rules defined in [HKP88]). Here we just restrictour attention to reductions that keep track of these points.

Definition 8.3 (Pointed Jungle and Pointed Reduction) A pointed jungle is apair (G, PG) where G is a jungle and PG ⊆ VG. We denote a pointed jungle only by itsjungle component. G• is the subjungle of G with VG• = {v′ ∈ VG | v ≥G v′ for some v ∈PG} and EG• = {e ∈ EG | sG(e), tG(e) ∈ V ∗

G•}.

A reduction G∗

=⇒G(R)

H is called pointed if PH = tr(PG). ✷

19

Page 20: Implementing Term Rewriting by Jungle Evaluation€¦ · term rewriting. The computation of term normal forms by jungle evaluation is the subject of section 6. In section 7, it is

Theorem 8.4 (Confluence of Pointed Reduction) Let→R

be terminating and con-

fluent and let H1∗⇐=G(R)

G∗

=⇒G(R)

H2 be pointed reductions. Then there are pointed reductions

H1∗

=⇒G(R)

X1 and H2∗

=⇒G(R)

X2 such that X•1∼= X•

2 .

ProofBy the termination theorem 7.1 there are pointed reductions Hi

∗=⇒G(R)

Xi such that Xi

(and thus X•i ) are normal forms for i = 1, 2. We show that TERMX•

1= TERMX•

2.

“⊆”: Let v be a node in X•1 . Then there is a point p1 in X1 with p1 ≥X1

v, so termX1(v)

is a subterm of termX1(p1).

Let tr1 be the track function of G∗

=⇒G(R)

H1∗

=⇒G(R)

X1 and let p be a point in G with tr1(p) =

p1. Now consider p2 = tr2(p), where tr2 is the track function of G∗

=⇒G(R)

H2∗

=⇒G(R)

X2. By

corollary 6.3 both termX1(p1) and termX2

(p2) are normal forms of termG(p). Since →R

is confluent we conclude that termX1(p1) = termX2

(p2).

So termX1(v) is a subterm of termX2

(p2); hence there is a node v′ in X2 with p2 ≥X2v′

and termX2(v′) = termX1

(v). Since v′ belongs to X•2 we get

termX•

1(v) = termX•

2(v′) ∈ TERMX•

2.

(Note that termH(u) = termH•(u) for each pointed jungle H and each node u in H•.)

“⊇”: By a symmetric argument.

Thus TERMX•

1= TERMX•

2. Moreover, X1 and X2 are fully collapsed by theorem 4.7,

so the uniqueness theorem 3.13 yields X•1∼= X•

2 . ✷

9 Summary and Outlook

We have shown how term rewriting can be (abstractly) implemented by graph rewritingwithin the jungle approach set up by [Plu86] and [HKP88].

Jungles are acyclic hypergraphs which represent finite sets of terms such that commonsubterms can be shared. In particular, fully collapsed jungles represent terms suchthat all equal subterms are shared. Every jungle can be transformed into a uniquefully collapsed jungle by applying folding rules. Term rewrite rules are translated intoevaluation rules which are sound in the sense that each evaluation step performs aparallel term rewriting step. When evaluation and folding rules are used together,jungle normal forms always represent term normal forms, even for non-left-linear termrewriting systems. Furthermore, terminating term rewriting systems result in termi-nating jungle systems. Although confluence is not preserved in general, terminatingand confluent term rewriting systems result in confluent jungle evaluation systems ifthe garbage produced by evaluation steps is ignored.

Comparison with the approach of Barendregt et al.In their paper [BvEG*87], Barendregt, van Eekelen, Glauert, Kennaway, Plasmeijer,

20

Page 21: Implementing Term Rewriting by Jungle Evaluation€¦ · term rewriting. The computation of term normal forms by jungle evaluation is the subject of section 6. In section 7, it is

and Sleep have proposed an alternative graph rewriting approach which is speciallydesigned to model term rewriting.

While [BvEG*87] mainly focusses on strategies to compute term normal forms, we donot consider strategies in this paper. (See [Plu90] for results on jungle evaluation strate-gies). Instead, we consider termination and confluence of jungle evaluation, dependingon properties of the given term rewriting system.

The main completeness result of [BvEG*87] states that term graphs representing nor-mal forms can be computed for weakly regular term rewrite systems (where “weaklyregular” essentially means left-linear and non-overlapping). We have shown that junglereduction is complete for arbitrary terminating and confluent term rewriting systems.

The use of folding rules in our approach allows us to consider arbitrary term rewritingsystems. [BvEG*87] treats only left-linear systems because graph rewriting withoutfolding is incomplete otherwise. Moreover, folding does not only guarantee complete-ness but also speeds up the evaluation: the change from exponential time to lineartime in the Fibonacci example shown in the introduction is only possible with folding.

OutlookThe work presented here should be continued in various directions:

Complexity analysis: In general, the space required for term rewriting grows expo-nentially with the number of rewrite steps since the size of a term may be multipliedin a single step. In contrast, jungle evaluation needs only linear space because eachevaluation step increases the size of a jungle only by a constant.

Beside this general observation, the time and space complexity of term rewriting andjungle evaluation should be compared in a precise way. Classes of term rewrite systemsshould be identified for which jungle evaluation is strictly more efficient than termrewriting.

Parallel evaluation: Jungle evaluation is based on the algebraic approach of graphgrammars for which parallelism and concurrency concepts have been developed (see,e.g., [Ehr83], [KW87]). It should be possible to exploit results from these areas to workout a theory of parallel jungle evaluation.

General jungle manipulation: The evaluation and folding rules used in this paper arespecial cases of hypergraph rules which preserve the jungle structure. Such jungle ruleshave been introduced in [Plu86] and [HKP88] as a general means for deriving junglesfrom jungles. One could think of a specification and programming approach based onthese rules which goes beyond the possibilities of term rewriting.

References

[BvEG*87] H.P. Barendregt, M.C.J.D. van Eekelen, J.R.W Glauert, J.R. Kennaway,M.J. Plasmeijer, M.R. Sleep: Term Graph Rewriting. Proc. PARLE, Lec-ture Notes in Comp. Sci. 259, 141-158 (1987)

[DJ90] N. Dershowitz and J.-P. Jouannaud: Rewrite Systems. Handbook of The-

21

Page 22: Implementing Term Rewriting by Jungle Evaluation€¦ · term rewriting. The computation of term normal forms by jungle evaluation is the subject of section 6. In section 7, it is

oretical Computer Science, Vol. B, chapter 15, North Holland (1990)

[DM79] N. Dershowitz and Z. Manna: Proving Termination with Multiset Order-ings. Comm. ACM 22(8), 465-476 (1979)

[Ehr79] H. Ehrig: Introduction to the Algebraic Theory of Graph Grammars. Proc.1st Graph Grammar Workshop, Lecture Notes in Comp. Sci. 73, 1-69(1979)

[Ehr83] H. Ehrig: Aspects of Concurrency in Graph Grammars. Proc. 2nd GraphGrammar Workshop, Lecture Notes in Comp. Sci. 153, 58-81 (1983)

[EM85] H. Ehrig, B. Mahr: Fundamentals of Algebraic Specification 1 – Equa-tions and Initial Semantics. Springer-Verlag, Berlin–Heidelberg–NewYork (1985)

[ER76] H. Ehrig, B.K. Rosen: Commutativity of Independent Transformations onComplex Objects. Research Report RC 6251, IBM T.J. Watson ResearchCenter, Yorktown Heights (1976)

[HKP88] A. Habel, H.-J. Kreowski, D. Plump: Jungle Evaluation. Proc. FifthWorkshop on Specification of Abstract Data Types, Lecture Notes inComp. Sci. 332, 92-112 (1988)

[Hof83] B. Hoffmann: Compiler Generation: From Language Descriptions to Ab-stract Compilers. Dissertation, TU Berlin (1983)

[HP88] B. Hoffmann, D. Plump: Jungle Evaluation for Efficient Term Rewriting.Proc. Algebraic and Logic Programming, Lecture Notes in Comp. Sci.343, 191-203 (1988)

[Hue80] G. Huet: Confluent Reductions: Abstract Properties and Applications toTerm Rewriting Systems. Journal of the ACM 27(4), 797-821 (1980)

[Klo90] J.W. Klop: Term Rewriting Systems: from Church-Rosser to Knuth-Bendix and Beyond. Proc. ICALP’90, Lecture Notes in Comp. Sci. 443,350-369, 1990.

[KW87] H.-J. Kreowski, A. Wilharm: Is Parallelism Already Concurrency? –Part 2: Non-sequential Processes in Graph Grammars. Proc. 3rd GraphGrammar Workshop, Lecture Notes in Comp. Sci. 291, 361-377 (1987)

[Pad82] P. Padawitz: Graph Grammars and Operational Semantics. TheoreticalComp. Sci. 19, 117-141 (1982)

[Plu86] D. Plump: Im Dschungel: Ein neuer Graph-Grammatik-Ansatz zur ef-fizienten Auswertung rekursiv definierter Funktionen. Diplomarbeit, Fach-bereich Mathematik/Informatik, Universitat Bremen (1986)

22

Page 23: Implementing Term Rewriting by Jungle Evaluation€¦ · term rewriting. The computation of term normal forms by jungle evaluation is the subject of section 6. In section 7, it is

[Plu90] D. Plump: Graph-Reducible Term Rewriting Systems. Proc. 4th GraphGrammar Workshop, to appear in LNCS (1990)

[Rao84] J.C. Raoult: On Graph Rewritings. Theoretical Comp. Sci. 32, 1-24 (1984)

[Sta80] J. Staples: Computations on Graph-like Expressions. Theoretical Comp.Sci. 10, 171-185 (1980)

23