Haskell推导约束适用于cabal包,但不能在从包导入时使用

时间:2014-11-26 02:35:50

标签: haskell cabal type-inference state-monad

我写了一个包含许多工作示例的cabal包。但是,当我从Examples目录中复制其中一个示例并尝试运行它时,我收到以下错误:

$ 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.

由于类型签名在cabal包中有效,我感觉我错过了导入,或者我需要在使用包外部的代码时略微调整类型签名。有任何想法吗?我只是想朝着正确的方向努力,我应该能够弄清楚具体细节。

可以在此处找到工作(当从包内加载cabal repl时)模块:https://github.com/cronburg/deckbuild/blob/master/Examples/GreedyInference.hs

编辑:我认为它与Lazy中的ClassControl.Monad.State有关。由于某些原因,推断需要Class,但是hackage说Lazy是默认值。我应该使用哪一个?为什么?

1 个答案:

答案 0 :(得分:1)

我最后只是在Game/Monad.hs处制作了一个包含以下内容的文件:

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

然后用import Control.State.Monad中的import Game.Monad替换了我的GreedyInference.hs。然后编译该文件而没有错误。

所以我认为评论中的@Rufflewind在正确的轨道上 - 我的包中导入的mtl类型与独立mtl导入的GreedyInference.hs类型不匹配。我仍然不确定为什么,因为版本匹配且所有相关的导入都是import Control.Monad.State