Prolog works by unification. It tries to make structures the same.

So, for example, if I try to unify [3, X, 5] with [3, 4, 5] that works fine and X = 4 .

In your code you are first saying A = B . That's fine as initially A and B are not instantiated so you are basically tying their destinies to each other - if A is later instantiated then B automatically is too, or visa-versa.

Then you go on to ask A is (7 * (X - 2)) . Now if you have already unified X with 6.5 then this can be evaluated - A is then unified with 7 * (6.5 - 2) or 31.5 . But remember that A was already unified with B , so B now also is 31.5 .

Now you say B is (3 * (X + 4)) . So with X unified to 6.5 and B unified with 31.5 this is the same as 31.5 is (3 * (6.5 + 4)) . The right hand side is 31.5 so this is 31.5 is 31.5 which is true.

So, solving for solve(6.5) works.