[Haskell-cafe] Weak pointers and referential transparency???

Brian Hulley brianh at metamilk.com
Sat Sep 9 18:47:43 EDT 2006

I have the following data structures:

    import System.Mem.Weak

    data Proxy = ...
    data Model = Model { _proxiesRef :: !(Ref.T [Weak Proxy]), ...}

(Ref.T is just a lifted IORef)

I was writing code like:

    createProxy :: MonadIO m => Model -> m Proxy
    createProxy Model{_proxiesRef = proxiesRef} = do
        proxies <- Ref.read proxiesRef
            proxy = Proxy{ ... }
        weakProxy <- liftIO $ mkWeakPtr proxy Nothing
        Ref.write proxiesRef $! (weakProxy : proxies)
        return proxy

where the intention is that whenever the returned proxy is no longer used, 
the table will no longer hold onto it (the table gets scavenged whenever I 
ask for the list of values in it) so there will be no space leak as proxies 
are created and discarded.

However when I looked at the above code more closely, it struck me that 
Haskell gives no guarantees that the returned proxy is actually the same 
object that is referenced by the table, because of referential transparency.

So my question is: will the above code work as expected and is this because 
we are allowed to assume non-referential transparency in situations like the 
above ie are we allowed to assume that all occurrences of the identifier 
"proxy" are always bound to a pointer to the same physical object and that 
all other uses of this value in the program are not going to cause it to be 
represented by multiple physical objects or otherwise split apart into 
registers etc?

In other words, if the entry for the proxy in the table stored in the Model 
dies, can I be absolutely 100% sure that this means the proxy no longer 
exists in any shape or form in the running program?

Thanks, Brian.
Logic empowers us and Love gives us purpose.
Yet still phantoms restless for eras long past,
congealed in the present in unthought forms,
strive mightily unseen to destroy us.


More information about the Haskell-Cafe mailing list