I have a question similar to a previous one, but where the superclass is Eq
. For example, suppose, I have the following:
{-# LANGUAGE DefaultSignatures #-}
class (Eq a) => Foo a where
size :: a -> Int
(==) :: a -> a -> Bool
(==) s t = (size s) == (size t)
(Note, that I've included the language extension as suggested in the solution to the aforementioned question)
I receive the following ghci error message:
Ambiguous occurrence ‘==’ It could refer to either ‘Main.==’, defined at permutations.lhs:162:3 or ‘Prelude.==’, imported from ‘Prelude’ at permutations.lhs:1:1 (and originally defined in ‘GHC.Classes’)
Am I trying to do something impossible in Haskell? I know that I could instead do something like
class (Eq a) => Foo a where
size :: a -> Int
data Bar = Qux [Int]
instance Foo Bar where
size (Qux xs) = length xs
instance Eq Bar where
(==) f g = (size f) == (size g)
but I'd then have to copy the definition of (==)
for every instance of Foo
, rather than making it a default definition.
I also realise that had I used by own superclass instead of Eq
, I could have written
class Bam a where
eqs :: a -> a -> Bool
default eqs :: Roo a => a -> a -> Bool
eqs f g = (size f) == (size g)
class (Bam a) => Roo a where
size :: a -> Int
The issue I have is that the superclass is Eq
, and that I don't want to repeat the same definition in each instance.