Algorithm Logic + Control PDF
Algorithm Logic + Control PDF
Programming J. J. Horning
1
Languages Editor
Algorithm =
Logic + Control
Robert Kowalski
Imperial College, London
Introduction
to general-purpose problem-solving strategies which do We study the affect of altering each of the above com-
not affect the meaning of the algorithm as it is deter- ponents of an algorithm. The final section of the paper
mined by the logic component L. Thus different algo- introduces a graphical notation for expressing, more
rithms A1 and Az, obtained by applying different meth- formally than in the rest of the paper, certain kinds of
ods of control Cx and C2 to the same logic definitions L, control information. Much of the material in this paper
are equivalent in the sense that they solve the same has been abstracted from lecture notes [23] prepared for
problems with the same results. Symbolically, if A1 = the advanced summer school on foundations of comput-
L + C1 and Az = L + C2, then A1 and Az are equivalent. ing held at the Mathematical Centre in Amsterdam in
The relationship of equivalence between algorithms, be- May 1974.
cause they have the same logic, is the basis for using
logical analysis to improve the efficiency of an algorithm
Notation
by retaining its logic but improving the way it is used. In
particular, replacing bottom-up by top-down control We use the clausal form of predicate logic. Simple
often (but not always) improves efficiency, whereas re- assertions are expressed by clauses:
placing top-down sequential solution of subproblems by Father (Zeus,Ares) ~--
top-down parallel solution seems never to decrease effi- Mother (Hera, Ares)
ciency. Father (Ares, Harmonia) ~-
Mother (Semele, Dionisius)
Both the logic and the control components of an Father (Zeus, Dionisius)
algorithm affect efficiency. The logic component ex- etc.
425 Communications July 1979
of Volume 22
the ACM Number 7
Here Father (x, y) states that x is the father of y and .... An are conditions of the clause and the atoms B1.... ,
Mother (x, y) states that x is the mother o f y . Bm are alternative conclusions of the clause. I f the clause
Clauses can also express general conditional propo- contains the variables Xl ..... Xk then interpret it as stating
sitions: that
Female (x) * - Mother (x, y) for all xl ..... xk
Male (x) ,--- Father (x, y) B1 or ... or Bm if A1 and ... and An.
Parent (x, y) ~ Mother (x, y)
Parent (x, y) ~-- Father (x, y). I f n = 0, then interpret it as stating unconditionally that
for all x~, .... xk
These state that B~ or ... or Bin.
x is f e m a l e / f x is mother of y,
I f m = 0, then interpret it as stating that
x is m a l e / f x is father of y,
x is parent o f y t f x is mother of y, and for no x~, ..., xk
x is parent o f y t f x is father ofy. A ~ and ... and A n.
The arrow ~ is the logical connective "if"; " x " and "y" I f m = n = O, then interpret the clause as a sentence
are variables representing any individuals; "Zeus," which is always false.
"Ares," etc. are constant symbols representing particular An atom (or atomic formula) is an expression of the
individuals; "Father," "Mother," "Female," etc. are form
predicate symbols representing relations among individ- P (h ..... tn)
uals. Variables in different clauses are distinct even if
they have the same names. where P is an n-place predicate symbol and t~, ..., t,, are
A clause can have several joint conditions or several terms. Interpret the atom as asserting that the relation
alternative conclusions. Thus called P holds a m o n g the individuals called t~. . . . . tn.
A term is a variable, a constant symbol, or an expres-
Grandparent (x, y) ,--- Parent (x, z), Parent (z, y)
Male (x), Female (x) ~-- Parent (x, y) sion of the form
Ancestor (x, y) ,--- Parent (x, y)
f (h . . . . . tn)
Ancestor (x, y) ~ Ancestor (x, z), Ancestor (z, y)
where f is an n-place function symbol and tl . . . . . tn are
where x, y, and z are variables, state that for all x, y, and terms.
g
The sets of predicate symbols, function symbols, con-
x is grandparent o f y / f x is parent of z a n d z is parent of y; stant symbols, and variables are any mutually disjoint
x is male or x is female t f x is parent of y; sets. (By convention, we reserve the lower case letters u,
x is ancestor o f y t f x is parent of y; and
v, w, x, y, z, with or without adornments, for variables.
x is ancestor o f y t f x is ancestor of z a n d z is ancestor ofy.
The type o f other kinds of symbols is identified by the
Problems to be solved are represented by clauses position they occupy in clauses.)
which are denials. The clauses Clausal form has the same expressive power as the
~-- Grandparent (Zeus, Harmonia) standard formulation of predicate logic. All variables Xl,
Ancestor (Zeus, x) .... Xk which occur in a clause C are implicitly governed
•,-- Male (x), Ancestor (x, Dionisius) by universal quantifiers Vxa . . . . . Vxk (for all xl and ...
and for all Xk). Thus C is an abbreviation for
where x is a variable state that
Vx~ ... Vxh C.
Zeus is not grandparent of Harmonia,
for no x is Zeus ancestor of x, and The existential quantifier 3 x (there exists an x) is avoided
for no x is x male a n d is x an ancestor of Dionisius.
by using constant symbols or function symbols to n a m e
A typical problem-solver (or theorem-prover) reacts individuals. For example, the clauses
to a denial by using other clauses to try to refute the Father (dad (x), x) ~ H u m a n (x)
denial. I f the denidl contains variables, then it is possible Mother (mum (x), x) <-- H u m a n (x)
to extract from the refutation the values of the variables
state that for all humans x, there exists an individual,
which account for the refutation and represent a solution
called dad (x), who is father of x, and there exists an
of the problem to be solved. In this example, different
individual, called m u m (x), who is mother of x.
refutations of the second denial find different x of which
Although the clausal form has the same power as the
Zeus is the ancestor:
standard form, it is not always as natural or as easy to
x = Ares, x = Harmonia, x = Dionisius. use. The definition of subset is an example: "x is a subset
More generally, we define clauses and their interpre- of y if for all z, z belongs to y if z belongs to x." The
tation as follows. A clause is an expression of the form definition in the standard form of logic
where B~. . . . . Bin, A1 . . . . . An are atoms. The atoms A~, _is a direct translation o f the English. The clausal form of
426 Communications July 1979
of Volume 22
the ACM Number 7
the definition can be systematically derived from the The problem of showing that Zeus is a grandparent
standard form. It can take considerable effort, however, of Harmonia can be solved either top-down or bottom-
to recognize the notion of subset in the resulting pair of up. Reasoning bottom-up, we start with the assertions
clauses: Father (Zeus, Ares) ~--
x C__y, arb (x, fl) ~ x Father (Ares, Harmonia) ~--
x CC_y ~-- arb ( x , y ) ~ y .
and use the clause Parent (x, y) ~-- Father (x, y) to derive
(Here we have used infix notatio 9 for predicate symbols, new assertions
writing xPy instead of P(x, y).) Parent (Zeus,Ares)
In this paper, we shall avoid the awkwardness of the Parent (Ares, Harmonia) ~--
clausal definition of subset by concentrating attention on
clauses which contain at the most one conclusion. Such Continuing bottom-up we derive, from the definition of
clauses, called Horn clauses, can be further classified into grandparent, the new assertion
four kinds: Grandparent (Zeus, Harmonia) *--
assertions (of the form) B which matches the original goal.
procedure declarations (of the form) B *---A1 .... , An Reasoning top-down, we start with the original goal
denials ~-- A~, ..., A,,
a n d contradiction ~--
of showing that Zeus is a grandparent of Harmonia
Grandparent (Zeus, Harmonia)
Assertions can be regarded as the special case of proce-
dure declarations where n = 0. and use the definition of grandparent to derive two new
The Horn clause subset of logic resembles conven- subgoals
tional programming languages more closely than either ,--- Parent (Zeus, z), Parent (z, Harmonia)
the full clausal or standard forms of logic. For example,
the notion of subset can be defined recursively by means by denying that any z is both a child of Zeus and a
of Horn clauses: parent of Harmonia. Continuing top-down and consid-
ering both subgoals (either one at a time or both simul-
x C y "~- E m p t y (x)
x _ y ~-- Split (x, z, x ' ) z ~y, x ' _ y .
taneously), we use the clause Parent (x, y) *--
Father (x, y) to replace the subproblem Parent (Zeus, z)
Here it is intended that the relationship Empty (x) holds by Father (Zeus, z) and the subproblem Parent (z, Har-
when x is empty, and Split (x, z, x') holds when x monia) by Father (z, Harmonia). The newly derived
consists of element z and subset x'. Horn clauses used in subproblems are solved compatibly by assertions which
this way, to define relations recursively, are related to determine "Ares" as the desired value of z.
Herbrand-Grdel recursion equations as described by In both the top-down and bottom-up solutions of the
Kleene [22], elaborated by McCarthy [28], employed for grandparent problem, we have mentioned the derivation
program transformation by Burstall and Darlington of only those clauses which directly contribute to the
[13], and augmented with control annotations by eventual solution. In addition to the derivation of rele-
Schwarz [34]. vant clauses, it is often unavoidable, during the course of
searching for a solution, to derive assertions or subgoals
which do not contribute to the solution. For example, in
the bottom-up search for a solution to the grandparent
Top-Down and Bottom-Up Interpretations of Horn problem, it is possible to derive the irrelevant assertions
Clauses Parent (Hera, Ares) ~--
Male (Zeus}
A typical Horn clause problem has the form of In the top-down search it is possible to replace the
subproblem Parent (Zeus, z) by the irrelevant and un-
(1) a set of clauses which defines a problem domain
solvable subproblem Mother (Zeus, z).
and
There are both proof procedures which understand
(2) a theorem which consists of (a) hypotheses repre-
logic top-down (e.g. model elimination [17], SL-resolu-
sented by assertions A1 ~ . . . . . An ~-- and (b) a
tion [20], and interconnectivity graphs [35]) as well as
conclusion which is negated and represented by a
ones which understand logic bottom-up (notably hyper-
denial ~--B1 ..... Bin.
resolution [35]). These proof procedures operate with the
In top-down problem-solving, we reason backwards clausal form of predicate logic and deal with both Horn
from the conclusion, repeatedly reducing goals to clauses and non-Horn clauses. Among clausal proof
subgoals until eventually all subgoals are solved directly procedures, the connection graph procedure [25] is able
by the original assertions. In bottom-up problem-solving, to mix top-down and bottom-up reasoning. Among non-
we reason forwards from the hypotheses, repeatedly clausal proof procedures, Gentzen systems [ 1] and Bled-
deriving new assertions from old ones until eventually soe's related natural deduction systems [5] provide facil-
the original goal is solved directly by derived assertions. ities for mixing top-down and bottom-up reasoning.
427 Communications July 1979
of Volume 22
the ACM Number 7
The terminology "top-down" and "bottom-up" ap- by the first procedure call in the goal statement
plied to proof procedures derives from our investigation
~-- Father (Zeus, z), Parent (z, Harmonia)
of the parsing problem formulated in predicate logic [23,
25]. Given a grammar formulated in clausal form, top- derives the new goal statement
down reasoning behaves as a top-down parsing algo-
rithm and bottom-up reasoning behaves as a bottom-up ~-- Parent (Ares, Harmonia).
algorithm. David Warren (unpublished) has shown how
to define a general proof procedure for Horn clauses, The matching substitution
which when applied to the parsing problem, behaves like z = Ares
the Earley parsing algorithm [16].
transmits output only. In general, however, a single
procedure invocation may transmit both input and out-
put.
The Procedural Interpretation of Horn Clauses The top-down interpretation of Horn clauses differs
in several important respects from procedure invocation
The procedural interpretation is the top-down inter- in conventional programming languages:
pretation. A clause of the form
(1) The body of a procedure is a set rather than a
B~----A~..... A. n>_O
sequence of procedure calls. This means that pro-
is interpreted as a procedure. The name of the procedure cedure calls can be executed in any sequence or in
is the conclusion B which identifies the form of the parallel.
problems which the procedure can solve. The body of (2) More than one procedure can have a name which
the procedure is the set o f procedure calls Ai. A clause of matches a given procedure call. Finding the "right"
the form procedure is a search problem which can be solved
*--Bl ..... Bm m-->O by trying the different procedures in sequence, in
parallel, or in other more sophisticated ways.
consisting entirely of procedure calls (or problems to be (3) The input-output arguments of a procedure are not
solved) behaves as a goal statement. A procedure fixed but depend upon the procedure call. A pro-
B,---A1 ..... A, cedure which tests that a relationship holds among
given individuals can also be used to f i n d individ-
is invoked by a procedure call Bi in the goal statement: uals for which the relationship holds.
(1) By matching the call Bi with the name B of the
procedure;
(2) By replacing the call Bi with the body of the pro- The Relationship Between Logic and Control
cedure obtaining the new goal statement
B1 . . . . . Bi-l, A1, ..., An, Bi+l .... , B m In the preceding sections we considered alternative
and; top-down and bottom-up control strategies for a fixed
(3) By applying the matching substitution 0 predicate logic representation of a problem-domain. Dif-
ferent control strategies for the same logical representa-
(B1 . . . . . Bi-1, A1 . . . . . An, Bi+l . . . . . Bin) O. tion generate different behaviors. However, information
(The matching substitution 0 replaces variables by terms about a problem-domain can be represented in logic in
in a manner which makes B and Bi identical: BO = B,O.) different ways. Alternative representations can have a
The part of the substitution 0 which affects variables in more significant effect on the efficiency of an algorithm
the original procedure calls B~..... Bm transmits output. than alternative control strategies for the same represen-
The part which affects variables in the new procedure tation.
calls A1 . . . . . An transmits input. Consider the problem of sorting a list. In one repre-
For example, ihvoking the grandparent procedure by sentation, we have the definition
the procedure call in
sorting x givesy ~---yis a permutationof x, y is ordered.
Grandparent (Zeus, Harmonia)
derives the new goal statement (Here we have used distributed infix notation for predi-
cate symbols, writing P l x l P z x 2 ... PnX,P,+I instead of
Parent (Zeus,z), Parent (z, Harmonia). P(x1 . . . . . Xn) where the Pi (possibly empty) are parts of
The matching substitution P.) As described in [24], different control strategies ap-
plied to the definition generate different behaviors. None
x = Zeus
using the definition of grandparent. In a conventional The same algorithm A can often be analyzed in
programming language, the parenthood relationship different ways:
might be stored in a two-dimensional array. A general A =LI+ CI=L2+ 6"2.
procedure for finding grandchildren (given grandpar-
Some of the behavior determined by the control com-
ents) might involve two iterative loops, one nested inside
ponent C1 in one analysis might be determined by the
the other, with a jump out when a solution has been
logic component L2 in another analysis. This has signif-
found. Similar behavior is obtained by interpreting the
icance for understanding the relationship between pro-
grandparent procedure top-down, executing procedure
gramming style and execution facilities. In the short term
calls one at a time, in the sequence in which they are
sophisticated behavior can be obtained by employing
written, trying alternative procedures (assertions in this
simple execution strategies and by writing complicated
case) one at a time in the order in which they are written.
programs. In the longer term the same behavior may be
The logical analysis of the conventional iterative algo-
obtained from simpler programs by using more sophis-
rithm does not concern recursion but involves sequential
ticated execution strategies.
search through a space of alternatives. The sequential
The path-finding problem illustrates a situation in
search strategy is identical to the backtracking strategy
which the same algorithm can be analyzed in different
for executing nondeterministic programs [ 18].
ways. Consider the problem of finding a path from A to
Representation of data by means of clauses, as in the
Z in the following directed graph.
family relationships example, rather than by means of
terms, is similar to the relational model of databases D
[10]. In both cases data is regarded as relationships
among individuals. When data is represented by conven-
tional data structures (terms), the programmer needs to
specify in the logic component of programs and queries
both how data is stored and how it is retrieved. When
data is represented relationally (by clauses), the program-
mer needs only to specify data in the logic component;
the control component manages both storage and re- rV
trieval. In one analysis, we employ a predicate G o ( x ) which
The desirability of separating logic and control is states that it is possible to go to x. The problem of going
now generally accepted in the field of databases. An from A to Z is then represented by two clauses. One
important advantage is that storage and retrieval schemes asserts that it is possible to go to A. The other denies that
can be changed and improved in the control component it is possible to go to Z. The arc directed from A to B is
without affecting the user's view of the data as defined represented by a clause which states that it is possible to
by the logic component. go to B if it is possible to go to A:
The suitability of a search strategy for retrieving data Go (.4) ,-- ~ Go (Z)
depends upon the structure in which the data is stored. Go (B) ~-- Go (A) Go (Z) <-- Go (X)
Iteration, regarded as sequential search, is suitable for Go (C) ~-- Go (.4) Go (Z) ,---Go (Y)
data stored sequentially in arrays or linked lists. Other Go (D) ~ Go (B) Go (Y) <-- Go (U)
Go (E) <-- Go (B) Go (Y) ~-- Go (V)
search strategies are more appropriate for other data
Go (X) ",- Go (E) etc.
structures, such as hash tables, binary trees, or semantic
networks. McSkimin and Minker [29], for example, Different control strategies determine different path-
store clauses in a manner which facilitates both parallel finding algorithms. Forward search from the initial node
search and finding all answers to a database query. A is bottom-up reasoning from the initial assertion
Deliyanni and Kowalski [ 15], on the other hand, propose G o ( A ) ~-- . Backward search from the goal node Z is
a path-finding strategy for retrieving data stored in se- top-down reasoning from the initial goal statement
mantic networks. *-- G o ( Z ) . Bidirectional search from both the initial
Representation of data by means of terms is a com- node and the goal node is the combination of top-down
mon feature of Horn clause programs written in and bottom-up reasoning. Whether the path-finding al-
PROLOG [12, 33, 38]. Tiirnlund [36], in particular, has gorithm investigates one path at a time (depth-first) or
investigated the use of terms as data structures in Horn develops all paths simultaneously (breadth-first) is a
432 Communications July 1979
of Volume 22
the A C M Number 7
matter of the search strategy used to investigate alter- top-down sequential manner behaves in the same way
natives. as the original formulation executed in a mixed top-
In the second analysis, we employ a predicate down, bottom-up fashion.
Go* (x, y ) which states that it is possible to go from x to
y. In addition to the assertions which describe the graph A Notation for Expressing Control Information
and the goal statement which describes the problem, The distinction between top-down and bottom-up ex-
there is a single clause which defines the logic of the ecution can be expressed in a graphical notation which
path-finding algorithms: uses arrows to indicate the flow of control. The same
<--Go* (A, Z) notation can be used to represent different combinations
Go* (A, B) <-- GO* (X, Z) <-- of top-down and bottom-up execution. The notation does
Go* (A, C) *-- Go* ( Y, Z) ~- not, however, aim to provide a complete language for
Go* (B, D) <--- Go* (U, Y) <'- expressing useful control information.
Go* (B, E) <-- Go* (V, Y) <-
Arrows are attached to atoms in clauses to indicate the
Go* (E, X) <---- etc.
Go* (x, y) <'- Go* (x, z), GO* (z, y) direction of transmission of processing activity from
clause to clause. For every pair of matching atoms in the
Here both forward search from the initial node A initial set of clauses (one atom in the conclusion of a
and backward search from the goal node Z are top-down clause and the other among the conditions of a clause),
reasoning from the initial goal statement <-- Go* (A, Z). there is an arrow directed from one atom to the other.
The difference between forward and backward search is For top-down execution, arrows are directed from
the difference in the choice of subproblems in the body conditions to conclusions. For the grandparent problem,
of the path-finding procedure. Solving Go* (x, z) before we have the graph:
Go* (z, y ) is forward search, and solving Go* (z, y ) before
Go* (x, z) is backward search. Coroutining between the <- Grandparent (Zeus,Harmonia)
two subproblems is bidirectional search. Bottom-up rea-
soning from the assertions which define the graph gen-
erates the transitive closure, effectively adding a new arc
to the graph directed from node x to node y, whenever Grandporent ( x , y ) Parent ( x , z ) , Parent ( z , y )
there is a path from x to y.
Many problem domains have in common with path-
finding that an initial state is given and the goal is to
achieve some final state. The two representations of the
Parent ( x , y ) ~- Father ( x , y ) Parent ( x , y ) ~- Mother ( x , y )
path-finding problem exemplify alternative representa-
tions which apply more generally in other problem do-
mains. Warren's plan-formation program [37], for ex-
ample, is of the type which contains both the given and
Father (Zeus, Ares) <-- Father (Ar~s,Harmonia} ~- Mother (Hera,Ares) <-
the goal state as different arguments of a single predicate.
It runs efficiently with the sequential execution facilities
Processing activity starts with the initial goal statement.
provided by P R O L O G . The alternative formulation,
It transmits activity to the body of the grandparent
which employs a predicate having one state as argument,
procedure, whose procedure calls, in turn, activate the
is easier to understand but more difficult to execute
efficiently. parenthood definitions. The database of assertions pas-
Even the definition of factorial can be represented in sively accepts processing activity without transmitting it
to other clauses.
two ways. The formulation discussed earlier corresponds
to the one-place predicate representation of path-finding. For bottom-up execution, arrows are directed from
conclusions to conditions:
The formulation here corresponds to the two-place pred-
icate representation. Read 4- Grandparent (Zeus,Harmonia)
F(x,y, u, v)
as stating that
the factorial of x is y G r a n d p o ~ n t ( x , y ) ~- PQrent ( x , z ) , Parent ( z , y )
given that the factorial of u is v.
F(x,y, x,y) <--
F (x, y, u, v) <--u plus 1 is u', u' times v is v', F (x, y, u', v').
To find the factorial of an integer represented by a term Purent ( x , y ) 4 - Father ( x , y ) Purent ( x , y ) ~" Mother ( x , y )
t, a single goal statement incorporates both the goal and
the basis of the recursion . . . .
,---F(t,y,O, 1).
The new formulation of factorial executed in a simple Father (7-eus.Ar~s) ~ - leather (Ares,Hormooia) ~- Moth~" (Ftera.Ares) ~-
(l) waits until x is asserted to be parent of z, then Given the goal of showing that x is a subset of y, the first
(2) finds a child y of z, and finally clause asserts that the individual named arb (x,y) belongs
(3) asserts that x is grandparent ofy. to x and the second clause generates the goal of showing
The combination indicated by that arb (x, y) belongs to y.
The grandparent definition illustrates the inadequacy
2 of the arrow notation for expressing certain kinds of
control information. Suppose that the grandparent defi-
Grandparent (x, y) ~-- Parent (x, z), Parent (z, y)
nition is to be used entirely top-down.
1 3
Grandparent (x, y) ~ Parent (x, z), Parent (z, y).
represents the algorithm which
(1) waits until x is asserted to be parent of z, then The effective sequencing of procedure calls in the body
(2) waits until it is given the problem of showing that of the procedure depends upon the parameters of the
x is grandparent of y,
activating procedure call:
(3) which it then attempts to solve by showing that z is
parent ofy. (1) If the problem is to find a grandchild y of a given
The algorithm represented by x, then it is more effective (i) first to find a child z
of x; (ii) and then to find a child y of z.
1 (2) If the problem is to find a grandparent x of a given
IL y, then it is better (i) first to find a parent z of y; (ii)
Grandparent (x, y) ~ Parent (x, z), Parent (z, y)
and then to find a parent x of z.
2 3
Such sequencing of procedure calls depending on the
(1) responds to the problem of showing that x is grand- pattern of input and output cannot be expressed in the
parent of y, arrow notation.
(2) by waiting until x is asserted to be parent of z, and In relational database query languages, input-sensi-
then tive sequencing of procedure calls needs to be determined
(3) attempting to show that z is parent ofy. by the data retrieval system rather than by the user.
Consider, for example, a database which defines the
Using the arrow notation, we can be more precise following relations:
than before about the bottom-up execution of the recur-
sive definition of Fibonacci number. The bottom-up Supplier (x, y, z) supplier n u m b e r x has n a m e y and status z,
Part (x, y, z) part n u m b e r x has n a m e y and unit cost z,
execution referred to previously is, in fact, a mixture of supplier n u m b e r x supplies part n u m b e r y in
Supply (x, y, z)
bottom-up and top-down execution: quantity z.
4
Given the query
the u + 2 Fib is x ~-- the u + 1 Fib is y, the u Fib is z, y plus z is x. W h a t is the n a m e of suppliers of pens?
Answer (y)
2 1 3 Answer (y) ~-- Supplier (x, y, z), Supply (x, u, v), Part (u, pen, w)