问题描述:

When I enter the following in Haskell:

{-# LANGUAGE FlexibleInstances, OverlappingInstances, UndecidableInstances #-}

class Class a

instance Class a

instance Eq a => Class a

I get this error when I load it up into GHCi:

Test.hs:5:10:

Duplicate instance declarations:

instance [overlap ok] Class a -- Defined at Test.hs:5:10

instance [overlap ok] Eq a => Class a -- Defined at Test.hs:6:10

Failed, modules loaded: none.

I sort of understand why it is failing, seeing that both are instances for a and neither is more specific, assuming that constraints are ignored. But why is it saying that overlap is ok?

Also, is it possible to add a language extension to allow these kinds of overlap to work and would that be useful?

And finally, is what I intend possible in the current GHC in another way (what I intend is to use one piece of code for instances of Eq and another for the rest)?

网友答案:

"But why is it saying that overlap is ok?"

Because you told it so with the language pragma.

Note that these instances are not merely overlapping, they are identical, as far as GHC's instance resolution is concerned. GHC knows no way at all to ever distinguish these instances, hence it rejects them. Not even IncoherentInstances helps with GHC yet. It will help in GHC 7.8, as Joachim Breitner just notified me.

And finally, is what I intend possible in the current GHC in another way (what I intend is to use one piece of code for instances of Eq and another for the rest)?

I would not be surprised if the new costraint kinds extensions would make it possible, but I've never worked with them, so I don't know how they work and what they can do. But your code would not look nearly as simple, if it works at all.

相关阅读:
Top