0% found this document useful (0 votes)
303 views61 pages

Prolog 101 PDF

This document discusses Prolog, an AI programming language used for logic programming and building expert systems. It provides an overview of Prolog, including its use of facts and rules to represent knowledge. It then demonstrates how to set up and run a basic Prolog program, representing knowledge and querying the knowledge base. Examples show facts, rules, queries, variables, structures, recursion, and modifying the knowledge base.

Uploaded by

Anonymous
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
303 views61 pages

Prolog 101 PDF

This document discusses Prolog, an AI programming language used for logic programming and building expert systems. It provides an overview of Prolog, including its use of facts and rules to represent knowledge. It then demonstrates how to set up and run a basic Prolog program, representing knowledge and querying the knowledge base. Examples show facts, rules, queries, variables, structures, recursion, and modifying the knowledge base.

Uploaded by

Anonymous
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 61

Prolog 101 Pau Rivera

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

Download and install SWI-Prolog


from https://www.swi-prolog.org/
Starting with Prolog
Open
Prolog and
you will
see the
Expert
System
shell.
Starting with Prolog
Click:
❑ New under
File to
create a
new
knowledge
base.
❑ Edit to
open an
existing
one.
Starting with Prolog
After
clicking
New/Edit,
the
Knowledge
Base editor
should
appear.
Running your code
1. Make sure
you are at
the correct
directory.
2. Type
consult(‘file
name’). or
[mykb]. to
start the
expert
system, or
click Consult
under File. NOTICE: always end with a period (.)
Representing Facts, Rules, …
Facts atom

▪ male(juan). female(maria). male(jose).


▪ parent(juan,maria). parent(juan,jose).
Rules variable
head ▪ offspring(Y,X) :- parent(X,Y). clause
▪ grandparent(X,Z) :-
parent(X,Y), if
parent(Y,Z).
body
comma = conjunction (and)
goal
… Procedures & Queries
Procedures predicate name: predecessor
▪ predecessor(X,Z) :- predicate arity: 2
parent(X,Z).
▪ predecessor(X,Z) :-
parent(X,Y), recursive rule
body predecessor(Y,Z).
Queries clause
▪ ?- parent(juan,X).
X=maria; variable instantiations
X=jose;
no semicolon = disjunction (or)
Backtracks
here goal satisfiability/failure
Hello World in Prolog
write(‘’). –
to display

nl – new line
My first knowledge base in Prolog
Type start.

NOTICE: the clause


ends with a
period (.)
Hello World in Prolog
write(‘’).
– to display

nl – new line
My first knowledge base in Prolog
Mary, Sandra,
Juliet, and Lisa are
females. Peter,
Bob, Paul, and
Drew are males.

Facts are written


this way.

NOTICE: always end with a period (.)


My first knowledge base in Prolog
Who are the
females?

Queries are written


this way.

Small letters for


specific values

Starts with Capital


letter for variables NOTICE: convention is different!
My first knowledge base in Prolog
Queries are written
this way.

Small letters for


specific values

Starts with Capital


letter for variables

Semi-colon = OR
NOTICE: convention is different!
My first knowledge base in Prolog
Queries are
written this way.

Small letters for


specific values

Starts with Capital


letter for variables

Comma = AND
My first knowledge base in Prolog
Queries are written
this way.

Small letters for


specific values

Starts with Capital


letter for variables

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.

Rules are written


this way.

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: returns false of nothing from the


KB can make it true.
My first knowledge base in Prolog
• write(‘’). –
to display
• write(‘’). – is
like a built-in
procedure
• Text will be
displayed if the
conditions are
satisfied.
My first knowledge base in Prolog
• A child is a
grandchild of
someone if a
parent of the child
is a child of
someone.

• You can display


values of
variables.
My first knowledge base in Prolog
• Who is the
grandchild of
Lisa?

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.

NOTICE: lisa is not accepted anymore. It


should be Lisa.
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
• 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.

NOTICE: the first query is a variable


assignment, NOT a comparison.
Prolog Operators Summary
Operators in other languages Prolog Operators
And ,
Or ;
Negation not() \+
Implication :- (denotes if)
= = =:=
!= =\=
>, < > <
<=, >= =< =>
+, -, /, //, * + - / // *
^ (exponent) **
and more…
My first knowledge base in Prolog
• read(Var). – to
read user input.
My first knowledge base in Prolog
• Pau is a variable.
• pau is a value.
• ‘Pau’ is a value.

• 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

Given the following sentences:


1. All dogs are animals. must
convert to
2. All animals eat. logical
3. Fido is a dog. expressions

Prove that: Fido will eat.


My second knowledge base in Prolog

Given the following sentences:


1. All dogs are animals.
2. All animals eat.
3. Fido is a dog.

Prove that: Fido will eat.

Let dog(x) – x is a dog.


animal(x) – x is an animal.
eat(x) – x eats.
My second knowledge base in Prolog

Let dog(x) – x is a dog.


animal(x) – x is an animal.
eat(x) – x eats.
1. ∀𝑥(𝑑𝑜𝑔 𝑥 → 𝑎𝑛𝑖𝑚𝑎𝑙 𝑥 ) Given remove
2. ∀𝑦 𝑎𝑛𝑖𝑚𝑎𝑙 𝑦 → 𝑒𝑎𝑡 𝑦 Given quantifiers

3. 𝑑𝑜𝑔(𝐹𝑖𝑑𝑜) Given

Prove that: 𝑒𝑎𝑡(𝐹𝑖𝑑𝑜)


My second knowledge base in Prolog

Let dog(x) – x is a dog.


animal(x) – x is an animal.
eat(x) – x eats.
1. ∀𝑥(𝑑𝑜𝑔 𝑥 → 𝑎𝑛𝑖𝑚𝑎𝑙 𝑥 ) Given
2. ∀𝑦 𝑎𝑛𝑖𝑚𝑎𝑙 𝑦 → 𝑒𝑎𝑡 𝑦 Given We are
feeding a rule
3. 𝑑𝑜𝑔(𝐹𝑖𝑑𝑜) Given to the
4. 𝑑𝑜𝑔 𝑥 → 𝑎𝑛𝑖𝑚𝑎𝑙 𝑥 1 (Universal Instantiation) computer, so
5. 𝑎𝑛𝑖𝑚𝑎𝑙 𝑦 → 𝑒𝑎𝑡 𝑦 2 (Universal Instantiation) do not assign
a specific
value. Retain
Prove that: 𝑒𝑎𝑡(𝐹𝑖𝑑𝑜)
the variable.
My second knowledge base in Prolog

Let dog(x) – x is a dog.


animal(x) – x is an animal.
eat(x) – x eats.
1. ∀𝑥(𝑑𝑜𝑔 𝑥 → 𝑎𝑛𝑖𝑚𝑎𝑙 𝑥 ) Given
2. ∀𝑦 𝑎𝑛𝑖𝑚𝑎𝑙 𝑦 → 𝑒𝑎𝑡 𝑦 Given
Convert to
3. 𝑑𝑜𝑔(𝐹𝑖𝑑𝑜) Given Horn Form.
4. 𝑑𝑜𝑔 𝑥 → 𝑎𝑛𝑖𝑚𝑎𝑙 𝑥 1 (Universal Instantiation) Then,
5. 𝑎𝑛𝑖𝑚𝑎𝑙 𝑦 → 𝑒𝑎𝑡 𝑦 2 (Universal Instantiation) translate to
Prolog
Prove that: 𝑒𝑎𝑡(𝐹𝑖𝑑𝑜)
My second knowledge base in Prolog
Small
letters for
specific
values.

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

Goal: ∃𝑥 𝑒𝑥𝑐𝑖𝑡𝑖𝑛𝑔(𝑥), find x instead of proving.


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.
5. ((¬𝑝𝑜𝑜𝑟(𝑥)  𝑠𝑚𝑎𝑟𝑡(𝑥)) → ℎ𝑎𝑝𝑝𝑦(𝑥)
6. (𝑟𝑒𝑎𝑑(𝑦) → 𝑠𝑚𝑎𝑟𝑡(𝑦)) Convert to
7. 𝑟𝑒𝑎𝑑(𝑗𝑜ℎ𝑛) ¬𝑝𝑜𝑜𝑟(𝑗𝑜ℎ𝑛) Horn Form,
8. (ℎ𝑎𝑝𝑝𝑦(𝑧) → 𝑒𝑥𝑐𝑖𝑡𝑖𝑛𝑔(𝑧)) then
translate to
Prolog
Goal: 𝑒𝑥𝑐𝑖𝑡𝑖𝑛𝑔(𝑥), find x instead of proving.
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.
9. ((¬𝑝𝑜𝑜𝑟(𝑥)  𝑠𝑚𝑎𝑟𝑡(𝑥)) → ℎ𝑎𝑝𝑝𝑦(𝑥)
10. 𝑟𝑒𝑎𝑑(𝑦) → 𝑠𝑚𝑎𝑟𝑡(𝑦)
11. 𝑟𝑒𝑎𝑑 𝑗𝑜ℎ𝑛
12. ¬𝑝𝑜𝑜𝑟(𝑗𝑜ℎ𝑛)
13. ℎ𝑎𝑝𝑝𝑦(𝑧) → 𝑒𝑥𝑐𝑖𝑡𝑖𝑛𝑔(𝑧)

Goal: 𝑒𝑥𝑐𝑖𝑡𝑖𝑛𝑔(𝑥), find x instead of proving.


My third knowledge base in Prolog
My third knowledge base in Prolog
Questions? ☺

You might also like