[Haskell] Re: Global Variables and IO initializers

Adrian Hey ahey at iee.org
Sat Nov 6 13:13:48 EST 2004

On Saturday 06 Nov 2004 1:07 pm, Keean Schupke wrote:
> Just been reading arround. According to ghc docs, the noinline
> pragma is in the Haskell98 report. On that basis what is wrong
> with using the following to initialise these top-level constants?
>     {-# NOINLINE newref #-}
>     newref :: IORef Int
>     newref = unsafePerformIO $ newIORef 0

1- It's awkward, and only safe if programmer actually remembers to
   use NOINLINE (compiler won't throw out an error if it's ommited).

2- It's ugly. Arguably this is mere aesthetics, but if it really is
   perfectly safe it should not be necessary to mention the word
   "unsafe" anywhere. With this solution programmers still have
   to "put their thinking caps on" to figure out if it's safe. It
   would be better if the language design and type system guaranteed
   it was safe.

3- According to current ghc docs, you still have to compile the module
   with the -fno-cse flag too. This may or may not be visible in the
   source code (with OPTIONS pragma). If it isn't the programmer has
   to look elsewhere (in makefiles or whatever) to check this flag
   has been used.

4- -fno-cse applies to an entire module, which will be overkill in
   most cases.

Adrian Hey

More information about the Haskell mailing list