Horn Clauses and Definite Clauses
ðŊ Learning Objectives
- Define Horn clauses, definite clauses, goal clauses, and facts
- Understand forward chaining and backward chaining algorithms
- Know why Horn clauses can be decided in linear time
- Understand the connection to Prolog and logic programming
1. Horn Clause Definition
A Horn clause is a clause (disjunction of literals) with at most one positive literal.
A Horn clause has the form: ÂŽAâ âĻ ÂŽAâ âĻ ... âĻ ÂŽAâ âĻ B
This is equivalent to the implication: Aâ â§ Aâ â§ ... â§ Aâ â B
Types of Horn Clauses
| Type | Form | Example | Meaning |
|---|---|---|---|
| Definite clause | Aâ â§ ... â§ Aâ â B | P â§ Q â R | Rule with body and head |
| Fact | B (no body) | P | A definite clause with empty body |
| Goal clause | Aâ â§ ... â§ Aâ â âĨ | ÂŽP âĻ ÂŽQ | Query / no positive literal |
ð Key Insight
A fact is a special case of a definite clause where the body is empty (equivalently, âĪ â B, which simplifies to B). Every fact is a unit clause with a single positive literal.
Example: Classifying Clauses
- {P} â Fact (definite clause, 1 positive literal, no negative literals)
- {ÂŽP, ÂŽQ, R} â Definite clause (1 positive: R) â means P â§ Q â R
- {ÂŽP, ÂŽQ} â Goal clause (0 positive literals) â means P â§ Q â âĨ
- {P, Q} â NOT a Horn clause (2 positive literals)
- {ÂŽP, Q, R} â NOT a Horn clause (2 positive literals)
2. Why Horn Clauses Matter
Theorem: Linear-Time Inference for Horn Clauses
Entailment for Horn clause knowledge bases can be decided in O(n) time, where n is the total size of the knowledge base. This is dramatically faster than the general propositional satisfiability problem, which is NP-complete.
Key reasons Horn clauses are important:
- They form the basis of Prolog and logic programming languages.
- Many real-world knowledge bases can be expressed using only Horn clauses.
- Forward and backward chaining are sound and complete for Horn clause KBs.
3. Forward Chaining
Start from known facts and repeatedly apply definite clauses whose premises are all satisfied to derive new facts. Continue until the query is proved or no new facts can be derived.
function FORWARD-CHAIN(KB, q):
// KB: set of Horn clauses, q: query symbol
count â table of premise counts for each clause
inferred â table mapping symbols to FALSE
agenda â list of known facts in KB
while agenda is not empty:
p â POP(agenda)
if p == q:
return TRUE
if inferred[p] == FALSE:
inferred[p] â TRUE
for each clause c where p is in premise of c:
decrement count[c]
if count[c] == 0:
add head of c to agenda
return FALSE
Example: Forward Chaining
Knowledge Base:
- Fact: P
- Fact: Q
- Rule: P â§ Q â R
- Rule: R â S
- Rule: S â§ P â T
Query: T?
| Step | Agenda | Inferred | Action |
|---|---|---|---|
| 1 | [P, Q] | {} | Pop P, mark inferred. R's count: 2â1 |
| 2 | [Q] | {P} | Pop Q, mark inferred. R's count: 1â0 â add R |
| 3 | [R] | {P, Q} | Pop R, mark inferred. S's count: 1â0 â add S |
| 4 | [S] | {P, Q, R} | Pop S, mark inferred. T's count: 2â1 (P already inferred decremented earlier) |
Wait â T needs S â§ P. P was already inferred, and now S is inferred, so T's count reaches 0. Add T to agenda.
Step 5: Pop T from agenda. T == query â return TRUE. â
ð Properties of Forward Chaining
- Sound and Complete for propositional Horn clause KBs
- Time complexity: O(n) where n = size of KB
- Data-driven: starts from facts, works toward the query
4. Backward Chaining
Start from the query and work backwards: to prove the query, find a rule whose head matches, then try to prove all premises of that rule. Repeat recursively until known facts are reached.
function BACKWARD-CHAIN(KB, q):
return BC-OR(KB, q, {})
function BC-OR(KB, goal, visited):
if goal â known_facts(KB):
return TRUE
if goal â visited:
return FALSE // avoid infinite loops
visited â visited ⊠{goal}
for each clause c in KB where head(c) == goal:
if BC-AND(KB, premises(c), visited):
return TRUE
return FALSE
function BC-AND(KB, goals, visited):
if goals is empty:
return TRUE
g â FIRST(goals)
if BC-OR(KB, g, visited):
return BC-AND(KB, REST(goals), visited)
return FALSE
Example: Backward Chaining for query T
Using the same KB as above:
- Goal: T. Find rule: S â§ P â T. Need to prove S and P.
- Subgoal: S. Find rule: R â S. Need to prove R.
- Subgoal: R. Find rule: P â§ Q â R. Need to prove P and Q.
- Subgoal: P. P is a known fact â Success.
- Subgoal: Q. Q is a known fact â Success.
- R is proved â S is proved.
- Subgoal: P (for T's rule). Already a fact â Success.
- T is proved! â
ð Forward vs Backward Chaining
| Property | Forward Chaining | Backward Chaining |
|---|---|---|
| Direction | Facts â Goal | Goal â Facts |
| Strategy | Data-driven | Goal-driven |
| Used in | Production systems, rule engines | Prolog, expert systems |
| Explores | All derivable facts | Only relevant facts |
| Completeness | Sound & Complete for Horn | Sound & Complete for Horn |
5. Connection to Prolog
Prolog (Programming in Logic) is a programming language based on Horn clauses. A Prolog program consists of:
- Facts:
parent(tom, bob).â Tom is a parent of Bob. - Rules:
grandparent(X, Z) :- parent(X, Y), parent(Y, Z). - Queries:
?- grandparent(tom, Z).
Prolog uses backward chaining with depth-first search to answer queries. It operates on first-order Horn clauses (extending propositional Horn clauses with variables).
6. GATE Exam Tips
ðŊ Key Points for GATE
- Know how to identify Horn clauses from a given set of clauses.
- Be able to trace forward and backward chaining step by step.
- Remember: Horn clause satisfiability is solvable in linear time.
- GATE may ask: "Which of the following is a Horn clause?" â count positive literals!