Compare lambda expressions

cym13 via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Apr 26 23:59:22 PDT 2016


On Tuesday, 26 April 2016 at 14:32:59 UTC, Alex wrote:
> Hi all!
>
> Not sure, if this belongs directly to the D language, or it is 
> rather maths, but:
> given to delegate generating functions and a main
>
> auto func1(int arg)
> {
>     bool delegate(int x) dg;
>     dg = (x) => arg * x;
>     return dg;
> }
>
> auto func2(int arg)
> {
>     bool delegate(int x) dg;
>     dg = (x) => arg * x * x / x;
>     return dg;
> }
>
> void main()
> {
>     auto dgf1 = func1(4);
>     writeln(dgf1(2));
>     auto dgf2 = func2(4);
>     writeln(dgf2(2));
>     writeln(dgf1 == dgf2);
> }
>
> the last line gives false, either in this form, as well with 
> .ptr, as well with .funcptr.
>
> The question is, how to say, whether two lambda expressions are 
> the same?
> What if the lambda expressions are predicates? This case would 
> be more important for me...
>
> I understand, that there is only a little hope to get answer 
> other then "impossible", even for the constrained case. In such 
> a case, I have another opportunity:
>
> Say, I define two lambda functions as strings in, say a json 
> file. I'm going to parse them and build functions out of them.
> How to force the input in a unified manner? In such a case, I 
> could compare the given strings with some weird regular 
> expression...
> So, the question for this case is:
> Is there some normalized form of writing a lambda expression? 
> Any literature would be more then sufficient as an answer...
>
> Many thanks in advance :)

IMHO, if you are to parse them from strings then you need a 
parser in order to build an abstract tree, a simpler intermediary 
language to restrict the possible patterns expressing an idea, an 
optimizer to reduce similar expressions to a unique one and then 
you way be able to spot that two given expressions share a common 
implementation and thus have the same effect but with no garanty 
to catch them all. As far as I know it's still a field of 
research.

The bulk of the problem doesn't have much to do with D itself 
though.


More information about the Digitalmars-d-learn mailing list