father(caroline, john). father(johnJr, john). father(john, joe). father(fred, johnJr). grandfath(X, Y) :- father(X, Z), father(Z, Y). anc(X, Y) :- father(X, Y). anc(X, Y) :- father(X, Z), anc(Z, Y). setof1(X, G, Z) :- setof(X, G, Z) -> true ; Z = []. % member function, non-built-in version m(X, [X | _]). m(X, [ _ | L]) :- m(X, L). % set intersection int(X, Y, Z) :- setof(A, (m(A, X), m(A, Y)), Z). % Using bagof instead of setof bagint(X, Y, Z) :- bagof(A, (m(A, X), m(A, Y)), Z).