You are on page 1of 30

PRACTICAL NO.

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

YAP is a high-performance Prolog compiler developed at LIACC/Universidade do Porto and


at COPPE Sistemas/UFRJ. Its Prolog engine is based in the WAM (Warren Abstract
Machine), with several optimizations for better performance. YAP follows the Edinburgh
tradition, and is largely compatible with the ISO-Prolog standard and with Quintus and
SICStus Prolog. YAP has been developed since 1985. The original version was written in
assembly, C and Prolog, and achieved high performance on m68k based machines. The
assembly code was used to implement the WAM emulators. Later emulators supported the
VAX, SPARC, and MIPS architectures. Work on the more recent version of YAP strives at
several goals:
Portability: The whole system is now written in C. YAP compiles in popular 32 bit
machines, such as Suns and Linux PCs, and in a 64 bit machines, the Alphas running OSF
UNIX and Linux.
Performance: We have optimised the emulator to obtain performance comparable to or
better than well-known Prolog systems. In fact, the current version of YAP performs better
than the original one, written in assembly language.
Robustness: We have tested the system with a large array of Prolog applications.
Extensibility: YAP was designed internally from the beginning to encapsulate manipulation
of terms. These principles were used, for example, to implement a simple and powerful C-
interface. The new version of YAP extends these principles to accommodate extensions to the
unification algorithm, that we believe will be useful to implement extensions such as
constraint programming.
Completeness: YAP has for a long time provided most builtins expected from an Edinburgh
Prolog implementation. These include I/O functionality, data-base operations, and modules.
Work on YAP AIMs now at being compatible with the Prolog standard.

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:

To compile YAP it should be sufficient to:


1. mkdir ARCH.
2. cd ARCH.
3. ../configure ...options. Notice that by default configure gives you a vanilla configuration.
For instance, in order to use co-routining and/or CLP you need to do ../configure --enable-
coroutining ...options...
4. check the Makefile for any extensions or changes you want to make. YAP uses autoconf.
Recent versions of YAP try to follow GNU conventions on where to place software. The
main executable is placed at BINDIR. This executable is actually a script that calls the Prolog
engine, stored at LIBDIR. LIBDIR is the directory where libraries are stored. YAPLIBDIR is
a subdirectory that contains the Prolog engine and a Prolog library.
INCLUDEDIR is used if you want to use YAP as a library. INFODIR is where to store info
files. Usually /usr/local/info, /usr/info, or /usr/share/info.
5. Make.
6. If the compilation succeeds, try ./yap.
7. If you feel satisfied with the result, do make install.
8. Make install-info will create the info files in the standard info directory.
9. Make html will create documentation in html format in the predefined directory. In most
systems you will need to be superuser in order to do make install and make info on the
standard directories.

[2] SWI-INSTALLATION ON UBUNTU AND EXECUTING PROLOG PROGRAMS :

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.

[3] PROLOG PROGRAMMING USING GPROLOG ON LINUX :

Prolog is a logical programming language perfect for description of artificial intelligence


knowledge base I was working on for one of my AI projects. The idea is that you make a
knowledge base and then ask your knowledge base what ever you need to know. As Linux
user I needed Linux tool so I went on Google and inquired about my options. I really like
GNU software so I've opted for GNU Prolog implementation called Gprolog. In this little
article I will show you how to get it working on Debian based operating systems. It is really
simple. First we will install Gprolog and create folder for our source code. You just open
your terminal and enter:
1. sudo apt-get install gprolog
Now we create directory where we will keep our prolog source code:
2. mkdir prolog_src
3. cd prolog_src
After this process is done you will have access to the Gprolog by typing, you would never
guess, gprolog in your terminal. But before you fire up the Gprolog you should create
knowledge base. You do this in any text editor by using Prolog syntax to create .pl file. As an
example we will create Prolog file called family.pl with source code from Wikipedia Prolog
site and then load this file into a Gprolog. First lets start our text editor with family.pl as
command line argument.
4. gedit family.pl

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:

AIM: Write a prolog program for fibonaaci series.

SOFTWARE REQUIRED: GNU Prolog

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 :

Thus we Implements a prolog program for fibonaaci series.

Sipna C.O.E.T. 7
PRACTICAL NO. 03 DATE:

AIM: Write a prolog program for construction of family tree

SOFTWARE REQUIRED : GNU Prolog

PROGRAM :

parent( pam, bob).


parent( tom, bob).
parent( tom, liz).
parent( bob, ann).
parent( bob, pat).
parent( pat, jim).

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 :

Thus we Implements a prolog program for construction of family tree

Sipna C.O.E.T. 11
PRACTICAL NO. 04 DATE:

AIM: Write a prolog program to implement 8-queen problem by using backtracking method.

SOFTWARE REQUIRED : GNU Prolog

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,1,4,6,3],[2,8,6,1,3,5,7,4],[3,1,7,5,8,2,4,6],[3,5,2,8,1,7,4,6],[3,5,2,8,6,4,7,1],
[3,5,7,1,4,2,8,6],[3,5,8,4,1,7,2,6],[3,6,2,5,8,1,7,4],[3,6,2,7,1,4,8,5],[3,6,2,7,5,1,8,4],
[3,6,4,1,8,5,7,2],[3,6,4,2,8,5,7,1],[3,6,8,1,4,7,5,2],[3,6,8,1,5,7,2,4],[3,6,8,2,4,1,7,5],
[3,7,2,8,5,1,4,6],[3,7,2,8,6,4,1,5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,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 :

Thus we Implements a prolog program to implement 8-queen problem by using backtracking


method.

Sipna C.O.E.T. 14
PRACTICAL NO. 05 DATE:

AIM: Write a prolog program to implement depth first search algorithm.


a

d
c
b

k f g h i

l
m

SOFTWARE REQUIRED : GNU Prolog

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

dfs([H|T], Explored, Goal, Solution):-


findall( X, (arc(H,X), \+ member(X,Explored), \+ member(X,[H|T]) ), Children),
append(Children,T,OpenList),
dfs(OpenList, [H|Explored], Goal, Solution).

dfs_start(InititalState, Goal, Es, Solution) :-


dfs([InititalState], [], Es, Goal, Solution).

dfs([H|_], Es0, Es, Goal, H) :-


call(Goal, H),
reverse(Es0, Es).

dfs([H|T], Es0, Es, Goal, Solution):-


findall(X, (arc(H, X),
\+ member(X, Es0),
\+ member(X, [H|T]) ), Children),
append(Children, T, OpenList),
dfs(OpenList, [H|Es0], Es, Goal, Solution).

Sipna C.O.E.T. 16
OUTPUT :

| ?- dfs_start(a, goal, Path, Solution).


Path = [a,b,c,k,f,l,d]
Solution = g ?

| ?- dfs_start(a, goal, Path, Solution).


Path = [a,b,c,k,f,l,d,g,h]
Solution = m ?

Conclusion :

Thus we Implements a prolog program to implement depth first search algorithm.

Sipna C.O.E.T. 17
PRACTICAL NO. 06 DATE:

AIM: Write a prolog program for tower of hanoi problem.

SOFTWARE REQUIRED : GNU Prolog

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 :

Thus we Implements a prolog program for tower of hanoi problem.

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.

SOFTWARE REQUIRED : GNU Prolog

PROGRAM :

start:-

write('Text is in file= '),


read(F),
write('What letter do you want to count='),
read(Let),
see(F),
process(Let,0),
seen.

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.

SOFTWARE REQUIRED : GNU Prolog

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.

SOFTWARE REQUIRED : GNU Prolog

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.

SOFTWARE REQUIRED : GNU Prolog

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

You might also like