Copyright © 2003 Published by Elsevier Science (USA).
Analysing the implicit complexity of programs
Available online 25 February 2003.
Abstract
We study termination proofs in order to (i) determine computational complexity of programs and (ii) generate efficient programs from the complexity analysis. For this, we construct a termination ordering, called light multiset path ordering (LMPO), which is a restriction of the multiset path ordering. We establish that the class of first order functional programs on lists which is terminating by LMPO characterises exactly the functions computable in polynomial time.
Article Outline
- 1. Introduction
- 2. Programming with rewrite rules
- 3. Light multiset path ordering
- 4. Characterisation of ptime
- 5. Bounding normal form size
- 5.1. A polynomial interpretation of terms
- 5.2. Properties of Fk
- 5.3. Bounding theorem
- 5.4. Quasi-interpretation of derivations
- 6. LMPO programs are ptime computable
- References
1. Introduction
What is the complexity of a function computed by a program? How can an efficient program which computes the same function be extracted? We shall attempt to answer to those questions. We consider first order functional programs whose data structure are lists. The result is a syntactic delineation of a significative class of programs which defines exactly all functions running in polynomial time. It is difficult to ascribe a precise meaning to “significative” because we are dealing with intentional properties of programming languages. So, we mean a class of programs containing algorithms which solve nontrivial problems naturally, as we shall try to illustrate in Example 4.2. The result is based on an analysis of program termination proofs to determine an approximation of the complexity of the computed function that we call the analysis of the implicit complexity of a program. We shall see that the programs considered calculate in exponential time, but denote polynomial time functions. For this, we combine termination orderings for term-rewriting systems (see the survey of Dershowitz [8]) and the ramification of recurrence schema arising from the work of Simmons [18], Bellantoni and Cook [2], and Leivant [15]. A similar approach, which replaces the above ramification principle by a semantic argument, has been suggested by Marion and Moyen [16]. Compared to the analysis of the complexity of a program like Benzinger’s prototype [3], this approach allows us to transform programs. The transformation is based on dynamic programming methods which were developed first by Cook [6] and generalised by Andersen and Jones [1 and 12].
This paper is organised as follows. Section 2 defines functions computed by rewrite systems and multiset path ordering. The construction of light multiset path orderings is given in Section 3. In Section 4, the main result is presented in Theorem 18. The proof is established by Theorems 19 and 20. The proof of Theorem 20 is first outlined and details are in 5 and 6.
2. Programming with rewrite rules
2.1. Programs over many-sorted algebra
A signature is a pair (
) where
is a finite set of sorts (atomic types) and Σ is a vocabulary on
. A vocabulary on
consists in a finite set of symbols. Every symbol in Σ has some fixed arity n and a type. The type of a symbol of arity 0 is a sort
of
. The type of a symbol of arity n>0 is an (n+1)-uplet of sorts of
that we write
.
Throughout, for every sort
there is a countably infinite set
of variables of sort
which is disjoint from vocabularies. Put
.
The set
is the set of freely generated terms of type
. The set of terms is
. The set of ground terms of type
is denoted by
. The set of ground terms is
. The set of variables in a term t is Var(t)
χ. A ground substitution is a mapping σ from χ to
, which respects type variables. The size, |t|, of a term
is the number of symbols in t
Definition 1. A program is a quadruplet
is a set of sorts.
is a vocabulary on
and symbols of
are called constructors.
is a vocabulary on
, disjoint of
, and symbols of
are of arity >0 and are called function symbols.
is a finite sequence of oriented equations. Every equation is of the form f(t1,…,tn)→t where
is of type
, each
,
such that FV(t)

i
nVar(ti).
The main function symbol is the last symbol defined in
.
2.2. Semantics
Actually, a program
denotes a term rewriting system. The set of equations
induces a rewriting rule → defined as follows: u→v if the term v is obtained from u by applying an equation of
. The relation
(
) denotes the transitive (reflexive-transitive) closure of →. We write
to mean that
and t is in normal form. A program is confluent if the induced rewriting rule → is confluent. One might consult [9] about general references on rewrite systems.
We give an operational semantics based on term rewriting. The domain of computation of a program
is the term algebra defined by the signature
. The interpretation of the sort
is the ground term algebra built on constructor terms; that is,
.
Definition 2. Let F be the main symbol of a confluent program
of type
. The function computed by
is
F
(u1,…,un) is undefined.Throughout, we shall only consider the normal forms, which are in
, as being meaningful.
Example 3. The following equations define the function which returns the minimum value in a list of numbers. The sorts are
and constructors are
2.3. Multiset path ordering
Multiset path ordering (MPO) was introduced by Plaisted [17] and Dershowitz [7]. MPO belongs to the family of syntactic term orderings whose well-foundness stems on Kruskal’s tree theorem [14].
We briefly describe MPO. A multiset M of terms of
is a finite mapping
which associates to each term t the number M(t) of terms t in M. Suppose that
is ordered by
. This ordering
induces an ordering
m on term multisets.
Definition 4. M
mN iff M≠N and for all
if M(s)>N(s), then there is
such that s
t and M(t)<N(t).
Throughout, the subterm relation will be noted
. For example, we see that {s(x),x,x}
m {s(x),s(x),x}.
Let
Σ be an ordering on Σ and ≈Σ be an equivalence relation on Σ which respects symbol arities. Then, we say that the quasi-ordering
Σ defined by
Σ
≈Σ is a precedence on Σ. From ≈Σ, we define the permutative congruence ≈ as the smallest equivalence relation on terms which satisfied f(t1,…,tn)≈g(s1,…,sn) if f≈Σg and ti≈Sπ(i) for some permutation π over {1,…,n}.
Definition 5. Let
Σ be a precedence on the signature Σ. The multiset path ordering
mpo is defined recursively by
- 1. s
mpof(…,ti,…), if s
mpoti 2. g(s1,…,sm)
mpof(t1,…,tn) if either g
Σf and si
mpof(t1,…,tn) for all i
m,or g≈Σf and {s1,…,sm}
mpom{t1,…,tn},
where
mpo=
mpo
≈.
A program
is terminating by MPO if there is a precedence on
such that for each equation l→r of
, we have r
mpol. Notice that we do take into account the types of terms to prove termination.
Hofbauer has shown in [10] that the class of functions computed by a confluent program which is terminated by MPO is exactly the class of the primitive recursive functions.
Example 6. The following program computes the exponential and terminates by MPO.
3. Light multiset path ordering
3.1. Valency and extension of orderings to n-uplets
The definition of a light multiset path ordering makes use of the notion of valency to restrict MPO.
Definition 7. A valency of a function symbol f of arity n is a mapping v(f):{1,…,n}
{0,1}.
We shall write f(…,ti,…) to mean that the term ti occurs at position i in f, which is a position of valency v(f,i).
The valency of a function symbol f indicates how to compare arguments of f. More precisely, it indicates first how to compare n-uplets of arguments and second how to compare an argument with a term by giving the ability of combining several term orderings. Thus, valencies generalise the notion of status as proposed by Kamin and Levy [13] in the following way. The status of function symbols allow one to compare f(s1,…,sn) with f(t1,…,tn) by lifting the term ordering to an ordering on sequences of terms which depends on f. The notion of valency extends this idea to the case where root symbols are different Throughout, we shall assign valencies to the function symbols
, but we shall never assign valencies to constructors of
. Constructors of
will be always treated separately.
Definition 8. Let v be a valency function on
. A permutation π over {1,…,n} respects the valency of f and g if the arity of f and g is n and v(g,i)=v(f,π(i)) for all i
n.
Definition 9. Let
0 and
1 be two term orderings. These orderings are lifted to an ordering over n-uplets of terms which respects the valency function v on
as follows. {s1,…,sn}
g,fv{t1,…,tn} iff there is a permutation π which respects the valency of f and g and which satisfies that there is j
n such that v(g,j)=1 and sj
1tπ(j), and for all i
n, si
v(g,i)tπ(i).
The above ordering on n-uplets of terms is a restriction of the multiset ordering, as defined in Definition 4, induced by the transitive closure of
1
0.
Definition 10. Let
be an equivalence relation on
. The permutative congruence ≈ which respects the valency v is the smallest equivalence relation on terms of
which satisfies:
- 1. c(s1,…,sn)≈c(t1,…,tn) if
and si≈ti for all i
n. 2. f(t1,…,tn)≈g(s1,…,sn) if
for some permutation π which respects the valency of f and g.
k)k=0,1 of orderings which is recursively defined on 1. s
kc(…,ti,…) if s
kti and .
2. s
kf(…,ti,…) if , and k
v(f,i).3. c(s1,…,sn)
kf(t1,…,tm) if , and si
kf(t1,…,tm), for each i
n. Note that c can be a 0-ary.4. g(s1,…,sn)
kf(t1,…,tm) if () and if si
max(k,v(g,i))f(t1,…,tm) for each i
n.5. g(s1,…,sn)
0f(t1,…,tn) if and {s1,…,sn}
vg,f{t1,…,tn},
where
k =
k
≈.
3.2. Properties
Proposition 12.
0 is an extension of
1; that is, s
1t implies s
0t.- The multiset path ordering
mpo is an extension of
0; that is, s
0t implies s
mpot.
1 is a rule of
0. By forgetting function symbol valencies, we also see that each rule of
0 is also a rule of
mpo. □Lemma 13. The relation (
i)i=0,1 is a partial ordering.Proof. Since
mpo is an extension of
i, we immediately see that
i is irreflexive. The transitivity of
i is tedious to demonstrate. So, we just outline the key steps of the proof. Assume that s
kt. We have the following:1. If s=c(s1,…,sn) and
, then for each i
n,si
kt.2. If s=g(s1,…,sn) and
, then for each i
n,si
max(k,v,(g,i))t.3. If
, then s
kc(…,t,…).4. If v(f,i)=k, then s
kf(…,t,…). where t is the ith argument of f.
i again by induction on |s|+|t|. □Neither
1 nor
0 is a total ordering. They are not simplification orderings because they are not monotonic. However, note that
0 is monotonic with respect to the argument of valency 1; i.e., si
1ti implies f(…,si,…)
0f(…,ti,…) if v(f,i)=1. Also
0 possesses the subterm property; that is, t
0f(…,t,…).
Proposition 14. Let s and t be two terms of
. We have s
1t iff s
0t iff s
t.
Proof. (1) s
1t implies s
0t by Proposition 12. (2) To prove that s
0t implies s
t we proceed by induction on |t|. But first observe that s≈t is equivalent to s=t by Definition 10. Otherwise s
0t=c(t0,…,tn), necessarily s
0tj, and so s
tj
t. (3) s
t implies s
0t is proved by induction on |t|. □
4. Characterisation of ptime
4.1. LMPO programs
Definition 15. A LMPO program is a sextuple
, where
is a confluent program, v is a valency function on
,
is a precedence on
, and such that the induced ordering
0 satisfies for each rule l→r,r
0l.
Termination of LMPO programs is obtained by the fact that the term ordering
0 is a restriction of
mpo, and so the rewrite relation of a LMPO program is well founded.
4.2. Examples
We shall now give several examples. This example shows that the class of LMPO programs encapsulates a broad class of algorithmic patterns.
For the sake of readability, we write the termination proof in sequent style. On the right, we note the rule number which is applied, following Definition 11. Also, we write in subscript the valency of arguments of a function symbol in the declaration of its type. Last we use a notational convention similar to that of [2] and write f(x1,…,xn;y1,…,ym), with a semicolon separating two lists of arguments, to indicate that v(f,i)=1 for i
{1,…,n} and v(f,n+j)=0 for j
{1,…,m}.
1. The program displayed in Example 3 is a LMPO program where valencies are set as
,
,
. The precedence is
. The two first equations are ordered because
0 possesses the subterm properties. The termination proof of the third one is:
| Full-size image (5K) |
The fourth equation is a special case of (3). The termination proof of the last equation is carried out as follows.
| Full-size image (4K) |
The program
computes the minimum of two integers n and m, written in unary, within O(min(n,m)) steps. So
is the “good” algorithms for the function
. Colson has demonstrated in [5] that if we consider the class of programs defined by means of strict primitive recursion, then the function
is obviously definable but not with a “good” algorithm. In particular, this implies that the characterization of the polynomial time functions of Bellantoni and Cook [2] does not comprise a “good” algorithm for
, due to the absence of simultaneous recursion is that class.
2. Given a list list of type
,
(list,
(list);) returns the list sorted. The algorithm is the insertion sort. The sort
, with (tt:
, ff:
) as a constructor set, denotes the truth values.
0<suc(y)→tt
x<0→ff
suc(x)<suc(y)→x<y
Put







E-mail Article
Add to my Quick Links

Cited By in Scopus (5)



