0% found this document useful (0 votes)
7 views58 pages

AI LAB Record (Final)

The document provides an overview of Prolog, including its history, features, applications, and limitations. It details various Prolog programming examples, such as basic arithmetic operations, factorial calculation, and the missionaries and cannibals problem. Additionally, it discusses the implementation of graph coloring and the blocks world problem using Prolog.

Uploaded by

hema.docs88
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views58 pages

AI LAB Record (Final)

The document provides an overview of Prolog, including its history, features, applications, and limitations. It details various Prolog programming examples, such as basic arithmetic operations, factorial calculation, and the missionaries and cannibals problem. Additionally, it discusses the implementation of graph coloring and the blocks world problem using Prolog.

Uploaded by

hema.docs88
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 58

EX.

NO:1 STUDY OF PROLOG

DATE:

AIM:

To study introduction about PROLOG. Prolog stands for Programming in Logic: The
language was originally developed in 1972 by Aliancolmerauer and Perouse at the University of
Marseilles in France.

HISTORY OF PROLOG:

Durin the 1970s prolog became popular in Europe for Artifical intelligence Applications
.During there early year both prolog and LISP were very slow in execution and consumed large
amounts of memory. Also user needs considerable programming experience to use a Prolog
program. The picture suddenly changed in 1981 at the Frist International conference on 5th
Generation System in Tokyo. The Japanese were having great difficulties competing with the
rapidly growing U.S. computer market. As a result prolog started in practical Use.

FEATURES OF PROLOG:

 It is an object-oriented language.
 It uses heuristics to solve problems.
 It is most efficient at formal reasoning.
 The Prolog system is developed & maintained by Knowledge Engineers.
 The Prolog programming is interactive and cycle development process.

PROGRAMMING IN PROLOG:

Data object are the basic blocks. Terms are used to represent the basic data type.
Procedures, clauses and predicates are written in order to implement programming constructs.
Progressive Substitution is used to match the clauses and rules. Recursion Process can also be
implemented in the prolog.

Operations on the terms are also done with a lot of ease.

APPLICATIONS OF PROLOG:

 In expert systems.
 In Natural Language Processing.
 In Robotics.
 In Gaming and Simulation.
FEATURES OF TURBO PROLOG:

 Compile stand alone program that will execute on a machine that Is not running Turbo
Prolog.
 A full complement of Standard predicates for many functions like string, file operation
are available.
 A functional interface to other language is provides allowing procedural language support
to be added to any Prolog Program.
 Declared variables are used to provide more secure development control.
 Both integer and real arithmetic are provided.
 An integrated editor is provided, making program development, compilation and
debugging very easy.

LIMITATIONS OF TURBO PROLOG:

 The variable declaration process imposes some difficulties on symbolic processing.


 It does not support virtual memory; program size is limited by disc space.
 It inefficient at numerical processing.

RESULT:

Thus the basic of PROLOG is studied.


EX.NO: 2(A) Example program on prolog

DATE:

AIM:

To implement basic prolog programs using SWI Prolog.

PROCEDURE:

Step 1: Type the program in Notepad

Step 2: Save it as filename.pl

Step 3: Open SWI Prolog software, go to file menu and select consult to include the program.

Step 4: Run the .pl file

Step 5: Enter the logic and determine the output.

PROGRAM:

Jack has apples

has(jack,apples).

has(ann,plums).

has(dan,money).

fruit(apples).

fruit(plums).
Output:

?- [lesson1]. /* loads the file */


yes
?- listing(fruit). /* lists the clauses */
fruit(apples).
fruit(plums).
?- listing(has).
has(jack,apples).
has(ann,plums).
has(dan,money).
?- has(jack,X). /* what has Jack? */
X = apples
?- has(jack,_). /* does Jack have something? */
yes
?- has(X,apples),has(Y,plums). /* who has apples and who has plums? */
X = jack
Y = ann
?- has(X,apples),has(X,plums). /* does someone have apples and plums? */
no
?- has(dan,X),fruit(X). /* has Dan fruits? */
no
?- has(X,Y),not fruit(Y). /* does someone have something else? */
X = dan
Y = money

RESULT:

Thus the BASIC PROLOG PROGRAMS have been executed and the output is verified
successfully.
EX.NO: 2(B) ARITHMETIC ON WIN-PROLOG

DATE:

AIM:

To implement basic prolog programs using SWI Prolog.

PROCEDURE:

Step 1: Type the program in Notepad

Step 2: Save it as filename.pl

Step 3: Open SWI Prolog software, go to file menu and select consult to include the program.

Step 4: Run the .pl file

Step 5: Enter the logic and determine the output.

PROGRAM:

Arithmetic in Win-Prolog

+ plus - minus
* multiplication ^ power
/ division // integer division
sqrt square root mod N mod M is the remainder N/M
Output:
?- X is 5+4, Y is 5-4.
X=9
Y=1
?- X is 5*4, Y is 2^3.
X = 20
Y=8
?- X is 234556^100.
Error 0:Arithmetic Overflow
?- X is 5/3, Y=5//3.
X=1.66666667
Y=1
?- X is sqrt(3),Y is 3^0.5.
X = 1.73205080756888
Y = 1.73205080756888
?- X is 8 mod 3.
X=2
?- Y is 10^3 * (1+1) + 3.
Y = 2003
?- halt. Exit from Prolog

RESULT:

Thus the BASIC PROLOG PROGRAMS have been executed and the output is verified
successfully.
EX.NO: 2(C) FACTORIAL OF A GIVEN NUMBER

DATE:

AIM:

To implement basic prolog programs using SWI Prolog.

PROCEDURE:

Step 1: Type the program in Notepad

Step 2: Save it as filename.pl

Step 3: Open SWI Prolog software, go to file menu and select consult to include the program.

Step 4: Run the .pl file

Step 5: Enter the logic and determine the output.

PROGRAM:

Factorial of a given number

fact(0,1).
fact(N,R):- fact(N1,R1),N is N1+1,R is R1*N.

/*Factorial of n is n!= 1 * 2* 3 * 4 *... * n */


Output:

?- fact(4,R).
R = 24

RESULT:

Thus the BASIC PROLOG PROGRAMS have been executed and the output is verified
successfully.
EX.NO: 2(D) ADD TWO NUMBERS

DATE:

AIM:

To implement basic prolog programs using SWI Prolog.

PROCEDURE:

Step 1: Type the program in Notepad

Step 2: Save it as filename.pl

Step 3: Open SWI Prolog software, go to file menu and select consult to include the program.

Step 4: Run the .pl file

Step 5: Enter the logic and determine the output.

PROGRAM:

Add two numbers

start:- sum,nl.
sum:- write('X= '),read(X),
write('Y= '),read(Y),
S is X+Y,
write('Sum is '),write(S).
Output:

?- start.
X= |: 1.
Y= |: 2.
Sum is 3
Yes

RESULT:

Thus the BASIC PROLOG PROGRAMS have been executed and the output is verified
successfully.
EX.NO: 2(E) ARITHMETIC MEAN
DATE:

AIM:

To implement basic prolog programs using SWI Prolog.

PROCEDURE:

Step 1: Type the program in Notepad

Step 2: Save it as filename.pl

Step 3: Open SWI Prolog software, go to file menu and select consult to include the program.

Step 4: Run the .pl file

Step 5: Enter the logic and determine the output.

PROGRAM:

Arithmetic mean

start:- write('X= '),read(X),


write('Y= '),read(Y),
R is (X+Y)/2,
write('R is '),write(R),nl.
Output:

?- start.
X= 4.
Y= 6.
R is 5
yes

RESULT:

Thus the basic prolog programs have been executed and the output is verified
successfully.
EX NO: 3 MISSIONARIES AND CANNIBALS PROBLEM

DATE:

AIM:
To implement three missionaries and three cannibals find themselves on one side of a
river. They have would like to get to the other side using prolog.

ALGORITHM:

STEP1: start

STEP2: Number of cannibals should lesser than the missionaries on either side.

STEP3: Only one boat is available to travel.

STEP4: Only one or maximum of two people can go in the boat at a time.

STEP5: All the six have to cross the river from bank.

STEP6: There is no restriction on the number of trips that can be made to reach of the
goal.

STEP7: Both the missionaries and cannibals can row the boat.

STEP8: stop
CODE:

initial_state(mc,mc(left,bank(3,3),bank(0,0))).

final_state(mc(right,bank(0,0),bank(3,3))).

move(mc(left,L,_R),Boat):-

choose_passengers(L,Boat).

move(mc(right,_L,R),Boat):-

choose_passengers(R,Boat).

choose_passengers(bank(C,_M),boat(2,0)):-

C>1.

choose_passengers(bank(_C,M),boat(0,2)):-

M>1.

choose_passengers(bank(C,M),boat(1,1)):-

C>0,M>0.

choose_passengers(bank(C,_M),boat(1,0)):-

C>0.

choose_passengers(bank(_C,M),boat(0,1)):-

M>0.

update(mc(B,L,R),Boat,mc(B1,L1,R1)):-

update_boat(B,B1),

update_banks(Boat,B,L,R,L1,R1).

update_boat(left,right).

update_boat(right,left).

update_banks(alone,_B,L,R,L,R).

update_banks(boat(C,M),left,

bank(LC,LM),bank(RC,RM),
bank(LC1,LM1),bank(RC1,RM1)):-

LM1 is LM-M,

RM1 is RM+M,

LC1 is LC-C,

RC1 is RC+C.

update_banks(boat(C,M),right,

bank(LC,LM),bank(RC,RM),

bank(LC1,LM1),bank(RC1,RM1)):-

LM1 is LM+M,

RM1 is RM-M,

LC1 is LC+C,

RC1 is RC-C.

legal(mc(left,_L,R)):-

\+illegal(R).

legal(mc(right,L,_R)):-

\+illegal(L).

illegal(bank(C,M)):-

M>C.

solve_dfs(State,_History,[step(State)]):-

final_state(State).

solve_dfs(State,History,[step(State,Move)|States]):-

move(State,Move),

update(State,Move,State1),

legal(State1),

\+member(State1,History),
solve_dfs(State1,[State1|History],States).

print_step(step(mc(left,L,R),B)):-

format("~p* ~p~n",[L,R]),

format(" ->~p-> ~n",[B]).

print_step(step(mc(right,L,R),B)):-

format("~p * ~p~n",[L,R]),

format(" <-~p<- ~n",[B]).

print_step(step(mc(left,L,R))):-

format("~p* ~p~n",[L,R]).

print_step(step(mc(right,L,R))):-

format("~p * ~p~n",[L,R]).

print_steps([A|AS]):-

print_step(A),

print_steps(AS).

print_steps([]).

go:-

initial_state(mc,State),

solve_dfs(State,[State],Steps),

print_steps(Steps).
OUTPUT:

1 ?- go.

Bank( 3 , 3 )* bank( 0 , 0 )

->boat( 1 , 1 )->

Bank( 2 , 2 ) * bank( 1 , 1 )

<-boat( 0 , 1 )<-

Bank( 2 , 3 )* bank( 1 , 0 )

->boat( 0 , 2 )->

Bank( 2 , 1 ) * bank( 1 , 2 )

<-boat( 0 , 1 )<-

Bank( 2 , 2 )* bank( 1 , 1 )

->boat( 0 , 2 )->

Bank( 1 , 1 ) * bank( 2 , 2 )

<-boat( 0 , 1 )<-

Bank( 1 , 2 )* bank( 2 , 1 )

->boat( 0 , 2 )->

Bank( 1 , 0 ) * bank( 2 , 3 )

<-boat( 0 , 1 )<-

Bank( 1 , 1 )* bank( 2 , 2)

->boat( 1 , 1 )->

Bank( 0 , 0 ) * bank( 3 , 3 )

true
RESULT

Thus the program for MISSIONARIES AND CANNIBALS PROBLEMS to cross from
one to other with the help of a boat is being implemented using prolog is verified and executed
successfully.
EX NO: 4 GRAPH COLORING

DATE:

AIM:
To implement graph coloring using prolog.

ALGORITHM:

Step 1: Start

Step 2: Starting the index and node values

Step 3: Optimal the number of colors

Step 4: Current vertex to be colored

Step 5: Color (j)= number of colors at A[0]….A[j]

Step 6: Variables for the set of free colors of x

Step 7: x is colored for the following for loop if given constraint is satisfied the solution is found
in pair.

Step 8: As the list of node-color assignment, number2color(C,N,W)

Step 9: Displays the output as in runtime using comp_statistics.

Step 10: Stop


CODE:

coloring_fd(Colors,Sol):-

findall([N1,N2],arc(N1,N2),Arcs),

findall(Node,vertex(Node),L),

length(L,NumNodes),

%length is a built-in predicate

%NumNodes is thus the number of nodes in L

color2number(Colors,1,C2N),

%represent solors by numbers starting from 1,and

%remember the correspondence in C2N

length(DomainVars,NumNodes),

%DomainVars is bound to a list of variable of size NumNodes

length(Colors,N),!,

domain(DomainVars,1,N),

%domainVars will be instantiated to number corresponding to colors

%Starting from 0,position of a var in DomainVars is the node number

contrain(DomainVars,Arcs),

%the contraint to be satisfied

labeling([],DomainVars),

%after this,a solution is found

pair(DomainVars,Sol,0,C2N).

%construct Sol as a list of node-color assignments

print_coloring(Sol),

comp_statistics.

%utilities
print_coloring([]):-write('Done!').

print_soloring([[N,C]|L]):-

write('Node'),write(N),write('gets'),

write('color'),write(C),nl,

print_coloring(L).

color2number([],_,[]).

color2number([A|L],N,[[A,N]|L1]):-

M is N+1,

color2number(L,M,L1).

pair([],[],_,_):- !.

pair([A|L],[[N,C]|L1],N,C2N):-

number2color(C,A,C2N),

N1 is N+1,

pair(L,L1,N1,C2N).

number2color(C,N,[[C,N]|W]).

number2color(C,N,[[C1N1]|W]):-

N\==N1,

number2color(C,N,W).

comp_statistics:-

statistics(runtime,[_,X]),

T is X/1000,

nl,

write('run time:'),

write(T),write('sec.'),nl.
OUTPUT:

1 ?- comp_tatistics.

run time:0. 109sec.

true.

RESULT:

Thus the program for GRAPH COLORING being implemented using prolog is verified
and executed successfully.
EX.NO: 5 BLOCKS WORLD PROBLEM
DATE:

AIM:
To execute the blocks world problem using SWI Prolog.

PROCEDURE:

Step 1: Type the program in Notepad

Step 2: Save it as filename.pl

Step 3: Open SWI Prolog software, go to file menu and select consult to include the program.

Step 4: Run the .pl file

Step 5: Enter the logic and determine the output.

PROGRAM:

FORWARD APPROACH:

on(a,b).
on(b,c).
on(c,table).
on(a,table).
on(b,c).
on(c,table).
put_on(A,B) :-
A \== table,
A \== B,
on(A,X),
clear(A),
clear(B),
retract(on(A,X)),
assert(on(A,B)),
assert(move(A,X,B)).
clear(table).
clear(B) :-
not(on(_X,B)).
action :- preconditions,
retract(affected_old_properties),
assert(new_properties).
RECURSION:
r_put_on(A,B) :-
on(A,B).
r_put_on(A,B) :-
not(on(A,B)),
A \== table,
A \== B,
clear_off(A), /* N.B. "action" used as precondition */
clear_off(B),
on(A,X),
retract(on(A,X)),
assert(on(A,B)),
assert(move(A,X,B)).
clear_off(table). /* Means there is room on table */
clear_off(A) :- /* Means already clear */
not(on(_X,A)).
clear_off(A) :-
A \== table,
on(X,A),
clear_off(X), /* N.B. recursion */
retract(on(X,A)),
assert(on(X,table)),
assert(move(X,A,table)).
action :- preconditions or actions,
retract(affected_old_properties),
assert(new_properties).
on(a,b).
on(b,c).
on(c,table).
OUTPUT:

CASE 1: FORWARD APPROACH:

?- put_on(a,table).
yes

?- listing(on), listing(move).
on(b,c).
on(c,table).
on(a,table).
move(a,b,table).
yes

?- put_on(c,a).
no

?- put_on(b,table), put_on(c,a).
yes

CASE 2: RECURSION:

?- r_put_on(c,a).
yes

?- listing(on), listing(move).
on(a,table).
on(b,table).
on(c,a).
move(a,b,table).
move(b,c,table).
move(c,table,a).

Yes

RESULT:
Thus the blocks world problem has been executed and the output is verified
successfully.
EX NO: 6 WATER JUG PROBLEM

DATE:

AIM:

To implement water jug problem in PROLOG programming language.

DESCRIPTION:

Given 2jugs, a 41 one and 31 one, neither having any measuring works on it. There is a
pump that can used to fill the jugs with water. Thereby achieving a final state of 21 in jug1 and
jug2.

ALGORITHM:

Step 1: Initial state.

Step 2:.By applying the rules, successors are created.

Step 3: With this successor, search is done successively, back tracking also done.

Step 4: Step 2 and 3 are repeated with goal state is reached.

RULES:

1) Fill 41 jug if not full.

2) Fill 31 jug if not full.

3) Power from 31 jug to 41 jug until 41 jug is full.

4) Pour from 41 jug to 31 jug until 3 jug is full.

5) Pour all contents of a 31 jug to other.

6) Pour all contents of a 41 jug to other.

a. (x,y/x<4)-> (4,y)
b. (x,y/y<3)-> (x,3)
c. (x,y/y>0)-> (0,y)
d. (x,y/y>0)-> (x,0)
e. (x,y/x+y>4 & y>0) -> (4,x+y-4)
f. (x,y/x+y>3 & x>0) -> (x+y-3,3)
g. (x,y/x+y<4 & y>0) -> (x+y,0)
h. (x,y/x+y<3 & x>0) -> (0,x+y)
i. (x,y/x>0) -> (x-d,y)
j. (x,y/x>0) -> (x,y-d)
k. (0,2) -> (2,0)

CODE:

min(X,Y,X):-X<Y,!.

min(_,Y,Y).

rev(L,R):-revacc(L,[],R).

revacc([],R,R):-!.

revacc([H|T],A,R):-revacc(T,[H|A],R).

%Solve water jug program using DFS

%X,Y are initial contents, Nx,Ny are final contents of jug1 of capacity_and jug2 of capacity My
respectively after pouring from jug1 into jug2

chk(_,X,My,Y,Nx,Ny):- X>0,Y<My,Ey is My-Y,min(X,Ey,P),

Nx is X-P ,Ny is Y+P.

%Given 3 jugs of capacities Mx,My,Mz and filled with X,Y,Z unit of a liquid respectively,give steps so
that finally they contain Fx,Fy,Fz units of the liquid respectively.

jug(Mx,My,Mz,X,Y,Z,Fx,Fy,Fz):-jug(Mx,X,My,Y,Mz,Z,Fx,Fy,Fz,[],['initially']).

jug(_,Fx,_,Fy,_,Fz,Fx,Fy,Fz,T,R):-
!,rev([[Fx,Fy,Fz],[Fx,Fy,Fz]|T],TR),rev(['Finally'|R],RR),display(TR,RR).

jug(Mx,x,My,Y,Mz,Z,Fx,Fy,Fz,T,R):-

chk(Mx,X,My,Y,Nx,Ny),not(member([Nx,Ny,Z],T)),jug(Mx,Nx,My,Ny,Mz,Z,Fx,Fy,Fz,[[X,Y,Z]|T],['Po
ur liquid from jug1 into jug2'|R]).

jug(Mx,X,My,Y,Mz,Z,Fx,Fy,Fz,T,R):-

chk(Mx,Y,Mz,Z,Nx,Nz),not(member([Nx,Y,Nz],T)),jug(Mx,Nx,My,Y,Mz,Nz,Fx,Fy,Fz,[[X,Y,Z]|T],['Pou
r liquid from jug1 into jug3'|R]).

jug(Mx,X,My,Y,Mz,Z,Fx,Fy,Fz,T,R):-

chk(Mx,Y,Mz,Z,Ny,Nz),not(member([X,Ny,Nz],T)),jug(Mx,X,My,Ny,Mz,Nz,Fx,Fy,Fz,[[X,Y,Z]|T],['Pou
r liquid from jug2 into jug3'|R]).
jug(Mx,X,My,Y,Mz,Z,Fx,Fy,Fz,T,R):-

chk(My,Y,Mx,X,Ny,Nx),not(member([Nx,Ny,Z],T)),jug(Mx,Ny,My,Ny,Mz,Z,Fx,Fy,Fz,[[X,Y,Z]|T],['Po
ur liquid from jug2 into jug1'|R]).

jug(Mx,X,My,Y,Mz,Z,Fx,Fy,Fz,T,R):-

chk(Mz,Z,Mx,X,Nz,Nx),not(member([Nx,Y,Nz],T)),jug(Mx,Nx,My,Y,Mz,Nz,Fx,Fy,Fz,[[X,Y,Z]|T],['Pou
r liquid from jug3 into jug1'|R]).

jug(Mx,X,My,Y,Mz,Z,Fx,Fy,Fz,T,R):-

chk(Mz,Z,My,Y,Nz,Ny),not(member([X,Ny,Nz],T)),jug(Mx,X,My,Ny,Mz,Nz,Fx,Fy,Fz,[[X,Y,Z]|T],['Pou
r liquid from jug3 into jug2'|R]).

display([],[]):-!.

display([T1|T],[R1|R]):-write(R1),write(':'),write(T1),nl,display(T,R).
OUTPUT:

For help, use ?- (help TOPIC). Or ?- apropos(word).

1 ?- jug(8 , 5 , 7 , 4 , 2 , 0 , 4 , 4 , 0).

initially : [4 , 2 , 0]

Pour liquid from jug1 into jug3 : [0 , 2 , 2]

Pour liquid from jug1 into jug3 : [0 , 2 , 4]

Pour liquid from jug1 into jug3 : [0 , 2 , 6]

Pour liquid from jug1 into jug3 : [1 , 2 , 7]

Pour liquid from jug1 into jug3 : [0 , 0 , 7]

Pour liquid from jug1 into jug3 : [0 , 5 , 2]

Pour liquid from jug1 into jug3 : [0 , 5 , 7]

Pour liquid from jug1 into jug3 : [7 , 5 , 0]

Pour liquid from jug1 into jug3 : [0 , 3 , 7]

Pour liquid from jug1 into jug3 : [7 , 3 , 0]

Pour liquid from jug1 into jug3 : [0 , 3 , 3]

Pour liquid from jug1 into jug3 : [0 , 3 , 6]

Pour liquid from jug1 into jug3 : [2 , 3 , 7]

Pour liquid from jug1 into jug3 : [8 , 3 , 1]

Pour liquid from jug1 into jug3 : [0 , 3 , 4]

Pour liquid from jug1 into jug3 : [0 , 0 , 4]

Pour liquid from jug1 into jug3 : [0 , 4 , 0]

Pour liquid from jug1 into jug3 : [0 , 4 , 4]

Pour liquid from jug1 into jug3 : [4 , 4 , 0]

Finally : [4 , 4 , 0].

true
RESULT:

Thus the water jug problem using prolog was executed and the output was verified
successfully.
EX NO: 7 Heuristic Algorithms(A* Algorithm)

DATE:

AIM:

To implement the heuristic search technique (A* Algorithm) in SWI Prolog.

DESCRIPTION:

The A* algorithm is a specialization of best first search it provides the general guide
lines with which to estimate the goal distances for general search graphs. It chooses a successor
with which the shortest estimated distance for expansion. The form of heuristic function for A*
is: f*(n)=g*(n)+h*(n). it then chooses the successor with the shortest estimated distance for
expansion and the process continuous and a goalis forced or the search ends failure.

ALGORITHM:

STEP1: Place the starting nodes on open.

STEP2: If open is empty stop and return failure.

STEP3: Remove from open the node n that has the smallest value of f(n) if the node is a
good node, return success and stop.

STEP4: Otherwise expand n, generating all its successor’s n and place n on closed. For
every successor n if n is not already open or closed attack a back pointer to n, complete f
x(n) and place it in an open.

STEP5: Return to step 2 starmethod () has argument start and solution.

STEP6: The biggest solution is get from the user and the nodes are transversed to reach
the goal starts with shortest distance.

STEP7: if the search end fails, then stop and return failure.

CODE:

astar( Start,Solution):- biggest( Big),

% Big > any f-value

expand( [], l(Start,0/0),Big,_, yes,Solution).

expand( P, l( N,_),_,_,yes, [N | P]):-goal( N), reverse([N|P], P1),write(P1),!.

%, write( [N | P] ).
expand( P, l( N,F/ G),Bound, Tree1, Solved, Sol):- F=<Bound,(
bagof(M/C,(s(N,M,C),\+member(M,P )),Succ),!,

succlist( G, Succ,Ts),bestf( Ts,F1),expand( P,t(N,F1/G,Ts),Bound,Tree1,Solved,Sol);Solved =


never).

%No successors-dead end

expand(P, t(N,F/G,[T |Ts]),Bound,Tree1,Solved,Sol):-


F=<Bound,bestf(Ts,BF),min(Bound,BF,Bound1),

expand([N|P],T,Bound1,T1,Solved1,Sol),continue(p,t(N,F/G,[T1|Ts]),Bound,Tree1,Solved1,Sol
ved,Sol).

expand(_,t(_,_,[]),_,_,never,_):-!.

%A dead tree will never be solved

expand(_,Tree,Bound,Tree,no,_):-f(Tree,F),F>Bound.

%Cannot grow-bound exceeded

continue(_,_,_,_,yes,yes, Sol).

continue(P,t(N,F/G,[T1|Ts]),Bound,Tree1,Solved1,Sol):-
(Solved1=no,insert(T1,Ts,NTs);Solved1=never,NTs=Ts),bestf(NTs,F1),expand(P,t(N,F1/G,NTs)
,Bound,Tree1,Solved,Sol).

succlist(_,[],[]).

succlist(GO,[N/C|NCs],Ts):-G is GO+C,h(N,H),

%Heuristic term h(N)

F is G+H,succlist(GO,NCs,Ts1),insert(l(N,F/G),Ts1,Ts).

%Insert T into list of trees Ts preserving order w.r.t f-values

insert(T,Ts,[T|Ts]):-f(T,F),bestf(Ts,F1),F=<F1,!.

insert(T,[T1|Ts],[T1|Ts1]):-insert(T,Ts,Ts1).

%Extract f-value

f( l(_,F/_),F).

f( t(_,F/_,_),F).
bestf([T|_],F):-f( T, F).

bestf([],Big):-biggest(Big).

min(X,Y,X):-X=<Y,!.

min(X,Y,Y).

biggest(9898).

goal('Bucharest').

s('Oradea','Zerind',71).

s('Oradea','Sibiu',151).

s('Zerind','Arad',75).

s('Arad','Sibiu',140).

s('Arad','Timisora',118).

s('Timisoara','Lugoj',111).

s('Lugoj','Mehadia',70).

s('Mehadia','Dobreta',75).

s('Dobreta','Craiova',120).

s('Craiova','RV',146).

s('Craiova','Pitesti',138).

s('RV','Sibiu',80).

s('RV','Pitesti',97).

s('Sibiu','Fagarus',90).

s('Fagarus','Bucharest',211).

s('Pitesti','Bucharest',101).

s('Bucharest','Giurgiu',90).

s('Bucharest','Urzium',85).

s('Urzium','Vaslui',142).
s('Vaslui','Iasi',92).

s('Iasi','Neamt',87).

s('Urzium','Hirsova',98).

s('Hirsova','Eforie',86).

s('Neamt','Eforie',2).

s('Eforie','Neamt',2).

s('Eforie','Hirsova',86).

s('Hirsova','Urzium',98).

s('Neamt','Iasi',87).

s('Iasi','Vaslui',92).

s('Vaslui','Urzium',142).

s('Urzium','Bucharest',85).

s('Giurgiu','Bucharest',90).

s('Zerind','Oradea',71).

s('Sibui','Oradea',151).

s('Arad','Zerind',75).

s('Sibiu','Arad',140).

s('Timisoara','Arad',118).

s('Lugoj','Timisoara',111).

s('Mehadia','Lugoj',70).

s('Dobreta','Mehadia',75).

s('Craiova','Dobreta',120).

s('RV','Craiova',146).

s('Pitesti','Craiova',138).

s('Sibiu','RV',80).
s('Pitesti','RV',97).

s('Fagarus','Sibiu',90).

s('Bucharest','Fagarus',211).

s('Bucharest','Pitesti',101).

h('Arad',366).

h('Bucharest',0).

h('Craiova',160).

h('Dobreta',242).

h('Fagarus',176).

h('Lugoj',244).

h('Mehadia',241).

h('Oradea',380).

h('Pitesti',100).

h('RV',193).

h('Sibiu',253).

h('Timisoara',329).

h('Zerind',374).

h('Giurgiu',77).

h('Urzium',80).

h('Vaslui',199).

h('Iasi',226).

h('Neamt',234).

h('Hirsova',151).

h('Eforie',161).
OUTPUT:

1 ?- astar(‘Hirsova’,solution).

[‘Hirsova’Urzium,Bucharest]

Solution = [‘Bucharest’, ‘urzium’, ‘Hirsova’]

RESULT:

Thus the A* algorithm using prolog was executed and the output was verified
successfully.
EX.NO: 8 Representation of Knowledge using
DATE: Propositional Logic and Querying

AIM:
To represent the Knowledge using Propositional Logic concept in SWI Prolog.

PROCEDURE:

Step 1: Type the program in Notepad

Step 2: Save it as filename.pl

Step 3: Open SWI Prolog software, go to file menu and select consult to include the program.

Step 4: Run the .pl file

Step 5: Enter the logic and determine the output.

PROGRAM:

woman(mia).
woman(jody).
woman(yolanda).
playsAirGuitar(jody).
party.
happy(yolanda).
listens2Music(mia).
listens2Music(yolanda):- happy(yolanda).
playsAirGuitar(mia):- listens2Music(mia).
playsAirGuitar(yolanda):- listens2Music(yolanda).
happy(vincent).
listens2Music(butch).
playsAirGuitar(vincent):-
listens2Music(vincent),
happy(vincent).
playsAirGuitar(butch):-
happy(butch).
playsAirGuitar(butch):-
listens2Music(butch).
loves(vincent,mia).
loves(marsellus,mia).
loves(pumpkin,honey_bunny).
loves(honey_bunny,pumpkin).
OUTPUT:
?- woman(mia).
Yes
?- playsAirGuitar(jody).
Yes
?- playsAirGuitar(mia).
No

?- playsAirGuitar(vincent).
No
?- tatooed(jody).
No
?- party.
yes
?- rockConcert.
No
?- playsAirGuitar(mia).
No
playsAirGuitar(mia):- listens2Music(mia).
No
?- playsAirGuitar(yolanda).
Yes
listens2Music(yolanda):- happy(yolanda).
Yes
playsAirGuitar(yolanda):- listens2Music(yolanda).
Yes
?- playsAirGuitar(vincent).
No
?- playsAirGuitar(butch).
Yes
?- woman(X).
X = mia ;
X = jody ;
X = yolanda

RESULT:
Thus the knowledge representation using Propositional Logic and Querying has been
executed and the output is verified successfully.
EX.NO:9 Representation of Knowledge using Predicate
DATE: Logic and Querying

AIM:
To represent the knowledge using Predicate logic in SWI Prolog.

PROCEDURE:

Step 1: Type the program in Notepad

Step 2: Save it as filename.pl

Step 3: Open SWI Prolog software, go to file menu and select consult to include the program.

Step 4: Run the .pl file

Step 5: Enter the logic and determine the output.

PROGRAM:

eats(fox, chicken).
eats(chicken, corn).
safe(_, []).
safe(_, Others) :- member(human, Others).
safe(Item, [Other|Others]) :- eats(Other, Item),!,fail
;
safe(Item, Others).
allSafe([],_).
allSafe([Item|Items], Others) :- safe(Item, Others), allSafe(Items, Others).
allSafe(X) :- allSafe(X,X).
won([],Right) :-
member(fox, Right),
member(chicken,Right),
member(corn, Right),
member(human, Right).
move(X, [X|Left], Right, NewLeft, [X|Right], A) :- append(Left, A, NewLeft).
move(X, [L|Left], Right, NewLeft, NewRight, A) :- move(X, Left, Right, NewLeft, NewRight,
[L|A]).
move(X, Left, Right, NewLeft, NewRight) :- move(X, Left, Right, NewLeft, NewRight, []).
validMove(OldLeft, OldRight, NewLeft, NewRight) :-
move(human, OldLeft, OldRight, NewLeft, NewRight).
validMove(OldLeft, OldRight, NewLeft, NewRight) :-
move(human, OldLeft, OldRight, Left, Right),
move(_, Left, Right, NewLeft, NewRight).
validMove(OldLeft, OldRight, NewLeft, NewRight) :-
member(human, OldRight),
validMove(OldRight, OldLeft, NewRight, NewLeft).
safeMove(OldLeft, OldRight, NewLeft, NewRight) :-
validMove(OldLeft, OldRight, NewLeft, NewRight),
allSafe(NewLeft),
allSafe(NewRight).
triedAlready(Left, Right, [[L1,R1]|Others]) :-
permutation(Left, L1),
permutation(Right, R1)
;
triedAlready(Left, Right, Others).
tryMoving(Left, Right, Game, Game) :- won(Left, Right).
tryMoving(Left, Right, Game, FinalGame) :-
safeMove(Left, Right, NewLeft, NewRight),
not(triedAlready(NewLeft, NewRight, Game)),
tryMoving(NewLeft, NewRight, [[NewLeft, NewRight]|Game], FinalGame).
playGame(FinalGame) :-
tryMoving([fox, chicken,corn, human],[],[[[chicken, human, fox, corn], []]], Game),
reverse(Game, FinalGame).
printGame([]).
printGame([[Left, Right]|Game]) :-
write(Left),nl,
write(' '),
write(Right),nl,
printGame(Game).
go :- playGame(X),printGame(X).
OUTPUT:

?- go.
[chicken, human, fox, corn]
[]
[fox, corn]
[chicken, human]
[human, fox, corn]
[chicken]
[corn]
[fox, human, chicken]
[chicken, human, corn]
[fox]
[chicken]
[corn, human, fox]
[human, chicken]
[fox, corn]
[]
[chicken, human, fox, corn]

Yes

RESULT:
Thus the knowledge representation using Predicate Logic and Querying has been
executed and the output is verified successfully.
EX.NO:10 Backward Chaining

DATE:

AIM:

To implement the backward chaining using SWI Prolog.

PROCEDURE:

Step 1: Type the program in Notepad

Step 2: Save it as filename.pl

Step 3: Open SWI Prolog software, go to file menu and select consult to include the program.

Step 4: Run the .pl file

Step 5: Enter the logic and determine the output.

PROGRAM:

% the murderer had brown hair:


murderer(X) :- hair(X, brown).
% mr_holman had a ring:
attire(mr_holman, ring).
% mr_pope had a watch:
attire(mr_pope, watch).
% If sir_raymond had tattered cuffs then mr_woodley had the pincenez:
attire(mr_woodley, pincenez) :-
attire(sir_raymond, tattered_cuffs).
% and vice versa:
attire(sir_raymond,pincenez) :-
attire(mr_woodley, tattered_cuffs).
% A person has tattered cuffs if he is in room 16:
attire(X, tattered_cuffs) :- room(X, 16).
% A person has black hair if he is in room 14, etc:
hair(X, black) :- room(X, 14).
hair(X, grey) :- room(X, 12).
hair(X, brown) :- attire(X, pincenez).
hair(X, red) :- attire(X, tattered_cuffs).
% mr_holman was in room 12, etc:
room(mr_holman, 12).
room(sir_raymond, 10).
room(mr_woodley, 16). room(X, 14) :- attire(X, watch).
OUTPUT:

?- murderer(X).
murderer(X)
hair(X, brown) /* murderer(X) :- hair(X, brown). */
attire(X, pincenez) /* hair(X, brown) :- attire(X, pincenez). */
X = mr_woodley
attire(sir_raymond, tattered_cuffs)
room(sir_raymond, 16)
FAIL (no facts or rules)
FAIL (no alternative rules)
REDO (found one alternative rule)
attire(X, pincenez)
X = sir_raymond
attire(mr_woodley, tattered_cuffs)
room(mr_woodley, 16)
SUCCESS
SUCCESS: X = sir_raymond
SUCCESS: X = sir_raymond
SUCCESS: X = sir_raymond
SUCCESS: X = sir_raymond

RESULT:
Thus the backward chaining program using SWI Prolog has been executed and the output
is verified successfully.
EX.NO:11 Unification
DATE:

AIM:
To implement the unification program using SWI Prolog.

PROCEDURE:

Step 1: Type the program in Notepad

Step 2: Save it as filename.pl

Step 3: Open SWI Prolog software, go to file menu and select consult to include the program.

Step 4: Run the .pl file

Step 5: Enter the logic and determine the output.

PROGRAM:

unify('test', 'this is a test').


run :- write('Enter something: '),
read(X),
unify(X, Y),
write('The answer is '), write(Y).
OUTPUT:

?- ['unify.pl'].
% unify.pl compiled 0.00 sec, -48 bytes
true.

?- run.
Enter something: test.
The answer is this is a test
true.

?- run.
Enter something: 'test'.
The answer is this is a test
true.

RESULT:
Thus the unification program using SWI Prolog has been implemented and the output is
verified successfully.
EX.NO: 12 Minimax Algorithm
DATE:

AIM:
To implement the minimax algorithm using SWI Prolog.

PROCEDURE:

Step 1: Type the program in Notepad

Step 2: Save it as filename.pl

Step 3: Open SWI Prolog software, go to file menu and select consult to include the program.

Step 4: Run the .pl file

Step 5: Enter the logic and determine the output.

PROGRAM:

%sum of elements in a list


sum([X|[Y|[Z|[]]]],S):-S is X+Y+Z.

%sum of elements in diagonal from left to right


ltrtdiagsum([X|_],[_|[Y|_]],[_|[_|[Z|[]]]],S):- S is X+Y+Z.

%right to left diagonal sum


rtltdiagsum([_|[_|[X|[]]]],[_|[Y|_]],[Z|_],S):- S is X+Y+Z.

%column sum
sumcol1([X|_],[Y|_],[Z|_],S):- S is X+Y+Z.
sumcol2([_|[X|_]],[_|[Y|_]],[_|[Z|_]],S):- S is X+Y+Z.
sumcol3([_|[_|[X|[]]]],[_|[_|[Y|[]]]],[_|[_|[Z|[]]]],S):- S is X+Y+Z.

%overall sum generator


compsums(X,Y,Z,S):-
sum(X,N1),sum(Y,N2),sum(Z,N3),
sumcol1(X,Y,Z,N4),
sumcol1(X,Y,Z,N5),
sumcol1(X,Y,Z,N6),
ltrtdiagsum(X,Y,Z,N7),
rtltdiagsum(X,Y,Z,N8),
S=[N1|[N2|[N3|[N4|[N5|[N6|[N7|N8]]]]]]].
%get best move => get best row then make change in that row.
%bestrow helper finds the row u want to fill in based on the fourth attribute which is index into the sum
list

bestrow1(_,_,_,1,1).
bestrow1(_,_,_,2,2).
bestrow1(_,_,_,3,3).
bestrow1([0|_],_,_,4,1).
bestrow1(_,[0|_],_,4,2).
bestrow1(_,_,[0|_],4,3).
bestrow1([_|[0|_]],_,_,5,1).
bestrow1(_,[_|[0|_]],_,5,2).
bestrow1(_,_,[_|[0|_]],5,3).
bestrow1([_|[_|[0|_]]],_,_,6,1).
bestrow1(_,[_|[_|[0|_]]],_,6,2).
bestrow1(_,_,[_|[_|[0|_]]],6,3).
bestrow1([0|_],_,_,7,1).
bestrow1(_,[_|[0|_]],_,7,2).
bestrow1(_,_,[_|[_|[0|_]]],7,3).
bestrow1([_|[_|[0|_]]],_,_,8,1).
bestrow1(_,[_|[0|_]],_,8,2).
bestrow1(_,_,[0|_],8,3).

% os = overall sum
% can we win

bestrow(X,Y,Z,OS,PRIND,MOVE):- nth(POS,OS,10),bestrow1(X,Y,Z,POS,N),PRIND=POS, MOVE =


N.
% can we win
bestrow(X,Y,Z,OS,PRIND,MOVE):- nth(POS,OS,12),bestrow1(X,Y,Z,POS,N),PRIND=POS, MOVE =
N.

% can we win
bestrow(X,Y,Z,OS,PRIND,MOVE):- nth(POS,OS,11),bestrow1(X,Y,Z,POS,N),PRIND=POS,MOVE =
N.

% can we win
bestrow(X,Y,Z,OS,PRIND,MOVE):- nth(POS,OS,5),bestrow1(X,Y,Z,POS,N),PRIND=POS,MOVE =
N.
% can we win
bestrow(X,Y,Z,OS,PRIND,MOVE):- nth(POS,OS,6),bestrow1(X,Y,Z,POS,N),PRIND=POS,MOVE =
N.

%make move in the best row


%make moev helpers

makemoverownp([0|X],[5|X]).
makemoverownp([X|[0|Y]],[X|[5|Y]]).
makemoverownp([X|[Y|[0|[]]]],[X|[Y|[5|[]]]]).
makemoverow1p([0|X],[5|X]).
makemoverow2p([X|[0|Y]],[X|[5|Y]]).
makemoverow3p([X|[Y|[0|[]]]],[X|[Y|[5|[]]]]).

makemove1(X,Y,Z,X1,Y,Z,PRIND,1):-
(PRIND=:=1;PRIND=:=2;PRIND=:=3),makemoverownp(X,X1).
makemove1(X,Y,Z,X,Y1,Z,PRIND,2):-
(PRIND=:=1;PRIND=:=2;PRIND=:=3),makemoverownp(Y,Y1).
makemove1(X,Y,Z,X,Y,Z1,PRIND,3):-
(PRIND=:=1;PRIND=:=2;PRIND=:=3),makemoverownp(Z,Z1).

makemove1(X,Y,Z,X1,Y,Z,4,1):-makemoverow1p(X,X1).
makemove1(X,Y,Z,X,Y1,Z,4,2):-makemoverow1p(Y,Y1).
makemove1(X,Y,Z,X,Y,Z1,4,3):-makemoverow1p(Z,Z1).

makemove1(X,Y,Z,X1,Y,Z,5,1):-makemoverow1p(X,X1).
makemove1(X,Y,Z,X,Y1,Z,5,2):-makemoverow1p(Y,Y1).
makemove1(X,Y,Z,X,Y,Z1,5,3):-makemoverow1p(Z,Z1).

makemove1(X,Y,Z,X1,Y,Z,6,1):-makemoverow1p(X,X1).
makemove1(X,Y,Z,X,Y1,Z,6,2):-makemoverow1p(Y,Y1).
makemove1(X,Y,Z,X,Y,Z1,6,3):-makemoverow1p(Z,Z1).

makemove1(X,Y,Z,X1,Y,Z,7,1):-makemoverow1p(X,X1).
makemove1(X,Y,Z,X,Y1,Z,7,2):-makemoverow1p(Y,Y1).
makemove1(X,Y,Z,X,Y,Z1,7,3):-makemoverow1p(Z,Z1).

makemove1(X,Y,Z,X1,Y,Z,8,1):-makemoverow1p(X,X1).
makemove1(X,Y,Z,X,Y1,Z,8,2):-makemoverow1p(Y,Y1).
makemove1(X,Y,Z,X,Y,Z1,8,3):-makemoverow1p(Z,Z1).

makemove(X,Y,Z,X1,Y1,Z1):-
compsums(X,Y,Z,OS),bestrow(X,Y,Z,OS,PRIND,MOVE),makemove1(X,Y,Z,X1,Y1,Z1,PRIND,MOV
E),!.

%main game loop comes now


%game loop helpers
disp(X,Y,Z):-write(X),write('\n'),write(Y),write('\n'),write(Z),write('\n').

userrowmove([0|X],[6|X],1).
userrowmove([X|[0|Y]],[X|[6|Y]],2).
userrowmove([X|[Y|[0|[]]]],[X|[Y|[6|[]]]],3).

usermove(X,Y,Z,1,C,X1,Y,Z):-userrowmove(X,X1,C).
usermove(X,Y,Z,2,C,X,Y1,Z):-userrowmove(Y,Y1,C).
usermove(X,Y,Z,3,C,X,Y,Z1):-userrowmove(Z,Z1,C).

%we win sequence


tictactoe(X,Y,Z):-compsums(X,Y,Z,OS),nth(_,OS,15),disp(X,Y,Z),write('COMPUTER WINS'),!.

%we win sequence


tictactoe(X,Y,Z):-compsums(X,Y,Z,OS),nth(_,OS,18),disp(X,Y,Z),write('U WIN'),!.

%check if we have position to move to get move details from user


tictactoe(X,Y,Z):-(nth(N,X,0);nth(N,Z,0)),write('enter position to mark :\n'),read(R),read(C),write('Your
move\n'),usermove(X,Y,Z,R,C,X1,Y1,Z1),disp(X1,Y1,Z1),makemove(X1,Y1,Z1,X2,Y2,Z2),write('CO
MPUTERS MOVE\n'), disp(X2,Y2,Z2),tictactoe(X2,Y2,Z2).

%game is a draw
tictactoe(_,_,_):-write('Game is a draw'),!.
OUTPUT:

| ?- tictactoe([0,0,0],[0,0,0],[0,0,0]).
enter position to mark :
1.
2.
Your move
[0,6,0]
[0,0,0]
[0,0,0]
COMPUTERS MOVE
[5,6,0]
[0,0,0]
[0,0,0]
enter position to mark :
2.
2.
Your move
[5,6,0]
[0,6,0]
[0,0,0]
COMPUTERS MOVE
[5,6,5]
[0,6,0]
[0,0,0]
enter position to mark :
2.
3.
Your move
[5,6,5]
[0,6,6]
[0,0,0]
COMPUTERS MOVE
[5,6,5]
[5,6,6]
[0,0,0]
enter position to mark :
3.
3.
Your move
[5,6,5]
[5,6,6]
[0,0,6]
COMPUTERS MOVE
[5,6,5]
[5,6,6]
[5,0,6]

[5,6,5]
[5,6,6]
[5,0,6]
COMPUTER WINS

RESULT:
Thus the tic-tac-toe game using minimax algorithm is implemented and the output is
verified successfully in SWI Prolog.
EX NO: 13 SPELL CHECKING

DATE:

AIM:

TO Perform Syntax Checking of sentences using the given grammar in PROLOG.

ALGORITHM:

Step 1: Start.

Step 2: Each terminal that is of the production is represented as a fact.

Step 3: Each production as a non-terminal is represented as rule.

Step 4: The root products are given the sentences as a link & it repeat the status.

Step 5: Stop.

RULES/GRAMMAR:

S->NP VP

NP->the NP

NP-> PRO

NP->PNI

NP-> ADJS N

ADJS-> ∑/ADJ AADJS

VP-> V

VP-> V NP

N-> File/Printer

ADJ-> short / long / fast

V-> printed / created / want


CODE:

% Adjectives

adj([short]).

adj(short).

adj([long]).

adj(long).

adj([fast]).

adj(fast).

% Verbs

v([printed]).

v(printed).

v([created]).

v(created).

v([want]).

v(want).

% Pronoun

pro([i]).

pro(i).

% Proper noun

pn([bill]).

pn(bill).

% Noun

n([file]).

n(file).

n([printer]).

n(printer).
% ADJS ->ADJADJS|E

adjs([X|Y]):- adj(X), adjs(Y).

adjs([]).

% NP1 ->ADJSN

np1(X):- prefix(Y,X) , adjs(Y) , append(Y , Z , X) , n(Z).

% NP

np([the | X]):- np1(X).

np(X):- pro(X).

np(X):- pn(X).

np(X):- np1(X).

% VP

vp(X):-v(X).

vp([X|Y]):- n(X) , np(Y).

%S

s(X) :- prefix(Y,X) , append(Y , Z , X) , np(Y) , vp(Z).

% check pred

check(X):- s(X),write('The sentence is correct'),!,true.

check(_):- write('The sentence is wrong').


OUTPUT:

1 ?- check([the,printer,printed]).

The sentence is correct

true.

2 ?- check([printer,the]).

The sentence is correct

true

3 ?- check([the,long,file,created]).

The sentence is correct

true.

4 ?-

RESULT:

Thus the program for performing syntax checking in prolog was executed and output was
verified successfully.
EX NO: 14 EXPERT SYSTEM FOR MEDICAL DIAGNOSIS

DATE:

AIM:
To design a rule based expert system in PROLOG for medical diagnosis.

DESCRIPTION:

The problem is that a given set of disease and the possible diagnosis are entered in the
database well in advance. Whenever the user enters the name, the symptoms are accepted. From
user add the correct diseases corresponding to the symptoms are given to the user.

ALGORITHM:

Step 1: Start

Step 2: Get the symptoms from the user as input.

Step 3: Match the input with the rules.

Step 4: Apply the rules in the facts.

Step 5: Repeat until some useful condition is derived.

Step 6: Stop.
CODE:

disease(y,y,y,y):-write('You are suffering from VIRAL FEVER').

disease(y,y,y,n):-write('You are suffering from COMMAN FEVER').

disease(y,y,n,y):-write('You are suffering from VIRAL FEVER').

disease(y,y,n,n):-write('You are suffering from OVERHOOKED ').

disease(y,n,y,y):-write('You are suffering from COLD ').

disease(y,n,y,n):-write('You are suffering from THROAT INFECTION').

disease(y,n,n,y):-write('You are suffering fromSEDENTARY').

disease(y,n,n,n):-write('You are suffering from HEADACHE').

disease(n,y,y,y):-write('You are suffering from FEVERE').

disease(n,y,y,n):-write('You are suffering from COMMAN COLD').

disease(n,y,n,y):-write('You are suffering from DEHYDRATION').

disease(n,y,n,n):-write('You are suffering fromDENGUE FEVER ').

disease(n,n,y,y):-write('You are suffering from CHICKEN GUNIYA').

disease(n,n,y,n):-write('You are suffering from SORE THROATE').

disease(n,n,n,y):-write('You can relax ,u are enervated').

disease(n,n,n,n):-write('You are alright').

run:-write('Do you have headache?'),

read(HA),

write('Do you have temperature?'),

read(TP),

write('Do you have sore throat?'),

read(ST),

write('Do you tired?'),

read(TR),

disease(HA,TP,ST,TR).
OUTPUT:

| ?- run.
Do you have HEADACHE? n.
Do you have TEMPARATURE? y.
Do you have a SORE THROAT? n.
Are you feeling TIRED? n.
You are suffering from DENGU FEVER
true ?;
no

RESULT:
Thus the program for implementing the expert systems for medical diagnosis using
prolog has been executed and the output was verified successfully.

You might also like