SWI: ‘+’, ‘-‘, ‘–‘, ‘?’ marks

huangapple go评论50阅读模式
英文:

SWI: '+', '-', '--', '?' marks

问题

有没有关于参数模式指示器如何工作的示例?

SWI文档中有关于它们的内容。

但我不知道它们如何应用。

英文:

Are there any examples of how argument mode indicators work?

There's something about them in SWI documentation.

But I have no idea how they can be applied.

答案1

得分: 2

那些是模式指示器,用于指示在调用目标时应如何使用每个参数的模式以及在目标成功完成时。

它们可以在运行时强制执行(例如使用mode指令),或者仅用作文档目的的提示(例如在SWI的文档中)。

这些指示器的确切含义还取决于您的Prolog处理器。
例如,在SWI中:

  • + 表示在调用目标时参数不应该是自由变量
  • -- 表示在调用目标时参数应该是自由变量,并将在那里实例化

为了将其用于文档目的并保持代码可读性,您可以像这样使用它(示例来自某个旧的SWI实现的sum_list/2):

%!  sum_list(+List, -Sum) is det.
%
%   Sum is the result of adding all numbers in List.
sum_list(Xs, Sum) :-
	sum_list(Xs, 0, Sum).

sum_list([], Sum, Sum).
sum_list([X|Xs], Sum0, Sum) :-
	Sum1 is Sum0 + X,
	sum_list(Xs, Sum1, Sum).
英文:

Those are mode indicators and are used to indicate the mode each argument should be used when calling a goal and upon successful completion of the goal.

They may be enforced on runtime (for example using a mode directive) or just used as a hint of for documentation purposes (for example in the documentation of SWI).

The exact meaning of those indicators also depend on your prolog processor.
For example in SWI:

  • + means that the argument should not be a free variable when calling the goal
  • -- means that the argument should be a free variable when calling the goal and would be instantiated there

To use them for documentation purposes and to keep the code readable you would use it like this (example taken from some old SWI implementation for sum_list/2):

%!  sum_list(+List, -Sum) is det.
%
%   Sum is the result of adding all numbers in List.
sum_list(Xs, Sum) :-
	sum_list(Xs, 0, Sum).

sum_list([], Sum, Sum).
sum_list([X|Xs], Sum0, Sum) :-
	Sum1 is Sum0 + X,
	sum_list(Xs, Sum1, Sum).

huangapple
  • 本文由 发表于 2023年2月14日 01:06:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75439037.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定