Why CTFE is context-sensitive?

Xinok xinok at live.com
Mon Jan 27 13:02:18 PST 2014


On Monday, 27 January 2014 at 18:30:43 UTC, Pierre Talbot wrote:
> On Monday, 27 January 2014 at 04:07:04 UTC, Andrei Alexandrescu 
> wrote:
>> On 1/26/14 3:22 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?
>>
>> Compilation would get awfully slow (and sometimes won't 
>> terminate).
>>
>> Andrei
>
> So it is theoretically possible? I mean if the compilation 
> doesn't terminate, the execution won't either for at least one 
> program input, so we can detect an infinite loop at 
> compile-time. Moreover, isn't the same problem with 
> context-sensitive CTFE?
>
> Pierre

Just because code runs for a long time doesn't mean that it will 
run forever. This is known as the Halting problem [1]. We could 
have configurable switches to halt CTFE if it runs for more than 
N seconds or allocates more than N MiB.

However, this still doesn't take away from the point that it 
would make compilation awfully slow with presumably little 
benefit. There are further issues in that CTFE isn't exactly 
stable yet (I've had it produce incorrect results in a few 
cases), so relying on it to produce correct, optimized code is a 
bad idea.

I would be in favor of some "lite-CTFE" for the sake of 
optimization. Simply halting if the interpreter encounters a loop 
or function recursion should make it pretty snappy. But I think 
many compilers / optimizers already do this to some extent.

[1] https://en.wikipedia.org/wiki/Halting_problem


More information about the Digitalmars-d mailing list