问题
?- maplist([X]>>(member(X,[1,2])),
[1,2]).
true
but:
?- X2s=[1,2],
X1s=[1,2],
maplist([X]>>(member(X,X1s)),
X2s).
X1s = X2s, X2s = [1, 2]
X1s is not grounded, despite being grounded.
Here a notebook testing it: https://swish.swi-prolog.org/p/ungrounded%20in%20map.swinb
What's up with that?
回答1:
Do not get confused by the SWI-Prolog variable-name-aliasing
?- debug(foo),X2s=[1,2],
X1s=[1,2],
maplist([X]>>(debug(foo,"Testing ~q",[X]),member(X,X1s)),X2s).
Warning: foo: no matching debug topic (yet)
% Testing 1
% Testing 2
X2s = X1s, X1s = [1, 2] ;
false.
just means
- The query succeeds with the
X
printed as expected (soX1s
andX2s
are visible insidemaplist/3
. X2s
andX1s
are aliases for the same term (which is true),[1, 2]
(also true).- A retry fails.
Actually, maplist/3
doesn't "shield" from any variables visible in the clause context, but the yall lambda notation has a way of specifying that the lambda expression >>
is referring to variables in the outer context, {}
:
?- debug(foo),X2s=[1,2],
X1s=[1,2],
maplist({X1s}/[X]>>(debug(foo,"Testing ~q",[X]),member(X,X1s)),X2s).
Although the {X1s}
properly doesn't do anything here. I don't know where it does ... "shielding off variables" is not properly done in Prolog (IMHO, it should, especially in bagof/3
and setgof/3
)
来源:https://stackoverflow.com/questions/61763971/can-maplistxtestx-xs-see-variable-in-outer-in-scope