purity and memory allocations/pointers

Timon Gehr timon.gehr at gmx.ch
Sat Aug 3 16:04:13 PDT 2013


On 08/03/2013 09:07 PM, Meta wrote:
> On Saturday, 3 August 2013 at 16:47:52 UTC, Timon Gehr wrote:
>> On 08/03/2013 05:59 PM, monarch_dodra wrote:
>>> One last question: Pointers.
>>>
>>> int get(int* p) pure
>>> {
>>>     return *p;
>>> }
>>>
>>> void main()
>>> {
>>>     int i = 0;
>>>     auto p = &i;
>>>     get(p);
>>> }
>>>
>>> Here, get, to me, is obviously not pure, since it depends on the state
>>> of the global "i". *Where* did "get" go wrong? Did I simply "abusively"
>>> mark get as pure? Is the "pure" keyword's guarantee simply "weak"?
>>> ...
>>
>> Yes, it's weak.
>
> It depends on whether you think a pointer dereference is pure or not  (I
> don't know the answer).

It is pure in D, but I guess you are not referring to that.
What's your understanding of purity in this context?

> That aside, as long as get doesn't modify the
> value at *p or change what p points to, this is strongly pure

Modification and dereference within a Haskell expression:

import Data.STRef
import Control.Monad.ST

x = runST $ do
   x <- newSTRef 0
   writeSTRef x 1
   v <- readSTRef x
   return v

main = print x

> (i.e., the academic definition of purity).

I wouldn't go that far.


More information about the Digitalmars-d mailing list