Consider a database of facts that describe parent relationships as well as gende
ID: 3537340 • Letter: C
Question
Consider a database of facts that describe parent relationships as well as gender relationships. The predicate parent(john,ann) is interpreted as: "John is a parent of Ann". The predicate male(john) is interpreted as: "John is a man". The predicate female(ann) is interpreted as: "Ann is a woman". So an example database of facts is:
parent(john,ann).
parent(jim,john).
parent(jim,keith).
parent(mary,ann).
parent(mary,sylvia).
parent(brian,sylvia).
male(keith).
male(jim).
female(sylvia).
female(ann).
male(brian).
Note that some things are not specified in the database above (e.g., male(john)).
Write a Prolog predicate uncle(X,Y) that is true if X is Y's uncle. Note that we are not considering uncles "by marriage", meaning that for X to be Y's uncle the two must be related by blood. Sample tests for your code (user input is in red):
?- uncle(keith,ann).
Yes
?- uncle(ann,mary).
No
?- uncle(keith,X).
X = ann ;
No
?- uncle(john,ann).
No
?- uncle(X,Y).
X = keith
Y = ann ;
No
Also, write a Prolog predicate halfsister(X,Y) that is true if X is Y's half-sister. Sample test for your code (user input is in red):
?- halfsister(ann,sylvia).
Yes
?- halfsister(X,sylvia).
X=ann ;
No
?- halfsister(X,Y).
X=ann
X=sylvia ;
X=sylvia
X=ann ;
No
Explanation / Answer
%%X is an uncle of Y if and only if X is a man, there exists some person Z who is
%%a parent of Y and X and Z have a common parent
uncle(X,Y) :- parent(Z,Y),parent(A,X),parent(A,Z),male(X).
half(B,C,X,Y):-parent(B,X),parent(C,Y),not(B=C),!.
%% The above rule is added to prevent backtracking (using ! or cut)
%% and producing same results again and again
%%X is a half sister of Y if and only if X is a woman, X and Y have one same parent
%%and one different parent
halfsister(X,Y):- female(X),parent(A,X),parent(A,Y),not(X=Y),half(B,C,X,Y).
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.