0% found this document useful (0 votes)
2 views

Prolog Lab manual - All programs

The Prolog Lab Manual outlines a series of experiments designed to teach various aspects of Prolog, including facts, rules, unification, recursion, lists, and string operations. It includes practical exercises, example codes, and links to video resources for deeper understanding. Additionally, it provides programming tasks for implementing a family tree, set operations, and a library management system.

Uploaded by

Aditya Raj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Prolog Lab manual - All programs

The Prolog Lab Manual outlines a series of experiments designed to teach various aspects of Prolog, including facts, rules, unification, recursion, lists, and string operations. It includes practical exercises, example codes, and links to video resources for deeper understanding. Additionally, it provides programming tasks for implementing a family tree, set operations, and a library management system.

Uploaded by

Aditya Raj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 10

Prolog Lab Manual

Prolog - List of Experiments

1. Study of facts, objects, predicates and variables in PROLOG.


2. Study of Rules and Unification in PROLOG.
3. Study of “cut” and “fail” predicate in PROLOG.
4. Study of arithmetic operators, simple input/output and compound goals in
PROLOG.
5. Study of recursion in PROLOG.
6. Study of Lists in PROLOG.
7. Study of dynamic database in PROLOG.
8. Study of string operations in PROLOG. Implement string operations like substring,
string position, palindrome etc.)
9. Write a prolog program to maintain family tree.
10. Write a prolog program to implement all set operations (Union, intersection,
complement etc.)
11. Write a prolog program to implement Library Management system.

Page 1 of 10
Prolog Lab Manual

2. Study of Rules and Unification in PROLOG.

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

ancestor(french(jean), B) = ancestor(A, irish(joe)).

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

3. Study of “cut” and “fail” predicate in PROLOG.

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.

The cut should be used sparingly.


But how do we state this in Prolog? As a first step, let's introduce another built-
in predicate: fail/0 .

As its name suggests, fail/0 is a special symbol that will


immediately fail when Prolog encounters it as a goal. That may not sound too useful, but
remember: when Prolog fails, it tries to backtrack.

It is used to Control Backtracking Approach

Page 5 of 10
Prolog Lab Manual

4. Study of arithmetic operators, simple input/output and compound goals in PROLOG.


Example 1: X = 3 + 4.
Justification
3 + 4 is just a term whose functor is + and arguments are 3 and 4

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

Arithmetic Expression Equality


?- 6+4=:=6*3-8.
yes

?-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

5. Prolog - Recursion in Family Relationship


Link: https://youtu.be/2QqQAiNmycI
% Given a list of lists, display the elements of each list in one line.
%Fetching
displaylist( [] ). %Terminate Fetching - Recursion Stop
displaylist([H1|T2]) :- doline(H1), nl, displaylist(T2).
%Display
doline( [] ). %Terminate Display - Recursion Stop
doline( [H1 | T1] ) :- write(H1), write(' '), doline(T1).

Query
displaylist([[a,b,c], [d,e,f],[g,h]]).

6. Study of Lists in PROLOG.

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.

Concatenation Concatenation is an operation which is used to join/add two lists.

Delete Items This operation removes the specified element from a list.

Append Items Append operation adds one list into another (as an item).

Insert Items This operation inserts a given item into a list.

https://www.tutorialspoint.com/prolog/prolog_lists.htm

7. Study of dynamic database in PROLOG.


<Try your own>

8. Study of string operations in PROLOG. Implement string operations like substring,


string position, palindrome etc.)
https://youtu.be/syGMNm25mfY

9. Write a prolog program to maintain family tree.


https://youtu.be/QOt_ipXv9kQ

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).

11. Write a prolog program to implement Library Management system.


<Try your own>

Page 9 of 10
Prolog Lab Manual

All Prolog examples


https://youtube.com/playlist?list=PLEJXowNB4kPy3_qhGksOO8ch_Di7T8_9E

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

You might also like