My code is:

parent(pamela, bob). parent(tom, bob). parent(tom, liz). parent(bob, ann). parent(bob, patricia). parent(patricia, jim). ancestor(X, Y) :- parent(X, Y); ancestor(X, Z), parent(Z, Y). % Кому Памела является предком? query1(X) :- ancestor(pamela, X), write(X), nl, fail. 

Request:

 query1(X). 

Gives an error message:

 ERROR: Out of local stack bob ann patricia jim 

Launched in the online translator ( link ).

What did I do wrong?

    1 answer 1

    If in a nutshell, you have received infinite recursion "X is the ancestor of Y, if X is the ancestor of Z, if Z is the ancestor of Z1, if Z1 is the ancestor of Z2, etc."

    It is treated by the transfer of recursion in the tail of the sentence:

     ancestor(X, Y) :- parent(X, Y). ancestor(X, Y) :- parent(X, Z), ancestor(Z, Y).