UniX
UniX

Reputation: 15

Why does the unification not work ? Prolog

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?

Upvotes: 1

Views: 59

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476557

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.

Upvotes: 2

Related Questions