Escolar Documentos
Profissional Documentos
Cultura Documentos
Lab File
Artificial Intelligence
(EIT-752)
B.Tech (IT)- VIIth Semester (IV Year )
AI Lab Manual
INDEX
S.No.
Date
Topic
Signature
AI Lab Manual
10
PROLOG
SWI-Prolog is free Prolog compiler licensed under the GPL, targeting primarily
the research and education. SWI-Prolog is designed and implemented by Jan
Wielemaker from the University of Amsterdam, department of Social Science
Informatics (SWI).
SWI-Prolog is written in ANSI C and should configure for most Unix machines
having an ANSI C Compiler. Ports have been made for:
1. MS Win32
2. MS Win3.1
3. MS DOS
4. OS/2
The Program
The program, sometimes called Database is a text file (*.pl) that contain the facts
and rules that will be used by the user of the program. It contains all the relations
that make this program.
The Query
When you launch a program you are in query mode. This mode is represented by
the sign?- at the beginning of the line. In query mode you ask questions about
relations described in the program.
Loading a program
When Prolog is launched the ?- should appear meaning you are in query mode. The
manner to launch a program depends of your compiler. For SWI-Prolog you can load
a program by typing the command [file]. when the file of your program is file.pl.
If you compiler is not SWI-Prolog you can also try the command reconsult(file).
[swi('demo/likes')].
AI Lab Manual
PROGRAM 1: Write a program for demonstrating the Medical Diagnosis System using
simple facts.
EXPLANATION:
symptom(chicken_pox,high_fever).
symptom(chicken_pox,chills).
symptom(dengu,high_fever).
symptom(cough,mild_body_ache).
symptom(cough,runny_nose).
symptom(jaundice,vomiting).
symptom(jaundice,high_fever).
QUERY:
goal:- symptom(dengu,high_fever).
goal:- symptom(dengu,chills).
AI Lab Manual
EXPLANATION:
symptom(chicken_pox,high_fever).
symptom(chicken_pox,chills).
symptom(dengu,high_fever).
symptom(cough,mild_body_ache).
symptom(cough,runny_nose).
symptom(jaundice,vomiting).
symptom(jaundice,high_fever).
QUERY:
symptom(dengu,What).
symptom(Disease,mild_body_ache).
AI Lab Manual
EXPLANATION:
symptom(banana,yellow).
symptom(apple,red).
symptom(guvava,green).
symptom(grapes,green).
symptom(cheery,red).
symptom(mango,green).
symptom(mango,yellow).
QUERY:
symptom(Fruit,red),symptom(Fruit,yellow).
symptom(apple,What), symptom(mango, What).
symptom(apple,What), symptom(mango, X).
AI Lab Manual
AI Lab Manual
AI Lab Manual
PROGRAM 6: Write a program for demonstrating Input & Output files in Prolog.
EXPLANATION:
hello:write('wat is ur name?'),
read(X),
write('hello'), tab(1),write(X).
AI Lab Manual
AI Lab Manual
AI Lab Manual
PROGRAM 8: Write a program to print the list of customers having different colors cars
with the price and models available.
EXPLANATION:
% List of customers with different colors%
cust_names([ruchin,sahil,shadab,anurag,siva],zinc).
cust_names([sadhana,soni,sonali],yellow).
cust_names([kannu,bharti,nirja,pooja],white).
% List of model with different colors%
model(white,[audi,jaguar,bmw]).
model(yellow,[polo,i20]).
model(zinc,[honda_city,fortuner]).
% Prices of cars with different colors%
price(white,700000).
price(yellow,380000).
price(zinc,660000).
cost(yellow):-cust_names(A,yellow),
write( '\n The name of customers who own a yellow color car is: '),tab(1),write(A),
price(yellow,X),
write( '\n The price of yellow color car is: '),tab(1),write(X),
model(yellow,Y),
write('\n The models available in yellow color '),tab(1),write(Y).
cost(white):-cust_names(A,white),
write( '\n The name of customers who own a white color car is: '),tab(1),write(A),
AI Lab Manual
price(white,X),
write( '\n The price of white color car is: '),tab(1),write(X),
model(white,Y),
write('\n The models available in white color '),tab(1),write(Y).
goal:write('\n Details of Customers who own a white car: \n '),
cost(white).
QUERY:
goal.
AI Lab Manual
PROGRAM 9: Write a program for demonstrating the use Cut, Fail & Not as
backtracking techniques in PROLOG.
EXPLANATION:
FAIL: The fail predicate is provided by Prolog, it causes the failure of the rule which
will be for ever valid, and nothing can change the statement of this predicate. A
typical use of fail is a negation of a predicate.
CUT: It is represented by an exclamation mark(!).The cut effectively tells Prolog to
freeze all the decisions made so far in this predicate. That is, if required to
backtrack, it will automatically fail without trying other alternatives. When the is
encountered, it re-routes backtracking.
NOT:
c(2).
/* Example of Not*/
bachelor(P) :- male(P), not(married(P)).
AI Lab Manual
male(rajat).
male(jerry).
married(jerry).
married(laxman).
QUERY:
bachelor(jerry).
married(jerry).
bachelor(rajat).
married(rajat).
AI Lab Manual
PROGRAM 10: Write a program to dynamically add rule or fact in database dynamically
using assert and retract.
EXPLANATION:
assert(c).: Add the rule c in the database.assert()
retract(c): Remove the c from the database.retract(c)
asserta(c): Add c at the beginning of the database.asserta()
assertz(c): Add c at the end of the database.assertz()