Prolog and Horn Clauses

Author
Affiliation

Clayton Cafiero

University of Vermont

Published

2026-05-25

Horn clauses in Prolog

A Horn clause is a disjunctive clause with at most one positive literal.

Example: (\neg p_1 \lor \neg p_2 \lor \neg p_3 \lor p_4).

We can further distinguish definite clauses, unit clauses (facts), and goal clauses.

  1. A Horn clause with exactly one positive literal is a definite clause. The example above is a definite clause.
  2. A unit clause without any negative literals is a unit clause (fact).
  3. A Horn clause without any positive literals is a goal clause.

Horn clauses as implications

Consider how we can rewrite this definite Horn clause:

(\neg p_1 \lor \neg p_2 \lor \neg p_3 \lor p_4)

The one positive literal is p_4. We can rewrite all the negations thus:

(\neg p_1 \lor \neg p_2 \lor \neg p_3)

\neg(p_1 \land p_2 \land p_3)

Therefore, the entire expression can be written

\neg(p_1 \land p_2 \land p_3) \lor p_4.

Recall that (\neg P \lor Q) is equivalent to (P \implies Q). So we have

\neg\neg(p_1 \land p_2 \land p_3) \implies p_4,

which is equivalent to

(p_1 \land p_2 \land p_3) \implies p_4.

Also, equivalent (where \Longleftarrow means "follows from" or "is implied by") to

p_4 \Longleftarrow (p_1 \land p_2 \land p_3).

Horn clauses as implications

(p_1 \land p_2 \land p_3) \implies p_4.

Now, this is interesting. We can convert a definite Horn clause to an equivalent implication, where if all the elements of the antecedent are true, then the consequent is true.

Complexity of satisfiability of Horn clauses

While unrestricted satisfiability problems are \mathsf{NP}-complete (e.g., 3SAT), satisfiability of Horn clauses, HORNSAT, is in \mathsf{P}. We can solve these problems in linear time—they're \mathcal{O}(n) ! (Actually, HORNSAT is \mathsf{P}-complete—not only is it in \mathsf{P}, but all problems in \mathsf{P} can be reduced to HORNSAT.)

So if we can render our problem instances using Horn clauses, we have a computationally efficient solution for them!

Other forms of Horn clauses as implications

Recall that a Horn clause without any positive literals is a goal clause.

If we have (\neg p_1 \lor \neg p_2 \lor \neg p_3 \lor \ldots), this is equivalent to

(p_1 \land p_2 \land p_3 \land \ldots) \implies \bot.

Also, interesting.

What does this mean in Prolog?

These are facts.

mammal(cat).
mammal(dog).

They have no negative literals. These are unit clauses.

In the notation we've used so far (with subscripted p), in FOL, these could be written p_1 and p_2.

Rules have what we call a head and a body. The general form is

head :- condition1, condition2, ...

Example:

pet(X) :- mammal(X), friendly(X)

In logic notation, if we write pet(X) as p_3, mammal(X) as p_1 and friendly(X) as p_2, then we have

p_3 \Longleftarrow (p_1 \land p_2).

This is a Horn clause (written as an implication). Our rule is a Horn clause.

(p_1 \land p_2) \implies p_3

(If it's a mammal and it's friendly, it's a pet.)

Because we the equivalence (\neg P \lor Q) \Leftrightarrow (P \implies Q), (p_1 \land p_2) \implies p_3 can be rewritten

(p_1 \land p_2) \implies p_3

\neg (p_1 \land p_2) \lor p_3

(\neg p_1 \lor \neg p_2) \lor p_3)

and we have the original form—a disjunction with at most one positive literal!

A query is just a body of conditions (goals) without a head. So if we ask

?- mammal(X), friendly(X)

we’re asking Prolog “can these conditions be satisfied?” That is, what are all the X that make this true? But again, this too is a Horn clause.

p_3 \Longleftarrow (p_1 \land p_2)

(p_1 \land p_2) \implies p_3

\neg (p_1 \land p_2) \lor p_3

(\neg p_1 \lor \neg p_2) \lor p_3.

That is, what X standing in for p_3 makes this true (there may be more than one).

Summary

Our facts are Horn clauses and our rules are Horn clauses. Our queries are Horn clauses. We’ve already seen that we can check the satisfaction of a Horn clause in linear time.

Prolog uses Horn clauses internally when checking to see if there are ways of satisfying a query.

The process works backward from the goal, and involves unification (matching terms) and resolution (applying rules), all within the framework of Horn clauses.

Copyright © 2023–2026 Clayton Cafiero

No generative AI was used in producing drafts of this material. This was written the old-fashioned way. AI was used to rewrite existing pseudocode in LaTeX to produce standalone *.tex files for rendering, and for revisions toward satisfying WCAG 2.1 AA-level accessibility standards as required by UVM policy. It may also have been used to proofread selected human-written prose. Claude 2.1.150 with model Sonnet 4.6. Revisions, if any, were performed by the author. AI was not used in generating any code whatsoever. All code samples and starter code are by the author only.