[Haskell-cafe] pointer equality
Levent Erkok
erkokl at gmail.com
Wed Jul 20 09:01:56 CEST 2011
On Jul 19, 2011, at 11:34 PM, Levent Erkok wrote:
> import System.Mem.StableName
>
> areEqual :: Eq a => a -> a -> IO Bool
> areEqual x y = do
> sx <- hashStableName `fmap` (x `seq` makeStableName x)
> sy <- hashStableName `fmap` (y `seq` makeStableName y)
> return $ (sx == sy) || x == y
One correction to the above code: Since we're actually comparing hashes, there's a non-zero chance that we might get a hash-collision; thus incorrectly identifying two different objects to be the same even though they have different stable names. To accommodate for that, you can use the hashes to index into a look-up table and then do a linear-scan to make sure it's an object that you've seen before. So the above code is *not* going to work for your purposes in general, but it can be extended to handle such equalities if you can afford to carry around the hash-table with you and be disciplined in how you perform your equality tests. Again, see Andy's paper (section 11) for further details on how he this problem can be handled in general.
-Levent.
More information about the Haskell-Cafe
mailing list