Skip to content

User Guide: Example in section 14.2 doesn't compile / typecheck

The example in 14.2 Using GHC as a Library doesn't compile and even typecheck anymore.

https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/extending_ghc.html#using-ghc-as-a-library

I created a little cabal project (https://github.com/supersven/ghc-api-doc-bug) with the example code taken from the user guide.

Building it leads to this type error message:

[nix-shell:~/projects/ghc-api-doc-bug]$ cabal new-build
Build profile: -w ghc-8.6.3 -O1
In order, the following will be built (use -v for more details):
 - ghc-api-doc-bug-0.1.0.0 (exe:ghc-api-doc-bug) (first run)
Preprocessing executable 'ghc-api-doc-bug' for ghc-api-doc-bug-0.1.0.0..
Building executable 'ghc-api-doc-bug' for ghc-api-doc-bug-0.1.0.0..
[1 of 1] Compiling Main             ( Main.hs, /home/sven/projects/ghc-api-doc-bug/dist-newstyle/build/x86_64-linux/ghc-8.6.3/ghc-api-doc-bug-0.1.0.0/x/ghc-api-doc-bug/build/ghc-api-doc-bug/ghc-api-doc-bug-tmp/Main.o )

Main.hs:5:1: error:
    * Couldn't match expected type `IO t0'
                  with actual type `m0 a0 -> m0 a0'
    * Probable cause: `main' is applied to too few arguments
      In the expression: main
      When checking the type of the IO action `main'
  |
5 | main =
  | ^

Main.hs:6:25: error:
    * Couldn't match type `DynFlags.WarnReason
                           -> Severity
                           -> SrcSpan
                           -> Outputable.PprStyle
                           -> ErrUtils.MsgDoc
                           -> IO ()'
                     with `IO ()'
      Expected type: DynFlags.FatalMessager
        Actual type: DynFlags.LogAction
    * Probable cause: `defaultLogAction' is applied to too few arguments
      In the first argument of `defaultErrorHandler', namely
        `defaultLogAction'
      In the expression: defaultErrorHandler defaultLogAction
      In the expression:
        defaultErrorHandler defaultLogAction
          $ do runGhc (Just libdir)
                 $ do dflags <- getSessionDynFlags
                      setSessionDynFlags dflags
                      ....
  |
6 |     defaultErrorHandler defaultLogAction $ do
  |                         ^^^^^^^^^^^^^^^^

Main.hs:7:7: error:
    * Couldn't match expected type `DynFlags.FlushOut'
                  with actual type `IO SuccessFlag'
    * In a stmt of a 'do' block:
        runGhc (Just libdir)
          $ do dflags <- getSessionDynFlags
               setSessionDynFlags dflags
               target <- guessTarget "test_main.hs" Nothing
               setTargets [target]
               ....
      In the second argument of `($)', namely
        `do runGhc (Just libdir)
              $ do dflags <- getSessionDynFlags
                   setSessionDynFlags dflags
                   ....'
      In the expression:
        defaultErrorHandler defaultLogAction
          $ do runGhc (Just libdir)
                 $ do dflags <- getSessionDynFlags
                      setSessionDynFlags dflags
                      ....
  |
7 |       runGhc (Just libdir) $ do
  |       ^^^^^^^^^^^^^^^^^^^^^^^^^...

I would suggest this replacement for the example (Like A Simple Example from https://wiki.haskell.org/GHC/As_a_library):

import GHC
import GHC.Paths ( libdir )
import DynFlags

main =
    defaultErrorHandler defaultFatalMessager defaultFlushOut $ do
      runGhc (Just libdir) $ do
        dflags <- getSessionDynFlags
        setSessionDynFlags dflags
        target <- guessTarget "test_main.hs" Nothing
        setTargets [target]
        load LoadAllTargets
Edited by Sven Tennie
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information