[Haskell-cafe] Ord (IORef a)?
Tom Pledger
tpledger at ihug.co.nz
Tue Jun 22 06:31:50 EDT 2004
John Meacham wrote:
>would it be possible to provide an Ord instance for (IORef a)? For
>things like loop detection, one may need to make many IORef comparasions
>and being able to use an efficient set would be a really big win.
>
>Since IORefs are created in the IO monad, the actual order can be
>arbitrary without breaking referential transparency (as long as it
>doesn't change over the lifetime of the IORef.)
> John
>
Someone posted an 'inside out' solution to this a while ago, but I'm
having trouble finding it in the mail archives.
It was along these lines:
type CollectionOfIoRef = Int
newtype IoRef a = IoRef (IORef (a, Array CollectionOfIoRef Bool))
You use this wrapper, IoRef, instead of IORef. To insert a reference
into a collection, you add the *collection* to the IoRef's hidden set.
Regards,
Tom
More information about the Haskell-Cafe
mailing list