How to bind a window library (in C) to Haskell?

Brian Hulley brianh at metamilk.com
Sun Feb 26 21:11:50 EST 2006


Hi -
I'd like to be able to use Haskell for the project I'm working on but the 
problem is that I've already written a lot of code for a nice GUI using 
DirectX in Visual C++.

I thought it might be possible to make up some kind of simple API for it 
which I could call from Haskell, so I started with the following simple 
Haskell module to try and see if I could understand the FFI:

           module Main where

           data Window = Window_Edit | Window_List | Window_Tree
           data Layout = Layout_Left | Layout_Right

           type Callback = () -> Bool

           foreign import ccall gui_init :: [(Window, Layout, Callback)] -> 
IO ()

           main = gui_init [(Window_Edit, Layout_Left, \_->True)]

Of course my "init" function is far too simple, but I was thinking this 
captures the essence of the kinds of things that would need to be passed to 
the C++ code ie a list of windows and call back functions etc.

However I've immediately run into a major problem. When I try to compile 
with ghc -fglasgow-exts --make main.hs I get an error: "unacceptable 
argument type in foreign declaration..."

My questions are:

1) Does this mean that the FFI can only pass Int, Char etc and not user 
defined data types or compound data types?

2) I'm also really confused by the different kinds of pointers available, 
and how to safely store a function closure in some C data structure, and how 
to use this to call the function from within C and access the result safely

3) When does GHC do garbage collection? Is the garbage collection done in 
the same thread as the executing program? Does GHC run a normal Haskell 
program using multiple threads? Would I need to link my C DLL with a 
multithreaded version of the C runtime to avoid problems?

Alternatively, has anyone managed to use DirectX or COM from within a 
Haskell program? (because then I could perhaps rewrite all my gui code from 
scratch in Haskell...) (I'm loathe to switch to OpenGL because OpenGL is 
very poorly supported on windows - a default WinXP installation does not 
come with hardware accelerated OpenGL drivers, afaik, and also I can't find 
any documentation on the Haskell OpenGL bindings except the Haddock type 
signatures which are just not nearly enough to understand how to use it, and 
some out of date docs)

Thanks, Brian.



More information about the Glasgow-haskell-users mailing list