% ================================================================= % == == % == An Introduction to ARTIFICIAL INTELLIGENCE == % == Janet Finlay and Alan Dix == % == UCL Press, 1996 == % == == % ================================================================= % == == % == chapter 1, page 22: semantic networks and inheritance == % == == % == Prolog example, Alan Dix, August 1996 == % == == % =================================================================
class(canine). class(wolf). class(dog). class(great_dane). class(rottweiler). class(basenji). object(scooby_do). object(shaggy). class(cartoon_character). is_a(wolf,canine). is_a(dog,canine). is_a(great_dane,dog). is_a(rottweiler,dog). is_a(basenji,dog). instance(scooby_do,great_dane). instance(scooby_do,cartoon_character). instance(shaggy,cartoon_character). prop(canine,carnivorous,true). prop(wolf,is_wild,true). prop(dog,has_tail,true). prop(dog,barks,true). prop(dog,legs,4). prop(great_dane,height,tall). prop(rottweiler,has_tail,false). prop(basenji,barks,false). prop(scooby_do,colour,brown). prop(shaggy,owns,scooby_do). prop(cartoon_character,is_drawn,true). has_prop(OC,P,V) :- prop(OC,P,V). % classes or objects has_prop(O,P,V) :- instance(O,C), has_prop(C,P,V), \+ prop(O,P,V1). % inheritance rule for objects % note that this only comes into effect if there is no % declared property for the object. % If the 'not prop' part were absent, Prolog would be able % to succeed twice on the predicate, the second time returning % the property of the parent. % It is also important that the negated predicate comes last % so that queries where P is uninstantiated work (as in the % suggested list of all properties below). has_prop(C1,P,V) :- is_a(C1,C2), has_prop(C2,P,V), \+ prop(C1,P,V1).
% RUNNING THIS CODE % % Look for properties of individual objects and classes. % For example: 'has_prop(scooby_do,carnivorous,X).' % Alternatively, just list all properties of a single % object or class: 'has_prop(basenji,P,X).' % Or even list all properties: 'has_prop(Z,P,X).' %
% EXAMPLES %
%> has_prop(scooby_do,carnivorous,X).
%
%> has_prop(basenji,P,X).
%
%> has_prop(Z,P,X).