[Haskell-cafe] Bad interaction of inlinePerformIO and mutable vectors

Michael Snoyman michael at snoyman.com
Thu Jul 31 10:57:31 UTC 2014

I'm trying to understand why some code isn't behaving as I'd expect, and to
determine whether it's a bug or not (and where that bug might be). Here's
the simplest version of the code:

    import Data.ByteString.Internal (inlinePerformIO)
    import qualified Data.Vector as V
    import qualified Data.Vector.Mutable as VM

    main :: IO ()
    main = do
        vm <- VM.new 1
        VM.write vm 0 'A'
        let x = inlinePerformIO $ VM.write vm 0 'B'
        x `seq` (V.freeze vm >>= print)

A more complete example is available on lpaste[1].

The problem is that I would expect the output to be "B", but in fact "A" is
still printed. From the longer paste that I linked to, you can see that:

* When using unsafePerformIO and unsafeDupablePerformIO, the semantics work
as I would have expected: "B" is printed after forcing evaluation of the
* If I add a `VM.read vm 0` call after the write, it also works.
* Using IORef, the behavior is also as I would have expected: as soon as
the result is evaluated, the reference is updated.

I'm testing on GHC 7.8.3, Ubuntu 64-bit, and compiling with -O2. I'm
curious if anyone has an idea as to why there is this difference in


[1] http://lpaste.net/108483
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20140731/dcaee812/attachment.html>

More information about the Haskell-Cafe mailing list