3

In Haskell, is there a way to test whether two IORefs are identical? I'm looking for something like this:

IORef a -> IORef a -> IO Bool

This would be useful if you want to visualize a graph made of IORefs, for example. I don't think it would break referential transparency, because IORefs have a meaningful identity (and the result could be in IO, anyway). And I assume it wouldn't be hard to implement this efficiently, as a pointer comparison.

Is this available somewhere? Or if not, why not?

(Edit: I just found System.Mem.StableName from a different SO question, which looks helpful.)

Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137
dpercy
  • 459
  • 6
  • 13
  • 4
    [It's called `(==)`](https://hackage.haskell.org/package/base-4.14.0.0/docs/Data-IORef.html#t:IORef) – HTNW May 05 '20 at 02:04

1 Answers1

10

Don't overthink it. You have instance Eq (IORef a), so you can just use ==, and the result isn't even in IO.

  • Note that this is identity for `IORef`s, but you may of course have multiple distinct `IORef`s pointing to the same actual *value*, that is, the normal non-observable sharing of Haskell references – Jon Purdy May 05 '20 at 17:20
  • Thanks, not sure how I missed that. :) – dpercy May 06 '20 at 17:30