Interfacing Tcl with Haskell

Haskell bindings to Tk

These are packages written by Haskell users to allow them to write Tk GUIs in Haskell. They are mainly interfaces to Tcl/Tk.

Tcl bindings to Haskell

This is the bit I'm interested in. How do you write a Tcl extension in Haskell? The short answer is you can't. There is a quite nice Foreign Function Interface defined for Haskell and implemented by GHC and Hugs (a compiler and interpreter resp.), which allows calling both ways - C to Haskell, and Haskell to C. It also provides methods for registering callbacks written in Haskell etc. On the face of it, this would seem to make writing a Tcl extension in Haskell easy, but there is a snag - none of the compilers will build dynamic libraries suitable for using with load. It also looks as if none of them are going to any time in the near future. It looks as though GHC can build dlls on Windows, so if you are on that platform, you are in luck, but everyone else isn't. :-( You can build a custom tclsh/wish with Haskell support compiled in, which might be a way to go for now. Creating an interface layer should be pretty easy as well, as you could map between Tcl types and Haskell types (eg. Tcl_Obj to TclObj), and the types of callbacks/functions could be defined as well.

An example

Here is a complete example of a custom tclsh with a Haskell command. This command takes an integer and returns it. Error checking removed for simplicity: