[Haskell] Global Variables and IO initializers: A proposal and semantics

John Meacham john at repetae.net
Wed Oct 13 05:51:08 EDT 2004

On Wed, Oct 13, 2004 at 07:20:06AM +0100, Jules Bean wrote:
> On 12 Oct 2004, at 23:33, John Meacham wrote:
> >  and via the FFI  just a
> > foreign import "&global_var" :: Ptr Int
> >  note that we do not need any foregin code, just an object which
> >allocates the space in the bss for global_var, the fact we can access
> >and work with such space from haskell, but have no way to allocate it
> >is quite telling that there is something missing in the language.
> >
> Yes, that's weird, isn't it?

Yeah, I noticed this oddity when I found I could create very fast global
variables via this trick and peeks and pokes to read/write them. but the
stub C file with just a 'int var;' was sort of silly, especially when I
knew that didn't generate any code at all, just some allocated space. 

> >
> >The basic idea is that your entire program behaves as if in a giant
> >'mdo' block, ordered in module dependency order.
> >
> I wondered if something like that could work, but I wasn't sure that 
> mdo allowed recursion in its let-bindings...

The mdo implementation in ghc does not actually, but that is mainly to
simplify some odd cases when a variable will appear monomorphic in one
place and polymorphic in another and is not actually a restriction of
recursive monads in general nor does it change the semantics of the
fixpoint operations on them, which is the main item I was trying to
leverage from those papers.

In section 3.2 of the mdo paper, an alternate translation of mdo is
given which does allow polymorphic let bindings and that is the
translation which should be used if you were to actually implement the
program transformation I described. 

Note that since we 'push' all the normal declarations of a module above
all monadic bindings, we don't run into the polymorphic problem
described in the paper, as our polymorphic normal declarations scope
over the entire expression in which they may be used.


John Meacham - ⑆repetae.net⑆john⑈ 

More information about the Haskell mailing list