Horn Clauses and Definite Clauses

GATE DA — High Importance

ðŸŽŊ 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

Definition: Horn Clause

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

TypeFormExampleMeaning
Definite clauseA₁ ∧ ... ∧ Aₖ → BP ∧ Q → RRule with body and head
FactB (no body)PA definite clause with empty body
Goal clauseA₁ ∧ ... ∧ Aₖ → âŠĨÂŽP âˆĻ ÂŽQQuery / 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

Forward Chaining (Data-Driven Reasoning)

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.

Algorithm: Forward Chaining
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?

StepAgendaInferredAction
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

Backward Chaining (Goal-Driven Reasoning)

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.

Algorithm: Backward Chaining
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:

  1. Goal: T. Find rule: S ∧ P → T. Need to prove S and P.
  2. Subgoal: S. Find rule: R → S. Need to prove R.
  3. Subgoal: R. Find rule: P ∧ Q → R. Need to prove P and Q.
  4. Subgoal: P. P is a known fact → Success.
  5. Subgoal: Q. Q is a known fact → Success.
  6. R is proved → S is proved.
  7. Subgoal: P (for T's rule). Already a fact → Success.
  8. T is proved! ✅

📝 Forward vs Backward Chaining

PropertyForward ChainingBackward Chaining
DirectionFacts → GoalGoal → Facts
StrategyData-drivenGoal-driven
Used inProduction systems, rule enginesProlog, expert systems
ExploresAll derivable factsOnly relevant facts
CompletenessSound & Complete for HornSound & 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!