Prolog:超出本地堆栈错误
Prolog: Out of local stack error
考虑以下代码(摘自 Introduction to Prolog by RP Suri):
/* A set of father-child pairs declared */
father("Motilal", "Jawaharlal").
father("Motilal", "Vijayalakshmi").
father("Motilal", "Krishna").
father("Jawaharlal", "Indira").
father("Ranjit", "Tara").
father("Ranjit", "Lekha").
father("Ranjit", "Rita").
father("Feroz", "Sanjay").
father("Feroz", "Rajiv").
father("Sanjay", "Varun").
father("Rajiv", "Rahul").
father("Rajiv", "Priyanka").
/* Mothers declared */
wife_of("Swaruprani", "Motilal").
wife_of("Kamla", "Jawaharlal").
wife_of("Vijayalakshmi", "Ranjit").
wife_of("Indira", "Feroz").
wife_of("Maneka", "Sanjay").
wife_of("Sonia", "Rajiv").
wife_of("Priyanka", "Robert").
/* Predicates declared */
mother(M, C) :- wife_of(M, Hus) , father(Hus, C).
parent(P, C) :- mother(P, C) ; father(P, C).
ancestor(X, Y) :- parent(X, Y).
ancestor(X, Y) :- ancestor(X, Z) , parent(Z, Y).
现在如果我查询
ancestor("Motilal", X)
我得到的输出为:
?- ancestor("Motilal", X).
X = "Jawaharlal" ;
X = "Vijayalakshmi" ;
.
.
.
X = "Priyanka" ;
(这是正确的输出),然后程序停止,几秒钟后我收到消息:
ERROR: Out of local stack
当我查询时发生了类似的事情
ancestor(X, "Motilal").
这应该 return 什么都没有,但是提示再次消失几秒钟,然后出现相同的错误消息。代码中有什么问题?
要了解代码中的问题,无需查看整个程序。相反,只需要查看一小部分导致未终止的原因就足够了:
?- ancestor(X, 'Motilal'), false.
ancestor(X, Y) :- false, parent(X, Y).
ancestor(X, Y) :- ancestor(X, Z) , false, parent(Z, Y).
这个片段已经没有结束,所以整个程序也没有结束。不管你在其他部分写了什么!所以你需要改变剩下的部分。否则,错误将一直存在。
有关更多信息,请参阅 failure-slice。
你看到你需要改变什么了吗?
把目标parent(Z, Y)
放在前面!
顺便说一句,在 Prolog 中,在这种情况下使用单引号原子是很常见的,因为这样效率更高!
考虑以下代码(摘自 Introduction to Prolog by RP Suri):
/* A set of father-child pairs declared */
father("Motilal", "Jawaharlal").
father("Motilal", "Vijayalakshmi").
father("Motilal", "Krishna").
father("Jawaharlal", "Indira").
father("Ranjit", "Tara").
father("Ranjit", "Lekha").
father("Ranjit", "Rita").
father("Feroz", "Sanjay").
father("Feroz", "Rajiv").
father("Sanjay", "Varun").
father("Rajiv", "Rahul").
father("Rajiv", "Priyanka").
/* Mothers declared */
wife_of("Swaruprani", "Motilal").
wife_of("Kamla", "Jawaharlal").
wife_of("Vijayalakshmi", "Ranjit").
wife_of("Indira", "Feroz").
wife_of("Maneka", "Sanjay").
wife_of("Sonia", "Rajiv").
wife_of("Priyanka", "Robert").
/* Predicates declared */
mother(M, C) :- wife_of(M, Hus) , father(Hus, C).
parent(P, C) :- mother(P, C) ; father(P, C).
ancestor(X, Y) :- parent(X, Y).
ancestor(X, Y) :- ancestor(X, Z) , parent(Z, Y).
现在如果我查询
ancestor("Motilal", X)
我得到的输出为:
?- ancestor("Motilal", X).
X = "Jawaharlal" ;
X = "Vijayalakshmi" ;
.
.
.
X = "Priyanka" ;
(这是正确的输出),然后程序停止,几秒钟后我收到消息:
ERROR: Out of local stack
当我查询时发生了类似的事情
ancestor(X, "Motilal").
这应该 return 什么都没有,但是提示再次消失几秒钟,然后出现相同的错误消息。代码中有什么问题?
要了解代码中的问题,无需查看整个程序。相反,只需要查看一小部分导致未终止的原因就足够了:
?- ancestor(X, 'Motilal'), false.ancestor(X, Y) :- false, parent(X, Y). ancestor(X, Y) :- ancestor(X, Z) , false,parent(Z, Y).
这个片段已经没有结束,所以整个程序也没有结束。不管你在其他部分写了什么!所以你需要改变剩下的部分。否则,错误将一直存在。
有关更多信息,请参阅 failure-slice。
你看到你需要改变什么了吗?
把目标
parent(Z, Y)
放在前面!
顺便说一句,在 Prolog 中,在这种情况下使用单引号原子是很常见的,因为这样效率更高!