Artificial Intelligence
Artificial Intelligence
Prolog Code
mortal(X):-person(X)
person(Socrates)
person(plato)
person(aristotle)
Experiment 2
Prolog Code
parent(suresh,shyam)
parent(Shyam,Ram).
male(suresh).
parent(suresh,ram).
female(ramya).
grandfather(X,Y):-parent(X,Z),parent(Z,Y),male(X).
Experiment 3
Code:
parent(a,b).
parent(a,c).
parent(a,d).
parent(b,e).
parent(b,f).
parent(c,h).
parent(c,i).
parent(d,i).
parent(d,k).
parent(h,l).
sibling(X,Y):-parent(Z,X),parent(Z,Y),X\=Y.
grandfather(X,Y):-parent(X,Z),parent(Z,Y).
cousin(X,Y):-grandfather(Z,X),grandfather(Z,Y),not(sibling(X,Y)),X\=Y.
Experiment 4
Hoofers Club
Tony, Shi-Kuo and Ellen belong to the Hoofers Club. Every member of the Hoofers
Club is either a skier or a mountain climber or both. No mountain climber likes rain,
and all skiers like snow. Ellen dislikes whatever Tony likes and likes whatever Tony
dislikes. Tony likes rain and snow.
Prolog Code
hoofers(tony).
hoofers(ski-kuo).
hoofers(ellen).
likes(tony,rain).
likes(tony,snow).
likes(ellen,X) :- not(likes(tony,X)).
dislikes(ellen,X):-likes(tony,X).
skier(X):-likes(X,snow).
climber(X):-dislikes(X,rain).
Experiment 5
List Operations
Split Operation
?- listsplit([a,b,c,d,e], A, B).
Output
A = a
B = [b, c, d, e] ;
member(Element, List).
Where List is any prolog list, and Element is any element in that list. The following, for instance,
succeeds (returns 'Yes'):
This query:
Element = a;
Element = b;
Element = c;
The built-in predicate append/ attaches a list to the back of another, in other words, it
concatenates two lists. It's used like this:
Result = [a, b, c, d, e, f]
Reversing a list
We will look at two ways to reverse a list, first of all the naive way is to simply keep taking the
head off and appending it onto the end,
reverse([],[]).
reverse([X|Xs],YsX) :- reverse(Xs,Ys), append(Ys,[X],YsX).
Executing it means that you traverse the list over and over again appending each time, A more
efficient version can be created by taking the definition of append:
then,
reverse(Xs,Ys) :- revappend(Xs,[],Ys).
Experiment 6
Arithmetic in Prolog
Prolog is not the programming language of choice for carrying out heavy-duty mathematics. It
does, however, provide arithmetical capabilities. The pattern for evaluating arithmetic
expressions is (where Expression is some arithmetical expression)
X is Expression
?- X is 10 - 5.
X=5
yes
?- X is 10 * 5.
X = 50
yes
?- X is 10 / 5.
X=2
yes
?- X is 10 + 5 * 6 / 3.
X = 20
yes
Program No. 7
Problem Statement:
Conversion from degree Celsius to degree Fahrenheit
Prolog Code:
run :-
write('Enter a temp in degrees Celsius '),
read(C),
convert(C,F),
write('The temp is '), write(F), write(' Degrees Fahrenheit'),
nl,
warning(F, Warning),
write(Warning).
convert(Cel, Fahr) :-
Fahr is 9.0 / 5.0 * Cel + 32.
warning(T, 'It_s really hot out') :- T > 90.
warning(T, 'Brass monkey danger!') :- T < 30.
warning(T, _) :- T >= 30, T =< 90. % Blank warning for normal temps
Output:
Run
Enter the temperature in degree Celsius:32.
The temperature is 89.59999999994 degrees Fahrenheit.
Program No. 8
Problem Statement:
Factorial of a number
Prolog code:
factorial(N, Fact) :-
N > 0,
N1 is N - 1,
factorial(N1, Fact1),
Fact is N * Fact1.
factorial(0, 1).
Output:
Factorial(3,6)
Yes
Factorial(3,5)
No
Program No. 9
Problem Statement: The law says that it is a crime for an American to sell
weapons to hostile nations. The country Nono, an enemy of America, has some
missiles, and all of its missiles were sold to it by Colonel West , who is American.
Prove that West is a criminal.
Prolog Program:
enemy(nono, america).
american('colonel west').
hostile(X) :- enemy(X, america).
weapon(missile).
sell('colonel west', missile, nono).
criminal(X) :- american(X), weapon(Y), hostile(Z), sell(X, Y, Z).
Output:
|?-Criminal(X)
X=colonel west
|?-Criminal(‘colonel west’)
Yes
|?-weapon(X)
X=missile
|?-hostile(X)
X=nono
Program No.10
Construct a DFA to accept even numbers of 0s and 1s
Prolog Code
parse(L):-start (S),trans(S,L)
trans(X,[A|B]:-delta(X,A,Y),write(X)
write(‘ ‘),write([A|B]),
nl,trans(y,B)
trans(X,[]):-final(X),write(X),write(‘ ‘)
write([]),nl
delta(0,a,1)
delta(0,b,30
delta(1,a,0)
delta(1,b,2)
delta(2,a,3)
delta(2,b,1)
delta(3,a,2)
delta(3,b,0)
start(0)
final(0)
|?-parse([a,b,a,b,a])
0 [a,b,a,b,a]
1 [b,a,b,a]
2 [a,b,a]
3 [b,a]
0 [a]
No
The program lists the rooms and things contained in each room. It also ascertains
whether or not two room are connected by a door so that the pursuer can explore
each room and find for the nani.
Prolog Program:
room(kitchen).
room(office).
room(hall).
room('dining room').
room(cellar).
location(desk,office).
location(apple,kitchen).
location(flashlight,desk).
location('washing machine',cellar).
location(nani,'washing machine').
location(broccoli,kitchen).
location(crackers,kitchen).
location(computer,office).
door(office,hall).
door(kitchen,office).
door(hall,'dining room').
door(kitchen,cellar).
door('dining room',kitchen).
connected(X,Y) :- door(X,Y); door(Y,X).
list_things(Place):- location(X,Place),write(X),nl,fail.
list_things(_).
list_connections(Place) :- connected(Place,X), write(X), nl, fail.
list_connections(_).
Output:
|?-list_connections(hall)
Dining room
Office
|?-list_connections(kitchen)
Office
Dining room
Cellar
|?-connected(hall,office)
Yes
|?-connected(hall,cellar)
No
|?-list_things(kitchen)
Apple
Broccoli
Crackers
|?-list_things(cellar)
Washing machine
|?-list_things(‘washing machine’)
Nani
Problem Statement: This object of this famous puzzle is to move N disks from the
left peg to the right peg using the center peg as an auxiliary holding peg. At no time
can a larger disk be placed upon a smaller disk. The following diagram depicts the
starting setup for N=3 disks.
Prolog Code:
hanoi(N) :-
move(N, source, dest, spare).
move(1, Source, Dest, _) :-
write('Move disk from '),
write(Source),
write(' to '),
write(Dest),
nl.
move(N, Source, Dest, Spare) :-
N > 1,
N1 is N - 1,
move(N1, Source, Spare, Dest),
move(1, Source, Dest, Spare),
move(N1, Spare, Dest, Source).
Output:
Hanoi(3)
Move disk from source to spare
Move disk from source to spare
Move disk from dest to spare
Move disk from source to dest
Move disk from spare to source
Move disk from spare to dest
Move disk from source to dest
Prolog Code:
/* animal.pro
animal identification game.
go :- hypothesize(Animal),
write('I guess that the animal is: '),
write(Animal),
nl,
undo.
/* hypotheses to be tested */
hypothesize(cheetah) :- cheetah, !.
hypothesize(tiger) :- tiger, !.
hypothesize(giraffe) :- giraffe, !.
hypothesize(zebra) :- zebra, !.
hypothesize(ostrich) :- ostrich, !.
hypothesize(penguin) :- penguin, !.
hypothesize(albatross) :- albatross, !.
hypothesize(unknown). /* no diagnosis */
ostrich :- bird,
verify(does_not_fly),
verify(has_long_neck).
penguin :- bird,
verify(does_not_fly),
verify(swims),
verify(is_black_and_white).
albatross :- bird,
verify(appears_in_story_Ancient_Mariner),
verify(flys_well).
/* classification rules */
mammal :- verify(has_hair), !.
mammal :- verify(gives_milk).
bird :- verify(has_feathers), !.
bird :- verify(flys),
verify(lays_eggs).
carnivore :- verify(eats_meat), !.
carnivore :- verify(has_pointed_teeth),
verify(has_claws),
verify(has_forward_eyes).
ungulate :- mammal,
verify(has_hooves), !.
ungulate :- mammal,
verify(chews_cud).
:- dynamic yes/1,no/1.
Output:
Go
Does the animal have the following attribute: has_hair?y.
Does the animal have the following attribute:eats_meat?y.
Does the animal have the following attribute:has_tawny_color?y.
Does the animal have the following attribute:has_dark_spots?y.
I guess that the animal is:cheetah.
Explanation:
The program starts on typing ‘go’. It then hypothesizes each animal turn by turn
based on the answers to the questions asked. Any hypothesis having the truth value
as “true” is guessed as the animal under consideration. On exhausting all the
animals and still not finding any match, it guesses the animal as ‘unknown’.