16
Experiment 1; Problem: For all X , if X is mortal then X is a person Prolog Code mortal(X):-person(X) person(Socrates) person(plato) person(aristotle) Experiment 2 Prolog Code Problem; Parent Child Relationship parent(suresh,shyam) parent(Shyam,Ram). male(suresh). parent(suresh,ram). female(ramya). grandfather(X,Y):-parent(X,Z),parent(Z,Y),male(X).

Documentat

Embed Size (px)

DESCRIPTION

ai

Citation preview

Page 1: Documentat

Experiment 1;

Problem: For all X , if X is mortal then X is a person

Prolog Code

mortal(X):-person(X)

person(Socrates)

person(plato)

person(aristotle)

Experiment 2

Prolog Code

Problem; Parent Child Relationship

parent(suresh,shyam)

parent(Shyam,Ram).

male(suresh).

parent(suresh,ram).

female(ramya).

grandfather(X,Y):-parent(X,Z),parent(Z,Y),male(X).

Page 2: Documentat

Experiment 3

Find Siblings and Cousins

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).

Page 3: Documentat

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.

Query: Is there a member of the Hoofers Club who is a mountain climber but not a skier?

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).

Page 4: Documentat

Experiment 5

List Operations

Given list [a, b, c, d, e]

Split Operation

?- listsplit([a,b,c,d,e], A, B).

Output

A = aB = [b, c, d, e] ;

Member is a standard prolog built-in predicate

member(Element, List).

Where List is any prolog list, and Element is any element in that list. The following, for instance, succeeds (returns 'Yes'):

member(a, [a, b, c]).member(b, [a, b, c]). member(c, [a, b, c]).

This query:

?- member(Element, [a, b, c]).

Will return the following values for Element:

Element = a;Element = b;Element = c;

The append predicate

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:

append(Xs, Ys, Zs)

Page 5: Documentat

Where Zs is Xs appended to Ys.

Can be used to append two lists as follows:

?- append([a, b, c], [d, e, f], Result).

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:

append([], Ys, Ys).append([X|Xs], Ys, [X|Zs]) :- append(Xs, Ys, Zs).

and changing it slightly,

revappend([], Ys, Ys).revappend([X|Xs], Ys, Zs) :- revappend(Xs, [X|Ys], Zs).

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

The variable X will be instantiated to the value of Expression. For example,

Page 6: Documentat

?- X is 10 + 5. X = 15 ? yes ?- 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. 7Problem 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 tempsOutput:RunEnter the temperature in degree Celsius:32.The temperature is 89.59999999994 degrees Fahrenheit.

Program No. 8Problem Statement:Factorial of a numberProlog code:

Page 7: Documentat

factorial(N, Fact) :-N > 0,N1 is N - 1,factorial(N1, Fact1),Fact is N * Fact1.factorial(0, 1).Output:Factorial(3,6)YesFactorial(3,5)No

Program No. 9Problem 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

Page 8: Documentat

Program No.10Construct 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,30delta(1,a,0)delta(1,b,2)delta(2,a,3)delta(2,b,1)delta(3,a,2)

Page 9: Documentat

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

Program No.11: (Monkey and Banana Problem)Problem Statement: There is a monkey at the door of a room. In the middle of the room a banana hangs from the ceiling. 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. The monkey can perform the following actions:-

1) Walk on the floor.2) Climb the box.3) Push the box around (if it is beside the box).4) Grasp the banana if it is standing on the box directly under the banana.

Explanation: We define the state as a 4-tuple:(monkey-at, on-floor, box-at, has-banana)

The proof that the monkey can reach the bananas is given by resolution. In finding a resolution proof, Prolog searches a data base of clauses in an exhaustive manner until a chain of unifications have been found to produce a proof.Prolog Program: move(state(middle,onbox,middle,hasnot), grasp, state(middle,onbox,middle,has)).move(state(P,onfloor,P,H), climb, state(P,onbox,P,H)).move(state(P1,onfloor,P1,H), push(P1,P2), state(P2,onfloor, P2, H)).move(state(P1, onfloor, B, H), walk(P1,P2), state(P2, onfloor, B, H)).canget(state(_,_,_,has)).canget(State1):-move(State1, Move, State2), canget(State2).Output:|?-canget(X)X=state(_2f8,_34h,_h,has)?Yes|?-canget(state(away, air, underground, have))No|?-canget(state(atdoor, onfloor, atwindow, hasnot))yes

Page 10: Documentat

Program No.12: (Nani Search)Problem Statement: Nani Search is an adventure game. Your persona as the adventurer is that of a three year old girl. The lost treasure with magical powers is your nani (security blanket). The terrifying obstacle between you and success is a dark room. It is getting late and you're tired, but you can't go to sleep without your nani. Your mission is to find the nani.Explanation:

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)

Page 11: Documentat

Dining roomOffice|?-list_connections(kitchen)OfficeDining roomCellar|?-connected(hall,office)Yes|?-connected(hall,cellar)No|?-list_things(kitchen)AppleBroccoliCrackers|?-list_things(cellar)Washing machine|?-list_things(‘washing machine’)Nani

Program No. 13(Towers of Hanoi)

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

Page 12: Documentat

Move disk from source to spareMove disk from dest to spareMove disk from source to destMove disk from spare to sourceMove disk from spare to destMove disk from source to dest

Program No. 14:(Animal Identification Problem)Problem Statement:

Prolog Code:/* animal.pro animal identification game.

start with ?- go. */

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 */

/* animal identification rules */cheetah :- mammal, carnivore, verify(has_tawny_color), verify(has_dark_spots).tiger :- mammal, carnivore, verify(has_tawny_color), verify(has_black_stripes).giraffe :- ungulate, verify(has_long_neck), verify(has_long_legs).zebra :- ungulate, verify(has_black_stripes).

ostrich :- bird, verify(does_not_fly), verify(has_long_neck).penguin :- bird, verify(does_not_fly), verify(swims),

Page 13: Documentat

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).

/* how to ask questions */ask(Question) :- write('Does the animal have the following attribute: '), write(Question), write('? '), read(Response), nl, ( (Response == yes ; Response == y) -> assert(yes(Question)) ; assert(no(Question)), fail).

:- dynamic yes/1,no/1.

/* How to verify something */verify(S) :- (yes(S) -> true ; (no(S) -> fail ; ask(S))).

/* undo all yes/no assertions */undo :- retract(yes(_)),fail. undo :- retract(no(_)),fail.undo.

Output:

Go

Page 14: Documentat

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’.