Two simple, efficient inference methods work with rules of the form "if conditions then conclusion".
Definite Clauses
A definite clause is a disjunction of literals with exactly one positive literal; it can be written as an implication p₁ ∧ p₂ ∧ ... ∧ pₙ → q, or as a fact when there are no premises. Knowledge bases of definite clauses are the basis of rule-based systems and Prolog. For such KBs, chaining is sound and complete, and runs in linear time in the size of the KB for propositional clauses.
Forward Chaining
Forward chaining is data-driven: start from the known facts, find a rule whose premises are all known, fire it to add its conclusion, and repeat until the query is derived or no new facts can be inferred (a fixed point). It works from facts towards goals and may derive many irrelevant facts. It is the method of production systems and expert-system shells (CLIPS) and suits monitoring and configuration problems. Example: rules A ∧ B → L; B ∧ L → M; L ∧ M → P; P → Q; facts A, B. Fire A ∧ B → L; then B ∧ L → M; then L ∧ M → P; then P → Q: the query Q is derived.
Backward Chaining
Backward chaining is goal-driven: start with the query q and look for rules that conclude q; to prove q, prove all premises of such a rule as new sub-goals, recursively; a goal that is a known fact succeeds. It works backwards and touches only relevant rules, so it is often much cheaper than forward chaining when there are many facts. It is the method of Prolog and of diagnostic systems, and is a depth-first search of an AND-OR graph. It may loop on recursive rules without loop checking. Example: to prove Q, prove P; to prove P, prove L and M; L needs A and B (known); M needs B and L (known).
Definition: Forward chaining. A data-driven inference method that applies rules to known facts to derive new facts until the goal is reached or no more facts can be derived.
Choosing the Direction
Use forward chaining when there are few goals and facts arrive as data; use backward chaining when there is a specific goal or question. Many systems use both. Forward chaining must be told when to stop; backward chaining answers one query at a time.
Table 3.4: Forward versus backward chaining
| Basis | Forward chaining | Backward chaining |
|---|---|---|
| Direction | Facts to goal | Goal to facts |
| Driven by | Data | Goal |
| Work done | May derive irrelevant facts | Only relevant rules |
| Typical use | Monitoring, planning | Diagnosis, queries, Prolog |
| Search | Breadth-like | Depth-first |
Example 3.4: Chaining on a small knowledge base
Problem: Rules: R1 A ∧ B → L; R2 B ∧ L → M; R3 L ∧ M → P; R4 P → Q. Facts: A, B. Prove Q by both methods.
Solution:
- Forward: A, B known; R1 fires → L; R2 (B, L) fires → M; R3 (L, M) fires → P; R4 fires → Q.
- Order of derivation: L, M, P, Q.
- Backward: goal Q needs P (R4); P needs L and M (R3); L needs A and B (R1: facts); M needs B and L (R2: both known).
- Sub-goals chain: Q ← P ← {L ← {A, B}, M ← {B, L}}.
Result: Q is proved by both methods; forward derives L, M, P, Q; backward verifies the same tree from the goal.
rules = [({"A", "B"}, "L"), ({"B", "L"}, "M"),
({"L", "M"}, "P"), ({"P"}, "Q")]
facts = {"A", "B"}
order = []
changed = True
while changed:
changed = False
for prem, concl in rules:
if prem <= facts and concl not in facts:
facts.add(concl); order.append(concl); changed = True
print("forward derived:", order)
def prove(goal, known={"A", "B"}):
if goal in known:
return True
return any(c == goal and all(prove(p) for p in pr)
for pr, c in rules)
print("backward proves Q:", prove("Q"))
Output
forward derived: ['L', 'M', 'P', 'Q']
backward proves Q: True
Common mistakes
- Using forward chaining for a single query and wasting effort on irrelevant facts.
- Forgetting the loop check in backward chaining with recursive rules.
- Applying chaining to sentences that are not definite clauses without conversion.
- Reversing the direction of the rules when chaining backward.
Key takeaways
- Definite clause: exactly one positive literal; rule form p₁ ∧ ... ∧ pₙ → q.
- Forward chaining is data-driven; backward chaining is goal-driven.
- Both are sound and complete for definite clauses; propositional chaining is linear time.
- Prolog uses backward chaining; production systems use forward chaining.
Practice questions
- Which method is goal-driven?
- What is a definite clause?
- When is forward chaining preferred?
Answers to the practice questions
- Backward chaining.
- A clause with exactly one positive literal, i.e. a rule with a single conclusion.
- When facts arrive as data and there is no single specific goal.
Sources
- Official BCA syllabus Artificial Intelligence, course code 26BCA404DS04, Unit III
