Cabal repl 无法 :load 库中的模块

4

你好,我在cabal文件中有以下模块:

library task1-lib
    exposed-modules:
        Interpreter,
        Parser
    build-depends: 
        base ^>=4.14.3.0,
        containers,
        mtl,
        parsec
    ghc-options: -Wall -Werror -Wcompat -Widentities -Wincomplete-uni-patterns -Wincomplete-record-updates -Wno-unused-top-binds -Wno-orphans -Wno-type-defaults

    hs-source-dirs:   
        task1/lib
    default-language: Haskell2010

executable task1
    main-is:          Main.hs
    ghc-options: -Wall -Werror -Wcompat -Widentities -Wincomplete-uni-patterns -Wincomplete-record-updates -Wno-unused-top-binds -Wno-orphans -Wno-type-defaults

    build-depends:    
        base ^>=4.14.3.0
    hs-source-dirs:   task1
    default-language: Haskell2010

当我执行以下操作时:
cabal repl
...
GHCi, version 8.10.7: https://www.haskell.org/ghc/  :? for help
[1 of 2] Compiling Interpreter      ( task1/lib/Interpreter.hs, interpreted )
[2 of 2] Compiling Parser           ( task1/lib/Parser.hs, interpreted )
Ok, two modules loaded.
> :l Parser

我收到了以下错误信息:
<no location info>: error: [-Wmissing-home-modules, -Werror=missing-home-modules]
    These modules are needed for compilation but not listed in your .cabal file's other-modules: 
        Interpreter

问题

当我想要加载一个单独的文件来尝试其中模块的函数时,该怎么做?

当我尝试运行cabal repl task1-lib命令时,出现了同样的情况。

编辑:

有一种可能的解决方法是通过在-Werror之后添加-Wwarn=missing-home-modules的方式修改cabal。这仍然将-Werror应用于所有警告,而不是将missing-home-modules错误保留为警告,并使得可以在repl中使用:load加载模块。

library task1-lib
    exposed-modules:
        Interpreter,
        Parser
    build-depends: 
        base ^>=4.14.3.0,
        containers,
        mtl,
        parsec
    ghc-options: -Wall -Werror -Wwarn=missing-home-modules -Wcompat -Widentities -Wincomplete-uni-patterns -Wincomplete-record-updates -Wno-unused-top-binds -Wno-orphans -Wno-type-defaults

    hs-source-dirs:   
        task1/lib
    default-language: Haskell2010

executable task1
    main-is:          Main.hs
    ghc-options: -Wall -Werror -Wcompat -Widentities -Wincomplete-uni-patterns -Wincomplete-record-updates -Wno-unused-top-binds -Wno-orphans -Wno-type-defaults

    build-depends:    
        base ^>=4.14.3.0
    hs-source-dirs:   task1
    default-language: Haskell2010

没错,那个可以运行,这种情况下没有错误。然而,那个导入只能启用模块导出的函数,所以我无法测试未导出的函数。当你 :load 一个模块时,你可以调用甚至未导出的函数。 - xbalaj
嗯...错误提示说问题在于Interpreter没有列在other-modules部分中 - 你尝试过添加它吗?如果那也不起作用,那我就不知道问题出在哪里了。你可以暂时移除导出声明,让所有东西都被导出。 - Vikstapolis
是的,也许我可以在“other-modules”中重复这些模块,但我在我查看的github存储库中都没有找到这样的配置。所以我想知道这里的正确解决方案是什么。也许其他人会有一些见解。 - xbalaj
1
我自己尝试了一下,实际上它产生的是一个警告而不是错误——你已经打开了-Werror标志,这将把它提升为错误。因此,您可以删除该标志。另外,我发现这个——警告似乎是一个可以忽略的错误。 - Vikstapolis
1
是的,你说得对,我也遇到了这个问题。但是我想启用-Werror。不过我在这个仓库https://github.com/jyp/dante/issues/60中找到了这个问题,并且有一个解决方法可以使用`-Wwarn=missing-home-modules`,这样只会将`missing-home-modules`作为警告,其他警告仍然是错误。所以我认为这是正确的可能解决方法。 - xbalaj
显示剩余2条评论
1个回答

2

import 仅使模块导出的函数可用

尝试使用 :module + *NameOfTheModule,而不是使用 :loadimport

根据:module的文档:

:module 支持模块上的 * 修饰符,它打开了一个模块的完整顶层作用域,而不仅仅是其导出内容。

这不能通过普通的 import 声明实现,它们的行为与常规 Haskell 程序中的 import 类似。


哦,我忘了感谢你。它有效了,谢谢 :) - xbalaj
我发现在 .ghci 中 :module 命令无法工作 :( - luochen1990
@luochen1990 或许 ghci 不知道这个包。如果你在一个 cabal 项目中,尝试使用 cabal repl 而不是 ghci。 - danidiaz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接