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 6677311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:06:11+00:00 2026-05-26T04:06:11+00:00

Let’s say charm.c has an enum key and a function get_key() that returns a

  • 0

Let’s say charm.c has an enum key and a function get_key() that returns a key type value.

How can I expose a corresponding Haskell Key record and function getKey :: IO Key?

And how can I do this without manually specifying how every single enum value maps to a Haskell value?

  • 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-26T04:06:12+00:00Added an answer on May 26, 2026 at 4:06 am

    For @KevinReid, here’s an example of how to do this with c2hs.

    Given the enum key in the file charm.h (I have no idea what’s in the enum, so I just filled in a few values)

    typedef enum
      {
        PLAIN_KEY = 0,
        SPECIAL_KEY  = 1,
        NO_KEY = 2
      }
    key;
    
    key get_key();
    

    You can use c2hs’s enum hook like this:

    {#enum key as Key {underscoreToCase} deriving (Eq, Show)#}
    

    To bind to a function, you can use either call or fun. call is simpler, but doesn’t do any marshaling. Here are examples of both. The ffi-wrapped get_key will return a CInt, so you need to either manually marshal it (if using call) or specify the marshaller (if using fun). c2hs doesn’t include enum marshallers so I’ve written my own here:

    module Interface  where -- file Interface.chs
    
    {#enum key as Key {underscoreToCase} deriving (Eq, Show)#}
    
    getKey = cIntToEnum `fmap` {#call get_key #}
    
    {#fun get_key as getKey2 { } -> `Key' cIntToEnum #}
    
    cIntToEnum :: Enum a => CInt -> a
    cIntToEnum = toEnum . cIntConv
    

    C2hs will generate the following Haskell from this (slightly cleaned up):

    data Key = PlainKey
             | SpecialKey
             | NoKey
             deriving (Eq,Show)
    instance Enum Key where
      fromEnum PlainKey = 0
      fromEnum SpecialKey = 1
      fromEnum NoKey = 2
    
      toEnum 0 = PlainKey
      toEnum 1 = SpecialKey
      toEnum 2 = NoKey
      toEnum unmatched = error ("Key.toEnum: Cannot match " ++ show unmatched)
    
    getKey = cIntToEnum `fmap` get_key
    
    getKey2 :: IO (Key)
    getKey2 =
      getKey2'_ >>= \res ->
      let {res' = cIntToEnum res} in
      return (res')
    
    cIntToEnum :: Enum a => CInt -> a
    cIntToEnum = toEnum . cIntConv
    
    foreign import ccall safe "foo.chs.h get_key"
      get_key :: (IO CInt)
    
    foreign import ccall safe "foo.chs.h get_key"
      getKey2'_ :: (IO CInt)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say i have an android device that has some extra buttons on it,
Let's say that I am using a 3rd party utility tool and I can't
Let's say we have a simple function defined in a pseudo language. List<Numbers> SortNumbers(List<Numbers>
Let's say I'm writing a PHP (>= 5.0) class that's meant to be a
Let's say that we have an ARGB color: Color argb = Color.FromARGB(127, 69, 12,
Let's say that I have an arbitrary string like `A man + a plan
Let's say that I'm currently designing an application where I will need to use
Let's say I have two entities: Physician Credentials And a physician can have many
Let's say I have a table that looks something like this: ------------------------------- id|column2|column3 |column4
Let's say I have some text as follows: do this, do that, then this,

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.