1
votes

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?

1
But m(2 * 3 + 4) = m(X + Y) does work! - false
True, but m(2 * 3 + 4) = m(X * Y) does not, and I dont know why. - UniX
The * is evaluated before + by BODMAS rule - Pratham
Are you sure ? I thought Prolog does only care about lexicographic equality. - UniX
Say write_canonical( m(2 * 3 + 4) = m(X * Y) ) to see how operators resolve. - false

1 Answers

2
votes

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.