Lingaya S Vidyapeeth, Faridabad
Lingaya S Vidyapeeth, Faridabad
Lingaya S Vidyapeeth, Faridabad
LAB FILE
BCA 3rd Year
WebSite: www.lingayasuniversity.edu.in
Name :
0
PROGRAM NO. - 1
PROLOG-PROGRAMMING IN LOGIC
PROLOG stands for Programming In Logic an idea that emerged in the early 1970 s to use
logic as programming language. The early developers of this idea included Robert Kowalski at
Edinburgh ( on the theoretical side ), Marrten van Emden at Edinburgh ( experimental
demonstration) and Alian Colmerauer at Marseilles (implementation ). David D.H. Warrens
efficient implementation at Edinburgh in the mid -1970s greatly contributed to the popularity of
PROLOG.
SYMBOLIC LANGUAGE
1
FOR EXAMPLE:
a) FACTS:
b ) RULES:
grandfather(X,Z)
parent(X,Y)
parent(Y,Z)
male(X)
c) QUERIES:
Once we have a database of facts (and, soon, rules) we can ask questions about the stored
information.
Suppose we want to know if Turing lectures in course 9020. We can ask:
lecture(turnig,9020).
Applications of Prolog
2
problem solving
PROGRAM NO. 2
road("Karimnagar","Warangal",100).
road("Karimnagar","Hyderabad",120).
road("Hyderabad","Warangal",120).
road("Warangal","Vijaywada",120).
road("Hyderabad","Vijaywada",120).
road("Hyderabad","Kazipet",170).
road("Kazipet","Vijaywada",220).
road("Warangal","Vishakapatnam",170).
road("Vijaywada","Vishakapatnam",120).
route(Town1,Town2,Distance):-
road(Town1,Town2,Distance).
route(Town1,Town2,Distance):-
road(Town1,X,Dist1),
route(X,Town2,Dist2),
Distance=Dist1+Dist2,
!.
Output:
3
PROGRAM NO. 3
Tower(1,A,C,B):-
write("move from",A,"to",C),nl.
Tower(N,A,C,B):-
Z=N-1,
Tower(Z,A,B,C),
write("move from",A,"to",C),nl,
Tower(Z,B,C,A).
Output:
move from1to3
move from1to2
move from3to2
move from1to3
move from2to1
move from2to3
move from1to3
yes
4
PROGRAM NO. 4
fact(integer,integer)
go
clauses
go:-
write("enter the no."),nl,
readint(A),
B=1,
fact(A,B).
fact(A,B):-
A<>1,
Y=A-1,
Z=A*B,
fact(Y,Z).
fact(1,B):-
write("factorial is",B).
Output:
5
PROGRAM NO. 5
not_(D,D)
and_(D,D,D)
or_(D,D,D)
txor_(D,D,D)
CLAUSES
not_(1,0). not_(0,1).
and_(0,0,0). and_(0,1,0).
and_(1,0,0). and_(1,1,1).
or_(0,0,0). or_(0,1,1).
or_(1,0,1). or_(1,1,1).
and_(Input1,N2,N6),
and_(Input2,N1,N5),
and_(Input3,N4,N8),
and_(Input4,N3,N7),
or_(N5,N6,N9),
or_(N7,N8,N10),
or_(N9,N10,Output).
Output:
PROGRAM NO. 6
7
AIM: TO IMPLEMENT FAMILY RELATIONSHIPS
DATABASE - tmp
son(STRING,STRING)
sister(STRING,STRING)
brother(STRING,STRING)
married(STRING,STRING)
son("Deepak","Jack").
sister("Jiya","swati").
brother("Sonia", "Harsh").
married("Jack", "Jiya").
married("Lokesh", "Sonia").
married("Priya","Deepak").
father(A,B):-son(B,A).
grandfather(A,B):-father(A,C), father(C,B).
sister_in_law(A,B):-married(A,C), sister(C,B).
sister_in_law(A,B):-brother(A,C), married(C,B).
brother_in_law(A,B):-married(A,C), brother(C,B).
brother_in_law(A,B):-brother(A,C), married(C,B).
father_in_law(A,B):-married(A,C), son(C,B).
father_in_law("Priya",Z),
format(Msg,"father_in_law(\"Priya\",%)",Z),
write(Msg).
%GOAL father_in_law("Priya",Z).
Output:
father_in_law("Priya",Jack)Z=Jack, Msg=father_in_law("Priya",Jack)
PROGRAM NO. 7
8
AIM: LOGON EXAMPLE WITH RECURSION AND NO REPEAT PREDICATE
logon :-
getinput(Name,Password),
user(Name,Password),
write("You are now logged on."),nl.
logon :-
write("Sorry, you are not permitted access."),
write("Please try again."),nl,
logon.
getinput(Name,Password) :-
write("Please Enter Your Name:"),
readln(Name),nl,
write("Please Enter Password:"),
readln(Password),nl.
user(bill,bigfoot).
user(john,superman).
user(sue,happy).
getinput(Name,Password).
Output:
Name=bill, Password=bigfoot
1 Solution
PROGRAM NO. 8
9
AIM: TO PRINT THE LIST OF CUSTOMERS HAVING DIFFERENT COLORED CARS
WITH PRICE AND MODEL AVAILABLE.
cust_names([jitander,preeti,veena,avinash,jyeshtha],green).
cust_names([arvind,poonam,abhijeet],white).
cust_names([daya,bhanu,anuradha,anju],red).
model(red,[hyundai_accent,ford_monedo,indgo]).
model(white,[maruti_esteem,maruti_baleno]).
model(green,[toyota_avalon,lotus_elise]).
model(green,[toyta_avalon,lotus_elise]).
price(red,450000).
price(white,350000).
price(green,430000).
cost(green):-
cust_names(A,green),nl,
write(A),nl,
price(green,X),nl,
write("the price of green color car is ",X),nl,
model(green,Y),nl,
write("the model available in green color ",Y),nl.
cost(white):-
cust_names(A,white),nl,
write(A),nl,
price (white,X),nl,
write("the price of white color car is ",X),nl,
model(blue,Y),nl,
write(" the models available in white color ",Y),nl.
goal:
write(" name list of customers who own green color car :-"),nl,
cost(green).
/*write("name list of customers who own red color car :-"),nl,
cost(red).
write("name list of customer who own white color car :-"),nl,
cost(white). */
Output:
name list of customers who own green color car :-
["jitender","preeti","veena","avinash","jyeshtha"]
10
the model available in green color ["toyota_avalon","lotus_elise"]
yes
11
PROGRAM NO. 9
waterjug(X,Y):-
X=0,
Y=0,
write("4l jug empty & 3l jug empty"),nl,
Z=0,
A=3,
waterjug(Z,A).
waterjug(X,Y):-
X=0,
Y=3,
write("4l jug empty & 3ljug 3l water"),nl,
Z=3,
A=0,
waterjug(Z,A).
waterjug(X,Y):-
X=3,
Y=0,
write(" 4l jug 3l water & 3l jug empty"),nl,
Z=3,
A=3,
waterjug(Z,A).
waterjug(X,Y):-
Z=3,
A=3,
write("4l jug 3l water& 3l jug 3l water"),nl,
Z=4,
A=2,
waterjug(Z,A).
waterjug(X,Y):-
X=4,
Y=2,
write("4l jugh 4l water & 3l jug 2l water"),nl,
Z=0,
A=2,
waterjug(Z,A).
waterjug(X,A):-
X=0,
Y=2,
write("4l jug empty & 3l jug 2l water"),nl,
Z=2,
A=0,
12
waterjug(Z,A).
waterjug(X,Y):-
X=2,
Y=0,
write("Goal Achieved").
goal:waterjug(0,0).
Output:
PROGRAM NO. 10
13
AIM: TO IMPLEMENT BREADTH FIRST SEARCH
pop([Head|Tail],S1,X):-
X=Head,
S1=Tail.
append([],LIST1,LIST1).
append([X|LIST1],LIST2,[X|LIST3]):-
append(LIST1,LIST2,LIST3).
checkbfs([]):-
write("STACK NULL").
checkbfs(STACK):-
write("checkbfs"),nl,
pop(STACK,STACK2,X),
test(X,STACK2,S3),
write(S3),nl,
checkbfs(S3).
test(A,STACK2,S3):-
A=5,nl,
write(A,"goal found"),
S3=[].
test(A,STACK2,S3):-
write(A),nl,
S3=STACK2.
go:-
A=[10],B=[7],C=[6],D=[5],E=[12],F=[11],G=[15],
H=[16],I=[17],J=[20],
append(I,J,R1),
append(G,H,R2),
append(E,F,R3),
append(R2,R1,R4),
append(R3,R4,R5),
append(C,D,R6),
append(B,R6,R7),
append(R7,R5,R8),
append(A,R8,R9),
ROOT=R9,
write("Starting Program\n"),nl,
checkbfs(ROOT).
Goal:go.
Output:
Starting Program
14
checkbfs
10
[7,6,5,12,11,15,16,17,20]
checkbfs
7
[6,5,12,11,15,16,17,20]
checkbfs
6
[5,12,11,15,16,17,20]
checkbfs
5goal found[]
STACK NULLyes
PROGRAM NO. 11
pop([Head|Tail],S1,X):-
X=Head,
S1=Tail.
append([],LIST1,LIST1).
append([X|LIST1],LIST2,[X|LIST3]):-
append(LIST1,LIST2,LIST3).
checkdfs([]):-
write("STACK NULL").
checkdfs(STACK):-
write("checkdfs"),nl,
pop(STACK,STACK2,X),
test(X,STACK2,S3),
write(S3),nl,
checkdfs(S3).
test(A,STACK2,S3):-
A=5,nl,
write(A,"goal found"),
S3=[].
test(A,STACK2,S3):-
write(A),nl,
S3=STACK2.
go:-
D=[16],
E=[12],
F=[11],
G=[6],
H=[8],
I=[28],
J=[5],
A=[14],
append(D,E,A2),
append(A,A2,A1),
B=A1,
append(G,H,B2),
append(F,B2,B3),
append(B,B3,B1),
C1=[7],
append(F,J,C2),
16
append(C1,C2,C),
ROOT1=[10],
append(B,C,R2),
append(A1,R2,R3),
append(ROOT1,R3,ROOT),
write("starting program"),nl,
checkdfs(ROOT).
Goal:go.
Output:
starting program
checkdfs
10
[14,16,12,14,16,12,7,11,5]
checkdfs
14
[16,12,14,16,12,7,11,5]
checkdfs
16
[12,14,16,12,7,11,5]
checkdfs
12
[14,16,12,7,11,5]
checkdfs
14
[16,12,7,11,5]
checkdfs
16
[12,7,11,5]
checkdfs
12
[7,11,5]
checkdfs
7
[11,5]
checkdfs
11
[5]
checkdfs
5goal found[]
STACK NULLyes
17
PROGRAM NO. 12
nondeterm solve
18
nondeterm candidate(HLIST,HLIST,HLIST,HLIST,HLIST)
nondeterm perm(HLIST)
nondeterm constraints(HLIST,HLIST,HLIST,HLIST,HLIST)
nondeterm permutation(NOLIST,NOLIST)
nondeterm delete(NO,NOLIST,NOLIST)
member(HOUSE,HLIST)
nondeterm next(NO,NO)
nondeterm lleft(NO,NO)
GOAL solve.
solve() :-
constraints(Colours,Drinks,Nationalities,Cigarettes,Pets),
candidate(Colours,Drinks,Nationalities,Cigarettes,Pets),
member(h(water,WaterHouse), Drinks),member(h(WaterColour,WaterHouse), Colours),
member(h(zebra,ZebraHouse), Pets),member(h(ZebraColour,ZebraHouse), Colours),
write("They drink water in the ",WaterColour," house\n"),
write("The zebra live in the ",ZebraColour," house\n").
perm([h(_,A),h(_,B),h(_,C),h(_,D),h(_,E)]) :-
permutation([A,B,C,D,E],[1,2,3,4,5]).
permutation([],[]).
permutation([A|X],Y) :- delete(A,Y,Y1), permutation(X,Y1).
delete(A,[A|X],X).
delete(A,[B|X],[B|Y]) :- delete(A,X,Y).
member(A,[A|_]) :- !.
member(A,[_|X]) :- member(A,X).
next(X,Y) :- lleft(X,Y).
next(X,Y) :- lleft(Y,X).
lleft(1,2).
lleft(2,3).
lleft(3,4).
lleft(4,5).
20
Output:
PROGRAM NO. 13
GLOBAL DATABASE
det( STRING )
noun( STRING )
21
rel( STRING )
verb( STRING )
% Parser
nondeterm s_determ( TOKL, TOKL, DETERM )
nondeterm s_nounp( TOKL, TOKL, NOUNP )
nondeterm s_relcl( TOKL, TOKL, RELCL )
nondeterm s_sentence( TOKL, TOKL, SENTENCE )
nondeterm s_verbp( TOKL, TOKL, VERBP )
% scanner
check(STRING)
tokl( STRING, TOKL )
GOAL
consult("sen_an.dba"),
write("Try: every men loves women\n"),
write("Write a sentence: "),
readln(STR),
tokl(STR,TOKL),
s_sentence( TOKL, RESTTOKL, SENT ),
RESTTOKL = [],
write(SENT).
s_sentence(TOKL,TOKL2,sent(NOUNP,VERBP)):-
s_nounp(TOKL,TOKL1,NOUNP),
s_verbp(TOKL1,TOKL2,VERBP),
TOKL2 = [] ,!.
s_sentence(_,_,_):-
write(">> Sentence not recognized\n"),fail.
22
s_nounp(TOKL,TOKL2,nounp(DETERM,NOUN,RELCL)):-
s_determ(TOKL,[NOUN|TOKL1],DETERM),
is_noun(NOUN),
s_relcl(TOKL1,TOKL2,RELCL).
s_determ([DETERM|TOKL],TOKL,determ(DETERM)):-
is_det(DETERM).
s_determ(TOKL,TOKL,none).
s_relcl([REL|TOKL],TOKL1,relcl(REL,VERBP)):-
is_rel(REL),
s_verbp(TOKL,TOKL1,VERBP).
s_relcl(TOKL,TOKL,none).
s_verbp([VERB|TOKL],TOKL1,verbp(VERB,NOUNP)):-
is_verb(VERB),
s_nounp(TOKL,TOKL1,NOUNP).
s_verbp([VERB|TOKL],TOKL,verb(VERB)):-
is_verb(VERB).
is_noun(X):-noun(X),!.
is_noun(X):-noun(Y),concat(Y,"s",X),!.
is_det(X):-det(X),!.
is_rel(X):-rel(X),!.
is_verb(X):-verb(X),!.
is_verb(X):-verb(Y),concat(Y,"s",X),!.
is_verb(X):-verb(Y),concat(Y,"ed",X),!.
is_verb(X):-verb(Y),concat(Y,"es",X),!.
is_verb(X):-verb(Y),concat(Y,"ing",X),!.
tokl(STR,[TOK|TOKL]) :-
fronttoken(STR,TOK,STR1),
check(TOK),!,
tokl(STR1,TOKL).
tokl(_,[]).
check(WORD):-is_noun(WORD),!.
check(WORD):-is_det(WORD),!.
check(WORD):-is_rel(WORD),!.
check(WORD):-is_verb(WORD),!.
check(WORD):- write(">> Unknown word: ",WORD),nl.
23
PROGRAM NO. 14
H,F,I,Y,Y1,Xdist,Dist1,Queen=integer
T,L,L1,PL,PT,Queen,Others=integer*
24
safe(L)
solution(L)
permutation(L,L)
del(I,L,L)
noattack(I,L,L)
solution(Queens):-
permutation([1,2,3,4,5,6,7,8],Queens),
safe(Queens).
permutation([],[]).
permutation([H|T],PL):-
permutation(T,PT),
del(H,PT,PI).
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).
OUTPUT:
Goal:
Solutions(s)
PROGRAM NO. 15
State1,State2,MH,MV,BP,HB,P1,P2=symbol
Move=symbol*
move(State1,Move,State2).
state(MH,MV,BP,HB).
push(P1,P2).
25
walk(P1,P2).
grasp.
climb.
move(State(middle,onbox,middle,hasnot),grasp,state(middle,onbox,middle,has)).
move(State(P,onfloor,P,H),climb,state(P,onbox,P,H)).
move(State(P1,onfloor,P1,H),push(P1,P2),state(P2,onfloor,P2,H)).
move state(P1,onfloor,B,H),
walk(P1,P2), % Walk from P1 to P2.
state(P2,onfloor,B,H)).
% change state (State): monkey can get banana in State.
26