[Haskell] threading mutable state through callbacks

Simon Marlow simonmar at microsoft.com
Mon Oct 11 11:03:11 EDT 2004


On 08 October 2004 19:18, Sven Panne wrote:

> Jules Bean wrote:
>> [...] Unfortunately, it's not going to work. It's not going to work
>> because some of the procedures take callbacks, and the callbacks are
>> values of type IO (). I can see two solutions to this:
>> 
>> a) revert to using an IORef [...]
>> b) write the callbacks as values of type StateT Env IO () [...]
> 
> or
> 
> 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.

Cheers,
	Simon


More information about the Haskell mailing list