determine the old line of succession prolog

2019-08-15 13:33发布

问题:

Given the british royal family, Using only male(X),female(X) and parent(X,Y) where the parent of X is Y, how to create the line of succession(X,Y) where Y is the successor of X in prolog

I have defined all the males and female and parent

I have tried this as well:

son(X,Y) :- parent(X,Y), male(Y).
daughter(X,Y) :- parent(X,Y), female(Y).
successor(X,Y):- (son(X,Z);daughter(X,Z)) , (Y=Z;successor(Z,Y)).

apparently it only work before anne after louise, it should go anne but not peter...

I have arrange the facts according to their birth in parent(X,Y). Facts:

male(charles).
male(william).
male(peter).
male(henry).
male(andrew).
male(edward).
male(viscount).
male(savanna).

female(elizabeth).
female(anne).
female(zara).
female(beatrice).
female(eugenie).
female(louise).
female(isla).

parent(elizabeth,charles).
parent(elizabeth,anne).
parent(elizabeth,andrew).
parent(elizabeth,edward).

parent(anne,peter).
parent(anne,zara).
parent(charles,william).
parent(charles,henry).
parent(andrew,beatrice).
parent(andrew,eugenie).
parent(edward,louise).
parent(edward,viscount).
parent(peter,savanna).
parent(peter,isla).

Results that i have gotten so far when querying successor(X,Y).

X = elizabeth,
Y = charles ;
X = elizabeth,
Y = william ;
X = elizabeth,
Y = henry ;
X = elizabeth,
Y = andrew ;
X = elizabeth,
Y = beatrice ;
X = elizabeth,
Y = eugenie ;
X = elizabeth,
Y = edward ;
X = elizabeth,
Y = viscount ;
X = elizabeth,
Y = louise ;

After this point, when i try to go the Anne family it goes wrong.

X = anne,
Y = peter ;
X = anne,
Y = savanna ;
X = anne,
Y = isla ;
X = charles,
Y = william ;
X = charles,
Y = henry ;
X = edward,
Y = viscount ;
X = peter,
Y = savanna ;
X = elizabeth,
Y = anne ;
X = elizabeth,
Y = peter ;
X = elizabeth,
Y = savanna ;
X = elizabeth,
Y = isla ;
X = elizabeth,
Y = zara ;
X = anne,
Y = zara ;
X = andrew,
Y = beatrice ;
X = andrew,
Y = eugenie ;
X = edward,
Y = louise ;
X = peter,
Y = isla ;

the desired output when going to anne tree which i want is

    X = elizabeth,
    Y = anne;
    X = anne,
    Y = peter ;
    X = anne,
    Y = savanna ;
    X = anne,
    Y = isla ;

i have been trying all sorts of combinations and this is the closest i got so far X is the parent while Y is the child in parent(X,Y).

tried combinations of successor:

successor(X,Y):- (son(X,Z);daughter(X,Z)) , (Y=Z;successor(Z,Y)). 
successor(X,Y):- parent(X,Z), (Y=Z ; successor(Z,Y)).
successor(X,Y):- (male(Z);female(Z)) , (Y=Z;successor(Z,Y)).
successor(X,Y):- (parent(X,Z),(male(Z);female(Z)), (Y=Z;successor(Z,Y)).

neither of them worked.

The actual result that show the old line of succession is charles' family, andrew's family , edward's family and lastly anne's family.

回答1:

Your problem is that you use a condition more complex than needed, and the database is not complete.

Specifically, charles and peter miss gender, as this code show

?- forall(((parent(P,_) ; parent(P,_)), \+(male(P);female(P))), writeln(P)).
charles
charles
peter
peter
charles
charles
peter
peter
true.

son and daughter depend on gender, and make you condition fail. You could correct your database, or stick to something simpler:

successor(X,Y) :- parent(X,Z), (Y=Z ; successor(Z,Y)).