I can not really understand why such line of code actually work :
From Network.HTTP.Simple
{-# LANGUAGE OverloadedStrings #-}
import Network.HTTP.Simple
import qualified Data.ByteString.Char8 as B8
main :: IO ()
main = httpBS "http://example.com" >>= B8.putStrLn . getResponseBody
I am able to rewrite the stuff with do notation :
test = do
request <- return "http://example.com"
result <- httpBS request
let body = getResponseBody result
B8.putStrLn body
This works, even If I cannot figure out what is the type of return "http://example.com"
.
Q1 : How the compiler manage to find which Monad I want to use ?
My guess is : it is coming up from the fact that return of the do block is an IO() and so it would be an IO (Request) ?
Now, When trying to use httpBS in more complex code, I have some difficulties
test.hs file :
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE OverloadedStrings #-}
import Network.HTTP.Simple
import qualified Data.ByteString.Char8 as B8
request = parseRequest "http://example.com"
this gives the error :
Prelude> :load test.hs
[1 of 1] Compiling Main ( test.hs, interpreted )
test.hs:8:11: error:
* Ambiguous type variable `m0' arising from a use of `parseRequest'
prevents the constraint `(Control.Monad.Catch.MonadThrow
m0)' from being solved.
Relevant bindings include
request :: m0 Request (bound at test.hs:8:1)
Probable fix: use a type annotation to specify what `m0' should be.
These potential instances exist:
instance e ~ GHC.Exception.SomeException =>
Control.Monad.Catch.MonadThrow (Either e)
-- Defined in `Control.Monad.Catch'
instance Control.Monad.Catch.MonadThrow IO
-- Defined in `Control.Monad.Catch'
instance Control.Monad.Catch.MonadThrow Maybe
-- Defined in `Control.Monad.Catch'
...plus one other
...plus 15 instances involving out-of-scope types
(use -fprint-potential-instances to see them all)
* In the expression: parseRequest "http://example.com"
In an equation for `request':
request = parseRequest "http://example.com"
|
8 | request = parseRequest "http://example.com"
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Failed, no modules loaded.
Ok. Entering same stuff in the interpreter works :
*Main Network.HTTP.Simple> import Network.HTTP.Simple
*Main Network.HTTP.Simple> req = parseRequest "http://example.com"
*Main Network.HTTP.Simple> :t req
req :: Control.Monad.Catch.MonadThrow m => m Request
*Main Network.HTTP.Simple> req
Request {
host = "example.com"
port = 80
secure = False
requestHeaders = []
path = "/"
queryString = ""
method = "GET"
proxy = Nothing
rawBody = False
redirectCount = 10
responseTimeout = ResponseTimeoutDefault
requestVersion = HTTP/1.1
}
It looks like the dreaded monomorphic restriction stuff that I already stumble accross c.f this question
So I understand I have to give the type.It's ok but then I can not figure out how to use it with the >>= notation , I can only manage to have it working with the do notation :
-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE OverloadedStrings #-}
import Network.HTTP.Simple
import qualified Data.ByteString.Char8 as B8
url = "http://example.com"
maybeRequest :: Maybe Request
maybeRequest = parseRequest url
ioRequest :: IO Request
ioRequest = parseRequest url
--no. wrong type ioRequest.
--testKO = httpBS ioRequest >>= B8.putStrLn . getResponseBody
--How to have it working with a one-liner and the >>= notation ?
--do notation ok
test = do
request <- ioRequest
response <- httpBS request
let body = getResponseBody response
B8.putStrLn body
Q2 : how to use the Request and httpBS with the (>>=) operator if the Request is to be build previously ?