Haskell deducing constraints works in cabal package, but not when importing from package

114 views Asked by At

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?

1

There are 1 answers

0
cronburg On BEST ANSWER

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.