简体   繁体   English

Prolog“超出本地堆栈”错误

[英]Prolog “Out of local stack” Error

Here is my simple Prolog program: 这是我简单的Prolog程序:

friend(X,Y):-
   knows(X,Y).
friend(X,Z):-
   friend(X,Y),
   friend(Y,Z).

knows(brian,tom).
knows(tom,peter).

If I type the following query 如果我键入以下查询

friend(brian,peter).

Prolog will give the following output: Prolog将提供以下输出:

?- friend(brian,peter).
true 

If a further type a semicolon, Prolog will say: 如果再输入一个分号,Prolog会说:

ERROR: Out of local stack

What am I doing wrong here? 我在这里做错了什么?

The error is in the second clause. 错误在第二个子句中。 It should be instead: 应该改为:

friend(X,Z):-
   knows(X,Y),
   friend(Y,Z).

Otherwise, when you ask Prolog for more solutions, you end up having the friend/2 predicate recursively calling itself without first establishing a knows/2 intermediate relation. 否则,当您向Prolog寻求更多解决方案时,最终您将需要friend/2谓词递归调用自身,而无需先建立一个knows/2中间关系。 You can learn more about the bug in your program by tracing the calls to the friend/2 predicate. 您可以通过跟踪对friend/2谓词的调用来了解有关程序中错误的更多信息。 Try: 尝试:

?- trace, friend(brian,peter).

The understand the source of non-termination in your program it suffices to look at the following failure-slice: 了解您程序中非终止的来源,只需查看以下失败切片即可:

friend(X,Y):- false,
   knows(X,Y).
friend(X,Z):- 
   friend(X,Y), false,
   friend(Y,Z).

knows(brian,tom) :- false.
knows(tom,peter) :- false.

It is because of friend(X, Z) :- friend(X, Y), ... that your program will not terminate. 正是由于friend(X, Z) :- friend(X, Y), ... ,您的程序才会终止。 It will produce answers, here and there, but ultimately it will loop. 它会在任何地方产生答案,但最终会循环。 For more, see . 有关更多信息,请参见

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM