Improving containers library

Bertram Felgenhauer bertram.felgenhauer at googlemail.com
Sat Mar 6 12:19:46 EST 2010


Axel Simon wrote:
> >Any easy way of comparing pointers? I mean, if I have something like
> >Tree a = N | B (Tree a) a (Tree a)
> >and have (l::Tree a) and (r::Tree a), can I ask about the "physical
> >equality"?
> 
> You can! Despite the names appearing in the following code, the
> following is safe:
> 
> -- | Equality on pointers.
> ptrEqual :: a -> a -> Bool
> ptrEqual x y = unsafePerformIO $ do
>   nx <- makeStableName x
>   ny <- makeStableName y
>   return (nx==ny)

{-# LANGUAGE MagicHash #-}
import GHC.Exts

ptrEqual' :: a -> a -> Bool
ptrEqual' x y = case reallyUnsafePtrEquality# x y of
    0# -> False
    1# -> True

This is actually a pointer comparison. I believe it can produce false
negatives though, because indirections are not followed. If that's
correct, a false negative may be turned into a positive by garbage
collection. So use with care.

regards,

Bertram


More information about the Libraries mailing list