Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6854809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:40:48+00:00 2026-05-27T01:40:48+00:00

I am trying to write Haskell FFI binding for some C structs. An example

  • 0

I am trying to write Haskell FFI binding for some C structs. An example is below:

typedef struct s0{int a; 
                  union{unsigned char b; 
                        struct s0*c; 
                        struct{unsigned char d[1];
                         }; };}*S;

My question is how to write the binding for it in chs (for c2hs) or hsc (for hsc2hs) format? I looked into the tutorials for c2hs but either didn’t get enough information, or didn’t understand it in the way, that would have helped me write the chs file for above definition.

I can generate haskell bindings using HSFFIG tool but it uses custom access method HSFFIG.FieldAccess.FieldAccess to define bindings. I prefer to write bindings that use core haskell FFI libraries, not third-party libraries.

Hence, this question about how to write a binding for recursive struct above in hsc format, or chs format that uses only core FFI libraries.

The actual definition is more complex, but once I figure out how to write above struct definition for c2hs or hsc2hs tools, I can go from there. I know Storable instances need to be defined for inner union and struct as well, but I don’t know how to write wrappers for recursive definition like above. Especially, how do you access inside struct/union from outer struct? I looked into HSFFIG definitions, but the access methods are HSFFIG defined access methods. So, I was unable to figure out how to translate it to chs definition that uses only core FFI libraries.

The questions I have seen in StackOverflow seem to be about simpler definitions. If there is a similar answer somewhere else, I will appreciate pointers.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T01:40:48+00:00Added an answer on May 27, 2026 at 1:40 am

    You can’t magic up an equiv data structure in either c2hs or hsc2hs. However, you can do your own marshaling in c2hs with only a little work.

    data MyType = Next MyType | MyChar Char | MyString String | MyEnd
    

    Then use hsc2hs’ newtype pointer functionality to declare a pointer for MyType (ie. s0). Then, write an explicit function using hsc2hs’ accessors to recursively walk your structure and build up your Haskell structure. At each step you test if you’ve hit a null pointer and if so return a MyEnd (or, depending on the data encoding, just check if the int indicating the type in the union is negative one or whatever), otherwise proceed to parse out whatever you have, and if that thing is a pointer, proceed recursively.

    You could do nearly the same thing with hsc2hs as well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently learning Haskell. I am trying to write a function which given
I'm trying to write a Haskell append function... Here's what I have: myappend ::
I'm trying to write the Haskell function 'splitEvery' in Python. Here is it's definition:
I'm trying to write a program in Haskell that gets a list (of integer)
Hello I am trying to write a very simple function in Haskell. However I
I'm trying to learn Haskell, so I decided to write a simple program to
I trying to use OpenCL using HASKELL. I write a simple program converting a
I'm trying to learn Haskell and want to write a small program which prints
I'm trying to write 'fizzbuzz' in haskell using list comprehensions. Why doesn't the following
I'm trying to write a hyperoperation function in haskell. It's usually wrritten as ackermann(a,b,n)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.