Expert Systems & Prolog
An expert system uses stored knowledge and logical rules to make decisions within a specific domain. In this lesson, you will connect expert-system architecture with Prolog: facts, rules, queries and logical inference.
Learning Objective
- 12.5.1.2 Create a simple expert system using Prolog
Success Criteria
I can identify the main components of an expert system · distinguish forward and backward chaining · write Prolog facts, rules and queries · trace a simple Prolog conclusion · create a small rule-based expert system.
Conceptual Anchor
The Expert's Decision Rules
Imagine a specialist who makes decisions using explicit rules: IF condition A and condition B are true, THEN conclude C. A traditional rule-based expert system stores this specialist knowledge and applies the rules systematically to known facts.
1 Simple Computer Support Rule
Known facts:
- no_power_light
- charger_connected
Rule:
IF no_power_light AND charger_connected
THEN possible_power_supply_problem
Conclusion:
possible_power_supply_problemExpert System Architecture
Select a component to see its role.
User
The user provides facts, observations or a query and receives the system's conclusion.
| Component | Main role |
|---|---|
| Knowledge Base | Stores domain knowledge as facts and rules. |
| Inference Engine | Selects and applies rules to facts to derive conclusions. |
| User Interface | Allows the user to enter information and receive conclusions. |
| Explanation Facility | Shows how a conclusion was reached or why a question was asked. |
How an Expert System Works
Forward vs Backward Chaining
Expert systems may reason from available data toward conclusions, or start from a goal and work backward to find supporting facts.
Forward Chaining
Starts from known facts and repeatedly applies rules to derive new facts.
DATA → CONCLUSION| Forward Chaining | Backward Chaining |
|---|---|
| Data-driven | Goal-driven |
| Starts from known facts | Starts from a hypothesis or query |
| “What can I conclude?” | “Can this goal be proved?” |
| Rules derive new facts | Rules identify facts needed to prove the goal |
Applications of Expert Systems
Healthcare
Rule-based support for diagnosis and clinical decision processes.
Engineering
Fault diagnosis in mechanical or electronic systems.
Geology
Mineral and oil exploration using geological evidence and expert rules.
Finance
Tax, financial or rule-based decision support.
Logistics
Decision support for routing and operational planning.
Identification
Identification of plants, animals or chemical/biological compounds.
Historical examples: MYCIN and DENDRAL
| System | Domain | Main purpose |
|---|---|---|
| MYCIN | Medicine | Rule-based support for diagnosing bacterial infections and recommending antibiotics. |
| DENDRAL | Chemistry | Used expert knowledge to help infer molecular structures from experimental data. |
Extension: Other reasoning approaches
| Approach | Idea |
|---|---|
| Rule-Based Reasoning | Uses explicit IF–THEN rules. |
| Certainty Factors | Attach a numerical degree of confidence to conclusions. |
| Fuzzy Logic | Represents partial truth rather than only true/false. |
| Probabilistic Reasoning | Uses probabilities to represent uncertainty. |
| Case-Based Reasoning | Uses similarities with previously solved cases. |
Advantages & Limitations
| Advantages | Limitations |
|---|---|
| Can preserve specialist knowledge. | Knowledge acquisition and maintenance can be expensive. |
| Can provide fast and consistent rule-based decisions. | Usually limited to a narrow problem domain. |
| Can store large amounts of facts and rules. | Only as reliable as the knowledge entered into the system — GIGO. |
| Can operate whenever the computer system is available. | May handle unusual situations poorly when no appropriate rule exists. |
| Can provide traceable reasoning when an explanation facility is present. | Users may incorrectly assume the system is always correct. |
Traditional Expert Systems ≠ Machine Learning
Traditional rule-based expert systems use an explicitly created knowledge base and inference rules. Machine-learning systems learn statistical patterns from data. Rule-based systems do not automatically learn new patterns from training data; their knowledge must be updated explicitly.
Implementing Knowledge with Prolog
Prolog means Programming in Logic. Instead of describing a step-by-step algorithm, the programmer declares facts and rules, then asks queries.
johnUsually begins
with a lowercase letter.PersonBegins
with uppercase or underscore.male(john).A
statement stored as true in the knowledge base.father(X,Y) :- ...A
conclusion true when its conditions are true.?- father(X,mary).A
question Prolog attempts to prove.Reading a Prolog Rule
father(X,Y)Head / Conclusion:-IFmale(X)Condition,ANDparent(X,Y)Condition.End| Symbol | Meaning |
|---|---|
:- |
“is true IF” |
, |
logical AND |
; |
logical OR |
\= |
not equal / cannot be unified as equal |
. |
end of a fact or rule |
2 Family Knowledge Base
% FACTS
male(charles).
male(william).
male(harry).
female(diana).
parent(charles, william).
parent(diana, william).
parent(charles, harry).
parent(diana, harry).
% RULES
mother(X, Y) :-
female(X),
parent(X, Y).
sibling(X, Y) :-
parent(Z, X),
parent(Z, Y),
X \= Y.
brother(X, Y) :-
male(X),
sibling(X, Y).How Prolog Finds Answers
Two useful ideas explain Prolog behaviour: unification matches variables with values that satisfy a fact or rule, while backtracking lets Prolog return to an earlier choice and search for another solution.
parent(charles, X).X = williamX = harry
Extension concept
For this lesson, students only need unification and backtracking at a simple conceptual level.
Prolog Query Lab
Select a query to see how the same knowledge base can prove facts or find values for variables.
Knowledge Base
male(charles). male(william). male(harry). female(diana). parent(charles, william). parent(diana, william). parent(charles, harry). parent(diana, harry). mother(X,Y) :- female(X), parent(X,Y). sibling(X,Y) :- parent(Z,X), parent(Z,Y), X \= Y. brother(X,Y) :- male(X), sibling(X,Y).
Choose a query
Prolog Error Checker
Many beginner errors come from capitalization, missing punctuation or incorrect rule syntax.
male(John).Build a Mini Expert System
A family tree is useful for learning Prolog syntax. Now use the same ideas to create a domain-specific expert system.
Computer troubleshooting.
Examples: no_power_light, fan_loud, computer_hot.
Connect combinations of symptoms to conclusions.
Test whether a problem can be inferred.
State which facts and rules led to each result.
3 Computer Troubleshooting Knowledge Base
% FACTS
no_power_light.
charger_connected.
fan_loud.
computer_hot.
wifi_connected.
internet_not_working.
% RULES
problem(power_supply) :-
no_power_light,
charger_connected.
problem(overheating) :-
fan_loud,
computer_hot.
problem(network) :-
wifi_connected,
internet_not_working.
% QUERY EXAMPLES
% ?- problem(power_supply).
% ?- problem(X).Minimum project requirements
At least 5 facts · at least 3 rules · at least 3 queries · one query containing a variable · a short explanation of how one conclusion was reached.
Common Pitfalls
1. Atom vs Variable
john is an atom. John is a variable because it begins with
uppercase.
2. Missing Full Stop
Prolog facts and rules normally end with .
3. Rule Direction
Read father(X,Y) :- male(X), parent(X,Y). as “father(X,Y) is true IF male(X)
AND parent(X,Y) are true.”
4. Forward ≠ Backward Chaining
Forward chaining begins with facts; backward chaining begins with a goal or query.
5. Expert Systems Are Domain-Specific
A rule-based system can only reason with the knowledge represented in its knowledge base.
Practice Tasks
Name the four main components of an expert system and state the role of each.
Explain the difference between forward and backward chaining. Which one begins with a hypothesis?
Classify each as Atom, Variable, Fact, Rule or Query: john,
Person, male(john)., father(X,Y) :- ....
Given parent(X,Y), write a Prolog rule for
grandparent(X,Z).
Predict the answers to ?- parent(charles, X). using the family knowledge
base.
Trace how Prolog can prove brother(harry, william) from the provided
facts and rules.
Find and correct errors in male(John) and
father(X,Y) : male(X), parent(X,Y).
Create a simple expert system in Prolog with at least 5 facts, 3 rules and 3 queries.
Exam Practice
Q1: State the purpose of the knowledge base in an expert system. [1]
Reveal marking point
Q2: Explain the role of the inference engine. [2]
Reveal marking points
Q3: Explain one difference between forward and backward chaining. [2]
Reveal marking points
Q4: Explain two limitations of an expert system. [4]
Reveal marking points
Self-Check Quiz
Q1: What does the Knowledge Base store?
Q2: Which component applies rules to facts?
Q3: Which chaining method starts with a goal?
Q4: What is the difference between john and
John?
john is an
atom; John is a variable.Q5: What does :- mean?
Q6: What does a comma mean inside a Prolog rule?
Q7: Why is the Explanation Facility useful?