SWI 序言:是否可以撤消回溯?

问题描述

是否可以在 SWI Prolog 中定义一个类似于 Sicstus Prolog 的 undo 谓词?来自 Sicstus 的描述:

undo(:Goal)
The goal call(Goal) (see section Control) is executed on backtracking. This predicate is useful if the Goal performs some side-effect that must be done on backtracking to undo another side-effect.

SWI 谓词 setup_call_cleanup 不会做,因为它会立即进行清理,而不是等到回溯。

解决方法

您如何看待在回溯时调用目标:

undo(Goal) :- true.
undo(Goal) :- call(Goal).

我无法访问 Sictus。