[Haskell] Re: Global Variables and IO initializers

Keean Schupke k.schupke at imperial.ac.uk
Sat Nov 6 07:43:11 EST 2004


Vincenzo Ciancia wrote:

>Yes, but I guess everybody would like a solution where
>
>myRef1 = unsafePerformIO $ newIORef 0
>myRef2 = unsafePerformIO $ newIORef 0
>
>are different variables. Also, it's not true that it's perfectly safe, 
>  
>
I don't understant this - they would be different variables with
Haskell as it stands now, and they would be different variables
using the unique monad (or equivalent) - You are right that it
is not perfectly safe, but only because the compiler might inline
the definition. Each inlined version of the function would then
refer to a different IORef.

With GHC you can use the noinline
pragma to tell the compiler not to inline the function - which makes
it completely safe, but compiler pragma's are implementation
specific and not a satisfactory solution. So you can see what is
needed is a way to tell the compiler not to inline the function
that is part of the language. Its tempting to define a keyword,
but this again is unsatifactory - as we would like to limit
proliferation of keywords... The solution seems to be to use
the type-system, and intoduce a type that implies functions
returning this type should not be inlined.

    Keean


More information about the Haskell mailing list