Next: Forgetting Constraints, Up: CLPFD Interface [Contents][Index]
A constraint is called in the same way as any other Prolog predicate. When called, the constraint is posted to the store. For example:
| ?- X in 1..5, Y in 2..8, X+Y #= T. X in 1..5, Y in 2..8, T in 3..13 | ?- X in 1.0..5.0, T in 3.0..13.0, X+Y $= T. X in 1.0..5.0, T in 3.0..13.0, Y in-2.0000000000000013..12.000000000000002
Note that the answer constraint shows the domains of nonground query variables, but does not show any constraints that may be attached to them.
Note also that #=, $=, and similar constraints are
term-expanded to internal (non-exported) predicates at compile time
when they occur in source code, and prior to execution when they occur
in queries. In that process, the role of any variable in those
constraints is fixed to being a domain variable. It does not work to
unify a domain variable with an expression such as X+Y. For
example:
| ?- X in 1..5, Y in 2..8, T #= U, U =.. [+,X,Y]. ! Type error in argument 2 of (=)/2 ! expected an integer, but found _250+_252 ! goal: _266=_250+_252 | ?- X in 1..5, Y in 2..8, U =.. [+,X,Y], T #= U. ! Type error in argument 2 of user:scalar_product_reif/6 ! expected 'numeric argument', but found _250+_252 ! goal: scalar_product_reif([-1,1],[_250+_252,_290],#=,0,1,[])
If such “late term expansion” is required, there is a way to delay
it until execution time, by wrapping the constraint in call/1:
| ?- X in 1..5, Y in 2..8, U =.. [+,X,Y], call(T #= U). U = X+Y, X in 1..5, Y in 2..8, T in 3..13
Normally, after posting a constraint, propagation to fixpoint is performed, which can be an overkill. The following provides a means of posting a set of constraints in one batch, suspending all propagation until the whole set has been posted. Suspending propagation can significantly reduce posting overhead.
fd_batch(+Goals) since release 4.2.1where Goals should be a list of constraints, or Prolog goals posting such constraints. Please note: Every constraint posting must be deterministic and must not be backtracked over. A posting that is backtracked over leads to undefined behavior.