
/* Adapted from Clocksin, W.F., Mellish, C.S., "Programming in Prolog"
   (3rd Ed.) Springer-Verlag, 1987 */

parent(john,paul).   /* paul is john's parent    */
parent(paul,tom).    /* tom is paul's parent     */
parent(tom,mary).    /* mary is tom's parent     */

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


% ?- ancestor(john,tom).
% ?- ancestor(john,X).
% ?- ancestor(Y,X).


/*
% Error 1: the program diverges but finds all answers

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

/*
% Error 2: the program just diverges

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