Search code examples
prologclpfd

Why is this code in PROLOG using constraints gives Operator expected syntax error?


col_tri(Vars):- Vars=[X1,X2,X3],
                Vars in 1..3,
                X1#\=X2,
                X1#\=X3,
                X2#\=X3,
                label(Vars).

This code is giving me this error in line 2 (Vars in 1..3,): ERROR: c:/users/xxxx/desktop/prolog/tp2.pl:2:20: Syntax error: Operator expected


Solution

  • Operators are things like the + in 1 + 1, and in your code the in in Vars in 1..3.

    Prolog code can define new operators at runtime.

    The in operator is not a standard part of Prolog, it's defined by the CLPFD library, which SWI Prolog has, but does not load automatically.

    And in is for a single variable on the left, there is also ins for a list of variables like your Vars. So the code should become:

    :- use_module(library(clpfd)).
    
    col_tri(Vars):- Vars=[X1,X2,X3],
                    Vars ins 1..3,
                    X1#\=X2,
                    X1#\=X3,
                    X2#\=X3,
                    label(Vars).