Forward & Backward Chaining in FOL

GATE Relevance: Medium

Prerequisites

🎯 Learning Objectives

  • Understand definite clauses and Datalog in the FOL setting
  • Trace forward chaining (data-driven) inference
  • Trace backward chaining (goal-driven) inference
  • Compare the two strategies and their trade-offs
  • Connect backward chaining to Prolog execution

1. Definite Clauses and Datalog

Definite Clause

A definite clause is a disjunction of literals with exactly one positive literal:

¬P1 ∨ ¬P2 ∨ … ∨ ¬Pn ∨ Q

Equivalently, in implication form: P1 ∧ P2 ∧ … ∧ Pn → Q

A clause with no negative literals (just Q) is a fact. A clause with both positive and negative literals is a rule.

Datalog

Datalog is a restricted subset of FOL: definite clauses with no function symbols (only constants, variables, and predicates). This restriction guarantees:

  • Forward chaining terminates (finite Herbrand base).
  • Backward chaining can be made to terminate with loop detection.

2. Forward Chaining (Data-Driven)

Algorithm: FOL Forward Chaining

Start from known facts and repeatedly apply rules to derive new facts until the query is answered or no new facts can be derived.

Forward Chaining Pseudocode
function FOL-FC-ASK(KB, query):
  repeat until new is empty:
    new ← {}
    for each rule (P1 ∧ ... ∧ Pn → Q) in KB:
      for each substitution θ such that
        Piθ matches a known fact for all i:
        q' ← Qθ
        if q' is not in KB and q' is not in new:
          add q' to new
          if q' unifies with query:
            return θ
    add new to KB
  return failure
Forward Chaining Example

KB:

  1. American(x) ∧ Weapon(y) ∧ Sells(x, y, z) ∧ Hostile(z) → Criminal(x)
  2. Owns(Nono, M1)
  3. Missile(M1)
  4. Missile(x) ∧ Owns(Nono, x) → Sells(West, x, Nono)
  5. Missile(x) → Weapon(x)
  6. Enemy(Nono, America)
  7. Enemy(x, America) → Hostile(x)
  8. American(West)

Query: Criminal(West)?

Iteration 1:

  • Rule 5 + fact 3: Weapon(M1) ✓
  • Rule 7 + fact 6: Hostile(Nono) ✓
  • Rule 4 + facts 3,2: Sells(West, M1, Nono) ✓

Iteration 2:

  • Rule 1 + facts 8, Weapon(M1), Sells(West,M1,Nono), Hostile(Nono): θ={x/West, y/M1, z/Nono} ⇒ Criminal(West) ✓

Query matched! □

3. Backward Chaining (Goal-Driven)

Algorithm: FOL Backward Chaining

Start from the query (goal) and work backward: find rules whose head matches the goal, then recursively prove the body literals.

Backward Chaining Pseudocode
function FOL-BC-ASK(KB, goal, θ):
  for each rule (P1 ∧ ... ∧ Pn → Q) in KB:
    θ' ← UNIFY(Q, goal, θ)
    if θ' ≠ failure:
      // Prove each body literal
      answers ← FOL-BC-ALL(KB, [P1,...,Pn], θ')
      for each θ'' in answers:
        yield θ''
  for each fact F in KB:
    θ' ← UNIFY(F, goal, θ)
    if θ' ≠ failure:
      yield θ'
Backward Chaining Trace (same KB)

Goal: Criminal(West)

  1. Match rule 1 head: Criminal(x), θ={x/West}
  2. Subgoals: American(West), Weapon(y), Sells(West,y,z), Hostile(z)
  3. American(West) — fact 8 ✓
  4. Weapon(y) — match rule 5: Missile(y), try y=M1 — fact 3 ✓ ⇒ Weapon(M1) ✓
  5. Sells(West, M1, z) — match rule 4: Missile(M1)✓ ∧ Owns(Nono,M1)✓ ⇒ z=Nono ✓
  6. Hostile(Nono) — match rule 7: Enemy(Nono, America) — fact 6 ✓

All subgoals proved ⇒ Criminal(West) confirmed. □

4. Comparison Table

AspectForward ChainingBackward Chaining
DirectionData → GoalGoal → Data
Driven byKnown factsQuery/goal
StrategyBottom-up, breadth-firstTop-down, depth-first
CompletenessComplete for DatalogComplete with loop detection
EfficiencyMay derive irrelevant factsFocuses on query-relevant facts
TerminationGuaranteed for DatalogMay loop without checks
Use casesProduction systems, monitoringQuery answering, Prolog

5. Connection to Prolog

Prolog implements backward chaining on definite clauses with depth-first search and unification. Key features:

  • Rules written as head :- body1, body2, ... (reads as head if body1 and body2 and …)
  • Uses SLD resolution (Selective Linear Definite clause resolution).
  • Prolog typically omits the occur check for efficiency.
  • Closed-world assumption: anything not provable is assumed false (negation as failure).
Prolog Version of the Criminal Example
criminal(X) :- american(X), weapon(Y),
               sells(X,Y,Z), hostile(Z).
sells(west, X, nono) :- missile(X), owns(nono, X).
weapon(X) :- missile(X).
hostile(X) :- enemy(X, america).
american(west).
missile(m1).
owns(nono, m1).
enemy(nono, america).

?- criminal(west).
% true.

Summary

  • Forward chaining is data-driven; backward chaining is goal-driven.
  • Both work on definite clauses (Horn clauses with one positive literal).
  • Datalog (no functions) ensures termination of forward chaining.
  • Prolog = backward chaining + depth-first search + unification.
  • Forward chaining is often used for monitoring/alerting; backward for querying.

References

  • Russell, S. & Norvig, P. AIMA, 4th Ed. — Sections 9.3–9.4
  • Bratko, I. Prolog Programming for Artificial Intelligence, 4th Ed.