what are the safety conditions for unsafeIOToST

I haven't been able to find it via Google or Haddock. An old message suggests is was just a matter of exceptions? 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?). Thanks!

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 ()? Roman

I would venture that the condition under which unsafeIOtoST would be safe is if all of the computations you are performing in the IO monad are only changing state that is local to the computation within the ST monad in which you are running. (For example, if there were no STRef type then you could emulate it using IORefs, and this would be safe since the IORefs couldn't leak outside of the ST monad.) It sounds like Unique doesn't satisfy this property since the side-effect of creating a new Unique will leak out of the ST monad. It would probably be better to use some other stream of unique values that runs inside the ST monad. Cheers, Greg On Apr 6, 2010, at 5:30 PM, Roman Leshchinskiy wrote:
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 ()?
Roman
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Gregory Crosswhite schrieb:
I would venture that the condition under which unsafeIOtoST would be safe is if all of the computations you are performing in the IO monad are only changing state that is local to the computation within the ST monad in which you are running. (For example, if there were no STRef type then you could emulate it using IORefs, and this would be safe since the IORefs couldn't leak outside of the ST monad.) This is the way I implemented ST and STRef for JHC: http://code.haskell.org/~thielema/statethread/
On Apr 6, 2010, at 5:30 PM, Roman Leshchinskiy wrote:
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.
But Storable in ST monad would be still dangerous, because pointers may point to non-allocated memory or point outside of an array.

On 08/04/2010, at 01:38, Henning Thielemann wrote:
On Apr 6, 2010, at 5:30 PM, Roman Leshchinskiy wrote:
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.
But Storable in ST monad would be still dangerous, because pointers may point to non-allocated memory or point outside of an array.
I don't think that's the kind of safety the original poster had in mind. You can have invalid memory accesses even in pure code but that's ok since we know what the semantics is: bottom. I understood the question to be about the conditions under which unsafeIOToST can violate referential transparency. Roman
participants (4)
-
Gregory Crosswhite
-
Henning Thielemann
-
Nicolas Frisby
-
Roman Leshchinskiy