Node:The Global Constraint Programming Interface, Next:Reflection Predicates, Previous:Defining Global Constraints, Up:Defining Global Constraints
This section describes a programming interface by means of which new constraints can be written. The interface consists of a set of predicates provided by this library module. Constraints defined in this way can take arbitrary arguments and may use any constraint solving algorithm, provided it makes sense. Reification cannot be expressed in this interface; instead, reification may be achieved by explicitly passing a 0/1-variable to the constraint in question.
Global constraints have state which may be updated each time the constraint is resumed. The state information may be used e.g. in incremental constraint solving.
The following two predicates are the principal entrypoints for defining and posting new global constraints:
clpfd:dispatch_global(+Constraint, +State0, -State, -Actions) [Hook]
Tells the solver how to solve constraints of the form Constraint. Defined as a dynamic, multifile predicate.
When defining a new constraint, a clause of this predicate must be added. Its body defines a constraint solving method and should always succeed deterministically. When a global constraint is called or resumed, the solver will call this predicate to deal with the constraint. NOTE: the constraint is identified by its principal functor; there is no provision for having two constraints with the same name in different modules.
State0 and State are the old and new state respectively.
The constraint solving method must not invoke the constraint solver recursively e.g. by binding variables or posting new constraints; instead, Actions should be unified with a list of requests to the solver. Each request should be of the following form:
exit
fail
X = V
X in R
X in_set S
call(Goal)
clpfd
module.
Goal is executed as any Prolog goal, but in a context where some constraints may already be enqueued for execution, in which case those constraints will run after the completion of the call request.
The constraint solving method is assumed to be idempotent. That is,
if the Actions were performed by the solver and the constraint resumed,
it should not produce any further Actions.
Thus the solver will not check for the resumption
conditions for the given constraint, while performing its Actions.
fd_global(:Constraint, +State, +Susp)
where Constraint is a constraint goal, State is its initial state, and Susp is a term encoding how the constraint should wake up in response to domain changes. This posts the constraint.
Susp is a list of F(Var) terms where Var is a variable to suspend on and F is a functor encoding when to wake up:
dom(X)
min(X)
max(X)
minmax(X)
val(X)
For an example of usage, see A Global Constraint Example.