1

我创建了一个简单的项目来演示这个问题:https ://github.com/jdevelop/testcabal

如果我使用“cabal install”编译和安装模块,我无法使用二进制序列化 TestData:

> ghci                                               
GHCi, version 7.0.4: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Loading package ffi-1.0 ... linking ... done.
ghci> :m +TestBinary.Test Data.Binary
ghci> randomData  . decode $ encode emptyTest 

<interactive>:1:24:
    No instance for (Binary TTestData)
      arising from a use of `encode'
    Possible fix: add an instance declaration for (Binary TTestData)
    In the second argument of `($)', namely `encode emptyTest'
    In the expression: randomData . decode $ encode emptyTest
    In an equation for `it':
        it = randomData . decode $ encode emptyTest

如果我直接将 Test.hs 加载到 ghci - 一切都按预期工作,

> ghci TestBinary/Test.hs 
GHCi, version 7.0.4: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Loading package ffi-1.0 ... linking ... done.
[1 of 1] Compiling TestBinary.Test  ( TestBinary/Test.hs, interpreted )
Ok, modules loaded: TestBinary.Test.
ghci> randomData  . decode $ encode emptyTest 
Loading package array-0.3.0.2 ... linking ... done.
Loading package bytestring-0.9.1.10 ... linking ... done.
Loading package containers-0.4.0.0 ... linking ... done.
Loading package binary-0.5.1.0 ... linking ... done.
"123456"

Haskell 编译器版本:

> ghci --version 
The Glorious Glasgow Haskell Compilation System, version 7.0.4
4

1 回答 1

2

在您的 .cabal 文件中,您有

Build-depends: base < 5, ghc-binary >= 0.5, bytestring >= 0.9.1

通常,ghc-binary不暴露,并且不打算由 GHC 本身使用。当您加载Data.Binary到 ghci 时,它会从binary包中加载模块,并且该包中的Binary类与来自 的类不同ghc-binary,因此TTestData没有实例。

如果您从源代码加载文件,ghci 不关心 .cabal 文件并binary直接使用该类,因此它可以工作。

您应该更改对binary包的依赖项。

于 2012-07-22T18:33:21.777 回答