[ Advanced Control and Suspensions | Reference Manual | Alphabetic Index ]
delayed_goals_number(?Var, -Number)
Succeeds if Number is the number of goals delayed by the variable Var.
- Var
- Any term.
- Number
- Integer or a variable.
Description
Unifies Number with the number of goals delayed by the variable Var. If
Var is instantiated, Number is unified with 1000000. If Var is not
instantiated and there are no goals delayed by it, Number is unified
with 0. This predicate does not construct the list of delayed goals and
hence it is faster than delayed_goals/2. Its purpose is to give each
variable a weight corresponding to the number of constraints imposed on
the variable to be able to select the most constrained one. It is
assumed that one million constraints cannot be placed on any variable.
Modes and Determinism
- delayed_goals_number(?, -) is det
Examples
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).
See Also
delayed_goals / 1, delayed_goals / 2, subcall / 2