CHARACTERIZATION
AND
ELIMINATION
Norman Harvard
Many Abstract. by interpretations procedure Q
wellknown of the
f(x)
functions recursion
are schema
OF REDUNDANCY
H.
computed
;
Cohen
However,
many
of
because
they
lead
is,
cause
they
times
then
return
a(x)
else .—
return
b(x,f(cl(x)),
these
with
))
by
f
with
identical
of
known
In
each
to
computes certain
the
be
to
equal
case
there
f(x)
without
for is
are
redundant
be
inefficient
computation.
called
That
recursively
several
arguments.
set
of
transformed about
by f(x)
~rocedure
We
a,
The
which that and
resulting
Figure and
the
results
in
specified
global
transformed
ability program
programs,
of these improvement
locations;
for
criterion redundancy.
the
to present the
establishing
an
the
1,
y+z,
J4 =
Cl(x)
we
obtain
given a Xth element
non
of
the
;
or
x.1
Q&;return
1
else
f(x1)
return
computation
f(2)
is
+
f(x2)
f(3) three
calls
on
f(5) is
is
times. f
pictured
computed
two
In
with
all,
only
in times,
there distinct
six
values. interpretations
certain
procedure is
for
that
computed
fifteen
For
in
without requirements
discuss
transformations system, and
X, returns
Note
11.
are
are all valueless intermediate and
each case recursion can be eliminated of a stack. We compare the storage
schema =
which
transformations
programs which leave
the a(x)
= which,
x.O
❑erging
computation. recursive
These procedures
interpret
and b(x,y,z) function,
f’(x)
U
assumptions.
p,
we
(x=O~x=l),
The
program provided
redundancy, assumptions
if ::
integer series:
argument
eKist f,
yield
partially
which,
when
a nonredundant
equivalent
to
of applied program
f.
The
9n , to
the
which
applicability
use
applicautomatic a general
existence
of
INTRODUCTION
Functions
often are
which
definitions recursion
Pn:
to
c2(x) = x2, following
Ci are satisfied. The transformed programs avoid redundancy by saving exactly those intermediate results which will be needed again later in the
1.
f
p(x)
negative Fibonacci
calls
functions.
formed tree for
each
a
multiple values.
these
acyclic graphs the computation
additional
the
definitions
to
instance:,
letting
redundancy depend on Ci. We explore four
about
assumptions
are
lead
argument
and nature of of the functions
analyze directed the nodes of
redundant
define
they
because
existence properties
of
these
identical
For . . ..f(cn(x)
interpretations
computations
final
PROGRAMS*
p(x)
Some of
sets
RECURSIVE
University
x1, the
on
IN
schema
P rocedure U p(x) MQQ
else
gn, f(x)
contract
under
by
;
return return
fellowship Navy
defined
and Naval
Figure
a(x) b(x,f(cl
suPPorted
*Research
recursive of the
elegant have interpretations
(x)),.
by by
U.S.
Electronic
an
..,f(cn(x)
IBM Department Systems
)) research of
11.
A
the
interpretation call on
f,
value
that
for
recursive
Command
represented
NOO03978G0020.
143
tree
of
computation
.
of’
and call.
cal 1s by
that
representation
f(5) ~2.
is
the
of
the Fibonacci under represents Each node labelled
The arising node.
sons
with of
directly
the
a node from
argument represent the
call
a
of
these
of
two
transformations
the
functions
frontier predicate
ci~
condition p and
the
such
following
to
each
relating function
the
interpretation,
the on condition
depends descent
a I
conditions
a
of
and
a
to
“ t~~
transformation
nonredundant
again.
relating
other
the For
a.
~alidity
the
Fibonacci would
yield
program:
In
many,
storage
can
inputs.
(In
9 whose amounts
computations
f(x)
=
; ; ~BACKO
S@
;
f2(x)
;
all
:=
1
:=
1
;
Q@ f2(x1)
been
storage have
:=
BACKI
BQCKI
:=
BACKO
to
f2.
for
the
in
BACKO .
BACKI
BACKO,
;
BACKO
:=
BACKI
+ BACK2
General
The
recursive
and
(It the
We
also value
exDlain schema
~.
the
four
are
f2(x)
is
of
f(x)
descent
Sections
condition 2
through
in
terms
with of
effect
in
each
and
than
program
5
we
of
Q
p(x) return
a(x)
else
return
b(x,c(x),d(x
simplicity
we
We explore
the
condition program
places
constants Section
c
We
frontier
for
mathematical the
improvement
After
we
consider
descent
program schema
the
of each
c, and dkscent
present
and validity
all
is
exploits
these
redundancy. of
In these
and
exploring programs
redundant
mathematical
which
result
of
of
As
in
Dvnamic
dvnamic
DroKrammin
use
value
many times. the method of
tabulation
Bird
storage
knowledge for
about results
the which
interpretation will
to not
be
any
recursively
appropriate
In
the
global
case
where
p(x)
locations needed
as
We assume
can
be
frontier
this
valid
the and
stillactive
in
computed condition
assumption;
for
most
we
naturally
have in
of
a
in
stack. of
the
recursion case
needed
144
the
side
which,
the
f(x)
done
i
1
:=
1
~
would
; ; 1 until
x1
IQ
bs.am BACK2
:=
BACK1
:
BACKI BACKO
:= :=
BACKO BACKI
~ + BACK2
BACKO
in without
example,
recursion
;
:=
of
expense Methods
discussed be
Fibonacci the
not
location.
at
can
do
effect
iterative. are
this
2
which
program:
Q2&; return &
Sections
designated
made
each In
this
the a
form be
eliminating
we reuse
a
easily
in
procedures
but
can
6;
programs
~rocedure
may be needed in some ways
use
by
itself
of
is
as
on, in values
This
needed
The
the
well
condition,
f(x).
results”
results are in
@
However,
later these
‘Tprevious
Qe&L?l BACKO
[1977b].
needed Using
recursion.
,
eliminating
essentially
simpler
by saving results which The approach resembles by
has as
called
recursive
their programs
clarity,
for
the more
Hopcroft, and is avoided
presented
This
initialize the global and any other values by the caller of f2.
it
are
Programming
redundant
[1974]),
be
When checks itself
f(x’),
of
placed
transformed
a
result
K (Aho, computation
Unman
it
descent
returns.
BACKI to
the
global
first calls
x? .
be
statement
5
Section of
of in
uses results.
cases.
return
transformed
full generality applicability
these
The
programs. a
are
that
through
the and in
implications
present
improvements efficient programs.
RelationshiD
to
values,
an
leaving These
of
the the
intend
argument
of
value
might
f2
formal
arising
a
of
x)
further
a
value
(which
we simply value a(x) resultsll
conjecture
n)2. both in redundant
of
condition
reflecting ~ we discuss
specialized and more
place
which
f without generalization
~n
interested nature
automatic
in
condition,
to are
d
domain the
to compute 6 we sketch the
transformations
each
and
constraints on the given
which,
appropriate
use
argument
we
f2,
value
of the
f2
and
without (For
make
storage
particular
program If not,
which may locations.
which of
argument
that
).
new
advantage
values
is true, with the Ifprevious
then
C2.)
to
the
previous
the
the
computes
invocations
;
x, true.
some
values global
taking
other
the
these
leaving
any other specified
with
f(x)
approaches
Since
program,
store
with argument is p(x)
recursively
by
procedure
a
calls previous
when
for many clever.
transformed
locations, ~:
locations to be more
a program
whether
into.
is the
such
when stored
Traditional
with
written
to
called whether
executed
value
value of f(x1) in BACK2 for x>I.)
transformations
.$?2> defined
global
transformations,
In
one.
BACK2 the
a weaker
under these
placing
Dlaces o; f(x2)
present
applicable the previous
and
procedure of
produce
determine
checking
f
Strategv The
BACKI,
sideeffect
which about
;
variables variables
by
for
[19701, among
;
BACK2
@lcJ The
times.
identified has
reuse we will
begin
o,ther
calls
location value
to
of unbounded
Hewitt 5 that
some
us
amount all
interpretations
calls only references
programming
recursive
BACKI else
allows
recursive and necessary, at
for
knowledge
makes
dynamic
fixed
require
are
Sophisticated
results
begin BACKO
are
inherently
interpretations
to
then
there
a
sufficient
these
which
fix.owx.l
cases be
of storage (Paterson and We show in Section [19711).
are procedure
all, to
Strong
interpretation
f2(x)
not
shown
general,
redundant.) Drocedure
but be
the be
Descent
Conditions We
partition @
subsets, cases, is
values
%
consists
in
the
refer
functions
of
%
in
domain
We shall
domain
.%? .
those
true;
values
the and
the
%
the
of
f c
(In
we consider c(x)=d(x)
very
require
the
m and all and
all
n
existence
such
x in g(gil(x))
that
for
which
and
d
strong in
x
notation
otherwise.)
condition
asserts on f%? for some on the existence for
that
c
we
%’.
Section
requirement
5
that and
d commute
The
Condition
Frontier The
and
%,
and
the toward
S be
a
means
set
of
functions
will
functions.)
The
frontier
requirement
that
for
1)
a(h(x))
2)
if
be all
(and
x
thus
f(h(x))
is
and descent we
%
h(x))
is
calls
The resulting descent L@.
the
for
Fibonacci
descent
by the
same
are roots nodes by
the
for
and
d,
into
merging
graph will Figure
dag
c
tree
a
nodes argument
of identical merging those be called a shows a 13
descent
tree
of
11.
@GJ%Q ,...,, .... >
say as
(b(x, b(c(x), o(c(c(x )):', a(d(c(x)))), b(d(x), / (b)
‘=.
b(c(x), o(c(c(x))),
all
& c
in
dc
d
cd
d
the
computation
,,
Figure of
descent is the h
;=
cc
these of S
o(c(d(x ))), o(d(d(x))))~
b[d(x), o(c(d(x))), o(d(d[x))))
a(d~a
(c)
to @
values Formally,
then
Figure tree
about
with
subtrees.) compressed
more
defined,
defined
is
in
descent
(dag)
to
map
and
f
the
subtrees,
weak (We
(Typically,
in
do
descent
is between
for
a descent
assumptions
two such nodes and we merge the
‘a)
and
cases.
compositions condition
(Any
Figure
descent
functions
depicted
is
where
graph
descent
surprisingly,
‘Ieasierft functions.
comrn’ess
i=O
become
descent
certain
may
compressed
we
if
.$%?.
condition boundary
is
for x
weaker
frontier !Inicett
11
f(5),
correspond
value.
integers
relatively
Not
of
acyclic
which
that
m and n, but of g. The
transformations
that
monotonically let
gi(x)
Figure
we make we
directed
IS hold 2
3
g and d(x).gn(x)
on
If
descent
condition Section
the
%?.)
purpose of the that there is a
assure
is
on
our
a
conventions in
computation
then
descent
and
tree
function.
p
false.
is
these
The
those
the
Ci
progressively
conditions, intricate.
is
as
c(d(x)).d(c(x))
consider
which
This weaker cn(x)=dm(x) that In Section 4 our Cn(x)=dm(x) that
implies on
condition
p
of a function
(The
for
of
12. the
base
cases,
descent 4?. In
condition c(d(x)).d(c(x)) c(d(x))=d(c(x)) not insist
f
two
the
A descent condition relationships on %. In Section
c(x)=gm(x)
iZ?.
of
general,
for I
the
into of
recursive
function of Yn a requirement among the descent for
f
domain
of
to
of
consists
Each
argument (under value
S,
and f(h(x))
12.
=
Descent
f(x).
Tree values,
the in the
trees (a) tree
for”
has (b)
assumption third value
for
every
that for
tree). Tree tree (a).
value
in
that p of tree
the (c)
nodes with
first is
a
labelled result is
and convenient
true (a) second
of with values
for
every
and false levels of
abbreviation
a(h(x)). (This is always the case, for maps @ into %, i.e., when for all h in S.) This condition whenever where
we h
answer the wrong
are
is in a(h(x))
asked
the
S and x is in and be sure
answer
undefined
for
only
if
instance, p(x) implies assures value
when h p(h(x)) us that
of
A 5
f(h(x)), 4
%, we may supply the that we have supplied
we
were
asked
for
an
value.
Descent
Trees
3
and
Compressed
Descent
DaRs 2
the
A descent computation
will
correspond
correspond
case,
be f(x)
call
on
we will
will
must
be
applied
a
f:
to
x
root
and to
will
each
In
that
with
the
calls,
and
the the
Jo Figure 13. A compressed descent dag for the f is the Fibonaoci where f(5), computation of has a (Since each of the nodes function. this is the minimal compressed different value, descent dag for that computation.)
result former
an argument value of descent functions to
obtain
that
I
calls by
nodes
[
node
the
represented
corresponding them with calls.
abbreviate sequence
representing x. Each node
The
identify
of
often only
tree some call,
corresponding call on f
the we will identify of the corresponding
we
a for
original
children from the
indicating
which
a
the
values
sometimes values by
of
Sometimes
argument
will
to to
will have arising node.
tree
value.
145
When
we
descent
have
tree
value
under
the
resulting
dag
for
graph
is
an
for
have
dag dag
is of
nodes
to
Formally,
two
nodes value,
minimal. a computation
The
of
set
of
require functions of
that in
be
a
descent sideeffects.
lie
the
of
characterize
f,
on
The
a
scalar
b,
same
as
a
p
not
of
the which
for x
tuple.
may
be
EXPLICIT
we
for all d may be program
have
not
arise
likely
to
programs, clarity
maximize
no
f
if
from be
or
notion
of
in rather
assignment
The
introduced
an
been a in which to
LISP
auxiliary
in
programs than

original
poor
found
above could have from pure LISP 
language.
(LAMBDA
throughout attempt
an
programmer
function
transformed f is not
regarded
(XY)
(APPEND
to and
called
avoid would
either case,
it
resulting
with
(DISTRIBUTE arguments
It
is
quite
for
and
inefficient
programs
have
he
disposal
(f
(f
has
less
x)) The
clear,
in
write
fact,
clear
but
software
competent
improve
to
(CDR Y.
to
(and, to
to
Y)))
(CDR(X))
been
programmer if
X)
since bound
reasonable
a
his
x
would
desirable) at
(CAR
computation, once and
program
tools
REDUNDANCY
Y
the redundant be evaluated
however.
2.
is
The program translation is
could
no
need
It
Algollike
entails
In the latter be tuplevalued.
will
hold course
transformed
as
do
the
(Formally,
condition set.) Of
redundancy
mechanicallygenerated written to
evaluating
the
value
behavior values
functions
or
we
parameter
or
descent
that
that
but
the those
defined. the
a, guarantee
returns domain
function
throughout
function,
function
as
assumed
without
c in the transformed to do so.
programming.
compressed defined.
efficiency. mechanical
will
valid condition.
frontier the empty
of
Explicit
compressed
minimal is uniquely
there
the
the
frontier
a compressed
the
Assumptions
It
is
a
used in place it is preferable
if
a given
transformation
imposition
call descent
compressed
same
This
a same
we
obeying no
the
assumptions,
minimal
the
of
have
assumptions,
which
dag
descent descent
of
the of
the
shown
interpretation
assumptions descent
be
set
set
all
can
a given
that
there
merged
which
those
programs
mechanically.
Definition
of
An redundancy
for be
constant
example, the
function
is
lists
a
and
atom
onto
of
let c(x) taking
each
let
sets,
represented
be
Definition
= CDR(X). and a list CONSing
( For
and
E?%.
We shall
and
common
definition
loss .
their
~
NULL(x) then
return
else
return APPEND(f(CDR(x))
of this
‘(())
Solution
to We can
f2(x), leaves
Explicit
))
transform
f
into
a
recursive
procedure but
always T, as
follows: procedure u
f2(x) p(x) :=
a(x)
!wi.in The
function
f2(c(x)) f
is
then
;
T
:=
b(x,T,T)
f(x)
a
common We
gn.
Figure
~ m
for
al 1
generator
interPretation
of
c
exhibits assume
without
and n are relatively the we consider case, n/gcd(m,n) in place of without
31
affecting
shows
the
the
structure
tree under these assumptions. present a transformation
Znl+”
@
93m(x)
g
,.
simply:
..
(x)
...,
31.”
nodes,
146
but
,.
The
most
~ zm.n[x)
,,
Descent”
at
9
Wzo(x)
g2m+”(x)
redundancy.
;
f2(x);~T~
nl+n(
9‘+”(X)
Figure
procedure
d(x)=gn(x)
respectively,
the descent section we
gqx]
;
~T
and
of integers
In which
A
Redundanc~
which does not return a result, the value of x in a global variable
positive
.& JQ$i?f
,
DISTRIBUTE(CAR(x),f(CDR(x))
interpretation
and
redundance.
and n g, m, value of gm or
;
g
the
generality that m this is not if m/gcd(m,n), and
&%~,n), f(x)
call
that
generator of .
g
c(x).gm(x)
say
Redundance
a given
function
that
d,
Generator
for
a
such
in
x
that
exist
powersets: ~rocedure
Common
Suppose
the list leads to to
of
there and n
the
example, to
lists,
as
COMMONGENERATORREDUNDANCY
3.
the
list whose = APPEND(Y,
of
sublist.
%.
lists.
a(x)
= d(x) an atom
result
in
LISP
is the b(x,y,z)
let
the
x
‘(()(B)(C D))) is wml C D)). ) This interpretation explicitly redundant
B)(A following
mapping
domain value
NIL ; ,2)) ; function
ex~licit
all
NULL(x);
returning
DISTRIBUTE(’A, ((A)(A the
the
whose is
exhibits for
predicate
element
DISTRIBUTE
J%
c(x)=d(x)
DISTRIBUTE(CAR(X) of
of
if
p(x)
only
Redundance
interpretation
Consider, Let
ExDlicit
kth k
level
distinct
gz”(x)
rn.zn
~m.2n(x
g3n(x)
(x
,,
tree
,,
for”
...
,.’
common
of
the
values.
tree
.,
generator has
Pk1
,.,
common
eliminates that
the
functions The
Fibonacci
holds
lprovided
for
the
Suppose
set
of
g~ven
program
Recall
redundancy. .
x2
x1,
then
and
x
d(x)
that
O2=2,
11.0,
undefined,
Implications set
of
tree Since
cannot
can
a these satisfied.
is
is
in
(ml)(n1)/2
is
of the
in
in
We
the
iehJ, almost
1
to
the
tree
This
f(gkl(x)),
. . . .
appropriate
it
calls
f(x).
The
each
in
is
undefined
left
in
a to
at we
the will
f(gM(gi(x)))
.
computed f(d(gi(x))).
b(gi(x),
side effect BACKg[i], for
n
f(c(gi(x))),
some
assures
f(gi(x))
is set
place
us
an
to =
O
which m and n
BACKg[n])
.
BACKg[O].
The
the
value
of
f(gj(x)) value
is
the
frontier
f(gi(x)).a(x)
or
into
BACKg[i]
and
recursive a value
true,
either
be
a return (If
f(gl+l(x))
shift
numbers
is
may
O
is
f(gi(g(x)))
O
into not
arbitrary
p(x)
that
undefined,
BACKg[i] p(x)
If
f does
j,
array
global values which
of leaving O
When
condition simply
form
transform which
BACKg[j].)
recursively
resulting
chain,
is
a minimum
the
up
then
BACKg[i],
to
place
BACKg[i+l],
b(x,BACKg[m],
b(x,f(c(x)),f(d(x))) program
.
f(x)
in
is:
on
f(x)
shown
tree
in
anti
compressed
the for
Figure
;
f2(x)
procedure
; BAW401
f;?
f2(x)
12S@?2 U p(x)
some f2(x)
a!!
ibegin
of is
;
~
bQ!3Q .l@
then,
value This
32.
descent
by
f(gk(x)),
g(x)
f(x)

gk(x),
compute values
calculates
descent as
form
f(go(x))=f(x) for write a procedure
results,
simple
equivalent the
to
of
f(gl(x)), We shall
itself
previous
of
reasonable
turn,
k.
using
expressions
expression
makes in
also
contains
every
computing,
which
compute
at
numbers.
almost
k@&j .
.
the value the value of
employ a to remember
procedure Thus
can
and f2(x),
the
range.
Appendix
natural
f
= f(gi(x)).
wil
needed later, procedure,
the
the
the
highest number Typically,
(m1)(n1)1.
small
expressed shown
of
corresponding
f(gi+m(x))
BACKg[O:max(m,n)]
a
but.has f(gl(x))
I
we )
value
that
is false. If we look nodes down the chain,
m of
the
the
say
❑
are
f(;?)
is
find
=
points.
and
to
chain,
Similarly, chain is
these,
equal to 01=1,
occurring
be
it
Using
Redundanc7~
expressions
expressed;
be be
all
Generator
number but
so
are
function
condition
Specifically, all
or
the
value
f(d(gi(x)))
in@
of Figure 31 is {gim+jn(x) m and n are relatively prime,
natural
be
The
.
S
x
the
f(c(gi(x))). nodes down
g(x)
in
each
in
and P(gi(x)) computed
find
m=2, n=l, condition
h(x)
to 1 at f(1)
Common
define
h and
undefined values of
1
generator
interpretation we
function
frontier
of
The
that
If
equal and
the
im+jn.
that
12.1.
function, f(0)=l=a(O)
descent
under x1.
each
and
Section
d(x)=gn(x) for The frontier
is either possible
The
in
common
lambda[x;x2]}
a(h(x))
jelhJ1. every
.
for
{lambda[x;x1],
constant Since
that
and
c(x)=gm(x) and in%= {2,3,...}.
requires {0,1}, a(h(x)).
discussed of
we want
node
gl(x), value
example
an
c(x)
will
redundancy,
condition
{gi!O
provides
can
generator
frontier
a
O Q
BACKg[i]
tree
:=
x
dag.
QId
(max(m,n))
:=
a(x)
Q2
;
g(x)
;
return Q@;
x
f2(g(x)) ti i Hg BACKg[i]
;
BACKg[OIl
:=
(max(m,n)) := BACKg[i1]
~
1
~
1 L@
;
b(x,BACKg[m],BACKg[n])
~
Note
that
calculated
g(x)
end of
of
the
the
are nth
value
92(X)
to
behaves are
array
array
values and
BACKg
values and
as
we
ascend
placed
at
93(X)
far
call until,
32. a
The
of
f2(g(x)). for
The some
compresaea
minimal
computation k,
of
value is
for
enough
depth
i
Figure
referenced, or compute results
f2(x)
based
chain
p(gk(x))
descc!nt on
COntinUeS is
a
of in
dag
such
be
true.
147
be
the
that one of
it
form
f(gim+jn(x))
je~} assures (ml)(n1)/2 (Any
value is useless
be
a(gi(x))
the
referenced never
mth a new It
However, we
the
if
the every
calculate
nature
of
for any values
placed must
and to
only migrate
max(m,n),
us that useless
undefined, values.)
end The
calculated far enough
which Thus
needed.
f(gi(x)) these
through
etc. exceeds
low
high
calculate of BACKg.
will be themselves
referenced,
recursion
fact
{im+jnlic~, more than computed.
recursive clownward
to the
that which
will migrate
the
recursion.
pass
order to beginning
the
Newly at
the
the
they
is possible that a $alue placed in BACKg, but never be to
queue: BACKg
towards from
as in
a in
migrate
referenced positions, be
as
placed
in tUrn
x,
no are
BACKg, out
to
ImDroving
the
Solution
to
Common
Generator
Otherwise
Redundancy The
efficiency
improved and
at
n
the
are be
unfolded
a
Thus
the of
in
these
subscripts,
which
will
time
evaluate of
‘!BACKg[i]
:=
~100p
by
~
to
a(gi(x))
i,
we may
a(x)
x:=
;
the
be
that
loops
in
above
there
in
an
replace
is
of
a
is
of
1,
to
without
so
strarts that
REDUNDANCY
Consider which
the
computation
There and
city, switch
k
from
involved
one
right
parallel
of
rush
the
interchanges. highways at
again
until
determine
out
at
DELAY[i,3h] the commuter
since highway
3h The
until
(k+l)th
to
allows
us
that
generality the
at
row of handled
be
DELAY
‘Interchange
solve
the
O!! on
problem
to the
highway
by
calling
of
traffic
it
i,
i+2.)
minimum
amount
interchanges
he
The
we
L7 exist all
do
of
may
time
in
each
highway.
the
highways
This
is
do
exhibits
is
to
have
which
the
of
integers the other array
we
represent
1 and 2 highway
(so that is 3h),
DELAYIO:k+l, needed
to
so
travel
to and
lfinterchange the
k+l!!
following
solution
to
the
pocedure
~
and as
is
the
is i
DELAY[i,h].O
Figure
41.
that
c(d(x))
=
c4(x)
d(c(x))
❑
that
in
the
i and j such and c(d(x)).d(c(x))
common
the
the
genrator
h, the
is
for
a,
then
the
to
the
set
of
c
c
and
and
Problem common
transformation redundancy
condition
{cmdn[o~m
which holds
O~n
program Commuter
for
m+n>O}.
i.O
computes
the
Problem:
;
i~k ~
return
min(DELAY[i,h],DELAY[i,3h]
else
return
min(DELAY[i,h]+f(i+l,h)
) ,
DELAY[i,3h]+ DELAY[i+l
,3h]+
f(i+2,3h) The the on
value
of
city highway
f(i,h) we
h.
If and
interchange, highway takes k+l time
(i.e., required
is
if
are
us the
from
If so
min(DELAY[k+l,h],DELAY[k+l
minimum
then can
time
to
reach
interchange
we are at simply select
interchange
i=k+l, that
)
approaching
i
the last whichever
k to interchange The most quickly. is min(DELAY[k,h] ,
destination) this to do
DELAY[k,3h]). destination,
i.k we
the
now
we
are
already
time
the ,3h])
required .
min(O,O)
at
Figure where
the is = O.
41. f is
the
Impatient
Commuter
is
labelled
with values
at
for
tree
program
the
argument
148
Descent
a that
the
computing Problem pair call.
(i,h)
for
f(o,l),
call the k.4.
solution Each
representing
d
and d
periodic of
a
periodic
%’.
and
Thus
frontier
functions
If
Commuter
present
in
c(x).gm(x)
generalization lie
the
for x
interpretation
that
Impatient
exhibiting
whenever
the
generator. strict
there
ci(x)=dj(x) for all
en(x).dm(x).gmn(x) However, ~.
on
common is a
programs
such
of
whenever
redundancy: g
redundancy.
valid
when
generator
solution
interpretation
that
case
generator on
no
an
redundance
c(d(x))=d(c(x)).gm+n(x)
the and
for
say
numbers are assumed we approach the city, final destination.)
recursive Impatient
f(i,h)
by
we
common a
have
DELAY[i,h] interchanges
between
on highway h, l~i
Then
that
in
observe
Redundance
periodic
certaidy
d(x)=gn(x)
of of
if we are on highway and we initialize
l:2]
k.4,
we
and
Periodic
general,
redundancy Suppose
with when
❑
exhibits integers x in ~,
not
amount given the each pair between
on
In
cross
problem
f(O,l)
surprising
Definition
highways at two is, if a commuter
interchange
d2(x)
enter
to
for
not
The the
in as
a may time all
and time
safely,
switch (That
can reach the city, takes to travel
consecutive
takes
=
tree is
d(x) = redundant,
and f is
.
into
commuter The but
lane
hour
interchange
the
commuter
left it
to
a
other. negligible,.
the
the Since
commuter
problem, Commuter
highways which
is
from
path
Impatient
at to
lane.
lanes permit
consecutive switches
two
highway
exit
the
the
switching
interchanges
time
are
shortest
call
interchanges
in
several
following
shall
we
Problem:
and
of
time and
to highway DELAY[i,h] +
is
is
of
h
:=
“BACKg[i]
This
time i+ 1
loss
minimum
f(o,l).
first
descent
so
row
the
values
on
Oth
we may
,
not
of
on highway we switch
.Tlne function f is, of course, an instance of with x corresponding to the pair . Then 9, P(x) min(DELAY[xl ,X2], (xl~k) , a(x) ❑ DELAY[x;,3_x2]) , min(DELAY[xl ,x2]+Y, b(x,y,z) = c(x) ❑ DELAY[xl, 3x2]+DELAY[x1+I ,3x21+Z),
PERIODIC
into
smaller
stay
the
commuter
a(gi(x)).”
4.
the
the (i+2)th interchange. allows the base cases
uniformly; assume
sequence the
take
constrained
least DELAY
form
amount the
in
equivalent
may Since
will have BACKg by
If
g(x)”
m
will,
assignments.
variables.
independent
Recall
assignments may replace
we
scalar
max(m,n)+l
can
program
sequence
small.
into
references
array constant
this
of clarity. values whose
constants,
practice, be
of expense
we
to the city if we stay minimum time to the city if 3h . The first of these f(i+l,h); the second +DELAY[i+l,3h] + f(i+2,3h),
to node the
Implications
of
Let
e .
commute,
ci
dag
m
n
and
of
in
by
comparable
i
by
lies
adjacent
this
one,
and
a
fact, into
the entire a network
43.
Given
upper it
lies
is
of,
possible
to
corresponding Let
to us
node
in
the the
c(ekcildo) corner of the
to to
that
were not in in B correspond
counterparts
be
further
Figure
x 43,
and
corresponding rectangles
between a certain
there
is nodes
x
and
y
It
to
the
In
other
rectangles
B
rectangles Thus
D, the
rectangles,
i.e.,
if
node
in
certain in
rectangle
connection
in in
%.)
a node
a
be and
compressed into the This compression
44. between
edge
an
C.
dag,
Similarly, coextensive.
connecticms is
in
descent
not
the
y
between
in the
representatives
Figure
of
44.
the rectangle. be
of
upper lower In
compressed in
Figure to
the
D and
result
E,
values
rectangle
labeled
in
42.
shown corner
the ekcildo;
of
left
of thus
Figure
B is
the
A The
one
the node it corresponds
just in
The
node
in
the
uppermost
below
and
to
the
right
in A corresponding to d(ekcodj1)
corresponds
side
rectangles
the
. ekcido. C occurs just
node
may in
would ekcil(x)
if
=
B.
subgraph
uppermost
below and corresponding
dag
ekcido nodes in the C correspond
righthand
shown
the
their
Thus the B and
rectangles
nodes
compressed
shown
.%?. is,
dag
are coextensive. F are mutually
Figure
a
(These
respectively, all the as
on
That rectangles
desc!ent
the
rectangle
subgraph
corresponding
compute
in
form
is
whose
may as
rectangle
assume
represents
upper
to
instance,
values
there
envision
rectangle
rectangles,
for
this
can
original
values
same words,
descent
compressed ekcsd,u(x),
righthand
dag
of
the
= e
c)f
value.
that
and C E, and
form
subgraph
that
whose
descent of
result
sides
the values
lower
the
of
preserves
This
j
dag the
same
ekdjl(x), follows
be
nonneg,ative
of
adjacent
of
m can
corners
the
part
where
descent
We
by
to
expressed as tj+u, integers and u
Notice
i
uppermost
d
be
k.
the
third
side
cmdn(x),
are
of the
rectangle
c and compressed But
s
compressed
to
side
righthand
form
rectangle. j
Since minimal
representation and U
42.
j
side
lefthand
and
a given
Figure
i
r
However, ci = dj = ek+lcOdC1.
ekcOdj
integers.
the
for
in an
the
the subgraph consisting of
O~u
forms
of
where
a unique where s
depicted
.$%?. the
s
Consider dag
O~s
are
ri+s,
cmdn . each value
f(x) has evcsdl.l(x),
on in
f(x)
and and j
t
dj
nonnegative
as
integers where
Redundancy
values
are
expressed
descent
.
all
descent
Then Thus
Periodic
❑
to ekcodjl; ekcodj.
thus
A to of
Figure
it
Each rectangle the configuration
43.
An
abstraction
of
represents of Figure
the
i.j 42.
nodes
descent
dag.
arranged
n
e%”d”
a ~k,fl~].l
Figure dag
42. consisting
The
subgraph of
the
of nodes
a compressed ekcsdu, OQ
descent Osu
44.
Figure dag
149
by
merging
The
result identical
of
compressing rectangles.
the
descent
in
Observe an
uppermost
the
form
that ~kcsdu
each
ekcodo
position
There the
is a descent
for s
in
Figure
43
has
to
a value
of
other
node
corresponding some
k.
Every
corresponds
to
a value
and u are uniquely of the node within
linear dag
ordering of
various
rectangles’
the
compressed
that
rectangle
corner
rectangle , where
the
the
that
among
Figure
the
44,
values descent
rectangle.
dag
of
Figure
44
BACKC[M]) :=
P(ARGUMENT[M,N]) ~
Periodic
;
XL?QLOS!Q
BACKd[N]
is
~ to
)])
~Nm(j2)sJwl
minimal. Solution
O.dQ
BACKd[(j1)],
in
corresponding to It follows k.
for
until
BACKd[(.i1)] := ~ p(iRGUMENT[M, (j1)]) ~ a(ARGUMENTIM,(jl ~ b(ARGUMENT[M,(j1)],
form by
rectangles
a1
!E@l
in
of the determined the
&M~(il)
a(ARGUMENT[M,N])
~
Redundance
b(ARGUMENT[M,N], BACKd[N],
These method
observations
for
computing
redundancy: RESULTIO:il,O:jl].
We
procedure
with
f2
f2(x)
will
RESULT[S,U], can
be
done
to
the
O@
O~u
Given
these
If
values values
to
the
to on of
for
can
the
(+i) and res~ectively~:
(+1)
frontier
to
;
BACKd[O]
the value expressed
M and
We omit ARGUMENT: modify the second
of in
in N,
an
amount
we maY
eliminate
first
“~
the by
cMdN(x) can, an explicit
replacing
M“
for form
of
time
the
arraY
looP
assignments
and of
the
form BACKd[n] U
the
rectangle
if be
evaluated
of
dag.
RESULT
represent
be
can
independent
e(x) = ci(x) initializing for the next
current
In addition, M and N,
which
this
descent
set
each
a(csdu(x)).
compressed we
a on in
true
is
:=
m
array, f into call
a
appropriate
the
BACKC[M]
without
f(csdu(x))
p(x)
f2 recursively the effect
values
and
of
BACKd[N+l])
fOllOWing f(x)
global
according
call has
(where mod j,
a
RESULT[S,U]
in
appropriate
the of
transform that
value
setting
the
rectangle
to
We will property the
we which
lower
folllows mod i
value
maintain
leave
by
RESULT
us
the
immediately,
condition, Otherwise, = dJ(x),
will
lead
:=
p(ARGUMENT[M,n])
as
~
a(ARGUMENT[M,n]
~
b(ARGUMENT[M,n],
) ...,
..)
addition by:
&KQ arrav
ARGUMENTIO:
~M~
(il),O:(j1)]
O until
12e&irl ARGUMENTIM,O] U
ii.O
x
else :=
,01)
c(AFiGUMENT[M(j1)
(j1)
RESULT[M,N]
~l
OQQ O@
until
the
is
f2(x)
begin ~M~O BACKC[M]
interchangeable, of i and j.)
of
N],
until
~N~
simply
by
and
d
interchanging
the
are
form O until
Solution
to
less
clear
A slightly uses
i+j
instead that
locations of
the only
the
important the
i.j
either
second
“~
to
M“
zero of result. BACKc[O:i1]
RESULT
and
the
second
In
are
to
of
it
will
the
improved
‘!=
this
form
M“
loop
represent the will
RESULT ; represent version is
rewritten
each
row
just the of
after kth the as
cOdN(x)))
;
form
a(T) b(T,BACKd[(.jl
U
of
BACKC[M]
150
)],BACKc[M]
form
for
)
;
cMdN(x))
a(T) b(T,BACKd[N],BACKd[N+l
Q@;
follows:
;
;
P(T) ~ ~
of the
algorithm,
cMd(jl)(x))
p(T)
T := (explicit BACKd[N] :=
that row of
for
:=
~ ~
global zero of
M=k iteration represent
(explicit
~
results,
RESULT . Observe RESULT which are or the end of those in row zero and introduce column
for
QQ!.Z.LOJ2Q
BACKd[(j1)]
program
intermediate
We will to represent
(Just before BACKd
row
(k(+i)l)th iteration, RESULT.)
loop
BACKd[O:j1] turn. below,
store
of
;
;
l!!2Q?2 T :=
Redundancy
version
cMdO(x))) @
m;
roles
locations in locations in at the beginning
column vectors
RESULT in the loop
Periodic
for (j1)
return f2(e(x))
the
ti
:=
~M~(i1)~l ImDrovin!2
(il)
:=
a((explicit
l])
c
QQQ
;
a((explicit roles
final
p(x) then
a(ARGUMENT[M,N]) b(ARGUMENT[M,N],
the
the
form:
BACKCIO]
BACKd[N]
course
possible,
following
return
;
gncJ (Of
)...)
m U
RESULTIM,n(+,i)
; ~b(T,...
;
fp(x)
procedure
:=
RESULTIM(+i)l,
cMdn(x))
~a(T)
improvement on
f(x)
12QQQ
P~ARGUMENT[M,N]) m ~
this
takes
Drocedure 1)
;
for
fiP(T)
;
@
d(ARGUMENT[M,N
form
:=
When
ARGUMENT[M,N]
~
(explicit
result
1 until
~Mfrom(i1)~1~ ~N~
:.
BACKd[n]
L@
:=
then
~N~
T
;
(i1)
:=
BACKd[O]
1)
(Again,
the
primarily replaced
use of a notational
by
a
sequence
and
variables, form for sequence.)
~loops convenience.
we
each
of
arrays Loops
have
a different
c“dn(x)
procedure
is be
can
to
assignments
may
value
and
~
scalar explicit
occurring
‘n
‘hat
This
is
an
f(n,k)
k=O
QL
;
k=n
then
return
1
else
return
f(nl,kl
instance
of
f?
)+f(nl,k)
with
x=,
(X70 ~ X2=X1 ) ! a(x) C() = , =
. COMMUTATIVE
5.
Since c and
, in IN,
REDUNDANCY
establish all Definition
of
Commutative
Finally, which d(c(x))
the
we
only for all
periodic
consider
descent x in
compressed condition. commutative frontier
will 5l(a)
{Cidj
turned
upward Formally,
and
and
min{k
be
all
x
on
in
its
for
rightward min{k I
from p(ck(d(x)))}
this
p(dk(c(x)))l
x
the
binomial range,
case the the legal (in which either
coefficient n~O
of
on dag
and
of
definition
n
and
k,
the has
Paterson
II.
this for
is
the
(in it
to
binomial
is
of
the binomial defined for
the
needed have
is
1 if xlij otherwise)
~,
,..
in
commutative to store
we
in
will,
from
general,
the
descent
original
tree
is
only
for
~, and
that
i+~n,
for
along with the descent dags, interpretations
oXo
(~a(c(xo)),
./2$ f(xol=b(~,
o(d(xt)
)))
b(c(xo),
o(c2(xo)),0
(cd(xo))).
b(d(%)).
o(d2(xo))))
Xo
Xo
:
C41 &!C)&
,;(Y2
*~9’(0’” c!om~essed degcent In (b), the counterclockwise
redundancy. rotated 45o of
the
frontier
dag
C2[XO)
dag of tO
Figure
cd (Xo)
52.
cd (x,J
The
value
interpretations J%, and minimal compressed each interpretation.
whenever
holds.
151
C(XJ
d(x~
C(xol
structure
which that
&&
. . . . . .
condition
For
@2.
oXo
f(xo)=b
(b)
frontier
the
dag.
false if
of f(xo), compressed 5.2
with
descent
p(x) and
Figure
are
= a (XJ
.!.,1
...
if
that
eliminate locations
interpretation the assumptions
The value and minimal
and
shows
dynamically.
a
the for
computed partial
redundant.
compressed
true
trees
proof
differs
whenever is XO.
ce~: f(x,J
to
replaces
consider except
displayed
$(,
are
proof it
be for
interpretations
storage
minimal we
their
results,
that
cannot locations
the
global
that
adapted
in
descent is
the
the or
that for the schema
for
commutatively
we
allocate
constant
been the
or
x2.x1,
undefined).
showed f
of
using
c(d(x))=d(c(x))
(a) has illustrate
(in
Redundance
if
be
temporary to
P(Cidj(xO))
minimal
to
x2j=O
if
interpretations
if by
each n, is free
The 51. commutative
To for
which case 1 or undefined)
:Hewitt[1970]
even
corresponding
Figure for
and
equal
either is
= = j
that
either
undefined);
either
adaptation
restricted
(o)
c2d2(x) .<.
is
the function than n storage
true
The
c2(x)c2d(x) :: . .
it
Commutative
and
An
results.
only
dz(x) cd2(x)
is then
case
exist
fewer
Oikin:
d(x) C?X) cd(x)
or x2j
coefficient and undefined
which
of
each n there g for which
\ p(ck(x))}
..
x2.x1,
x2j=xlij is
(in
using
redundancy
n!/k!(nk)!,
x2=0 x2=0,
case
coefficient
Implications
the
min{klp(dk(x))},
commutative
recursive
cond~tion,
. we show
and If
Consequently,
example
if
which is in
redundancy An
~, xlij
x2j>xlij
J%.
following
in
= Y+Z,
d() = d(c()) %?. For i
case,
i.e.,
I
j
on
P()
b(x,y,z)
the
monotonically to point
~min{k
and
binomial
functions is
i
coefficient of 1 or undefined.
x2j
descent eliminating
takes (where the
S’proceeds point
of
minimal
of
clarity),
for
and
❑
whenever
set
computation 5l(b)
side
this
valid
the
Whenever
~
~ the
for
will
holds
between
I
called shows
redundancy
ie~, jeiN}. the dag for the entire in Figure shown shape frontier
be
under
~
been
is that c(d(x)) generalization
dag descent Our transformation condition
for
interpretations
condition .%. This
redundancy Figure
redundance.
Redundance
and
c(d()) d commute
c~d.d() the frontier
1,
❑
C2(XO)
dz(xo)
of
f(xo)
under
d(xo)
de(xo)
cd(x.J
each
of
@l, $2” ‘he ‘esCent descent dag are shown
the tree for
We will
show
under storage
interpretation locations.
involves a leaf
either of the
the
node
ing
the
and
placing
call
the
to
a
Each
that
to are
shaped like @n, becomes
Of f(xo) location
dag
for
(Intuitively,
@n
See of
in
leaving use at
map
this
operations
@n
described the
for
dag
to must
the
dag
the
leftmost
this
node
to
be
plays we
Jln1, there become closed the moment
a
in
the
no
have
edge
of the
of
on
some
level
location;
to
be
by
is,
@n
required by the minimum
at
the
~n at number
moment
the
the
the of dag
the the
Figure first
(The
f of
A mapping is
mapped
:=
i+l
by
the

;
to of
is
of
the
the
in
global
array,
values
and
compute
as
row
of
place execute
leaves When
O~i~n. is
done O
recursively 
so
We
row.
1,
with
which
O~i
ARGUMENT
that will
which
true), this in BACKd[i], guarantees that f2
drawn BACKd,
is
call
=
ARGUMENT, that
ddepth(x)
BACKd[i],
array
dag
procedure f2(x,n) in BACKd[i],
we
c(x)
if
array, a
in
it
will
to
global
values
di(x), O~i
for
think
let
d,
ddepth without be computed.
to
result
into a f(di(x))
in
locations
function is
condition
f(di(C(X))) of
to
p and
instance, ddepth(x)
redundancy
Another
Otherwise,
the
commutative ddepth
and
frontier
set
of
places We
that
then
ARGUMENT[i]
a(di(x)) the 100P
in
.
BACKd[i],
tO
~
at of
i
~
BACKd[i]
ddepth(x)l :=
xl
~
O I@
b(ARGUMENT[i], BACKd[i], BACKd[i+l]),
of
becomes
—
Dccj
from to
is true. f(x) is
computed
(For X.SIN,
and
is O (i.e., ,P(x) placing a(dl(x))
arguments
Since
moment locations
i
forc$fn
53. dag
smallest
be
interpretation
argument
value
valid.)
of
‘nl less number
;
will use the how that function
dag.
n
Dug
the
Q
directly. “x=O,”
is
5l(b),
ddepth(x) simply by
value
dag
p(x)
the
solution
Figure
hold
otherwise
is a way for the dag for using one storage location of closure than the minimum
locations That
;
array
We
represent
dag
sequence
on
a global
rotated
just for
open. the
close
ddepth(x)
solution
transform
the
one
created
be
p(dn(X)) whenever
i
Our
The
for
mapping However,
the
to
d(x)
x.) use
in
a
operations
least
in
x
always
not
be computable = x1, p(x)
dag
dag
of
depending
specifying
operations
level
would
exhibit
o;
may d(x)
‘f(x).
locations We can
at
role
kth
the
closed,
dag
or,
consider
deleting
storage
storage,
&,
~n1 , creates a graph by cutting off
storage is closed.
node
of
such that has a value can
x:= return
(il)th
node
first
in
the for
closes
any
of
Redundance
value
l?egi?l i := while
for
the
the operations. be
the
path
operations
required
applying other
dag
on
Now
by on
n
]rocedure
dag,
location
node
sequence
@nl, node
@n
corresponding
storage closure.
for
ficst
and to all
a
amount
for
exist no way
interpretations
ddepth
This
computation
mapped to the dag
which
minimum
program
the following of the dag for
ith
possible the dag
to
dag
the
for
is of
defined.
and
open
storage
53.)
the fewest the moment
the
involving
@n level
Figure
sequence
closing
The
the number ddepth(x)
one storage of f(xo)
this mapping @nl as the dag for the upper lefthand
same shape on nodes
for e%n. sequence
the
one
fixed
Commutative
Define
storage
an
descent
Consider a subgraph
LZnl: for
moment
least
a
these
the
that there which there is
location).
requires computation
. from
to
natural Clearly,
value
n+ 1
that
than
redundancy.
Solution
any
oPen the
storage
compressed
at .%1
of the dag the (kl)th
l
the
more
established ~ for
using
though
commutative
closed.
dag
that
observing
mapping the
on
at
requires under
isomorphic level node
required
by
a
f
calculate
one
have for
root in
is
(with
in
to
cor
held
the
show
under @O trivially and proving that
than to
root
least
we
even
We the
whose
dag
with
to
this
$n
under more
the
nodes
currently
closed
the minimal closed.
We do
from
path
the
at
node,
a of
is
Thus @n 1. interpretations
colnput
interior location.
not
dag
suffices
locations
@n
only
begins
corresponding
that
is
are
the
n+l
computation
storage
Otherwise
with it
a
there
values
computation
Thus
in
if
the
closed
f(xo)
least
locations, to
value
location.
The
of
storage
consisting
responding
concludes
step
at
the value corresponding to storage location, or taking to children of a given
two
open
of
requires
corresponding
dag
storage
computation
@n
from
value
leaf
the
placing dag in a corresponding
values
interior
that
the
the (il)th
dag
for node
@n to the dag on the (kl)th
for $nl level of
152
The ith node on the second dag.
the
kth
level
of
the
result in cdepth(x)
which leaves f(di(x)) in BACKd[i], O~i@. (At the beginning of the i.k passage through the loop, BACKd[j] . f(dj(x)), k<.@, and BACKd[j] . f(dj(c(x))) = f(cdj(x)), O~~k. Since k < ddepth(x), p(x) is false, so b(ARGUMENT[i], BACKd[i],BACKd[i+l ])=b(dk(x) ,f(cdk(x)),f(dk+l (x)) = b(dk(x),f(c(dk(x))),f(d(dk(x) ))) = f(dk(x)). Thus , after the i=k passage through the loop, BACKd[k] holds f(dk(x)). The
entire
program
procedure
f(x)
!Z2&LiQ D :=
looks
program ddepth(x).
if
If the value of’ a(x) is the same for all x in 93, then BACKd cam be initialized with every element equal to that constant Then the value. two loops which set elements of BACKd to a(x) for some x can be eliminated. (This is legitimate because the array elements which are set by these loops are not referenced by the program before the loops are encountered.)
this:
;
BACKd[O:D], ; BACKd[O]
procedure
ARGUMENTIO:D]
6.
;
f2(x,n) p(x)
;
~
w ~i~Ountiln@2 w BACKd[i] X := d(x) Q@; return
:=
d; rir := ddepth(x) ; f2(c(x),m1) ; ~ i from O until l&&QL ARGUMENT[i] X := d(x)
:.
a(x)
;
The use of global variables is only one of a number of implementations which could have been used. An obvious alternative would be to rewrite f2 to return a tuple consisting of those results instead of leaving which will be needed later, those results in global variables. (There would between be an obvious onetoone correspondence elements of the tuple and elements of the global mathematically This seems arrays.) approach cleaner than the use of global variables, but it was felt that the use of global variables would be more familiar to being more most readers, consistent with programming languages such as PL/I and Algol 60.
n @ x
;
&;
i from m until n & BACKd[i] :. a(ARGUMENT[i]) ~i~ml~l until BACKd[i] :. b(ARGUMENT[i], BACKd[i], BACKd[i+l])
CONCLUSIONS
We have presented a recursion schema, ~, defining a function, f, together with various sets of assumptions under which the definition of f calls for redundant computation. For each set of assumptions we have investigated the nature of the redundancy, recursive program, and presented a using the predicate and function signs of J?, which computes f without redundant recursive calls. As expected, we found that weaker assumptions require more intricate computations. We also found that weaker sets of assumptions required greater amounts of global storage.
;
M U
spaceefficient smaller than
;
ddepth(x)
&2&.! ~. f2(x,D) return
like
a more is usually
~
: 0~
~ RemovinQ
is ImDroving
the
Solution
to
Commutative
Redunda~
Each the
Recursis
of the general
~rocedure U p(x) ~ else
As before, this program can be improved in many cases: If di(x) can be computed in an amount of time independent of i (e.g. when d(x).x1, di(x)=xi), then ARGUMENT can be eliminated. If di(x) nor ddepth(x) can be computed neither directly, then time would be saved by combining the initialization of ARGUMENT with the calculation of ddepth. However, this would be expensive in terms of space, since we would then have to make ARGUMENT local to save it across recursive calls on f2. Of course the roles of interchanged if appropriate throughout program. the b(ARGUMENT[i],BACKd[i],BACKd[i+l b(ARGUMENT[i],BACKc[i+l],BACKc[i] would be replaced by cdepth.)
of
the
versions fcjrm f2(x) A(x) begin
of
f2
earlier
presented
;
f2(h(x
)
;
B(x)
~,
which case h maps where x may be a tuple (in tuples to tuples) and A and B are procedures global executed for their side effects on References to the global variables may variables. (The version of f2 occur free in A or B. developed for commutative redundancy is not quite remedied by in this form, but this is easily moving the assignment to m to just after the computing recursive call, at the cost of ddepth(x) twice.) Using the “direct” method of Bird [1977a], We recursion removal described by can rewrite f2 nonrecursively by using a stack S to maintain an itinerary of values on which B must still be executed,, The resulting version of f2 preserves the order of the calls on A and B:
c and d can be changes are made (For instance, ]) would become ), and ddepth Such changes would
153
procedure
f2(x)
&lX&l stack S := while
In
;
S ; emptystack not p(x)
; do — <= x ; x
&@QS A(x) ; while not
empty(S)
x<=S;
:=
h(x)
~
;
& B(X)@
established that the case, we have of f2 do not nitie their space versions The number of global in the stack. by each program is a fair measure used Interprogramst requirements. space of q displaying explicit, common or periodic redundancy can be computed storage; commutative fixed amount of really has a higher space complexity.
any
recursive complexity locations of these pretations genrator, using a redundancy
Q@ Generalization This program may be improved. As suggested by Darlington and Burstall [1976], recursion can be eliminated without the use of a stack if there is a single recursive call whose descent function is invertible; the resulting program can be made more efficient when there is a unique base even case. When h has an inverse, x may be set to hl(x) directly instead of by popping a stack as above. The resulting program: lrocedure f2(x) ; !2!2@2 Y :=x; while not p(y) A(y) ; a y+x @
~
Y :=
hl(y)
WY:=
h(y)
; B(Y)
Drocedure
h the
has no inverse, stack by using
f2(x)
12f@l Y :=x ; n := o; while not Y:= A(y) ; fii~n1~luntil 12Qk3LQ Y:=x; @C j B(Y) &
n>2
for 9, provided that all of the ci are equal on into k
;
~
(This assumes that neither A(Y) nor B(Y) modifies If this is not the case, we can replace “A(y)” Y. or IfB(y)” by ‘Iz := y ; B(z),t! by “Z := y ; A(z)t! as necessary.) If there is a unique base case X. the such that p(x) is true if and only if X=xo? initial assignment and whileloop may be replaced by the single assignment y :. Xo. if
9,,
present eliminating The methods we for redundancy in 9’=92 can easily be extended to In general we will require that yn, n=3, 4, . . . . the descent conditions hold for all of the descent Explict redundancy can functions simultaneously. the same way as for Pn in exactly be eliminated
a
Even eliminate follows:
to
to one such oblong couldbe stored in a il x i2 x .. . improvement applying an x in array, or, by similar to that presented in Section 4, by n arrays, each of n1 dimensions and representing a The the ndimensional oblong. crosssection of redundancy for P can solution for commutative We require that all the also be extended to ~n. descent functions be pairwise commutative on L%, and we use an (nl)dimensional array BACK whose ith length in the dimension is set to The procedure f2 takes on 2
we can always a counter, as
;
procedure
f2(x,y2,
. . ..yn)
p(x) then begin Set
BACK[i2,.
;
u p(y) h(y)
Q
@ ; n
:=
n+l
E@ O@
from
1 until
i @
y
:.
a(c212. o
,.,in]
..Cnln(x)), . . ..O~i&n
to ;
d;
;
f2(cl(x),c2depth(x), . to Set BACK[i2, ..., in] all such (i22c2depth(x) QE . . . . @ (O~i2fi2 m
gnJ Although this increases from some value n to
the number of calls on h (n2n)/2, it is a reasonable quiokly, so computed very approach if h can be that most of the time is still spent in the if there computation of A and B. In particular, is a way to compute the value of hi(x) in an amount of time independent of i, the body of the outer ~loop can be replaced by the equivalent of !tB(hl(x)) .!! Even if none of these conditions holds, we can compute f2 with a fixed amount of storage in time proportional to nl+(l/k), where k is an arbitrary integer, using the construction of Chandra [1973]. The amount of space required is O(k).
‘e~~;;;;t;~~i~~~
. .,cn~depth(~)) ; a(cz=z.. .cn=n(x)) that . . J2C in~cndepth(x)) ~ O~infin) ;
for
~JY,in],...,
BACK[i2, . . ..in_l.in+ll) for all such that ,. ..,O~ificdepth(x)x) 0~i2ic2depth(x) in an order which guarantees that BACK[i2, . . ..+l.l, in],in] is always earlier than &ACK[i2,..., ij, in],in],
, set 2~jin
@@ four For each of the condition frontier an obvious way.
154
for
kinds ~2
of can
redundancy, be generalized
the in
functions to some value is less than nk for all Clearly, sufficiently high k. whenever this condition holds, there is going to be a duplication of ar’gument values on the kth level of the descent tree. In fact, a sufficient condition for redundancy is that the number of values obtainable by k gr fewer applications of the n descent functions is less than (nk+l1)/(n1) for sufficiently high k. This guarantees that there is a duplication of argument values somewhere in the first k levels of the descent tree.
We have not addressed the question of’ what we can do when a particular form of redundancy exists among certain subsets of the descent functions in pn, but not among all of them simultaneously. Additional problems arise when we consider the possibility of different subsets of the descent functions sharing different forms of redundancy, or the possibility that only a proper subset of the descent functions are in any way redwdant. Neither have we discussed the more ,general any n) where situation (not an instance of &’n for the number of recursive calls invoked by tlhe call f(x) depends on x. The dynamic programming examples of Aho, Hopcroft, and Unman [19’74] are of this form.
One example of redundancy commutativity is a recursive program the solution to the Tower of Hanoi string. The program is:
not due to which returns Problem as a
ADDliCakiOnS
procedure ~ n.O then else
The program transformations presented here can be applied to actual progranrs. More importantly, they can be incorporated in automatic program transformation systems. One such system is described by Burstall and Darlington [1977]. That system uses heuristic methods to spply a series of simple but powerful transformatic>ns to recursively defined functions. One of the most effective transformations, however, the definition of new functions in terms of old ones, has not yet been automated. Among the examples of definition presented by Burstall and Darlington is the definition of a function which returns a tuple consisting of the nth & (n.l)th elements of the Fibonacci series, by calling itself recursively to obtain the values of the (nl)th and (n2)th elements of the series. But that is exactly the program that result by applyirlg our would transformation for common generator redundancy to the usual recursive Fibonacci program (provided that we used tuplevalued return values instead of global variables). The methods presented in this paper provide both a way to recognize a particular situation in which a definition would be useful, and a way to formulate the definition itself.
HANOI(n,from,to,using) return return
;
~ HANOI(nl,from,using,to) move(from,to) II HANOI(nl,using ,to,from)
!!
This
is an with x = instance of 9’, , ❑ (x1=O), a(x) ❑ e, p(x) b(x,y,z) = ❑ c(x) y! lmove(x2,x3){lz, , and d(x) = . Observe that c and d do not commute. (If they periodic did, this would be an example of ❑ . redundancy, since c2(x) = d2(x) On each level of the descent tree n is constant, and there are only six possible permutations of from, to, and using, so there can be no more than six distinct values at each level of the descent tree. (In fact, there are at most three distinct three of values at each level of the tree, since the permutations can only result from an odd number of applications of c and d, and the other three only from an even number.) The descent tree typical and minimal compressed descent dag for a computation of HANOI are shown in Figure 61.
Effective procedures exist to find true !!blocksn of straightdescent conditions, given line code which compute descent functions. Let Ul,... ,uq and Vi,... ,vr be represent the block obtained by and then using its output valus block Lewis
b2; and let bn=bbnl [1977] proves that
th~se values u~+l.. .uq+q VIJ1 . .. VrJr. A General
it
of it, . . ..iqand the computes
Definition
blocks; executing as input
for n>l, is possible
let blb2 block bl values to with to
bl=b. compute which jl, . . ..jr for same function as
5,0,b,c
4, c,b,a
4, o,c,b 3, b.c,o ..+
d
c
d c
of
d
c
3,c, o,b
3,0,b, c
*c= l, b,c,a  .
Each form of redundancy we have discussed has involved descent functions which commute. Redundant calls arise because arguments are just repeated applications of the same two (or n) descent functions, in differing but irrelevant orders. (In fact, each minimal compressed descent dag we have discussed is a compression of the minimal compressed descent dag for commutative redundancy.) This is not meant to imply that the descent functions must commute in order .For an interpretation or ~n to define a redundant program. To the contrary, redundancy exists whenever the maximum number of distinct values obtainable by k applications of the descent
s~
d
c
d
c 0,o, c,b
2, b,o,c
2,c,b,o
2,a, c,b
Redundancy
I,o, b,c
d
c
d
d
c O,C,b,a
. .*
d
c
d
c
d
I,c,a,b
~
+*
d
C* O,b,a,c
tree minimal The descent and Figure 61. call for dag the descent compressed HANOI.(5,a,b,c). In the tree, left sons represent right represent sons and of c applications applications of d. In the dag, edges are labelled they descent functions the according to represent. Edges coming in from the right edge of the page are continuations of edges leaving the leftmost node of the previous level, and visa versa; the dag does not get any wider after the third level.
155
As this example demonstrates, our taxonomy of redunancy is far from complete. The characterization of other forms of redundancy, and the formulation of program transformations applicable to those forms of redundancy, are topics for future research. It may be possible to formulate a general theory of redundancy which encompasses all these forms. (The characterization of redundancy in terms of the number of distinct values obtainable by k or fewer applications of the descent functions would be a typical component of such a theory.)
APPENDIX. RELATIVELY
THE SET miN+n~. PRIME
Proof
1. The largest Sm,n 2. natural
is
natural
not
there are not included
exactly in .$m,n.
of
the
First
included
Let
(ml)(n1)/2
which follows
of
Sm,n
Result
is
itself
a member
of
k .
(m1)(n1)1
= innmn.
We have
shown
that k is the highest integer not expressible in suppose i, 04iSk, is expressible the form am+bn. Then ki cannot be, for if ki=a’m+b’n, as am+bn. . (a+a’)m+ then k . i+(ki) = (am+bn)+(a’m+b’n) suppose that i cannot be (b+b’)n. Conversely, BY “ the lemma, i can be am+bn. expressed as expressed either as ambn, with aO, or as In the first case, ki = bnam, with bO. = (nal )m+(bl)n, where na1~0 (mnmn)(ambn ) case, ki = second the and bl~O; in (mnmn)(bnam) = (al)m+(mbl)n) where al~O and mb120,
It “ wellknown that the set {Om,lm, . . ..~.l)m} s Sm,n contains exactly one representative of each congruence class mod n. In fact, each of the n numbers in that set is the smallest member of its congruence class which is in Sm n. Once a congruence class is “introduced” 9 n numbers, all larger into Sm,n by one of these numbers in that congruence class may be generated by adding appropriate multiples of n. Clearly, any number generated by adding a multiple of n to a member
second elegant makes
By Euclid’s algorithm, any integer can be Proof —. expressed in at least one of the four forms am+bn, By hypothesis, x cannot be am+bn ambn, ambn, Since x>O (x is in in the first form. expressed ~ and not zero, Since O=Om+On), x T~nO~ be is fourth form. the expressed in Assume ambn or bnam. expressible either as Then x . ambn ❑ and let q.floor(a/n). x=ambn (aqn+qn)mbn = (aqn)m+qnmbn . (aqn)m(bqm)n. Since x cannot Since aqn = (a mod n)! OiaqnO. i.e., negative, lx! x=bnam, the proof is analogous.
(Note that if m and n are relatively prime, all integers are expressible in the form am+bn, where a and b are integers. Sm,n is the subset of the integers obtained by restricting a and b to be nonnegative.)
Proof
Result
a>O.
(m1)(n111.
In all, numbers
Second
Let m and n be relatively prime positive Lemma —. Any x in ~ not expressible in the form integers. am+bn, ae~, belhl, is expressible either as ambn, with ()~aO, or as bnam, With O
m AND n
number
the
In place of our original proof of the result, we present a much shorter and more proof by Christos Papadimitriou. The proof use of the following lemma:
We will prove the following results about the set Sm,n ❑ {am+bn ! m and n are relatvely prime positive integers, aclt4, belN}:
in
of
am+bn o~ki~k
Thus if
for OSiSk, i is expressible in the form Furthermore, if ki is not. and only establishes a This whenever OLiLk.
onetoone correspondence between those members are in Sm n and those which {0,1,. ... k} which It follows that exactiy half, or (k+l)/2, not. Since k is these integers are in Sm,n. in Sm n, there exist in highest integer not (k+l)/2 . (ml)(n1)/2 eleme~ts of ~ which not in Sm,n.
Sm,n.
(nl)m is the last number in Sm n a new congruence class. it numbers greater than (nl)m are Furthermore, the n1 integers preceding (nl)m belong to congruence
of are of the all are
The number introduces that all
in Sm,n. immediately classes introduced into Sm,n, and therefore belong to Sm,n themselves. The number preceding these nl integers, however, belongs to the mod n congruence class which is not introduced until (nl)m, so it cannot be a member of Sm n. This number, (n1)m(n1)1 = (ml)(n1)1, ‘is therefore the highest number not in Sm n, establishing the first result. (A similar prhof of this result appears in a different context in Rangel [1974].)
is grateful to The author Acknowledgements. Thomas Cheatham, Harry Lewis, and Professors Christos Papadimitriou for their careful reading of the manuscript and many helpful suggestions.
156
REFERENCES
Aho ,
Hopcroft, J.E., and [:i;ii. The Design and Analvsis AddisonWesley, Algorithms, Massachusetts
Bird,
R.S. elimination, 434439
Bird,
[1977al. ~
Notes &
R.S. [1977b]. Improving recursion, introduction of (November), pp. 856863
Burstall, R.M., transformation recursive (January), Chandra, A.K. linear Record ~ Switchin~&
Darlington, system programs, ~ PP. 4467
Unman, J.D. of Computer Reading,
recursion (June), pp.
on 6
No.
programs by the CACM 20, No. 11
and
J. for ~,
[19771. developing No.
A 1
[1973]. Efficient compilation Of recursive programs, Conference 14th Annual SvM Dosium g .— Automata Theorv, pp. 1625
Darlington, J., and Burstall, R.M. [1976]. A system which automatically improves programs, &&@ Informatica ~, No. 1, pp. 4160 Lewis,
ERRATUM
H.R. [1977], A new decidable problem, with applications, Proceedings, IEEE 18th .A—— SvmQosium Foundations QQ ~ ComDuter Science, pp. 6273
the
In the sequence
solution
Paterson, M.S,, Comparative ~ Conference parallel
and Hewitt, C.E. [19701. schematology, Record of Pro.iect ~ Concurrent Svstems ~ Commutation, pp. llg127
H.R. Translating [19711. equations into flow charts, (June), PP. 254285
commutative
Mi&l
ARGUMENT[i] x := d(x)
:= x
;
Q@;
&
Rangel, J.L. [1974]. The equivalence problem for regular expressions over one letter is elementary, Conference Record. ~ti Annual SvmDosium ~ Switching and Aut~mata Theorv, pp. 2427 Strong,
to
should
i from BACKd[i]
be replaced ~i
m until n @ :. a(ARGUMENT[i]) by:
from
O until
max(m,n)
L@.
&Ya!l
recursion JCSS ~, INo. 3
ARGUMENT[i] := x ; x := d(x) Q@; i ~o~m until max(m,n) @ BACKd[i] := a(ARGUMENT[i 1)
,
Without this correction, a problem arises for some x: In ddepth(c(x) ).ddepth(x) with arguments c(x) invocation of f2 be set to n+l, and the ddepth(x)  1, m will of the second ~loop will not be executed Consequently, the first time that the once. of the loop ~
is
i ~m, m1 * BACKd[i] := b(ARGUMENT[i ], BACKd[i], BACKd[i+l])
executed,
undefined. one element, base rule in
157
BACKd[i+l
The correction BACKd[m], is each invocation
1
until
O@
].BACKd[m]
assures initialized of f2.
when the and body even body
will
that
at using
be
least the