Success: % Make an intelligent permutation choosing % the most constrained variable. perm([], []). perm([Var|List], Values) :- delayed_goals_number(Var, C), maxval(List, Var, C, Chosen, RestVar), delete(Chosen, Values, RestVal), perm(RestVar, RestVal). maxval(L, Chosen, 1000000, Chosen, L) :- !. maxval([], Chosen, _, Chosen, []). maxval([X|L], SoFar, MaxVal, Chosen, [V|Rest]) :- delayed_goals_number(X, C), (C =< MaxVal -> V = X, Next = SoFar, Max = MaxVal ; V = SoFar, Next = X, Max = C), maxval(L, Next, Max, Chosen, Rest). % the values are generated in the listed order [eclipse]: perm([A, B, C], [1,2,3]). A = 1 B = 2 C = 3 More? (;) yes. % B is more constrained than the others, and so % its value will be generated first. [eclipse]: B < 3, perm([A, B, C], [1,2,3]). A = 2 B = 1 C = 3 More? (;) Fail: X > 0, delayed_goals_number(X, 0).