Why cannot top level module be set to main in Hint

403 Views Asked by At

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"
2

There are 2 best solutions below

1
On BEST ANSWER

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"].

0
On

It would appear that it compiles the code OK, but then when it goes back to load the current interpreted modules, a problem occurs.

It loads Main with findModule, but, apparently, loads the wrong Main: It's loading the application Main, which indeed was not interpreted, sees that, and dies.

(Though I should add I haven't used Hint so I'm kind of guessing ;)