prologclpq

Equation solver in SWI-Prolog


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.


Solution

  • 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.