只是为了好玩,我想创建一个知道它有多长的类型级别列表。像这样的东西:
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeOperators #-}
import GHC.TypeLits
data family TypeList a (n::Nat)
data instance TypeList a (0) = EmptyList
data instance TypeList a (1) = TL1 a (TypeList a (0))
data instance TypeList a (2) = TL2 a (TypeList a (1))
data instance TypeList a (3) = TL3 a (TypeList a (2))
但是,我当然想将其概括为:
data instance TypeList a (n) = TL3 a (TypeList a (n-1))
但这会产生错误:
TypeList.hs:15:53: parse error on input `-'
Failed, modules loaded: none.
另一种尝试:
data instance TypeList a (n+1) = TL3 a (TypeList a (n))
还会产生错误:
Illegal type synonym family application in instance: n + 1
In the data instance declaration for `TypeList'
我认为这样的事情一定是可能的。绝对可以使用以下符号:
data Zero
data Succ a
但我无法用更好看的版本弄清楚。