我需要编写一个小的Prolog程序,用于检查给定的人是否是第二个人的祖先。以下是事实和规则:
mother(tim, anna).
mother(anna, fanny).
mother(daniel, fanny).
mother(celine, gertrude).
father(tim, bernd).
father(anna, ephraim).
father(daniel, ephraim).
father(celine, daniel).
parent(X,Y) :- mother(X,Y).
parent(X,Y) :- father(X,Y).
判断一个人是否是另一个人的祖先很简单:
ancestor(X, Y) :- parent(X, Y).
ancestor(X, Y) :- parent(X, Z), ancestor(Z, Y).
现在我需要编写一个名为ancestor(X,Y,Z)的方法,它还应该打印出两个人之间的关系。它应该像这样:
?- ancestor(ephraim, tim, X).
false.
?- ancestor(tim, ephraim, X).
X = father(mother(tim)).
这就是问题所在:我不知道如何做到这一点。