我在实现代表嵌套数组中值结构的IsList
实例时遇到了麻烦。GADT
这是完整的代码:
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TypeFamilies #-}
import GHC.Exts (IsList (..))
data ValType = TInt | TList
data Val (t :: ValType) where
I :: Int -> Val 'TInt
L :: [Val a] -> Val 'TList
instance Show (Val t) where
show (I i) = "I " ++ show i
show (L a) = show a
instance IsList (Val 'TList) where
type Item (Val 'TList) = forall a . Val a
fromList = L
toList = error "Not implemented!"
我看到这样的错误:
GADT.hs:20:10: error:
• Illegal polymorphic type: forall (a :: ValType). Val a
• In the type instance declaration for ‘Item’
In the instance declaration for ‘IsList (Val 'TList)’
|
20 | type Item (Val 'TList) = forall a . Val a
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
我部分理解为什么我有这个错误。但我想知道是否可以IsList
为类型实现实例Val
?