Question

I want to have a list L with three elements A,B, and C with the following constraint,

:- use_module(library(clpfd)).
L[A,B,C], L ins 1..3, A#=B+C.

But, it gives an error - Syntax error: Operator expected.

Was it helpful?

Solution

Just answering, so that the question goes off the list of unanswered questions: The error could be also the (:-)/2, not only the missing (=)/2. So the following session wurks:

Welcome to SWI-Prolog (Multi-threaded, 64 bits, Version 7.1.11)
Copyright (c) 1990-2014 University of Amsterdam, VU Amsterdam

?- use_module(library(clpfd)).
%   library(occurs) compiled into occurs 0.00 sec, 14 clauses
%  library(apply_macros) compiled into apply_macros 0.01 sec, 51 clauses
%  library(assoc) compiled into assoc 0.01 sec, 103 clauses
% library(clpfd) compiled into clpfd 0.12 sec, 2,694 clauses
true.

?- L=[A,B,C], L ins 1..3, A#=B+C.
L = [A, B, C],
A in 2..3,
B+C#=A,
B in 1..2,
C in 1..2.

In the above we only got as far as stating a problem including equations and variable ranges. To enumerate solutions one has to use the label/2 predicate as well:

?- L=[A,B,C], L ins 1..3, A#=B+C, label(L).
L = [2, 1, 1],
A = 2,
B = C, C = 1 
L = [3, 1, 2],
A = 3,
B = 1,
C = 2 
L = [3, 2, 1],
A = 3,
B = 2,
C = 1.

Bye

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top