[Haskell-cafe] Suggestions for simulating Object ID

Hemanth Kapila saihemanth at gmail.com
Tue Jun 30 05:55:26 EDT 2009

Hello all,
Can some one please suggest something on simulating some sort of
"object-Ids" in Haskell?

I mean, something like the following relation will hold:
> a1 == a2  =  (objectID a1) == (objectID a2)

Currently I have something like
> import Data.Hash
>import qualified Data.ByteString.Char8  as B

> newtype UniqueID = UniqueID (Hash, B.ByteString)

I have a type class called Identifiable, the instances of which are supposed
to give a unqiue bytestring representation of the instance which can be
arbitrarily long but should satisfy the condition mentioned earlier.

> class  Identifiable a  where
>    instanceID :: a  -> B.ByteString

and finally, the required funtion objectID is defined as :
> objectID :: (Hashable a, Identifiable a) => a -> UniqueID
> objectID a1 = UniqueID (hash a1, instanceID a1)

while comparing object Ids, I compare the hash values first (which can be
faster for arbitrary values). Only if they are equal then I
compare the instanceIDs and count on laziness for avoiding the construction
of the bytestrings where they are not needed.

Can you please suggest a better approach?
 I also toyed with the idea of storing all the data 'objects' in an array
and use the index as the identifier but the trouble is I am not aware of all
the number of instances before hand under all the circumstances.

Many thanks
Hemanth K
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20090630/f355fe45/attachment.html

More information about the Haskell-Cafe mailing list