[Haskell-cafe] Global Variables and IO initializers

Benjamin Franksen benjamin.franksen at bessy.de
Mon Nov 8 22:14:06 EST 2004

On Tuesday 09 November 2004 03:18, Ben Rudiak-Gould wrote:
> Adrian Hey's example of a badly-written C library is one. But I agree
> with Robert Dockins that such cases are better solved in C than in Haskell.

Yes. Your code won't depend on compiler (dependent) flags to disable 
optimizations, which for my taste is just a little bit too obscure and is 
anyway non-portable.

What follows are very raw ideas. Please bear with me if you think it is 

We could reduce the pain of applying the C wrapper solution a bit by adding 
some support in the FFI. I imagine a feature to allow writing small C 
wrappers around imported foreign routines directly inside the Haskell module. 
Such code would need to be quoted properly (e.g. encoded as Haskell string, 
similar to the foreign entity names now). Eiffel has such a feature for its 
own variant of FFI and it has proven quite useful in practice.

I know of at least one other case where such a feature might be helpful, 
namely for APIs where C structures get passed by value. At the moment one has 
to either lie to the FFI (declare the routine as if teh structure fields were 
passed separately, after checking that this is ok with respect to argument 
order and alignment) or else write a C wrapper routine (which is somewhat 
safer and more portable).

The advantage of doing it through FFI support is that we avoid encouraging bad 
programming style by making global mutable variables an all too easily 
accessible and apparently safe feature.

I have some ideas how to handle stdxyz without using global state but they are 
not though out and it is too late now anyway.

Top level things with identity are evil.	-- Lennart Augustsson

More information about the Haskell-Cafe mailing list