Marshalling arrays without flakiness?

Hi, I would like to know whether there is a good way to marshal the following structure to C without using pointer arithmetic done by a programmer (as opposed to a tool). typedef struct{ double a[10]; double b[10]; double b[10]; } foo; I don't need this functionality, but it would make Haskell -> C interfaces less flaky. Basically, writing Haskell -> C interfaces seem to be a very unpopular thing to do (as opposed to C -> Haskell interfaces). For example, on Hackage (actually a checkout of a few months ago) there is only one package using pokeArray and it is not pretty; it uses pointer arithmetic and all other kinds of hard-coded addresses. If I change the order of the C fields it will break, if I change the length of the buffer they use, it breaks. Just look at it, and it will break ;) If you are a bit creative, it is possible to make it work already, by using the best features of both hsc2hs and c2hs, but it is hardly elegant. I suppose the real solution would be to extend c2hs with an offsetof function (for which patches already exist, btw), but it might be that I have missed some other solution. -- Best Regards, Ron de Bruijn, Developer, Gamr7

I would like to know whether there is a good way to marshal the following structure to C without using pointer arithmetic done by a programmer (as opposed to a tool).
typedef struct{ double a[10]; double b[10]; double b[10]; } foo;
With my my 'bindings-common' package it's done like this:
#starttype struct foo
#array_field a , CDouble
#array_field b , CDouble
#array_field c , CDouble
#stoptype
There's no problem if you change the order of the fields, or
ommit one of them. To be consistent with Marshal.Array, Haskell
datatype fields corresponding to a, b and c will be lists. If you
try to poke some value with list fields containing more than 10
elements, only the first 10 will be stored. That size is detected
automatically.
If you want to check how does the code you write compare to
the Haskell code you get, you can check this binding to posix

I would like to know whether there is a good way to marshal the following structure to C without using pointer arithmetic done by a programmer (as opposed to a tool).
Here is an example with poke. It's completely useless, except for
demonstration :) You have a global variable in C that receives
data in a struct, and a 'print' function that prints it.
*** C header: ***
#define NPOWERS 6
typedef struct {
double number;
double negpowers[NPOWERS];
} power_struct;
extern power_struct np;
void print_np (void);
*** C definition: ***
power_struct np;
void print_np (void)
{
int i;
printf("\n");
printf ("Negative powers of %g: \n",np.number);
for (i=0;i<=NPOWERS;i++) printf(" %g",np.negpowers[i]);
printf("\n");
}
*** Binding module, with hsc2hs macros: ***
#starttype power_struct
#field number , CDouble
#array_field negpowers , CDouble
#stoptype
#globalvar np ,
participants (2)
-
Maurício CA
-
Ron de Bruijn