简体   繁体   English

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

[英]'Failed to load interface' error when loading compiled modules in ghci

Hello Haskell community, 您好Haskell社区,

I'm new to Haskell and ran into a problem when I tried to structure my first bigger project. 当我尝试构建我的第一个更大的项目时,我是Haskell的新手并遇到了一个问题。

Here's the minimal example of the problem (I'm using cabal to build). 这是问题的最小例子(我正在使用cabal来构建)。

This is the directory structure of a simple module: 这是一个简单模块的目录结构:

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

The source for FooMod1.hs: FooMod1.hs的来源:

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

import FooMod1.C1
import FooMod1.T1

The source for C1.hs: C1.hs的来源:

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

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

The source for T1.hs: 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"

The source for the cabal file: 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

and the Setup.hs: 和Setup.hs:

module Main where

import Distribution.Simple

main = defaultMain

I can do 我可以

cabal configure
cabal build
cabal install

without any problem. 没有任何问题。 When I start ghci and 当我开始ghci和

import FooMod1

it loads the module and I can see the data constructors. 它加载模块,我可以看到数据构造函数。 But when I try to get the type of a function for example 但是当我试图获得一个函数的类型时

:t c1FooFun

or construct a value I get: 或者构造一个我得到的值:

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' reveals nothing. 'ghc-pkg check'什么也没透露。

What am I missing? 我错过了什么? I looked it up in the Haskell 2010 Standard ( http://www.haskell.org/onlinereport/haskell2010/haskellch5.html ) and i can't find the error. 我在Haskell 2010标准( http://www.haskell.org/onlinereport/haskell2010/haskellch5.html )中查找了它,我找不到错误。 So my questions are 所以我的问题是

1) Why am I getting this error? 1)为什么我收到此错误?

2) Is structuring a hierarchical modules like that good practice? 2)构建像这样的良好实践的分层模块吗? (assume considerably larger programs) (假设相当大的程序)

Many thanks in advance! 提前谢谢了!

Jules 儒勒

Edit: September 2016 编辑:2016年9月

Since I originally answered this question there is a growing practice of defining Foo.Internal modules that are still exposed. 自从我最初回答这个问题以来,有一种越来越多的定义Foo.Internal做法。 Foo.Internal模块仍然暴露出来。 In the original answer below I suggested using the other-modules field. 在下面的原始答案中,我建议使用other-modules字段。 A practice that is now popular is to define Foo.Internal.* modules that are exposed but explicitly not part of the supported API. 现在流行的做法是定义Foo.Internal.*模块,这些模块已公开但明确不属于受支持的API。 The rational for this pattern is explained in the answers to this question . 这个模式的理性在这个问题的答案中得到了解释。


As noted in the comments your .cabal file is missing the other-modules line. 如评论中所述,您的.cabal文件缺少other-modules行。 I think cabal install then only installs FoodMod1 since that is all it's been told about. 我认为cabal install只安装了FoodMod1因为它已被告知。

This is a nice way to create internal modules with, for instance, types that are used throughout your cabal package which you don't want to expose in the package API. 这是一种创建内部模块的好方法,例如,您不希望在包API中公开的整个cabal包中使用的类型。 Since the other-modules modules cannot be imported from outside your package it allows you to create package private functionality. 由于无法从包外部导入other-modules模块,因此您可以创建包私有功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM