Search code examples
haskellghchintghc-api

Why cannot top level module be set to main in Hint


Why cannot top level module be set to "Main" in Hint (Language.Haskell.Interpreter)?

Allow me to demonstrate:

module Main where

import Language.Haskell.Interpreter
import Control.Monad

main = do 
  res <- runInterpreter (test "test")
  case res of
       Left e -> putStrLn (show e)
       Right t -> putStrLn (show t) 
  return ()

test :: String -> Interpreter ()
test mname = 
  do
    loadModules [mname ++ ".hs"]
    setTopLevelModules ["Main"]

Will result in:

NotAllowed "These modules are not interpreted:\nMain\n"

Solution

  • As the documentation says, top level modules have to be interpreted, i.e. not compiled.

    When loading a module, a compiled version will be used if it's available. The GHCi manual has more detailed information on this.

    I'm guessing there's a test.o and test.hi in the same folder from an earlier build. I was able to reproduce the error with these files present. Deleting them solves the problem, as the module will then be interpreted.

    You can also force a module to be loaded in interpreted mode by prefixing the module name with an asterisk, e.g. loadModules ["*" ++ mname ++ ".hs"].