SlideShare a Scribd company logo
Prolog Programming
slides written by
Dr W.F. Clocksin
The Plan
• An example program
• Syntax of terms
• Some simple programs
• Terms as data structures, unification
• The Cut
• Writing real programs
What is Prolog?
• Prolog is the most widely used language to
have been inspired by logic programming
research. Some features:
• Prolog uses logical variables. These are not
the same as variables in other languages.
Programmers can use them as ‘holes’ in data
structures that are gradually filled in as
computation proceeds.
…More
• Unification is a built-in term-manipulation
method that passes parameters, returns
results, selects and constructs data
structures.
• Basic control flow model is backtracking.
• Program clauses and data have the same
form.
• The relational form of procedures makes it
possible to define ‘reversible’ procedures.
…More
• Clauses provide a convenient way to express
case analysis and nondeterminism.
• Sometimes it is necessary to use control
features that are not part of ‘logic’.
• A Prolog program can also be seen as a
relational database containing rules as well
as facts.
What a program looks like
/* At the Zoo */
elephant(george).
elephant(mary).
panda(chi_chi).
panda(ming_ming).
dangerous(X) :- big_teeth(X).
dangerous(X) :- venomous(X).
guess(X, tiger) :- stripey(X), big_teeth(X), isaCat(X).
guess(X, koala) :- arboreal(X), sleepy(X).
guess(X, zebra) :- stripey(X), isaHorse(X).
Prolog is a ‘declarative’ language
• Clauses are statements about what is true
about a problem, instead of instructions how
to accomplish the solution.
• The Prolog system uses the clauses to work
out how to accomplish the solution by
searching through the space of possible
solutions.
• Not all problems have pure declarative
specifications. Sometimes extralogical
statements are needed.
Example: Concatenate lists a and b
list procedure cat(list a, list b)
{
list t = list u = copylist(a);
while (t.tail != nil) t = t.tail;
t.tail = b;
return u;
}
In an imperative language
In a declarative language
In a functional language
cat(a,b) ≡
if b = nil then a
else cons(head(a),
cat(tail(a),b))
cat([], Z, Z).
cat([H|T], L, [H|Z]) :- cat(T, L, Z).
Complete Syntax of Terms
Term
Constant VariableCompound Term
Atom Number
alpha17
gross_pay
john_smith
dyspepsia
+
=/=
’12Q&A’
0
1
57
1.618
2.04e-27
-13.6
likes(john, mary)
book(dickens, Z, cricket)
f(x)
[1, 3, g(a), 7, 9]
-(+(15, 17), t)
15 + 17 - t
X
Gross_pay
Diagnosis
_257
_
Names an individual Stands for an individual
unable to be named when
program is written
Names an individual
that has parts
Compound Terms
parents(spot, fido, rover)
The parents of Spot are Fido and Rover.
Functor (an atom) of arity 3. components (any terms)
It is possible to depict the term as a tree:
parents
roverfidospot
Compound Terms
=/=(15+X, (0*a)+(2<<5))
Some atoms have built-in operator declarations so they
may be written in a syntactically convenient form. The
meaning is not affected. This example looks like an
arithmetic expression, but might not be. It is just a term.
<<
2
+
a
*
0
X
+
15
=/=
5
More about operators
• Any atom may be designated an operator. The only
purpose is for convenience; the only effect is how the
term containing the atom is parsed. Operators are
‘syntactic sugar’.
• We won’t be designating operators in this course, but
it is as well to understand them, because a number of
atoms have built-in designations as operators.
• Operators have three properties: position,
precedence and associativity.
more…
Examples of operator properties
Position Operator Syntax Normal Syntax
Prefix: -2 -(2)
Infix: 5+17 +(17,5)
Postfix: N! !(N)
Associativity: left, right, none.
X+Y+Z is parsed as (X+Y)+Z
because addition is left-associative.
Precedence: an integer.
X+Y*Z is parsed as X+(Y*Z)
because multiplication has higher precedence.
These are all the
same as the
normal rules of
arithmetic.
The last point about Compound Terms…
Constants are simply compound terms of arity 0.
badger
means the same as
badger()
Structure of Programs
• Programs consist of procedures.
• Procedures consist of clauses.
• Each clause is a fact or a rule.
• Programs are executed by posing queries.
An example…
Example
elephant(george).
elephant(mary).
elephant(X) :- grey(X), mammal(X), hasTrunk(X).
Procedure for elephant
Predicate
Clauses
Rule
Facts
Example
?- elephant(george).
yes
?- elephant(jane).
no
Queries
Replies
Clauses: Facts and Rules
Head :- Body. This is a rule.
Head. This is a fact.
‘if’
‘provided that’
‘turnstile’
Full stop at the end.
Body of a (rule) clause contains goals.
likes(mary, X) :- human(X), honest(X).
Head Body
Goals
Exercise: Identify all the parts of
Prolog text you have seen so far.
Interpretation of Clauses
Clauses can be given a declarative reading or a
procedural reading.
H :- G1, G2, …, Gn.
“That H is provable follows from goals
G1, G2, …, Gn being provable.”
“To execute procedure H, the
procedures called by goals G1, G2, …,
Gn are executed first.”
Declarative reading:
Procedural reading:
Form of clause:
male(bertram).
male(percival).
female(lucinda).
female(camilla).
pair(X, Y) :- male(X), female(Y).
?- pair(percival, X).
?- pair(apollo, daphne).
?- pair(camilla, X).
?- pair(X, lucinda).
?- pair(X, X).
?- pair(bertram, lucinda).
?- pair(X, daphne).
?- pair(X, Y).
Worksheet 2
drinks(john, martini).
drinks(mary, gin).
drinks(susan, vodka).
drinks(john, gin).
drinks(fred, gin).
pair(X, Y, Z) :-
drinks(X, Z),
drinks(Y, Z).
?- pair(X, john, martini).
?- pair(mary, susan, gin).
?- pair(john, mary, gin).
?- pair(john, john, gin).
?- pair(X, Y, gin).
?- pair(bertram, lucinda).
?- pair(bertram, lucinda, vodka).
?- pair(X, Y, Z).
This definition forces X and Y to be distinct:
pair(X, Y, Z) :- drinks(X, Z), drinks(Y, Z), X == Y.
Worksheet 3
berkshire
wiltshire
surrey
hampshire sussex
kent
How to represent this relation?
Note that borders are symmetric.
(a) Representing a symmetric relation.
(b) Implementing a strange ticket condition.
WS3
border(sussex, kent).
border(sussex, surrey).
border(surrey, kent).
border(hampshire, sussex).
border(hampshire, surrey).
border(hampshire, berkshire).
border(berkshire, surrey).
border(wiltshire, hampshire).
border(wiltshire, berkshire).
This relation represents
one ‘direction’ of border:
What about the other?
(a) Say border(kent, sussex).
border(sussex, kent).
(b) Say
adjacent(X, Y) :- border(X, Y).
adjacent(X, Y) :- border(Y, X).
(c) Say
border(X, Y) :- border(Y, X).
WS3
valid(X, Y) :- adjacent(X, Z), adjacent(Z, Y)
Now a somewhat strange type of discount ticket. For the
ticket to be valid, one must pass through an intermediate
county.
A valid ticket between a start and end county obeys the
following rule:
WS3
border(sussex, kent).
border(sussex, surrey).
border(surrey, kent).
border(hampshire, sussex).
border(hampshire, surrey).
border(hampshire, berkshire).
border(berkshire, surrey).
border(wiltshire, hampshire).
border(wiltshire, berkshire).
adjacent(X, Y) :- border(X, Y).
adjacent(X, Y) :- border(Y, X).
?- valid(wiltshire, sussex).
?- valid(wiltshire, kent).
?- valid(hampshire, hampshire).
?- valid(X, kent).
?- valid(sussex, X).
?- valid(X, Y).
valid(X, Y) :-
adjacent(X, Z),
adjacent(Z, Y)
Worksheet 4
a(g, h).
a(g, d).
a(e, d).
a(h, f).
a(e, f).
a(a, e).
a(a, b).
a(b, f).
a(b, c).
a(f, c).
arc
a
ed
g
h
f
c
b
path(X, X).
path(X, Y) :- a(X, Z), path(Z, Y).
Note that Prolog
can distinguish
between the 0-ary
constant a (the
name of a node) and
the 2-ary functor a
(the name of a
relation). ?- path(f, f).
?- path(a, c).
?- path(g, e).
?- path(g, X).
?- path(X, h).
But what happens if…
a(g, h).
a(g, d).
a(e, d).
a(h, f).
a(e, f).
a(a, e).
a(a, b).
a(b, f).
a(b, c).
a(f, c).
a(d, a).
a
ed
g
h
f
c
b
path(X, X).
path(X, Y) :- a(X, Z), path(Z, Y).
This program works only
for acyclic graphs. The
program may infinitely
loop given a cyclic graph.
We need to leave a ‘trail’
of visited nodes. This is
accomplished with a data
structure (to be seen
later).
Unification
• Two terms unify if substitutions can be made for any
variables in the terms so that the terms are made
identical. If no such substitution exists, the terms do
not unify.
• The Unification Algorithm proceeds by recursive
descent of the two terms.
 Constants unify if they are identical
 Variables unify with any term, including other
variables
 Compound terms unify if their functors and
components unify.
Examples
The terms f(X, a(b,c)) and f(d, a(Z, c)) unify.
Z c
ad
f
b c
aX
f
The terms are made equal if d is substituted for X, and b
is substituted for Z. We also say X is instantiated to d and
Z is instantiated to b, or X/d, Z/b.
Examples
The terms f(X, a(b,c)) and f(Z, a(Z, c)) unify.
Z c
aZ
f
b c
aX
f
Note that Z co-refers within the term.
Here, X/b, Z/b.
Examples
The terms f(c, a(b,c)) and f(Z, a(Z, c)) do not
unify.
Z c
aZ
f
b c
ac
f
No matter how hard you try, these two terms cannot
be made identical by substituting terms for
variables.
Exercise
Do terms g(Z, f(A, 17, B), A+B, 17) and
g(C, f(D, D, E), C, E) unify?
A B
+f
g
Z 17
A B17
Cf
g
C E
D ED
Exercise
First write in the co-referring variables.
A B
+f
g
Z 17
A B17
Cf
g
C E
D ED
Exercise
A B
+f
g
Z 17
A B17
Cf
g
C E
D ED
Z/C, C/Z
Now proceed by recursive descent
We go top-down, left-to-right, but
the order does not matter as long as
it is systematic and complete.
Exercise
A B
+f
g
Z 17
A B17
Cf
g
C E
D ED
Z/C, C/Z, A/D, D/A
Exercise
A B
+f
g
Z 17
A B17
Cf
g
C E
D ED
Z/C, C/Z, A/17, D/17
Exercise
A B
+f
g
Z 17
A B17
Cf
g
C E
D ED
Z/C, C/Z, A/17, D/17, B/E, E/B
Exercise
A B
+f
g
Z 17
A B17
Cf
g
C E
D ED
Z/17+B, C/17+B, A/17, D/17, B/E, E/B
Exercise
A B
+f
g
Z 17
A B17
Cf
g
C E
D ED
Z/17+17, C/17+17, A/17, D/17, B/17, E/17
Exercise – Alternative Method
Z/C
A B
+f
g
Z 17
A B17
Cf
g
C E
D ED
Exercise – Alternative Method
Z/C
A B
+f
g
C 17
A B17
Cf
g
C E
D ED
Exercise – Alternative Method
A/D, Z/C
A B
+f
g
C 17
A B17
Cf
g
C E
D ED
Exercise – Alternative Method
D/17, A/D, Z/C
D B
+f
g
C 17
D B17
Cf
g
C E
D ED
Exercise – Alternative Method
D/17, A/17, Z/C
17 B
+f
g
C 17
17 B17
Cf
g
C E
17 E17
Exercise – Alternative Method
B/E, D/17, A/17, Z/C
17 B
+f
g
C 17
17 B17
Cf
g
C E
17 E17
Exercise – Alternative Method
B/E, D/17, A/17, Z/C
17 E
+f
g
C 17
17 E17
Cf
g
C E
17 E17
Exercise – Alternative Method
C/17+E, B/E, D/17, A/17, Z/C
17 E
+f
g
C 17
17 E17
Cf
g
C E
17 E17
Exercise – Alternative Method
C/17+E, B/E, D/17, A/17, Z/17+E
17 E
+f
g
+
17
17 E17
+f
g
+ E
17 E17
17 E
17 E
E17
Exercise – Alternative Method
E/17, C/17+E, B/E, D/17, A/17, Z/C
17 E
+f
g
+
17
17 E17
+f
g
+ E
17 E17
17 E
17 E
E17
Exercise – Alternative Method
E/17, C/17+17, B/17, D/17, A/17, Z/C
17 17
+f
g
+
17
17 1717
+f
g
+ 17
17 1717
17 17
17 17
1717

More Related Content

PPT
Pl vol1
Aarsh Ps
 
PPT
Prolog programming
Harry Potter
 
PPT
Bioinformatica 06-10-2011-p2 introduction
Prof. Wim Van Criekinge
 
PPT
09 logic programming
saru40
 
PPTX
Prolog 7-Languages
Pierre de Lacaze
 
PDF
Slides Workshopon Explainable Logic-Based Knowledge Representation (XLoKR 2020)
Federal University of Technology of Parana
 
PPT
Regex Basics
Jeremy Coates
 
PPTX
Bioinformatics p2-p3-perl-regexes v2014
Prof. Wim Van Criekinge
 
Pl vol1
Aarsh Ps
 
Prolog programming
Harry Potter
 
Bioinformatica 06-10-2011-p2 introduction
Prof. Wim Van Criekinge
 
09 logic programming
saru40
 
Prolog 7-Languages
Pierre de Lacaze
 
Slides Workshopon Explainable Logic-Based Knowledge Representation (XLoKR 2020)
Federal University of Technology of Parana
 
Regex Basics
Jeremy Coates
 
Bioinformatics p2-p3-perl-regexes v2014
Prof. Wim Van Criekinge
 

What's hot (20)

PPTX
theory of computation lecture 02
8threspecter
 
PDF
Regular expression
Rajon
 
PDF
Hantgan And Davis Abstract Lsa And Mid Phon
ahantgan
 
PPT
Prolog 01
saru40
 
PPTX
Prolog2 (1)
university of sargodha
 
PPTX
#8 formal methods – pro logic
Sharif Omar Salem
 
PPTX
ProLog (Artificial Intelligence) Introduction
wahab khan
 
PPT
Predlogic
saru40
 
PDF
Optimization of probabilistic argumentation with Markov processes
Emmanuel Hadoux
 
PPTX
Overview prolog
Harry Potter
 
PPT
Chaps 1-3-ai-prolog
saru40
 
PDF
07.2 Holland's Genetic Algorithms Schema Theorem
Andres Mendez-Vazquez
 
PPT
Prolog basics
shivani saluja
 
PPTX
PROLOG: Cuts And Negation In Prolog
DataminingTools Inc
 
PPT
Learning sets of rules, Sequential Learning Algorithm,FOIL
Pavithra Thippanaik
 
PDF
10 logic+programming+with+prolog
baran19901990
 
PPTX
Regular expression
Larry Nung
 
PDF
Term Rewriting
Eelco Visser
 
PPTX
Python advanced 2. regular expression in python
John(Qiang) Zhang
 
PPT
Php String And Regular Expressions
mussawir20
 
theory of computation lecture 02
8threspecter
 
Regular expression
Rajon
 
Hantgan And Davis Abstract Lsa And Mid Phon
ahantgan
 
Prolog 01
saru40
 
#8 formal methods – pro logic
Sharif Omar Salem
 
ProLog (Artificial Intelligence) Introduction
wahab khan
 
Predlogic
saru40
 
Optimization of probabilistic argumentation with Markov processes
Emmanuel Hadoux
 
Overview prolog
Harry Potter
 
Chaps 1-3-ai-prolog
saru40
 
07.2 Holland's Genetic Algorithms Schema Theorem
Andres Mendez-Vazquez
 
Prolog basics
shivani saluja
 
PROLOG: Cuts And Negation In Prolog
DataminingTools Inc
 
Learning sets of rules, Sequential Learning Algorithm,FOIL
Pavithra Thippanaik
 
10 logic+programming+with+prolog
baran19901990
 
Regular expression
Larry Nung
 
Term Rewriting
Eelco Visser
 
Python advanced 2. regular expression in python
John(Qiang) Zhang
 
Php String And Regular Expressions
mussawir20
 
Ad

Viewers also liked (7)

PPT
Introduction to logic and prolog - Part 1
Sabu Francis
 
PPT
Handling User Input and Processing Form Data
Nicole Ryan
 
PPTX
Introduction on Prolog - Programming in Logic
Vishal Tandel
 
PPTX
PROLOG: Introduction To Prolog
DataminingTools Inc
 
PPTX
Introduction to Prolog
Chamath Sajeewa
 
PPTX
Prolog Programming : Basics
Mitul Desai
 
PPT
Expert Systems & Prolog
Fatih Karatana
 
Introduction to logic and prolog - Part 1
Sabu Francis
 
Handling User Input and Processing Form Data
Nicole Ryan
 
Introduction on Prolog - Programming in Logic
Vishal Tandel
 
PROLOG: Introduction To Prolog
DataminingTools Inc
 
Introduction to Prolog
Chamath Sajeewa
 
Prolog Programming : Basics
Mitul Desai
 
Expert Systems & Prolog
Fatih Karatana
 
Ad

Similar to Pl vol1 (20)

DOCX
COMM 166 Final Research Proposal GuidelinesThe proposal should.docx
monicafrancis71118
 
DOCX
COMM 166 Final Research Proposal GuidelinesThe proposal should.docx
cargillfilberto
 
DOCX
COMM 166 Final Research Proposal GuidelinesThe proposal should.docx
drandy1
 
PDF
Logic Programming and ILP
Pierre de Lacaze
 
DOCX
AI Lab Manual.docx
KPRevathiAsstprofITD
 
PPT
PROLOGPROLOGPROLOGPROLOGPROLOGPROLOGPROLOGPROLOGPROLOG
Kdas004
 
PPT
Chaps 1-3-ai-prolog
juanpaperez1234
 
PDF
Introduction to prolog
Rakhi Sinha
 
PDF
The Logical Burrito - pattern matching, term rewriting and unification
Norman Richards
 
PPT
Prolog programming
Young Alista
 
PPT
Prolog programming
Tony Nguyen
 
PPT
Prolog programming
James Wong
 
PPT
Prolog programming
Luis Goldster
 
PPT
Prolog programming
David Hoen
 
PPT
Prolog programming
Fraboni Ec
 
PDF
Scope Graphs: A fresh look at name binding in programming languages
Eelco Visser
 
PPT
PredicateLogic or FOL for Computer Science
parvath vigna
 
PPT
Lec8-PredicateLogic knowledge representation.ppt
ratnababum
 
PPTX
UOS-BSIT-3811-Artificial-Intelligence-Introduction-to-prolog-PDF.pptx
qasim ali
 
COMM 166 Final Research Proposal GuidelinesThe proposal should.docx
monicafrancis71118
 
COMM 166 Final Research Proposal GuidelinesThe proposal should.docx
cargillfilberto
 
COMM 166 Final Research Proposal GuidelinesThe proposal should.docx
drandy1
 
Logic Programming and ILP
Pierre de Lacaze
 
AI Lab Manual.docx
KPRevathiAsstprofITD
 
PROLOGPROLOGPROLOGPROLOGPROLOGPROLOGPROLOGPROLOGPROLOG
Kdas004
 
Chaps 1-3-ai-prolog
juanpaperez1234
 
Introduction to prolog
Rakhi Sinha
 
The Logical Burrito - pattern matching, term rewriting and unification
Norman Richards
 
Prolog programming
Young Alista
 
Prolog programming
Tony Nguyen
 
Prolog programming
James Wong
 
Prolog programming
Luis Goldster
 
Prolog programming
David Hoen
 
Prolog programming
Fraboni Ec
 
Scope Graphs: A fresh look at name binding in programming languages
Eelco Visser
 
PredicateLogic or FOL for Computer Science
parvath vigna
 
Lec8-PredicateLogic knowledge representation.ppt
ratnababum
 
UOS-BSIT-3811-Artificial-Intelligence-Introduction-to-prolog-PDF.pptx
qasim ali
 

Recently uploaded (20)

PPTX
Tunnel Ventilation System in Kanpur Metro
220105053
 
PPTX
business incubation centre aaaaaaaaaaaaaa
hodeeesite4
 
PDF
Zero carbon Building Design Guidelines V4
BassemOsman1
 
PDF
Introduction to Ship Engine Room Systems.pdf
Mahmoud Moghtaderi
 
PDF
flutter Launcher Icons, Splash Screens & Fonts
Ahmed Mohamed
 
PPTX
22PCOAM21 Session 2 Understanding Data Source.pptx
Guru Nanak Technical Institutions
 
PDF
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
PDF
The Effect of Artifact Removal from EEG Signals on the Detection of Epileptic...
Partho Prosad
 
PDF
settlement FOR FOUNDATION ENGINEERS.pdf
Endalkazene
 
PDF
Software Testing Tools - names and explanation
shruti533256
 
PDF
2010_Book_EnvironmentalBioengineering (1).pdf
EmilianoRodriguezTll
 
PPTX
database slide on modern techniques for optimizing database queries.pptx
aky52024
 
PDF
Traditional Exams vs Continuous Assessment in Boarding Schools.pdf
The Asian School
 
PPTX
MT Chapter 1.pptx- Magnetic particle testing
ABCAnyBodyCanRelax
 
DOCX
SAR - EEEfdfdsdasdsdasdasdasdasdasdasdasda.docx
Kanimozhi676285
 
PPTX
Inventory management chapter in automation and robotics.
atisht0104
 
PDF
2025 Laurence Sigler - Advancing Decision Support. Content Management Ecommer...
Francisco Javier Mora Serrano
 
PDF
top-5-use-cases-for-splunk-security-analytics.pdf
yaghutialireza
 
PPTX
easa module 3 funtamental electronics.pptx
tryanothert7
 
PDF
Unit I Part II.pdf : Security Fundamentals
Dr. Madhuri Jawale
 
Tunnel Ventilation System in Kanpur Metro
220105053
 
business incubation centre aaaaaaaaaaaaaa
hodeeesite4
 
Zero carbon Building Design Guidelines V4
BassemOsman1
 
Introduction to Ship Engine Room Systems.pdf
Mahmoud Moghtaderi
 
flutter Launcher Icons, Splash Screens & Fonts
Ahmed Mohamed
 
22PCOAM21 Session 2 Understanding Data Source.pptx
Guru Nanak Technical Institutions
 
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
The Effect of Artifact Removal from EEG Signals on the Detection of Epileptic...
Partho Prosad
 
settlement FOR FOUNDATION ENGINEERS.pdf
Endalkazene
 
Software Testing Tools - names and explanation
shruti533256
 
2010_Book_EnvironmentalBioengineering (1).pdf
EmilianoRodriguezTll
 
database slide on modern techniques for optimizing database queries.pptx
aky52024
 
Traditional Exams vs Continuous Assessment in Boarding Schools.pdf
The Asian School
 
MT Chapter 1.pptx- Magnetic particle testing
ABCAnyBodyCanRelax
 
SAR - EEEfdfdsdasdsdasdasdasdasdasdasdasda.docx
Kanimozhi676285
 
Inventory management chapter in automation and robotics.
atisht0104
 
2025 Laurence Sigler - Advancing Decision Support. Content Management Ecommer...
Francisco Javier Mora Serrano
 
top-5-use-cases-for-splunk-security-analytics.pdf
yaghutialireza
 
easa module 3 funtamental electronics.pptx
tryanothert7
 
Unit I Part II.pdf : Security Fundamentals
Dr. Madhuri Jawale
 

Pl vol1

  • 1. Prolog Programming slides written by Dr W.F. Clocksin
  • 2. The Plan • An example program • Syntax of terms • Some simple programs • Terms as data structures, unification • The Cut • Writing real programs
  • 3. What is Prolog? • Prolog is the most widely used language to have been inspired by logic programming research. Some features: • Prolog uses logical variables. These are not the same as variables in other languages. Programmers can use them as ‘holes’ in data structures that are gradually filled in as computation proceeds.
  • 4. …More • Unification is a built-in term-manipulation method that passes parameters, returns results, selects and constructs data structures. • Basic control flow model is backtracking. • Program clauses and data have the same form. • The relational form of procedures makes it possible to define ‘reversible’ procedures.
  • 5. …More • Clauses provide a convenient way to express case analysis and nondeterminism. • Sometimes it is necessary to use control features that are not part of ‘logic’. • A Prolog program can also be seen as a relational database containing rules as well as facts.
  • 6. What a program looks like /* At the Zoo */ elephant(george). elephant(mary). panda(chi_chi). panda(ming_ming). dangerous(X) :- big_teeth(X). dangerous(X) :- venomous(X). guess(X, tiger) :- stripey(X), big_teeth(X), isaCat(X). guess(X, koala) :- arboreal(X), sleepy(X). guess(X, zebra) :- stripey(X), isaHorse(X).
  • 7. Prolog is a ‘declarative’ language • Clauses are statements about what is true about a problem, instead of instructions how to accomplish the solution. • The Prolog system uses the clauses to work out how to accomplish the solution by searching through the space of possible solutions. • Not all problems have pure declarative specifications. Sometimes extralogical statements are needed.
  • 8. Example: Concatenate lists a and b list procedure cat(list a, list b) { list t = list u = copylist(a); while (t.tail != nil) t = t.tail; t.tail = b; return u; } In an imperative language In a declarative language In a functional language cat(a,b) ≡ if b = nil then a else cons(head(a), cat(tail(a),b)) cat([], Z, Z). cat([H|T], L, [H|Z]) :- cat(T, L, Z).
  • 9. Complete Syntax of Terms Term Constant VariableCompound Term Atom Number alpha17 gross_pay john_smith dyspepsia + =/= ’12Q&A’ 0 1 57 1.618 2.04e-27 -13.6 likes(john, mary) book(dickens, Z, cricket) f(x) [1, 3, g(a), 7, 9] -(+(15, 17), t) 15 + 17 - t X Gross_pay Diagnosis _257 _ Names an individual Stands for an individual unable to be named when program is written Names an individual that has parts
  • 10. Compound Terms parents(spot, fido, rover) The parents of Spot are Fido and Rover. Functor (an atom) of arity 3. components (any terms) It is possible to depict the term as a tree: parents roverfidospot
  • 11. Compound Terms =/=(15+X, (0*a)+(2<<5)) Some atoms have built-in operator declarations so they may be written in a syntactically convenient form. The meaning is not affected. This example looks like an arithmetic expression, but might not be. It is just a term. << 2 + a * 0 X + 15 =/= 5
  • 12. More about operators • Any atom may be designated an operator. The only purpose is for convenience; the only effect is how the term containing the atom is parsed. Operators are ‘syntactic sugar’. • We won’t be designating operators in this course, but it is as well to understand them, because a number of atoms have built-in designations as operators. • Operators have three properties: position, precedence and associativity. more…
  • 13. Examples of operator properties Position Operator Syntax Normal Syntax Prefix: -2 -(2) Infix: 5+17 +(17,5) Postfix: N! !(N) Associativity: left, right, none. X+Y+Z is parsed as (X+Y)+Z because addition is left-associative. Precedence: an integer. X+Y*Z is parsed as X+(Y*Z) because multiplication has higher precedence. These are all the same as the normal rules of arithmetic.
  • 14. The last point about Compound Terms… Constants are simply compound terms of arity 0. badger means the same as badger()
  • 15. Structure of Programs • Programs consist of procedures. • Procedures consist of clauses. • Each clause is a fact or a rule. • Programs are executed by posing queries. An example…
  • 16. Example elephant(george). elephant(mary). elephant(X) :- grey(X), mammal(X), hasTrunk(X). Procedure for elephant Predicate Clauses Rule Facts
  • 18. Clauses: Facts and Rules Head :- Body. This is a rule. Head. This is a fact. ‘if’ ‘provided that’ ‘turnstile’ Full stop at the end.
  • 19. Body of a (rule) clause contains goals. likes(mary, X) :- human(X), honest(X). Head Body Goals Exercise: Identify all the parts of Prolog text you have seen so far.
  • 20. Interpretation of Clauses Clauses can be given a declarative reading or a procedural reading. H :- G1, G2, …, Gn. “That H is provable follows from goals G1, G2, …, Gn being provable.” “To execute procedure H, the procedures called by goals G1, G2, …, Gn are executed first.” Declarative reading: Procedural reading: Form of clause:
  • 21. male(bertram). male(percival). female(lucinda). female(camilla). pair(X, Y) :- male(X), female(Y). ?- pair(percival, X). ?- pair(apollo, daphne). ?- pair(camilla, X). ?- pair(X, lucinda). ?- pair(X, X). ?- pair(bertram, lucinda). ?- pair(X, daphne). ?- pair(X, Y).
  • 22. Worksheet 2 drinks(john, martini). drinks(mary, gin). drinks(susan, vodka). drinks(john, gin). drinks(fred, gin). pair(X, Y, Z) :- drinks(X, Z), drinks(Y, Z). ?- pair(X, john, martini). ?- pair(mary, susan, gin). ?- pair(john, mary, gin). ?- pair(john, john, gin). ?- pair(X, Y, gin). ?- pair(bertram, lucinda). ?- pair(bertram, lucinda, vodka). ?- pair(X, Y, Z). This definition forces X and Y to be distinct: pair(X, Y, Z) :- drinks(X, Z), drinks(Y, Z), X == Y.
  • 23. Worksheet 3 berkshire wiltshire surrey hampshire sussex kent How to represent this relation? Note that borders are symmetric. (a) Representing a symmetric relation. (b) Implementing a strange ticket condition.
  • 24. WS3 border(sussex, kent). border(sussex, surrey). border(surrey, kent). border(hampshire, sussex). border(hampshire, surrey). border(hampshire, berkshire). border(berkshire, surrey). border(wiltshire, hampshire). border(wiltshire, berkshire). This relation represents one ‘direction’ of border: What about the other? (a) Say border(kent, sussex). border(sussex, kent). (b) Say adjacent(X, Y) :- border(X, Y). adjacent(X, Y) :- border(Y, X). (c) Say border(X, Y) :- border(Y, X).
  • 25. WS3 valid(X, Y) :- adjacent(X, Z), adjacent(Z, Y) Now a somewhat strange type of discount ticket. For the ticket to be valid, one must pass through an intermediate county. A valid ticket between a start and end county obeys the following rule:
  • 26. WS3 border(sussex, kent). border(sussex, surrey). border(surrey, kent). border(hampshire, sussex). border(hampshire, surrey). border(hampshire, berkshire). border(berkshire, surrey). border(wiltshire, hampshire). border(wiltshire, berkshire). adjacent(X, Y) :- border(X, Y). adjacent(X, Y) :- border(Y, X). ?- valid(wiltshire, sussex). ?- valid(wiltshire, kent). ?- valid(hampshire, hampshire). ?- valid(X, kent). ?- valid(sussex, X). ?- valid(X, Y). valid(X, Y) :- adjacent(X, Z), adjacent(Z, Y)
  • 27. Worksheet 4 a(g, h). a(g, d). a(e, d). a(h, f). a(e, f). a(a, e). a(a, b). a(b, f). a(b, c). a(f, c). arc a ed g h f c b path(X, X). path(X, Y) :- a(X, Z), path(Z, Y). Note that Prolog can distinguish between the 0-ary constant a (the name of a node) and the 2-ary functor a (the name of a relation). ?- path(f, f). ?- path(a, c). ?- path(g, e). ?- path(g, X). ?- path(X, h).
  • 28. But what happens if… a(g, h). a(g, d). a(e, d). a(h, f). a(e, f). a(a, e). a(a, b). a(b, f). a(b, c). a(f, c). a(d, a). a ed g h f c b path(X, X). path(X, Y) :- a(X, Z), path(Z, Y). This program works only for acyclic graphs. The program may infinitely loop given a cyclic graph. We need to leave a ‘trail’ of visited nodes. This is accomplished with a data structure (to be seen later).
  • 29. Unification • Two terms unify if substitutions can be made for any variables in the terms so that the terms are made identical. If no such substitution exists, the terms do not unify. • The Unification Algorithm proceeds by recursive descent of the two terms.  Constants unify if they are identical  Variables unify with any term, including other variables  Compound terms unify if their functors and components unify.
  • 30. Examples The terms f(X, a(b,c)) and f(d, a(Z, c)) unify. Z c ad f b c aX f The terms are made equal if d is substituted for X, and b is substituted for Z. We also say X is instantiated to d and Z is instantiated to b, or X/d, Z/b.
  • 31. Examples The terms f(X, a(b,c)) and f(Z, a(Z, c)) unify. Z c aZ f b c aX f Note that Z co-refers within the term. Here, X/b, Z/b.
  • 32. Examples The terms f(c, a(b,c)) and f(Z, a(Z, c)) do not unify. Z c aZ f b c ac f No matter how hard you try, these two terms cannot be made identical by substituting terms for variables.
  • 33. Exercise Do terms g(Z, f(A, 17, B), A+B, 17) and g(C, f(D, D, E), C, E) unify? A B +f g Z 17 A B17 Cf g C E D ED
  • 34. Exercise First write in the co-referring variables. A B +f g Z 17 A B17 Cf g C E D ED
  • 35. Exercise A B +f g Z 17 A B17 Cf g C E D ED Z/C, C/Z Now proceed by recursive descent We go top-down, left-to-right, but the order does not matter as long as it is systematic and complete.
  • 36. Exercise A B +f g Z 17 A B17 Cf g C E D ED Z/C, C/Z, A/D, D/A
  • 37. Exercise A B +f g Z 17 A B17 Cf g C E D ED Z/C, C/Z, A/17, D/17
  • 38. Exercise A B +f g Z 17 A B17 Cf g C E D ED Z/C, C/Z, A/17, D/17, B/E, E/B
  • 39. Exercise A B +f g Z 17 A B17 Cf g C E D ED Z/17+B, C/17+B, A/17, D/17, B/E, E/B
  • 40. Exercise A B +f g Z 17 A B17 Cf g C E D ED Z/17+17, C/17+17, A/17, D/17, B/17, E/17
  • 41. Exercise – Alternative Method Z/C A B +f g Z 17 A B17 Cf g C E D ED
  • 42. Exercise – Alternative Method Z/C A B +f g C 17 A B17 Cf g C E D ED
  • 43. Exercise – Alternative Method A/D, Z/C A B +f g C 17 A B17 Cf g C E D ED
  • 44. Exercise – Alternative Method D/17, A/D, Z/C D B +f g C 17 D B17 Cf g C E D ED
  • 45. Exercise – Alternative Method D/17, A/17, Z/C 17 B +f g C 17 17 B17 Cf g C E 17 E17
  • 46. Exercise – Alternative Method B/E, D/17, A/17, Z/C 17 B +f g C 17 17 B17 Cf g C E 17 E17
  • 47. Exercise – Alternative Method B/E, D/17, A/17, Z/C 17 E +f g C 17 17 E17 Cf g C E 17 E17
  • 48. Exercise – Alternative Method C/17+E, B/E, D/17, A/17, Z/C 17 E +f g C 17 17 E17 Cf g C E 17 E17
  • 49. Exercise – Alternative Method C/17+E, B/E, D/17, A/17, Z/17+E 17 E +f g + 17 17 E17 +f g + E 17 E17 17 E 17 E E17
  • 50. Exercise – Alternative Method E/17, C/17+E, B/E, D/17, A/17, Z/C 17 E +f g + 17 17 E17 +f g + E 17 E17 17 E 17 E E17
  • 51. Exercise – Alternative Method E/17, C/17+17, B/17, D/17, A/17, Z/C 17 17 +f g + 17 17 1717 +f g + 17 17 1717 17 17 17 17 1717