有没有办法自动为 Eq (和显示)派生实例Power
?我设法找到http://www.haskell.org/ghc/docs/7.4.2/html/users_guide/deriving.html但我找不到与下面代码相关的任何解释。
此外,如果对下面创建的效果有更好的实践,我愿意接受建议,因为我是 Haskell 和函数式编程的新手。
{-# LANGUAGE ExistentialQuantification #-}
class Country a
instance Country CountrySet1
data CountrySet1 =
Belgium |
Algeria
deriving (Show)
data Power =
forall a. Country a => Power a |
Netural |
Water
deriving (Eq, Show)
编辑:我知道这是一种 hack,但由于它几乎都是用前奏函数完成的,它应该得到正确的结果,除非存在完全恶意代码(这是“开放世界假设”最常见的情况) .
class Country a where
show :: a -> String
instance Country CountrySet1 where
show a = Prelude.show a
data CountrySet1 =
England |
Turkey
deriving (Show)
data Power =
forall a. Country a => Power a |
Netural |
Water
instance Show Power where
show (Power b) = "Power" ++ Main.show b
show (Netural) = "Netural"
show (Water) = "Water"
instance Eq Power where
(==) a b = Prelude.show a == Prelude.show b