You are on page 1of 12


AIM: To depict family relationships CODE: domains person=symbol predicates male(person) female(person) father(person, person) mother(person, person) sis(person, person) bro(person, person) uncle(person, person) grandpa(person, person) clauses male(salman). male(arbaaz). male(sohail). male(salim). male(arhaan). female(malaika). female(amrita). female(salma). female(arpita). mother(salman, salma). mother(arhaan, malaika). mother(arbaaz, salma). mother(sohail, salma). mother(arpita, salma). father(salman, salim). father(arhaan, arbaaz). father(arbaaz, salim). father(sohail, salim). father(arpita, salim). parent(X, Y) if mother(X, Y). parent(X, Y) if father(X, Y). bro(X, Y) if male(Y) and

parent(X, P) and parent(Y, P) and X<>Y. sis(X, Y) if female(Y) and parent(X, P) and parent(Y, P) and X<>Y. uncle(X, U) if mother(X, P) and bro(P, U). uncle(X, U) if father(X, P) and bro(P, U). grandpa(X, G) if father(P, G) and mother(X, P). grandpa(X, G) if father(X, P) and father(P, G). OUTPUT: GOAL: parent(salman, X) X=salma X=salim GOAL: grandpa(arhaan, X) X=salim GOAL: uncle(arhaan, X) X=salman X=sohail

INDEX Sno. Practical Date Signature

AIM: Water Jug Problem CODE: domains j1=integer j2=integer predicates water(j1,j2) water(integer,integer,integer,integer) clauses water(2,B,_,_):-B<4. water(A,_):-A>4,write("Jug One capacity exceeded"). water(_,B):-B>3,write("Jug two capacity exceeded"). water(A,B):-A=4,B=0,X=1,Y=3,write("jug1=",X," ","jug2=",Y). water(P,Q):-P=1,Q=3,P1=1,Q1=0,write("jug1=",P1," ","jug2=",Q1). water(P,Q):-P=1,Q=0,P1=0,Q1=1,write("jug1=",P1," ","jug2=",Q1). water(P,Q):-P=0,Q=1,P1=4,Q1=1,write("jug1=",P1," ","jug2=",Q1). water(P,Q):-P=4,Q=1,P1=2,Q1=3,write("jug1=",P1," ","jug2=",Q1),write("Solution reached").

AIM: Eight queen Problem CODE: domains H, F, I, Y, Y1, Xdist, Dist1, Queen=integer T, L, L1, PL, PT, Queen, Others=integer* predicates safe(L) solution(L) permutation(L,L) del(I, L, L) noattack(I, L, L) clauses solution(Queens):permutation([1,2,3,4,5,6,7,8]), Queens), safe(Queens). permutation([], []). permutation([H], [T], PL):permuatation(T, PT), del(H, PT, PI). del(I, [I|L], L). del(I, [F|L], [F|L1]):del(I, L, L1). safe([]). safe([Queen|Others]):safe(Others), noattack(Queen, Others, 1). noattack(_, [], _). noattack(Y, [Y1|Ylist], Xdist):Y1-Y<>Xdist, Y-Y1<>Xdist, Dist1=Xdist+1, Noattack(Y, Ylist, Dist1).

AIM: Tic Tac Toe CODE: :- dynamic o/1. :- dynamic x/1. ordered_line(1,2,3). ordered_line(4,5,6). ordered_line(7,8,9). ordered_line(1,4,7). ordered_line(2,5,8). ordered_line(3,6,9). ordered_line(1,5,9). ordered_line(3,5,7). line(A,B,C) :- ordered_line(A,B,C). line(A,B,C) :- ordered_line(A,C,B). line(A,B,C) :- ordered_line(B,A,C). line(A,B,C) :- ordered_line(B,C,A). line(A,B,C) :- ordered_line(C,A,B). line(A,B,C) :- ordered_line(C,B,A). move(A) :- good(A), empty(A). full(A) :- x(A). full(A) :- o(A). empty(A) :- not(full(A)). good(A) :- win(A). good(A) :- block_win(A). good(A) :- split(A). good(A) :- block_split(A). good(A) :- build(A). good(5). good(1). good(3). good(7). good(9). good(2). good(4). good(6). good(8). win(A) :- x(B), x(C), line(A,B,C). block_win(A) :- o(B), o(C), line(A,B,C). split(A) :- x(B), x(C), different(B,C), line(A,B,D), line(A,C,E), empty(D), empty(E). same(A,A). different(A,B) :- not(same(A,B)). block_split(A) :- o(B), o(C), different(B,C), line(A,B,D), line(A,C,E), empty(D), empty(E).

build(A) :- x(B), line(A,B,C), empty(C). all_full :- full(1),full(2),full(3),full(4),full(5),full(6),full(7),full(8),full(9). done :- ordered_line(A,B,C), x(A), x(B), x(C), write('I won.'),nl. done :- ordered_line(A,B,C), o(A), o(B), o(C), write('You won.'),nl. done :- all_full, write('Draw.'), nl. getmove :- repeat, write('Please enter a move: '),read(X), between(1,9,X), empty(X), assert(o(X)). makemove :- move(X),!, assert(x(X)). makemove :- all_full. printsquare(N) :- o(N), write(' o '). printsquare(N) :- x(N), write(' x '). printsquare(N) :- empty(N), write(' '). printboard :- printsquare(1),printsquare(2),printsquare(3),nl, printsquare(4),printsquare(5),printsquare(6),nl, printsquare(7),printsquare(8),printsquare(9),nl. clear :- x(A), retract(x(A)), fail. clear :- o(A), retract(o(A)), fail. play :- not(clear), repeat, getmove, makemove, printboard, done.

AIM: Travelling Salesman Problem CODE: domains D12,D13,D14,D23,D24,D34=Integer TT1,TT2,TT3,TT4=Integer City1,City2,City3,City4=Symbol X,Y,Z,W=Integer predicates travel(symbol,symbol,symbol,symbol) go getdist(integer,integer,integer,integer,integer,integer) min0(integer,integer,integer,integer) min1(integer,integer,integer,integer) min2(integer,integer,integer,integer) min3(integer,integer,integer,integer) min4(integer,integer,integer,integer,integer) goal go. clauses go:clearwindow, travel(City1 , City2 , City3 , City4), write (" ", City1," ",City2," ",City3," ",City4),nl, getdist(D12,D13,D14,D23,D24,D34), write (" ", City1," ",City2," ",City3),nl, write (City1," "," ",D12," ", D13),nl, write (City2," ",D12," ",D23),nl, write (City3," ",D13," ", D23 ," "),nl, write (City4," ",D14," ",D24," ",D34," "),nl, write("Starting from " ,City1 ),nl, min0(D12,D13,D14,D12), TT1=D12+D23+D34, write("Now go from"," ", City1," ", "to"," " ,City2),nl, write("Cheap path is"," " , City1, "to"," ", City2," ","to"," ",City3," ","to"," ",City4),nl, write("Total Distance in this case is"," ",TT1),nl,

write("Now starting from ",City2),nl, min1(D12,D23,D24,D12), TT2=D12+D13+D34, write("Now go from"," ",City2," ","to"," ",City1),nl, write("Cheap path is"," ",City2," ","to"," ",City1," ", "to"," ",City3," ","to"," ",City4),nl, write("Total distance in this case is"," " ,TT2),nl, write("Now starting from ",City3),nl, min2(D13,D23,D34,D13), TT3=D13+D12+D24, write("Now go from"," ",City3," ","to"," ",City1),nl, write("Cheap path is "," ",City3," ","to"," ",City1," ","to", " ",City2," ","to"," ",City4),nl, write("Total distance in this case is", " ",TT3),nl, write("Now starting from ",City4),nl, min3(D14,D24,D34,D14), TT4=D14+D12+D23, write("Now go from"," ",City4," ","to"," ",City1),nl, write("Cheap path is "," ",City4," ","to"," ",City1," ","to", " ",City2," ","to"," ",City3),nl, write("Total distance in this case is"," ",TT4),nl, write("Now comparing total distnaces in three cases conclusion is:"),nl, min4(TT1,TT2,TT3,TT4,TT1). travel(City1,City2,City3,City4):write("Enter the First City"),nl, readln(City1),nl, write("Enter the Second City"),nl, readln(City2),nl, write("Enter the Third City"),nl, readln(City3),nl, write("Enter the Fourth City"),nl, readln(City4),nl. getdist(D12,D13,D14,D23,D24,D34):write("Enter the distance b/w City1 and City2"),nl, readint(D12),nl, write("Enter the distance b/w City1 and City3"),nl, readint(D13),nl, write("Enter the distance b/w City1 and city4"),nl, readint(D14),nl, write("Enter the distance b/w City2 and City3"),nl, readint(D23),nl, write("Enter the distance b/w City2 and City4"),nl,

readint(D24),nl, write("enter the distance b/w City3 and City4"),nl, readint(D34),nl. min0(X,Y,Z,X):X<=Y,X<=Z,!. min0(X,Y,Z,X):Y<=X,Y<=Z, write("Now go from City1 to City3"),nl, write("cheap path is City1 to City3 to City2 to City4"),nl. min1(X,Y,Z,X):X<=Y,X<=Z,!. min1(X,Y,Z,X):Y<=X,X<=Z, write("Now go from City2 to city1"),nl, write("cheap path is City2 to City1 to City3 to City4"),nl. min2(X,Y,Z,X):X<=Y,X<=Z,!. min2(X,Y,Z,X):Y<=X,Y<=Z, write("Now go from City3 to City1"),nl, write("cheap path is City3 to City1 to City2 to City4"),nl. min3(X,Y,Z,X):X<=Y,X<=Z,!. min3(X,Y,Z,X):Y<=X,Y<=Z, write("Now go from City4 to City1"),nl, write("cheap path is City4 to City1 to City2 to city3"),nl. min4(X,Y,Z,W,X):X<=Y,X<=Z,X<=W, write("cheapeast path is from City1 to City2 to City3 to City4"),nl,!. min4(X,Y,Z,W,X):Y<=X,Y<=Z,X<=W, write("cheapeast path is from City2 to City1 to City3 to city4"),nl,!. min4(X,Y,Z,W,X):Z<=X,Z<=Y,Z<=W, write("cheapeast path is from City3 to City1 to City2 to City4"),nl,!. /*min4(X,Y,Z,W,X):W<=X,W<=Y,W<=Z, write("cheapest path is from City4 to City1 to City2 to City3" ),nl,!.*/

AIM: Monkey Banana Problem CODE: domains Height,Jump,Stick,Table=integer predicates go attempt(integer,Height,integer,integer,integer) task(integer) clauses go:clearwindow, write("\n\nspecify the following ways:"), write("\n\t\t\t1. Monkey can Jump "), readint(Jump), write("\n\t\t\t2. Length of Stick "), readint(Stick), write("\n\t\t\t3. Height of Table "), readint(Table), write("\n\t\t\t4. Height of bunch of bananas "), readint(Height), attempt(_,Height,Jump,Stick,Table), readln(_). attempt(1,Height,Jump,_,_):write("\n\n1. Monkey jumped to catch bananas"), Height<=Jump, task(1). attempt(2,Height,Jump,Stick,_):write("\t....failed"), write("\n\n2. Monkey jumped with the stick"), Height<=Jump+Stick, task(1). attempt(3,Height,Jump,_,Table):write("\t....failed"), write("\n\n3. Monkey placed the table"), write("\n monkey jumped from the table for bananas "), Height<=Jump+Table, task(1).

attempt(4,Height,Jump,Stick,Table):write("\t.....failed"), write("\n\n4. Monkey jumped with stick from the table"), Height<=Jump+Table+Stick, task(1). attempt(5,_,_,_,_):write("\n....failed"). task(1):write("\n.....Monkey successfully catch the bunch of bananas"). Output:: specify the following ways: 1. Monkey can Jump 3 2. Length of Stick 4 3. Height of Table 3 4. Height of bunch of bananas 9 1. Monkey jumped to catch bananas ....failed 2. Monkey jumped with the stick ....failed 3. Monkey placed the table monkey jumped from the table for bananas .....failed 4. Monkey jumped with stick from the table .....Monkey successfully catch the bunch of bananas