4

我一直在教我自己关于类型级编程,并想写一个简单的自然数加法类型的函数。我这工作的第一个版本如下:玩DataKinds - 种类不匹配错误

data Z 
data S n 

type One = S Z 
type Two = S (S Z) 

type family Plus m n :: * 
type instance Plus Z n = n 
type instance Plus (S m) n = S (Plus m n) 

所以在GHCI我可以这样做:

ghci> :t undefined :: Plus One Two 
undefined :: Plus One Two :: S * (S * (S * Z)) 

预期其中一期工程。于是我决定通过修改ZS类型尝试的DataKinds扩展:

data Nat = Z | S Nat 

而且Plus系列现在返回Nat类型:

type family Plus m n :: Nat 

修改后的代码编译,但问题是我现在在测试时出现错误:

Kind mis-match 
Expected kind `OpenKind', but `Plus One Two' has kind `Nat' 
In an expression type signature: Plus One Two 
In the expression: undefined :: Plus One Two 

我已经搜索了解决方案,但Google已经失败了我。解决方案是否存在或我是否遇到了一些语言限制?

+1

使用'得到Plus One Two类型:太好了!加上一个两'在ghci。 – Satvik

+0

我认为在'undefined :: SomeType'中,'SomeType'只能是'*'种类。 – Satvik

+0

如果你真的想在运行时携带一个类型见证,你可以使用标准['代理'技巧](http://hackage.haskell.org/packages/archive/tagged/0.4.4/doc/html/数据Proxy.html)。 –

回答

7

我觉得你测试的方式是不正确的。 undefined可以是任何类型的*(我在这里可能是错的)。

在ghci中

ghci>:t (undefined :: 'Z) 

<interactive>:1:15: 
    Kind mis-match 
    Expected kind `OpenKind', but `Z' has kind `Nat' 
    In an expression type signature: Z 
    In the expression: (undefined :: Z) 

试试这个你仍然可以通过在ghci中使用:kind!

ghci>:kind! Plus One Two 
Plus One Two :: Nat 
= S (S (S 'Z)) 
+0

谢谢!这样可行。当然,Nat会成为一个子类(是这样的吗?)*? –

+0

我不认为有什么像subkind,但我不是专家。 – Satvik

+0

@TomSavage:不,''*比看起来更具体:那些只是[_lifted types_](http://www.haskell.org/ghc/docs/7.4.2/html/users_guide/kind-polymorphism -and-promotion.html)。 – leftaroundabout