10.2 Example I


In this and the following two examples, we will try to match two terms. We will spell out the steps taken by the Prolog system in matching, and refer to the rules given on the previous page.


We can try to match the following two terms:

1) person(Nick, G)


2) person(Nick, gender(male))

Matching the two structures

The two terms are structures, so we invoke rule 2 and see whether they match:

  1. They have the same functor ("person"), so the first requirement is taken care of.
  2. They have the same arity (2), so the second requirement is taken care of.
  3. But do their arguments match, from left to right?

In order to check 3., we try to match the arguments from left to right.

Matching the first argument

Nick (from term 1) is an atomic constant. According to rule 1, constants match other constants if they are the same. This is the case, since Nick (from term 2) is also an atomic constant and is the same as the Nick from term 1.

So we proceed to the next argument. Does "G" match "gender(male)"?

Matching the second argument

According to rule 3, free variables match any constants and structures, and the variables are bound to the value of the constant or structure. "gender(male)" is a structure, so G matches "gender(male)". In addition, G is bound to the value "gender(male)".


Thus the overall result is that the matching succeeds, and G is bound to "gender(male)".


Next, we proceed with the second example out of three.

PrevLite: 10.1 Rules of matching
NextLite: 10.3 Example II

Prev: 10.1 Rules of matching
Up: 10 Matching
Next: 10.3 Example II