I have found a wild way to get maximum number of a set defined by a predicate p/1. It uses
findall/3 might be used to collect all the numbers into a list. However, this widely recommended method was difficult to be applied to my case which is related to iterated deepening for a theorem prover.
% ?- Max=max(0), max_of_p(Max). %@ Max = max(3). max_of_p(Max):- p(X), Max = max(M), M0 is max(M, X), nb_setarg(1, Max, M0), fail. max_of_p(_). p(1). p(2). p(3).