
On Sun, 15 Mar 2009, Ryan Ingram wrote:
unsafeInterleaveIO allows embedding side effects into a pure computation. This means you can potentially observe if some pure value has been evaluated or not; the result of your code could change depending how lazy/strict it is, which is very hard to predict!
For example:
-- given f :: Integer -> Integer
main = do r <- newIORef 0 v <- unsafeInterleaveIO $ do writeIORef r 1 return 1 x <- case f v of 0 -> return 0 n -> return (n - 1) y <- readIORef r print y
-- a couple of examples: f x = 0 -- program prints "0" -- f x = x -- program prints "1"
Interesting example. I have implemented the lazyio package. It asserts that unsafely interleaved IO actions are performed in order. This should prevent such problems, does it? However, they are only avoided within the LazyIO monad. Running the LazyIO monad in IO allows the same corruptions as shown above. http://hackage.haskell.org/cgi-bin/hackage-scripts/package/lazyio