Lab Manual FOR Is Lab: WCTM /It/Lab Manual/6Th Sem/If Lab
Lab Manual FOR Is Lab: WCTM /It/Lab Manual/6Th Sem/If Lab
LAB MANUAL
FOR
IS LAB
1
INTELLIGENT SYSTEM LAB MANUAL
WCTM /IT/LAB MANUAL/6TH SEM/IF LAB
STUDY OF PROLOG
Prolog – Programming in Logic
PROLOG stands for Programming In Logic – an idea that emerged in the early 1970s 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 Alain Colmerauer at Marseilles ( implementation).
David D.H.Warren’s efficient implementation at Edinburgh in the mid – 1970’s greatly
contributed to the popularity of PROLOG.
SYMBOLIC LANGUAGE
PROLOG is a programming language for symbolic , non – numeric computation. It is
especially well suited for solving problems that involve objects and relations between
objects .
For example:
1. Facts : Some facts about family relationships could be written as :
sister(sue, bill)
parent(ann, sam)
2
INTELLIGENT SYSTEM LAB MANUAL
WCTM /IT/LAB MANUAL/6TH SEM/IF LAB
parent(joe,ann)
male(joe)
female(ann)
3. Queries : Given a data of facts and rules such as that above, we mat make queries
by tying after a query symbol ‘?_’ statements such as :
?_parent(X,sam)
X=ann
?_male(joe)
yes
?_grandfather(X,Y)
X=joe, Y=sam
?_female(joe)
no
META-PROGRAMMING
A meta-program is a program that other programs as data. Interpreters and compilers are
examples of meta-programs. Meta-interpreter is a particular kind of meta-program: an
interpreter for a language written in that language. So a PROLOG meta-interpreter is an
interpreter for PROLOG, itself written in PROLOG.
3
INTELLIGENT SYSTEM LAB MANUAL
WCTM /IT/LAB MANUAL/6TH SEM/IF LAB
On the other hand, there is not much emphasis on efficiency of implementation. Once the
ideas are developed, a prototype may have to be re-implemented, possibly in another,
more efficient programming language. Even if this is necessary, the prototype is useful
because it usually helps to speed up the creative development stage.
4
INTELLIGENT SYSTEM LAB MANUAL
WCTM /IT/LAB MANUAL/6TH SEM/IF LAB
STEP 2 : Calculate the permutation of the above eight numbers stored in set P.
STEP 3 : Let the position where the first queen to be placed be (1,Y), for second be
(2,Y1) and so on and store the positions in Q.
STEP 4 : Check for the safety of the queens through the predicate , ‘noattack ()’.
STEP 5 : Calculate Y1-Y and Y-Y1. If both are not equal to Xdist , which is the X –
distance between the first queen and others, then go to Step 6 else go to Step 7.
STEP 7 : Repeat above for the rest of the queens , until the end of the list is reached .
STEP 9 : Exit.
5
INTELLIGENT SYSTEM LAB MANUAL
WCTM /IT/LAB MANUAL/6TH SEM/IF LAB
START
H= integer
T= integer*
Take solution in Q
Is Y1- YES Is Y-
Y<>Xdist Y1<>Xdist
NO YES
Increment Xdist by 1
STOP Is
Print Q Q=0
YES
NO
6
INTELLIGENT SYSTEM LAB MANUAL
WCTM /IT/LAB MANUAL/6TH SEM/IF LAB
predicates
safe(T)
solution(T)
permutation(T,T)
del(H,T,T)
noattack(H,T,H)
clauses
del(I,[I|L],L). /*to take a position from the permutation of list*/
del(I,[F|L],[F|L1]):-
del(I,L,L1).
7
INTELLIGENT SYSTEM LAB MANUAL
WCTM /IT/LAB MANUAL/6TH SEM/IF LAB
OUTPUT:-
goal:-solution(Q).
Q=[“3”,”8”,”4”,”7”,”1”,”6”,2”,”5”]
8
INTELLIGENT SYSTEM LAB MANUAL
WCTM /IT/LAB MANUAL/6TH SEM/IF LAB
STEP 2 : If the initial state is a goal state, quit and return success.
(a) Generate a successor, E, of the initial state. If there are no more successors,
signal failure.
STEP 5 : Exit.
9
INTELLIGENT SYSTEM LAB MANUAL
WCTM /IT/LAB MANUAL/6TH SEM/IF LAB
START
NO Is node= YES
Generate a successor Print output as same
E, of initial state goal state node
YES
NO
STOP
10
INTELLIGENT SYSTEM LAB MANUAL
WCTM /IT/LAB MANUAL/6TH SEM/IF LAB
predicates
child(X,X)
childnode(X,X,Y)
path(X,X,Y)
clauses
child(a,b). /*b is child of a*/
child(a,c). /*c is child of a*/
child(a,d). /*d is child of a*/
child(b,e). /*b is child of b*/
child(b,f). /*f is child of b*/
child(c,g). /*g is child of c*/
path(A,G,[A|Z]):- /*to find the path from root to leaf*/
childnode(A,G,Z).
childnode(A,G,[G]):- /*to determine whether a node is child of other*/
child(A,G).
childnode(A,G,[X|L]):-
child(A,X),
childnode(X,G,L).
11
INTELLIGENT SYSTEM LAB MANUAL
WCTM /IT/LAB MANUAL/6TH SEM/IF LAB
OUTPUT:-
goal:-path(a,e,L)
L=[“a”,”b”,”e”]
12
INTELLIGENT SYSTEM LAB MANUAL
WCTM /IT/LAB MANUAL/6TH SEM/IF LAB
Step 1: Declare the functions for member, concatenation, permutation, add and delete.
Step 8: Exit.
13
INTELLIGENT SYSTEM LAB MANUAL
WCTM /IT/LAB MANUAL/6TH SEM/IF LAB
START
X=integer
Y=integer*
1. Member
2. Concatenation
3. Permutation
4. Add
5. Delete
Enter your choice
Store choice in X
1 2 3 4 5
14
INTELLIGENT SYSTEM LAB MANUAL
WCTM /IT/LAB MANUAL/6TH SEM/IF LAB
1 2 2
YES
A is member STOP
of L1
5
4
15
INTELLIGENT SYSTEM LAB MANUAL
WCTM /IT/LAB MANUAL/6TH SEM/IF LAB
predicates
member(X,Y)
concatenation(Y,Y,Y)
add(X,Y,Y)
delete(X,Y,Y)
permutation(Y,Y)
choice(X)
goal
makewindow(1, 18, 680, “Menu Driven”, 1, 1, 20, 70),
write(“1. Member\n 2. Concatenation\n 3. Permutation\n 4. Add\n 5. Delete\n “),
write(“Enter the choice :: “),
readint(X),
choice(X).
clauses
choice(1):- /*Member function*/
write(“\nEnter the set :: “),
readterm(Y,L1),
write(“\nEnter the number to be checked :: “),
readint(A),
member(A,L1),
write(A),
write(“is a member of “),
write(L1),
write(“\n\nEnter your choice again :: “),
readint(X),
choice(X).
16
INTELLIGENT SYSTEM LAB MANUAL
WCTM /IT/LAB MANUAL/6TH SEM/IF LAB
choice(X).
17
INTELLIGENT SYSTEM LAB MANUAL
WCTM /IT/LAB MANUAL/6TH SEM/IF LAB
18
INTELLIGENT SYSTEM LAB MANUAL
WCTM /IT/LAB MANUAL/6TH SEM/IF LAB
OUTPUT:-
goal:
1. Member
2. Concatenate
3. Permutation
4. Add
5. Delete
19
INTELLIGENT SYSTEM LAB MANUAL
WCTM /IT/LAB MANUAL/6TH SEM/IF LAB
STEP 3 : Check whether H is also a member of the other list L1.If yes, goto Step4 else
goto Step5.
STEP 4 : check H is the last element of the list L. If yes, goto Step 6 else goto Step 5.
STEP 5 : Compare the rest of the elements of the tail T with that of the other list L1.
Goto Step3.
Do not duplicate the elements.
STEP 8 : Exit.
20
INTELLIGENT SYSTEM LAB MANUAL
WCTM /IT/LAB MANUAL/6TH SEM/IF LAB
START
X= integer*
Y= integer*
Is NO Compare T with
H=L L1
YES
Store H in X
NO
Is
H=0
YES
STOP
21
INTELLIGENT SYSTEM LAB MANUAL
WCTM /IT/LAB MANUAL/6TH SEM/IF LAB
predicates
member(X,L)
union(L,L,L)
clauses
member(X,[X|_]). /*to find the member of the list*/
member(X,[_|T]):-
member(X,T).
22
INTELLIGENT SYSTEM LAB MANUAL
WCTM /IT/LAB MANUAL/6TH SEM/IF LAB
OUTPUT:-
goal:-
union([1,2,3,4,5],[1,4,5,3,6,7],X)
X=[1,2,3,4,5,6,7]
23
INTELLIGENT SYSTEM LAB MANUAL
WCTM /IT/LAB MANUAL/6TH SEM/IF LAB
STEP 3 : Check whether H is also a member of the other list L1. If yes , go to Step 4 else
go to Step5.
STEP 5 : Check H is the last element of the list L. If yes, go to Step7 else go to Step 6.
STEP 6 : Compare the rest of the elements of the Tail T with that of the other list L1 .
Goto Step 3 .
STEP 8 : Exit.
24
INTELLIGENT SYSTEM LAB MANUAL
WCTM /IT/LAB MANUAL/6TH SEM/IF LAB
START
X= integer*
Y= integer*
Is
H=L NO Compare T with L
YES
Store H in X
Is NO
H=0
YES
STOP
25
INTELLIGENT SYSTEM LAB MANUAL
WCTM /IT/LAB MANUAL/6TH SEM/IF LAB
predicates
member(X,L)
intersection(L,L,L)
clauses
member(X,[X|_]). /*to find the member of the list*/
member(X,[_|T):-
member(X,T).
26
INTELLIGENT SYSTEM LAB MANUAL
WCTM /IT/LAB MANUAL/6TH SEM/IF LAB
OUTPUT:-
goal:-
intersection([3,5,7,4,2],[1,3,2,4,5,6],X)
X=[3,5]
27
INTELLIGENT SYSTEM LAB MANUAL
WCTM /IT/LAB MANUAL/6TH SEM/IF LAB
Step 4: If X! =0 i.e. =B, then perform factorial of B and print the result.
Step 5: Exit.
28
INTELLIGENT SYSTEM LAB MANUAL
WCTM /IT/LAB MANUAL/6TH SEM/IF LAB
START
X= integer
A=1
Is YES Factorial of X
X=0 is 1
NO
fact(B,A)
C=A*B
D=B-1
fact(D,C)
STOP
29
INTELLIGENT SYSTEM LAB MANUAL
WCTM /IT/LAB MANUAL/6TH SEM/IF LAB
predicates
go
factX,X)
clauses
go:-
write(“\nEnter the no. :: “),
readint(X),
A=1.
30
INTELLIGENT SYSTEM LAB MANUAL
WCTM /IT/LAB MANUAL/6TH SEM/IF LAB
OUTPUT:-
goal:-
go
Enter the no. :: 4
Factorial of 4 is 16
31
INTELLIGENT SYSTEM LAB MANUAL
WCTM /IT/LAB MANUAL/6TH SEM/IF LAB
STEP 2 : Create a variable called NODE-LIST and set it to the initial state.
(b) For each way that each rule can match the state described in E do :
STEP 5 : Exit.
32
INTELLIGENT SYSTEM LAB MANUAL
WCTM /IT/LAB MANUAL/6TH SEM/IF LAB
START
NO
Check rules that match E
Is new
NO Add new state to the end of
state=
NODE_LIST
goal state
YES
A STOP
33
INTELLIGENT SYSTEM LAB MANUAL
WCTM /IT/LAB MANUAL/6TH SEM/IF LAB
predicates
solve(L, L)
member(X,L)
extend(L, L)
conc(X, L, L)
breadthfirst(L, L)
goal(X)
clauses
solve(start, solution):- /*solution is a state from start to a goal*/
breadthfirst([[start]],solution).
breadthfirst([path|paths], solution):-
extend(path,newpaths),
conc(paths,newpaths,path1),
breadthfirst(path1,solution).
extend([node|path],newpaths):-
bagof([newnode, node|path],(s(node,
newnode),notmember(newnode,[node|path])), newpaths),!.
extend(path, []).
conc([], L, L).
member(X, [X|T]).
member(X, [H|T]):-
member(X, T).
34
INTELLIGENT SYSTEM LAB MANUAL
WCTM /IT/LAB MANUAL/6TH SEM/IF LAB
OUTPUT:-
35
INTELLIGENT SYSTEM LAB MANUAL
WCTM /IT/LAB MANUAL/6TH SEM/IF LAB
domains
State1,State2,MH,MV,Bp,HB,P1,P2=symbol
Move=symbol*
predicates
move(State1,Move,State2).
state(MH,MV,BP,HB).
push(P1,P2).
walk(P1,P2).
graps.
climb.
clauses
move(state(middle,onbox,middle,hasnot), /*Before move*/
grasp, /*Grasp banana*/
state (middle,onbox,middle,has)). /*After move*/
move(state(P,onfloor,P,H),
climb, /*Climb box*/
state(P,onbox,P,H)).
move(state(P1,onfloor,P1,H),
push(P1,P2), /*Push box from P1 to 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)).
36
INTELLIGENT SYSTEM LAB MANUAL
WCTM /IT/LAB MANUAL/6TH SEM/IF LAB
OUTPUT:-
goal: canget(atdoor,atfloor,window,hasnot)
No solution.
37
INTELLIGENT SYSTEM LAB MANUAL
WCTM /IT/LAB MANUAL/6TH SEM/IF LAB
predicates
permute(Y,Y)
delete(X,Y,Y)
clauses
delete(X,[X|T],T).
delete(X,[H|T],[H|T1]):-
delete(X,T,T1).
permute([],[]).
permute([H|T],PL):-
permute(T,PT),
delete(H,PL,PT).
38
INTELLIGENT SYSTEM LAB MANUAL
WCTM /IT/LAB MANUAL/6TH SEM/IF LAB
OUTPUT:-
goal:permute([1,2],A)
A=[1,2]
A=[2,1]
2 Solutions
39
INTELLIGENT SYSTEM LAB MANUAL
WCTM /IT/LAB MANUAL/6TH SEM/IF LAB
predicates
concatenate(Y,Y,Y)
clauses
concatenate([],[]).
concatenate([H|T],L,[H|T1]):-
concatenate(T,L1,L2).
40
INTELLIGENT SYSTEM LAB MANUAL
WCTM /IT/LAB MANUAL/6TH SEM/IF LAB
OUTPUT:-
goal:concatenate([1,2,3],[4,5],A)
A=[1,2,3,4,5]
41
INTELLIGENT SYSTEM LAB MANUAL
WCTM /IT/LAB MANUAL/6TH SEM/IF LAB
predicates
member(X,Y)
clauses
member(X,[X|T]).
member(X,[F|L]):-
member(X,L).
42
INTELLIGENT SYSTEM LAB MANUAL
WCTM /IT/LAB MANUAL/6TH SEM/IF LAB
OUTPUT:-
goal:member(2,[2,3,4])
Yes
43
INTELLIGENT SYSTEM LAB MANUAL