本文介绍了太多的回溯:为什么会有“重做"?这里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Prolog 中做一个非常简单的练习,在跟踪中有一些我不明白的东西.该程序是对表示为后继的整数的大于"(>):

I'm doing a very simple exercise in Prolog and there's something I don't understand in the trace. The program is a "greater than" (>) on integers represented as successors:

greater_than(succ(_), 0).
greater_than(succ(A), succ(B)) :-
  greater_than(A, B).

我的问题:我不明白为什么请求 greater_than(succ(succ(succ(0))),succ(0)) 会在以下跟踪:

My problem: I don't understand why the request greater_than(succ(succ(succ(0))),succ(0)) generates a redo in the following trace:

[trace] ?- greater_than(succ(succ(succ(0))),succ(0)).
Call: (6) greater_than(succ(succ(succ(0))), succ(0)) ? creep
Call: (7) greater_than(succ(succ(0)), 0) ? creep
Exit: (7) greater_than(succ(succ(0)), 0) ? creep
Exit: (6) greater_than(succ(succ(succ(0))), succ(0)) ? creep
true ;
Redo: (7) greater_than(succ(succ(0)), 0) ? creep
Fail: (7) greater_than(succ(succ(0)), 0) ? creep
Fail: (6) greater_than(succ(succ(succ(0))), succ(0)) ? creep
false.

为什么这里有一个redo?我怎样才能避免它(当然,没有削减)?

Why is there a redo here? How can I avoid it (without a cut, of course)?

顺便说一句,在你问之前:不,这不是某种家庭作业......

BTW, before you ask : no, it's not some kind of homework...

推荐答案

好的,这是一个编译器优化,给定的编译器/版本组合可能有也可能没有.

OK, so it's a compiler optimization that a given compiler/version combination might or might not have.

更高版本的 SWI 没有这个问题.它可能与子句索引有关.这种行为会出现在没有索引的实现上,或者只有第一个参数上的索引.

Later versions of SWI do not have this problem. It is probably related to clause indexing. This behaviour would be seen on implementations without indexing, or such that index on the first argument only.

但显然,"SWI-Prolog 提供了对多个参数的即时"索引".SWI 5.6.56 手册声明最多 4参数可以被索引".所以它可能索引不止一个.

But apparently, "SWI-Prolog provides `just-in-time' indexing over multiple arguments". SWI 5.6.56 manual states that "at most 4 arguments can be indexed". So it probably indexes more than one.

这篇关于太多的回溯:为什么会有“重做"?这里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-07 17:45