Grégoire Locqueville on Nostr: I'm trying my hand at writing #haskell bindings to a C library! It's definitely a ...
I'm trying my hand at writing #haskell bindings to a C library! It's definitely a little rough but I'm making good progress 😅
Question:
The C library has functions that produce an array but expect you to manage memory by yourself — so basically, you allocate an array, give the pointer (as well as some other arguments) to the C library function, and the function writes into the array and returns nothing.
In Haskell, this translates to a function `SomeArguments -> Ptr CDouble -> IO ()`. I would like to use that function to define a higher-level binding to that function, in the form of a function of type `SomeArguments -> Vector Double`. What's the best way to do that? This seems like a common enough idiom in C that I assume someone has had to deal with the same thing before, maybe there are some examples out there?
Published at
2025-03-24 16:44:08Event JSON
{
"id": "6cefec62e44c94bf79dce0e695c9dea5ab3989c1637d5be9e32448be5843fc7b",
"pubkey": "da2ffb9942d327b65ee66f152d69a427f114a4a42f69a4d9b88ab9cab96339b9",
"created_at": 1742834648,
"kind": 1,
"tags": [
[
"t",
"haskell"
],
[
"proxy",
"https://mathstodon.xyz/users/glocq/statuses/114218411537219956",
"activitypub"
]
],
"content": "I'm trying my hand at writing #haskell bindings to a C library! It's definitely a little rough but I'm making good progress 😅\n\nQuestion:\n\nThe C library has functions that produce an array but expect you to manage memory by yourself — so basically, you allocate an array, give the pointer (as well as some other arguments) to the C library function, and the function writes into the array and returns nothing.\n\nIn Haskell, this translates to a function `SomeArguments -\u003e Ptr CDouble -\u003e IO ()`. I would like to use that function to define a higher-level binding to that function, in the form of a function of type `SomeArguments -\u003e Vector Double`. What's the best way to do that? This seems like a common enough idiom in C that I assume someone has had to deal with the same thing before, maybe there are some examples out there?",
"sig": "fc2281acfb9156a14680835d5f973989e8894cff60367fe258a02b121ec185a662366bab508757e14a5f39db5320f5a0e7c360c7d225b2b84902a17c1469811d"
}