/* AI Assignment 3: semantic network exercise (Using Figure 10.9, page 351 in AIMA 2nd edition textbook). I've included some things to watch for ("DON'T DO THIS"). */ % Semantic network definition: sister_of(mary,john). %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Class membership %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% member_of(mary,female_persons). member_of(john,male_persons). % The following is for the given definition for "HasMother" % (see p. 350: after converting A -> (B -> C) to A ^ B -> C). member_of(Y,female_persons) :- has_mother(X,Y), isa(X,persons). % ***NOT REQUIRED: this is for my own testing. % Note that calling all_prop(X,Y,Z) reveals that Frieda has 2 legs as well. has_mother(john,frieda). subset_of(female_persons,persons). subset_of(male_persons,persons). subset_of(persons,mammals). % DON'T DO THIS: leads to infinite looping for false queries, % e.g. subset_of(mmm,mammals). % subset_of(X,Z) :- subset_of(X,Y), subset_of(Y,Z). % Transitive definition of subset. subset(X,Y) :- subset_of(X,Y). subset(X,Z) :- subset_of(X,Y), subset(Y,Z). % DON'T DO THIS: reversing the order will lead to infinite looping for false queries. % Unfortunately, the textbook gives an example of this form on p. 292. %subset(X,Z) :- subset(X,Y), subset_of(Y,Z). % Transitive definintion of 'isa' relation (class membership). % Same restrictions apply to defining this predicate as for 'subset.' isa(X,Y) :- member_of(X,Y). isa(X,Z) :- member_of(X,Y), subset(Y,Z). %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Properties (incl. inheritence and overrides) % % Assume single-inheritence in the network, % incl. restriction that are objects are a % member_of at most one class. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Using general framework for named properties in a network (e.g. legs). object_has(john,legs,1). class_members_have(persons,legs,2). % The cut (!) operator is used here to insure that the first matching % property we find along the inheritence hierarchy is returned (NOTE: % this prevents querying for all properties). % **This avoid john having both 1 and 2 legs (use ';' to query for % subsequent unifications). has(X,Y,Z) :- object_has(X,Y,Z), !. % special case for object. has(X,Y,Z) :- class_members_have(X,Y,Z), !. has(X,Y,Z) :- member_of(X,C), has(C,Y,Z). % Immediate parent class. has(X,Y,Z) :- subset_of(X,C), has(C,Y,Z). % Transitive cases. % DON'T DO THIS: this definition is incorrect, but useful for retrieving all properties % (including those that are overridden, such as the number of legs john has). all_prop(X,Y,Z) :- object_has(X,Y,Z). all_prop(X,Y,Z) :- isa(X,C), class_members_have(C,Y,Z). %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Queries asked for %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Requested queries (I'll run these manually in the SWI shell). % has(mary,legs,X). % has(john,legs,X). % isa(john,mammal). % isa(mary,male_persons). % isa(mary,persons).