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

  • SEARCH
  • Home
  • 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 9011357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:42:24+00:00 2026-06-16T02:42:24+00:00

In my Haskell program i have an ADT with many constructors: data MyData =

  • 0

In my Haskell program i have an ADT with many constructors:

data MyData = Con1 |
    Con2 |
    ...
    Con20

I have an foreign export ccall function, which wraps [MyData] into array of StablePtr's. After calling it i need to determine which constructor have been used to construct each element.

It could be solved this way

foreign export ccall getType :: StablePtr MyData -> IO CInt
getType (Con1) = return 1
getType (Con2) = return 2
...

but then i would need to manually define these constants in C header. This is error-prone, so i wonder if there is a way to make GHC do this job for me.

  • 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-06-16T02:42:25+00:00Added an answer on June 16, 2026 at 2:42 am

    I’ve found solution for this.

    I’ve defined possible constructor types as enum in my C code:

    typedef enum
    {
        MyDataCon1,
        MyDataCon2,
        ...
        MyDataCon20
    } MyDataConstructor;
    

    Then i used C->Haskell enum hook in my Haskell source:

    {#enum MyDataConstructor deriving (Show) #}
    

    After preprocessing this line turns to

    data MyDataConstructor = MyDataCon1
        | MyDataCon2
        ...
        | MyDataCon20
    

    Now i can define getType this way:

    foreign export ccall getType :: StablePtr MyData -> IO CInt
    getType md = do
        md' <- deRefStablePtr md
        case md' of
            Con1 -> return $ fromEnum MyDataCon1
            Con2 -> return $ fromEnum MyDataCon2
            ...
            Con20 -> return $ fromEnum MyDataCon20
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Haskell program which uses Data.Map.! at several places. After executing the
Suppose that in a Haskell program I have some data whose type is something
I'm writing an audio program in Haskell using Portaudio. I have a function that
I have a haskell program that uses Data.Set and Data.IntMap, what flags do I
I have a fully functional redact program written in haskell.it replaces all the words
I have a Haskell program which processes a text file and builds a Map
I'm looking to have my Haskell program read settings from an external file, to
I have some troubles with Haskell's type system. Situation: Following program is taking list
I have a Haskell program that shows a prompt and then accepts input from
I have been struggling to send emails form within a Haskell program for a

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.