用ResourceT解决问题

时间:2012-05-23 23:31:13

标签: haskell monads monad-transformers conduit

我正在调整此example,特别是客户端。我会告诉你我认为麻烦是什么,遵循代码及其产生的错误。

> {-# 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的实例

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

如下所述,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的实例在哪里?

2 个答案:

答案 0 :(得分:4)

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

最简单的方法是测试代码。 Cabal将确保您拥有合适的库版本。

答案 1 :(得分:2)

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

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

import Control.Monad.Trans.Resource () -- Only import instances