HomeContents: |
## 10.2 Example I## IntroductionIn 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. ## ExampleWe can try to match the following two terms: 1) person(Nick, G) with 2) person(Nick, gender(male)) ## Matching the two structuresThe two terms are structures, so we invoke rule 2 and see whether they match: - They have the same functor ("person"), so the first requirement is taken care of.
- They have the same arity (2), so the second requirement is taken care of.
- 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 argumentNick (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 argumentAccording 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)". ## ConclusionThus the overall result is that the matching succeeds, and G is bound to "gender(male)". ## NextNext, we proceed with the second example out of three. PrevLite: 10.1 Rules of matchingNextLite: 10.3 Example IIPrev: 10.1 Rules of matchingUp: 10 MatchingNext: 10.3 Example II |