The SICStus manual for the CLP(FD) library says:
nvalue(?N, +Variables)
whereVariables
is a list of domain variables with finite bounds or integers, andN
is an integer or a domain variable. True ifN
is the number of distinct values taken byVariables
.
This is particularly useful when one wants to minimize the number of distinct values in the solution. For example, if one is trying to distribute stuff into bags of different sizes, and want to minimize the number of bags.
Is there an equivalent predicate (or way) for achieving the same in SWI Prolog?
After @jschimpf comment, I've rethought the algorithm.
further cleanup
again, after @jschimpf note, I've tweaked the code: now it's very compact, thanks to libraries apply and yall.
old answer, buggy
my naive attempt, based on reification:
running your example query:
edit
my code was a bit pedantic, of course could be more compact (and clear ?):
note that findall/3 will not work, since the copy of reified variable D would lose the posted constraints.