[Haskell-cafe] Efficient object identity (aka symbols as data)

Jacek Generowicz jacek.generowicz at cern.ch
Thu May 26 11:59:37 CEST 2011


On 2011 May 26, at 11:16, Christopher Done wrote:

> On 26 May 2011 10:45, Jacek Generowicz <jacek.generowicz at cern.ch>  
> wrote:
>> What is the Haskell approach to efficient comparison and lookup of  
>> objects
>> by their identity?
>
> Often you just provide your own and implement Eq.
>
>> I should be able to run the program on data that becomes available  
>> at run
>> time.
>
> Typically you define an id generator and zip anything coming from the
> input stream up with that generator.

Makes sense.

>> Whatever algorithm I choose to use for the optimization, will have  
>> to do
>> lots of comparisons of Groups and Persons where their *identity* is  
>> all that
>> matters: you don't need to look inside the objects.
>
> To achieve this abstraction the usual way is just implementing Eq:
>
> instance Eq Person where
>   Person{personId=id1} == Person{personId=id2} = id1 == id2

Any comments on the relative efficiency of the above as compared to

A == B in the context of

data Foo = A | B | C | D | ... lots more ...

?

(I imagine that a Sufficiently Smart Compiler could reduce (==) ::  
Person Person to just integer comparison.)

Thank you.




More information about the Haskell-Cafe mailing list