1

我正在调整这个例子,特别是客户端。我会告诉你我认为问题是什么,遵循代码和它产生的错误。

> {-# LANGUAGE OverloadedStrings #-}
> import Network.HTTP.Conduit
> ( http, parseUrl, newManager,def, withManager, RequestBody (RequestBodyLBS)
> , requestBody, method, Response (..)
> )
> import Data.Aeson (Value (Object, String))
> import Data.Aeson.Parser (json)
> import Data.Conduit
> import Data.Conduit.Attoparsec (sinkParser)
> import Control.Monad.IO.Class (liftIO)
> import Control.Monad.Trans.Class (lift)
> import Data.Aeson (encode, (.=), object)


> main :: IO ()
> main = withManager $ \manager -> do
>    value <- makeValue
> -- We need to know the size of the request body, so we convert to a
> -- ByteString
>    let valueBS = encode value
>    req' <- parseUrl "http://10.64.16.6:3000/"
>    let req = req' { method = "POST", requestBody = RequestBodyLBS valueBS }
>    Response status version headers body <- http req manager
>    resValue <- body $$ sinkParser json
>    handleResponse resValue

> -- Application-specific function to make the request value
> makeValue :: ResourceT IO Value
> makeValue = return $ object
>    [ ("foo" .= ("bar" :: String))
>    ]

> -- Application-specific function to handle the response from the server
> handleResponse :: Value -> ResourceT IO ()
> handleResponse foo = do  
>    _ <- lift (print foo)
>    return ()

No instance for (Control.Monad.Trans.Class.MonadTrans ResourceT)
  arising from a use of `lift'
Possible fix:
  add an instance declaration for
  (Control.Monad.Trans.Class.MonadTrans ResourceT)
In a stmt of a 'do' block: _ <- lift (print foo)
In the expression:
  do { _ <- lift (print foo);
       return () }
In an equation for `handleResponse':
    handleResponse foo
      = do { _ <- lift (print foo);
             return () }

这是问题所在,错误说没有 Control.Monad.Trans.Class.MonadTrans ResourceT 的实例

但我认为有,由于这个文档。那么哪里出了问题?

如下所述,有一些 janke 正在发生Control.Monad.Trans.Resource

这是ResourceT自省的结果。

*Main Control.Monad.Trans.Resource> :i ResourceT
newtype ResourceT m a
  = Control.Monad.Trans.Resource.ResourceT (GHC.IORef.IORef
                                              Control.Monad.Trans.Resource.ReleaseMap
                                            -> m a)
    -- Defined in `Control.Monad.Trans.Resource'
instance Monad m => Monad (ResourceT m)
  -- Defined in `Control.Monad.Trans.Resource'
instance Functor m => Functor (ResourceT m)
  -- Defined in `Control.Monad.Trans.Resource'
instance MonadBaseControl b m => MonadBaseControl b (ResourceT m)
  -- Defined in `Control.Monad.Trans.Resource'
instance MonadThrow m => MonadThrow (ResourceT m)
  -- Defined in `Control.Monad.Trans.Resource'

版本resourcet

[mlitchard@Boris Boris]$ ghc-pkg list resourcet
WARNING: there are broken packages.  Run 'ghc-pkg check' for more details.
/usr/lib/ghc-7.4.1/package.conf.d
/home/mlitchard/.ghc/x86_64-linux-7.4.1/package.conf.d
   resourcet-0.3.2.1

关于如何进行的任何想法?MonadTrans ResourceT 的实例在哪里?

4

2 回答 2

4

如果我是一个赌徒,我会说你安装了某个库的多个版本。例如,假设您有 0.2 和 0.3 版本的转换器,并且 resourcet 是针对 0.2 版本构建的。当您编写代码时import Control.Monad.Trans.Class (lift),您正在导入 0.3 版lift,但没有涉及它的实例。

测试这一点的最简单方法是对你的代码进行 cabalize。Cabal 将确保您拥有所涉及库的正确版本。

于 2012-05-24T03:53:40.623 回答
2

编辑:这个答案不能解决问题,导入仍然不能解决实例,即使它应该。这似乎是 GHC 或其他系统中的错误。

Data.Conduit模块只是为了方便而重新导出ResourceT;资源单子转换器在单独的包中定义resourcetconduit因此不会重新导出类实例(显然?)。您需要手动导入Control.Monad.Trans.Resource才能访问关联的类实例。这当然可以使用以下语法来完成:

import Control.Monad.Trans.Resource () -- Only import instances
于 2012-05-23T23:45:14.033 回答