I am trying to unify the following terms in Prolog.
m(2 * 3 + 4) = m(X * Y).
The reaction is "false".
Why? Would X=2 and Y= 3+4 not work?
The operators take precedence into account: the + operator has a lower precedence than the * operator. This thus means that:
m(2 * 3 + 4)
is parsed as:
m((2 * 3) + 4)
or more canonical:
m(+(*(2,3),4))
But this does not follow the pattern:
m(*(X,Y))
hence unification fails.
You can unify this by adding brackets, like:
?- m(2 * (3+4)) = m(X * Y).
X = 2,
Y = 3+4.
m(2 * 3 + 4) = m(X + Y)does work! - falsewrite_canonical( m(2 * 3 + 4) = m(X * Y) )to see how operators resolve. - false