Could you tell me how to find up to N unique solutions of a goal in Prolog?
I know using findall/3 all the solutions of a goal can be found, but for a goal which has too many, or infinite solutions, I want to find only up to N unique solutions if it is enough.
What I want to do is like this:
?- find_unique_n(10, X, any_goal(X), Xs).
Xs = [...] % up to 10 unique solutions.
If the total number of the unique solutions for a goal is below N, I want to find all of them.
Edit: As false pointed out, itt was not clear what 'unique solutions' means. If sample_goal/1 is defined as below:
sample_goal(1).
sample_goal(1).
sample_goal(2).
sample_goal(2).
the expected results are:
?- find_unique_n(1, X, sample_goal(X), Xs).
Xs = [1]
?- find_unique_n(2, X, sample_goal(X), Xs).
Xs = [1,2]
?- find_unique_n(3, X, sample_goal(X), Xs).
Xs = [1,2]
And for goals with infinite solutions, the expected results are:
?- find_unique_n(2, X, (repeat, between(1,2,X)), Xs).
Xs = [1,2]
?- find_unique_n(3, X, (repeat, between(1,2,X)), Xs).
% This won't stop, it's ok