1

I wrote a cabal package with a number of working examples. However, when I copy one of these examples out of the Examples directory and try to run it, I get the error below:

$ cabal sandbox init
$ cabal add-source deckbuild/
$ cabal install deckbuild/
$ cabal repl
GHCi, version 7.8.3: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
λ> :l GreedyInference.hs 
[1 of 1] Compiling Main             ( GreedyInference.hs, interpreted )

GreedyInference.hs:93:27:
      Could not deduce (mtl-2.1.3.1:Control.Monad.State.Class.MonadState
                          Game (StateT Game m))
        arising from a use of ‘runGame’
      from the context (MonadState Game m, MonadIO m)
        bound by the type signature for
                   runGreedy :: (MonadState Game m, MonadIO m) =>
                                (Double, Double) -> m Game
        at GreedyInference.hs:92:14-94
      In the first argument of ‘execStateT’, namely ‘runGame’
      In the expression: execStateT runGame
      In the expression: execStateT runGame $ greedyGame ps

GreedyInference.hs:107:29:
      No instance for (MonadState Game IO)
        arising from a use of ‘runGreedy’
      In the second argument of ‘($)’, namely
        ‘runGreedy (param0, param1)’
      In the expression: unsafePerformIO $ runGreedy (param0, param1)
      In an equation for ‘g’:
          g = unsafePerformIO $ runGreedy (param0, param1)
Failed, modules loaded: none.

Since the type signatures work in the cabal package, I have the feeling I'm missing an import or I need to tweak the type signatures slightly when using the code from outside the package. Any ideas? I'm just looking for a push in the right direction and I should be able to figure out the specifics.

The working (when loaded with cabal repl from inside the package) module can be found here: https://github.com/cronburg/deckbuild/blob/master/Examples/GreedyInference.hs

EDIT: I think it has something to do with Lazy vs Class in Control.Monad.State. The deduce wants Class for some reason, but hackage says Lazy is the default. Which one should I be using and why?

cronburg
  • 892
  • 1
  • 8
  • 24
  • Is `mtl-2.1.3.1` the same version of `mtl` that you used to compile your package containing actual instances for `Game`? It looks like there's some kind of version conflict going on. – Rufflewind Nov 26 '14 at 02:53
  • when I cabal repl inside the package: `Loading package mtl-2.1.3.1 ... linking ... done.` – cronburg Nov 26 '14 at 02:56
  • But what's the version of `mtl` that is actually being used by your `GreedyInference.hs`? (Try using `cabal repl -v`.) – Rufflewind Nov 26 '14 at 03:08
  • Where is `Game` defined? Is it fully inferred, or do you specify it in either of the packages? – jamshidh Nov 26 '14 at 03:12
  • `Game` is a record type deriving `Typeable,Eq,Ord,Show` in a module imported by `GreedyInference.hs`. The `ghc --make ... Examples.GreedyInference` shows `mtl-2.1.3.1` when compiling inside the package. I tried `cabal repl -v` from outside the package, but it didn't say anything about `mtl`. Do I have to explicitly install `mtl` into the sandbox before I can `:l GreedyInference.hs`? There's only one package (`deckbuild`) which I'm trying to use from a separate (external to the package) sandbox. – cronburg Nov 26 '14 at 03:18

1 Answers1

1

I ended up just making a file at Game/Monad.hs containing:

module Game.Monad (execStateT, MonadState, MonadIO) where
import Control.Monad.State

Then replaced my import Control.State.Monad with import Game.Monad in GreedyInference.hs. The file then compiled without the error.

So I think @Rufflewind in the comments was on the right track - the mtl types imported in my package weren't matching the mtl types imported by the standalone GreedyInference.hs. I'm still not sure why though since the versions matched and all the pertinent imports are import Control.Monad.State.

cronburg
  • 892
  • 1
  • 8
  • 24