Prolog 中的失败谓词有什么用?

发布于 2024-09-04 19:19:00 字数 19 浏览 6 评论 0原文

我想不出我需要它的情况。

I can't come up with a situation where I would need it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

滥情稳全场 2024-09-11 19:19:00

优雅的系统提供 false/0 作为命令式 fail/0 的声明性同义词。它有用的一个例子是,当您手动想要强制回溯副作用时,例如:

?- between(1,3,N), format("line ~w\n", [N]), false.
line 1
line 2
line 3

您还可以使用任何失败的目标,例如更短一点的目标,而不是 false/0

?- between(1,3,N), format("line ~w\n", [N]), 0=1.
line 1
line 2
line 3

因此, false/0 不是严格需要的,但相当不错。

编辑:我有时会看到初学者想要声明“我的关系不适用于空列表”,然后添加:

my_relation([]) :- false。

他们的代码。这是没有必要的,并且不是使用false/0的好例子,除了以编程方式生成的故障切片中的例子。相反,集中精力陈述你们关系中有效的事情。在这种情况下,只需省略整个子句,并仅为非空列表(即至少具有一个元素)定义关系:

my_relation([L|Ls]) :- 等

或者,如果您还描述除列表之外的其他术语,请使用如下约束:

my_relation(T) :- diff(T, []) 等。

仅给出其中一个(或什至两者) ) 这两个子句中,查询 ?- my_relation([]). 将自动失败。没有必要引入一个永远无法达到此目的的附加条款。

Elegant systems provide false/0 as a declarative synonym for the imperative fail/0. An example where it is useful is when you manually want to force backtracking for side-effects, like:

?- between(1,3,N), format("line ~w\n", [N]), false.
line 1
line 2
line 3

Instead of false/0, you can also use any goal that fails, for example a bit shorter:

?- between(1,3,N), format("line ~w\n", [N]), 0=1.
line 1
line 2
line 3

Thus, false/0 is not strictly needed but quite nice.

EDIT: I sometimes see beginners who want to state for example "my relation does not hold for the empty list", and then add:

my_relation([]) :- false.

to their code. This is not necessary, and not a good example of using false/0, except for example in failure slices that are programmatically generated. Instead, concentrate on stating the things that hold about your relation. In this case, just leave out the entire clause, and define the relation only for lists that are not empty, i.e., have at least one element:

my_relation([L|Ls]) :- etc.

or, if you are describing other terms in addition to lists as well, use a constraint like:

my_relation(T) :- dif(T, []), etc.

Given only either (or even both) of these two clauses, the query ?- my_relation([]). will automatically fail. It is not necessary to introduce an additional clause which never succeeds for that purpose.

献世佛 2024-09-11 19:19:00

显式失败。 fail 通常与 cut: ... !, failed. 结合使用以强制失败。

对于所有构造。显式使用 fail/false 来枚举通过回溯是一种非常容易出错的活动。考虑一个例子:

... ( generator(X), action(X), fail ; true ), ...

这个想法是为所有X“执行”操作。但是,如果 action(X) 失败会发生什么?这个结构只是继续下一个候选——就好像什么都没发生一样。以这种方式,某些错误可能会在很长一段时间内未被检测到。

对于这种情况,最好使用 \+ (generator(X), \+ action(X) ) ,如果 action(X) 对于某些 失败,它会失败>X。有些系统将其作为内置的 forall/2 提供。就个人而言,我更喜欢在这种情况下使用 \+ ,因为 \+ 更清楚,该构造不会留下绑定。

故障切片。出于诊断目的,有意将 false 添加到程序中通常很有用。有关更多详细信息,请参阅

Explicit failure. fail is often used in conjunction with cut: ... !, fail. to enforce failure.

For all construct. Explicit usage of fail/false to enumerate via backtracking is a very error prone activity. Consider a case:

... ( generator(X), action(X), fail ; true ), ...

The idea is thus to "do" action for all X. But what happens, if action(X) fails? This construct simply continues with the next candidate — as if nothing happened. In this manner certain errors may remain undetected for very long.

For such cases it is better to use \+ ( generator(X), \+ action(X) ) which fails, should action(X) fail for some X. Some systems offer this as a built-in forall/2. Personally, I prefer to use \+ in this case because the \+ is a bit clearer that the construct does not leave a binding.

Failure-slice. For diagnostic purposes it is often useful to add on purpose false into your programs. See for more details.

软糯酥胸 2024-09-11 19:19:00

一个案例(取自使用 Eclipse 的约束逻辑编程)是 not 的实现/1:

:- op(900, fy, not).
not Q :- Q, !, fail.
not _ .

如果 Q 成功,则 cut (!) 导致第二个 not 子句被丢弃,而失败则确保结果为负。如果 Q 失败,则首先触发第二个 not 子句。

One case (taken from Constraint Logic Programming using Eclipse) is an implementation of not/1:

:- op(900, fy, not).
not Q :- Q, !, fail.
not _ .

If Q succeeds, the cut (!) causes the second not clause to be discarded, and the fail ensures a negative result. If Q fails, then the second not clause fires first.

南冥有猫 2024-09-11 19:19:00

失败的另一个用途是在使用具有副作用的谓词时通过替代方案强制回溯:

writeall(X) :- member(A,X), write(A), fail.
writeall(_).

尽管有些人可能不认为这种特别好的编程风格。 :)

Another use for fail is to force backtracking through alternatives when using predicates with side effects:

writeall(X) :- member(A,X), write(A), fail.
writeall(_).

Some people might not consider this particularly good programming style though. :)

总以为 2024-09-11 19:19:00

fail/0 是一个特殊符号,当 prolog 遇到它作为目标时,它将立即失败。

fail 通常与 CUT(!) 结合使用来强制失败。

like(me,X) :- chess(X),!,fail.
like(me,X) :- games(X).

fail/0 is a special symbol that will immediately fail when prolog encounters it as a goal.

fail is often used in conjunction with CUT(!) to enforce failure.

like(me,X) :- chess(X),!,fail.
like(me,X) :- games(X).
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文