Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Haskell中的派生实例_Haskell_Deriving - Fatal编程技术网

Haskell中的派生实例

Haskell中的派生实例,haskell,deriving,Haskell,Deriving,我希望像这样使用派生实例: data Test3D = forall a. (Show a, Eq a, Typeable a, Generic a) => Test3D { testDt :: String , testPrm :: a } deriving (Show, Eq, Typeable, Generic) instance Binar

我希望像这样使用派生实例:

data Test3D = forall a. (Show a, Eq a, Typeable a, Generic a)
                => Test3D { testDt :: String
                          , testPrm :: a
                          }
   deriving (Show, Eq, Typeable, Generic)

instance Binary (Test3D)
$(deriveJSON defaultOptions ''Test3D)
但我从GHC收到:

• Can't make a derived instance of ‘Show Test3D’:
        Constructor ‘Test3D’ has existentials or constraints in its type
        Possible fix: use a standalone deriving declaration instead
• In the data declaration for ‘Test3D’
这种方式对我的项目非常方便。我找不到解决办法

是否有任何方法可以将派生实例用于此类数据

是否有任何方法可以将派生实例用于此类数据

对。按照GHC的建议,制定一个独立的派生条款:

{-# LANGUAGE StandaloneDeriving, ExistentialQuantification #-}

data Test3D = forall a. (Show a)
                => Test3D { testDt :: String
                          , testPrm :: a
                          }

deriving instance Show Test3D

您不能做的是派生一个
Eq
实例,因为不同的值实际上可能包含不同的类型,并且只能通过
Typeable

将它们与动态cast hack进行比较。您将如何编写实例?如果您想显示有关
testPrm
的任何信息,那么即使写出类型签名也会很困难。如果你不知道怎么做,那么GHC就不能自动为你做。另一方面,如果您不关心显示
testPrm
,那么实例很容易手工编写,您不需要派生它。这涉及到为每个
泛型
编写一个
二进制
实例。这看起来是一项相当复杂的任务。我认为,我们不能现实地希望automagic衍生机制能为我们解决这个问题。如果我们使用
二进制a
而不是
通用a
,也许我们可以手动编写一个更简单的实例?也许我没有正确地实现我的想法。我只想有一些类型的数据,这些数据又包含一个未知类型的字段。但是字段类型通过类型类满足一定的要求。所有这些都应该是(显示a、等式a、可键入a、通用等)。