lastWrite = unsafePerformIO $ newRef 0
lastRead = unsafePerformIO $ newRef 0
to declare two global mutable variables for use in fixpoint iteration.
Am I correct that Haskell is within its rights to merge these into a
single reference? Is there some clean way of telling Haskell not to
do this?
In this case I can avoid the potential problem in a variety of ways
(changing one of the 0's to 1's, using newRef (0,0), etc.), but it'd
be nice to have a safe general solution.
Thanks,
Geoffrey
You are correct. The standard idiom is:
lastWrite :: IORef Int
{-# NOINLINE lastWrite #-}
lastWrite = unsafePerformIO $ newRef 0
It has been suggested (though I haven't seen concrete evidence) that as of 7.0
this is insufficient and you also need:
{-# OPTIONS_GHC -fno-cse #-}
But of course impacts the whole module, and so people sometimes put all their
"globals" into a single module without other code.
There are also people who claim this whole thing is bad and you should find
other ways to pass globals through functions.