当Haskell类型包含以下内容时,它的含义是什么:" ...与推断的签名一样通用......"?

时间:2017-11-10 23:41:02

标签: haskell

编辑: @luqui正在使用GHCi中更简单的可重现案例:

Prelude> let (a, b) = ('x', return 'y')

<interactive>:1:5: error:
    * Ambiguous type variable `m0'
      prevents the constraint `(Monad m0)' from being solved.
    * When checking that the inferred type
        a :: forall (m :: * -> *). Monad m => Char
      is as general as its inferred signature
        a :: Char

但是,以下所有变种如何运作?

Prelude> let c = ('x', return 'y')
Prelude> :t c
c :: Monad m => (Char, m Char)
Prelude> let d = 'x'; e = return 'y'
Prelude> :t d
d :: Char
Prelude> :t e
e :: Monad m => m Char
Prelude> :t (d, e)
(d, e) :: Monad m => (Char, m Char)
Prelude>

所有这些都不一样吗?

原始问题:

这是一个具体的例子:

• Ambiguous type variable ‘m0’
  prevents the constraint ‘(Monad m0)’ from being solved.

• When checking that the inferred type
    logger :: forall b t (m :: * -> *).
              Monad m =>
              Data.Text.Internal.Text -> IO ()
  is as general as its inferred signature
    logger :: Data.Text.Internal.Text -> IO ()

here是一些背景信息和一些在上述特定情况下重现错误的说明。

但即使在一般情况下,我也很好奇:在什么情况下会出现这种错误,以及它试图传达什么?

1 个答案:

答案 0 :(得分:2)

我认为问题在于你正在使用模式绑定。

(a, b) = ('x', return 'y')

基本上就是这个

ab = ('x', return 'y')
a = fst ab
b = snd ab

ab的类型是

ab :: Monad m => (Char, m Char)

所以a的类型为

a :: Monad m => Char

什么是m?它似乎不在=>的右侧,所以它不明确。