Why cannot top level module be set to main in Hint

2019-07-17 07:13发布

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条回答
ゆ 、 Hurt°
2楼-- · 2019-07-17 07:43

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

查看更多
家丑人穷心不美
3楼-- · 2019-07-17 08:04

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 ;)

查看更多
登录 后发表回答