0% found this document useful (0 votes)
157 views3 pages

Family Tree Prolog Program

This document describes a Prolog program that defines attributes of different fruits and allows the user to ask questions to determine the type of fruit based on its attributes like color, shape, whether it has a stem or stone. The program contains facts that define attributes for different fruits, asks the user questions to determine unknown attributes, and remembers the user's answers to previously asked questions to avoid asking the same questions again. It then uses the user-provided attribute values to deduce and output the type of fruit.

Uploaded by

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

Family Tree Prolog Program

This document describes a Prolog program that defines attributes of different fruits and allows the user to ask questions to determine the type of fruit based on its attributes like color, shape, whether it has a stem or stone. The program contains facts that define attributes for different fruits, asks the user questions to determine unknown attributes, and remembers the user's answers to previously asked questions to avoid asking the same questions again. It then uses the user-provided attribute values to deduce and output the type of fruit.

Uploaded by

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

Family Tree Prolog Program

--------------------------------------------------------------------------------------------------------------------------------------

Name: Vivek Wani

Gr.No:182142

Roll No:80

Batch:B4

Program

% Data: fruit(X) :- attributes(Y)

fruit(banana) :- colour(yellow), shape(crescent).

fruit(apple) :- (colour(green); colour(red)), shape(sphere), stem(yes).

fruit(lemon) :- colour(yellow), (shape(sphere);shape('tapered sphere')), acidic(yes).

fruit(lime) :- colour(green), shape(sphere), acidic(yes).

fruit(pear) :- colour(green), shape('tapered sphere').

fruit(plum) :- colour(purple), shape(sphere), stone(yes).

fruit(grape) :- (colour(purple);colour(green)), shape(sphere).

fruit(orange) :- colour(orange), shape(sphere).

fruit(satsuma) :- colour(orange), shape('flat sphere').

fruit(peach) :- colour(peach).

fruit(rhubarb) :- (colour(red); colour(green)), shape(stick).

fruit(cherry) :- colour(red), shape(sphere), stem(yes), stone(yes).

% Asks

colour(X) :- menuask(colour, X, [red, orange, yellow, green, purple, peach]).

shape(X) :- menuask(shape, X, [sphere, crescent, 'tapered sphere', 'flat sphere', stick]).

acidic(X) :- ask(acidic, X).

stem(X) :- ask(stem, X).

stone(X) :- ask(stone, X).

:- dynamic(known/3).

:- discontiguous menuask/3.
:- discontiguous ask/2.

% Remember what I've been told is correct

ask(Attr, Val) :- known(yes, Attr, Val), !.

menuask(Attr, Val, _) :- known(yes, Attr, Val), !.

% Remember what I've been told is wrong

ask(Attr, Val) :- known(_, Attr, Val), !, fail.

menuask(Attr, Val, _) :- known(_, Attr, Val), !, fail.

% Remember when I've been told an attribute has a different value

ask(Attr, Val) :- known(yes, Attr, V), V \== Val, !, fail.

menuask(Attr, Val, _) :- known(yes, Attr, V), V \== Val, !, fail.

% % I don't know this, better ask!

ask(Attr, Val) :- write(Attr:Val), write('? '),

read(Ans),

asserta(known(Ans, Attr, Val)), Ans == yes.

menuask(Attr, Val, List) :-

write('What is the value for '), write(Attr), write('? '), nl,

write(List), nl,

read(Ans),

check_val(Ans, Attr, Val, List),

asserta(known(yes, Attr, Ans)),

Ans == Val.

check_val(Ans, _, _, List) :- member(Ans, List), !.

check_val(Ans, Attr, Val, List) :-

write(Ans), write(' is not a known answer, please try again.'), nl,

menuask(Attr, Val, List).

go :- fruit(Fruit), write('The fruit is '), write(Fruit), nl.


Output

You might also like