'How to define transitive higher order function in prolog

I have a bunch of two arity functions:

forward(X, Y) :- X;Y. % Not the literal implementation.
backward(X, Y) :- X,Y.

I want to define transitive relation for this functions like this:

forward8(X, Y) :- forward(X, Y); forward(X, Z); forward8(Z, Y).

% Same for backward8.

Is there a more elegant way of defining these functions with a single higher order function, or is this ok?

For example I thought about projection:

projection(fn, X, Y) :- fn(X, Y); fn(X, Z), fn(Z, Y).

I am ok with defining the functions for each case, I am just learning what are my options.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source