purity and memory allocations/pointers

Meta jared771 at gmail.com
Sat Aug 3 17:50:44 PDT 2013


On Saturday, 3 August 2013 at 23:04:14 UTC, Timon Gehr wrote:
>> 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?

I'm thinking of when a pointer points to an invalid location 
(0x00000000 or similar) and is dereferenced. I'm not sure if that 
would be considered impure or not, i.e., causes side effects.

>> 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 apologize, as I don't know how familiar you are with Haskell, 
so forgive me if I'm telling you something you already know. That 
code is 100% pure and side-effect free; no variables are being 
modified. Haskell only simulates side-effects with monads, and 
do-notation (syntwhat you use in this example) which is syntactic 
sugar.

>> (i.e., the academic definition of purity).
>
> I wouldn't go that far.

Perhaps that may go too far, as academics love to obfuscate 
topics with a bunch of extraneous cruft, but the fact remains 
that purity means:

1. No modification of local or global state (side-effects)
2. No dependence on global mutable state.


More information about the Digitalmars-d mailing list