ghci 未从文件加载函数

发布于 2024-09-03 12:58:44 字数 323 浏览 4 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我乃一代侩神 2024-09-10 12:58:44

我的猜测是您已经在源文件中定义了一个 main 函数。

如果您定义了 main 函数,则使用 :l test 加载模块不会导入除 main 之外的任何函数。在这种情况下,您可以通过在模块名称前面添加星号来加载它::l *test
原因是编译后的二进制文件会隐藏未导出的顶级函数。前面加上星号会强制 GHCi 忽略预编译模块 (test) 并解释源文件 (test.hs)。

[jkramer/sgi5k:.../haskell]# cat test.hs 

main = do
    print $ doubleMe 2

doubleMe x = x + x

[jkramer/sgi5k:.../haskell]# ghc --make test
[jkramer/sgi5k:.../haskell]# ghci
[...some messages...]
>> :l test
Ok, modules loaded: Main.
>> :t doubleMe

<interactive>:1:0: Not in scope: `doubleMe'
>> :l *test
[1 of 1] Compiling Main             ( test.hs, interpreted )
Ok, modules loaded: Main.
>> :t doubleMe
doubleMe :: (Num a) => a -> a

检查这些链接以获取更多信息:

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 but main. 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).

[jkramer/sgi5k:.../haskell]# cat test.hs 

main = do
    print $ doubleMe 2

doubleMe x = x + x

[jkramer/sgi5k:.../haskell]# ghc --make test
[jkramer/sgi5k:.../haskell]# ghci
[...some messages...]
>> :l test
Ok, modules loaded: Main.
>> :t doubleMe

<interactive>:1:0: Not in scope: `doubleMe'
>> :l *test
[1 of 1] Compiling Main             ( test.hs, interpreted )
Ok, modules loaded: Main.
>> :t doubleMe
doubleMe :: (Num a) => a -> a

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

知你几分 2024-09-10 12:58:44
  1. 从目录中删除 test.hi 和 test.o,然后尝试 ghci test。 [有时,当我运行 ghc file.hs (而不是 ghc --make file.hs)时,它会给出未定义的引用错误,但会创建由 读取的此类文件>ghci 稍后。也许这是一个错误。]

  2. 尝试

    :cd "<文件路径>"
    :l 测试
    :浏览
    

    在 ghci 中。结果如何?

  1. Remove test.hi and test.o from the directory and then try ghci test. [Sometimes when I run ghc file.hs (and not ghc --make file.hs) it gives undefined reference error, but creates such files that are read by ghci later. Maybe this is a bug.]

  2. Try

    :cd "<path to your file>"
    :l test
    :browse
    

    in ghci. What is the result?

一江春梦 2024-09-10 12:58:44

您确定正在加载正确的 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.

三人与歌 2024-09-10 12:58:44

这也发生在我身上 - 如果其他人遇到它并偶然发现此页面,我的问题是我运行 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文