在 ghci 中加载已编译模块时出现“无法加载接口”错误

2024-02-07

你好哈斯克尔社区,

我是 Haskell 的新手,当我尝试构建我的第一个更大的项目时遇到了问题。

这是问题的最小示例(我正在使用 cabal 来构建)。

这是一个简单模块的目录结构:

FooMod1
|- FooMod1.cabal
|- Setup.hs
|- src
  |- FooMod1.hs
  |- FooMod1
    |- C1.hs
    |- T1.hs

FooMod1.hs 的来源:

module FooMod1 (
    C1(..) ,
    T1(..) ,
) where 

import FooMod1.C1
import FooMod1.T1

C1.hs的来源:

module FooMod1.C1 (
    C1(..)
) where

class C1 a where
    c1FooFun :: a -> IO ()

T1.hs的来源:

module FooMod1.T1 (
    T1(..)
) where

import FooMod1.C1

data T1 = T1 deriving(Show)

instance C1 T1 where
    c1FooFun T1 = putStrLn "c1FooFun from T1"

cabal 文件的来源:

Name:                      FooMod1
Version:                   0.0.1
Cabal-version:             >=1.10
Build-type:                Simple

library 
  build-depends:           base >= 4 && < 5
  if impl(ghc >= 7.0.0)
     default-language:     Haskell2010
  ghc-options:             -Wall
  exposed-modules:         FooMod1

  ghc-options:             -Wall -rtsopts
  hs-source-dirs:          src, src/FooMod1
  default-language:        Haskell2010

和Setup.hs:

module Main where

import Distribution.Simple

main = defaultMain

我可以

cabal configure
cabal build
cabal install

没有任何问题。当我启动 ghci 时

import FooMod1

它加载模块,我可以看到数据构造函数。 但是当我尝试获取函数的类型时

:t c1FooFun

或构造一个我得到的值:

Failed to load interface for `FooMod1.C1'
There are files missing in the `FooMod1-0.0.1' package,
try running 'ghc-pkg check'.
Use -v to see a list of the files searched for.
In the expression: c1FooFun

“ghc-pkg check”没有透露任何内容。

我缺少什么?我在 Haskell 2010 Standard 中查找了它(http://www.haskell.org/onlinereport/haskell2010/haskellch5.html http://www.haskell.org/onlinereport/haskell2010/haskellch5.html),我找不到错误。所以我的问题是

1)为什么我会收到此错误?

2)构建分层模块是一种好的做法吗? (假设相当大的程序)

提前谢谢了!

Jules


编辑:2016 年 9 月

自从我最初回答这个问题以来,越来越多的人定义了Foo.Internal仍然暴露的模块。在下面的原始答案中,我建议使用other-modules场地。现在流行的一种做法是定义Foo.Internal.*公开但明确不属于受支持 API 的模块。答案中解释了这种模式的合理性这个问题 https://stackoverflow.com/q/9190638/166732.


正如评论中所指出的,您的.cabal文件缺少other-modules线。我认为cabal install然后只安装FoodMod1因为这就是它所讲述的全部内容。

这是创建内部模块的好方法,例如使用在整个 cabal 包中使用的类型,而您不想在包 API 中公开这些类型。自从other-modules模块不能从包外部导入,它允许您创建包私有功能。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 ghci 中加载已编译模块时出现“无法加载接口”错误 的相关文章

随机推荐