my question is similar to what has been asked here: determine the old line of succession prolog and this Prolog - recursing down family tree As the solutions stated there are not really I wanted, can anyone give an idea on how to solve the following ? Thanks!
Facts:
male(charles).
male(william).
male(peter).
male(henry).
male(andrew).
male(edward).
female(elizabeth).
female(anne).
female(zara).
female(eugenie).
parent(elizabeth,charles).
parent(elizabeth,anne).
parent(elizabeth,andrew).
parent(elizabeth,edward).
parent(charles,william).
parent(charles,henry).
parent(anne,peter).
parent(anne,zara).
parent(andrew,eugenie).
Rules:
son(X,Y) :- parent(Y,X), male(X).
daughter(X,Y) :- parent(Y,X), female(X).
successor(X,Y):- (son(X,Z);daughter(X,Z)) , (Y=Z;successor(Z,Y)).
Results that i have gotten so far when querying successor(X,Y).
X = charles,
Y = elizabeth ;
X = andrew,
Y = elizabeth ;
X = edward,
Y = elizabeth ;
X = william,
Y = charles ;
X = william,
Y = elizabeth ;
X = henry,
Y = charles ;
X = henry,
Y = elizabeth ;
X = peter,
Y = anne ;
X = peter,
Y = elizabeth ;
X = anne,
Y = elizabeth ;
X = zara,
Y = anne ;
X = zara,
Y = elizabeth ;
The actual result that I wanted to show is charles' family, andrew's family , edward's family and lastly anne's family but the son of anne appeared first. I am guessing that it is because the son(X,Y) rule executes first and finds all the matches and there is a problem with successor(X,Y). Is there a way that I can make it execute after anne is found as a result? Thanks!
The actual answer would be something like
X = charles,
Y = elizabeth;
X = william,
Y = charles;
X = william,
Y = elizabeth ;
X = henry,
Y = charles ;
X = henry,
Y = elizabeth ;
X = andrew,
Y = elizabeth ;
X = eugenie,
Y = andrew ;
X = eugenie,
Y = elizabeth ;
X = edward,
Y = elizabeth ;
X = anne,
Y = elizabeth ;
X = peter,
Y = anne ;
X = peter,
Y = elizabeth ;
X = zara,
Y = anne ;
X = zara,
Y = elizabeth ;