unsafePerformIO and IORefs

Michael Weber michaelw@foldr.org
Mon, 18 Nov 2002 20:05:53 +0100


On Mon, Nov 18, 2002 at 10:36:05AM -0800, Hal Daume III wrote:
> You can't.  CSE (common subexpression elimination) will replace any
> occurances of 'newState 0' in a function body with the same value.
> 
> In short: don't use upIO :)

Sorry, cannot resist to pour a little salt onto the wound :)

[232]% grep global ghc/compiler/utils/Util.lhs 
        , global
global :: a -> IORef a
global a = unsafePerformIO (newIORef a)
[233]%

ghc/compiler/HsVersions.h:
[...]
#ifdef __GLASGOW_HASKELL__
#define GLOBAL_VAR(name,value,ty)  \
name = Util.global (value) :: IORef (ty); \
{-# NOINLINE name #-}
#endif


[237]% grep -r GLOBAL_VAR ghc/compiler | wc -l  
     90


Muahahah... ;-P


Cheers,
Michael