Logic programming
The primary virtue of logic programming
is its declarative semantics.
A logic program can be read as a theory stating
relations between entities in a particular domain.
For a programmer, such an interpretation allows to
separate the concerns for the logical structure of an algorithm
from issues of control.
The famous phrase
[M] algorithm = logic + control
of [Ko79] states this principle succinctly.
The idea of using predicate logic as a programming language
arose from research in automated theorem-proving
in the early 70s
and resulted in the language Prolog.
The first implementation was by Roussel/Colmerauer.
Soon afterwards, efficient implementations became available
which demonstrated the fruitfulness of this idea,
at least for the kind of problems to be found
in academic settings.
By now Prolog is a widely accepted programming tool
which is applied in areas like
databases,
problem solving,
natural language processing,
compiler design
and, not the least important, expert systems.
According to [Bu83] Prolog has often been used to prototype
small- to medium-scale expert systems in a business environment.
As another indication of the potential
of the paradigm, it may be mentioned that the Japanese
Fifth Generation Computers project is based on logic
programming.
The logical language used in logic programming
is called Horn clause logic,
which is a subset of predicate logic
that enables an efficient computational interpretation.
C.f. [DoGa84].
\nop{
Polynomial time/pebblings.
}
In this section, which is necessarily of an introductory nature,
we will explore the notion of a logic program
and its mathematical, logical foundations enabling a declarative
reading of a program.
Complementary to the declarative interpretation
we will define a procedural interpretation
that allows logic programs to be used for computing.\ftn{
Readers not interested in the mathematical foundations of logic
programming are advised to jump to section \ref{dig/prolog}.
}
We will then describe the language Prolog,
including the so-called impure or extra-logical
facilities that are in practice considered necessary
for using Prolog in actual programming tasks.
We will defer a discussion of the merits of Prolog
from a software engineering perspective to section [softw].
Declarative versus procedural semantics
Logic is an excellent vehicle for reasoning about the
state of affairs in a particular world.
The advantage of logic is that it offers
a natural formalism to express the facts and rules that pertain
to that world.
We will explain how such facts and rules can be stated in a logic program.
Our treatment is based on [Ll84].
\prologindex{declarative semantics}
.so programs formulas
.so models
.so procedural
The logical variable
\prologindex{logical variable}
Apart from being a means to establish the satisfiability of a goal,
the power of logic programming lies in the way values are
computed during an inference.
The output of a goal with variables is a substitution binding these variables to terms.
Terms are the elements of the universe a logic program deals with.
As we have seen in section [programs], defining logic programs,
terms are either constants, variables or compound terms
consisting of a function-symbol
and zero or more argument-terms.
We may use a logic program to define terms in a formal way.
The program
\oprog{terms}{
}
assumes a constant 0 and a one-argument function-symbol s
and defines terms in accordance with the definition
given earlier.
The goal
has as solutions all the possible bindings
of X to the terms contained in the set
[]
which represents the so-called Herbrand universe of the program terms.
The possible output that may result from evaluating the goal
is given by the substitutions
[] , , ,...
binding X to the elements of the Herbrand universe.
The question that we will answer in this section is how we are able
to find these substitutions.
Substitutions
\prologindex{substitutions}
Recall that a substitution is (represented by) a set of the form
that binds each variable to a term ,
for .
Applying a substitution to a term is recursively defined by
\hspace{0.7cm}
\begin{tabular}{l l}
& for a constant c, \\
& for and X otherwise, and \\
& for a compound term
\end{tabular}
In other words, applying a substitution to a constant has no effect.
Applying a substitution to a variable X results in the term t when
the binding X/t occurs in .
Applying a substitution to a compound term
results in the term in which
is applied recursively to the argument terms .
As an example, applying the substitution gives
[] $0%h = 0X %h = s(0)Y %h = Ys(X)%h = s(s(0))%h_2%h_1%h_1%h_2t1%h_2 != t2%h_2%h_1%h_1 %h_2%h_1 = {{X1/t1,...,X_n/t_n}}%h_2 = {{Y1/t1',...,Y_k/t_k'}}{{X1/t1%h_2,...,X_n/t_n%h_2,Y1/t1',...,Y_k/t_k'}}%h_2%h_1%h_1 %h_2.
Moreover, it is easy to check that the composition of substitutions is associative,
that is that $(%h_1 %h_2) %h_3 = %h_1 (%h_2 %h_3)%h_1 = {{ X/s(X1) }} %h_2 = {{ X1/s(X2) }}%h_1 %h_2 = {{X/s(s(X2)), X1/s(X2)}}%ht1%h = t2%h%h%h = {{X/s(0)}}f(X,Y)f(s(0),Y)%h' = {{X/s(0), Y/0}}%h%h%s%g%s = %h %g%h_1%h_2%h_1 \c %h_2%h_1 \c fail = fail fail \c %h_2 = fail%h_2%h_1%h_1 %h_2 = fail%e%h \c %e = %e \c %h = %h%hunify(c1,c2) = %ec1 = c2unify(X,t) = {{X/t}}unify(t,X) = unify(X,t)unify(f(t1,...,t_n), f(t1',...,t_n')) = unify(t1,t1') \c ... \c unify(t_n,t_n')unify(t1,t2) = failunify(p(s(X),0), p(Y,Z)) = {{Y/s(X)}} \c {{Z/0}} = {{ Y/s(X), Z/0 }}unify(p(s(X),0), p(Y,X)) = {{Y/s(X)}} \c {{X/0}} = {{ Y/s(0), X/0 }}unify(p(s(X),0), p(Y,s(Z))) = {{Y/s(X)}} \c fail = failunify(p(s(X),0), p(Y,Y)) = {{Y/s(X)}} \c {{Y/0}} = failfail ~ <- X = s(X){{X/s(X)}}position(1,1)position(1,2)position(8,8)same_row(position(X,Y), position(X,Z)) <-
Examples
Concluding our discussion of Prolog,
two examples will be given that will illustrate how to
use Prolog for implementing search in a finite search space.
The first example presents a solution to a problem in chess, the N-queens problem.
Our solution is given for , but may be easily generalized to
other values of N.
The second example illustrates depth-first search with a loop-check
built-in to prevent non-termination.
.so queens states