[英]Java 8 lambda and alpha equivalence
I am wondering, is any nice way (if it is possible at all) to implement an alpha-equivalence comparison in Java-8? 我想知道,在Java-8中实现alpha等价比较是否有任何好方法(如果可能的话)?
Obviously these two lambda-s are alpha-equivalent. 显然,这两个lambda-s是α等价的。 Let us suppose that for some circumstances we want to detect this fact. 让我们假设在某些情况下我们想要发现这个事实。 How it can be achieved? 如何实现?
Predicate<Integer> l1 = x -> x == 1;
Predicate<Integer> l2 = y -> y == 1;
I'm going out on a limb with this answer, but it may be worth mentioning this: 这个答案让我走出困境,但值得一提的是:
There is no way to do this. 没有办法做到这一点。 As Brian Goetz pointed out in an answer to a related question , there are no specified, reliable ways of obtaining the "contents" of a lambda, in that sense. 正如Brian Goetz在回答相关问题时指出的那样,在这个意义上,没有明确,可靠的方法来获得lambda的“内容”。
But (and now comes the vague, handwaving part) : 但是(现在是模糊的,手工操作的部分):
There is no way to do this yet . 有没有办法做到这一点呢 。
It might be possible to do this in the future. 将来有可能这样做。 Maybe not with Java 9, but later. 也许不是Java 9,但后来。 The Project Panama has ambituous goals, among them, giving the developer a deeper access to lambdas, aiding in further (runtime) optimizations, translations and processing. 巴拿马项目有一些目标,其中包括让开发人员更深入地访问lambda,帮助进一步(运行时)优化,翻译和处理。
And recently, Radosław Smogura posted in the mailing list : 最近, RadosławSmogura发布在邮件列表中 :
I try to capture lambda expression to get them as expression tree during runtime. 我尝试捕获lambda表达式,以便在运行时将它们作为表达式树。 I'm able to do it for simple lambdas like (o) -> (var == var) && ((varX == varX) && (someField + 1 == 1)), so later user can use (missing) API to examine tree. 我能够为简单的lambda做这样的事情,如(o) - >(var == var)&&((varX == varX)&&(someField + 1 == 1)),以便后来的用户可以使用(缺少)API检查树。
Right now tree can be accessed with code like this: 现在可以使用以下代码访问树:
Method m = BasicMatadataTest.class.getDeclaredMethod("lambda$meta0"); Expression e = (Expression) m.invoke(null); BinaryExpression top = (BinaryExpression) e; BinaryExpression vars = (BinaryExpression) top.getLefthandExpression(); // represents (var == var) (VariableExpression) vars.getLefthandExpression() // represents first var, and it's reference equal to vars.getRighthandExpression() as it's same variable
... ...
The key point here may be the comment: 这里的关键点可能是评论:
represents first var, and it's reference equal to vars.getRighthandExpression() as it's same variable 表示第一个var,它的引用等于 vars.getRighthandExpression(),因为它是相同的变量
(ebm) (EBM)
So if I understood your question and this mailing list post correctly, then it might be possible to determine the equivalence between such expressions: Comparing the tree structure would be fairly trivial (given the functionality sketched above). 因此,如果我理解你的问题和这个邮件列表正确发布,那么就有可能确定这些表达式之间的等价性:比较树结构将是相当微不足道的(给定上面概述的功能)。 And then it might boil down to treating two VariableExpression
as being "equal", regardless of the actual variable name . 然后它可能归结为将两个VariableExpression
视为“相等”,而不管实际的变量名称 。
The mailing list message points to the repositories: 邮件列表消息指向存储库:
- https://bitbucket.org/radoslaw_smogura/java-lambda-metaexpression-jdk9-langtools/branch/metaexpr https://bitbucket.org/radoslaw_smogura/java-lambda-metaexpression-jdk9-langtools/branch/metaexpr
- https://bitbucket.org/radoslaw_smogura/java-lambda-metaexpresions-jdk/commits/branch/metaexpr https://bitbucket.org/radoslaw_smogura/java-lambda-metaexpresions-jdk/commits/branch/metaexpr
(Disclaimer: I have not tested this, and don't know how to get this running (or whether it works at all) - but to my understanding, it is at least very close to what the question was actually about). (免责声明:我没有对此进行过测试,也不知道如何运行(或者是否可以运行) - 但根据我的理解,它至少非常接近问题的实际内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.