An expert system is a program that captures the know-how of a human expert as a set of rules, then uses those rules to answer questions or make decisions in that expert's field.
What Is an Expert System?
An expert system solves problems in a narrow domain the same way a human specialist would — by applying stored expert knowledge to the specific facts of a situation. Instead of one block of code with the logic baked in, the knowledge and the reasoning are kept separate, which makes the rules easy to inspect, explain, and update.
Components of an Expert System
Every expert system is built from three core parts.
Knowledge Base
The knowledge base stores everything the system knows about the domain — facts and IF-THEN rules written by capturing a human expert's knowledge, like IF has_fever AND has_cough THEN suspect_flu.
Inference Engine
The inference engine is the reasoning part. It takes the facts it's given, checks them against the rules in the knowledge base, and works out what new facts or conclusions follow.
How Expert Systems Make Decisions
The inference engine reaches a conclusion by chaining rules together, in one of two directions.
- Forward chaining starts with known facts and fires any rule whose conditions are met, adding new facts until nothing more can be concluded.
- Backward chaining starts with a goal (like "does the patient have the flu?") and works backward, checking what facts would be needed to prove it.
Example: A Simple Forward-Chaining Expert System
This toy system uses forward chaining to reach a diagnosis from a small set of starting facts. It's a simplified illustration of the mechanism, not real medical guidance.
rules = [
(["has_fever", "has_cough"], "suspect_flu"),
(["suspect_flu", "has_bodyache"], "diagnosis_flu"),
(["has_fever", "has_rash"], "suspect_measles"),
(["suspect_measles", "has_watery_eyes"], "diagnosis_measles"),
]
def forward_chain(known_facts):
facts = set(known_facts)
changed = True
fired = []
while changed:
changed = False
for conditions, conclusion in rules:
if all(c in facts for c in conditions) and conclusion not in facts:
facts.add(conclusion)
fired.append((conditions, conclusion))
changed = True
return facts, fired
known_facts = ["has_fever", "has_cough", "has_bodyache"]
facts, fired = forward_chain(known_facts)
print("Starting facts:", known_facts)
print()
print("Rules fired:")
for conditions, conclusion in fired:
print(f" IF {' AND '.join(conditions)} THEN {conclusion}")
print()
print("Final facts:", sorted(facts))Output
Starting facts: ['has_fever', 'has_cough', 'has_bodyache']
Rules fired:
IF has_fever AND has_cough THEN suspect_flu
IF suspect_flu AND has_bodyache THEN diagnosis_flu
Final facts: ['diagnosis_flu', 'has_bodyache', 'has_cough', 'has_fever', 'suspect_flu']Explanation:
The first rule fires because both has_fever and has_cough are already known, adding suspect_flu. That new fact then satisfies the second rule's conditions along with has_bodyache, adding diagnosis_flu. The measles rules never fire, since has_rash was never in the starting facts — this is forward chaining: each new fact can unlock further rules, until nothing new is left to conclude.
Real Examples: MYCIN
MYCIN, built at Stanford in the 1970s, is one of the earliest and best-known expert systems. It used around 600 rules to help diagnose bacterial infections and recommend antibiotics, working through a backward-chaining dialogue that asked the doctor for exactly the facts it needed to test each rule.
MYCIN also attached a certainty factor to its conclusions, since medical evidence is rarely 100% conclusive. It was never used on real patients, but studies found its recommendations matched expert judgment about as often as human specialists did.
Quick Summary
| Part | Role |
| Knowledge Base | Stores facts and IF-THEN rules from a human expert |
| Inference Engine | Applies the rules to reach conclusions |
| Forward Chaining | Starts from facts, works toward a conclusion |
| Backward Chaining | Starts from a goal, works backward to the facts needed |
| MYCIN | Real expert system for diagnosing bacterial infections |