Prolog Lab manual - All programs
Prolog Lab manual - All programs
Page 1 of 10
Prolog Lab Manual
Link-1: https://youtu.be/_EakgGNPE3o
Link-2: https://youtu.be/ArOS4ZGfnog
For example,
The terms dog(X) and dog(fido) can be unified by binding variable X to atom fido,i.e. giving X
the value fido. The terms owns(john,fido) and owns(P,Q) can be unified by binding variables P
and Q to atoms john and fido, respectively.
The equivalent Prolog Code
owns(john,fido) = owns(P,Q).
Page 2 of 10
Prolog Lab Manual
bing = bong.
bing = bing.
2 = 2.
'joe' = joe.
'2' = 2.
joe = X.
X = Y.
Page 3 of 10
Prolog Lab Manual
J = joe, J = john.
J = joe, J = joe.
ancestor(french(jean), B) = ancestor(A, irish(joe)).
ancestor(french(jean), irish(joe)) = ancestor(X, Y).
likes(X, X) = likes(joe, pizza).
likes(joe, pizza) = likes(joe, pizza).
likes(joe, X) = likes(joe, pizza).
father(X) = X.
father(ram) = X.
person(X,Y,Z) = person(john,smith,27)
person(john,Y,23) = person(X,smith,27).
pred1(X,Y,[a,b,c]) = pred1(A,prolog,B).
pred2(X,X,man) = pred2(london,dog,A). /*repeated variable*/
pred3(X,X,man) = pred3(london,london,A).
pred(alpha,beta,mypred(X,X,Y)) = pred(P,Q,mypred(no,yes,maybe)).
pred(alpha,beta,mypred(X,Z,Y)) = pred(P,Q,mypred(no,yes,maybe)).
pred(alpha,beta,mypred(X,X,Y)) = pred(P,Q,mypred(no,no,maybe)).
Exercise
Which of the following pairs will unify?
Where necessary, indicate what value the variable would be instantiated with in order to
successfully unify.
1. luke = luke.
2. luke = leia.
3. ‘luke’ = luke.
4. ‘Luke’ = luke.
5. Luke = luke.
6. jedi(luke) = luke.
7. jedi(luke) = X.
8. jedi(luke) = jedi(X).
9. father(vader, A) = father(B, luke).
10. heroes(han, X, luke) = heroes(Y, ben, X).
11. heroes(han, X, luke) = heroes(Y, ben).
12. jedi(X) = X.
13. characters(hero(luke), villain(vader)) = characters(X, Y).
14. characters(hero(luke), X) = characters(X, villain(vader)).
Page 4 of 10
Prolog Lab Manual
Link-1: https://youtu.be/Eymm8FSSBWc
Link-2: https://youtu.be/n4cm2zZb1Xo
The cut, in Prolog, is a goal, written as !, which always succeeds, but cannot be backtracked. It
is best used to prevent unwanted backtracking, including the finding of extra solutions
by Prolog and to avoid unnecessary computations.
Page 5 of 10
Prolog Lab Manual
Example 2: X is 3+4.
Justification
The addition here was carried out by a special procedure that is associated with the operator +.
We call such procedures built-in procedures
?-6+4=6+4.
?-6+4=4+6.
6+4=:=4+6.
?- sqrt(36)+4=:=5*11-45.
yes
?- 10=\=8+3.
Yes
?- likes(X,prolog)==likes(X,prolog).
X=_
?- likes(X,prolog)==likes(Y,prolog).
no
(X and Y are different variables)
?- X is 10,pred1(X)==pred1(10).
X = 10
?- X==0. no ?- 6+4==3+7.
no
?- X=0,X=:=0.
X=0
?- 6+4=3+7.
No
?- 6+4=:=3+7.
yes
Non-Unification between Two Terms \= The goal Term1\=Term2 succeeds if Term1=Term2
fails, i.e. the two terms cannot be unified. Otherwise it fails.
?- 6+4\=3+7.
Yes
read(X).
Page 6 of 10
Prolog Lab Manual
add(X,Y) :- Y is (X+3)*2.
add(2,X)
Page 7 of 10
Prolog Lab Manual
Query
displaylist([[a,b,c], [d,e,f],[g,h]]).
Link: https://youtu.be/bkAi-x2kZhY
Example: 1
List Basics, Find the output
[X, Y, Z] = [1, 2, 3].
[X | Y] = [1, 2, 3].
[X | Y] = [1].
[X, Y | Z] = [fred, jim, jill, mary].
[X | Y] = [[a, f(e)], [n, m, [2]]].
Example: 2
Consider the following fact.
p([H|T], H, T).
Let’s see what happens when we ask some simple queries.
? p([a,b,c], X, Y).
| ?- p([a], X, Y).
| ?- p([], X, Y).
List operations
Operations Definition
Membership During this operation, we can verify whether a given element is member of
Checking specified list or not?
Page 8 of 10
Prolog Lab Manual
Length Calculation With this operation, we can find the length of a list.
Delete Items This operation removes the specified element from a list.
Append Items Append operation adds one list into another (as an item).
https://www.tutorialspoint.com/prolog/prolog_lists.htm
10. Write a prolog program to implement all set operations (Union, intersection,
complement etc.)
union([ ], X, X).
union([X|R], Y, Z):- member(X, Y), !, union(R, Y, Z).
union([X|R], Y, [X|Z]):- union(R, Y, Z).
Query
union([0, 1, 6, 3], [a, b, 8, 2, 3, 9], L).
Program
squares([ ], [ ]).
squares([N|T], [S|ST]):- S is N*N, squares(T, ST).
Query
squares([0, 2, 3, 1], X).
Page 9 of 10
Prolog Lab Manual
https://youtu.be/SykxWpFwMGs
More references:
https://www.tutorialspoint.com/prolog/prolog_lists.htm
https://eecs.ceas.uc.edu/~annexsfs/Courses/cs323/Prolognotes1.html
https://www.cs.auckland.ac.nz/courses/compsci367s2c/tutorials/2013.d/prolog_3.pdf
http://www.cs.trincoll.edu/~ram/cpsc352/notes/prolog/factsrules.html
http://www.cse.unsw.edu.au/~cs9416/prolog/intro.html
http://www.dai.ed.ac.uk/groups/ssp/bookpages/quickprolog/node12.html
https://www.scss.tcd.ie/~dwoods/1617/CS1LL2/HT/wk4/lec4.pdf
https://www.youtube.com/watch?v=Eymm8FSSBWc
Page 10 of 10