[Haskell] threading mutable state through callbacks

Adrian Hey ahey at iee.org
Tue Oct 12 06:23:57 EDT 2004

On Monday 11 Oct 2004 4:03 pm, Simon Marlow wrote:
> On 08 October 2004 19:18, Sven Panne wrote:
> > c) Give up any hope of clean semantics and simply use a common hack
> > like:
> >
> >        {-# NOINLINE myGlobalVar #-}
> >        myGlobalVar :: IORef Int
> >        myGlobalVar = unsafePerformIO (newIORef 0)
> >
> >     My GLUT binding does it happily, as does GHC itself, [snip]
> I'd like to add that while the implementation might be a little unsafe,
> there's no problem in principle with the semantics of top-level IORefs.
> We could add such a thing as a GHC extension, but it would be nice if it
> were an instance of a more general-purpose extension.

I found myself doing this kind of thing quite a bit recently for FFI
binding. It would be nice have some solution such that the compiler
was guaranteed to be aware of the semantic problems and we didn't have
to rely on unsafePerformIO, NOINLINE etc hackery. I don't know what more
general-purpose extension you have in mind, but couldn't you just borrow
from do syntax at the top level, something like this..

> myThing :: Thing
> myThing <- newThing

(where newThing :: IO Thing)

Adrian Hey

More information about the Haskell mailing list