假设我们有以下代码,其中几个类型最终被放置在另外两个类型中,其中最外层是一个 GADT:
{-# LANGUAGE FlexibleInstances,
GADTSyntax,
GADTs,
OverlappingInstances,
StandaloneDeriving #-}
data SomeType1 = SomeType1 deriving Show
data SomeType2 = SomeType2 deriving Show
class SomeClass d where
instance SomeClass SomeType1 where
instance SomeClass SomeType2 where
data WrapperType t where
WrapperType :: (SomeClass t, Show t) => t -> (WrapperType t)
instance Show (WrapperType SomeType1) where
show (WrapperType d) = "correct"
instance Show (WrapperType t) where
show (WrapperType d) = "incorrect"
data ListWrap where
ListWrap :: [(WrapperType d)] -> ListWrap
deriving instance Show ListWrap
现在,写作[WrapperType SomeType1]
给了我想要的东西:
*MyModule> [WrapperType SomeType1]
[correct]
但是一旦我把它放进去,ListWrap
我就选择了错误的Show
实例来显示内容:
*MyModule> ListWrap [WrapperType SomeType1]
ListWrap [incorrect]
一定有一些关于类型类和/或 GADT 的东西我无法理解——它可能是什么?