1

可能重复:
不完整的类型签名

考虑以下:

import Network.HTTP.Conduit  

(parseUrl "http://stackoverflow.com") :: Maybe a

parseUrl返回Failure HttpException m => m (Request m')

它的文档说:

由于此函数使用Failure,因此返回单子可以是任何 的实例Failure,例如IOor Maybe

但是,当我尝试强制parseUrl使用Maybe时,出现以下错误:

main.hs:9:11:
    Couldn't match type `a' with `Request m'0'
      `a' is a rigid type variable bound by
          an expression type signature: Maybe a at main.hs:9:10
    Expected type: Maybe a
      Actual type: Maybe (Request m'0)

无论如何强制类型Maybe而不指定完整的确切类型?包括 GHC 扩展在内的答案都很好。

请注意,这有效:

f :: Maybe a -> Maybe a
f x = x

f (parseUrl "http://stackoverflow.com")

但这对我来说似乎很丑。

4

1 回答 1

1

你可以使用asTypeOf

main = do
    print (parseUrl "http://stackoverflow.com" `asTypeOf` Nothing)

迫使 monad 成为Maybe. 并不是说那获得了太多

main = do
    print (parseUrl "http://stackoverflow.com" :: Maybe (Request m))
于 2012-07-31T06:18:38.860 回答