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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:55:58+00:00 2026-06-12T17:55:58+00:00

I have been trying to get some basic dynamic code compilation working using the

  • 0

I have been trying to get some basic dynamic code compilation working using the GHC API by following a tutorial found here.

This code:

import GHC
import GHC.Paths
import DynFlags
import Unsafe.Coerce

main :: IO ()
main =
    defaultErrorHandler defaultDynFlags $ do
      func <- runGhc (Just libdir) $ do
        dflags <- getSessionDynFlags
        setSessionDynFlags dflags
        target <- guessTarget "Test.hs" Nothing
        addTarget target
        r <- load LoadAllTargets
        case r of
          Failed -> error "Compilation failed"
          Succeeded -> do
            m <- findModule (mkModuleName "Test") Nothing
            setContext [] [m]
            value <- compileExpr ("Test.print")
            do let value' = (unsafeCoerce value) :: String -> IO ()
               return value'
      func "Hello"
      return ()

Should get the print function from another file called Test.hs, load it and run its print function.

I compile the code with ghc version 7.4.1 using the command:

ghc -package ghc --make Api.hs

But receive the following error:

Api.hs:8:25:
    Couldn't match expected type `Severity' with actual type `Settings'
    Expected type: LogAction
      Actual type: Settings -> DynFlags
    In the first argument of `defaultErrorHandler', namely
      `defaultDynFlags'
    In the expression: defaultErrorHandler defaultDynFlags

What am I doing wrong? I have checked the GHC API docs but am not well versed enough in this kind of thing to understand most of it.

  • 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-12T17:56:00+00:00Added an answer on June 12, 2026 at 5:56 pm

    The tutorial is out of date. In ghc-7.0.* and previous, the type of defaultErorHandler was

    defaultErrorHandler :: (ExceptionMonad m, MonadIO m) => DynFlags -> m a -> m a
    

    and defaultDynFlags was just a value.

    As of ghc-7.2.*, the type of defaultErrorHandler is

    defaultErrorHandler :: (ExceptionMonad m, MonadIO m) => LogAction -> m a -> m a
    

    defaultDynFlags is a function

    defaultDynFlags :: Settings -> DynFlags
    

    and LogAction is a synonym

    type LogAction = Severity -> SrcSpan -> PprStyle -> Message -> IO ()
    

    In 7.6, it has changed again, we now have

    defaultErrorHandler :: (ExceptionMonad m, MonadIO m) => FatalMessager -> FlushOut -> m a -> m a
    

    with

    type FatalMessager = String -> IO ()
    

    and FlushOut being a newtype wrapper around IO ().

    I’m not very familiar with the GHC Api (a too fast-moving target for me), so I’m not sure how the working code should look like, but for the 7.2 and 7.4 series, the first argument to defaultErrorHandler should probably be defaultLogAction.

    Also the type of setContext has changed, I don’t know if what I have does what you want, but it compiles (with 7.4.2; but you also need the ghc-paths package in addition to ghc for the GHC.Paths module) – I haven’t tried to run it, though.

    import GHC
    import GHC.Paths
    import DynFlags
    import Unsafe.Coerce
    
    main :: IO ()
    main =
        defaultErrorHandler defaultLogAction $ do
          func <- runGhc (Just libdir) $ do
            dflags <- getSessionDynFlags
            setSessionDynFlags dflags
            target <- guessTarget "Test.hs" Nothing
            addTarget target
            r <- load LoadAllTargets
            case r of
              Failed -> error "Compilation failed"
              Succeeded -> do
                m <- findModule (mkModuleName "Test") Nothing
                setContext [IIModule m]
                value <- compileExpr ("Test.print")
                do let value' = (unsafeCoerce value) :: String -> IO ()
                   return value'
          func "Hello"
          return ()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Building my Django app, I have been using some code from django-basic-app , in
I have been trying all day to get some data properly formatted in a
I have been trying to get my head around some errors I get when
I have been trying to get my Arduino/Eclipse environment setup. For some reason I
I've been trying to get the UNIQUE constraint placed on some attributes I have
I have been trying to get a handle on using MVC 4.0 with EF
I am trying to do some basic geocoding using the google maps API and
I have been trying to do some basic communications between a BlackBerry and a
I've been trying to get a basic socket connection working between my server and
I have been trying to get this code to update, and it just will

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.