所以有人告诉我一个特定的谓词必须在 +,+ 模式下工作。这在 Prolog 中是什么意思?
3 回答
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)
为了给您一个明确的答案,您需要告诉我们的不仅仅是 +,+。对于参数仅为原子的谓词,事情是明确定义的:p(+,+) 意味着只应在两个参数都是原子的情况下调用谓词。
但是如果我们有,比如说列表,事情就更复杂了。在这种情况下有两种含义。考虑member/2
哪个成功member(2,[1,2,3])
。
查询member(2,[X])
或member(2,[X|Xs])
现在是+,+还是不是?
ISO Prolog 中也使用的直接解释说(引用 8.1.2.2 Mode of an argument, from ISO/IEC 13211-1:1995):
+
参数应被实例化,
从这个意义上说,上面的两个查询都是+,+。
但是,还有另一种解释隐含地假设我们可以访问谓词的定义。这种解释源于 DEC-10 Prolog 的模式声明,它是第一个 Prolog 系统之一。所以让我们看看member/2
:
member(X, [X|_]).
member(X, [_|Xs]) :-
member(X, Xs).
模式member(+,+)
现在意味着在执行目标时,该模式将适用于所有子目标。也就是说,member(2,[X])
将是 +,+ 而member(2,[X|Xs])
不是因为
它的 subgoal member(2,Xs)
。
人们确实经常混淆这些概念。因此,当您谈论列表或其他复合术语时,询问其含义会有所帮助。
有关模式的更多信息,请参阅此答案。
这意味着谓词的参数都将是输入参数(尽管不是纯输入)。
该页面对 Prolog 的所有调用模式进行了简洁的描述。