Why cannot top level module be set to main in Hint

做~自己de王妃 提交于 2019-12-11 03:13:06

问题


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"

回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/7134520/why-cannot-top-level-module-be-set-to-main-in-hint

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!