问题
likes(tom,jerry).
likes(mary,john).
likes(mary,mary).
likes(tom,mouse).
likes(jerry,jerry).
likes(jerry,cheese).
likes(mary,fruit).
likes(john,book).
likes(mary,book).
likes(tom,john).
likes(john,X):-likes(X,john), X\=john.
Hi there, above is a very simple prolog file, with some facts and only one rule: John likes anyone who likes him. But after loading this file and ask Prolog the following query:
likes(john,X).
The program crashes. The reason is somehow prolog gets stuck at likes(john,john)
even though the rule states that X\=john
.
Any advice?
回答1:
Ironically, given the site we're on, you're getting a stack overflow.
It does this because of the order of execution that prolog uses, it's going to go into an infinite recursion at likes(X,john)
in your rule, it activates the rule again - not a fact - never getting to the X\=john
bit.
One way to fix this is to have your rule named differently from your fact like this:
kindoflikes(tom,jerry).
kindoflikes(mary,john).
kindoflikes(mary,mary).
kindoflikes(tom,mouse).
kindoflikes(jerry,jerry).
kindoflikes(jerry,cheese).
kindoflikes(mary,fruit).
kindoflikes(john,book).
kindoflikes(mary,book).
kindoflikes(tom,john).
likes(Y,X):- kindoflikes(X,Y), X\=Y.
likex(Y,X):- kindoflikes(Y,X), X\=Y.
Note the reversal of X and Y in the kindoflikes in the two rule definitions. So you get:
?- likes(john,X).
X = mary ;
X = tom ;
X = book.
But you're not locked into finding what john likes, and you can do:
?- likes(jerry,X).
X = tom ;
X = cheese.
回答2:
Your first question was why your program crashes. I am not sure what kind of Prolog system you are using, but many systems produce a clean "resource error" which can be handled from within Prolog.
Your actual problem is that your program does not terminate for the query likes(john, X)
. It gives you the expected answers and only then it loops.
?- likes(john,X). X = book ; X = mary ; X = tom ; ERROR: Out of local stack
You have been pretty lucky that you detected that problem so rapidly. Imagine more answers, and it would have not been that evident that you have the patience to go through all answers. But there is a shortcut for that. Ask instead:
?- likes(john, X), false.
This false
goal is never true. So it readily prevents any answer. At best, a query with false
at the end terminates. Currently this is not the case. The reason for this non-termination is best seen when considering the following failure-slice (look up other answers for more details):
?- likes(john,X), false.likes(tom,jerry) :- false.likes(mary,john) :- false.likes(mary,mary) :- false.likes(tom,mouse) :- false.likes(jerry,jerry) :- false.likes(jerry,cheese) :- false.likes(mary,fruit) :- false.likes(john,book) :- false.likes(mary,book) :- false.likes(tom,john) :- false. likes(john,X) :- likes(X,john), false,X\=john.
So it is this tiny little part of your program that is responsible for the stack overflow. To fix the problem we have to do something in that tiny little part. Here is one: add a goal dif(X, john)
such that the rule now reads:
likes(john,X) :- dif(X, john), likes(X,john).
dif/2
is available in many Prolog systems like: SICStus, SWI, YAP, B, IF.
来源:https://stackoverflow.com/questions/10360223/why-does-prolog-crash-in-this-simple-example