?- prime_suspect( Who, robbery ).
is asked, the rule:
prime_suspect( Person, Crime ):-
crime( Crime, Victim, Time, Place ),
possible_suspect( Person ),
was_at( Person, Time, Place ),
had_motive_against( Person, Victim ).
becomes:
prime_suspect( Person, robbery ):-
The question has specified that the value of Crime should be robbery. Therefore it
cannot be changed. PROLOG is not free, should no suspect be found for robbery, to
substitute some other crime! Since the value is given in the question, it cannot be 'resatisfied'. But the same is not true for variables which are not specified in the question - for example, Person. If Fred is the first possible suspect to be tried (line 3) but line 4 fails (Fred turns out not to have been in the park on Tuesday night) then PROLOG tries to resatisfy the goal represented by line 3. That is, PROLOG tries to find anotherpossible suspect to check as the prime suspect. Assuming that PROLOG tries all the names it has in line 3, and they all fail at line 4 (that is, none of them were in the park on Tuesday night), then PROLOG backtracks and tries to resatisfy the previous goal (line 2). The value of the variable
and tries all the possible suspects again as candidates for this robbery.
In Figure 13, a solid arrow represents a request for information by a particular goal (e.g. the first such arrow below represents a call by the second line of the program for values to fill the variables Victim, Time and Place).
As indicated in Figure 13, when backtracking in this way, PROLOG remembers which predicates it has tried in the database. In this way, it avoids getting into a loop in which, say, a suspect who has been investigated once for a particular crime unsuccessfully is investigated again and again. Only when all possible answers have been tried and have failed does the head of the prime_suspect rule fail.
This is because there is a second prime_suspect predicate in our program. PROLOG automatically tries the next matching predicate if a previous one fails. Just as it tries the next person if the previous one is found not to qualify as a prime suspect, so PROLOG does the same with rules. This means that if no-one were found to be the prime suspect (if all the names failed) then PROLOG would look for another prime_suspect predicate (fact or rule) which matches the question.
(since it works from top to bottom in the database). This matches the question (it has two arguments) and so it is tried (Figure 14).
Since the second prime_suspect predicate is a fact not a rule, it has no conditions which must be satisfied. Since it matches, the goal is satisfied. PROLOG therefore
replies to our question as indicated in Figure 15.
We can have many alternative predicates (facts and rules), each to be tried should the previous one fail. But recall that PROLOG tries the topmost predicates in the database first, and works downwards. Therefore if we had placed the second prime_suspect predicate just described above the other one, then PROLOG would have come to it first. Since it matches our original question, the lower-placed predicate would never be tried, and PROLOG would reply:
even if the other predicate could successfully have found a prime suspect!
crime( robbery, jim, wednesday, pub ).
Let us see how this works in detail. We will assume that Fred was not in the park on Tuesday night, and that we have retracted (deleted) from the database the fact:was_at( fred, tuesday_night, park ).
A broken arrow represents the supply of information to the goal which called it. In the diagram below, the rule is listed out in full at each stage - as it appears when calling for information, then again when receiving that information.
A double-thickness arrow represents backtracking to try to resatisfy a previous goal. The stages the rule goes through as it is processed are shown in Figure 13.
Figure 13
However, assuming that all possible suspects are investigated and they all fail, PROLOG will not simply reply:no
In this case, PROLOG would come to the second predicate:prime_suspect( unknown, Crime ).
Figure 14
Figure 15
Person = unknown
Return to the top of this page
Return to the introductory page
Return to the previous section: Rules
Go on to the next section: The 'cut'