use*_*763 4 haskell generic-programming ghc
启用时PolyKinds,以前有效的类型签名可能会变为无效.
以下代码编译时没有PolyKinds.
{-# LANGUAGE KindSignatures #-}
import GHC.Generics
foo :: Constructor c => t c (f :: * -> *) a -> [Char]
foo = conName
Run Code Online (Sandbox Code Playgroud)
当我启用PolyKinds它无法编译.
Kind incompatibility when matching types:
t0 :: * -> (* -> *) -> * -> *
t :: * -> (* -> *) -> k -> *
Expected type: t c f a -> [Char]
Actual type: t0 c f a0 -> [Char]
Relevant bindings include
foo :: t c f a -> [Char] (bound at Gen.hs:8:1)
In the expression: conName
In an equation for ‘foo’: foo = conName
Run Code Online (Sandbox Code Playgroud)
有没有办法让一个类型签名foo时PolyKinds被启用?
请注意,差异是
t0 :: * -> (* -> *) -> * -> *
t :: * -> (* -> *) -> k -> *
Run Code Online (Sandbox Code Playgroud)
也就是说,在其中一个签名GHC认为第三个论点t应该有类型*,而在另一个它认为它应该具有多态类型k.
我认为前一个签名(with *)来自conName,而后者k隐含地来自你的签名foo- k因为现在已经PolyKinds启用了,所有你的类型签名在可能的情况下被解释为多态类型.此外,根据设计,签名被认为是为函数提供完整的"API",这意味着当您明确指定签名时,不会尝试从函数的其余部分推断出类型.
在你的签名中,该t类型的第三个参数是a,所以你可以通过添加一个注释来修复它,使其与conName想要的一致:
foo :: Constructor c => t c (f :: * -> *) (a :: *) -> [Char]
foo = conName
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
70 次 |
| 最近记录: |