我有这个代码片段,它使用了大量的 GHC 扩展:
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
import GHC.Exts (Constraint)
data HList :: [*] -> * where
Nil :: HList '[]
Cons :: a -> HList l -> HList (a ': l)
type family All (p :: * -> Constraint) (xs :: HList [*]) :: Constraint where
All p Nil = ()
All p (Cons x xs) = (p x, All p xs)
GHC 抱怨说:
‘HList’ of kind ‘[*] -> *’ is not promotable
In the kind ‘HList [*]’
为什么我不能晋升HList
为一种?我使用 GHC7.8.2
和7.11
.
当然,使用内置的'[]
作品就好了:
type family All (p :: * -> Constraint) (xs :: [*]) :: Constraint where
All p '[] = ()
All p (x ': xs) = (p x, All p xs)
我想使用我自己的HList
而不是'[]
因为实际HList
支持附加并且看起来像这样:
type family (:++:) (xs :: [*]) (ys :: [*]) where
'[] :++: ys = ys
xs :++: '[] = xs
(x ': xs) :++: ys = x ': (xs :++: ys)
data HList :: [*] -> * where
Nil :: HList '[]
Cons :: a -> HList l -> HList (a ': l)
App :: Hlist a -> HList b -> HList (a :++: b)
编辑:主要目标是让 GHC 推断
(All p xs, All p ys) ==> All p (xs :++: ys)
这样我就可以写了
data Dict :: Constraint -> * where
Dict :: c => Dict c
witness :: Dict (All p xs) -> Dict (All p ys) -> Dict (All p (xs :++: ys))
witness Dict Dict = Dict
我曾希望为附加类型级别列表添加显式表示可以帮助我实现这一目标。还有其他方法可以说服 GHC 吗?