`read/1` 调用后的 SWI Prolog 回溯行为 [英] SWI Prolog backtracking behavior after `read/1` call

查看:41
本文介绍了`read/1` 调用后的 SWI Prolog 回溯行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看另一个 Prolog 问题在 StackOverflow 上遇到这种情况.

I was looking at another Prolog question on StackOverflow and encountered this situation.

假设我有以下事实:

foo(1).
foo(2).
foo(3).
foo(4).

在 SWI Prolog(7.4.2 版)提示下,我执行了以下操作:

At the SWI Prolog (version 7.4.2) prompt, I exercised this:

2 ?- write('Enter number: '), read(X), nl, foo(Y), Y > X.
Enter number: 1.

X = 1,
Y = 2.

3 ?-

如您所见,SWI Prolog 提供了一种解决方案,不提示其他解决方案(确实存在).它不会回溯.

As you can see, SWI Prolog provides one solution with no prompt for additional solutions (which do exist). It does not backtrack.

在 GNU Prolog(1.4.4 版)中,行为更符合我的预期:

In GNU Prolog (version 1.4.4), the behavior is more what I would expect:

| ?- write('Enter number: '), read(X), nl, foo(Y), Y > X.
Enter number: 1.

X = 1
Y = 2 ? ;

X = 1
Y = 3 ? ;

X = 1
Y = 4

yes
| ?-


感谢@trivelt 将问题简化为:


Thanks to @trivelt for a reduction of the problem to simply:

?- foo(X).           % Backtracks and finds all solutions for X
?- read(_), foo(X).  % Does not backtrack and finds only one solution for X


这是 SWI 7.4.2 版实现中的错误吗?或者这些替代行为是可接受的/预期的行为吗?


Is this a bug in the SWI version 7.4.2 implementation? Or are these alternative acceptable/expected behaviors?

推荐答案

至少,我认为这是 SWI 顶层的严重缺陷,即与 程序可能会以这种意想不到的方式干扰顶层控制.

At the very least, I would regard it as a severe deficit of the SWI toplevel that an interaction with a program may interfere in this unexpected way with the toplevel control.

在 SWI-Prolog 中,这被归档为问题#166:

In SWI-Prolog, this is filed as issue #166:

在顶级提交中意外使用 read/1

SWI 顶层的这个缺点阻止用户在许多实际相关的情况下看到所有解决方案.

This shortcoming of the SWI toplevel prevents users to see all solutions in many cases of practical relevance.

既然 ECLiPSe 已经修复了这个问题,也许有人会在某个时候在 SWI 中修复它.

Since ECLiPSe has already fixed this issue, maybe someone will fix it in SWI too at some point.

这篇关于`read/1` 调用后的 SWI Prolog 回溯行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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