你好 Haskell 社区,
我是 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 标准 ( http://www.haskell.org/onlinereport/haskell2010/haskellch5.html ) 中查找了它,但找不到错误。所以我的问题是
1)为什么我会收到这个错误?
2) 构建分层模块是否像这样的好习惯? (假设相当大的程序)
提前谢谢了!
朱尔斯
最佳答案
编辑:2016 年 9 月
由于我最初回答了这个问题,因此定义 Foo.Internal
的做法越来越多。仍然暴露的模块。在下面的原始答案中,我建议使用 other-modules
field 。现在流行的一种做法是定义 Foo.Internal.*
公开但明确不属于受支持 API 的模块。对 this question 的回答中解释了这种模式的合理性。 .
如评论中所述,您的 .cabal
文件缺少 other-modules
线。我认为 cabal install
然后只安装 FoodMod1
因为这就是它被告知的全部内容。
这是创建内部模块的好方法,例如,您不想在包 API 中公开的整个 cabal 包中使用的类型。由于other-modules
模块不能从包外部导入,它允许您创建包私有(private)功能。
关于haskell - 在 ghci 中加载已编译模块时出现“加载接口(interface)失败”错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20446802/