www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Why CTFE is context-sensitive?

reply "Pierre Talbot" <ptalbot**mustberemoved** hyc.**thisto**io> writes:
Hi,

I was wondering why CTFE is context sensitive, why don't we check
every expressions and run the CTFE if it applies?
Jan 26 2014
next sibling parent reply "Martin Cejp" <minexew gmail.com> writes:
On Sunday, 26 January 2014 at 09:59:01 UTC, Pierre Talbot wrote:
 Hi,

 I was wondering why CTFE is context sensitive, why don't we 
 check
 every expressions and run the CTFE if it applies?
Speed?
Jan 26 2014
parent "Pierre Talbot" <ptalbot**mustberemoved** hyc.**thisto**io> writes:
On Sunday, 26 January 2014 at 12:52:38 UTC, Martin Cejp wrote:
 On Sunday, 26 January 2014 at 09:59:01 UTC, Pierre Talbot wrote:
 Hi,

 I was wondering why CTFE is context sensitive, why don't we 
 check
 every expressions and run the CTFE if it applies?
Speed?
Hmm, I knew I shouldn't have duplicated this post! The speed is certainly a factor but an option could have been added to the compiler to enable context-sensitivity in case we'd like the compiler to be faster (or at least, an option to enable context-insensivity). So it doesn't sound to me to be the only reason.
Jan 26 2014
prev sibling next sibling parent reply Timon Gehr <timon.gehr gmx.ch> writes:
On 01/26/2014 10:59 AM, Pierre Talbot wrote:
 Hi,

 I was wondering why CTFE is context sensitive, why don't we check
 every expressions and run the CTFE if it applies?
When does it apply?
Jan 26 2014
parent reply "Pierre Talbot" <ptalbot**mustberemoved** hyc.**thisto**io> writes:
On Sunday, 26 January 2014 at 13:48:47 UTC, Timon Gehr wrote:
 On 01/26/2014 10:59 AM, Pierre Talbot wrote:
 Hi,

 I was wondering why CTFE is context sensitive, why don't we 
 check
 every expressions and run the CTFE if it applies?
When does it apply?
According to http://dlang.org/function.html#interpretation * initialization of a static variable * dimension of a static array * argument for a template value parameter As shown in the examples: int square(int i) { return i * i; } void foo() { writeln(square(4)); // run time }
Jan 26 2014
next sibling parent "Pierre Talbot" <ptalbot**mustberemoved** hyc.**thisto**io> writes:
On Sunday, 26 January 2014 at 15:06:30 UTC, Pierre Talbot wrote:
 On Sunday, 26 January 2014 at 13:48:47 UTC, Timon Gehr wrote:
 On 01/26/2014 10:59 AM, Pierre Talbot wrote:
 Hi,

 I was wondering why CTFE is context sensitive, why don't we 
 check
 every expressions and run the CTFE if it applies?
I start to think that it was mostly for a matter of convenience, if we use CTFE everywhere, we need a way to desactivate it (for example for code depending on endianness or specific floating arithmetic model). I read somewhere that it was really nice to not introduce new keywords (maybe needed if we want to explicitly deactivate this optimization). Through it's just pure speculation and I'd be glad to have the real reason.
Jan 26 2014
prev sibling parent Timon Gehr <timon.gehr gmx.ch> writes:
On 01/26/2014 04:06 PM, Pierre Talbot wrote:
 On Sunday, 26 January 2014 at 13:48:47 UTC, Timon Gehr wrote:
 On 01/26/2014 10:59 AM, Pierre Talbot wrote:
 Hi,

 I was wondering why CTFE is context sensitive, why don't we check
 every expressions and run the CTFE if it applies?
When does it apply?
According to http://dlang.org/function.html#interpretation * initialization of a static variable * dimension of a static array * argument for a template value parameter
* initializer of 'enum' constant. * argument to pragma(msg, ...) * argument to string mixin * static if condition * static assert condition * Length of fixed-length array * Index and slice indices into template argument list * ...
 As shown in the examples:

 int square(int i) {
    return i * i;
 }

 void foo() {
    writeln(square(4)); // run time
 }
This is what we are already doing, so it makes no sense to ask why we don't do it. I was asking what would be the alternative criterion.
Jan 26 2014
prev sibling next sibling parent "yazd" <yazan.dabain gmail.com> writes:
On Sunday, 26 January 2014 at 09:59:01 UTC, Pierre Talbot wrote:
 Hi,

 I was wondering why CTFE is context sensitive, why don't we 
 check
 every expressions and run the CTFE if it applies?
The halting problem possibly?
Jan 26 2014
prev sibling parent reply =?UTF-8?B?U2ltZW4gS2rDpnLDpXM=?= <simen.kjaras gmail.com> writes:
On 2014-01-26 09:59, Pierre Talbot wrote:> Hi,
 I was wondering why CTFE is context sensitive, why don't we check
 every expressions and run the CTFE if it applies?
Mostly because it's not necessary, and takes more time than simply compiling it. For an optimization step, opportunistic CTFE would certainly be possible (it is, after all, simply constant folding on steroids). The situations in which CTFE is attempted today are when it's explicitly required - either because the value is used at compile time, or because it's assigned to a manifest constant or static variable (which are defined to be required to be known at compile time). This means the compiler does not need to spend inordinate amounts of time testing to see if the code compiles with CTFE, and that it can give sensible error messages when something cannot be CTFE'd. If instead opportunistic CTFE were the norm, you would need to disassemble your program to see if the compiler did in fact CTFE the code that you think it should. -- Simen
Jan 26 2014
parent reply "Pierre Talbot" <ptalbot**mustberemoved** hyc.**thisto**io> writes:
On Monday, 27 January 2014 at 01:39:47 UTC, Simen Kjærås wrote:
 On 2014-01-26 09:59, Pierre Talbot wrote:> Hi,
 I was wondering why CTFE is context sensitive, why don't we
check
 every expressions and run the CTFE if it applies?
Mostly because it's not necessary, and takes more time than simply compiling it. For an optimization step, opportunistic CTFE would certainly be possible (it is, after all, simply constant folding on steroids). The situations in which CTFE is attempted today are when it's explicitly required - either because the value is used at compile time, or because it's assigned to a manifest constant or static variable (which are defined to be required to be known at compile time). This means the compiler does not need to spend inordinate amounts of time testing to see if the code compiles with CTFE, and that it can give sensible error messages when something cannot be CTFE'd. If instead opportunistic CTFE were the norm, you would need to disassemble your program to see if the compiler did in fact CTFE the code that you think it should. -- Simen
We could do opportunistic CTFE by default, and if the user want to ensure it is done, instead of disassemble the program, he could just use the usual techniques (static, ...). But ok, what I wanted to hear is that it is theoretically possible but technically problematic. Thanks to all, Pierre
Jan 27 2014
parent Brad Roberts <braddr puremagic.com> writes:
On 1/27/14 10:40 AM, Pierre Talbot wrote:
 On Monday, 27 January 2014 at 01:39:47 UTC, Simen Kjærås wrote:
 On 2014-01-26 09:59, Pierre Talbot wrote:> Hi,
 I was wondering why CTFE is context sensitive, why don't we
check
 every expressions and run the CTFE if it applies?
Mostly because it's not necessary, and takes more time than simply compiling it. For an optimization step, opportunistic CTFE would certainly be possible (it is, after all, simply constant folding on steroids). The situations in which CTFE is attempted today are when it's explicitly required - either because the value is used at compile time, or because it's assigned to a manifest constant or static variable (which are defined to be required to be known at compile time). This means the compiler does not need to spend inordinate amounts of time testing to see if the code compiles with CTFE, and that it can give sensible error messages when something cannot be CTFE'd. If instead opportunistic CTFE were the norm, you would need to disassemble your program to see if the compiler did in fact CTFE the code that you think it should. -- Simen
We could do opportunistic CTFE by default, and if the user want to ensure it is done, instead of disassemble the program, he could just use the usual techniques (static, ...). But ok, what I wanted to hear is that it is theoretically possible but technically problematic. Thanks to all, Pierre
One of the problems is on the opposite side, ensuring it's NOT done when you don't want it to be. Consider an example where you have a blob of obfuscated data and a de-obfuscation routine. It _could_ be executed at compile time, but that defeats the point. What we have right now puts the control in the hands of the developer.
Jan 27 2014