[Haskell] Re: Global Variables and IO initializers

oleg at pobox.com oleg at pobox.com
Thu Nov 4 23:15:06 EST 2004

Koen Claessen wrote:
> Imagine a commutative monad, CIO. Commutative monads have
> the property that it does not matter in what order actions
> are performed, they will have the same effect. In other
> words, for all m1 :: CIO A, m2 :: CIO B, k :: A -> B -> CIO
> C, it should hold that:
>   do a <- m1             do b <- m2
>      b <- m2     ===        a <- m1
>      k a b                  k a b

... provided 'a' and 'b' are distinct variables, right?

The following is legal in Haskell
> do
>   x <- ...
>   let foo = ... x ...
>   x <- ...
>   let foo = ... x ...

That shows that variables add a bit more complexity to the
question. Not only the dependency in actions should be considered, but
also data dependency. In the above code, if action 'm2' happens to
refer to the variable 'a', does commutativity still hold? Here are a
few more similar example, assuming the proposed 'global level <-'
syntax and the commutative nature of the action of creating an IORef:

> z <- newIORef x
> x = [y1,y2]
> y1 <- newIORef t
> y2 <- newIORef t
> t <- newIORef True

Should this be accepted? Should the actions be executed as written?
What if it were written
> y1 <- (return $! t) >>= newIORef

A similar example:

> class C a where op :: a -> a
> y <- newIORef (op (undefined::IORef Bool))
> x <- newIORef True
> instance C (IORef Bool) where op _ = x

which shows that the data dependency analysis is a bit trickier than
expected, even if the definitions appear to be `well-ordered'.

Template Haskell may bring some new complications. Suppose module A
imports module B and uses some of the functions of B at TH
time. Module A is also free to use bindings of B at run time. Now, if
module B has initializing actions, would they be executed once or
twice? Would created CIORefs be shared across the phases? Would that
behavior be consistent with respect to module A being compiled or
interpreted? These questions do arise in Scheme -- where the agreement
seems to be to have two kinds of imports: regular import and import

[regarding partial signatures]
> Ah! I had forgotten about that. See also:
>   http://www.mail-archive.com/haskell@haskell.org/msg05186.html

Incidently, some of that is already available in Haskell,

More information about the Haskell mailing list