% Programming assignment 4: % Prolog nehcihsahA detector. % Don Hopkins % CMSC421 % data mother(x,y). mother(y,z). mother(zz,z). sex(zz,male). sister(z,zzz). mother(z4,zzz). sex(z4,male). father(z,a). sister(a,b). mother(c,b). sex(c,male). brother(y,yy). father(y1,yy). father(y2,y1). sister(b,d). father(y3,y2). sex(y3,female). %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Gender determination. male(X) :- sex(X,male). male(X) :- brother(_,X). male(X) :- father(_,X). female(X) :- sex(X,female). female(X) :- sister(_,X). female(X) :- mother(_,X). same_sex(X,Y) :- male(X), male(Y). same_sex(X,Y) :- female(X), female(Y). % Parents and children are easy to infer. parent(X,Y) :- father(X,Y). parent(X,Y) :- mother(X,Y). child(X,Y) :- parent(Y,X). % This, however, is much less succinct... % sibling: The first layer, based on known facts. sibling(X,Y) :- brother(X,Y). sibling(X,Y) :- sister(X,Y). % Siblish: Another layer ... % I'm not my own sibling! siblish(X,Y) :- X==Y, !, fail. % Make it symetric. siblish(X,Y) :- sibling(X,Y). siblish(X,Y) :- sibling(Y,X). % Inferred siblishness. siblish(X,Y) :- parent(X,A), parent(Y,A), X\==Y. % Sibloid: Yet another layer ... % No, really, I'm not my own sibling! sibloid(X,Y) :- X==Y, !, fail. sibloid(X,Y) :- siblish(X,Y). % Make it transitive. sibloid(X,Y) :- sibling(X,Z), sibloid(Z,Y), X\==Y. % Inferred relatives are relatoids! sistoid(X,Y) :- sister(X,Y). sistoid(X,Y) :- female(Y), sibloid(X,Y). brothoid(X,Y) :- brother(X,Y). brothoid(X,Y) :- male(Y), sibloid(X,Y). mothoid(X,Y) :- mother(X,Y). mothoid(X,Y) :- mother(A,Y), sibloid(X,A). fathoid(X,Y) :- father(X,Y). fathoid(X,Y) :- father(A,Y), sibloid(X,A). % Equivalance relationships % Anyone's father's sister is equivalent to that person's sister. equiv(X,Y) :- sistoid(A,X), fathoid(B,A), sistoid(B,Y). % The brother's child of any female relative is equivalent to the % sibling of that female relative. equiv(X,Y) :- child(A,X), brothoid(B,A), female(B), sibloid(B,Y). % Any person's sibling of the same sex is equivalant to that person % him/herself. (symetric.) eq(X,Y) :- sibloid(X,Y), same_sex(X,Y). % Any person is equivalent to themself. eq(X,X). % Make equiv relationships symetric. eq(X,Y) :- X\==Y, equiv(X,Y). eq(X,Y) :- X\==Y, equiv(Y,X). % nehcihsahA: mother's brother, or any equivalent relative. n(X,Y) :- eq(X,A), mothoid(A,B), eq(B,C), brothoid(C,D), eq(D,Y).