On Tue, 13 Sep 2011 07:43:55 -0400, bearophile <[email protected]> wrote:

Jonathan M Davis:

However, Foo.y is not encapsulated
by a strongly pure function at all. Other functions can alter alter it. So, it
breaks purity.

Thank you for your explanation :-)

So, let's change the situation a bit. If the struct Foo is the only thing present in a module (to avoid someone to touch its private members), and the y field is "private static" only foo2 is able to touch it. In this case isn't foo2 weakly pure?


struct Foo {
    private static int y;
    static pure void foo2() {
        Foo.y++;
    }
}
void main() {}

weak purity vs. strong purity is determined by the parameters. Strong-pure functions must have all parameters immutable or implicitly convertable to immutable.

Since foo2 has no parameters, it will be classified as strong-pure.

Yet, pure optimizations cannot be used on it. For example, for a strong-pure function fn, the following calls can be reduced to one call:

int i = fn();
int j = fn(); // can be rewritten to int i = j

But foo2 cannot be optimized this way, it would change the value of y.

What you want to do is consider the private static state of a class/struct to be part of the parameters to such functions, which I think negates a lot of possible optimizations, when those static variables are not used. It makes too broad an assumption.

Not to mention, static private variables are accessible from *any* member function, which means any member function of a class/struct which contains private static variables could not be strong-pure.

But the real killer: the side effect makes foo2 not composable into strong-pure functions. That is, a strong pure function that calls a weak pure function *does not* become weak pure. However, in this case, it would have to. The whole system breaks down once you start implying global state is a parameter.

-Steve

Reply via email to