Prolog 101 PDF
Prolog 101 PDF
Programming Languages
C Java Prolog
Procedural Programming Object-oriented Logic Programming
Language Programming Language Language
❑ Steps ❑ Objects ❑ Facts
❑ Relationships ❑ Rules
Expert System
GOAL: to teach computers to think like human experts in a
particular domain
facts
output
rules
An Expert System in Prolog
Expert System – an AI program that uses domain-specific
knowledge to provide “expert quality” performance in a problem
domain.
ES Components:
▪ Knowledge Base Power resides here
▪ Inference Engine
▪ Explanation Subsystem
Expert System Shell
▪ User Interface
▪ Knowledge Base Editor
Knowledge Engineering – systematic process of Expert System
development
Typical Expert System Architecture
Starting with Prolog
nl – new line
My first knowledge base in Prolog
Type start.
nl – new line
My first knowledge base in Prolog
Mary, Sandra,
Juliet, and Lisa are
females. Peter,
Bob, Paul, and
Drew are males.
Semi-colon = OR
NOTICE: convention is different!
My first knowledge base in Prolog
Queries are
written this way.
Comma = AND
My first knowledge base in Prolog
Queries are written
this way.
Semi-colon = OR
My first knowledge base in Prolog
Mary and
Peter are
parents of Bob.
Drew and Paul
are parents of
Juliet. Lisa is a
parent of
Peter.
My first knowledge base in Prolog
Who are the
parents of
Bob?
My first knowledge base in Prolog
A male parent is a
father, while a
female parent is a
mother.
Procedures are
written this way.
NOTICE: if is written as :-
My first knowledge base in Prolog
• Who is the father
of Bob?
• Whose mother is
Mary?
• Who is the
mother of Juliet
and Peter?
• Whose father is
Bob?
NOTICE:
My first knowledge base in Prolog
• You can display
the sentence like
an actual English
sentence,
wherein names
start with
uppercase letter.
• Enclose values
with quotations
if you want it to
start with
uppercase letter.
My first knowledge base in Prolog
• Enclose values
with quotations
if you want it to
start with
uppercase letter.
• ~w for value
• ~s for string
• ~n for new line
My first knowledge base in Prolog
• Another way to
format the
sentence.
• ~w for value
• ~s for string
• ~n for new line
My first knowledge base in Prolog
• debug. – to open
the debugger.
• nodebug. – to
close the debugger.
My first knowledge base in Prolog
• trace. – to open
the tracing.
• notrace. – to
close the tracing.
• nodebug. – to
close the tracing.
My first knowledge base in Prolog
• Procedures are
written this way.
• This allows
recursion.
NOTICE:
My second knowledge base in Prolog
What does
20.55 and
120.55 mean?
My second knowledge base in Prolog
Structures
are written this
way.
My first knowledge base in Prolog
What does
20.55 and
120.55 mean?
My first knowledge base in Prolog
• Structures are
written this way.
My first knowledge base in Prolog
• Structures are
written this way.
• A variable starting
with underscore
(_) is an
anonymous
variable.
My first knowledge base in Prolog
• You can also
perform
operations.
• Enclose values
with quotations
if you want it to
start with
uppercase letter.
My first knowledge base in Prolog
• Loops are written
this way.
My first knowledge base in Prolog
• Loops run this
way.
How to modify knowledge base?
:- dynamic(fact/arity). – will dictate which
predicates are editable.
assertz(fact). – will add fact at the end of the group
with same predicate.
asserta(fact). – will add fact at the start of the group
with same predicate.
retract(fact). – will remove fact from the knowledge
base.
NOTICE: file will not be updated, only the
data in the current run.
How to modify knowledge base?
:- dynamic(fact/arity). – will dictate which
predicates are editable.
assertz(fact). – will add fact at the end of the group
with same predicate.
asserta(fact). – will add fact at the start of the group
with same predicate.
retract(fact). – will remove fact from the knowledge
base.
NOTICE: study file manipulation in Prolog
if you want the file to be updated as well.
My second knowledge base in Prolog
3. 𝑑𝑜𝑔(𝐹𝑖𝑑𝑜) Given
Starts with
Capital
letter for
variables.
NOTICE: convention is different!
My second knowledge base in Prolog
Our
knowledge
base, mykb,
entails the
given clause.
𝑚𝑦𝑘𝑏
⊨ 𝑒𝑎𝑡(𝑓𝑖𝑑𝑜)
My third knowledge base in Prolog
All people that are not poor and are smart are happy.
Those people that read are not stupid. John can read and
is wealthy. Happy people have exciting lives. Can
someone be found with an exciting life?
My third knowledge base in Prolog
Let poor(x) – x is poor or not wealthy.
smart(x) – x is smart or not stupid.
happy(x) – x is happy.
read(x) – x can read.
exciting(x) – x has exciting life.
1. ∀𝑥 ((¬𝑝𝑜𝑜𝑟(𝑥) 𝑠𝑚𝑎𝑟𝑡(𝑥)) → ℎ𝑎𝑝𝑝𝑦(𝑥))
2. ∀𝑦 (𝑟𝑒𝑎𝑑(𝑦) → 𝑠𝑚𝑎𝑟𝑡(𝑦))
3. 𝑟𝑒𝑎𝑑(𝑗𝑜ℎ𝑛) ¬𝑝𝑜𝑜𝑟(𝑗𝑜ℎ𝑛) remove
4. ∀𝑧 (ℎ𝑎𝑝𝑝𝑦(𝑧) → 𝑒𝑥𝑐𝑖𝑡𝑖𝑛𝑔(𝑧)) quantifiers