我一直在教我自己关于类型级编程,并想写一个简单的自然数加法类型的函数。我这工作的第一个版本如下:玩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))
预期其中一期工程。于是我决定通过修改Z
和S
类型尝试的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已经失败了我。解决方案是否存在或我是否遇到了一些语言限制?
使用'得到
Plus One Two
类型:太好了!加上一个两'在ghci。 – Satvik我认为在'undefined :: SomeType'中,'SomeType'只能是'*'种类。 – Satvik
如果你真的想在运行时携带一个类型见证,你可以使用标准['代理'技巧](http://hackage.haskell.org/packages/archive/tagged/0.4.4/doc/html/数据Proxy.html)。 –