【问题标题】:ghci not loading function from fileghci没有从文件加载函数
【发布时间】:2023-03-30 19:12:01
【问题描述】:

在 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> 

为什么?如何解决?

【问题讨论】:

    标签: haskell ghci


    【解决方案1】:

    我的猜测是你在源文件中定义了一个主函数。

    如果您定义了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

    【讨论】:

    • 这不是操作的问题,但你解决了我的问题。感谢 *module_name 技巧。
    • 这对我不起作用。首先,在你加载测试之后,提示符看起来像 *Main>,而不是 >>。我还收到一条错误消息,告诉我 :找不到模块 `*fadetoblack':使用 -v 查看搜索的文件列表。
    【解决方案2】:
    1. 从目录中删除 test.hi 和 test.o,然后尝试 ghci test。 [有时当我运行ghc file.hs(而不是ghc --make file.hs)时,它会给出未定义的引用错误,但会创建稍后由ghci 读取的此类文件。也许这是一个错误。]

    2. 试试

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

      在 ghci 中。结果如何?

    【讨论】:

    • 在 OS X 10.11.6 和 GHCI 8.2.2 上,您不必在 &lt;path to your file&gt; 处使用双引号。你可以试试:cd &lt;path to your file&gt;
    【解决方案3】:

    您确定加载的 test.hs 正确吗?也许你在错误的目录中。或者你添加doubleMe的定义后没有保存test.hs。

    【讨论】:

    • 我在 Desktop 中创建了一个文件夹,而不是文件夹的快捷方式。
    【解决方案4】:

    这也发生在我身上——万一其他人碰到它并偶然发现这个页面,我的问题是我运行 GHCI 的虚拟机磁盘空间不足——提示它尝试加载一个空文件每次。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多