unique identifiers as a separate library

Simon Marlow marlowsd at gmail.com
Mon Dec 22 05:35:35 EST 2008

Iavor Diatchki wrote:
> Hello,
> I have made the two changes that Simon suggested and uploaded a new
> version of the library.   By the way, GHC seemed to work correctly
> even without the extra boolean parameter, perhaps it treats
> unsafePerformIO specially somehow?  A somewhat related question:  I
> ended up using three calls to unsafeInterleaveIO which seems like a
> bit much.  Could I have done it in a different way somehow?  This is
> what I did:
> gen r = do v <- unsafeInterleaveIO (genSym r)
>                    ls <- unsafeInterleaveIO (gen r)
>                    rs <- unsafeInterleaveIO (gen r)
>                    return (Node v ls rs)

I'd probably do it like this:

 > gen r = unsafeDupableInterleaveIO $ do
 >            v <- unsafeDupableInterleaveIO (genSym r)
 >            ls <- gen r
 >            rs <- gen r
 >            return (Node v ls rs)

which is close to the way GHC does it, except that we do indeed call genSym 
for every node.  Calling genSym is cheaper than building the thunk for 
unsafeDupableInterleaveIO, although if there's an atomicModifyIORef 
involved that will probably tip the balance the other way.


More information about the Glasgow-haskell-users mailing list