Skip to content
Tutorials Center
My learning

Topics

Lesson 28 of 36 ยท Rule-based Expert Systems

Backward Chaining: Goal-Driven Reasoning

Build a backward-chaining engine that proves a goal by finding rules that conclude it, recursing on sub-goals and asking the user only for missing facts. Runnable Python.

Forward chaining answers "what follows from what I know?". Backward chaining answers a different question: "is this goal true, and what do I need to find out to prove it?" It starts from a hypothesis, looks for rules that could conclude it, and turns each condition of those rules into a sub-goal. If a sub-goal cannot be proven from rules, the system asks the user (or checks a sensor).

This is how a doctor reasons: "could it be strep throat? Strep throat needs fever and swollen glands, so let me check for those". It is also how Prolog works. Because it only looks at what is relevant to the goal, backward chaining asks fewer questions than a system that gathers everything first.

The proof procedure

To prove goal G:

  1. If G is already a known fact, it is proven.
  2. Otherwise find every rule with G as its conclusion. For each one, try to prove all its conditions (each one is a new goal). If all succeed, G is proven.
  3. If no rule concludes G (it is a leaf), ask the user, and remember the answer.
  4. If everything fails, G is not proven.
flowchart TD
  G["Goal: bird_is_penguin"] --> C1["can_swim?"]
  G --> C2["cannot_fly?"]
  G --> C3["black_and_white?"]
  C1 -->|"ask user"| A1["yes"]
  C2 -->|"ask user"| A2["yes"]
  C3 -->|"ask user"| A3["yes"]

An AND tree: the goal holds only if all of its conditions hold. Leaf goals are answered by the user.

Backward chaining

Start with a goal Is it a penguin?
Find rules Which conclude it?
Sub-goals Prove each condition
Ask Only for missing facts

Depth-first, goal-directed

1 / 2

A backward chainer in Python

The engine below proves a goal recursively. The ask function stands in for the user; here it reads scripted answers and records every question, so you can see how few questions are asked.

Python Proving a goal, asking only when needed

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The engine asked only four questions (has_feathers, lays_eggs, cannot_fly, swims). It never asked about milk, trunks or necks, because those facts are irrelevant to the goal penguin. A forward-chaining system would have wanted every fact.

Notice that all(...) short-circuits: as soon as one condition fails, the remaining conditions are not asked. The order of conditions in a rule therefore matters for efficiency. Put the cheapest or most decisive question first.

Trying several goals

A diagnostic system usually has several candidate hypotheses. It tests them one after another, and, thanks to the known memory, answers asked for one hypothesis are reused for the next.

Python Testing several hypotheses with shared answers

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

bird is proven once and reused. The second hypothesis, swan, only needs to ask about can_fly and long_neck because everything about birds is already known.

Avoiding loops

If one rule says A if B and another says B if A, a naive prover recurses forever. Fix it by remembering the goals currently under proof and failing a goal that appears again on its own path.

Python Detecting a circular rule set

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

When to choose backward chaining

  • You have a specific question or small set of hypotheses.
  • Gathering facts is expensive (lab tests, user questions), so you want only the necessary ones.
  • You want a natural why explanation: "I am asking about swimming because I am trying to show this is a penguin".

Mixed strategies are common. Many real systems use forward chaining to react to incoming data and backward chaining when a user asks "why is this alarm on?" or "could it be X?". The next lesson adds certainty to the conclusions with certainty factors and explanations.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does a backward chainer do when a goal has no rule concluding it?

    Leaf goals are not derivable from rules, so the value must come from outside.
  2. 2. Why does backward chaining usually ask fewer questions?

    Goal-directed search limits the facts needed to those in the goal's proof tree.
  3. 3. What does the AND in a rule's condition list mean for proof?

    A rule fires only when all its conditions hold, so every sub-goal must be proven.
  4. 4. How can you stop infinite recursion with circular rules?

    A goal that reappears on its own proof path cannot help prove itself.

Summary

  • Backward chaining proves a goal by finding rules that conclude it and proving their conditions as sub-goals.
  • Leaves are answered by the user, and answers are remembered.
  • It asks only relevant questions and explains "why" naturally; condition order affects efficiency.
  • Track the proof path to avoid loops.
  • Choose it for diagnosis and goal questions; use forward chaining for data-driven reactions.

Next: Certainty Factors and Explanations