为什么填充类型的孔会导致GHC编译错误?

时间:2014-08-20 01:51:38

标签: haskell ghc

下面的代码编译得很好:

ecbEncryptRandomly :: RandomGen g => ByteString -> g -> (ByteString, g)
ecbEncryptRandomly bs gen = let key :: AES
                                (key, newGen) = random gen
                            in  (ecbEncrypt key bs, newGen)

现在,我喜欢漂亮的东西,所以我想我只是为let中的整个元组提供了一个类型注释:

ecbEncryptRandomly :: RandomGen g => ByteString -> g -> (ByteString, g)
ecbEncryptRandomly bs gen = let (key, newGen) = random gen :: (AES, g)
                            in  (ecbEncrypt key bs, newGen)

GHC不喜欢这一点,并吐出以下错误:

ECBCBCoracle.hs:32:56:
Could not deduce (g ~ g1)
from the context (RandomGen g)
  bound by the type signature for
             ecbEncryptRandomly :: RandomGen g =>
                                   ByteString -> g -> (ByteString, g)
  at ECBCBCoracle.hs:31:23-71
  ‘g’ is a rigid type variable bound by
      the type signature for
        ecbEncryptRandomly :: RandomGen g =>
                              ByteString -> g -> (ByteString, g)
      at ECBCBCoracle.hs:31:23
  ‘g1’ is a rigid type variable bound by
       an expression type signature: (AES, g1) at ECBCBCoracle.hs:32:49
Relevant bindings include
  gen :: g (bound at ECBCBCoracle.hs:32:23)
  ecbEncryptRandomly :: ByteString -> g -> (ByteString, g)
    (bound at ECBCBCoracle.hs:32:1)
In the first argument of ‘random’, namely ‘gen’
In the expression: random gen :: (AES, g)

由于某种原因,GHC似乎想让元组中的g成为一种新类型?最令人困惑的是,当我使用打字的洞时,它表明我采用g作为我的类型!

换句话说,以下代码:

ecbEncryptRandomly :: RandomGen g => ByteString -> g -> (ByteString, g)
ecbEncryptRandomly bs gen = let key :: AES
                                (key, _) = random gen
                            in  (ecbEncrypt key bs, _)

生成以下错误:

ECBCBCoracle.hs:34:53:
Found hole ‘_’ with type: g
Where: ‘g’ is a rigid type variable bound by
           the type signature for
             ecbEncryptRandomly :: RandomGen g =>
                                   ByteString -> g -> (ByteString, g)
           at ECBCBCoracle.hs:31:23
Relevant bindings include
  key :: AES (bound at ECBCBCoracle.hs:33:34)
  gen :: g (bound at ECBCBCoracle.hs:32:23)
  bs :: ByteString (bound at ECBCBCoracle.hs:32:20)
  ecbEncryptRandomly :: ByteString -> g -> (ByteString, g)
    (bound at ECBCBCoracle.hs:32:1)
In the expression: _
In the expression: (ecbEncrypt key bs, _)
In the expression:
  let
    key :: AES
    (key, _) = random gen
  in (ecbEncrypt key bs, _)

发生了什么事?

1 个答案:

答案 0 :(得分:5)

我认为您需要ScopedTypeVariables扩展,以使g值实际上相同g而不是分开。