[dmd-beta] beta 2

Jonathan M Davis jmdavisProg at gmx.com
Wed Jul 6 14:16:32 PDT 2011


On 2011-07-06 14:09, Walter Bright wrote:
> On 7/6/2011 1:29 PM, Jonathan M Davis wrote:
> > On 2011-07-06 12:50, Jonathan M Davis wrote:
> >> But they're supposed to be weakly pure, because they don't alter an
> >> global or static variables. They're _supposed_ to be able to alter
> >> stuff which is passed to them. They're just weakly pure instead of
> >> strongly pure and thus can't be optimized out. Then strongly pure
> >> functions can call them and retain all of their guarantees. It sounds
> >> like you're throwing out the whole idea of weak purity. Certainly, as
> >> it stands, weak purity is pretty thoroughly trashed if not outright
> >> gone.
> > 
> > Correction, they're weakly pure because they don't access global or
> > static variables which can be mutated (so they can access them if
> > they're immutable or if they're const value types but otherwise can't
> > access them and still be pure).
> > 
> > As I understand it, this is how purity works:
> > 
> > 1. A function can be pure if it doesn't can any functions which are not
> > pure and if it doesn't access any global or static variables which can
> > be mutated by anything over the course of the program (so it can access
> > global and static variables which are immutable or which are const value
> > types).
> > 
> > It is perfectly
> > 
> > legal for a pure function to alter its arguments as long as they're not
> > const or immutable.
> 
> This is what I have difficulty with. Consider:
> 
> pure void foo(int* p) { *p = 3; }

And why isn't it weakly pure? If foo is called from a strongly pure function 
(or is anywhere in the call chain of a strongly pure function), then p cannot 
possibly point to global or static data. It would have had to have been 
allocated during the call of the strongly pure function (be it in the strongly 
pure function itself or in one of the functions in its call chain). So, it 
doesn't violate the purity of the strongly pure function at all. And if foo is 
called outside of a pure function (or is anywhere in a call chain which 
doesn't include a strongly pure function), then it doesn't matter whether p 
points to a global or static variable.

Sure, a call to foo cannot be optimized away due to purity, but it's 
definitely weakly pure and does not violate the purity of any strongly pure 
function which calls it.

- Jonathan M Davis

- Jonathan M Davis


More information about the dmd-beta mailing list