[Haskell-cafe] what are the safety conditions for unsafeIOToST

Roman Leshchinskiy rl at cse.unsw.edu.au
Tue Apr 6 20:30:07 EDT 2010

On 07/04/2010, at 07:33, Nicolas Frisby wrote:

> I haven't been able to find it via Google or Haddock. An old message
> suggests is was just a matter of exceptions?

I don't think that's correct. You can implement unsafePerformIO in terms unsafeIOToST:

unsafePerformIO :: IO a -> a
unsafePerformIO p = runST (unsafeIOToST p)

In fact, the only safe-ish use for it I have found is to use Storable-related functions in ST, hoping that the instances don't actually use any real IO functionality. Arguably, this shouldn't be necessary as Storable should live in ST anyway.

> I only want to use the IO for generating Data.Uniques to pair with
> STRefs in order to make a map of them. I'm guessing this would be a
> safe use since it's exception free (... right?).

It's hard to tell without looking at your code. But if you are generating Uniques in ST then it's probably unsafe:

foo :: () -> Unique
foo _ = runST (unsafeIOToST newUnique)

What's the value of foo ()?


More information about the Haskell-Cafe mailing list