What does +,+ mode in Prolog mean?

你说的曾经没有我的故事 提交于 2019-12-18 07:01:09

问题


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


回答1:


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)



回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/8283091/what-does-mode-in-prolog-mean

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!