[Haskell-cafe] Re: How to obtain unique integers
es at ertes.de
Fri Mar 5 07:38:30 EST 2010
Robert Rothenberg <robrwo at gmail.com> wrote:
> I'm implementing a variant of Prolog in Haskell (yes, I know others
> already exist...) and am looking to solve the following issues:
> (1) I need to translate anonymous variables "_" into unique variable
> names. Data.Unique does not give me a printable identifier. Is there
> another package that does this?
> (2) Is there a cabal package similar to the Atom.hs module form ginsu,
> that associates strings with unique hashes?
> The alternative of course is to maintain state variables and pass them
> along to display and parsing functions, but that makes it difficult to
> make the terms instances of Read/Show without reference to a global
> variable anyhow.
Well, you can use hashUnique, but there are no guarantees about
collisions. If you need thread safety, the easiest approach is an id
generator thread like this:
idVar <- newEmptyMVar
forkIO . mapM (putMVar idVar) $ [0..]
let getId = takeMVar idVar
If you don't need it, you will need to pass state around, just like with
nightmare = unsafePerformIO (getWrongWife >>= sex)
More information about the Haskell-Cafe