Order of unknowns in Prolog constraint logic programming (clpr)

374 Views Asked by At

I have:

:-use_module(library(clpr)).
comp(X, Y, Z):-
    {X = Y * Z, Y = Z, Y > 0, Z > 0}.

Which with the query:

?-comp(X,3,Z).

Yields:

X = 9.0,
Z = 3.0

as expected. But why doesn't

comp(9,Y,Z).

also give me values for Y and Z? What I get is instead:

{Z>0.0,Y=Z,9-Y*Z=0.0},
{9-Y*Z=0.0},
{9-Y*Z=0.0}

Thanks!

2

There are 2 best solutions below

0
On BEST ANSWER

Better with constraints over finite domains using this module:

:-use_module(library(clpfd)).
comp(X, Y, Z):-
    X #= Y * Z, Y #= Z, Y #> 0, Z #> 0.

With

comp(9,Y,Z).

I get:

Y = Z, Z = 3
1
On

Probably a weakness of the used CLP(R) that quadratic case doesn't work so well. After Y = Z, it is evident that X = Y**2, and then with X = 9 and Y > 0, you should easily get Y = 3. Which CLP(R) do you use?

A CLP(R) need not only support linear equalities and inequalities. Using for example Gröbner Basis algorithm a CLP(R) could do more, even algebraically. Some computer algebra system can do that easily.

So I guess its not a problem of Prolog per se, rather of the library. Strictly speaking CLP(X) only indicates a domain X. For the domain R of real numbers there is wide variety of potential equation and inequation solvers.