type of stToIO

Ross Paterson ross at soi.city.ac.uk
Thu Oct 5 13:23:41 EDT 2000


On Thu, Oct 05, 2000 at 09:25:18AM -0700, Simon Peyton-Jones wrote:
| | The library documentation says
| | 
| | 	stToIO :: ST s a -> IO a
| | 
| | but PrelIOBase says
| | 
| | 	stToIO :: ST RealWorld a -> IO a
| | 
| | The documented type would be unsafe (as it is in Classic Hugs), so it
| | seems necessary to mention the real world in the documentation.
| 
| I discussed this with John Launchbury who pointed out a much
| better type for stToIO
| 
| 	stToIO :: (forall s. ST s a) -> IO a
| 
| That avoids exposing RealWorld while preserving soundness.  Better, eh?

I'm not so sure.  The current type (in GHC) means something: the IO
monad ensures sequential access to its own region of the heap (though
maybe RealWorld isn't the best name for this).  And it's more general,
e.g. I can write

> data Var v = Var { readVar :: IO v, writeVar :: v -> IO () }

> newVar :: v -> IO (Var v)
> newVar v = stToIO $ do
>		r <- newSTRef v
>		return (Var {
>				readVar = stToIO (readSTRef r),
>				writeVar = stToIO . writeSTRef r
>			})

which would be forbidden by the John's runST-style type.




More information about the Glasgow-haskell-bugs mailing list