Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python-"is"-like equality operator for Haskell/GHC

Is there a GHC-specific "unsafe" extension to ask whether two Haskell references point to the same location?

I'm aware this can break referential transparency if not used properly. But there should be little harm (unless I'm missing something), if it is used very careful, as a means for optimizations by short-cutting recursive (or expensive) data traversal, e.g. for implementing an optimized Eq instance, e.g.:

instance Eq ComplexTree where
   a == b  = (a `unsafeSameRef` b) || (a `deepCompare` b)

providing deepCompare is guaranteed to be true if unsafeSameRef decides true (but not necessarily the other way around).

EDIT/PS: Thanks to the answer pointing to System.Mem.StableName, I was able to also find the paper Stretching the storage manager: weak pointers and stable names in Haskell which happens to have addressed this very problem already over 10 years ago...

like image 797
hvr Avatar asked Apr 18 '11 11:04

hvr


1 Answers

GHC's System.Mem.StableName solves exactly this problem.

like image 165
Roman Cheplyaka Avatar answered Nov 15 '22 22:11

Roman Cheplyaka