使用Cabal安装模块后,未解析Typeclass实例

时间:2012-07-22 18:16:47

标签: haskell ghc typeclass cabal

我创建了一个简单的项目来演示这个问题: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

1 个答案:

答案 0 :(得分: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包。