为什么不能在提示中将顶级模块设置为 main
为什么不能在提示(Language.Haskell.Interpreter)中将顶级模块设置为“Main”?
请允许我演示:
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"]
将导致:
NotAllowed "These modules are not interpreted:\nMain\n"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
作为 文档说,顶级模块必须被解释,即不被编译。
加载模块时,如果可用,将使用编译版本。 GHCi 手册有更详细的信息这。
我猜测早期版本的同一个文件夹中有一个
test.o
和test.hi
。我能够使用这些文件重现该错误。删除它们可以解决问题,因为模块将被解释。您还可以通过在模块名称前添加星号来强制以解释模式加载模块,例如
loadModules ["*" ++ mname ++ ".hs"]
。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
andtest.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"]
.看起来它编译代码没问题,但是当它返回加载当前解释的模块时,出现了问题。
它使用 Main rel="nofollow">findModule,但是,显然,加载了错误的
Main
:它正在加载应用程序Main
,它确实没有被解释,看见了,就死了。(虽然我应该补充一下,我没有使用过提示,所以我有点猜测;)
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 wrongMain
: It's loading the applicationMain
, which indeed was not interpreted, sees that, and dies.(Though I should add I haven't used Hint so I'm kind of guessing ;)