Tom*_*age 5 haskell ghc type-families type-level-computation data-kinds
我一直在教自己关于类型级编程,并希望编写一个简单的自然数加法类型函数.我的第一个版本如下:
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)
Run Code Online (Sandbox Code Playgroud)
所以在GHCi我能做到:
ghci> :t undefined :: Plus One Two
undefined :: Plus One Two :: S * (S * (S * Z))
Run Code Online (Sandbox Code Playgroud)
哪个按预期工作.然后我决定通过修改Z和S类型来尝试DataKinds扩展:
data Nat = Z | S Nat
Run Code Online (Sandbox Code Playgroud)
Plus系列现在可以返回Nat一种:
type family Plus m n :: Nat
Run Code Online (Sandbox Code Playgroud)
修改后的代码编译但问题是我现在在测试时遇到错误:
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
Run Code Online (Sandbox Code Playgroud)
我搜索过一个解决方案,但谷歌让我失望了.是否存在解决方案或者我是否达到某种语言限制?
我认为你测试的方式不正确.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)
Run Code Online (Sandbox Code Playgroud)
你仍然可以Plus One Two通过:kind!在ghci中使用来获得类型
ghci>:kind! Plus One Two
Plus One Two :: Nat
= S (S (S 'Z))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1166 次 |
| 最近记录: |