我的问题类似于此处提出的问题:determine the old line of succession prolog和此Prolog - recursing down family tree 正如所说的解决方案并不是我真正想要的,有谁能提出如何解决以下问题的想法?谢谢!
事实:
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).
规则:
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)).
查询后继者(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 ;
我想展示的实际结果是查尔斯'家人,安德鲁的家人,爱德华的家人和最后安妮的家人,但安妮的儿子首先出现。我猜这是因为儿子(X,Y)规则首先执行并找到所有匹配,并且后继(X,Y)存在问题。有没有办法让我在找到安妮后执行它?谢谢!
实际答案可能是
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 ;