Information Technology Reference
In-Depth Information
(
)
2.
Assuming there are already clauses making S
n
true, write clauses to ensure
(
+
)
is true.
So proceed in two steps:
that S
n
1
1.
Write clauses for above (
for those cases where x is above y with no blocks
between them. In this case, block x must be directly on y :
x , y
)
above(X,Y) :- on(X,Y).
2.
Assume the above predicate works properly whenever there are n blocks between
the top and bottom blocks.
Suppose we have x and y with n
1 blocks between them. Then there must be a
z such that x is directly on z , and z is above y , with n blocks between them. That
means that above (
+
z , y
)
will work properly. So write the clause
above(X,Y) :- on(X,Z), above(Z,Y).
Thus, by mathematical induction, the above predicate will do the right thing no
matter how many blocks there are.
4.5 Nonterminating programs
Programs where the same predicate appears in the head and the body of a clause
open up the possibility that the back-chaining will get stuck in a loop and go on
forever. An example is the knowledge base in section 2.5.1 containing the clause
poodle(X) :- poodle(X).
A clause like this is (trivially) true, so one might think at first that it would not hurt
to include it in a Prolog program. But it violates the third programming requirement:
a program must be in a form suitable for back-chaining. As shown in chapter 2, this
poodle clause can cause back-chaining to run forever (unless it is stopped).
But it is not just clauses like this that are problematic. Consider the following
version of opp_sex from the family example:
opp_sex(X,Y) :- male(X), female(Y).
opp_sex(X,Y) :- opp_sex(Y,X).
At first sight, this looks perfect. Both clauses are true, and together they entail that
John is of opposite sex from Jane and that Jane is of opposite sex from John. They are
logically correct.
 
Search WWH ::




Custom Search