[Haskell-cafe] Ord (IORef a)?
simonpj at microsoft.com
Tue Jun 22 05:38:05 EDT 2004
| From: John Meacham
| would it be possible to provide an Ord instance for (IORef a)? For
| things like loop detection, one may need to make many IORef
| 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.)
I think that'd be entirely reasonable. Two runs of the same program
might give observably different behaviour wrt Ord, but that's ok because
the refs are allocates in the monad, as you say.
There is an efficiency cost though. Each IORef would need to have an
extra field, to record its allocation ID. (Address is not enough -- the
garbage collector can mangle them.)
My own view is that this is fine -- IORefs shouldn't be in your inner
loop, so an extra word in each is no big deal. Unless there are views
to the contrary, I'd be happy to see this in GHC.
More information about the Haskell-Cafe