SWI-Prolog option processing
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) :-
funct开发者_JS百科or(Opt, OptName, 1),
arg(1, Opt, OptVal),
memberchk(OptName=OptVal, Options), !.
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?
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.
精彩评论