我使用 Haskell 和功能图形库来表示图形。有两种方法可以直接比较图,通过函数equal或通过另一个函数,我写的是isIsomorph。我想使用哈希图来收集图表。为此,我必须为我的图表创建类Eq的实例。但是我需要两个哈希图,第一个用于由函数equal比较的图,第二个用于由函数isIsomorph比较的图。
如果我做
type Fragment = Gr Atom Bond {-- Gr is a type constructor from the Functional Graph Library}
instance Eq (Gr Atom Bond) where
g == g1 = equal g g1
instance Eq Fragment where
g == g1 = isIsomorph g g1
我有一个预期的错误
Duplicate instance declarations:
instance [overlap ok] Eq (Gr Atom Bond) -- Defined at HLab.hs:45:10
instance [overlap ok] Eq Fragment -- Defined at HLab.hs:48:10
因为类型decalration 只是换行。
我可以用另一种方式
data Fragment = Fragment {fgraph :: Gr Atom Bond}
instance Eq (Gr Atom Bond) where
g == g1 = equal g g1
instance Eq Fragment where
Fragment g == Fragment g1 = isIsomorph g g1
这是正确的,但我使用了“重”类型的构造函数数据,这种方式也很不方便,我必须通过附加函数 fgraph 从片段中获取图形。
是否有任何“美丽”和“真实”的方式将这种类型划分为代码的各个部分?