I just spent the last week or so figuring out how to execute C++ code from C# as part of my day job. It took us forever to figure it out, but the final solution is fairly simple.
Now I'm curious... How hard would it be to call Haskell from C#? (Note carefully: That's call Haskell from C#, not the other way around. So the main executable is C#.)
If it's really hard, I won't bother. But if it's reasonably easy, I might have to have a play with it...
Basically, we wrote some C++ code. On Windows it gets compiled into a DLL, on Linux it gets compiled into a shared object (*.so
). Then on the C# side you do a DllImport
and write some manual memory management code if you're trying to pass anything nontrivial across. (E.g., arrays, strings, etc.)
I know GHC is supposed to support building shared libraries on both platforms, but I'm not sure of the technical details. What's the syntax for exporting stuff, and does the caller have to do anything special to initialise the DLL first?
To be concrete: Suppose there exists a function foobar :: FilePath -> IO Int32
. Can somebody throw together a small sketch showing:
- What Haskell declarations I need to write to expose this to the outside world.
- How do I tell GHC to build a single self-contained DLL / SO file.
- Anything special the caller needs to do, beyond the usual process of binding
foobar
itself.
I'm not too worried about the actual syntax for the C# side; I think I've more or less puzzled that out.
P.S. I did briefly look at hs-dotnet
, but this appears to be Windows-specific. (I.e., won't work with Mono, so won't work on Linux.)
As far as both languages are concerned, you can basically pretend you're trying to interface with C code.
This is a complex topic, so rather than try to explain all of it, I will focus on making a simple example that you can build on using the resources linked below.
First, you need to write wrappers for your Haskell functions that use types from the
Foreign.C.*
modules instead of the usual haskell types.CInt
instead ofInt
,CString
instead ofString
, etc. This is the most complicated step, especially when you have to deal with user-defined types.You also have to write
foreign export
declarations for those functions using theForeignFunctionInterface
extension.Then, when compiling, you tell GHC to make a shared library:
From the C# side, in addition to importing the function you want to call, you also have to import
hs_init()
and call it to initialize the runtime system before you can call any Haskell functions. You should also callhs_exit()
when you're done.Now we compile and run:
It works!
Resources:
For reference, I was able to get the following procedure to work under Windows...
Compile this with
This produces half a dozen files, one of which is
HSdll.dll
. I then copied that into a Visual Studio C# project, and did the following:The
Console.WriteLine()
calls are obviously optional.I haven't tried running this under Mono / Linux yet, but it's presumably similar.
In summary, it's approximately the same difficulty as getting a C++ DLL to work. (I.e., getting the type signatures to match up and making marshaling work correctly is the hard bit.)
I also had to edit the project settings and select "allow unsafe code".