+,+ 是什么意思? Prolog中的模式是什么意思?

发布于 2024-12-18 05:35:01 字数 48 浏览 0 评论 0原文

所以我被告知特定谓词必须在 +,+ 模式下工作。这在 Prolog 中意味着什么?

So am being told a specific predicate has to work in +,+ mode. What does that mean in Prolog?

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

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

发布评论

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

评论(3

想挽留 2024-12-25 05:35:01

当人们想要在序言中提供有关谓词的信息时,经常使用这些约定:

arity : predicate/3 表示谓词需要 3 个参数。

参数:predicate(+Element, +List, -Result) 表示 Element 和 List 不应该是自由变量,而 Result 应该是自由变量,谓词才能正常工作。 ?当两者都可以时使用,上面的答案中提到了@,但实际上并没有那么多使用(至少在 swi-pl 文档中),并且意味着输入在调用期间不会被绑定。

因此,告诉 somepredicate 在 +, + 模式下工作是告诉它的快捷方式:

% somepredicate/2 : somepredicate(+Input1, +Input2)

When one wants to give information on a predicate in prolog, those conventions are often used :

arity : predicate/3 means predicate takes 3 arguments.

parameters : predicate(+Element, +List, -Result) means that Element and List should not be free variables and that Result should be a free variable for the predicate to work properly. ? is used when it can be both, @ is mentionned on the above answer but is not really used as much (at least in swi-pl doc) and means that the input will not be bound during the call.

so telling that somepredicate works in +, + mode is a shortcut for telling that :

% somepredicate/2 : somepredicate(+Input1, +Input2)
生生漫 2024-12-25 05:35:01

为了给您一个明确的答案,您需要告诉我们的不仅仅是+,+。对于参数仅为原子的谓词,事情是明确定义的:p(+,+) 意味着该谓词只能在两个参数都是原子的情况下调用。

但如果我们有列表,事情就会变得更加复杂。这种情况有两层含义。考虑 member/2 ,它对 member(2,[1,2,3]) 是成功的。

查询 member(2,[X])member(2,[X|Xs]) 现在是 +,+ 吗?

ISO Prolog 中也使用的直接解释表示(引用 ISO/IEC 13211-1:1995 中的 8.1.2.2 参数模式):

  • + 参数应被实例化,

在这个意义上上面的两个查询都是+,+。

然而,还有另一种解释,它隐含地假设我们可以访问谓词的定义。这种解释源于 DEC-10 Prolog(最早的 Prolog 系统之一)的模式声明。那么让我们看看 member/2

member(X, [X|_]).
member(X, [_|Xs]) :-
   member(X, Xs).

模式 member(+,+) 现在意味着在执行目标时,此模式将适用于所有 em> 子目标。也就是说,member(2,[X]) 将为 +,+,而 member(2,[X|Xs])不是
因为它的子目标 member(2,Xs)

人们确实经常混淆这些概念。因此,当您谈论列表或其他复合术语时,询问其含义会有所帮助。

有关模式的更多信息,请参阅此答案

In order to give you a definite answer you need to tell us more than just +,+. For predicates whose arguments are only atoms, things are well defined: p(+,+) means that the predicate should only be called with both arguments being atoms.

But if we have, say lists, things are more complex. There are two meanings in that case. Consider member/2 which succeeds for member(2,[1,2,3]).

Are the queries member(2,[X]) or member(2,[X|Xs]) now +,+ or not?

The direct interpretation which is also used in ISO Prolog says that (quoting 8.1.2.2 Mode of an argument, from ISO/IEC 13211-1:1995):

  • + the argument shall be instantiated,

In that sense both queries above are +,+.

However, there is another interpretation which implicitly assumes that we have access to the definition of the predicate. This interpretation stems from the mode declarations of DEC-10 Prolog, one of the first Prolog systems. So lets look at member/2:

member(X, [X|_]).
member(X, [_|Xs]) :-
   member(X, Xs).

A mode member(+,+) would now mean that when executing a goal, this mode will hold for all subgoals. That is, member(2,[X]) would be +,+ whereas member(2,[X|Xs]) is not
because of its subgoal member(2,Xs).

People do confuse these notions quite frequently. So when you are talking about lists or other compound terms, it helps to ask what is meant.

For more on modes see this answer.

倾城花音 2024-12-25 05:35:01

这意味着谓词的参数都将是输入参数(尽管不是纯输入)。

此页面对 Prolog 的所有调用进行了简洁的描述模式。

It means that the arguments to the predicate will both be input arguments (though not pure input).

This page has a succint description of all of Prolog's call modes.

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