prologswi-prologclpr

SWI Prolog, CLP(R): Can I bind a constraint to a variable?


Or can a constraint variable be bound to another variable (see the example below)?

?- use_module(library(clpr)).
true.

% this works
?- {X >= 5.0, X =< 10.0}, minimize(X).
X = 5.0 .

% but I do not know why this fails
?- C = {X >= 5.0, X =< 10.0}, minimize(X).
false.

% and this also fails consequently
?- C = {X >= 5.0, X =< 10.0}, term_variables(C, [Var]), minimize(Var).
false.

Solution

  • Prolog doesn't have 'assignment', so beware that generally you should first understand its peculiar programming model. In this particular case, you can 'invoke' your bindings, giving to library(clpr) a chance to perform its complex duties:

    ?- use_module(library(clpr)).
    true.
    
    ?- {X >= 5.0, X =< 10.0}, minimize(X).
    X = 5.0 ;
    false.
    
    ?- C = {X >= 5.0, X =< 10.0}, C, minimize(X).
    C = {5.0>=5.0, 5.0=<10.0},
    X = 5.0 ;
    false.
    

    but I think that applying systematically this trick to your constraints model could result in a brittle application.