You donβt need so much !/0 : Prolog can often say that your predicates are deterministic.
Let me first suggest the next version of your code. It uses names that are more relational, does not contain !/0 and uses higher order predicates to make the code shorter.
:- use_module(library(clpfd)). bartenders_cocktails_variables(Bs, Cs, Vs) :- length(Bs, LBs), maplist(bartenders_cocktail_variable(Bs, LBs), Cs, Vs). bartenders_cocktail_variable(Bs, N, C, V) :- V in 1..N, foldl(compatible_bartender(C,V), Bs, 1, _). compatible_bartender(C, V, Cs, N0, N1) :- ( member(C, Cs) -> true ; V
Notice that I am counting up and not down to list the bartenders (which are just lists of cocktails that they can mix), as this seems more natural. I was also able to omit (\+)/1 by simply switching if-then-else branches.
An example query showing that a predicate is deterministic in this use case:
?- bartenders_cocktails_variables([[a,b],[a,b],[x,y]], [x,a,b], Vars). Vars = [3, _G1098, _G1101], _G1098 in 1..2, _G1101 in 1..2.
We see: cocktail x must be mixed by a third bartender, etc.
I think this part of your program may not be responsible for the slow performance that you are describing. Perhaps other parts of your program (inadvertently) are not determinate? Maybe try different labeling strategies or other restrictions? We can help you more if you post more contexts.