Can maplist([X]>>(test(X,Xs)) see variable in outer in scope?

做~自己de王妃 提交于 2021-01-29 15:52:55

问题


?- 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 (so X1s and X2s are visible insidemaplist/3.
  • X2s and X1s 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

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