2

I want to write a program in SWI-Prolog that solves equations. I know GNU Prolog and that makes me nervous... What is wrong here?

equation(X1,X2) :-
  {
    2*X1 + 3*X2 =:= 6,
    {X1 is 0; X1 is 1},
    {X2 is 0; X2 is 1}
  }.

X1 and X2 always equal to 0 or 1.

false
  • 10,182
  • 12
  • 93
  • 182
Nickon
  • 8,180
  • 10
  • 52
  • 105

1 Answers1

2

I have a file with

:- [library(clpq)].
eq(X1, X2) :- {2 * X1 + 3 * X2 =:= 6}.

then I compile and run and I get:

?- eq(A,B).
{B=2-2 rdiv 3*A}.

It's the result you're expecting?

edit

?- eq(A,B),A=1.
A = 1,
B = 4 rdiv 3.

?- eq(A,B),B=1.
A = 3 rdiv 2,
B = 1.

Section A.8.3 of the documentation says that unification can hold 'outside' constraints specification. Then you can experiment freely with additional bounding. But if you impose that both A and B will bind, you should choose appropriate values. AFAIK the values you posted initially cannot satisfy the equation.

CapelliC
  • 57,813
  • 4
  • 41
  • 80
  • I want X1 and X2 to be 0 or 1, so I need to specify a domain for them, but I don't know how. In GNU Prolog I'd do it like this: `fd_domain(X1, {0,1})`. In SWI Prolog I can't make it work (I need this prolog, because the factors could be decimal. – Nickon Feb 14 '13 at 09:33