2
votes

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

1
What Prolog are you using and what do you think the :: operator is? - Paul Brown
Both B-Prolog and ECLiPSe define a ::/2 operator for constraints. Both provide alternatives to it (in/2 in the case of B-Prolog and in_set_range/2 in the case of ECLiPSe). - Paulo Moura
The ::/2 syntax originates from the very first finite-domain CLP system en.wikipedia.org/wiki/CHIP_(programming_language), and is used in CLP(FD) implementations such as ECLiPSe's library(ic). - jschimpf

1 Answers

2
votes

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.