Questions tagged [haskell-ffi]
17 questions
9
votes
1 answer
How to link to a C# (i.e., managed) DLL from Haskell?
I am trying to build a Windows DLL from my Haskell code. The functions in this DLL are supposed to be called from a managed code in C#. And, atleast one of the function (defined in the c# code) is to be called from a function in this DLL.
At the…

arvindd
- 346
- 2
- 8
5
votes
1 answer
Haskell FFI: Wrapping a C struct containing a separately allocated string (char*)
Suppose you have a C-struct
typedef struct {
uint32_t num;
char* str;
} MyStruct;
and a function f that does some operation on it,
void f(MyStruct* p);
The C API demands that the char* be allocated a sufficient buffer before calling…

mcmayer
- 1,931
- 12
- 22
2
votes
0 answers
Is it possible to program GPU in pure Haskell?
I wonder, whether I can write code, which ought to be evaluated by GPU, in pure Haskell.
For example, a function for multiplication of two large matrices in GPU have (theoretically) no side effects, therefore it might be possible to call it from…

Přemysl Šťastný
- 1,676
- 2
- 18
- 39
2
votes
1 answer
How do I use a FunPtr from Haskell?
Assuming the following files:
test2.h:
typedef int (*signature) ();
extern const signature lol2;
test2.c:
#include "test2.h"
int lol() {
return 42;
}
const signature lol2 = lol;
Test2.hs:
module Main where
import Foreign.C
import…

Janus Troelsen
- 20,267
- 14
- 135
- 196
1
vote
1 answer
Do variables initialized in C header files only get allocated once when a function from that header file is called from Haskell?
Assuming that every source c file using a variable initialized in a C header file is declared using extern, that variable should be only allocated once no matter how many times a function defined in a source c file including that header and…

Anon
- 375
- 1
- 7
1
vote
1 answer
Why am I getting parse error on input "{"
I am trying to run the very first example in this tutorial:
Call JVM Methods from Haskell
module Main where
{-# LANGUAGE QuasiQuotes #-}
{-# OPTIONS_GHC -fplugin=Language.Java.Inline.Plugin #-}
import Language.Java (withJVM)
import…

James Strieter
- 775
- 3
- 10
1
vote
1 answer
UB due to allocaArray automatic cleanup or not?
i have this function in my code that seems to work just fine:
-- type declaration just for reference, i don't have it in my actual code
retrieveVulkanArray :: Storable a => (Ptr Word32 -> Ptr a -> IO b) -> IO (Ptr a, Int)
retrieveVulkanArray' f =
…

Pavel Beliy
- 494
- 1
- 3
- 14
1
vote
1 answer
Most efficient way of converting a Data.ByteString.Lazy to a CStringLen
I need to encode some data to JSON and then push is to the syslog using hsyslog. The types of the two relevant functions are:
Aeson.encode :: a -> Data.ByteString.Lazy.ByteString
System.Posix.Syslog.syslog :: Maybe Facility
…

Saurabh Nanda
- 6,373
- 5
- 31
- 60
1
vote
1 answer
Stack Build Cannot Find Local Library
I successfully replicated this example using GHC by itself.
https://wiki.haskell.org/Calling_Haskell_from_C
The end goal is to write 99% of my program in Haskell, and then call it from an event loop written in C:
#include
#ifdef…

James Strieter
- 775
- 3
- 10
1
vote
0 answers
how to generate static library with Haskell FFI?
to compile a haskell library to use in C++ today I use stack ghc:
stack ghc -- -c -dynamic -fPIC -XHaskell2010 -XOverloadedStrings TestFFI.hs
stack ghc -- --make -dynamic -shared -fPIC -XHaskell2010 -XOverloadedStrings TestFFI.hs -o…

Alex
- 3,301
- 4
- 29
- 43
0
votes
2 answers
Open file in haskell, passing filepath in C FFI call (CString)
I want to open a file in Haskell, but I want the top level function to be called from C (I want to pass the filepath from C).
I'm having trouble getting the filepath CString into a type that I can use readFile on.
Here's my first attempt, adapting…

Aidenhjj
- 1,249
- 1
- 14
- 27
0
votes
0 answers
Haskell MPFR instance for Storable results in Exeception
I've been using the hmpfr package for arbitrary precision numbers in haskell.
For efficiency i would like to use Storable Vectors instead of Boxed. There is a Storable instance for MPFR. However, whenever i use MPFR type numbers with Storable…

Anon
- 375
- 1
- 7
0
votes
1 answer
How to elegantly represent finite Haskell recursive datastructure in Python?
Let's have some finite recursive datastructure in Haskell. Eg.
data Tree = Node Tree Tree | Nil
I need to be able to load such datastructure from Haskell to Python, change it and return it back to Haskell.
Is there some standard/elegant way how to…

Přemysl Šťastný
- 1,676
- 2
- 18
- 39
0
votes
0 answers
Haskell function that takes and returns a 'c' array
I'm looking for a complete working example of a Haskell FFI function that conceptually has signature: ByteString -> ByteString, i.e. which:
Takes as input an array of 8-bit unsigned integers which has been allocated on the 'c' heap (i.e. with a…

NietzscheanAI
- 966
- 6
- 16
0
votes
1 answer
How to Call C++ Setters & Getters from Haskell
I know how to call pure C++ functions from Haskell, but wondering how to get GHC to accept functions that have side effects.
I want Haskell to have read-only access to a C++ linked list, and exclusive write access to a C++ variable. For…

James Strieter
- 775
- 3
- 10