I need an ordered list of Objects that satisfy Goal. setof takes care of the ordering, but fails when no Objects satisfy Goal. I want to return an empty list in
If you do not need the potential nondeterminism or the variable-quantification features of setof, you can stick with findall/3. This is deterministic and doesn't fail:
?- findall(X, fail, Xs).
Xs = []
yes
You can then sort the results yourself using sort/2:
findall(Object, Goal, UnsortedWithDuplicates),
sort(UnsortedWithDuplicates, List)