On Fri, 17 Dec 2010 09:39:19 -0500, Don <[email protected]> wrote:

Steven Schveighoffer wrote:
On Fri, 17 Dec 2010 02:42:14 -0500, bearophile <[email protected]> wrote:

http://www.reddit.com/r/programming/comments/enajl/purity_in_d_language/

Bye,
bearophile


You've got two statements here which are both sort-of true, but taken together are not correct:

<1>
Another inaccuracy (really an omission) is that a weakly pure function is just like a pure function but cannot be memoized. In fact, it cannot be optimized in any way like strongly pure functions can.

<2>
This actually ties together nicely with my first point -- a pure function that returns a mutable pointer must be weakly pure.

A function which has immutably pure parameters can undergo *some* optimisation, even if the return value is a mutable pointer. For example, if the parameters are identical for both calls, you can do a deepdup of the first return value instead of calling the function again.

Yes, I agree with this. However, I still believe statement 1 is still correct, because you really have just introduced a third class of pure functions that I was not aware of :)

So we have:

weak-pure functions : functions which can accept and return any type of values, but can only call pure functions and cannot access global state. middle-pure ? functions : weak-pure functions which accept only immutable or implicitly convertable to immutable values, but returns a mutable reference. strong-pure funtions : pure functions which accept and return only immutable or implicitly convertible to immutable values.

There actually could be another class, const-pure functions: pure functions which accept and return only const or implicitly convertible to const values.

These 4th class of functions could be classified as strong-pure when called with all immutable or implicitly convertible to immutable parameters.

I suspect the compiler will need to classify things as strong or weak pure, and store various attributes on weak-pure functions to see what optimizations can be had.

This shouldn't matter too much to the user, he should be oblivious to such optimizations. I think it's going to be very difficult to 'accidentally' create pure functions that could be optimized better.

-Steve

P.

Reply via email to