Lecture 9 Prolog Programming
Lecture 9 Prolog Programming
Dr Khadija Kanwal
Institute of CS&IT
The Women University Multan
Prolog Programming
slides written by
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).
cat(a,b)
In a functional language if b = nil then a
else cons(head(a),
cat(tail(a),b))
cat([], Z, Z).
In a declarative language cat([H|T], L, [H|Z]) :- cat(T, L, Z).
Complete Syntax of Terms
Term
* <<
15 X
0 a 2 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’.
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)
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
Predicate
Facts
elephant(george).
Clauses elephant(mary).
elephant(X) :- grey(X), mammal(X),
hasTrunk(X).
Rule
Example
?- elephant(george).
Queries
yes
?- elephant(jane).
Replies
no
Clauses: Facts and Rules
‘if’
‘provided that’
‘turnstile’
Goals
berkshire surrey
kent
f a
d
X a
Z c
b c
f a
Z
X a
Z c
b c
f a
Z
c a
Z c
b c
g g
Z f + 17 C f C E
A B
A 17 B D D E
Exercise
First write in the co-referring variables.
g g
Z f + 17 C f C E
A B
A 17 B D D E
Exercise Now proceed by recursive descent
Z/C, C/Z We go top-down, left-to-right, but
the order does not matter as long as
it is systematic and complete.
g g
Z f + 17 C f C E
A B
A 17 B D D E
Exercise
Z/C, C/Z, A/D, D/A
g g
Z f + 17 C f C E
A B
A 17 B D D E
Exercise
Z/C, C/Z, A/17, D/17
g g
Z f + 17 C f C E
A B
A 17 B D D E
Exercise
Z/C, C/Z, A/17, D/17, B/E, E/B
g g
Z f + 17 C f C E
A B
A 17 B D D E
Exercise
Z/17+B, C/17+B, A/17, D/17, B/E, E/B
g g
Z f + 17 C f C E
A B
A 17 B D D E
Exercise
Z/17+17, C/17+17, A/17, D/17, B/17, E/17
g g
Z f + 17 C f C E
A B
A 17 B D D E
Exercise – Alternative Method
Z/C
g g
Z f + 17 C f C E
A B
A 17 B D D E
Exercise – Alternative Method
Z/C
g g
C f + 17 C f C E
A B
A 17 B D D E
Exercise – Alternative Method
A/D, Z/C
g g
C f + 17 C f C E
A B
A 17 B D D E
Exercise – Alternative Method
D/17, A/D, Z/C
g g
C f + 17 C f C E
D B
D 17 B D D E
Exercise – Alternative Method
D/17, A/17, Z/C
g g
C f + 17 C f C E
17 B
17 17 B 17 17 E
Exercise – Alternative Method
B/E, D/17, A/17, Z/C
g g
C f + 17 C f C E
17 B
17 17 B 17 17 E
Exercise – Alternative Method
B/E, D/17, A/17, Z/C
g g
C f + 17 C f C E
17 E
17 17 E 17 17 E
Exercise – Alternative Method
C/17+E, B/E, D/17, A/17, Z/C
g g
C f + 17 C f C E
17 E
17 17 E 17 17 E
Exercise – Alternative Method
C/17+E, B/E, D/17, A/17, Z/17+E
g g
+ +
f + 17 f + E
17 E
1 E
17 E 1 E
17 17 E 7 17 17 E
7
Exercise – Alternative Method
E/17, C/17+E, B/E, D/17, A/17, Z/C
g g
+ +
f + 17 f + E
17 E
1 E
17 E 1 E
17 17 E 7 17 17 E
7
Exercise – Alternative Method
E/17, C/17+17, B/17, D/17, A/17, Z/C
g g
+ +
f + 17 f + 17
17 17
1 1
17 17 1 1
17 17 17 7 717 17 1
7 7 7
Thank You!