Unit 12.1B · Term 1

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.

Lesson Presentation

Expert Systems & Prolog · Slides for classroom use

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_problem

Expert System Architecture

Expert System Architecture — User Interface, Inference Engine and Knowledge Base
The user supplies information through the User Interface. The Inference Engine applies rules from the Knowledge Base to reach a conclusion.

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

1
User provides factssymptoms, measurements or observations
2
Facts enter the systemthrough the user interface
3
Rules are searchedfor conditions matching known facts
4
Matching rule firesthe inference engine applies logic
5
Conclusion is producedor a new fact is derived
6
Reason is explainedthe reasoning path can be inspected

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.

AtomjohnUsually begins with a lowercase letter.
VariablePersonBegins with uppercase or underscore.
Factmale(john).A statement stored as true in the knowledge base.
Rulefather(X,Y) :- ...A conclusion true when its conditions are true.
Query?- father(X,mary).A question Prolog attempts to prove.

Reading a Prolog Rule

father(X,Y)Head / Conclusion
:-IF
male(X)Condition
,AND
parent(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.

Queryparent(charles, X).
UnificationMatch facts with the query pattern.
BacktrackingSearch again after the first answer.
AnswersX = william
X = 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).
Choose the most important issue.

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.

1Choose the domain

Computer troubleshooting.

2Define facts

Examples: no_power_light, fan_loud, computer_hot.

3Write rules

Connect combinations of symptoms to conclusions.

4Ask queries

Test whether a problem can be inferred.

5Explain reasoning

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

Remember

Name the four main components of an expert system and state the role of each.

Understand

Explain the difference between forward and backward chaining. Which one begins with a hypothesis?

Apply

Classify each as Atom, Variable, Fact, Rule or Query: john, Person, male(john)., father(X,Y) :- ....

Apply

Given parent(X,Y), write a Prolog rule for grandparent(X,Z).

Apply

Predict the answers to ?- parent(charles, X). using the family knowledge base.

Analyze

Trace how Prolog can prove brother(harry, william) from the provided facts and rules.

Analyze

Find and correct errors in male(John) and father(X,Y) : male(X), parent(X,Y).

Create

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
Stores domain knowledge, such as facts and rules, that the inference engine can use.

Q2: Explain the role of the inference engine. [2]

Reveal marking points
It selects/applies relevant rules to known facts and uses logical reasoning to derive new facts or conclusions.

Q3: Explain one difference between forward and backward chaining. [2]

Reveal marking points
Forward chaining starts from known facts and derives conclusions; backward chaining starts from a goal/hypothesis and searches for facts/rules that prove it.

Q4: Explain two limitations of an expert system. [4]

Reveal marking points
Examples: expensive knowledge acquisition/maintenance; narrow domain; unusual cases may not match any rule; results depend on the quality of stored knowledge; users may place too much trust in the system.

Self-Check Quiz

Q1: What does the Knowledge Base store?

Domain knowledge such as facts and rules.

Q2: Which component applies rules to facts?

The Inference Engine.

Q3: Which chaining method starts with a goal?

Backward chaining.

Q4: What is the difference between john and John?

john is an atom; John is a variable.

Q5: What does :- mean?

It means “is true IF”.

Q6: What does a comma mean inside a Prolog rule?

Logical AND.

Q7: Why is the Explanation Facility useful?

It shows how a conclusion was reached or why the system asked a question.