Aim: To Study Artificial Intelligence Language Prolog Using "SWI Prolog"
Aim: To Study Artificial Intelligence Language Prolog Using "SWI Prolog"
Code:
likes(ram,mango).
likes(bill,candy).
girl(seema).
red(rose).
owns(john,gold).
Output:
Devanshi Parejiya(17012021005)
Artificial Intelligence Practical-3
Output:
2. Write predicates one converts Celsius temperatures to Fahrenheit, the other checks if the
temperature is below freezing.
Code:
c_to_f(C,F):-
F is C*9/5+32.
freezing(F):- F=<32,
write("It's freezing").
freezing(F):- write("It's not
freezing").
Output:
3. Write Prolog clauses to check whether a given line segment is horizontal, vertical or oblique.
Code:
vertical(seg(point(X,Y),point(X,Y1))).
horizontal(seg(point(_,Y),point(_,Y))).
oblique(seg(point(X1,Y1),point(X2,Y2))):-
X1\==X2, Y1\==Y2.
Devanshi Parejiya(17012021005)
Artificial Intelligence Practical-3
Output:
Fibonacci_Code:
fib(X,Y1,Y2):-
X>0, X1 is X -
1,
fib(X1,Y2,Y3),
Y1 is Y2 + Y3, write(Y1),nl. fib(0,1,0).
Factorial_Code:
fact(N,F):- N>0,
N1 is n-1,
fact(N1,F1), F is
N* F1. fact(0,1).
Factorial_Output:
Devanshi Parejiya(17012021005)
Artificial Intelligence Practical-3
Output:
Fibonacci _Output:
Devanshi Parejiya(17012021005)
Artificial Intelligence Practical-3
5. Write a Prolog program to formulate rules to capture the following relationships for following
family tree.
Code:
mother(X,Y):-parent(X,Y),female(X). father(X,Y):parent(X,Y),male(X).
haschild(X):-parent(X,_).
sister(X,Y):parent(Z,X),paarent(Z,Y),female(X),X\==Y.
brother(X,Y):parent(Z,X),paarent(Z,Y),male(X),X\==Y.
grandparent(X,Y):-parent(X,Z),parent(Z,Y).
grandmother(X,Z):-mother(X,Y),parent(Y,Z).
grandfather(X,Z):father(X,Y),parent(Y,Z).
wife(X,Y):parent(X,Z),parent(Y,Z),female(X),male(Y).
uncle(X,Z):-brother(X,Y),parent(Y,Z).
predecessor(X,Z):-parent(X,Z).
predecessor(X,Z):-parent(X,Y),predecessor(Y,Z).
female(kdt). female(kgt).
female(rgc). female(rjt).
male(adt). male(tgt).
male(ygc). male(jgt).
parent(kdt,tgt). parent(ygc,tgt).
parent(ygc,kgt).
Devanshi Parejiya(17012021005)
Artificial Intelligence Practical-3
Output:
parent(tgt,rjt). parent(tgt,rgc).
parent(rgc,adt). parent(tgt,jgt).
parent(jgt,peter).
6. Write a program to solve the following problem. Imagine a room containing a monkey, chair
and some bananas. That has been hung from the centre of the ceiling. If the monkey is clever
enough he can reach the bananas by placing the chair directly below the bananas and climb on
the chair. The problem is to prove the monkey can reach the bananas. The monkey wants it, but
cannot jump high enough from the floor. At the window of the room there is a box that the
monkey can use.
Code:
in_room(bananas).
in_room(chair).
Devanshi Parejiya(17012021005)
Artificial Intelligence Practical-3
in_room(monkey).
dexterous(monkey). tall(chair).
can_move(monkey,chair,bananas).
can_climb(monkey,chair).
can_reach(X,Y):-
dexterous(X),vclose(X,Y).
vclose(X,Z):- get_on(X,Y),
under(Y,Z), tall(Y).
get_on(X,Y):-
can_climb(X,Y).
under(Y,Z):- in_room(X), in_room(Y),
in_room(Z), can_move(X,Y,Z).
Devanshi Parejiya(17012021005)
Artificial Intelligence Practical-3
Output:
Devanshi Parejiya(17012021005)
Artificial Intelligence Practical-3
7. Write a program for search, concatenate, insert and remove function of lists.
Code:
/*search an element */
add(X,L,[X|L]).
/*concatination*/
concate([H1|T1],L2,[H1|T2]):- concate(T1,L2,T2).
concate([],L2,L2).
de(H,[H],[]). de(X,[X|T1],T1).
de(X,[H|T],[H|T1]):-
de(X,T,T1). de(X,[_],_):-
Devanshi Parejiya(17012021005)
Artificial Intelligence Practical-3
Output:
writef("Element %t not
found",[X]).
Code:
go :-
write('What is the patient''s name? '), read(Patient),
hypothesis(Patient,Disease),
write_list([Patient,'probably has ',Disease,'.']),nl.
symptom(Patient,fever) :-
write_list(['Does ',Patient,' have a fever (y/n) ?']),
response(Reply), Reply='y'.
symptom(Patient,rash) :-
write_list(['Does ',Patient,' have a rash (y/n) ?']),
response(Reply), Reply='y'.
symptom(Patient,headache) :-
write_list(['Does ',Patient,' have a headache (y/n) ?']),
response(Reply), Reply='y'.
symptom(Patient,runny_nose) :-
write_list(['Does ',Patient,' have a runny_nose (y/n) ?']),
Devanshi Parejiya(17012021005)
Artificial Intelligence Practical-3
response(Reply), Reply='y'.
symptom(Patient,conjunctivitis) :-
write_list(['Does ',Patient,' have a conjunctivitis (y/n) ?']),
response(Reply), Reply='y'.
symptom(Patient,cough) :-
write_list(['Does ',Patient,' have a cough (y/n) ?']),
response(Reply), Reply='y'.
symptom(Patient,body_ache) :-
write_list(['Does ',Patient,' have a body_ache (y/n) ?']),
response(Reply), Reply='y'.
symptom(Patient,chills) :-
write_list(['Does ',Patient,' have a chills (y/n) ?']),
response(Reply), Reply='y'.
symptom(Patient,sore_throat) :-
write_list(['Does ',Patient,' have a sore_throat (y/n) ?']),
response(Reply), Reply='y'.
symptom(Patient,sneezing) :-
write_list(['Does ',Patient,' have a sneezing (y/n) ?']),
response(Reply), Reply='y'.
symptom(Patient,swollen_glands) :- write_list(['Does
',Patient,' have a swollen_glands (y/n) ?']),
response(Reply), Reply='y'.
hypothesis(Patient,measles) :-
symptom(Patient,fever), symptom(Patient,cough),
symptom(Patient,conjunctivitis),
Devanshi Parejiya(17012021005)
Artificial Intelligence Practical-3
symptom(Patient,runny_nose),
symptom(Patient,rash).
hypothesis(Patient,german_measles) :-
symptom(Patient,fever),
symptom(Patient,headache),
symptom(Patient,runny_nose),
symptom(Patient,rash).
hypothesis(Patient,flu) :-
symptom(Patient,fever),
symptom(Patient,headache),
symptom(Patient,body_ache),
symptom(Patient,conjunctivitis),
symptom(Patient,chills),
symptom(Patient,sore_throat),
symptom(Patient,runny_nose),
symptom(Patient,cough).
hypothesis(Patient,common_cold) :-
symptom(Patient,headache),
symptom(Patient,sneezing),
symptom(Patient,sore_throat),
symptom(Patient,runny_nose),
symptom(Patient,chills).
hypothesis(Patient,mumps) :-
symptom(Patient,fever),
symptom(Patient,swollen_glands).
hypothesis(Patient,chicken_pox) :-
symptom(Patient,fever),
symptom(Patient,chills),
symptom(Patient,body_ache),
symptom(Patient,rash).
hypothesis(Patient,measles) :-
symptom(Patient,cough),
symptom(Patient,sneezing),
symptom(Patient,runny_nose).
Devanshi Parejiya(17012021005)
Artificial Intelligence Practical-3
response(Reply) :-
get_single_char(Code),
put_code(Code), nl, char_code(Reply,
Code).
Output:
Devanshi Parejiya(17012021005)