Hacker News new | ask | show | jobs
by peterkelly 4442 days ago
And now for exercise 2:

Port the solution to Haskell

2 comments

  newtype X = X (IORef Int)
  instance Num X where
    fromInteger = X . unsafePerformIO . newIORef . fromInteger
  instance Eq X where
    (X a) == (X b) = unsafePerformIO $ do
                       x <- readIORef a
                       y <- readIORef b
                       writeIORef a $ x+1
                       return $ x == y

  ghci> a <- fmap X $ newIORef 1
  ghci> a == 1 && a == 2
  True
Edit: formatting.
even more general:

    let a = 1; _ == _ = True in a == 1 && a == 2