What means MutVar# ?

Sven Panne Sven.Panne at aedion.de
Thu Apr 8 18:45:36 EDT 2004

Hans Nikolaus Beck wrote:
> [...] I've readed tha writeIORef (and readIoRef etc) is used to write directly
> to memory places fo implementing variables, as in example in HOpenGL used. Is
> this also Haskell standard ?

Huh? I'm not sure what you mean exactly, but with the help of unsafePerformIO
and a pragma for clever compilers you can simulate something like a global
variable in Haskell. Here an excerpt from the GLUT menu handling module:

    {-# NOINLINE theMenuTable #-}
    theMenuTable :: IORef MenuTable
    theMenuTable = unsafePerformIO (newIORef emptyMenuTable)

    getMenuTable :: IO MenuTable
    getMenuTable = readIORef theMenuTable

    modifyMenuTable :: (MenuTable -> MenuTable) -> IO ()
    modifyMenuTable = modifyIORef theMenuTable

Definitely not something to be proud of, but quite handy from time to time. :-)
Alternatives are passing the IORef to every function which needs it (but this
would clutter up the GLUT API in the above case) or using the FFI for holding
global data on the C side. GHC uses a similar hack internally, too, BTW.


More information about the Glasgow-haskell-users mailing list