www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Compare lambda expressions

reply Alex <sascha.orlov gmail.com> writes:
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 :)
Apr 26 2016
next sibling parent Meta <jared771 gmail.com> writes:
There's no way to do this in D. There have been several proposals 
but nothing implemented as of yet.
Apr 26 2016
prev sibling parent reply cym13 <cpicard openmailbox.org> writes:
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.
Apr 26 2016
parent Alex <sascha.orlov gmail.com> writes:
On Wednesday, 27 April 2016 at 06:59:22 UTC, cym13 wrote:
 On Tuesday, 26 April 2016 at 14:32:59 UTC, Alex wrote:

 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.
Ok! Thanks!
 The bulk of the problem doesn't have much to do with D itself 
 though.
Yes, I see... The working idea from early in the morning is the following: Force this by the structure of the input file itself: E. g. in the json file, make a section, where lambda expressions are defined, then, in sections where they are needed reference them. This would reduce repeated definitions and would obsolete the need for comparison...
Apr 27 2016