[Haskell-beginners] Builds with cabal, but not with GHC

Daniel Fischer daniel.is.fischer at googlemail.com
Wed Sep 28 17:37:54 CEST 2011


On Wednesday 28 September 2011, 17:03:33, Amy de Buitléir wrote:
> I'm getting some weird errors when I try to compile this with GHC, or
> load it in GHCi, but it compiles (and runs) just fine using cabal.
> Here's the file...
> 
<snip>
> 
> When I compile it with ghc, I get the following:
> 
> $ ghc -hide-package monads-fd Test.hs
> [1 of 1] Compiling Main             ( Test.hs, Test.o )
> 
> Test.hs:20:9:
>     No instance for (mtl-1.1.1.1:Control.Monad.Error.Class.MonadError
>                        CPError (ErrorT CPError IO))

That's a hint.
The error message mentions the specific version of the package in which the 
class is defined.
That usually means you are/the compiler is trying to build using 
incompatible packages. In this case, it looks as though the used ConfigFile 
was built against something other than mtl-1.1.1.1, maybe mtl-2.*

Have you different versions of ConfigFile installed?


> 
> I tried adding the instance declarations, but I don't think I did it
> right because I then had to add a bunch of imports, and the problems
> just snowballed.

If the used packages are incompatible, the given 'probable fix' will not 
work, the problem lies deeper. The missing instance is just the place where 
ghc notices that it won't work, without figuring out the exact cause.

> 
> With this cabal file, I can do "cabal install", and the program compiles
> and runs just fine.
> 
> **********
> FILE: Creatur.cabal
> **********
> 
> Name:               Creatur
> Version:             2.0
> Description:       Créatúr
> License:             OtherLicense
> License-file:       LICENSE
> Author:              Amy de Buitléir
> Maintainer:        amy at nualeargais.ie
> Build-Type:          Simple
> Cabal-Version:    >=1.2
> 
> Executable amy-test
>   Main-Is:         Test.hs
>   GHC-Options:     -Wall -Werror
>   Build-Depends:     base >= 4 && < 5, mtl ==1.1.*, ConfigFile ==1.0.*
> **********

Yes, Cabal sees the dependencies and chooses a compatible set of versions 
(if possible, otherwise it fill fail and tell you why).
GHC only sees which packages are needed when following the imports during 
compilation, so it doesn't create a consistent install plan but just 
chooses the latest available version of each package and hopes for the 
best.

> 
> Can anyone tell me how to modify the code so it will compile? Thank you
> in advance.

You don't need to change the source, just the command line. You have to 
tell GHC explicitly which packages to use.

$ cabal install --dry-run -v3

in the package directory will give you a lot of output, you're interested 
in the "selecting xyz-0.1.2" bits.

Then

$ ghc -hide-all-packages -package base -package mtl-1.1.1.1 -package 
ConfigFile-1.0.? Test.hs

should work.




More information about the Beginners mailing list