ghci 未从文件加载函数
在 test.hs 中,我有:
doubleMe x = x + x
在 ghci 中,我输入:
Prelude> :l test
[1 of 1] Compiling Main ( test.hs, interpreted )
Ok, modules loaded: Main.
*Main> doubleMe 9
<interactive>:1:0: Not in scope: `doubleMe'
*Main>
为什么?如何修复?
In test.hs, I have:
doubleMe x = x + x
In ghci, I type:
Prelude> :l test
[1 of 1] Compiling Main ( test.hs, interpreted )
Ok, modules loaded: Main.
*Main> doubleMe 9
<interactive>:1:0: Not in scope: `doubleMe'
*Main>
Why? How to fix?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我的猜测是您已经在源文件中定义了一个 main 函数。
如果您定义了
main
函数,则使用:l test
加载模块不会导入除main
之外的任何函数。在这种情况下,您可以通过在模块名称前面添加星号来加载它::l *test
。原因是编译后的二进制文件会隐藏未导出的顶级函数。前面加上星号会强制 GHCi 忽略预编译模块 (test) 并解释源文件 (test.hs)。
检查这些链接以获取更多信息:
http:// www.haskell.org/ghc/docs/6.12.2/html/users_guide/ghci-compiled.html
http://www.haskell .org/ghc/docs/6.12.2/html/users_guide/interactive-evaluation.html#ghci-scope
My guess is that you have defined a main function in your source file.
If you have defined a
main
function, loading the module with:l test
won't import any functions butmain
. In that case you can load it by prepending an asterix to the module name::l *test
.The reason is that the compiled binary will hide non-exported top-level functions. Prepending an asterix forces GHCi to ignore the precompiled module (test) and interprete the source file instead (test.hs).
Check these links for further information:
http://www.haskell.org/ghc/docs/6.12.2/html/users_guide/ghci-compiled.html
http://www.haskell.org/ghc/docs/6.12.2/html/users_guide/interactive-evaluation.html#ghci-scope
从目录中删除 test.hi 和 test.o,然后尝试
ghci test
。 [有时,当我运行ghc file.hs
(而不是ghc --make file.hs
)时,它会给出未定义的引用错误,但会创建由读取的此类文件>ghci
稍后。也许这是一个错误。]尝试
在 ghci 中。结果如何?
Remove test.hi and test.o from the directory and then try
ghci test
. [Sometimes when I runghc file.hs
(and notghc --make file.hs
) it gives undefined reference error, but creates such files that are read byghci
later. Maybe this is a bug.]Try
in ghci. What is the result?
您确定正在加载正确的 test.hs 吗?也许您位于错误的目录中。或者也许您在添加 doubleMe 的定义后没有保存 test.hs 。
Are you sure that you're loading the right test.hs? Maybe you're in the wrong directory. Or maybe you didn't save test.hs after adding the definition of doubleMe.
这也发生在我身上 - 如果其他人遇到它并偶然发现此页面,我的问题是我运行 GHCI 的虚拟机磁盘空间不足 - 提示它每次都尝试加载一个空文件。
This happened to me, too - and in case anyone else runs into it and stumbles across this page, my issue was that the VM I was running GHCI in was out of disk space - prompting it to try and load an empty file each time.