SWI-Prolog 选项处理
我正在使用 SWI-Prolog,并且很困惑为什么要编写选项库来给出以下输出:
?- option(a(A), [a=1, a=2, a(3)]).
A = 3.
?- option(b(B), [b=1, b=2]).
B = 1.
我期望 A=1 ...不过,查看选项库代码,这个结果显然是有意的(git 链接),但为什么不是这样一个错误?
option(Opt, Options) :- % make option processing stead-fast
arg(1, Opt, OptVal),
nonvar(OptVal), !,
functor(Opt, OptName, 1),
functor(Gen, OptName, 1),
option(Gen, Options),
Opt = Gen.
option(Opt, Options) :-
get_option(Opt, Options), !.
get_option(Opt, Options) :-
memberchk(Opt, Options), !.
get_option(Opt, Options) :-
functor(Opt, OptName, 1),
arg(1, Opt, OptVal),
memberchk(OptName=OptVal, Options), !.
I am using SWI-Prolog and am confused why the option library would be written to give the following outputs:
?- option(a(A), [a=1, a=2, a(3)]).
A = 3.
?- option(b(B), [b=1, b=2]).
B = 1.
I would expect A=1 ... Looking through the option library code though, this result is clearly intended (git link), but why is this not a bug?
option(Opt, Options) :- % make option processing stead-fast
arg(1, Opt, OptVal),
nonvar(OptVal), !,
functor(Opt, OptName, 1),
functor(Gen, OptName, 1),
option(Gen, Options),
Opt = Gen.
option(Opt, Options) :-
get_option(Opt, Options), !.
get_option(Opt, Options) :-
memberchk(Opt, Options), !.
get_option(Opt, Options) :-
functor(Opt, OptName, 1),
arg(1, Opt, OptVal),
memberchk(OptName=OptVal, Options), !.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于您引用的代码中使用了memberchk/2(这是半确定性的,即它最多成功一次),因此非确定性(A=1;A=2;等等)似乎明确不 有意。如果有的话,矛盾的选项可能会引发域错误,不是吗?
Since memberchk/2 (which is semi-deterministic, i.e., it succeeds at most once) is used in the code you quote, non-determinism (A=1 ; A = 2 ; etc.) seems explicitly not intended. If anything, contradictory options should maybe raise a domain error, no?
该文档说 Name = Value 语法已被弃用,取而代之的是 Name(Value) 语法。
通过首先检查首选形式,在
library(option)
代码中表示这一点似乎是合理的。The documentation says the Name = Value syntax is deprecated in favor of the Name(Value) syntax.
It seems reasonable this would be represented in the
library(option)
code by checking first for the preferred form.