Download Prolog Programming A First Course

Transcript
Chapter 9
Modifying the Search Space
extralogical
We describe solutions to various problems of control raised in
chapter ??.
We detail other useful Prolog built-in predicates that are nonlogical.
9.1
A Special Control Predicate
We now present a solution to the practical problems posed in chapter ??
about how to control Prolog’s search strategy. We summarised these issues
as ones of:
• Commit
• Make Determinate
• Fail Goal Now
In each of these cases the solution is to make use of a built-in predicate which
always succeeds —but with a very unpleasant side-effect. This notorious
predicate is known as the cut and written !/0.
The reason why cut (!/0) is so unpleasant are that it effects Prolog’s search
tree. Consequently, by adding a cut, the program’s meaning may change
radically. We sometimes say that a cut that does this is a red cut. On the
other hand, the placing of a cut may not change the intended meaning but
simply junk a part of the search tree where it is known that there is no legal
solution. Such a cut is termed a green cut. The Art of Prolog by Sterling
and Shapiro has a nice section on the cut [Sterling & Shapiro, 1986].
We now go over how to solve the three control problems.
9.1.1
Commit
Assume we want to make Social Security payments. That is, pay(X,Y)
means “pay the sum X to Y”. Assume that we also have this code fragment.
86