ResourceT,MonadTrans实例不可用

时间:2015-11-04 20:27:58

标签: haskell

使用 ResourceT 1.1.6 运行 Linux Mint 17.2 Rafaela GHC 7.8.4 ,我试图写一个简单的&#34 ; Hello World":

import Control.Monad.Trans.Resource
import Control.Monad.Trans.Class

main = runResourceT $ lift (putStrLn "Hello, World!")

但:

No instance for (Control.Monad.Trans.Class.MonadTrans
                   Control.Monad.Trans.Resource.Internal.ResourceT)
  arising from a use of ‘Control.Monad.Trans.Class.lift’
In the second argument of ‘(GHC.Base.$)’, namely
  ‘Control.Monad.Trans.Class.lift
     (System.IO.putStrLn "Hello, World!")’
In the expression:
  Control.Monad.Trans.Resource.runResourceT
  GHC.Base.$
    Control.Monad.Trans.Class.lift (System.IO.putStrLn "Hello, World!")
In an equation for ‘Main.main’:
    Main.main
      = Control.Monad.Trans.Resource.runResourceT
        GHC.Base.$
          Control.Monad.Trans.Class.lift (System.IO.putStrLn "Hello, World!")

如错误消息所示,MonadTrans ResourceT事物未定义但在Control.Monad.Trans.Resource模块中明确定义并且已导入!

更奇怪的是检查ghci中的模块:

Prelude Control.Monad.Trans.Resource Control.Monad.Trans.Class> :i ResourceT 
type role ResourceT representational nominal
newtype ResourceT (m :: * -> *) a
  = Control.Monad.Trans.Resource.Internal.ResourceT {Control.Monad.Trans.Resource.Internal.unResourceT :: GHC.IORef.IORef
                                                                                                            Control.Monad.Trans.Resource.Internal.ReleaseMap
                                                                                                          -> m a}
    -- Defined in ‘Control.Monad.Trans.Resource.Internal’
instance [safe] Monad m => Monad (ResourceT m)
  -- Defined in ‘Control.Monad.Trans.Resource.Internal’
instance [safe] Functor m => Functor (ResourceT m)
  -- Defined in ‘Control.Monad.Trans.Resource.Internal’
instance [safe] (MonadThrow m, Control.Monad.Base.MonadBase IO m,
                 transformers-0.3.0.0:Control.Monad.IO.Class.MonadIO m,
                 Control.Applicative.Applicative m) =>
                MonadResource (ResourceT m)
  -- Defined in ‘Control.Monad.Trans.Resource.Internal’
instance [safe] MonadBaseControl b m =>
                MonadBaseControl b (ResourceT m)
  -- Defined in ‘Control.Monad.Trans.Resource.Internal’
instance [safe] MonadThrow m => MonadThrow (ResourceT m)
  -- Defined in ‘Control.Monad.Trans.Resource.Internal’

并且因为它表明即使在那里也没有MonadTrans ResourceT的东西。

1 个答案:

答案 0 :(得分:4)

您很可能安装了两个变形金刚包(您可以使用ghc-pkg list transformers进行测试)。我写了一篇博文,详细说明了这一点:

http://www.yesodweb.com/blog/2014/09/woes-multiple-package-versions

我的建议:使用Stack安装您的软件包,然后使用stack runghc yourfile.hs运行您的文件,这将确保一套一致的软件包。