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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T04:41:57+00:00 2026-06-06T04:41:57+00:00

i.e. is it possible to embed Haskell code in a C library so that

  • 0

i.e. is it possible to embed Haskell code in a C library so that the user of the library doesn’t have to know Haskell is being used? In particular, so that the user could use multiple libraries that embed Haskell, without any conflicts?

As far as I understand things, you embed between calls to hs_init and hs_exit, but these involve global state shenanigans and should conflict with other calls, no?

  • 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-06T04:41:58+00:00Added an answer on June 6, 2026 at 4:41 am

    Yes, it’s possible to call Haskell code from C (and vice versa) through FFI, the Foreign Function Interface. Unfortunately, as the haskell.org docs says, you can’t avoid the calls to initialize and finalize the haskell environment:

    The call to hs_init() initializes GHC’s runtime system. Do NOT try to
    invoke any Haskell functions before calling hs_init(): bad things will
    undoubtedly happen.

    But, this is interesting also:

    There can be multiple calls to hs_init(), but each one should be
    matched by one (and only one) call to hs_exit()

    And furthermore:

    The FFI spec requires the implementation to support re-initialising
    itself after being shut down with hs_exit(), but GHC does not
    currently support that.

    Basically my idea is that you may exploit this specifications in order to write youself a wrapper C++ class that manages the calls to hs_init and hs_exit for you, in example by using template methods surrounded by hs_init and hs_exit that you can override using any haskell call you want.
    However, beware of interactions with other libraries calling haskell code: nested layers of calls to hs_init and hs_exit should be OK (so it’s safe to use libraries which calls them in between your wrappers), but the total number of calls should always match, meaning that if those libraries only initialize the environment without trying to close it, then it’s up to you to finish the job.

    Another (probably better) idea, without exploiting inheritance and overriding, may be to have a simple class HaskellEnv that calls hs_init in the constructor and hs_exit in the destructor. If you declare them as automatic variables, you’ll obtain that the calls to hs_init and hs_exit will always be matched, and the latest call to hs_exit will be made as soon as the latest HaskellEnv object is destructed when you leave its scope.
    Have a look at this question in order to prevent the creation of objects on the heap (they may be dangerous in this case).

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

Sidebar

Related Questions

I know that it's possible to embed @Html in Javascript in MVC3, but I
Here is the code that I use to embed video from YouTube to my
Is it possible to embed a custom binary file inside a C# class library
Is it possible to embed paypal in an IFRAME. I have tried to do
Is it possible to embed images an email being sent from my app? Can
Is it possible to embed a windows form within another windows form? I have
Is it possible to embed a DOS console in a Windows Form or User
I use MVCMailer with asp.net MVC 3. It's a great library but I have
It's possible to embed php code in symfony's yml fixture file. My table entry
I've learned that it's possible to embed an image in an HTML page like

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.