Prolog Sav 2023
Prolog Sav 2023
Prolog
Prof. S A Vahora
Introduction
Backtracking
Recursion in prolog
• Any function which calls itself is called recursive function.
• In Prolog, recursion appears when a predicate contain a goal that refers to itself.
• This simply means a program calls itself typically until some final point is reached.
• In Prolog and in any language, a recursive definition always has at least two parts.
• A first fact that act like a stopping condition and a rule that call itself simplified.
• At each level the first fact is checked. If the fact is true then the recursion ends. If
not the recursion continue.
• A recursive rule must never call itself with the same arguments. If that happens
then the program will never end.
Find factorial of given number
fact.pl
fact(0,Result) :-
Result is 1.
fact(N,Result) :-
N > 0,
N1 is N-1,
fact(N1,Result1),
Result is Result1*N.
Output
?fact(2,F).
F = 2
?fact(3,F).
F = 6
Find the sum of first N natural numbers
Number.pl
sum(0,0).
sum(N,R):-
N > 0,
N1 is N-1,
sum(N1,R1),
R is R1+N.
Output
?sum(4,SUM).
SUM = 10
Print Fibonacci series
Fibonacci.pl
fibonacci(1).
fibonacci(N) :-
N1 = N - 1,
N1 >= 0,!,
fibonacci(N1),
write(F1," ,"),
F = F1 + N.
Output
?fibonacci(2).
0 1
?fibonacci(3).
0 1 2
List
• it is a finite sequence of elements.
• A list in PROLOG is a structure of the form
[t1,t2,t3 … tn]
• The elements in list organised in two section head and tail.
• The direct access to only one element call head , while the rest forms
the list called the Tail.
[Head|Tail]
• where Head is a single element, while Tail is list.
check weather an object X is member of list L or not.
Output
List.pl
?find(a,[a,b,c]).
find(X,[X|TAIL]). true
find(X,[_|TAIL]):- find(X,TAIL). ?find(d,[a,b,c]).
false
Calculating the number of items of a given
list.
Output
List.pl
? length([a,b,c],N).
length([],0). N = 3
length([_|TAIL],N) :- ? length([],0).
length(TAIL,N1),N is N1 +1. N = 0
Find the nth element of a given list.
Output
List.pl
? match([a,b,c],0,N).
match([H|_],0,H). N = a
match([_|T],N,H) :-
N > 0,
N1 is N-1,
match(T,N1,H).
Merge two List
Output
List.pl
?:con([a,b,c],[1,2],N).
con([],L1,L1). N = [a,b,c,1,2]
con([X|Tail],L2,[X|Tail1]):-
con(Tail,L2,Tail1).
Operation
A query predicate matches a rule head or
fact (either one with variables) if
If the predic a te names and arities match then
each of the k-terms much match. So for foo(t1,
t2, t3) to match foo(s1, s2, s3) we must have that
t1 matches s1, t2 matches s2, and t3 matches t3.
During this matching process we might have
to “bind” some of the variables to make
the terms match.
These bindings are then passed on into the
new query (consisting of the rule body and
the left over query predic a tes).
Variable Matching (Unification)
Two terms (with variables match if):
If both are constants (identifiers, numbers, or
strings)
and are identic a l.
If one or both are bound variables then they
match
if what the variables are bound to match.
X and mary where X is bound to the value mary will
match.
X and Y where X is bound to mary and Y is bound to
mary
will match,
X and ann where X is bound to mary will not match.
Variable Matching (Unification)
If one of the terms is an unbound variable
then they match AND we bind the variable
to the term.
X and mary where X is unbound match and
make X
bound to mary.
X and Y where X is unbound and Y is bound to
mary
match and make X bound to mary.
X and Y where both X and Y are unbound
match and make X bound to Y (or vice
versa).
Variable Matching (Unification)
If the two terms are struc tures
t = f(t1, t2, ..., tk)
s = g(s1, s2, ..., sn)
Then these two terms matc h if
the identifiers “f” and “g” are identic al.
They both have identic al arity (k=n)
Each of the terms ti, si match (recursively).
E.g.
date(X, may, 1900) and date(3, may, Y) matc h and make X
bound
to 3 and Y bound to 1900.
equal(2 + 2, 3 + 1) and equal(X + Y, Z) match and make X
bound to
1, Y bound to 2, and Z bound to “3+1”.
Note that to then evaluate Z by using “is”.
date(f(X,a), may, g(a,b)) and date(Z, may, g(Y,Q)) match and
make Z bound to “f(X,a)”, Y bound to a, and Q bound to b.
Note we can bind a variable to a term containing another variable!
The predicate “=” shows what Prolog unifies!
Unification Examples
Which of the following are
unifiable?
Term1 Term 2 Bindings if unifiable
X f(a,b) X=f(a,b)
f(X,a) g(X,a)
3 2+1 No! use is if want 3
book(X,1) book(Z)
[1,2,3] [X|Y] X=1, Y=[2,3]
[a,b,X] [Y|[3,4]]
[a|X] [X|Y] X=a Y=a improper list
X(a,b) f(Z,Y)
[X|Y|Z] [a,b,c,d] X=a. Y=b, Z=[c,d]
Comparison operators
Prolog cut !
cut.pl Output
a(X) :- b(X),!,c(X). ?:a(x).
a(X) :- d(X). X = 1
b(1).
b(4).
c(1).
c(3).
d(4).
Fail
Fail.pl
• Negation as Failure a(X) :- b(X),c(X),fail.
a(X) :- d(X).
• Perform failure when condition does not satisfy.
• Fail predicate simply fails the rule. b(1).
b(4).
• A typical use of fail is a negation of a predicate c(1).
c(3).
• When Prolog fails, it tries to backtrack. Thus fail can be d(4).