unsafePerformIO around FFI calls

Simon Marlow simonmar@microsoft.com
Tue, 23 Jul 2002 17:46:00 +0100


> If this is true, then is it equivalently safe to wrap the following
> Haskell action in unsafePerformIO:
>=20
>   myFunc i =3D
>     do arr <- newArray (0,255) 0
>        mapM_ (\j -> writeArray arr j (i+j)) [0..255]
>        foo <- newIORef 0
>        mapM_ (\j -> readArray arr j >>=3D modifyIORef foo (+j)) =
[0..255]
>        readIORef foo >>=3D return
>=20
> ? (don't call me on syntax errors -- i haven't checked this=20
> at all, but you should get the idea)

Yes, but for this particular case you would use the ST monad instead
(with STArray and STRef).  That way you get to avoid mentioning anything
with 'unsafe' in its name, and the compiler does the safety proof for
you.

Cheers,
	Simon