Recursion 跟踪prolog递归深度的方法?

Recursion 跟踪prolog递归深度的方法?,recursion,prolog,Recursion,Prolog,在prolog中,我的数据库设置如下 male("John"). male("Bob"). male("Billy"). male("Gary"). Parent("Bob","John"). Parent("Billy","Bob"). Parent("Gary", "Billy"). ancestor(Ancestor, Descendant) :- parent(Ancestor, Descendant). ancestor(Ancestor, Descendant) :-

在prolog中,我的数据库设置如下

male("John").
male("Bob").
male("Billy").
male("Gary").

Parent("Bob","John").
Parent("Billy","Bob").
Parent("Gary", "Billy").


ancestor(Ancestor, Descendant) :-
    parent(Ancestor, Descendant).
ancestor(Ancestor, Descendant) :-
    parent(Ancestor, CommonAncestor),
    ancestor(CommonAncestor, Descendant).
是否有可能跟踪这个祖先函数进入递归的程度?例如,如果我们运行

?- ancestor("Billy", "John", X).
有没有可能让X返回2,或者在

?- ancestor("Bob", "John", X).
让X返回1?

您可以写:

ancestor(Ancestor, Descendant,1) :-
    parent(Ancestor, Descendant).
ancestor(Ancestor, Descendant,X) :-
    parent(Ancestor, CommonAncestor),
    ancestor(CommonAncestor, Descendant,X1),
    X is X1+1.
一些例子:

?- ancestor("Billy", "John", X).
X = 2 ;
false.

?- ancestor("Bob", "John", X).
X = 1 ;
false.