RE: [Haskell] IO Regions in Haskell98
| however that there is a Haskell98 implementation of IO regions. The | solution involves no higher-ranked types, and works both in GHC and | Hugs. The idea is trivial: given | | > newtype Q = Q Handle | > newtype IOM a = IOM (IO a) | > qGetChar :: Q -> IOM Char | > withFile :: FilePath -> (Q -> IOM a) -> IOM a | | we wish to assure that the handle Q never escapes, neither explicitly | nor implicitly. Don't side effects kill you? withFile "foo" (\q -> writeIORef var q) So would unsafePerformIO withFile "foo" (\q -> return (unsafePerformIO (qGetChar q))) Simon
Simon Peyton-Jones wrote:
Don't side effects kill you?
withFile "foo" (\q -> writeIORef var q)
No. Computations within the body of |withFile| must be within the |IOM| monad. These computations are built from |unit| and those computations exported from IORegions98 that are deemed safe. There is no lift function from IO to the IOM monad, as any such generic function will indeed be unsafe. Currently IORegions98 does not export any writeIORef-like operation in the IOM monad. We can do so, if we wish. But in that case, we must obey the safety invariant and define this function as: qWriteIORef :: Typeable a => IORef a -> a -> IOM mark () That precludes storing of Qhandles or IOM computations.
So would unsafePerformIO
withFile "foo" (\q -> return (unsafePerformIO (qGetChar q)))
with unsafePerformIO (or related unsafeCoerce), all things are possible. They are Alpha and Omega (probably, more of the latter...) But even then, one cannot apply unsafePerformIO to (qGetChar q) because the type of (qGetChar q) is |IOM mark Char| rather than just |IO Char|.
participants (2)
-
oleg@pobox.com -
Simon Peyton-Jones