Logic Programming: Clauses as Programs

How proof search can compute answers, and why a program's logical consequences must be distinguished from the behavior of its execution strategy.

Modern logic · Part 21Historical questions · mathematical ideas

When do logical clauses compute answers, and when does their execution miss them?

Connect SLD resolution with the least Herbrand model, then distinguish depth-first search, tabling, and default negation.

Selected landmarksSeries timeline ↗

Colmerauer, Roussel, and collaborators develop Prolog from natural-language processing and automated deduction. Their history distinguishes a preliminary 1971 version from the more developed 1972 system.

Source: Colmerauer and Roussel, The Birth of Prolog (1992 manuscript)

A theorem prover usually starts with a statement and searches for a derivation. A logic program can start with an open query and search for substitutions that make the query follow from its clauses.

The resulting proof search computes an answer. This connects deduction with programming while making the choice of search strategy especially visible.

The emergence of Prolog

Robert Kowalski developed a procedural interpretation of logical clauses, drawing on resolution and work with Donald Kuehner on linear resolution. His 1974 paper “Predicate Logic as Programming Language” presented the approach systematically.

Portrait of Robert Kowalski in 2009
Robert Kowalski (b. 1941) The procedural interpretation of clauses, logic, and control.
Sources and credit for Robert Kowalski

Yongyuth Permpoontanalarp, 9 November 2009, CC BY 3.0, via Wikimedia Commons Image source · CC BY 3.0 · Biographical dates

Portrait of Alain Colmerauer in 1988
Alain Colmerauer (1941–2017) With Philippe Roussel and collaborators, the development of Prolog.
Sources and credit for Alain Colmerauer

Alaindavid2, 5 July 1988, CC BY-SA 4.0, via Wikimedia Commons Image source · CC BY-SA 4.0 · Biographical dates

Alain Colmerauer, Philippe Roussel, and their collaborators in Marseille developed Prolog in the early 1970s, with natural-language processing playing an important role in its origins.

The interaction between theoretical interpretation and implementation is central to this history. Prolog did not emerge from a purely abstract theorem followed by an automatic translation into software.

Colmerauer and Roussel's first-person history distinguishes preliminary work in 1971 from the more developed system of 1972 and subsequent refinement.

Kowalski's later formulation “algorithm = logic + control” captures the distinction we will examine: clauses describe relationships, while control determines how their consequences are explored.

Horn clauses as rules

A definite Horn clause has one positive head and a conjunction of positive atoms in its body:

B1∧⋯∧Bn→H.B_1\land\cdots\land B_n\to H.

Its variables are universally quantified. Operationally, it can be read: to establish HH, establish each BiB_i with compatible substitutions.

In conventional Prolog notation, the direction is written as a head followed by its conditions:

prolog
ancestor(X, Y) :- parent(X, Y).
ancestor(X, Y) :- parent(X, Z), ancestor(Z, Y).

The notation does not make the relationship into an ordinary assignment statement. It describes a relation that can support different queries.

A query with computed answers

Add two facts:

prolog
parent(ada, ben).
parent(ben, cara).

Ask:

prolog
?- ancestor(ada, Who).

Using the first ancestor clause reduces the goal to finding a fact of the form parent(ada, Who). Unification with the first fact gives the answer Who = ben.

On backtracking, the recursive clause can be tried. It introduces an intermediate person Z and the goals parent(ada, Z) and ancestor(Z, Who).

The parent fact sets Z = ben. The remaining ancestor goal can then use the direct clause and the second parent fact, producing Who = cara.

Each answer arises from a proof of a corresponding ground instance. The variable assignments accumulated along the derivation form the computed substitution.

SLD resolution

SLD resolution specializes linear resolution to definite clauses with a selected goal atom.

At each step, the procedure selects an atom from the current goal, chooses a program clause whose head unifies with it, and replaces the atom with the clause's body under the resulting substitution.

Variables in reused clauses must be renamed freshly to prevent unrelated uses from accidentally sharing variables.

For an equality-free definite first-order program, a ground atomic query AA is a logical consequence exactly when the goal ←A\leftarrow A has a successful SLD derivation. Any fixed rule for selecting the next goal atom admits such a successful branch when one is needed; fresh clause variables and sound finite-term unification remain part of the setting. This is an existence and search-completeness result, not a claim that every traversal of the resulting tree will find that branch. Apt and van Emden's strong-completeness theorem, Theorem 6.6, makes this distinction explicit. The earlier van Emden–Kowalski account connects execution with the least-model semantics developed below.

Ordinary Prolog execution usually selects goals from left to right and explores alternatives depth first in clause order. This is efficient and predictable for many programs, but it can remain on an infinite branch while a successful branch exists elsewhere.

The same consequence, a different execution

Consider this program:

prolog
p(X) :- p(X).
p(a).

The fact p(a) makes p(a) a logical consequence. The first clause adds a logically redundant implication.

Under ordinary depth-first execution with the recursive clause first, a query for p(a) repeatedly selects the same recursive clause. It never reaches the fact.

Moving the fact before the recursive clause yields an immediate first answer. Reordering clauses has not changed the set of logical consequences, but it has changed what the execution produces before diverging.

Tabling can address important classes of repeated subgoals by storing and reusing answers. Breadth-first or other fairer searches make different tradeoffs. Logic programming therefore contains an ongoing study of control, not a promise that control disappears.

Tabling changes the treatment of recursion

Memoization normally stores the result of a completed function call. Tabling also coordinates calls that are still being evaluated. If a recursive query meets an equivalent active subgoal, it can suspend that dependency and consume answers as they become available, instead of descending into the same branch forever.

For the program p(X) :- p(X). together with p(a)., the recursive dependency adds no new answer by itself. The fact supplies a, and completion of the dependency determines that no further answers arise. This is the operational counterpart of the least-fixed-point calculation.

For finite positive Datalog programs, tabling can terminate because only finitely many relevant subgoals and answers exist. With function symbols, a query can still generate infinitely many distinct terms. Tabling is therefore not a general solution to undecidability. Chen and David S. Warren's work on SLG evaluation extended the technique to programs with negation through delayed dependencies; David S. Warren is distinct from David H. D. Warren of the Warren Abstract Machine.

Negation and the closed world

Under negation as failure, a negated goal succeeds when the search for its positive form finitely fails. If the positive goal succeeds, the negated goal fails; divergence of that search does not count as finite failure.

That is different from deriving classical negation. If a database contains no record that a person is enrolled, the absence can support “not enrolled” only under an additional interpretation that the database is sufficiently complete for that question.

With variables, the distinction becomes sharper. Testing whether any instance of a goal succeeds is not the same operation as enumerating all objects for which its classical negation holds.

Keith Clark's work on negation as failure and later semantic accounts helped formalize such issues. Stratification, well-founded semantics, and stable-model semantics provide different treatments in richer languages.

These developments should be introduced with their assumptions. There is no single interpretation that turns every operational use of failure into ordinary classical negation.

Nonmonotonic reasoning also developed as a separate knowledge-representation program. Reiter's default logic and McCarthy's circumscription, both published in 1980, offered distinct ways for new facts to defeat previously licensed conclusions. The earlier discussion of defaults and circumscription gives their constructions and a worked example. The stable-model account below connects with that tradition; it should not be read as a claim that these different formalisms always return the same conclusions.

Two meanings for a negative cycle

Consider a ground program in schematic default-negation notation:

text
p :- not q.
q :- not p.

Interpreting these rules requires a policy for circular negative dependencies. Under the stable-model semantics, associated with Michael Gelfond and Vladimir Lifschitz in 1988, there are two stable models: one containing pp alone and one containing qq alone.

The test is concrete; Lifschitz gives the reduct construction in “Thirteen Definitions of a Stable Model”. For a candidate set of atoms, discard each rule whose negative body mentions an atom in the candidate, then remove the remaining negative literals. The candidate must equal the least model of the resulting positive program. With candidate {p}\{p\}, the second rule disappears and the first becomes a fact, yielding precisely {p}\{p\}. The other case is symmetric. Neither the empty set nor {p,q}\{p,q\} passes the test.

Under the well-founded semantics developed by Allen Van Gelder, Kenneth Ross, and John Schlipf, both atoms are undefined in this example. An unresolved negative cycle need not be arbitrarily assigned a truth value. Stable models support alternative complete answer sets; well-founded semantics supplies a single, possibly partial account. This branch of the history leads to answer-set programming and nonmonotonic reasoning, rather than merely faster Prolog execution.

Cut, unification, and implementation choices

Prolog's cut prunes alternatives in the search. Used carefully, it can remove redundant work; used differently, it can change which answers are returned.

A program containing cut therefore needs an account of its control behavior in addition to a reading of its pure clauses.

The occurs check is another boundary. Standard finite-term unification rejects a proposed equation such as X = f(X). Some implementations omit the check for efficiency or support rational-tree terms intentionally.

Neither choice should be silently presented as exactly the finite first-order term calculus in the resolution note.

David H. D. Warren's work on efficient Prolog implementation, including the Warren Abstract Machine, helped make logic programming practical. That implementation history belongs alongside the logical principles because memory organization and execution costs shaped the usable language.

Declarative and fixed-point semantics

Maarten van Emden and Kowalski's 1976 work connected operational, model-theoretic, and fixed-point accounts of definite programs.

Begin with the program's facts. Repeatedly add every head whose body is already established. The least fixed point of this immediate-consequence operation gives the least Herbrand model in the standard setting.

For the ancestor example, the parent facts first support direct ancestors. Further iterations add longer chains.

This describes the meaning independently of a particular depth-first traversal. The separation lets us ask whether an implementation computes the intended relation and how much of that relation it actually enumerates.

Computing the least model explicitly

Let TP(I)T_P(I) contain the heads of all ground instances of program clauses whose body atoms belong to II. Facts enter automatically because their bodies are empty. Starting with I0=∅I_0=\varnothing, set In+1=TP(In)I_{n+1}=T_P(I_n).

For the ancestor program:

StageNewly available facts
I1I_1parent(ada, ben), parent(ben, cara)
I2I_2ancestor(ada, ben), ancestor(ben, cara)
I3I_3ancestor(ada, cara)

Here the operator is monotone: additional premises cannot invalidate an already applicable positive rule. The increasing sequence reaches the least fixed point; with function symbols, it may require the union of all finite stages rather than a finite stopping point. This links the van Emden–Kowalski semantics to the earlier study of fixed points in programming-language semantics.

The least-model restriction matters. A classical model of the clauses could contain extra ancestor pairs not generated by the program. The least Herbrand model contains exactly the ground atomic consequences, supplying a precise intended relation for the positive program.

Datalog and constraints

Datalog restricts the basic setting to function-free rules over a finite database, with suitable safety conditions. The resulting finite collection of possible ground atoms supports terminating fixed-point evaluation for positive programs.

This makes recursive relations useful in databases and program analysis. Termination comes from the restrictions and finite setting, not from logic programming in complete generality.

Constraint logic programming extends answer substitutions with constraints in a chosen domain. Joxan Jaffar and Jean-Louis Lassez's work in the 1980s provided a general framework, alongside related developments such as Colmerauer's constraint-oriented Prolog systems.

An answer may then specify a relationship such as x+y=10x+y=10 with x≥0x\ge0, rather than enumerate every numerical pair immediately. Constraint solving cooperates with logical search, but the permitted domain and solver determine the guarantees.

Constraints can represent an entire family of answers

For the constraint query x+y=10x+y=10 with x≥0x\ge0 and y≥0y\ge0 over the reals, a solver can return the relation

0≤x≤10,y=10−x.0\le x\le10,\qquad y=10-x.

This is a compact description of infinitely many answers. Over integers it describes eleven pairs; over another domain the semantics changes again. A constraint logic program interleaves clause selection with maintaining such a constraint store. An inconsistent store closes that branch, while a residual satisfiable store can be part of the answer.

The advance in Jaffar and Lassez's framework was to state the relationship between this execution and a chosen constraint theory systematically. It extends the earlier insight that proof search can compute substitutions, while preserving the obligation to specify what operations and decision procedures mean.

From generated answers to checked mathematics

Logic programming shows how proof search can become execution. Proof assistants pursue a complementary aim: support human-directed mathematical development while controlling the rules by which a theorem is accepted.

Check your understanding: can a true query run forever?

For p(X) :- p(X). followed by p(a)., explain the three outcomes: logical consequence, ordinary depth-first Prolog, and tabled evaluation.

The clauses entail p(a) because it is a fact. Depth-first evaluation can repeatedly choose the first clause and never reach that fact. Tabled evaluation records the recursive dependency, obtains a from the fact, and avoids re-expanding the same call indefinitely. These are different statements about meaning and execution. Changing the search strategy does not change the positive program's least Herbrand model.

Sources and further reading