L11_Prolog
L11_Prolog
Lecture Module 11
Objective
● What is Prolog?
● Prolog program
● Syntax of Prolog
● Prolog Control Strategy
● Execution of Prolog Query
● Programming Techniques
● Termination
● Rule Order
● Goal Order
● Rule Redundancy
● Iterative Programming
What is Prolog?
● Prolog is acronym of PROgramming in LOGic.
● Prolog program is sequence of rules and facts.
● Prolog Rule for grandfather is defined as:
father(james, robert).
Glimpse of Prolog Program
/* Rules: “X is grand father of Y if X is father of Z who is parent of Y */
gfather(X, Y) :- father(X, Z), parent(Z, Y). (1)
parent(X, Y) :- father(X, Y). (2)
parent(X, Y) :- mother(X, Y). (3)
/*Facts “James is father of robert” */
father(james, robert). (4)
father(mike, william). (5)
father(william, james). (6)
father(robert, hency). (7)
father(robert, cris). (8)
/* Goals ‘ Is abraham a grand father of mike?’*/
?- gfather(abraham, mike).
?- gfather(X, Y).
?- father(X, Y), father(Y, mike).
Back
General Syntax of Prolog
● The rules and facts in Prolog are terminated by full stop (.).
● Goal can be included in the program or given at the prompt.
● Goal can be single or conjunction of sub-goal(s) terminated by full
stop.
● Constants are numerals and symbols such as, 4, mary, etc.
● String is a sequence of characters and is enclosed within single
quotes e.g., 'This is a string'.
● Predicate names must start with alphabet and are formed by
using lower case letters, numerals and underscore ( _ ).
● Variable names are formed similar to predicate names but it must
start with upper case letter. Normally, we use X, Y, Z, ... for
variables.
Prolog Control Strategy
?- gfather(james, hency).
Proof tree: ?- gfather(james, hency).
(1)
?- father(james, Z), parent(Z, hency).
(4) { Z = robert }
?- parent(robert, hency).
(2)
?- father(robert, hency).
(7)
Answer: X = robert
Programming Techniques
● Recursion is one of the most important execution
control techniques in Prolog.
● Iteration is another important programming
technique.
● Prolog does not support iteration.
● It can be achieved by
− making recursive call to be the last sub goal in the rule
− using the concept of accumulators which are special types
of arguments used for holding intermediate results.
● The issues of termination, rule order, goal order and
redundancy are also important.
Recursion in Prolog
● Consider a classical example of computing factorial
using recursion.
● Recurrence relation or mathematical definition for
computing factorial of positive integer is:
1, if n = 0
FACTORIAL (n) =
n * FACTORIAL (n-1),
otherwise
● In Prolog, program for computing factorial of a
positive number is written using above definition.
● Factorial predicate contains two arguments; one as
input and another as output.
Recursion - Cont..
/*fact(N, R) – succeeds if R is unified with the factorial of
N */
factorial(0, 1). (1)
factorial(N, R) :- N1 is N–1, factorial(N1, R1), R is N * R1. (2)
r s
t
● Graph G is represented by set of connected edges.
G = { edge(p, q), edge(q, r), edge(q, s), edge(s, t)}
● Write Prolog Program to check whether there is a
route from one node to another node.
Program
● Route from node A to B is defined as follows:
− route from A to B if there is a root from A some node C and an
edge from C to B.
− route from A to B if there is a direct edge from A to B.
● Prolog Program
Non terminating
● Non terminating program because of left recursion.
● Better way of writing a rule is to write recursive call as the last
call.
route(A, B) :- edge(A, C), route(C, B).
Circular Definition
● Non termination occurs because of Circular definition
of the rules.
● For example, ground and nonground queries will not
terminate with respect to the following program.
teacher(X, Y) :- student(Y, X). (1)
student(X, Y) :- teacher(Y, X). (2)
student(robert, mike).
student(john, robert).
(2) (3) {R = 6}
fails succeeds
Answer: R = 6