Forward & Backward Chaining in FOL
Prerequisites
- FOL Inference Rules — UI, EI, Modus Ponens
- Unification — MGU, substitution
🎯 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
A definite clause is a disjunction of literals with exactly one positive literal:
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 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)
Start from known facts and repeatedly apply rules to derive new facts until the query is answered or no new facts can be derived.
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
KB:
- American(x) ∧ Weapon(y) ∧ Sells(x, y, z) ∧ Hostile(z) → Criminal(x)
- Owns(Nono, M1)
- Missile(M1)
- Missile(x) ∧ Owns(Nono, x) → Sells(West, x, Nono)
- Missile(x) → Weapon(x)
- Enemy(Nono, America)
- Enemy(x, America) → Hostile(x)
- 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)
Start from the query (goal) and work backward: find rules whose head matches the goal, then recursively prove the body literals.
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 θ'
Goal: Criminal(West)
- Match rule 1 head: Criminal(x), θ={x/West}
- Subgoals: American(West), Weapon(y), Sells(West,y,z), Hostile(z)
- American(West) — fact 8 ✓
- Weapon(y) — match rule 5: Missile(y), try y=M1 — fact 3 ✓ ⇒ Weapon(M1) ✓
- Sells(West, M1, z) — match rule 4: Missile(M1)✓ ∧ Owns(Nono,M1)✓ ⇒ z=Nono ✓
- Hostile(Nono) — match rule 7: Enemy(Nono, America) — fact 6 ✓
All subgoals proved ⇒ Criminal(West) confirmed. □
4. Comparison Table
| Aspect | Forward Chaining | Backward Chaining |
|---|---|---|
| Direction | Data → Goal | Goal → Data |
| Driven by | Known facts | Query/goal |
| Strategy | Bottom-up, breadth-first | Top-down, depth-first |
| Completeness | Complete for Datalog | Complete with loop detection |
| Efficiency | May derive irrelevant facts | Focuses on query-relevant facts |
| Termination | Guaranteed for Datalog | May loop without checks |
| Use cases | Production systems, monitoring | Query 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).
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.