maplist([X]>>(test(X,Xs)) 可以在范围外看到变量吗? [英] Can maplist([X]>>(test(X,Xs)) see variable in outer in scope?

查看:35
本文介绍了maplist([X]>>(test(X,Xs)) 可以在范围外看到变量吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

?- maplist([X]>>(member(X,[1,2])),
        [1,2]).
true

但是:

?- X2s=[1,2],
   X1s=[1,2],
   maplist([X]>>(member(X,X1s)),
           X2s).
X1s = X2s, X2s = [1, 2]

X1s 未接地,尽管已接地.

X1s is not grounded, despite being grounded.

这是一个测试它的笔记本:https://swish.swi-prolog.org/p/ungrounded%20in%20map.swinb

Here a notebook testing it: https://swish.swi-prolog.org/p/ungrounded%20in%20map.swinb

这是怎么回事?

推荐答案

不要被 SWI-Prolog 变量名别名混淆

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.

只是意味着

  • 查询成功,X 按预期打印(因此 X1sX2smaplist/3 中可见>.
  • X2sX1s 是同一个词条的别名(这是对的),[1, 2](也是对的).
  • 重试失败.
  • 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.

实际上,maplist/3 不会屏蔽"子句上下文中可见的任何变量,但是 yall lambda 符号 有一种方法可以指定 lambda 表达式 >> 是指外部上下文中的变量,{}:

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).

尽管 {X1s} 在这里没有做任何事情.我不知道它在哪里...... Prolog 中没有正确完成屏蔽变量"(恕我直言,它应该,尤其是在 bagof/3setgof/3>)

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)

这篇关于maplist([X]>>(test(X,Xs)) 可以在范围外看到变量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆