Algorithm Logic + Control PDF
Algorithm Logic + Control PDF
Programming
Languages
J. J. Horning
Editor
Algorithm =
Logic + Control
Robert Kowalski
Imperial College, London
Introduction
July 1979
Volume 22
Number 7
Algorithms for computing factorials are a simple example. The definition of factorial constitutes the logic component of the algorithms:
1 is the factorial o f 0;
u is the factorial o f x + 1 ~ v is the factorial o f x a n d u is v times
x+l.
The definition can be used bottom-up to derive a sequence of assertions about factorial or it can be used topdown to reduce the problem of computing the factorial
of x + 1 to the subproblems of computing the factorial
of x and multiplying the result by x + 1. Different ways
of using the same definition give rise to different algorithms. Bottom-up use of the definition behaves like
iteration. Top-down use behaves like recursive evaluation.
The manner in which the logic component is used to
solve problems constitutes the control component. As a
first approximation, we restrict the control component C
to general-purpose problem-solving strategies which do
not affect the meaning of the algorithm as it is determined by the logic component L. Thus different algorithms A1 and Az, obtained by applying different methods of control Cx and C2 to the same logic definitions L,
are equivalent in the sense that they solve the same
problems with the same results. Symbolically, if A1 =
L + C1 and Az = L + C2, then A1 and Az are equivalent.
The relationship of equivalence between algorithms, because they have the same logic, is the basis for using
logical analysis to improve the efficiency of an algorithm
by retaining its logic but improving the way it is used. In
particular, replacing bottom-up by top-down control
often (but not always) improves efficiency, whereas replacing top-down sequential solution of subproblems by
top-down parallel solution seems never to decrease efficiency.
Both the logic and the control components of an
algorithm affect efficiency. The logic component ex425
cLOgic
Abstract
procedure
definitions
./
//ithm
\
Oefinitk)ns of
data -structures
represented by /
~,~Control component
/
Direction
Strategy for Strategy for
(e.g. top-down execution of investigating
or bottorn-up) i)rocedure
alternative
calls (e.g.
procedures
sequential or
parallel )
Scheme for
storing data
represented
relationally
We study the affect of altering each of the above components of an algorithm. The final section of the paper
introduces a graphical notation for expressing, more
formally than in the rest of the paper, certain kinds of
control information. Much of the material in this paper
has been abstracted from lecture notes [23] prepared for
the advanced summer school on foundations of computing held at the Mathematical Centre in Amsterdam in
May 1974.
Notation
We use the clausal form of predicate logic. Simple
assertions are expressed by clauses:
July 1979
Volume 22
Number 7
is f e m a l e / f x is mother of y,
is m a l e / f x is father of y,
is parent o f y t f x is mother of y, and
is parent o f y t f x is father ofy.
The arrow ~ is the logical connective "if"; " x " and "y"
are variables representing any individuals; "Zeus,"
"Ares," etc. are constant symbols representing particular
individuals; "Father," "Mother," "Female," etc. are
predicate symbols representing relations among individuals. Variables in different clauses are distinct even if
they have the same names.
A clause can have several joint conditions or several
alternative conclusions. Thus
Grandparent (x, y) ,--- Parent (x, z), Parent (z, y)
Male (x), Female (x) ~-- Parent (x, y)
Ancestor (x, y) ,--- Parent (x, y)
Ancestor (x, y) ~ Ancestor (x, z), Ancestor (z, y)
is
is
is
is
More generally, we define clauses and their interpretation as follows. A clause is an expression of the form
Bh...,Bm*--AI,...,A,~
re, n-->0,
426
Communications
of
the ACM
July 1979
Volume 22
Number 7
B
B *---A1 .... , An
~-- A~, ..., A,,
~--
July 1979
Volume 22
Number 7
The terminology "top-down" and "bottom-up" applied to proof procedures derives from our investigation
of the parsing problem formulated in predicate logic [23,
25]. Given a grammar formulated in clausal form, topdown reasoning behaves as a top-down parsing algorithm and bottom-up reasoning behaves as a bottom-up
algorithm. David Warren (unpublished) has shown how
to define a general proof procedure for Horn clauses,
which when applied to the parsing problem, behaves like
the Earley parsing algorithm [16].
n>_O
B,---A1 ..... A,
(2)
(3)
and;
By applying the matching substitution 0
(B1 . . . . . Bi-1, A1 . . . . . An, Bi+l . . . . . Bin) O.
Zeus
y = Harmonia
transmits input only. Invoking the assertional procedure
Father (Zeus, Ares)
4211
(2)
m-->O
(1)
(3)
July 1979
Volume 22
Number 7
Data Structures
If the predicates "empty," "first," and "rest" are eliminated from the definition of quicksort by a preliminary
bottom-up deduction, then the original data-structurefree definition can be replaced by a definition which
mixes the data structures with the procedures
Communications
of
the A C M
July 1979
V o l u m e 22
Number 7
In the simplest top-down execution strategy, procedure calls are executed one at a time in the sequence in
which they are written. Typically an algorithm can be
improved by executing the same procedure calls either
as coroutines or as communicating parallel processes.
The new algorithm
A 2 = L + C2
430
C1
July 1979
V o l u m e 22
Number 7
The mathematician is likely to understand such a definition bottom-up, generating the sequence of assertions
The
The
The
The
etc.
factorial
factorial
factorial
factorial
of
of
of
of
0 is
1 is
2 is
3 is
1
1
2
6
<--<-<-<--
0-th
l-th
2-th
3-th
Fibonacci
Fibonacci
Fibonacci
Fibonacci
number
number
number
number
is
is
is
is
1 ~-1
2 ~-3 ~--
J u l y 1979
V o l u m e 22
Number 7
Parent
Parent
Parent
Parent
Parent
etc.
(Zeus, Ares)
(Hera, Ares) ~(Ares, Harmonia) ~-(Semele, Dionisius) ~(Zeus, Dionisius)
CI=L2+
6"2.
Some of the behavior determined by the control component C1 in one analysis might be determined by the
logic component L2 in another analysis. This has significance for understanding the relationship between programming style and execution facilities. In the short term
sophisticated behavior can be obtained by employing
simple execution strategies and by writing complicated
programs. In the longer term the same behavior may be
obtained from simpler programs by using more sophisticated execution strategies.
The path-finding problem illustrates a situation in
which the same algorithm can be analyzed in different
ways. Consider the problem of finding a path from A to
Z in the following directed graph.
D
rV
(.4)
(B)
(C)
(D)
(E)
(X)
,-~-- Go (A)
~-- Go (.4)
~ Go (B)
<-- Go (B)
",- Go (E)
~ Go (Z)
Go (Z) <-- Go (X)
Go (Z) ,---Go (Y)
Go (Y) <-- Go (U)
Go (Y) ~-- Go (V)
etc.
Different control strategies determine different pathfinding algorithms. Forward search from the initial node
A is bottom-up reasoning from the initial assertion
G o ( A ) ~-- . Backward search from the goal node Z is
top-down reasoning from the initial goal statement
*-- G o ( Z ) . Bidirectional search from both the initial
node and the goal node is the combination of top-down
and bottom-up reasoning. Whether the path-finding algorithm investigates one path at a time (depth-first) or
develops all paths simultaneously (breadth-first) is a
Communications
of
the A C M
July 1979
Volume 22
Number 7
etc.
Grandporent ( x , y )
Parent ( x , z ) ,
Parent ( x , y ) ~- Father ( x , y )
Parent ( z , y )
Parent ( x , y ) ~- Mother ( x , y )
(Zeus,Harmonia)
F(x,y, u, v)
as stating that
the factorial of x is y
given that the factorial of u is v.
G r a n d p o ~ n t ( x , y ) ~- PQrent ( x , z ) ,
Parent ( z , 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
t, a single goal statement incorporates both the goal and
the basis of the recursion
,---F(t,y,O, 1).
Purent ( x , y ) 4 - Father ( x , y )
. . . .
Father (7-eus.Ar~s) ~ -
433
Communications
of
the ACM
July 1979
Volume 22
Number 7
(3)
IL
Grandparent (x, y) ~ Parent (x, z), Parent (z, y)
2
(2)
(3)
(1)
July 1979
Volume 22
Number 7
July 1979
Volume 22
Number 7
23. K o w a l s k i , R . A . L o g i c f o r p r o b l e m - s o l v i n g . M e m o N o . 75, D e p t .
C o m p u t . Logic, U. o f E d i n b u r g h , 1974.
24. K o w a l s k i , R . A . P r e d i c a t e logic as p r o g r a m m i n g l a n g u a g e .
Information Processing 74, N o r t h - H o l l a n d P u b . Co., A m s t e r d a m ,
1974, pp. 5 6 9 - 5 7 4 .
25. K o w a l s k i , R . A . A p r o o f p r o c e d u r e u s i n g c o n n e c t i o n g r a p h s .
J. A C M 22, 4 (Oct. 1974), 5 7 2 - 9 5 .
26. K o w a l s k i , R.A., a n d K u e h n e r , D. L i n e a r r e s o l u t i o n w i t h selection
f u n c t i o n . Artif. IntelL 2 (1971), 2 2 7 - 2 6 0 .
27. L o v e l a n d , D . W . A s i m p l i f i e d f o r m a t for the m o d e l - e l i m i n a t i o n
t h e o r e m - p r o v i n g p r o c e d u r e . J. A C M 16, 3 ( J u l y 1969), 3 4 9 - 3 6 3 .
28. M a c C a r t h y , J. A basis f o r a m a t h e m a t i c a l t h e o r y o f
c o m p u t a t i o n . In Computer Programming and F o r m a l Systems, P.
B r a t f o r t a n d D. H i r s c h b e r g , Eds., N o r t h - H o l l a n d P u b . Co.,
A m s t e r d a m , 1967.
29. M c S k i m i n , J.R., a n d M i n k e r , J. T h e use o f a s e m a n t i c n e t w o r k in
a d e d u c t i v e q u e s t i o n - a n s w e r i n g system. Proc. Int. J o i n t C o n f . Artif.
lntell., 1977, pp. 5 0 - 5 8 .
30. Petri, C . A . G r u n d s a t z l i c h e s z u r B e s c h r e i b u n g d i s k r e t e r P r o z e s s e
3. C o l l o q . u b e r A u t o m a t h e n t h e o r i e , B i r k h a u s e r V e r l a g , Basel,
S w i t z e r l a n d , 1967.
31. Pratt, V . R . T h e c o m p e t e n c e / p e r f o r m a n c e d i c h o t o m y in
p r o g r a m m i n g . Proc. F o u r t h A C M S I G A C T / S I G P L A N
Symp. on
Professional Activities:
Calendar of Events
A C M ' s calendar policy is to list open computer science meetings that are held on a not-forprofit basis. Not included in the calendar are educational seminars, institutes, and courses. Submittals should be substantiated with name of the
sponsoring organization, fee schedule, and chairm a n ' s name and full address.
One telephone number contact for those interested in attending a meeting will be given when
a number is specified for this purpose.
All requests for A C M sponsorship or cooperation should be addressed to Chairman, Conferences and Symposia Committee, Seymour J.
Wolfson. 643 MacKenzie Hall, Wayne State University, Detroit, MI 48202, with a copy to Louis
Flora, Conference Coordinator, A C M Headquarters, 1133 Avenue of the Americas, New York,
NY 10036; 212 265-6300. F o r European events, a
copy of the request should also be sent to the
European Representative. Technical Meeting Request F o r m s for this purpose can be obtained
from A C M Headquarters or from the European
Regional Representative. Lead time should include
2 months (3 months if for Europe) for processing
of the request, plus the necessary months (minimum 3) for any publicity to appear in Communications.
P r i n c i p l e s o f P r o g r a m m i n g L a n g u a g e s , S a n t a M o n i c a , Calif., J a n .
1977, p p 194-200.
32. R o b i n s o n , J.A. A u t o m a t i c d e d u c t i o n w i t h h y p e r - r e s o l u t i o n . Int.
J. Comput. Math. 1 (1965), 2 2 7 - 3 4 .
33. R o u s s e l , P. M a n u a l d e r e f e r e n c e et d ' U t i l i s a t i o n . G r o u p e d ' I n t e l l .
Artif., U E R , M a r s e i l l e - L u m i n y , F r a n c e , 1975.
34. S c h w a r z , J. U s i n g a n n o t a t i o n s to m a k e r e c u r s i o n e q u a t i o n s
b e h a v e . Res. M e m o , D e p t . Artif. Intell., U. o f E d i n b u r g h , 1977.
35. Sickel, S. A s e a r c h t e c h n i q u e f o r c l a u s e i n t e r c o n n e c t i v i t y g r a p h s .
I E E E Trans. Comptrs. ( S p e c i a l Issue o n A u t o m a t i c T h e o r e m
P r o v i n g ) , A u g . 1976.
36. T~irnlund, S.A. A n i n t e r p r e t e r f o r the p r o g r a m m i n g l a n g u a g e
p r e d i c a t e logic. Proc. Int. J o i n t C o n f . Artif. Intell., Tiblisi, 1975, pp.
601-608.
37. W a r r e n , D. A s y s t e m f o r g e n e r a t i n g plans. M e m o N o . 76, D e p t .
C o m p u t . Logic, U. o f E d i n b u r g h , 1974.
38. W a r r e n , D., Pereira, L . M . , a n d Pereira, F. P R O L O G - - T h e
l a n g u a g e a n d its i m p l e m e n t a t i o n c o m p a r e d w i t h L I S P . Proc. S y m p .
o n Artif. Intell. a n d P r o g r a m m i n g L a n g u a g e s ; S I G P L A N N o t i c e s
( A C M ) 12, 8; S I G A R T N e w s l e t t e r s ( A C M ) 64 ( A u g . 1977), pp. 1 0 9 115
39. W i r t h , N. Algorithms + Data Structures = Programs. P r e n t i c e H a l l , E n g l e w o o d Cliffs, N.J., 1976.
Symposium
on
Distributed
University of Cambridge, England. Sponsor: Association for Literary and Linguistic Research.
Contact: J.L. Dawson, Secretary, 1980 Symposium, Literary and Linguistic Computing Centre,
Sidgcwick Site, Cambridge CB3 9DA, England.
2 May 1980
1979 Summer Computer Simulation Conference, Toronto, Ont., Canada. Sponsors: SCS, ISA,
South Africa. Sponsor: University of Natal. Contact: H. Roland Weistroffer, Computer Science
436
Communications
of
the ACM
De~t.. University of Natal. King George V Avenue, Durban, 4001, Republic of South Africa.
27-29 July 1979
Seminar on Scientific Go Theory (with European Go Congress 1979) near Bonn, W. Germany.
Contact: Klaus Heine, Kleiststr. 67, 294 Wilhelmshaven, W. Germany.
6-8 August 1979
1979 Pattern Recognition and Image Proc-
essing Conference, Chicago, IlL Sponsor: 1EEECS. Contact: PRIP79, Box 639, Silver Spring, MD.
6-8 August 1979
Seventh Conference on Electronic Computation, St. Louis, Mo. Sponsors: ASCE. Washing-
A C M S I G P L A N Symposium on Compiler
(San Diego), L a Jolla, Calif. Sponsor: ACL. Contact: Donald E. Walker, A C L See'y-Treas., SRI
International, Menlo Park, C A 94025.
13-15 August 1979