Question

I'm adapting this example, in particular, the client. I'll tel you what I think the trouble is, following the code and the error it generates.

> {-# 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 () }

Here's the problem, the error says there is no instance for Control.Monad.Trans.Class.MonadTrans ResourceT

But I think there is, due to this documentation. So where have things gone wrong?

As mentioned below there is something janke going on with Control.Monad.Trans.Resource

Here's the results of the ResourceT introspection.

*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'

version of resourcet is

[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

Any ideas on how to proceed? Where's the instance for MonadTrans ResourceT?

Was it helpful?

Solution

EDIT: This answer does not solve the problem, the import does still not resolve the instances, even though it should. This seems to be a bug in either GHC or some other system.

The Data.Conduit module only re-exports ResourceT as a convenience; the resource monad transformer is defined in a separate package resourcet. conduit does for that reason not re-export class instances (apparently?). You need to manually import Control.Monad.Trans.Resource to gain access to the associated class instances. This can of course be done with the following syntax:

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

OTHER TIPS

If I were a betting man, I would say you have multiple versions of some library installed. For example, say you have version 0.2 and 0.3 of transformers, and resourcet is built against version 0.2. When you write the code import Control.Monad.Trans.Class (lift) you're importing version 0.3 of lift, but there are no instances for it involved.

Easiest way to test this is to cabalize your code. Cabal will make sure you have the right versions of the libraries involved.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top