

An example of using wrap_predicate/4 to compute GCD:
:- wrap_predicate(gcd(A,B,Gcd), gcd_wrap, W, gcd_wrap(W, A, B, Gcd)). gcd(X, Y, Gcd), X < Y => gcd(X, Y-X, Gcd). gcd(X, Y, Gcd), X > Y => gcd(Y, X-Y, Gcd). gcd(X, _, Gcd) => Gcd = X. gcd_wrap(call(Closure), X, Y, Gcd) :- functor(Closure, ClosureBlob, 3), X_eval is X, Y_eval is Y, call(ClosureBlob, X_eval, Y_eval, G), Gcd is G.