[Haskell-cafe] How to check object's identity?

Thomas Davie tom.davie at gmail.com
Sun Jan 4 12:33:03 EST 2009


On 4 Jan 2009, at 18:08, Aaron Tomb wrote:

>
> On Jan 3, 2009, at 7:28 AM, Xie Hanjian wrote:
>
>> Hi,
>>
>> I tried this in ghci:
>>> Prelude> 1:2:[] == 1:2:[]
>> True
>>
>> Does this mean (:) return the same object on same input, or
>> (==) is not for identity checking? If the later is true, how
>> can I check two object is the *same* object?
>
> As others have explained, the == operator doesn't tell you whether  
> two values are actually stored at the same location in memory. If  
> you really need to do this, however, GHC does provide a primitive  
> for comparing the addresses of two arbitrary values:
>
>    reallyUnsafePtrEquality# :: a -> a -> Int#
>
>    http://haskell.org/ghc/docs/latest/html/libraries/ghc-prim/GHC-Prim.html#22
>
> Take note of the "reallyUnsafe" prefix, though. :-) It's not  
> something most programs should ever need to deal with.

Of note, you probably don't need to do this.  It's usually safer to  
associate data with a key, using Data.Map, or just pairing objects  
with a unique id.

Bob


More information about the Haskell-Cafe mailing list