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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T20:04:26+00:00 2026-05-29T20:04:26+00:00

I have an existing Haskell function that uses the GHC API to dynamically load

  • 0

I have an existing Haskell function that uses the GHC API to dynamically load compiled code from a module. It is based on the code from the blog post Dynamic Compilation and Loading of Modules in Haskell.

The code works fine in GHC 7.0, but had to be slightly modified to compile in GHC 7.2, because the GHC API changed.

The code now throws a runtime error in GHC 7.2:

mkTopLevEnv: not a home module (module name):(function name)

The code is

evalfuncLoadFFI String moduleName, 
                String externalFuncName, 
                String internalFuncName = do

  result <- liftIO $ defaultRunGhc $ do
    dynflags <- GHC.getSessionDynFlags
    _ <- GHC.setSessionDynFlags dynflags
    m <- GHC.findModule (GHC.mkModuleName moduleName) Nothing

--------------------------------------------------------    
-- The following code works fine in GHC 7.0.4:
--
--  GHC.setContext [] [(m, Nothing)]
--
-- This new code attempts to set context to the module, 
-- but throws an error in GHC 7.2:
--
    (_,oi) <- GHC.getContext
    GHC.setContext [m] oi
--------------------------------------------------------

    fetched <- GHC.compileExpr (moduleName ++ "." ++ externalFuncName)
    return (Unsafe.Coerce.unsafeCoerce fetched :: [LispVal] -> IOThrowsError LispVal)
  defineVar env internalFuncName (IOFunc result)

For reference, the full code is available online in FFI.hs (github.com).

Does anyone have any idea how to fix or work around this problem?

Also, could this be caused by the new Safe Haskell changes in GHC 7.2, or is it just due to modifications to the GHC API?

  • 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-29T20:04:27+00:00Added an answer on May 29, 2026 at 8:04 pm

    The current module context is reserved for modules that are currently being compiled, i.e. when you specify modules in the context, they must explicitly not be external.

    Instead, you should specify the wanted module as an import, in the second argument of setContext. This can be done like so:

    GHC.setContext []
      -- import qualified Module
      [ (GHC.simpleImportDecl . GHC.mkModuleName $ moduleName)
        { GHC.ideclQualified = True
        }
      -- -- import qualified Data.Dynamic
      -- , (GHC.simpleImportDecl . GHC.mkModuleName $ "Data.Dynamic")
      --   { GHC.ideclQualified = True
      --   }
      ]
    fetched <- GHC.compileExpr $ moduleName ++ "." ++ externalFuncName
    return . unsafeCoerce $ fetched
    -- or:
    -- fetched <- GHC.dynCompileExpr $ moduleName ++ "." ++ externalFuncName
    -- return . fromDynamic (error "Illegal type cast") $ fetched
    

    PS: it might be a good idea to use GHC.dynCompileExpr instead, so that you can avoid the unsafeCoerce. You must add a qualified import for Data.Dynamic in the context for it to work, but a Data.Dynamic.Dynamic value is generally nicer to work with, since you can handle type errors more gracefully. I’ve added the code for that as comments in the above code.


    Update

    And here is the syntax for GHC 7.4.1:

    GHC.setContext  
      -- import qualified Module
      [ GHC.IIDecl $ 
        (GHC.simpleImportDecl . GHC.mkModuleName $ moduleName)
        {GHC.ideclQualified = True}
      ]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have existing code for an ASP .NET application that uses reflection to load
I have existing code that uses CMNewProfileSearch to find then iterate over the color
I have existing java code and need to create Design Document based on that.
I have: existing object oriented native code API (non GUI) GUI application that works
i have existing code that generates a spreadsheet from excel using OpenXML. It works
I have existing chat server code written in C/C++ that communicates over TCP/IP with
I have existing code that's like: final Term t = /* ... */; final
I currently have existing code that automates and email and sends files. I now
I'm working on a project where the client wants to have existing pages dynamically
For whatever reason, I have a lot of clients that have existing data that's

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.