Partially pure (Re: Fully transitive const is not necessary)

Don Clugston dac at nospam.com.au
Thu Apr 3 08:35:48 PDT 2008


Walter Bright wrote:
> Bill Baxter wrote:
>> So does that mean
>>
>>   pure int f(const Class c)
>>   { ...
>>   }
>>
>> will *not* be allowed?  Because some part of c *could* change, even if 
>> it's not f() doing the changing.  I.e. another thread could be 
>> changing c concurrently.
> 
> Right. That declaration of f is erroneous.
> 
>> If so, that seems unnecessarily restrictive to me.  Any side effect or 
>> indeterminacy of f() in that case is not because of f()'s doing.  So 
>> f()'s behavior *is* pure even if it takes a const argument.  It's not 
>> f's fault if the environment it's living in is unstable.
> 
> A function is pure or it isn't, there really isn't any room for 
> "unstable" purity, or the point of purity is lost.

I think there is. A function which accesses a global is inherently impure. But, 
a function which takes a non-invariant argument, can in theory safely be called 
from inside a pure function, provided any non-invariant arguments are local 
variables, or invariant.

eg.

class C
{
   int numcalls;
   this() { numcalls=0; }
   void foo() { ++numcalls; } // Not pure - but no global side effects.
}

pure int bar(int x)
{
     C c = new C;
     for (int i=0; i<x; ++i) c.foo();
     return c.numcalls;
}

Is bar() pure or not?

Incidentally this type of code currently works in CTFE.


> 
> Look at it another way. You can take the value of the bits pushed on the 
> stack as arguments to a pure function, and compare that with previous 
> bits passed to the same function, and if there's a binary bit match, you 
> can skip calling the function and return a previously cached result.
> 
> If that cannot be done, then the function is NOT pure.
> 
>> Maybe there need to be two levels of pure?  One says this function by 
>> itself has no side effects, the other says additionally that the 
>> function is invariant with respect to the environment.  The former 
>> category of functions can all be run in parallel safely provided there 
>> are no other threads modifying the environment simultaneously.
> 
> No, you cannot run them simultaneously. The problem with const (instead 
> of invariant) is that the transitive state of the passed object is NOT 
> guaranteed to be the same:



More information about the Digitalmars-d mailing list