Search code examples
prologclpfd

Prolog clpfd :: operator expected


Dear Stackoverflow Community,

I just wanted to test out the constrained logic programming library (clpfd) for Prolog. So I am including the library by calling

:- use_module(library(clpfd)).

Then I want to do sth like the following.

[X,Y] :: [1..2], X #\= Y, X+Y #\= 3.

But I always get the answer that

ERROR: Syntax error: Operator expected
ERROR: [X,Y]
ERROR: ** here **
ERROR:  :: [1..2], X #\= Y, X+Y #\= 3 .

The same happens when executing the following example

? member(X,[42,1,17]), [X,Y] :: [0..20].

ERROR: Syntax error: Operator expected
ERROR: member(X,[42,1,17]), [X,Y]
ERROR: ** here **
ERROR:  :: [0..20] .

Seems like Prolog does not recognise the :: operator properly. Any help is appreciated


Solution

  • As far as I know, there is no (::)/2 predicate in the clpfd library. You are probably looking for the ins/2 predicate. For example:

    ?- [X,Y] ins 1..2, X #\= Y, X+Y #\= 3, label([X,Y]).
    false.
    
    ?- [X,Y] ins 1..3, X #\= Y, X+Y #\= 3, label([X,Y]).
    X = 1,
    Y = 3 ;
    X = 2,
    Y = 3 ;
    X = 3,
    Y = 1 .
    

    So if X and Y are in 1..2, then there is no solution, since your first constraint says X should be different from Y, and the second constraint says that X + Y should be different from 3.

    In case we add 3 to the result, then there are solutions.

    We can here use ins/2 to filter as well:

    ?- member(X,[42,1,17]), [X,Y] ins 0..20.
    X = 1,
    Y in 0..20 ;
    X = 17,
    Y in 0..20.