Professional Documents
Culture Documents
01 DATE:
AIM: Study and installation of various tools for prolog programming language in linux.
THEORY:
Here we are studying the 3 tools as follows:
[1] YAP (YET ANOTHER PROLOG):
Sipna C.O.E.T. 1
Openness: We would like to make new development of YAP open to the user community.
Research: YAP has been a vehicle for research within and outside our group. Currently
research is going on parallelisation and tabulation, and support for Bayesian Networks.
Installing YAP:
I have an interest in playing and fuxing with prolog, I have installed the swi-prolog and added
the repository, just in case anyone is interested on which one commands I used:
% sudo apt-add-repository ppa:swi-prolog/stable
% sudo apt-get update
% sudo apt-get install swi-prolog
Sipna C.O.E.T. 2
How do I actually begin to write prolog codes on my linux machine? for my regular
programming I use VIM to write/edit/debug and terminal to compile. Can I use vim to write
prolog? How do i compile or use the prolog interpreter(i think that is what it is called)?
Yes, you can use any text editor, incl. VIM. Once you have written a prolog source file, say,
file.pl, you can load it into swi-prolog like so:
swipl -s file.pl
This will compile your file and take you to an interactive shell where you can then ask
queries against the definitions in your file. If you want to use your prolog program in a batch
mode, you can use:
swipl -s file.pl -t goal
where goal is the goal/query you want to evaluate. Note that in this case you won't be getting
the option to ask for alternative solutions.
Sipna C.O.E.T. 3
Next we paste our knowledge base from Wikipedia or whatever source code you have. Here's
the contents of file:
Example of Family.pl :
mother_child(trude, sally).
father_child(tom, sally).
father_child(tom, erica).
father_child(mike, tom).
sibling(X, Y) :- parent_child(Z, X), parent_child(Z, Y).
parent_child(X, Y) :- father_child(X, Y).
parent_child(X, Y) :- mother_child(X, Y).
We hit save and close our text editor. Now that we have our knowledge base we can run
Gprolog from the same directory where we have created our family.pl knowledge base, like
this:
5. gprolog
When Gprolog starts, we should get following prompt:
6. GNU Prolog 1.3.0
By Daniel Diaz
Copyright (C) 1999-2007 Daniel Diaz
| ?-
7. Now we load our knowledge base by typing following and pressing enter.
[family].
The dot at the end is like semicolon in other programming languages in a way that every
prolog "sentence" ends with dot. This is the response from Gprolog:
compiling /home/marko/prolog_src/family.pl for byte code...
/home/marko/prolog_src/family.pl compiled, 0 lines read - 132 bytes written, 6 ms
Now we can ask our knowledge base and receive logical answers. If we use Wikipedia
example we can ask our knowledge base to give as all father-child combinations defined in
our knowledge base:
father_child(Father, Child).
Sipna C.O.E.T. 4
Gprolog logical response would be:
Child = sally
Father = tom ? ;
Child = erica
Father = tom ? ;
Child = tom
Father = mike
yes
To continue with the next result press ; key and if you want to exit Gprolog press ctrl+c and
then e. To find out more about Gprolog you can consult Gprolog manual. You can find it on
/usr/share/doc/gprolog-doc/ path inside gprolog.pdf file. Hope that's enough to get you started
with prolog on Linux.
Conclusion :
Thus we are using the intresting tool called 'GPROLOG' for compilation purpose and
moreover we have studied the three different tools for prolog programming language.
Sipna C.O.E.T. 5
PRACTICAL NO. 02 DATE:
PROGRAM:
% fibonacci.pl
:- dynamic(stored/1).
memo(Goal) :-
stored(Goal) -> true;
Goal,
assertz(stored(Goal)).
fib(1,1) :-
!, write('1, ').
fib(2,1) :-
!, write('1, ').
fib(N,F) :-
N1 is N-1, memo(fib(N1,F1)),
N2 is N-2, memo(fib(N2,F2)),
F is F1 + F2,
write(F),
write(', ').
% interactive
[fibonacci].
fib(16,X),
write('...'), nl.
Sipna C.O.E.T. 6
OUTPUT:
| ?- fib(16,X).
1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987,
X = 987
Yes
Conclusion :
Sipna C.O.E.T. 7
PRACTICAL NO. 03 DATE:
PROGRAM :
female( pam).
female( liz).
female( ann).
female( pat).
male( tom).
male( bob).
male( jim).
offspring( Y, X) :-
parent( X, Y).
mother( X, Y) :-
parent( X, Y),
female( X).
grandparent( X, Z) :-
parent( X, Y),
parent( Y, Z).
Sipna C.O.E.T. 8
sister( X, Y) :-
parent( Z, X),
parent( Z, Y),
female( X),
X \= Y.
predecessor( X, Z) :-
% Rule pr1
parent( X, Z).
predecessor( X, Z) :-
% Rule pr2
parent( X, Y),
predecessor( Y, Z).
OUTPUT :-
| ?- grandparent(X,Y).
X = pam
Y = ann ? ;
X = pam
Y = pat ? ;
X = tom
Y = ann ? ;
X = tom
Y = pat ? ;
X = bob
Sipna C.O.E.T. 9
Y = jim ? ;
(1 ms) no
| ?- parent(X,Y).
X = pam
Y = bob ? ;
X = tom
Y = bob ? ;
X = tom
Y = liz ? ;
X = bob
Y = ann ? ;
X = bob
Y = pat ? ;
X = pat
Y = jim
yes
| ?- mother(X,Y).
X = pam
Y = bob ?
| ?- grandparent(X,Y).
X = pam
Y = ann ? ;
Sipna C.O.E.T. 10
X = pam
Y = pat ? ;
X = tom
Y = ann ? ;
X = tom
Y = pat ? ;
X = bob
Y = jim ? ;
(1 ms) no
| ?- sister(X,Y).
X = liz
Y = bob ? ;
X = ann
Y = pat ? ;
X = pat
Y = ann ?
Conclusion :
Sipna C.O.E.T. 11
PRACTICAL NO. 04 DATE:
AIM: Write a prolog program to implement 8-queen problem by using backtracking method.
PROGRAM :
queensSolution(A) :-
queens(A)
, \+ diagonalContact(A).
rank(1).
rank(2).
rank(3).
rank(4).
rank(5).
rank(6).
rank(7).
rank(8).
queens([A,B,C,D,E,F,G,H]) :-
queens ([A,B,C,D,E,F,G,H]),
queens_([A,B,C,D,E,F,G,H]).
queens ([A]) :-
rank(A).
queens ([A|B]) :-
rank(A),
queens (B).
Sipna C.O.E.T. 12
queens_([_]).
queens_([A|B]) :-
\+ member(A,B),
queens_(B).
diagonalContact([A|B]) :-
diagonalContact_(A,1,B);
diagonalContact(B).
diagonalContact_(A,B,[C|D]) :-
A+B =:= C;
A - B =:= C;
( E is B+1, diagonalContact_(A,E,D)).
Sipna C.O.E.T. 13
OUTPUT :
| ?- setof(A,queensSolution(A),B), length(B,C).
B=
[[1,5,8,6,3,7,2,4],[1,6,8,3,7,4,2,5],[1,7,4,6,8,2,5,3],[1,7,5,8,2,4,6,3],[2,4,6,8,3,1,7,5],
[2,5,7,1,3,8,6,4],[2,5,7,4,1,8,6,3],[2,6,1,7,4,8,3,5],[2,6,8,3,1,4,7,5],[2,7,3,6,8,5,1,4],
[2,7,5,8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[6,3,5,7,1,4,2,8],[6,3,5,8,1,4,2,7],[6,3,7,2,4,8,1,5],[6,3,7,2,8,5,1,4],[6,3,7,4,1,8,2,5],
[6,4,1,5,8,2,7,3],[6,4,2,8,5,7,1,3],[6,4,7,1,3,5,2,8],[6,4,7,1,8,2,5,3],[6,8,2,4,1,7,5,3],
[7,1,3,8,6,4,2,5],[7,2,4,1,8,5,3,6],[7,2,6,3,1,4,8,5],[7,3,1,6,8,5,2,4],[7,3,8,2,5,1,6,4],
[7,4,2,5,8,1,3,6],[7,4,2,8,6,1,3,5],[7,5,3,1,6,8,2,4],[8,2,4,1,7,5,3,6],[8,2,5,3,1,7,4,6],
[8,3,1,6,2,5,7,4],[8,4,1,3,6,2,7,5]]
C = 92
(23066 ms) yes
Conclusion :
Sipna C.O.E.T. 14
PRACTICAL NO. 05 DATE:
d
c
b
k f g h i
l
m
PROGRAM :
goal(g).
arc(a,b).
arc(a,c).
arc(a,d).
arc(c,k).
arc(c,f).
arc(d,g).
arc(d,h).
arc(d,i).
arc(f,l).
arc(h,m).
dfs_start(InititalState,Goal,Solution) :-
dfs([InititalState],[],Goal,Solution).
Sipna C.O.E.T. 15
dfs([H|_],_,Goal,[H]):-
Check =.. [Goal,H],
call(Check).
Sipna C.O.E.T. 16
OUTPUT :
Conclusion :
Sipna C.O.E.T. 17
PRACTICAL NO. 06 DATE:
PROGRAM :
move(1,X,Y,_) :-
write('Move top disk from '),
write(X), write(' to '),
write(Y),nl.
move(N,X,Y,Z) :-
N>1,
M is N-1,
move(M,X,Z,Y),
move(1,X,Y,_),
move(M,Z,Y,X).
Sipna C.O.E.T. 18
OUTPUT :
?- move(3,left,right,center).
Move top disk from left to right
Move top disk from left to center
Move top disk from right to center
Move top disk from left to right
Move top disk from center to left
Move top disk from center to right
Move top disk from left to right
Yes
Conclusion :
Sipna C.O.E.T. 19
PRACTICAL NO. 07 DATE:
AIM: Write a prolog program that counts a letter in a text file with reccursion.
PROGRAM :
start:-
process(Let,N):-
get0(Char),
process1(Char,Let,N).
process1(-1,Let,N):-
write('This letter occurs '),
write(N),
write(' times.'),!.
process1(Char,Let,K):-
name(Let,[Char]),
K1 is K+1,
process(Let,K1).
Sipna C.O.E.T. 20
process1(Char,Let,K):-
process(Let,K).
OUTPUT :
?- start.
Text is in file= 'mary.txt'.
What letter do you want to count= a.
This letter occurs 5 times.
Conclusion :
Thus we Implements a prolog program that counts a letter in a text file with reccursion.
Sipna C.O.E.T. 21
PRACTICAL NO. 08 DATE:
AIM: Write a prolog program to implement map colouring problem for 5 nodes.
PROGRAM :
adjacent(1,2). adjacent(2,1).
adjacent(1,3). adjacent(3,1).
adjacent(1,4). adjacent(4,1).
adjacent(1,5). adjacent(5,1).
adjacent(2,3). adjacent(3,2).
adjacent(2,4). adjacent(4,2).
adjacent(3,4). adjacent(4,3).
adjacent(4,5). adjacent(5,4).
/* */
color(1,red,a). color(1,red,b).
color(2,blue,a). color(2,blue,b).
color(3,green,a). color(3,green,b).
color(4,yellow,a). color(4,blue,b).
color(5,blue,a). color(5,green,b).
/* */
conflict(Coloring) :-
adjacent(X,Y),
color(X,Color,Coloring),
color(Y,Color,Coloring).
Sipna C.O.E.T. 22
/* */
conflict(R1,R2,Coloring) :-
adjacent(R1,R2),
color(R1,Color,Coloring),
color(R2,Color,Coloring).
OUTPUT :
?- adjacent(2,3).
yes
?- adjacent(5,3).
no
?- adjacent(3,R).
R=1;
R=2;
R=4;
no
?- conflict(a).
no
?- conflict(b).
yes
?- conflict(Which).
Which = b
?- conflict(R1,R2,b).
R1 = 2
R2 = 4
?- conflict(R1,R2,b),color(R1,C,b).
R1 = 2
R2 = 4
C = blue
Sipna C.O.E.T. 23
Conclusion :
Thus we Implements a prolog program to implement map colouring problem for 5 nodes.
Sipna C.O.E.T. 24
PRACTICAL NO. 09 DATE:
2
AIM: Write a prolog program to find roots of quadratic equation Ax +Bx+C.
PROGRAM :
start:-
reads(A,B,C),
solve(A,B,C),!,
continue.
reads(A,B,C):-
write('Input the coefficients:'),nl,
write('A='),
read(A),
write('B='),
read(B),
write('C='),
read(C).
delta(A,B,C,R):-
D is (B*B-4*A*C),
D >= 0,
R is sqrt(D).
solve(A,B,C):-
A=\=0,
delta(A,B,C,R),
X1 is (-B+R)/(2*A),
X2 is (-B-R)/(2*A),
write('Solutions are '),
Sipna C.O.E.T. 25
write('x1= '),
write(X1),
write(' x2='),
write(X2),nl.
solve(A,B,C):-
A=0,
X is (-C/B),
write('Equation is order I. Solution is: '),
write(X),nl.
solve(A,B,C):-
delta(A,B,C,R), R=0,
X is (-B/(2*A)),
write('Has one solution: '),
write(X),nl.
solve(A,B,C):-
write('No solutions.'),nl.
Continue:-
write('Do you want to continue(y/n)?='),
read(X),
(X=y;X=ya;X=yes),
start.
Sipna C.O.E.T. 26
OUTPUT :
?- start.
Input the coefficients:
A= 1.
B= -3.
C= 2.
Solutions are x1= 2 x2= 1
Do you want to continue(Y/N)?= N.
Conclusion :
2
Thus we Implements a prolog program to find roots of quadratic equation Ax +Bx+C.
Sipna C.O.E.T. 27
PRACTICAL NO. 10 DATE:
AIM : Write a prolog program to determine songs and its particular raga.
PROGRAM :
mela_raga(malhar).
mela_raga(bhairav).
same_family(malhar, sahana).
same_family(malhar, khamas).
same_family(bhairav, saveri).
same_family(R, R).
same_family(R1, R2) :-
same_family(R2, R1).
song(igiripai, sahana).
song(chakkani, malhar).
song(brocheva, khamas).
song(karikalaba, saveri).
song(merusamana, bhairav).
song(devaddeva,bhairav).
likes(siva, saveri).
likes(kanchan, khamas).
likes(meenal, khamas).
likes(aparna, saveri).
likes(kanchan, saveri).
Sipna C.O.E.T. 28
dislikes(kanchan,sahana).
dislikes(aparna,khamas).
dislikes(meenal,bhairav).
likes_song(Person, Song) :-
song(Song, Raga),
likes(Person, Raga).
dislikes_song(Person, Song) :-
song(Song, Raga),
dislikes(Person, Raga).
likes_song(Person, Song) :-
likes(Person, Raga1),
song(Song, Raga2),
same_family(Raga1, Raga2).
OUTPUT :
| ?- likes(X,khamas).
X = kanchan ? ;
X = meenal ? ;
no
| ?- likes(X,sahana).
no
| ?- likes(kanchan,X).
X = khamas ? ;
X = saveri
yes
Sipna C.O.E.T. 29
| ?- dislikes(X,khamas).
X = aparna ? ;
no
| ?- dislikes(meenal,X).
X = bhairav
yes
| ?- likes_song(X,brocheva).
X = kanchan ? ;
X = meenal ? ;
no
| ?- dislikes_song(X,brocheva).
X = aparna ? ;
no.
| ?-
song(X,bhairav).
X = merusamana ? ;
X = devaddeva
yes
| ?- song(X,malhar).
X = chakkani ? ;
no
| ?- song(igiripai,X).
X = sahana
(1 ms) yes
Conclusion :
Thus we Implements a prolog program to determine songs and its particular raga.
Sipna C.O.E.T. 30