[Haskell-cafe] Is (==) commutative?

Twan van Laarhoven twanvl at gmail.com
Tue Jul 24 15:23:43 CEST 2012


On 24/07/12 14:44, timothyhobbs at seznam.cz wrote:
> There's always this, for ALL types a :(  So even where you would think that the
> documentation can claim that a given Eq instance follows the law of
> commutativity, it really cannot.

Once you invoke unsafePerformIO, you can break the type system, and probably 
execute random assembly code and write to any memory location. So, at this point 
all bets for the rest of the program are of. It is up to the programmer to 
verify that all the necessary invariants still hold when you do unsafe stuff.

In other words, the issue here is not with a particular Eq instance, it is with 
unsafePerformIO.

Essentially `unsafePerformIO (x :: IO Int)` is not really an Int, but rather a 
value that behaves like one in most but not all cases.


Also, let a = unsafePerformIO x in (a,a) is *not* the same thing as 
(unsafePerformIO x,unsafePerformIO x). For your entertainment:

     λ> do m <- newEmptyMVar ;putMVar m 1 ; let {a =(unsafePerformIO (do v <- 
takeMVar m; putMVar m 2; return v)); b = (unsafePerformIO (do v <- takeMVar m; 
putMVar m 1 ; return v))} ; print (a == b); print (b == a)
     False
     False


Twan



More information about the Haskell-Cafe mailing list