简体   繁体   English

错误:未处理的异常:超出本地堆栈 - Prolog

[英]ERROR: Unhandled exception: Out of local stack - Prolog

I was trying to write a simple version of length/3 in Prolog. 我试图在Prolog中编写一个简单版本的length/3

If you look at the standard implementation of length/2 it is something as below: 如果你看一下length/2的标准实现,它是如下所示:

length([], 0).
length([Head|Tail], N) :-
    length(Tail, N1),
    N is 1+N1.

While my code uses almost the same idea but I get the error "out of local stack", and I think it happens because of deep recursion. 虽然我的代码使用几乎相同的想法,但我得到错误“超出本地堆栈”,我认为它发生是因为深度递归。 I couldn't figure why. 我无法理解为什么。 And my code: 我的代码:

mylength(_, [], 0).
mylength([H1|T1], [H1copy|T1copy], N) :-
    mylength([H1|T1], [T1copy], N1),
    N is N1 + 1.

Your predicate mylength/3 has the following problem. 你的谓词mylength/3有以下问题。 I assume that you want to run it with the mode mylength(+,-,-) . 我假设你想用模式mylength(+,-,-)运行它。 It is then the case that the first argument is not decending. 那么第一个参数不是下降的情况。

The problem is the second clause, where the first argument [H1|T1] in the head is passed unchanged to the first argument of the same predicate in the body, contrary to what happens in length/2. 问题是第二个子句,其中头部中的第一个参数[H1|T1]更改地传递给正文中同一谓词的第一个参数,这与length / 2中发生的情况相反。 Resulting in infinite recursion, since the given second argument [T1copy] does not match the first clause. 导致无限递归,因为给定的第二个参数[T1copy]与第一个子句不匹配。

The infinite recursion can lead to a memory exhaust, since for example each invocation will generate a new variable for H1copy. 无限递归可能导致内存耗尽,因为例如每次调用都会为H1copy生成一个新变量。 The particular error message depends on the Prolog system. 特定的错误消息取决于Prolog系统。

Bye 再见

PS: That there might be something wrong is aleady seen when consulting the predicate. PS:在咨询谓词时可能会出现问题。 One gets a singleton warning for H1copy . 一个人获得了H1copy的单身警告。

But for example in the newest SWI-Prolog version, the out of local stack error will take some time, since SWI-Prolog does carbage collection. 但是,例如在最新的SWI-Prolog版本中,由于SWI-Prolog进行了垃圾收集,因此本地堆栈错误将耗费一些时间。 After ^C and g I see: 在^ C和g后我看到:

Welcome to SWI-Prolog (Multi-threaded, 64 bits, Version 7.1.16)
Copyright (c) 1990-2014 University of Amsterdam, VU Amsterdam

Action (h for help) ? goals

[552,959] mylength([1, 2, 3], [[]], _G40)
[552,958] mylength('<garbage_collected>', '<garbage_collected>', _G40)
[552,957] mylength('<garbage_collected>', '<garbage_collected>', _G40)
[552,956] mylength('<garbage_collected>', '<garbage_collected>', _G40)
[552,955] mylength('<garbage_collected>', '<garbage_collected>', _G40)

I guess the above means that at the current moment where I have interrupted the Prolog interpreter the call stack had already a depth of 552959, ie half a million! 我想上面的意思是,在我打断Prolog解释器的当前时刻,调用堆栈的深度已经达到552959,即五十万!

After running for several minutes, I get the following error message in SWI-Prolog: ERROR: Out of local stack . 运行几分钟后,我在SWI-Prolog中收到以下错误消息: ERROR: Out of local stack

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

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