a(a).
r(a,b).
r(a,c).
s(b,c).

r(X,f(X)):- a(X), not rec(X).
r(X,g(X)):- a(X), not rec(X).
s(f(X),g(X)):- a(X), not rec(X).
d(f(X)):- a(X), not rec(X).
d(g(X)):- a(X), not rec(X).

r(h(X,Y),X):- s(X,Y), not d(X), not d(Y).
r(h(X,Y),Y):- s(X,Y), not d(X), not d(Y).
a(h(X,Y)):- s(X,Y), not d(X), not d(Y).
rec(h(X,Y)):- s(X,Y), not d(X), not d(Y).
