pure functions cannot be removed, actually: pure vs. total

Steven Schveighoffer schveiguy at yahoo.com
Mon Jun 11 18:45:27 UTC 2018


On 6/11/18 2:23 PM, Timon Gehr wrote:
> On 11.06.2018 20:15, Steven Schveighoffer wrote:
>> On 6/11/18 10:43 AM, Timon Gehr wrote:
>>> On 11.06.2018 16:39, Timon Gehr wrote:
>>>>
>>>> FeepingCreature's example does not really qualify as a do-nothing 
>>>> loop, because the loop produces a value that you (presumably) later 
>>>> access.
>>>
>>> Hm, ok. The problem is actually there, but it is not that the 'find' 
>>> call will get removed, it is that the loop within find's 
>>> implementation will be seen as doing nothing and producing nothing 
>>> after inlining, breaking find's postcondition.
>>
>> Actually, one thing that can be determined statically is that it will 
>> never return. Essentially, this particular find function (with 
>> predicate inlining) will fold to:
>>
>> while(true) {}
>>
>> Since the final return outside the loop is obviously never reached, 
>> and the return inside the loop cannot be reached.
>>
>> So I would assume this would properly be translated to an infinite loop.
>>
>> I was not expecting that the compiler might eliminate infinite loops 
>> that it can prove are infinite loops, more like it should eliminate 
>> calls that trivially can be proven to do nothing based on the 
>> signature of the function.
>>
> 
> Well, if the language definition is reasonable, then if calls to `void 
> foo()pure @nothrow { while(true){} }` can be elided, so can the 
> `while(true){}` loop itself.

Yes, it does seem that way.

Eliding (or moving) opaque calls in general if you can't see what the 
call is actually doing seems to be a minefield that probably cannot 
reasonably be navigated. The only reasonable elision seems to be for 
pure calls that have already been made so we know they actually return.

I'm curious if Haskell can properly navigate this with its lazy calls, 
or if it is subject to such oddities.

-Steve


More information about the Digitalmars-d mailing list