简体   繁体   English

Prolog-错误:在Linux中超出本地堆栈

[英]Prolog - ERROR: Out of local stack in linux

I have a very simple program in swipl 我在swipl中有一个非常简单的程序

edge(X,Y) :- edge(X,Z),edge(Z,Y).
edge(a,b).
edge(a,f).
edge(b,c).
edge(c,d).
edge(g,c).
edge(f,g).
edge(f,c).
edge(f,e).
edge(c,e).
edge(e,d).

But when Ι make a query edge(a,c). 但是当Ι进行查询edge(a,c). Ι get a Out of local stack exception. 我得到本地堆栈外异常。 The strange thing is that when I do the same query in windows the program works perfectly. 奇怪的是,当我在Windows中执行相同的查询时,程序运行完美。

I tried to increase the local stack but simply the program takes longer to throw the exception. 我试图增加本地堆栈,但是简单地,该程序需要更长的时间来引发异常。

To actually see what is the problem consider the following : 要实际查看问题所在,请考虑以下

edge(X,Y) :- edge(X,Z), false, edge(Z,Y).
edge(a,b) :- false.
edge(a,f) :- false.
edge(b,c) :- false.
edge(c,d) :- false.
edge(g,c) :- false.
edge(f,g) :- false.
edge(f,c) :- false.
edge(f,e) :- false.
edge(c,e) :- false.
edge(e,d) :- false.

If this fragment of your program does not terminate, then also your original program does not terminate. 如果您的程序片段没有终止,那么您的原始程序也不会终止。 Look at what remained! 看看还剩下什么! A single recursion, where the arguments are either ignored ( Y ) or passed around ( X ). 单个递归,其中参数被忽略( Y )或传递( X )。 Thus, there is no way whatsoever that this program terminates. 因此,该程序无法终止。 In other words: Your program terminates never. 换句话说:您的程序永不终止。

Your predicate defines a lovely infinite loop. 您的谓词定义了一个可爱的无限循环。 It just sits there and calls itself and never even tries the facts since the predicate is first and is named the same as the facts. 它只是坐在那里自称,甚至从不尝试事实,因为谓词是第一个,并且与事实的名称相同。

What will help is: (1) assert the facts before the predicate, (2) don't define your predicate with the same name as the facts (technically, it doesn't mean the same thing so why should it have the same name?), and (3) in the recursive clause, check a pre-defined edge before the recursive call. 会有所帮助的是:(1)在谓词之前断言事实,(2)不要用与事实相同的名称来定义您的谓词(从技术上讲,它并不意味着相同的事物,所以为什么它应该具有相同的名称?),以及(3)在递归子句中,在递归调用之前检查预定义的边。

edge(a,b).
edge(a,f).
edge(b,c).
edge(c,d).
edge(g,c).
edge(f,g).
edge(f,c).
edge(f,e).
edge(c,e).
edge(e,d).

connected(X, Y) :-
    edge(X, Y).
connected(X, Y) :-
    edge(X, Z), connected(Z, Y).

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

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