回溯太多:为什么会有“重做"提示?这里? [英] Too much backtracking: why is there a "redo" here?

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

问题描述

我在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))在以下跟踪中生成redo的原因:

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 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.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆