1

I created simple project to demonstrate the issue: https://github.com/jdevelop/testcabal

If I compile and install the module with 'cabal install', I can not serialize TestData with Binary:

> 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

If I load the Test.hs into ghci directly - everything works as expected,

> 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"

Version of Haskell compiler:

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

1 Answer 1

2

In your .cabal file, you have

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

Usually, ghc-binary is not exposed, and it is not intended to be used except by GHC itself. When you load Data.Binary into ghci, it loads the module from the binary package, and the Binary class from that package is a different one than the one from ghc-binary, hence TTestData has no instance.

If you load the file from source, ghci doesn't care about the .cabal file and uses the class from binary directly, hence it works.

You should change the dependency to the binary package.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.