5
votes

I am able to export simple haskell functions through FFI containing standard data types. However, I do not know how to export a function that takes data type other than standard data types.

E.g.

data SomeType a = SomeType a
data SomeOtherType b = SomeOtherType b

doSomething:: SomeType a -> SomeOtherType b

How can I export function doSomething?

The existing documents talks about very simple examples.

2
You can only export functions that work on the types that C knows about.augustss
Related (duplicate?) question: stackoverflow.com/questions/4502115/…John L

2 Answers

2
votes

The alternative is to lift the function to use StablePtr, and export the function with the StablePtr. Of Course as pointed out in the above answer you have use the function with instance that cane be exported.

It would be great to use something like H/Direct to plumb this code into c++ objects so that it can be accessed as object.

1
votes

Short answer is, you can't.

You need to pick an instance of the function and export that.

e.g. doSomething :: SomeType Int -> SomeOtherType Int is exportable. I wrote a longer answer here that might be helpful

The reason is that the Haskell side needs to know how to marshall the structure, how much memory to allocate etc.