Home > Mobile >  Why Prolog can't see the obvious answer?
Why Prolog can't see the obvious answer?

Time:12-10

f(9) = f(A), X = A, X = T * T

Why prolog cant see A = 9, X = 9, T = 3/-3, am I making a syntax error here? If so what is the correct form of this query? I am getting false, but there is obviously an easy answer to this equation.

CodePudding user response:

in one sense you are making an interesting error because you are magically expecting prolog to be able to deduce a solution from a constraint (X=T*T) (NOTE: that's a good thing, that's a very prolog expectation). there are two things not working here

  • to get prolog to understand you want to evaluate a math expression you need to use "is" instead of equal
  • you need to give prolog a hint about what values might work for T

here is something in the neighborhood of what you were trying that will work

f(9) = f(A), X = A, between(-100,100,T), X is T * T

CodePudding user response:

Besides the good solution already proposed by @madeofmistake, another alternative is to use the library for constraint logic programming over finite domains (clpfd). After loading this library, you can use the operator #= to evaluate arithmetic constraints.

?- use_module(library(clpfd)).
true.

?- f(9) = f(A), X = A, X #= T * T.
A = X, X = 9,
T in -3 \/ 3.
  • Related