我写了一个包含许多工作示例的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
中的Class
与Control.Monad.State
有关。由于某些原因,推断需要Class
,但是hackage说Lazy
是默认值。我应该使用哪一个?为什么?
答案 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
。