On the Adequacy of Graph Rewriting for Simulating Term Rewriting J. R. KENNAWAY University
of East Anglia
and J. W. KLOP CWI, Vrije Universiteit and M. R. SLEEP University
of East Anglia
and F. J. DE VRIES
Cwl Several authors have investigated the correspondence between graph rewriting and term rewriting. Almost invariably they have considered only acyclic graphs. Yet cyclic graphs naturally arise from certain optimizations in implementing functional languages. They correspond to infinite terms, and their reductions correspond to transfinite term-reduction sequences, which have recently received detailed attention. We formalize the close correspondence between finitary cyclic graph rewriting and a restricted form of infinitary term rewriting, called rational term rewriting. This subsumes the known relation between finitary acyclic graph rewriting and finitary term rewriting. Surprisingly, the correspondence breaks down for general infinitary rewriting. We present an example showing that infinitary term rewriting is strictly more powerful than infinitary graph rewriting. The study also clarifies the technical difficulties resulting from the combination of collapsing rewrite rules and cyclic graphs.
by Abstract Devices]: Models of Categories and Subject Descriptors: F.1.l [Computation Computation; F.4.1 [Mathematical Logic and Formal Languages]: Mathematical Logic; F.4.2 [Mathematical Logic and Formal Languages]: Grammars and Other Rewriting Systems
The authors were partially sponsored by SEMAGRAPH, ESPRIT Basic Research Action 3074. The first author was also partially supported by a SERC Advanced Fellowship, and by SERC grant no. GR/F 91582. Authors’ addresses: J. R. Kennaway and M. R. Sleep, School of Information Systems, University of East Anglia, Norwich NR4 7TJ, United Kingdom; email:firk;mrs} @sys.uea.ac.uk; J. W. Klop, Department of Software Technology, CWI, P.O. Box 94079, 1090 GA Amsterdam, The Netherlands and Department of Mathematics and Computer Science, Vrije Universiteit, de Boelelaan 1081a, 1081 HV Amsterdam, The Netherlands; email:
[email protected]; F. J. de Vries, Department of Software Technology, CWI, P.O. Box 94079, 1090 GA Amsterdam, The Netherlands; email:
[email protected]. Permission to copy without fee all or part of this material is granted provided that the copies are not made or distributed for direct commercial advantage, the ACM copyright notice and the title of the publication and its date appear, and notice is given that copying is by permission of the Association for Computing Machinery. To copy otherwise, or to republish, requires a fee and/or specific permission. 01994 ACM 0164-0925/94/0500-0493 $03.50 ACM Transact,om onPmgrzummngLanguage8and Systems,VO116, NO 3, May 1994,Pages493-523
494
.
J, R. Kennaway
General
Terms:
Additional rewriting
Languages,
et al
Theory
Key Words and Phrases:
Functional
programming,
graph rewriting,
orthogonal
term
1. INTRODUCTION Graph rewriting is a well-known technique systems. The advantage it has over “naive” term
rewrite
rule
Times(Succ(
would
x),
require
rewrite
such
~
Add(
y, Times(
two copies
implementation
This saves time in normal form,
term rewrite is that where a
as
y)
making
for implementing term rewriting
can
x,
y))
of the second
instead
make
argument
two
to Times,
references
the graph
to a single
copy.
and space, especially when the argument is not itself as can be the case when the language has call-by-need
semantics. Graph rewriting is frequently used to implement [Peyton Jones 1987]. Besides the advantages illustrated ing
allows
a subtle
the following Repeat(n) Each
time
made.
But
subterm side, write graph
optimization
rule +
the
in the computation
for constructing
rule
Repeat(n)
of fixed
list
of copies
points.
Consider
of its argument:
Repeat(n)).
Cons(n,
is used,
additionally,
an infinite
functional languages above, graph rewrit-
two
copies
the reference
can be replaced
of a pointer
to the
on the right-hand by a reference
argument
n are
side of the rule
to the
to the root of the right-hand
giving a graph rewrite rule with a cyclic right-hand side which we can as Repeat(n) + x :Cons(n, x). The syntax x :Cons( n, x) here denotes a with
two nodes,
one of which
is labeled
with
the symbol
Cons
and has
two out-arcs, the second of which points to itself. A single application of this rule constructs the entire infinite list at once, as a finite cyclic structure. Figure 1 shows the effects of these two different representations of the rule. The correctness
of the graph-rewriting
a piece of well-known
folklore.
implementation
For acyclic
graphs
been studied in Staples [1980], Barendregt [1989], and Farmer et al. [1990]. Only the graphs at all. In an obvious
and
intuitive
sense,
acyclic
of term
the formal
rewriting
relationship
is has
et al. [1987], Farmer and Watro last two of these consider cyclic graphs
can be “unraveled”
to
trees—the syntax trees of terms. Cyclic graphs can be similarly unraveled, but give rise to infinite trees, which we can regard as infinite terms. A single reduction in a cyclic graph can correspond to the reduction of infinitely many redexes in the corresponding infinite term. A finite sequence of graph reductions may correspond to a term reduction sequence of length greater than co. A precise account of the relationship between graph rewriting, including cyclic graphs, and term rewriting must therefore consider infinitary term rewriting. In a related paper [Kennaway et al. 1993b] the authors have set ACM Transactions on Programming
Languages and Systems, Vol. 16, No 3, May 1994.
Graph Rewriting for Simulating
\
Repeat
I
Cons
v
I 3
Term Rewriting
Cons / 3“
3
/ 3 Figure
out the foundations
of infinitary
term
1.
rewriting
for orthogonal
systems. In this
article
rewriting,
and
show that implements
for orthogonal systems of rewrite rules, finitary in this sense a restricted version of infinitary
This that
we
define
a notion
finitary
of one
and
rewrite
subsumes and makes more precise for orthogonal systems, finitary
finitary
term
prisingly, rewriting. Our
rewriting.
infinitary present
‘3
Repeat
Repeat
495
.
We show graph
definition
term
and
implementing
term
~aph term
not
implementation
implement by
one
graph We
rewriting rewriting. et al. [1987] implements
of a counterexample
does
rewrite
another.
the result of Barendregt acyclic graph rewriting
by means
rewriting of an
infinitary system
term
that,
infinitary system
surterm
of another
(called here an adequate mapping from one to the other) adds to the abundance of concepts of simulation, in term rewriting (for example, Barendregt [1987] and O’Donnell [1985]), complexity theory (for an overview see Van Erode Boas [1990]) or programming languages [Mitchell 1991].
2.
2.1
FINITARY Basic
AND
INFINITARY
TERM
REWRITING
Definitions
General introductions to finitary term rewriting may be found in Dershowitz and Jouannaud [1990] and Klop [1992]. Here we shall define the basic concepts of infinitary term rewriting, rewriting as a special case. For the formalization of infinitary matical vanced
concepts: knowledge
Smyth [1992] A signature
which rewriting,
can be seen as including we will
require
finitary
certain
mathe-
metric spaces, continuity, and ordinal numbers. No adis required, only a familiarity with the basic definitions.
and Phillips 2 consists
[1992] give clear expositions of the material. of a countably infinite set Var~ of variables
(x,
y,
z ,...
) and a nonempty set of function symbols of various finite arities >0. Constants are function symbols with arity O. Variables are also deemed to have arity O. An (infinitary) term over X is a finite or infinite ordered tree. Each node of the tree is labeled of the tuple
by a member
is equal
to the arity
ACM Transactions
of Z and has a tuple of the label
on Programming
of descendants.
of the node.
Term(Z)
The size
is the set of
Languages and Systems, Vol. 16, No. 3, May 1994.
496
J, R, Kennaway
.
infinitary
terms
trees. An
occurrence
occurrence
as,
induction
is e.g.,
if
of Ter”(
a
positive
finite
the
the
form
of
set
O, this
integer
and
u
rence
the
AddList(Plus(8, There
length
z~ is an
is a useful
otherwise
t and t‘ such that
u.
Then
metric
n, where
2
an
for
by
all
member
t
t. If
of 0(t).
occurrence,
belongs
to
,
of
3), Nil))).
of O(t) only
write
tn),1 s i s n, and u = O(tL). Note that O(t) O(Cons(3, Cons(2, the nodes of t.For example,
F(tl,...
F(tl,....tn)I i “ u = t, I u. The depth
is
We
t is defined
of
is the
is in 1– 1 correspondence with Nil))) = {e, 1, 2, 2.1, 2. 2}. If u is a prefix of concatenation of u and u is written u . u. If u ● O(t) then the subterm t Iu at occurrence
t I e = t and
of the finite
integers.
● is a member
of arity
i is an
Z’) consisting
of occurrences
0(t)
occurrence
symbol
i . u, where
t has
sequence
empty
or a function
occurrence
O(t)
Z. Ter( Z) is the subset
1 “ 3 “ 2 “ 2. The
as follows:
is a variable An
over
et al
For set
the nodes of t and
let
as follows:
t at occur-
of
t == Cons(4,
Cons(12,
3).
Define
of the
u < U. The
u is defined
~ 1 = P1us(8, Terti(2).
length
write
of a subterm
example,
t I2.2
on the
n is the
U, we
d(t,
shortest
t‘)
to be O if t = t‘,
occurrence
t‘corresponding
to that
u common
to
occurrence
bear
different symbols. A substitution is a map o- :Varz + Term(S). CTis extended to a function on a(F(tl,....tn))= F(a(tl),....a(tn)),and to the whole of Ter(X) by defining Terr”(S) by requiring that it be continuous with respect to the metric. Intut by replacing every itively, this just means that a(t) is obtained from occurrence such
of every
occurrences,
defined
variable v(t)
on a finite
A rewrite
set
rule
x
is called
in
t by a(x).
an
instance
of variables
is a pair
xl,
...,
( tl,t,)of terms
There of
may
t.We may
Xn as [xl
in Ter”(
=
be
infinitely
write
tl,
...,
2), written
many
a substitution x.
tn].
=
as tl+ t,,such
and not a variable, and every variable occurring in t, occurs tl.tl and t, are called the left- and right-hand sides of the rule. It is A redex (reducible expression) of a term t consists finitary if t, is also finite. u of t, a rule tl+ t,, and a substitution CT, such that of an occurrence t Iu = a (tl).The result of reducing this redex is the term resulting from to t‘ by reduction of replacing the subterm of t at ZL by a(t,).If t reduces t + t‘.Concatenating reduction steps we get either a some redex, we write finite reduction sequence to + tl ~ “”” t,, which we also denote by to -n tn, or an infinite reduction to - tl + t2 + ....A normal form is a term containthat
tl
also
in
ing
is finite
no redexes. We
require
grounds
al.
left-hand
of whether
in
a finite
time)
some
of the
[ 1993b]). infinite
cyclic
right-hand require
an upper ACM
bound
Transactions
is, that
in
on the
technical sides
sides we
a system
on Programming
in
be
order
to
of the Languages
on
by
both
problems.
model
graph
left-hand
infinitely
many
Vol. 16, No
will
be sides
Kennaway We
in
et fact
rewrite
rules
with
sides
are
also
side rules,
to be finite, there
sides.
and Systems,
should
left-hand
left-hand
left-hand
rule
to fail—see
cause
each
philosophical
a given
(infinite
rewriting do not
require
having
finite
grounds
Unbounded
while size
to
is a redex
of infinitary
sides.
unproblematic—that
a rule
a term
right-hand
right-hand
of
and
properties
Infinite
require
do not
side
question
decidable cause
the
(the
3, May
1994.
should
we be
Graph Rewriting for Simulating A ( finitary)term (T, R), where
rewrite
system
R is a set of rewrite
(or
TRS)
rules
Term Rewriting
over
a signature
.
497
Z is a pair
in T x T, and T is a set of terms
over
2 which is closed under reduction by R and the subterm relation. We do not require a TRS to contain every term which can be constructed from the signature. This allows us to uniformly treat such things as typed or sorted systems, where there are constraints closure conditions on T are for convenience. under substitutions, since, for example, in cannot must
be applied not imply
A transfinite whose domain
to boolean that
values,
Add(True,
of terms
Add( x, y) and True
continuous if the sequence of terms { fp I D < a} is a (with the usual topology on ordinals) to T (with above). For each ~ < a, let d~ be the depth of the
reduced
in the step from fp to fP + ~. The sequence is strongly continu{dP I ~ < A} tends to every limit ordinal A < a, the sequence is Cauchy convergent if it is Cauchy continuous and converges to a limit, denoted by fm. It is strongly convergent if in addition
ous if for infinity. It topologically the sequence
{dD \ ~ < a} tends
We consider transfinite allow
the legality
y) is also legal.
reduction sequence of a TRS (T, R) consists of a function f is an ordinal a, such that f maps each /3 < a to a reduction
step fp - fP + ~. f is Cauchy a continuous function from the metric topology defined redex
on which terms are “legal.” The Note that T need not be closed a typed system where addition
strongly
reduction
the definition
to infinity.
convergent sequence.
reduction
Cauchy
to be the
convergence
of the fundamental
notions
appropriate
alone
of residuals
notion
of
is insufficient
to
and projection,
as
shown in the next section. We write a +“ b (resp.,
a +