
Nick Name
On Fri, 31 Jan 2003 07:47:43 +0000 Glynn Clements
wrote:
The usual fudge is:
import IORef import IOExts
globalVar :: IORef Int globalVar = unsafePerformIO $ newIORef 0
I see in the documentation of unsafePerformIO that no one makes guarantees about the order in wich unsafePerformIO arguments are performed (especially they don't have to be executed BEFORE main), so this trick should not be adviced as a general practice; it should be pointed out that it works in GHC but could not work in other compilers.
English nit: I think you mean ``could fail in other compilers'': it's not really impossible for the trick to work in other compilers than GHC. Otherwise, though, see my other post on this subject: unsafePerformIO will perform its action when the variable is accessed, so you can't write a Haskell program which differentiates between what any compiler actually does and running the variable allocations before main.
Vincenzo
-- Fedeli alla linea, anche quando non c' Quando l'imperatore malato, quando muore,o dubbioso, o perplesso. Fedeli alla linea la linea non c'. [CCCP]
Jon Cast

On Fri, 31 Jan 2003, Jon Cast wrote:
Otherwise, though, see my other post on this subject: unsafePerformIO will perform its action when the variable is accessed, so you can't write a Haskell program which differentiates between what any compiler actually does and running the variable allocations before main.
If you initialize this "global variable" with a value from a file which is changed by main (or over time) then order does matter. I claim unsafePerformIO is very often unsafe - the question is more like if the "level of unsafety" is acceptable for a particular application. /Patrik

G'day all. On Fri, Jan 31, 2003 at 01:54:26PM -0600, Jon Cast wrote:
Otherwise, though, see my other post on this subject: unsafePerformIO will perform its action when the variable is accessed, so you can't write a Haskell program which differentiates between what any compiler actually does and running the variable allocations before main.
As has been pointed out, there is no language requirement for a Haskell implementation to be "fully lazy". In particular, it is technically possible for an implementation to garbage collect globalVar and re-evaluate it on the next call. Haskell 2 should probably have a pragma controlling this. Cheers, Andrew Bromage
participants (3)
-
Andrew J Bromage
-
Jon Cast
-
Patrik Jansson